Introducing a new way to review your PRs.

Read the announcement

celestiaorg / celestia-core

A fork of CometBFT

View on GitHub

20 documents · Updated Nov 23, 2026

docs / state-and-block-storage

State and block storage

State and block storage preserve the chain data needed to restart a node, validate new blocks, answer history queries, recover after a crash, and prove recent validator behavior. The state store keeps the latest execution state plus selected historical data. The block store keeps contiguous block data and the metadata that lets the node find and verify it. Read this page with State execution to connect persisted data with the execution flow that creates it.

Storage model

The storage model separates data by its role in chain progress. The state store answers “what state and rules apply at this height?” The block store answers “what block, parts, and votes belong to this height?”

flowchart LR
    A[Committed block and application result] --> B[State store]
    A --> C[Block store]
    B --> D[Latest state snapshot]
    B --> E[Historical validators and consensus parameters]
    B --> F[FinalizeBlock responses]
    C --> G[Block metadata]
    C --> H[Block parts]
    C --> I[Commits and seen commits]
    C --> J[Transaction result indexes]
    D --> K[Restart and block validation]
    G --> L[Block lookup and retention]
    H --> L
    I --> L
    J --> M[Transaction history queries]

The two stores use the same database abstraction but have different retention rules. State data is often represented by a change height or checkpoint instead of a full copy at every height. Block data remains a contiguous range from the stored base height through the latest height until pruning advances that base.

State snapshots

The state snapshot is the latest committed consensus state. It includes the chain identity and height, the current and next validator sets, consensus parameters, application hash, result hash, and application timeouts. A node loads this snapshot on start and falls back to the genesis document when the database has no state yet (state/state.go• L39-82, state/store.go• L142-181).

Saving a snapshot also records the validator and consensus-parameter information needed for the next height. The write uses one synchronized batch so a restart does not see a new snapshot without its related validation data (state/store.go• L209-248). State sync can install a snapshot at a non-zero height through the bootstrap path. That path also creates the surrounding historical entries and records the sync height needed to resume block sync after a restart (state/store.go• L251-293).

References: state/state.go• L39-82 state/store.go• L53-88 state/store.go• L142-181 state/store.go• L209-248 state/store.go• L251-293

Historical validation data

Historical validator sets and consensus parameters support validation of blocks and evidence after the node has moved past the height where those values were active. The store writes a full validator set when it changes and at large-height checkpoints. Other heights point back to the last change, so loading a height reconstructs the effective validator set and proposer priority instead of storing a full copy each time (state/store.go• L608-653, state/store.go• L678-710).

Consensus parameters follow the same change-height pattern, without the periodic validator checkpoint. A load first reads the entry for the requested height and then follows its last-change height when the entry does not contain a full value (state/store.go• L717-787).

References: state/state.go• L58-73 state/store.go• L608-653 state/store.go• L678-710 state/store.go• L717-787

FinalizeBlock responses

FinalizeBlock responses provide execution results for history queries and crash recovery. The store can retain a response for every height when response retention is enabled. It always keeps the most recent response in a separate record so the node can recover when the application has committed but the state snapshot was not saved yet (state/store.go• L477-520, state/store.go• L522-603).

The latest-response record includes its height. Recovery rejects it when that height is not the height the caller expects. Older data can use the legacy response format, which the loader converts to the current response shape (state/store.go• L522-561, state/store.go• L825-873). When full response retention is disabled, height-specific loads report that the responses were not persisted. This does not remove the latest response needed for recovery (state/store.go• L477-491, state/store.go• L580-603).

References: state/store.go• L42-49 state/store.go• L477-520 state/store.go• L522-603 state/store.go• L825-873

Block persistence

The block store treats a block as a group of related records. It saves the block parts first, then the block metadata. Metadata acts as the durable indication that the complete block can be loaded. The store also keeps a height range descriptor so restart can restore the first and last contiguous block heights (store/store.go• L37-52, store/store.go• L598-629, store/store.go• L756-769, store/store.go• L851-875).

