Skip to content

fix(consensus/XDPoS): fix pre-existing underflow in GetSigningTxCountByEpoch - #2572

Open
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-signing-tx-count-epoch0
Open

gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-signing-tx-count-epoch0

Conversation

@gzliudan

Copy link
Copy Markdown
Collaborator

Problem

XDPoS_getSigningTxCountByEpoch walks from epochBlockNum-1 down to the previous epoch switch block. The v1 engine counts the genesis block as the epoch switch that opens epoch 0 (0 % Epoch == 0), so calling the RPC with block number 0 passes the epoch-switch guard and then underflows the unsigned subtraction in consensus/XDPoS/api.go:

failed to get header at number 18446744073709551615 hash 0x0000...0000

The message names a block that cannot exist instead of naming the reason. Nothing crashes and no wrong data is returned, but the endpoint is unusable for the case: it is reachable with XDPoS_getSigningTxCountByEpoch(0) (or "earliest") and answers with a number that can never be looked up.

Fix

No epoch precedes genesis, so there is no range of blocks to walk and no signing to count there. Return an empty count, and state the case in the doc comment of the method.

Test

TestGetSigningTxCountByEpochGenesis asserts the precondition (the v1 engine reads genesis as an epoch switch, which is what makes the case reachable at all) and pins the empty answer for block 0. Without the fix it fails with the underflow message above.

Note

This is a pre-existing defect of the endpoint, present since it was added in #2223; it is unrelated to the import-loop work in flight. There is no upstream counterpart to port - the RPC is XDPoS-specific.

…ByEpoch

The endpoint walks from epochBlockNum-1 down to the previous epoch switch
block, but the v1 engine counts the genesis block as the epoch switch that
opens epoch 0. Calling the RPC with block number 0 therefore passed the
epoch-switch guard and then underflowed the unsigned number: the walk asked
for the header at 2^64-1 and the endpoint failed with "failed to get header
at number 18446744073709551615 hash 0x0000...", naming a block that cannot
exist instead of the reason.

No epoch precedes genesis, so there is no signing to count there. Answer
with an empty count, and say so in the doc comment.

Introduced together with the endpoint (XinFinOrg#2223); no upstream counterpart to
port, the RPC is XDPoS-specific.
@coderabbitai

coderabbitai Bot commented Sep 16, 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: Advanced

Run ID: 79a151a1-6eeb-429c-9042-929b2a708124

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.

🟢 Approval recommended

The focused guard prevents the underflow and is covered by an appropriate regression test.

Pull request overview

Fixes unsigned underflow when querying signing counts for the genesis epoch switch.

Changes:

  • Returns an empty count for genesis.
  • Documents genesis behavior.
  • Adds regression coverage.
File summaries
File Description
consensus/XDPoS/api.go Guards the backward walk at block 0.
consensus/XDPoS/api_test.go Tests genesis epoch-switch behavior.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

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

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