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"""6Test addrv2 relay7"""9import time11from test_framework.messages import (12CAddress,13msg_addrv2,14msg_sendaddrv2,15)16from test_framework.p2p import (17P2PInterface,18P2P_SERVICES,19)20from test_framework.test_framework import BitcoinTestFramework21from test_framework.util import assert_equal23I2P_ADDR = "c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p"24ONION_ADDR = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"26ADDRS = []27for i in range(10):28addr = CAddress()29addr.time = int(time.time()) + i30addr.port = 8333 + i31addr.nServices = P2P_SERVICES32# Add one I2P and one onion V3 address at an arbitrary position.33if i == 5:34addr.net = addr.NET_I2P35addr.ip = I2P_ADDR36addr.port = 037elif i == 8:38addr.net = addr.NET_TORV339addr.ip = ONION_ADDR40else:41addr.ip = f"123.123.123.{i % 256}"42ADDRS.append(addr)45class AddrReceiver(P2PInterface):46addrv2_received_and_checked = False48def __init__(self):49super().__init__(support_addrv2 = True)51def on_addrv2(self, message):52expected_set = set((addr.ip, addr.port) for addr in ADDRS)53received_set = set((addr.ip, addr.port) for addr in message.addrs)54if expected_set == received_set:55self.addrv2_received_and_checked = True57def wait_for_addrv2(self):58self.wait_until(lambda: "addrv2" in self.last_message)61def calc_addrv2_msg_size(addrs):62size = 1 # vector length byte63for addr in addrs:64size += 4 # time65size += 1 # services, COMPACTSIZE(P2P_SERVICES)66size += 1 # network id67size += 1 # address length byte68size += addr.ADDRV2_ADDRESS_LENGTH[addr.net] # address69size += 2 # port70return size72class AddrTest(BitcoinTestFramework):73def set_test_params(self):74self.setup_clean_chain = True75self.num_nodes = 176self.extra_args = [["[email protected]"]]78def run_test(self):79self.log.info('Check disconnection when sending sendaddrv2 after verack')80conn = self.nodes[0].add_p2p_connection(P2PInterface())81with self.nodes[0].assert_debug_log(['sendaddrv2 received after verack, disconnecting peer=0']):82conn.send_without_ping(msg_sendaddrv2())83conn.wait_for_disconnect()85self.log.info('Create connection that sends addrv2 messages')86addr_source = self.nodes[0].add_p2p_connection(P2PInterface())87msg = msg_addrv2()89self.log.info('Check that addrv2 message content is relayed and added to addrman')90addr_receiver = self.nodes[0].add_p2p_connection(AddrReceiver())91msg.addrs = ADDRS92msg_size = calc_addrv2_msg_size(ADDRS)93with self.nodes[0].assert_debug_log([94f'received: addrv2 ({msg_size} bytes) peer=1',95f'sending addrv2 ({msg_size} bytes) peer=2',96]):97addr_source.send_and_ping(msg)98self.nodes[0].setmocktime(int(time.time()) + 30 * 60)99addr_receiver.wait_for_addrv2()101assert addr_receiver.addrv2_received_and_checked102assert_equal(len(self.nodes[0].getnodeaddresses(count=0, network="i2p")), 0)104self.log.info('Send too-large addrv2 message')105msg.addrs = ADDRS * 101106with self.nodes[0].assert_debug_log(['addrv2 message size = 1010']):107addr_source.send_without_ping(msg)108addr_source.wait_for_disconnect()112if __name__ == '__main__':113AddrTest(__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.