From f49cf4a0dff14a4a82e22823cd783b946b5204df Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sun, 22 Nov 2026 01:04:13 -0500 Subject: [PATCH] Unvalidate the pooled spenders of a reorganized block. --- .../database/impl/query/pool/pool_suffice.ipp | 65 +++++++++++ include/bitcoin/database/query.hpp | 7 ++ test/query/pool/pool_suffice.cpp | 109 ++++++++++++++++++ 3 files changed, 181 insertions(+) diff --git a/include/bitcoin/database/impl/query/pool/pool_suffice.ipp b/include/bitcoin/database/impl/query/pool/pool_suffice.ipp index 47150815f..6916653f0 100644 --- a/include/bitcoin/database/impl/query/pool/pool_suffice.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_suffice.ipp @@ -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(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 @@ -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 { diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 85ff6f067..48ed417fd 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -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; @@ -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. /// ----------------------------------------------------------------------- diff --git a/test/query/pool/pool_suffice.cpp b/test/query/pool/pool_suffice.cpp index b12ae06c2..2dacd4cd7 100644 --- a/test/query/pool/pool_suffice.cpp +++ b/test/query/pool/pool_suffice.cpp @@ -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)