Skip to content

consensus: Better document ComputeMerkleRoot & add test for return value #28457

Closed

Description

@fanquake

At least twice we've had attempts to refactor ComputeMerkleRoot (#22046 & #22046). While the value of any sort of refactor is dubious, it may also come with subtle, unintended side-effects, see review comment here:

  1. The added break only saves time if we receive a mutated block with repeated transactions. So this change isn't relevant unless a peer send us a block they mutated on purpose, in which case they get disconnected for that. So it really shouldn't change anything in a non-adversarial scenario.
  1. If we break early and don't compute the full merkle root anymore, the return value of BlockMerkleRoot() changes. As a result, code calling BlockMerkleRoot()

    bitcoin/src/validation.cpp

    Lines 3534 to 3542 in 8f7b9eb

    uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
    if (block.hashMerkleRoot != hashMerkleRoot2)
    return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-txnmrklroot", "hashMerkleRoot mismatch");
    // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
    // of transactions in a block without affecting the merkle root of a block,
    // while still invalidating it.
    if (mutated)
    return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-txns-duplicate", "duplicate transaction");
    would now return "bad-txnmrklroot" as an error instead of "bad-txns-duplicate", because now the first check already fails. This seems incorrect.

It might be good to better document this code, and add a test that would fail if a similar refactor is attempted in future.

Activity

  1. jonatack commented on Sep 12, 2023

    @jonatack
    Member

    subtle, unintended side-effects, see review comment here:

    1. If we break early and don't compute the full merkle root anymore

    I'm not sure that review is correct. The break only exits the loop that sets the mutation localvar to save on needless further iterations. There shouldn't be a change in behavior.

  2. mzumsande commented on Sep 12, 2023

    @mzumsande
    Contributor

    @jonatack is right, I misread the code, and the PR doesn't change behavior. Might still be good to better document this code though.

  3. fanquake commented on Sep 12, 2023

    @fanquake
    MemberAuthor

    Might still be good to better document this code though.

    Yea. I don't think we are going to make the suggested change in either case.

  4. l0rinc commented on Apr 26, 2026

    @l0rinc
    Contributor

    My understanding is that no code change is needed here, only documentation that detecting a mutation only sets the output flag, not the returned root (plus an explicit regression test for that invariant), see #35161.

  5. fanquake commented on May 22, 2026

    @fanquake
    MemberAuthor

    Any discussion can continue in #35161.

  6. added a commit that references this issue on Aug 20, 2026
    02306cc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions