//! Consensus rules and UTXO management for Synor blockchain. //! //! This crate implements: //! - UTXO (Unspent Transaction Output) model with DAG support //! - Transaction validation rules //! - Block validation rules //! - Difficulty adjustment algorithm (DAA) //! - Coinbase and reward calculations //! //! # Architecture Overview //! //! ```text //! ┌─────────────────────────────────────────────────┐ //! │ BlockValidator │ //! │ (validates headers, PoW, merkle roots, etc.) │ //! └────────────────────┬────────────────────────────┘ //! │ //! ┌───────────┴───────────┐ //! │ │ //! ▼ ▼ //! ┌─────────────────┐ ┌─────────────────────┐ //! │ TransactionValidator │ │ UtxoSet │ //! │ (structure, sigs) │ │ (UTXO tracking) │ //! └─────────────────┘ └─────────────────────┘ //! │ │ //! └───────────┬───────────┘ //! ▼ //! ┌─────────────────┐ //! │ DifficultyManager │ //! │ (DAA adjustments) │ //! └─────────────────┘ //! ``` //! //! # UTXO Model with DAG //! //! Unlike traditional blockchains with linear chains, Synor uses a DAG. //! This means multiple blocks can be created in parallel, potentially //! spending the same UTXOs. The UTXO model must: //! //! 1. Track UTXOs per block (not global state) //! 2. Handle conflicting transactions (double spends) //! 3. Use block ordering from GHOSTDAG for conflict resolution //! //! # Quick Start //! //! ## Validating Transactions //! //! ```rust,ignore //! use synor_consensus::{TransactionValidator, UtxoSet}; //! //! // Create validator with default settings //! let validator = TransactionValidator::new(); //! //! // Validate transaction structure (no UTXO check) //! validator.validate_structure(&tx)?; //! //! // Validate against UTXO set //! let fee = validator.validate_against_utxos(&tx, &utxo_set, current_daa_score)?; //! ``` //! //! ## Working with UTXOs //! //! ```rust,ignore //! use synor_consensus::{UtxoSet, UtxoDiff, UtxoEntry}; //! //! // Create a new UTXO set //! let utxo_set = UtxoSet::new(); //! //! // Add a UTXO //! utxo_set.add(outpoint, entry)?; //! //! // Create a diff from a transaction //! let diff = utxo_set.create_transaction_diff(&tx, block_daa_score)?; //! //! // Apply the diff //! utxo_set.apply_diff(&diff)?; //! ``` //! //! ## Difficulty Adjustment //! //! ```rust,ignore //! use synor_consensus::{DifficultyManager, DaaParams}; //! //! // Create manager with default params (10 blocks/second target) //! let manager = DifficultyManager::with_defaults(); //! //! // Calculate next difficulty from block window //! let new_bits = manager.calculate_next_difficulty(&block_window)?; //! //! // Validate proof of work //! let valid = manager.validate_pow(&block_hash, bits); //! ``` //! //! # Key Constants //! //! | Constant | Value | Description | //! |----------|-------|-------------| //! | `MAX_TRANSACTION_SIZE` | 100 KB | Maximum transaction size | //! | `MAX_BLOCK_MASS` | 500,000 | Maximum block weight | //! | `COINBASE_MATURITY` | 100 blocks | Blocks before coinbase spendable | //! | `TARGET_BLOCK_TIME_MS` | 100ms | Target time between blocks | //! | `BLOCKS_PER_SECOND` | 10 | Target block production rate | #![allow(dead_code)] pub mod difficulty; pub mod genesis; pub mod rewards; pub mod utxo; pub mod validation; pub use difficulty::{DaaParams, DifficultyManager}; pub use genesis::{ChainConfig, Checkpoint, GenesisAllocation, GenesisError}; pub use rewards::{BlockReward, RewardCalculator}; pub use utxo::{UtxoDiff, UtxoEntry, UtxoError, UtxoSet}; pub use validation::{BlockValidator, TransactionValidator, ValidationError}; /// Maximum transaction size in bytes. pub const MAX_TRANSACTION_SIZE: usize = 100_000; // 100 KB /// Maximum block mass (weight). pub const MAX_BLOCK_MASS: u64 = 500_000; /// Maximum signature operations per block. pub const MAX_SIG_OPS_PER_BLOCK: u64 = 20_000; /// Minimum transaction fee (in sompi). pub const MIN_RELAY_FEE: u64 = 1_000; // 0.00001 SYNOR /// Coinbase maturity (blocks before coinbase can be spent). pub const COINBASE_MATURITY: u64 = 100; /// Target block time in milliseconds. pub const TARGET_BLOCK_TIME_MS: u64 = 100; // 10 blocks per second /// Blocks per second target. pub const BLOCKS_PER_SECOND: u64 = 10; #[cfg(test)] mod tests { use super::*; #[test] #[allow(clippy::assertions_on_constants)] fn test_constants() { assert!(MAX_TRANSACTION_SIZE > 0); assert!(MAX_BLOCK_MASS > 0); assert!(COINBASE_MATURITY > 0); assert_eq!(1000 / TARGET_BLOCK_TIME_MS, BLOCKS_PER_SECOND); } }