Skip to content

Commit

Permalink
consesus/mekle: don't perform the ignored malleation check for witnesses
Browse files Browse the repository at this point in the history
The result of the malleation check is ignored, since it's not possible
to exploit it for the witness tree without exploiting it for the
transaction tree (where we do check it).

The check involves going through all the leaves and nodes of the tree
and comparing them. Since the result is ignored, we better not perform
the check at all.

Since the ignored `malleated` boolean was the only call site, remove the
argument altogether.
  • Loading branch information
darosior committed Mar 1, 2024
1 parent ba907f9 commit 1c1fa20
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/consensus/merkle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ uint256 BlockMerkleRoot(const CBlock& block, bool* mutated)
return ComputeMerkleRoot(std::move(leaves), mutated);
}

uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated)
uint256 BlockWitnessMerkleRoot(const CBlock& block)
{
std::vector<uint256> leaves;
leaves.resize(block.vtx.size());
leaves[0].SetNull(); // The witness hash of the coinbase is 0.
for (size_t s = 1; s < block.vtx.size(); s++) {
leaves[s] = block.vtx[s]->GetWitnessHash();
}
return ComputeMerkleRoot(std::move(leaves), mutated);
return ComputeMerkleRoot(std::move(leaves));
}

2 changes: 1 addition & 1 deletion src/consensus/merkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ uint256 BlockMerkleRoot(const CBlock& block, bool* mutated = nullptr);
* Compute the Merkle root of the witness transactions in a block.
* *mutated is set to true if a duplicated subtree was found.
*/
uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated = nullptr);
uint256 BlockWitnessMerkleRoot(const CBlock& block);

#endif // BITCOIN_CONSENSUS_MERKLE_H
5 changes: 2 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3754,7 +3754,7 @@ std::vector<unsigned char> ChainstateManager::GenerateCoinbaseCommitment(CBlock&
int commitpos = GetWitnessCommitmentIndex(block);
std::vector<unsigned char> ret(32, 0x00);
if (commitpos == NO_WITNESS_COMMITMENT) {
uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
uint256 witnessroot = BlockWitnessMerkleRoot(block);
CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot);
CTxOut out;
out.nValue = 0;
Expand Down Expand Up @@ -3893,8 +3893,7 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
if (DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_SEGWIT)) {
int commitpos = GetWitnessCommitmentIndex(block);
if (commitpos != NO_WITNESS_COMMITMENT) {
bool malleated = false;
uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
uint256 hashWitness = BlockWitnessMerkleRoot(block);
// The malleation check is ignored; as the transaction tree itself
// already does not permit it, it is impossible to trigger in the
// witness tree.
Expand Down

0 comments on commit 1c1fa20

Please sign in to comment.