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"""6Test p2p addr-fetch connections7"""9import time11from test_framework.messages import (12CAddress,13msg_addr,14)15from test_framework.p2p import (16P2PInterface,17p2p_lock,18P2P_SERVICES,19)20from test_framework.test_framework import BitcoinTestFramework21from test_framework.util import assert_equal23ADDR = CAddress()24ADDR.time = int(time.time())25ADDR.nServices = P2P_SERVICES26ADDR.ip = "192.0.0.8"27ADDR.port = 1844430class P2PAddrFetch(BitcoinTestFramework):31def set_test_params(self):32self.setup_clean_chain = True33self.num_nodes = 135def assert_getpeerinfo(self, *, peer_ids):36num_peers = len(peer_ids)37info = self.nodes[0].getpeerinfo()38assert_equal(len(info), num_peers)39for n in range(0, num_peers):40assert_equal(info[n]['id'], peer_ids[n])41assert_equal(info[n]['connection_type'], 'addr-fetch')43def run_test(self):44node = self.nodes[0]45self.log.info("Connect to an addr-fetch peer")46peer_id = 047peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=peer_id, connection_type="addr-fetch")48self.assert_getpeerinfo(peer_ids=[peer_id])50self.log.info("Check that we send getaddr but don't try to sync headers with the addr-fetch peer")51peer.sync_with_ping()52with p2p_lock:53assert_equal(peer.message_count['getaddr'], 1)54assert_equal(peer.message_count['getheaders'], 0)56self.log.info("Check that answering the getaddr with a single address does not lead to disconnect")57# This prevents disconnecting on self-announcements58msg = msg_addr()59msg.addrs = [ADDR]60peer.send_and_ping(msg)61self.assert_getpeerinfo(peer_ids=[peer_id])63self.log.info("Check that answering with larger addr messages leads to disconnect")64msg.addrs = [ADDR] * 265peer.send_without_ping(msg)66peer.wait_for_disconnect(timeout=5)68self.log.info("Check timeout for addr-fetch peer that does not send addrs")69peer_id = 170peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=peer_id, connection_type="addr-fetch")72time_now = int(time.time())73self.assert_getpeerinfo(peer_ids=[peer_id])75# Expect addr-fetch peer connection to be maintained up to 5 minutes.76node.setmocktime(time_now + 295)77self.assert_getpeerinfo(peer_ids=[peer_id])79# Expect addr-fetch peer connection to be disconnected after 5 minutes.80node.setmocktime(time_now + 301)81peer.wait_for_disconnect(timeout=5)82self.assert_getpeerinfo(peer_ids=[])85if __name__ == '__main__':86P2PAddrFetch(__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.