fluxd icon indicating copy to clipboard operation
fluxd copied to clipboard

Compact Headers & Compact Blocks - BIP 152 Implementation

Open blondfrogs opened this issue 2 months ago • 0 comments

Compact Headers & Compact Blocks - BIP 152 Implementation

Implements BIP 152 style bandwidth optimization for both header sync and block relay.

Features

  1. Compact Headers (cmpheaders):
  • New message for efficient header sync of checkpointed blocks
  • Omits Equihash nSolution (~1344 bytes) from POW block headers
  • ~80% bandwidth reduction (344KB vs 1.7MB per 2000 headers)
  • PON blocks send full data (already compact)
  1. Compact Blocks (cmpctblock - BIP 152):
  • Send blocks with short transaction IDs instead of full transactions
  • Reconstruct blocks from mempool + short IDs
  • Request missing transactions via getblocktxn/blocktxn messages
  • High-bandwidth mode for low-latency block relay
  • Ring buffer of 100 recent transactions for better reconstruction
  1. Headers-First Sync:
  • Download all headers before requesting blocks
  • Multi-peer parallel block downloads after header sync
  • CanDirectFetch() gates block downloads until synced
  • Periodic disk flush (every 250k headers) prevents data loss

Network Protocol

  • Protocol version 170021 (SENDCMPCT_VERSION/CMPHEADERS_VERSION)
  • Messages: cmpheaders, sendcmpct, cmpctblock, getblocktxn, blocktxn
  • Checkpoint validation for security (compact headers only below checkpoint)
  • Initialize pindexBestKnownBlock from peer nStartingHeight for multi-peer sync

Database

  • Stores actual block hash for compact headers (can't compute without nSolution)
  • Backwards compatible serialization
  • Old nodes ↔ new nodes: fully compatible

Implementation

New files:

  • blockencodings.cpp/h - BIP 152 compact block structures
  • crypto/siphash.cpp/h - Short transaction ID generation

Modified:

  • main.cpp - Protocol handlers, sync logic
  • chain.h - CDiskBlockIndex with hashBlock storage
  • version.h, main.h, net.cpp/h, and 6 others

Testing

  • ✅ Synced to 2M+ blocks at ~45 blocks/sec
  • ✅ Backwards compatible with protocol < 170021
  • ✅ Database compatibility verified

blondfrogs avatar Oct 31 '25 17:10 blondfrogs