| Crates.io | brk_structs |
| lib.rs | brk_structs |
| version | 0.0.109 |
| created_at | 2025-08-07 19:24:44.372935+00 |
| updated_at | 2025-09-20 17:21:10.181048+00 |
| description | Structs used throughout BRK |
| homepage | https://bitcoinresearchkit.org |
| repository | https://github.com/bitcoinresearchkit/brk |
| max_upload_size | |
| id | 1785676 |
| size | 347,038 |
Bitcoin-aware type system and zero-copy data structures for blockchain analysis.
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:
zerocopy derives for high-performance serializationallocative integrationTarget Use Cases:
cargo add brk_structs
use brk_structs::*;
// Bitcoin-specific types
let height = Height::new(800000);
let timestamp = Timestamp::from(1684771200u32);
let date = Date::from(timestamp);
let sats = Sats::new(100_000_000); // 1 BTC in satoshis
// Price data structures
let price_cents = Cents::from(Dollars::from(50000.0));
let ohlc = OHLCCents::from((
Open::new(price_cents),
High::new(price_cents * 1.02),
Low::new(price_cents * 0.98),
Close::new(price_cents * 1.01),
));
// Address classification
let output_type = OutputType::P2PKH;
println!("Is spendable: {}", output_type.is_spendable());
println!("Has address: {}", output_type.is_address());
// Time indexing
let date_index = DateIndex::try_from(date)?;
let week_index = WeekIndex::from(date);
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 identifiersOutputType: Comprehensive Bitcoin script type classification
is_spendable(), is_address(), is_unspendable()Address Index Types: Specialized indexes for each address type
P2PKHAddressIndex, P2SHAddressIndex, P2WPKHAddressIndexP2WSHAddressIndex, P2TRAddressIndex, etc.Price Representations:
Cents: Integer cent representation for precise financial calculationsDollars: Floating-point dollar amounts with cent conversionOHLCCents / OHLCDollars / OHLCSats: OHLC data in different denominationsOHLC Components:
Open<T>, High<T>, Low<T>, Close<T>: Generic price point wrappersCalendar Types:
DateIndex: Days since Bitcoin genesis (2009-01-03)WeekIndex: Week-based indexing for aggregationMonthIndex, QuarterIndex, SemesterIndex: Hierarchical time periodsYearIndex, DecadeIndex: Long-term time categorizationEpoch Types:
HalvingEpoch: Bitcoin halving period classificationDifficultyEpoch: Difficulty adjustment epoch trackingStored Primitives: Memory-efficient wrappers for database storage
StoredU8, StoredU16, StoredU32, StoredU64: Unsigned integersStoredI16: Signed integersStoredF32, StoredF64: Floating-point numbersStoredBool: Boolean valuesStoredString: String storage optimizationuse brk_structs::Height;
let current_height = Height::new(800000);
let next_height = current_height.incremented();
// Check halving schedule
let blocks_until_halving = current_height.left_before_next_halving();
println!("Blocks until next halving: {}", blocks_until_halving);
// Difficulty adjustment tracking
let blocks_until_adjustment = current_height.left_before_next_diff_adj();
use brk_structs::*;
// Create OHLC data from individual price points
let daily_ohlc = OHLCDollars::from((
Open::from(45000.0),
High::from(47500.0),
Low::from(44000.0),
Close::from(46800.0),
));
// Convert between price denominations
let ohlc_cents: OHLCCents = daily_ohlc.into();
let sats_per_dollar = Sats::_1BTC / daily_ohlc.close;
// Aggregate multiple OHLC periods
let weekly_close = vec![
Close::from(46800.0),
Close::from(48200.0),
Close::from(47100.0),
].iter().sum::<Close<Dollars>>();
use brk_structs::OutputType;
use bitcoin::{Address, Network};
let address_str = "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2";
let address = Address::from_str(address_str)?.assume_checked();
let output_type = OutputType::from(&address);
match output_type {
OutputType::P2PKH => println!("Legacy address"),
OutputType::P2WPKH => println!("Native SegWit address"),
OutputType::P2SH => println!("Script hash address"),
OutputType::P2TR => println!("Taproot address"),
_ => println!("Other address type: {:?}", output_type),
}
// Check spend conditions
if output_type.is_spendable() && output_type.is_address() {
println!("Spendable address-based output");
}
use brk_structs::*;
let date = Date::new(2023, 6, 15);
let date_index = DateIndex::try_from(date)?;
// Convert to different time granularities
let week_index = WeekIndex::from(date);
let month_index = MonthIndex::from(date);
let quarter_index = QuarterIndex::from(date);
// Calculate completion percentage for current day
let completion_pct = date.completion();
println!("Day {}% complete", completion_pct * 100.0);
All major types implement zerocopy traits (FromBytes, IntoBytes, KnownLayout) enabling:
The type system enforces Bitcoin domain constraints:
Height prevents integer overflow in block calculationsTimestamp handles Unix epoch edge casesOutputType enum covers all Bitcoin script patternsStorage types provide space optimization:
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