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"""Test ping message6"""8import time10from test_framework.messages import (11msg_pong,12msg_generic,13)14from test_framework.p2p import P2PInterface15from test_framework.test_framework import BitcoinTestFramework16from test_framework.util import (17assert_equal,18assert_not_equal,19)22PING_INTERVAL = 2 * 6023TIMEOUT_INTERVAL = 20 * 6026class NodeNoPong(P2PInterface):27def on_ping(self, message):28pass31class PingPongTest(BitcoinTestFramework):32def set_test_params(self):33self.setup_clean_chain = True34self.num_nodes = 135# Set the peer connection timeout low. It does not matter for this36# test, as long as it is less than TIMEOUT_INTERVAL.37self.extra_args = [['-peertimeout=1']]39def check_peer_info(self, *, pingtime, minping, pingwait):40stats = self.nodes[0].getpeerinfo()[0]41assert_equal(stats.pop('pingtime', None), pingtime)42assert_equal(stats.pop('minping', None), minping)43assert_equal(stats.pop('pingwait', None), pingwait)45def mock_forward(self, delta):46self.mock_time += delta47self.nodes[0].setmocktime(self.mock_time)49def run_test(self):50self.mock_time = int(time.time())51self.mock_forward(0)53self.log.info('Check that ping is sent after connection is established')54no_pong_node = self.nodes[0].add_p2p_connection(NodeNoPong())55self.mock_forward(3)56assert_not_equal(no_pong_node.last_message.pop('ping').nonce, 0)57self.check_peer_info(pingtime=None, minping=None, pingwait=3)59self.log.info('Reply without nonce cancels ping')60with self.nodes[0].assert_debug_log(['pong peer=0: Short payload']):61no_pong_node.send_and_ping(msg_generic(b"pong", b""))62self.check_peer_info(pingtime=None, minping=None, pingwait=None)64self.log.info('Reply without ping')65with self.nodes[0].assert_debug_log([66'pong peer=0: Unsolicited pong without ping, 0 expected, 0 received, 8 bytes',67]):68no_pong_node.send_and_ping(msg_pong())69self.check_peer_info(pingtime=None, minping=None, pingwait=None)71self.log.info('Reply with wrong nonce does not cancel ping')72assert 'ping' not in no_pong_node.last_message73with self.nodes[0].assert_debug_log(['pong peer=0: Nonce mismatch']):74# mock time PING_INTERVAL ahead to trigger node into sending a ping75self.mock_forward(PING_INTERVAL + 1)76no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)77self.mock_forward(9)78# Send the wrong pong79no_pong_node.send_and_ping(msg_pong(no_pong_node.last_message.pop('ping').nonce - 1))80self.check_peer_info(pingtime=None, minping=None, pingwait=9)82self.log.info('Reply with zero nonce does cancel ping')83with self.nodes[0].assert_debug_log(['pong peer=0: Nonce zero']):84no_pong_node.send_and_ping(msg_pong(0))85self.check_peer_info(pingtime=None, minping=None, pingwait=None)87self.log.info('Check that ping is properly reported on RPC')88assert 'ping' not in no_pong_node.last_message89# mock time PING_INTERVAL ahead to trigger node into sending a ping90self.mock_forward(PING_INTERVAL + 1)91no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)92ping_delay = 2993self.mock_forward(ping_delay)94no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)95no_pong_node.send_and_ping(msg_pong(no_pong_node.last_message.pop('ping').nonce))96self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)98self.log.info('Check that minping is decreased after a fast roundtrip')99# mock time PING_INTERVAL ahead to trigger node into sending a ping100self.mock_forward(PING_INTERVAL + 1)101no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)102ping_delay = 9103self.mock_forward(ping_delay)104no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)105no_pong_node.send_and_ping(msg_pong(no_pong_node.last_message.pop('ping').nonce))106self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)108self.log.info('Check that peer is disconnected after ping timeout')109assert 'ping' not in no_pong_node.last_message110self.nodes[0].ping()111no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)112with self.nodes[0].assert_debug_log(['ping timeout: 1201.000000s']):113self.mock_forward(TIMEOUT_INTERVAL // 2)114# Check that sending a ping does not prevent the disconnect115no_pong_node.sync_with_ping()116self.mock_forward(TIMEOUT_INTERVAL // 2 + 1)117no_pong_node.wait_for_disconnect()120if __name__ == '__main__':121PingPongTest(__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.