Skip to content

Commit

Permalink
Merge 2ff6ec1 into 817a3f6
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Mar 21, 2024
2 parents 817a3f6 + 2ff6ec1 commit 659106a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
33 changes: 30 additions & 3 deletions include/bitcoin/database/impl/query/initialize.ipp
Expand Up @@ -60,13 +60,40 @@ size_t CLASS::get_fork() const NOEXCEPT
}

TEMPLATE
size_t CLASS::get_last_associated() const NOEXCEPT
size_t CLASS::get_top_preconfirmable() const NOEXCEPT
{
return get_last_associated_from(get_fork());
return get_top_preconfirmable_from(get_fork());
}

TEMPLATE
size_t CLASS::get_last_associated_from(size_t height) const NOEXCEPT
size_t CLASS::get_top_preconfirmable_from(size_t height) const NOEXCEPT
{
if (height >= height_link::terminal)
return max_size_t;

while (!is_zero(++height))
{
switch (get_block_state(to_candidate(height)).value())
{
case error::block_confirmable:
case error::block_preconfirmable:
continue;
default:
break;
}
}

return --height;
}

TEMPLATE
size_t CLASS::get_top_associated() const NOEXCEPT
{
return get_top_associated_from(get_fork());
}

TEMPLATE
size_t CLASS::get_top_associated_from(size_t height) const NOEXCEPT
{
if (height >= height_link::terminal)
return max_size_t;
Expand Down
6 changes: 2 additions & 4 deletions include/bitcoin/database/impl/query/validate.ipp
Expand Up @@ -163,8 +163,7 @@ code CLASS::get_block_state(const header_link& link) const NOEXCEPT
if (!store_.validated_bk.get(fk, valid))
return error::integrity;

// Should only be pre/confirmable if associated (not verified).
// Fees only valid if block_preconfirmable or block_confirmable.
// Fees only valid if block_confirmable.
return to_block_code(valid.code);
}

Expand All @@ -180,8 +179,7 @@ code CLASS::get_block_state(uint64_t& fees,
if (!store_.validated_bk.get(fk, valid))
return error::integrity;

// Should only be pre/confirmable if associated (not verified).
// Fees only valid if block_preconfirmable or block_confirmable.
// Fees only valid if block_confirmable.
fees = valid.fees;
return to_block_code(valid.code);
}
Expand Down
6 changes: 4 additions & 2 deletions include/bitcoin/database/query.hpp
Expand Up @@ -82,8 +82,10 @@ class query
inline size_t get_top_candidate() const NOEXCEPT;
inline size_t get_top_confirmed() const NOEXCEPT;
size_t get_fork() const NOEXCEPT;
size_t get_last_associated() const NOEXCEPT;
size_t get_last_associated_from(size_t height) const NOEXCEPT;
size_t get_top_associated() const NOEXCEPT;
size_t get_top_associated_from(size_t height) const NOEXCEPT;
size_t get_top_preconfirmable() const NOEXCEPT;
size_t get_top_preconfirmable_from(size_t height) const NOEXCEPT;
associations get_all_unassociated() const NOEXCEPT;
associations get_unassociated_above(size_t height,
size_t count=max_size_t) const NOEXCEPT;
Expand Down
44 changes: 22 additions & 22 deletions test/query/initialize.cpp
Expand Up @@ -244,40 +244,40 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_fork__confirmed_ahead__expected)
BOOST_REQUIRE_EQUAL(query.get_fork(), 1u);
}

// get_last_associated_from/get_last_associated
// get_top_associated_from/get_top_associated

BOOST_AUTO_TEST_CASE(query_initialize__get_last_associated_from__terminal__max_size_t)
BOOST_AUTO_TEST_CASE(query_initialize__get_top_associated_from__terminal__max_size_t)
{
settings settings{};
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BOOST_REQUIRE_EQUAL(store.create(events), error::success);
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(max_size_t), max_size_t);
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(height_link::terminal), max_size_t);
BOOST_REQUIRE_EQUAL(query.get_last_associated(), 0u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(max_size_t), max_size_t);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(height_link::terminal), max_size_t);
BOOST_REQUIRE_EQUAL(query.get_top_associated(), 0u);

// unassociated, but correct.
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(42), 42u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(42), 42u);
}

BOOST_AUTO_TEST_CASE(query_initialize__get_last_associated_from__initialized__zero)
BOOST_AUTO_TEST_CASE(query_initialize__get_top_associated_from__initialized__zero)
{
settings settings{};
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BOOST_REQUIRE_EQUAL(store.create(events), error::success);
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE_EQUAL(query.get_last_associated(), 0u);
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(0), 0u);
BOOST_REQUIRE_EQUAL(query.get_top_associated(), 0u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(0), 0u);

// unassociated, but correct.
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(42), 42u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(42), 42u);
}

BOOST_AUTO_TEST_CASE(query_initialize__get_last_associated_from__non_candidate__expected)
BOOST_AUTO_TEST_CASE(query_initialize__get_top_associated_from__non_candidate__expected)
{
settings settings{};
settings.path = TEST_DIRECTORY;
Expand All @@ -290,16 +290,16 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_last_associated_from__non_candidate__
BOOST_REQUIRE(query.set(test::block3, test::context));
BOOST_REQUIRE(query.push_candidate(query.to_header(test::block1.hash())));
BOOST_REQUIRE(query.push_candidate(query.to_header(test::block2.hash())));
BOOST_REQUIRE_EQUAL(query.get_last_associated(), 2u);
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(0), 2u);
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(1), 2u);
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(2), 2u);
BOOST_REQUIRE_EQUAL(query.get_top_associated(), 2u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(0), 2u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(1), 2u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(2), 2u);

// unassociated, but correct.
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(3), 3u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(3), 3u);
}

BOOST_AUTO_TEST_CASE(query_initialize__get_last_associated_from__gapped_candidate__expected)
BOOST_AUTO_TEST_CASE(query_initialize__get_top_associated_from__gapped_candidate__expected)
{
settings settings{};
settings.path = TEST_DIRECTORY;
Expand All @@ -313,15 +313,15 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_last_associated_from__gapped_candidat
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())));
BOOST_REQUIRE_EQUAL(query.get_last_associated(), 1u);
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(0), 1u);
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(1), 1u);
BOOST_REQUIRE_EQUAL(query.get_top_associated(), 1u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(0), 1u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(1), 1u);

// gapped, but correct.
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(2), 3u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(2), 3u);

// unassociated, but correct.
BOOST_REQUIRE_EQUAL(query.get_last_associated_from(3), 3u);
BOOST_REQUIRE_EQUAL(query.get_top_associated_from(3), 3u);
}

// get_unassociated_above/get_all_unassociated
Expand Down

0 comments on commit 659106a

Please sign in to comment.