Skip to content

Commit bcfc180

Browse files
authored
Merge pull request #712 from evoskuil/master
Add get_confirmed_headers().
2 parents 9182685 + e48c7c7 commit bcfc180

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

include/bitcoin/database/impl/query/height.ipp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,42 @@ hashes CLASS::get_confirmed_hashes(size_t first, size_t count) const NOEXCEPT
236236
return out;
237237
}
238238

239+
TEMPLATE
240+
header_links CLASS::get_confirmed_headers(size_t first,
241+
size_t maximum) const NOEXCEPT
242+
{
243+
// Empty is always a successful/valid result for this method.
244+
if (is_zero(maximum))
245+
return {};
246+
247+
// First requested height is currently above top.
248+
const auto top = get_top_confirmed();
249+
if (first > top)
250+
return {};
251+
252+
// add1(top) cannot overflow, as indexed block maximum cannot exceed size_t.
253+
maximum = system::limit(maximum, add1(top) - first);
254+
auto last = first + sub1(maximum);
255+
256+
// Due to reorganization it is possible for this height to now be terminal.
257+
auto link = to_confirmed(last);
258+
259+
// Walk link back to first indexed header (for reorg safety).
260+
while (link.is_terminal() && last > first)
261+
link = to_confirmed(--last);
262+
263+
// No headers are currently confirmed at/above first.
264+
if (link.is_terminal())
265+
return {};
266+
267+
// Compiler should optimize out last to_parent() call.
268+
header_links out(add1(last - first));
269+
for (auto& value: std::views::reverse(out))
270+
link = to_parent(value = link);
271+
272+
return out;
273+
}
274+
239275
// writers
240276
// ----------------------------------------------------------------------------
241277

include/bitcoin/database/impl/query/optional.ipp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,15 @@ CLASS::hash_option CLASS::get_confirmed_interval(size_t height) const NOEXCEPT
310310
return {};
311311

312312
table::txs::get_interval txs{};
313-
if (!store_.txs.get(to_confirmed(height), txs))
313+
if (!store_.txs.at(to_confirmed(height), txs))
314314
return {};
315315

316316
return txs.interval;
317317
}
318318

319319
// protected
320320
TEMPLATE
321-
void CLASS::push_merkle(hashes& to, hashes&& from, size_t first) NOEXCEPT
321+
void CLASS::push_merkle(hashes& to, hashes&& from, size_t first) const NOEXCEPT
322322
{
323323
using namespace system;
324324
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
333333
// protected
334334
TEMPLATE
335335
code CLASS::get_merkle_proof(hashes& proof, hashes roots, size_t target,
336-
size_t waypoint) NOEXCEPT
336+
size_t waypoint) const NOEXCEPT
337337
{
338338
const auto span = interval_span();
339339
BC_ASSERT(!is_zero(span));
@@ -353,7 +353,7 @@ code CLASS::get_merkle_proof(hashes& proof, hashes roots, size_t target,
353353

354354
// protected
355355
TEMPLATE
356-
code CLASS::get_merkle_tree(hashes& tree, size_t waypoint) NOEXCEPT
356+
code CLASS::get_merkle_tree(hashes& tree, size_t waypoint) const NOEXCEPT
357357
{
358358
const auto span = interval_span();
359359
BC_ASSERT(!is_zero(span));
@@ -384,7 +384,7 @@ code CLASS::get_merkle_tree(hashes& tree, size_t waypoint) NOEXCEPT
384384

385385
TEMPLATE
386386
code CLASS::get_merkle_root_and_proof(hash_digest& root, hashes& proof,
387-
size_t target, size_t waypoint) NOEXCEPT
387+
size_t target, size_t waypoint) const NOEXCEPT
388388
{
389389
if (target > waypoint)
390390
return error::merkle_arguments;

include/bitcoin/database/query.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ class query
538538
hashes get_candidate_hashes(const heights& heights) const NOEXCEPT;
539539
hashes get_confirmed_hashes(const heights& heights) const NOEXCEPT;
540540
hashes get_confirmed_hashes(size_t first, size_t count) const NOEXCEPT;
541+
header_links get_confirmed_headers(size_t first,
542+
size_t maximum) const NOEXCEPT;
541543

542544
header_links get_confirmed_fork(const header_link& fork) const NOEXCEPT;
543545
header_links get_candidate_fork(size_t& fork_point) const NOEXCEPT;
@@ -573,7 +575,7 @@ class query
573575
uint64_t& balance, const hash_digest& key,
574576
bool turbo=false) const NOEXCEPT;
575577
code get_merkle_root_and_proof(hash_digest& root, hashes& proof,
576-
size_t target, size_t checkpoint) NOEXCEPT;
578+
size_t target, size_t checkpoint) const NOEXCEPT;
577579

578580
bool is_filtered_body(const header_link& link) const NOEXCEPT;
579581
bool get_filter_body(filter& out, const header_link& link) const NOEXCEPT;
@@ -721,10 +723,10 @@ class query
721723
size_t interval_span() const NOEXCEPT;
722724
hash_option get_confirmed_interval(size_t height) const NOEXCEPT;
723725
hash_option create_interval(header_link link, size_t height) const NOEXCEPT;
724-
void push_merkle(hashes& branch, hashes&& hashes, size_t first) NOEXCEPT;
725-
code get_merkle_tree(hashes& roots, size_t waypoint) NOEXCEPT;
726+
void push_merkle(hashes& branch, hashes&& hashes, size_t first) const NOEXCEPT;
727+
code get_merkle_tree(hashes& roots, size_t waypoint) const NOEXCEPT;
726728
code get_merkle_proof(hashes& proof, hashes roots, size_t target,
727-
size_t waypoint) NOEXCEPT;
729+
size_t waypoint) const NOEXCEPT;
728730

729731
/// tx_fk must be allocated.
730732
/// -----------------------------------------------------------------------

0 commit comments

Comments
 (0)