Changeset View
Changeset View
Standalone View
Standalone View
src/test/merkle_tests.cpp
Show All 12 Lines | |||||
// Older version of the merkle root computation code, for comparison. | // Older version of the merkle root computation code, for comparison. | ||||
static uint256 BlockBuildMerkleTree(const CBlock &block, bool *fMutated, | static uint256 BlockBuildMerkleTree(const CBlock &block, bool *fMutated, | ||||
std::vector<uint256> &vMerkleTree) { | std::vector<uint256> &vMerkleTree) { | ||||
vMerkleTree.clear(); | vMerkleTree.clear(); | ||||
// Safe upper bound for the number of total nodes. | // Safe upper bound for the number of total nodes. | ||||
vMerkleTree.reserve(block.vtx.size() * 2 + 16); | vMerkleTree.reserve(block.vtx.size() * 2 + 16); | ||||
for (std::vector<CTransactionRef>::const_iterator it(block.vtx.begin()); | for (std::vector<CTransactionRef>::const_iterator it(block.vtx.begin()); | ||||
it != block.vtx.end(); ++it) | it != block.vtx.end(); ++it) | ||||
vMerkleTree.push_back((*it)->GetId()); | vMerkleTree.push_back((*it)->GetHash()); | ||||
int j = 0; | int j = 0; | ||||
bool mutated = false; | bool mutated = false; | ||||
for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) { | for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) { | ||||
for (int i = 0; i < nSize; i += 2) { | for (int i = 0; i < nSize; i += 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 | ||||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | for (int i = 0; i < 32; i++) { | ||||
mtx = insecure_rand() % ntx; | mtx = insecure_rand() % ntx; | ||||
} | } | ||||
std::vector<uint256> newBranch = | std::vector<uint256> newBranch = | ||||
BlockMerkleBranch(block, mtx); | BlockMerkleBranch(block, mtx); | ||||
std::vector<uint256> oldBranch = | std::vector<uint256> oldBranch = | ||||
BlockGetMerkleBranch(block, merkleTree, mtx); | BlockGetMerkleBranch(block, merkleTree, mtx); | ||||
BOOST_CHECK(oldBranch == newBranch); | BOOST_CHECK(oldBranch == newBranch); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
ComputeMerkleRootFromBranch(block.vtx[mtx]->GetId(), | ComputeMerkleRootFromBranch(block.vtx[mtx]->GetHash(), | ||||
newBranch, mtx) == oldRoot); | newBranch, mtx) == oldRoot); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |