You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

merkle_tests.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2015 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "consensus/merkle.h"
  5. #include "test/test_bitcoin.h"
  6. #include "random.h"
  7. #include <boost/test/unit_test.hpp>
  8. BOOST_FIXTURE_TEST_SUITE(merkle_tests, TestingSetup)
  9. // Older version of the merkle root computation code, for comparison.
  10. static uint256 BlockBuildMerkleTree(const CBlock& block, bool* fMutated, std::vector<uint256>& vMerkleTree)
  11. {
  12. vMerkleTree.clear();
  13. vMerkleTree.reserve(block.vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes.
  14. for (std::vector<CTransaction>::const_iterator it(block.vtx.begin()); it != block.vtx.end(); ++it)
  15. vMerkleTree.push_back(it->GetHash());
  16. int j = 0;
  17. bool mutated = false;
  18. for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
  19. {
  20. for (int i = 0; i < nSize; i += 2)
  21. {
  22. int i2 = std::min(i+1, nSize-1);
  23. if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) {
  24. // Two identical hashes at the end of the list at a particular level.
  25. mutated = true;
  26. }
  27. vMerkleTree.push_back(Hash(vMerkleTree[j+i].begin(), vMerkleTree[j+i].end(),
  28. vMerkleTree[j+i2].begin(), vMerkleTree[j+i2].end()));
  29. }
  30. j += nSize;
  31. }
  32. if (fMutated) {
  33. *fMutated = mutated;
  34. }
  35. return (vMerkleTree.empty() ? uint256() : vMerkleTree.back());
  36. }
  37. // Older version of the merkle branch computation code, for comparison.
  38. static std::vector<uint256> BlockGetMerkleBranch(const CBlock& block, const std::vector<uint256>& vMerkleTree, int nIndex)
  39. {
  40. std::vector<uint256> vMerkleBranch;
  41. int j = 0;
  42. for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
  43. {
  44. int i = std::min(nIndex^1, nSize-1);
  45. vMerkleBranch.push_back(vMerkleTree[j+i]);
  46. nIndex >>= 1;
  47. j += nSize;
  48. }
  49. return vMerkleBranch;
  50. }
  51. static inline int ctz(uint32_t i) {
  52. if (i == 0) return 0;
  53. int j = 0;
  54. while (!(i & 1)) {
  55. j++;
  56. i >>= 1;
  57. }
  58. return j;
  59. }
  60. BOOST_AUTO_TEST_CASE(merkle_test)
  61. {
  62. for (int i = 0; i < 32; i++) {
  63. // Try 32 block sizes: all sizes from 0 to 16 inclusive, and then 15 random sizes.
  64. int ntx = (i <= 16) ? i : 17 + (insecure_rand() % 4000);
  65. // Try up to 3 mutations.
  66. for (int mutate = 0; mutate <= 3; mutate++) {
  67. int duplicate1 = mutate >= 1 ? 1 << ctz(ntx) : 0; // The last how many transactions to duplicate first.
  68. if (duplicate1 >= ntx) break; // Duplication of the entire tree results in a different root (it adds a level).
  69. int ntx1 = ntx + duplicate1; // The resulting number of transactions after the first duplication.
  70. int duplicate2 = mutate >= 2 ? 1 << ctz(ntx1) : 0; // Likewise for the second mutation.
  71. if (duplicate2 >= ntx1) break;
  72. int ntx2 = ntx1 + duplicate2;
  73. int duplicate3 = mutate >= 3 ? 1 << ctz(ntx2) : 0; // And for the the third mutation.
  74. if (duplicate3 >= ntx2) break;
  75. int ntx3 = ntx2 + duplicate3;
  76. // Build a block with ntx different transactions.
  77. CBlock block;
  78. block.vtx.resize(ntx);
  79. for (int j = 0; j < ntx; j++) {
  80. CMutableTransaction mtx;
  81. mtx.nLockTime = j;
  82. block.vtx[j] = mtx;
  83. }
  84. // Compute the root of the block before mutating it.
  85. bool unmutatedMutated = false;
  86. uint256 unmutatedRoot = BlockMerkleRoot(block, &unmutatedMutated);
  87. BOOST_CHECK(unmutatedMutated == false);
  88. // Optionally mutate by duplicating the last transactions, resulting in the same merkle root.
  89. block.vtx.resize(ntx3);
  90. for (int j = 0; j < duplicate1; j++) {
  91. block.vtx[ntx + j] = block.vtx[ntx + j - duplicate1];
  92. }
  93. for (int j = 0; j < duplicate2; j++) {
  94. block.vtx[ntx1 + j] = block.vtx[ntx1 + j - duplicate2];
  95. }
  96. for (int j = 0; j < duplicate3; j++) {
  97. block.vtx[ntx2 + j] = block.vtx[ntx2 + j - duplicate3];
  98. }
  99. // Compute the merkle root and merkle tree using the old mechanism.
  100. bool oldMutated = false;
  101. std::vector<uint256> merkleTree;
  102. uint256 oldRoot = BlockBuildMerkleTree(block, &oldMutated, merkleTree);
  103. // Compute the merkle root using the new mechanism.
  104. bool newMutated = false;
  105. uint256 newRoot = BlockMerkleRoot(block, &newMutated);
  106. BOOST_CHECK(oldRoot == newRoot);
  107. BOOST_CHECK(newRoot == unmutatedRoot);
  108. BOOST_CHECK((newRoot == uint256()) == (ntx == 0));
  109. BOOST_CHECK(oldMutated == newMutated);
  110. BOOST_CHECK(newMutated == !!mutate);
  111. // If no mutation was done (once for every ntx value), try up to 16 branches.
  112. if (mutate == 0) {
  113. for (int loop = 0; loop < std::min(ntx, 16); loop++) {
  114. // If ntx <= 16, try all branches. Otherise, try 16 random ones.
  115. int mtx = loop;
  116. if (ntx > 16) {
  117. mtx = insecure_rand() % ntx;
  118. }
  119. std::vector<uint256> newBranch = BlockMerkleBranch(block, mtx);
  120. std::vector<uint256> oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx);
  121. BOOST_CHECK(oldBranch == newBranch);
  122. BOOST_CHECK(ComputeMerkleRootFromBranch(block.vtx[mtx].GetHash(), newBranch, mtx) == oldRoot);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. BOOST_AUTO_TEST_SUITE_END()