Block metadata

Block metadata is the compact identity and summary of a block. It contains the block ID, part-set header, header, encoded block size, and transaction count. The part-set header tells the loader how many parts belong to the block and which part-set hash they must produce (types/block_meta.go• L11-27, types/block_meta.go• L29-49).

The block store also maps a block hash to its height. A lookup by height reads metadata directly. A lookup by hash resolves the height first and then uses the same height-based path (store/store.go• L287-330). This keeps metadata useful even when the full block parts are later removed for evidence retention.

References: types/block_meta.go• L11-27 types/block_meta.go• L29-85 store/store.go• L174-204 store/store.go• L287-330 store/store.go• L789-815

Block parts

A block part is a fixed-size piece of the serialized block plus its Merkle proof. A part set records the total part count and root hash. Loading a block uses metadata to create the expected part set, reads every part, and joins the bytes before decoding the block (types/part_set.go• L50-76, types/part_set.go• L147-177, store/store.go• L184-238).

The save path requires a complete part set and contiguous block height. It writes all parts before metadata, which prevents a visible metadata record from pointing to an incomplete block. Large part sets may write parts outside the main batch to limit batch size, but metadata is still written only after the parts are present (store/store.go• L672-737).

References: types/part_set.go• L50-76 types/part_set.go• L212-273 types/part_set.go• L572-725 store/store.go• L184-238 store/store.go• L672-737

Commit records

Commit records preserve the validator votes that support a block. The block at height H contains the last commit for height H-1, so the block store writes that commit under height H-1. It also stores the locally seen commit for H, which lets a restarted node continue from the latest committed height before the next block carries that commit (types/block.go• L863-955, store/store.go• L332-416, store/store.go• L722-735).

When vote extensions are active, the store keeps an extended commit in addition to the normal commit. The normal form supports the usual commit path. The extended form keeps vote extension data for consumers that need it. The store validates that extension data is present when saving this form (types/block.go• L743-789, types/block.go• L1079-1171, store/store.go• L631-669).

References: types/block.go• L623-713 types/block.go• L743-789 types/block.go• L863-955 types/block.go• L1079-1171 store/store.go• L332-416 store/store.go• L631-669 store/store.go• L722-735

Transaction result indexes

Transaction result indexes connect a transaction hash to the block position and execution summary needed by history queries. The stored information includes the block height, transaction index, status, gas use, signers, and an error log for failed transactions (store/store.go• L928-985).

The index is written for every transaction after execution results are matched to the block's transaction list. The write path can use hashes prepared during block assembly, which avoids repeating hash work for large transactions while keeping lookup keys stable (store/store.go• L930-968, store/store_tx_info_test.go• L41-74).

References: store/store.go• L928-985 store/store_tx_info_test.go• L19-74

Retention and compaction

Retention removes old logical records while preserving the minimum history needed for validation, evidence, recovery, and configured queries. Compaction is a later database task that rewrites storage so deleted records no longer occupy the same physical space. The two actions are separate: pruning changes what can be loaded, while compaction improves the database layout.

State pruning

State pruning removes heights in the half-open range [from, to). It deletes old response records and removes validator or parameter entries unless they are needed as a change target, a validator checkpoint, or evidence support. The target height must remain available so the store can resolve the records that it keeps (state/store.go• L295-328, state/store.go• L334-436).

The prune operation writes in reverse height order and flushes large batches as it works. It may retain some older records when an incremental prune cannot scan the non-ordered keys efficiently. If pruning reaches the configured compaction interval, it starts background compaction rather than making the caller wait (state/store.go• L334-464).

References: state/store.go• L295-328 state/store.go• L334-436 state/store.go• L439-464

Block pruning

