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.

merkleblock.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "merkleblock.h"
  6. #include "hash.h"
  7. #include "consensus/consensus.h"
  8. #include "utilstrencodings.h"
  9. using namespace std;
  10. CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter)
  11. {
  12. header = block.GetBlockHeader();
  13. vector<bool> vMatch;
  14. vector<uint256> vHashes;
  15. vMatch.reserve(block.vtx.size());
  16. vHashes.reserve(block.vtx.size());
  17. for (unsigned int i = 0; i < block.vtx.size(); i++)
  18. {
  19. const uint256& hash = block.vtx[i].GetHash();
  20. if (filter.IsRelevantAndUpdate(block.vtx[i]))
  21. {
  22. vMatch.push_back(true);
  23. vMatchedTxn.push_back(make_pair(i, hash));
  24. }
  25. else
  26. vMatch.push_back(false);
  27. vHashes.push_back(hash);
  28. }
  29. txn = CPartialMerkleTree(vHashes, vMatch);
  30. }
  31. CMerkleBlock::CMerkleBlock(const CBlock& block, const std::set<uint256>& txids)
  32. {
  33. header = block.GetBlockHeader();
  34. vector<bool> vMatch;
  35. vector<uint256> vHashes;
  36. vMatch.reserve(block.vtx.size());
  37. vHashes.reserve(block.vtx.size());
  38. for (unsigned int i = 0; i < block.vtx.size(); i++)
  39. {
  40. const uint256& hash = block.vtx[i].GetHash();
  41. if (txids.count(hash))
  42. vMatch.push_back(true);
  43. else
  44. vMatch.push_back(false);
  45. vHashes.push_back(hash);
  46. }
  47. txn = CPartialMerkleTree(vHashes, vMatch);
  48. }
  49. uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid) {
  50. if (height == 0) {
  51. // hash at height 0 is the txids themself
  52. return vTxid[pos];
  53. } else {
  54. // calculate left hash
  55. uint256 left = CalcHash(height-1, pos*2, vTxid), right;
  56. // calculate right hash if not beyond the end of the array - copy left hash otherwise1
  57. if (pos*2+1 < CalcTreeWidth(height-1))
  58. right = CalcHash(height-1, pos*2+1, vTxid);
  59. else
  60. right = left;
  61. // combine subhashes
  62. return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
  63. }
  64. }
  65. void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) {
  66. // determine whether this node is the parent of at least one matched txid
  67. bool fParentOfMatch = false;
  68. for (unsigned int p = pos << height; p < (pos+1) << height && p < nTransactions; p++)
  69. fParentOfMatch |= vMatch[p];
  70. // store as flag bit
  71. vBits.push_back(fParentOfMatch);
  72. if (height==0 || !fParentOfMatch) {
  73. // if at height 0, or nothing interesting below, store hash and stop
  74. vHash.push_back(CalcHash(height, pos, vTxid));
  75. } else {
  76. // otherwise, don't store any hash, but descend into the subtrees
  77. TraverseAndBuild(height-1, pos*2, vTxid, vMatch);
  78. if (pos*2+1 < CalcTreeWidth(height-1))
  79. TraverseAndBuild(height-1, pos*2+1, vTxid, vMatch);
  80. }
  81. }
  82. uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch) {
  83. if (nBitsUsed >= vBits.size()) {
  84. // overflowed the bits array - failure
  85. fBad = true;
  86. return uint256();
  87. }
  88. bool fParentOfMatch = vBits[nBitsUsed++];
  89. if (height==0 || !fParentOfMatch) {
  90. // if at height 0, or nothing interesting below, use stored hash and do not descend
  91. if (nHashUsed >= vHash.size()) {
  92. // overflowed the hash array - failure
  93. fBad = true;
  94. return uint256();
  95. }
  96. const uint256 &hash = vHash[nHashUsed++];
  97. if (height==0 && fParentOfMatch) // in case of height 0, we have a matched txid
  98. vMatch.push_back(hash);
  99. return hash;
  100. } else {
  101. // otherwise, descend into the subtrees to extract matched txids and hashes
  102. uint256 left = TraverseAndExtract(height-1, pos*2, nBitsUsed, nHashUsed, vMatch), right;
  103. if (pos*2+1 < CalcTreeWidth(height-1)) {
  104. right = TraverseAndExtract(height-1, pos*2+1, nBitsUsed, nHashUsed, vMatch);
  105. if (right == left) {
  106. // The left and right branches should never be identical, as the transaction
  107. // hashes covered by them must each be unique.
  108. fBad = true;
  109. }
  110. } else {
  111. right = left;
  112. }
  113. // and combine them before returning
  114. return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
  115. }
  116. }
  117. CPartialMerkleTree::CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) : nTransactions(vTxid.size()), fBad(false) {
  118. // reset state
  119. vBits.clear();
  120. vHash.clear();
  121. // calculate height of tree
  122. int nHeight = 0;
  123. while (CalcTreeWidth(nHeight) > 1)
  124. nHeight++;
  125. // traverse the partial tree
  126. TraverseAndBuild(nHeight, 0, vTxid, vMatch);
  127. }
  128. CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}
  129. uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch) {
  130. vMatch.clear();
  131. // An empty set will not work
  132. if (nTransactions == 0)
  133. return uint256();
  134. // check for excessively high numbers of transactions
  135. if (nTransactions > MAX_BLOCK_SIZE / 60) // 60 is the lower bound for the size of a serialized CTransaction
  136. return uint256();
  137. // there can never be more hashes provided than one for every txid
  138. if (vHash.size() > nTransactions)
  139. return uint256();
  140. // there must be at least one bit per node in the partial tree, and at least one node per hash
  141. if (vBits.size() < vHash.size())
  142. return uint256();
  143. // calculate height of tree
  144. int nHeight = 0;
  145. while (CalcTreeWidth(nHeight) > 1)
  146. nHeight++;
  147. // traverse the partial tree
  148. unsigned int nBitsUsed = 0, nHashUsed = 0;
  149. uint256 hashMerkleRoot = TraverseAndExtract(nHeight, 0, nBitsUsed, nHashUsed, vMatch);
  150. // verify that no problems occurred during the tree traversal
  151. if (fBad)
  152. return uint256();
  153. // verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
  154. if ((nBitsUsed+7)/8 != (vBits.size()+7)/8)
  155. return uint256();
  156. // verify that all hashes were consumed
  157. if (nHashUsed != vHash.size())
  158. return uint256();
  159. return hashMerkleRoot;
  160. }