Crates.io | brk_parser |
lib.rs | brk_parser |
version | 0.0.95 |
created_at | 2025-02-23 23:28:21.613168+00 |
updated_at | 2025-08-28 10:45:36.033291+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 | 62,134 |
High-performance Bitcoin block parser for raw Bitcoin Core block files
brk_parser
provides efficient sequential access to Bitcoin Core's raw block files (blkXXXXX.dat
), delivering blocks in height order with automatic fork filtering and XOR encryption support. Built for blockchain analysis and indexing applications that need complete Bitcoin data access.
use brk_parser::Parser;
use brk_structs::Height;
use bitcoincore_rpc::{Auth, Client};
// Setup RPC client (must have static lifetime)
let rpc = Box::leak(Box::new(Client::new(
"http://localhost:8332",
Auth::CookieFile(Path::new("~/.bitcoin/.cookie")),
)?));
// Create parser
let parser = Parser::new(
Path::new("~/.bitcoin/blocks").to_path_buf(),
Path::new("./output").to_path_buf(),
rpc,
);
// Parse all blocks sequentially
parser.parse(None, None)
.iter()
.for_each(|(height, block, hash)| {
println!("Block {}: {} ({} txs)", height, hash, block.txdata.len());
});
// Parse specific height range
let start = Some(Height::new(800_000));
let end = Some(Height::new(800_100));
parser.parse(start, end)
.iter()
.for_each(|(height, block, hash)| {
// Process blocks 800,000 to 800,100
});
// Get single block by height
let genesis = parser.get(Height::new(0));
println!("Genesis has {} transactions", genesis.txdata.len());
use brk_parser::Parser;
use bitcoin::Block;
fn analyze_blockchain(parser: &Parser) {
let mut total_transactions = 0;
let mut total_outputs = 0;
parser.parse(None, None)
.iter()
.for_each(|(height, block, _hash)| {
total_transactions += block.txdata.len();
total_outputs += block.txdata.iter()
.map(|tx| tx.output.len())
.sum::<usize>();
if height.0 % 10000 == 0 {
println!("Processed {} blocks", height);
}
});
println!("Total transactions: {}", total_transactions);
println!("Total outputs: {}", total_outputs);
}
The parser returns tuples for each block:
Height
: Block height (sequential: 0, 1, 2, ...)Block
: Complete block data from the bitcoin
crateBlockHash
: Block's cryptographic hashBenchmarked on MacBook Pro M3 Pro:
blocks/
directoryThe parser saves parsing state in {output_dir}/blk_index_to_blk_recap.json
containing:
Note: Only one parser instance should run at a time as the state file doesn't support concurrent access.
bitcoin
- Bitcoin protocol types and block parsingbitcoincore_rpc
- RPC communication with Bitcoin Corecrossbeam
- Multi-producer, multi-consumer channelsrayon
- Data parallelism for block decodingserde
- State serialization and persistenceThis README was generated by Claude Code