|
| 1 | +# Catalyst Consensus |
| 2 | + |
| 3 | +The `catalyst-consensus` crate implements the collaborative consensus mechanism for the Catalyst Network. This is a novel 4-phase consensus protocol that enables democratic, energy-efficient consensus without the waste associated with Proof-of-Work mining. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Catalyst consensus protocol replaces competitive mining with collaborative consensus among selected producer nodes. Instead of racing to solve cryptographic puzzles, producers work together through four distinct phases to build and validate ledger state updates. |
| 8 | + |
| 9 | +## Key Features |
| 10 | + |
| 11 | +- **4-Phase Collaborative Process**: Construction → Campaigning → Voting → Synchronization |
| 12 | +- **Energy Efficient**: No wasteful cryptographic puzzle solving |
| 13 | +- **Democratic**: All selected producers participate and are rewarded |
| 14 | +- **Deterministic Producer Selection**: Uses XOR-based randomization from previous state |
| 15 | +- **Supermajority Thresholds**: Configurable confidence levels for each phase |
| 16 | +- **Compensation System**: Automatic reward distribution to participating producers |
| 17 | + |
| 18 | +## Architecture |
| 19 | + |
| 20 | +# Catalyst Consensus |
| 21 | + |
| 22 | +The `catalyst-consensus` crate implements the collaborative consensus mechanism for the Catalyst Network. This is a novel 4-phase consensus protocol that enables democratic, energy-efficient consensus without the waste associated with Proof-of-Work mining. |
| 23 | + |
| 24 | +## Overview |
| 25 | + |
| 26 | +The Catalyst consensus protocol replaces competitive mining with collaborative consensus among selected producer nodes. Instead of racing to solve cryptographic puzzles, producers work together through four distinct phases to build and validate ledger state updates. |
| 27 | + |
| 28 | +## Key Features |
| 29 | + |
| 30 | +- **4-Phase Collaborative Process**: Construction → Campaigning → Voting → Synchronization |
| 31 | +- **Energy Efficient**: No wasteful cryptographic puzzle solving |
| 32 | +- **Democratic**: All selected producers participate and are rewarded |
| 33 | +- **Asynchronous Operation**: Built on tokio for high-performance async execution |
| 34 | +- **Network Integration**: Uses catalyst-network for P2P communication |
| 35 | +- **Cryptographic Security**: Leverages catalyst-crypto for signatures and hashing |
| 36 | + |
| 37 | +## Architecture |
| 38 | + |
| 39 | +### Core Components |
| 40 | + |
| 41 | +- **`CatalystConsensus`**: Main consensus engine orchestrating the 4-phase process |
| 42 | +- **`ConsensusProtocol`**: Trait defining consensus behavior for block proposals and voting |
| 43 | +- **`ConsensusPhase`**: Enum representing the current phase of consensus |
| 44 | +- **`LedgerCycle`**: Management of consensus cycles and timing |
| 45 | +- **`ConsensusMessage`**: Message types for inter-node communication during consensus |
| 46 | + |
| 47 | +### Message Types |
| 48 | + |
| 49 | +- **`ProducerQuantity`**: Construction phase - hash of partial ledger update |
| 50 | +- **`ProducerCandidate`**: Campaigning phase - proposed majority update |
| 51 | +- **`ProducerVote`**: Voting phase - vote on final ledger state |
| 52 | +- **`ProducerOutput`**: Synchronization phase - finalized update location |
| 53 | + |
| 54 | +## The 4-Phase Protocol |
| 55 | + |
| 56 | +### Phase 1: Construction (ConsensusPhase::Construction) |
| 57 | +Producers collect transactions and create partial ledger state updates, broadcasting their producer quantities. |
| 58 | + |
| 59 | +### Phase 2: Campaigning (ConsensusPhase::Campaigning) |
| 60 | +Producers identify the most common partial update and broadcast candidates representing the majority consensus. |
| 61 | + |
| 62 | +### Phase 3: Voting (ConsensusPhase::Voting) |
| 63 | +Producers vote on final ledger states and create compensation entries for rewards. |
| 64 | + |
| 65 | +### Phase 4: Synchronization (ConsensusPhase::Synchronization) |
| 66 | +Final consensus is reached and the agreed ledger update is broadcast to the network. |
| 67 | + |
| 68 | +## Usage |
| 69 | + |
| 70 | +### Basic Setup |
| 71 | + |
| 72 | +```rust |
| 73 | +use catalyst_consensus::{CatalystConsensus, ConsensusConfig}; |
| 74 | +use catalyst_crypto::KeyPair; |
| 75 | +use catalyst_network::{NetworkService, NetworkConfig}; |
| 76 | +use std::sync::Arc; |
| 77 | + |
| 78 | +// Create consensus configuration |
| 79 | +let config = ConsensusConfig { |
| 80 | + phase_duration_ms: 10_000, // 10 seconds per phase |
| 81 | + min_producers: 3, |
| 82 | + max_producers: 100, |
| 83 | + message_timeout_ms: 5_000, |
| 84 | +}; |
| 85 | + |
| 86 | +// Setup network and crypto |
| 87 | +let mut rng = rand::thread_rng(); |
| 88 | +let keypair = KeyPair::generate(&mut rng); |
| 89 | +let node_id = [1u8; 32]; |
| 90 | +let network_config = NetworkConfig::default(); |
| 91 | +let network = Arc::new(NetworkService::new(network_config).await?); |
| 92 | + |
| 93 | +// Create consensus engine |
| 94 | +let consensus = CatalystConsensus::new(node_id, keypair, network); |
| 95 | +``` |
| 96 | + |
| 97 | +### Starting Consensus |
| 98 | + |
| 99 | +```rust |
| 100 | +// Start the consensus engine (runs indefinitely) |
| 101 | +consensus.start().await?; |
| 102 | +``` |
| 103 | + |
| 104 | +### Block Proposal and Voting |
| 105 | + |
| 106 | +```rust |
| 107 | +use catalyst_consensus::{Transaction, ConsensusProtocol}; |
| 108 | + |
| 109 | +// Create transactions |
| 110 | +let transactions = vec![ |
| 111 | + Transaction { |
| 112 | + id: [1u8; 32], |
| 113 | + data: b"Transfer 100 KAT".to_vec(), |
| 114 | + }, |
| 115 | +]; |
| 116 | + |
| 117 | +// Propose a block |
| 118 | +let mut consensus_engine = consensus; |
| 119 | +let proposal = consensus_engine.propose_block(transactions).await?; |
| 120 | + |
| 121 | +// Validate the proposal |
| 122 | +let is_valid = consensus_engine.validate_proposal(&proposal).await?; |
| 123 | + |
| 124 | +// Vote on the proposal |
| 125 | +if is_valid { |
| 126 | + let signature = catalyst_crypto::Signature::new([0u8; 64]); |
| 127 | + consensus_engine.vote(&proposal.hash, signature).await?; |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +## Configuration |
| 132 | + |
| 133 | +### ConsensusConfig Options |
| 134 | + |
| 135 | +```rust |
| 136 | +pub struct ConsensusConfig { |
| 137 | + pub phase_duration_ms: u32, // Duration of each phase |
| 138 | + pub min_producers: u32, // Minimum producers required |
| 139 | + pub max_producers: u32, // Maximum producers allowed |
| 140 | + pub message_timeout_ms: u32, // Timeout for message collection |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### Recommended Settings |
| 145 | + |
| 146 | +- **Development**: 5-10 second phases, 2-5 producers |
| 147 | +- **Testnet**: 10-15 second phases, 5-10 producers |
| 148 | +- **Mainnet**: 15-30 second phases, 10+ producers |
| 149 | + |
| 150 | +## Message Flow |
| 151 | + |
| 152 | +1. **Construction Phase**: Each producer broadcasts `ProducerQuantity` messages |
| 153 | +2. **Campaigning Phase**: Producers broadcast `ProducerCandidate` messages |
| 154 | +3. **Voting Phase**: Producers broadcast `ProducerVote` messages |
| 155 | +4. **Synchronization Phase**: Final `ProducerOutput` messages are broadcast |
| 156 | + |
| 157 | +## Error Handling |
| 158 | + |
| 159 | +The consensus engine provides detailed error types: |
| 160 | + |
| 161 | +```rust |
| 162 | +pub enum ConsensusError { |
| 163 | + InvalidProposal(String), |
| 164 | + NetworkError(String), |
| 165 | + ValidationError(String), |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +## Examples |
| 170 | + |
| 171 | +Run the simple consensus example: |
| 172 | + |
| 173 | +```bash |
| 174 | +cargo run --example simple_consensus |
| 175 | +``` |
| 176 | + |
| 177 | +## Testing |
| 178 | + |
| 179 | +Run the test suite: |
| 180 | + |
| 181 | +```bash |
| 182 | +# All tests |
| 183 | +cargo test |
| 184 | + |
| 185 | +# Specific test |
| 186 | +cargo test test_consensus_creation |
| 187 | + |
| 188 | +# With output |
| 189 | +cargo test -- --nocapture |
| 190 | +``` |
| 191 | + |
| 192 | +## Integration |
| 193 | + |
| 194 | +The consensus crate integrates with: |
| 195 | + |
| 196 | +- **`catalyst-network`**: P2P communication and message broadcasting |
| 197 | +- **`catalyst-crypto`**: Cryptographic operations and key management |
| 198 | +- **Future integration**: `catalyst-utils`, `catalyst-storage`, `catalyst-config` |
| 199 | + |
| 200 | +## Dependencies |
| 201 | + |
| 202 | +- `catalyst-network`: Network layer for P2P communication |
| 203 | +- `catalyst-crypto`: Cryptographic primitives |
| 204 | +- `tokio`: Async runtime |
| 205 | +- `serde`: Serialization framework |
| 206 | +- `tracing`: Logging and observability |
| 207 | +- `hex`: Hexadecimal encoding utilities |
| 208 | + |
| 209 | +## Implementation Status |
| 210 | + |
| 211 | +✅ **Completed:** |
| 212 | +- Core consensus engine structure |
| 213 | +- 4-phase protocol framework |
| 214 | +- Basic block proposal and voting |
| 215 | +- Network integration |
| 216 | +- Async operation support |
| 217 | +- Configuration management |
| 218 | + |
| 219 | +🚧 **In Progress:** |
| 220 | +- Full message serialization/deserialization |
| 221 | +- Producer selection algorithms |
| 222 | +- Reward distribution mechanisms |
| 223 | +- Enhanced validation logic |
| 224 | + |
| 225 | +📋 **Planned:** |
| 226 | +- DFS integration for ledger storage |
| 227 | +- Advanced producer reputation |
| 228 | +- Cross-shard consensus coordination |
| 229 | +- Performance optimizations |
| 230 | + |
| 231 | +## Performance |
| 232 | + |
| 233 | +Current implementation provides: |
| 234 | + |
| 235 | +- **Phase Duration**: 5-30 seconds per phase configurable |
| 236 | +- **Producer Support**: 2-100+ producers |
| 237 | +- **Async Operations**: Non-blocking network and consensus operations |
| 238 | +- **Memory Efficient**: Minimal state retention between cycles |
| 239 | + |
| 240 | +## Contributing |
| 241 | + |
| 242 | +1. Follow existing code patterns and async/await usage |
| 243 | +2. Add tests for new functionality |
| 244 | +3. Use appropriate logging with tracing |
| 245 | +4. Handle errors gracefully with detailed context |
| 246 | +5. Update documentation for API changes |
| 247 | + |
| 248 | +## License |
| 249 | + |
| 250 | +This crate is part of the Catalyst Network project. See the project root for license information. |
0 commit comments