From f0a21831087410687c4ca31ac00e44f380b859be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Sat, 22 Nov 2025 13:30:04 +0100 Subject: [PATCH] test: adjust `ComputeMerkleRoot` tests Update the integer fuzz test to move the vector into `ComputeMerkleRoot`, matching production usage patterns and avoiding unnecessary copies. Update `merkle_test_BlockWitness` to use an odd number of transactions to ensure the test covers the scenario where leaf duplication occurs. Also switch to `GetWitnessHash` to match `BlockWitnessMerkleRoot` semantics. The manual vector setup retains the exact-size `resize` to explicitly verify the behavior against the calculated root. --- src/test/fuzz/integer.cpp | 4 ++-- src/test/merkle_tests.cpp | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/test/fuzz/integer.cpp b/src/test/fuzz/integer.cpp index 89c29dbcb70..3effc1c19f0 100644 --- a/src/test/fuzz/integer.cpp +++ b/src/test/fuzz/integer.cpp @@ -80,8 +80,8 @@ FUZZ_TARGET(integer, .init = initialize_integer) } constexpr uint256 u256_min{"0000000000000000000000000000000000000000000000000000000000000000"}; constexpr uint256 u256_max{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}; - const std::vector v256{u256, u256_min, u256_max}; - (void)ComputeMerkleRoot(v256); + std::vector v256{u256, u256_min, u256_max}; + (void)ComputeMerkleRoot(std::move(v256)); (void)DecompressAmount(u64); { if (std::optional parsed = ParseMoney(FormatMoney(i64))) { diff --git a/src/test/merkle_tests.cpp b/src/test/merkle_tests.cpp index 649da07cf7e..3a5720fedd5 100644 --- a/src/test/merkle_tests.cpp +++ b/src/test/merkle_tests.cpp @@ -232,8 +232,9 @@ BOOST_AUTO_TEST_CASE(merkle_test_BlockWitness) { CBlock block; - block.vtx.resize(2); - for (std::size_t pos = 0; pos < block.vtx.size(); pos++) { + constexpr size_t vtx_count{3}; + block.vtx.resize(vtx_count); + for (std::size_t pos = 0; pos < vtx_count; pos++) { CMutableTransaction mtx; mtx.nLockTime = pos; block.vtx[pos] = MakeTransactionRef(std::move(mtx)); @@ -242,12 +243,12 @@ BOOST_AUTO_TEST_CASE(merkle_test_BlockWitness) uint256 blockWitness = BlockWitnessMerkleRoot(block); std::vector hashes; - hashes.resize(block.vtx.size()); - hashes[0].SetNull(); - hashes[1] = block.vtx[1]->GetHash().ToUint256(); + hashes.resize(vtx_count); // Note: leaving odd count to exercise old behavior + for (size_t pos{1}; pos < vtx_count; ++pos) { + hashes[pos] = block.vtx[pos]->GetWitnessHash().ToUint256(); + } uint256 merkleRootofHashes = ComputeMerkleRoot(hashes); - BOOST_CHECK_EQUAL(merkleRootofHashes, blockWitness); } BOOST_AUTO_TEST_SUITE_END()