Older version of the merkle root computation code, for comparison.
| 26 | |
| 27 | // Older version of the merkle root computation code, for comparison. |
| 28 | static uint256 BlockBuildMerkleTree(const CBlock& block, bool* fMutated, std::vector<uint256>& vMerkleTree) |
| 29 | { |
| 30 | vMerkleTree.clear(); |
| 31 | vMerkleTree.reserve(block.vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes. |
| 32 | for (std::vector<CTransactionRef>::const_iterator it(block.vtx.begin()); it != block.vtx.end(); ++it) |
| 33 | vMerkleTree.push_back((*it)->GetHash().ToUint256()); |
| 34 | int j = 0; |
| 35 | bool mutated = false; |
| 36 | for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) |
| 37 | { |
| 38 | for (int i = 0; i < nSize; i += 2) |
| 39 | { |
| 40 | int i2 = std::min(i+1, nSize-1); |
| 41 | if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) { |
| 42 | // Two identical hashes at the end of the list at a particular level. |
| 43 | mutated = true; |
| 44 | } |
| 45 | vMerkleTree.push_back(Hash(vMerkleTree[j+i], vMerkleTree[j+i2])); |
| 46 | } |
| 47 | j += nSize; |
| 48 | } |
| 49 | if (fMutated) { |
| 50 | *fMutated = mutated; |
| 51 | } |
| 52 | return (vMerkleTree.empty() ? uint256() : vMerkleTree.back()); |
| 53 | } |
| 54 | |
| 55 | // Older version of the merkle branch computation code, for comparison. |
| 56 | static std::vector<uint256> BlockGetMerkleBranch(const CBlock& block, const std::vector<uint256>& vMerkleTree, int nIndex) |