Skip to content

fix(core): fix wrong section count after chain indexer reorg - #2582

Open
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-chain-indexer-reorg
Open

gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-chain-indexer-reorg

Conversation

@gzliudan

@gzliudan gzliudan commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

This fixes the intermittent TestChainIndexerWithChildren failure:

https://github.com/XinFinOrg/XDPoSChain/actions/runs/35316718324/job/105509861843

--- FAIL: TestChainIndexerWithChildren (9.20s)
    chain_indexer_test.go:155: Canonical section count mismatch: have 501, want 502

After a reorg a child indexer kept one section too many. newHead() cascaded the reorg to children as changed*sectionSize, the first invalidated block, while the prefix it keeps itself ends at changed*sectionSize-1, the number the forward cascade hands over. A child taking the invalidated block for a valid one kept the section it had built before the reorg that covers it, so its count stayed one above the blocks really on the chain until verifyLastHead() rolled that section back once the new fork was written. The cascade now hands over the end of the retained prefix, so a child can only keep sections the parent itself still indexes, and the reorg expectation helper of the test mirrors the same number.

Not an intermittent timing failure: with section sizes that make a child section straddle the reorg point it fails every time, which TestMinReorgStraddle now pins down deterministically (parent sectionSize 1, child sectionSize 2, reorg at block 50, new fork above it). Without the fix it fails 3/3 with have 25, want 26; with it the case passes in 0.01s.

No upstream fix exists for this one: go-ethereum never changed this cascade after ethereum#19748 and ethereum#20506, which this tree already carries, and it removed the chain indexer together with the old bloombits logic in ethereum#31081. AddChildIndexer has no caller outside the tests, so the reorg cascade only runs there today.

Verification: make all, make quick-test and one full make test at this commit. Under load (8 busy loops plus 12 concurrent package test runs) the randomized TestChainIndexerWithChildren failed about once per 150 runs before the change and 240 runs stayed green with it.

After a reorg a child indexer kept one section too many: it reported N+1
sections while the parent had already dropped the blocks that section covers,
and the count only came back down once the new fork blocks were written and
verifyLastHead() rolled the stale section back. TestChainIndexerWithChildren
trips over that window and fails with 'Canonical section count mismatch: have N,
want N+1' whenever the random section sizes make a child section straddle the
reorg point.

newHead() cascaded the reorg to children as changed*sectionSize, the first
invalidated block, while the prefix it keeps itself ends at
changed*sectionSize-1, the number the forward cascade hands over. A child taking
the invalidated block for a valid one keeps the section it built before the
reorg that covers it. Cascade the end of the retained prefix instead, so a
child can only keep sections the parent itself still indexes, and mirror the
same number in testChainIndexBackend.reorg() so the test model stays in step.

Add TestMinReorgStraddle, a deterministic two level case (parent sectionSize 1,
child sectionSize 2, reorg at block 50) that fails before the change and passes
after it, instead of relying on the random parameters of
TestChainIndexerWithChildren to hit the window.

No upstream fix to port: go-ethereum never changed this cascade after ethereum#19748 and
ethereum#20506, which this tree already carries, and it removed the chain indexer along
with the old bloombits logic in ethereum#31081.
@coderabbitai

coderabbitai Bot commented Sep 18, 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: 45ffb202-5bcc-4741-b52e-590e05084142

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.

@gzliudan
gzliudan force-pushed the fix-chain-indexer-reorg branch from a15dbd8 to 92b6c64 Compare September 18, 2026 08:45
@gzliudan gzliudan changed the title fix(core): fix wrong section count after chain indexer reorg and indexer death on unexpected Process call fix(core): fix wrong section count after chain indexer reorg Sep 18, 2026
@gzliudan
gzliudan requested review from AnilChinchawale, anunay-xin, benjamin202410, liam-lai and wanwiset25 and a balanced review from Copilot September 18, 2026 08:49

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.

🟡 Changes recommended

The empty retained-prefix case still incorrectly preserves child data as though block zero were retained.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Fixes child chain-indexer section counts after reorgs.

Changes:

  • Cascades the retained section’s final block.
  • Updates test reorg expectations.
  • Adds deterministic straddling-section coverage.
File summaries
File Description
core/chain_indexer.go Corrects reorg cascading.
core/chain_indexer_test.go Updates expected cascade boundaries.
core/chain_indexer_reorg_test.go Adds deterministic regression coverage.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Balanced

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

Comment thread core/chain_indexer.go

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 boundary correction is consistent with forward cascading and is covered by a focused regression test.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

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