Changeset View
Changeset View
Standalone View
Standalone View
src/test/merkle_tests.cpp
Show All 14 Lines | |||||
static uint256 | static uint256 | ||||
ComputeMerkleRootFromBranch(const uint256 &leaf, | ComputeMerkleRootFromBranch(const uint256 &leaf, | ||||
const std::vector<uint256> &vMerkleBranch, | const std::vector<uint256> &vMerkleBranch, | ||||
uint32_t nIndex) { | uint32_t nIndex) { | ||||
uint256 hash = leaf; | uint256 hash = leaf; | ||||
for (std::vector<uint256>::const_iterator it = vMerkleBranch.begin(); | for (std::vector<uint256>::const_iterator it = vMerkleBranch.begin(); | ||||
it != vMerkleBranch.end(); ++it) { | it != vMerkleBranch.end(); ++it) { | ||||
if (nIndex & 1) { | if (nIndex & 1) { | ||||
hash = Hash(it->begin(), it->end(), hash.begin(), hash.end()); | hash = Hash(*it, hash); | ||||
} else { | } else { | ||||
hash = Hash(hash.begin(), hash.end(), it->begin(), it->end()); | hash = Hash(hash, *it); | ||||
} | } | ||||
nIndex >>= 1; | nIndex >>= 1; | ||||
} | } | ||||
return hash; | return hash; | ||||
} | } | ||||
/** | /** | ||||
* This implements a constant-space merkle root/path calculator, limited to 2^32 | * This implements a constant-space merkle root/path calculator, limited to 2^32 | ||||
▲ Show 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) { | ||||
int i2 = std::min(i + 1, nSize - 1); | int i2 = std::min(i + 1, nSize - 1); | ||||
if (i2 == i + 1 && i2 + 1 == nSize && | if (i2 == i + 1 && i2 + 1 == nSize && | ||||
vMerkleTree[j + i] == vMerkleTree[j + i2]) { | vMerkleTree[j + i] == vMerkleTree[j + i2]) { | ||||
// Two identical hashes at the end of the list at a particular | // Two identical hashes at the end of the list at a particular | ||||
// level. | // level. | ||||
mutated = true; | mutated = true; | ||||
} | } | ||||
vMerkleTree.push_back( | vMerkleTree.push_back( | ||||
Hash(vMerkleTree[j + i].begin(), vMerkleTree[j + i].end(), | Hash(vMerkleTree[j + i], vMerkleTree[j + i2])); | ||||
vMerkleTree[j + i2].begin(), vMerkleTree[j + i2].end())); | |||||
} | } | ||||
j += nSize; | j += nSize; | ||||
} | } | ||||
if (fMutated) { | if (fMutated) { | ||||
*fMutated = mutated; | *fMutated = mutated; | ||||
} | } | ||||
return (vMerkleTree.empty() ? uint256() : vMerkleTree.back()); | return (vMerkleTree.empty() ? uint256() : vMerkleTree.back()); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 194 Lines • Show Last 20 Lines |