| 55 | } |
| 56 | |
| 57 | uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid) { |
| 58 | //we can never have zero txs in a merkle block, we always need the coinbase tx |
| 59 | //if we do not have this assert, we can hit a memory access violation when indexing into vTxid |
| 60 | assert(vTxid.size() != 0); |
| 61 | if (height == 0) { |
| 62 | // hash at height 0 is the txids themselves |
| 63 | return vTxid[pos]; |
| 64 | } else { |
| 65 | // calculate left hash |
| 66 | uint256 left = CalcHash(height-1, pos*2, vTxid), right; |
| 67 | // calculate right hash if not beyond the end of the array - copy left hash otherwise |
| 68 | if (pos*2+1 < CalcTreeWidth(height-1)) |
| 69 | right = CalcHash(height-1, pos*2+1, vTxid); |
| 70 | else |
| 71 | right = left; |
| 72 | // combine subhashes |
| 73 | return Hash(left, right); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) { |
| 78 | // determine whether this node is the parent of at least one matched txid |