Skip to content

Commit 39f5758

Browse files
authored
Merge pull request #650 from evoskuil/master
Adapt to system min block version, add get_block/get_header from locator.
2 parents fbbef6d + ede30b2 commit 39f5758

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef LIBBITCOIN_DATABASE_QUERY_HEIGHT_IPP
2020
#define LIBBITCOIN_DATABASE_QUERY_HEIGHT_IPP
2121

22+
#include <algorithm>
2223
#include <bitcoin/system.hpp>
2324
#include <bitcoin/database/define.hpp>
2425

@@ -59,6 +60,98 @@ size_t CLASS::get_confirmed_size(size_t top) const NOEXCEPT
5960
return wire;
6061
}
6162

63+
// locator readers
64+
// ----------------------------------------------------------------------------
65+
// These do not require strict consistency.
66+
67+
TEMPLATE
68+
CLASS::headers CLASS::get_headers(const hashes& locator,
69+
const hash_digest& stop, size_t limit) const NOEXCEPT
70+
{
71+
headers out{};
72+
const auto span = get_locator_span(locator, stop, limit);
73+
out.reserve(span.size());
74+
75+
for (auto height = span.begin; height < span.end; ++height)
76+
{
77+
// Terminal implies intervening reorganization.
78+
const auto link = to_confirmed(height);
79+
if (link.is_terminal())
80+
return {};
81+
82+
out.push_back(get_header(link));
83+
BC_ASSERT(!is_null(out.back()));
84+
}
85+
86+
return out;
87+
}
88+
89+
TEMPLATE
90+
hashes CLASS::get_blocks(const hashes& locator,
91+
const hash_digest& stop, size_t limit) const NOEXCEPT
92+
{
93+
hashes out{};
94+
const auto span = get_locator_span(locator, stop, limit);
95+
out.reserve(span.size());
96+
97+
for (auto height = span.begin; height < span.end; ++height)
98+
{
99+
// Terminal implies intervening reorganization.
100+
const auto link = to_confirmed(height);
101+
if (link.is_terminal())
102+
return {};
103+
104+
out.push_back(get_header_key(link));
105+
BC_ASSERT(out.back() != system::null_hash);
106+
}
107+
108+
return out;
109+
}
110+
111+
TEMPLATE
112+
CLASS::span CLASS::get_locator_span(const hashes& locator,
113+
const hash_digest& stop, size_t limit) const NOEXCEPT
114+
{
115+
using namespace system;
116+
span out{};
117+
118+
// Start at fork point, stop at given header (both excluded).
119+
const auto start = add1(get_fork(locator));
120+
const auto last1 = (stop == null_hash) ? max_uint32 :
121+
get_height(to_header(stop)).value;
122+
123+
// Determine number of headers requested, limited by max allowed.
124+
const auto request = floored_subtract<size_t>(last1, start);
125+
const auto allowed = std::min(request, limit);
126+
127+
// Set end to (start + allowed), limited by (top + 1).
128+
const auto top1 = ceilinged_add(get_top_confirmed(), one);
129+
const auto end = std::min(ceilinged_add(start, allowed), top1);
130+
131+
// Convert negative range to empty.
132+
out.end = std::max(start, end);
133+
return out;
134+
}
135+
136+
// protected
137+
TEMPLATE
138+
size_t CLASS::get_fork(const hashes& locator) const NOEXCEPT
139+
{
140+
// Locator is presumed (by convention) to be in order by height.
141+
for (const auto& hash: locator)
142+
{
143+
const auto link = to_header(hash);
144+
const auto height = get_height(link);
145+
146+
table::height::record confirmed{};
147+
if (store_.confirmed.get(height, confirmed) &&
148+
confirmed.header_fk == link)
149+
return height;
150+
}
151+
152+
return zero;
153+
}
154+
62155
// shared_lock readers
63156
// ----------------------------------------------------------------------------
64157
// Protected against index pop (low contention) to ensure branch consistency.

include/bitcoin/database/query.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class query
6767
using script = system::chain::script;
6868
using output = system::chain::output;
6969
using header = system::chain::header;
70+
using headers = system::chain::header_cptrs;
7071
using transaction = system::chain::transaction;
7172
using transactions = system::chain::transaction_cptrs;
7273
using inputs_ptr = system::chain::inputs_ptr;
@@ -534,6 +535,12 @@ class query
534535
bool pop_candidate() NOEXCEPT;
535536
bool pop_confirmed() NOEXCEPT;
536537

538+
/// Populate message payloads from locator.
539+
headers get_headers(const hashes& locator, const hash_digest& stop,
540+
size_t limit) const NOEXCEPT;
541+
hashes get_blocks(const hashes& locator, const hash_digest& stop,
542+
size_t limit) const NOEXCEPT;
543+
537544
/// Optional Tables.
538545
/// -----------------------------------------------------------------------
539546

@@ -557,6 +564,23 @@ class query
557564
const hash_digest& head) NOEXCEPT;
558565

559566
protected:
567+
struct span
568+
{
569+
size_t size() const NOEXCEPT { return end - begin; }
570+
size_t begin;
571+
size_t end;
572+
};
573+
574+
/// Network
575+
/// -----------------------------------------------------------------------
576+
577+
/// Height of highest confirmed block (assumes locator descending).
578+
size_t get_fork(const hashes& locator) const NOEXCEPT;
579+
580+
/// Height of highest confirmed block (assumes locator descending).
581+
span get_locator_span(const hashes& locator, const hash_digest& stop,
582+
size_t limit) const NOEXCEPT;
583+
560584
/// Translate.
561585
/// -----------------------------------------------------------------------
562586
uint32_t to_input_index(const tx_link& parent_fk,

test/query/context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ BOOST_AUTO_TEST_CASE(query_context__get_candidate_chain_state__genesis__expected
5151
test::genesis.header().timestamp(),
5252
0u,
5353
0u,
54-
1u,
54+
0u,
5555
0u
5656
};
5757

@@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(query_context__get_candidate_chain_state__block1__expected)
8383
test::block1.header().timestamp(), // timestamp
8484
test::genesis.header().timestamp(), // mtp
8585
1u, // height
86-
1u, // minimum_block_version
86+
0u, // minimum_block_version
8787
486604799u // work_required
8888
};
8989

0 commit comments

Comments
 (0)