Skip to content

Commit 39d3b53

Browse files
committed
Rename merkle branch to path
1 parent b042c4f commit 39d3b53

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

src/consensus/merkle.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated)
8484
}
8585

8686
/* This implements a constant-space merkle root/path calculator, limited to 2^32 leaves. */
87-
static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot, bool* pmutated, uint32_t branchpos, std::vector<uint256>* pbranch) {
88-
if (pbranch) pbranch->clear();
87+
static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot, bool* pmutated, uint32_t leaf_pos, std::vector<uint256>* path)
88+
{
89+
if (path) path->clear();
8990
if (leaves.size() == 0) {
9091
if (pmutated) *pmutated = false;
9192
if (proot) *proot = uint256();
@@ -105,18 +106,18 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
105106
// First process all leaves into 'inner' values.
106107
while (count < leaves.size()) {
107108
uint256 h = leaves[count];
108-
bool matchh = count == branchpos;
109+
bool matchh = count == leaf_pos;
109110
count++;
110111
int level;
111112
// For each of the lower bits in count that are 0, do 1 step. Each
112113
// corresponds to an inner value that existed before processing the
113114
// current leaf, and each needs a hash to combine it.
114115
for (level = 0; !(count & ((uint32_t{1}) << level)); level++) {
115-
if (pbranch) {
116+
if (path) {
116117
if (matchh) {
117-
pbranch->push_back(inner[level]);
118+
path->push_back(inner[level]);
118119
} else if (matchlevel == level) {
119-
pbranch->push_back(h);
120+
path->push_back(h);
120121
matchh = true;
121122
}
122123
}
@@ -144,8 +145,8 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
144145
// If we reach this point, h is an inner value that is not the top.
145146
// We combine it with itself (Bitcoin's special rule for odd levels in
146147
// the tree) to produce a higher level one.
147-
if (pbranch && matchh) {
148-
pbranch->push_back(h);
148+
if (path && matchh) {
149+
path->push_back(h);
149150
}
150151
h = Hash(h, h);
151152
// Increment count to the value it would have if two entries at this
@@ -154,11 +155,11 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
154155
level++;
155156
// And propagate the result upwards accordingly.
156157
while (!(count & ((uint32_t{1}) << level))) {
157-
if (pbranch) {
158+
if (path) {
158159
if (matchh) {
159-
pbranch->push_back(inner[level]);
160+
path->push_back(inner[level]);
160161
} else if (matchlevel == level) {
161-
pbranch->push_back(h);
162+
path->push_back(h);
162163
matchh = true;
163164
}
164165
}
@@ -171,18 +172,18 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
171172
if (proot) *proot = h;
172173
}
173174

174-
static std::vector<uint256> ComputeMerkleBranch(const std::vector<uint256>& leaves, uint32_t position) {
175+
static std::vector<uint256> ComputeMerklePath(const std::vector<uint256>& leaves, uint32_t position) {
175176
std::vector<uint256> ret;
176177
MerkleComputation(leaves, nullptr, nullptr, position, &ret);
177178
return ret;
178179
}
179180

180-
std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position)
181+
std::vector<uint256> TransactionMerklePath(const CBlock& block, uint32_t position)
181182
{
182183
std::vector<uint256> leaves;
183184
leaves.resize(block.vtx.size());
184185
for (size_t s = 0; s < block.vtx.size(); s++) {
185186
leaves[s] = block.vtx[s]->GetHash();
186187
}
187-
return ComputeMerkleBranch(leaves, position);
188+
return ComputeMerklePath(leaves, position);
188189
}

src/consensus/merkle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated = nullptr);
3232
*
3333
* @return merkle path ordered from the deepest
3434
*/
35-
std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position = 0);
35+
std::vector<uint256> TransactionMerklePath(const CBlock& block, uint32_t position = 0);
3636

3737
#endif // BITCOIN_CONSENSUS_MERKLE_H

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ class BlockTemplateImpl : public BlockTemplate
913913

914914
std::vector<uint256> getCoinbaseMerklePath() override
915915
{
916-
return BlockMerkleBranch(m_block_template->block);
916+
return TransactionMerklePath(m_block_template->block);
917917
}
918918

919919
bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CMutableTransaction coinbase) override

src/test/merkle_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(merkle_test)
135135
if (ntx > 16) {
136136
mtx = m_rng.randrange(ntx);
137137
}
138-
std::vector<uint256> newBranch = BlockMerkleBranch(block, mtx);
138+
std::vector<uint256> newBranch = TransactionMerklePath(block, mtx);
139139
std::vector<uint256> oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx);
140140
BOOST_CHECK(oldBranch == newBranch);
141141
BOOST_CHECK(ComputeMerkleRootFromBranch(block.vtx[mtx]->GetHash(), newBranch, mtx) == oldRoot);

0 commit comments

Comments
 (0)