↑ ↓ navigate↵ open

Architecture

The Lean/Rust split, FFI entry points and encodings, package layers, build and threading.

On this page · 7
  1. 01Responsibilities
  2. 02Entry points
  3. 03Calls from Lean
  4. 04shim.c and core-ffi
  5. 05Lean package layers
  6. 06Build and linking
  7. 07Threads

Responsibilities

Lean core (proven, static library)Rust node
State transition: applyTxHTTP API; validator network from v2
Authorization: signature result, nonce, feeMempool and block building
Consensus decisions: stepSlot clock
Fee rule: nextBaseFeeDisk: 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

HTTP threads→channel→main thread

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
The core reaches the cryptography through @[extern]; everything else it needs, Rust hands it as bytes.

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.

ExportInputOutput
leansvm_consensus_genesisme, validators, genesis hash (4)state (20 words), or empty if invalid
leansvm_consensus_stepstate (20) ‖ eventstate (20) ‖ actions (10 words each), or empty if malformed
leansvm_apply_txfields (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_feefloor, target, current, used[next], or empty if malformed
LeanCore (core-ffi)
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

ExternImplementationContract
leansvm_lean_sha3_256RustCrypto sha3 0.1232-byte digest
leansvm_lean_mldsa44_verifymldsa-native v2.1.0false 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-ffi is the only crate with unsafe; the node crate sets unsafe_code = "forbid". Clippy requires a SAFETY: comment and one unsafe operation per block.
  • shim.c exists because most lean.h helpers are static inline. One generic call serves every ByteArray → ByteArray export; 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, then lean_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

LayerContentsLinked into node
Spec/Codec, Primitives (the only @[extern]s), Consensus, Fee, Ledgeryes
Proof/Theorems about Spec: Consensus, Fee, Ledgerno (library LeanSvmProof)
FFI/@[export] entry points, encodings, roundtrip and spec theoremsyes
Exec/Reserved: faster versions of Spec definitions, each proven equal—
Audit.lean#guard_msgs on the axioms of every main theoremno

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

ThreadDoesTalks to
mainOwns LeanCore and Node: slots, block building, mempool, disk, index writesLean core
HTTP (one per connection)Parses requests, serves history queries from the indexmain, 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.