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) 2021-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 compact blocks HB selection logic."""7from test_framework.test_framework import BitcoinTestFramework8from test_framework.util import assert_equal11class CompactBlocksConnectionTest(BitcoinTestFramework):12"""Test class for verifying selection of HB peer connections."""14def set_test_params(self):15self.setup_clean_chain = True16self.num_nodes = 618def peer_info(self, from_node, to_node):19"""Query from_node for its getpeerinfo about to_node."""20for peerinfo in self.nodes[from_node].getpeerinfo():21if "(testnode%i)" % to_node in peerinfo['subver']:22return peerinfo23return None25def setup_network(self):26self.setup_nodes()27# Start network with everyone disconnected28self.sync_all()30def relay_block_through(self, peer):31"""Relay a new block through peer peer, and return HB status between 1 and [2,3,4,5]."""32self.connect_nodes(peer, 0)33self.generate(self.nodes[0], 1)34self.disconnect_nodes(peer, 0)36def status_to():37return [self.peer_info(1, i)['bip152_hb_to'] for i in range(2, 6)]39def status_from():40return [self.peer_info(i, 1)['bip152_hb_from'] for i in range(2, 6)]42self.wait_until(lambda: status_to() == status_from())43return status_to()45def run_test(self):46self.log.info("Testing reserved high-bandwidth mode slot for outbound peer...")48# Connect everyone to node 0, and mine some blocks to get all nodes out of IBD.49for i in range(1, 6):50self.connect_nodes(i, 0)51self.generate(self.nodes[0], 2)52for i in range(1, 6):53self.disconnect_nodes(i, 0)55# Construct network topology:56# - Node 0 is the block producer57# - Node 1 is the "target" node being tested58# - Nodes 2-5 are intermediaries.59# - Node 1 has an outbound connection to node 260# - Node 1 has inbound connections from nodes 3-561self.connect_nodes(3, 1)62self.connect_nodes(4, 1)63self.connect_nodes(5, 1)64self.connect_nodes(1, 2)66# Mine blocks subsequently relaying through nodes 3,4,5 (inbound to node 1)67for nodeid in range(3, 6):68status = self.relay_block_through(nodeid)69assert_equal(status, [False, nodeid >= 3, nodeid >= 4, nodeid >= 5])71# And again through each. This should not change HB status.72for nodeid in range(3, 6):73status = self.relay_block_through(nodeid)74assert_equal(status, [False, True, True, True])76# Now relay one block through peer 2 (outbound from node 1), so it should take HB status77# from one of the inbounds.78status = self.relay_block_through(2)79assert_equal(status[0], True)80assert_equal(sum(status), 3)82# Now relay again through nodes 3,4,5. Since 2 is outbound, it should remain HB.83for nodeid in range(3, 6):84status = self.relay_block_through(nodeid)85assert status[0]86assert status[nodeid - 2]87assert_equal(sum(status), 3)89# Reconnect peer 2, and retry. Now the three inbounds should be HB again.90self.disconnect_nodes(1, 2)91self.connect_nodes(1, 2)92for nodeid in range(3, 6):93status = self.relay_block_through(nodeid)94assert not status[0]95assert status[nodeid - 2]96assert_equal(status, [False, True, True, True])99if __name__ == '__main__':100CompactBlocksConnectionTest(__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.