bench/test: clarify merkle bench and witness test intent

Follow-up to bitcoin/bitcoin#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.

Co-authored-by: w0xlt <94266259+w0xlt@users.noreply.github.com>
This commit is contained in:
Lőrinc 2026-01-21 23:22:22 +01:00
parent 5715748333
commit 8b9d30e3fa
No known key found for this signature in database
GPG Key ID: 669FFF0FFA477A76
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();
}