Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2022-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""6Test that transaction announcements are only queued for peers that have7successfully completed the version handshake.9Topology:11tx_originator ----> node[0] <---- spy13We test that a transaction sent by tx_originator is only relayed to spy14if it was received after spy's version handshake completed.161. Fully connect tx_originator172. Connect spy (no version handshake)183. tx_originator sends tx1194. spy completes the version handshake205. tx_originator sends tx2216. We check that only tx2 is announced on the spy interface22"""23from test_framework.messages import (24msg_wtxidrelay,25msg_verack,26msg_tx,27CInv,28MSG_WTX,29)30from test_framework.p2p import (31P2PInterface,32)33from test_framework.test_framework import BitcoinTestFramework34from test_framework.wallet import MiniWallet36class P2PTxSpy(P2PInterface):37def __init__(self):38super().__init__()39self.all_invs = []41def on_version(self, message):42self.send_without_ping(msg_wtxidrelay())44def on_inv(self, message):45self.all_invs += message.inv47def wait_for_inv_match(self, expected_inv):48self.wait_until(lambda: len(self.all_invs) == 1 and self.all_invs[0] == expected_inv)50class TxPrivacyTest(BitcoinTestFramework):51def set_test_params(self):52self.num_nodes = 154def run_test(self):55self.wallet = MiniWallet(self.nodes[0])57tx_originator = self.nodes[0].add_p2p_connection(P2PInterface())58spy = self.nodes[0].add_p2p_connection(P2PTxSpy(), wait_for_verack=False)59spy.wait_for_verack()61# tx_originator sends tx162tx1 = self.wallet.create_self_transfer()["tx"]63tx_originator.send_and_ping(msg_tx(tx1))65# Spy sends the verack66spy.send_and_ping(msg_verack())68# tx_originator sends tx269tx2 = self.wallet.create_self_transfer()["tx"]70tx_originator.send_and_ping(msg_tx(tx2))72# Spy should only get an inv for the second transaction as the first73# one was received pre-verack with the spy74spy.wait_for_inv_match(CInv(MSG_WTX, tx2.wtxid_int))76if __name__ == '__main__':77TxPrivacyTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.