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

Code before:
        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

    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._queue_dict:
                self._queue_dict[peer_id] = PriorityQueue()
                self._index_to_peer_map.append(peer_id)
            if self._queue_dict[peer_id].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:
                    # Compute the fee per cost using values from the peer with
                    # the highest advertised cost.
                    priority = float("inf")
                    tx_info = max(tx.peers_with_tx.values(), key=lambda p: p.advertised_cost, default=None)
                    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
                self._queue_dict[peer_id].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

    async def pop(self) -> TransactionQueueEntry:
        await self._queue_length.acquire()
        if not self._high_priority_queue.empty():
            return self._high_priority_queue.get()
        result: TransactionQueueEntry | None = None
        while True:
            peer_queue = self._queue_dict[self._index_to_peer_map[self._list_cursor]]
            if not peer_queue.empty():
                _, result = peer_queue.get()
            self._list_cursor += 1
            if self._list_cursor > len(self._index_to_peer_map) - 1:
                # reset iterator
                self._list_cursor = 0
                new_peer_map = []
                for peer_id in self._index_to_peer_map:
                    if self._queue_dict[peer_id].empty():
                        self._queue_dict.pop(peer_id)
                    else:
                        new_peer_map.append(peer_id)

Review comment (suggestion):
here too. It seems more appropriate to set:
```
priority = tx_info.advertised_cost / tx_info.advertised_fee
```

Code after:
        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

    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._queue_dict:
                self._queue_dict[peer_id] = PriorityQueue()
                self._index_to_peer_map.append(peer_id)
            if self._queue_dict[peer_id].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._queue_dict[peer_id].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

    async def pop(self) -> TransactionQueueEntry:
        await self._queue_length.acquire()
        if not self._high_priority_queue.empty():
            return self._high_priority_queue.get()
        result: TransactionQueueEntry | None = None
        while True:
            peer_queue = self._queue_dict[self._index_to_peer_map[self._list_cursor]]
            if not peer_queue.empty():
                _, result = peer_queue.get()
            self._list_cursor += 1
            if self._list_cursor > len(self._index_to_peer_map) - 1:
                # reset iterator
                self._list_cursor = 0
                new_peer_map = []
                for peer_id in self._index_to_peer_map:
                    if self._queue_dict[peer_id].empty():
                        self._queue_dict.pop(peer_id)
                    else:
                        new_peer_map.append(peer_id)
                self._index_to_peer_map = new_peer_map
            if result is not None:
                return result