Skip to content

Commit

Permalink
Merge pull request #466 from evoskuil/master
Browse files Browse the repository at this point in the history
Implement get_unassociated_count_above(size_t, size_t).
  • Loading branch information
evoskuil authored May 11, 2024
2 parents 7febb19 + 8c43463 commit ceab5c4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/bitcoin/database/impl/query/initialize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,17 @@ size_t CLASS::get_unassociated_count() const NOEXCEPT

TEMPLATE
size_t CLASS::get_unassociated_count_above(size_t height) const NOEXCEPT
{
return get_unassociated_count_above(height, max_size_t);
}

TEMPLATE
size_t CLASS::get_unassociated_count_above(size_t height,
size_t maximum) const NOEXCEPT
{
size_t count{};
const auto top = get_top_candidate();
while (++height <= top)
while (++height <= top && count < maximum)
if (!is_associated(to_candidate(height)))
++count;

Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class query
size_t count, size_t last) const NOEXCEPT;
size_t get_unassociated_count() const NOEXCEPT;
size_t get_unassociated_count_above(size_t height) const NOEXCEPT;
size_t get_unassociated_count_above(size_t height,
size_t last) const NOEXCEPT;
hashes get_candidate_hashes(const heights& heights) const NOEXCEPT;
hashes get_confirmed_hashes(const heights& heights) const NOEXCEPT;

Expand Down
14 changes: 14 additions & 0 deletions test/query/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,20 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_unassociated_count_above__gapped_cand
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(2), 1u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(3), 0u);

BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(0, 0), 0u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(0, 1), 1u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(0, 2), 2u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(0, 3), 2u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(1, 0), 0u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(1, 1), 1u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(1, 2), 2u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(1, 3), 2u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(2, 0), 0u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(2, 1), 1u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(2, 2), 1u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(3, 0), 0u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(3, 1), 0u);

// There is one unassociated block at block 2.
BOOST_REQUIRE(query.set(test::block3)); // associated
BOOST_REQUIRE_EQUAL(query.get_unassociated_count(), 1u);
Expand Down

0 comments on commit ceab5c4

Please sign in to comment.