brk_structs
Bitcoin-aware type system and zero-copy data structures for blockchain analysis.
Overview
This crate provides a comprehensive type system for Bitcoin blockchain analysis, featuring zero-copy data structures, memory-efficient storage types, and Bitcoin-specific primitives. Built on zerocopy and vecdb, it offers type-safe representations for blockchain data with optimized serialization and database integration.
Key Features:
- Zero-copy data structures with
zerocopyderives for high-performance serialization - Bitcoin-specific types for heights, timestamps, addresses, and transaction data
- OHLC (Open, High, Low, Close) price data structures for financial analysis
- Comprehensive address type classification (P2PK, P2PKH, P2SH, P2WPKH, P2WSH, P2TR, etc.)
- Time-based indexing types (Date, DateIndex, WeekIndex, MonthIndex, etc.)
- Memory allocation tracking with
allocativeintegration - Stored primitive wrappers for space-efficient database storage
Target Use Cases:
- Bitcoin blockchain analysis and research tools
- Financial data processing and OHLC calculations
- Database-backed blockchain indexing systems
- Memory-efficient UTXO set management
Installation
Quick Start
use *;
// Bitcoin-specific types
let height = new;
let timestamp = from;
let date = from;
let sats = new; // 1 BTC in satoshis
// Price data structures
let price_cents = from;
let ohlc = from;
// Address classification
let output_type = P2PKH;
println!;
println!;
// Time indexing
let date_index = try_from?;
let week_index = from;
API Overview
Core Bitcoin Types
Height: Block height with overflow-safe arithmetic operationsTimestamp: Unix timestamp with Bitcoin genesis epoch supportDate: Calendar date with blockchain-specific formatting (YYYYMMDD)Sats: Satoshi amounts with comprehensive arithmetic operationsBitcoin: Floating-point BTC amounts with satoshi conversionBlockHash/TxId: Cryptographic hash identifiers
Address and Output Types
-
OutputType: Comprehensive Bitcoin script type classification- Standard types: P2PK (33/65-byte), P2PKH, P2SH, P2WPKH, P2WSH, P2TR
- Special types: P2MS (multisig), OpReturn, P2A (address), Empty, Unknown
- Type-checking methods:
is_spendable(),is_address(),is_unspendable()
-
Address Index Types: Specialized indexes for each address type
P2PKHAddressIndex,P2SHAddressIndex,P2WPKHAddressIndexP2WSHAddressIndex,P2TRAddressIndex, etc.
Financial Data Types
Price Representations:
Cents: Integer cent representation for precise financial calculationsDollars: Floating-point dollar amounts with cent conversionOHLCCents/OHLCDollars/OHLCSats: OHLC data in different denominations
OHLC Components:
Open<T>,High<T>,Low<T>,Close<T>: Generic price point wrappers- Support for arithmetic operations and automatic conversions
Time Indexing System
Calendar Types:
DateIndex: Days since Bitcoin genesis (2009-01-03)WeekIndex: Week-based indexing for aggregationMonthIndex,QuarterIndex,SemesterIndex: Hierarchical time periodsYearIndex,DecadeIndex: Long-term time categorization
Epoch Types:
HalvingEpoch: Bitcoin halving period classificationDifficultyEpoch: Difficulty adjustment epoch tracking
Storage Types
Stored Primitives: Memory-efficient wrappers for database storage
StoredU8,StoredU16,StoredU32,StoredU64: Unsigned integersStoredI16: Signed integersStoredF32,StoredF64: Floating-point numbersStoredBool: Boolean valuesStoredString: String storage optimization
Examples
Block Height Operations
use Height;
let current_height = new;
let next_height = current_height.incremented;
// Check halving schedule
let blocks_until_halving = current_height.left_before_next_halving;
println!;
// Difficulty adjustment tracking
let blocks_until_adjustment = current_height.left_before_next_diff_adj;
Price Data Processing
use *;
// Create OHLC data from individual price points
let daily_ohlc = from;
// Convert between price denominations
let ohlc_cents: OHLCCents = daily_ohlc.into;
let sats_per_dollar = _1BTC / daily_ohlc.close;
// Aggregate multiple OHLC periods
let weekly_close = vec!.iter.;
Address Type Classification
use OutputType;
use ;
let address_str = "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2";
let address = from_str?.assume_checked;
let output_type = from;
match output_type
// Check spend conditions
if output_type.is_spendable && output_type.is_address
Time-Based Indexing
use *;
let date = new;
let date_index = try_from?;
// Convert to different time granularities
let week_index = from;
let month_index = from;
let quarter_index = from;
// Calculate completion percentage for current day
let completion_pct = date.completion;
println!;
Architecture
Zero-Copy Design
All major types implement zerocopy traits (FromBytes, IntoBytes, KnownLayout) enabling:
- Direct memory mapping from serialized data
- Efficient network protocol handling
- High-performance database operations
- Memory layout guarantees for cross-platform compatibility
Type Safety
The type system enforces Bitcoin domain constraints:
Heightprevents integer overflow in block calculationsTimestamphandles Unix epoch edge casesOutputTypeenum covers all Bitcoin script patterns- Address types ensure correct hash length validation
Memory Efficiency
Storage types provide space optimization:
- Stored primitives reduce allocation overhead
- OHLC structures support both heap and stack allocation
- Index types enable efficient range queries
- Hash types use fixed-size arrays for predictable memory usage
Code Analysis Summary
Main Categories: 70+ struct types across Bitcoin primitives, financial data, time indexing, and storage optimization
Zero-Copy Support: Comprehensive zerocopy implementation for all major types
Type Safety: Bitcoin domain-specific constraints with overflow protection and validation
Financial Types: Multi-denomination OHLC support with automatic conversions
Address System: Complete Bitcoin script type classification with 280 enum variants
Time Indexing: Hierarchical calendar system from daily to decade-level granularity
Storage Integration: vecdb::StoredCompressed traits for efficient database operations
Architecture: Type-driven design prioritizing memory efficiency and domain correctness
This README was generated by Claude Code