diff --git a/include/bitcoin/node/chase.hpp b/include/bitcoin/node/chase.hpp index 73c898e5..43b23753 100644 --- a/include/bitcoin/node/chase.hpp +++ b/include/bitcoin/node/chase.hpp @@ -154,7 +154,7 @@ enum class chase block, /// The store has been pruned (once, following the first current block). - /// Issued by 'snapshot' and handled by none. + /// Issued by 'snapshot' and handled by 'transaction'. pruned, /// The confirmed chain is no longer current. diff --git a/include/bitcoin/node/chasers/chaser.hpp b/include/bitcoin/node/chasers/chaser.hpp index a6308695..ef3efeb0 100644 --- a/include/bitcoin/node/chasers/chaser.hpp +++ b/include/bitcoin/node/chasers/chaser.hpp @@ -94,6 +94,12 @@ class BCN_API chaser /// Reset store disk full condition. virtual code reload(const store::event_handler& handler) NOEXCEPT; + /// Mining. + /// ----------------------------------------------------------------------- + + /// Build a template of pooled txs, parents first, and its height. + virtual void request_template(template_handler&& handler) NOEXCEPT; + /// Events. /// ----------------------------------------------------------------------- diff --git a/include/bitcoin/node/chasers/chaser_template.hpp b/include/bitcoin/node/chasers/chaser_template.hpp index 16529511..81ed4600 100644 --- a/include/bitcoin/node/chasers/chaser_template.hpp +++ b/include/bitcoin/node/chasers/chaser_template.hpp @@ -38,10 +38,27 @@ class BCN_API chaser_template code start() NOEXCEPT override; + /// The latest template of pooled txs, parents first, and its height. + virtual void get_template(template_handler&& handler) NOEXCEPT; + protected: virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT; - virtual void do_transaction(transaction_t value) NOEXCEPT; + virtual void do_update() NOEXCEPT; + virtual void do_get_template(const template_handler& handler) NOEXCEPT; + +private: + void build() NOEXCEPT; + void handle_build(const code& ec, size_t height, + const database::tx_links& links) NOEXCEPT; + void do_build(const code& ec, size_t height, + const database::tx_links& links) NOEXCEPT; + + // These are protected by strand. + database::tx_links links_{}; + size_t height_{}; + bool building_{}; + bool stale_{}; }; } // namespace node diff --git a/include/bitcoin/node/chasers/chaser_transaction.hpp b/include/bitcoin/node/chasers/chaser_transaction.hpp index 21a86969..6e24abe3 100644 --- a/include/bitcoin/node/chasers/chaser_transaction.hpp +++ b/include/bitcoin/node/chasers/chaser_transaction.hpp @@ -51,6 +51,13 @@ class BCN_API chaser_transaction const database::tx_links& links, const database::header_link& link, submit_handler&& handler) NOEXCEPT; + /// Build a template of pooled txs, parents first, and its height. + virtual void build_template(template_handler&& handler) NOEXCEPT; + + /// Lowest rate (per virtual kilobyte) confirmed by the target block. + virtual void estimate(size_t target, estimate_mode mode, + estimate_handler&& handler) NOEXCEPT; + protected: using chaser::broadcast; @@ -62,6 +69,9 @@ class BCN_API chaser_transaction const system::chain::transactions_cptr& txs, const database::tx_links& links, const database::header_link& link, const submit_handler& handler) NOEXCEPT; + virtual void do_build_template(const template_handler& handler) NOEXCEPT; + virtual void do_estimate(size_t target, estimate_mode mode, + const estimate_handler& handler) NOEXCEPT; /// Recompute the pool context, closing the pool if not current, and map /// scan starts: built once current, extended by block until pooling, and @@ -71,6 +81,9 @@ class BCN_API chaser_transaction /// Pool the unpooled txs of a reorganized block. virtual void do_reorganized(header_t link) NOEXCEPT; + /// Raise the arrivals floor above the pruned block. + virtual void do_pruned(header_t link) NOEXCEPT; + /// Map scan starts from the confirmed top (scan threadpool). virtual void do_scan(const database::header_link& top, size_t bound, bool remap) NOEXCEPT; @@ -80,6 +93,7 @@ class BCN_API chaser_transaction private: static size_t to_rate(const system::chain::transaction& tx) NOEXCEPT; + system::pool::algorithm::totals_t block_limit() const NOEXCEPT; code validate(size_t& index, const system::chain::transaction_cptrs& txs, const std::vector& stored) NOEXCEPT; @@ -94,6 +108,7 @@ class BCN_API chaser_transaction const code& ec) NOEXCEPT; void broadcast(const system::chain::transaction& tx) const NOEXCEPT; void start_scan(bool remap) NOEXCEPT; + uint64_t arriving(uint64_t rate, estimate_mode mode) const NOEXCEPT; // These are thread safe. network::threadpool scan_threadpool_; @@ -101,8 +116,13 @@ class BCN_API chaser_transaction // These are protected by strand. system::chain::context pool_{}; + query::pool_projection projection_{}; + query::pool_arrivals arrivals_{}; + size_t floor_{}; bool pooling_{}; bool scanning_{}; + bool projected_{}; + bool arrived_{}; }; } // namespace node diff --git a/include/bitcoin/node/define.hpp b/include/bitcoin/node/define.hpp index 74467ebd..a6da93b5 100644 --- a/include/bitcoin/node/define.hpp +++ b/include/bitcoin/node/define.hpp @@ -36,13 +36,25 @@ typedef std::error_code code; /// Estimate types. enum class estimate_mode { + /// The pool as it is, with no arrivals. basic, + + /// Arrivals weighted by recency, each block half of the one above it. geometric, + + /// Arrivals at their mean over the economical window. economical, + + /// Arrivals at their peak over the conservative window. conservative, + unknown }; constexpr size_t maximum_estimate_horizon = 1008; + +/// Blocks of arrivals (two hours and one day at ten minutes per block). +constexpr size_t economical_estimate_window = 12; +constexpr size_t conservative_estimate_window = 144; typedef std::function estimate_handler; /// Organization types. @@ -58,6 +70,10 @@ typedef std::shared_ptr map_ptr; typedef std::function map_handler; +/// Mining types. +typedef std::function template_handler; + /// Event desubscriber. typedef network::desubscriber event_subscriber; typedef event_subscriber::handler event_notifier; diff --git a/include/bitcoin/node/full_node.hpp b/include/bitcoin/node/full_node.hpp index 2cc086ff..6e567f3c 100644 --- a/include/bitcoin/node/full_node.hpp +++ b/include/bitcoin/node/full_node.hpp @@ -89,6 +89,12 @@ class BCN_API full_node const database::tx_links& links, const database::header_link& link, submit_handler&& handler) NOEXCEPT; + /// Build a template of pooled txs, parents first, and its height. + virtual void build_template(template_handler&& handler) NOEXCEPT; + + /// The latest template of pooled txs, parents first, and its height. + virtual void get_template(template_handler&& handler) NOEXCEPT; + /// Manage download queue. virtual void get_hashes(map_handler&& handler) NOEXCEPT; virtual void put_hashes(const map_ptr& map, @@ -170,7 +176,7 @@ class BCN_API full_node /// Methods. /// ----------------------------------------------------------------------- - /// Get current fee estimate. + /// Lowest rate (per virtual kilobyte) confirmed by the target block. void estimate(size_t target, estimate_mode mode, estimate_handler&& handler) NOEXCEPT; diff --git a/include/bitcoin/node/protocols/protocol.hpp b/include/bitcoin/node/protocols/protocol.hpp index 6becde4d..ba2dbb49 100644 --- a/include/bitcoin/node/protocols/protocol.hpp +++ b/include/bitcoin/node/protocols/protocol.hpp @@ -94,7 +94,7 @@ class BCN_API protocol virtual void connect(const network::config::endpoint& endpoint, network::net::channel_notifier&& handler) NOEXCEPT; - /// Get current fee estimate. + /// Lowest rate (per virtual kilobyte) confirmed by the target block. void estimate(size_t target, estimate_mode mode, estimate_handler&& handler) NOEXCEPT; diff --git a/include/bitcoin/node/sessions/session.hpp b/include/bitcoin/node/sessions/session.hpp index 0ce71d37..a56129bf 100644 --- a/include/bitcoin/node/sessions/session.hpp +++ b/include/bitcoin/node/sessions/session.hpp @@ -102,7 +102,7 @@ class BCN_API session virtual void connect(const network::config::endpoint& endpoint, network::net::channel_notifier&& handler) NOEXCEPT; - /// Get current fee estimate. + /// Lowest rate (per virtual kilobyte) confirmed by the target block. void estimate(size_t target, estimate_mode mode, estimate_handler&& handler) NOEXCEPT; diff --git a/include/bitcoin/node/settings.hpp b/include/bitcoin/node/settings.hpp index 5da7343e..3b773f52 100644 --- a/include/bitcoin/node/settings.hpp +++ b/include/bitcoin/node/settings.hpp @@ -33,9 +33,29 @@ class BCN_API settings settings() NOEXCEPT; settings(system::chain::selection context) NOEXCEPT; + /// [pool] settings. + struct BCN_API transaction_pool + { + DEFAULT_COPY_MOVE_DESTRUCT(transaction_pool); + transaction_pool() NOEXCEPT; + + /// Properties. + double minimum_fee_rate; + uint32_t maximum_outcomes; + uint32_t bytes_per_sigop; + uint32_t coinbase_weight; + uint32_t maximum_chunk_depth; + + /// Helpers. + virtual uint64_t minimum_fee_rate_() const NOEXCEPT; + }; + /// The network of the defaults. system::chain::selection network; + /// The transaction pool. + transaction_pool pool; + /// Properties. uint32_t threads; bool delay_inbound; @@ -51,7 +71,6 @@ class BCN_API settings bool cpu_batch_verify; bool cpu_batch_silent; float allowed_deviation; - double minimum_fee_rate; uint64_t batch_verify; uint64_t batch_silent; uint16_t current_connections; @@ -70,7 +89,6 @@ class BCN_API settings virtual size_t threads_() const NOEXCEPT; virtual size_t maximum_height_() const NOEXCEPT; virtual size_t maximum_concurrency_() const NOEXCEPT; - virtual uint64_t minimum_fee_rate_() const NOEXCEPT; virtual uint64_t services_provided() const NOEXCEPT; virtual uint64_t services_required() const NOEXCEPT; virtual bool use_gpu_() const NOEXCEPT; diff --git a/src/chasers/chaser.cpp b/src/chasers/chaser.cpp index 74e054a5..6b4881a2 100644 --- a/src/chasers/chaser.cpp +++ b/src/chasers/chaser.cpp @@ -70,6 +70,11 @@ void chaser::resume() NOEXCEPT node_.resume(); } +void chaser::request_template(template_handler&& handler) NOEXCEPT +{ + node_.build_template(std::move(handler)); +} + code chaser::prune(const store::event_handler& handler) NOEXCEPT { return node_.prune(handler); diff --git a/src/chasers/chaser_template.cpp b/src/chasers/chaser_template.cpp index b235165b..b966f6f8 100644 --- a/src/chasers/chaser_template.cpp +++ b/src/chasers/chaser_template.cpp @@ -40,13 +40,23 @@ chaser_template::chaser_template(full_node& node) NOEXCEPT // start // ---------------------------------------------------------------------------- -// TODO: initialize template state. code chaser_template::start() NOEXCEPT { SUBSCRIBE_CHASE(handle_chase, _1, _2); return error::success; } +// methods +// ---------------------------------------------------------------------------- + +void chaser_template::get_template(template_handler&& handler) NOEXCEPT +{ + if (closed()) + return; + + POST(do_get_template, std::move(handler)); +} + // event handlers // ---------------------------------------------------------------------------- @@ -60,12 +70,13 @@ bool chaser_template::handle_chase(const code&, event_value value) NOEXCEPT if (suspended()) return true; - // TODO: also handle confirmed/unconfirmed. switch (to_chase(value)) { case chase::transaction: + case chase::organized: + case chase::reorganized: { - POST(do_transaction, to_payload(value).link); + POST(do_update); break; } case chase::stop: @@ -81,10 +92,70 @@ bool chaser_template::handle_chase(const code&, event_value value) NOEXCEPT return true; } -// TODO: handle transaction graph change (may issue 'candidate'). -void chaser_template::do_transaction(transaction_t) NOEXCEPT +// Pool changes during a build coalesce into one following build. +void chaser_template::do_update() NOEXCEPT +{ + BC_ASSERT(stranded()); + + if (building_) + { + stale_ = true; + return; + } + + build(); +} + +void chaser_template::do_get_template(const template_handler& handler) NOEXCEPT { BC_ASSERT(stranded()); + handler(error::success, height_, links_); +} + +// build +// ---------------------------------------------------------------------------- + +// private +void chaser_template::build() NOEXCEPT +{ + BC_ASSERT(stranded()); + + building_ = true; + stale_ = false; + request_template(BIND(handle_build, _1, _2, _3)); +} + +// private +void chaser_template::handle_build(const code& ec, size_t height, + const database::tx_links& links) NOEXCEPT +{ + POST(do_build, ec, height, links); +} + +// private +void chaser_template::do_build(const code& ec, size_t height, + const database::tx_links& links) NOEXCEPT +{ + BC_ASSERT(stranded()); + + building_ = false; + if (closed() || + ec == network::error::service_stopped || + ec == error::pooling_disabled) + return; + + if (ec) + { + fault(ec); + return; + } + + links_ = links; + height_ = height; + notify(error::success, chases::template_{ height }); + + if (stale_) + build(); } BC_POP_WARNING() diff --git a/src/chasers/chaser_transaction.cpp b/src/chasers/chaser_transaction.cpp index c9ee051f..d2911846 100644 --- a/src/chasers/chaser_transaction.cpp +++ b/src/chasers/chaser_transaction.cpp @@ -86,6 +86,11 @@ bool chaser_transaction::handle_chase(const code&, event_value value) NOEXCEPT POST(do_reorganized, to_payload(value).link); break; } + case chase::pruned: + { + POST(do_pruned, to_payload(value).link); + break; + } case chase::stop: { return false; @@ -105,7 +110,9 @@ bool chaser_transaction::handle_chase(const code&, event_value value) NOEXCEPT void chaser_transaction::do_bump() NOEXCEPT { BC_ASSERT(stranded()); + const auto opened = pooling_; pooling_ = false; + projected_ = false; if (closed() || !is_current_chain(true)) return; @@ -134,6 +141,10 @@ void chaser_transaction::do_bump() NOEXCEPT pool_ = chain_state{ *state, system_settings() }.context(); query.set_pooling(); pooling_ = true; + + // Rows that arrived while the pool was closed are not in it. + if (!opened) + floor_ = std::max(floor_, pool_.height); } // Scan starts are mapped after the latch, as pooled rows are unordered. @@ -232,6 +243,24 @@ void chaser_transaction::do_reorganized(header_t link) NOEXCEPT } } +// Rows confirmed by the pruned block are not retained by the prune. +void chaser_transaction::do_pruned(header_t link) NOEXCEPT +{ + BC_ASSERT(stranded()); + if (closed()) + return; + + size_t height{}; + if (!archive().get_height(height, link)) + { + fault(error::transaction9); + return; + } + + projected_ = false; + floor_ = std::max(floor_, add1(height)); +} + // methods // ---------------------------------------------------------------------------- @@ -254,6 +283,23 @@ void chaser_transaction::submit_compact(const transactions_cptr& txs, POST(do_submit_compact, txs, links, link, std::move(handler)); } +void chaser_transaction::build_template(template_handler&& handler) NOEXCEPT +{ + if (closed()) + return; + + POST(do_build_template, std::move(handler)); +} + +void chaser_transaction::estimate(size_t target, estimate_mode mode, + estimate_handler&& handler) NOEXCEPT +{ + if (closed()) + return; + + POST(do_estimate, target, mode, std::move(handler)); +} + // private void chaser_transaction::do_submit(const transactions_cptr& txs, bool test, const submit_handler& handler) NOEXCEPT @@ -293,6 +339,8 @@ void chaser_transaction::do_submit(const transactions_cptr& txs, bool test, return; } + projected_ = false; + // Parents precede children, so a parent is archived before it is resolved. const auto time = network::unix_time(); for (index = zero; index < txs->size(); ++index) @@ -369,6 +417,8 @@ void chaser_transaction::do_submit_compact(const transactions_cptr& txs, return; } + projected_ = false; + using link_t = database::tx_link::integer; const auto filler = [&](link_t& fk, size_t index) NOEXCEPT { @@ -418,6 +468,118 @@ void chaser_transaction::do_submit_compact(const transactions_cptr& txs, handler(error::success, {}); } +// private +void chaser_transaction::do_build_template( + const template_handler& handler) NOEXCEPT +{ + BC_ASSERT(stranded()); + + if (closed()) + { + handler(network::error::service_stopped, {}, {}); + return; + } + + if (!pooling_) + { + handler(error::pooling_disabled, {}, {}); + return; + } + + // The template is the first block of the projection. + const auto& settings = node_settings().pool; + database::tx_links links{}; + const auto ec = archive().get_pool_template(links, projection_, + block_limit(), settings.maximum_chunk_depth, settings.bytes_per_sigop, + settings.maximum_outcomes); + + projected_ = !ec; + arrived_ = false; + handler(ec, pool_.height, links); +} + +// private +void chaser_transaction::do_estimate(size_t target, estimate_mode mode, + const estimate_handler& handler) NOEXCEPT +{ + BC_ASSERT(stranded()); + + if (closed()) + { + handler(network::error::service_stopped, {}); + return; + } + + if (!network_settings().enable_relay) + { + handler(error::estimate_disabled, {}); + return; + } + + if (!pooling_) + { + handler(error::estimate_premature, {}); + return; + } + + if (mode == estimate_mode::unknown || target >= maximum_estimate_horizon) + { + handler(error::estimate_false, {}); + return; + } + + if (!projected_) + { + projection_ = {}; + arrived_ = false; + } + + const auto& query = archive(); + const auto& settings = node_settings().pool; + const auto count = add1(target); + const auto limit = block_limit(); + if (const auto ec = query.get_pool_projection(projection_, count, limit, + settings.maximum_chunk_depth, settings.bytes_per_sigop, + settings.maximum_outcomes)) + { + projected_ = false; + handler(fault(ec), {}); + return; + } + + // Arrivals are whole from the floor to the height of the open interval. + projected_ = true; + if (!arrived_ && mode != estimate_mode::basic) + { + 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, + heights, settings.bytes_per_sigop)) + { + handler(fault(ec), {}); + return; + } + + arrived_ = true; + } + + const auto arrivals = [&](uint64_t rate) NOEXCEPT + { + return arriving(rate, mode); + }; + + using algorithm = pool::algorithm; + const auto space = limit.size; + const auto rate = algorithm::estimate(projection_, count, space, arrivals); + if (rate == max_uint64) + { + handler(error::estimate_false, {}); + return; + } + + handler(error::success, std::max(rate, settings.minimum_fee_rate_())); +} + // utility // ---------------------------------------------------------------------------- @@ -513,6 +675,51 @@ code chaser_transaction::unconfirmable(const database::header_link& link, return ec; } +// The size arriving in a block at or above the rate. +uint64_t chaser_transaction::arriving(uint64_t rate, + estimate_mode mode) const NOEXCEPT +{ + using algorithm = pool::algorithm; + uint64_t size{}; + + switch (mode) + { + case estimate_mode::geometric: + { + for (const auto& takes: std::views::reverse(arrivals_)) + { + const auto above = algorithm::above(takes, rate); + size = to_half(ceilinged_add(size, above)); + } + + return size; + } + case estimate_mode::economical: + { + const auto window = economical_estimate_window; + const auto count = std::min(arrivals_.size(), window); + for (size_t age{}; age < count; ++age) + { + const auto above = algorithm::above(arrivals_.at(age), rate); + size = ceilinged_add(size, above); + } + + return is_zero(count) ? size : floored_divide(size, count); + } + case estimate_mode::conservative: + { + for (const auto& takes: arrivals_) + size = std::max(size, algorithm::above(takes, rate)); + + return size; + } + default: + { + return size; + } + } +} + // The fee and size are recomputed here, as they are for the package rate and // for block fees, so the rate could instead be cached on the transaction. size_t chaser_transaction::to_rate(const chain::transaction& tx) NOEXCEPT @@ -526,6 +733,25 @@ size_t chaser_transaction::to_rate(const chain::transaction& tx) NOEXCEPT return limit(floored_divide(rate, size)); } +// A block less its header, largest tx count and declared coinbase weight. +pool::algorithm::totals_t chaser_transaction::block_limit() const NOEXCEPT +{ + constexpr auto count = variable_size(max_block_size / min_transaction_size); + constexpr auto bytes = chain::header::serialized_size() + count; + constexpr auto fixed = light_weight_factor * bytes; + const auto& settings = node_settings().pool; + 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; + return + { + {}, + possible_narrow_cast(weight / light_weight_factor), + possible_narrow_cast(sigops) + }; +} + // validation // ---------------------------------------------------------------------------- @@ -547,6 +773,11 @@ code chaser_transaction::validate(size_t& index, const transaction_cptrs& txs, if (const auto ec = block::populate(txs, pool_, false)) return ec; + const auto& settings = node_settings().pool; + const auto bip16 = pool_.is_enabled(flags::bip16_rule); + const auto bip141 = pool_.is_enabled(flags::bip141_rule); + const auto bytes_per_sigop = settings.bytes_per_sigop; + uint64_t fee{}, size{}; for (index = zero; index < txs.size(); ++index) { @@ -557,19 +788,35 @@ code chaser_transaction::validate(size_t& index, const transaction_cptrs& txs, if (const auto ec = accept(tx)) return ec; + const auto sigops = tx.signature_operations(bip16, bip141); + const auto cost = ceilinged_multiply(sigops, bytes_per_sigop); fee = ceilinged_add(fee, tx.fee()); - size = ceilinged_add(size, possible_wide_cast( - tx.virtual_size())); + size = ceilinged_add(size, greater(tx.virtual_size(), cost)); } // Compared in satoshis per virtual kilobyte. - const auto rate = node_settings().minimum_fee_rate_(); + const auto rate = settings.minimum_fee_rate_(); if (ceilinged_multiply(fee, 1'000_u64) < ceilinged_multiply(rate, size)) { index = zero; return error::insufficient_fee; } + bool covered{}; + if (const auto ec = archive().get_pool_coverage(covered, txs, stored, + database::context::from(pool_), rate, settings.bytes_per_sigop, + block_limit(), settings.maximum_outcomes)) + { + index = zero; + return ec == database::error::pool_limit ? ec : fault(ec); + } + + if (!covered) + { + index = zero; + return error::insufficient_fee; + } + // Script validation is the most costly, so it follows the guards. for (index = zero; index < txs.size(); ++index) if (!stored.at(index)) @@ -596,7 +843,9 @@ code chaser_transaction::accept(const chain::transaction& tx) NOEXCEPT if (!ec) ec = tx.accept(pool_); if (!ec) ec = tx.accept_guard(pool_); if (!ec) ec = tx.confirm_guard(pool_); - return ec; + + // An unconfirmed conflict is admitted by coverage. + return ec == system::error::double_spend ? error::success : ec; } // A block tx is under proof of work, so it is not subject to policy (fee diff --git a/src/full_node.cpp b/src/full_node.cpp index 8b4a4534..9982aa32 100644 --- a/src/full_node.cpp +++ b/src/full_node.cpp @@ -195,6 +195,16 @@ void full_node::submit_compact(const system::chain::transactions_cptr& txs, chaser_transaction_.submit_compact(txs, links, link, std::move(handler)); } +void full_node::build_template(template_handler&& handler) NOEXCEPT +{ + chaser_transaction_.build_template(std::move(handler)); +} + +void full_node::get_template(template_handler&& handler) NOEXCEPT +{ + chaser_template_.get_template(std::move(handler)); +} + void full_node::get_hashes(map_handler&& handler) NOEXCEPT { chaser_download_.get_hashes(std::move(handler)); @@ -472,10 +482,10 @@ time_t full_node::start_time() const NOEXCEPT // Methods. // ---------------------------------------------------------------------------- -void full_node::estimate(size_t, estimate_mode, +void full_node::estimate(size_t target, estimate_mode mode, estimate_handler&& handler) NOEXCEPT { - handler(error::estimate_disabled, {}); + chaser_transaction_.estimate(target, mode, std::move(handler)); } void full_node::performance(object_key key, uint64_t speed, diff --git a/src/protocols/protocol.cpp b/src/protocols/protocol.cpp index 3be770d8..b974bf17 100644 --- a/src/protocols/protocol.cpp +++ b/src/protocols/protocol.cpp @@ -73,7 +73,7 @@ bool protocol::is_current_time(uint32_t timestamp) const NOEXCEPT uint64_t protocol::minimum_fee_rate() const NOEXCEPT { - return is_current_chain(true) ? node_settings().minimum_fee_rate_() : + return is_current_chain(true) ? node_settings().pool.minimum_fee_rate_() : system_settings().max_money(); } diff --git a/src/protocols/protocol_transaction_in_106.cpp b/src/protocols/protocol_transaction_in_106.cpp index f20040df..4b071c12 100644 --- a/src/protocols/protocol_transaction_in_106.cpp +++ b/src/protocols/protocol_transaction_in_106.cpp @@ -200,10 +200,12 @@ void protocol_transaction_in_106::do_handle_submit(const code& ec, // against the presumed next block, so neither lock is misbehavior. // The rate and suspension cannot be advertised below bip133, so a tx that // violates either is not misbehavior (see protocol_transaction_in_70013). + // The peer cannot know the extent of a conflict component held here. if (ec && (ec != error::pooling_disabled) && (ec != error::insufficient_fee) && (ec != error::duplicate_transaction) && + (ec != database::error::pool_limit) && (ec != system::error::double_spend) && (ec != system::error::absolute_time_locked) && (ec != system::error::relative_time_locked) && diff --git a/src/settings.cpp b/src/settings.cpp index e0098769..9791344b 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -27,8 +27,26 @@ using namespace bc::network; namespace libbitcoin { namespace node { +settings::transaction_pool::transaction_pool() NOEXCEPT + : minimum_fee_rate{ 0.000001 }, + maximum_outcomes{ 1'024 }, + bytes_per_sigop{ 20 }, + coinbase_weight{ 2'000 }, + maximum_chunk_depth{ 25 } +{ +} + +// The configured rate is btc/vkb, as reported to clients (see relayfee). +uint64_t settings::transaction_pool::minimum_fee_rate_() const NOEXCEPT +{ + // Ceilinged, as a configured minimum is not rounded down. + return to_ceilinged_integer(minimum_fee_rate * + chain::satoshi_per_bitcoin); +} + settings::settings() NOEXCEPT : network{ chain::selection::none }, + pool{}, threads{ 0 }, delay_inbound{ true }, thread_priority{ true }, @@ -43,7 +61,6 @@ settings::settings() NOEXCEPT cpu_batch_verify{ false }, cpu_batch_silent{ false }, allowed_deviation{ 1.5 }, - minimum_fee_rate{ 0.000001 }, batch_verify{ 1'000'000 }, batch_silent{ 1'000'000 }, current_connections{ 10 }, @@ -103,14 +120,6 @@ size_t settings::maximum_concurrency_() const NOEXCEPT return to_bool(maximum_concurrency) ? maximum_concurrency : max_size_t; } -// The configured rate is btc/vkb, as reported to clients (see relayfee). -uint64_t settings::minimum_fee_rate_() const NOEXCEPT -{ - // Ceilinged, as a configured minimum is not rounded down. - return to_ceilinged_integer(minimum_fee_rate * - chain::satoshi_per_bitcoin); -} - uint64_t settings::services_provided() const NOEXCEPT { using namespace network::messages::peer; diff --git a/test/chasers/chaser_template.cpp b/test/chasers/chaser_template.cpp index 9b541102..c4a5ed30 100644 --- a/test/chasers/chaser_template.cpp +++ b/test/chasers/chaser_template.cpp @@ -17,12 +17,185 @@ * along with this program. If not, see . */ #include "../test.hpp" +#include "../functional/p2p_setup_fixture.hpp" +#include -BC_AUTO_TEST_SUITE(chaser_template_tests) +using namespace system; -BC_AUTO_TEST_CASE(chaser_template_test) +static constexpr uint64_t parent_value = 100'000; + +// An unconfirmed tx with an anyone-can-spend output. +static const chain::transaction& parent() NOEXCEPT +{ + const chain::point point{ one_hash, 0u }; + const chain::operations ops{ { chain::opcode::push_positive_1 } }; + const chain::script spendable{ ops }; + static const chain::transaction instance + { + 1u, + chain::inputs{ { point, chain::script{}, max_uint32 } }, + chain::outputs{ { parent_value, spendable } }, + 0u + }; + + return instance; +} + +static chain::transaction::cptr spend(const hash_digest& hash, + uint64_t value) NOEXCEPT +{ + const chain::point point{ hash, 0u }; + const chain::operations ops{ { chain::opcode::push_positive_1 } }; + const chain::script spendable{ ops }; + return std::make_shared(chain::transaction + { + 1u, + chain::inputs{ { point, chain::script{}, max_uint32 } }, + chain::outputs{ { value, spendable } }, + 0u + }); +} + +static chain::transaction::cptr spend(uint64_t value) NOEXCEPT +{ + return spend(parent().hash(false), value); +} + +static chain::transactions_cptr package( + const chain::transaction_cptrs& txs) NOEXCEPT { - BC_REQUIRE(true); + return std::make_shared(txs); +} + +static bool stored_parent(node::query& query) NOEXCEPT +{ + return query.set(parent()); +} + +BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) + +struct chaser_template_setup_fixture + : p2p_setup_fixture +{ + using built = std::tuple; + + inline chaser_template_setup_fixture() + : p2p_setup_fixture(stored_parent, [](configuration& config) + { + config.network.enable_relay = true; + config.node.currency_window_minutes = 0; + }) + { + subscribe(); + } + + code submit(const chain::transactions_cptr& txs) + { + std::promise promise{}; + node_.submit(txs, false, [&](const code& ec, size_t) NOEXCEPT + { + promise.set_value(ec); + }); + + return promise.get_future().get(); + } + + built get_template() + { + std::promise promise{}; + node_.get_template([&](const code& ec, size_t height, + const database::tx_links& links) NOEXCEPT + { + promise.set_value({ ec, height, links }); + }); + + return promise.get_future().get(); + } + + // True when a template event leaves the expected template (bounded). + bool templated(const database::tx_links& expected) + { + node::chases::template_ event{}; + while (await(event)) + if (std::get<2>(get_template()) == expected) + return true; + + return false; + } + + database::tx_link link(const chain::transaction::cptr& tx) + { + return query_.to_tx(tx->hash(false)); + } +}; + +BC_FIXTURE_TEST_SUITE(chaser_template_tests, chaser_template_setup_fixture) + +BC_AUTO_TEST_CASE(chaser_template__get_template__started__empty) +{ + const auto [ec, height, links] = get_template(); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE(links.empty()); +} + +BC_AUTO_TEST_CASE(chaser_template__transaction__single__templated_at_next_height) +{ + const auto tx = spend(parent_value); + const auto ec = submit(package({ tx })); + const auto templated_tx = templated({ link(tx) }); + const auto height = std::get<1>(get_template()); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE(templated_tx); + BC_REQUIRE_EQUAL(height, 1u); +} + +BC_AUTO_TEST_CASE(chaser_template__transaction__parent_and_child__parent_first) +{ + const auto first = spend(parent_value); + const auto second = spend(first->hash(false), parent_value); + const auto ec = submit(package({ first, second })); + const auto templated_both = templated({ link(first), link(second) }); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE(templated_both); +} + +BC_AUTO_TEST_CASE(chaser_template__transaction__separate_packages__both_templated) +{ + const auto first = spend(parent_value); + const auto second = spend(first->hash(false), parent_value); + const auto first_ec = submit(package({ first })); + const auto second_ec = submit(package({ second })); + const auto templated_both = templated({ link(first), link(second) }); + BC_REQUIRE_EQUAL(first_ec, node::error::success); + BC_REQUIRE_EQUAL(second_ec, node::error::success); + BC_REQUIRE(templated_both); +} + +BC_AUTO_TEST_CASE(chaser_template__transaction__conflict__higher_fee_only) +{ + const auto first = spend(parent_value); + const auto second = spend(sub1(parent_value)); + const auto first_ec = submit(package({ first })); + const auto second_ec = submit(package({ second })); + const auto templated_rival = templated({ link(second) }); + BC_REQUIRE_EQUAL(first_ec, node::error::success); + BC_REQUIRE_EQUAL(second_ec, node::error::success); + BC_REQUIRE(templated_rival); +} + +BC_AUTO_TEST_CASE(chaser_template__transaction__child_of_losing_rival__rival_only) +{ + const auto first = spend(parent_value); + const auto child = spend(first->hash(false), parent_value); + const auto second = spend(sub1(parent_value)); + const auto first_ec = submit(package({ first, child })); + const auto second_ec = submit(package({ second })); + const auto templated_rival = templated({ link(second) }); + BC_REQUIRE_EQUAL(first_ec, node::error::success); + BC_REQUIRE_EQUAL(second_ec, node::error::success); + BC_REQUIRE(templated_rival); } BC_AUTO_TEST_SUITE_END() + +BC_POP_WARNING() diff --git a/test/chasers/chaser_transaction.cpp b/test/chasers/chaser_transaction.cpp index 4b8bde2c..65b75d28 100644 --- a/test/chasers/chaser_transaction.cpp +++ b/test/chasers/chaser_transaction.cpp @@ -193,6 +193,95 @@ struct chaser_transaction_setup_fixture return promise.get_future().get(); } + using built = std::tuple; + built build_template() + { + std::promise promise{}; + node_.build_template([&](const code& ec, size_t height, + const database::tx_links& links) NOEXCEPT + { + promise.set_value({ ec, height, links }); + }); + + return promise.get_future().get(); + } + + using estimated = std::pair; + estimated estimate(size_t target, + node::estimate_mode mode=node::estimate_mode::basic) + { + std::promise promise{}; + node_.estimate(target, mode, [&](const code& ec, uint64_t rate) NOEXCEPT + { + promise.set_value({ ec, rate }); + }); + + return promise.get_future().get(); + } + + // Confirm a block of only a coinbase above genesis. + database::header_link confirm() + { + const system::settings bitcoin{ chain::selection::mainnet }; + const auto& genesis = bitcoin.genesis_block; + const auto time = add1(genesis.header().timestamp()); + const auto bits = genesis.header().bits(); + const chain::transaction coinbase + { + 1u, + chain::inputs{ { chain::point{}, chain::script{}, max_uint32 } }, + chain::outputs{ { 42u, chain::script{} } }, + 0u + }; + const chain::header header + { + 1u, genesis.hash(), null_hash, time, bits, 0u + }; + const chain::block block{ header, { coinbase } }; + if (!query_.set(block, database::context{ 0, 1, 0 }, {}, false, true)) + return {}; + + const auto link = query_.to_header(block.hash()); + if (!query_.push_candidate(link) || !query_.push_confirmed(link, true)) + return {}; + + node_.notify(node::error::success, chases::organized{ link }); + return link; + } + + // True once the pool context is of the height. + bool bumped(size_t height) + { + using namespace std::chrono; + const auto deadline = steady_clock::now() + seconds(10); + while (steady_clock::now() < deadline) + { + if (std::get<1>(build_template()) == height) + return true; + + std::this_thread::sleep_for(milliseconds(10)); + } + + return false; + } + + // True once the estimate is the rate. + bool estimated_at(size_t target, node::estimate_mode mode, uint64_t rate) + { + using namespace std::chrono; + const auto deadline = steady_clock::now() + seconds(10); + while (steady_clock::now() < deadline) + { + const auto result = estimate(target, mode); + if (!result.first && result.second == rate) + return true; + + std::this_thread::sleep_for(milliseconds(10)); + } + + return false; + } + bool suspended() { using namespace std::chrono; @@ -269,6 +358,47 @@ struct chaser_transaction_setup_fixture } } + // Announce the tx, true if the node requests it, then send it. + bool relay(const chain::transaction::cptr& tx) + { + const auto hash = tx->hash(false); + const auto version = node_version->value; + send(peer::inventory{ { { type_id::transaction, hash } } }, version); + if (receive(peer::get_data::command).empty()) + return false; + + send(peer::transaction{ tx }, version); + return true; + } + + // The tx hashes announced to the active peer, to the count. + hashes announced(size_t count) + { + hashes out{}; + const auto version = node_version->value; + while (out.size() < count) + { + const auto payload = receive(peer::inventory::command); + const auto message = peer::inventory::deserialize(version, payload); + BC_REQUIRE(message); + for (const auto& item: message->items) + out.push_back(item.hash); + } + + return out; + } + + // The hash of the tx served to the active peer upon request. + hash_digest served(const hash_digest& hash) + { + const auto version = node_version->value; + send(peer::get_data{ { { type_id::transaction, hash } } }, version); + const auto payload = receive(peer::transaction::command); + const auto message = peer::transaction::deserialize(version, payload); + BC_REQUIRE(message); + return message->transaction_ptr->hash(false); + } + // Wait (bounded) for the command, pinging until it is received. data_chunk await_before_pong(const std::string& command) { @@ -285,6 +415,20 @@ struct chaser_transaction_setup_fixture } }; +struct chaser_transaction_distribution_setup_fixture + : chaser_transaction_setup_fixture +{ + inline chaser_transaction_distribution_setup_fixture() + : chaser_transaction_setup_fixture([](configuration& config) + { + config.network.enable_relay = true; + config.network.inbound.connections = 2; + config.node.currency_window_minutes = 0; + }) + { + } +}; + struct chaser_transaction_relay_setup_fixture : chaser_transaction_setup_fixture { @@ -318,7 +462,50 @@ struct chaser_transaction_fee_setup_fixture { config.network.enable_relay = true; config.node.currency_window_minutes = 0; - config.node.minimum_fee_rate = 0.0000152587890625; + config.node.pool.minimum_fee_rate = 0.0000152587890625; + }) + { + } +}; + +struct chaser_transaction_limit_setup_fixture + : chaser_transaction_setup_fixture +{ + inline chaser_transaction_limit_setup_fixture() + : chaser_transaction_setup_fixture([](configuration& config) + { + config.network.enable_relay = true; + config.node.currency_window_minutes = 0; + config.node.pool.maximum_outcomes = 1; + }) + { + } +}; + +struct chaser_transaction_reserve_setup_fixture + : chaser_transaction_setup_fixture +{ + inline chaser_transaction_reserve_setup_fixture() + : chaser_transaction_setup_fixture([](configuration& config) + { + config.network.enable_relay = true; + config.node.currency_window_minutes = 0; + config.node.pool.coinbase_weight = 4'000'000; + }) + { + } +}; + +// A block holds one test tx (61 of its 80 virtual bytes). +struct chaser_transaction_single_setup_fixture + : chaser_transaction_setup_fixture +{ + inline chaser_transaction_single_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; }) { } @@ -535,10 +722,51 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__submit__valid_twice__duplicate_transact BC_REQUIRE_EQUAL(submit(package({ tx })).first, node::error::duplicate_transaction); } -BC_FIXTURE_TEST_CASE(chaser_transaction__submit__conflict__double_spend, chaser_transaction_pooling_setup_fixture) +BC_FIXTURE_TEST_CASE(chaser_transaction__submit__conflict__success_archived, chaser_transaction_pooling_setup_fixture) +{ + const auto first = spend(parent_value); + const auto second = spend(sub1(parent_value)); + const auto first_result = submit(package({ first })); + const auto second_result = submit(package({ second })); + BC_REQUIRE_EQUAL(first_result.first, node::error::success); + BC_REQUIRE_EQUAL(second_result.first, node::error::success); + BC_REQUIRE(query_.is_tx(first->hash(false))); + BC_REQUIRE(query_.is_tx(second->hash(false))); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__submit__conflict_no_added_fee__insufficient_fee, chaser_transaction_fee_setup_fixture) +{ + const auto first = submit(package({ spend(parent_value - 1'000) })); + const auto second = submit(package({ spend(parent_value - 999) })); + BC_REQUIRE_EQUAL(first.first, node::error::success); + BC_REQUIRE_EQUAL(second.first, node::error::insufficient_fee); + BC_REQUIRE_EQUAL(second.second, zero); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__submit__conflict_added_fee__success_archived, chaser_transaction_fee_setup_fixture) +{ + const auto second = spend(parent_value - 2'000); + const auto first_result = submit(package({ spend(parent_value - 1'000) })); + const auto second_result = submit(package({ second })); + BC_REQUIRE_EQUAL(first_result.first, node::error::success); + BC_REQUIRE_EQUAL(second_result.first, node::error::success); + BC_REQUIRE(query_.is_tx(second->hash(false))); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__submit__conflict_outcomes_over_limit__pool_limit, chaser_transaction_limit_setup_fixture) +{ + const auto first = submit(package({ spend(parent_value) })); + const auto second = submit(package({ spend(sub1(parent_value)) })); + BC_REQUIRE_EQUAL(first.first, node::error::success); + BC_REQUIRE_EQUAL(second.first, database::error::pool_limit); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__submit__conflict_over_block__pool_limit, chaser_transaction_reserve_setup_fixture) { - BC_REQUIRE_EQUAL(submit(package({ spend(parent_value) })).first, node::error::success); - BC_REQUIRE_EQUAL(submit(package({ spend(sub1(parent_value)) })).first, system::error::double_spend); + const auto first = submit(package({ spend(parent_value - 1'000) })); + const auto second = submit(package({ spend(parent_value - 2'000) })); + BC_REQUIRE_EQUAL(first.first, node::error::success); + BC_REQUIRE_EQUAL(second.first, database::error::pool_limit); } BC_FIXTURE_TEST_CASE(chaser_transaction__submit__parent_and_child__success_archived, chaser_transaction_pooling_setup_fixture) @@ -581,6 +809,236 @@ BC_FIXTURE_TEST_CASE(chaser_transaction__submit__internal_relative_lock__relativ BC_REQUIRE(!query_.is_tx(first->hash(false))); } +// build_template + +BC_FIXTURE_TEST_CASE(chaser_transaction__build_template__relay_disabled__pooling_disabled, chaser_transaction_setup_fixture) +{ + const auto [ec, height, links] = build_template(); + BC_REQUIRE_EQUAL(ec, node::error::pooling_disabled); + BC_REQUIRE(links.empty()); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__build_template__empty_pool__empty_at_next_height, chaser_transaction_pooling_setup_fixture) +{ + const auto [ec, height, links] = build_template(); + BC_REQUIRE_EQUAL(ec, database::error::success); + BC_REQUIRE_EQUAL(height, 1u); + BC_REQUIRE(links.empty()); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__build_template__parent_and_child__parent_first, chaser_transaction_pooling_setup_fixture) +{ + const auto first = spend(parent_value); + const auto second = spend(first->hash(false), parent_value); + const auto result = submit(package({ first, second })); + const auto [ec, height, links] = build_template(); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE_EQUAL(ec, database::error::success); + BC_REQUIRE_EQUAL(links.size(), 2u); + BC_REQUIRE_EQUAL(links.front(), query_.to_tx(first->hash(false))); + BC_REQUIRE_EQUAL(links.back(), query_.to_tx(second->hash(false))); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__build_template__conflict__higher_fee_only, chaser_transaction_pooling_setup_fixture) +{ + const auto first = spend(parent_value); + const auto second = spend(sub1(parent_value)); + const auto first_result = submit(package({ first })); + const auto second_result = submit(package({ second })); + const auto [ec, height, links] = build_template(); + BC_REQUIRE_EQUAL(first_result.first, node::error::success); + BC_REQUIRE_EQUAL(second_result.first, node::error::success); + BC_REQUIRE_EQUAL(ec, database::error::success); + BC_REQUIRE_EQUAL(links.size(), 1u); + BC_REQUIRE_EQUAL(links.front(), query_.to_tx(second->hash(false))); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__build_template__child_of_losing_rival__excluded, chaser_transaction_pooling_setup_fixture) +{ + const auto first = spend(parent_value); + const auto child = spend(first->hash(false), parent_value); + const auto second = spend(sub1(parent_value)); + const auto first_result = submit(package({ first, child })); + const auto second_result = submit(package({ second })); + const auto [ec, height, links] = build_template(); + BC_REQUIRE_EQUAL(first_result.first, node::error::success); + BC_REQUIRE_EQUAL(second_result.first, node::error::success); + BC_REQUIRE_EQUAL(ec, database::error::success); + BC_REQUIRE_EQUAL(links.size(), 1u); + BC_REQUIRE_EQUAL(links.front(), query_.to_tx(second->hash(false))); +} + +// estimate + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__relay_disabled__estimate_disabled, chaser_transaction_setup_fixture) +{ + BC_REQUIRE_EQUAL(estimate(0).first, node::error::estimate_disabled); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__not_current__estimate_premature, chaser_transaction_relay_setup_fixture) +{ + BC_REQUIRE_EQUAL(estimate(0).first, node::error::estimate_premature); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__unknown_mode__estimate_false, chaser_transaction_pooling_setup_fixture) +{ + BC_REQUIRE_EQUAL(estimate(0, node::estimate_mode::unknown).first, node::error::estimate_false); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__target_at_horizon__estimate_false, chaser_transaction_pooling_setup_fixture) +{ + BC_REQUIRE_EQUAL(estimate(node::maximum_estimate_horizon).first, node::error::estimate_false); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__no_block_space__estimate_false, chaser_transaction_reserve_setup_fixture) +{ + BC_REQUIRE_EQUAL(estimate(0).first, node::error::estimate_false); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__empty_pool__zero, chaser_transaction_pooling_setup_fixture) +{ + const auto [ec, rate] = estimate(0); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE_EQUAL(rate, 0u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__empty_pool_minimum_fee_rate__minimum_fee_rate, chaser_transaction_fee_setup_fixture) +{ + const auto [ec, rate] = estimate(0); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE_EQUAL(rate, 1'526u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__full_block__above_lowest_take, chaser_transaction_single_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ first, second })); + const auto [ec, rate] = estimate(0); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE_EQUAL(rate, 16'394u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__open_block__zero, chaser_transaction_single_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ first, second })); + const auto [ec, rate] = estimate(1); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE_EQUAL(rate, 0u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__after_template__continued, chaser_transaction_single_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ first, second })); + const auto [built_ec, height, links] = build_template(); + const auto next = estimate(0); + const auto after = estimate(1); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE_EQUAL(built_ec, database::error::success); + BC_REQUIRE_EQUAL(links.size(), 1u); + BC_REQUIRE_EQUAL(next.first, node::error::success); + BC_REQUIRE_EQUAL(next.second, 16'394u); + BC_REQUIRE_EQUAL(after.first, node::error::success); + BC_REQUIRE_EQUAL(after.second, 0u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__pool_written__projected_again, chaser_transaction_single_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); + 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, 16'394u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__basic_after_block__no_arrivals, chaser_transaction_single_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ first, second })); + const auto confirmed = !confirm().is_terminal(); + const auto current = bumped(2); + const auto [ec, rate] = estimate(1, node::estimate_mode::basic); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(confirmed); + BC_REQUIRE(current); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE_EQUAL(rate, 0u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__economical_after_block__arrivals_fill_open_block, chaser_transaction_single_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ first, second })); + const auto confirmed = !confirm().is_terminal(); + const auto current = bumped(2); + const auto [ec, rate] = estimate(1, node::estimate_mode::economical); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(confirmed); + BC_REQUIRE(current); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE_EQUAL(rate, 16'394u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__conservative_after_block__arrivals_fill_open_block, chaser_transaction_single_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ first, second })); + const auto confirmed = !confirm().is_terminal(); + const auto current = bumped(2); + const auto [ec, rate] = estimate(1, node::estimate_mode::conservative); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(confirmed); + BC_REQUIRE(current); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE_EQUAL(rate, 16'394u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__estimate__geometric_after_block__arrivals_fill_open_block, chaser_transaction_single_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ first, second })); + const auto confirmed = !confirm().is_terminal(); + const auto current = bumped(2); + const auto [ec, rate] = estimate(1, node::estimate_mode::geometric); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(confirmed); + BC_REQUIRE(current); + BC_REQUIRE_EQUAL(ec, node::error::success); + BC_REQUIRE_EQUAL(rate, 16'394u); +} + +BC_FIXTURE_TEST_CASE(chaser_transaction__pruned__arrivals_below_floor__not_counted, chaser_transaction_single_setup_fixture) +{ + const auto first = spend(parent_value - 1'000); + const auto second = spend(first->hash(false), parent_value - 3'000); + const auto result = submit(package({ first, second })); + const auto link = confirm(); + const auto current = bumped(2); + const auto counted = estimate(1, node::estimate_mode::economical); + node_.notify(node::error::success, chases::pruned{ link }); + BC_REQUIRE_EQUAL(result.first, node::error::success); + BC_REQUIRE(current); + BC_REQUIRE_EQUAL(counted.second, 16'394u); + BC_REQUIRE(estimated_at(1, node::estimate_mode::economical, 0)); +} + // do_bump BC_FIXTURE_TEST_CASE(chaser_transaction__organized__invalid_confirmed_top__suspended_pooling_disabled, chaser_transaction_pooling_setup_fixture) @@ -987,6 +1445,96 @@ BC_FIXTURE_TEST_CASE(protocol_transaction_in__transaction__requested_insufficien // protocol_transaction_out +// distribution (tx in, acceptance, tx out) + +BC_FIXTURE_TEST_CASE(protocol_transaction__distribution__peer_tx__accepted_announced_and_served, chaser_transaction_distribution_setup_fixture) +{ + const auto tx = spend(parent_value); + const auto hash = tx->hash(false); + const hashes expected{ hash }; + BC_REQUIRE(handshake(0, peer::level::maximum_protocol, true)); + + const auto sink = connect(); + BC_REQUIRE(handshake(0, peer::level::maximum_protocol, true)); + BC_REQUIRE(!received_before_pong(peer::inventory::command)); + + select(zero); + BC_REQUIRE(relay(tx)); + BC_REQUIRE(archived(hash)); + BC_REQUIRE(pooled(query_.to_tx(hash))); + BC_REQUIRE(!received_before_pong(peer::inventory::command)); + + select(sink); + BC_REQUIRE_EQUAL(announced(1), expected); + BC_REQUIRE_EQUAL(served(hash), hash); +} + +BC_FIXTURE_TEST_CASE(protocol_transaction__distribution__peer_chain__announced_in_order, chaser_transaction_distribution_setup_fixture) +{ + const auto first = spend(parent_value); + const auto second = spend(first->hash(false), parent_value); + const auto third = spend(second->hash(false), parent_value); + const hashes expected{ first->hash(false), second->hash(false), third->hash(false) }; + BC_REQUIRE(handshake(0, peer::level::maximum_protocol, true)); + + const auto sink = connect(); + BC_REQUIRE(handshake(0, peer::level::maximum_protocol, true)); + BC_REQUIRE(!received_before_pong(peer::inventory::command)); + + select(zero); + BC_REQUIRE(relay(first)); + BC_REQUIRE(archived(first->hash(false))); + BC_REQUIRE(relay(second)); + BC_REQUIRE(archived(second->hash(false))); + BC_REQUIRE(relay(third)); + BC_REQUIRE(archived(third->hash(false))); + + select(sink); + BC_REQUIRE_EQUAL(announced(3), expected); + BC_REQUIRE_EQUAL(served(third->hash(false)), third->hash(false)); +} + +BC_FIXTURE_TEST_CASE(protocol_transaction__distribution__peer_conflict__both_accepted_and_announced, chaser_transaction_distribution_setup_fixture) +{ + const auto first = spend(parent_value); + const auto second = spend(sub1(parent_value)); + const hashes expected{ first->hash(false), second->hash(false) }; + BC_REQUIRE(handshake(0, peer::level::maximum_protocol, true)); + + const auto sink = connect(); + BC_REQUIRE(handshake(0, peer::level::maximum_protocol, true)); + BC_REQUIRE(!received_before_pong(peer::inventory::command)); + + select(zero); + BC_REQUIRE(relay(first)); + BC_REQUIRE(archived(first->hash(false))); + BC_REQUIRE(relay(second)); + BC_REQUIRE(archived(second->hash(false))); + + select(sink); + BC_REQUIRE_EQUAL(announced(2), expected); +} + +BC_FIXTURE_TEST_CASE(protocol_transaction__distribution__submitted_tx__announced_to_every_peer, chaser_transaction_distribution_setup_fixture) +{ + const auto tx = spend(parent_value); + const hashes expected{ tx->hash(false) }; + BC_REQUIRE(handshake(0, peer::level::maximum_protocol, true)); + BC_REQUIRE(!received_before_pong(peer::inventory::command)); + + const auto other = connect(); + BC_REQUIRE(handshake(0, peer::level::maximum_protocol, true)); + BC_REQUIRE(!received_before_pong(peer::inventory::command)); + + BC_REQUIRE_EQUAL(submit(package({ tx })).first, node::error::success); + + select(zero); + BC_REQUIRE_EQUAL(announced(1), expected); + + select(other); + BC_REQUIRE_EQUAL(announced(1), expected); +} + BC_FIXTURE_TEST_CASE(protocol_transaction_out__start__minimum_fee_rate__fee_filter, chaser_transaction_fee_setup_fixture) { BC_REQUIRE(handshake(0, peer::level::maximum_protocol, true)); diff --git a/test/full_node.cpp b/test/full_node.cpp index 5962fa6f..b43e082e 100644 --- a/test/full_node.cpp +++ b/test/full_node.cpp @@ -464,8 +464,11 @@ BC_AUTO_TEST_CASE(full_node__submit__relay_disabled__pooling_disabled) BC_REQUIRE_EQUAL(promise.get_future().get(), error::pooling_disabled); } -BC_AUTO_TEST_CASE(full_node__estimate__disabled__estimate_disabled) +BC_AUTO_TEST_CASE(full_node__estimate__relay_disabled__estimate_disabled) { + BC_REQUIRE(initialize()); + BC_REQUIRE(!start()); + std::promise promise{}; node_.estimate(1, estimate_mode::basic, [&](const code& ec, uint64_t) NOEXCEPT { diff --git a/test/functional/p2p_setup_fixture.cpp b/test/functional/p2p_setup_fixture.cpp index fc763340..c231ba6b 100644 --- a/test/functional/p2p_setup_fixture.cpp +++ b/test/functional/p2p_setup_fixture.cpp @@ -100,12 +100,14 @@ p2p_setup_fixture::p2p_setup_fixture(const initializer& setup, // Block until the node is running. ec = running.get_future().get(); BC_REQUIRE_MESSAGE(!ec, ec.message()); - REQUIRE_SUCCESS(socket_, connect, node_.binding()); + connect(); } p2p_setup_fixture::~p2p_setup_fixture() { - socket_.close(); + for (auto& peer: sockets_) + peer.close(); + node_.close(); const auto ec = store_.close([](auto, auto) {}); BC_WARN_MESSAGE(!ec, ec.message()); @@ -123,19 +125,19 @@ void p2p_setup_fixture::send(const std::string& command, std::copy(payload.begin(), payload.end(), std::next(frame.begin(), heading::size())); - test::write(socket_, network::io::buffer(frame)); + test::write(socket(), network::io::buffer(frame)); } std::pair p2p_setup_fixture::receive() { data_array head_data{}; - test::read(socket_, network::io::buffer(head_data)); + test::read(socket(), network::io::buffer(head_data)); const auto head = heading::deserialize(head_data); BC_REQUIRE(head); data_chunk payload(head->payload_size); if (!payload.empty()) - test::read(socket_, network::io::buffer(payload)); + test::read(socket(), network::io::buffer(payload)); return { head->command, std::move(payload) }; } @@ -191,14 +193,14 @@ bool p2p_setup_fixture::dropped(const std::string& command) { code ec{}; data_array head_data{}; - network::io::read(socket_, network::io::buffer(head_data), ec); + network::io::read(socket(), network::io::buffer(head_data), ec); if (ec) return true; const auto head = heading::deserialize(head_data); BC_REQUIRE(head); data_chunk payload(head->payload_size); - network::io::read(socket_, network::io::buffer(payload), ec); + network::io::read(socket(), network::io::buffer(payload), ec); if (ec) return true; @@ -207,6 +209,24 @@ bool p2p_setup_fixture::dropped(const std::string& command) } } +size_t p2p_setup_fixture::connect() +{ + auto& peer = sockets_.emplace_back(io_); + REQUIRE_SUCCESS(peer, connect, node_.binding()); + active_ = sub1(sockets_.size()); + return active_; +} + +void p2p_setup_fixture::select(size_t peer) +{ + active_ = peer; +} + +network::io::socket& p2p_setup_fixture::socket() +{ + return sockets_.at(active_); +} + bool p2p_setup_fixture::handshake(uint64_t services, uint32_t value, bool relay, bool witness_tx) { diff --git a/test/functional/p2p_setup_fixture.hpp b/test/functional/p2p_setup_fixture.hpp index 93ed6b50..be5d19cb 100644 --- a/test/functional/p2p_setup_fixture.hpp +++ b/test/functional/p2p_setup_fixture.hpp @@ -103,6 +103,12 @@ struct p2p_setup_fixture /// The node's version message (set by handshake). network::messages::peer::version::cptr node_version{}; + /// Connect another peer to the node, made the active peer. + size_t connect(); + + /// Make the peer at the index the active peer (the first is zero). + void select(size_t peer); + /// Subscribe to chaser events, returning the subscription key. object_key subscribe(); @@ -147,8 +153,11 @@ struct p2p_setup_fixture bound_node node_; private: + network::io::socket& socket(); + network::io::context io_{}; - network::io::socket socket_{ io_ }; + std::deque sockets_{}; + size_t active_{}; std::shared_ptr subscription_ { std::make_shared() diff --git a/test/settings.cpp b/test/settings.cpp index 56bec043..1e7b1f22 100644 --- a/test/settings.cpp +++ b/test/settings.cpp @@ -44,8 +44,6 @@ BC_AUTO_TEST_CASE(settings__node__default_context__expected) BC_REQUIRE_EQUAL(node.use_gpu, true); BC_REQUIRE_EQUAL(node.cpu_batch_verify, false); BC_REQUIRE_EQUAL(node.cpu_batch_silent, false); - BC_REQUIRE_EQUAL(node.minimum_fee_rate, 0.000001); - BC_REQUIRE_EQUAL(node.minimum_fee_rate_(), 100_u64); BC_REQUIRE_EQUAL(node.allowed_deviation, 1.5); BC_REQUIRE_EQUAL(node.batch_verify, 1'000'000_u64); BC_REQUIRE_EQUAL(node.batch_silent, 1'000'000_u64); @@ -203,4 +201,17 @@ BC_AUTO_TEST_CASE(settings__batch_silent___cpu_unconfigured__false) BC_REQUIRE(!node.batch_silent_()); } +// [pool] + +BC_AUTO_TEST_CASE(settings__pool__default__expected) +{ + const node::settings::transaction_pool pool{}; + BC_REQUIRE_EQUAL(pool.minimum_fee_rate, 0.000001); + BC_REQUIRE_EQUAL(pool.minimum_fee_rate_(), 100_u64); + 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.maximum_chunk_depth, 25_u32); +} + BC_AUTO_TEST_SUITE_END() diff --git a/test/test.cpp b/test/test.cpp index d03a30c0..f66b05b8 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -187,7 +187,7 @@ node::settings node_settings() NOEXCEPT out.network = system::chain::selection::mainnet; out.batch_verify = 0; out.batch_silent = 0; - out.minimum_fee_rate = 0.0; + out.pool.minimum_fee_rate = 0.0; out.maximum_concurrency = 50'000; return out; }