Rename merkle branch to path

This commit is contained in:
Sjors Provoost 2024-10-31 10:56:16 -07:00
parent b042c4f053
commit 39d3b538e6
No known key found for this signature in database
GPG key ID: 57FF9BDBCC301009
4 changed files with 18 additions and 17 deletions

View file

@ -84,8 +84,9 @@ uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated)
}
/* This implements a constant-space merkle root/path calculator, limited to 2^32 leaves. */
static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot, bool* pmutated, uint32_t branchpos, std::vector<uint256>* pbranch) {
if (pbranch) pbranch->clear();
static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot, bool* pmutated, uint32_t leaf_pos, std::vector<uint256>* path)
{
if (path) path->clear();
if (leaves.size() == 0) {
if (pmutated) *pmutated = false;
if (proot) *proot = uint256();
@ -105,18 +106,18 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
// First process all leaves into 'inner' values.
while (count < leaves.size()) {
uint256 h = leaves[count];
bool matchh = count == branchpos;
bool matchh = count == leaf_pos;
count++;
int level;
// For each of the lower bits in count that are 0, do 1 step. Each
// corresponds to an inner value that existed before processing the
// current leaf, and each needs a hash to combine it.
for (level = 0; !(count & ((uint32_t{1}) << level)); level++) {
if (pbranch) {
if (path) {
if (matchh) {
pbranch->push_back(inner[level]);
path->push_back(inner[level]);
} else if (matchlevel == level) {
pbranch->push_back(h);
path->push_back(h);
matchh = true;
}
}
@ -144,8 +145,8 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
// 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
// the tree) to produce a higher level one.
if (pbranch && matchh) {
pbranch->push_back(h);
if (path && matchh) {
path->push_back(h);
}
h = Hash(h, h);
// 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
level++;
// And propagate the result upwards accordingly.
while (!(count & ((uint32_t{1}) << level))) {
if (pbranch) {
if (path) {
if (matchh) {
pbranch->push_back(inner[level]);
path->push_back(inner[level]);
} else if (matchlevel == level) {
pbranch->push_back(h);
path->push_back(h);
matchh = true;
}
}
@ -171,18 +172,18 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
if (proot) *proot = h;
}
static std::vector<uint256> ComputeMerkleBranch(const std::vector<uint256>& leaves, uint32_t position) {
static std::vector<uint256> ComputeMerklePath(const std::vector<uint256>& leaves, uint32_t position) {
std::vector<uint256> ret;
MerkleComputation(leaves, nullptr, nullptr, position, &ret);
return ret;
}
std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position)
std::vector<uint256> TransactionMerklePath(const CBlock& block, uint32_t position)
{
std::vector<uint256> leaves;
leaves.resize(block.vtx.size());
for (size_t s = 0; s < block.vtx.size(); s++) {
leaves[s] = block.vtx[s]->GetHash();
}
return ComputeMerkleBranch(leaves, position);
return ComputeMerklePath(leaves, position);
}

View file

@ -32,6 +32,6 @@ uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated = nullptr);
*
* @return merkle path ordered from the deepest
*/
std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position = 0);
std::vector<uint256> TransactionMerklePath(const CBlock& block, uint32_t position = 0);
#endif // BITCOIN_CONSENSUS_MERKLE_H

View file

@ -913,7 +913,7 @@ public:
std::vector<uint256> getCoinbaseMerklePath() override
{
return BlockMerkleBranch(m_block_template->block);
return TransactionMerklePath(m_block_template->block);
}
bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CMutableTransaction coinbase) override

View file

@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(merkle_test)
if (ntx > 16) {
mtx = m_rng.randrange(ntx);
}
std::vector<uint256> newBranch = BlockMerkleBranch(block, mtx);
std::vector<uint256> newBranch = TransactionMerklePath(block, mtx);
std::vector<uint256> oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx);
BOOST_CHECK(oldBranch == newBranch);
BOOST_CHECK(ComputeMerkleRootFromBranch(block.vtx[mtx]->GetHash(), newBranch, mtx) == oldRoot);