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) 2024-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 the -port option and its interactions with7-bind.8"""10from test_framework.test_framework import (11BitcoinTestFramework,12)13from test_framework.util import (14p2p_port,15)18class PortTest(BitcoinTestFramework):19def set_test_params(self):20self.setup_clean_chain = True21# Avoid any -bind= on the command line.22self.bind_to_localhost_only = False23self.num_nodes = 125def run_test(self):26node = self.nodes[0]27node.has_explicit_bind = True28port1 = p2p_port(self.num_nodes)29port2 = p2p_port(self.num_nodes + 5)31self.log.info("When starting with -port, bitcoind binds to it and uses port + 1 for an onion bind")32with node.assert_debug_log(expected_msgs=[f'Bound to 0.0.0.0:{port1}', f'Bound to 127.0.0.1:{port1 + 1}']):33self.restart_node(0, extra_args=["-listen", f"-port={port1}"])35self.log.info("When specifying -port multiple times, only the last one is taken")36with node.assert_debug_log(expected_msgs=[f'Bound to 0.0.0.0:{port2}', f'Bound to 127.0.0.1:{port2 + 1}'], unexpected_msgs=[f'Bound to 0.0.0.0:{port1}']):37self.restart_node(0, extra_args=["-listen", f"-port={port1}", f"-port={port2}"])39self.log.info("When specifying ports with both -port and -bind, the one from -port is ignored")40with node.assert_debug_log(expected_msgs=[f'Bound to 0.0.0.0:{port2}'], unexpected_msgs=[f'Bound to 0.0.0.0:{port1}']):41self.restart_node(0, extra_args=["-listen", f"-port={port1}", f"-bind=0.0.0.0:{port2}"])43self.log.info("When -bind specifies no port, the values from -port and -bind are combined")44with self.nodes[0].assert_debug_log(expected_msgs=[f'Bound to 0.0.0.0:{port1}']):45self.restart_node(0, extra_args=["-listen", f"-port={port1}", "-bind=0.0.0.0"])47self.log.info("When an onion bind specifies no port, the value from -port, incremented by 1, is taken")48with self.nodes[0].assert_debug_log(expected_msgs=[f'Bound to 127.0.0.1:{port1 + 1}']):49self.restart_node(0, extra_args=["-listen", f"-port={port1}", "-bind=127.0.0.1=onion"])51self.log.info("Invalid values for -port raise errors")52self.stop_node(0)53node.extra_args = ["-listen", "-port=65536"]54node.assert_start_raises_init_error(expected_msg="Error: Invalid port specified in -port: '65536'")55node.extra_args = ["-listen", "-port=0"]56node.assert_start_raises_init_error(expected_msg="Error: Invalid port specified in -port: '0'")59if __name__ == '__main__':60PortTest(__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.