Skip to content

interfaces: fix findAncestorByHash false positive - #36473

Closed
Romansage6 wants to merge 1 commit into
bitcoin:masterfrom
Romansage6:2026-10-fix-findancestorbyhash
Closed

Romansage6 wants to merge 1 commit into
bitcoin:masterfrom
Romansage6:2026-10-fix-findancestorbyhash

Conversation

@Romansage6

Copy link
Copy Markdown

findAncestorByHash looks up both block_hash and ancestor_hash, then was
only supposed to clear the ancestor pointer when the lookups succeeded
but the ancestor relationship didn't hold:

if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;

Because of the && short circuit, an unknown block_hash makes the
whole condition false without ever evaluating the relationship check,
so a successful ancestor_hash lookup is passed through unmodified
and the function reports true for a descendant that doesn't exist.

Before:

chain->findAncestorByHash(uint256{}, active[0]->GetBlockHash()) // returns true

After this change it correctly returns false when either hash is
unknown, in addition to the existing relationship check.

Tested with:

cmake --build build --target test_bitcoin -j8
./build/bin/test_bitcoin --run_test=interfaces_tests

Full suite (./build/bin/test_bitcoin, 877 cases) also passes with no
regressions. Added two new cases to interfaces_tests/findAncestorByHash
covering unknown block_hash and unknown ancestor_hash, which fail
on master and pass with this fix.

Fixes #36472

findAncestorByHash looked up both block_hash and ancestor_hash, then
cleared the ancestor pointer only when both lookups succeeded but the
ancestor relationship did not hold:

  if (block && ancestor && block->GetAncestor(...) != ancestor)
      ancestor = nullptr;

Because of the && short circuit, an unknown block_hash made the whole
condition false without ever running the relationship check, so a
successful ancestor_hash lookup was passed through unmodified and the
function reported true for a descendant that does not exist.

Clear the ancestor whenever either lookup fails, not only when the
relationship check fails.

Add unit test cases for an unknown block_hash and an unknown
ancestor_hash, matching the reported regression.

Fixes bitcoin#36472
@DrahtBot DrahtBot added the IPC label Oct 8, 2026
@DrahtBot

DrahtBot commented Oct 8, 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 8, 2026

Copy link
Copy Markdown
Contributor

This seems to be a drive-by LLM contribution from an issue that was created not even an hour ago. Closing again.

@sedited sedited closed this Oct 8, 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

3 participants