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 per-peer message capture capability.7Additionally, the output of contrib/message-capture/message-capture-parser.py should be verified manually.8"""10import glob11from io import BytesIO12import os14from test_framework.p2p import P2PDataStore, MESSAGEMAP15from test_framework.test_framework import BitcoinTestFramework16from test_framework.util import assert_equal18TIME_SIZE = 819LENGTH_SIZE = 420MSGTYPE_SIZE = 1222def mini_parser(dat_file: str) -> None:23"""Parse a data file created by CaptureMessageToFile.25From the data file we'll only check the structure.27We won't care about things like:28- Deserializing the payload of the message29- This is managed by the deserialize methods in test_framework.messages30- The order of the messages31- There's no reason why we can't, say, change the order of the messages in the handshake32- Message Type33- We can add new message types35We're ignoring these because they're simply too brittle to test here.36"""37with open(dat_file, 'rb') as f_in:38# This should have at least one message in it39assert os.fstat(f_in.fileno()).st_size >= TIME_SIZE + LENGTH_SIZE + MSGTYPE_SIZE40while True:41tmp_header_raw = f_in.read(TIME_SIZE + LENGTH_SIZE + MSGTYPE_SIZE)42if not tmp_header_raw:43break44tmp_header = BytesIO(tmp_header_raw)45tmp_header.read(TIME_SIZE) # skip the timestamp field46msgtype = tmp_header.read(MSGTYPE_SIZE).rstrip(b'\x00')47assert msgtype in MESSAGEMAP48length: int = int.from_bytes(tmp_header.read(LENGTH_SIZE), "little")49data = f_in.read(length)50assert_equal(len(data), length)54class MessageCaptureTest(BitcoinTestFramework):55def set_test_params(self):56self.num_nodes = 157self.extra_args = [["-capturemessages"]]58self.setup_clean_chain = True60def run_test(self):61capturedir = self.nodes[0].chain_path / "message_capture"62# Connect a node so that the handshake occurs63self.nodes[0].add_p2p_connection(P2PDataStore())64self.nodes[0].disconnect_p2ps()65recv_file = glob.glob(os.path.join(capturedir, "*/msgs_recv.dat"))[0]66mini_parser(recv_file)67sent_file = glob.glob(os.path.join(capturedir, "*/msgs_sent.dat"))[0]68mini_parser(sent_file)71if __name__ == '__main__':72MessageCaptureTest(__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.