diff --git a/include/bitcoin/database/impl/query/properties_block.ipp b/include/bitcoin/database/impl/query/properties_block.ipp index a1c88b19f..4108b1e7d 100644 --- a/include/bitcoin/database/impl/query/properties_block.ipp +++ b/include/bitcoin/database/impl/query/properties_block.ipp @@ -110,6 +110,27 @@ bool CLASS::get_work(uint256_t& work, const header_link& link) const NOEXCEPT return result; } +// TODO: optimize with stored aggregate. +TEMPLATE +bool CLASS::get_branch_work(uint256_t& work, const header_link& link) const NOEXCEPT +{ + uint256_t proof{}; + auto parent = link; + work = zero; + + while (get_work(proof, parent)) + { + work += proof; + parent = to_parent(parent); + + // Genesis parent link is terminal. + if (parent.is_terminal()) + return true; + } + + return false; +} + TEMPLATE bool CLASS::get_bits(uint32_t& bits, const header_link& link) const NOEXCEPT { @@ -248,6 +269,22 @@ size_t CLASS::get_tx_count(const header_link& link) const NOEXCEPT return txs.number; } +// TODO: optimize with stored aggregate. +TEMPLATE +size_t CLASS::get_branch_tx_count(const header_link& link) const NOEXCEPT +{ + size_t count{}; + auto parent = link; + + while (!parent.is_terminal()) + { + count += get_tx_count(parent); + parent = to_parent(parent); + } + + return count; +} + TEMPLATE tx_link CLASS::get_position_tx(const header_link& link, size_t position) const NOEXCEPT diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index ab58bbb67..6e3848cbb 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -375,6 +375,7 @@ class query hash_digest get_top_candidate_hash() const NOEXCEPT; hashes get_tx_keys(const header_link& link) const NOEXCEPT; size_t get_tx_count(const header_link& link) const NOEXCEPT; + size_t get_branch_tx_count(const header_link& link) const NOEXCEPT; inline hash_digest get_header_key(const header_link& link) const NOEXCEPT; inline hash_digest get_tx_key(const tx_link& link) const NOEXCEPT; inline ins_key get_point_key(const ins_link& link) const NOEXCEPT; @@ -570,6 +571,7 @@ class query bool get_timestamp(uint32_t& timestamp, const header_link& link) const NOEXCEPT; bool get_version(uint32_t& version, const header_link& link) const NOEXCEPT; bool get_work(uint256_t& work, const header_link& link) const NOEXCEPT; + bool get_branch_work(uint256_t& work, const header_link& link) const NOEXCEPT; bool get_bits(uint32_t& bits, const header_link& link) const NOEXCEPT; bool get_context(context& ctx, const header_link& link) const NOEXCEPT; bool get_context(chain_context& ctx,