|
| 1 | +# Ledger Crate Summary |
| 2 | + |
| 3 | +The ledger crate is the most complex component in the codebase. For architecture overview and design details, see [docs/handover/ledger-crate.md](../docs/handover/ledger-crate.md). |
| 4 | + |
| 5 | +## Quick Reference |
| 6 | + |
| 7 | +**Core Ledger** |
| 8 | +- `src/base.rs` - BaseLedger trait (fundamental interface) |
| 9 | +- `src/database/` - In-memory account storage |
| 10 | +- `src/mask/` - Layered ledger views with Arc-based sharing |
| 11 | +- `src/tree.rs` - Merkle tree operations |
| 12 | + |
| 13 | +**Transaction Processing** |
| 14 | +- `src/transaction_pool.rs` - Mempool with fee-based ordering |
| 15 | +- `src/staged_ledger/` - Block validation and transaction application |
| 16 | +- `src/scan_state/` - SNARK work coordination and parallel scan |
| 17 | + |
| 18 | +**Proof System** |
| 19 | +- `src/proofs/` - Transaction, block, and zkApp proof generation/verification |
| 20 | +- `src/sparse_ledger/` - Minimal ledger representation for proofs |
| 21 | +- `src/zkapps/` - zkApp transaction processing |
| 22 | + |
| 23 | +**Account Management** |
| 24 | +- `src/account/` - Account structures, balances, permissions |
| 25 | + |
| 26 | +## Critical Issues |
| 27 | + |
| 28 | +**Error Handling** |
| 29 | +- Too many `.unwrap()` and `.expect()` calls in production code paths (excluding tests) |
| 30 | +- Critical transaction processing paths could panic the node |
| 31 | +- Inconsistent error handling across modules |
| 32 | +- Verification key lookup bug fix from upstream Mina Protocol needs to be ported (https://github.com/MinaProtocol/mina/pull/16699) |
| 33 | + |
| 34 | +**Monolithic Structure** |
| 35 | +- Single massive crate with files exceeding 6,000+ lines |
| 36 | +- Deep coupling between components that should be independent |
| 37 | +- Hard to maintain, test, and develop in parallel |
| 38 | + |
| 39 | +**Performance** |
| 40 | +- Excessive cloning of large structures in hot paths: |
| 41 | + - `SparseLedger::of_ledger_subset_exn()` calls `oledger.copy()` creating unnecessary deep clones for sparse ledger construction |
| 42 | + - Transaction pool operations clone transaction objects with acknowledged TODO comments about performance |
| 43 | +- No memory pooling or reuse strategies (could help with memory fragmentation in WASM) |
| 44 | + |
| 45 | +**Memory Management** |
| 46 | +- Memory-only implementation, no persistence for production |
| 47 | +- There's an unused `ondisk` implementation but we were planning a more comprehensive global solution (see persistence.md) |
| 48 | +- Thread-local caching holds memory indefinitely |
| 49 | + |
| 50 | +## Refactoring Plan |
| 51 | + |
| 52 | +**Phase 1: Safety** |
| 53 | +- Replace `.unwrap()` with proper error propagation in production code |
| 54 | +- Reduce cloning in hot paths |
| 55 | +- Standardize error types |
| 56 | + |
| 57 | +**Phase 2: Decomposition** |
| 58 | +Break into focused crates: `mina-account`, `mina-ledger`, `mina-transaction-logic`, `mina-scan-state`, `mina-transaction-pool`, `mina-proofs` |
| 59 | + |
| 60 | +Changes must maintain strict OCaml compatibility while improving performance for production. |
0 commit comments