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:
- 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.
- 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() https://github.com/bitcoin/bitcoin/blob/8f7b9eb8711fdb32e8bdb59d2a7462a46c7a8086/src/validation.cpp#L3534-L3542 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.