Skip to main content

Module wire

Module wire 

Source
Expand description

Bitcoin P2P wire format serialization

Implements Bitcoin P2P protocol message framing: Format: [magic:4][command:12][length:4][checksum:4][payload:var]

  • Magic bytes: Network identifier (mainnet, testnet, etc.)
  • Command: 12-byte ASCII string (null-padded)
  • Length: Payload length in bytes (little-endian u32)
  • Checksum: First 4 bytes of double SHA256 of payload
  • Payload: Message-specific data

Constants§

MAX_MESSAGE_PAYLOAD
Maximum message payload size (32 MB)
MESSAGE_HEADER_SIZE
Bitcoin P2P message header size (magic + command + length + checksum)

Functions§

calculate_checksum
Calculate checksum for message payload (first 4 bytes of double SHA256)
deserialize_addr
deserialize_addrv2
Deserialize AddrV2Message from Bitcoin wire format (BIP155)
deserialize_block
deserialize_blocktxn
deserialize_cmpctblock
deserialize_feefilter
deserialize_getblocks
deserialize_getblocktxn
deserialize_getdata
Deserialize GetDataMessage from Bitcoin wire format
deserialize_getheaders
Deserialize GetHeadersMessage from Bitcoin wire format
deserialize_headers
Deserialize HeadersMessage from Bitcoin wire format
deserialize_inv
Deserialize InvMessage from Bitcoin wire format
deserialize_message
Deserialize Bitcoin P2P wire format to network message
deserialize_notfound
Deserialize NotFoundMessage from Bitcoin wire format.
deserialize_ping
Deserialize PingMessage from Bitcoin wire format
deserialize_pong
Deserialize PongMessage from Bitcoin wire format
deserialize_reject
deserialize_sendcmpct
deserialize_tx
deserialize_version
Deserialize VersionMessage from Bitcoin wire format
serialize_addr
Serialize AddrMessage to Bitcoin wire format (legacy addr) Format: CompactSize(count) + [timestamp(4) + services(8) + addr(16) + port(2)]*
serialize_addrv2
Serialize AddrV2Message to Bitcoin wire format (BIP155) Format: CompactSize(count) + [time(4) + services(8) + address_type(1) + address(var) + port(2)]*
serialize_block
serialize_blocktxn
BIP152: blocktxn - block_hash(32) + count(varint) + transactions
serialize_cmpctblock
BIP152: cmpctblock - header(80) + nonce(8) + shortids(varint+6*count) + prefilled(varint+each: diff_index+tx)
serialize_feefilter
Serialize FeeFilterMessage per BIP133: 8-byte feerate (LE)
serialize_getblocks
Serialize GetBlocksMessage - same structure as GetHeaders (version + locator + hash_stop)
serialize_getblocktxn
BIP152: getblocktxn - block_hash(32) + indexes(varint count + diff-encoded varints)
serialize_getdata
Serialize GetDataMessage to Bitcoin wire format Format: identical to Inv - count (varint) + count * (type u32 LE + hash 32 bytes)
serialize_getheaders
Serialize GetHeadersMessage to Bitcoin wire format Format: version (4 bytes LE) + hash_count (varint) + hashes (32 bytes each) + hash_stop (32 bytes)
serialize_headers
Serialize HeadersMessage to Bitcoin wire format Format: count (varint) + headers (each: 80 bytes header + varint tx_count which is always 0)
serialize_inv
Serialize InvMessage to Bitcoin wire format Format: count (varint) + count * (type u32 LE + hash 32 bytes)
serialize_message
Serialize a network message to Bitcoin P2P wire format
serialize_notfound
Serialize NotFoundMessage to Bitcoin wire format. Format: identical to Inv/GetData - count (varint) + count * (type u32 LE + hash 32 bytes)
serialize_ping
Serialize PingMessage to Bitcoin wire format (8-byte nonce)
serialize_pong
Serialize PongMessage to Bitcoin wire format (8-byte nonce)
serialize_reject
Serialize RejectMessage per BIP61: message(12) + ccode(1) + reason(var) + data(32 optional)
serialize_sendcmpct
BIP152: sendcmpct - 1 byte prefer_cmpct + 8 bytes version (LE)
serialize_tx
serialize_version
Serialize VersionMessage to Bitcoin wire format Format per Bitcoin protocol specification: