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) 2022-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 -discover command."""7import socket9from test_framework.test_framework import BitcoinTestFramework10from test_framework.util import assert_equal13def is_valid_ipv4_address(address):14try:15socket.inet_aton(address)16except socket.error:17return False18return True21def is_valid_ipv6_address(address):22try:23socket.inet_pton(socket.AF_INET6, address)24except socket.error:25return False26return True29class DiscoverTest(BitcoinTestFramework):30def set_test_params(self):31self.setup_clean_chain = True32self.bind_to_localhost_only = False33self.num_nodes = 135def validate_addresses(self, addresses_obj):36for address_obj in addresses_obj:37address = address_obj['address']38self.log.info(f"Validating {address}")39valid = (is_valid_ipv4_address(address)40or is_valid_ipv6_address(address))41assert_equal(valid, True)43def test_local_addresses(self, test_case, *, expect_empty=False):44self.log.info(f"Restart node with {test_case}")45self.restart_node(0, test_case)46network_info = self.nodes[0].getnetworkinfo()47network_enabled = [n for n in network_info['networks']48if n['reachable'] and n['name'] in ['ipv4', 'ipv6']]49local_addrs = list(network_info["localaddresses"])50if expect_empty or not network_enabled:51assert_equal(local_addrs, [])52elif len(local_addrs) > 0:53self.validate_addresses(local_addrs)55def run_test(self):56test_cases = [57["-listen", "-discover"],58["-discover"],59]61test_cases_empty = [62["-discover=0"],63["-listen", "-discover=0"],64[],65]67for test_case in test_cases:68self.test_local_addresses(test_case, expect_empty=False)70for test_case in test_cases_empty:71self.test_local_addresses(test_case, expect_empty=True)74if __name__ == '__main__':75DiscoverTest(__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.