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) 2010 ArtForz -- public domain half-a-node3# Copyright (c) 2012 Jeff Garzik4# Copyright (c) 2010-present The Bitcoin Core developers5# Distributed under the MIT software license, see the accompanying6# file COPYING or http://www.opensource.org/licenses/mit-license.php.7"""Bitcoin test framework primitive and message structures9CBlock, CTransaction, CBlockHeader, CTxIn, CTxOut, etc....:10data structures that should map to corresponding structures in11bitcoin/primitives13msg_block, msg_tx, msg_headers, etc.:14data structures that represent network messages16ser_*, deser_*: functions that handle serialization/deserialization.18Classes use __slots__ to ensure extraneous attributes aren't accidentally added19by tests, compromising their intended effect.20"""21from base64 import b32decode, b32encode22import copy23import hashlib24from io import BytesIO25import math26import random27import socket28import time29import unittest31from test_framework.crypto.siphash import siphash25632from test_framework.util import (33assert_equal,34assert_not_equal,35)37MAX_LOCATOR_SZ = 10138MAX_BLOCK_WEIGHT = 400000039MAX_BLOCK_SIGOPS_COST = 8000040DEFAULT_BLOCK_RESERVED_WEIGHT = 800041MINIMUM_BLOCK_RESERVED_WEIGHT = 200042MAX_BLOOM_FILTER_SIZE = 3600043MAX_BLOOM_HASH_FUNCS = 5045COIN = 100000000 # 1 btc in satoshis46MAX_MONEY = 21000000 * COIN48MAX_BIP125_RBF_SEQUENCE = 0xfffffffd # Sequence number that is rbf-opt-in (BIP 125) and csv-opt-out (BIP 68)49MAX_SEQUENCE_NONFINAL = 0xfffffffe # Sequence number that is csv-opt-out (BIP 68)50SEQUENCE_FINAL = 0xffffffff # Sequence number that disables nLockTime if set for every input of a tx52MAX_PROTOCOL_MESSAGE_LENGTH = 4000000 # Maximum length of incoming protocol messages53MAX_HEADERS_RESULTS = 2000 # Number of headers sent in one getheaders result54MAX_INV_SIZE = 50000 # Maximum number of entries in an 'inv' protocol message56NODE_NONE = 057NODE_NETWORK = (1 << 0)58NODE_BLOOM = (1 << 2)59NODE_WITNESS = (1 << 3)60NODE_COMPACT_FILTERS = (1 << 6)61NODE_NETWORK_LIMITED = (1 << 10)62NODE_P2P_V2 = (1 << 11)64MSG_TX = 165MSG_BLOCK = 266MSG_FILTERED_BLOCK = 367MSG_CMPCT_BLOCK = 468MSG_WTX = 569MSG_WITNESS_FLAG = 1 << 3070MSG_TYPE_MASK = 0xffffffff >> 271MSG_WITNESS_TX = MSG_TX | MSG_WITNESS_FLAG73FILTER_TYPE_BASIC = 075WITNESS_SCALE_FACTOR = 477DEFAULT_ANCESTOR_LIMIT = 25 # default max number of in-mempool ancestors78DEFAULT_DESCENDANT_LIMIT = 25 # default max number of in-mempool descendants79DEFAULT_CLUSTER_LIMIT = 64 # default max number of transactions in a cluster82# Default setting for -datacarriersize.83MAX_OP_RETURN_RELAY = 100_00086DEFAULT_MEMPOOL_EXPIRY_HOURS = 336 # hours88TX_MIN_STANDARD_VERSION = 189TX_MAX_STANDARD_VERSION = 391MAGIC_BYTES = {92"mainnet": b"\xf9\xbe\xb4\xd9",93"testnet4": b"\x1c\x16\x3f\x28",94"regtest": b"\xfa\xbf\xb5\xda",95"signet": b"\x0a\x03\xcf\x40",96}98def sha256(s):99return hashlib.sha256(s).digest()102def sha3(s):103return hashlib.sha3_256(s).digest()106def hash256(s):107return sha256(sha256(s))110def ser_compact_size(l):111r = b""112if l < 253:113r = l.to_bytes(1, "little")114elif l < 0x10000:115r = (253).to_bytes(1, "little") + l.to_bytes(2, "little")116elif l < 0x100000000:117r = (254).to_bytes(1, "little") + l.to_bytes(4, "little")118else:119r = (255).to_bytes(1, "little") + l.to_bytes(8, "little")120return r123def deser_compact_size(f):124nit = int.from_bytes(f.read(1), "little")125if nit == 253:126nit = int.from_bytes(f.read(2), "little")127elif nit == 254:128nit = int.from_bytes(f.read(4), "little")129elif nit == 255:130nit = int.from_bytes(f.read(8), "little")131return nit134def ser_varint(l):135r = b""136while True:137r = bytes([(l & 0x7f) | (0x80 if len(r) > 0 else 0x00)]) + r138if l <= 0x7f:139return r140l = (l >> 7) - 1143def deser_varint(f):144n = 0145while True:146dat = f.read(1)[0]147n = (n << 7) | (dat & 0x7f)148if (dat & 0x80) > 0:149n += 1150else:151return n154def deser_string(f):155nit = deser_compact_size(f)156return f.read(nit)159def ser_string(s):160return ser_compact_size(len(s)) + s163def deser_uint256(f):164return int.from_bytes(f.read(32), 'little')167def ser_uint256(u):168return u.to_bytes(32, 'little')171def uint256_from_str(s):172return int.from_bytes(s[:32], 'little')175def uint256_from_compact(c):176nbytes = (c >> 24) & 0xFF177v = (c & 0xFFFFFF) << (8 * (nbytes - 3))178return v181# deser_function_name: Allow for an alternate deserialization function on the182# entries in the vector.183def deser_vector(f, c, deser_function_name=None):184nit = deser_compact_size(f)185r = []186for _ in range(nit):187t = c()188if deser_function_name:189getattr(t, deser_function_name)(f)190else:191t.deserialize(f)192r.append(t)193return r196# ser_function_name: Allow for an alternate serialization function on the197# entries in the vector (we use this for serializing the vector of transactions198# for a witness block).199def ser_vector(l, ser_function_name=None):200r = ser_compact_size(len(l))201for i in l:202if ser_function_name:203r += getattr(i, ser_function_name)()204else:205r += i.serialize()206return r209def deser_uint256_vector(f):210nit = deser_compact_size(f)211r = []212for _ in range(nit):213t = deser_uint256(f)214r.append(t)215return r218def ser_uint256_vector(l):219r = ser_compact_size(len(l))220for i in l:221r += ser_uint256(i)222return r225def deser_string_vector(f):226nit = deser_compact_size(f)227r = []228for _ in range(nit):229t = deser_string(f)230r.append(t)231return r234def ser_string_vector(l):235r = ser_compact_size(len(l))236for sv in l:237r += ser_string(sv)238return r241def deser_block_spent_outputs(f):242nit = deser_compact_size(f)243return [deser_vector(f, CTxOut) for _ in range(nit)]246def from_hex(obj, hex_string):247"""Deserialize from a hex string representation (e.g. from RPC)249Note that there is no complementary helper like e.g. `to_hex` for the250inverse operation. To serialize a message object to a hex string, simply
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.