MCPcopy Create free account
hub / github.com/ElementsProject/elements / BlockBuildMerkleTree

Function BlockBuildMerkleTree

src/test/merkle_tests.cpp:130–155  ·  view source on GitHub ↗

Older version of the merkle root computation code, for comparison.

Source from the content-addressed store, hash-verified

128
129// Older version of the merkle root computation code, for comparison.
130static uint256 BlockBuildMerkleTree(const CBlock& block, bool* fMutated, std::vector<uint256>& vMerkleTree)
131{
132 vMerkleTree.clear();
133 vMerkleTree.reserve(block.vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes.
134 for (std::vector<CTransactionRef>::const_iterator it(block.vtx.begin()); it != block.vtx.end(); ++it)
135 vMerkleTree.push_back((*it)->GetHash());
136 int j = 0;
137 bool mutated = false;
138 for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
139 {
140 for (int i = 0; i < nSize; i += 2)
141 {
142 int i2 = std::min(i+1, nSize-1);
143 if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) {
144 // Two identical hashes at the end of the list at a particular level.
145 mutated = true;
146 }
147 vMerkleTree.push_back(Hash(vMerkleTree[j+i], vMerkleTree[j+i2]));
148 }
149 j += nSize;
150 }
151 if (fMutated) {
152 *fMutated = mutated;
153 }
154 return (vMerkleTree.empty() ? uint256() : vMerkleTree.back());
155}
156
157// Older version of the merkle branch computation code, for comparison.
158static std::vector<uint256> BlockGetMerkleBranch(const CBlock& block, const std::vector<uint256>& vMerkleTree, int nIndex)

Callers 1

BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 10

HashFunction · 0.50
uint256Class · 0.50
clearMethod · 0.45
reserveMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
push_backMethod · 0.45
GetHashMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected