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 is_strong(header), rename is_strong(spend), tests, comments. #486

Merged
merged 2 commits into from
Jun 7, 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
12 changes: 10 additions & 2 deletions include/bitcoin/database/impl/query/confirm.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool CLASS::is_spent(const spend_link& link) const NOEXCEPT

// unused
TEMPLATE
bool CLASS::is_strong(const spend_link& link) const NOEXCEPT
bool CLASS::is_strong_spend(const spend_link& link) const NOEXCEPT
{
return !to_block(to_spend_tx(link)).is_terminal();
}
Expand Down Expand Up @@ -284,13 +284,15 @@ inline error::error_t CLASS::unspendable_prevout(const point_link& link,
return error::success;
}


TEMPLATE
inline error::error_t CLASS::unspent_duplicates(const tx_link& link,
const context& ctx) const NOEXCEPT
{
if (!ctx.is_enabled(system::chain::flags::bip30_rule))
return error::success;

// This will be empty if current block is not set_strong.
const auto coinbases = to_strongs(get_tx_key(link));
if (coinbases.empty())
return error::integrity;
Expand Down Expand Up @@ -319,7 +321,7 @@ code CLASS::block_confirmable(const header_link& link) const NOEXCEPT
if (txs.empty())
return error::success;

code ec{};
code ec{};
if ((ec = unspent_duplicates(txs.front(), ctx)))
return ec;

Expand Down Expand Up @@ -363,6 +365,12 @@ bool CLASS::set_strong(const header_link& link, const tx_links& txs,
});
}

TEMPLATE
bool CLASS::is_strong(const header_link& link) const NOEXCEPT
{
return !to_block(to_coinbase(link)).is_terminal();
}

TEMPLATE
bool CLASS::set_strong(const header_link& link) NOEXCEPT
{
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ class query
/// These are not used in confirmation.
/// These rely on strong (use only for confirmation process).
bool is_spent(const spend_link& link) const NOEXCEPT;
bool is_strong(const spend_link& link) const NOEXCEPT;
bool is_strong(const header_link& link) const NOEXCEPT;
bool is_strong_spend(const spend_link& link) const NOEXCEPT;
bool is_mature(const spend_link& link, size_t height) const NOEXCEPT;
bool is_locked(const spend_link& link, uint32_t sequence,
const context& ctx) const NOEXCEPT;
Expand Down
32 changes: 29 additions & 3 deletions test/query/confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,32 @@ BOOST_AUTO_TEST_CASE(query_confirm__is_spent_output__strong_confirmed__true)
BOOST_REQUIRE(query.is_spent_output(query.to_output(1, 1))); // block1a
}

BOOST_AUTO_TEST_CASE(query_confirm__is_strong_spend__strong__true)
{
settings settings{};
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BOOST_REQUIRE_EQUAL(store.create(events_handler), error::success);
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE(query.is_strong_spend(query.to_spend(0, 0)));
}

BOOST_AUTO_TEST_CASE(query_confirm__is_strong_spend__weak__false)
{
settings settings{};
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BOOST_REQUIRE_EQUAL(store.create(events_handler), error::success);
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE(query.set(test::block1, context{}));
BOOST_REQUIRE(!query.is_strong_spend(query.to_spend(1, 0)));
BOOST_REQUIRE(query.set_strong(1));
BOOST_REQUIRE(query.is_strong_spend(query.to_spend(1, 0)));
}

// The coinbase tx is strong.
BOOST_AUTO_TEST_CASE(query_confirm__is_strong__strong__true)
{
settings settings{};
Expand All @@ -246,7 +272,7 @@ BOOST_AUTO_TEST_CASE(query_confirm__is_strong__strong__true)
test::query_accessor query{ store };
BOOST_REQUIRE_EQUAL(store.create(events_handler), error::success);
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE(query.is_strong(query.to_spend(0, 0)));
BOOST_REQUIRE(query.is_strong(0));
}

BOOST_AUTO_TEST_CASE(query_confirm__is_strong__weak__false)
Expand All @@ -258,9 +284,9 @@ BOOST_AUTO_TEST_CASE(query_confirm__is_strong__weak__false)
BOOST_REQUIRE_EQUAL(store.create(events_handler), error::success);
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE(query.set(test::block1, context{}));
BOOST_REQUIRE(!query.is_strong(query.to_spend(1, 0)));
BOOST_REQUIRE(!query.is_strong(1));
BOOST_REQUIRE(query.set_strong(1));
BOOST_REQUIRE(query.is_strong(query.to_spend(1, 0)));
BOOST_REQUIRE(query.is_strong(1));
}

BOOST_AUTO_TEST_CASE(query_confirm__is_spent__unspent__false)
Expand Down
15 changes: 15 additions & 0 deletions test/query/translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ BOOST_AUTO_TEST_CASE(query_translate__to_header__always__expected)
BOOST_REQUIRE_EQUAL(query.to_header(test::block1.hash()), 1u);
}

BOOST_AUTO_TEST_CASE(query_translate__to_coinbase__always__expected)
{
settings settings{};
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BOOST_REQUIRE_EQUAL(store.create(events_handler), error::success);
BOOST_REQUIRE_EQUAL(query.to_header(test::genesis.hash()), header_link::terminal);
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE_EQUAL(query.to_coinbase(0), 0u);
BOOST_REQUIRE(query.to_coinbase(1).is_terminal());
BOOST_REQUIRE(query.set(test::block1, test::context));
BOOST_REQUIRE_EQUAL(query.to_coinbase(1), 1u);
}

// to_point

BOOST_AUTO_TEST_CASE(query_translate__to_point__null_points__empty_points_table)
Expand Down
Loading