Cloud archival storage
Cloud archival storage keeps a durable copy of finalized chain and tracked-shard data outside the node's active store. A writer publishes versioned batches and state snapshots to cloud storage. Recent readers follow those batches into a local query store, while historical readers use a snapshot and later changes to build a selected range. See Storage and state, Persistent storage and flat state, and State synchronization and state parts for the wider storage model.
Archive purpose
The archive separates long-term data retention from the hot store. It supports node recovery, historical queries, and a query node that can keep following chain data without running the full consensus and execution path.
Archive roles
The system has three main roles that meet at the cloud bucket:
| Role | Purpose | Durable result |
|---|---|---|
| Writer | Copies finalized block and tracked-shard data from a local store. | Cloud batches, epoch data, heads, and snapshots |
| Recent reader | Follows new cloud batches into a local store. | A queryable store that advances in complete windows |
| Historical reader | Builds a local store for a chosen height range. | A bounded query store with optional shard state |
The writer and readers use separate progress markers. This lets a writer lag or resume without making a reader expose a partially copied window. The cloud archive can also have several writers because each writer can cover a different shard set.
References: chain/…/cloud_archival_writer.rs• L107-117 chain/…/cloud_recent_reader.rs• L55-67 chain/…/cloud_historical_reader.rs• L16-33 core/…/cloud_archival_store.rs• L14-20
Archive data flow
The archive flow turns finalized local data into cloud objects and then reconstructs the local rows that readers need. This diagram shows the relationship between the writer, bucket, and both reader modes.
flowchart LR
F[Finalized local store] --> W[Cloud archival writer]
W --> B[Cloud bucket]
B --> R[Recent reader]
B --> H[Historical reader]
R --> Q1[Recent query store]
H --> Q2[Historical query store]
The writer archives only data below its finalized head. A reader advances its own local chain heads only after the related block and shard rows are present. State snapshots provide a starting root; shard changes then move that root through the requested heights.
References: chain/…/cloud_archival_writer.rs• L331-363 chain/…/cloud_archival_utils.rs• L269-283 chain/…/cloud_historical_reader.rs• L164-197
Cloud storage layout
Cloud objects use a chain-specific prefix and stable logical names. The layout keeps small metadata and progress objects easy to inspect, while block and shard data use bounded batch objects so readers do not fetch one object per height.
Object groups
| Object group | Main content | Reader use |
|---|---|---|
| Metadata | Bucket configuration and the block head | Finds the archive contract and block coverage |
| Shard heads | One head per archived shard | Finds shard coverage for a reader window |
| Epoch objects | Epoch rules, shard layout, and related validator data | Resolves chain context and epoch changes |
| Block batches | Block data for each height in a range | Rebuilds canonical chain rows |
| Shard batches | New or carried chunk data, state changes, receipts, outcomes, and chunk metadata | Rebuilds shard rows and state changes |
| State headers and parts | Snapshot header plus numbered trie parts | Installs a shard state root for historical reads |
The archive stores one entry per height in block and shard batches. A missing entry means the height had no produced block. A shard batch may start above the requested height or end below it when a resharding adds or retires a shard.
References: core/…/file_id.rs• L28-49 core/…/blocks.rs• L117-130 core/…/shards.rs• L434-448 core/…/mod.rs• L76-110
Archive contract
Data blobs are versioned serialized records and are compressed before upload. Metadata heads and configuration are small uncompressed records. The bucket stores its compression and batch settings so later writers must use the same contract.
The block and shard batch range is part of the data contract. Readers validate that the number of entries matches the declared height range before using a batch. This check prevents a damaged or mismatched object from shifting data onto the wrong heights.
References: core/…/archive.rs• L78-112 core/…/archive.rs• L129-174 core/…/retrieve.rs• L107-146 core/…/bucket_config.rs• L3-19
Archive writing
The cloud archival writer runs beside the node and copies only finalized data that the hot store still retains. An archival node processes every block instead of using epoch sync to skip older history, so the local source can provide the continuous chain data that the archive publishes. The writer tracks block data and each shard independently, then uses the lowest component head as the safe common progress point.
Finalized batch flow
When the writer starts, it checks the bucket contract and reads any external heads. It reconciles those heads with local progress, refuses to resume from data already removed by garbage collection, and then uploads complete batches. Epoch data is published before a head can advertise a batch that needs it.
The writer publishes a block head and one head per shard. Local progress records are advanced after the corresponding uploads. A lagging component therefore causes catch-up without forcing already complete components to upload again.
References: chain/…/cloud_archival_writer.rs• L229-261 chain/…/cloud_archival_writer.rs• L711-743 chain/…/cloud_archival_writer.rs• L373-404 core/…/cloud_archival_store.rs• L46-64 chain/…/handler.rs• L204-235
Block and shard records
Block data carries the information needed to rebuild canonical chain and epoch lookups. Shard data carries the information needed to rebuild chunk, receipt, outcome, chunk-extra, and state-change rows.
Shard records have two forms:
- New chunk data carries the chunk and the rows created when the shard processes a new chunk.
- Carried data carries the shard state and execution metadata when the block carries the prior chunk forward.
The writer normalizes node-local measurements before upload when those values would differ between writers. This keeps equivalent writers able to produce the same archive bytes.
References: core/…/blocks.rs• L16-38 core/…/shards.rs• L29-77 core/…/shards.rs• L106-120 core/…/shards.rs• L131-164
Epoch and snapshot publication
Epoch objects give readers the rules needed to interpret blocks around an epoch boundary. They include epoch information, the shard layout, the first block context, and selected data from the epoch below.
State snapshots are produced by the state-sync dump path for tracked shards. A snapshot has one header and a set of numbered state parts. The dump path reuses existing headers and parts when possible, retries external writes, and limits concurrent part creation. The writer can force a snapshot for a resharding epoch even when the normal snapshot cadence would skip it.
References: core/…/epoch_data.rs• L16-43 nearcore/…/state_sync.rs• L274-286 nearcore/…/state_sync.rs• L630-720 nearcore/…/state_sync.rs• L733-787 test-loop-tests/…/cloud_archival.rs• L708-738
Resharding records
Resharding changes the shard layout while the archive still needs a continuous read path. The writer archives carried shards across the boundary, keeps the retired parent through the old boundary, and attaches inverse state changes to new child shards in the short gap below the state-sync point.
Inverse state changes record the value before each changed key. They give a future reader the data needed to walk back from an epoch snapshot when a new child shard does not yet have its own complete snapshot. Readers still have explicit gaps in resharding support, so changes to this area must be checked against both writer output and reader reconstruction.
References: chain/…/cloud_archival_writer.rs• L129-150 chain/…/cloud_archival_writer.rs• L615-708 core/…/shards.rs• L345-379 chain/…/cloud_historical_reader.rs• L75-83
Cloud readers
Readers turn cloud objects into an ordinary local store. They use archive heads to decide what data is available and persist their own position so a restart can repeat incomplete work without exposing it as complete. State-sync recovery follows the same principle for shard state: it reuses local headers and parts when present, validates new data before storing it, and retries failed transfers.
Recent reader
The recent reader follows the bucket as a moving source. It opens a window after its reader head, copies the block batch, waits until every tracked shard has a covering batch, applies the shard data, and then commits the new reader head and query heads together.
This all-components rule matters when block data and shard data arrive at different times. The reader waits rather than serving a chain point whose block rows exist but whose shard rows do not. A restart can safely pull the same window again because the reader head did not move before the window was complete.
sequenceDiagram
participant C as Cloud bucket
participant R as Recent reader
participant S as Local store
R->>C: Read block head
R->>C: Read block batch
R->>C: Check every tracked shard head
R->>C: Read shard batches
R->>S: Write block and shard rows
R->>S: Apply state changes and verify roots
R->>S: Commit reader head and query heads
On first takeover, the reader starts at the local final head and removes height rows above it because those rows may describe a discarded fork. It also checks that each tracked shard has a readable state anchor before following the bucket.
References: chain/…/cloud_recent_reader.rs• L92-115 chain/…/cloud_recent_reader.rs• L145-216 chain/…/cloud_recent_reader.rs• L219-279 chain/…/downloader.rs• L20-27 chain/…/util.rs• L13-21
Historical reader
The historical reader bootstraps a selected inclusive range. It first finds a present block below the range and loads the epoch context for that anchor. For each tracked shard, it finds the newest usable snapshot at or below the start and applies shard batches until the requested range is covered.
The reader can skip state. In that mode it rebuilds chain and shard metadata but leaves the state column empty, so the resulting store can answer non-state queries only. A bootstrap may cover the end of the batch that contains the requested end height because batches are the archive's unit of transfer.
References: chain/…/cloud_historical_reader.rs• L25-113 chain/…/cloud_historical_reader.rs• L116-161 tools/…/historical_reader.rs• L8-20
State reconstruction
State reconstruction starts from the root named by a snapshot header. The reader installs every state part into the shard trie, then applies the recorded state changes in batch order. After each change set, it compares the resulting root with the root recorded in shard metadata.
The reader keeps newly inserted trie nodes visible during a batch before committing them. If a batch fails before commit, the local trie stays at its prior root and the reader can retry from the same saved position.
References: chain/…/cloud_reader_trie_utils.rs• L23-59 chain/…/cloud_reader_trie_utils.rs• L122-193 chain/…/cloud_archival_utils.rs• L210-240
Shard state recovery
Shard state recovery turns a validated state header and its parts into active shard state. The process first gets the header, then downloads missing parts in parallel, applies them to the runtime, restores flat storage, and asks the chain to finalize the result. This path supports both cloud-backed sources and peer-backed state sync through the shared download source.
The recovery path is safe to resume because each part is marked after application. An unfinished apply keeps those marks, while a completed apply clears their meaning before later finalization changes the state. A canceled or failed part remains eligible for another attempt, and a canceled shard does not proceed to finalization.
This diagram shows how shard state moves from a source into the local store and runtime.
flowchart LR
H[State header] --> P[Validated state parts]
P --> D[Local state-part store]
D --> A[Runtime apply]
A --> F[Flat storage and memtrie]
F --> C[Chain finalization]
References: chain/…/downloader.rs• L38-127 chain/…/downloader.rs• L133-210 chain/…/shard.rs• L55-75 chain/…/shard.rs• L101-141 chain/…/shard.rs• L143-247 chain/…/util.rs• L7-21
Reader column contract
The reader rebuilds the columns needed for long-term queries from block, shard, epoch, and state data. It intentionally does not reproduce node metadata, transient state-sync rows, or records used only by block processing and garbage collection.
Every retained local column must have one explicit decision: the cloud reader rebuilds it or it is intentionally skipped. This makes additions to the local store visible as archive contract changes instead of silent gaps in a bootstrapped node.
References: core/…/mod.rs• L114-155 core/…/mod.rs• L157-191 core/…/mod.rs• L207-225
Operational tooling
The cloud archive tool exposes separate commands for inspection, historical bootstrap, recent following, and serving a completed local store. This separation keeps a read-only inspection task from changing the archive and lets operators choose whether a recent reader also serves queries.
Archive commands
| Command | Use |
|---|---|
status | Compare external block and shard heads with local writer, reader, and chain heads. |
bootstrap | Build a local store for a height range, with an option to omit state. |
follow | Run the recent reader and optionally serve queries from the same local store. |
serve | Serve queries from a local store without a cloud-follow process. |
The follow command must use one process for reading and serving because the serving side would otherwise keep a store snapshot from before the follow loop's writes. Operators should use the status output to distinguish cloud coverage from the local chain point that queries currently serve.
References: tools/…/cli.rs• L8-24 tools/…/status.rs• L21-35 tools/…/status.rs• L87-109 tools/…/recent_reader.rs• L54-60
Recovery tooling
The fully qualified script home/…/recover_missing_archival_data.sh is a separate repair procedure for older local archival data. It requires the node to be stopped, uses the cold store, and runs trie restoration and database resharding steps. It is idempotent, but the procedure can take several days, so it is an operational recovery tool rather than part of the normal cloud writer loop.
References: scripts/recover_missing_archival_data.sh• L4-17 scripts/recover_missing_archival_data.sh• L20-32 scripts/recover_missing_archival_data.sh• L35-80
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to batch size, compression, object names, or serialization versions must remain compatible between all writers, readers, and existing bucket objects.
- Changes to block or shard batch ranges must preserve one position per height and the meaning of an empty position for a skipped slot.
- Changes to writer head updates must preserve the order: required epoch data and batch objects first, then external heads, then local progress.
- Changes to tracked shard selection must keep writer coverage aligned with reader coverage when a reader needs complete shard data.
- Changes to state snapshot headers, part numbering, or state roots must stay aligned with the state-sync dumper and historical snapshot installer.
- Changes to shard layouts or resharding boundaries must preserve carried shards, retired parent rows, child inverse changes, and reader anchors.
- Changes to retained database columns must classify each column as rebuilt or skipped in the cloud reader contract.
- Changes to reader commits must keep the reader head and served chain heads in one atomic local update.
- Changes to initial sync phase selection must keep archival nodes on full block sync so the writer does not miss historical chain data.
- Changes to state-part reuse, validation, or apply markers must preserve safe retries for both cloud readers and peer-backed state sync.
- Changes to epoch-sync bootstrap must preserve proof validation and must not make archival nodes skip the historical blocks that the writer needs.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A writer resumes from a cloud head below the local garbage-collection tail | The writer cannot rebuild the missing batch and stops instead of advancing an incomplete archive | Check the minimum component head against local retention before writing |
| A bucket uses different batch or compression settings from a writer | Objects can be placed under incompatible ranges or cannot be read as expected | Validate the stored bucket configuration at writer startup |
| A block or shard batch has a bad range or entry count | Data can be assigned to the wrong height or the reader cannot continue | Validate the blob and reject out-of-range requests |
| A recent reader advances after only block data arrives | Queries can expose a chain point without the shard data needed for its state | Wait for every tracked shard in the window before saving the reader head |
| A state part or shard change produces a different state root | The reconstructed trie is incomplete or belongs to the wrong state | Compare the applied root with the recorded chunk-extra root |
| A state-sync header or part is accepted without validation | The node can persist state that does not match the requested shard root | Validate headers through the chain and validate each part against the state root before storing it |
| A state apply restarts with stale applied-part marks | The runtime can skip parts or combine data from different apply attempts | Keep marks only for an unfinished apply and clear the base state before a fresh apply |
| A new retained column has no reader classification | A bootstrapped store can silently omit data needed for long-term queries | Require each retained column to be rebuilt or explicitly skipped |
| Epoch data is missing near a batch boundary | Readers cannot resolve shard layout or epoch rules for the batch | Publish epoch data before publishing the corresponding head |
| An archival node uses a sync path that skips older blocks | The local source has gaps that the writer cannot publish as a continuous archive | Force archival nodes to use block sync rather than epoch sync |
| An epoch-sync proof fails peer, age, or cryptographic checks | A stale or invalid peer cannot choose the node's sync point or initialize its epoch state | Validate the proof before applying it and reset data only when the proof shows the head is beyond the sync horizon |
Key files
| File | Why to read it |
|---|---|
| chain/…/cloud_archival_writer.rs | Shows the finalized-data loop, independent component heads, restart behavior, and resharding boundaries. |
| core/…/archive.rs | Shows the upload contract for epoch, block, shard, and head objects. |
| core/…/file_id.rs | Shows the stable cloud object layout. |
| core/…/blocks.rs | Shows the block batch shape and skipped-height rule. |
| core/…/shards.rs | Shows new versus carried shard data and inverse changes. |
| core/…/retrieve.rs | Shows download, decompression, deserialization, and validation boundaries. |
| chain/…/cloud_recent_reader.rs | Shows how a live reader waits for a complete window and resumes safely. |
| chain/…/cloud_historical_reader.rs | Shows range bootstrap, anchor selection, and snapshot-based recovery. |
| chain/…/cloud_archival_utils.rs | Shows how cloud records become local chain, shard, receipt, and epoch rows. |
| chain/…/cloud_reader_trie_utils.rs | Shows snapshot installation and state-change application at a high level. |
| chain/…/downloader.rs | Shows local reuse, validation, retry, and persistence of state headers and parts. |
| chain/…/shard.rs | Shows parallel part recovery, resumable apply, flat-storage setup, and chain finalization. |
| chain/…/util.rs | Shows the local state-header lookup and state-sync result metrics. |
| chain/…/epoch.rs | Shows proof validation and epoch-state bootstrap used when a node starts far behind. |
| nearcore/…/state_sync.rs | Shows how state snapshot headers and parts are generated and uploaded. |
| core/…/mod.rs | Defines the reader's rebuilt-column contract and public cloud access model. |
| tools/…/cli.rs | Lists the operator entry points for status, bootstrap, follow, and serve. |
| tools/…/status.rs | Shows which local and external heads an operator can compare. |
| scripts/recover_missing_archival_data.sh | Documents the separate local archival recovery procedure. |