Skip to content
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
65 changes: 65 additions & 0 deletions include/bitcoin/database/impl/query/pool/pool_suffice.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ code CLASS::get_pool_revalidation(tx_links& out,
return error::success;
}

TEMPLATE
code CLASS::get_pool_unvalidated(tx_links& out) const NOEXCEPT
{
using namespace system;
using algorithm = system::pool::algorithm;
using link_t = algorithm::link_t;
out.clear();
if (!store_.pool.enabled())
return error::success;

pool_guards guards{};
algorithm::columns_t columns{};
if (!get_pool_columns(columns, guards))
return error::integrity;

const auto rows = possible_narrow_cast<link_t>(columns.cells.size());
for (link_t row{}; row < rows; ++row)
{
const auto cell = algorithm::from_column(columns.cells[row]);
if (!algorithm::is_absent(cell) ||
is_zero(bit_and(cell, algorithm::insufficient)) ||
is_nonzero(algorithm::from_column(columns.flags[row])))
continue;

const auto tx = to_pool_tx(pool_link{ row });
if (!tx.is_terminal())
out.push_back(tx);
}

return error::success;
}

TEMPLATE
bool CLASS::set_pooled_context(const tx_link& link,
const context& ctx) NOEXCEPT
Expand All @@ -86,6 +118,39 @@ bool CLASS::set_pooled_context(const tx_link& link,
// ========================================================================
}

// A tx spending an output of the block (coinbase included) may depend on its
// confirmation, so it is not sufficient under any context until revalidated.
TEMPLATE
bool CLASS::set_pool_unvalidated(size_t& out,
const header_link& link) NOEXCEPT
{
out = zero;
if (!store_.pool.enabled())
return true;

for (const auto fk: to_transactions(link))
{
const tx_link tx{ fk };
const auto outputs = to_outputs(tx).size();
for (uint32_t index{}; index < outputs; ++index)
{
for (const auto& in: to_spenders(tx, index))
{
const auto spender = to_input_tx(in);
if (!is_pooled(spender))
continue;

if (!set_pooled_context(spender, {}))
return false;

++out;
}
}
}

return true;
}

TEMPLATE
bool CLASS::set_pool_context(size_t& out, const context& ctx) NOEXCEPT
{
Expand Down
7 changes: 7 additions & 0 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,10 @@ class query
code get_pool_revalidation(tx_links& out,
const context& ctx) const NOEXCEPT;

/// Pooled txs out without a validation context, in pool order (requires
/// the pool index).
code get_pool_unvalidated(tx_links& out) const NOEXCEPT;

/// Setters.
bool set_pooled(const tx_link& link, const transaction& tx,
const context& ctx, uint32_t time) NOEXCEPT;
Expand All @@ -799,6 +803,9 @@ class query
/// Set the context of the pooled tx, as validated under it.
bool set_pooled_context(const tx_link& link, const context& ctx) NOEXCEPT;

/// Clear the context of each pooled spender of the block's txs, out count.
bool set_pool_unvalidated(size_t& out, const header_link& link) NOEXCEPT;

/// Coverage.
/// -----------------------------------------------------------------------

Expand Down
109 changes: 109 additions & 0 deletions test/query/pool/pool_suffice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,115 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_revalidation__lower_height__not_listed)
BC_REQUIRE(links.empty());
}

// get_pool_unvalidated

BC_AUTO_TEST_CASE(query_pool__get_pool_unvalidated__more_flags__not_listed)
{
settings settings{ test::database_settings() };
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BC_REQUIRE(!store.create(test::events_handler));
BC_REQUIRE(query.initialize(test::genesis));

store.set_pooling();
const context more{ bip113 | flags::bip16_rule, 8, 9 };
const auto tx = to_spend(genesis_coinbase(), 4'999'999'000);
const auto pooled = pool_populated(query, tx);
size_t count{};
const auto swept = query.set_pool_context(count, more);
tx_links links{};
const auto ec = query.get_pool_unvalidated(links);

BC_REQUIRE(pooled);
BC_REQUIRE(swept);
BC_REQUIRE_EQUAL(count, 1u);
BC_REQUIRE_EQUAL(ec, error::success);
BC_REQUIRE(links.empty());
}

// set_pool_unvalidated

BC_AUTO_TEST_CASE(query_pool__set_pool_unvalidated__coinbase_spender__unvalidated_and_listed)
{
settings settings{ test::database_settings() };
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BC_REQUIRE(!store.create(test::events_handler));
BC_REQUIRE(query.initialize(test::genesis));

store.set_pooling();
const context ctx{ bip113, 8, 9 };
const auto parent = to_spend(genesis_coinbase(), 4'999'999'000);
const auto child = to_spend(parent.hash(false), 4'999'998'000);
const auto pooled = pool_populated(query, parent) && pool_populated(query, child);
size_t marked{};
const auto unvalidated = query.set_pool_unvalidated(marked, query.to_header(test::genesis.hash()));
size_t count{};
const auto swept = query.set_pool_context(count, ctx);
tx_links links{};
const auto ec = query.get_pool_unvalidated(links);

BC_REQUIRE(pooled);
BC_REQUIRE(unvalidated);
BC_REQUIRE_EQUAL(marked, 1u);
BC_REQUIRE(swept);
BC_REQUIRE_EQUAL(count, 1u);
BC_REQUIRE_EQUAL(pool_cell(query, store, parent), absent_insufficient);
BC_REQUIRE_EQUAL(pool_cell(query, store, child), absent_ancestor);
BC_REQUIRE_EQUAL(ec, error::success);
BC_REQUIRE_EQUAL(links.size(), one);
BC_REQUIRE_EQUAL(links.front(), query.to_tx(parent.hash(false)));
}

BC_AUTO_TEST_CASE(query_pool__set_pool_unvalidated__revalidated__present_after_sweep)
{
settings settings{ test::database_settings() };
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BC_REQUIRE(!store.create(test::events_handler));
BC_REQUIRE(query.initialize(test::genesis));

store.set_pooling();
const context ctx{ bip113, 8, 9 };
const auto tx = to_spend(genesis_coinbase(), 4'999'999'000);
const auto pooled = pool_populated(query, tx);
size_t marked{};
const auto unvalidated = query.set_pool_unvalidated(marked, query.to_header(test::genesis.hash()));
size_t excluded{};
const auto swept = query.set_pool_context(excluded, ctx);
const auto set = query.set_pooled_context(query.to_tx(tx.hash(false)), ctx);
size_t restored{ 42 };
const auto resumed = query.set_pool_context(restored, ctx);

BC_REQUIRE(pooled);
BC_REQUIRE(unvalidated);
BC_REQUIRE_EQUAL(marked, 1u);
BC_REQUIRE(swept);
BC_REQUIRE_EQUAL(excluded, 1u);
BC_REQUIRE(set);
BC_REQUIRE(resumed);
BC_REQUIRE(is_zero(restored));
BC_REQUIRE(system::pool::algorithm::is_present(pool_cell(query, store, tx)));
}

BC_AUTO_TEST_CASE(query_pool__set_pool_unvalidated__no_pooled_spender__true_none)
{
settings settings{ test::database_settings() };
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BC_REQUIRE(!store.create(test::events_handler));
BC_REQUIRE(query.initialize(test::genesis));

store.set_pooling();
size_t marked{ 42 };
BC_REQUIRE(query.set_pool_unvalidated(marked, query.to_header(test::genesis.hash())));
BC_REQUIRE(is_zero(marked));
}

// set_pooled_context

BC_AUTO_TEST_CASE(query_pool__set_pooled_context__revalidated__present_after_sweep)
Expand Down
Loading