Skip to content

Commit 3c2b779

Browse files
committed
Split out merkle_branch(size_t position, hashes&& leaves).
1 parent d54d56e commit 3c2b779

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

include/bitcoin/system/chain/block.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class BC_API block
4343
typedef std::shared_ptr<const block> cptr;
4444

4545
static bool is_malleable64(const transaction_cptrs& txs) NOEXCEPT;
46+
static hashes merkle_branch(size_t position, hashes&& leaves) NOEXCEPT;
4647
static uint64_t subsidy(size_t height, uint64_t subsidy_interval,
4748
uint64_t initial_block_subsidy_satoshi, bool bip42) NOEXCEPT;
4849

src/chain/block.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,24 +444,29 @@ bool block::is_invalid_merkle_root() const NOEXCEPT
444444
return generate_merkle_root(false) != header_->merkle_root();
445445
}
446446

447-
hashes block::merkle_branch(size_t position, bool witness) const NOEXCEPT
447+
// static
448+
hashes block::merkle_branch(size_t position, hashes&& leaves) NOEXCEPT
448449
{
449-
auto level = transaction_hashes(witness);
450-
if (position >= level.size())
450+
if (position >= leaves.size())
451451
return {};
452452

453453
hashes branch{};
454-
branch.reserve(ceilinged_log2(level.size()));
455-
for (auto at = position; level.size() > one; at = to_half(at))
454+
branch.reserve(ceilinged_log2(leaves.size()));
455+
for (auto at = position; leaves.size() > one; at = to_half(at))
456456
{
457-
if (is_odd(level.size())) level.push_back(level.back());
458-
branch.push_back(level.at(is_even(at) ? add1(at) : sub1(at)));
459-
sha256::merkle_hash(level);
457+
if (is_odd(leaves.size())) leaves.push_back(leaves.back());
458+
branch.push_back(leaves.at(is_even(at) ? add1(at) : sub1(at)));
459+
sha256::merkle_hash(leaves);
460460
}
461461

462462
return branch;
463463
}
464464

465+
hashes block::merkle_branch(size_t position, bool witness) const NOEXCEPT
466+
{
467+
return merkle_branch(position, transaction_hashes(witness));
468+
}
469+
465470
// Accept (contextual).
466471
// ----------------------------------------------------------------------------
467472

0 commit comments

Comments
 (0)