From 5e1d35f92f4a97bfaa8e128fb7201debc392f2c7 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sun, 22 Nov 2026 01:04:13 -0500 Subject: [PATCH 1/3] Archive the coinbase of each compact block. --- src/chasers/chaser_transaction_compact.cpp | 5 +++-- test/chasers/chaser_transaction_compact.cpp | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/chasers/chaser_transaction_compact.cpp b/src/chasers/chaser_transaction_compact.cpp index e15d850f..59e56d06 100644 --- a/src/chasers/chaser_transaction_compact.cpp +++ b/src/chasers/chaser_transaction_compact.cpp @@ -197,14 +197,15 @@ void chaser_transaction::do_archive_block( } // The stored tx, or validated and archived, pooled under the block context. +// Each block archives its own coinbase. code chaser_transaction::fill(database::tx_link& out, const chain::transaction& tx, const chain::context& ctx) NOEXCEPT { auto& query = archive(); - out = to_stored(tx); + out = tx.is_coinbase() ? database::tx_link{} : to_stored(tx); if (!out.is_terminal()) { - if (tx.is_coinbase() || !query.is_pooling() || !reusable(ctx) || + if (!query.is_pooling() || !reusable(ctx) || query.is_pooled(out) || query.is_confirmed_tx(out)) return error::success; diff --git a/test/chasers/chaser_transaction_compact.cpp b/test/chasers/chaser_transaction_compact.cpp index 5e77dabd..76de995b 100644 --- a/test/chasers/chaser_transaction_compact.cpp +++ b/test/chasers/chaser_transaction_compact.cpp @@ -47,6 +47,21 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__stored_unpooled_tx__poo BC_REQUIRE_EQUAL(query_.to_tx(tx->hash(false)), stored); } +BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__stored_coinbase__archived_again, chaser_transaction_header_setup_fixture) +{ + const auto& block1 = p2p_compact_setup_fixture::block1(); + const auto link = query_.to_header(block1.hash()); + const auto coinbase = block1.transactions_ptr()->front(); + BC_REQUIRE(query_.set(*coinbase)); + + const auto stored = query_.to_tx(coinbase->hash(false)); + const auto result = submit_compact(package({ coinbase }), { database::tx_link::terminal }, link); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(query_.is_associated(link)); + BC_REQUIRE_EQUAL(query_.to_duplicates(coinbase->hash(false)).size(), 2u); + BC_REQUIRE(query_.to_tx(coinbase->hash(false)) != stored); +} + BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__not_pooling__pooling_disabled, chaser_transaction_closed_header_setup_fixture) { const auto link = query_.to_header(p2p_compact_setup_fixture::block1().hash()); From 98edac28482b73cd8a05a54c07e0524baf6eb566 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sun, 22 Nov 2026 01:04:13 -0500 Subject: [PATCH 2/3] Revalidate the spenders of a reorganized block by admission. --- .../node/chasers/chaser_transaction.hpp | 5 ++- src/chasers/chaser_transaction.cpp | 43 +++++++++++++++---- src/chasers/chaser_transaction_submit.cpp | 15 ++++--- test/chasers/chaser_transaction.cpp | 39 +++++++++++++++++ 4 files changed, 86 insertions(+), 16 deletions(-) diff --git a/include/bitcoin/node/chasers/chaser_transaction.hpp b/include/bitcoin/node/chasers/chaser_transaction.hpp index 6433232c..4626f861 100644 --- a/include/bitcoin/node/chasers/chaser_transaction.hpp +++ b/include/bitcoin/node/chasers/chaser_transaction.hpp @@ -211,7 +211,8 @@ class BCN_API chaser_transaction code check(const system::chain::transaction_cptrs& txs, const std::vector& stored, bool announced) NOEXCEPT; - code accept(const system::chain::transaction& tx) NOEXCEPT; + code accept(const system::chain::transaction& tx, + const system::chain::context& ctx) NOEXCEPT; code admit(database::tx_link& out, const system::chain::transaction& tx, uint32_t time) NOEXCEPT; code validate(const system::chain::transaction& tx, @@ -278,7 +279,9 @@ class BCN_API chaser_transaction bool due_{ true }; bool scanning_{}; bool revalidating_{}; + bool unvalidated_{}; uint32_t revalidated_flags_{}; + system::chain::context retried_{}; std::vector marks_{}; std::array rates_{}; }; diff --git a/src/chasers/chaser_transaction.cpp b/src/chasers/chaser_transaction.cpp index e37a869a..d47136e2 100644 --- a/src/chasers/chaser_transaction.cpp +++ b/src/chasers/chaser_transaction.cpp @@ -249,23 +249,35 @@ bool chaser_transaction::sweep() NOEXCEPT return applied; } -// Rows left out by flags are revalidated once per change of flags, so a row -// that fails is not retried until the flags change again. +// Rows left out by flags are revalidated once per change of flags, and rows +// without a context once per change of the pool context, so a row that fails +// is retried only under another context. void chaser_transaction::start_revalidation() NOEXCEPT { BC_ASSERT(stranded()); - if (revalidating_ || pool_.flags == revalidated_flags_) + if (revalidating_) return; + const auto flagged = pool_.flags != revalidated_flags_; + const auto moved = unvalidated_ || pool_.height != retried_.height || + pool_.median_time_past != retried_.median_time_past; + + if (!flagged && !moved) + return; + + auto& query = archive(); database::tx_links links{}; const auto ctx = database::context::from(pool_); - if (const auto ec = archive().get_pool_revalidation(links, ctx)) + if (const auto ec = flagged ? query.get_pool_revalidation(links, ctx) : + query.get_pool_unvalidated(links)) { fault(ec); return; } revalidated_flags_ = pool_.flags; + retried_ = pool_; + unvalidated_ = false; if (links.empty()) return; @@ -274,6 +286,8 @@ void chaser_transaction::start_revalidation() NOEXCEPT BIND(do_revalidate, std::move(links), pool_)); } +// Revalidation is admission under the context, as confirmation of what a row +// spends may differ from when it was pooled. void chaser_transaction::do_revalidate(const database::tx_links& links, const chain::context& ctx) NOEXCEPT { @@ -288,13 +302,15 @@ void chaser_transaction::do_revalidate(const database::tx_links& links, const auto offset = std::distance(links.data(), &link); const auto at = sign_cast(offset); const auto tx = query.get_transaction(link, true); - passed.at(at) = to_int(tx && !validate(*tx, ctx)); + passed.at(at) = to_int(tx && !accept(*tx, ctx) && + !tx->connect(ctx)); }); POST(do_revalidated, links, std::move(passed), ctx); } -// The pool context may have moved under the revalidation. +// The pool context may have moved under the revalidation, and a reorganization +// under it may have unvalidated a row that it passed. void chaser_transaction::do_revalidated(const database::tx_links& links, const data_chunk& passed, const chain::context& ctx) NOEXCEPT { @@ -303,9 +319,14 @@ void chaser_transaction::do_revalidated(const database::tx_links& links, if (closed()) return; - if (!is_sufficient(ctx)) + if (unvalidated_ || !is_sufficient(ctx)) { revalidated_flags_ = {}; + unvalidated_ = true; + due_ = true; + if (!sweep()) + fault(error::transaction11); + return; } @@ -418,14 +439,20 @@ void chaser_transaction::do_reorganized(header_t link) NOEXCEPT auto& query = archive(); if (is_indexing()) { + size_t count{}; const auto start = now(); - const auto applied = query.set_pool_block(link, false); + const auto applied = query.set_pool_block(link, false) && + query.set_pool_unvalidated(count, link); + report(pool_scan::block_out, start); if (!applied) { fault(error::transaction10); return; } + + unvalidated_ = unvalidated_ || is_nonzero(count); + due_ = due_ || is_nonzero(count); } do_bump(); diff --git a/src/chasers/chaser_transaction_submit.cpp b/src/chasers/chaser_transaction_submit.cpp index 10f33a9d..ea95ff1b 100644 --- a/src/chasers/chaser_transaction_submit.cpp +++ b/src/chasers/chaser_transaction_submit.cpp @@ -162,7 +162,7 @@ void chaser_transaction::do_submit(const transactions_cptr& txs, bool test, continue; const auto& tx = *txs->at(index); - auto ec = accept(tx); + auto ec = accept(tx, pool_); if (!ec && !spends_held(set, tx)) ec = system::error::missing_previous_output; @@ -458,7 +458,8 @@ code chaser_transaction::admit(database::tx_link& link, return error::success; } -code chaser_transaction::accept(const chain::transaction& tx) NOEXCEPT +code chaser_transaction::accept(const chain::transaction& tx, + const chain::context& ctx) NOEXCEPT { code ec{}; @@ -466,15 +467,15 @@ code chaser_transaction::accept(const chain::transaction& tx) NOEXCEPT // Guards ensure tx does not violate presumed block consensus rules. // These are DoS guards when validating a tx outside of a block. if (!ec) ec = tx.check(); - if (!ec) ec = tx.check(pool_); + if (!ec) ec = tx.check(ctx); if (!ec) ec = tx.check_guard(); - if (!ec) ec = tx.check_guard(pool_); + if (!ec) ec = tx.check_guard(ctx); // Store population follows all checks that do not require it. if (!ec) archive().populate_with_metadata(tx, true, true); - if (!ec) ec = tx.accept(pool_); - if (!ec) ec = tx.accept_guard(pool_); - if (!ec) ec = tx.confirm_guard(pool_); + if (!ec) ec = tx.accept(ctx); + if (!ec) ec = tx.accept_guard(ctx); + if (!ec) ec = tx.confirm_guard(ctx); // An unconfirmed conflict is admitted by coverage. return ec == system::error::double_spend ? error::success : ec; diff --git a/test/chasers/chaser_transaction.cpp b/test/chasers/chaser_transaction.cpp index cdc32402..ede27648 100644 --- a/test/chasers/chaser_transaction.cpp +++ b/test/chasers/chaser_transaction.cpp @@ -234,6 +234,45 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__reorganized__unpooled_block_tx__pooled, BC_REQUIRE_EQUAL(record.time, block.header().timestamp()); } +BC_FIXTURE_TEST_CASE(chaser_transaction__reorganized__relative_spender_of_reconfirmed_parent__revalidated, chaser_transaction_relative_setup_fixture) +{ + constexpr auto insufficient = bit_or(pool::algorithm::absent, pool::algorithm::insufficient); + const auto block2 = confirm(); + const auto raised = bumped(3); + const auto tx = spend_relative(parent().hash(false), parent_value - 1'000, 2); + const auto result = submit(package({ tx })); + const auto link = query_.to_tx(tx->hash(false)); + const auto before = cell(link); + const auto popped2 = query_.pop_confirmed(); + node_.notify(node::error::success, chases::reorganized{ block2 }); + const auto lowered2 = bumped(2); + const auto block1 = query_.to_header(parent_block().hash()); + const auto popped1 = query_.pop_confirmed(); + node_.notify(node::error::success, chases::reorganized{ block1 }); + const auto lowered1 = bumped(1); + const auto other1 = confirm(); + const auto other2 = confirm(to_shared(parent())); + const auto regained = bumped(3); + const auto locked = cell(link); + const auto other3 = confirm(); + const auto unlocked = celled(link, pool::algorithm::unlabeled); + + BC_REQUIRE(!block2.is_terminal()); + BC_REQUIRE(raised); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(pool::algorithm::is_present(before)); + BC_REQUIRE(popped2); + BC_REQUIRE(lowered2); + BC_REQUIRE(popped1); + BC_REQUIRE(lowered1); + BC_REQUIRE(!other1.is_terminal()); + BC_REQUIRE(!other2.is_terminal()); + BC_REQUIRE(regained); + BC_REQUIRE_EQUAL(locked, insufficient); + BC_REQUIRE(!other3.is_terminal()); + BC_REQUIRE(unlocked); +} + BC_AUTO_TEST_SUITE_END() BC_AUTO_TEST_SUITE(chaser_transaction_scan_tests) From d53f7776b30762f1f8779c0b300bcb1b5975b243 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sun, 22 Nov 2026 01:48:37 -0500 Subject: [PATCH 3/3] Run the tx chaser on its own thread, at pool thread priority. --- .../node/chasers/chaser_transaction.hpp | 8 ++++++++ include/bitcoin/node/settings.hpp | 2 ++ src/chasers/chaser_transaction.cpp | 20 ++++++++++++++++++- src/settings.cpp | 10 +++++++++- test/settings.cpp | 9 +++++++++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/include/bitcoin/node/chasers/chaser_transaction.hpp b/include/bitcoin/node/chasers/chaser_transaction.hpp index 4626f861..8b7a7b2c 100644 --- a/include/bitcoin/node/chasers/chaser_transaction.hpp +++ b/include/bitcoin/node/chasers/chaser_transaction.hpp @@ -173,6 +173,10 @@ class BCN_API chaser_transaction const system::data_chunk& passed, const system::chain::context& ctx) NOEXCEPT; + // Override base class strand because it sits on the network thread pool. + network::io::strand& strand() NOEXCEPT override; + bool stranded() const NOEXCEPT override; + private: /// A set under submission, its running set and outcome. struct submission @@ -262,7 +266,11 @@ class BCN_API chaser_transaction /// Fire the event with the microseconds since the start. void fire_span(events event, const time_point& start) NOEXCEPT; + // This is not thread safe. + network::threadpool transaction_threadpool_; + // These are thread safe. + network::io::strand transaction_strand_; network::threadpool scan_threadpool_; network::threadpool revalidation_threadpool_; stopper stopping_{}; diff --git a/include/bitcoin/node/settings.hpp b/include/bitcoin/node/settings.hpp index f747c149..ef270e6b 100644 --- a/include/bitcoin/node/settings.hpp +++ b/include/bitcoin/node/settings.hpp @@ -46,10 +46,12 @@ class BCN_API settings uint32_t bytes_per_sigop; uint32_t coinbase_weight; uint32_t maximum_chunk_depth; + bool thread_priority; /// Helpers. virtual uint64_t minimum_fee_rate_() const NOEXCEPT; virtual uint64_t cutoff_rate_() const NOEXCEPT; + virtual network::processing_priority thread_priority_() const NOEXCEPT; }; /// The network of the defaults. diff --git a/src/chasers/chaser_transaction.cpp b/src/chasers/chaser_transaction.cpp index d47136e2..717b4817 100644 --- a/src/chasers/chaser_transaction.cpp +++ b/src/chasers/chaser_transaction.cpp @@ -34,8 +34,11 @@ using namespace std::placeholders; BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR) +// Independent threadpool and strand (base class strand uses network pool). chaser_transaction::chaser_transaction(full_node& node) NOEXCEPT : chaser(node), + transaction_threadpool_(one, node.node_settings().pool.thread_priority_()), + transaction_strand_(transaction_threadpool_.service().get_executor()), scan_threadpool_(one, node.node_settings().thread_priority_()), revalidation_threadpool_(one, node.node_settings().thread_priority_()), outbound_excluded_(unmask_right( @@ -61,13 +64,15 @@ void chaser_transaction::stopping(const code& ec) NOEXCEPT stopping_.store(true); scan_threadpool_.stop(); revalidation_threadpool_.stop(); + transaction_threadpool_.stop(); chaser::stopping(ec); } // The strand is no longer running when the chaser is stopped. void chaser_transaction::stop() NOEXCEPT { - if (!scan_threadpool_.join() || !revalidation_threadpool_.join()) + if (!scan_threadpool_.join() || !revalidation_threadpool_.join() || + !transaction_threadpool_.join()) { BC_ASSERT_MSG(false, "failed to join threadpool"); std::abort(); @@ -631,6 +636,19 @@ pool::algorithm::totals_t chaser_transaction::block_limit() const NOEXCEPT }; } +// Overrides due to independent priority thread pool +// ---------------------------------------------------------------------------- + +network::io::strand& chaser_transaction::strand() NOEXCEPT +{ + return transaction_strand_; +} + +bool chaser_transaction::stranded() const NOEXCEPT +{ + return transaction_strand_.running_in_this_thread(); +} + BC_POP_WARNING() BC_POP_WARNING() diff --git a/src/settings.cpp b/src/settings.cpp index 1853c4fc..3ba4f362 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -33,7 +33,8 @@ settings::transaction_pool::transaction_pool() NOEXCEPT maximum_outcomes{ 1'024 }, bytes_per_sigop{ 20 }, coinbase_weight{ 2'000 }, - maximum_chunk_depth{ 25 } + maximum_chunk_depth{ 25 }, + thread_priority{ true } { } @@ -52,6 +53,13 @@ uint64_t settings::transaction_pool::cutoff_rate_() const NOEXCEPT chain::satoshi_per_bitcoin); } +network::processing_priority +settings::transaction_pool::thread_priority_() const NOEXCEPT +{ + return thread_priority ? network::processing_priority::high : + network::processing_priority::medium; +} + settings::settings() NOEXCEPT : network{ chain::selection::none }, pool{}, diff --git a/test/settings.cpp b/test/settings.cpp index dfb5b4c9..191fed5f 100644 --- a/test/settings.cpp +++ b/test/settings.cpp @@ -216,6 +216,15 @@ BC_AUTO_TEST_CASE(settings__pool__default__expected) BC_REQUIRE_EQUAL(pool.bytes_per_sigop, 20_u32); BC_REQUIRE_EQUAL(pool.coinbase_weight, 2'000_u32); BC_REQUIRE_EQUAL(pool.maximum_chunk_depth, 25_u32); + BC_REQUIRE(pool.thread_priority); + BC_REQUIRE(pool.thread_priority_() == network::processing_priority::high); +} + +BC_AUTO_TEST_CASE(settings__pool__thread_priority_false__medium) +{ + node::settings::transaction_pool pool{}; + pool.thread_priority = false; + BC_REQUIRE(pool.thread_priority_() == network::processing_priority::medium); } BC_AUTO_TEST_SUITE_END()