diff --git a/include/bitcoin/node/chasers/chaser_transaction.hpp b/include/bitcoin/node/chasers/chaser_transaction.hpp index 8b7a7b2c..49995b40 100644 --- a/include/bitcoin/node/chasers/chaser_transaction.hpp +++ b/include/bitcoin/node/chasers/chaser_transaction.hpp @@ -91,6 +91,9 @@ class BCN_API chaser_transaction /// Reserve a free slot of the tier, relay_slots if none (thread safe). virtual size_t reserve_relay(relay_tier tier) NOEXCEPT; + /// Start relay to the slot, once its channel is notified of relay. + virtual void activate_relay(size_t slot) NOEXCEPT; + /// Release the relay slot, free once cleared from the marks. virtual void release_relay(size_t slot) NOEXCEPT; @@ -139,12 +142,16 @@ class BCN_API chaser_transaction virtual void do_subscribe_pool(const pool_notifier& handler) NOEXCEPT; /// Relay slot management. + virtual void do_activate_relay(size_t slot) NOEXCEPT; virtual void do_release_relay(size_t slot) NOEXCEPT; virtual void do_set_relay_rate(size_t slot, uint64_t rate) NOEXCEPT; virtual void do_set_relayed(size_t slot, const database::tx_links& links) NOEXCEPT; virtual void do_relay_pool(size_t slot) NOEXCEPT; + /// Sweep the pool to one slot owed a sweep, then post for the next. + virtual void do_sweep_relay() NOEXCEPT; + /// Apply the organized block to the pool index, then bump. virtual void do_organized(header_t link) NOEXCEPT; @@ -170,7 +177,7 @@ class BCN_API chaser_transaction /// Restore the pooled txs that passed, if the context still holds. virtual void do_revalidated(const database::tx_links& links, - const system::data_chunk& passed, + const system::data_chunk& passed, const std::vector& sigops, const system::chain::context& ctx) NOEXCEPT; // Override base class strand because it sits on the network thread pool. @@ -203,9 +210,11 @@ class BCN_API chaser_transaction system::pool::algorithm::totals_t block_limit() const NOEXCEPT; /// Submission steps. - void hold(submission& set) const NOEXCEPT; + void hold(submission& set) NOEXCEPT; bool spends_held(const submission& set, const system::chain::transaction& tx) const NOEXCEPT; + bool spends_held(const system::chain::transaction& tx) const NOEXCEPT; + bool is_held(const system::chain::input& in) const NOEXCEPT; void pass_over(submission& set, size_t index, const code& ec) NOEXCEPT; void count(submission& set, const system::chain::transaction& tx) const NOEXCEPT; @@ -219,12 +228,15 @@ class BCN_API chaser_transaction 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, + code validate(bool& guarded, const system::chain::transaction& tx, const system::chain::context& ctx) NOEXCEPT; database::tx_link to_stored( const system::chain::transaction& tx) const NOEXCEPT; code fill(database::tx_link& out, const system::chain::transaction& tx, const system::chain::context& ctx) NOEXCEPT; + bool set_pooled(const database::tx_link& link, + const system::chain::transaction& tx, + const system::chain::context& ctx) NOEXCEPT; code unconfirmable(const database::header_link& link, size_t height, const code& ec) NOEXCEPT; void broadcast(const system::chain::transaction& tx) const NOEXCEPT; @@ -240,6 +252,13 @@ class BCN_API chaser_transaction /// slot lacks, where that clears the slot's rate. bool sweep_relay(size_t slot) NOEXCEPT; + /// Owe the slot a sweep, made at once unless it was swept since the last + /// block, and otherwise after the next. + void owe_relay(size_t slot) NOEXCEPT; + + /// The relaying slots owed a sweep and not swept since the last block. + uint64_t sweepable() const NOEXCEPT; + /// The marks of the pool row, grown with the table. uint64_t& marks(size_t row) NOEXCEPT; @@ -283,15 +302,24 @@ class BCN_API chaser_transaction // These are protected by strand. pool_subscriber subscriber_{}; system::chain::context pool_{}; + database::header_link top_{}; size_t floor_{}; bool due_{ true }; bool scanning_{}; bool revalidating_{}; bool unvalidated_{}; - uint32_t revalidated_flags_{}; + bool estimating_{}; + bool arrived_{}; + time_point estimated_{}; + query::pool_projection projection_{}; + query::pool_arrivals arrivals_{}; system::chain::context retried_{}; std::vector marks_{}; std::array rates_{}; + uint64_t relaying_{}; + uint64_t owed_{}; + uint64_t swept_{}; + bool sweeping_{}; }; } // namespace node diff --git a/include/bitcoin/node/full_node.hpp b/include/bitcoin/node/full_node.hpp index 852796b3..da9b28d4 100644 --- a/include/bitcoin/node/full_node.hpp +++ b/include/bitcoin/node/full_node.hpp @@ -126,8 +126,9 @@ class BCN_API full_node virtual bool reserve_compact() NOEXCEPT; virtual void release_compact() NOEXCEPT; - /// Reserve (relay_slots if none) or release a relay slot. + /// Reserve (relay_slots if none), activate or release a relay slot. virtual size_t reserve_relay(relay_tier tier) NOEXCEPT; + virtual void activate_relay(size_t slot) NOEXCEPT; virtual void release_relay(size_t slot) NOEXCEPT; /// Set the fee rate of the relay slot's peer. diff --git a/include/bitcoin/node/protocols/protocol_peer.hpp b/include/bitcoin/node/protocols/protocol_peer.hpp index 0fb5580f..92890999 100644 --- a/include/bitcoin/node/protocols/protocol_peer.hpp +++ b/include/bitcoin/node/protocols/protocol_peer.hpp @@ -79,8 +79,10 @@ class BCN_API protocol_peer /// The memory pool of the peer is requested once relaying. virtual bool request_mempool() const NOEXCEPT; - /// Reserve or release the channel's relay slot, false if none is free. + /// Reserve, activate or release the channel's relay slot, reserve false + /// if none is free. virtual bool reserve_relay() NOEXCEPT; + virtual void activate_relay() NOEXCEPT; virtual void release_relay() NOEXCEPT; /// The channel's relay slot, relay_slots if none. diff --git a/include/bitcoin/node/protocols/protocol_transaction_out_106.hpp b/include/bitcoin/node/protocols/protocol_transaction_out_106.hpp index 7820fef5..4fd9d821 100644 --- a/include/bitcoin/node/protocols/protocol_transaction_out_106.hpp +++ b/include/bitcoin/node/protocols/protocol_transaction_out_106.hpp @@ -63,6 +63,7 @@ class BCN_API protocol_transaction_out_106 /// Handle chaser events. virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT; + void subscribed(const code& ec, object_key key) NOEXCEPT override; /// Announce the relayed tx if the channel's slot is in the word. virtual bool do_announce(transaction_t link, uint64_t slots) NOEXCEPT; diff --git a/include/bitcoin/node/protocols/protocol_transaction_out_70013.hpp b/include/bitcoin/node/protocols/protocol_transaction_out_70013.hpp index 950ad40c..f23a3a39 100644 --- a/include/bitcoin/node/protocols/protocol_transaction_out_70013.hpp +++ b/include/bitcoin/node/protocols/protocol_transaction_out_70013.hpp @@ -45,6 +45,7 @@ class BCN_API protocol_transaction_out_70013 protected: /// Handle chaser events. bool handle_chase(const code& ec, event_value value) NOEXCEPT override; + void subscribed(const code& ec, object_key key) NOEXCEPT override; /// Capture the peer's minimum fee rate for announcements. virtual bool handle_receive_fee_filter(const code& ec, diff --git a/include/bitcoin/node/sessions/session.hpp b/include/bitcoin/node/sessions/session.hpp index ba7095aa..34362ae0 100644 --- a/include/bitcoin/node/sessions/session.hpp +++ b/include/bitcoin/node/sessions/session.hpp @@ -100,8 +100,9 @@ class BCN_API session /// The relay slot tier of the session's peers. virtual relay_tier relay_tier_() const NOEXCEPT; - /// Reserve (relay_slots if none) or release a relay slot. + /// Reserve (relay_slots if none), activate or release a relay slot. virtual size_t reserve_relay() NOEXCEPT; + virtual void activate_relay(size_t slot) NOEXCEPT; virtual void release_relay(size_t slot) NOEXCEPT; /// Set the fee rate of the relay slot's peer. diff --git a/include/bitcoin/node/settings.hpp b/include/bitcoin/node/settings.hpp index ef270e6b..bb6cac60 100644 --- a/include/bitcoin/node/settings.hpp +++ b/include/bitcoin/node/settings.hpp @@ -45,12 +45,15 @@ class BCN_API settings uint32_t maximum_outcomes; uint32_t bytes_per_sigop; uint32_t coinbase_weight; + uint32_t coinbase_sigops; uint32_t maximum_chunk_depth; + uint16_t estimate_cache_seconds; bool thread_priority; /// Helpers. virtual uint64_t minimum_fee_rate_() const NOEXCEPT; virtual uint64_t cutoff_rate_() const NOEXCEPT; + virtual network::steady_clock::duration estimate_cache() const NOEXCEPT; virtual network::processing_priority thread_priority_() const NOEXCEPT; }; diff --git a/src/chasers/chaser_transaction.cpp b/src/chasers/chaser_transaction.cpp index 717b4817..b5be00ce 100644 --- a/src/chasers/chaser_transaction.cpp +++ b/src/chasers/chaser_transaction.cpp @@ -145,6 +145,7 @@ bool chaser_transaction::handle_chase(const code&, event_value value) NOEXCEPT void chaser_transaction::do_bump() NOEXCEPT { BC_ASSERT(stranded()); + estimating_ = false; const auto opened = pooling_.load(); pooling_ = false; if (closed() || !is_current_chain(true)) @@ -154,8 +155,10 @@ void chaser_transaction::do_bump() NOEXCEPT auto& query = archive(); if (network_settings().enable_relay) { + // The context is of the block the index was last applied through, as + // the store's top may be above it. size_t height{}; - const auto top = query.to_top_confirmed(); + const auto top = top_.is_terminal() ? query.to_top_confirmed() : top_; if (!query.get_height(height, top)) { fault(error::transaction9); @@ -195,6 +198,11 @@ void chaser_transaction::do_bump() NOEXCEPT // Rows that arrived while the pool was closed are not in it. if (!opened) floor_ = std::max(floor_, pool_.height); + + // Sweeps deferred to the block are now made. + swept_ = {}; + if (!sweeping_) + do_sweep_relay(); } } @@ -255,15 +263,15 @@ bool chaser_transaction::sweep() NOEXCEPT } // 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. +// not validated 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_) return; - const auto flagged = pool_.flags != revalidated_flags_; + const auto flagged = pool_.flags != retried_.flags; const auto moved = unvalidated_ || pool_.height != retried_.height || pool_.median_time_past != retried_.median_time_past; @@ -272,15 +280,23 @@ void chaser_transaction::start_revalidation() NOEXCEPT auto& query = archive(); database::tx_links links{}; + database::tx_links unvalidated{}; const auto ctx = database::context::from(pool_); - if (const auto ec = flagged ? query.get_pool_revalidation(links, ctx) : - query.get_pool_unvalidated(links)) + code ec{}; + if (flagged) + ec = query.get_pool_revalidation(links, ctx); + + if (!ec) + ec = query.get_pool_unvalidated(unvalidated); + + if (ec) { fault(ec); return; } - revalidated_flags_ = pool_.flags; + links.insert(links.end(), unvalidated.begin(), unvalidated.end()); + retried_ = pool_; unvalidated_ = false; if (links.empty()) @@ -292,12 +308,16 @@ void chaser_transaction::start_revalidation() NOEXCEPT } // Revalidation is admission under the context, as confirmation of what a row -// spends may differ from when it was pooled. +// spends may differ from when it was pooled. Sigops are counted under the +// flags of the context, so a tx that passes is counted again. void chaser_transaction::do_revalidate(const database::tx_links& links, const chain::context& ctx) NOEXCEPT { const auto& query = archive(); + const auto bip16 = ctx.is_enabled(flags::bip16_rule); + const auto bip141 = ctx.is_enabled(flags::bip141_rule); data_chunk passed(links.size()); + std::vector sigops(links.size()); std::for_each(poolstl::execution::par, links.cbegin(), links.cend(), [&](const auto& link) NOEXCEPT { @@ -307,17 +327,22 @@ 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 && !accept(*tx, ctx) && - !tx->connect(ctx)); + if (!tx || accept(*tx, ctx) || !spends_held(*tx) || + tx->connect(ctx)) + return; + + sigops.at(at) = tx->signature_operations(bip16, bip141); + passed.at(at) = one; }); - POST(do_revalidated, links, std::move(passed), ctx); + POST(do_revalidated, links, std::move(passed), std::move(sigops), ctx); } // 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 + const data_chunk& passed, const std::vector& sigops, + const chain::context& ctx) NOEXCEPT { BC_ASSERT(stranded()); revalidating_ = false; @@ -326,7 +351,7 @@ void chaser_transaction::do_revalidated(const database::tx_links& links, if (unvalidated_ || !is_sufficient(ctx)) { - revalidated_flags_ = {}; + retried_.flags = {}; unvalidated_ = true; due_ = true; if (!sweep()) @@ -343,7 +368,8 @@ void chaser_transaction::do_revalidated(const database::tx_links& links, if (is_zero(passed.at(at))) continue; - if (!query.set_pooled_context(links.at(at), revalidated)) + if (!query.set_pooled_context(links.at(at), revalidated, + sigops.at(at))) { fault(error::transaction2); return; @@ -419,11 +445,12 @@ void chaser_transaction::do_organized(header_t link) NOEXCEPT fire(events::pool_rows, query.pool_records()); } + top_ = link; do_bump(); } // The block was organized before it was announced, so its transition has -// been applied, and its prevout cache is no longer required by the pool. +// been applied. void chaser_transaction::do_block(header_t link) NOEXCEPT { BC_ASSERT(stranded()); @@ -460,6 +487,7 @@ void chaser_transaction::do_reorganized(header_t link) NOEXCEPT due_ = due_ || is_nonzero(count); } + top_ = query.to_parent(link); do_bump(); if (!pooling_) return; @@ -490,16 +518,12 @@ void chaser_transaction::do_reorganized(header_t link) NOEXCEPT } // Block txs are already silent indexed. - const auto admit = now(); - const auto pooled = query.set_pooled(fk, *tx, ctx, ctx.timestamp); - report(pool_scan::admission, admit); - if (!pooled) + if (!set_pooled(fk, *tx, ctx)) { fault(error::transaction2); return; } - due_ = due_ || !is_sufficient(ctx); notify(error::success, chases::transaction{ fk }); broadcast(*tx); } @@ -523,6 +547,7 @@ void chaser_transaction::do_pruned(header_t link) NOEXCEPT } floor_ = std::max(floor_, add1(height)); + estimating_ = false; } // utility @@ -617,7 +642,8 @@ void chaser_transaction::broadcast(const chain::transaction& tx) const NOEXCEPT broadcast(emplace_shared(view)); } -// A block less its header, largest tx count and declared coinbase weight. +// A block less its header, largest tx count and declared coinbase weight and +// sigops. pool::algorithm::totals_t chaser_transaction::block_limit() const NOEXCEPT { constexpr auto count = variable_size(max_block_size / min_transaction_size); @@ -627,7 +653,8 @@ pool::algorithm::totals_t chaser_transaction::block_limit() const NOEXCEPT const auto reserved = ceilinged_add(fixed, settings.coinbase_weight); const auto weight = floored_subtract(max_block_weight, reserved); const auto bip141 = pool_.is_enabled(flags::bip141_rule); - const auto sigops = bip141 ? max_fast_sigops : max_block_sigops; + const auto limit = bip141 ? max_fast_sigops : max_block_sigops; + const auto sigops = floored_subtract(limit, settings.coinbase_sigops); return { {}, diff --git a/src/chasers/chaser_transaction_compact.cpp b/src/chasers/chaser_transaction_compact.cpp index 59e56d06..e4283256 100644 --- a/src/chasers/chaser_transaction_compact.cpp +++ b/src/chasers/chaser_transaction_compact.cpp @@ -196,11 +196,12 @@ void chaser_transaction::do_archive_block( handler(query.set_code(*block, link, strong, bypass, height, prune)); } -// The stored tx, or validated and archived, pooled under the block context. -// Each block archives its own coinbase. +// The stored tx, or validated and archived, pooled under the block context if +// guarded. Each block archives its own coinbase. code chaser_transaction::fill(database::tx_link& out, const chain::transaction& tx, const chain::context& ctx) NOEXCEPT { + bool guarded{}; auto& query = archive(); out = tx.is_coinbase() ? database::tx_link{} : to_stored(tx); if (!out.is_terminal()) @@ -209,26 +210,25 @@ code chaser_transaction::fill(database::tx_link& out, query.is_pooled(out) || query.is_confirmed_tx(out)) return error::success; - if (const auto ec = validate(tx, ctx)) + if (const auto ec = validate(guarded, tx, ctx)) return ec; - const auto admit = now(); - const auto admitted = query.set_pooled(out, tx, ctx, ctx.timestamp); - report(pool_scan::admission, admit); - if (!admitted) + if (!guarded) + return error::success; + + if (!set_pooled(out, tx, ctx)) return fault(error::transaction2); if (ctx.height >= query.silent_start_height() && ctx.is_enabled(flags::bip341_rule) && !query.set_silent(out, tx)) return fault(error::transaction6); - due_ = due_ || !is_sufficient(ctx); notify(error::success, chases::transaction{ out }); broadcast(tx); return error::success; } - if (const auto ec = validate(tx, ctx)) + if (const auto ec = validate(guarded, tx, ctx)) return ec; bool pooled{}; @@ -240,18 +240,8 @@ code chaser_transaction::fill(database::tx_link& out, const auto coinbase = tx.is_coinbase(); if (!pooled && !coinbase) { - if (reusable(ctx)) - { - const auto admit = now(); - const auto admitted = query.set_pooled(out, tx, ctx, - ctx.timestamp); - - report(pool_scan::admission, admit); - if (!admitted) - return fault(error::transaction2); - - due_ = due_ || !is_sufficient(ctx); - } + if (guarded && reusable(ctx) && !set_pooled(out, tx, ctx)) + return fault(error::transaction2); if (ctx.height >= query.silent_start_height() && ctx.is_enabled(flags::bip341_rule) && !query.set_silent(out, tx)) @@ -268,6 +258,17 @@ code chaser_transaction::fill(database::tx_link& out, return error::success; } +// Pooled at the block's time, a sweep due if its context does not suffice. +bool chaser_transaction::set_pooled(const database::tx_link& link, + const chain::transaction& tx, const chain::context& ctx) NOEXCEPT +{ + const auto start = now(); + const auto pooled = archive().set_pooled(link, tx, ctx, ctx.timestamp); + report(pool_scan::admission, start); + due_ = due_ || (pooled && !is_sufficient(ctx)); + return pooled; +} + // An identified block with an invalid tx is invalid. code chaser_transaction::unconfirmable(const database::header_link& link, size_t height, const code& ec) NOEXCEPT @@ -285,19 +286,29 @@ code chaser_transaction::unconfirmable(const database::header_link& link, // ---------------------------------------------------------------------------- // A block tx is under proof of work, so it is not subject to policy (fee -// rate, guards) and may conflict with unconfirmed txs. -code chaser_transaction::validate(const chain::transaction& tx, +// rate, guards) and may conflict with unconfirmed txs. It is guarded if it +// also passes the guards of admission, which a pooled tx must. +code chaser_transaction::validate(bool& guarded, const chain::transaction& tx, const chain::context& ctx) NOEXCEPT { + guarded = false; code ec{}; if (!ec) ec = tx.check(); if (!ec) ec = tx.check(ctx); if (ec || tx.is_coinbase()) return ec; - archive().populate_with_metadata(tx); + archive().populate_with_metadata(tx, true, true); ec = tx.accept(ctx); if (!ec) ec = tx.connect(ctx); + if (ec) + return ec; + + const auto guard = tx.confirm_guard(ctx); + guarded = !tx.check_guard() && !tx.check_guard(ctx) && + !tx.accept_guard(ctx) && spends_held(tx) && + (!guard || guard == system::error::double_spend); + return ec; } diff --git a/src/chasers/chaser_transaction_query.cpp b/src/chasers/chaser_transaction_query.cpp index 9d9884bd..9465ed35 100644 --- a/src/chasers/chaser_transaction_query.cpp +++ b/src/chasers/chaser_transaction_query.cpp @@ -182,13 +182,28 @@ void chaser_transaction::do_estimate(size_t target, estimate_mode mode, const auto& settings = node_settings().pool; const auto count = add1(target); const auto limit = block_limit(); - query::pool_projection projection{}; const auto start = now(); - const auto projected = query.get_pool_projection(projection, count, - limit, settings.maximum_chunk_depth, settings.cutoff_rate_(), - settings.bytes_per_sigop, settings.maximum_outcomes); + + // Targets estimated while the projection is held are of one pool. + const auto held = estimating_ && + (start - estimated_) < settings.estimate_cache(); + + if (!held) + { + projection_ = {}; + arrived_ = false; + estimated_ = start; + } + + const auto projected = held ? + query.get_pool_continuation(projection_, count, limit, + settings.maximum_chunk_depth, settings.bytes_per_sigop) : + query.get_pool_projection(projection_, count, limit, + settings.maximum_chunk_depth, settings.cutoff_rate_(), + settings.bytes_per_sigop, settings.maximum_outcomes); report(pool_scan::projection, start); + estimating_ = !projected; if (projected) { handler(fault(projected), {}); @@ -196,27 +211,29 @@ void chaser_transaction::do_estimate(size_t target, estimate_mode mode, } // Arrivals are whole from the floor to the height of the open interval. - query::pool_arrivals arrivals{}; - if (mode != estimate_mode::basic) + if (mode != estimate_mode::basic && !arrived_) { const auto whole = floored_subtract(pool_.height, floor_); const auto heights = std::min(whole, conservative_estimate_window); - if (const auto ec = query.get_pool_arrivals(arrivals, pool_.height, + if (const auto ec = query.get_pool_arrivals(arrivals_, pool_.height, heights, settings.bytes_per_sigop)) { + estimating_ = false; handler(fault(ec), {}); return; } + + arrived_ = true; } const auto arrive = [&](uint64_t rate) NOEXCEPT { - return arriving(arrivals, rate, mode); + return arriving(arrivals_, rate, mode); }; using algorithm = pool::algorithm; const auto space = limit.size; - const auto rate = algorithm::estimate(projection, count, space, arrive); + const auto rate = algorithm::estimate(projection_, count, space, arrive); if (rate == max_uint64) { handler(error::estimate_false, {}); diff --git a/src/chasers/chaser_transaction_relay.cpp b/src/chasers/chaser_transaction_relay.cpp index 46e73b11..f5e65d6e 100644 --- a/src/chasers/chaser_transaction_relay.cpp +++ b/src/chasers/chaser_transaction_relay.cpp @@ -61,6 +61,14 @@ size_t chaser_transaction::reserve_relay(relay_tier tier) NOEXCEPT return relay_slots; } +void chaser_transaction::activate_relay(size_t slot) NOEXCEPT +{ + if (closed() || slot >= relay_slots) + return; + + POST(do_activate_relay, slot); +} + void chaser_transaction::release_relay(size_t slot) NOEXCEPT { if (closed() || slot >= relay_slots) @@ -94,6 +102,16 @@ void chaser_transaction::relay_pool(size_t slot) NOEXCEPT POST(do_relay_pool, slot); } +// A slot is relayed to only once its channel is notified of relay, as a tx +// relayed to it before then is marked held by a peer that is not sent it. +void chaser_transaction::do_activate_relay(size_t slot) NOEXCEPT +{ + BC_ASSERT(stranded()); + set_right_into(relaying_, slot); + if (!sweeping_) + do_sweep_relay(); +} + void chaser_transaction::do_release_relay(size_t slot) NOEXCEPT { BC_ASSERT(stranded()); @@ -101,6 +119,9 @@ void chaser_transaction::do_release_relay(size_t slot) NOEXCEPT set_right_into(mark, slot, false); rates_.at(slot) = zero; + set_right_into(relaying_, slot, false); + set_right_into(owed_, slot, false); + set_right_into(swept_, slot, false); reserved_.fetch_and(bit_not(bit_right(slot))); } @@ -110,15 +131,56 @@ void chaser_transaction::do_set_relay_rate(size_t slot, uint64_t rate) NOEXCEPT BC_ASSERT(stranded()); const auto prior = rates_.at(slot); rates_.at(slot) = rate; - if (rate < prior && pooling_ && !sweep_relay(slot)) - fault(database::error::integrity); + if (rate < prior) + owe_relay(slot); } void chaser_transaction::do_relay_pool(size_t slot) NOEXCEPT { BC_ASSERT(stranded()); - if (pooling_ && !sweep_relay(slot)) + owe_relay(slot); +} + +// private +// A sweep is a scan of the pool at a peer's request, so a slot is swept once +// between blocks, and a later request is met after the next block. +void chaser_transaction::owe_relay(size_t slot) NOEXCEPT +{ + BC_ASSERT(stranded()); + set_right_into(owed_, slot); + if (!sweeping_) + do_sweep_relay(); +} + +// private +uint64_t chaser_transaction::sweepable() const NOEXCEPT +{ + return bit_and(bit_and(owed_, relaying_), bit_not(swept_)); +} + +// One slot is swept in a turn of the strand, so other work is not held up by +// the sweeps of many slots. +void chaser_transaction::do_sweep_relay() NOEXCEPT +{ + BC_ASSERT(stranded()); + sweeping_ = false; + if (closed() || !pooling_ || is_zero(sweepable())) + return; + + const auto slot = right_zeros(sweepable()); + set_right_into(owed_, slot, false); + set_right_into(swept_, slot); + if (!sweep_relay(slot)) + { fault(database::error::integrity); + return; + } + + if (is_zero(sweepable())) + return; + + sweeping_ = true; + POST(do_sweep_relay); } // private diff --git a/src/chasers/chaser_transaction_submit.cpp b/src/chasers/chaser_transaction_submit.cpp index ea95ff1b..e978256c 100644 --- a/src/chasers/chaser_transaction_submit.cpp +++ b/src/chasers/chaser_transaction_submit.cpp @@ -224,8 +224,9 @@ void chaser_transaction::do_submit(const transactions_cptr& txs, bool test, } // private -// A tx of the set is stored if pooled or confirmed (other than a coinbase). -void chaser_transaction::hold(submission& set) const NOEXCEPT +// A tx of the set is stored if pooled or confirmed (other than a coinbase), +// and if pooled is held by the peer that sent it. +void chaser_transaction::hold(submission& set) NOEXCEPT { const auto& query = archive(); set.stored.resize(set.txs->size()); @@ -236,8 +237,16 @@ void chaser_transaction::hold(submission& set) const NOEXCEPT set.stored.at(index) = !tx.is_coinbase() && !link.is_terminal() && (query.is_pooled(link) || query.is_confirmed_tx(link)); - if (set.stored.at(index)) - set.held.insert(tx.get_hash(false)); + if (!set.stored.at(index)) + continue; + + set.held.insert(tx.get_hash(false)); + if (set.slot >= relay_slots) + continue; + + const auto row = query.to_pool_row(link); + if (!row.is_terminal()) + set_right_into(marks(row.value), set.slot); } } @@ -245,18 +254,31 @@ void chaser_transaction::hold(submission& set) const NOEXCEPT bool chaser_transaction::spends_held(const submission& set, const chain::transaction& tx) const NOEXCEPT { - const auto& query = archive(); return std::ranges::all_of(*tx.inputs_ptr(), [&](const auto& in) NOEXCEPT { - if (set.held.contains(in->point().hash())) - return true; + return set.held.contains(in->point().hash()) || is_held(*in); + }); +} - const database::tx_link parent{ in->metadata.parent_tx }; - return !parent.is_terminal() && - (query.is_confirmed_tx(parent) || query.is_pooled(parent)); +// private +bool chaser_transaction::spends_held( + const chain::transaction& tx) const NOEXCEPT +{ + return std::ranges::all_of(*tx.inputs_ptr(), [&](const auto& in) NOEXCEPT + { + return is_held(*in); }); } +// private +bool chaser_transaction::is_held(const chain::input& in) const NOEXCEPT +{ + const auto& query = archive(); + const database::tx_link parent{ in.metadata.parent_tx }; + return !parent.is_terminal() && + (query.is_confirmed_tx(parent) || query.is_pooled(parent)); +} + // private void chaser_transaction::pass_over(submission& set, size_t index, const code& ec) NOEXCEPT @@ -396,7 +418,7 @@ code chaser_transaction::clear(size_t& at, submission& set, set.cleared = true; } - if (!admitted.empty() && !relay(admitted, set.slot, reserved_.load())) + if (!admitted.empty() && !relay(admitted, set.slot, relaying_)) { at = set.first; return fault(database::error::integrity); @@ -442,11 +464,24 @@ code chaser_transaction::admit(database::tx_link& link, if (!query.is_pooled(link)) { + size_t repooled{}; const auto start = now(); - const auto admitted = query.set_pooled(link, tx, pool_, time); + const auto admitted = query.set_pooled(repooled, link, tx, + database::context::from(pool_), time); + report(pool_scan::admission, start); if (!admitted) return fault(error::transaction2); + + // A spender pooled again keeps its context, which may not suffice, + // and one not validated for want of this parent may now pass. + if (is_nonzero(repooled)) + { + due_ = true; + unvalidated_ = true; + if (!sweep()) + return fault(error::transaction11); + } } if (!archived && query.silent_enabled() && !query.set_silent(link, tx)) diff --git a/src/chasers/chaser_validate_parallel.cpp b/src/chasers/chaser_validate_parallel.cpp index 379a486d..699c1cab 100644 --- a/src/chasers/chaser_validate_parallel.cpp +++ b/src/chasers/chaser_validate_parallel.cpp @@ -233,7 +233,7 @@ code chaser_validate::validate_pooled(bool& pooled, const header_link& link, const chain::context& ctx) NOEXCEPT { const auto ec = archive().validate_pooled(link, ctx, subsidy_interval_, - initial_subsidy_); + initial_subsidy_, signet_challenge_); pooled = !ec; if (!ec || ec == database::error::unvalidated) diff --git a/src/full_node.cpp b/src/full_node.cpp index c4ca4b8f..5e40f413 100644 --- a/src/full_node.cpp +++ b/src/full_node.cpp @@ -274,6 +274,11 @@ size_t full_node::reserve_relay(relay_tier tier) NOEXCEPT return chaser_transaction_.reserve_relay(tier); } +void full_node::activate_relay(size_t slot) NOEXCEPT +{ + chaser_transaction_.activate_relay(slot); +} + void full_node::release_relay(size_t slot) NOEXCEPT { chaser_transaction_.release_relay(slot); diff --git a/src/protocols/protocol_peer.cpp b/src/protocols/protocol_peer.cpp index 17696795..169114a7 100644 --- a/src/protocols/protocol_peer.cpp +++ b/src/protocols/protocol_peer.cpp @@ -80,6 +80,11 @@ bool protocol_peer::reserve_relay() NOEXCEPT return slot < relay_slots; } +void protocol_peer::activate_relay() NOEXCEPT +{ + session_->activate_relay(channel_->relay_slot()); +} + void protocol_peer::release_relay() NOEXCEPT { const auto slot = channel_->relay_slot(); diff --git a/src/protocols/protocol_transaction_in_106.cpp b/src/protocols/protocol_transaction_in_106.cpp index 82038506..1bcc8f3e 100644 --- a/src/protocols/protocol_transaction_in_106.cpp +++ b/src/protocols/protocol_transaction_in_106.cpp @@ -135,13 +135,17 @@ get_data protocol_transaction_in_106::create_get_data( for (const auto& item: message.view(inventory_type())) { // The peer has the tx, so it is not announced back to it. + const auto repeated = was_announced(item.hash); set_announced(item.hash); - // A known tx is held by the peer, and a tx is requested once. + // A known tx is held by the peer (a pooled one is marked so, once), + // and a tx is requested once. const request slot{ sequence, set.hashes.size() }; if (const auto link = to_known(item.hash); !link.is_terminal()) { - known.push_back(link); + if (!repeated && archive().is_pooled(link)) + known.push_back(link); + continue; } diff --git a/src/protocols/protocol_transaction_in_70013.cpp b/src/protocols/protocol_transaction_in_70013.cpp index 726890b5..5f378e0f 100644 --- a/src/protocols/protocol_transaction_in_70013.cpp +++ b/src/protocols/protocol_transaction_in_70013.cpp @@ -44,7 +44,7 @@ void protocol_transaction_in_70013::do_handle_submit(const code& ec, if (stopped() || ec == network::error::service_stopped) return; - if (ec == error::pooling_disabled) + if (ec == error::pooling_disabled && !is_current_chain(true)) { LOGR("Tx from [" << opposite() << "] " << ec.message()); fire(events::tx_dropped, count); diff --git a/src/protocols/protocol_transaction_out_106.cpp b/src/protocols/protocol_transaction_out_106.cpp index fb563e33..af92d0a2 100644 --- a/src/protocols/protocol_transaction_out_106.cpp +++ b/src/protocols/protocol_transaction_out_106.cpp @@ -43,8 +43,10 @@ void protocol_transaction_out_106::start() NOEXCEPT if (started()) return; - // A channel with no relay slot is relayed nothing. - reserve_relay(); + // A channel with no relay slot is relayed nothing, and stopping has + // already been called for one stopped before its protocols attach. + if (!stopped()) + reserve_relay(); // Events subscription is asynchronous, events may be missed. subscribe_chase(BIND(handle_chase, _1, _2)); @@ -53,6 +55,21 @@ void protocol_transaction_out_106::start() NOEXCEPT protocol_peer::start(); } +// The slot is relayed to once the channel is notified of relay. +void protocol_transaction_out_106::subscribed(const code& ec, + object_key) NOEXCEPT +{ + BC_ASSERT(stranded()); + + if (stopped(ec)) + { + unsubscribe_chase(); + return; + } + + activate_relay(); +} + void protocol_transaction_out_106::stopping(const code& ec) NOEXCEPT { BC_ASSERT(stranded()); diff --git a/src/protocols/protocol_transaction_out_70013.cpp b/src/protocols/protocol_transaction_out_70013.cpp index 53ac8c72..0c5e6a7a 100644 --- a/src/protocols/protocol_transaction_out_70013.cpp +++ b/src/protocols/protocol_transaction_out_70013.cpp @@ -45,7 +45,17 @@ void protocol_transaction_out_70013::start() NOEXCEPT SUBSCRIBE_CHANNEL(fee_filter, handle_receive_fee_filter, _1, _2); protocol_transaction_out_70001::start(); - do_send_fee_filter(); +} + +// The peer is told what to send once relay to it is started. +void protocol_transaction_out_70013::subscribed(const code& ec, + object_key key) NOEXCEPT +{ + BC_ASSERT(stranded()); + + protocol_transaction_out_70001::subscribed(ec, key); + if (!stopped(ec)) + do_send_fee_filter(); } // Outbound (feefilter). diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp index 17c15ed4..5b8cc1bf 100644 --- a/src/sessions/session.cpp +++ b/src/sessions/session.cpp @@ -143,6 +143,11 @@ size_t session::reserve_relay() NOEXCEPT return node_.reserve_relay(relay_tier_()); } +void session::activate_relay(size_t slot) NOEXCEPT +{ + node_.activate_relay(slot); +} + void session::release_relay(size_t slot) NOEXCEPT { node_.release_relay(slot); diff --git a/src/settings.cpp b/src/settings.cpp index 3ba4f362..4345ba39 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -33,7 +33,9 @@ settings::transaction_pool::transaction_pool() NOEXCEPT maximum_outcomes{ 1'024 }, bytes_per_sigop{ 20 }, coinbase_weight{ 2'000 }, + coinbase_sigops{ 400 }, maximum_chunk_depth{ 25 }, + estimate_cache_seconds{ 7 }, thread_priority{ true } { } @@ -53,6 +55,12 @@ uint64_t settings::transaction_pool::cutoff_rate_() const NOEXCEPT chain::satoshi_per_bitcoin); } +network::steady_clock::duration +settings::transaction_pool::estimate_cache() const NOEXCEPT +{ + return network::seconds(estimate_cache_seconds); +} + network::processing_priority settings::transaction_pool::thread_priority_() const NOEXCEPT { diff --git a/test/chasers/chaser_transaction.cpp b/test/chasers/chaser_transaction.cpp index ede27648..09c92419 100644 --- a/test/chasers/chaser_transaction.cpp +++ b/test/chasers/chaser_transaction.cpp @@ -182,6 +182,22 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__organized__pooled_tx__pool_rows_fired, BC_REQUIRE_EQUAL(rows.front(), 1u); } +// The second block is confirmed before the first is applied to the pool. +BC_FIXTURE_TEST_CASE(chaser_transaction__organized__store_ahead__context_of_applied_block, chaser_transaction_pooling_setup_fixture) +{ + const auto first = push(); + const auto second = push(); + node_.notify(node::error::success, chases::organized{ first }); + const auto applied = bumped(3); + node_.notify(node::error::success, chases::organized{ second }); + const auto followed = bumped(4); + + BC_REQUIRE(!first.is_terminal()); + BC_REQUIRE(!second.is_terminal()); + BC_REQUIRE(applied); + BC_REQUIRE(followed); +} + // The subscription completes after the chaser is notified of the block. BC_FIXTURE_TEST_CASE(chaser_transaction__organized__relay_off__pool_rows_not_fired, chaser_transaction_relay_off_setup_fixture) { @@ -273,6 +289,79 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__reorganized__relative_spender_of_reconf BC_REQUIRE(unlocked); } +// The block of a flags zero context is not pooled again when popped. +BC_FIXTURE_TEST_CASE(chaser_transaction__reorganized__spender_of_unpooled_parent__out_until_reconfirmed, chaser_transaction_single_setup_fixture) +{ + constexpr auto insufficient = bit_or(pool::algorithm::absent, pool::algorithm::insufficient); + const auto first = spend(parent_value - 1'000); + const auto block = confirm(first); + const auto raised = bumped(3); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ second })); + const auto link = query_.to_tx(second->hash(false)); + const auto before = cell(link); + const auto popped = query_.pop_confirmed(); + const auto events = reported(3, [&]() + { + node_.notify(node::error::success, chases::reorganized{ block }); + }); + + const auto unpooled = !query_.is_pooled(query_.to_tx(first->hash(false))); + const auto revalidated = cell(link); + const auto pushed = query_.push_confirmed(block, true); + node_.notify(node::error::success, chases::organized{ block }); + const auto restored = celled(link, pool::algorithm::unlabeled); + + BC_REQUIRE(!block.is_terminal()); + BC_REQUIRE(raised); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(pool::algorithm::is_present(before)); + BC_REQUIRE(popped); + BC_REQUIRE_EQUAL(events.size(), 3u); + BC_REQUIRE(events.at(0).scan == node::pool_scan::block_out); + BC_REQUIRE(events.at(1).scan == node::pool_scan::context); + BC_REQUIRE(events.at(2).scan == node::pool_scan::context); + BC_REQUIRE(unpooled); + BC_REQUIRE_EQUAL(revalidated, insufficient); + BC_REQUIRE(pushed); + BC_REQUIRE(restored); +} + +// The spender is pooled again after its parent, which is admitted and not +// confirmed, and its context is swept before it is revalidated. +BC_FIXTURE_TEST_CASE(chaser_transaction__reorganized__parent_submitted__spender_swept_then_revalidated, chaser_transaction_single_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto block = confirm(first); + const auto raised = bumped(3); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ second })); + const auto popped = query_.pop_confirmed(); + const auto out = reported(3, [&]() + { + node_.notify(node::error::success, chases::reorganized{ block }); + }); + + const auto again = reported(4, [&]() + { + submit(package({ first })); + }); + + const auto restored = celled(query_.to_tx(second->hash(false)), pool::algorithm::unlabeled); + + BC_REQUIRE(!block.is_terminal()); + BC_REQUIRE(raised); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(popped); + BC_REQUIRE_EQUAL(out.size(), 3u); + BC_REQUIRE_EQUAL(again.size(), 4u); + BC_REQUIRE(again.at(0).scan == node::pool_scan::coverage); + BC_REQUIRE(again.at(1).scan == node::pool_scan::admission); + BC_REQUIRE(again.at(2).scan == node::pool_scan::context); + BC_REQUIRE(again.at(3).scan == node::pool_scan::context); + BC_REQUIRE(restored); +} + BC_AUTO_TEST_SUITE_END() BC_AUTO_TEST_SUITE(chaser_transaction_scan_tests) diff --git a/test/chasers/chaser_transaction_compact.cpp b/test/chasers/chaser_transaction_compact.cpp index 76de995b..ea2dc5e4 100644 --- a/test/chasers/chaser_transaction_compact.cpp +++ b/test/chasers/chaser_transaction_compact.cpp @@ -31,9 +31,9 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__invalid_tx_link__integr BC_REQUIRE(!query_.is_associated(link)); } -BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__stored_unpooled_tx__pooled, chaser_transaction_header_setup_fixture) +BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__stored_unpooled_tx__pooled, chaser_transaction_child_header_setup_fixture) { - const auto link = query_.to_header(p2p_compact_setup_fixture::block1().hash()); + const auto link = query_.to_header(child_header().hash()); const auto tx = spend(parent_value); BC_REQUIRE(query_.set(*tx)); @@ -47,6 +47,42 @@ 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__unstored_tx__archived_and_pooled, chaser_transaction_child_header_setup_fixture) +{ + const auto link = query_.to_header(child_header().hash()); + const auto tx = spend(parent_value); + const auto result = submit_compact(package({ tx }), { database::tx_link{ 0 }, database::tx_link::terminal }, link); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(query_.is_associated(link)); + BC_REQUIRE(query_.is_pooled(query_.to_tx(tx->hash(false)))); +} + +// The parent is archived, but neither confirmed nor pooled. +BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__stored_spender_of_unpooled_parent__not_pooled, chaser_transaction_header_setup_fixture) +{ + const auto link = query_.to_header(p2p_compact_setup_fixture::block1().hash()); + const auto tx = spend(parent_value); + BC_REQUIRE(query_.set(*tx)); + + const auto stored = query_.to_tx(tx->hash(false)); + const auto result = submit_compact(package({ tx }), { database::tx_link{ 0 }, database::tx_link::terminal }, link); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(query_.is_associated(link)); + BC_REQUIRE(!query_.is_pooled(stored)); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__submit_compact__unstored_spender_of_unpooled_parent__archived_not_pooled, chaser_transaction_header_setup_fixture) +{ + const auto link = query_.to_header(p2p_compact_setup_fixture::block1().hash()); + const auto tx = spend(parent_value); + const auto result = submit_compact(package({ tx }), { database::tx_link{ 0 }, database::tx_link::terminal }, link); + const auto stored = query_.to_tx(tx->hash(false)); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(query_.is_associated(link)); + BC_REQUIRE(!stored.is_terminal()); + BC_REQUIRE(!query_.is_pooled(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(); diff --git a/test/chasers/chaser_transaction_query.cpp b/test/chasers/chaser_transaction_query.cpp index a46daa93..23146c4c 100644 --- a/test/chasers/chaser_transaction_query.cpp +++ b/test/chasers/chaser_transaction_query.cpp @@ -245,7 +245,46 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__after_template__continued, ch BC_REQUIRE_EQUAL(after.second, 0u); } -BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__pool_written__projected_again, chaser_transaction_single_setup_fixture) +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__pool_written_projection_held__unchanged, chaser_transaction_held_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto first_result = submit(package({ first })); + const auto before = estimate(0); + const auto second_result = submit(package({ second })); + const auto after = estimate(0); + const auto deeper = estimate(1); + BC_REQUIRE_EQUAL(first_result.first, node::error::success); + BC_REQUIRE_EQUAL(before.first, node::error::success); + BC_REQUIRE_EQUAL(before.second, 0u); + BC_REQUIRE_EQUAL(second_result.first, node::error::success); + BC_REQUIRE_EQUAL(after.first, node::error::success); + BC_REQUIRE_EQUAL(after.second, 0u); + BC_REQUIRE_EQUAL(deeper.first, node::error::success); + BC_REQUIRE_EQUAL(deeper.second, 0u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__block_organized_projection_held__projected_again, chaser_transaction_held_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto first_result = submit(package({ first })); + const auto before = estimate(0); + const auto second_result = submit(package({ second })); + const auto confirmed = !confirm().is_terminal(); + const auto current = bumped(3); + const auto after = estimate(0); + BC_REQUIRE_EQUAL(first_result.first, node::error::success); + BC_REQUIRE_EQUAL(before.first, node::error::success); + BC_REQUIRE_EQUAL(before.second, 0u); + BC_REQUIRE_EQUAL(second_result.first, node::error::success); + BC_REQUIRE(confirmed); + BC_REQUIRE(current); + BC_REQUIRE_EQUAL(after.first, node::error::success); + BC_REQUIRE_EQUAL(after.second, 16'394u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__pool_written__projected_again, chaser_transaction_live_setup_fixture) { const auto first = spend(parent_value - 1'000); const auto second = spend(first->hash(false), parent_value - 3'000); diff --git a/test/chasers/chaser_transaction_relay.cpp b/test/chasers/chaser_transaction_relay.cpp index f41e9ff1..08c9299f 100644 --- a/test/chasers/chaser_transaction_relay.cpp +++ b/test/chasers/chaser_transaction_relay.cpp @@ -83,12 +83,21 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__relay__no_slot__not_relayed, chaser_tra BC_REQUIRE(recorded(events).empty()); } +BC_FIXTURE_TEST_CASE(chaser_transaction__relay__reserved_slot_not_started__not_relayed, chaser_transaction_pooling_setup_fixture) +{ + BC_REQUIRE_EQUAL(node_.reserve_relay(node::relay_tier::manual), 0u); + + const auto events = record(); + BC_REQUIRE_EQUAL(submit(package({ spend(parent_value) })).first, node::error::success); + BC_REQUIRE(recorded(events).empty()); +} + BC_FIXTURE_TEST_CASE(chaser_transaction__relay__two_slots__relayed_to_both, chaser_transaction_pooling_setup_fixture) { const auto tx = spend(parent_value); const relayed expected{ { tx->hash(false), 0b11 } }; - BC_REQUIRE_EQUAL(node_.reserve_relay(node::relay_tier::manual), 0u); - BC_REQUIRE_EQUAL(node_.reserve_relay(node::relay_tier::manual), 1u); + BC_REQUIRE_EQUAL(reserve_relaying(), 0u); + BC_REQUIRE_EQUAL(reserve_relaying(), 1u); const auto events = record(); BC_REQUIRE_EQUAL(submit(package({ tx })).first, node::error::success); @@ -97,7 +106,7 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__relay__two_slots__relayed_to_both, chas BC_FIXTURE_TEST_CASE(chaser_transaction__relay__test__not_relayed, chaser_transaction_pooling_setup_fixture) { - BC_REQUIRE_EQUAL(node_.reserve_relay(node::relay_tier::manual), 0u); + BC_REQUIRE_EQUAL(reserve_relaying(), 0u); const auto events = record(); BC_REQUIRE_EQUAL(submit(package({ spend(parent_value) }), true).first, node::error::success); @@ -108,8 +117,8 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__relay__origin__not_relayed_to_origin, c { const auto tx = spend(parent_value); const relayed expected{ { tx->hash(false), 0b10 } }; - BC_REQUIRE_EQUAL(node_.reserve_relay(node::relay_tier::manual), 0u); - BC_REQUIRE_EQUAL(node_.reserve_relay(node::relay_tier::manual), 1u); + BC_REQUIRE_EQUAL(reserve_relaying(), 0u); + BC_REQUIRE_EQUAL(reserve_relaying(), 1u); const auto events = record(); BC_REQUIRE_EQUAL(submit_announced(package({ tx }), 0).first, node::error::success); @@ -118,7 +127,7 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__relay__origin__not_relayed_to_origin, c BC_FIXTURE_TEST_CASE(chaser_transaction__relay__below_rate__not_relayed, chaser_transaction_pooling_setup_fixture) { - const auto slot = node_.reserve_relay(node::relay_tier::manual); + const auto slot = reserve_relaying(); node_.set_relay_rate(slot, 100'000); const auto events = record(); @@ -133,7 +142,7 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__relay__child_pays_for_parent__both_in_o const auto low = spend(parent_value - 1'000); const auto high = spend(low->hash(false), parent_value - 21'000); const relayed expected{ { low->hash(false), 0b01 }, { high->hash(false), 0b01 } }; - const auto slot = node_.reserve_relay(node::relay_tier::manual); + const auto slot = reserve_relaying(); node_.set_relay_rate(slot, 100'000); const auto first = record(); @@ -150,7 +159,7 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__relay__parent_held__child_alone, chaser const auto low = spend(parent_value - 1'000); const auto high = spend(low->hash(false), parent_value - 21'000); const relayed expected{ { high->hash(false), 0b01 } }; - const auto slot = node_.reserve_relay(node::relay_tier::manual); + const auto slot = reserve_relaying(); node_.set_relay_rate(slot, 100'000); BC_REQUIRE_EQUAL(submit(package({ low })).first, node::error::success); node_.set_relayed(slot, { query_.to_tx(low->hash(false)) }); @@ -164,7 +173,7 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__relay__rate_fall__swept, chaser_transac { const auto low = spend(parent_value - 1'000); const relayed expected{ { low->hash(false), 0b01 } }; - const auto slot = node_.reserve_relay(node::relay_tier::manual); + const auto slot = reserve_relaying(); node_.set_relay_rate(slot, 100'000); BC_REQUIRE_EQUAL(submit(package({ low })).first, node::error::success); @@ -177,7 +186,7 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__relay__rate_fall__swept, chaser_transac BC_FIXTURE_TEST_CASE(chaser_transaction__relay__rate_rise__not_swept, chaser_transaction_pooling_setup_fixture) { const auto low = spend(parent_value - 1'000); - const auto slot = node_.reserve_relay(node::relay_tier::manual); + const auto slot = reserve_relaying(); node_.set_relay_rate(slot, 100'000); BC_REQUIRE_EQUAL(submit(package({ low })).first, node::error::success); @@ -187,15 +196,73 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__relay__rate_rise__not_swept, chaser_tra BC_REQUIRE(recorded(events).empty()); } +// The slot is swept once between blocks, and its later fall after the next. +BC_FIXTURE_TEST_CASE(chaser_transaction__relay__second_rate_fall__swept_after_block, chaser_transaction_pooling_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 2'000); + const relayed expected{ { second->hash(false), 0b01 } }; + const auto slot = reserve_relaying(); + node_.set_relay_rate(slot, 100'000); + BC_REQUIRE_EQUAL(submit(package({ first })).first, node::error::success); + node_.set_relay_rate(slot, 10'000); + node_.set_relay_rate(slot, 100'000); + BC_REQUIRE_EQUAL(submit(package({ second })).first, node::error::success); + + const auto deferred = record(); + node_.set_relay_rate(slot, 10'000); + BC_REQUIRE(!get_confirmable_txs().first); + BC_REQUIRE(recorded(deferred).empty()); + + const auto swept = record(); + BC_REQUIRE(!confirm().is_terminal()); + BC_REQUIRE(bumped(3)); + BC_REQUIRE(recorded(swept) == expected); +} + +// The sweep requested of a reserved slot is made when relay to it starts. +BC_FIXTURE_TEST_CASE(chaser_transaction__relay_pool__slot_not_started__swept_when_started, chaser_transaction_pooling_setup_fixture) +{ + const auto tx = spend(parent_value); + const relayed expected{ { tx->hash(false), 0b01 } }; + const auto slot = node_.reserve_relay(node::relay_tier::manual); + BC_REQUIRE_EQUAL(submit(package({ tx })).first, node::error::success); + + const auto owed = record(); + node_.relay_pool(slot); + BC_REQUIRE(!get_confirmable_txs().first); + BC_REQUIRE(recorded(owed).empty()); + + const auto started = record(); + node_.activate_relay(slot); + BC_REQUIRE(!get_confirmable_txs().first); + BC_REQUIRE(recorded(started) == expected); +} + +// A pooled tx that its peer sends again is held by that peer. +BC_FIXTURE_TEST_CASE(chaser_transaction__relay__pooled_tx_sent_by_peer__not_swept_to_it, chaser_transaction_pooling_setup_fixture) +{ + const auto low = spend(parent_value - 1'000); + const auto slot = reserve_relaying(); + node_.set_relay_rate(slot, 100'000); + BC_REQUIRE_EQUAL(submit(package({ low })).first, node::error::success); + BC_REQUIRE_EQUAL(submit_announced(package({ low }), slot).first, node::error::duplicate_transaction); + + const auto events = record(); + node_.set_relay_rate(slot, 10'000); + BC_REQUIRE(!get_confirmable_txs().first); + BC_REQUIRE(recorded(events).empty()); +} + BC_FIXTURE_TEST_CASE(chaser_transaction__release_relay__reserved_again__marks_cleared, chaser_transaction_pooling_setup_fixture) { const auto tx = spend(parent_value); const relayed expected{ { tx->hash(false), 0b01 } }; - BC_REQUIRE_EQUAL(node_.reserve_relay(node::relay_tier::manual), 0u); + BC_REQUIRE_EQUAL(reserve_relaying(), 0u); BC_REQUIRE_EQUAL(submit(package({ tx })).first, node::error::success); node_.release_relay(0); BC_REQUIRE(!get_confirmable_txs().first); - BC_REQUIRE_EQUAL(node_.reserve_relay(node::relay_tier::manual), 0u); + BC_REQUIRE_EQUAL(reserve_relaying(), 0u); const auto events = record(); node_.set_relay_rate(0, 1); diff --git a/test/chasers/chaser_transaction_setup_fixture.hpp b/test/chasers/chaser_transaction_setup_fixture.hpp index bd9f12e4..a2415cc4 100644 --- a/test/chasers/chaser_transaction_setup_fixture.hpp +++ b/test/chasers/chaser_transaction_setup_fixture.hpp @@ -72,6 +72,20 @@ inline chain::transaction::cptr spend(uint64_t value) NOEXCEPT return spend(parent().hash(false), value); } +// A spend of the parent to an output of one sigop. +inline chain::transaction::cptr spend_signed(uint64_t value) NOEXCEPT +{ + const chain::point point{ parent().hash(false), 0u }; + const chain::operations ops{ { chain::opcode::checksig } }; + return std::make_shared(chain::transaction + { + 1u, + chain::inputs{ { point, chain::script{}, max_uint32 } }, + chain::outputs{ { value, chain::script{ ops } } }, + 0u + }); +} + inline chain::transaction::cptr spend_relative(const hash_digest& hash, uint64_t value, uint32_t sequence) NOEXCEPT { @@ -162,6 +176,28 @@ inline bool stored_header(node::query& query) NOEXCEPT return query.set(parent()) && query.set(header1, context1, work1, false); } +// The header of a block 2 above the parent block. +inline const chain::header& child_header() NOEXCEPT +{ + const auto& header1 = parent_block().header(); + static const chain::header instance + { + 1u, header1.hash(), null_hash, add1(header1.timestamp()), + header1.bits(), 2u + }; + + return instance; +} + +// The parent confirmed in block 1, block 2 archived as a header without txs. +inline bool stored_child_header(node::query& query) NOEXCEPT +{ + const auto& header1 = parent_block().header(); + const database::context context2{ deployed, 2, header1.timestamp() }; + return stored_parent(query) && + query.set(child_header(), context2, {}, false); +} + // Block 1 of a coinbase and a spend of the parent. inline const chain::block& reorganized_block() NOEXCEPT { @@ -240,6 +276,14 @@ struct chaser_transaction_setup_fixture return promise.get_future().get(); } + // Reserve a relay slot of the tier and start relay to it. + size_t reserve_relaying(node::relay_tier tier=node::relay_tier::manual) + { + const auto slot = node_.reserve_relay(tier); + node_.activate_relay(slot); + return slot; + } + // Reserve every free relay slot of the tier, returning the count. size_t reserve_relays(node::relay_tier tier=node::relay_tier::manual) { @@ -382,8 +426,18 @@ struct chaser_transaction_setup_fixture return promise.get_future().get(); } - // Confirm a block of a coinbase, and the tx if any, above genesis. + // Confirm a block of a coinbase, and the tx if any, and notify of it. database::header_link confirm(const chain::transaction::cptr& tx={}) + { + const auto link = push(tx); + if (!link.is_terminal()) + node_.notify(node::error::success, chases::organized{ link }); + + return link; + } + + // Confirm a block of a coinbase, and the tx if any, in the store only. + database::header_link push(const chain::transaction::cptr& tx={}) { const system::settings bitcoin{ chain::selection::mainnet }; const auto& genesis = bitcoin.genesis_block; @@ -417,7 +471,6 @@ struct chaser_transaction_setup_fixture if (!query_.push_candidate(link) || !query_.push_confirmed(link, true)) return {}; - node_.notify(node::error::success, chases::organized{ link }); return link; } @@ -785,6 +838,21 @@ struct chaser_transaction_reserve_setup_fixture } }; +// The coinbase is declared to count every sigop of a block. +struct chaser_transaction_sigops_setup_fixture + : chaser_transaction_setup_fixture +{ + inline chaser_transaction_sigops_setup_fixture() + : chaser_transaction_setup_fixture([](configuration& config) + { + config.network.enable_relay = true; + config.node.currency_window_minutes = 0; + config.node.pool.coinbase_sigops = 80'000; + }) + { + } +}; + // A block holds one test tx (61 of its 80 virtual bytes). struct chaser_transaction_single_setup_fixture : chaser_transaction_setup_fixture @@ -800,6 +868,38 @@ struct chaser_transaction_single_setup_fixture } }; +// Estimates of a held projection, never expired by age. +struct chaser_transaction_held_setup_fixture + : chaser_transaction_setup_fixture +{ + inline chaser_transaction_held_setup_fixture() + : chaser_transaction_setup_fixture([](configuration& config) + { + config.network.enable_relay = true; + config.node.currency_window_minutes = 0; + config.node.pool.coinbase_weight = 3'999'348; + config.node.pool.estimate_cache_seconds = max_uint16; + }) + { + } +}; + +// Estimates of the pool as it is, no projection held. +struct chaser_transaction_live_setup_fixture + : chaser_transaction_setup_fixture +{ + inline chaser_transaction_live_setup_fixture() + : chaser_transaction_setup_fixture([](configuration& config) + { + config.network.enable_relay = true; + config.node.currency_window_minutes = 0; + config.node.pool.coinbase_weight = 3'999'348; + config.node.pool.estimate_cache_seconds = 0; + }) + { + } +}; + struct chaser_transaction_no_witness_setup_fixture : chaser_transaction_setup_fixture { @@ -858,6 +958,19 @@ struct chaser_transaction_header_setup_fixture } }; +struct chaser_transaction_child_header_setup_fixture + : chaser_transaction_setup_fixture +{ + inline chaser_transaction_child_header_setup_fixture() + : chaser_transaction_setup_fixture([](configuration& config) + { + config.network.enable_relay = true; + config.node.currency_window_minutes = 0; + }, stored_child_header) + { + } +}; + struct chaser_transaction_closed_header_setup_fixture : chaser_transaction_setup_fixture { diff --git a/test/chasers/chaser_transaction_submit.cpp b/test/chasers/chaser_transaction_submit.cpp index 8e6eb4d9..a6b44b89 100644 --- a/test/chasers/chaser_transaction_submit.cpp +++ b/test/chasers/chaser_transaction_submit.cpp @@ -247,6 +247,18 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__submit_announced__conflict_over_block__ BC_REQUIRE(query_.is_tx(child->hash(false))); } +BC_FIXTURE_TEST_CASE(chaser_transaction__submit__over_reserved_sigops__pool_limit, chaser_transaction_sigops_setup_fixture) +{ + const auto result = submit(package({ spend_signed(parent_value - 1'000) })); + BC_REQUIRE_EQUAL(result.first, database::error::pool_limit); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__submit__no_sigops_reserved_sigops__success, chaser_transaction_sigops_setup_fixture) +{ + const auto result = submit(package({ spend(parent_value - 1'000) })); + BC_REQUIRE_EQUAL(result.first, node::error::success); +} + BC_FIXTURE_TEST_CASE(chaser_transaction__submit__over_block__pool_limit_not_archived, chaser_transaction_reserve_setup_fixture) { const auto tx = spend(parent_value - 1'000); diff --git a/test/settings.cpp b/test/settings.cpp index 191fed5f..509039d4 100644 --- a/test/settings.cpp +++ b/test/settings.cpp @@ -215,7 +215,10 @@ BC_AUTO_TEST_CASE(settings__pool__default__expected) BC_REQUIRE_EQUAL(pool.maximum_outcomes, 1'024_u32); BC_REQUIRE_EQUAL(pool.bytes_per_sigop, 20_u32); BC_REQUIRE_EQUAL(pool.coinbase_weight, 2'000_u32); + BC_REQUIRE_EQUAL(pool.coinbase_sigops, 400_u32); BC_REQUIRE_EQUAL(pool.maximum_chunk_depth, 25_u32); + BC_REQUIRE_EQUAL(pool.estimate_cache_seconds, 7_u16); + BC_REQUIRE(pool.estimate_cache() == steady_clock::duration(seconds(7))); BC_REQUIRE(pool.thread_priority); BC_REQUIRE(pool.thread_priority_() == network::processing_priority::high); }