Repository: bitcoin/bitcoin
File: test/functional/p2p_private_broadcast.py (Python)

Code before:
                "tx": 1,
                "ping": 1
            })
            dummy_address = CAddress()
            dummy_address.nServices = 0
            assert_equal(peer.last_message["version"].nVersion, P2P_VERSION)
            assert_equal(peer.last_message["version"].nServices, 0)
            assert_equal(peer.last_message["version"].nTime, 0)
            assert_equal(peer.last_message["version"].addrTo, dummy_address)
            assert_equal(peer.last_message["version"].addrFrom, dummy_address)
            assert_equal(peer.last_message["version"].strSubVer, "/pynode:0.0.1/")
            assert_equal(peer.last_message["version"].nStartingHeight, 0)
            assert_equal(peer.last_message["version"].relay, 0)
            assert_equal(peer.last_message["tx"].tx.txid_hex, tx["txid"])
            self.log.info(f"{label}: ok: outbound connection i={i} is private broadcast of txid={tx['txid']}")
            broadcasts_done += 1

        # Verify the tx we just observed is tracked in getprivatebroadcastinfo.
        pbinfo = self.nodes[0].getprivatebroadcastinfo()
        pending = [t for t in pbinfo["transactions"] if t["txid"] == tx["txid"] and t["wtxid"] == tx["wtxid"]]
        assert_equal(len(pending), 1)
        assert_equal(pending[0]["hex"].lower(), tx["hex"].lower())
        peers = pending[0]["peers"]
        assert len(peers) >= NUM_PRIVATE_BROADCAST_PER_TX
        assert all("address" in p and "sent" in p for p in peers)
        assert sum(1 for p in peers if "received" in p) >= NUM_PRIVATE_BROADCAST_PER_TX

    def run_test(self):
        tx_originator = self.nodes[0]
        tx_receiver = self.nodes[1]
        far_observer = tx_receiver.add_p2p_connection(P2PInterface())

        wallet = MiniWallet(tx_originator)

        # Fill tx_originator's addrman.
        for addr in ADDRMAN_ADDRESSES:
            res = tx_originator.addpeeraddress(address=addr, port=8333, tried=False)
            if not res["success"]:
                self.log.debug(f"Could not add {addr} to tx_originator's addrman (collision?)")

        self.wait_until(lambda: len(self.destinations) == NUM_INITIAL_CONNECTIONS)

        # The next opened connection by tx_originator should be "private broadcast"
        # for sending the transaction. The SOCKS5 proxy should redirect it to tx_receiver.

        txs = wallet.create_self_transfer_chain(chain_length=3)
        self.log.info(f"Created txid={txs[0]['txid']}: for basic test")
        self.log.info(f"Created txid={txs[1]['txid']}: for broadcast with dependency in mempool + rebroadcast")
        self.log.info(f"Created txid={txs[2]['txid']}: for broadcast with dependency not in mempool")
        tx_originator.sendrawtransaction(hexstring=txs[0]["hex"], maxfeerate=0.1)


Review comment (suggestion):
some people prefer to use the dedicated asserts that return better :
```suggestion
        assert_greater_than_or_equal(sum(1 for p in peers if "received" in p), NUM_PRIVATE_BROADCAST_PER_TX)
```

Code after:
                "inv": 1,
                "tx": 1,
                "ping": 1
            })
            dummy_address = CAddress()
            dummy_address.nServices = 0
            assert_equal(peer.last_message["version"].nVersion, P2P_VERSION)
            assert_equal(peer.last_message["version"].nServices, 0)
            assert_equal(peer.last_message["version"].nTime, 0)
            assert_equal(peer.last_message["version"].addrTo, dummy_address)
            assert_equal(peer.last_message["version"].addrFrom, dummy_address)
            assert_equal(peer.last_message["version"].strSubVer, "/pynode:0.0.1/")
            assert_equal(peer.last_message["version"].nStartingHeight, 0)
            assert_equal(peer.last_message["version"].relay, 0)
            assert_equal(peer.last_message["tx"].tx.txid_hex, tx["txid"])
            self.log.info(f"{label}: ok: outbound connection i={i} is private broadcast of txid={tx['txid']}")
            broadcasts_done += 1

        # Verify the tx we just observed is tracked in getprivatebroadcastinfo.
        pbinfo = self.nodes[0].getprivatebroadcastinfo()
        pending = [t for t in pbinfo["transactions"] if t["txid"] == tx["txid"] and t["wtxid"] == tx["wtxid"]]
        assert_equal(len(pending), 1)
        assert_equal(pending[0]["hex"].lower(), tx["hex"].lower())
        peers = pending[0]["peers"]
        assert len(peers) >= NUM_PRIVATE_BROADCAST_PER_TX
        assert all("address" in p and "sent" in p for p in peers)
        assert_greater_than_or_equal(sum(1 for p in peers if "received" in p), NUM_PRIVATE_BROADCAST_PER_TX)

    def run_test(self):
        tx_originator = self.nodes[0]
        tx_receiver = self.nodes[1]
        far_observer = tx_receiver.add_p2p_connection(P2PInterface())

        wallet = MiniWallet(tx_originator)

        # Fill tx_originator's addrman.
        for addr in ADDRMAN_ADDRESSES:
            res = tx_originator.addpeeraddress(address=addr, port=8333, tried=False)
            if not res["success"]:
                self.log.debug(f"Could not add {addr} to tx_originator's addrman (collision?)")

        self.wait_until(lambda: len(self.destinations) == NUM_INITIAL_CONNECTIONS)

        # The next opened connection by tx_originator should be "private broadcast"
        # for sending the transaction. The SOCKS5 proxy should redirect it to tx_receiver.

        txs = wallet.create_self_transfer_chain(chain_length=3)
        self.log.info(f"Created txid={txs[0]['txid']}: for basic test")
        self.log.info(f"Created txid={txs[1]['txid']}: for broadcast with dependency in mempool + rebroadcast")
        self.log.info(f"Created txid={txs[2]['txid']}: for broadcast with dependency not in mempool")
        tx_originator.sendrawtransaction(hexstring=txs[0]["hex"], maxfeerate=0.1)