From e3ffc11075d2681bb7b4c8a424ab38280a04216c Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Mon, 23 Nov 2026 11:20:20 -0500 Subject: [PATCH 1/7] Admit component past outcome limit when its scored outcomes cover it. --- .../impl/query/pool/pool_coverage.ipp | 12 ++++----- test/query/pool/pool_coverage.cpp | 25 ++++++++++++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/include/bitcoin/database/impl/query/pool/pool_coverage.ipp b/include/bitcoin/database/impl/query/pool/pool_coverage.ipp index abc6a0fd5..29675b642 100644 --- a/include/bitcoin/database/impl/query/pool/pool_coverage.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_coverage.ipp @@ -229,14 +229,12 @@ code CLASS::get_pool_coverage(bool& out, const pool_graph& graph, !algorithm::ancestry(sum, g, component, limit)) return error::pool_limit; - const auto outcomes = algorithm::outcomes(component); - if (outcomes > maximum_outcomes) + std::vector masks{}; + algorithm::leaves(masks, component, g, maximum_outcomes, rate, + bytes_per_sigop); + if (masks.empty()) return error::pool_limit; - std::vector masks(outcomes); - for (size_t index{}; index < outcomes; ++index) - masks[index] = algorithm::outcome(component, index); - const auto best = algorithm::evaluate(masks, component, g); const auto bytes = algorithm::chargeable(component, g, bytes_per_sigop); each = each && algorithm::covered(best, bytes, rate); @@ -247,7 +245,7 @@ code CLASS::get_pool_coverage(bool& out, const pool_graph& graph, // family within a block, beyond which each component pays its own. uint64_t fee{}, bytes{}; const auto bounded = algorithm::family(fee, bytes, g, measured, - limit.size, bytes_per_sigop, maximum_outcomes); + limit.size, rate, bytes_per_sigop, maximum_outcomes); out = bounded ? algorithm::covered(fee, bytes, rate) : each; diff --git a/test/query/pool/pool_coverage.cpp b/test/query/pool/pool_coverage.cpp index 7422b870a..b6a671b46 100644 --- a/test/query/pool/pool_coverage.cpp +++ b/test/query/pool/pool_coverage.cpp @@ -235,7 +235,7 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_coverage__confirmed_spender__not_covered) BC_REQUIRE(!covered); } -BC_AUTO_TEST_CASE(query_pool__get_pool_coverage__outcomes_over_limit__pool_limit) +BC_AUTO_TEST_CASE(query_pool__get_pool_coverage__outcomes_over_limit__covered) { settings settings{ test::database_settings() }; settings.path = TEST_DIRECTORY; @@ -252,6 +252,29 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_coverage__outcomes_over_limit__pool_limit const context ctx{ bip113, 8, 9 }; const auto ec = query.get_pool_coverage(covered, txs, { false }, ctx, 0, 20, block_limit, 1); + BC_REQUIRE(pooled); + BC_REQUIRE(populated); + BC_REQUIRE_EQUAL(ec, error::success); + BC_REQUIRE(covered); +} + +BC_AUTO_TEST_CASE(query_pool__get_pool_coverage__no_outcomes__pool_limit) +{ + 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 auto pooled = pool_populated(query, to_spend(genesis_coinbase(), 4'999'999'000)); + transaction_cptrs txs{}; + const auto populated = pool_candidate(txs, query, to_spend(genesis_coinbase(), 4'999'999'500)); + bool covered{}; + const context ctx{ bip113, 8, 9 }; + const auto ec = query.get_pool_coverage(covered, txs, { false }, ctx, 0, 20, block_limit, 0); + BC_REQUIRE(pooled); BC_REQUIRE(populated); BC_REQUIRE_EQUAL(ec, error::pool_limit); From e22a2cc5b7caae434f63e006502d045e09196619 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Mon, 23 Nov 2026 14:25:01 -0500 Subject: [PATCH 2/7] List confirmable rows by their keys, without probing the pool. --- .../bitcoin/database/impl/query/pool/pool_confirmable.ipp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp b/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp index 279d6bcf0..3a72756f7 100644 --- a/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp @@ -140,9 +140,7 @@ code CLASS::get_confirmable_txs(tx_links& out) const NOEXCEPT if (!algorithm::is_present(cell)) continue; - const auto tx = to_pool_tx(pool_link{ row }); - if (!tx.is_terminal()) - out.push_back(tx); + out.push_back(tx_link{ store_.pool.get_key(pool_link{ row }) }); } return error::success; @@ -167,8 +165,7 @@ code CLASS::get_confirmable_fees(fee_rates& out) const NOEXCEPT for (link_t row{}; row < rows; ++row) { const auto cell = algorithm::from_column(columns.cells[row]); - if (!algorithm::is_present(cell) || - to_pool_tx(pool_link{ row }).is_terminal()) + if (!algorithm::is_present(cell)) continue; const auto totals = algorithm::from_column(columns.totals[row]); From 9fd114480781ff52fe46081108a7f5b5a2a4753e Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Mon, 23 Nov 2026 14:25:02 -0500 Subject: [PATCH 3/7] Read the ids, totals and parent positions of template txs. --- .../impl/query/pool/pool_template.ipp | 43 +++++++++++++++++ include/bitcoin/database/query.hpp | 4 ++ include/bitcoin/database/types/pooled_tx.hpp | 12 +++++ test/query/pool/pool_template.cpp | 48 +++++++++++++++++++ 4 files changed, 107 insertions(+) diff --git a/include/bitcoin/database/impl/query/pool/pool_template.ipp b/include/bitcoin/database/impl/query/pool/pool_template.ipp index 82361af35..09d3d9ee9 100644 --- a/include/bitcoin/database/impl/query/pool/pool_template.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_template.ipp @@ -59,6 +59,49 @@ code CLASS::get_pool_template(tx_links& out, pool_projection& projection, return error::success; } +TEMPLATE +code CLASS::get_template_txs(template_txs& out, + const tx_links& links) const NOEXCEPT +{ + using prevout = table::prevout::slab_get; + out.clear(); + out.reserve(links.size()); + std::unordered_map positions{}; + positions.reserve(links.size()); + for (const tx_link link: links) + { + table::pool_edge edge{}; + table::pool_total total{}; + const auto fk = store_.pool.first(link); + if (fk.is_terminal() || !store_.pool.totals.get(fk, total) || + !store_.pool.edges.get(fk, edge)) + return error::integrity; + + table::spends::get_refs::parents parents(edge.inputs); + table::spends::get_refs spent{ {}, parents }; + if (!store_.spends.get(edge.spends_fk, spent)) + return error::integrity; + + template_tx tx{ get_tx_key(link), get_wtxid_key(fk), total.fee, + total.sigops, {} }; + for (const auto merged: parents) + { + const tx_link parent{ prevout::output_tx_fk(merged) }; + const auto it = positions.find(parent.value); + if (it == positions.end()) + continue; + + if (!system::contains(tx.parents, it->second)) + tx.parents.push_back(it->second); + } + + positions.emplace(link.value, out.size()); + out.push_back(std::move(tx)); + } + + return error::success; +} + // protected TEMPLATE void CLASS::set_pool_resolution(pool_projection& projection, diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index a07ed65c6..b7543ad4d 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -866,6 +866,10 @@ class query const pool_totals& budget, size_t depth, uint64_t cutoff, uint32_t bytes_per_sigop, size_t maximum_outcomes) const NOEXCEPT; + /// Ids, totals and parent positions of each pooled tx of template. + code get_template_txs(template_txs& out, + const tx_links& links) const NOEXCEPT; + /// Estimate. /// ----------------------------------------------------------------------- diff --git a/include/bitcoin/database/types/pooled_tx.hpp b/include/bitcoin/database/types/pooled_tx.hpp index 5083620e5..634342942 100644 --- a/include/bitcoin/database/types/pooled_tx.hpp +++ b/include/bitcoin/database/types/pooled_tx.hpp @@ -40,6 +40,18 @@ struct pooled_tx std::vector prevouts{}; }; +/// Pooled tx of template, ids and totals with positions of its parents. +struct template_tx +{ + system::hash_digest txid{}; + system::hash_digest wtxid{}; + uint64_t fee{}; + size_t sigops{}; + std::vector parents{}; +}; + +using template_txs = std::vector; + } // namespace database } // namespace libbitcoin diff --git a/test/query/pool/pool_template.cpp b/test/query/pool/pool_template.cpp index f47e61c2b..3d7a8dbe7 100644 --- a/test/query/pool/pool_template.cpp +++ b/test/query/pool/pool_template.cpp @@ -191,4 +191,52 @@ BC_AUTO_TEST_CASE(query_pool__get_pool_template__chain_over_budget__first_block_ BC_REQUIRE(!projection.exhausted); } +// get_template_txs +// ---------------------------------------------------------------------------- + +BC_AUTO_TEST_CASE(query_pool__get_template_txs__parent_and_child__child_depends_on_parent) +{ + 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 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); + const tx_links links{ query.to_tx(parent.hash(false)), query.to_tx(child.hash(false)) }; + template_txs out{}; + const auto ec = query.get_template_txs(out, links); + + BC_REQUIRE(pooled); + BC_REQUIRE_EQUAL(ec, error::success); + BC_REQUIRE_EQUAL(out.size(), 2u); + BC_REQUIRE(out[0].txid == parent.hash(false)); + BC_REQUIRE(out[0].wtxid == parent.hash(true)); + BC_REQUIRE_EQUAL(out[0].fee, 1'000u); + BC_REQUIRE(out[0].parents.empty()); + BC_REQUIRE(out[1].txid == child.hash(false)); + BC_REQUIRE(out[1].wtxid == child.hash(true)); + BC_REQUIRE_EQUAL(out[1].fee, 1'000u); + BC_REQUIRE_EQUAL(out[1].parents.size(), 1u); + BC_REQUIRE_EQUAL(out[1].parents[0], 0u); +} + +BC_AUTO_TEST_CASE(query_pool__get_template_txs__not_pooled__integrity) +{ + 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(); + template_txs out{}; + BC_REQUIRE_EQUAL(query.get_template_txs(out, { 0 }), error::integrity); +} + BC_AUTO_TEST_SUITE_END() From 2bc5efa6305d84216f9fe300d37538d8a9f9c211 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Mon, 23 Nov 2026 17:32:41 -0500 Subject: [PATCH 4/7] Retain coinbase txs whole when pruning. --- .../impl/query/archive/chain_writer.ipp | 9 +++- .../impl/query/archive/wire_writer.ipp | 11 +++-- test/query/archive/chain_reader.cpp | 35 ++++++++++++++++ test/query/archive/wire_reader.cpp | 10 ++--- test/query/properties_block.cpp | 31 +++++++++++++- test/query/properties_tx.cpp | 42 +++++++++++++++++-- test/store/store_open.cpp | 4 +- 7 files changed, 125 insertions(+), 17 deletions(-) diff --git a/include/bitcoin/database/impl/query/archive/chain_writer.ipp b/include/bitcoin/database/impl/query/archive/chain_writer.ipp index 0f44947b2..c564e34c5 100644 --- a/include/bitcoin/database/impl/query/archive/chain_writer.ipp +++ b/include/bitcoin/database/impl/query/archive/chain_writer.ipp @@ -456,13 +456,15 @@ code CLASS::set_code(const block& block, const header_link& key, if (fk.is_terminal()) return error::tx_tx_allocate; + // A coinbase is never pruned, so neither is a block of only its coinbase. code ec{}; auto it = links.begin(); for (const auto& tx: *block.transactions_ptr()) { if (*it == tx_link::terminal) { - if ((ec = set_code(fk, *tx, bypass, prune))) + const auto pruned = prune && !tx->is_coinbase(); + if ((ec = set_code(fk, *tx, bypass, pruned))) return ec; *it = fk++; @@ -471,8 +473,11 @@ code CLASS::set_code(const block& block, const header_link& key, ++it; } + const auto& all = *block.transactions_ptr(); + const auto pruned = prune && std::any_of(all.begin(), all.end(), + [](const auto& tx) NOEXCEPT { return !tx->is_coinbase(); }); return set_txs(key, std::move(links), light, heavy, std::move(interval), - strong, prune); + strong, pruned); // ======================================================================== } diff --git a/include/bitcoin/database/impl/query/archive/wire_writer.ipp b/include/bitcoin/database/impl/query/archive/wire_writer.ipp index 7897c60e8..dec0f1b06 100644 --- a/include/bitcoin/database/impl/query/archive/wire_writer.ipp +++ b/include/bitcoin/database/impl/query/archive/wire_writer.ipp @@ -267,7 +267,7 @@ code CLASS::set_code(const block_view& block, const header_link& key, points += tx.inputs(); outputs += tx.outputs(); - input_bytes += tx.input_table_size(prune); + input_bytes += tx.input_table_size(prune && !tx.is_coinbase()); output_bytes += tx.outputs() * tx_link::size + tx.output_table_size(); } @@ -332,10 +332,11 @@ code CLASS::set_code(const block_view& block, const header_link& key, { if (*it == tx_link::terminal) { - if ((ec = set_code(twins, ptrs, fks, tx, bypass, prune))) + const auto pruned = prune && !tx.is_coinbase(); + if ((ec = set_code(twins, ptrs, fks, tx, bypass, pruned))) return ec; - const auto in_bytes = tx.input_table_size(prune); + const auto in_bytes = tx.input_table_size(pruned); // Output rows are parent fk prefixed (see table::output::put_view). const auto out_bytes = tx.outputs() * tx_link::size + @@ -365,8 +366,10 @@ code CLASS::set_code(const block_view& block, const header_link& key, if (!store_.duplicate.put(twin, table::duplicate::record{})) return error::tx_duplicate_put; + const auto pruned = prune && std::any_of(views.begin(), views.end(), + [](const auto& tx) NOEXCEPT { return !tx.is_coinbase(); }); return set_txs(key, std::move(links), light, heavy, std::move(interval), - strong, prune); + strong, pruned); // ======================================================================== } diff --git a/test/query/archive/chain_reader.cpp b/test/query/archive/chain_reader.cpp index 2dbdd18a7..3c868aded 100644 --- a/test/query/archive/chain_reader.cpp +++ b/test/query/archive/chain_reader.cpp @@ -208,6 +208,41 @@ BC_AUTO_TEST_CASE(query_chain_reader__get_input__genesis__expected) BC_CHECK(input->script() == *query.get_input_script(point)); } +BC_AUTO_TEST_CASE(query_chain_reader__get_input_sequence__genesis__expected) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_CHECK(!store.create(test::events_handler)); + BC_CHECK(query.initialize(test::genesis)); + + uint32_t sequence{}; + const auto& tx = *test::genesis.transactions_ptr()->front(); + BC_CHECK(query.get_input_sequence(sequence, query.to_point(query.to_tx(tx.hash(false)), 0u))); + BC_CHECK_EQUAL(sequence, tx.inputs_ptr()->front()->sequence()); +} + +BC_AUTO_TEST_CASE(query_chain_reader__get_input_sequence__pruned__expected) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_CHECK(!store.create(test::events_handler)); + BC_CHECK(query.initialize(test::genesis)); + BC_CHECK(query.set(test::block1a.header(), test::context, {}, false)); + BC_CHECK(query.set(test::block1a, false, false, true)); + + uint32_t sequence{}; + const auto& tx = *test::block1a.transactions_ptr()->front(); + const auto link = query.to_tx(tx.hash(false)); + BC_CHECK(query.is_pruned(link)); + BC_CHECK(query.get_input_sequence(sequence, query.to_point(link, 0u))); + BC_CHECK_EQUAL(sequence, tx.inputs_ptr()->front()->sequence()); + BC_CHECK(query.get_point(query.to_point(link, 0u)) == tx.inputs_ptr()->front()->point()); +} + BC_AUTO_TEST_CASE(query_chain_reader__get_inputs__tx_not_found__nullptr) { settings settings{ test::database_settings() }; diff --git a/test/query/archive/wire_reader.cpp b/test/query/archive/wire_reader.cpp index d2dd67a6c..c4c7e5b3b 100644 --- a/test/query/archive/wire_reader.cpp +++ b/test/query/archive/wire_reader.cpp @@ -196,9 +196,9 @@ BC_AUTO_TEST_CASE(query_wire_reader__get_wire_tx__pruned__empty) test::query_t query{ store }; BC_CHECK(!store.create(test::events_handler)); BC_CHECK(query.initialize(test::genesis)); - BC_CHECK(query.set(test::block1.header(), database::context{ 0, 1, 0 }, {}, false)); - BC_CHECK(query.set(test::block1, false, false, true)); - BC_CHECK(!query.to_tx(test::block1.transactions_ptr()->front()->hash(false)).is_terminal()); + BC_CHECK(query.set(test::block1a.header(), database::context{ 0, 1, 0 }, {}, false)); + BC_CHECK(query.set(test::block1a, false, false, true)); + BC_CHECK(!query.to_tx(test::block1a.transactions_ptr()->front()->hash(false)).is_terminal()); BC_CHECK(query.get_wire_tx(1, true).empty()); BC_CHECK(query.get_wire_tx(1, false).empty()); BC_CHECK(!store.close(test::events_handler)); @@ -235,8 +235,8 @@ BC_AUTO_TEST_CASE(query_wire_reader__get_wire_block__pruned__empty) test::query_t query{ store }; BC_CHECK(!store.create(test::events_handler)); BC_CHECK(query.initialize(test::genesis)); - BC_CHECK(query.set(test::block1.header(), database::context{ 0, 1, 0 }, {}, false)); - BC_CHECK(query.set(test::block1, false, false, true)); + BC_CHECK(query.set(test::block1a.header(), database::context{ 0, 1, 0 }, {}, false)); + BC_CHECK(query.set(test::block1a, false, false, true)); BC_CHECK(query.get_wire_block(1, true).empty()); BC_CHECK(query.get_wire_block(1, false).empty()); BC_CHECK(query.get_wire_block(1, true, true).empty()); diff --git a/test/query/properties_block.cpp b/test/query/properties_block.cpp index 40de24b35..50381439b 100644 --- a/test/query/properties_block.cpp +++ b/test/query/properties_block.cpp @@ -212,6 +212,19 @@ BC_AUTO_TEST_CASE(query_properties_block__get_wtxids__genesis__coinbase_witness_ // is_pruned BC_AUTO_TEST_CASE(query_properties_block__is_pruned__pruned__true) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_CHECK(!store.create(test::events_handler)); + BC_CHECK(query.initialize(test::genesis)); + BC_CHECK(query.set(test::block1a.header(), test::context, {}, false)); + BC_CHECK(query.set(test::block1a, false, false, true)); + BC_CHECK(query.is_pruned(header_link{ 1 })); +} + +BC_AUTO_TEST_CASE(query_properties_block__is_pruned__pruned_coinbase_only__false) { settings settings{ test::database_settings() }; settings.path = TEST_DIRECTORY; @@ -221,7 +234,23 @@ BC_AUTO_TEST_CASE(query_properties_block__is_pruned__pruned__true) BC_CHECK(query.initialize(test::genesis)); BC_CHECK(query.set(test::block1.header(), test::context, {}, false)); BC_CHECK(query.set(test::block1, false, false, true)); - BC_CHECK(query.is_pruned(header_link{ 1 })); + BC_CHECK(!query.is_pruned(header_link{ 1 })); + BC_CHECK(query.get_block(1, true)); +} + +BC_AUTO_TEST_CASE(query_properties_block__is_pruned__pruned_coinbase_and_spend__true) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_CHECK(!store.create(test::events_handler)); + BC_CHECK(query.initialize(test::genesis)); + BC_CHECK(query.set(test::block1a.header(), test::context, {}, false)); + BC_CHECK(query.set(test::block1a, false, false, false)); + BC_CHECK(query.set(test::block_coinbase_spend_1a.header(), test::context, {}, false)); + BC_CHECK(query.set(test::block_coinbase_spend_1a, false, false, true)); + BC_CHECK(query.is_pruned(query.to_header(test::block_coinbase_spend_1a.hash()))); } BC_AUTO_TEST_CASE(query_properties_block__is_pruned__unpruned__false) diff --git a/test/query/properties_tx.cpp b/test/query/properties_tx.cpp index d3837fd11..7fbb8f902 100644 --- a/test/query/properties_tx.cpp +++ b/test/query/properties_tx.cpp @@ -100,6 +100,19 @@ BC_AUTO_TEST_CASE(query_properties_tx__is_coinbase__non_coinbase__false) // is_pruned BC_AUTO_TEST_CASE(query_properties_tx__is_pruned__pruned__true) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_CHECK(!store.create(test::events_handler)); + BC_CHECK(query.initialize(test::genesis)); + BC_CHECK(query.set(test::block1a.header(), test::context, {}, false)); + BC_CHECK(query.set(test::block1a, false, false, true)); + BC_CHECK(query.is_pruned(tx_link{ 1 })); +} + +BC_AUTO_TEST_CASE(query_properties_tx__is_pruned__pruned_coinbase_only__false) { settings settings{ test::database_settings() }; settings.path = TEST_DIRECTORY; @@ -109,7 +122,30 @@ BC_AUTO_TEST_CASE(query_properties_tx__is_pruned__pruned__true) BC_CHECK(query.initialize(test::genesis)); BC_CHECK(query.set(test::block1.header(), test::context, {}, false)); BC_CHECK(query.set(test::block1, false, false, true)); - BC_CHECK(query.is_pruned(tx_link{ 1 })); + BC_CHECK(!query.is_pruned(tx_link{ 1 })); + BC_CHECK(query.get_transaction(1, true)); +} + +BC_AUTO_TEST_CASE(query_properties_tx__is_pruned__pruned_block_coinbase__false) +{ + settings settings{ test::database_settings() }; + settings.path = TEST_DIRECTORY; + test::chunk_store store{ settings }; + test::query_accessor query{ store }; + BC_CHECK(!store.create(test::events_handler)); + BC_CHECK(query.initialize(test::genesis)); + BC_CHECK(query.set(test::block1a.header(), test::context, {}, false)); + BC_CHECK(query.set(test::block1a, false, false, false)); + BC_CHECK(query.set(test::block_coinbase_spend_1a.header(), test::context, {}, false)); + BC_CHECK(query.set(test::block_coinbase_spend_1a, false, false, true)); + + const auto& txs = *test::block_coinbase_spend_1a.transactions_ptr(); + const auto coinbase = query.to_tx(txs.front()->hash(false)); + const auto spend = query.to_tx(txs.back()->hash(false)); + BC_CHECK(!query.is_pruned(coinbase)); + BC_CHECK(query.is_pruned(spend)); + BC_CHECK(*query.get_transaction(coinbase, true) == *txs.front()); + BC_CHECK(!query.get_transaction(spend, true)); } BC_AUTO_TEST_CASE(query_properties_tx__is_pruned__unpruned__false) @@ -161,8 +197,8 @@ BC_AUTO_TEST_CASE(query_properties_tx__is_pruned_input__pruned__true) test::query_accessor query{ store }; BC_CHECK(!store.create(test::events_handler)); BC_CHECK(query.initialize(test::genesis)); - BC_CHECK(query.set(test::block1.header(), test::context, {}, false)); - BC_CHECK(query.set(test::block1, false, false, true)); + BC_CHECK(query.set(test::block1a.header(), test::context, {}, false)); + BC_CHECK(query.set(test::block1a, false, false, true)); BC_CHECK(query.is_pruned_input(1)); } diff --git a/test/store/store_open.cpp b/test/store/store_open.cpp index 457fde6e1..dc880fafd 100644 --- a/test/store/store_open.cpp +++ b/test/store/store_open.cpp @@ -302,8 +302,8 @@ static bool set_unflagged_pruned_block(test::map_store& store) NOEXCEPT { database::query query{ store }; if (!query.initialize(test::genesis) || - !query.set(test::block1.header(), test::context, {}, false) || - !query.set(test::block1, false, false, true)) + !query.set(test::block1a.header(), test::context, {}, false) || + !query.set(test::block1a, false, false, true)) return false; table::transaction::record record{}; From d4d231b6c90607aa26d9546fe181db678fd7cda4 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Mon, 23 Nov 2026 17:32:41 -0500 Subject: [PATCH 5/7] Read the sequence of an input. --- .../database/impl/query/archive/chain_reader.ipp | 15 +++++++++++++++ include/bitcoin/database/query.hpp | 1 + test/query/archive/chain_reader.cpp | 6 +++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/bitcoin/database/impl/query/archive/chain_reader.ipp b/include/bitcoin/database/impl/query/archive/chain_reader.ipp index ad06844a2..ff244e287 100644 --- a/include/bitcoin/database/impl/query/archive/chain_reader.ipp +++ b/include/bitcoin/database/impl/query/archive/chain_reader.ipp @@ -207,6 +207,21 @@ typename CLASS::point CLASS::get_point( return { point.hash, point.index }; } +// ins_link->sequence +// ---------------------------------------------------------------------------- + +TEMPLATE +bool CLASS::get_input_sequence(uint32_t& out, + const ins_link& link) const NOEXCEPT +{ + table::ins_sequence::get_input ins{}; + if (!store_.ins.sequence.get(link, ins)) + return false; + + out = ins.sequence; + return true; +} + // ins_link->witness // ---------------------------------------------------------------------------- diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index b7543ad4d..c26757f6d 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -559,6 +559,7 @@ class query bool witness) const NOEXCEPT; point get_point(const ins_link& link) const NOEXCEPT; + bool get_input_sequence(uint32_t& out, const ins_link& link) const NOEXCEPT; witness::cptr get_witness(const ins_link& link) const NOEXCEPT; script::cptr get_input_script(const ins_link& link) const NOEXCEPT; input::cptr get_input(const ins_link& link, bool witness) const NOEXCEPT; diff --git a/test/query/archive/chain_reader.cpp b/test/query/archive/chain_reader.cpp index 3c868aded..855d65ec1 100644 --- a/test/query/archive/chain_reader.cpp +++ b/test/query/archive/chain_reader.cpp @@ -370,9 +370,9 @@ BC_AUTO_TEST_CASE(query_chain_reader__get_transaction__pruned__nullptr) test::query_accessor query{ store }; BC_CHECK(!store.create(test::events_handler)); BC_CHECK(query.initialize(test::genesis)); - BC_CHECK(query.set(test::block1.header(), test::context, {}, false)); - BC_CHECK(query.set(test::block1, false, false, true)); - BC_CHECK(!query.to_tx(test::block1.transactions_ptr()->front()->hash(false)).is_terminal()); + BC_CHECK(query.set(test::block1a.header(), test::context, {}, false)); + BC_CHECK(query.set(test::block1a, false, false, true)); + BC_CHECK(!query.to_tx(test::block1a.transactions_ptr()->front()->hash(false)).is_terminal()); BC_CHECK(!query.get_transaction(1, true)); BC_CHECK(!query.get_transaction(1, false)); BC_CHECK(!query.get_transactions(1, false)); From d6e0c44393ad75179438957594688ec288ba2151 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Mon, 23 Nov 2026 17:38:10 -0500 Subject: [PATCH 6/7] Read the unconfirmed pooled ancestry of txs from the pool columns. --- .../impl/query/pool/pool_confirmable.ipp | 57 ++++++++++++++ include/bitcoin/database/query.hpp | 4 + include/bitcoin/database/types/pooled_tx.hpp | 10 +++ test/query/pool/pool_confirmable.cpp | 74 +++++++++++++++++++ 4 files changed, 145 insertions(+) diff --git a/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp b/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp index 3a72756f7..5d8b45c76 100644 --- a/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_confirmable.ipp @@ -146,6 +146,63 @@ code CLASS::get_confirmable_txs(tx_links& out) const NOEXCEPT return error::success; } +TEMPLATE +code CLASS::get_pooled_ancestry(pooled_rows& out, + const tx_links& links) 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; + + std::vector pending{}; + pending.reserve(links.size()); + for (const tx_link link: links) + { + const auto row = store_.pool.first(link); + if (row.is_terminal()) + return error::integrity; + + pending.push_back(possible_narrow_cast(row.value)); + } + + std::unordered_set seen{}; + while (!pending.empty()) + { + const auto row = pending.back(); + pending.pop_back(); + if (!seen.insert(row).second) + continue; + + const auto edges = algorithm::from_column(columns.edges[row]); + const auto end = edges.first + edges.count; + for (auto input = edges.first; input < end; ++input) + { + const auto parent = algorithm::from_column(columns.rows[input]); + if (parent == algorithm::none) + continue; + + const auto cell = algorithm::from_column(columns.cells[parent]); + if (!algorithm::is_boundary(cell)) + pending.push_back(parent); + } + + const tx_link tx{ store_.pool.get_key(pool_link{ row }) }; + out.push_back({ row, tx.value, + algorithm::from_column(columns.totals[row]) }); + } + + std::ranges::sort(out, {}, &pooled_row::row); + return error::success; +} + TEMPLATE code CLASS::get_confirmable_fees(fee_rates& out) const NOEXCEPT { diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index c26757f6d..5aa984c5d 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -762,6 +762,10 @@ class query tx_links to_confirmable_parents(const tx_link& link) const NOEXCEPT; tx_links to_confirmable_children(const tx_link& link) const NOEXCEPT; + /// Rows of pooled txs and of their unconfirmed pooled ancestors, by row. + code get_pooled_ancestry(pooled_rows& out, + const tx_links& links) const NOEXCEPT; + /// Confirmable ancestors, or descendants, of tx (excluding tx). tx_links to_confirmable_ancestors(const tx_link& link) const NOEXCEPT; tx_links to_confirmable_descendants(const tx_link& link) const NOEXCEPT; diff --git a/include/bitcoin/database/types/pooled_tx.hpp b/include/bitcoin/database/types/pooled_tx.hpp index 634342942..3e916e5a0 100644 --- a/include/bitcoin/database/types/pooled_tx.hpp +++ b/include/bitcoin/database/types/pooled_tx.hpp @@ -52,6 +52,16 @@ struct template_tx using template_txs = std::vector; +/// Pool row of tx, with its totals. +struct pooled_row +{ + table::pool::link::integer row{}; + table::transaction::link::integer tx{}; + system::pool::algorithm::totals_t totals{}; +}; + +using pooled_rows = std::vector; + } // namespace database } // namespace libbitcoin diff --git a/test/query/pool/pool_confirmable.cpp b/test/query/pool/pool_confirmable.cpp index 1159f7e75..a2c7b00f4 100644 --- a/test/query/pool/pool_confirmable.cpp +++ b/test/query/pool/pool_confirmable.cpp @@ -420,6 +420,80 @@ BC_AUTO_TEST_CASE(query_pool__to_confirmable_children__conflicted_child__empty) BC_REQUIRE(query.to_confirmable_children(parent_link).empty()); } +// get_pooled_ancestry +// ---------------------------------------------------------------------------- + +BC_AUTO_TEST_CASE(query_pool__get_pooled_ancestry__pooled_chain__rows_by_row) +{ + 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 auto parent = to_spend(genesis_coinbase(), 3u); + const auto child = to_spend(parent.hash(false), 2u); + const auto grandchild = to_spend(child.hash(false), 1u); + BC_REQUIRE(archive_pool(query, parent)); + BC_REQUIRE(archive_pool(query, child)); + BC_REQUIRE(archive_pool(query, grandchild)); + + pooled_rows out{}; + const auto parent_link = query.to_tx(parent.hash(false)); + const auto child_link = query.to_tx(child.hash(false)); + const auto grandchild_link = query.to_tx(grandchild.hash(false)); + BC_REQUIRE_EQUAL(query.get_pooled_ancestry(out, { grandchild_link, child_link }), error::success); + BC_REQUIRE_EQUAL(out.size(), 3u); + BC_REQUIRE_EQUAL(out[0].tx, parent_link.value); + BC_REQUIRE_EQUAL(out[1].tx, child_link.value); + BC_REQUIRE_EQUAL(out[2].tx, grandchild_link.value); + BC_REQUIRE_EQUAL(out[0].row, query.to_pool_row(parent_link).value); + + test::pool_totals totals{}; + BC_REQUIRE(query.get_pooled_totals(totals, grandchild_link)); + BC_REQUIRE_EQUAL(out[2].totals.fee, totals.fee); + BC_REQUIRE_EQUAL(out[2].totals.size, totals.size); +} + +BC_AUTO_TEST_CASE(query_pool__get_pooled_ancestry__confirmed_parent__child_only) +{ + 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 auto parent = to_spend(genesis_coinbase(), 2u); + const auto child = to_spend(parent.hash(false), 1u); + BC_REQUIRE(archive_pool(query, parent)); + BC_REQUIRE(archive_pool(query, child)); + BC_REQUIRE(!archive_confirm(query, parent).is_terminal()); + + pooled_rows out{}; + const auto child_link = query.to_tx(child.hash(false)); + BC_REQUIRE_EQUAL(query.get_pooled_ancestry(out, { child_link }), error::success); + BC_REQUIRE_EQUAL(out.size(), 1u); + BC_REQUIRE_EQUAL(out[0].tx, child_link.value); +} + +BC_AUTO_TEST_CASE(query_pool__get_pooled_ancestry__not_pooled__integrity) +{ + 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(); + pooled_rows out{}; + BC_REQUIRE_EQUAL(query.get_pooled_ancestry(out, { 0 }), error::integrity); +} + // to_confirmable_ancestors BC_AUTO_TEST_CASE(query_pool__to_confirmable_ancestors__pooled_chain__ancestors) From 4a3fc332526fdab271c04abdaaf22cda91fe4492 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Mon, 23 Nov 2026 17:38:10 -0500 Subject: [PATCH 7/7] Pool a tx with its counted sigops, its spenders only if archived before. --- .../database/impl/query/pool/pool_writer.ipp | 28 ++++++++++++++----- include/bitcoin/database/query.hpp | 7 ++++- test/query/pool/pool_writer.cpp | 22 +++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/include/bitcoin/database/impl/query/pool/pool_writer.ipp b/include/bitcoin/database/impl/query/pool/pool_writer.ipp index 754f5cefa..1d12b12f5 100644 --- a/include/bitcoin/database/impl/query/pool/pool_writer.ipp +++ b/include/bitcoin/database/impl/query/pool/pool_writer.ipp @@ -44,14 +44,29 @@ bool CLASS::set_pooled(const tx_link& link, const transaction& tx, TEMPLATE bool CLASS::set_pooled(size_t& repooled, const tx_link& link, const transaction& tx, const context& ctx, uint32_t time) NOEXCEPT +{ + using namespace system::chain; + const auto bip16 = ctx.is_enabled(flags::bip16_rule); + const auto bip141 = ctx.is_enabled(flags::bip141_rule); + const auto sigops = tx.signature_operations(bip16, bip141); + return set_pooled(repooled, link, tx, ctx, time, sigops, true); +} + +TEMPLATE +bool CLASS::set_pooled(size_t& repooled, const tx_link& link, + const transaction& tx, const context& ctx, uint32_t time, size_t sigops, + bool archived) NOEXCEPT { repooled = zero; if (!store_.pool.enabled()) return true; + if (!set_pool_row(link, tx, ctx, time, sigops)) + return false; + tx_links purged{}; - if (!set_pool_row(link, tx, ctx, time) || !set_pool_purged(purged, link) || - !set_pool_repooled(purged)) + if (archived && (!set_pool_purged(purged, link) || + !set_pool_repooled(purged))) return false; repooled = purged.size(); @@ -61,7 +76,7 @@ bool CLASS::set_pooled(size_t& repooled, const tx_link& link, // protected TEMPLATE bool CLASS::set_pool_row(const tx_link& link, const transaction& tx, - const context& ctx, uint32_t time) NOEXCEPT + const context& ctx, uint32_t time, size_t sigops) NOEXCEPT { using namespace system; using algorithm = system::pool::algorithm; @@ -103,9 +118,6 @@ bool CLASS::set_pool_row(const tx_link& link, const transaction& tx, conflicted |= conflict; } - const auto bip16 = ctx.is_enabled(chain::flags::bip16_rule); - const auto bip141 = ctx.is_enabled(chain::flags::bip141_rule); - const auto sigops = tx.signature_operations(bip16, bip141); using lane_t = schema::pool::witness_lane; const auto hash = tx.get_hash(true); const auto words = from_little_endians(array_cast(hash)); @@ -254,9 +266,11 @@ bool CLASS::set_pool_repooled(const tx_links& purged) NOEXCEPT { const tx_link link{ value }; table::pool::record record{}; + table::pool_total total{}; table::pool_scalar flags{}, height{}, median{}; const auto row = store_.pool.first(link); if (!store_.pool.get(row, record) || + !store_.pool.totals.get(row, total) || !store_.pool.flags.get(row, flags) || !store_.pool.heights.get(row, height) || !store_.pool.medians.get(row, median)) @@ -267,7 +281,7 @@ bool CLASS::set_pool_repooled(const tx_links& purged) NOEXCEPT return false; const context ctx{ flags.value, height.value, median.value }; - if (!set_pool_row(link, *tx, ctx, record.time)) + if (!set_pool_row(link, *tx, ctx, record.time, total.sigops)) return false; } diff --git a/include/bitcoin/database/query.hpp b/include/bitcoin/database/query.hpp index 5aa984c5d..28f6e6c2c 100644 --- a/include/bitcoin/database/query.hpp +++ b/include/bitcoin/database/query.hpp @@ -796,6 +796,11 @@ class query bool set_pooled(size_t& repooled, const tx_link& link, const transaction& tx, const context& ctx, uint32_t time) NOEXCEPT; + /// As above with sigops counted, descendants only if archived before. + bool set_pooled(size_t& repooled, const tx_link& link, + const transaction& tx, const context& ctx, uint32_t time, + size_t sigops, bool archived) NOEXCEPT; + /// Rebuild pool index heads from pool (after compaction). bool set_pool_index() NOEXCEPT; @@ -1278,7 +1283,7 @@ class query /// Pool tx at a new row, its spenders not yet considered. bool set_pool_row(const tx_link& link, const transaction& tx, - const context& ctx, uint32_t time) NOEXCEPT; + const context& ctx, uint32_t time, size_t sigops) NOEXCEPT; /// Purge pooled spenders of tx and theirs, ascending by row. bool set_pool_purged(tx_links& out, const tx_link& link) NOEXCEPT; diff --git a/test/query/pool/pool_writer.cpp b/test/query/pool/pool_writer.cpp index 958ae2b61..6f64fe39d 100644 --- a/test/query/pool/pool_writer.cpp +++ b/test/query/pool/pool_writer.cpp @@ -69,6 +69,28 @@ BC_AUTO_TEST_CASE(query_pool__set_pooled__time__stored) BC_REQUIRE_EQUAL(record.time, 0x01020304u); } +BC_AUTO_TEST_CASE(query_pool__set_pooled__given_sigops__stored) +{ + 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)); + + const auto& tx = test::tx_spend_one_hash; + const auto& in = *tx.inputs_ptr()->front(); + in.metadata.parent_tx = 42; + in.metadata.coinbase = false; + size_t repooled{ 42 }; + BC_REQUIRE(query.set_pooled(repooled, 1, tx, context{ bip113, 8, 9 }, 0, 7, false)); + BC_REQUIRE_EQUAL(repooled, 0u); + + test::pool_totals totals{}; + BC_REQUIRE(query.get_pooled_totals(totals, 1)); + BC_REQUIRE_EQUAL(totals.sigops, 7u); +} + BC_AUTO_TEST_CASE(query_pool__set_pooled__unlinked_parent__resolved) { settings settings{ test::database_settings() };