Skip to content

fix(consensus/XDPoS): rebuild missing V2 snapshot and deduplication#2480

Open
gzliudan wants to merge 2 commits into
XinFinOrg:release/v2.8-testnetfrom
gzliudan:rebuild-snapshot-v28
Open

fix(consensus/XDPoS): rebuild missing V2 snapshot and deduplication#2480
gzliudan wants to merge 2 commits into
XinFinOrg:release/v2.8-testnetfrom
gzliudan:rebuild-snapshot-v28

Conversation

@gzliudan

Copy link
Copy Markdown
Collaborator

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 apply

  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Changes that don't change source code or tests
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • revert: Revert something
  • style: Changes that do not affect the meaning of the code
  • test: Adding missing tests or correcting existing tests

Impacted Components

Which parts of the codebase does this PR touch?
Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Tested on a private network from the genesis block and monitored the chain operating correctly for multiple epochs.
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

gzliudan added 2 commits July 21, 2026 17:03
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
@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bd6b7166-03dc-4e7b-8710-e432603dfc51

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant