Block and state synchronization
Block synchronization and state synchronization bootstrap a node that does not yet have the current chain state. Block sync catches up through the block pool and reactor. State sync restores a recent application snapshot through peers, checks it against trusted chain data, and then hands the node to block sync. The peer transport that carries these exchanges is part of the networking system.
Synchronization modes
The node has two bootstrap paths. Block sync replays blocks from the local store height onward. State sync restores application state near the chain head. It then starts block sync at the next height so the node can reach consensus with a current and checked state.
| Mode | Starting data | Main result |
|---|---|---|
| Block sync | The latest local state and block height | Consecutive checked blocks and updated application state |
| State sync | A trusted chain point and a peer-provided application snapshot | Restored application state and its commit at the snapshot height |
State sync and block sync share the peer network but use separate message channels. The state sync reactor serves snapshots and chunks while a sync is active. The block sync reactor remains ready to continue when state restoration finishes. References: node/node.go• L226-279, node/setup.go• L573-624, statesync/reactor.go• L31-83, blocksync/reactor.go• L64-86
Block synchronization
Block synchronization fills the gap between the local height and the highest usable peer height. The block pool tracks peer ranges and assigns one requester to each target height. It chooses peers that can serve the height. It also retries work when a peer is slow, unavailable, or removed. A block counts as peer progress only when the pool stores it. Duplicate or late data does not keep a stalled peer alive.
Block request flow
The block request flow exchanges peer status and block data. It advances only after the next block is checked. This diagram shows how the pool, reactor, peers, and local stores work together.
sequenceDiagram
participant Pool as Block pool
participant Reactor as Block sync reactor
participant Peer as Peer
participant Store as Block store
participant App as Application state
Reactor->>Peer: Status request
Peer-->>Reactor: Base and height
Reactor->>Pool: Register peer range
Pool->>Reactor: Block request for height H
Reactor->>Peer: Block request H
Peer-->>Reactor: Block H and commit data
Reactor->>Pool: Add response
Pool->>Reactor: Consecutive blocks H and H+1
Reactor->>Reactor: Verify commit and block
Reactor->>Store: Save checked block
Reactor->>App: Apply checked block
Reactor->>Pool: Advance to H+1
The reactor asks each connected peer for its base and height. The pool accepts ranges that can serve the current height. It limits outstanding work per peer to preserve peer diversity and control slow connections. It removes peers that fail to deliver data and reschedules their requests.
The pool binds each response to a requester and to a peer that received that request. It accepts the first usable copy for storage. A late copy from another peer can settle an old request. It does not replace the stored block or earn new liveness credit. An unsolicited response cannot satisfy an outstanding request. Error reporting happens after pool state is unlocked. A blocked error consumer therefore cannot stop other pool operations. References: blocksync/pool.go• L154-169, blocksync/pool.go• L393-479, blocksync/pool.go• L613-718, blocksync/pool_test.go• L738-807, blocksync/pool_test.go• L843-1053
The reactor needs two consecutive blocks before it commits the first one to local storage. The second block contains the commit that proves the first block. The reactor checks the commit against the validator set. It validates the block and can ask the application to confirm the block data. It also checks vote-extension signatures when the chain requires them. Only checked data is saved and applied. Message and reactor checks reject malformed, oversized, unsolicited, or inconsistent peer data before it can change sync state. References: blocksync/pool.go• L80-89, blocksync/pool.go• L309-327, blocksync/pool.go• L393-479, blocksync/reactor.go• L215-332, blocksync/reactor.go• L467-646, blocksync/msgs.go• L20-142, blocksync/reactor_fuzz_test.go• L456-611
Catch-up completion
Block sync ends when the local height reaches the highest height that a usable peer can serve. A peer whose base is ahead of the local height does not count until the local height reaches that base. The reactor then stops the pool and switches the consensus reactor to normal block production and voting.
The pool keeps waiting when all peers report ranges that start after the local height. This prevents an unusable range from falsely declaring the node caught up. A new network with no produced blocks is different. Peers at height zero allow the node to enter consensus and produce the first block. A local validator with enough voting power can also let the reactor leave block sync so it does not block the chain. References: blocksync/pool.go• L261-307, blocksync/pool.go• L330-357, blocksync/pool.go• L581-598, blocksync/pool_test.go• L563-599, blocksync/pool_test.go• L683-736, blocksync/reactor.go• L406-465
State synchronization
State synchronization restores application state without replaying every block from genesis. It uses a light client to obtain trusted chain data. It discovers snapshots from connected peers. The state sync reactor then brokers snapshot operations between peers and the application.
Snapshot discovery
Snapshot discovery collects metadata before any state data is applied. A snapshot identifies application state by its height, format, chunk count, hash, and metadata. The pool groups equal snapshots from different peers so the syncer can use more than one sender. It ranks candidates by height and format. It removes candidates or senders that fail.
The reactor asks connected peers for recent snapshots. A serving peer reads its recent snapshots from the application and sends their metadata to the requester. The syncer waits for the configured discovery period when needed. It then selects the best available candidate. If no usable candidate exists and discovery remains enabled, it starts another discovery round. References: statesync/reactor.go• L91-183, statesync/reactor.go• L288-319, statesync/snapshots.go• L13-26, statesync/snapshots.go• L42-118, statesync/snapshots.go• L156-222, statesync/syncer.go• L133-189
Trusted state data
The light client supplies the trust anchor for the snapshot. The state provider checks chain headers through multiple RPC sources. It obtains the application hash for the snapshot height from verified chain data. If the primary source conflicts with a witness, the light client can use the honest witness. Without an agreeing source, state sync stops. The provider also verifies later chain data before it builds the state needed after restoration.
The provider obtains the commit and validator data needed after the snapshot. It verifies the commit against the validator set before using it for bootstrap. It also checks that consensus parameters belong to the requested height. These checks bind the opaque application snapshot to a chain point signed by validators. References: statesync/stateprovider.go• L27-36, statesync/stateprovider.go• L47-85, statesync/stateprovider.go• L88-129, statesync/stateprovider.go• L132-206, statesync/stateprovider_test.go• L93-180
Chunk transfer
Chunk transfer moves the selected snapshot from peers to the local application. The syncer allocates each chunk index to a fetcher. Each fetcher requests a chunk from a peer that advertised the snapshot. The queue accepts a valid chunk from any peer that supplies it. It records the actual sender so the application and syncer can act on sender-specific failures.
The queue checks the snapshot height, format, and index before it stores a chunk. It writes chunks to temporary files instead of keeping the full snapshot in memory. It returns available chunks in index order, even when peers send them out of order. Missing chunks, duplicate responses, slow peers, and closed queues remain visible to the syncer so it can retry or fail restoration. A discarded chunk becomes available for allocation again. A retry can also return an already fetched chunk without fetching it again. References: statesync/chunks.go• L27-61, statesync/chunks.go• L64-107, statesync/chunks.go• L109-197, statesync/chunks.go• L233-298, statesync/chunks.go• L310-331, statesync/chunks_test.go• L56-117, statesync/chunks_test.go• L140-194, statesync/chunks_test.go• L196-327, statesync/chunks_test.go• L350-495
Before loading application data, the reactor avoids work that cannot be queued for a peer. It records replies that it skips or cannot send. This protects other peers and keeps a slow connection from blocking state sync. References: statesync/reactor.go• L129-257, statesync/reactor.go• L274-285, statesync/reactor_fuzz_test.go• L224-432, statesync/reactor_slow_peer_test.go• L82-174
Snapshot restoration
Restoration starts when the application accepts the snapshot offer. The offer includes snapshot metadata and the trusted application hash and version. The application then receives chunks in index order and rebuilds its state. After each chunk it can request a refetch, retry the current chunk, reject a sender, retry the whole snapshot, or abort.
When the application rejects a sender, the syncer removes that peer from future snapshot selection and discards the peer's unreturned chunks. The reactor also disconnects the peer at the P2P layer. This prevents a rejected sender from continuing to provide data during the active restore.
The syncer checks the application after all chunks are applied. It compares the reported application hash and last block height with trusted snapshot values. It then checks the application version against verified chain state. A successful result returns the restored state and commit to the node bootstrap flow. References: statesync/syncer.go• L253-341, statesync/syncer.go• L344-379, statesync/syncer.go• L381-440, statesync/syncer.go• L513-545, statesync/reactor.go• L322-358, statesync/reactor_fuzz_test.go• L387-432
Bootstrap handoff
The node stores the restored state and the commit at the snapshot height before it resumes block synchronization. Block sync starts at the next block height. It closes the gap between the restored state and the current chain. After block sync reaches the network head, consensus takes control.
This handoff preserves the link between the state store, block store, and block pool. The block pool moves to the post-snapshot height before it creates new block requests. Offline bootstrap records the synced height so a later block sync start does not begin at height zero or use an inconsistent store height. References: node/setup.go• L573-624, node/node.go• L261-279, blocksync/pool.go• L330-337, blocksync/reactor.go• L100-126, blocksync/reactor.go• L172-187
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the snapshot metadata contract aligned across the application snapshot API, state sync messages, snapshot pool keys, and chunk queue checks.
- Keep the trusted snapshot height, application hash, commit, and restored state consistent with the height at which block sync starts.
- Keep the block response contract aligned with application-version limits, commit verification, and vote-extension requirements so the handoff to consensus has the data it needs.
- Keep peer range validation and catch-up status aligned so pruned peers cannot raise the sync target before they can serve the local height.
- Keep peer rejection paths connected to both snapshot selection and the P2P peer switch. A sender rejected by the application must not continue to serve chunks.
- Keep snapshot and chunk size limits aligned with message validation and the configured maximum snapshot chunk count.
- Keep consensus parameter responses bound to the height used to build the restored state.
- Keep commit verification in the state provider aligned with the trust model for blocks reached by backward light-client verification.
- Keep snapshot discovery waits and retry handling aligned with syncer outcomes. A short wait or an unhandled retry can prevent a node from finding a usable snapshot or completing restoration.
- Keep application responses for chunk refetch and sender rejection aligned with queue ownership. A refetch must make the index available again. A rejected sender must leave both snapshot selection and the active peer set.
- Keep send-queue checks and
RepliesNotSentlabels aligned with snapshot and chunk reply paths. A slow peer must not cause avoidable application loads or block replies to other peers. - Keep pool error reporting outside the pool lock. A blocked error channel must not prevent height, peer range, or request state from being read or changed.
- Keep block liveness accounting tied to stored responses. Duplicate, late, or unsolicited blocks must not clear unrelated requests or extend a peer's useful lifetime.
- Keep block-pool height changes from state sync aligned with the snapshot height. New requests must start at the first block after the restored state.
- Keep chunk allocation and retry state aligned. Discarded chunks must be allocatable again, while ordinary retries must not create duplicate fetch ownership.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A peer advertises a base above its height or reports a range that moves backward | The peer can poison catch-up progress or create an unusable request range | The block pool rejects invalid or regressing reports and counts only ranges that cover the current height |
| A peer advertises a range that starts after the local height | The node can wait for a target that no connected peer can serve or switch to consensus too early | The pool excludes unreachable ranges from its maximum height and checks them again as the local height advances |
| A block response comes from a peer that did not receive its request | The response is not trusted and the peer is removed | The pool binds each response to the requester and the peer that received the request |
| A late or duplicate block arrives after another copy was stored | An unrelated request can be cleared or a slow peer can appear healthy | The pool credits only the stored copy and handles old responses according to their request |
| A commit or vote-extension signature is invalid | The block is not saved or applied | The block sync reactor checks the required commit data before persistence |
| A block exceeds the active application limit | The peer is removed and the block is not stored | Message checks and block processing use the application limits committed for that height |
| A snapshot has too many chunks or a chunk has the wrong identity | The message or chunk cannot be used and may consume unsafe resources | Message validation and the chunk queue enforce limits and snapshot identity |
| A peer's send queue is full or cannot accept a reply | The application may do needless snapshot work and the reply is not delivered | The reactor checks queue capacity before loading data, uses non-blocking sends, and records the reason |
| A chunk is supplied by an untrusted sender or the application rejects that sender | Restoration can use bad data or the sender can continue sending chunks | The queue records the actual sender, the syncer removes it, and the reactor disconnects it |
| The application asks to refetch a chunk or retry a snapshot | Restoration can stall or reuse stale chunk state | The syncer returns requested indexes to the queue and restarts the required delivery path |
| Snapshot discovery finds no usable candidate | State sync waits for another round or stops when discovery is disabled | The syncer repeats discovery when configured and returns a no-snapshot result otherwise |
| The restored application hash, height, or version differs from trusted data | The node would run an unchecked state | Final application verification rejects the restoration |
| A backward-checked commit has invalid validator signatures | The node could bootstrap from an unsigned state point | The state provider checks the commit against its validator set |
| Consensus parameters are returned for the wrong height | The restored state could use rules from another chain point | The state provider rejects the response before returning restored state |
| State and block stores do not share the restored height | The next block sync start is ambiguous or inconsistent | Bootstrap stores the commit and synced height, and block sync checks the store and state heights |
| A chunk is discarded without returning it to allocation | A fetcher can wait for a chunk that no longer has an owner | The chunk queue clears its allocation and return state and makes the index available again |
Key files
| File | Why to read it |
|---|---|
| node/setup.go | Shows the handoff from state sync to block sync and the final bootstrap actions. |
| blocksync/pool.go | Defines peer range tracking, block request scheduling, retries, response accounting, catch-up status, and the post-state-sync height. |
| blocksync/reactor.go | Connects block messages to the pool, checks blocks, saves them, and switches to consensus. |
| blocksync/reactor_fuzz_test.go | Shows block reactor behavior under peer interleavings and invalid block responses. |
| statesync/reactor.go | Connects snapshot and chunk messages to the application and active syncer. It also protects peers and other work from full send queues. |
| statesync/syncer.go | Defines snapshot selection, trusted-data checks, chunk fetching, application restore, sender rejection, and final verification. |
| statesync/snapshots.go | Explains how snapshots are grouped by identity, ranked, and removed after rejection. |
| statesync/chunks.go | Explains ordered chunk delivery, temporary storage, allocation, retries, and sender tracking. |
| statesync/chunks_test.go | Shows the queue guarantees for ordering, duplicate handling, allocation, discard, retry, sender tracking, and cleanup. |
| statesync/stateprovider.go | Defines the trusted chain data used to verify a snapshot and build restored state. |
| statesync/reactor_fuzz_test.go | Shows bounded message handling, peer removal, chunk queue invariants, and scripted restore outcomes. |
| statesync/reactor_slow_peer_test.go | Shows that a peer that stops reading does not delay replies to another peer. |
| statesync/metrics.go | Defines state-sync activity and unsent-reply metrics used to observe the reactor. |
| config/config.go | Defines the state sync and block sync controls that shape discovery, trust, chunk fetching, and data checks. |