Merge bitcoin/bitcoin#34376: bench/test: clarify merkle bench and witness test intent

8b9d30e3fa bench/test: clarify merkle bench and witness test intent (Lőrinc)

Pull request description:

  Follow-up to #32497.

  Clarify why the witness merkle test uses an odd leaf count (it exercises leaf duplication in `ComputeMerkleRoot()`), and make the coinbase witness hash initialization explicit.

  Also simplify the leaf-copy loop in the `MerkleRoot` benchmark for readability.

  No production code is changed in this follow-up, for simplicity and safety.

ACKs for top commit:
  optout21:
    ACK 8b9d30e3fa
  maflcko:
    lgtm ACK 8b9d30e3fa
  achow101:
    ACK 8b9d30e3fa
  w0xlt:
    ACK 8b9d30e3fa
  danielabrozzoni:
    tACK 8b9d30e3fa

Tree-SHA512: 6efca7c19ebf96bb8d0def4217ed30d3b74b58a7be15566967e98aba9b03aaddd0e0ebb3b8f43130b5f397a7d9eed0470a48a55438f440e0bceefb87edd16b27
This commit is contained in:
Ava Chow
2026-01-22 13:49:33 -08:00
2 changed files with 4 additions and 3 deletions

View File

@@ -25,8 +25,8 @@ static void MerkleRoot(benchmark::Bench& bench)
bench.name(mutate ? "MerkleRootWithMutation" : "MerkleRoot").batch(hashes.size()).unit("leaf").run([&] {
std::vector<uint256> leaves;
leaves.reserve((hashes.size() + 1) & ~1ULL); // capacity rounded up to even
for (size_t s = 0; s < hashes.size(); s++) {
leaves.push_back(hashes[s]);
for (const auto& hash : hashes) {
leaves.push_back(hash);
}
bool mutated{false};

View File

@@ -243,7 +243,8 @@ BOOST_AUTO_TEST_CASE(merkle_test_BlockWitness)
uint256 blockWitness = BlockWitnessMerkleRoot(block);
std::vector<uint256> hashes;
hashes.resize(vtx_count); // Note: leaving odd count to exercise old behavior
hashes.resize(vtx_count); // Odd count exercises leaf duplication in ComputeMerkleRoot (which can append one extra hash).
hashes[0] = uint256::ZERO; // The witness hash of the coinbase is 0.
for (size_t pos{1}; pos < vtx_count; ++pos) {
hashes[pos] = block.vtx[pos]->GetWitnessHash().ToUint256();
}