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

Code before:
    _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
        self._queue_length = asyncio.Semaphore(0)  # default is 1
        self._index_to_peer_map = []
        self._normal_priority_queues = {}
        self._high_priority_queue = SimpleQueue()  # we don't limit the number of high priority transactions
        self.peer_size_limit = peer_size_limit
        self.log = log
        self._max_tx_clvm_cost = max_tx_clvm_cost
        self._cleanup_counter = 0

    def put(self, tx: TransactionQueueEntry, peer_id: bytes32 | None, high_priority: bool = False) -> None:
        if peer_id is None or high_priority:  # when it's local there is no peer_id.
            self._high_priority_queue.put(tx)
        else:
            if peer_id not in self._normal_priority_queues:
                self._normal_priority_queues[peer_id] = NormalPriorityQueue(PriorityQueue(), 0)
                self._index_to_peer_map.append(peer_id)
            if self._normal_priority_queues[peer_id].priority_queue.qsize() < self.peer_size_limit:
                tx_info = tx.peers_with_tx.get(peer_id)
                if tx_info is not None and tx_info.advertised_cost > 0:
                    fpc = tx_info.advertised_fee / tx_info.advertised_cost
                    # PriorityQueue returns lowest first so we invert
                    priority = -fpc
                else:
                    # This peer didn't advertise cost and fee information for
                    # this transaction (it sent a `RespondTransaction` message
                    # instead of a `NewTransaction` one).
                    priority = float("inf")
                self._normal_priority_queues[peer_id].priority_queue.put((priority, tx))
            else:
                self.log.warning(f"Transaction queue full for peer {peer_id}")
                raise TransactionQueueFull(f"Transaction queue full for peer {peer_id}")
        self._queue_length.release()  # increment semaphore to indicate that we have a new item in the queue

    def _cleanup_peer_queues(self) -> None:
        """
        Removes empty peer queues and updates the cursor accordingly.
        """
        new_peer_map = []
        for peer_id in self._index_to_peer_map:
            if self._normal_priority_queues[peer_id].priority_queue.empty():
                self._normal_priority_queues.pop(peer_id, None)
            else:

Review comment (suggestion):
If you add:
```
self._queue_length.release()
return
```
here, you can de-indent the else-block

Code after:
    _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
    # 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
        self._queue_length = asyncio.Semaphore(0)  # default is 1
        self._index_to_peer_map = []
        self._peers_transactions_queues = {}
        self._high_priority_queue = SimpleQueue()  # we don't limit the number of high priority transactions
        self.peer_size_limit = peer_size_limit
        self.log = log
        self._max_tx_clvm_cost = max_tx_clvm_cost
        self._cleanup_counter = 0

    def put(self, tx: TransactionQueueEntry, peer_id: bytes32 | None, high_priority: bool = False) -> None:
        if peer_id is None or high_priority:  # when it's local there is no peer_id.
            self._high_priority_queue.put(tx)
            self._queue_length.release()
            return
        peer_queue = self._peers_transactions_queues.get(peer_id)
        if peer_queue is None:
            peer_queue = PeerTransactionsQueue()
            self._peers_transactions_queues[peer_id] = peer_queue
            self._index_to_peer_map.append(peer_id)
        if self._peers_transactions_queues[peer_id].priority_queue.qsize() >= self.peer_size_limit:
            self.log.warning(f"Transaction queue full for peer {peer_id}")
            raise TransactionQueueFull(f"Transaction queue full for peer {peer_id}")
        tx_info = tx.peers_with_tx.get(peer_id)
        if tx_info is not None and tx_info.advertised_cost > 0:
            fpc = tx_info.advertised_fee / tx_info.advertised_cost
            # PriorityQueue returns lowest first so we invert
            priority = -fpc
        else:
            # This peer didn't advertise cost and fee information for
            # this transaction (it sent a `RespondTransaction` message
            # instead of a `NewTransaction` one).
            priority = float("inf")
        peer_queue.priority_queue.put((priority, tx))
        self._queue_length.release()  # increment semaphore to indicate that we have a new item in the queue

    def _cleanup_peer_queues(self) -> None:
        """