Crates.io | brk_indexer |
lib.rs | brk_indexer |
version | 0.0.95 |
created_at | 2025-02-23 23:30:40.558838+00 |
updated_at | 2025-08-28 10:46:18.052161+00 |
description | A Bitcoin indexer built on top of brk_parser |
homepage | https://bitcoinresearchkit.org |
repository | https://github.com/bitcoinresearchkit/brk |
max_upload_size | |
id | 1566725 |
size | 143,805 |
High-performance Bitcoin blockchain indexer with dual storage architecture
brk_indexer
processes raw Bitcoin Core block data and creates efficient storage structures using both vectors (time-series) and key-value stores (lookups). It serves as the foundation of BRK's data pipeline, organizing all blockchain data into optimized formats for fast retrieval and analysis.
Vector Storage (time-series data):
Key-Value Storage (lookups):
Complete support for all Bitcoin address types:
use brk_indexer::Indexer;
use brk_parser::Parser;
use bitcoincore_rpc::{Auth, Client};
use vecdb::Exit;
// Setup Bitcoin Core RPC connection
let rpc = Box::leak(Box::new(Client::new(
"http://localhost:8332",
Auth::CookieFile(Path::new("~/.bitcoin/.cookie")),
)?));
// Create parser for Bitcoin Core block files
let parser = Parser::new(
Path::new("~/.bitcoin/blocks").to_path_buf(),
Path::new("./brk_data").to_path_buf(),
rpc
);
// Create indexer with forced import (resets if needed)
let mut indexer = Indexer::forced_import(Path::new("./brk_data"))?;
// Setup graceful shutdown handler
let exit = Exit::new();
exit.set_ctrlc_handler();
// Index the blockchain
let indexes = indexer.index(&parser, rpc, &exit, true)?;
println!("Indexed up to height: {}", indexes.height);
use std::time::{Duration, Instant};
use std::thread::sleep;
// Continuous indexing loop for real-time updates
loop {
let start_time = Instant::now();
// Index new blocks
let indexes = indexer.index(&parser, rpc, &exit, true)?;
println!("Indexed to height {} in {:?}",
indexes.height, start_time.elapsed());
// Check for exit signal
if exit.is_signaled() {
println!("Graceful shutdown requested");
break;
}
// Wait before next update cycle
sleep(Duration::from_secs(5 * 60));
}
// Access the underlying storage structures
let vecs = &indexer.vecs;
let stores = &indexer.stores;
// Get block hash at specific height
let block_hash = vecs.height_to_blockhash.get(Height::new(800_000))?;
// Look up transaction by prefix
let tx_prefix = TxidPrefix::from(&txid);
let tx_index = stores.txidprefix_to_txindex.get(&tx_prefix)?;
// Get address data
let address_hash = AddressBytesHash::from(&address_bytes);
let type_index = stores.addressbyteshash_to_anyaddressindex.get(&address_hash)?;
Benchmarked on MacBook Pro M3 Pro (36GB RAM):
The indexer creates this storage structure:
brk_data/
├── indexed/
│ ├── vecs/ # Vector storage
│ │ ├── height_to_* # Height-indexed data
│ │ ├── txindex_to_* # Transaction-indexed data
│ │ └── outputindex_to_* # Output-indexed data
│ └── stores/ # Key-value stores
│ ├── hash_lookups/ # Block/TX hash mappings
│ └── address_maps/ # Address type mappings
└── metadata/ # Versioning and state
The indexer maintains current indices during processing:
pub struct Indexes {
pub height: Height, // Current block height
pub txindex: TxIndex, // Current transaction index
pub inputindex: InputIndex, // Current input index
pub outputindex: OutputIndex, // Current output index
pub p2pkhaddressindex: P2PKHAddressIndex, // P2PKH address index
// ... indices for all address types
}
~/.bitcoin/blocks/
brk_parser
- Bitcoin block parsing and sequential accessbrk_store
- Key-value storage wrapper (fjall-based)vecdb
- Vector database for time-series storagebitcoin
- Bitcoin protocol types and parsingrayon
- Parallel processing frameworkbitcoincore_rpc
- Bitcoin Core RPC clientThis README was generated by Claude Code