Storage and recovery
Data directory layout, record formats, write order, startup replay, snapshots, index and verify.
On this page · 8
Data directory
data/
- genesis.txt
- chain id, genesis time, slot length, fee floor and target, validators, allocations
- validator.key
- the validator's key, a hex seed readable only by its owner; back it up
- consensus.state
- the core's state, replaced atomically before any of its actions run
- pending/
- a proposed block, written before it is proposed
- blocks.log
- finalized blocks, append-only, each record with a SHA3-256 checksum
- snapshot
- all accounts after a finalized block, and where that block sits in the log
- snapshot.prev
- the snapshot before it; startup begins from the older of the two
- index.redb
- blocks, transactions and account history for the API; rebuilt from the log if needed
blocks.log is opened for append and locked with try_lock; a second node on the same directory exits with “in use by another leansvm-node”.
Write order
| Step | Write | Before |
|---|---|---|
| Consensus step | consensus.state (atomic) | executing any returned action |
| Proposal | pending/<block hash> (atomic) | proposing the block |
| Finalization | append to blocks.log + sync_all | applying the block |
| After apply | index.redb (durability Immediate) | — |
| Every snapshot_every slots | snapshot (atomic), old one → snapshot.prev | — |
write_atomic
write <name>.tmp → fsync → rename to <name> → fsync directory
blocks.log
record
len u32 LE ‖ block encoding (len bytes) ‖ SHA3-256(block encoding)
block encoding = slot u64 ‖ proposer u64 ‖ parent (32) ‖ tx_count u32 ‖ (tx_len u32 ‖ tx)*
- Max record
- 8 MiB (larger length = damage)
- Checksum
- SHA3-256, 32 B, after the body
- Empty block
- ≈ 88 B on disk
- Reader
- streaming, never cuts the file
| On open | Condition | Action |
|---|---|---|
| Torn tail | short length/checksum or record past EOF, and consensus.state one block ahead, and pending/ holds that block | drop the partial append, re-append from pending/ |
| Torn tail | any other case | refuse to start; file untouched |
| Checksum mismatch | not the last record | refuse: “damaged at byte N … restore it from a backup” |
Snapshots
snapshot v2 (LE)
"leansvm/snapshot" (16) ‖ version u32 = 2 ‖ genesis_hash (32)
‖ height u64 ‖ head_slot u64 ‖ head_hash (32)
‖ record_start u64 ‖ record_end u64 // the head block's record in blocks.log
‖ base_fee u64 ‖ burned u128 ‖ account_count u64
‖ (address (32) ‖ balance u64 ‖ nonce u64 ‖ key_flag u8 ‖ [public_key 1312])* // address order
‖ SHA3-256(everything above)
- Interval
- --snapshot-every, default 3,600 slots
- Generations
- snapshot, snapshot.prev
- Startup picks
- older usable one whose record matches the log
- Status retention
- rejected / dropped kept one interval
- Written on
- main thread (holds every account)
- Rejected if
- bad checksum, other chain, version ≠ 2, duplicates, trailing bytes
Startup
- Lock
blocks.log, openindex.redb, loadgenesis.txtandvalidator.key(the key must match the genesis validator). - Load the older usable snapshot whose height is not above the index height; if there is none, start from genesis.
- Replay the later blocks through
applyTxandnextBaseFee(≈ 20,000 tx/s), indexing blocks the index lacks in batches of 1,000. - If
consensus.stateshows a finalized block the log lacks, take it frompending/, drop a partial trailing record, then append and apply it. Any other unreadable record stops startup. - Clear
pending/. An index that does not match the log is rebuilt from genesis (INDEX_MISMATCH).
Chain index (index.redb)
| Table | Key | Value |
|---|---|---|
| blocks | height u64 | height, slot, hash, parent, proposer, txs, tx_bytes, base_fee, fees u128 |
| block_hashes | hash (32) | height u64 |
| txs | tx id (32) | id, height, slot, index, sender, recipient, amount, nonce, max_fee, priority_fee, fee, size |
| block_txs | height u64 BE ‖ index u32 BE | tx id |
| account_txs | address (32) ‖ height u64 BE ‖ index u32 BE | tx id (inserted for sender and recipient) |
redb 5: copy-on-write B-trees, checksums, readers that do not block the writer. Derived data only; only applied transactions are indexed.
genesis.txt
genesis.txt
# Lean SVM genesis
chain_id=1
slot_ms=1000
genesis_time_ms=1764633600000
fee_floor=1
block_target_bytes=500000
validator=<1,312-byte public key, hex>
alloc=<address hex>:<units>
alloc=<address hex>:<units>
key=value, no spaces around=;#lines and blank lines ignored; unknown keys rejected.allocamounts are units, without duplicates, and must sum to exactly 10^15.genesis_time_msis when slot 1 starts;initsets it to the current time.- Genesis hash = SHA3-256(
"leansvm/genesis/v1"‖ parameters ‖ validator key ‖ allocations).
verify
shell
leansvm-node verify data # node stopped
Replays blocks.log from genesis through the core and compares the result with both snapshots, the chain index and consensus.state.