Skip to content

fix(core): store gap snapshot before writeHeadBlock to close crash window#2476

Open
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-leveldb-not-found
Open

fix(core): store gap snapshot before writeHeadBlock to close crash window#2476
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-leveldb-not-found

Conversation

@gzliudan

Copy link
Copy Markdown
Collaborator

Proposed changes

UpdateMasternodes (called via UpdateM1) was invoked after writeHeadBlock, leaving a window where the node head pointed to a gap block but no corresponding snapshot existed in the database. If the process was killed or restarted during the lengthy per-candidate EVM calls in UpdateM1, the snapshot would never be written. On the next boot the node would load the gap block as its head, then fail with:

Cannot find snapshot from last gap block err="leveldb: not found"

on every block in the following epoch, silently dropping out of consensus.

Fix: add updateM1ForBlock(block, statedb) which reads candidates and stakes directly from the committed state trie (same as downloader.generateSnapshot) without depending on bc.CurrentBlock() or bc.CurrentHeader(). Both the canonical-chain path (writeBlockWithState) and the reorg path (reorg) now call updateM1ForBlock with bc.StateAt(block.Root()) before writeHeadBlock, so the snapshot is durable before the head markers are persisted.

The original UpdateM1 is retained unchanged for external callers and tests.

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

@coderabbitai

coderabbitai Bot commented Jul 20, 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 Plus

Run ID: e90f9dc7-cbb1-427c-84db-e3f8ce38ab30

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a crash/consensus-drop window around XDPoS gap-block snapshot creation by ensuring the masternode snapshot is generated and stored before head markers are persisted (both on normal canonical inserts and during reorgs).

Changes:

  • Move gap-block masternode snapshot generation to occur before writeHeadBlock in writeBlockWithState.
  • Apply the same “snapshot-first” ordering in the reorg head-advancement loop.
  • Introduce updateM1ForBlock(block, statedb) to compute candidates/stakes directly from the committed state trie (avoiding dependence on bc.CurrentBlock()/bc.CurrentHeader()).

Comment thread core/blockchain.go Outdated
Comment thread core/blockchain.go Outdated
Comment thread core/blockchain.go Outdated
Comment thread core/blockchain.go Outdated
Comment thread core/blockchain.go Outdated
@gzliudan
gzliudan force-pushed the fix-leveldb-not-found branch 2 times, most recently from b1fdf32 to d93b6cd Compare July 21, 2026 03:35
@gzliudan
gzliudan requested a review from Copilot July 21, 2026 03:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread consensus/tests/engine_v1_tests/block_signer_test.go
Comment thread consensus/tests/engine_v1_tests/block_signer_test.go
Comment thread core/blockchain.go
@gzliudan
gzliudan force-pushed the fix-leveldb-not-found branch 3 times, most recently from 0294177 to 2e605b3 Compare July 23, 2026 06:36
…ndow

UpdateMasternodes (called via UpdateM1) was invoked after writeHeadBlock,
leaving a window where the node head pointed to a gap block but no
corresponding snapshot existed in the database. If the process was killed
or restarted during the lengthy per-candidate EVM calls in UpdateM1, the
snapshot would never be written. On the next boot the node would load the
gap block as its head, then fail with:

  Cannot find snapshot from last gap block err="leveldb: not found"

on every block in the following epoch, silently dropping out of consensus.

Fix: add updateM1ForBlock(block, statedb) which reads candidates and stakes
directly from the committed state trie (same as downloader.generateSnapshot)
without depending on bc.CurrentBlock() or bc.CurrentHeader(). Both the
canonical-chain path (writeBlockWithState) and the reorg path (reorg) now
call updateM1ForBlock with bc.StateAt(block.Root()) before writeHeadBlock,
so the snapshot is durable before the head markers are persisted.

The original UpdateM1 is retained unchanged for external callers and tests.
@gzliudan
gzliudan force-pushed the fix-leveldb-not-found branch from 2e605b3 to 5b1992b Compare July 23, 2026 10:01
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.

2 participants