@@ -23,110 +23,6 @@ static uint256 ComputeMerkleRootFromBranch(const uint256& leaf, const std::vecto
23
23
return hash;
24
24
}
25
25
26
- /* This implements a constant-space merkle root/path calculator, limited to 2^32 leaves. */
27
- static void MerkleComputation (const std::vector<uint256>& leaves, uint256* proot, bool * pmutated, uint32_t branchpos, std::vector<uint256>* pbranch) {
28
- if (pbranch) pbranch->clear ();
29
- if (leaves.size () == 0 ) {
30
- if (pmutated) *pmutated = false ;
31
- if (proot) *proot = uint256 ();
32
- return ;
33
- }
34
- bool mutated = false ;
35
- // count is the number of leaves processed so far.
36
- uint32_t count = 0 ;
37
- // inner is an array of eagerly computed subtree hashes, indexed by tree
38
- // level (0 being the leaves).
39
- // For example, when count is 25 (11001 in binary), inner[4] is the hash of
40
- // the first 16 leaves, inner[3] of the next 8 leaves, and inner[0] equal to
41
- // the last leaf. The other inner entries are undefined.
42
- uint256 inner[32 ];
43
- // Which position in inner is a hash that depends on the matching leaf.
44
- int matchlevel = -1 ;
45
- // First process all leaves into 'inner' values.
46
- while (count < leaves.size ()) {
47
- uint256 h = leaves[count];
48
- bool matchh = count == branchpos;
49
- count++;
50
- int level;
51
- // For each of the lower bits in count that are 0, do 1 step. Each
52
- // corresponds to an inner value that existed before processing the
53
- // current leaf, and each needs a hash to combine it.
54
- for (level = 0 ; !(count & ((uint32_t {1 }) << level)); level++) {
55
- if (pbranch) {
56
- if (matchh) {
57
- pbranch->push_back (inner[level]);
58
- } else if (matchlevel == level) {
59
- pbranch->push_back (h);
60
- matchh = true ;
61
- }
62
- }
63
- mutated |= (inner[level] == h);
64
- h = Hash (inner[level], h);
65
- }
66
- // Store the resulting hash at inner position level.
67
- inner[level] = h;
68
- if (matchh) {
69
- matchlevel = level;
70
- }
71
- }
72
- // Do a final 'sweep' over the rightmost branch of the tree to process
73
- // odd levels, and reduce everything to a single top value.
74
- // Level is the level (counted from the bottom) up to which we've sweeped.
75
- int level = 0 ;
76
- // As long as bit number level in count is zero, skip it. It means there
77
- // is nothing left at this level.
78
- while (!(count & ((uint32_t {1 }) << level))) {
79
- level++;
80
- }
81
- uint256 h = inner[level];
82
- bool matchh = matchlevel == level;
83
- while (count != ((uint32_t {1 }) << level)) {
84
- // If we reach this point, h is an inner value that is not the top.
85
- // We combine it with itself (Bitcoin's special rule for odd levels in
86
- // the tree) to produce a higher level one.
87
- if (pbranch && matchh) {
88
- pbranch->push_back (h);
89
- }
90
- h = Hash (h, h);
91
- // Increment count to the value it would have if two entries at this
92
- // level had existed.
93
- count += ((uint32_t {1 }) << level);
94
- level++;
95
- // And propagate the result upwards accordingly.
96
- while (!(count & ((uint32_t {1 }) << level))) {
97
- if (pbranch) {
98
- if (matchh) {
99
- pbranch->push_back (inner[level]);
100
- } else if (matchlevel == level) {
101
- pbranch->push_back (h);
102
- matchh = true ;
103
- }
104
- }
105
- h = Hash (inner[level], h);
106
- level++;
107
- }
108
- }
109
- // Return result.
110
- if (pmutated) *pmutated = mutated;
111
- if (proot) *proot = h;
112
- }
113
-
114
- static std::vector<uint256> ComputeMerkleBranch (const std::vector<uint256>& leaves, uint32_t position) {
115
- std::vector<uint256> ret;
116
- MerkleComputation (leaves, nullptr , nullptr , position, &ret);
117
- return ret;
118
- }
119
-
120
- static std::vector<uint256> BlockMerkleBranch (const CBlock& block, uint32_t position)
121
- {
122
- std::vector<uint256> leaves;
123
- leaves.resize (block.vtx .size ());
124
- for (size_t s = 0 ; s < block.vtx .size (); s++) {
125
- leaves[s] = block.vtx [s]->GetHash ();
126
- }
127
- return ComputeMerkleBranch (leaves, position);
128
- }
129
-
130
26
// Older version of the merkle root computation code, for comparison.
131
27
static uint256 BlockBuildMerkleTree (const CBlock& block, bool * fMutated , std::vector<uint256>& vMerkleTree)
132
28
{
0 commit comments