Skip to content

Commit b3067f4

Browse files
author
MarcoFalke
committed
Merge #16865: test: add some unit tests for merkle.cpp
8573429 test: add some unit tests for merkle.cpp (soroosh-sdi) Pull request description: following situations are covered: - empty block - one Tx - Merkle root of a block with odd Txs should not change with repeating last one - Merkle root is computed with combining Merkle root of left subtree and right subtree - block witness is Merkle root of a block when setting first Tx to zero. Signed-off-by: soroosh-sdi <[email protected]> ACKs for top commit: laanwj: ACK 8573429 Tree-SHA512: e12228171de8f8480f173c9d9d0359f00f46bf09075e0767f5f1a367478a1b7b6d177d230f7e930914915cd2c6b66b18d24b1682f1233c38e97954ba331e5773
2 parents 99beda4 + 8573429 commit b3067f4

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

src/test/merkle_tests.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,104 @@ BOOST_AUTO_TEST_CASE(merkle_test)
249249
}
250250
}
251251

252+
253+
BOOST_AUTO_TEST_CASE(merkle_test_empty_block)
254+
{
255+
bool mutated = false;
256+
CBlock block;
257+
uint256 root = BlockMerkleRoot(block, &mutated);
258+
259+
BOOST_CHECK_EQUAL(root.IsNull(), true);
260+
BOOST_CHECK_EQUAL(mutated, false);
261+
}
262+
263+
BOOST_AUTO_TEST_CASE(merkle_test_oneTx_block)
264+
{
265+
bool mutated = false;
266+
CBlock block;
267+
268+
block.vtx.resize(1);
269+
CMutableTransaction mtx;
270+
mtx.nLockTime = 0;
271+
block.vtx[0] = MakeTransactionRef(std::move(mtx));
272+
uint256 root = BlockMerkleRoot(block, &mutated);
273+
BOOST_CHECK_EQUAL(root, block.vtx[0]->GetHash());
274+
BOOST_CHECK_EQUAL(mutated, false);
275+
}
276+
277+
BOOST_AUTO_TEST_CASE(merkle_test_OddTxWithRepeatedLastTx_block)
278+
{
279+
bool mutated;
280+
CBlock block, blockWithRepeatedLastTx;
281+
282+
block.vtx.resize(3);
283+
284+
for (std::size_t pos = 0; pos < block.vtx.size(); pos++) {
285+
CMutableTransaction mtx;
286+
mtx.nLockTime = pos;
287+
block.vtx[pos] = MakeTransactionRef(std::move(mtx));
288+
}
289+
290+
blockWithRepeatedLastTx = block;
291+
blockWithRepeatedLastTx.vtx.push_back(blockWithRepeatedLastTx.vtx.back());
292+
293+
uint256 rootofBlock = BlockMerkleRoot(block, &mutated);
294+
BOOST_CHECK_EQUAL(mutated, false);
295+
296+
uint256 rootofBlockWithRepeatedLastTx = BlockMerkleRoot(blockWithRepeatedLastTx, &mutated);
297+
BOOST_CHECK_EQUAL(rootofBlock, rootofBlockWithRepeatedLastTx);
298+
BOOST_CHECK_EQUAL(mutated, true);
299+
}
300+
301+
BOOST_AUTO_TEST_CASE(merkle_test_LeftSubtreeRightSubtree)
302+
{
303+
CBlock block, leftSubtreeBlock, rightSubtreeBlock;
304+
305+
block.vtx.resize(4);
306+
std::size_t pos;
307+
for (pos = 0; pos < block.vtx.size(); pos++) {
308+
CMutableTransaction mtx;
309+
mtx.nLockTime = pos;
310+
block.vtx[pos] = MakeTransactionRef(std::move(mtx));
311+
}
312+
313+
for (pos = 0; pos < block.vtx.size() / 2; pos++)
314+
leftSubtreeBlock.vtx.push_back(block.vtx[pos]);
315+
316+
for (pos = block.vtx.size() / 2; pos < block.vtx.size(); pos++)
317+
rightSubtreeBlock.vtx.push_back(block.vtx[pos]);
318+
319+
uint256 root = BlockMerkleRoot(block);
320+
uint256 rootOfLeftSubtree = BlockMerkleRoot(leftSubtreeBlock);
321+
uint256 rootOfRightSubtree = BlockMerkleRoot(rightSubtreeBlock);
322+
std::vector<uint256> leftRight;
323+
leftRight.push_back(rootOfLeftSubtree);
324+
leftRight.push_back(rootOfRightSubtree);
325+
uint256 rootOfLR = ComputeMerkleRoot(leftRight);
326+
327+
BOOST_CHECK_EQUAL(root, rootOfLR);
328+
}
329+
330+
BOOST_AUTO_TEST_CASE(merkle_test_BlockWitness)
331+
{
332+
CBlock block;
333+
334+
block.vtx.resize(2);
335+
for (std::size_t pos = 0; pos < block.vtx.size(); pos++) {
336+
CMutableTransaction mtx;
337+
mtx.nLockTime = pos;
338+
block.vtx[pos] = MakeTransactionRef(std::move(mtx));
339+
}
340+
341+
uint256 blockWitness = BlockWitnessMerkleRoot(block);
342+
343+
std::vector<uint256> hashes;
344+
hashes.resize(block.vtx.size());
345+
hashes[0].SetNull();
346+
hashes[1] = block.vtx[1]->GetHash();
347+
348+
uint256 merkelRootofHashes = ComputeMerkleRoot(hashes);
349+
350+
BOOST_CHECK_EQUAL(merkelRootofHashes, blockWitness);
351+
}
252352
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)