Skip to content

interfaces: fix findAncestorByHash returning true for unknown block - #36490

Closed
Yudis-bit wants to merge 1 commit into
bitcoin:masterfrom
Yudis-bit:fix-findancestor-unknown
Closed

Yudis-bit wants to merge 1 commit into
bitcoin:masterfrom
Yudis-bit:fix-findancestor-unknown

Conversation

@Yudis-bit

Copy link
Copy Markdown

Fixes #36472.

When findAncestorByHash(block_hash, ancestor_hash, ...) is called with an unknown or null block_hash, LookupBlockIndex(block_hash) returns nullptr. The preceding check if (block && ancestor && block->GetAncestor(...) != ancestor) ancestor = nullptr; evaluates to false when block is null, leaving ancestor untouched. If ancestor_hash exists, FillBlock(ancestor, ...) evaluates to true, falsely reporting that an unknown descendant descends from a known block.

This patch updates the guard in src/node/interfaces.cpp to ensure ancestor is reset to null whenever !block || !ancestor || block->GetAncestor(ancestor->nHeight) != ancestor.

Unit test coverage is added in src/test/interfaces_tests.cpp asserting that findAncestorByHash returns false when block_hash is null, preserves height = -1 in the output struct, returns false when ancestor_hash is null, returns false when both are null, and correctly returns true for identical blocks.

When the block argument cannot be resolved via LookupBlockIndex (i.e. block is nullptr), findAncestorByHash previously left ancestor untouched, incorrectly returning true if ancestor was valid.

Fix this by ensuring ancestor is set to nullptr if either block or ancestor is null, or if block does not descend from ancestor. Add unit test assertions covering unknown block, unknown ancestor, both unknown, and height preservation.
Copilot AI balanced review requested due to automatic review settings October 9, 2026 16:01
@DrahtBot DrahtBot added the IPC label Oct 9, 2026

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@DrahtBot

DrahtBot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

@sedited

sedited commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Another duplicate of #36473 (comment)

@sedited sedited closed this Oct 9, 2026
ryanofsky added a commit to ryanofsky/bitcoin that referenced this pull request Nov 5, 2026
Chain::findAncestorByHash returned true when block_hash was unknown and
ancestor_hash was known, because the check clearing the ancestor pointer
only ran when both hashes were found. Clear the ancestor whenever the
block is not found.

Fixes bitcoin#36472, reported by enirox001 while testing the IPC Chain interface
from bitcoin#29409. The same fix was previously proposed in bitcoin#36473, bitcoin#36484 and
bitcoin#36490 by Romansage6 and Yudis-bit.

This change was written with Claude Opus 5.5 (1M context).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

interfaces: findAncestorByHash returns true for an unknown descendant

4 participants