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.
501# both blocks (same work as tip)502test_node.send_header_for_blocks(blocks[1:2])503test_node.sync_with_ping()504test_node.wait_for_getdata([x.hash_int for x in blocks[0:2]], timeout=DIRECT_FETCH_RESPONSE_TIME)506# Announcing 16 more headers should trigger direct fetch for 14 more507# blocks508test_node.send_header_for_blocks(blocks[2:18])509test_node.sync_with_ping()510test_node.wait_for_getdata([x.hash_int for x in blocks[2:16]], timeout=DIRECT_FETCH_RESPONSE_TIME)512# Announcing 1 more header should not trigger any response513test_node.last_message.pop("getdata", None)514test_node.send_header_for_blocks(blocks[18:19])515test_node.sync_with_ping()516with p2p_lock:517assert "getdata" not in test_node.last_message519self.log.info("Part 4: success!")521# Now deliver all those blocks we announced.522[test_node.send_without_ping(msg_block(x)) for x in blocks]524self.log.info("Part 5: Testing handling of unconnecting headers")525# First we test that receipt of an unconnecting header doesn't prevent526# chain sync.527expected_hash = tip528NUM_HEADERS = 100529for i in range(NUM_HEADERS):530self.log.debug("Part 5.{}: starting...".format(i))531test_node.last_message.pop("getdata", None)532blocks = []533# Create two more blocks.534for _ in range(2):535blocks.append(create_block(tip, height=height, ntime=block_time))536blocks[-1].solve()537tip = blocks[-1].hash_int538block_time += 1539height += 1540# Send the header of the second block -> this won't connect.541test_node.send_header_for_blocks([blocks[1]])542test_node.wait_for_getheaders(block_hash=expected_hash)543test_node.send_header_for_blocks(blocks)544test_node.wait_for_getdata([x.hash_int for x in blocks])545[test_node.send_without_ping(msg_block(x)) for x in blocks]546test_node.sync_with_ping()547assert_equal(self.nodes[0].getbestblockhash(), blocks[1].hash_hex)548expected_hash = blocks[1].hash_int550blocks = []551# Now we test that if we repeatedly don't send connecting headers, we552# don't go into an infinite loop trying to get them to connect.553for _ in range(NUM_HEADERS + 1):554blocks.append(create_block(tip, height=height, ntime=block_time))555blocks[-1].solve()556tip = blocks[-1].hash_int557block_time += 1558height += 1560for i in range(1, NUM_HEADERS):561with p2p_lock:562test_node.last_message.pop("getheaders", None)563# Send an empty header as a failed response to the received getheaders564# (from the previous iteration). Otherwise, the new headers will be565# treated as a response instead of as an announcement.566test_node.send_header_for_blocks([])567# Send the actual unconnecting header, which should trigger a new getheaders.568test_node.send_header_for_blocks([blocks[i]])569test_node.wait_for_getheaders(block_hash=expected_hash)571# Finally, check that the inv node never received a getdata request,572# throughout the test573assert "getdata" not in inv_node.last_message575if __name__ == '__main__':576SendHeadersTest(__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.