| Crates.io | brk_parser |
| lib.rs | brk_parser |
| version | 0.0.109 |
| created_at | 2025-02-23 23:28:21.613168+00 |
| updated_at | 2025-09-20 17:21:30.1967+00 |
| description | A very fast Bitcoin block parser and iterator built on top of bitcoin-rust |
| homepage | https://bitcoinresearchkit.org |
| repository | https://github.com/bitcoinresearchkit/brk |
| max_upload_size | |
| id | 1566722 |
| size | 73,689 |
High-performance Bitcoin block parser for raw Bitcoin Core block files with XOR encryption support.
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:
Target Use Cases:
cargo add brk_parser
use brk_parser::Parser;
use bitcoincore_rpc::{Client, Auth, RpcApi};
use brk_structs::Height;
use std::path::PathBuf;
// Initialize Bitcoin Core RPC client
let rpc = Box::leak(Box::new(Client::new(
"http://localhost:8332",
Auth::None
).unwrap()));
// Create parser with blocks directory
let blocks_dir = PathBuf::from("/path/to/bitcoin/blocks");
let outputs_dir = Some(PathBuf::from("./parser_output"));
let parser = Parser::new(blocks_dir, outputs_dir, rpc);
// Parse blocks in height range
let start_height = Some(Height::new(700000));
let end_height = Some(Height::new(700100));
let receiver = parser.parse(start_height, end_height);
// Process blocks as they arrive
for (height, block, block_hash) in receiver.iter() {
println!("Block {}: {} transactions", height, block.txdata.len());
println!("Block hash: {}", block_hash);
}
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 timeParser::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.
The parser implements a three-stage pipeline:
.dat files, identifies magic bytes, extracts raw block datause brk_parser::Parser;
let parser = Parser::new(blocks_dir, Some(output_dir), rpc);
// Parse all blocks from height 650000 onwards
let receiver = parser.parse(Some(Height::new(650000)), None);
for (height, block, hash) in receiver.iter() {
println!("Processing block {} with {} transactions",
height, block.txdata.len());
// Process block transactions
for (idx, tx) in block.txdata.iter().enumerate() {
println!(" Tx {}: {}", idx, tx.txid());
}
}
use brk_parser::Parser;
let parser = Parser::new(blocks_dir, Some(output_dir), rpc);
// Process specific block range
let start = Height::new(600000);
let end = Height::new(600999);
let receiver = parser.parse(Some(start), Some(end));
let mut total_tx_count = 0;
for (height, block, _hash) in receiver.iter() {
total_tx_count += block.txdata.len();
if height == end {
break; // End of range reached
}
}
println!("Processed 1000 blocks with {} total transactions", total_tx_count);
use brk_parser::Parser;
let parser = Parser::new(blocks_dir, Some(output_dir), rpc);
// Parser automatically handles file metadata tracking
// Only processes blocks that have been modified since last run
let receiver = parser.parse(None, None); // Process all available blocks
for (height, block, hash) in receiver.iter() {
// Parser ensures blocks are delivered in chronological order
// even when processing multiple .dat files in parallel
if height.as_u32() % 10000 == 0 {
println!("Reached block height {}", height);
}
}
The parser uses a sophisticated multi-threaded architecture:
.dat files and identifies block boundariesBitcoin Core optionally XOR-encrypts block files using an 8-byte key stored in xor.dat. The parser:
The parser handles Bitcoin Core's block file structure:
blk*.dat filesMain 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