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

Code before:
        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
            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._queue_dict[peer_id]
                if peer_queue.empty():
                    continue
                # There is no peek method so we access the internal `queue`
                _, entry = peer_queue.queue[0]
                tx_info = entry.peers_with_tx.get(peer_id)
                if tx_info is None:
                    top_tx_advertised_cost = max(
                        (t.advertised_cost for t in entry.peers_with_tx.values()), default=self._max_tx_clvm_cost
                    )
                else:
                    top_tx_advertised_cost = tx_info.advertised_cost
                top_txs_advertised_costs[peer_id] = top_tx_advertised_cost
                if self._deficit_counters[peer_id] >= top_tx_advertised_cost:
                    # This peer can afford its top transaction
                    _, entry = peer_queue.get()
                    self._deficit_counters[peer_id] -= top_tx_advertised_cost
                    if peer_queue.empty():
                        self._deficit_counters[peer_id] = 0
                    # Let's advance the cursor to the next peer
                    self._list_cursor = (peer_index + 1) % num_peers
                    # See if we need to perform the periodic cleanup
                    self._cleanup_counter = (self._cleanup_counter + 1) % 100
                    if self._cleanup_counter == 0:
                        self._cleanup_peer_queues()
                    return entry
            # None of the peers could afford to send their top transactions, so
            # let's add the lowest cost among transactions to all the deficit
            # counters for the next iteration.
            if len(top_txs_advertised_costs) == 0:
                continue

Review comment (performance):
I don't think you need the `_list_cursor` anymore, now that you look at every peer every time you pop. There's a risk it might not be very efficient though.
It might be OK for a first version. A heap might be a more efficient choice

Code after:
        """
        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():
                    continue
                # There is no peek method so we access the internal `queue`
                _, entry = peer_queue.priority_queue.queue[0]
                tx_info = entry.peers_with_tx.get(peer_id)
                # If we don't know the cost information for this transaction
                # we fallback to the highest cost.
                if tx_info is not None:
                    # At this point we have no transactions with zero cost
                    assert tx_info.advertised_cost > 0
                    top_tx_advertised_cost = tx_info.advertised_cost
                else:
                    top_tx_advertised_cost = self._max_tx_clvm_cost
                top_txs_advertised_costs[peer_id] = top_tx_advertised_cost
                if peer_queue.deficit >= top_tx_advertised_cost:
                    # This peer can afford its top transaction
                    _, entry = peer_queue.priority_queue.get()
                    peer_queue.deficit -= top_tx_advertised_cost
                    if peer_queue.priority_queue.empty():
                        peer_queue.deficit = 0
                    # Let's advance the cursor to the next peer