Skip to content

Commit

Permalink
Merge pull request #426 from evoskuil/master
Browse files Browse the repository at this point in the history
Add get_top_preconfirmable, rename get_top_associated.
  • Loading branch information
evoskuil committed Mar 23, 2024
2 parents 817a3f6 + f89ff15 commit 1320f3a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 46 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
32 changes: 15 additions & 17 deletions include/bitcoin/database/impl/query/validate.ipp
Expand Up @@ -124,19 +124,19 @@ bool CLASS::get_context(context& ctx,
return true;
}

TEMPLATE
bool CLASS::get_check_context(context& ctx, hash_digest& hash,
uint32_t& timestamp, const header_link& link) const NOEXCEPT
{
table::header::get_check_context header{};
if (!store_.header.get(link, header))
return false;

hash = std::move(header.key);
ctx = std::move(header.ctx);
timestamp = header.timestamp;
return true;
}
////TEMPLATE
////bool CLASS::get_check_context(context& ctx, hash_digest& hash,
//// uint32_t& timestamp, const header_link& link) const NOEXCEPT
////{
//// table::header::get_check_context header{};
//// if (!store_.header.get(link, header))
//// return false;
////
//// hash = std::move(header.key);
//// ctx = std::move(header.ctx);
//// timestamp = header.timestamp;
//// return true;
////}

TEMPLATE
code CLASS::get_header_state(const header_link& link) const NOEXCEPT
Expand All @@ -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
8 changes: 4 additions & 4 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 Expand Up @@ -288,8 +290,6 @@ class query
bool get_version(uint32_t& version, const header_link& link) const NOEXCEPT;
bool get_bits(uint32_t& bits, const header_link& link) const NOEXCEPT;
bool get_context(context& ctx, const header_link& link) const NOEXCEPT;
bool get_check_context(context& ctx, hash_digest& hash, uint32_t& timestamp,
const header_link& link) const NOEXCEPT;

bool set_block_preconfirmable(const header_link& link) NOEXCEPT;
bool set_block_unconfirmable(const header_link& link) 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 1320f3a

Please sign in to comment.