Skip to content

fix(cicd,cmd,consensus/XDPoS,eth): fix fast sync#2272

Open
wgr523 wants to merge 29 commits into
dev-upgradefrom
fixfastsync-new
Open

fix(cicd,cmd,consensus/XDPoS,eth): fix fast sync#2272
wgr523 wants to merge 29 commits into
dev-upgradefrom
fixfastsync-new

Conversation

@wgr523
Copy link
Copy Markdown
Collaborator

@wgr523 wgr523 commented Apr 3, 2026

Proposed changes

Fix fast sync. I'll add a doc about how to do fast sync. Tested on private net and devnet.

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
Copy link
Copy Markdown

coderabbitai Bot commented Apr 3, 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: 8a0183f2-f9f0-44ea-9f4c-454910fd161b

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
  • Commit unit tests in branch fixfastsync-new

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.

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

This PR aims to fix fast sync on XDPoSChain by making fast sync pivot selection configurable (number/hash/root), adding “gap pivot” state sync + snapshot generation, and adjusting consensus header verification behavior to better support fast-sync header insertion.

Changes:

  • Add fast-sync pivot configuration to ethconfig (TOML + CLI flags) and wire it into the downloader at node startup.
  • Extend the downloader to support a fixed pivot, optional pivot hash verification, gap-pivot state syncs, and snapshot generation.
  • Adjust XDPoS v1/v2 header verification and epoch-switch info lookup to support reduced verification modes used during header-chain validation.

Reviewed changes

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

Show a summary per file
File Description
eth/ethconfig/config.go Adds FastSyncPivot* fields to the eth configuration struct.
eth/ethconfig/gen_config.go Updates TOML marshal/unmarshal for the new fast-sync pivot fields.
eth/downloader/statesync.go Removes an outdated comment about fast sync usage.
eth/downloader/downloader.go Implements configurable pivot/gap pivot logic and snapshot generation; changes fast-sync header verification constants/behavior.
eth/backend.go Wires configured pivot settings into the downloader during backend initialization.
consensus/XDPoS/engines/engine_v2/verifyHeader.go Adjusts verification flow to use header-provided validators/penalties when not full-verifying.
consensus/XDPoS/engines/engine_v2/utils.go Updates getEpochSwitchInfo call sites for new signature.
consensus/XDPoS/engines/engine_v2/timeout.go Updates getEpochSwitchInfo call sites for new signature.
consensus/XDPoS/engines/engine_v2/snapshot.go Exports snapshot constructor/storage helpers (NewSnapshot/StoreSnapshot).
consensus/XDPoS/engines/engine_v2/snapshot_test.go Updates tests to use exported snapshot helpers.
consensus/XDPoS/engines/engine_v2/epochSwitch.go Refactors getEpochSwitchInfo to accept parent-header slices for VerifyHeaders optimization and softens snapshot failure handling.
consensus/XDPoS/engines/engine_v2/engine.go Updates snapshot usage and verifyQC/getEpochSwitchInfo interactions for new signatures/exports.
consensus/XDPoS/engines/engine_v1/engine.go Skips checkpoint signer checks when not doing full verification.
cmd/XDC/main.go Registers new CLI flags for fast-sync pivot configuration.
cmd/utils/flags.go Defines and applies new fast-sync pivot flags into ethconfig.
cicd/testnet/start.sh Adds environment-driven fast-sync pivot args.
cicd/mainnet/start.sh Adds environment-driven fast-sync pivot args.
cicd/local/start.sh Adds environment-driven fast-sync pivot args.
cicd/devnet/start.sh Adds environment-driven fast-sync pivot args.

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

Comment thread eth/downloader/downloader.go
Comment thread eth/downloader/downloader.go
Comment thread eth/downloader/downloader.go
Comment thread cmd/utils/flags.go Outdated
Comment thread eth/downloader/downloader.go
Comment thread eth/downloader/downloader.go
@gzliudan gzliudan changed the title Fix fast sync fix(cicd,cmd,consensus/XDPoS,eth): fix fast sync Apr 3, 2026
@wgr523 wgr523 force-pushed the fixfastsync-new branch 2 times, most recently from b04ac82 to 7b85d50 Compare April 12, 2026 15:18
Comment thread cicd/devnet/start.sh Outdated
Comment thread eth/downloader/downloader.go Outdated
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

Copilot reviewed 18 out of 18 changed files in this pull request and generated 9 comments.

Comments suppressed due to low confidence (1)

