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"""Test ThreadDNSAddressSeed logic for querying DNS seeds."""7import itertools9from test_framework.netutil import UNREACHABLE_PROXY_ARG10from test_framework.p2p import P2PInterface11from test_framework.test_framework import BitcoinTestFramework14class P2PDNSSeeds(BitcoinTestFramework):15def set_test_params(self):16self.setup_clean_chain = True17self.num_nodes = 118self.extra_args = [["-dnsseed=1", UNREACHABLE_PROXY_ARG]]20def run_test(self):21self.init_arg_tests()22self.existing_outbound_connections_test()23self.existing_block_relay_connections_test()24self.force_dns_test()25self.wait_time_tests()27def init_arg_tests(self):28fakeaddr = "fakenodeaddr.fakedomain.invalid."30self.log.info("Check that setting -connect disables -dnsseed by default")31self.nodes[0].stop_node()32with self.nodes[0].assert_debug_log(expected_msgs=["DNS seeding disabled"], timeout=2):33self.start_node(0, extra_args=[f"-connect={fakeaddr}", UNREACHABLE_PROXY_ARG])35self.log.info("Check that running -connect and -dnsseed means DNS logic runs.")36with self.nodes[0].assert_debug_log(expected_msgs=["Loading addresses from DNS seed"], timeout=12):37self.restart_node(0, extra_args=[f"-connect={fakeaddr}", "-dnsseed=1", UNREACHABLE_PROXY_ARG])39self.log.info("Check that running -forcednsseed and -dnsseed=0 throws an error.")40self.nodes[0].stop_node()41self.nodes[0].assert_start_raises_init_error(42expected_msg="Error: Cannot set -forcednsseed to true when setting -dnsseed to false.",43extra_args=["-forcednsseed=1", "-dnsseed=0"],44)46self.log.info("Check that running -forcednsseed and -connect throws an error.")47# -connect soft sets -dnsseed to false, so throws the same error48self.nodes[0].stop_node()49self.nodes[0].assert_start_raises_init_error(50expected_msg="Error: Cannot set -forcednsseed to true when setting -dnsseed to false.",51extra_args=["-forcednsseed=1", f"-connect={fakeaddr}"],52)54# Restore default bitcoind settings55self.restart_node(0)57def existing_outbound_connections_test(self):58# Make sure addrman is populated to enter the conditional where we59# delay and potentially skip DNS seeding.60self.nodes[0].addpeeraddress("192.0.0.8", 8333)62self.log.info("Check that we *do not* query DNS seeds if we have 2 outbound connections")64self.restart_node(0)65with self.nodes[0].assert_debug_log(expected_msgs=["P2P peers available. Skipped DNS seeding."], timeout=12):66for i in range(2):67self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i, connection_type="outbound-full-relay")69def existing_block_relay_connections_test(self):70# Make sure addrman is populated to enter the conditional where we71# delay and potentially skip DNS seeding. No-op when run after72# existing_outbound_connections_test.73self.nodes[0].addpeeraddress("192.0.0.8", 8333)75self.log.info("Check that we *do* query DNS seeds if we only have 2 block-relay-only connections")77self.restart_node(0)78with self.nodes[0].assert_debug_log(expected_msgs=["Loading addresses from DNS seed"], timeout=12):79# This mimics the "anchors" logic where nodes are likely to80# reconnect to block-relay-only connections on startup.81# Since we do not participate in addr relay with these connections,82# we still want to query the DNS seeds.83for i in range(2):84self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=i, connection_type="block-relay-only")86def force_dns_test(self):87self.log.info("Check that we query DNS seeds if -forcednsseed param is set")89with self.nodes[0].assert_debug_log(expected_msgs=["Loading addresses from DNS seed"], timeout=12):90# -dnsseed defaults to 1 in bitcoind, but 0 in the test framework,91# so pass it explicitly here92self.restart_node(0, ["-forcednsseed", "-dnsseed=1", UNREACHABLE_PROXY_ARG])94# Restore default for subsequent tests95self.restart_node(0)97def wait_time_tests(self):98self.log.info("Check the delay before querying DNS seeds")100# Populate addrman with < 1000 addresses101for i in range(5):102a = f"192.0.0.{i}"103self.nodes[0].addpeeraddress(a, 8333)105# The delay should be 11 seconds106with self.nodes[0].assert_debug_log(expected_msgs=["Waiting 11 seconds before querying DNS seeds.\n"], timeout=2):107self.restart_node(0)109# Populate addrman with > 1000 addresses110for i in itertools.count():111first_octet = i % 2 + 1112second_octet = i % 256113third_octet = i % 100114a = f"{first_octet}.{second_octet}.{third_octet}.1"115self.nodes[0].addpeeraddress(a, 8333)116if (i > 1000 and i % 100 == 0):117# The addrman size is non-deterministic because new addresses118# are sorted into buckets, potentially displacing existing119# addresses. Periodically check if we have met the desired120# threshold.121if len(self.nodes[0].getnodeaddresses(0)) > 1000:122break124# The delay should be 5 mins125with self.nodes[0].assert_debug_log(expected_msgs=["Waiting 300 seconds before querying DNS seeds.\n"], timeout=2):126self.restart_node(0)129if __name__ == '__main__':130P2PDNSSeeds(__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.