Skip to content

fix(consensus/XDPoS,core): fix verifyheaders parent lookups for epoch switches#2276

Open
gzliudan wants to merge 1 commit intoXinFinOrg:v2.6.x-betafrom
gzliudan:fix-verifyheaders
Open

fix(consensus/XDPoS,core): fix verifyheaders parent lookups for epoch switches#2276
gzliudan wants to merge 1 commit intoXinFinOrg:v2.6.x-betafrom
gzliudan:fix-verifyheaders

Conversation

@gzliudan
Copy link
Copy Markdown
Collaborator

@gzliudan gzliudan commented Apr 5, 2026

Proposed changes

Problem: VerifyHeaders could fail on epoch-switch batches when consensus hooks looked up parent headers through ChainReader before the batch had been written to disk.

Cause: the import paths passed the raw chain reader into VerifyHeaders, so helper lookups could only see DB-backed state and missed in-flight headers during block and header-only imports.

Solution: wrap the VerifyHeaders entry points with a batch-aware verifyChainReader so current-batch headers and blocks are visible during validation, add focused regression tests for epoch-switch parent resolution, and keep wrapper reuse as a no-op instead of mutating an existing cache.

Fix: XinFinOrg/XinFin-Node#258

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

Copilot AI review requested due to automatic review settings April 5, 2026 19:56
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 5, 2026

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: 305d6d3b-b5c4-46e6-9c16-8d878ebef0e6

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 and usage tips.

… switches

Problem: VerifyHeaders could fail on epoch-switch batches when consensus hooks looked up parent headers through ChainReader before the batch had been written to disk.

Cause: the import paths passed the raw chain reader into VerifyHeaders, so helper lookups could only see DB-backed state and missed in-flight headers during block and header-only imports.

Solution: wrap the VerifyHeaders entry points with a batch-aware verifyChainReader so current-batch headers and blocks are visible during validation, add focused regression tests for epoch-switch parent resolution, and keep wrapper reuse as a no-op instead of mutating an existing cache.
@gzliudan gzliudan force-pushed the fix-verifyheaders branch from a9a8405 to ab8dc39 Compare April 5, 2026 19:58
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes XDPoS header batch verification during epoch switches by ensuring consensus parent/header lookups can see in-flight (not-yet-persisted) headers/blocks during VerifyHeaders, preventing false unknown ancestor-style failures during imports.

Changes:

  • Introduces a batch-aware consensus.ChainReader wrapper (verifyChainReader) that shadows header/block lookups with current-batch entries.
  • Updates block and header-only import paths to pass a batch-aware reader into VerifyHeaders for XDPoS.
  • Adds regression tests covering epoch-switch parent resolution during VerifyHeaders and InsertHeaderChain.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
core/headerchain.go Wraps VerifyHeaders calls with a batch-aware reader for XDPoS during header-chain validation.
core/blockchain.go Wraps VerifyHeaders calls with a batch-aware reader (including blocks) for XDPoS during block insertion.
consensus/XDPoS/XDPoS.go Ensures XDPoS VerifyHeaders uses a batch-aware reader so helper lookups can see in-batch headers.
consensus/XDPoS/verify_chain_reader.go Adds verifyChainReader implementation to shadow header/block lookups with batch data.
consensus/XDPoS/verify_chain_reader_test.go Adds unit tests validating wrapper behavior (nil-safety, shadowing, reuse semantics).
consensus/tests/engine_v2_tests/verify_chain_reader_test.go Adds focused regression tests for epoch-switch parent resolution in VerifyHeaders and header-only import.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

3 participants