diff --git a/builds/gnu/Makefile.am b/builds/gnu/Makefile.am index 9e5e4910a..99bf2dd27 100644 --- a/builds/gnu/Makefile.am +++ b/builds/gnu/Makefile.am @@ -56,7 +56,6 @@ src_libbitcoin_node_la_SOURCES = \ ${srcdir}/../../src/configuration.cpp \ ${srcdir}/../../src/define.cpp \ ${srcdir}/../../src/error.cpp \ - ${srcdir}/../../src/estimator.cpp \ ${srcdir}/../../src/full_node.cpp \ ${srcdir}/../../src/rolling_bloom_filter.cpp \ ${srcdir}/../../src/settings.cpp \ @@ -64,7 +63,6 @@ src_libbitcoin_node_la_SOURCES = \ ${srcdir}/../../src/chasers/chaser.cpp \ ${srcdir}/../../src/chasers/chaser_confirm.cpp \ ${srcdir}/../../src/chasers/chaser_download.cpp \ - ${srcdir}/../../src/chasers/chaser_estimate.cpp \ ${srcdir}/../../src/chasers/chaser_header.cpp \ ${srcdir}/../../src/chasers/chaser_snapshot.cpp \ ${srcdir}/../../src/chasers/chaser_storage.cpp \ @@ -138,7 +136,6 @@ include_bitcoin_node_HEADERS = \ ${srcdir}/../../include/bitcoin/node/configuration.hpp \ ${srcdir}/../../include/bitcoin/node/define.hpp \ ${srcdir}/../../include/bitcoin/node/error.hpp \ - ${srcdir}/../../include/bitcoin/node/estimator.hpp \ ${srcdir}/../../include/bitcoin/node/events.hpp \ ${srcdir}/../../include/bitcoin/node/full_node.hpp \ ${srcdir}/../../include/bitcoin/node/preprocessor.hpp \ @@ -161,7 +158,6 @@ include_bitcoin_node_chasers_HEADERS = \ ${srcdir}/../../include/bitcoin/node/chasers/chaser.hpp \ ${srcdir}/../../include/bitcoin/node/chasers/chaser_confirm.hpp \ ${srcdir}/../../include/bitcoin/node/chasers/chaser_download.hpp \ - ${srcdir}/../../include/bitcoin/node/chasers/chaser_estimate.hpp \ ${srcdir}/../../include/bitcoin/node/chasers/chaser_header.hpp \ ${srcdir}/../../include/bitcoin/node/chasers/chaser_snapshot.hpp \ ${srcdir}/../../include/bitcoin/node/chasers/chaser_storage.hpp \ @@ -243,7 +239,6 @@ test_libbitcoin_node_test_SOURCES = \ ${srcdir}/../../test/channel_peer.cpp \ ${srcdir}/../../test/configuration.cpp \ ${srcdir}/../../test/error.cpp \ - ${srcdir}/../../test/estimator.cpp \ ${srcdir}/../../test/full_node.cpp \ ${srcdir}/../../test/main.cpp \ ${srcdir}/../../test/rolling_bloom_filter.cpp \ @@ -252,7 +247,6 @@ test_libbitcoin_node_test_SOURCES = \ ${srcdir}/../../test/chasers/chaser.cpp \ ${srcdir}/../../test/chasers/chaser_confirm.cpp \ ${srcdir}/../../test/chasers/chaser_download.cpp \ - ${srcdir}/../../test/chasers/chaser_estimate.cpp \ ${srcdir}/../../test/chasers/chaser_header.cpp \ ${srcdir}/../../test/chasers/chaser_template.cpp \ ${srcdir}/../../test/chasers/chaser_transaction.cpp \ diff --git a/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj b/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj index f54709a7b..313638091 100644 --- a/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj @@ -100,14 +100,12 @@ - - diff --git a/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj.filters index 6dd6efb47..8356746cd 100644 --- a/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj.filters @@ -42,9 +42,6 @@ src\chasers - - src\chasers - src\chasers @@ -63,9 +60,6 @@ src - - src - src diff --git a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj index 5f83ffca3..28d5f23c1 100644 --- a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj @@ -119,7 +119,6 @@ - @@ -132,7 +131,6 @@ - @@ -177,7 +175,6 @@ - @@ -188,7 +185,6 @@ - diff --git a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters index cc479dd57..0fbfea37e 100644 --- a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters @@ -69,9 +69,6 @@ src\chasers - - src\chasers - src\chasers @@ -108,9 +105,6 @@ src - - src - src @@ -239,9 +233,6 @@ include\bitcoin\node\chasers - - include\bitcoin\node\chasers - include\bitcoin\node\chasers @@ -272,9 +263,6 @@ include\bitcoin\node - - include\bitcoin\node - include\bitcoin\node diff --git a/include/bitcoin/node.hpp b/include/bitcoin/node.hpp index f174b9f8f..ec0898123 100644 --- a/include/bitcoin/node.hpp +++ b/include/bitcoin/node.hpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -35,7 +34,6 @@ #include #include #include -#include #include #include #include diff --git a/include/bitcoin/node/chase.hpp b/include/bitcoin/node/chase.hpp index f8f9e8379..73c898e55 100644 --- a/include/bitcoin/node/chase.hpp +++ b/include/bitcoin/node/chase.hpp @@ -150,11 +150,11 @@ enum class chase /// ----------------------------------------------------------------------- /// A current block has been organized. - /// Issued by 'confirm' and handled by 'protocol_header/block_out/estimator'. + /// Issued by 'confirm' and handled by 'protocol_header/block_out'. block, /// The store has been pruned (once, following the first current block). - /// Issued by 'snapshot' and handled by 'estimate'. + /// Issued by 'snapshot' and handled by none. pruned, /// The confirmed chain is no longer current. diff --git a/include/bitcoin/node/chasers/chaser_estimate.hpp b/include/bitcoin/node/chasers/chaser_estimate.hpp deleted file mode 100644 index 10b178368..000000000 --- a/include/bitcoin/node/chasers/chaser_estimate.hpp +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Copyright (c) 2011-2026 libbitcoin developers - * - * This file is part of libbitcoin. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_ESTIMATE_HPP -#define LIBBITCOIN_NODE_CHASERS_CHASER_ESTIMATE_HPP - -#include -#include -#include -#include - -namespace libbitcoin { -namespace node { - -class full_node; - -/// Maintain a running fee estimate cache. -class BCN_API chaser_estimate - : public chaser -{ -public: - DELETE_COPY_MOVE_DESTRUCT(chaser_estimate); - - chaser_estimate(full_node& node) NOEXCEPT; - - code start() NOEXCEPT override; - void stopping(const code& ec) NOEXCEPT override; - - /// Returns max_uint64 when disabled. - void estimate(size_t target, estimator::mode mode, - estimate_handler&& handler) NOEXCEPT; - - /// Returns zero when disabled (thread safe). - size_t top_height() const NOEXCEPT; - - /// Initialization is complete and successful. - bool initialized() const NOEXCEPT; - -protected: - virtual bool handle_chase(const code& ec, event_value value) NOEXCEPT; - - virtual void do_pruned(header_t link) NOEXCEPT; - virtual void do_initialize(header_t link) NOEXCEPT; - virtual void do_organized(header_t link) NOEXCEPT; - virtual void do_reorganized(header_t link) NOEXCEPT; - virtual void do_transaction(transaction_t link) NOEXCEPT; - -private: - void do_estimate(size_t target, estimator::mode mode, - const estimate_handler& handler) NOEXCEPT; - - // These are thread safe. - std::atomic_bool stopping_{}; - std::atomic_bool initialized_{}; - - // These are protected by strand. - estimator::ptr estimator_{}; - bool pruned_{}; -}; - -} // namespace node -} // namespace libbitcoin - -#endif diff --git a/include/bitcoin/node/chasers/chasers.hpp b/include/bitcoin/node/chasers/chasers.hpp index 7ebbb9ab4..921bb6bf7 100644 --- a/include/bitcoin/node/chasers/chasers.hpp +++ b/include/bitcoin/node/chasers/chasers.hpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include diff --git a/include/bitcoin/node/define.hpp b/include/bitcoin/node/define.hpp index 481dc3d54..74467ebdf 100644 --- a/include/bitcoin/node/define.hpp +++ b/include/bitcoin/node/define.hpp @@ -34,6 +34,15 @@ namespace node { typedef std::error_code code; /// Estimate types. +enum class estimate_mode +{ + basic, + geometric, + economical, + conservative, + unknown +}; +constexpr size_t maximum_estimate_horizon = 1008; typedef std::function estimate_handler; /// Organization types. diff --git a/include/bitcoin/node/error.hpp b/include/bitcoin/node/error.hpp index 95f1fc555..6f2b18434 100644 --- a/include/bitcoin/node/error.hpp +++ b/include/bitcoin/node/error.hpp @@ -126,12 +126,6 @@ enum error_t : uint8_t transaction7, transaction8, transaction9, - estimates_initialize, - estimates_push1, - estimates_push2, - estimates_pop1, - estimates_pop2, - estimates_pool, batch1, batch2, batch3, diff --git a/include/bitcoin/node/estimator.hpp b/include/bitcoin/node/estimator.hpp deleted file mode 100644 index 65983d684..000000000 --- a/include/bitcoin/node/estimator.hpp +++ /dev/null @@ -1,190 +0,0 @@ -/** - * Copyright (c) 2011-2026 libbitcoin developers - * - * This file is part of libbitcoin. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -#ifndef LIBBITCOIN_NODE_ESTIMATOR_HPP -#define LIBBITCOIN_NODE_ESTIMATOR_HPP - -#include -#include - -namespace libbitcoin { -namespace node { - -/// Fee estimator with contained accumulator. -/// Accumulator is typically too large for stack creation. -/// Thread safe, blocking calls to estimate() during updates. -/// Initialize on chain current coalesce after snapshot/prune. -/// If chain falls > 1008/2 (?) blocks behind, reset and wait for coalesce. -/// Only txs pooled before confirmation contribute, by blocks waited. -class BCN_API estimator -{ -public: - typedef std::unique_ptr ptr; - static constexpr size_t maximum_horizon = 1008; - static constexpr uint64_t estimate_failed = max_uint64; - - DELETE_COPY_MOVE_DESTRUCT(estimator); - - /// Estimation modes. - enum class mode - { - basic, - geometric, - economical, - conservative, - unknown - }; - - /// Construct (use heap allocation). - estimator() NOEXCEPT {}; - - /// Fee estimation in satoshis/transaction virtual size (not thread safe). - /// Pass zero to target next block for confirmation, range:0..1007. - uint64_t estimate(size_t target, mode mode) const NOEXCEPT; - - /// Populate accumulator with count blocks up to the top confirmed block, - /// and with the unconfirmed pooled transactions. - bool initialize(const std::atomic_bool& cancel, const query& query, - size_t count=maximum_horizon) NOEXCEPT; - - /// Update accumulator (not thread safe). - bool push(const query& query, const database::header_link& link) NOEXCEPT; - bool pool(const query& query, const database::tx_link& link) NOEXCEPT; - bool pop(const query& query, const database::header_link& link) NOEXCEPT; - - /// Top height of accumulator (thread safe). - size_t top_height() const NOEXCEPT; - -protected: - using rate = database::fee_rate; - using rates = database::fee_rates; - using rate_sets = database::fee_rate_sets; - using bins = std::array; - - /// Bucket depth sizing parameters (number of blocks). - enum horizon : size_t - { - /// 2 hrs × 60 mins/hr / 10 mins/block = 12 blocks. - small = 12, - - /// 8 hrs × 60 mins/hr / 10 mins/block = 48 blocks. - medium = 48, - - /// 7 days * 24 hrs/day × 60 mins/hr / 10 mins/block = 1008 blocks. - large = maximum_horizon - }; - - /// Bucket count sizing parameters. - struct sizing - { - static constexpr double min = 0.1; - static constexpr double max = 100'000.0; - static constexpr double step = 1.05; - - /// Derived from min/max/step above. - static constexpr size_t count = std::tuple_size_v; - }; - - /// Estimation confidences. - struct confidence - { - static constexpr double low = 0.60; - static constexpr double mid = 0.85; - static constexpr double high = 0.95; - }; - - /// Accumulator (persistent, decay-weighted counters). - struct accumulator - { - template - struct bucket - { - /// Total scaled txs in bucket. - size_t total{}; - - /// confirmed[n]: scaled txs not confirmed within n+1 blocks. - std::array confirmed; - }; - - /// Current block height of accumulated state. - std::atomic top_height{}; - - /// Accumulated scaled fee in decayed buckets by horizon. - /// Array count is the half life of the decay it implies. - std::array, sizing::count> small{}; - std::array, sizing::count> medium{}; - std::array, sizing::count> large{}; - - /// Unconfirmed pooled txs by bin, in slots of pool entry height. - std::array pending{}; - std::array entries{}; - - /// Unconfirmed pooled txs displaced from the pending slots. - bins aged{}; - }; - - // C++23: make consteval. - static inline double decay_rate() NOEXCEPT - { - static const auto rate = std::pow(0.5, 1.0 / sizing::count); - return rate; - } - - // C++23: make constexpr. - static inline double to_scale_term(size_t age) NOEXCEPT - { - return system::power(decay_rate(), age); - } - - // C++23: make constexpr. - static inline double to_scale_factor(bool push) NOEXCEPT - { - return std::pow(decay_rate(), push ? +1.0 : -1.0); - } - - /// Bin of the tx rate, false if the tx is below the minimum rate. - static bool to_bin(size_t& out, const rate& tx) NOEXCEPT; - - accumulator& history() NOEXCEPT; - const accumulator& history() const NOEXCEPT; - bool initialize(const rate_sets& blocks, const rates& pooled, - size_t top) NOEXCEPT; - bool push(const rates& block) NOEXCEPT; - bool pop(const rates& block) NOEXCEPT; - bool pool(const rate& tx) NOEXCEPT; - void conflict(const rates& txs, bool push) NOEXCEPT; - uint64_t compute(size_t target, double confidence, - bool geometric=false) const NOEXCEPT; - -private: - using waits = std::array; - - bool update(const rates& block, size_t height, bool push) NOEXCEPT; - void pend(size_t bin, size_t height, bool enter) NOEXCEPT; - void pending(bins& failed, size_t target) const NOEXCEPT; - void decay(auto& buckets, double factor) NOEXCEPT; - void decay(bool push) NOEXCEPT; - - accumulator fees_{}; - std::array waits_{}; -}; - -} // namespace node -} // namespace libbitcoin - -#endif diff --git a/include/bitcoin/node/full_node.hpp b/include/bitcoin/node/full_node.hpp index db1b3ff03..2cc086ff3 100644 --- a/include/bitcoin/node/full_node.hpp +++ b/include/bitcoin/node/full_node.hpp @@ -22,7 +22,6 @@ #include #include #include -#include #include namespace libbitcoin { @@ -172,7 +171,7 @@ class BCN_API full_node /// ----------------------------------------------------------------------- /// Get current fee estimate. - void estimate(size_t target, estimator::mode mode, + void estimate(size_t target, estimate_mode mode, estimate_handler&& handler) NOEXCEPT; /// Handle performance, base returns false (implied terminate). @@ -213,7 +212,6 @@ class BCN_API full_node chaser_confirm chaser_confirm_; chaser_transaction chaser_transaction_; chaser_template chaser_template_; - chaser_estimate chaser_estimate_; chaser_snapshot chaser_snapshot_; chaser_storage chaser_storage_; event_subscriber event_subscriber_{}; diff --git a/include/bitcoin/node/protocols/protocol.hpp b/include/bitcoin/node/protocols/protocol.hpp index d16deccf4..6becde4d4 100644 --- a/include/bitcoin/node/protocols/protocol.hpp +++ b/include/bitcoin/node/protocols/protocol.hpp @@ -22,7 +22,6 @@ #include #include #include -#include // Only session.hpp. #include @@ -96,7 +95,7 @@ class BCN_API protocol network::net::channel_notifier&& handler) NOEXCEPT; /// Get current fee estimate. - void estimate(size_t target, estimator::mode mode, + void estimate(size_t target, estimate_mode mode, estimate_handler&& handler) NOEXCEPT; /// Suspensions. diff --git a/include/bitcoin/node/sessions/session.hpp b/include/bitcoin/node/sessions/session.hpp index c8a2bd063..0ce71d379 100644 --- a/include/bitcoin/node/sessions/session.hpp +++ b/include/bitcoin/node/sessions/session.hpp @@ -21,7 +21,6 @@ #include #include -#include namespace libbitcoin { namespace node { @@ -104,7 +103,7 @@ class BCN_API session network::net::channel_notifier&& handler) NOEXCEPT; /// Get current fee estimate. - void estimate(size_t target, estimator::mode mode, + void estimate(size_t target, estimate_mode mode, estimate_handler&& handler) NOEXCEPT; /// Handle performance, base returns false (implied terminate). diff --git a/include/bitcoin/node/settings.hpp b/include/bitcoin/node/settings.hpp index b50284140..5da7343e2 100644 --- a/include/bitcoin/node/settings.hpp +++ b/include/bitcoin/node/settings.hpp @@ -55,7 +55,6 @@ class BCN_API settings uint64_t batch_verify; uint64_t batch_silent; uint16_t current_connections; - uint16_t fee_estimate_horizon; uint32_t maximum_height; uint32_t maximum_concurrency; uint32_t silent_start_height; @@ -71,11 +70,9 @@ 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 size_t fee_estimate_horizon_() 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 fee_estimate_enabled() const NOEXCEPT; virtual bool use_gpu_() const NOEXCEPT; virtual bool batch_verify_enabled() const NOEXCEPT; virtual bool batch_silent_enabled() const NOEXCEPT; diff --git a/src/chasers/chaser_estimate.cpp b/src/chasers/chaser_estimate.cpp deleted file mode 100644 index e0cdb0a91..000000000 --- a/src/chasers/chaser_estimate.cpp +++ /dev/null @@ -1,275 +0,0 @@ -/** - * Copyright (c) 2011-2026 libbitcoin developers - * - * This file is part of libbitcoin. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -#include - -#include -#include -#include -#include -#include - -namespace libbitcoin { -namespace node { - -#define CLASS chaser_estimate - -using namespace system; -using namespace std::placeholders; - -BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) - -chaser_estimate::chaser_estimate(full_node& node) NOEXCEPT - : chaser(node) -{ -} - -// start/stop -// ---------------------------------------------------------------------------- - -code chaser_estimate::start() NOEXCEPT -{ - if (node_settings().fee_estimate_enabled()) - { - SUBSCRIBE_CHASE(handle_chase, _1, _2); - } - - return error::success; -} - -void chaser_estimate::stopping(const code& ec) NOEXCEPT -{ - stopping_.store(true); - chaser::stopping(ec); -} - -// methods -// ---------------------------------------------------------------------------- - -void chaser_estimate::estimate(size_t target, estimator::mode mode, - estimate_handler&& handler) NOEXCEPT -{ - if (!node_settings().fee_estimate_enabled()) - { - handler(error::estimate_disabled, {}); - return; - } - - POST(do_estimate, target, mode, std::move(handler)); -} - -// private -void chaser_estimate::do_estimate(size_t target, estimator::mode mode, - const estimate_handler& handler) NOEXCEPT -{ - BC_ASSERT(stranded()); - - // Check this under strand so that chase can initialize first. - if (!initialized()) - { - handler(error::estimate_premature, {}); - return; - } - - const auto value = estimator_->estimate(target, mode); - const auto ec = (value < to_unsigned(max_int64) ? error::success : - error::estimate_false); - - // Successful value is always castable to int64_t. - handler(ec, value); -} - -size_t chaser_estimate::top_height() const NOEXCEPT -{ - return initialized() ? estimator_->top_height() : zero; -} - -bool chaser_estimate::initialized() const NOEXCEPT -{ - return initialized_.load(std::memory_order_relaxed); -} - -// event handlers -// ---------------------------------------------------------------------------- -// protected - -bool chaser_estimate::handle_chase(const code&, event_value value) NOEXCEPT -{ - if (closed()) - return false; - - // Keep updating the fee accumulator (blocks continue organizing). - ////if (suspended()) - //// return true; - - switch (to_chase(value)) - { - // Initialization follows the prune (which rewrites the pool) and - // retries on each current block until the chain reaches the horizon. - case chase::pruned: - { - if (!initialized()) - { - POST(do_pruned, to_payload(value).link); - } - - break; - } - case chase::block: - { - if (!initialized()) - { - POST(do_initialize, to_payload(value).link); - } - - break; - } - case chase::organized: - { - if (initialized()) - { - POST(do_organized, - to_payload(value).link); - } - - break; - } - case chase::reorganized: - { - if (initialized()) - { - POST(do_reorganized, - to_payload(value).link); - } - - break; - } - case chase::transaction: - { - if (initialized()) - { - POST(do_transaction, - to_payload(value).link); - } - - break; - } - case chase::stop: - { - return false; - } - default: - { - break; - } - } - - return true; -} - -void chaser_estimate::do_pruned(header_t link) NOEXCEPT -{ - BC_ASSERT(stranded()); - pruned_ = true; - do_initialize(link); -} - -void chaser_estimate::do_initialize(header_t) NOEXCEPT -{ - BC_ASSERT(stranded()); - - if (!pruned_ || initialized()) - return; - - // Preempt initialize fault when horizon exceeds chain length. - const auto horizon = node_settings().fee_estimate_horizon_(); - if (horizon > add1(archive().get_top_confirmed())) - return; - - // Heap-allocate the estimator due to size. - estimator_ = std::make_unique(); - if (!estimator_->initialize(stopping_, archive(), horizon)) - { - fault(error::estimates_initialize); - estimator_.reset(); - return; - } - - initialized_.store(true, std::memory_order_relaxed); -} - -void chaser_estimate::do_organized(header_t link) NOEXCEPT -{ - BC_ASSERT(stranded()); - - if (initialized()) - { - const auto& query = archive(); - const auto height = query.get_height(link); - - if (height.is_terminal()) - { - fault(error::estimates_push1); - return; - } - - // Organization events backlog during initialization. - if (height.value <= estimator_->top_height()) - return; - - if (!estimator_->push(query, link)) - fault(error::estimates_push2); - } -} - -void chaser_estimate::do_reorganized(header_t link) NOEXCEPT -{ - BC_ASSERT(stranded()); - - if (initialized()) - { - const auto& query = archive(); - const auto height = query.get_height(link); - - if (height.is_terminal()) - { - fault(error::estimates_pop1); - return; - } - - // Organization events backlog during initialization. - if (height.value > estimator_->top_height()) - return; - - if (!estimator_->pop(query, link)) - fault(error::estimates_pop2); - } -} - -void chaser_estimate::do_transaction(transaction_t link) NOEXCEPT -{ - BC_ASSERT(stranded()); - - if (initialized() && !estimator_->pool(archive(), link)) - fault(error::estimates_pool); -} - -BC_POP_WARNING() - -} // namespace node -} // namespace libbitcoin diff --git a/src/define.cpp b/src/define.cpp index 329eac31b..0df2b1708 100644 --- a/src/define.cpp +++ b/src/define.cpp @@ -35,8 +35,7 @@ // block_memory : block_arena // settings : define // configuration : settings -// estimator : define -// /chasers : configuration estimator [forward: full_node] +// /chasers : configuration [forward: full_node] // /channels : configuration // /protocols : /channels /chasers [sessions/session.hpp] // /sessions : /protocols [forward: full_node] diff --git a/src/error.cpp b/src/error.cpp index b8b32f127..519ede079 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -116,12 +116,6 @@ DEFINE_ERROR_T_MESSAGE_MAP(error) { transaction7, "transaction7" }, { transaction8, "transaction8" }, { transaction9, "transaction9" }, - { estimates_initialize, "estimates_initialize" }, - { estimates_push1, "estimates_push1" }, - { estimates_push2, "estimates_push2" }, - { estimates_pop1, "estimates_pop1" }, - { estimates_pop2, "estimates_pop2" }, - { estimates_pool, "estimates_pool" }, { batch1, "batch1" }, { batch2, "batch2" }, { batch3, "batch3" }, diff --git a/src/estimator.cpp b/src/estimator.cpp deleted file mode 100644 index 904c794a4..000000000 --- a/src/estimator.cpp +++ /dev/null @@ -1,437 +0,0 @@ -/** - * Copyright (c) 2011-2026 libbitcoin developers - * - * This file is part of libbitcoin. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -#include - -#include -#include -#include -#include - -namespace libbitcoin { -namespace node { - -using namespace system; - -// public -// ---------------------------------------------------------------------------- - -uint64_t estimator::estimate(size_t target, mode mode) const NOEXCEPT -{ - constexpr size_t large = horizon::large; - if (target >= large) - return estimate_failed; - - // Valid results are effectively limited to at least 1 sat/vb. - // threshold_fee is thread safe but values are affected during update. - switch (mode) - { - case mode::basic: - { - return compute(target, confidence::high); - } - case mode::geometric: - { - return compute(target, confidence::high, true); - } - case mode::economical: - { - const auto target1 = to_half(target); - const auto target2 = target; - const auto target3 = std::min(large, two * target); - const auto fee1 = compute(target1, confidence::low); - const auto fee2 = compute(target2, confidence::mid); - const auto fee3 = compute(target3, confidence::high); - return std::max({ fee1, fee2, fee3 }); - } - case mode::conservative: - { - const auto target1 = to_half(target); - const auto target2 = target; - const auto target3 = std::min(large, two * target); - const auto fee1 = compute(target1, confidence::low); - const auto fee2 = compute(target2, confidence::mid); - const auto fee3 = compute(target3, confidence::high); - return std::max({ fee1, fee2, fee3 }); - } - default: - case mode::unknown: - { - return estimate_failed; - } - } -} - -bool estimator::initialize(const std::atomic_bool& cancel, const query& query, - size_t count) NOEXCEPT -{ - if (is_zero(count)) - return true; - - const auto top = query.get_top_confirmed(); - if (count > add1(top)) - return false; - - rates pooled{}; - rate_sets blocks{}; - const auto start = top - sub1(count); - return query.get_branch_fees(cancel, blocks, start, count) && - query.get_confirmable_fees(pooled) && initialize(blocks, pooled, top); -} - -bool estimator::push(const query& query, - const database::header_link& link) NOEXCEPT -{ - if (is_add_overflow(top_height(), one)) - return false; - - rates block{}, conflicts{}; - if (!query.get_block_fees(block, link) || - !query.get_block_conflict_fees(conflicts, link) || !push(block)) - return false; - - conflict(conflicts, true); - return true; -} - -bool estimator::pop(const query& query, - const database::header_link& link) NOEXCEPT -{ - if (is_subtract_overflow(top_height(), one)) - return false; - - rates block{}, conflicts{}; - if (!query.get_block_fees(block, link) || - !query.get_block_conflict_fees(conflicts, link)) - return false; - - conflict(conflicts, false); - return pop(block); -} - -bool estimator::pool(const query& query, const database::tx_link& link) NOEXCEPT -{ - rate tx{}; - return query.get_tx_fees(tx, link) && pool(tx); -} - -size_t estimator::top_height() const NOEXCEPT -{ - return fees_.top_height.load(std::memory_order_relaxed); -} - -// protected -// ---------------------------------------------------------------------------- - -bool estimator::to_bin(size_t& out, const rate& tx) NOEXCEPT -{ - // std::log (replace static with constexpr in c++26). - static const auto growth = std::log(sizing::step); - - if (is_zero(tx.fee)) - return false; - - const auto rate = to_floating(tx.fee) / tx.bytes; - if (rate < sizing::min) - return false; - - // Clamp overflow to last bin. - const auto bin = std::log(rate / sizing::min) / growth; - out = std::min(to_floored_integer(bin), sub1(sizing::count)); - return true; -} - -estimator::accumulator& estimator::history() NOEXCEPT -{ - return fees_; -} - -const estimator::accumulator& estimator::history() const NOEXCEPT -{ - return fees_; -} - -// Blocks end at the top height, pooled txs are entered as unconfirmed. -bool estimator::initialize(const rate_sets& blocks, const rates& pooled, - size_t top) NOEXCEPT -{ - const auto count = blocks.size(); - if (count > add1(top)) - return false; - - fees_.top_height.store(top, std::memory_order_relaxed); - auto height = add1(top) - count; - - // 3-4 secs slower when parallel at 1008 blocks. - for (const auto& block: blocks) - if (!update(block, height++, true)) - return false; - - for (const auto& tx: pooled) - if (!pool(tx)) - return false; - - return true; -} - -// Blocks must be pushed in order (but independent of chain index). -bool estimator::push(const rates& block) NOEXCEPT -{ - decay(true); - fees_.top_height.fetch_add(one, std::memory_order_relaxed); - return update(block, top_height(), true); -} - -// Blocks must be pushed in order (but independent of chain index). -bool estimator::pop(const rates& block) NOEXCEPT -{ - const auto result = update(block, top_height(), false); - decay(false); - fees_.top_height.fetch_sub(one, std::memory_order_relaxed); - return result; -} - -// A tx is pending from its pool entry height until confirmed. -bool estimator::pool(const rate& tx) NOEXCEPT -{ - if (is_zero(tx.bytes)) - return false; - - // The store does not pool, so there is no entry height. - if (tx.pooled_height == max_size_t) - return true; - - size_t bin{}; - if (to_bin(bin, tx)) - pend(bin, tx.pooled_height, true); - - return true; -} - -// Txs conflicted by a block leave pending when it is pushed, reenter on pop. -void estimator::conflict(const rates& txs, bool push) NOEXCEPT -{ - for (const auto& tx: txs) - { - size_t bin{}; - if (tx.pooled_height != max_size_t && to_bin(bin, tx)) - pend(bin, tx.pooled_height, !push); - } -} - -uint64_t estimator::compute(size_t target, double confidence, - bool geometric) const NOEXCEPT -{ - const auto threshold = [](double part, double total, size_t) NOEXCEPT - { - return part / total; - }; - - // Geometric distribution approximation, not a full Markov process. - const auto markov = [](double part, double total, size_t target) NOEXCEPT - { - return power(part / total, target); - }; - - // Pending txs that have waited beyond the target have failed it. - bins failed{}; - pending(failed, target); - - const auto call = [&](const auto& buckets) NOEXCEPT - { - BC_PUSH_WARNING(NO_UNGUARDED_POINTERS) - const auto& contribution = geometric ? markov : threshold; - BC_POP_WARNING() - - constexpr auto magic_number = 2u; - const auto at_least_four = magic_number * add1(target); - double total{}, part{}; - auto index = buckets.size(); - auto found = index; - for (const auto& bucket: std::views::reverse(buckets)) - { - --index; - total += to_floating(bucket.total + failed.at(index)); - part += to_floating(bucket.confirmed.at(target) + failed.at(index)); - if (total < at_least_four) - continue; - - if (contribution(part, total, target) > (1.0 - confidence)) - break; - - found = index; - } - - if (found == buckets.size()) - return max_uint64; - - const auto minimum = sizing::min * std::pow(sizing::step, found); - return to_ceilinged_integer(minimum); - }; - - if (target < horizon::small) return call(fees_.small); - if (target < horizon::medium) return call(fees_.medium); - if (target < horizon::large) return call(fees_.large); - return max_uint64; -} - -// private -// ---------------------------------------------------------------------------- - -void estimator::decay(bool push) NOEXCEPT -{ - const auto factor = to_scale_factor(push); - decay(fees_.large, factor); - decay(fees_.medium, factor); - decay(fees_.small, factor); -} - -void estimator::decay(auto& buckets, double factor) NOEXCEPT -{ - for (auto& bucket: buckets) - { - bucket.total = to_floored_integer(bucket.total * factor); - for (auto& count: bucket.confirmed) - count = to_floored_integer(count * factor); - } -} - -bool estimator::update(const rates& block, size_t height, bool push) NOEXCEPT -{ - constexpr auto large = maximum_horizon; - bins counts{}; - - for (const auto& tx: block) - { - if (is_zero(tx.bytes)) - return false; - - // A tx not pooled before its block has no wait. - if (tx.pooled_height == max_size_t) - continue; - - size_t bin{}; - if (!to_bin(bin, tx)) - continue; - - // Blocks waited beyond the first eligible, capped at the horizon. - const auto waited = std::min(floored_subtract(height, tx.pooled_height), large); - ++counts.at(bin); - ++waits_.at(bin).at(waited); - pend(bin, tx.pooled_height, !push); - } - - // At age zero scale term is one. - const auto age = top_height() - height; - const auto scale = to_scale_term(age); - const auto term = [&](size_t count) NOEXCEPT - { - const auto scaled = to_floored_integer(count * scale); - return push ? scaled : twos_complement(scaled); - }; - - const auto call = [&](auto& buckets) NOEXCEPT - { - // The array count of the buckets element type. - const auto horizon = buckets.front().confirmed.size(); - - for (size_t bin{}; bin < counts.size(); ++bin) - { - const auto count = counts.at(bin); - if (is_zero(count)) - continue; - - auto& bucket = buckets.at(bin); - const auto& wait = waits_.at(bin); - bucket.total += term(count); - - // Txs that waited beyond the target failed it. - size_t failing{}; - for (auto waited = add1(horizon); waited <= large; ++waited) - failing += wait.at(waited); - - for (auto target = horizon; !is_zero(target); --target) - { - failing += wait.at(target); - bucket.confirmed.at(sub1(target)) += term(failing); - } - } - }; - - call(fees_.large); - call(fees_.medium); - call(fees_.small); - - for (size_t bin{}; bin < counts.size(); ++bin) - if (!is_zero(counts.at(bin))) - waits_.at(bin).fill(zero); - - return true; -} - -// A slot holds the txs of one entry height, displacing those of an earlier. -void estimator::pend(size_t bin, size_t height, bool enter) NOEXCEPT -{ - const auto slot = height % horizon::large; - auto& entry = fees_.entries.at(slot); - auto& counts = fees_.pending.at(slot); - - if (entry != height) - { - // Displaced to aged (leaving), or displaced by the slot (entering). - if (!enter || (entry > height)) - { - auto& aged = fees_.aged.at(bin); - aged = enter ? add1(aged) : floored_subtract(aged, one); - return; - } - - for (size_t index{}; index < counts.size(); ++index) - fees_.aged.at(index) += counts.at(index); - - counts.fill(zero); - entry = height; - } - - auto& count = counts.at(bin); - count = enter ? add1(count) : floored_subtract(count, one); -} - -// Pending txs by bin that have waited beyond the target blocks. -void estimator::pending(bins& failed, size_t target) const NOEXCEPT -{ - const auto top = top_height(); - for (size_t slot{}; slot < horizon::large; ++slot) - { - // Eligible blocks passed without confirmation. - const auto entry = fees_.entries.at(slot); - const auto waited = (top < entry) ? zero : add1(top - entry); - if (waited <= target) - continue; - - const auto& counts = fees_.pending.at(slot); - for (size_t bin{}; bin < counts.size(); ++bin) - failed.at(bin) += counts.at(bin); - } - - for (size_t bin{}; bin < failed.size(); ++bin) - failed.at(bin) += fees_.aged.at(bin); -} - -} // namespace node -} // namespace libbitcoin diff --git a/src/full_node.cpp b/src/full_node.cpp index 656902a96..8b4a45347 100644 --- a/src/full_node.cpp +++ b/src/full_node.cpp @@ -20,7 +20,6 @@ #include #include -#include #include namespace libbitcoin { @@ -47,7 +46,6 @@ full_node::full_node(query& query, const configuration& configuration, chaser_confirm_(*this), chaser_transaction_(*this), chaser_template_(*this), - chaser_estimate_(*this), chaser_snapshot_(*this), chaser_storage_(*this) { @@ -83,7 +81,6 @@ void full_node::do_start(const result_handler& handler) NOEXCEPT ((ec = chaser_confirm_.start())) || ((ec = chaser_transaction_.start())) || ((ec = chaser_template_.start())) || - ((ec = chaser_estimate_.start())) || ((ec = chaser_snapshot_.start())) || ((ec = chaser_storage_.start()))) { @@ -130,7 +127,6 @@ void full_node::close() NOEXCEPT chaser_confirm_.stop(); chaser_transaction_.stop(); chaser_template_.stop(); - chaser_estimate_.stop(); chaser_snapshot_.stop(); chaser_storage_.stop(); } @@ -147,7 +143,6 @@ void full_node::do_close() NOEXCEPT chaser_confirm_.stopping(network::error::service_stopped); chaser_transaction_.stopping(network::error::service_stopped); chaser_template_.stopping(network::error::service_stopped); - chaser_estimate_.stopping(network::error::service_stopped); chaser_snapshot_.stopping(network::error::service_stopped); chaser_storage_.stopping(network::error::service_stopped); @@ -477,10 +472,10 @@ time_t full_node::start_time() const NOEXCEPT // Methods. // ---------------------------------------------------------------------------- -void full_node::estimate(size_t target, estimator::mode mode, +void full_node::estimate(size_t, estimate_mode, estimate_handler&& handler) NOEXCEPT { - chaser_estimate_.estimate(target, mode, std::move(handler)); + handler(error::estimate_disabled, {}); } void full_node::performance(object_key key, uint64_t speed, diff --git a/src/protocols/protocol.cpp b/src/protocols/protocol.cpp index 4429ff53b..3be770d8c 100644 --- a/src/protocols/protocol.cpp +++ b/src/protocols/protocol.cpp @@ -95,7 +95,7 @@ size_t protocol::inbound_channel_count() const NOEXCEPT // Methods. // ---------------------------------------------------------------------------- -void protocol::estimate(size_t target, estimator::mode mode, +void protocol::estimate(size_t target, estimate_mode mode, estimate_handler&& handler) NOEXCEPT { session_->estimate(target, mode, std::move(handler)); diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp index 630396745..786af6871 100644 --- a/src/sessions/session.cpp +++ b/src/sessions/session.cpp @@ -125,7 +125,7 @@ void session::unsubscribe_chase(object_key key) NOEXCEPT // Methods. // ---------------------------------------------------------------------------- -void session::estimate(size_t target, estimator::mode mode, +void session::estimate(size_t target, estimate_mode mode, estimate_handler&& handler) NOEXCEPT { node_.estimate(target, mode, std::move(handler)); diff --git a/src/settings.cpp b/src/settings.cpp index 02f1d4f46..e0098769c 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -20,7 +20,6 @@ #include #include -#include using namespace bc::system; using namespace bc::network; @@ -48,7 +47,6 @@ settings::settings() NOEXCEPT batch_verify{ 1'000'000 }, batch_silent{ 1'000'000 }, current_connections{ 10 }, - fee_estimate_horizon{ 0 }, ////snapshot_bytes{ 200'000'000'000 }, ////snapshot_valid{ 250'000 }, ////snapshot_confirm{ 500'000 }, @@ -113,11 +111,6 @@ uint64_t settings::minimum_fee_rate_() const NOEXCEPT chain::satoshi_per_bitcoin); } -size_t settings::fee_estimate_horizon_() const NOEXCEPT -{ - return std::min(fee_estimate_horizon, estimator::maximum_horizon); -} - uint64_t settings::services_provided() const NOEXCEPT { using namespace network::messages::peer; @@ -153,11 +146,6 @@ uint64_t settings::services_required() const NOEXCEPT return services; } -bool settings::fee_estimate_enabled() const NOEXCEPT -{ - return to_bool(fee_estimate_horizon_()); -} - bool settings::use_gpu_() const NOEXCEPT { return use_gpu && system::batched::accelerated(); diff --git a/test/chasers/chaser_estimate.cpp b/test/chasers/chaser_estimate.cpp deleted file mode 100644 index 2b89f347b..000000000 --- a/test/chasers/chaser_estimate.cpp +++ /dev/null @@ -1,360 +0,0 @@ -/** - * Copyright (c) 2011-2026 libbitcoin developers - * - * This file is part of libbitcoin. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -#include "../test.hpp" -#include "../functional/p2p_setup_fixture.hpp" -#include -#include -#include - -using namespace system; - -BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) - -struct chaser_estimate_setup_fixture - : p2p_setup_fixture -{ - using result = std::pair; - - // Mainnet block 1 is confirmed. - static bool confirm1(node::query& query) NOEXCEPT - { - const system::settings bitcoin{ chain::selection::mainnet }; - const auto& genesis = bitcoin.genesis_block.header(); - const auto& block1 = p2p_compact_setup_fixture::block1(); - const database::context context1{ 0, 1, genesis.timestamp() }; - const auto work1 = genesis.proof() + block1.header().proof(); - return query.set(block1, context1, work1, false, true) && - query.push_candidate(query.to_header(block1.hash())) && - query.push_confirmed(query.to_header(block1.hash()), true); - } - - // Mainnet block 1 is a candidate, so the store is not coalesced. - static bool candidate1(node::query& query) NOEXCEPT - { - const system::settings bitcoin{ chain::selection::mainnet }; - const auto& genesis = bitcoin.genesis_block.header(); - const auto& block1 = p2p_compact_setup_fixture::block1(); - const database::context context1{ 0, 1, genesis.timestamp() }; - const auto work1 = genesis.proof() + block1.header().proof(); - return query.set(block1, context1, work1, false, true) && - query.push_candidate(query.to_header(block1.hash())); - } - - inline chaser_estimate_setup_fixture(uint16_t horizon, - const initializer& setup=confirm1) - : p2p_setup_fixture(setup, [=](configuration& config) - { - config.node.fee_estimate_horizon = horizon; - }) - { - } - - result estimate(size_t target, estimator::mode mode) - { - std::promise promise{}; - node_.estimate(target, mode, [&](const code& ec, uint64_t value) NOEXCEPT - { - promise.set_value({ ec, value }); - }); - - return promise.get_future().get(); - } - - // Wait (bounded) for the estimator to leave the premature state. - result initialized(size_t target, estimator::mode mode) - { - using namespace std::chrono; - const auto deadline = steady_clock::now() + seconds(10); - auto value = estimate(target, mode); - while (value.first == node::error::estimate_premature && - steady_clock::now() < deadline) - { - std::this_thread::sleep_for(milliseconds(10)); - value = estimate(target, mode); - } - - return value; - } - - void notify_pruned(const hash_digest& hash) - { - const auto link = query_.to_header(hash); - node_.notify(node::error::success, chases::pruned{ link.value }); - } - - bool confirm2() - { - const system::settings bitcoin{ chain::selection::mainnet }; - const auto& genesis = bitcoin.genesis_block.header(); - const auto& block1 = p2p_compact_setup_fixture::block1(); - const auto& block2 = p2p_compact_setup_fixture::block2(); - const database::context context2{ 0, 2, block1.header().timestamp() }; - const auto work1 = genesis.proof() + block1.header().proof(); - const auto work2 = work1 + block2.header().proof(); - return query_.set(block2, context2, work2, false, true) && - query_.push_candidate(query_.to_header(block2.hash())) && - query_.push_confirmed(query_.to_header(block2.hash()), true); - } - - bool store_header2() - { - const system::settings bitcoin{ chain::selection::mainnet }; - const auto& genesis = bitcoin.genesis_block.header(); - const auto& header1 = p2p_compact_setup_fixture::block1().header(); - const auto& header2 = p2p_compact_setup_fixture::block2().header(); - const database::context context2{ 0, 2, header1.timestamp() }; - const auto work2 = genesis.proof() + header1.proof() + header2.proof(); - return query_.set(header2, context2, work2, false); - } - - bool suspended() - { - using namespace std::chrono; - const auto deadline = steady_clock::now() + seconds(10); - while (steady_clock::now() < deadline) - { - if (node_.suspended()) - return true; - - std::this_thread::sleep_for(milliseconds(10)); - } - - return false; - } -}; - -struct chaser_estimate_disabled_setup_fixture - : chaser_estimate_setup_fixture -{ - inline chaser_estimate_disabled_setup_fixture() - : chaser_estimate_setup_fixture(0) - { - } -}; - -struct chaser_estimate_excess_setup_fixture - : chaser_estimate_setup_fixture -{ - inline chaser_estimate_excess_setup_fixture() - : chaser_estimate_setup_fixture(3) - { - } -}; - -struct chaser_estimate_enabled_setup_fixture - : chaser_estimate_setup_fixture -{ - inline chaser_estimate_enabled_setup_fixture() - : chaser_estimate_setup_fixture(2) - { - } -}; - -struct chaser_estimate_uncoalesced_setup_fixture - : chaser_estimate_setup_fixture -{ - inline chaser_estimate_uncoalesced_setup_fixture() - : chaser_estimate_setup_fixture(2, candidate1) - { - } -}; - -// Runs the protected handlers on the chaser strand. -class estimate_accessor - : public node::chaser_estimate -{ -public: - using chaser_estimate::chaser_estimate; - - template - void run(Method&& method) NOEXCEPT - { - std::promise promise{}; - network::io::post(strand(), [&]() NOEXCEPT - { - method(); - promise.set_value(); - }); - - promise.get_future().wait(); - } - - bool chase(const node::event_value& value) NOEXCEPT - { - const auto result = handle_chase({}, value); - run([]() NOEXCEPT {}); - return result; - } - - void pruned(const database::header_link& link) NOEXCEPT - { - run([&]() NOEXCEPT { do_pruned(link.value); }); - } - - void organized(const database::header_link& link) NOEXCEPT - { - run([&]() NOEXCEPT { do_organized(link.value); }); - } - - void reorganized(const database::header_link& link) NOEXCEPT - { - run([&]() NOEXCEPT { do_reorganized(link.value); }); - } - - void transaction(const database::tx_link& link) NOEXCEPT - { - run([&]() NOEXCEPT { do_transaction(link.value); }); - } -}; - -BC_POP_WARNING() - -BC_AUTO_TEST_SUITE(chaser_estimate_tests) - -BC_FIXTURE_TEST_CASE(chaser_estimate__estimate__disabled__estimate_disabled, chaser_estimate_disabled_setup_fixture) -{ - const auto result = estimate(1, estimator::mode::basic); - BC_REQUIRE_EQUAL(result.first, node::error::estimate_disabled); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__estimate__not_initialized__estimate_premature, chaser_estimate_uncoalesced_setup_fixture) -{ - const auto result = estimate(1, estimator::mode::basic); - BC_REQUIRE_EQUAL(result.first, node::error::estimate_premature); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__estimate__horizon_exceeds_chain__estimate_premature, chaser_estimate_excess_setup_fixture) -{ - notify_pruned(p2p_compact_setup_fixture::block1().hash()); - const auto result = estimate(1, estimator::mode::basic); - BC_REQUIRE_EQUAL(result.first, node::error::estimate_premature); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__estimate__initialized_no_fees__estimate_false, chaser_estimate_enabled_setup_fixture) -{ - notify_pruned(p2p_compact_setup_fixture::block1().hash()); - const auto result = initialized(1, estimator::mode::basic); - BC_REQUIRE_EQUAL(result.first, node::error::estimate_false); - BC_REQUIRE_EQUAL(result.second, estimator::estimate_failed); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__top_height__not_initialized__zero, chaser_estimate_enabled_setup_fixture) -{ - const node::chaser_estimate instance{ node_ }; - BC_REQUIRE(!instance.initialized()); - BC_REQUIRE_EQUAL(instance.top_height(), zero); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__pruned__horizon_within_chain__initialized, chaser_estimate_enabled_setup_fixture) -{ - estimate_accessor instance{ node_ }; - instance.pruned(query_.to_header(p2p_compact_setup_fixture::block1().hash())); - BC_REQUIRE(instance.initialized()); - BC_REQUIRE_EQUAL(instance.top_height(), 1u); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__organized__next_confirmed__pushed, chaser_estimate_enabled_setup_fixture) -{ - estimate_accessor instance{ node_ }; - instance.pruned(query_.to_header(p2p_compact_setup_fixture::block1().hash())); - BC_REQUIRE(confirm2()); - - const auto link2 = query_.to_header(p2p_compact_setup_fixture::block2().hash()); - instance.organized(link2); - BC_REQUIRE_EQUAL(instance.top_height(), 2u); - - instance.organized(link2); - BC_REQUIRE_EQUAL(instance.top_height(), 2u); - BC_REQUIRE(!node_.suspended()); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__reorganized__top__popped, chaser_estimate_enabled_setup_fixture) -{ - estimate_accessor instance{ node_ }; - instance.pruned(query_.to_header(p2p_compact_setup_fixture::block1().hash())); - BC_REQUIRE(confirm2()); - - const auto link2 = query_.to_header(p2p_compact_setup_fixture::block2().hash()); - instance.organized(link2); - instance.reorganized(link2); - BC_REQUIRE_EQUAL(instance.top_height(), 1u); - - instance.reorganized(link2); - BC_REQUIRE_EQUAL(instance.top_height(), 1u); - BC_REQUIRE(!node_.suspended()); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__handle_chase__initialized__handled, chaser_estimate_enabled_setup_fixture) -{ - estimate_accessor instance{ node_ }; - const auto link1 = query_.to_header(p2p_compact_setup_fixture::block1().hash()); - BC_REQUIRE(instance.chase(chases::block{ link1.value })); - BC_REQUIRE(!instance.initialized()); - - BC_REQUIRE(instance.chase(chases::pruned{ link1.value })); - BC_REQUIRE(instance.initialized()); - BC_REQUIRE(confirm2()); - - const auto link2 = query_.to_header(p2p_compact_setup_fixture::block2().hash()); - BC_REQUIRE(instance.chase(chases::organized{ link2.value })); - BC_REQUIRE_EQUAL(instance.top_height(), 2u); - - BC_REQUIRE(instance.chase(chases::reorganized{ link2.value })); - BC_REQUIRE_EQUAL(instance.top_height(), 1u); - - const auto coinbase = p2p_compact_setup_fixture::block2().transactions_ptr()->front(); - BC_REQUIRE(instance.chase(chases::transaction{ query_.to_tx(coinbase->hash(false)).value })); - BC_REQUIRE(instance.chase(chases::bump{})); - BC_REQUIRE(!instance.chase(chases::stop{})); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__organized__unstored_link__suspended, chaser_estimate_enabled_setup_fixture) -{ - estimate_accessor instance{ node_ }; - instance.pruned(query_.to_header(p2p_compact_setup_fixture::block1().hash())); - instance.organized(database::header_link{ 42 }); - BC_REQUIRE(suspended()); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__organized__header_only__suspended, chaser_estimate_enabled_setup_fixture) -{ - estimate_accessor instance{ node_ }; - instance.pruned(query_.to_header(p2p_compact_setup_fixture::block1().hash())); - BC_REQUIRE(store_header2()); - instance.organized(query_.to_header(p2p_compact_setup_fixture::block2().hash())); - BC_REQUIRE(suspended()); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__reorganized__unstored_link__suspended, chaser_estimate_enabled_setup_fixture) -{ - estimate_accessor instance{ node_ }; - instance.pruned(query_.to_header(p2p_compact_setup_fixture::block1().hash())); - instance.reorganized(database::header_link{ 42 }); - BC_REQUIRE(suspended()); -} - -BC_FIXTURE_TEST_CASE(chaser_estimate__transaction__unstored_link__suspended, chaser_estimate_enabled_setup_fixture) -{ - estimate_accessor instance{ node_ }; - instance.pruned(query_.to_header(p2p_compact_setup_fixture::block1().hash())); - instance.transaction(database::tx_link{ 42 }); - BC_REQUIRE(suspended()); -} - -BC_AUTO_TEST_SUITE_END() diff --git a/test/error.cpp b/test/error.cpp index ed83f3f6f..ff521e9c2 100644 --- a/test/error.cpp +++ b/test/error.cpp @@ -270,60 +270,6 @@ BC_AUTO_TEST_CASE(error_t__code__confirm1__true_expected_message) BC_REQUIRE_EQUAL(ec.message(), "confirm1"); } -BC_AUTO_TEST_CASE(error_t__code__estimates_initialize__true_expected_message) -{ - constexpr auto value = error::estimates_initialize; - const auto ec = code(value); - BC_REQUIRE(ec); - BC_REQUIRE(ec == value); - BC_REQUIRE_EQUAL(ec.message(), "estimates_initialize"); -} - -BC_AUTO_TEST_CASE(error_t__code__estimates_push1__true_expected_message) -{ - constexpr auto value = error::estimates_push1; - const auto ec = code(value); - BC_REQUIRE(ec); - BC_REQUIRE(ec == value); - BC_REQUIRE_EQUAL(ec.message(), "estimates_push1"); -} - -BC_AUTO_TEST_CASE(error_t__code__estimates_push2__true_expected_message) -{ - constexpr auto value = error::estimates_push2; - const auto ec = code(value); - BC_REQUIRE(ec); - BC_REQUIRE(ec == value); - BC_REQUIRE_EQUAL(ec.message(), "estimates_push2"); -} - -BC_AUTO_TEST_CASE(error_t__code__estimates_pop1__true_expected_message) -{ - constexpr auto value = error::estimates_pop1; - const auto ec = code(value); - BC_REQUIRE(ec); - BC_REQUIRE(ec == value); - BC_REQUIRE_EQUAL(ec.message(), "estimates_pop1"); -} - -BC_AUTO_TEST_CASE(error_t__code__estimates_pop2__true_expected_message) -{ - constexpr auto value = error::estimates_pop2; - const auto ec = code(value); - BC_REQUIRE(ec); - BC_REQUIRE(ec == value); - BC_REQUIRE_EQUAL(ec.message(), "estimates_pop2"); -} - -BC_AUTO_TEST_CASE(error_t__code__estimates_pool__true_expected_message) -{ - constexpr auto value = error::estimates_pool; - const auto ec = code(value); - BC_REQUIRE(ec); - BC_REQUIRE(ec == value); - BC_REQUIRE_EQUAL(ec.message(), "estimates_pool"); -} - // batch BC_AUTO_TEST_CASE(error_t__code__batch1__true_expected_message) diff --git a/test/estimator.cpp b/test/estimator.cpp deleted file mode 100644 index 9666f5b87..000000000 --- a/test/estimator.cpp +++ /dev/null @@ -1,1065 +0,0 @@ -/** - * Copyright (c) 2011-2026 libbitcoin developers - * - * This file is part of libbitcoin. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -#include "test.hpp" - -BC_AUTO_TEST_SUITE(estimator_tests) - -using namespace system; - -struct acessor - : node::estimator -{ - typedef std::shared_ptr ptr; - - static acessor::ptr create() NOEXCEPT - { - return { new acessor(),[](acessor* ptr) NOEXCEPT { delete ptr; } }; - } - - using rate = estimator::rate; - using rates = estimator::rates; - using rate_sets = estimator::rate_sets; - using confidence = estimator::confidence; - using horizon = estimator::horizon; - using sizing = estimator::sizing; - using estimator::decay_rate; - using estimator::to_scale_term; - using estimator::to_scale_factor; - using estimator::to_bin; - using estimator::history; - using estimator::initialize; - using estimator::push; - using estimator::pop; - using estimator::pool; - using estimator::conflict; - using estimator::compute; -}; - -// Rate of 1/10 (0.1) is bin 0. -constexpr size_t bin0_bytes = 10; -constexpr uint64_t bin0_fee = 1; - -// decay_rate - -BC_AUTO_TEST_CASE(estimator__decay_rate__invoke__expected) -{ - const auto expected = std::pow(0.5, 1.0 / acessor::sizing::count); - BC_REQUIRE_CLOSE(acessor::decay_rate(), expected, 0.000001); -} - -// to_scale_term - -BC_AUTO_TEST_CASE(estimator__to_scale_term__zero__one) -{ - BC_REQUIRE_EQUAL(acessor::to_scale_term(0u), 1.0); -} - -BC_AUTO_TEST_CASE(estimator__to_scale_term__non_zero__expected) -{ - const auto rate = acessor::decay_rate(); - constexpr auto age = 42u; - const auto expected = std::pow(rate, age); - BC_REQUIRE_CLOSE(acessor::to_scale_term(age), expected, 0.000001); -} - -// to_scale_factor - -BC_AUTO_TEST_CASE(estimator__to_scale_factor__push_true__decay_rate) -{ - const auto rate = acessor::decay_rate(); - const auto expected = std::pow(rate, +1.0); - BC_REQUIRE_CLOSE(acessor::to_scale_factor(true), expected, 0.000001); -} - -BC_AUTO_TEST_CASE(estimator__to_scale_factor__push_false__inverse_decay_rate) -{ - const auto rate = acessor::decay_rate(); - const auto expected = std::pow(rate, -1.0); - BC_REQUIRE_CLOSE(acessor::to_scale_factor(false), expected, 0.000001); -} - -// top_height - -BC_AUTO_TEST_CASE(estimator__top_height__default__zero) -{ - const auto instance = acessor::create(); - BC_REQUIRE_EQUAL(instance->top_height(), 0u); -} - -BC_AUTO_TEST_CASE(estimator__top_height__non_default__expected) -{ - const auto instance = acessor::create(); - instance->history().top_height = 42u; - BC_REQUIRE_EQUAL(instance->top_height(), 42u); -} - -// estimate - -BC_AUTO_TEST_CASE(estimator__estimate__unknown_mode__max_uint64) -{ - const auto instance = acessor::create(); - const auto estimate = instance->estimate(0u, estimator::mode::unknown); - - BC_REQUIRE_EQUAL(estimate, max_uint64); -} - -// to_bin - -BC_AUTO_TEST_CASE(estimator__to_bin__zero_fee__false) -{ - size_t bin{ 42 }; - BC_REQUIRE(!acessor::to_bin(bin, { bin0_bytes, 0u, 1u })); -} - -BC_AUTO_TEST_CASE(estimator__to_bin__below_minimum_rate__false) -{ - size_t bin{ 42 }; - BC_REQUIRE(!acessor::to_bin(bin, { 100u, bin0_fee, 1u })); -} - -BC_AUTO_TEST_CASE(estimator__to_bin__minimum_rate__bin_zero) -{ - size_t bin{ 42 }; - BC_REQUIRE(acessor::to_bin(bin, { bin0_bytes, bin0_fee, 1u })); - BC_REQUIRE_EQUAL(bin, 0u); -} - -BC_AUTO_TEST_CASE(estimator__to_bin__one_step__bin_one) -{ - size_t bin{ 42 }; - BC_REQUIRE(acessor::to_bin(bin, { 1000u, 106u, 1u })); - BC_REQUIRE_EQUAL(bin, 1u); -} - -BC_AUTO_TEST_CASE(estimator__to_bin__maximum_rate__last_bin) -{ - size_t bin{ 42 }; - BC_REQUIRE(acessor::to_bin(bin, { 1u, max_uint64, 1u })); - BC_REQUIRE_EQUAL(bin, sub1(acessor::sizing::count)); -} - -// initialize - -BC_AUTO_TEST_CASE(estimator__initialize__empty__true_height_unchanged) -{ - const auto instance = acessor::create(); - const acessor::rate_sets blocks{}; - const acessor::rates pooled{}; - BC_REQUIRE(instance->initialize(blocks, pooled, 0)); - BC_REQUIRE_EQUAL(instance->top_height(), 0u); - BC_REQUIRE_EQUAL(instance->history().small[0].total, 0u); -} - -BC_AUTO_TEST_CASE(estimator__initialize__more_blocks_than_height__false_height_unchanged) -{ - const auto instance = acessor::create(); - const acessor::rate_sets blocks(3); - BC_REQUIRE(!instance->initialize(blocks, {}, 1)); - BC_REQUIRE_EQUAL(instance->top_height(), 0u); -} - -BC_AUTO_TEST_CASE(estimator__initialize__two_blocks__true_top_height) -{ - const auto instance = acessor::create(); - const acessor::rate_sets blocks(2); - BC_REQUIRE(instance->initialize(blocks, {}, 5)); - BC_REQUIRE_EQUAL(instance->top_height(), 5u); -} - -BC_AUTO_TEST_CASE(estimator__initialize__unpooled_tx__not_counted) -{ - const auto instance = acessor::create(); - const acessor::rates block{ { bin0_bytes, bin0_fee } }; - const acessor::rate_sets blocks{ block }; - BC_REQUIRE(instance->initialize(blocks, {}, 1)); - BC_REQUIRE_EQUAL(instance->history().small.at(0).total, 0u); - BC_REQUIRE_EQUAL(instance->history().medium.at(0).total, 0u); - BC_REQUIRE_EQUAL(instance->history().large.at(0).total, 0u); -} - -BC_AUTO_TEST_CASE(estimator__initialize__single_block_no_wait__populates_expected) -{ - const auto instance = acessor::create(); - - // Pooled at height 1 and confirmed at height 1, no wait. - const acessor::rates block{ { bin0_bytes, bin0_fee, 1u } }; - const acessor::rate_sets blocks{ block }; - BC_REQUIRE(instance->initialize(blocks, {}, 1)); - - const auto& small0 = instance->history().small.at(0); - BC_REQUIRE_EQUAL(small0.total, 1u); - BC_REQUIRE_EQUAL(small0.confirmed[0], 0u); - BC_REQUIRE_EQUAL(small0.confirmed[1], 0u); - BC_REQUIRE_EQUAL(small0.confirmed[11], 0u); - - const auto& medium0 = instance->history().medium.at(0); - BC_REQUIRE_EQUAL(medium0.total, 1u); - BC_REQUIRE_EQUAL(medium0.confirmed[0], 0u); - BC_REQUIRE_EQUAL(medium0.confirmed[47], 0u); - - const auto& large0 = instance->history().large.at(0); - BC_REQUIRE_EQUAL(large0.total, 1u); - BC_REQUIRE_EQUAL(large0.confirmed[0], 0u); - BC_REQUIRE_EQUAL(large0.confirmed[1007], 0u); -} - -BC_AUTO_TEST_CASE(estimator__initialize__single_block_waited__fails_shorter_targets) -{ - const auto instance = acessor::create(); - - // Pooled at height 3 and confirmed at height 5, waited 2 blocks. - const acessor::rates block{ { bin0_bytes, bin0_fee, 3u } }; - const acessor::rate_sets blocks{ block }; - BC_REQUIRE(instance->initialize(blocks, {}, 5)); - - const auto& small0 = instance->history().small.at(0); - BC_REQUIRE_EQUAL(small0.total, 1u); - BC_REQUIRE_EQUAL(small0.confirmed[0], 1u); - BC_REQUIRE_EQUAL(small0.confirmed[1], 1u); - BC_REQUIRE_EQUAL(small0.confirmed[2], 0u); - BC_REQUIRE_EQUAL(small0.confirmed[11], 0u); - - const auto& medium0 = instance->history().medium.at(0); - BC_REQUIRE_EQUAL(medium0.total, 1u); - BC_REQUIRE_EQUAL(medium0.confirmed[0], 1u); - BC_REQUIRE_EQUAL(medium0.confirmed[1], 1u); - BC_REQUIRE_EQUAL(medium0.confirmed[2], 0u); - BC_REQUIRE_EQUAL(medium0.confirmed[47], 0u); - - const auto& large0 = instance->history().large.at(0); - BC_REQUIRE_EQUAL(large0.total, 1u); - BC_REQUIRE_EQUAL(large0.confirmed[0], 1u); - BC_REQUIRE_EQUAL(large0.confirmed[1], 1u); - BC_REQUIRE_EQUAL(large0.confirmed[2], 0u); - BC_REQUIRE_EQUAL(large0.confirmed[1007], 0u); -} - -BC_AUTO_TEST_CASE(estimator__initialize__waited_beyond_horizon__fails_all_targets) -{ - const auto instance = acessor::create(); - - // Pooled at height 1 and confirmed at height 2000, beyond the horizon. - const acessor::rates block{ { bin0_bytes, bin0_fee, 1u } }; - const acessor::rate_sets blocks{ block }; - BC_REQUIRE(instance->initialize(blocks, {}, 2000)); - - const auto& small0 = instance->history().small.at(0); - BC_REQUIRE_EQUAL(small0.total, 1u); - BC_REQUIRE_EQUAL(small0.confirmed[0], 1u); - BC_REQUIRE_EQUAL(small0.confirmed[11], 1u); - - const auto& large0 = instance->history().large.at(0); - BC_REQUIRE_EQUAL(large0.total, 1u); - BC_REQUIRE_EQUAL(large0.confirmed[0], 1u); - BC_REQUIRE_EQUAL(large0.confirmed[1007], 1u); -} - -BC_AUTO_TEST_CASE(estimator__initialize__two_blocks_with_data__oldest_decayed) -{ - // Oldest: 1 tx, floor(1 * decay_rate) = 0. Newest: 2 txs, 2 * 1.0 = 2. - const auto instance = acessor::create(); - const acessor::rates oldest{ { bin0_bytes, bin0_fee, 1u } }; - const acessor::rates newest{ { bin0_bytes, bin0_fee, 2u }, { bin0_bytes, bin0_fee, 2u } }; - const acessor::rate_sets blocks{ oldest, newest }; - BC_REQUIRE(instance->initialize(blocks, {}, 2)); - BC_REQUIRE_EQUAL(instance->top_height(), 2u); - BC_REQUIRE_EQUAL(instance->history().small.at(0).total, 2u); -} - -BC_AUTO_TEST_CASE(estimator__initialize__pooled__pending_at_entry_height) -{ - const auto instance = acessor::create(); - const acessor::rate_sets blocks{}; - const acessor::rates pooled{ { bin0_bytes, bin0_fee, 3u }, { bin0_bytes, bin0_fee, 3u }, { bin0_bytes, bin0_fee, 7u } }; - BC_REQUIRE(instance->initialize(blocks, pooled, 2)); - BC_REQUIRE_EQUAL(instance->history().entries.at(3), 3u); - BC_REQUIRE_EQUAL(instance->history().pending.at(3).at(0), 2u); - BC_REQUIRE_EQUAL(instance->history().entries.at(7), 7u); - BC_REQUIRE_EQUAL(instance->history().pending.at(7).at(0), 1u); - BC_REQUIRE_EQUAL(instance->history().aged.at(0), 0u); -} - -BC_AUTO_TEST_CASE(estimator__initialize__pooled_zero_bytes__false) -{ - const auto instance = acessor::create(); - const acessor::rate_sets blocks{}; - const acessor::rates pooled{ { 0u, bin0_fee, 3u } }; - BC_REQUIRE(!instance->initialize(blocks, pooled, 2)); -} - -// pool - -BC_AUTO_TEST_CASE(estimator__pool__zero_bytes__false) -{ - const auto instance = acessor::create(); - BC_REQUIRE(!instance->pool({ 0u, bin0_fee, 1u })); -} - -BC_AUTO_TEST_CASE(estimator__pool__unpooled__true_not_pending) -{ - const auto instance = acessor::create(); - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee })); - BC_REQUIRE_EQUAL(instance->history().pending.at(0).at(0), 0u); - BC_REQUIRE_EQUAL(instance->history().aged.at(0), 0u); -} - -BC_AUTO_TEST_CASE(estimator__pool__below_minimum_rate__true_not_pending) -{ - const auto instance = acessor::create(); - BC_REQUIRE(instance->pool({ 100u, bin0_fee, 1u })); - BC_REQUIRE_EQUAL(instance->history().entries.at(1), 0u); - BC_REQUIRE_EQUAL(instance->history().pending.at(1).at(0), 0u); -} - -BC_AUTO_TEST_CASE(estimator__pool__pooled__pending) -{ - const auto instance = acessor::create(); - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 1u })); - BC_REQUIRE_EQUAL(instance->history().entries.at(1), 1u); - BC_REQUIRE_EQUAL(instance->history().pending.at(1).at(0), 1u); -} - -BC_AUTO_TEST_CASE(estimator__pool__later_height_same_slot__displaces_to_aged) -{ - const auto instance = acessor::create(); - constexpr auto large = acessor::horizon::large; - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 1u })); - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, add1(large) })); - BC_REQUIRE_EQUAL(instance->history().entries.at(1), add1(large)); - BC_REQUIRE_EQUAL(instance->history().pending.at(1).at(0), 1u); - BC_REQUIRE_EQUAL(instance->history().aged.at(0), 1u); -} - -BC_AUTO_TEST_CASE(estimator__pool__earlier_height_same_slot__aged) -{ - const auto instance = acessor::create(); - constexpr auto large = acessor::horizon::large; - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, add1(large) })); - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 1u })); - BC_REQUIRE_EQUAL(instance->history().entries.at(1), add1(large)); - BC_REQUIRE_EQUAL(instance->history().pending.at(1).at(0), 1u); - BC_REQUIRE_EQUAL(instance->history().aged.at(0), 1u); -} - -// push - -BC_AUTO_TEST_CASE(estimator__push__empty_block__decays_and_increments) -{ - const auto instance = acessor::create(); - constexpr auto initial = 100u; - instance->history().small[0].total = initial; - const auto factor = acessor::to_scale_factor(true); - const auto expected = to_floored_integer(initial * factor); - const acessor::rates empty{}; - BC_REQUIRE(instance->push(empty)); - BC_REQUIRE_EQUAL(instance->top_height(), 1u); - BC_REQUIRE_EQUAL(instance->history().small[0].total, expected); -} - -BC_AUTO_TEST_CASE(estimator__push__single_tx_no_wait__populates_expected) -{ - const auto instance = acessor::create(); - - // Pooled at height 1 and confirmed at height 1, no wait. - const acessor::rates block{ { bin0_bytes, bin0_fee, 1u } }; - BC_REQUIRE(instance->push(block)); - BC_REQUIRE_EQUAL(instance->top_height(), 1u); - - const auto& small0 = instance->history().small.at(0); - BC_REQUIRE_EQUAL(small0.total, 1u); - BC_REQUIRE_EQUAL(small0.confirmed[0], 0u); - BC_REQUIRE_EQUAL(small0.confirmed[11], 0u); - - const auto& medium0 = instance->history().medium.at(0); - BC_REQUIRE_EQUAL(medium0.total, 1u); - BC_REQUIRE_EQUAL(medium0.confirmed[0], 0u); - BC_REQUIRE_EQUAL(medium0.confirmed[47], 0u); - - const auto& large0 = instance->history().large.at(0); - BC_REQUIRE_EQUAL(large0.total, 1u); - BC_REQUIRE_EQUAL(large0.confirmed[0], 0u); - BC_REQUIRE_EQUAL(large0.confirmed[1007], 0u); -} - -BC_AUTO_TEST_CASE(estimator__push__single_tx_waited__fails_shorter_targets) -{ - const auto instance = acessor::create(); - instance->history().top_height = 4u; - - // Pooled at height 2 and confirmed at height 5, waited 3 blocks. - const acessor::rates block{ { bin0_bytes, bin0_fee, 2u } }; - BC_REQUIRE(instance->push(block)); - BC_REQUIRE_EQUAL(instance->top_height(), 5u); - - const auto& small0 = instance->history().small.at(0); - BC_REQUIRE_EQUAL(small0.total, 1u); - BC_REQUIRE_EQUAL(small0.confirmed[0], 1u); - BC_REQUIRE_EQUAL(small0.confirmed[1], 1u); - BC_REQUIRE_EQUAL(small0.confirmed[2], 1u); - BC_REQUIRE_EQUAL(small0.confirmed[3], 0u); - BC_REQUIRE_EQUAL(small0.confirmed[11], 0u); -} - -BC_AUTO_TEST_CASE(estimator__push__pending_tx__leaves_pending) -{ - const auto instance = acessor::create(); - instance->history().top_height = 4u; - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 5u })); - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 5u })); - BC_REQUIRE_EQUAL(instance->history().pending.at(5).at(0), 2u); - - const acessor::rates block{ { bin0_bytes, bin0_fee, 5u } }; - BC_REQUIRE(instance->push(block)); - BC_REQUIRE_EQUAL(instance->history().pending.at(5).at(0), 1u); - BC_REQUIRE_EQUAL(instance->history().small.at(0).total, 1u); -} - -BC_AUTO_TEST_CASE(estimator__push__aged_tx__leaves_aged) -{ - const auto instance = acessor::create(); - constexpr auto large = acessor::horizon::large; - instance->history().top_height = large; - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 1u })); - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, add1(large) })); - BC_REQUIRE_EQUAL(instance->history().aged.at(0), 1u); - - const acessor::rates block{ { bin0_bytes, bin0_fee, 1u } }; - BC_REQUIRE(instance->push(block)); - BC_REQUIRE_EQUAL(instance->history().aged.at(0), 0u); - BC_REQUIRE_EQUAL(instance->history().pending.at(1).at(0), 1u); - BC_REQUIRE_EQUAL(instance->history().large.at(0).total, 1u); - BC_REQUIRE_EQUAL(instance->history().large.at(0).confirmed[1007], 1u); -} - -BC_AUTO_TEST_CASE(estimator__push__unpending_tx__saturates) -{ - const auto instance = acessor::create(); - const acessor::rates block{ { bin0_bytes, bin0_fee, 1u } }; - BC_REQUIRE(instance->push(block)); - BC_REQUIRE_EQUAL(instance->history().pending.at(1).at(0), 0u); - BC_REQUIRE_EQUAL(instance->history().aged.at(0), 0u); -} - -// pop - -BC_AUTO_TEST_CASE(estimator__pop__empty_block__decays_and_decrements) -{ - const auto instance = acessor::create(); - instance->history().top_height = 1u; - constexpr auto initial = 100u; - instance->history().small[0].total = initial; - const auto factor = acessor::to_scale_factor(false); - const auto expected = to_floored_integer(initial * factor); - const acessor::rates empty{}; - BC_REQUIRE(instance->pop(empty)); - BC_REQUIRE_EQUAL(instance->top_height(), 0u); - BC_REQUIRE_EQUAL(instance->history().small[0].total, expected); -} - -BC_AUTO_TEST_CASE(estimator__pop__reverses_push__restores_buckets_and_pending) -{ - const auto instance = acessor::create(); - instance->history().top_height = 4u; - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 2u })); - - // Pooled at height 2 and confirmed at height 5, waited 3 blocks. - const acessor::rates block{ { bin0_bytes, bin0_fee, 2u } }; - BC_REQUIRE(instance->push(block)); - BC_REQUIRE_EQUAL(instance->history().pending.at(2).at(0), 0u); - BC_REQUIRE_EQUAL(instance->history().small.at(0).confirmed[2], 1u); - - BC_REQUIRE(instance->pop(block)); - BC_REQUIRE_EQUAL(instance->top_height(), 4u); - BC_REQUIRE_EQUAL(instance->history().pending.at(2).at(0), 1u); - - const auto& small0 = instance->history().small.at(0); - BC_REQUIRE_EQUAL(small0.total, 0u); - BC_REQUIRE_EQUAL(small0.confirmed[0], 0u); - BC_REQUIRE_EQUAL(small0.confirmed[1], 0u); - BC_REQUIRE_EQUAL(small0.confirmed[2], 0u); - BC_REQUIRE_EQUAL(small0.confirmed[11], 0u); - - const auto& medium0 = instance->history().medium.at(0); - BC_REQUIRE_EQUAL(medium0.total, 0u); - BC_REQUIRE_EQUAL(medium0.confirmed[0], 0u); - BC_REQUIRE_EQUAL(medium0.confirmed[47], 0u); - - const auto& large0 = instance->history().large.at(0); - BC_REQUIRE_EQUAL(large0.total, 0u); - BC_REQUIRE_EQUAL(large0.confirmed[0], 0u); - BC_REQUIRE_EQUAL(large0.confirmed[1007], 0u); -} - -// conflict - -BC_AUTO_TEST_CASE(estimator__conflict__push__leaves_pending) -{ - const auto instance = acessor::create(); - instance->history().top_height = 4u; - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 2u })); - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 2u })); - - instance->conflict({ { bin0_bytes, bin0_fee, 2u } }, true); - BC_REQUIRE_EQUAL(instance->history().pending.at(2).at(0), 1u); - BC_REQUIRE_EQUAL(instance->history().small.at(0).total, 0u); -} - -BC_AUTO_TEST_CASE(estimator__conflict__pop__reenters_pending) -{ - const auto instance = acessor::create(); - instance->history().top_height = 4u; - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 2u })); - - const acessor::rates conflicts{ { bin0_bytes, bin0_fee, 2u } }; - instance->conflict(conflicts, true); - BC_REQUIRE_EQUAL(instance->history().pending.at(2).at(0), 0u); - - instance->conflict(conflicts, false); - BC_REQUIRE_EQUAL(instance->history().pending.at(2).at(0), 1u); -} - -BC_AUTO_TEST_CASE(estimator__conflict__aged__leaves_aged) -{ - const auto instance = acessor::create(); - constexpr auto large = acessor::horizon::large; - instance->history().top_height = large; - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 1u })); - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, add1(large) })); - BC_REQUIRE_EQUAL(instance->history().aged.at(0), 1u); - - instance->conflict({ { bin0_bytes, bin0_fee, 1u } }, true); - BC_REQUIRE_EQUAL(instance->history().aged.at(0), 0u); - BC_REQUIRE_EQUAL(instance->history().pending.at(1).at(0), 1u); -} - -BC_AUTO_TEST_CASE(estimator__conflict__unpooled__not_pending) -{ - const auto instance = acessor::create(); - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 2u })); - - instance->conflict({ { bin0_bytes, bin0_fee, max_size_t } }, true); - BC_REQUIRE_EQUAL(instance->history().pending.at(2).at(0), 1u); -} - -// compute - -BC_AUTO_TEST_CASE(estimator__compute__default_state__max_uint64) -{ - const auto instance = acessor::create(); - BC_REQUIRE_EQUAL(instance->compute(0, acessor::confidence::high), max_uint64); - BC_REQUIRE_EQUAL(instance->compute(1, acessor::confidence::mid, true), max_uint64); - BC_REQUIRE_EQUAL(instance->compute(50, acessor::confidence::low), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__compute__insufficient_total__max_uint64) -{ - const auto instance = acessor::create(); - constexpr auto bin = 0u; - - // < at_least_four=2 for target=0. - constexpr auto value = 1u; - instance->history().small[bin].total = value; - instance->history().small[bin].confirmed[0] = value; - BC_REQUIRE_EQUAL(instance->compute(0, acessor::confidence::high), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__compute__low_failure_basic__expected_fee) -{ - const auto instance = acessor::create(); - constexpr auto bin = 0u; - constexpr auto total = 10u; - - // 0/10 = 0 <= 0.05. - constexpr auto failure = 0u; - instance->history().small[bin].total = total; - instance->history().small[bin].confirmed[0] = failure; - const auto fee = to_ceilinged_integer(acessor::sizing::min * std::pow(acessor::sizing::step, bin)); - BC_REQUIRE_EQUAL(instance->compute(0, acessor::confidence::high), fee); -} - -BC_AUTO_TEST_CASE(estimator__compute__high_failure_basic__max_uint64) -{ - const auto instance = acessor::create(); - constexpr auto bin = 0u; - constexpr auto total = 10u; - - // 1/10 = 0.1 > 0.05. - constexpr auto failure = 1u; - instance->history().small[bin].total = total; - instance->history().small[bin].confirmed[0] = failure; - BC_REQUIRE_EQUAL(instance->compute(0, acessor::confidence::high), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__compute__multi_bin_basic__expected_fee) -{ - const auto instance = acessor::create(); - constexpr auto low_bin = 0u; - constexpr auto high_bin = 1u; - constexpr auto total = 10u; - - // high failure in low bin. - constexpr auto low_failure = 10u; - - // low failure in high bin. - constexpr auto high_failure = 0u; - instance->history().small[low_bin].total = total; - instance->history().small[low_bin].confirmed[0] = low_failure; - instance->history().small[high_bin].total = total; - instance->history().small[high_bin].confirmed[0] = high_failure; - - // Cumulative at high_bin: 0/10 = 0 <= 0.05, then at low_bin: 10/20 = 0.5 > 0.05, found=1. - const auto fee = to_ceilinged_integer(acessor::sizing::min * std::pow(acessor::sizing::step, high_bin)); - BC_REQUIRE_EQUAL(instance->compute(0, acessor::confidence::high), fee); -} - -BC_AUTO_TEST_CASE(estimator__compute__pending_within_target__not_counted) -{ - const auto instance = acessor::create(); - constexpr auto bin = 0u; - constexpr auto total = 10u; - instance->history().small[bin].total = total; - instance->history().top_height = 5u; - - // Pooled at height 6 with top at 5, no blocks waited. - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 6u })); - const auto fee = to_ceilinged_integer(acessor::sizing::min * std::pow(acessor::sizing::step, bin)); - BC_REQUIRE_EQUAL(instance->compute(0, acessor::confidence::high), fee); -} - -BC_AUTO_TEST_CASE(estimator__compute__pending_beyond_target__failed) -{ - const auto instance = acessor::create(); - constexpr auto bin = 0u; - constexpr auto total = 10u; - instance->history().small[bin].total = total; - instance->history().top_height = 5u; - - // Pooled at height 5 with top at 5, one block waited: 1/11 > 0.05. - BC_REQUIRE(instance->pool({ bin0_bytes, bin0_fee, 5u })); - BC_REQUIRE_EQUAL(instance->compute(0, acessor::confidence::high), max_uint64); - - // Target 1 (two blocks) is not yet failed. - const auto fee = to_ceilinged_integer(acessor::sizing::min * std::pow(acessor::sizing::step, bin)); - BC_REQUIRE_EQUAL(instance->compute(1, acessor::confidence::high), fee); -} - -BC_AUTO_TEST_CASE(estimator__compute__aged__failed_all_targets) -{ - const auto instance = acessor::create(); - constexpr auto bin = 0u; - constexpr auto total = 10u; - instance->history().small[bin].total = total; - instance->history().aged[bin] = 1u; - BC_REQUIRE_EQUAL(instance->compute(0, acessor::confidence::high), max_uint64); - BC_REQUIRE_EQUAL(instance->compute(11, acessor::confidence::high), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__compute__geometric_target_one__matches_basic) -{ - const auto instance = acessor::create(); - constexpr auto bin = 0u; - constexpr auto total = 10u; - constexpr auto failure = 0u; - instance->history().small[bin].total = total; - instance->history().small[bin].confirmed[1] = failure; - const auto fee = to_ceilinged_integer(acessor::sizing::min * std::pow(acessor::sizing::step, bin)); - const auto basic = instance->compute(1, acessor::confidence::high, false); - const auto geometric = instance->compute(1, acessor::confidence::high, true); - BC_REQUIRE_EQUAL(basic, fee); - BC_REQUIRE_EQUAL(geometric, fee); -} - -BC_AUTO_TEST_CASE(estimator__compute__geometric_high_target__expected) -{ - const auto instance = acessor::create(); - constexpr auto bin = 0u; - constexpr auto total = 10u; - - // p=0.1, pow(0.1,2)=0.01 < 0.05, so found=0. - constexpr auto failure = 1u; - instance->history().small[bin].total = total; - instance->history().small[bin].confirmed[2] = failure; - const auto fee = to_ceilinged_integer(acessor::sizing::min * std::pow(acessor::sizing::step, bin)); - BC_REQUIRE_EQUAL(instance->compute(2, acessor::confidence::high, true), fee); - - // Contrast with basic: 0.1 > 0.05, would be max_uint64. - BC_REQUIRE_EQUAL(instance->compute(2, acessor::confidence::high, false), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__compute__large_target__max_uint64) -{ - const auto instance = acessor::create(); - instance->history().large[0].total = 10'000u; - BC_REQUIRE_EQUAL(instance->compute(acessor::horizon::large, acessor::confidence::low), max_uint64); -} - -// estimate (modes) - -BC_AUTO_TEST_CASE(estimator__estimate__large_target__max_uint64) -{ - const auto instance = acessor::create(); - instance->history().large[0].total = 10'000u; - BC_REQUIRE_EQUAL(instance->estimate(acessor::horizon::large, estimator::mode::basic), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__estimate__basic__expected) -{ - const auto instance = acessor::create(); - instance->history().small[0].total = 10u; - instance->history().small[0].confirmed[2] = 1u; - BC_REQUIRE_EQUAL(instance->estimate(1, estimator::mode::basic), 1u); - BC_REQUIRE_EQUAL(instance->estimate(2, estimator::mode::basic), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__estimate__geometric__expected) -{ - const auto instance = acessor::create(); - instance->history().small[0].total = 10u; - instance->history().small[0].confirmed[2] = 1u; - BC_REQUIRE_EQUAL(instance->estimate(2, estimator::mode::geometric), 1u); -} - -BC_AUTO_TEST_CASE(estimator__estimate__economical_double_target_failed__max_uint64) -{ - const auto instance = acessor::create(); - instance->history().small[0].total = 10u; - instance->history().small[0].confirmed[2] = 1u; - BC_REQUIRE_EQUAL(instance->estimate(1, estimator::mode::economical), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__estimate__economical_all_targets_pass__expected) -{ - const auto instance = acessor::create(); - instance->history().small[0].total = 10u; - BC_REQUIRE_EQUAL(instance->estimate(1, estimator::mode::economical), 1u); -} - -BC_AUTO_TEST_CASE(estimator__estimate__economical_target_failed__max_uint64) -{ - const auto instance = acessor::create(); - instance->history().small[0].total = 20u; - instance->history().small[0].confirmed[4] = 10u; - BC_REQUIRE_EQUAL(instance->estimate(4, estimator::mode::economical), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__estimate__conservative_double_target_failed__max_uint64) -{ - const auto instance = acessor::create(); - instance->history().small[0].total = 10u; - instance->history().small[0].confirmed[2] = 1u; - BC_REQUIRE_EQUAL(instance->estimate(1, estimator::mode::conservative), max_uint64); -} - -BC_AUTO_TEST_CASE(estimator__estimate__conservative_all_targets_pass__expected) -{ - const auto instance = acessor::create(); - instance->history().small[0].total = 10u; - BC_REQUIRE_EQUAL(instance->estimate(1, estimator::mode::conservative), 1u); -} - -BC_AUTO_TEST_CASE(estimator__estimate__conservative_target_failed__max_uint64) -{ - const auto instance = acessor::create(); - instance->history().small[0].total = 20u; - instance->history().small[0].confirmed[4] = 10u; - BC_REQUIRE_EQUAL(instance->estimate(4, estimator::mode::conservative), max_uint64); -} - -// history - -BC_AUTO_TEST_CASE(estimator__history__const__expected) -{ - const auto instance = acessor::create(); - instance->history().top_height = 42u; - const auto& constant = *instance; - BC_REQUIRE_EQUAL(constant.history().top_height, 42u); -} - -// update - -BC_AUTO_TEST_CASE(estimator__push__zero_bytes__false) -{ - const auto instance = acessor::create(); - const acessor::rates block{ { 0u, bin0_fee, 1u } }; - BC_REQUIRE(!instance->push(block)); -} - -BC_AUTO_TEST_CASE(estimator__push__zero_fee__not_counted) -{ - const auto instance = acessor::create(); - const acessor::rates block{ { bin0_bytes, 0u, 1u } }; - BC_REQUIRE(instance->push(block)); - BC_REQUIRE_EQUAL(instance->history().small[0].total, 0u); -} - -BC_AUTO_TEST_CASE(estimator__push__below_minimum_rate__not_counted) -{ - const auto instance = acessor::create(); - const acessor::rates block{ { 100u, bin0_fee, 1u } }; - BC_REQUIRE(instance->push(block)); - BC_REQUIRE_EQUAL(instance->history().small[0].total, 0u); -} - -// query - -struct estimator_query_setup_fixture -{ - DELETE_COPY_MOVE(estimator_query_setup_fixture); - - static database::settings configure() NOEXCEPT - { - database::settings settings{ test::database_settings() }; - settings.path = TEST_DIRECTORY; - return settings; - } - - estimator_query_setup_fixture() - : settings_{ configure() }, store_{ settings_ }, query_{ store_ } - { - BC_REQUIRE(test::clear(test::directory)); - const auto ec = store_.create([](auto, auto) NOEXCEPT {}); - BC_REQUIRE_MESSAGE(!ec, ec.message()); - const system::settings bitcoin{ chain::selection::mainnet }; - BC_REQUIRE(query_.initialize(bitcoin.genesis_block)); - } - - ~estimator_query_setup_fixture() - { - const auto ec = store_.close([](auto, auto) NOEXCEPT {}); - BC_WARN_MESSAGE(!ec, ec.message()); - test::clear(test::directory); - } - - const database::settings settings_; - node::store store_; - node::query query_; - const std::atomic_bool cancel_{}; -}; - -BC_FIXTURE_TEST_CASE(estimator__initialize__query_zero_count__true_height_unchanged, estimator_query_setup_fixture) -{ - const auto instance = acessor::create(); - BC_REQUIRE(instance->initialize(cancel_, query_, 0)); - BC_REQUIRE_EQUAL(instance->top_height(), 0u); -} - -BC_FIXTURE_TEST_CASE(estimator__initialize__query_count_exceeds_chain__false, estimator_query_setup_fixture) -{ - const auto instance = acessor::create(); - BC_REQUIRE(!instance->initialize(cancel_, query_, 2)); - BC_REQUIRE_EQUAL(instance->top_height(), 0u); -} - -BC_FIXTURE_TEST_CASE(estimator__push__query_maximum_top_height__false, estimator_query_setup_fixture) -{ - const auto instance = acessor::create(); - instance->history().top_height = max_size_t; - BC_REQUIRE(!instance->push(query_, query_.to_confirmed(0))); - BC_REQUIRE_EQUAL(instance->top_height(), max_size_t); -} - -BC_FIXTURE_TEST_CASE(estimator__pop__query_zero_top_height__false, estimator_query_setup_fixture) -{ - const auto instance = acessor::create(); - BC_REQUIRE(!instance->pop(query_, query_.to_confirmed(0))); - BC_REQUIRE_EQUAL(instance->top_height(), 0u); -} - -static chain::block estimator_block(const chain::block& parent, - uint32_t height, const chain::transactions& txs) -{ - hashes ids{}; - for (const auto& tx: txs) - ids.push_back(tx.hash(false)); - - const auto previous = parent.hash(); - const auto root = sha256::merkle_root(std::move(ids)); - const auto timestamp = parent.header().timestamp() + 600u; - const auto bits = parent.header().bits(); - const chain::header header{ 1u, previous, root, timestamp, bits, height }; - return { header, txs }; -} - -static chain::transaction estimator_coinbase(uint8_t tag, uint64_t value) -{ - const chain::point null{ null_hash, chain::point::null_index }; - const chain::script tagged{ data_chunk{ 0x01, tag }, false }; - const chain::script op_true{ data_chunk{ 0x51 }, false }; - const chain::input input{ null, tagged, max_uint32 }; - const chain::output output{ value, op_true }; - return { 1u, chain::inputs{ input }, chain::outputs{ output }, 0u }; -} - -static bool estimator_confirm(node::query& query, const chain::block& block, - uint32_t height) -{ - const database::context context{ 0, height, block.header().timestamp() }; - if (!query.set(block, context, {}, false, true)) - return false; - - const auto link = query.to_header(block.hash()); - return query.push_candidate(link) && query.push_confirmed(link, true); -} - -static size_t estimator_small_total(const acessor& instance) -{ - size_t total{}; - for (const auto& bucket: instance.history().small) - total += bucket.total; - - return total; -} - -static chain::transaction estimator_spend(const chain::transaction& parent, - uint64_t fee) -{ - const chain::input input{ chain::point{ parent.hash(false), 0 }, chain::script{}, max_uint32 }; - const chain::output output{ parent.outputs_ptr()->front()->value() - fee, chain::script{ data_chunk{ 0x51 }, false } }; - return { 1u, chain::inputs{ input }, chain::outputs{ output }, 0u }; -} - -static bool estimator_pool(node::store& store, node::query& query, - const chain::transaction& tx, uint32_t height, uint64_t fee) -{ - const database::context context{ 0, height, 0 }; - return store.pool.put(query.to_tx(tx.hash(false)), database::table::pool::record{ {}, context, fee, {}, {} }); -} - -static size_t estimator_bin(const chain::transaction& tx, uint64_t fee) -{ - size_t bin{}; - BC_REQUIRE(acessor::to_bin(bin, { tx.virtual_size(), fee, 0u })); - return bin; -} - -BC_FIXTURE_TEST_CASE(estimator__initialize__query_unpooled_block_tx__not_counted, estimator_query_setup_fixture) -{ - constexpr uint64_t value = 5'000'000'000; - const system::settings bitcoin{ chain::selection::mainnet }; - const auto& genesis = bitcoin.genesis_block; - const auto coinbase1 = estimator_coinbase(1, value); - const auto spend = estimator_spend(coinbase1, 10'000u); - const auto block1 = estimator_block(genesis, 1, { coinbase1 }); - const auto block2 = estimator_block(block1, 2, { estimator_coinbase(2, value), spend }); - BC_REQUIRE(estimator_confirm(query_, block1, 1)); - BC_REQUIRE(estimator_confirm(query_, block2, 2)); - - const auto instance = acessor::create(); - BC_REQUIRE(instance->initialize(cancel_, query_, 3)); - BC_REQUIRE_EQUAL(instance->top_height(), 2u); - BC_REQUIRE_EQUAL(estimator_small_total(*instance), 0u); -} - -BC_FIXTURE_TEST_CASE(estimator__initialize__query_unconfirmed_pooled__pending, estimator_query_setup_fixture) -{ - constexpr uint64_t value = 5'000'000'000; - const system::settings bitcoin{ chain::selection::mainnet }; - const auto& genesis = bitcoin.genesis_block; - const auto coinbase1 = estimator_coinbase(1, value); - const auto spend = estimator_spend(coinbase1, 10'000u); - const auto block1 = estimator_block(genesis, 1, { coinbase1 }); - const auto block2 = estimator_block(block1, 2, { estimator_coinbase(2, value), spend }); - BC_REQUIRE(estimator_confirm(query_, block1, 1)); - - // Block 2 is archived but not confirmed, so the spend is pending. - BC_REQUIRE(query_.set(block2, database::context{ 0, 2, 0 }, {}, false, false)); - BC_REQUIRE(estimator_pool(store_, query_, spend, 2, 10'000u)); - - const auto instance = acessor::create(); - BC_REQUIRE(instance->initialize(cancel_, query_, 2)); - BC_REQUIRE_EQUAL(instance->top_height(), 1u); - BC_REQUIRE_EQUAL(estimator_small_total(*instance), 0u); - BC_REQUIRE_EQUAL(instance->history().entries.at(2), 2u); - BC_REQUIRE_EQUAL(instance->history().pending.at(2).at(estimator_bin(spend, 10'000u)), 1u); -} - -BC_FIXTURE_TEST_CASE(estimator__pool__query_pooled_link__pending, estimator_query_setup_fixture) -{ - constexpr uint64_t value = 5'000'000'000; - const system::settings bitcoin{ chain::selection::mainnet }; - const auto& genesis = bitcoin.genesis_block; - const auto coinbase1 = estimator_coinbase(1, value); - const auto spend = estimator_spend(coinbase1, 10'000u); - const auto block1 = estimator_block(genesis, 1, { coinbase1 }); - const auto block2 = estimator_block(block1, 2, { estimator_coinbase(2, value), spend }); - BC_REQUIRE(estimator_confirm(query_, block1, 1)); - BC_REQUIRE(query_.set(block2, database::context{ 0, 2, 0 }, {}, false, false)); - BC_REQUIRE(estimator_pool(store_, query_, spend, 2, 10'000u)); - - const auto instance = acessor::create(); - BC_REQUIRE(instance->pool(query_, query_.to_tx(spend.hash(false)))); - BC_REQUIRE_EQUAL(instance->history().entries.at(2), 2u); - BC_REQUIRE_EQUAL(instance->history().pending.at(2).at(estimator_bin(spend, 10'000u)), 1u); -} - -BC_FIXTURE_TEST_CASE(estimator__pool__query_unknown_link__false, estimator_query_setup_fixture) -{ - const auto instance = acessor::create(); - BC_REQUIRE(!instance->pool(query_, 42)); -} - -BC_FIXTURE_TEST_CASE(estimator__pop__query_top_link__removes_top_block_fees, estimator_query_setup_fixture) -{ - constexpr uint64_t value = 5'000'000'000; - const system::settings bitcoin{ chain::selection::mainnet }; - const auto& genesis = bitcoin.genesis_block; - const auto coinbase1 = estimator_coinbase(1, value); - const auto spend = estimator_spend(coinbase1, 10'000u); - const auto block1 = estimator_block(genesis, 1, { coinbase1 }); - const auto block2 = estimator_block(block1, 2, { estimator_coinbase(2, value), spend }); - BC_REQUIRE(estimator_confirm(query_, block1, 1)); - BC_REQUIRE(estimator_confirm(query_, block2, 2)); - BC_REQUIRE(estimator_pool(store_, query_, spend, 1, 10'000u)); - - const auto instance = acessor::create(); - BC_REQUIRE(instance->initialize(cancel_, query_, 3)); - BC_REQUIRE_EQUAL(instance->top_height(), 2u); - BC_REQUIRE_GT(estimator_small_total(*instance), 0u); - - BC_REQUIRE(instance->pop(query_, query_.to_header(block2.hash()))); - BC_REQUIRE_EQUAL(instance->top_height(), 1u); - BC_REQUIRE_EQUAL(estimator_small_total(*instance), 0u); -} - -BC_FIXTURE_TEST_CASE(estimator__push__query_top_link__adds_top_block_fees, estimator_query_setup_fixture) -{ - constexpr uint64_t value = 5'000'000'000; - const system::settings bitcoin{ chain::selection::mainnet }; - const auto& genesis = bitcoin.genesis_block; - const auto coinbase1 = estimator_coinbase(1, value); - const auto spend = estimator_spend(coinbase1, 10'000u); - const auto block1 = estimator_block(genesis, 1, { coinbase1 }); - const auto block2 = estimator_block(block1, 2, { estimator_coinbase(2, value), spend }); - BC_REQUIRE(estimator_confirm(query_, block1, 1)); - BC_REQUIRE(query_.set(spend)); - BC_REQUIRE(estimator_pool(store_, query_, spend, 1, 10'000u)); - - const auto instance = acessor::create(); - BC_REQUIRE(instance->initialize(cancel_, query_, 2)); - BC_REQUIRE_EQUAL(instance->top_height(), 1u); - BC_REQUIRE_EQUAL(estimator_small_total(*instance), 0u); - - store_.set_pooling(); - BC_REQUIRE(estimator_confirm(query_, block2, 2)); - BC_REQUIRE(instance->push(query_, query_.to_header(block2.hash()))); - BC_REQUIRE_EQUAL(instance->top_height(), 2u); - BC_REQUIRE_GT(estimator_small_total(*instance), 0u); -} - -BC_AUTO_TEST_SUITE_END() diff --git a/test/full_node.cpp b/test/full_node.cpp index d79136fe1..5962fa6f5 100644 --- a/test/full_node.cpp +++ b/test/full_node.cpp @@ -467,7 +467,7 @@ BC_AUTO_TEST_CASE(full_node__submit__relay_disabled__pooling_disabled) BC_AUTO_TEST_CASE(full_node__estimate__disabled__estimate_disabled) { std::promise promise{}; - node_.estimate(1, estimator::mode::basic, [&](const code& ec, uint64_t) NOEXCEPT + node_.estimate(1, estimate_mode::basic, [&](const code& ec, uint64_t) NOEXCEPT { promise.set_value(ec); }); diff --git a/test/protocols/protocol.cpp b/test/protocols/protocol.cpp index 9697e1eec..1ed9e55cc 100644 --- a/test/protocols/protocol.cpp +++ b/test/protocols/protocol.cpp @@ -273,7 +273,7 @@ BC_AUTO_TEST_CASE(protocol__organize__block1_header_not_milestone__height_one) BC_AUTO_TEST_CASE(protocol__estimate__default__disabled) { std::promise result{}; - accessor.estimate(1, estimator::mode::basic, [&](const code& ec, uint64_t) NOEXCEPT + accessor.estimate(1, estimate_mode::basic, [&](const code& ec, uint64_t) NOEXCEPT { result.set_value(ec); }); diff --git a/test/sessions/session.cpp b/test/sessions/session.cpp index 5e265924a..a14cece57 100644 --- a/test/sessions/session.cpp +++ b/test/sessions/session.cpp @@ -369,7 +369,7 @@ BC_AUTO_TEST_CASE(session__submit__relay_disabled__pooling_disabled) BC_AUTO_TEST_CASE(session__estimate__disabled__estimate_disabled) { std::promise promise{}; - session_.estimate(1, estimator::mode::basic, [&](const code& ec, uint64_t) NOEXCEPT + session_.estimate(1, estimate_mode::basic, [&](const code& ec, uint64_t) NOEXCEPT { promise.set_value(ec); }); diff --git a/test/settings.cpp b/test/settings.cpp index 55adb7ef6..56bec0430 100644 --- a/test/settings.cpp +++ b/test/settings.cpp @@ -50,7 +50,6 @@ BC_AUTO_TEST_CASE(settings__node__default_context__expected) BC_REQUIRE_EQUAL(node.batch_verify, 1'000'000_u64); BC_REQUIRE_EQUAL(node.batch_silent, 1'000'000_u64); BC_REQUIRE_EQUAL(node.current_connections, 10_u16); - BC_REQUIRE_EQUAL(node.fee_estimate_horizon, 0u); BC_REQUIRE_EQUAL(node.maximum_height, 0_u32); BC_REQUIRE_EQUAL(node.maximum_height_(), max_size_t); BC_REQUIRE_EQUAL(node.silent_start_height, 0xffffffff_u32); @@ -67,8 +66,6 @@ BC_AUTO_TEST_CASE(settings__node__default_context__expected) BC_REQUIRE_EQUAL(node.threads_(), cores()); BC_REQUIRE_EQUAL(node.maximum_height_(), max_size_t); BC_REQUIRE_EQUAL(node.maximum_concurrency_(), 50'000_size); - BC_REQUIRE_EQUAL(node.fee_estimate_horizon_(), 0_size); - BC_REQUIRE(!node.fee_estimate_enabled()); BC_REQUIRE_EQUAL(node.use_gpu_(), system::batched::accelerated()); BC_REQUIRE(node.batch_verify_enabled()); BC_REQUIRE(node.batch_silent_enabled()); diff --git a/test/test.cpp b/test/test.cpp index 1e6219f0e..d03a30c07 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -187,7 +187,6 @@ node::settings node_settings() NOEXCEPT out.network = system::chain::selection::mainnet; out.batch_verify = 0; out.batch_silent = 0; - out.fee_estimate_horizon = 0; out.minimum_fee_rate = 0.0; out.maximum_concurrency = 50'000; return out;