brk_structs
Bitcoin-aware type system and data structures for blockchain analysis
brk_structs
provides the foundational type system for the Bitcoin Research Kit (BRK). It offers strongly-typed, storage-optimized data structures that encode Bitcoin protocol knowledge and enable efficient blockchain data analysis.
What it provides
- Type Safety: Distinct wrapper types prevent common mistakes (e.g., confusing block height with transaction index)
- Storage Optimization: Zero-copy serialization and optional compression for efficient disk storage
- Bitcoin Protocol Knowledge: Built-in understanding of halving epochs, address types, and blockchain constants
- Comprehensive Time Indexing: Multiple temporal granularities for time-series analysis
- Flexible Grouping: Framework for categorizing and filtering blockchain data
Key Features
Core Blockchain Types
Height
- Block heights with Bitcoin-specific arithmeticTxid
/BlockHash
- Transaction and block identifiers with prefix optimizationTxIndex
/InputIndex
/OutputIndex
- Component indices with type safetyDate
/Timestamp
- Time representations anchored to Bitcoin genesis block
Value and Currency Types
Sats
- Satoshi amounts with comprehensive arithmetic operationsBitcoin
- BTC amounts with precision handlingDollars
/Cents
- Fiat currency for price analysis- OHLC types (
Open<T>
,High<T>
,Low<T>
,Close<T>
) for market data
Address System
OutputType
- All Bitcoin script types (P2PKH, P2SH, P2WPKH, P2WSH, P2TR, etc.)AddressBytes
- Type-safe address data extraction- Address-specific indices for each output type
AnyAddressIndex
- Unified address indexing
Temporal Indexing
DateIndex
- Days since Bitcoin genesis- Time granularities:
WeekIndex
,MonthIndex
,QuarterIndex
,YearIndex
,DecadeIndex
HalvingEpoch
- Bitcoin halving periods (every 210,000 blocks)DifficultyEpoch
- Difficulty adjustment periods
Usage
Basic Types and Conversions
use *;
// Type-safe blockchain data
let height = new;
let epoch = from;
let amount = _1BTC * 2;
// Time-based indexing
let date = new;
let month_idx = from;
Address Handling
// Extract address information from Bitcoin scripts
let output_type = from;
if output_type.is_address
Zero-Copy Storage
// Efficient serialization without copying
let height_bytes = height.as_bytes;
let recovered_height = read_from_bytes?;
Data Grouping and Filtering
// Flexible filtering for analytics
let utxo_groups = UTXOGroups ;
Storage Optimization
All types implement zero-copy serialization traits:
- Zero overhead: Direct memory mapping without serialization costs
- Optional compression: Configurable zstd compression for space efficiency
- Type safety: Compile-time guarantees about data layout and endianness
Dependencies
bitcoin
- Bitcoin protocol types and script parsingvecdb
- Vector database storage traitszerocopy
- Zero-copy serialization frameworkserde
- JSON serialization supportjiff
- Modern date/time handling
This README was generated by Claude Code