brk_parser
High-performance Bitcoin block parser for raw Bitcoin Core block files with XOR encryption support.
Overview
This crate provides a multi-threaded Bitcoin block parser that processes raw Bitcoin Core .dat
files from the blockchain directory. It supports XOR-encoded block data, parallel processing with rayon
, and maintains chronological ordering through crossbeam channels. The parser integrates with Bitcoin Core RPC to validate block confirmations and handles file metadata tracking for incremental processing.
Key Features:
- Multi-threaded pipeline architecture with crossbeam channels
- XOR decryption support for encrypted block files
- Parallel block decoding with rayon thread pools
- Chronological block ordering with height-based validation
- Bitcoin Core RPC integration for confirmation checking
- File metadata tracking and incremental processing
- Magic byte detection for block boundary identification
Target Use Cases:
- Bitcoin blockchain analysis tools requiring raw block access
- Historical data processing applications
- Block explorers and analytics platforms
- Research tools needing ordered block iteration
Installation
Quick Start
use Parser;
use ;
use Height;
use PathBuf;
// Initialize Bitcoin Core RPC client
let rpc = Box leak;
// Create parser with blocks directory
let blocks_dir = from;
let outputs_dir = Some;
let parser = new;
// Parse blocks in height range
let start_height = Some;
let end_height = Some;
let receiver = parser.parse;
// Process blocks as they arrive
for in receiver.iter
API Overview
Core Types
Parser
: Main parser coordinating multi-threaded block processingAnyBlock
: Enum representing different block states (Raw, Decoded, Skipped)XORBytes
: XOR key bytes for decrypting block dataXORIndex
: Circular index for XOR byte applicationBlkMetadata
: Block file metadata including index and modification time
Key Methods
Parser::new(blocks_dir: PathBuf, outputs_dir: Option<PathBuf>, rpc: &'static Client) -> Self
Creates a new parser instance with blockchain directory and RPC client.
parse(&self, start: Option<Height>, end: Option<Height>) -> Receiver<(Height, Block, BlockHash)>
Returns a channel receiver that yields blocks in chronological order for the specified height range.
Processing Pipeline
The parser implements a three-stage pipeline:
- File Reading Stage: Scans
.dat
files, identifies magic bytes, extracts raw block data - Decoding Stage: Parallel XOR decryption and Bitcoin block deserialization
- Ordering Stage: RPC validation and chronological ordering by block height
Examples
Basic Block Iteration
use Parser;
let parser = new;
// Parse all blocks from height 650000 onwards
let receiver = parser.parse;
for in receiver.iter
Range-Based Processing
use Parser;
let parser = new;
// Process specific block range
let start = new;
let end = new;
let receiver = parser.parse;
let mut total_tx_count = 0;
for in receiver.iter
println!;
Incremental Processing with Metadata
use Parser;
let parser = new;
// Parser automatically handles file metadata tracking
// Only processes blocks that have been modified since last run
let receiver = parser.parse; // Process all available blocks
for in receiver.iter
Architecture
Multi-Threading Design
The parser uses a sophisticated multi-threaded architecture:
- File Scanner Thread: Reads raw bytes from
.dat
files and identifies block boundaries - Decoder Thread Pool: Parallel XOR decryption and block deserialization using rayon
- Ordering Thread: RPC validation and chronological ordering with future block buffering
XOR Encryption Support
Bitcoin Core optionally XOR-encrypts block files using an 8-byte key stored in xor.dat
. The parser:
- Automatically detects XOR encryption presence
- Implements circular XOR index for efficient decryption
- Supports both encrypted and unencrypted block files
Block File Management
The parser handles Bitcoin Core's block file structure:
- Scans directory for
blk*.dat
files - Tracks file modification times for incremental processing
- Maintains block height mappings with RPC validation
- Exports processing metadata for resumable operations
Code Analysis Summary
Main Type: Parser
struct coordinating multi-threaded block processing pipeline
Threading: Three-stage pipeline using crossbeam channels with bounded capacity (50)
Parallelization: rayon-based parallel block decoding with configurable batch sizes
XOR Handling: Custom XORBytes and XORIndex types for efficient encryption/decryption
RPC Integration: Bitcoin Core RPC validation for block confirmation and height mapping
File Processing: Automatic .dat
file discovery and magic byte boundary detection
Architecture: Producer-consumer pattern with ordered delivery despite parallel processing
This README was generated by Claude Code