State synchronization and state parts
State synchronization lets a node recover shard state from a trusted chain point without replaying the full chain. The system chooses a final sync point, transfers a verified snapshot header and its partitioned state parts, rebuilds local trie and flat state, and then resumes normal block processing. See Storage and state for the wider storage model, Persistent storage and flat state for the storage layers used after recovery, and Cloud archival storage for older snapshot retention.
Synchronization model
Shard snapshot
A shard snapshot is the state of one shard at a chain-defined boundary. Each tracked shard is an independent recovery unit, but all units use the same sync point and must finish before the node resets its heads and continues processing.
The sync point differs by protocol mode. In the non-SPICE path, the point is reached after every shard has enough new chunks and the snapshot represents state before the sync block's chunk. In the SPICE path, the point is the first block of the epoch once it is final, and the snapshot represents the state left by that block's chunk. Its later certified execution result supplies the committed state root.
References: chain/…/utils.rs• L164-194 chain/…/utils.rs• L202-259 chain/…/mod.rs• L305-347
Synchronization point
The synchronization point is a final chain anchor that gives the snapshot a stable meaning. Non-SPICE selection waits for two new chunks in every shard; a missing chunk delays the point because that shard may still have older state. SPICE can use the epoch's first block because every shard advances at each block.
When headers arrive out of order, the chain can derive the point again by walking the final chain. It does not select a block above the final head. The node also downloads the prior and extra blocks needed to apply the first post-snapshot chunks and resume block processing.
References: chain/…/utils.rs• L262-317 chain/…/utils.rs• L320-389 chain/…/utils.rs• L426-463 chain/…/tests.rs• L94-161 chain/…/tests.rs• L164-218
Sync lifecycle
The state sync coordinator manages block prerequisites and runs one recovery task for each tracked shard. It limits peer downloads and state application separately so recovery can use parallel work without overloading storage or the network.
The lifecycle moves from chain context to shard snapshots and then back to normal processing:
flowchart TD
A[Choose final sync point] --> B[Obtain sync block and extra blocks]
B --> C[Start one recovery unit per tracked shard]
C --> D[Download and verify each shard snapshot]
D --> E[Rebuild local state]
E --> F[Reset chain heads]
F --> G[Resume block processing]
A -->|point is stale| H[Return to epoch synchronization]
An epoch sync proof can show that the node is beyond the useful recovery horizon. In that case, the node requests a data reset. If the proof leaves the node inside the horizon, it keeps the store and selects a suitable sync phase instead of resetting on the peer's advertised height alone.
References: chain/…/mod.rs• L43-71 chain/…/mod.rs• L73-109 chain/…/mod.rs• L305-349 chain/…/mod.rs• L352-443 chain/…/epoch.rs• L163-176 chain/…/epoch.rs• L566-585
State partitioning
State parts
A state part is a self-contained range of serialized trie state. The range is paired with enough boundary and sibling data to prove its place under the shard state root. This lets the receiver validate one part without holding the other parts.
Parts target similar memory use rather than equal key counts. The part count is tied to the snapshot's state size, and a part number is meaningful only with that total count. This keeps the producer and receiver aligned on the same partition.
The producer can form the range from flat state, then resolve referenced values from trie state and rebuild the part's trie view. The resulting boundary data and trie nodes use the same recorded-read model as validation, so disk, flat-state, and in-memory trie access do not change the part's meaning.
References: core/…/state_parts.rs• L1-18 core/…/state_parts.rs• L90-111 core/…/state_parts.rs• L158-300 core/…/trie_recording.rs• L116-137 core/…/trie_storage.rs• L309-361 core/…/state_sync.rs• L351-355 core/…/state_part.rs• L18-29
State part transfer
Peer transfer exposes two requests for each shard and sync point: one for the snapshot header and one for a selected part. The requester also sends the snapshot identity used by hosts to advertise the matching state.
The downloader first reuses a matching header or part already stored locally. For new data, it downloads the item, validates it against the shard state, and stores it only after validation. Failed downloads and failed part checks are retried, while cancellation stops the work.
The shard flow asks for parts in a shuffled order and runs several requests at once. It keeps only failed part indexes for the next attempt, which preserves download parallelism without treating completion order as part order.
References: chain/…/downloader.rs• L20-35 chain/…/downloader.rs• L38-127 chain/…/downloader.rs• L133-205 chain/…/shard.rs• L87-141 chain/…/util.rs• L13-21
Peer response matching
The network bridge matches every update to the shard, sync point, and header or part requested. It accepts host data only from the peer selected for that request. A network report that a request was not sent may arrive before the selected peer is known, so the bridge holds only that network-generated report until peer selection completes and drops early host messages.
This ordering protects both correctness and retry speed. A failed direct connection ends the pending wait, while an unrelated or early peer message cannot complete another request or fill its pending buffer.
References: chain/…/network.rs• L40-181 chain/…/network.rs• L235-304 chain/…/tests.rs• L467-611
State reconstruction
Snapshot verification
The header is the first trust boundary. It binds the requested shard and sync point to the state root that every part must represent. Non-SPICE headers prove the relevant chunk and receipt links. SPICE headers bind the root to a certified execution result for the sync block's chunk.
The receiver then checks each part independently against the header root and its assigned range. Validation walks the expected trie interval through partial storage and rejects both missing nodes and extra nodes. A complete download is not enough: the node accepts the snapshot only when the checked parts can rebuild the expected trie.
References: chain/…/adapter.rs• L368-551 chain/…/adapter.rs• L532-580 chain/…/downloader.rs• L164-190 core/…/state_parts.rs• L302-319 core/…/state_parts.rs• L399-421 core/…/trie_storage.rs• L309-361
Recovery flow
The per-shard recovery flow hides whether data came from a peer or a local cache. It keeps valid downloads, retries failed work, and starts reconstruction only after all required parts are present.
The main flow is:
flowchart TD
A[Request shard header] --> B[Verify chain and root context]
B --> C[Request parts in parallel]
C --> D[Check each part and its range]
D --> E[Keep accepted parts]
E -->|failed parts remain| C
E --> F[Clear or resume partial apply]
F --> G[Apply trie and flat-state changes]
G --> H[Load the recovered shard state]
H --> I[Finalize shard recovery]
D -->|failure| C
After download, the shard either resumes an interrupted apply or clears the old flat state and applied-part marks. Each accepted part yields trie changes and flat-state changes for the recovered root. Contract code found in the part is retained for the restored shard. The node applies parts in parallel and records each completed part so a cancelled or restarted operation can skip work that is still valid.
The recovered shard is then exposed through its normal trie state. The node can use persistent trie data, flat state, or an in-memory trie according to its catch-up state and configuration. When every tracked shard is complete, the coordinator resets the chain heads and returns control to normal synchronization.
References: chain/…/shard.rs• L143-247 chain/…/shard.rs• L250-275 chain/…/shard.rs• L318-367 chain/…/downloader.rs• L38-205 core/…/mod.rs• L532-540 core/…/state_parts.rs• L424-480 core/…/shard_tries.rs• L491-528 chain/…/mod.rs• L364-443
Sync point agreement
The requester and the serving host must identify the same snapshot. For non-SPICE, a sync point maps to the block two links before it because the host snapshot is taken before the sync block's chunk. For SPICE, the snapshot is taken at the sync block itself, so the advertised snapshot hash equals the sync hash.
This mapping is a network contract. If the requester uses a different snapshot identity than the host, valid state can appear unavailable even when the host has it.
References: chain/…/network.rs• L201-223 chain/…/network.rs• L321-384 chain/…/tests.rs• L467-507
Change checklist
When these files or sections are changed, remember to consider these:
- Keep sync-point selection, finality checks, snapshot timing, and the requester-to-host snapshot hash mapping aligned for both protocol modes.
- Keep the network request identity and pending-response key aligned. A changed shard, sync point, or part identity can route a valid response to the wrong recovery task.
- Preserve the early
NotSenthandling contract when changing direct peer connections. Network failure reports can arrive before the selected peer is known. - Keep shuffled, unordered part work aligned with retry tracking. Failed indexes must return to the next round without treating completion order as part identity.
- Keep state-part boundary calculation, recorded proof nodes, validation, and application aligned. A change in one stage can make valid parts fail or can omit state from recovery.
- Keep trie, flat-state, and contract-code outputs from part application tied to the same state root. Restoring only one output can leave later execution with an incomplete view.
- Keep partial-storage replay equivalent across disk, flat-state, cached, and in-memory trie reads. These access paths must produce the same verified part and state witness.
- Preserve the applied-part and flat-state resume rules. A restart must not reuse marks after a completed apply has changed the stored state.
- Base data-reset and phase selection on a validated epoch sync proof. A peer's advertised height alone must not trigger state abandonment.
- Keep local header and part cache keys tied to the same shard and sync point. Reusing data from another snapshot can bypass the intended verification context.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The sync point is above finality or is selected with incomplete shard progress | The snapshot anchor can move or leave one shard behind | Require finality and apply the protocol-specific progress rule |
| A direct state request cannot reach its host | The downloader can wait for a response that will never arrive | Treat NotSent as a failed request and retry without the full timeout |
| A host message arrives before the network identifies the selected peer | An unrelated peer could affect the pending request | Buffer only network-generated NotSent reports and reject wrong senders |
| The requester and host use different snapshot hash rules | Available headers or parts appear missing | Use the same non-SPICE and SPICE mapping on both sides |
| An epoch sync proof is too old, too recent, from the wrong peer, or fails cryptographic checks | The node can accept an unsafe chain context or remain on an unusable recovery path | Check the expected peer, proof age, epoch links, and validator endorsements before applying it |
| A part is built with flat-state entries but a referenced trie value is unavailable | Part creation or later reconstruction fails | Resolve referenced values from the state trie before final part assembly |
| A part has a missing, extra, or duplicate trie node | The node rejects the part or cannot rebuild the expected range | Validate the exact nodes visited for the part against the header root |
| A restart reuses applied-part marks after flat state is already complete | The node can skip needed reconstruction work | Keep marks only for an unfinished apply and rebuild the flat state when required |
Key files
| File | Why to read it |
|---|---|
| chain/…/utils.rs | Defines sync-point selection, finality rules, retention, and extra block needs. |
| chain/…/tests.rs | Shows the non-SPICE threshold and SPICE first-block behavior under finality. |
| chain/…/mod.rs | Coordinates block prerequisites, per-shard recovery, concurrency, stale-point handling, and completion. |
| chain/…/epoch.rs | Validates epoch sync proofs and chooses between data reset and continued synchronization. |
| chain/…/network.rs | Connects snapshot requests to peers and handles request identity, peer matching, and direct-request failure ordering. |
| chain/…/downloader.rs | Reuses cached snapshot data, validates new headers and parts, persists valid data, and retries failed downloads. |
| chain/…/shard.rs | Coordinates parallel part transfer, retry rounds, state apply, restart recovery, trie loading, and shard finalization. |
| chain/…/util.rs | Provides the shared lookup for cached shard headers and download result tracking. |
| chain/…/adapter.rs | Binds snapshot headers and parts to chain proofs and state roots. |
| core/…/state_parts.rs | Defines part boundaries, proof coverage, part validation, state reconstruction, and flat-state integration. |
| core/…/trie_recording.rs | Defines the recorded trie view used to capture boundary and proof nodes. |
| core/…/trie_storage.rs | Defines partial storage used to replay and verify only the nodes in a part. |
| core/…/shard_tries.rs | Connects recovered state to persistent, flat, cached, and in-memory shard views. |
| core/…/state_part.rs | Defines the state-part identity and wire representation. |