MCPcopy Create free account
hub / github.com/ElementsProject/elements / ExtractMatches

Method ExtractMatches

src/merkleblock.cpp:149–180  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

147CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}
148
149uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex) {
150 vMatch.clear();
151 // An empty set will not work
152 if (nTransactions == 0)
153 return uint256();
154 // check for excessively high numbers of transactions
155 if (nTransactions > MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT)
156 return uint256();
157 // there can never be more hashes provided than one for every txid
158 if (vHash.size() > nTransactions)
159 return uint256();
160 // there must be at least one bit per node in the partial tree, and at least one node per hash
161 if (vBits.size() < vHash.size())
162 return uint256();
163 // calculate height of tree
164 int nHeight = 0;
165 while (CalcTreeWidth(nHeight) > 1)
166 nHeight++;
167 // traverse the partial tree
168 unsigned int nBitsUsed = 0, nHashUsed = 0;
169 uint256 hashMerkleRoot = TraverseAndExtract(nHeight, 0, nBitsUsed, nHashUsed, vMatch, vnIndex);
170 // verify that no problems occurred during the tree traversal
171 if (fBad)
172 return uint256();
173 // verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
174 if ((nBitsUsed+7)/8 != (vBits.size()+7)/8)
175 return uint256();
176 // verify that all hashes were consumed
177 if (nHashUsed != vHash.size())
178 return uint256();
179 return hashMerkleRoot;
180}

Callers 1

Calls 3

uint256Class · 0.70
clearMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected