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) 2019-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.6"""7Test seednode interaction with the AddrMan8"""9import random10import time12from test_framework.netutil import UNREACHABLE_PROXY_ARG13from test_framework.test_framework import BitcoinTestFramework15ADD_NEXT_SEEDNODE = 1018class P2PSeedNodes(BitcoinTestFramework):19def set_test_params(self):20self.num_nodes = 121# Specify a non-working proxy to make sure no actual connections to random IPs are attempted.22self.extra_args = [[UNREACHABLE_PROXY_ARG]]23self.disable_autoconnect = False25def test_no_seednode(self):26self.log.info("Check that if no seednode is provided, the node proceeds as usual (without waiting)")27with self.nodes[0].assert_debug_log(expected_msgs=[], unexpected_msgs=["Empty addrman, adding seednode", f"Couldn't connect to peers from addrman after {ADD_NEXT_SEEDNODE} seconds. Adding seednode"], timeout=ADD_NEXT_SEEDNODE):28self.restart_node(0, extra_args=self.nodes[0].extra_args)30def test_seednode_empty_addrman(self):31seed_node = "25.0.0.1"32self.log.info("Check that the seednode is immediately added on bootstrap on an empty addrman")33with self.nodes[0].assert_debug_log(expected_msgs=[f"Empty addrman, adding seednode ({seed_node}) to addrfetch"], timeout=ADD_NEXT_SEEDNODE):34self.restart_node(0, extra_args=self.nodes[0].extra_args + [f'-seednode={seed_node}'])36def test_seednode_non_empty_addrman(self):37self.log.info("Check that if addrman is non-empty, seednodes are queried with a delay")38seed_node = "25.0.0.2"39node = self.nodes[0]40# Fill the addrman with unreachable nodes41for i in range(10):42ip = f"{random.randrange(128,169)}.{random.randrange(1,255)}.{random.randrange(1,255)}.{random.randrange(1,255)}"43port = 8333 + i44node.addpeeraddress(ip, port)46# Restart the node so seednode is processed again.47with node.assert_debug_log(expected_msgs=["trying v1 connection"], timeout=ADD_NEXT_SEEDNODE):48self.restart_node(0, extra_args=self.nodes[0].extra_args + [f'-seednode={seed_node}'])50with node.assert_debug_log(expected_msgs=[f"Couldn't connect to peers from addrman after {ADD_NEXT_SEEDNODE} seconds. Adding seednode ({seed_node}) to addrfetch"], unexpected_msgs=["Empty addrman, adding seednode"], timeout=ADD_NEXT_SEEDNODE * 1.5):51node.setmocktime(int(time.time()) + ADD_NEXT_SEEDNODE + 1)53def run_test(self):54self.test_no_seednode()55self.test_seednode_empty_addrman()56self.test_seednode_non_empty_addrman()59if __name__ == '__main__':60P2PSeedNodes(__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.