interfaces: fix findAncestorByHash returning true for unknown block #36490

Closed
Yudis-bit wants to merge 1 commit from fix-findancestor-unknown into master AGit
Yudis-bit commented 2026-10-09 16:01:38 +00:00 (Migrated from github.com)

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.

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.
Copilot (Migrated from github.com) reviewed 2026-10-09 16:01:44 +00:00
Copilot (Migrated from github.com) left a comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
DrahtBot commented 2026-10-09 16:01:48 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36490 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36490.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36490#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Base: 4bacf21a13c2ed25ef9362ca38f26bf0a67d22c9
Head: 3cae888dc80124123d9c14c46fe0445feeda1c4e

I found no actionable issues in this static review.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `4bacf21a13c2ed25ef9362ca38f26bf0a67d22c9` Head: `3cae888dc80124123d9c14c46fe0445feeda1c4e` I found no actionable issues in this static review. [Full review report](<https://review.fish.foo/traces/36490-3cae888dc80124123d9c14c46fe0445feeda1c4e-652-1.html>)
sedited commented 2026-10-09 19:40:38 +00:00 (Migrated from github.com)
Another duplicate of https://github.com/bitcoin/bitcoin/pull/36473#issuecomment-6060602624
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36490
No description provided.