fix(model): reject duplicate subtree roots in top-level merkle tree#761
Merged
ordishs merged 3 commits intoMay 12, 2026
Merged
Conversation
Contributor
|
🤖 Claude Code Review Status: Complete Current Review: This PR adds defense-in-depth validation to CheckMerkleRoot by rejecting duplicate subtree root hashes before building the top-level merkle tree. The implementation is sound: ✅ Implementation Quality:
✅ Test Coverage:
✅ Context Verification:
No issues found. The fix appropriately closes the hardening gap identified in the audit without introducing side effects. |
Contributor
Benchmark Comparison ReportBaseline: Current: Summary
All benchmark results (sec/op)
Threshold: >10% with p < 0.05 | Generated: 2026-05-12 08:24 UTC |
icellan
approved these changes
May 12, 2026
freemans13
approved these changes
May 12, 2026
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Closes #4593.
Summary
Defense-in-depth:
Block.CheckMerkleRootbuilds a top-level merkle tree from subtree-root hashes without an explicit duplicate-hash guard. Duplicates at this layer would indicate a bug elsewhere (per-tx duplicates are caught upstream), but the missing guard means a malformed block passing earlier checks could still produce a coincidentally-valid merkle root. The audit (#4593) flagged this as a hardening gap with no current consensus impact.Fix
Add an O(n)
map[chainhash.Hash]struct{}dedup pass folded into the existingAddNodeloop. On any collision, returnBlockInvalidErrorbefore continuing tree construction.Test plan
model/Block_test.goconstructs a block with two subtrees sharing the same root hash; assertsCheckMerkleRootreturnsBlockInvalidErrorand the message identifies the colliding hash.go test -race ./model/...passes (no regression in existingCheckMerkleRoottests).