eth/downloader/downloader.go:523

  • When a fixed pivot is configured, syncWithPeer does not validate that pivotNumber is <= the remote height. If pivotNumber is greater than the chain head, header fetching can terminate while d.committed stays 0, potentially stalling fast sync. Consider validating d.pivotNumber <= height (and also d.pivotNumber > 0) up front and returning a configuration error if it's out of range.
		if d.pivotNumber != 0 {
			// Use configured pivot block
			log.Info("Using configured pivot block", "number", d.pivotNumber)
			pivot = d.pivotNumber
			if pivot <= origin {
				origin = pivot - 1
			}
		} else if height <= uint64(fsMinFullBlocks) {
			origin = 0
		} else {
			pivot = height - uint64(fsMinFullBlocks)
			if pivot <= origin {

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

Comment thread eth/downloader/downloader.go Outdated
Comment thread eth/downloader/downloader.go
Comment thread cmd/utils/flags.go Outdated
Comment thread eth/downloader/downloader.go
Comment thread eth/downloader/downloader.go
Comment thread consensus/XDPoS/engines/engine_v2/verifyHeader.go
Comment thread consensus/XDPoS/engines/engine_v1/engine.go
Comment thread cmd/utils/flags.go
Comment thread eth/downloader/downloader_test.go Outdated
@wgr523 wgr523 force-pushed the fixfastsync-new branch from 1ba23e0 to b496cd2 Compare May 27, 2026 14:58
wgr523 and others added 11 commits May 29, 2026 00:16
Add new CLI flags to configure a fixed pivot block for fast sync:
- --fastsyncpivotnumber: Pivot block number (0 = use default calculation)
- --fastsyncpivothash: Pivot block hash for verification

Changes:
- Add FastSyncPivotNumber and FastSyncPivotHash to ethconfig.Config
- Add pivotNumber and pivotHash fields to Downloader struct
- Add SetPivotBlock() method to configure pivot before sync
- Use configured pivot in syncWithPeer instead of dynamic calculation
- Prevent pivot block from moving during sync when configured
- Verify pivot block header hash after state sync completes
- Add state root verification after state sync completes

This allows operators to sync from a specific trusted checkpoint block
instead of relying on the dynamic pivot calculation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Changes:
- Add pivotGapNumbers field to Downloader struct
- Calculate gap pivot numbers in SetPivotBlock()
- Sync gap pivot states in processFastSyncContent() before primary pivot
@wgr523 wgr523 force-pushed the fixfastsync-new branch from b496cd2 to a6da06f Compare May 28, 2026 17:23
*/
// Retrieve the snapshot needed to verify this header and cache it
snap, err := x.snapshot(chain, number-1, header.ParentHash, parents, nil)
if err != nil {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

add error log

err = x.checkSignersOnCheckpoint(chain, header, signers)
if err == nil {
return x.verifySeal(chain, header, parents, fullVerify)
}
Copy link
Copy Markdown
Collaborator

@benjamin202410 benjamin202410 May 29, 2026

Choose a reason for hiding this comment

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

add error log, you can put info or debug level since you already handled it

return x.verifySeal(chain, header, parents, fullVerify)
signers, err = x.getSignersFromContract(chain, header)
if err != nil {
return err
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

add error log

err = x.checkSignersOnCheckpoint(chain, header, signers)
if err == nil {
return x.verifySeal(chain, header, parents, fullVerify)
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

what if it is still err != nil?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

re-written

func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainReader, header *types.Header, hash common.Hash) (*types.EpochSwitchInfo, error) {
// headers is allow to be nil. if not nil, headers contain header (as last item) and its parents if any, which can be used to avoid
// fetching from the database during recursive lookups (useful during VerifyHeaders).
func (x *XDPoS_v2) getEpochSwitchInfo(chain consensus.ChainReader, headers []*types.Header, hash common.Hash) (*types.EpochSwitchInfo, error) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

need to discuss the headers []*types.Header input

Comment thread eth/downloader/downloader.go
Comment thread eth/downloader/downloader.go Outdated
standbynodes = common.RemoveItemFromArray(standbynodes, penalties)
snap, err := x.getSnapshot(chain, h.Number.Uint64(), false)
if err != nil {
log.Warn("[getEpochSwitchInfo] Adaptor v2 getSnapshot has error, cannot get standbynodes", "err", err)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We have moved this from error to warn, we won't know if snapshot has error, is it expected behaviour?

d.pivotGapNumbers = nil
for i := uint64(0); ; i++ {
gapNumber := baseGap + epoch*i
if gapNumber >= number {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

pivot block is excluded from gap snapshot generation when it itself a gap block
Suppose pivot number is 1350, for i=1 gapnumber=1350 and number=1350 so pivotGapNumbers won't include 1350 as 1350>=1350, so snapshot won't exist for this
Please let me know if I am understanding this correctly

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.

4 participants