1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 <consensus/consensus.h>
10 #include <utilstrencodings.h>
13 CMerkleBlock::CMerkleBlock(const CBlock
& block
, CBloomFilter
* filter
, const std::set
<uint256
>* txids
)
15 header
= block
.GetBlockHeader();
17 std::vector
<bool> vMatch
;
18 std::vector
<uint256
> vHashes
;
20 vMatch
.reserve(block
.vtx
.size());
21 vHashes
.reserve(block
.vtx
.size());
23 for (unsigned int i
= 0; i
< block
.vtx
.size(); i
++)
25 const uint256
& hash
= block
.vtx
[i
]->GetHash();
26 if (txids
&& txids
->count(hash
)) {
27 vMatch
.push_back(true);
28 } else if (filter
&& filter
->IsRelevantAndUpdate(*block
.vtx
[i
])) {
29 vMatch
.push_back(true);
30 vMatchedTxn
.emplace_back(i
, hash
);
32 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
) {
41 //we can never have zero txs in a merkle block, we always need the coinbase tx
42 //if we do not have this assert, we can hit a memory access violation when indexing into vTxid
43 assert(vTxid
.size() != 0);
45 // hash at height 0 is the txids themself
48 // calculate left hash
49 uint256 left
= CalcHash(height
-1, pos
*2, vTxid
), right
;
50 // calculate right hash if not beyond the end of the array - copy left hash otherwise
51 if (pos
*2+1 < CalcTreeWidth(height
-1))
52 right
= CalcHash(height
-1, pos
*2+1, vTxid
);
56 return Hash(BEGIN(left
), END(left
), BEGIN(right
), END(right
));
60 void CPartialMerkleTree::TraverseAndBuild(int height
, unsigned int pos
, const std::vector
<uint256
> &vTxid
, const std::vector
<bool> &vMatch
) {
61 // determine whether this node is the parent of at least one matched txid
62 bool fParentOfMatch
= false;
63 for (unsigned int p
= pos
<< height
; p
< (pos
+1) << height
&& p
< nTransactions
; p
++)
64 fParentOfMatch
|= vMatch
[p
];
66 vBits
.push_back(fParentOfMatch
);
67 if (height
==0 || !fParentOfMatch
) {
68 // if at height 0, or nothing interesting below, store hash and stop
69 vHash
.push_back(CalcHash(height
, pos
, vTxid
));
71 // otherwise, don't store any hash, but descend into the subtrees
72 TraverseAndBuild(height
-1, pos
*2, vTxid
, vMatch
);
73 if (pos
*2+1 < CalcTreeWidth(height
-1))
74 TraverseAndBuild(height
-1, pos
*2+1, vTxid
, vMatch
);
78 uint256
CPartialMerkleTree::TraverseAndExtract(int height
, unsigned int pos
, unsigned int &nBitsUsed
, unsigned int &nHashUsed
, std::vector
<uint256
> &vMatch
, std::vector
<unsigned int> &vnIndex
) {
79 if (nBitsUsed
>= vBits
.size()) {
80 // overflowed the bits array - failure
84 bool fParentOfMatch
= vBits
[nBitsUsed
++];
85 if (height
==0 || !fParentOfMatch
) {
86 // if at height 0, or nothing interesting below, use stored hash and do not descend
87 if (nHashUsed
>= vHash
.size()) {
88 // overflowed the hash array - failure
92 const uint256
&hash
= vHash
[nHashUsed
++];
93 if (height
==0 && fParentOfMatch
) { // in case of height 0, we have a matched txid
94 vMatch
.push_back(hash
);
95 vnIndex
.push_back(pos
);
99 // otherwise, descend into the subtrees to extract matched txids and hashes
100 uint256 left
= TraverseAndExtract(height
-1, pos
*2, nBitsUsed
, nHashUsed
, vMatch
, vnIndex
), right
;
101 if (pos
*2+1 < CalcTreeWidth(height
-1)) {
102 right
= TraverseAndExtract(height
-1, pos
*2+1, nBitsUsed
, nHashUsed
, vMatch
, vnIndex
);
104 // The left and right branches should never be identical, as the transaction
105 // hashes covered by them must each be unique.
111 // and combine them before returning
112 return Hash(BEGIN(left
), END(left
), BEGIN(right
), END(right
));
116 CPartialMerkleTree::CPartialMerkleTree(const std::vector
<uint256
> &vTxid
, const std::vector
<bool> &vMatch
) : nTransactions(vTxid
.size()), fBad(false) {
121 // calculate height of tree
123 while (CalcTreeWidth(nHeight
) > 1)
126 // traverse the partial tree
127 TraverseAndBuild(nHeight
, 0, vTxid
, vMatch
);
130 CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}
132 uint256
CPartialMerkleTree::ExtractMatches(std::vector
<uint256
> &vMatch
, std::vector
<unsigned int> &vnIndex
) {
134 // An empty set will not work
135 if (nTransactions
== 0)
137 // check for excessively high numbers of transactions
138 if (nTransactions
> MAX_BLOCK_WEIGHT
/ MIN_TRANSACTION_WEIGHT
)
140 // there can never be more hashes provided than one for every txid
141 if (vHash
.size() > nTransactions
)
143 // there must be at least one bit per node in the partial tree, and at least one node per hash
144 if (vBits
.size() < vHash
.size())
146 // calculate height of tree
148 while (CalcTreeWidth(nHeight
) > 1)
150 // traverse the partial tree
151 unsigned int nBitsUsed
= 0, nHashUsed
= 0;
152 uint256 hashMerkleRoot
= TraverseAndExtract(nHeight
, 0, nBitsUsed
, nHashUsed
, vMatch
, vnIndex
);
153 // verify that no problems occurred during the tree traversal
156 // verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
157 if ((nBitsUsed
+7)/8 != (vBits
.size()+7)/8)
159 // verify that all hashes were consumed
160 if (nHashUsed
!= vHash
.size())
162 return hashMerkleRoot
;