Skip to content

fix(eth): guard stalling-peer checks against missing local TD - #2523

Closed
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:guard-nil-td
Closed

fix(eth): guard stalling-peer checks against missing local TD#2523
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:guard-nil-td

Conversation

@gzliudan

@gzliudan gzliudan commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

Proposed changes

This PR solves the comment #2515 (comment).

GetTd returns nil when the TD entry is absent from the database (legacy XDPoS chaindata predating the TD index), which td.Cmp(nil) turns into a panic. Guard the stalling-peer checks in processHeaders and the sync threshold checks in ProtocolManager.synchronise: a missing TD skips the comparison and is reported via a counter metric, warning once per downloader lifetime.

The detection loss is bounded: header insertion rejects blocks whose parent TD is missing, so skipping the check cannot hide a peer that delivered an invalid chain.

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 Aug 15, 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: eadaa1e9-7691-48f2-ae03-49015917946e

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

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

Prevents missing local total-difficulty entries from panicking Ethereum synchronization paths.

Changes:

  • Adds nil-TD guards, one-time warnings, and metrics.
  • Adds full, fast, and light synchronization coverage.
  • Adds test helpers for legacy chaindata scenarios.

Reviewed changes

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

Show a summary per file
File Description
eth/sync.go Guards protocol-manager TD comparisons.
eth/sync_test.go Tests full and fast sync behavior.
eth/metrics.go Adds protocol sync counter.
eth/helper_test.go Adds passive and missing-TD test helpers.
eth/handler.go Tracks one-time warning state.
eth/downloader/metrics.go Adds downloader missing-TD counter.
eth/downloader/downloader.go Guards stalling-peer TD comparisons.
eth/downloader/downloader_test.go Tests downloader behavior across protocols and modes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread eth/sync.go Outdated
Comment on lines +252 to +254
if td == nil {
pm.warnMissingTd(currentBlock.Number.Uint64(), currentBlock.Hash())
} else if pTd.Cmp(td) <= 0 {
Comment thread eth/downloader/downloader.go Outdated
Comment on lines +1382 to +1384
if localTD == nil {
d.warnMissingTd(head.Number.Uint64(), head.Hash())
return nil
Comment thread eth/helper_test.go
Comment on lines +164 to +165
head := blockchain.CurrentBlock()
rawdb.DeleteTd(db, head.Hash(), head.Number.Uint64())
…ises

Legacy XDPoS chaindata predates the TD index, leaving the head without
a total difficulty entry. Treating a missing TD as lower and entering
the downloader was not panic-safe: a peer fork rooted below the current
head passes the ErrUnknownAncestor check when the fork parent has a TD
entry, and WriteHeader then compares externTd against a nil localTd.
The full-block fork-choice paths dereference the current TD as well.

Reconstruct the head TD before the sync threshold checks instead:
BlockChain.RepairMissingTd walks the canonical parents down to the
nearest ancestor with a known TD (or genesis) and persists the
accumulated values in batches. synchronise repairs the block and snap
heads, skipping the cycle if reconstruction fails. A still-missing
local TD is treated as zero at the remaining fork-choice comparison
sites as defense in depth.

The downloader no longer skips the stalling-peer check when the local
TD is missing: the unverifiable TD promise is conservatively treated
as stalling, so a peer advertising an arbitrary TD without delivering
headers is dropped instead of the node being marked synchronised.

Add a missingtd_repaired counter and update the missing-head-TD tests
to assert the repaired behavior.
@gzliudan

Copy link
Copy Markdown
Collaborator Author

This PR is incomplete. When completed, it will become complex and massive. It was replaced by #2524.

@gzliudan gzliudan closed this Aug 16, 2026
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