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.

block.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "core/block.h"
  6. #include "hash.h"
  7. #include "tinyformat.h"
  8. #include "utilstrencodings.h"
  9. uint256 CBlockHeader::GetHash() const
  10. {
  11. return Hash(BEGIN(nVersion), END(nNonce));
  12. }
  13. uint256 CBlock::BuildMerkleTree(bool* fMutated) const
  14. {
  15. /* WARNING! If you're reading this because you're learning about crypto
  16. and/or designing a new system that will use merkle trees, keep in mind
  17. that the following merkle tree algorithm has a serious flaw related to
  18. duplicate txids, resulting in a vulnerability (CVE-2012-2459).
  19. The reason is that if the number of hashes in the list at a given time
  20. is odd, the last one is duplicated before computing the next level (which
  21. is unusual in Merkle trees). This results in certain sequences of
  22. transactions leading to the same merkle root. For example, these two
  23. trees:
  24. A A
  25. / \ / \
  26. B C B C
  27. / \ | / \ / \
  28. D E F D E F F
  29. / \ / \ / \ / \ / \ / \ / \
  30. 1 2 3 4 5 6 1 2 3 4 5 6 5 6
  31. for transaction lists [1,2,3,4,5,6] and [1,2,3,4,5,6,5,6] (where 5 and
  32. 6 are repeated) result in the same root hash A (because the hash of both
  33. of (F) and (F,F) is C).
  34. The vulnerability results from being able to send a block with such a
  35. transaction list, with the same merkle root, and the same block hash as
  36. the original without duplication, resulting in failed validation. If the
  37. receiving node proceeds to mark that block as permanently invalid
  38. however, it will fail to accept further unmodified (and thus potentially
  39. valid) versions of the same block. We defend against this by detecting
  40. the case where we would hash two identical hashes at the end of the list
  41. together, and treating that identically to the block having an invalid
  42. merkle root. Assuming no double-SHA256 collisions, this will detect all
  43. known ways of changing the transactions without affecting the merkle
  44. root.
  45. */
  46. vMerkleTree.clear();
  47. vMerkleTree.reserve(vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes.
  48. for (std::vector<CTransaction>::const_iterator it(vtx.begin()); it != vtx.end(); ++it)
  49. vMerkleTree.push_back(it->GetHash());
  50. int j = 0;
  51. bool mutated = false;
  52. for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
  53. {
  54. for (int i = 0; i < nSize; i += 2)
  55. {
  56. int i2 = std::min(i+1, nSize-1);
  57. if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) {
  58. // Two identical hashes at the end of the list at a particular level.
  59. mutated = true;
  60. }
  61. vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]),
  62. BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2])));
  63. }
  64. j += nSize;
  65. }
  66. if (fMutated) {
  67. *fMutated = mutated;
  68. }
  69. return (vMerkleTree.empty() ? 0 : vMerkleTree.back());
  70. }
  71. std::vector<uint256> CBlock::GetMerkleBranch(int nIndex) const
  72. {
  73. if (vMerkleTree.empty())
  74. BuildMerkleTree();
  75. std::vector<uint256> vMerkleBranch;
  76. int j = 0;
  77. for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
  78. {
  79. int i = std::min(nIndex^1, nSize-1);
  80. vMerkleBranch.push_back(vMerkleTree[j+i]);
  81. nIndex >>= 1;
  82. j += nSize;
  83. }
  84. return vMerkleBranch;
  85. }
  86. uint256 CBlock::CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
  87. {
  88. if (nIndex == -1)
  89. return 0;
  90. for (std::vector<uint256>::const_iterator it(vMerkleBranch.begin()); it != vMerkleBranch.end(); ++it)
  91. {
  92. if (nIndex & 1)
  93. hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
  94. else
  95. hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
  96. nIndex >>= 1;
  97. }
  98. return hash;
  99. }
  100. std::string CBlock::ToString() const
  101. {
  102. std::stringstream s;
  103. s << strprintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
  104. GetHash().ToString(),
  105. nVersion,
  106. hashPrevBlock.ToString(),
  107. hashMerkleRoot.ToString(),
  108. nTime, nBits, nNonce,
  109. vtx.size());
  110. for (unsigned int i = 0; i < vtx.size(); i++)
  111. {
  112. s << " " << vtx[i].ToString() << "\n";
  113. }
  114. s << " vMerkleTree: ";
  115. for (unsigned int i = 0; i < vMerkleTree.size(); i++)
  116. s << " " << vMerkleTree[i].ToString();
  117. s << "\n";
  118. return s.str();
  119. }