Skip to content

Commit

Permalink
Merge pull request #415 from evoskuil/master
Browse files Browse the repository at this point in the history
Change to get_unassociated_above, add associations.top
  • Loading branch information
evoskuil authored Mar 10, 2024
2 parents ae9d934 + d209578 commit 85ce7f4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
7 changes: 7 additions & 0 deletions include/bitcoin/database/association.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ class associations
{
return find(height) != pos_end();
}

/// The context of the maximum ordered_unique index (must not be empty).
inline const system::chain::context& top() const NOEXCEPT
{
BC_ASSERT(!empty());
return get<association::pos>().rbegin()->context;
}
};

} // namespace database
Expand Down
8 changes: 5 additions & 3 deletions include/bitcoin/database/impl/query/initialize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,24 @@ size_t CLASS::get_last_associated_from(size_t height) const NOEXCEPT
TEMPLATE
associations CLASS::get_all_unassociated() const NOEXCEPT
{
return get_all_unassociated_above(get_fork());
return get_unassociated_above(get_fork(), max_size_t);
}

TEMPLATE
associations CLASS::get_all_unassociated_above(size_t height) const NOEXCEPT
associations CLASS::get_unassociated_above(size_t height,
size_t count) const NOEXCEPT
{
associations out{};
const auto top = get_top_candidate();
while (++height <= top)
while (++height <= top && !is_zero(count))
{
const auto header_fk = to_candidate(height);
if (!is_associated(header_fk))
{
table::header::get_check_context context{};
if (store_.header.get(header_fk, context))
{
--count;
out.insert(association
{
header_fk,
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class query
size_t get_last_associated() const NOEXCEPT;
size_t get_last_associated_from(size_t height) const NOEXCEPT;
associations get_all_unassociated() const NOEXCEPT;
associations get_all_unassociated_above(size_t height) const NOEXCEPT;
associations get_unassociated_above(size_t height,
size_t count=max_size_t) const NOEXCEPT;
hashes get_candidate_hashes(const heights& heights) const NOEXCEPT;
hashes get_confirmed_hashes(const heights& heights) const NOEXCEPT;

Expand Down
22 changes: 13 additions & 9 deletions test/query/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_last_associated_from__gapped_candidat
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(3), 3u);
}

// get_all_unassociated_above/get_all_unassociated
// get_unassociated_above/get_all_unassociated

BOOST_AUTO_TEST_CASE(query_initialize__get_all_unassociated_above__initialized__empty)
BOOST_AUTO_TEST_CASE(query_initialize__get_unassociated_above__initialized__empty)
{
settings settings{};
settings.path = TEST_DIRECTORY;
Expand All @@ -335,11 +335,11 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_all_unassociated_above__initialized__
BOOST_REQUIRE_EQUAL(store.create(events), error::success);
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE(query.get_all_unassociated().empty());
BOOST_REQUIRE(query.get_all_unassociated_above(0).empty());
BOOST_REQUIRE(query.get_all_unassociated_above(1).empty());
BOOST_REQUIRE(query.get_unassociated_above(0).empty());
BOOST_REQUIRE(query.get_unassociated_above(1).empty());
}

BOOST_AUTO_TEST_CASE(query_initialize__get_all_unassociated_above__gapped_candidate__expected)
BOOST_AUTO_TEST_CASE(query_initialize__get_unassociated_above__gapped_candidate__expected)
{
settings settings{};
settings.path = TEST_DIRECTORY;
Expand Down Expand Up @@ -370,7 +370,7 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_all_unassociated_above__gapped_candid
// There are two unassociated blocks above genesis (fork point).
BOOST_REQUIRE_EQUAL(query.get_all_unassociated().size(), 2u);

const auto unassociated0 = query.get_all_unassociated_above(0);
const auto unassociated0 = query.get_unassociated_above(0);
BOOST_REQUIRE(!unassociated0.empty());
BOOST_REQUIRE_EQUAL(unassociated0.size(), 2u);

Expand Down Expand Up @@ -398,8 +398,10 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_all_unassociated_above__gapped_candid
BOOST_REQUIRE_EQUAL(it3->context.median_time_past, context3.mtp);
BOOST_REQUIRE_EQUAL(it3->context.height, context3.height);

const auto unassociated1 = query.get_all_unassociated_above(1);
const auto unassociated1 = query.get_unassociated_above(1);
BOOST_REQUIRE_EQUAL(unassociated1.size(), 2u);
BOOST_REQUIRE(query.get_unassociated_above(1, 0).empty());
BOOST_REQUIRE_EQUAL(query.get_unassociated_above(1, 1).size(), 1u);

const auto it2s = unassociated1.find(context2.height);
BOOST_REQUIRE(it2s != unassociated1.pos_end());
Expand All @@ -414,8 +416,9 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_all_unassociated_above__gapped_candid
BOOST_REQUIRE_EQUAL(it3s->context.timestamp, test::block3.header().timestamp());
BOOST_REQUIRE_EQUAL(it3s->context.median_time_past, context3.mtp);
BOOST_REQUIRE_EQUAL(it3s->context.height, context3.height);
BOOST_REQUIRE_EQUAL(unassociated1.top().height, context3.height);

const auto unassociated2 = query.get_all_unassociated_above(2);
const auto unassociated2 = query.get_unassociated_above(2);
BOOST_REQUIRE_EQUAL(unassociated2.size(), 1u);

const auto it3a = unassociated2.find(test::block3.hash());
Expand All @@ -424,8 +427,9 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_all_unassociated_above__gapped_candid
BOOST_REQUIRE_EQUAL(it3a->context.timestamp, test::block3.header().timestamp());
BOOST_REQUIRE_EQUAL(it3a->context.median_time_past, context3.mtp);
BOOST_REQUIRE_EQUAL(it3a->context.height, context3.height);
BOOST_REQUIRE_EQUAL(unassociated2.top().height, context3.height);

const auto unassociated3 = query.get_all_unassociated_above(3);
const auto unassociated3 = query.get_unassociated_above(3);
BOOST_REQUIRE_EQUAL(unassociated3.size(), 0u);

// There are two unassociated blocks above block 1 (new fork point).
Expand Down

0 comments on commit 85ce7f4

Please sign in to comment.