Expand description
§brk_indexer
Parses and indexes the entire Bitcoin blockchain so you can look up any block, transaction, input, output, or address by index in O(1).
§How It’s Organized
Every entity gets a sequential index in blockchain order:
- Block 0, 1, 2, … → height
- Transaction 0, 1, 2, … → txindex
- Input 0, 1, 2, … → txinindex
- Output 0, 1, 2, … → txoutindex
- Address 0, 1, 2, … → addressindex (per address type)
Data is stored in append-only vectors keyed by these indexes. Each block also stores the first index of each entity type it contains (e.g. first_txindex, first_txoutindex), so you can find all transactions, inputs, outputs, and addresses in any block in O(1).
§What’s Indexed
§Per Block (keyed by height)
- Block hash, timestamp, difficulty, size, weight
§Per Transaction (keyed by txindex)
- Txid, version, locktime, base size, total size, RBF flag, block height
§Per Input (keyed by txinindex)
- Spent outpoint, containing txindex, and the spent output’s type and address index
§Per Output (keyed by txoutindex)
- Value in satoshis, script type, address index within that type, containing txindex
Script types: P2PK (compressed/uncompressed), P2PKH, P2SH, P2WPKH, P2WSH, P2TR, P2A, P2MS, OP_RETURN, Empty, Unknown
§Per Address (keyed by addressindex, one set per type)
- Raw address bytes (20-65 bytes depending on type: pubkey, pubkey hash, script hash, witness program, etc.)
Address types each get their own index space: P2PK65, P2PK33, P2PKH, P2SH, P2WPKH, P2WSH, P2TR, P2A
§Per Non-Address Script (OP_RETURN, P2MS, Empty, Unknown)
- Containing txindex
§Key-Value Stores
On top of the vectors, key-value stores enable lookups that aren’t sequential:
| Store | Purpose |
|---|---|
| txid prefix → txindex | Look up a transaction by its txid |
| block hash prefix → height | Look up a block by its hash |
| address hash → addressindex | Look up an address (per type) |
| addressindex + txindex | All transactions involving an address |
| addressindex + outpoint | Unspent outputs for an address (live UTXO set) |
| height → coinbase tag | Miner-embedded message per block |
§How It Works
- Block metadata — store block hash, difficulty, timestamp, size, weight
- Compute TXIDs — parallel SHA256d across all transactions
- Process outputs — classify script types, extract addresses, detect new unique addresses
- Process inputs — resolve spent outpoints, look up address info
- Finalize — update address stores, UTXO set mutations, push all vectors
- Snapshot — periodic flush to disk for crash recovery
Reorg handling is built-in: on chain reorganization, the indexer rolls back to the last valid state.
§Performance
| Version | Machine | Time | Disk | Peak Disk | Memory | Peak Memory |
|---|---|---|---|---|---|---|
| v0.2.0-pre | MBP M3 Pro (36GB, internal SSD) | 2h40 | 239 GB | 302 GB | 5.9 GB | 13 GB |
| v0.1.0-alpha.0 | Mac Mini M4 (16GB, external SSD) | 4.9h | 233 GB | 303 GB | 5.4 GB | 11 GB |
Full benchmark data: bitcoinresearchkit/benches
§Recommended: mimalloc v3
Use mimalloc v3 as the global allocator to reduce memory usage.
§Built On
vecdbfor append-only vectors — integer-compressed (PcoVec) or raw bytes (BytesVec)brk_iteratorfor block iterationbrk_storefor key-value storage (fjall LSM)brk_typesfor domain types
Macros§
- parallel_
import - Imports multiple items in parallel using thread::scope.
Each expression must return Result
.
Structs§
- Addr
Type Vecs - Addrs
Vecs - Blocks
Vecs - Indexer
- Indexes
- Blockchain-level indexes tracking current positions for various entity types. Used by brk_indexer during block processing.
- Inputs
Vecs - Outputs
Vecs - Script
Type Vecs - Scripts
Vecs - Stores
- Transactions
Vecs - TxMetadata
Vecs - Vecs