From 0d7f963fba5551860080d5f5e3fea88ca332f1be Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sun, 15 Feb 2026 17:05:28 -0500 Subject: [PATCH 1/4] Use const. --- include/bitcoin/database/impl/query/optional.ipp | 8 ++++---- include/bitcoin/database/query.hpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/bitcoin/database/impl/query/optional.ipp b/include/bitcoin/database/impl/query/optional.ipp index 6d53dcd1..0f5947fe 100644 --- a/include/bitcoin/database/impl/query/optional.ipp +++ b/include/bitcoin/database/impl/query/optional.ipp @@ -318,7 +318,7 @@ CLASS::hash_option CLASS::get_confirmed_interval(size_t height) const NOEXCEPT // protected TEMPLATE -void CLASS::push_merkle(hashes& to, hashes&& from, size_t first) NOEXCEPT +void CLASS::push_merkle(hashes& to, hashes&& from, size_t first) const NOEXCEPT { using namespace system; for (const auto& row: block::merkle_branch(first, from.size())) @@ -333,7 +333,7 @@ void CLASS::push_merkle(hashes& to, hashes&& from, size_t first) NOEXCEPT // protected TEMPLATE code CLASS::get_merkle_proof(hashes& proof, hashes roots, size_t target, - size_t waypoint) NOEXCEPT + size_t waypoint) const NOEXCEPT { const auto span = interval_span(); BC_ASSERT(!is_zero(span)); @@ -353,7 +353,7 @@ code CLASS::get_merkle_proof(hashes& proof, hashes roots, size_t target, // protected TEMPLATE -code CLASS::get_merkle_tree(hashes& tree, size_t waypoint) NOEXCEPT +code CLASS::get_merkle_tree(hashes& tree, size_t waypoint) const NOEXCEPT { const auto span = interval_span(); BC_ASSERT(!is_zero(span)); @@ -384,7 +384,7 @@ code CLASS::get_merkle_tree(hashes& tree, size_t waypoint) NOEXCEPT TEMPLATE code CLASS::get_merkle_root_and_proof(hash_digest& root, hashes& proof, - size_t target, size_t waypoint) NOEXCEPT + size_t target, size_t waypoint) const NOEXCEPT { if (target > waypoint) return error::merkle_arguments; diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 39a24229..4332bf82 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -573,7 +573,7 @@ class query uint64_t& balance, const hash_digest& key, bool turbo=false) const NOEXCEPT; code get_merkle_root_and_proof(hash_digest& root, hashes& proof, - size_t target, size_t checkpoint) NOEXCEPT; + size_t target, size_t checkpoint) const NOEXCEPT; bool is_filtered_body(const header_link& link) const NOEXCEPT; bool get_filter_body(filter& out, const header_link& link) const NOEXCEPT; @@ -721,10 +721,10 @@ class query size_t interval_span() const NOEXCEPT; hash_option get_confirmed_interval(size_t height) const NOEXCEPT; hash_option create_interval(header_link link, size_t height) const NOEXCEPT; - void push_merkle(hashes& branch, hashes&& hashes, size_t first) NOEXCEPT; - code get_merkle_tree(hashes& roots, size_t waypoint) NOEXCEPT; + void push_merkle(hashes& branch, hashes&& hashes, size_t first) const NOEXCEPT; + code get_merkle_tree(hashes& roots, size_t waypoint) const NOEXCEPT; code get_merkle_proof(hashes& proof, hashes roots, size_t target, - size_t waypoint) NOEXCEPT; + size_t waypoint) const NOEXCEPT; /// tx_fk must be allocated. /// ----------------------------------------------------------------------- From 610d6cf179d565a9dbaeed5b6d3464aef95fe4a0 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sun, 15 Feb 2026 17:05:46 -0500 Subject: [PATCH 2/4] Fix get_confirmed_interval (at vs. get). --- include/bitcoin/database/impl/query/optional.ipp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bitcoin/database/impl/query/optional.ipp b/include/bitcoin/database/impl/query/optional.ipp index 0f5947fe..3f1426a5 100644 --- a/include/bitcoin/database/impl/query/optional.ipp +++ b/include/bitcoin/database/impl/query/optional.ipp @@ -310,7 +310,7 @@ CLASS::hash_option CLASS::get_confirmed_interval(size_t height) const NOEXCEPT return {}; table::txs::get_interval txs{}; - if (!store_.txs.get(to_confirmed(height), txs)) + if (!store_.txs.at(to_confirmed(height), txs)) return {}; return txs.interval; From 7d6f608e6392eeda306082394a3fb9eed5ae0c81 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sun, 15 Feb 2026 17:05:59 -0500 Subject: [PATCH 3/4] Add get_confirmed_headers(). --- .../bitcoin/database/impl/query/height.ipp | 36 +++++++++++++++++++ include/bitcoin/database/query.hpp | 2 ++ 2 files changed, 38 insertions(+) diff --git a/include/bitcoin/database/impl/query/height.ipp b/include/bitcoin/database/impl/query/height.ipp index b5a81a48..5c486d43 100644 --- a/include/bitcoin/database/impl/query/height.ipp +++ b/include/bitcoin/database/impl/query/height.ipp @@ -236,6 +236,42 @@ hashes CLASS::get_confirmed_hashes(size_t first, size_t count) const NOEXCEPT return out; } +TEMPLATE +header_links CLASS::get_confirmed_headers(size_t first, + size_t count) const NOEXCEPT +{ + // Empty is always a successful/valid result for this method. + if (is_zero(count)) + return {}; + + // First requested height is currently above top. + const auto top = get_top_confirmed(); + if (first > top) + return {}; + + // add1(top) cannot overflow, as indexed block count cannot exceed size_t. + count = system::limit(count, add1(top) - first); + auto last = first + sub1(count); + + // Due to reorganization it is possible for this height to now be terminal. + auto link = to_confirmed(last); + + // Walk link back to first indexed header (for reorg safety). + while (link.is_terminal() && last > first) + link = to_confirmed(--last); + + // No headers are currently confirmed at/above first. + if (link.is_terminal()) + return {}; + + // Compiler should optimize out last to_parent() call. + header_links out(add1(last - first)); + for (auto& value: std::views::reverse(out)) + link = to_parent(value = link); + + return out; +} + // writers // ---------------------------------------------------------------------------- diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 4332bf82..577922bd 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -538,6 +538,8 @@ class query hashes get_candidate_hashes(const heights& heights) const NOEXCEPT; hashes get_confirmed_hashes(const heights& heights) const NOEXCEPT; hashes get_confirmed_hashes(size_t first, size_t count) const NOEXCEPT; + header_links get_confirmed_headers(size_t first, + size_t count) const NOEXCEPT; header_links get_confirmed_fork(const header_link& fork) const NOEXCEPT; header_links get_candidate_fork(size_t& fork_point) const NOEXCEPT; From e48c7c718832e93642cb8714ada7603f9a7547dd Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sun, 15 Feb 2026 17:21:31 -0500 Subject: [PATCH 4/4] Deconflict similar/misleading method signatures. --- include/bitcoin/database/impl/query/height.ipp | 10 +++++----- include/bitcoin/database/query.hpp | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/bitcoin/database/impl/query/height.ipp b/include/bitcoin/database/impl/query/height.ipp index 5c486d43..1835d8a8 100644 --- a/include/bitcoin/database/impl/query/height.ipp +++ b/include/bitcoin/database/impl/query/height.ipp @@ -238,10 +238,10 @@ hashes CLASS::get_confirmed_hashes(size_t first, size_t count) const NOEXCEPT TEMPLATE header_links CLASS::get_confirmed_headers(size_t first, - size_t count) const NOEXCEPT + size_t maximum) const NOEXCEPT { // Empty is always a successful/valid result for this method. - if (is_zero(count)) + if (is_zero(maximum)) return {}; // First requested height is currently above top. @@ -249,9 +249,9 @@ header_links CLASS::get_confirmed_headers(size_t first, if (first > top) return {}; - // add1(top) cannot overflow, as indexed block count cannot exceed size_t. - count = system::limit(count, add1(top) - first); - auto last = first + sub1(count); + // add1(top) cannot overflow, as indexed block maximum cannot exceed size_t. + maximum = system::limit(maximum, add1(top) - first); + auto last = first + sub1(maximum); // Due to reorganization it is possible for this height to now be terminal. auto link = to_confirmed(last); diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 577922bd..68d0d8d7 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -539,7 +539,7 @@ class query hashes get_confirmed_hashes(const heights& heights) const NOEXCEPT; hashes get_confirmed_hashes(size_t first, size_t count) const NOEXCEPT; header_links get_confirmed_headers(size_t first, - size_t count) const NOEXCEPT; + size_t maximum) const NOEXCEPT; header_links get_confirmed_fork(const header_link& fork) const NOEXCEPT; header_links get_candidate_fork(size_t& fork_point) const NOEXCEPT;