BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
merkleblock.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 
6 #include <merkleblock.h>
7 
8 #include <hash.h>
9 #include <consensus/consensus.h>
10 #include <utilstrencodings.h>
11 
12 
13 CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids)
14 {
15  header = block.GetBlockHeader();
16 
17  std::vector<bool> vMatch;
18  std::vector<uint256> vHashes;
19 
20  vMatch.reserve(block.vtx.size());
21  vHashes.reserve(block.vtx.size());
22 
23  for (unsigned int i = 0; i < block.vtx.size(); i++)
24  {
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);
31  } else {
32  vMatch.push_back(false);
33  }
34  vHashes.push_back(hash);
35  }
36 
37  txn = CPartialMerkleTree(vHashes, vMatch);
38 }
39 
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);
44  if (height == 0) {
45  // hash at height 0 is the txids themself
46  return vTxid[pos];
47  } else {
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);
53  else
54  right = left;
55  // combine subhashes
56  return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
57  }
58 }
59 
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];
65  // store as flag bit
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));
70  } else {
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);
75  }
76 }
77 
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
81  fBad = true;
82  return uint256();
83  }
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
89  fBad = true;
90  return uint256();
91  }
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);
96  }
97  return hash;
98  } else {
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);
103  if (right == left) {
104  // The left and right branches should never be identical, as the transaction
105  // hashes covered by them must each be unique.
106  fBad = true;
107  }
108  } else {
109  right = left;
110  }
111  // and combine them before returning
112  return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
113  }
114 }
115 
116 CPartialMerkleTree::CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) : nTransactions(vTxid.size()), fBad(false) {
117  // reset state
118  vBits.clear();
119  vHash.clear();
120 
121  // calculate height of tree
122  int nHeight = 0;
123  while (CalcTreeWidth(nHeight) > 1)
124  nHeight++;
125 
126  // traverse the partial tree
127  TraverseAndBuild(nHeight, 0, vTxid, vMatch);
128 }
129 
130 CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}
131 
132 uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex) {
133  vMatch.clear();
134  // An empty set will not work
135  if (nTransactions == 0)
136  return uint256();
137  // check for excessively high numbers of transactions
138  if (nTransactions > MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT)
139  return uint256();
140  // there can never be more hashes provided than one for every txid
141  if (vHash.size() > nTransactions)
142  return uint256();
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())
145  return uint256();
146  // calculate height of tree
147  int nHeight = 0;
148  while (CalcTreeWidth(nHeight) > 1)
149  nHeight++;
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
154  if (fBad)
155  return uint256();
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)
158  return uint256();
159  // verify that all hashes were consumed
160  if (nHashUsed != vHash.size())
161  return uint256();
162  return hashMerkleRoot;
163 }
CBlockHeader header
Public only for unit testing.
Definition: merkleblock.h:137
uint256 ExtractMatches(std::vector< uint256 > &vMatch, std::vector< unsigned int > &vnIndex)
extract the matching txid&#39;s represented by this partial merkle tree and their respective indices with...
void TraverseAndBuild(int height, unsigned int pos, const std::vector< uint256 > &vTxid, const std::vector< bool > &vMatch)
recursive function that traverses tree nodes, storing the data as bits and hashes ...
Definition: merkleblock.cpp:60
CBlockHeader GetBlockHeader() const
Definition: block.h:109
unsigned int nTransactions
the total number of transactions in the block
Definition: merkleblock.h:54
Definition: block.h:74
bool fBad
flag set when encountering invalid data
Definition: merkleblock.h:63
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:44
unsigned int CalcTreeWidth(int height) const
helper function to efficiently calculate the number of nodes at given height in the merkle tree ...
Definition: merkleblock.h:66
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:50
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:119
#define BEGIN(a)
Utilities for converting data from/to strings.
isminefilter filter
Definition: rpcwallet.cpp:1011
std::vector< uint256 > vHash
txids and internal hashes
Definition: merkleblock.h:60
#define END(a)
CPartialMerkleTree txn
Definition: merkleblock.h:138
std::vector< bool > vBits
node-is-parent-of-matched-txid bits
Definition: merkleblock.h:57
256-bit opaque blob.
Definition: uint256.h:122
std::vector< CTransactionRef > vtx
Definition: block.h:78
uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector< uint256 > &vMatch, std::vector< unsigned int > &vnIndex)
recursive function that traverses tree nodes, consuming the bits and hashes produced by TraverseAndBu...
Definition: merkleblock.cpp:78
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition: merkleblock.h:146
uint256 CalcHash(int height, unsigned int pos, const std::vector< uint256 > &vTxid)
calculate the hash of a node in the merkle tree (at leaf level: the txid&#39;s themselves) ...
Definition: merkleblock.cpp:40