Chia Generator Parser
A production-ready Rust crate for parsing Chia blockchain generator bytecode, extracting transaction generators and reference lists from serialized blocks. This implementation exactly mirrors the Python chia.full_node.full_block_utils module.
Overview
This crate provides efficient, production-quality parsing of Chia blockchain blocks to extract:
- Transaction Generator Bytecode: The CLVM program that generates transactions
- Generator Reference List: Block heights referenced by the generator
- Previous Block Header Hash: For block chain verification
- Block Height & Transaction Status: Fast block metadata extraction
- Header Block Generation: For networking and filtering
- Advanced Generator Analysis: Pattern detection, entropy analysis, and validation
🚀 Production Features
✅ Complete Python Compatibility
block_info_from_block()- Extract complete generator informationgenerator_from_block()- Extract raw generator bytecodeget_height_and_tx_status_from_block()- Fast height/status extractionheader_block_from_block()- Generate header blocks for networking
✅ Production CLVM Implementation
- Proper CLVM Serialization Length: Production-quality length calculation
- Full Format Support: Null atoms, cons cells, integers, variable-length encoding
- Error Validation: Comprehensive bounds checking and format validation
✅ Advanced Analysis
- Pattern Detection: Identify CLVM structures and coin patterns
- Entropy Analysis: Calculate bytecode randomness/complexity
- Performance Optimized: Zero-copy parsing, direct offset calculations
✅ Enterprise Error Handling
- Comprehensive Error Types: Specific errors for each failure mode
- Buffer Validation: Prevents buffer overruns and underruns
- Format Validation: Validates CLVM encoding and block structure
Architecture
The crate implements exactly the same logic as Chia's Python full_block_utils.py:
// Python: block_info_from_block(buf)
let block_info = parser.parse_block_info?;
// Python: generator_from_block(buf)
let generator_bytes = parser.extract_generator_from_block?;
// Python: get_height_and_tx_status_from_block(buf)
let height_info = parser.get_height_and_tx_status_from_block?;
Usage
Add this to your Cargo.toml:
[]
= { = "./crate/chia-generator-parser" }
Production Block Parsing
use ;
let parser = new;
// Parse complete block information (Python: block_info_from_block)
let block_info = parser.parse_block_info?;
println!;
println!;
println!;
// Extract raw generator (Python: generator_from_block)
let raw_generator = parser.extract_generator_from_block?;
// Get block metadata (Python: get_height_and_tx_status_from_block)
let height_info = parser.get_height_and_tx_status_from_block?;
println!;
Generator Analysis & Validation
// Parse and analyze generator from hex
let parsed_generator = parser.parse_generator_from_hex?;
println!;
println!;
println!;
println!;
println!;
Block Structure Parsing
The parser follows the exact block structure as defined in the Chia protocol:
Header Block Generation
// Generate header block for networking (Python: header_block_from_block)
let header_block = parser.header_block_from_block?;
Implementation Details
Production CLVM Parsing
The parser uses production-quality CLVM serialization matching chia_rs::serialized_length:
Zero-Copy Stream Parsing
// Efficient streaming parser - no full deserialization required
buf = self.skip_list?;
buf = self.skip_reward_chain_block?;
// ... continue parsing to reach generator
Optimized TransactionsInfo Parsing
// Direct offset calculation like Python reference
Testing & Validation
Run Production Test Suite
This validates:
- CLVM Length Calculation: All serialization formats
- Pattern Detection: CLVM and coin pattern recognition
- Error Handling: Buffer validation and edge cases
- Block Structure: Full parsing compatibility
Run Basic Usage Example
Error Handling
Production-grade error handling with specific error types:
use GeneratorParserError;
match parser.parse_block_info
Compatibility Matrix
| Python Function | Rust Method | Status |
|---|---|---|
block_info_from_block() |
parse_block_info() |
✅ Complete |
generator_from_block() |
extract_generator_from_block() |
✅ Complete |
get_height_and_tx_status_from_block() |
get_height_and_tx_status_from_block() |
✅ Complete |
header_block_from_block() |
header_block_from_block() |
✅ Complete |
chia_rs.serialized_length() |
calculate_serialized_length() |
✅ Complete |
Performance Characteristics
- Zero-copy Parsing: Efficient streaming without full deserialization
- Memory Efficient: No intermediate allocations for parsing
- Fast Pattern Detection: Optimized bytecode analysis
- Production Validated: Handles real Chia blockchain data
Production Readiness
- ✅ Complete Python Compatibility: All reference functions implemented
- ✅ Production CLVM Parsing: Full serialization format support
- ✅ Comprehensive Error Handling: Enterprise-grade validation
- ✅ Advanced Analysis: Pattern detection and entropy calculation
- ✅ Performance Optimized: Zero-copy, direct offset calculations
- ✅ Thoroughly Tested: Production test suite validates all features
License
MIT License
Contributing
This is a production implementation that maintains strict compatibility with the Python reference. Changes should:
- Maintain exact Python
full_block_utils.pycompatibility - Pass the production test suite:
cargo run --example production_test - Include comprehensive error handling
- Follow Rust performance best practices