Skip to content

State database ​

This page describes the current version

The internals on this page can change between v0.x versions. The database schema can also change. A new version can need a new import.

Cinder keeps its state in libmdbx, the storage engine that reth also uses. The schema is Cinder's own. This page describes the tables, how the node stores the tries, the undo records, the cross-block cache and the views that read the database.

The current schema has the number 3.

Design ​

The schema has three main ideas:

  • Hashed flat state. Accounts and storage slots are in flat tables, keyed by the keccak hash of the address and of the slot. Execution reads a value with one lookup, not with a walk down the trie.
  • Tries by path. The trie tables store a node under its path, not under its hash. When a block changes a node, the new node replaces the old one at the same key. The database keeps no old trie nodes.
  • Few stored nodes. The trie tables store only branch and extension nodes. The node rebuilds leaves and bottom branches from the flat state when it needs them.

Each write of the node is one MDBX write transaction, and each commit is durable (synced). The blocks that the node keeps in memory are not in the database until the writer thread writes them.

Tables ​

State ​

TableKeyValue
acctkeccak(address)RLP of the account: nonce, balance, storage root, code hash
storkeccak(address), with one entry for each slot (dupsort)keccak(slot) followed by the value, big-endian, without leading zeros
codecode hashbytecode
atriepacked pathbranch or extension node of the account trie
striekeccak(address) followed by the packed pathbranch or extension node of a storage trie

Blocks and history ​

TableKeyValue
hdrblock number (8 bytes, big-endian)block hash followed by the header RLP
hnumblock hashblock number, for every stored header
bodyblock numberbody RLP: transactions, ommers, withdrawals
rcptblock numberRLP list of the network encodings of the receipts
sndrblock numbertransaction senders, 20 bytes each
txidxtransaction hashblock number (8 bytes) and index (4 bytes)
lfltblock numberthe log filter row of the block (from 0.9.5, see below)
undoblock numberthe reverse state diff of the block (see below)
metanameschema: the schema number. head: number and hash of the database head. safe, finalized: number and hash of the safe and finalized blocks of the last forkchoice update

The node writes the history of a block (body, rcpt, sndr, txidx) in the same transaction as the state of the block. When a reorg replaces written blocks, the write of the new branch deletes their history in the same transaction. History exists from the block where the node started. The import from reth does not copy bodies or receipts.

Log filter rows ​

From 0.9.5, the node writes a log filter row (lflt) for each block with its receipts, and deletes it with them. The row holds a version byte, the first 8 bytes of the block hash and a 24-bit fingerprint of each distinct key of the logs of the block: each address, and each topic with its position. eth_getLogs, eth_getFilterLogs and eth_getFilterChanges read the receipts only of the blocks whose row has a fingerprint of the filter's address and of a topic at each position that the filter sets. A fingerprint can pass for a key that the block does not have, never the other way, so the answers do not change. The header bloom of a mainnet block is mostly full, so it lets 30 to 46% of the blocks through for a filter with few matches. The rows add about 4.9 KB per block on disk.

A block without a row, or with a row of another block hash (an older version wrote or deleted the block), is read as before. For the older blocks of a database, cinder --build-log-filters --db DIR writes the rows. Read Log filter rows.

Trie nodes by path ​

The key ​

A packed path holds two nibbles in each byte, then one byte with the number of nibbles. Every path, from the root (no nibbles) to 64 nibbles, has a key of 33 bytes. A storage trie key starts with the 32-byte hash of the account address, so it has 65 bytes.

The value ​

The value is two bytes of kinds (little-endian), then the RLP of the node. The node sets bit i of kinds when child i of a branch is a branch or extension node, stored or rebuilt. A child with its bit clear is a leaf.

What the tables do not store ​

The tables do not store these nodes:

  • Leaves. A leaf is one key and one value from the flat state.
  • Bottom branches. A bottom branch is a branch whose children are all leaves.
  • Small nodes. A node with an RLP of less than 32 bytes is inside the RLP of its parent.

A root node that is a branch or extension (not a bottom branch) is in the table at the empty path, also when it is small. A root that is a leaf or a bottom branch is rebuilt, as any other leaf or bottom branch.

When a trie walk reaches a node that is not in the table, the database rebuilds it from the flat tables. It reads the flat records whose key starts with the path of the node:

  • One record under the path gives a leaf.
  • Two to 16 records give a bottom branch.

Without bottom branches, a full mainnet import became smaller, from 434 GB to 262 GB (schema 1 against schema 2).

The database checks every node ​

Every node that the database gives to a trie must hash to the reference that its parent holds. This includes the nodes that the database rebuilds. A missing or old node is a corruption error, not a wrong trie. The database never returns a node that does not match.

Undo records and the undo window ​

For each block that it writes, the node also writes an undo record. The undo record is the reverse diff of the block: the values before the block of every account, slot and code that the block changed. If the node applies the undo record to the state after the block, it gets the state before the block.

The node keeps the undo records of the last 128 written blocks. This is the undo window. Each forward commit deletes the record that leaves the window, also when the block has no record of its own.

The node uses the undo records for three tasks:

  • Reorgs below the database head. The node executes the blocks of the new branch over reverse layers made from the undo records. When the branch becomes the head, the writer thread writes it in one transaction. The transaction deletes the header, history and undo record of each old block above the fork base and writes the new blocks. The state root of the commit is one replay of the undo records and the new diffs over the old database head. The database is never on the fork base. For details, read Writing blocks.
  • State below the head for JSON-RPC. A request for an older block lays the undo records of the later blocks over the state of the head. For more, read JSON-RPC.
  • Benchmarks. cinder-bench-node executes recent blocks again over the undo records, as read-only. Next to a node that writes the database, a run that sees a commit fails with the retry error -10 (#24, open).

A block that clears storage that existed cannot be undone with a diff. The node writes no undo record for such a block. A fork below that block is then not possible, and the node answers SYNCING.

The cross-block cache ​

The cross-block cache keeps values of the database head in memory, from one block to the next. It holds three kinds of entries:

  • Accounts, including accounts that do not exist.
  • Storage slots, including empty slots.
  • Trie nodes, both stored nodes and rebuilt nodes, keyed by owner and path. A trie node with an RLP of more than 532 bytes does not go into the cache.

--cache-mb sets the size (default 1024 MB, 0 turns the cache off). The cache uses a fixed amount of memory: 20% for accounts, 45% for slots and 35% for trie nodes. Each table is four-way set-associative, with one lock for each set. So the parallel readers of one block can use it at the same time. A full set replaces its least used entry. The node asks the kernel to put the tables on huge pages.

Trie nodes need no invalidation ​

A cached node is valid only for the hash that its parent expects. The cache compares the hash on each read. An old node at the same path is a miss. So a commit can put its new trie nodes in the cache at once, before the write is complete. The next block then finds the new upper nodes of the tries in memory.

Accounts and slots use epochs ​

Account and slot entries must hold the values of the current database head. A reader that opened an older snapshot must not put its older values in the cache. The cache uses an epoch counter for this:

  1. A view reads the epoch before it opens its snapshot.
  2. Before a commit, the committing thread increments the epoch. An odd epoch means that an update is in progress.
  3. When the new values are in the cache, the thread that applied them increments the epoch again.
  4. A reader puts an account or slot in the cache only if the epoch did not change since its snapshot and was not odd at that time.

The writer thread does not apply its account and slot updates at its commit. The engine thread applies them when it publishes the batch, when no block executes. For the reason, read Writing blocks.

When a block clears the storage of an account, the cache increments a counter for that account. All cached slots of the account with an older counter become misses.

Other caches ​

The node also keeps contract bytecode, JUMPDEST analyses and precompile results across blocks. These caches use hashes as keys and need no invalidation. For details, read Block pipeline.

Views ​

A view is one MVCC snapshot of the database. A block reads all of its state through one view: the flat state, the code, the headers and the trie nodes. The view also rebuilds leaves and bottom branches.

Many threads read one view in parallel: the executing thread, the prewarm, the coordinator of the streaming state root and its I/O pool, the trie prefetch and the storage roots. In MDBX each thread needs its own read transaction.

Pinned views ​

The chain opens its views pinned:

  1. The view opens the read transactions of all its threads at once.
  2. It checks that all of them have the same transaction ID.
  3. If a commit came between two opens, it closes them and tries again, up to 8 times.

Then each thread takes one of the open transactions. A commit after the open does not reach any of them. Without a pin, a thread opens its transaction at its first read. If a commit came before that read, the thread gets an error.

The chain also checks that the head of the snapshot is the database head that it knows. If the writer committed a batch that the engine thread did not publish yet, the view is newer than the chain. The chain then publishes the batch first and runs the block again.

Views of JSON-RPC requests ​

A JSON-RPC request reads its own snapshot. This snapshot can be different from the head that the cache holds, so the state queries and the calls do not use the cross-block cache. One exception is eth_sendRawTransaction: its admission check reads the head state through the cache.

In the node, a request also takes the chain's snapshot of its canonical blocks in memory: the blocks not written yet, or a branch on a fork base below the database head. The engine thread publishes a new snapshot after each forkchoice update and after each written batch. A request reads the blocks of the snapshot from memory (header, body, receipts, state diff) and the older blocks from its database snapshot. Thus the block tag latest is the canonical head, and pending comes after it. A block of the snapshot stays in memory while a request holds the snapshot, also after the writer wrote it. safe and finalized are the blocks of the last forkchoice update, which the node also writes to the meta table with its blocks. cinder --serve reads the database only. Read JSON-RPC.

A request reads its database snapshot through one MDBX read transaction. The state view of a call, a trace or a proof reads through the same transaction, so it has the same snapshot. The transactions come from a pool of reset transactions (at most 128). A new read transaction binds a reader slot under a lock that all processes on the database share. A reset transaction keeps its slot and holds no snapshot, so the writer can use the old pages again. At the start of a request, the node renews the transaction on the newest snapshot, without the lock. At the end of the request, the node resets it and puts it back into the pool. If the pool is full, the node closes it. Each item of a batch does the same. Before it opens its read transaction, a request takes a reader permit, and each item of a batch takes one in turn. Read MDBX settings.

Overlay of unwritten trie nodes ​

A view can have an overlay: a function that returns trie nodes by hash before the view reads the cache or the database. The chain uses it for a block whose parent is not written yet. The overlay gives the trie nodes that the unwritten ancestors changed. The coordinator of the streaming state root reads through the same overlay while the block executes. For details, read Block pipeline.

MDBX settings ​

  • The node opens the environment without read-ahead and without sticky threads. A thread can hold a read snapshot while it writes.
  • The reader table has one slot for each read transaction of all processes on the database. The node asks for 4096 slots, and MDBX makes a table of 4208 slots. The node keeps 512 slots for its own reads: block import, the writer, the warm-up, the Engine API and other work. The JSON-RPC requests of a process take reader permits for the other slots: 3696 permits. The first process that opens the database sets the size of the table. A process that opens the database while another process has it open gets the table of that process. For example, a process of 0.9.6 or earlier makes a table of 624 slots. 0.9.6 and earlier asked for 512 slots and had no permits (#321). For the effect on JSON-RPC, read More JSON-RPC load.
  • The writer uses the oldest free pages of the file first, the default order of MDBX (#294). 0.9.6 and earlier used the newest free pages first (MDBX_LIFORECLAIM), and the older free pages stayed unused. An existing database uses its free pages again. No new import is necessary. For the effect on the disk size, read Disk.
  • The database file grows in steps of 4 GiB, up to 4 TiB. It does not become smaller on its own.
  • The page size is the page size of the operating system (4 KB on x86-64), unless cinder-import --page-size set another size when it created the database. In a test with 16 KB pages, writes were faster but latency was worse, on a machine with a small page cache.
  • Every commit of the node is durable (synced). A test without the sync at each commit made the mean time of a block longer, so the project did not keep it.
  • Each commit copies every page that it changes and the B-tree branches above it. So the node writes about 65.2 MB to the disk per mainnet block, about 3.2 times more than reth 2.7 (live, 2026-10-16, #59). For the measurement, read Writing blocks.

The database directory also holds two files of the P2P part: nodekey and p2p-nodes. For these files, read P2P.

For the disk size against reth, read Performance.