1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
6 #include "merkleblock.h"
9 #include "primitives/block.h" // for MAX_BLOCK_SIZE
10 #include "utilstrencodings.h"
14 CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter)
16 header = block.GetBlockHeader();
19 vector<uint256> vHashes;
21 vMatch.reserve(block.vtx.size());
22 vHashes.reserve(block.vtx.size());
24 for (unsigned int i = 0; i < block.vtx.size(); i++)
26 const uint256& hash = block.vtx[i].GetHash();
27 if (filter.IsRelevantAndUpdate(block.vtx[i]))
29 vMatch.push_back(true);
30 vMatchedTxn.push_back(make_pair(i, hash));
33 vMatch.push_back(false);
34 vHashes.push_back(hash);
37 txn = CPartialMerkleTree(vHashes, vMatch);
40 uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid) {
42 // hash at height 0 is the txids themself
45 // calculate left hash
46 uint256 left = CalcHash(height-1, pos*2, vTxid), right;
47 // calculate right hash if not beyond the end of the array - copy left hash otherwise1
48 if (pos*2+1 < CalcTreeWidth(height-1))
49 right = CalcHash(height-1, pos*2+1, vTxid);
53 return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
57 void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) {
58 // determine whether this node is the parent of at least one matched txid
59 bool fParentOfMatch = false;
60 for (unsigned int p = pos << height; p < (pos+1) << height && p < nTransactions; p++)
61 fParentOfMatch |= vMatch[p];
63 vBits.push_back(fParentOfMatch);
64 if (height==0 || !fParentOfMatch) {
65 // if at height 0, or nothing interesting below, store hash and stop
66 vHash.push_back(CalcHash(height, pos, vTxid));
68 // otherwise, don't store any hash, but descend into the subtrees
69 TraverseAndBuild(height-1, pos*2, vTxid, vMatch);
70 if (pos*2+1 < CalcTreeWidth(height-1))
71 TraverseAndBuild(height-1, pos*2+1, vTxid, vMatch);
75 uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch) {
76 if (nBitsUsed >= vBits.size()) {
77 // overflowed the bits array - failure
81 bool fParentOfMatch = vBits[nBitsUsed++];
82 if (height==0 || !fParentOfMatch) {
83 // if at height 0, or nothing interesting below, use stored hash and do not descend
84 if (nHashUsed >= vHash.size()) {
85 // overflowed the hash array - failure
89 const uint256 &hash = vHash[nHashUsed++];
90 if (height==0 && fParentOfMatch) // in case of height 0, we have a matched txid
91 vMatch.push_back(hash);
94 // otherwise, descend into the subtrees to extract matched txids and hashes
95 uint256 left = TraverseAndExtract(height-1, pos*2, nBitsUsed, nHashUsed, vMatch), right;
96 if (pos*2+1 < CalcTreeWidth(height-1)) {
97 right = TraverseAndExtract(height-1, pos*2+1, nBitsUsed, nHashUsed, vMatch);
99 // If the left and right branch should never be identical as the transaction
100 // hashes covered by them must be unique.
106 // and combine them before returning
107 return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
111 CPartialMerkleTree::CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) : nTransactions(vTxid.size()), fBad(false) {
116 // calculate height of tree
118 while (CalcTreeWidth(nHeight) > 1)
121 // traverse the partial tree
122 TraverseAndBuild(nHeight, 0, vTxid, vMatch);
125 CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}
127 uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch) {
129 // An empty set will not work
130 if (nTransactions == 0)
132 // check for excessively high numbers of transactions
133 if (nTransactions > MAX_BLOCK_SIZE / 60) // 60 is the lower bound for the size of a serialized CTransaction
135 // there can never be more hashes provided than one for every txid
136 if (vHash.size() > nTransactions)
138 // there must be at least one bit per node in the partial tree, and at least one node per hash
139 if (vBits.size() < vHash.size())
141 // calculate height of tree
143 while (CalcTreeWidth(nHeight) > 1)
145 // traverse the partial tree
146 unsigned int nBitsUsed = 0, nHashUsed = 0;
147 uint256 hashMerkleRoot = TraverseAndExtract(nHeight, 0, nBitsUsed, nHashUsed, vMatch);
148 // verify that no problems occured during the tree traversal
151 // verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
152 if ((nBitsUsed+7)/8 != (vBits.size()+7)/8)
154 // verify that all hashes were consumed
155 if (nHashUsed != vHash.size())
157 return hashMerkleRoot;