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 that the proper port is used for -externalip=7"""9import os11from test_framework.test_framework import BitcoinTestFramework, SkipTest12from test_framework.test_node import (13FailedToStartError,14)15from test_framework.util import assert_equal, p2p_port17# We need to bind to a routable address for this test to exercise the relevant18# code. Those addresses must be on an interface that is UP and is not a loopback19# interface (IFF_LOOPBACK).20# To set these routable addresses on the machine, use:21# Linux:22# First find your interfaces: ip addr show23# Then use your actual interface names (replace INTERFACE_NAME with yours):24# ip addr add 1.1.1.5/32 dev INTERFACE_NAME # to set up25# ip addr del 1.1.1.5/32 dev INTERFACE_NAME # to remove it26# FreeBSD and MacOS:27# ifconfig INTERFACE_NAME 1.1.1.5/32 alias # to set up28# ifconfig INTERFACE_NAME 1.1.1.5 -alias # to remove it, after the test29ADDR = os.getenv("BIND_TEST_ROUTABLE_IPV4", "1.1.1.5")31# array of tuples [arguments, expected port in localaddresses]32EXPECTED = [33[['-externalip=2.2.2.2', '-port=30001'], 30001],34[['-externalip=2.2.2.2', '-port=30002', f'-bind={ADDR}'], 30002],35[['-externalip=2.2.2.2', f'-bind={ADDR}'], 'default_p2p_port'],36[['-externalip=2.2.2.2', '-port=30003', f'-bind={ADDR}:30004'], 30004],37[['-externalip=2.2.2.2', f'-bind={ADDR}:30005'], 30005],38[['-externalip=2.2.2.2:30006', '-port=30007'], 30006],39[['-externalip=2.2.2.2:30008', '-port=30009', f'-bind={ADDR}'], 30008],40[['-externalip=2.2.2.2:30010', f'-bind={ADDR}'], 30010],41[['-externalip=2.2.2.2:30011', '-port=30012', f'-bind={ADDR}:30013'], 30011],42[['-externalip=2.2.2.2:30014', f'-bind={ADDR}:30015'], 30014],43[['-externalip=2.2.2.2', '-port=30016', f'-bind={ADDR}:30017',44f'-whitebind={ADDR}:30018'], 30017],45[['-externalip=2.2.2.2', '-port=30019',46f'-whitebind={ADDR}:30020'], 30020],47]49class BindPortExternalIPTest(BitcoinTestFramework):50def set_test_params(self):51# Avoid any -bind= on the command line. Force the framework to avoid adding -bind=127.0.0.1.52self.setup_clean_chain = True53self.bind_to_localhost_only = False54self.num_nodes = len(EXPECTED)55self.extra_args = list(map(lambda e: e[0], EXPECTED))57def setup_network(self):58self.setup_nodes()60def setup_nodes(self):61self.add_nodes(self.num_nodes, self.extra_args)62# TestNode.start() will add -bind= to extra_args if has_explicit_bind is63# False. We do not want any -bind= thus set has_explicit_bind to True.64for node in self.nodes:65node.has_explicit_bind = True66try:67self.start_nodes()68except FailedToStartError as e:69self.cleanup_partially_started_nodes()70if 'Unable to bind to ' in str(e):71raise SkipTest(72"To run this test, set BIND_TEST_ROUTABLE_IPV4 to an address "73f"assigned to a non-loopback interface on this machine (currently {ADDR}).")74raise76def run_test(self):77self.log.info("Test the proper port is used for -externalip=")78for i in range(len(EXPECTED)):79expected_port = EXPECTED[i][1]80if expected_port == 'default_p2p_port':81expected_port = p2p_port(i)82found = False83for local in self.nodes[i].getnetworkinfo()['localaddresses']:84if local['address'] == '2.2.2.2':85assert_equal(local['port'], expected_port)86found = True87break88assert found90if __name__ == '__main__':91BindPortExternalIPTest(__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.