Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

merkleblock.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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  const static std::set<int> allowedTxTypes = {
30  };
31 
32  for (unsigned int i = 0; i < block.vtx.size(); i++)
33  {
34  const auto& tx = *block.vtx[i];
35  const uint256& hash = tx.GetHash();
36  bool isAllowedType = tx.nVersion != 3 || allowedTxTypes.count(tx.nType) != 0;
37 
38  if (txids && txids->count(hash)) {
39  vMatch.push_back(true);
40  } else if (isAllowedType && filter && filter->IsRelevantAndUpdate(*block.vtx[i])) {
41  vMatch.push_back(true);
42  vMatchedTxn.emplace_back(i, hash);
43  } else {
44  vMatch.push_back(false);
45  }
46  vHashes.push_back(hash);
47  }
48 
49  txn = CPartialMerkleTree(vHashes, vMatch);
50 }
51 
52 uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid) {
53  //we can never have zero txs in a merkle block, we always need the coinbase tx
54  //if we do not have this assert, we can hit a memory access violation when indexing into vTxid
55  assert(vTxid.size() != 0);
56  if (height == 0) {
57  // hash at height 0 is the txids themself
58  return vTxid[pos];
59  } else {
60  // calculate left hash
61  uint256 left = CalcHash(height-1, pos*2, vTxid), right;
62  // calculate right hash if not beyond the end of the array - copy left hash otherwise
63  if (pos*2+1 < CalcTreeWidth(height-1))
64  right = CalcHash(height-1, pos*2+1, vTxid);
65  else
66  right = left;
67  // combine subhashes
68  return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
69  }
70 }
71 
72 void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) {
73  // determine whether this node is the parent of at least one matched txid
74  bool fParentOfMatch = false;
75  for (unsigned int p = pos << height; p < (pos+1) << height && p < nTransactions; p++)
76  fParentOfMatch |= vMatch[p];
77  // store as flag bit
78  vBits.push_back(fParentOfMatch);
79  if (height==0 || !fParentOfMatch) {
80  // if at height 0, or nothing interesting below, store hash and stop
81  vHash.push_back(CalcHash(height, pos, vTxid));
82  } else {
83  // otherwise, don't store any hash, but descend into the subtrees
84  TraverseAndBuild(height-1, pos*2, vTxid, vMatch);
85  if (pos*2+1 < CalcTreeWidth(height-1))
86  TraverseAndBuild(height-1, pos*2+1, vTxid, vMatch);
87  }
88 }
89 
90 uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex) {
91  if (nBitsUsed >= vBits.size()) {
92  // overflowed the bits array - failure
93  fBad = true;
94  return uint256();
95  }
96  bool fParentOfMatch = vBits[nBitsUsed++];
97  if (height==0 || !fParentOfMatch) {
98  // if at height 0, or nothing interesting below, use stored hash and do not descend
99  if (nHashUsed >= vHash.size()) {
100  // overflowed the hash array - failure
101  fBad = true;
102  return uint256();
103  }
104  const uint256 &hash = vHash[nHashUsed++];
105  if (height==0 && fParentOfMatch) { // in case of height 0, we have a matched txid
106  vMatch.push_back(hash);
107  vnIndex.push_back(pos);
108  }
109  return hash;
110  } else {
111  // otherwise, descend into the subtrees to extract matched txids and hashes
112  uint256 left = TraverseAndExtract(height-1, pos*2, nBitsUsed, nHashUsed, vMatch, vnIndex), right;
113  if (pos*2+1 < CalcTreeWidth(height-1)) {
114  right = TraverseAndExtract(height-1, pos*2+1, nBitsUsed, nHashUsed, vMatch, vnIndex);
115  if (right == left) {
116  // The left and right branches should never be identical, as the transaction
117  // hashes covered by them must each be unique.
118  fBad = true;
119  }
120  } else {
121  right = left;
122  }
123  // and combine them before returning
124  return Hash(BEGIN(left), END(left), BEGIN(right), END(right));
125  }
126 }
127 
128 CPartialMerkleTree::CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) : nTransactions(vTxid.size()), fBad(false) {
129  // reset state
130  vBits.clear();
131  vHash.clear();
132 
133  // calculate height of tree
134  int nHeight = 0;
135  while (CalcTreeWidth(nHeight) > 1)
136  nHeight++;
137 
138  // traverse the partial tree
139  TraverseAndBuild(nHeight, 0, vTxid, vMatch);
140 }
141 
142 CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}
143 
144 uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex) {
145  vMatch.clear();
146  // An empty set will not work
147  if (nTransactions == 0)
148  return uint256();
149  // check for excessively high numbers of transactions
150  if (nTransactions > MaxBlockSize(true) / 60) // 60 is the lower bound for the size of a serialized CTransaction
151  return uint256();
152  // there can never be more hashes provided than one for every txid
153  if (vHash.size() > nTransactions)
154  return uint256();
155  // there must be at least one bit per node in the partial tree, and at least one node per hash
156  if (vBits.size() < vHash.size())
157  return uint256();
158  // calculate height of tree
159  int nHeight = 0;
160  while (CalcTreeWidth(nHeight) > 1)
161  nHeight++;
162  // traverse the partial tree
163  unsigned int nBitsUsed = 0, nHashUsed = 0;
164  uint256 hashMerkleRoot = TraverseAndExtract(nHeight, 0, nBitsUsed, nHashUsed, vMatch, vnIndex);
165  // verify that no problems occurred during the tree traversal
166  if (fBad)
167  return uint256();
168  // verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
169  if ((nBitsUsed+7)/8 != (vBits.size()+7)/8)
170  return uint256();
171  // verify that all hashes were consumed
172  if (nHashUsed != vHash.size())
173  return uint256();
174  return hashMerkleRoot;
175 }
CBlockHeader header
Public only for unit testing.
Definition: merkleblock.h:131
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:72
CBlockHeader GetBlockHeader() const
Definition: block.h:107
unsigned int nTransactions
the total number of transactions in the block
Definition: merkleblock.h:54
Definition: block.h:72
unsigned int MaxBlockSize(bool fDIP0001Active)
Definition: consensus.h:12
bool fBad
flag set when encountering invalid data
Definition: merkleblock.h:63
bool IsRelevantAndUpdate(const CTransaction &tx)
Also adds any outputs which match the filter to the filter (to match their spending txes) ...
Definition: bloom.cpp:233
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:46
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
false true true true
Definition: bls_dkg.cpp:176
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:50
#define BEGIN(a)
Utilities for converting data from/to strings.
false
Definition: bls_dkg.cpp:168
std::vector< uint256 > vHash
txids and internal hashes
Definition: merkleblock.h:60
#define END(a)
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:84
CPartialMerkleTree txn
Definition: merkleblock.h:132
std::vector< bool > vBits
node-is-parent-of-matched-txid bits
Definition: merkleblock.h:57
256-bit opaque blob.
Definition: uint256.h:123
std::vector< CTransactionRef > vtx
Definition: block.h:76
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:90
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition: merkleblock.h:140
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:52
Released under the MIT license