fix(consensus/XDPoS,core,eth/downloader): rebuild missing V2 gap snapshots - #2480
Closed
gzliudan wants to merge 1 commit into
Closed
fix(consensus/XDPoS,core,eth/downloader): rebuild missing V2 gap snapshots#2480gzliudan wants to merge 1 commit into
gzliudan wants to merge 1 commit into
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
gzliudan
requested review from
AnilChinchawale,
anunay-xin,
benjamin202410,
liam-lai and
wanwiset25
July 21, 2026 09:18
gzliudan
force-pushed
the
rebuild-snapshot-v28
branch
from
August 6, 2026 10:41
bd36cb5 to
72cb73d
Compare
wgr523
reviewed
Aug 6, 2026
gzliudan
force-pushed
the
rebuild-snapshot-v28
branch
2 times, most recently
from
August 7, 2026 04:47
701ed23 to
8a38693
Compare
gzliudan
force-pushed
the
rebuild-snapshot-v28
branch
2 times, most recently
from
August 7, 2026 07:58
217abe6 to
07c6e7d
Compare
gzliudan
force-pushed
the
rebuild-snapshot-v28
branch
from
August 7, 2026 09:09
07c6e7d to
6be5186
Compare
…shots A node can lose its persisted V2 gap snapshot when the process exits between writeHeadBlock and StoreSnapshot in writeBlockWithState: the head markers are already on disk, the snapshot is not, and nothing recreates it. getSnapshot then fails with a leveldb "not found" error for the whole affected epoch and the node drops out of consensus participation. Making that write ordering atomic prevents new holes but cannot repair a database that already has one. Add a guarded self-healing path in engine_v2.getSnapshot: when no snapshot is stored for the gap block, rebuild it from the committed state trie at gapHeader.Root, then persist and cache it. The derivation itself moves into engine_v2.BuildSnapshotFromState, which eth/downloader.generateSnapshot now also uses. The downloader already carried its own copy of this derivation, and the unstable xdc_sort ordering it shares with core.BlockChain.UpdateM1 must not drift: a different equal-stake order yields a different masternode set. The shared builder also refuses to produce an empty snapshot, which the downloader previously persisted and which would then permanently mask the missing masternode list. A gap pivot state sync that finds no candidates therefore now fails loudly instead of storing that empty snapshot. The rebuild is only attempted when it is both safe and useful: - skip when a snapshot is already stored: only a genuinely missing key is healed, so a decode or I/O error never overwrites a stored masternode set; - skip at or before V2 SwitchBlock, where the initial snapshot still comes from Initial() rather than the state trie; - reject numbers that are not real gap blocks, since they can originate from unauthenticated vote/timeout messages; - skip gap blocks more than two epochs behind the chain head, so a peer cannot force trie reads and database writes for arbitrary historic gap numbers; - skip when the chain reader does not implement the new GapStateReader interface, which core.BlockChain satisfies via a compile-time assertion; - skip while the gap block itself is not imported yet, where the missing state is transient and retrying later succeeds. Past that point the gap block is imported, so a rebuild that failed on its state cannot start working later: the trie is pruned and can never come back, so the gap block is recorded in an LRU and never retried, instead of re-reading the trie on every vote verification. Concurrent callers for the same gap block are collapsed with singleflight. Pruned state is logged at warn rather than error, and everything degrades to debug while syncing, where a missing gap snapshot is expected and not actionable. The rebuild needs the gap block's state root to still be readable, which the targeted case implies: a node only keeps the gap block as its head across a restart when that root was committed. Otherwise loadLastState finds no head state, repair() rewinds to an ancestor that has one, and re-importing the gap block runs UpdateM1 and writes the snapshot again. What is left are roots that are gone for good, such as offline pruning, gap blocks below a fast sync pivot, or a side chain whose trie was collected. No later attempt can recover those, which is what the permanent give-up above encodes. A rebuilt snapshot is derived from the gap block state, while UpdateM1 takes candidates from the head state and stakes from the validator contract at "latest". Those agree on the canonical import path, where the head is the gap block itself, but not necessarily on the reorg path, so a rebuilt masternode set can differ from what a peer persisted through a reorg. That is accepted here: the alternative is no snapshot at all. Add coverage for the state-derived builder and for every getSnapshot guard, and seed the downloader test genesis with the minimum masternode voting contract storage the builder reads.
gzliudan
force-pushed
the
rebuild-snapshot-v28
branch
from
August 7, 2026 09:31
6be5186 to
ea01f2b
Compare
Collaborator
Author
|
replaced by #2508 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
A node can lose its persisted V2 gap snapshot when the process exits between
writeHeadBlock and StoreSnapshot in writeBlockWithState: the head markers are
already on disk, the snapshot is not, and nothing recreates it. getSnapshot then
fails with a leveldb "not found" error for the whole affected epoch and the node
drops out of consensus participation. Making that write ordering atomic prevents
new holes but cannot repair a database that already has one.
Add a guarded self-healing path in engine_v2.getSnapshot: when no snapshot is
stored for the gap block, rebuild it from the committed state trie at
gapHeader.Root, then persist and cache it.
The derivation itself moves into engine_v2.BuildSnapshotFromState, which
eth/downloader.generateSnapshot now also uses. The downloader already carried
its own copy of this derivation, and the unstable xdc_sort ordering it shares
with core.BlockChain.UpdateM1 must not drift: a different equal-stake order
yields a different masternode set. The shared builder also refuses to produce an
empty snapshot, which the downloader previously persisted and which would then
permanently mask the missing masternode list. A gap pivot state sync that finds
no candidates therefore now fails loudly instead of storing that empty snapshot.
The rebuild is only attempted when it is both safe and useful:
healed, so a decode or I/O error never overwrites a stored masternode set;
Initial() rather than the state trie;
unauthenticated vote/timeout messages;
force trie reads and database writes for arbitrary historic gap numbers;
interface, which core.BlockChain satisfies via a compile-time assertion;
is transient and retrying later succeeds.
Past that point the gap block is imported, so a rebuild that failed on its state
cannot start working later: the trie is pruned and can never come back, so the
gap block is recorded in an LRU and never retried, instead of re-reading the
trie on every vote verification. Concurrent callers for the same gap block are
collapsed with singleflight. Pruned state is logged at warn rather than error,
and everything degrades to debug while syncing, where a missing gap snapshot is
expected and not actionable.
The rebuild needs the gap block's state root to still be readable, which the
targeted case implies: a node only keeps the gap block as its head across a
restart when that root was committed. Otherwise loadLastState finds no head
state, repair() rewinds to an ancestor that has one, and re-importing the gap
block runs UpdateM1 and writes the snapshot again. What is left are roots that
are gone for good, such as offline pruning, gap blocks below a fast sync pivot,
or a side chain whose trie was collected. No later attempt can recover those,
which is what the permanent give-up above encodes.
A rebuilt snapshot is derived from the gap block state, while UpdateM1 takes
candidates from the head state and stakes from the validator contract at
"latest". Those agree on the canonical import path, where the head is the gap
block itself, but not necessarily on the reorg path, so a rebuilt masternode set
can differ from what a peer persisted through a reorg. That is accepted here:
the alternative is no snapshot at all.
Add coverage for the state-derived builder and for every getSnapshot guard, and
seed the downloader test genesis with the minimum masternode voting contract
storage the builder reads.
Types of changes
What types of changes does your code introduce to XDC network?
Put an
✅in the boxes that applyImpacted Components
Which parts of the codebase does this PR touch?
Put an
✅in the boxes that applyChecklist
Put an
✅in the boxes once you have confirmed below actions (or provide reasons on not doing so) that