Repository: Chia-Network/chia-blockchain
File: chia/full_node/tx_processing_queue.py (Python)

Code before:
    advertised_cost: uint64


@dataclass(frozen=True, order=True)
class TransactionQueueEntry:
    """
    A transaction received from peer. This is put into a queue, and not yet in the mempool.
    """

    transaction: SpendBundle = field(compare=False)
    transaction_bytes: bytes | None = field(compare=False)
    spend_name: bytes32
    peer: WSChiaConnection | None = field(compare=False)
    test: bool = field(compare=False)
    # IDs of peers that advertised this transaction via new_transaction, along
    # with their hostname, fee and cost.
    peers_with_tx: dict[bytes32, PeerWithTx] = field(default_factory=dict, compare=False)
    done: ValuedEvent[tuple[MempoolInclusionStatus, Err | None]] = field(
        default_factory=ValuedEvent,
        compare=False,
    )


@dataclass
class NormalPriorityQueue:
    priority_queue: PriorityQueue[tuple[float, TransactionQueueEntry]]
    deficit: int


@dataclass
class TransactionQueue:
    """
    This class replaces one queue by using a high priority queue for local transactions and separate queues for peers.
    Local transactions are processed first.
    Then the next transaction is taken from the next non-empty queue after the last processed queue. (round-robin)
    This decreases the effects of one peer spamming your node with transactions.
    """

    _list_cursor: int  # this is which index
    _queue_length: asyncio.Semaphore
    _index_to_peer_map: list[bytes32]
    _normal_priority_queues: dict[bytes32, NormalPriorityQueue]
    _high_priority_queue: SimpleQueue[TransactionQueueEntry]
    peer_size_limit: int
    log: logging.Logger
    _max_tx_clvm_cost: uint64
    # Each 100 pops we do a cleanup of empty peer queues
    _cleanup_counter: int

    def __init__(self, peer_size_limit: int, log: logging.Logger, max_tx_clvm_cost: uint64) -> None:
        self._list_cursor = 0

Review comment (suggestion):
I think this member also warrants a comment. I imagine that `float` is fee per cost, or is it cost per fee? Or maybe negative fee-per-cost.

Code after:
    advertised_cost: uint64


@dataclass(frozen=True, order=True)
class TransactionQueueEntry:
    """
    A transaction received from peer. This is put into a queue, and not yet in the mempool.
    """

    transaction: SpendBundle = field(compare=False)
    transaction_bytes: bytes | None = field(compare=False)
    spend_name: bytes32
    peer: WSChiaConnection | None = field(compare=False)
    test: bool = field(compare=False)
    # IDs of peers that advertised this transaction via new_transaction, along
    # with their hostname, fee and cost.
    peers_with_tx: dict[bytes32, PeerWithTx] = field(default_factory=dict, compare=False)
    done: ValuedEvent[tuple[MempoolInclusionStatus, Err | None]] = field(
        default_factory=ValuedEvent,
        compare=False,
    )


@dataclass
class PeerTransactionsQueue:
    # Peer's priority queue of the form (negative fee per cost, entry).
    # We sort like this because PriorityQueue returns lowest first.
    priority_queue: PriorityQueue[tuple[float, TransactionQueueEntry]] = field(default_factory=PriorityQueue)
    # Peer's deficit in the context of deficit round robin algorithm. The unit
    # here is in CLVM cost.
    deficit: int = field(default=0, init=False)


@dataclass
class TransactionQueue:
    """
    This class replaces one queue by using a high priority queue for local transactions and separate queues for peers.
    Local transactions are processed first.
    Then the next transaction is taken from the next non-empty queue after the last processed queue. (round-robin)
    This decreases the effects of one peer spamming your node with transactions.
    """

    _list_cursor: int  # this is which index
    _queue_length: asyncio.Semaphore
    _index_to_peer_map: list[bytes32]
    _peers_transactions_queues: dict[bytes32, PeerTransactionsQueue]
    _high_priority_queue: SimpleQueue[TransactionQueueEntry]
    peer_size_limit: int
    log: logging.Logger
    # Fallback cost for transactions without cost information
    _max_tx_clvm_cost: uint64