Skip to content

fix: Refactor block height retrieval with blockchain service#417

Merged
ianhe8x merged 1 commit intosubquery:mainfrom
Devs-Infrastructure:main
Mar 3, 2026
Merged

fix: Refactor block height retrieval with blockchain service#417
ianhe8x merged 1 commit intosubquery:mainfrom
Devs-Infrastructure:main

Conversation

@lorrod
Copy link
Copy Markdown

@lorrod lorrod commented Feb 27, 2026

Description

When POI (Proof of Index) is not enabled and a fork is detected, the Ethereum unfinalized blocks service was returning a partial header object containing only blockHeight forces it to interpret as Header type.

The Header type requires blockHeight, blockHash, parentHash, and timestamp. Downstream (@node-core's init and reindex) expects a full header; receiving only blockHeight caused "Node failed to start" and broken rewind behaviour.

https://github.com/subquery/subql/blob/f136cbb1ff98d685cd5bcbe5a855feb5386b1989/packages/node-core/src/utils/blocks.ts#L57

export function getHistoricalUnit(mode: HistoricalMode, header: Header): number {
  if (mode === 'timestamp') {
    if (!header.timestamp) {
      throw new Error('Timestamp missing on current block header');
    }
    return header.timestamp.getTime();
  }

  return header.blockHeight;
}

Fix: Use the blockchain service to fetch a full header for the rewind height instead of constructing a partial object:

return this.blockchainService.getHeaderForHeight(
  Math.max(
    0,
    forkedHeader.blockHeight -
      (this.nodeConfig as EthereumNodeConfig).blockForkReindex,
  ),
);

No new dependencies. Relevant context: unfinalized block rewind flow in node-core, and the Header type contract in @subql/node-core.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist

  • I have tested locally
  • I have performed a self review of my changes
  • Updated any relevant documentation
  • Linked to any relevant issues
  • I have added tests relevant to my changes
  • Any dependent changes have been merged and published in downstream modules
  • My code is up to date with the base branch
  • I have updated relevant changelogs. We suggest using chan

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of unfinalized block handling by using actual blockchain header data instead of synthetic fallback objects in edge cases.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 75483ba and 4768c32.

📒 Files selected for processing (1)
  • packages/node/src/indexer/unfinalizedBlocks.service.ts

📝 Walkthrough

Walkthrough

Modified the getLastCorrectFinalizedBlock function in the unfinalizedBlocks service. When POI is not enabled, replaced synthetic Header object construction with actual header retrieval via blockchainService.getHeaderForHeight using calculated height.

Changes

Cohort / File(s) Summary
Header Retrieval Logic
packages/node/src/indexer/unfinalizedBlocks.service.ts
Changed fallback rewind source from synthetic Header-like object to real header lookup via blockchainService.getHeaderForHeight with calculated height in non-POI code path.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A header that's real, not just a dream,
We fetch from the chain, by height at the seam,
No synthetic tricks, just truth from the block,
Our unfinalized path now stands strong as a rock! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: refactoring block height retrieval to use the blockchain service instead of a synthetic object.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

@lorrod
Copy link
Copy Markdown
Author

lorrod commented Feb 27, 2026

This helped me to investigate the issue, might be useful:
subquery#3018

@ianhe8x ianhe8x merged commit ba5206d into subquery:main Mar 3, 2026
1 check passed
@ianhe8x
Copy link
Copy Markdown

ianhe8x commented Mar 3, 2026

good catch.

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