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