Block pruning advances the base to a requested retain height and removes older block parts, hash links, transaction links, seen commits, and most commit records. It loads each block before deletion so it can remove transaction-hash indexes and determine the evidence boundary (store/store.go• L418-475).

The evidence window protects data that can prove malicious validator behavior. For heights inside that window, pruning removes parts so the full block is no longer loadable, but keeps block metadata and commit data. Heights below the window lose those records as well. The base descriptor is updated before each flushed batch because a database batch is not assumed to be atomic (store/store.go• L447-505).

References: store/store.go• L418-475 store/store.go• L477-535 store/store.go• L756-769

Database compaction

Compaction runs in the background after enough state or block data has been pruned. Each store allows only one compaction at a time. A close waits for an active compaction before it closes the database, which prevents the background task from using a closed handle (state/store.go• L428-465, state/store.go• L818-823, store/store.go• L537-595, store/store.go• L782-785).

The block store compacts the height-keyed record families across the newly pruned range. Hash-keyed records are left to the database's normal background work. Range bounds are a space-saving hint rather than a data rule, so live records remain safe even when decimal height keys do not sort perfectly across digit boundaries (store/store.go• L537-595).

References: state/store.go• L428-465 state/store.go• L818-823 store/store.go• L537-595 store/store.go• L782-785

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the state snapshot write and its next-height validator and consensus-parameter records aligned. A mismatch can make a valid snapshot unable to validate the next block.
  • Preserve the height relationship between a block's embedded last commit and the separately stored commit. Recovery and evidence checks use different commit records.
  • Keep block metadata creation after complete part persistence. A metadata record is treated as evidence that the block parts can be loaded.
  • Keep transaction result indexes keyed by the same transaction hashes used by history queries. Precomputed hashes must still match the block's transaction order.
  • Update both normal and extended commit retention when changing block pruning. Evidence and vote-extension consumers may need different records.
  • Keep prune boundaries consistent with the evidence retain height. Removing a header or commit too early can prevent proof of validator faults.
  • Review compaction triggers and close behavior together. A new background database task must not outlive its database handle.

Important failure modes

TriggerConsequenceGuard
A state is saved without matching validator or consensus-parameter historyThe node cannot load the rules needed for a later heightSave the related records in the same synchronized state write
Block metadata exists but a part is missing or corruptA block load returns no block or raises a storage corruption failureWrite complete parts before metadata and validate parts on load
A height-specific response is requested while response retention is disabledThe response is unavailable even though crash recovery can still workCheck the response-retention option before relying on historical results
Transaction results do not match the block transaction countThe transaction index is not writtenRequire one execution result for each block transaction
A transaction result is indexed under a wrong or stale hashA history query misses the transaction or returns another transaction's resultUse the block's valid hash set in its transaction order
Pruning removes metadata or commits inside the evidence windowThe node loses data needed to verify evidenceCalculate and preserve the evidence retain height
A prune or compaction runs while its database is closedThe background operation can fail or access an invalid handleWait for in-flight compaction during store close
A block is saved at a non-contiguous heightThe block range descriptor no longer represents one continuous chainRequire the next block height unless the store is empty

Key files

FileWhy to read it
state/store.goDefines state snapshots, historical validation data, response retention, state pruning, and state compaction.
state/state.goDefines the state snapshot that drives block validation and restart.
store/store.goDefines block metadata, parts, commits, transaction result indexes, block-range tracking, block pruning, and block compaction.
store/store_tx_info_test.goShows the transaction index contract and the benefit of reusing prepared transaction hashes.
types/block_meta.goDefines the identity and summary retained for each block.
types/part_set.goDefines block part boundaries, completeness, and proof-backed reconstruction.
types/block.goDefines blocks, headers, commits, and extended commits that cross the storage boundary.
state/store_test.goShows the guarantees for validator lookup, response retention, state pruning, and state-sync bootstrap height.
store/store_test.goShows the guarantees for block loading, evidence retention, crash-safe re-pruning, and asynchronous compaction.