Changeset View
Changeset View
Standalone View
Standalone View
src/test/merkle_tests.cpp
Show First 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | while (count < leaves.size()) { | ||||
if (matchh) { | if (matchh) { | ||||
pbranch->push_back(inner[level]); | pbranch->push_back(inner[level]); | ||||
} else if (matchlevel == level) { | } else if (matchlevel == level) { | ||||
pbranch->push_back(h); | pbranch->push_back(h); | ||||
matchh = true; | matchh = true; | ||||
} | } | ||||
} | } | ||||
mutated |= (inner[level] == h); | mutated |= (inner[level] == h); | ||||
CHash256() | CHash256().Write(inner[level]).Write(h).Finalize(h.begin()); | ||||
.Write(inner[level].begin(), 32) | |||||
.Write(h.begin(), 32) | |||||
.Finalize(h.begin()); | |||||
} | } | ||||
// Store the resulting hash at inner position level. | // Store the resulting hash at inner position level. | ||||
inner[level] = h; | inner[level] = h; | ||||
if (matchh) { | if (matchh) { | ||||
matchlevel = level; | matchlevel = level; | ||||
} | } | ||||
} | } | ||||
// Do a final 'sweep' over the rightmost branch of the tree to process | // Do a final 'sweep' over the rightmost branch of the tree to process | ||||
Show All 9 Lines | static void MerkleComputation(const std::vector<uint256> &leaves, | ||||
bool matchh = matchlevel == level; | bool matchh = matchlevel == level; | ||||
while (count != (((uint32_t)1) << level)) { | while (count != (((uint32_t)1) << level)) { | ||||
// If we reach this point, h is an inner value that is not the top. | // If we reach this point, h is an inner value that is not the top. | ||||
// We combine it with itself (Bitcoin's special rule for odd levels in | // We combine it with itself (Bitcoin's special rule for odd levels in | ||||
// the tree) to produce a higher level one. | // the tree) to produce a higher level one. | ||||
if (pbranch && matchh) { | if (pbranch && matchh) { | ||||
pbranch->push_back(h); | pbranch->push_back(h); | ||||
} | } | ||||
CHash256() | CHash256().Write(h).Write(h).Finalize(h.begin()); | ||||
.Write(h.begin(), 32) | |||||
.Write(h.begin(), 32) | |||||
.Finalize(h.begin()); | |||||
// Increment count to the value it would have if two entries at this | // Increment count to the value it would have if two entries at this | ||||
// level had existed. | // level had existed. | ||||
count += (((uint32_t)1) << level); | count += (((uint32_t)1) << level); | ||||
level++; | level++; | ||||
// And propagate the result upwards accordingly. | // And propagate the result upwards accordingly. | ||||
while (!(count & (((uint32_t)1) << level))) { | while (!(count & (((uint32_t)1) << level))) { | ||||
if (pbranch) { | if (pbranch) { | ||||
if (matchh) { | if (matchh) { | ||||
pbranch->push_back(inner[level]); | pbranch->push_back(inner[level]); | ||||
} else if (matchlevel == level) { | } else if (matchlevel == level) { | ||||
pbranch->push_back(h); | pbranch->push_back(h); | ||||
matchh = true; | matchh = true; | ||||
} | } | ||||
} | } | ||||
CHash256() | CHash256().Write(inner[level]).Write(h).Finalize(h.begin()); | ||||
.Write(inner[level].begin(), 32) | |||||
.Write(h.begin(), 32) | |||||
.Finalize(h.begin()); | |||||
level++; | level++; | ||||
} | } | ||||
} | } | ||||
// Return result. | // Return result. | ||||
if (pmutated) { | if (pmutated) { | ||||
*pmutated = mutated; | *pmutated = mutated; | ||||
} | } | ||||
if (proot) { | if (proot) { | ||||
▲ Show 20 Lines • Show All 247 Lines • Show Last 20 Lines |