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.
1001cmpct_block = HeaderAndShortIDs()1002cmpct_block.initialize_from_block(block)1003msg = msg_cmpctblock(cmpct_block.to_p2p())1004return block, msg1006# Sends a compact block, uses presence of GETBLOCKTXN as a heuristic1007# for whether or not the node processed the CMPCTBLOCK.1008# Note: since we never fulfill the GETBLOCKTXN, this will never change1009# the HB status of a connection.1010def ignores_compact_block(conn, solicited=False):1011block, msg = build_compact_block()1012conn.clear_getblocktxn()1013if solicited:1014conn.send_without_ping(msg_headers([block]))1015conn.wait_for_getdata([block.hash_int], timeout=30)1016conn.send_and_ping(msg)1017with p2p_lock:1018return "getblocktxn" not in conn.last_message1020# create new p2p connection for a fresh state w/o any prior sendcmpct messages sent1021unsolicited_peer = self.nodes[0].add_p2p_connection(TestP2PConn())1022self.assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)1024self.log.info("Test that a node ignores unsolicited CMPCTBLOCK messages from peers that have not sent SENDCMPCT.")1025assert ignores_compact_block(unsolicited_peer, solicited=False)1027self.log.info("Test that a node ignores solicited CMPCTBLOCK messages from peers that have not sent SENDCMPCT.")1028assert ignores_compact_block(unsolicited_peer, solicited=True)1030# Unsolicited peer announces CMPCTBLOCK support with SENDCMPCT message,1031# but still non-HB.1032unsolicited_peer.send_and_ping(msg_sendcmpct())1033self.assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)1035self.log.info("Test that a node ignores unsolicited CMPCTBLOCK messages from non-HB peers.")1036assert ignores_compact_block(unsolicited_peer, solicited=False)1037self.assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)1039self.log.info("Test that a node does not ignore solicited CMPCTBLOCK messages from non-HB peers.")1040assert not ignores_compact_block(unsolicited_peer, solicited=True)1041self.assert_highbandwidth_states(node, idx=-1, hb_to=False, hb_from=False)1043# The node will ask for transactions from an unsolicited compact block1044# it receives from a high bandwidth peer, we need to use one set up earlier,1045# since all the slots are full.1046self.log.info("Test that a node does not ignore unsolicited CMPCTBLOCK messages from HB peers.")1048hb_peer_idx = -21049self.assert_highbandwidth_states(node, idx=hb_peer_idx, hb_to=True, hb_from=False)1050hb_peer = self.nodes[0].p2ps[hb_peer_idx]1051assert not ignores_compact_block(hb_peer, solicited=False)1053def run_test(self):1054self.wallet = MiniWallet(self.nodes[0])1056# Setup the p2p connections1057self.segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn())1058self.additional_segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn())1059self.onemore_inbound_node = self.nodes[0].add_p2p_connection(TestP2PConn())1060self.outbound_node = self.nodes[0].add_outbound_p2p_connection(TestP2PConn(), p2p_idx=3, connection_type="outbound-full-relay")1062# We will need UTXOs to construct transactions in later tests.1063self.make_utxos()1065assert softfork_active(self.nodes[0], "segwit")1067self.log.info("Testing SENDCMPCT p2p message... ")1068self.test_sendcmpct(self.segwit_node)1069self.test_sendcmpct(self.additional_segwit_node)1070self.test_sendcmpct(self.onemore_inbound_node)1071self.test_sendcmpct(self.outbound_node)1073self.log.info("Testing compactblock construction...")1074self.test_compactblock_construction(self.segwit_node)1076self.log.info("Testing compactblock requests (segwit node)... ")1077self.test_compactblock_requests(self.segwit_node)1079self.log.info("Testing getblocktxn requests (segwit node)...")1080self.test_getblocktxn_requests(self.segwit_node)1082self.log.info("Testing getblocktxn handler (segwit node should return witnesses)...")1083self.test_getblocktxn_handler(self.segwit_node)1085self.log.info("Testing compactblock requests/announcements not at chain tip...")1086self.test_compactblocks_not_at_tip(self.segwit_node)1088self.log.info("Testing handling of low-work compact blocks...")1089self.test_low_work_compactblocks(self.segwit_node)1091self.log.info("Testing handling of incorrect blocktxn responses...")1092self.test_incorrect_blocktxn_response(self.segwit_node)1094self.log.info("Testing reconstructing compact blocks with a stalling peer...")1095self.test_compactblock_reconstruction_stalling_peer(self.segwit_node, self.additional_segwit_node)1097self.log.info("Testing reconstructing compact blocks from multiple peers...")1098self.test_compactblock_reconstruction_parallel_reconstruction(stalling_peer=self.segwit_node, inbound_peer=self.onemore_inbound_node, delivery_peer=self.additional_segwit_node, outbound_peer=self.outbound_node)1100# Test that if we submitblock to node1, we'll get a compact block1101# announcement to all peers.1102# (Post-segwit activation, blocks won't propagate from node0 to node11103# automatically, so don't bother testing a block announced to node0.)1104self.log.info("Testing end-to-end block relay...")1105self.request_cb_announcements(self.segwit_node)1106self.request_cb_announcements(self.additional_segwit_node)1107self.test_end_to_end_block_relay([self.segwit_node, self.additional_segwit_node])1109self.log.info("Testing handling of invalid compact blocks...")1110self.test_invalid_tx_in_compactblock(self.segwit_node)1111self.test_empty_getblocktxn_disconnects()1113# The previous test will lead to a disconnection. Reconnect before continuing.1114self.segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn())1115self.segwit_node.send_and_ping(msg_sendcmpct())1117self.log.info("Testing handling of multiple blocktxn responses...")1118self.test_multiple_blocktxn_response(self.segwit_node)1120# The previous test will lead to a disconnection. Reconnect before continuing.1121self.segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn())1122self.segwit_node.send_and_ping(msg_sendcmpct())1124self.log.info("Testing invalid announce field in sendcmpct message...")1125self.test_invalid_sendcmpct_announce()1127self.log.info("Testing invalid index in cmpctblock message...")1128self.test_invalid_cmpctblock_message()1130self.log.info("Testing high-bandwidth mode states via getpeerinfo...")1131self.test_highbandwidth_mode_states_via_getpeerinfo()1133self.log.info("Testing CMPCTBLOCK messages are ignored when expected...")1134self.test_compact_blocks_ignored()1137if __name__ == '__main__':1138CompactBlocksTest(__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.