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

Code before:
        if self._normal_priority_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:
        """
        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:
                new_peer_map.append(peer_id)
        self._index_to_peer_map = new_peer_map
        if len(self._index_to_peer_map) > 0:
            self._list_cursor %= len(self._index_to_peer_map)
        else:
            self._list_cursor = 0

    async def pop(self) -> TransactionQueueEntry:
        await self._queue_length.acquire()
        if not self._high_priority_queue.empty():
            return self._high_priority_queue.get()
        while True:
            # Map of peer ID to its top transaction's advertised cost. We want
            # to service transactions fairly between peers, based on cost, so
            # we need to find the lowest cost transaction among the top ones.
            top_txs_advertised_costs: dict[bytes32, uint64] = {}
            # Let's see if a peer can afford to send its top transaction
            num_peers = len(self._index_to_peer_map)
            if num_peers == 0:
                continue
            start = self._list_cursor
            for offset in range(num_peers):
                peer_index = (start + offset) % num_peers
                peer_id = self._index_to_peer_map[peer_index]
                peer_queue = self._normal_priority_queues[peer_id]
                if peer_queue.priority_queue.empty():

Review comment (suggestion):
```suggestion
        for idx, peer_id in enumerate(self._index_to_peer_map):
            if self._normal_priority_queues[peer_id].priority_queue.empty():
                self._normal_priority_queues.pop(peer_id, None)
                if idx <= self._list_cursor:
                    self._list_cursor -= 1
            else:
                new_peer_map.append(peer_id)
```

Code after:
            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:
        """
        Removes empty peer queues and updates the cursor accordingly.
        """
        new_peer_map = []
        for idx, peer_id in enumerate(self._index_to_peer_map):
            if self._peers_transactions_queues[peer_id].priority_queue.empty():
                self._peers_transactions_queues.pop(peer_id, None)
                if idx < self._list_cursor:
                    self._list_cursor -= 1
            else:
                new_peer_map.append(peer_id)
        self._index_to_peer_map = new_peer_map
        if self._list_cursor >= len(self._index_to_peer_map):
            self._list_cursor = 0

    async def pop(self) -> TransactionQueueEntry:
        await self._queue_length.acquire()
        if not self._high_priority_queue.empty():
            return self._high_priority_queue.get()
        while True:
            # Map of peer ID to its top transaction's advertised cost. We want
            # to service transactions fairly between peers, based on cost, so
            # we need to find the lowest cost transaction among the top ones.
            top_txs_advertised_costs: dict[bytes32, uint64] = {}
            # Let's see if a peer can afford to send its top transaction
            num_peers = len(self._index_to_peer_map)
            assert num_peers != 0
            start = self._list_cursor
            for offset in range(num_peers):
                peer_index = (start + offset) % num_peers
                peer_id = self._index_to_peer_map[peer_index]
                peer_queue = self._peers_transactions_queues[peer_id]
                if peer_queue.priority_queue.empty():