Architecture
The Lean/Rust split, FFI entry points and encodings, package layers, build and threading.
On this page · 7
Responsibilities
| Lean core (proven, static library) | Rust node |
|---|---|
| State transition: applyTx | HTTP API; validator network from v2 |
| Authorization: signature result, nonce, fee | Mempool and block building |
| Consensus decisions: step | Slot clock |
| Fee rule: nextBaseFee | Disk: write-ahead state, block log, snapshots, index |
| Slashing rules (v2) | Signing; ML-DSA-44 and SHA3-256 implementations |
Clients
Wallets, explorers and the leansvm CLI
Rust · leansvm-node
slot clock · mempool · blocks.log · snapshots · index
Lean 4 · proven
The core
- consensus step
- applyTx
- nextBaseFee
C and Rust · trusted
Cryptography
- ML-DSA-44 · mldsa-native
- SHA3-256 · sha3
Entry points
Every export is ByteArray → ByteArray. All integers are u64 little-endian; a 32-byte hash is four u64 words, which is byte-identical to the raw hash.
| Export | Input | Output |
|---|---|---|
| leansvm_consensus_genesis | me, validators, genesis hash (4) | state (20 words), or empty if invalid |
| leansvm_consensus_step | state (20) ‖ event | state (20) ‖ actions (10 words each), or empty if malformed |
| leansvm_apply_tx | fields (29 words), sender key, tx key, signature | [0, sender (4), balance, nonce, recipient (4), balance, nonce, fee, bindKey] or [error code] or [255] |
| leansvm_next_base_fee | floor, target, current, used | [next], or empty if malformed |
fn consensus_genesis(&self, me: u64, validators: u64, genesis: Hash32) -> Option<ConsensusState>
fn consensus_step(&self, state: &ConsensusState, event: &Event) -> Option<(ConsensusState, Vec<Action>)>
fn apply_tx(&self, chain_id: u64, base_fee: u64, sender: (&Address, &Account),
recipient: (&Address, &Account), tx: &Tx) -> Result<TxEffect, TxError>
fn next_base_fee(&self, params: FeeParams, current: u64, used_bytes: u64) -> u64
Each message type has a proven roundtrip, decode? (encode x) = some x, and each export a theorem that it computes the spec function on encoded input. Rust holds the consensus state as an opaque 160-byte value.
Calls from Lean
| Extern | Implementation | Contract |
|---|---|---|
| leansvm_lean_sha3_256 | RustCrypto sha3 0.12 | 32-byte digest |
| leansvm_lean_mldsa44_verify | mldsa-native v2.1.0 | false unless pk = 1,312 B, sig = 2,420 B, context ≤ 255 B, and the signature verifies |
These are opaque declarations; no theorem assumes anything about their results. Lean cannot evaluate them at build time, so code that reaches them is tested from Rust rather than with #guard.
shim.c and core-ffi
core-ffiis the only crate withunsafe; the node crate setsunsafe_code = "forbid". Clippy requires aSAFETY:comment and one unsafe operation per block.shim.cexists because mostlean.hhelpers arestatic inline. One generic call serves everyByteArray → ByteArrayexport; adding an export touches Lean and Rust, not C.- Exports take ownership of their arguments (Lean 4.36 rejects
@&on@[export]), so the shim never releases an input after passing it. - Initialization:
lean_initialize_runtime_module(), then the package’s module initializer, thenlean_io_mark_end_initialization().lean_set_exit_on_panic(true)makes a Lean panic stop the process instead of continuing with a default value.
Lean package layers
| Layer | Contents | Linked into node |
|---|---|---|
| Spec/ | Codec, Primitives (the only @[extern]s), Consensus, Fee, Ledger | yes |
| Proof/ | Theorems about Spec: Consensus, Fee, Ledger | no (library LeanSvmProof) |
| FFI/ | @[export] entry points, encodings, roundtrip and spec theorems | yes |
| Exec/ | Reserved: faster versions of Spec definitions, each proven equal | — |
| Audit.lean | #guard_msgs on the axioms of every main theorem | no |
core-ffi/tests/lean_rules.rs enforces the layering and the placement of @[extern] and @[export], and rejects these anywhere in the core: sorry, admit, native_decide, bv_decide, ofReduceBool, implemented_by, partial, unsafe, while, Float, IO, dbg_trace, ]!, get!, panic!. Indexing uses bounds proofs (b[i]'h). Lake runs with warningAsError = true. No Lake dependencies, no Mathlib.
Build and linking
- Driver
- core-ffi/build.rs → lake build
- Lean output
- static library LeanSvmCore
- Runtime libs
- Init, leanrt, cadical, uv, gmp, c++
- Library path
- lean --print-prefix
- Linux runtime deps
- glibc, libm, libgcc_s
- Lean on servers
- not required
Threads
| Thread | Does | Talks to |
|---|---|---|
| main | Owns LeanCore and Node: slots, block building, mempool, disk, index writes | Lean core |
| HTTP (one per connection) | Parses requests, serves history queries from the index | main, over sync_channel(1024) |
LeanCore is !Send and !Sync; LeanCore::init() returns WrongThread on any thread other than the first. The main loop stops answering requests when the next slot is due.