Skip to main content

Crate brk_reader

Crate brk_reader 

Source
Expand description

§brk_reader

Streams Bitcoin blocks from Bitcoin Core’s raw blk*.dat files in chain order.

§Requirements

A running Bitcoin Core node with RPC access. The reader needs:

  • The blocks/ directory (for blk*.dat files)
  • RPC connection (to resolve block heights and filter orphan blocks)

§Quick Start

let bitcoin_dir = Client::default_bitcoin_path();
let client = Client::new(
    Client::default_url(),
    Auth::CookieFile(bitcoin_dir.join(".cookie")),
)?;
let reader = Reader::new(bitcoin_dir.join("blocks"), &client);

// Stream the entire chain
for block in reader.read(None, None) {
    println!("{}: {}", block.height(), block.hash());
}

// Or a specific range (inclusive)
for block in reader.read(Some(Height::new(800_000)), Some(Height::new(850_000))) {
    // ...
}

§What You Get

Each ReadBlock gives you access to:

FieldDescription
block.height()Block height
block.hash()Block hash
block.headerBlock header (timestamp, nonce, difficulty, …)
block.txdataAll transactions
block.coinbase_tag()Miner’s coinbase tag
block.metadata()Position in the blk file
block.tx_metadata()Per-transaction blk file positions

Reader is thread-safe and cheap to clone (Arc-backed).

§How It Works

Three-thread pipeline connected by bounded channels:

blk*.dat ──► File Reader ──► Parser Pool ──► Orderer ──► Receiver<ReadBlock>
              1 thread        up to 4         1 thread
  1. File reader binary-searches to the starting blk file, scans for magic bytes, segments raw blocks
  2. Parser pool XOR-decodes and deserializes blocks in parallel, skips out-of-range blocks via header timestamp, filters orphans via RPC
  3. Orderer buffers out-of-order arrivals, validates prev_blockhash continuity, emits blocks sequentially

Structs§

Reader
Bitcoin BLK file reader
ReaderInner
Receiver
The receiving side of a channel.
XORBytes
XORIndex

Constants§

XOR_LEN