2013-01-08 12:02:51 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-01-08 12:02:51 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-11-18 22:03:02 +01:00
|
|
|
#include "primitives/block.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2014-09-25 05:32:36 +02:00
|
|
|
#include "hash.h"
|
2014-08-20 10:51:18 +02:00
|
|
|
#include "tinyformat.h"
|
2014-09-25 05:32:36 +02:00
|
|
|
#include "utilstrencodings.h"
|
2014-12-19 11:39:38 +01:00
|
|
|
#include "crypto/common.h"
|
2013-01-08 13:17:15 +01:00
|
|
|
|
2013-06-25 03:03:03 +02:00
|
|
|
uint256 CBlockHeader::GetHash() const
|
|
|
|
{
|
2015-01-18 19:23:05 +01:00
|
|
|
return SerializeHash(*this);
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
2014-09-16 00:30:05 +02:00
|
|
|
uint256 CBlock::BuildMerkleTree(bool* fMutated) const
|
2013-06-25 03:03:03 +02:00
|
|
|
{
|
2014-09-16 00:30:05 +02:00
|
|
|
/* WARNING! If you're reading this because you're learning about crypto
|
|
|
|
and/or designing a new system that will use merkle trees, keep in mind
|
|
|
|
that the following merkle tree algorithm has a serious flaw related to
|
|
|
|
duplicate txids, resulting in a vulnerability (CVE-2012-2459).
|
|
|
|
|
|
|
|
The reason is that if the number of hashes in the list at a given time
|
|
|
|
is odd, the last one is duplicated before computing the next level (which
|
|
|
|
is unusual in Merkle trees). This results in certain sequences of
|
|
|
|
transactions leading to the same merkle root. For example, these two
|
|
|
|
trees:
|
|
|
|
|
|
|
|
A A
|
|
|
|
/ \ / \
|
|
|
|
B C B C
|
|
|
|
/ \ | / \ / \
|
|
|
|
D E F D E F F
|
|
|
|
/ \ / \ / \ / \ / \ / \ / \
|
|
|
|
1 2 3 4 5 6 1 2 3 4 5 6 5 6
|
|
|
|
|
|
|
|
for transaction lists [1,2,3,4,5,6] and [1,2,3,4,5,6,5,6] (where 5 and
|
|
|
|
6 are repeated) result in the same root hash A (because the hash of both
|
|
|
|
of (F) and (F,F) is C).
|
|
|
|
|
|
|
|
The vulnerability results from being able to send a block with such a
|
|
|
|
transaction list, with the same merkle root, and the same block hash as
|
|
|
|
the original without duplication, resulting in failed validation. If the
|
|
|
|
receiving node proceeds to mark that block as permanently invalid
|
|
|
|
however, it will fail to accept further unmodified (and thus potentially
|
|
|
|
valid) versions of the same block. We defend against this by detecting
|
|
|
|
the case where we would hash two identical hashes at the end of the list
|
|
|
|
together, and treating that identically to the block having an invalid
|
|
|
|
merkle root. Assuming no double-SHA256 collisions, this will detect all
|
|
|
|
known ways of changing the transactions without affecting the merkle
|
|
|
|
root.
|
|
|
|
*/
|
2013-06-25 03:03:03 +02:00
|
|
|
vMerkleTree.clear();
|
2014-09-16 00:30:05 +02:00
|
|
|
vMerkleTree.reserve(vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes.
|
2014-10-10 06:42:03 +02:00
|
|
|
for (std::vector<CTransaction>::const_iterator it(vtx.begin()); it != vtx.end(); ++it)
|
|
|
|
vMerkleTree.push_back(it->GetHash());
|
2013-06-25 03:03:03 +02:00
|
|
|
int j = 0;
|
2014-09-16 00:30:05 +02:00
|
|
|
bool mutated = false;
|
2013-06-25 03:03:03 +02:00
|
|
|
for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < nSize; i += 2)
|
|
|
|
{
|
|
|
|
int i2 = std::min(i+1, nSize-1);
|
2014-09-16 00:30:05 +02:00
|
|
|
if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) {
|
|
|
|
// Two identical hashes at the end of the list at a particular level.
|
|
|
|
mutated = true;
|
|
|
|
}
|
2013-06-25 03:03:03 +02:00
|
|
|
vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]),
|
|
|
|
BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2])));
|
|
|
|
}
|
|
|
|
j += nSize;
|
|
|
|
}
|
2014-09-16 00:30:05 +02:00
|
|
|
if (fMutated) {
|
|
|
|
*fMutated = mutated;
|
|
|
|
}
|
2014-12-15 09:11:16 +01:00
|
|
|
return (vMerkleTree.empty() ? uint256() : vMerkleTree.back());
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint256> CBlock::GetMerkleBranch(int nIndex) const
|
|
|
|
{
|
|
|
|
if (vMerkleTree.empty())
|
|
|
|
BuildMerkleTree();
|
|
|
|
std::vector<uint256> vMerkleBranch;
|
|
|
|
int j = 0;
|
|
|
|
for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
|
|
|
|
{
|
|
|
|
int i = std::min(nIndex^1, nSize-1);
|
|
|
|
vMerkleBranch.push_back(vMerkleTree[j+i]);
|
|
|
|
nIndex >>= 1;
|
|
|
|
j += nSize;
|
|
|
|
}
|
|
|
|
return vMerkleBranch;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 CBlock::CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
|
|
|
|
{
|
|
|
|
if (nIndex == -1)
|
2014-12-15 09:11:16 +01:00
|
|
|
return uint256();
|
2014-10-10 06:42:03 +02:00
|
|
|
for (std::vector<uint256>::const_iterator it(vMerkleBranch.begin()); it != vMerkleBranch.end(); ++it)
|
2013-06-25 03:03:03 +02:00
|
|
|
{
|
|
|
|
if (nIndex & 1)
|
2014-10-10 06:42:03 +02:00
|
|
|
hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
|
2013-06-25 03:03:03 +02:00
|
|
|
else
|
2014-10-10 06:42:03 +02:00
|
|
|
hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
|
2013-06-25 03:03:03 +02:00
|
|
|
nIndex >>= 1;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2014-08-20 10:26:27 +02:00
|
|
|
std::string CBlock::ToString() const
|
2013-06-25 03:03:03 +02:00
|
|
|
{
|
2014-08-20 10:26:27 +02:00
|
|
|
std::stringstream s;
|
|
|
|
s << strprintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
|
2014-01-16 16:15:27 +01:00
|
|
|
GetHash().ToString(),
|
2013-06-25 03:03:03 +02:00
|
|
|
nVersion,
|
2014-01-16 16:15:27 +01:00
|
|
|
hashPrevBlock.ToString(),
|
|
|
|
hashMerkleRoot.ToString(),
|
2013-06-25 03:03:03 +02:00
|
|
|
nTime, nBits, nNonce,
|
|
|
|
vtx.size());
|
|
|
|
for (unsigned int i = 0; i < vtx.size(); i++)
|
|
|
|
{
|
2014-08-20 10:26:27 +02:00
|
|
|
s << " " << vtx[i].ToString() << "\n";
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
2014-08-20 10:26:27 +02:00
|
|
|
s << " vMerkleTree: ";
|
2013-06-25 03:03:03 +02:00
|
|
|
for (unsigned int i = 0; i < vMerkleTree.size(); i++)
|
2014-08-20 10:26:27 +02:00
|
|
|
s << " " << vMerkleTree[i].ToString();
|
|
|
|
s << "\n";
|
|
|
|
return s.str();
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|