|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +title: Architecture Overview |
| 4 | +description: Understand OpenMina's Redux-style state machine architecture and design principles |
| 5 | +slug: /developers/architecture |
| 6 | +--- |
| 7 | + |
| 8 | +# OpenMina Architecture |
| 9 | + |
| 10 | +OpenMina follows a Redux-style state machine architecture for predictable, debuggable behavior. This design ensures that all state changes are traceable and the system behavior is deterministic. |
| 11 | + |
| 12 | +## Core Principles |
| 13 | + |
| 14 | +### State Machine Pattern |
| 15 | + |
| 16 | +OpenMina implements Redux principles adapted for a blockchain node: |
| 17 | + |
| 18 | +- **State** - Centralized, immutable data structure representing the entire node state |
| 19 | +- **Actions** - Events that trigger state changes throughout the system |
| 20 | +- **Enabling Conditions** - Guards that prevent invalid state transitions |
| 21 | +- **Reducers** - Pure functions that update state and dispatch new actions |
| 22 | +- **Effects** - Thin wrappers for service calls and side effects |
| 23 | +- **Services** - Separate threads handling I/O and heavy computation |
| 24 | + |
| 25 | +### Predictable State Management |
| 26 | + |
| 27 | +Every state change in OpenMina follows the same pattern: |
| 28 | + |
| 29 | +```rust |
| 30 | +// 1. Action is dispatched |
| 31 | +dispatch(SomeAction { data }); |
| 32 | + |
| 33 | +// 2. Enabling condition is checked |
| 34 | +if enabling_condition_met(&state, &action) { |
| 35 | + // 3. Reducer processes the action |
| 36 | + let new_state = reducer(state, action); |
| 37 | + |
| 38 | + // 4. Effects may be triggered |
| 39 | + trigger_effects(&new_state, &action); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Architecture Styles |
| 44 | + |
| 45 | +The codebase contains two architectural styles: |
| 46 | + |
| 47 | +### New Style (Recommended) |
| 48 | +- **Unified reducers** that handle both state updates and action dispatch |
| 49 | +- Single file per component containing all logic |
| 50 | +- Cleaner separation of concerns |
| 51 | + |
| 52 | +### Old Style (Being Migrated) |
| 53 | +- Separate reducer and effects files |
| 54 | +- Split between `*_reducer.rs` and `*_effects.rs` |
| 55 | +- Gradually being converted to new style |
| 56 | + |
| 57 | +## Component Structure |
| 58 | + |
| 59 | +### Core Components |
| 60 | + |
| 61 | +**Node** - Main node logic |
| 62 | +- Block production and validation |
| 63 | +- Transaction pool management |
| 64 | +- Consensus and blockchain state |
| 65 | +- RPC interface |
| 66 | + |
| 67 | +**P2P** - Networking layer |
| 68 | +- Dual transport: libp2p and WebRTC |
| 69 | +- Peer discovery and connection management |
| 70 | +- Message routing and validation |
| 71 | + |
| 72 | +**Ledger** - Blockchain state |
| 73 | +- Account state and transactions |
| 74 | +- Proof verification |
| 75 | +- Scan state management |
| 76 | + |
| 77 | +**Core** - Shared utilities |
| 78 | +- Common types and data structures |
| 79 | +- Cryptographic primitives |
| 80 | +- Configuration management |
| 81 | + |
| 82 | +### File Organization |
| 83 | + |
| 84 | +Each component follows consistent patterns: |
| 85 | + |
| 86 | +- `*_state.rs` - State definitions and data structures |
| 87 | +- `*_actions.rs` - Action types and event definitions |
| 88 | +- `*_reducer.rs` - State transition logic |
| 89 | +- `*_effects.rs` - Service interaction wrappers |
| 90 | +- `*_service.rs` - Service interface definitions |
| 91 | +- `summary.md` - Component documentation |
| 92 | + |
| 93 | +## Data Flow |
| 94 | + |
| 95 | +1. **External Events** (network messages, user commands) create actions |
| 96 | +2. **Actions** flow through the dispatch system |
| 97 | +3. **Enabling Conditions** validate whether actions can be processed |
| 98 | +4. **Reducers** compute new state based on current state and action |
| 99 | +5. **Effects** trigger service calls when state changes require external interaction |
| 100 | +6. **Services** handle async operations and generate new events |
| 101 | + |
| 102 | +## Key Benefits |
| 103 | + |
| 104 | +- **Debuggability** - Complete state history and action replay |
| 105 | +- **Testability** - Pure functions and predictable state changes |
| 106 | +- **Maintainability** - Clear separation of concerns and data flow |
| 107 | +- **Performance** - Efficient state updates and selective processing |
| 108 | + |
| 109 | +## Development Guidelines |
| 110 | + |
| 111 | +- Use `bug_condition!` macro for theoretically unreachable code paths |
| 112 | +- Extract complex logic into state methods rather than bloating reducers |
| 113 | +- Prefer enabling conditions over error handling in reducers |
| 114 | +- Document component responsibilities in `summary.md` files |
| 115 | + |
| 116 | +For detailed implementation examples, see the component-specific documentation in the codebase. |
0 commit comments