Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/test get_unassociated_count/above. #416

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/bitcoin/database/impl/query/initialize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ associations CLASS::get_unassociated_above(size_t height,
return out;
}

TEMPLATE
size_t CLASS::get_unassociated_count() const NOEXCEPT
{
return get_unassociated_count_above(get_fork());
}

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

return count;
}

TEMPLATE
hashes CLASS::get_candidate_hashes(const heights& heights) const NOEXCEPT
{
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 @@ -87,6 +87,8 @@ class query
associations get_all_unassociated() const NOEXCEPT;
associations get_unassociated_above(size_t height,
size_t count=max_size_t) const NOEXCEPT;
size_t get_unassociated_count() const NOEXCEPT;
size_t get_unassociated_count_above(size_t height) const NOEXCEPT;
hashes get_candidate_hashes(const heights& heights) const NOEXCEPT;
hashes get_confirmed_hashes(const heights& heights) const NOEXCEPT;

Expand Down
54 changes: 54 additions & 0 deletions test/query/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,60 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_unassociated_above__gapped_candidate_
BOOST_REQUIRE_EQUAL(query.get_all_unassociated().size(), 0u);
}

// get_unassociated_count_above/get_unassociated_count

BOOST_AUTO_TEST_CASE(query_initialize__get_unassociated_count_above__gapped_candidate__expected)
{
settings settings{};
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BOOST_REQUIRE_EQUAL(store.create(events), error::success);

constexpr database::context context2
{
0x01020302, // flags
0x00121312, // height (3 bytes)
0x21222322 // mtp
};
constexpr database::context context3
{
0x01020303, // flags
0x00121313, // height (3 bytes)
0x21222323 // mtp
};
BOOST_REQUIRE(query.initialize(test::genesis)); // associated
BOOST_REQUIRE(query.set(test::block1, test::context)); // associated
BOOST_REQUIRE(query.set(test::block2.header(), context2)); // header only
BOOST_REQUIRE(query.set(test::block3.header(), context3)); // header only
BOOST_REQUIRE(query.push_candidate(query.to_header(test::block1.hash())));
BOOST_REQUIRE(query.push_candidate(query.to_header(test::block2.hash())));
BOOST_REQUIRE(query.push_candidate(query.to_header(test::block3.hash())));

// There are two unassociated blocks above genesis.
BOOST_REQUIRE_EQUAL(query.get_unassociated_count(), 2u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(0), 2u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(1), 2u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(2), 1u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(3), 0u);

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

// There are no unassociated blocks.
BOOST_REQUIRE(query.set(test::block2)); // associated
BOOST_REQUIRE_EQUAL(query.get_unassociated_count(), 0u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(0), 0u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(1), 0u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(2), 0u);
BOOST_REQUIRE_EQUAL(query.get_unassociated_count_above(3), 0u);
}

// get_candidate_hashes

BOOST_AUTO_TEST_CASE(query_initialize__get_candidate_hashes__initialized__one)
Expand Down