MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / BlockBuildMerkleTree

Function BlockBuildMerkleTree

src/test/merkle_tests.cpp:28–53  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

26
27// Older version of the merkle root computation code, for comparison.
28static 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.
56static std::vector<uint256> BlockGetMerkleBranch(const CBlock& block, const std::vector<uint256>& vMerkleTree, int nIndex)

Callers 1

BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 11

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
backMethod · 0.45

Tested by

no test coverage detected