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) 2016-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 various net timeouts.7- Create three peers:9no_verack_node - we never send a verack in response to their version10no_version_node - we never send a version (only a ping)11no_send_node - we never send any P2P message.13- Wait 1 second14- Assert that we're connected15- Send a ping to no_verack_node and no_version_node16- Wait 1 second17- Assert that we're still connected18- Send a ping to no_verack_node and no_version_node19- Wait 2 seconds20- Assert that we're no longer connected (timeout to receive version/verack is 3 seconds)21"""23from test_framework.messages import msg_ping24from test_framework.p2p import P2PInterface25from test_framework.test_framework import BitcoinTestFramework27import time30class TestP2PConn(P2PInterface):31def on_version(self, message):32# Don't send a verack in response33pass36class TimeoutsTest(BitcoinTestFramework):37def set_test_params(self):38self.setup_clean_chain = True39self.num_nodes = 140# set timeout to receive version/verack to 3 seconds41self.extra_args = [["-peertimeout=3"]]43def mock_forward(self, delta):44self.mock_time += delta45self.nodes[0].setmocktime(self.mock_time)47def run_test(self):48self.mock_time = int(time.time())49self.mock_forward(0)51# Setup the p2p connections, making sure the connections are established before the mocktime is bumped52with self.nodes[0].assert_debug_log(['Added connection peer=0'], timeout=2):53no_verack_node = self.nodes[0].add_p2p_connection(TestP2PConn(), wait_for_verack=False)54with self.nodes[0].assert_debug_log(['Added connection peer=1'], timeout=2):55no_version_node = self.nodes[0].add_p2p_connection(TestP2PConn(), send_version=False, wait_for_verack=False)56with self.nodes[0].assert_debug_log(['Added connection peer=2'], timeout=2):57no_send_node = self.nodes[0].add_p2p_connection(TestP2PConn(), send_version=False, wait_for_verack=False)59# Wait until we got the verack in response to the version. Though, don't wait for the other node to receive the60# verack, since we never sent one61no_verack_node.wait_for_verack()63self.mock_forward(1)65assert no_verack_node.is_connected66assert no_version_node.is_connected67assert no_send_node.is_connected69with self.nodes[0].assert_debug_log(['Unsupported message "ping" prior to verack from peer=0'], timeout=2):70no_verack_node.send_without_ping(msg_ping())72with self.nodes[0].assert_debug_log(['non-version message before version handshake. Message "ping" from peer=1'], timeout=2):73no_version_node.send_without_ping(msg_ping())75self.mock_forward(1)76assert "version" in no_verack_node.last_message78assert no_verack_node.is_connected79assert no_version_node.is_connected80assert no_send_node.is_connected82no_verack_node.send_without_ping(msg_ping())83no_version_node.send_without_ping(msg_ping())85if self.options.v2transport:86expected_timeout_logs = [87"version handshake timeout, disconnecting peer=0",88"version handshake timeout, disconnecting peer=1",89"version handshake timeout, disconnecting peer=2",90]91else:92expected_timeout_logs = [93"version handshake timeout, disconnecting peer=0",94"socket no message in first 3 seconds, never sent to peer, disconnecting peer=1",95"socket no message in first 3 seconds, never received from peer, never sent to peer, disconnecting peer=2",96]98with self.nodes[0].assert_debug_log(expected_msgs=expected_timeout_logs):99self.mock_forward(2)100no_verack_node.wait_for_disconnect(timeout=1)101no_version_node.wait_for_disconnect(timeout=1)102no_send_node.wait_for_disconnect(timeout=1)104self.stop_nodes(0)105self.nodes[0].assert_start_raises_init_error(106expected_msg='Error: peertimeout must be a positive integer.',107extra_args=['-peertimeout=0'],108)111if __name__ == '__main__':112TimeoutsTest(__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.