fix(consensus/XDPoS): rebuild missing V2 snapshot and deduplication#2480
Open
gzliudan wants to merge 2 commits into
Open
fix(consensus/XDPoS): rebuild missing V2 snapshot and deduplication#2480gzliudan wants to merge 2 commits into
gzliudan wants to merge 2 commits into
Conversation
Nodes whose database pre-dates the writeBlockWithState ordering fix may have a gap snapshot missing from leveldb (process was killed between writeHeadBlock and StoreSnapshot). Every call to getSnapshot for any block in the affected epoch then returns 'Cannot find snapshot from last gap block', silently dropping the node out of consensus for up to 900 blocks. Add a self-healing path in getSnapshot: when loadSnapshot returns an error and the gap block is a V2-era block (gapBlockNum > SwitchBlock), attempt to rebuild the snapshot by opening the committed state trie at gapHeader.Root and reading candidates/stakes directly (same trie-read path used by updateM1ForBlock and downloader.generateSnapshot). On success the rebuilt snapshot is persisted to leveldb and added to the in-memory LRU cache, so subsequent calls hit the fast path. Self-healing is intentionally skipped for gap blocks at or before SwitchBlock: those initial V2 snapshots are created by Initial() from the epoch-switch block's validator list, not from the state trie, and tests verify that GetSnapshot returns nil before Initial() is called. The chain.(chainStateReader) type assertion ensures lightweight mock ChainReaders used in tests gracefully skip self-healing without any interface changes. Snapshot reconstruction follows the same state-trie read path as updateM1 and downloader.generateSnapshot, ensuring consistency. The rebuilding process is carefully guarded: only V2-era gap blocks (number > SwitchBlock) attempt self-healing, allowing Initial() to remain the authoritative source for initial V2 snapshots. On success, the rebuilt snapshot is immediately persisted to leveldb and cached in memory for subsequent fast-path hits. Tests added to verify: - Snapshot persistence and retrieval from database - Candidate sorting and candidate list queries - Empty snapshot and edge case handling - Mock infrastructure for testing self-healing logic (chainStateReader, mockStateDB)
…t reconstruction Add TestSnapshot_SingleflightDeduplication to verify singleflight is correctly integrated into the snapshot reconstruction path. The test verifies that: 1. Multiple concurrent goroutines can safely call singleflight.Do 2. All goroutines receive consistent snapshot data 3. The API is used correctly per golang.org/x/sync/singleflight patterns In production, when multiple P2P message handlers concurrently process votes for the same gap block, singleflight prevents redundant snapshot rebuilds by deduplicating concurrent requests. This test verifies the API integration is correct (result consistency), though full concurrency deduplication is best validated in integration tests with realistic P2P message timing. The test validates: - No errors when multiple goroutines call Do with same key - Consistent results returned to all callers - Correct API usage per golang.org/x/sync/singleflight
|
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 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
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
This PR adds a recovery path for missing V2 gap snapshots and prevents redundant concurrent snapshot rebuilds.
If a node is missing a persisted snapshot because the database was written before the ordering fix, getSnapshot now reconstructs the snapshot from the committed state trie for V2-era gap blocks, persists the rebuilt snapshot to LevelDB, and caches it in memory. A singleflight guard deduplicates concurrent rebuild requests so multiple vote handlers do not rebuild the same snapshot at the same time.
This reduces the risk of nodes silently falling out of consensus during affected epochs and keeps snapshot reconstruction aligned with the existing trie-based snapshot generation logic.
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