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) 2020-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"""Test transaction relay behavior during IBD:6- Set fee filters to MAX_MONEY7- Don't request transactions8- Ignore all transaction messages9"""11from decimal import Decimal12import time14from test_framework.blocktools import create_block15from test_framework.messages import (16CInv,17COIN,18CTransaction,19from_hex,20msg_inv,21msg_tx,22MSG_WTX,23)24from test_framework.p2p import (25NONPREF_PEER_TX_DELAY,26P2PDataStore,27P2PInterface,28p2p_lock,29)30from test_framework.test_framework import BitcoinTestFramework32MAX_FEE_FILTER = Decimal(9936506) / COIN33NORMAL_FEE_FILTER = Decimal(10) / COIN36class P2PIBDTxRelayTest(BitcoinTestFramework):37def set_test_params(self):38self.setup_clean_chain = True39self.num_nodes = 240self.extra_args = [41["-minrelaytxfee={:.8f}".format(NORMAL_FEE_FILTER)],42["-minrelaytxfee={:.8f}".format(NORMAL_FEE_FILTER)],43]45def run_test(self):46self.log.info("Check that nodes set minfilter to MAX_MONEY while still in IBD")47for node in self.nodes:48assert node.getblockchaininfo()['initialblockdownload']49self.wait_until(lambda: all(peer['minfeefilter'] == MAX_FEE_FILTER for peer in node.getpeerinfo()))51self.nodes[0].setmocktime(int(time.time()))52self.log.info("Mine one old block so we stay in IBD, then remember its coinbase wtxid")53block = create_block(int(self.nodes[0].getbestblockhash(), 16), height=1, ntime=int(time.time()) - 2 * 24 * 60 * 60)54block.solve()55self.nodes[0].submitblock(block.serialize().hex())56assert self.nodes[0].getblockchaininfo()['initialblockdownload']57# BlockConnected is async. Sync while in IBD so a delayed event cannot58# add ibd_wtxid to the recently-confirmed filter.59self.nodes[0].syncwithvalidationinterfacequeue()60ibd_wtxid = int(self.nodes[0].getblock(f"{block.hash_int:064x}", 2)["tx"][0]["hash"], 16)62self.log.info("Check that nodes don't send getdatas for transactions while still in IBD")63peer_inver = self.nodes[0].add_p2p_connection(P2PDataStore())64txid = 0xdeadbeef65peer_inver.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=txid)]))66# The node should not send a getdata, but if it did, it would first delay 2 seconds67self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)68peer_inver.sync_with_ping()69with p2p_lock:70assert txid not in peer_inver.getdata_requests71self.nodes[0].disconnect_p2ps()73self.log.info("Check that nodes don't process unsolicited transactions while still in IBD")74# A transaction hex pulled from tx_valid.json. There are no valid transactions since no UTXOs75# exist yet, but it should be a well-formed transaction.76rawhex = "0100000001b14bdcbc3e01bdaad36cc08e81e69c82e1060bc14e518db2b49aa43ad90ba260000000004a01ff473" + \77"04402203f16c6f40162ab686621ef3000b04e75418a0c0cb2d8aebeac894ae360ac1e780220ddc15ecdfc3507ac48e168" + \78"1a33eb60996631bf6bf5bc0a0682c4db743ce7ca2b01ffffffff0140420f00000000001976a914660d4ef3a743e3e696a" + \79"d990364e555c271ad504b88ac00000000"80assert self.nodes[1].decoderawtransaction(rawhex) # returns a dict, should not throw81tx = from_hex(CTransaction(), rawhex)82peer_txer = self.nodes[0].add_p2p_connection(P2PInterface())83with self.nodes[0].assert_debug_log(expected_msgs=["received: tx"], unexpected_msgs=["was not accepted"]):84peer_txer.send_and_ping(msg_tx(tx))85self.nodes[0].disconnect_p2ps()87# Come out of IBD by generating a block88self.generate(self.nodes[0], 1)90self.log.info("Check that nodes reset minfilter after coming out of IBD")91for node in self.nodes:92assert not node.getblockchaininfo()['initialblockdownload']93self.wait_until(lambda: all(peer['minfeefilter'] == NORMAL_FEE_FILTER for peer in node.getpeerinfo()))95self.log.info("Check that txs confirmed during IBD are not in the recently-confirmed filter once out of ibd")96peer_inver = self.nodes[0].add_p2p_connection(P2PDataStore())97peer_inver.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=ibd_wtxid)]))98self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)99peer_inver.wait_for_getdata([ibd_wtxid])100self.nodes[0].disconnect_p2ps()102self.log.info("Check that nodes process the same transaction, even when unsolicited, when no longer in IBD")103peer_txer = self.nodes[0].add_p2p_connection(P2PInterface())104with self.nodes[0].assert_debug_log(expected_msgs=["was not accepted"]):105peer_txer.send_and_ping(msg_tx(tx))107if __name__ == '__main__':108P2PIBDTxRelayTest(__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.