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

Code before:
        """
        A peer notifies us of a new transaction.
        Requests a full transaction if we haven't seen it previously, and if the fees are enough.
        """
        # Ignore if syncing
        if self.full_node.sync_store.get_sync_mode():
            return None
        if not (await self.full_node.synced()):
            return None

        # If already seen, the cost and fee must match, otherwise ban the peer
        mempool_item = self.full_node.mempool_manager.get_mempool_item(transaction.transaction_id, include_pending=True)
        if mempool_item is not None:
            if mempool_item.cost != transaction.cost or mempool_item.fee != transaction.fees:
                self.log.warning(
                    f"Banning peer {peer.peer_node_id}. Sent us an already seen tx {transaction.transaction_id} "
                    f"with mismatch on cost {transaction.cost} vs validation cost {mempool_item.cost} and/or "
                    f"fee {transaction.fees} vs {mempool_item.fee}."
                )
                await peer.close(RATE_LIMITER_BAN_SECONDS)
            return None

        if self.full_node.mempool_manager.is_fee_enough(transaction.fees, transaction.cost):
            # If there's current pending request just add this peer to the set of peers that have this tx
            if transaction.transaction_id in self.full_node.full_node_store.pending_tx_request:
                if transaction.transaction_id in self.full_node.full_node_store.peers_with_tx:
                    current_map = self.full_node.full_node_store.peers_with_tx[transaction.transaction_id]
                    if peer.peer_node_id in current_map:
                        return None
                    current_map[peer.peer_node_id] = (transaction.fees, transaction.cost)
                    return None
                else:
                    self.full_node.full_node_store.peers_with_tx[transaction.transaction_id] = {
                        peer.peer_node_id: (transaction.fees, transaction.cost)
                    }
                    return None

            self.full_node.full_node_store.pending_tx_request[transaction.transaction_id] = peer.peer_node_id
            self.full_node.full_node_store.peers_with_tx[transaction.transaction_id] = {
                peer.peer_node_id: (transaction.fees, transaction.cost)
            }

            task_id: bytes32 = bytes32.secret()
            fetch_task = create_referenced_task(
                tx_request_and_timeout(self.full_node, transaction.transaction_id, task_id)
            )
            self.full_node.full_node_store.tx_fetch_tasks[task_id] = fetch_task
            return None
        return None

    @metadata.request(reply_types=[ProtocolMessageTypes.respond_transaction])

Review comment (suggestion):
I'm not sure if it's better, but you could avoid a lookup by using `.get()` instead `in` and check if what you get back is `None` or not.

Code after:
    async def new_transaction(
        self, transaction: full_node_protocol.NewTransaction, peer: WSChiaConnection
    ) -> Optional[Message]:
        """
        A peer notifies us of a new transaction.
        Requests a full transaction if we haven't seen it previously, and if the fees are enough.
        """
        # Ignore if syncing
        if self.full_node.sync_store.get_sync_mode():
            return None
        if not (await self.full_node.synced()):
            return None

        # If already seen, the cost and fee must match, otherwise ban the peer
        mempool_item = self.full_node.mempool_manager.get_mempool_item(transaction.transaction_id, include_pending=True)
        if mempool_item is not None:
            if mempool_item.cost != transaction.cost or mempool_item.fee != transaction.fees:
                self.log.warning(
                    f"Banning peer {peer.peer_node_id}. Sent us an already seen tx {transaction.transaction_id} "
                    f"with mismatch on cost {transaction.cost} vs validation cost {mempool_item.cost} and/or "
                    f"fee {transaction.fees} vs {mempool_item.fee}."
                )
                await peer.close(CONSENSUS_ERROR_BAN_SECONDS)
            return None

        if self.full_node.mempool_manager.is_fee_enough(transaction.fees, transaction.cost):
            # If there's current pending request just add this peer to the set of peers that have this tx
            if transaction.transaction_id in self.full_node.full_node_store.pending_tx_request:
                current_map = self.full_node.full_node_store.peers_with_tx.get(transaction.transaction_id)
                if current_map is None:
                    self.full_node.full_node_store.peers_with_tx[transaction.transaction_id] = {
                        peer.peer_node_id: PeerWithTx(
                            peer_host=peer.peer_info.host,
                            advertised_fee=transaction.fees,
                            advertised_cost=transaction.cost,
                        )
                    }
                    return None
                prev = current_map.get(peer.peer_node_id)
                if prev is not None:
                    if prev.advertised_fee != transaction.fees or prev.advertised_cost != transaction.cost:
                        self.log.warning(
                            f"Banning peer {peer.peer_node_id}. Sent us a new tx {transaction.transaction_id} with "
                            f"mismatch on cost {transaction.cost} vs previous advertised cost {prev.advertised_cost} "
                            f"and/or fee {transaction.fees} vs previous advertised fee {prev.advertised_fee}."
                        )
                        await peer.close(CONSENSUS_ERROR_BAN_SECONDS)
                    return None
                current_map[peer.peer_node_id] = PeerWithTx(
                    peer_host=peer.peer_info.host, advertised_fee=transaction.fees, advertised_cost=transaction.cost
                )