diff --git a/include/bitcoin/database/impl/query/initialize.ipp b/include/bitcoin/database/impl/query/initialize.ipp index ec2d4859..2346bca1 100644 --- a/include/bitcoin/database/impl/query/initialize.ipp +++ b/include/bitcoin/database/impl/query/initialize.ipp @@ -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; diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 8afca07f..9fbb21b1 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -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; diff --git a/test/query/initialize.cpp b/test/query/initialize.cpp index eae6ca79..e1d0e62e 100644 --- a/test/query/initialize.cpp +++ b/test/query/initialize.cpp @@ -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);