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

Code before:
        """
        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)
            if num_peers == 0:
                assert False  # pragma: no cover
            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

Review comment (suggestion):
```suggestion
            assert num_peers != 0
```

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