bench/test: clarify merkle bench and witness test intent
What changed, and why it matters
This is a follow-up cleanup commit that only touches benchmark and test files. It rewrites a loop for readability and adds explanatory comments about why a test uses an odd number of leaves and how the coinbase witness hash is initialized. No production code is changed, so there is no security impact.
No security action needed. This is a non-functional documentation and readability improvement in test/benchmark code.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies src/bench/merkle_root.cpp to replace an index-based loop with a range-based for loop when copying hashes into a leaves vector. It also updates src/test/merkle_tests.cpp to clarify that the odd leaf count in the witness merkle test exercises leaf duplication in ComputeMerkleRoot(), and explicitly sets hashes[0] to uint256::ZERO to represent the coinbase witness hash. The commit message explicitly states no production code is changed.
Changed components
src/bench/merkle_root.cppsrc/test/merkle_tests.cppInspect captured patch +4 / −3
diff --git a/src/bench/merkle_root.cpp b/src/bench/merkle_root.cpp
index 0f59ccf8..17f7fa86 100644
--- a/src/bench/merkle_root.cpp
+++ b/src/bench/merkle_root.cpp
@@ -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};
diff --git a/src/test/merkle_tests.cpp b/src/test/merkle_tests.cpp
index 347261df..dc479141 100644
--- a/src/test/merkle_tests.cpp
+++ b/src/test/merkle_tests.cpp
@@ -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();
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.