|
| 1 | +/** |
| 2 | + * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS) |
| 3 | + * |
| 4 | + * This file is part of libbitcoin. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +#ifndef LIBBITCOIN_SERVER_ESTIMATOR_HPP |
| 20 | +#define LIBBITCOIN_SERVER_ESTIMATOR_HPP |
| 21 | + |
| 22 | +#include <atomic> |
| 23 | +#include <bitcoin/server.hpp> |
| 24 | + |
| 25 | +namespace libbitcoin { |
| 26 | +namespace server { |
| 27 | + |
| 28 | +class BCS_API estimator |
| 29 | +{ |
| 30 | +public: |
| 31 | + DELETE_COPY_MOVE_DESTRUCT(estimator); |
| 32 | + |
| 33 | + /// Estimation modes. |
| 34 | + enum class mode |
| 35 | + { |
| 36 | + basic, |
| 37 | + markov, |
| 38 | + economical, |
| 39 | + conservative |
| 40 | + }; |
| 41 | + |
| 42 | + estimator() NOEXCEPT {}; |
| 43 | + |
| 44 | + /// Fee estimation in satoshis / transaction virtual size. |
| 45 | + /// Pass zero to target next block for confirmation, range:0..1007. |
| 46 | + uint64_t estimate(size_t target, mode mode) const NOEXCEPT; |
| 47 | + |
| 48 | + /// Populate accumulator. |
| 49 | + bool initialize(std::atomic_bool& cancel, const node::query& query, |
| 50 | + size_t top, size_t count) NOEXCEPT; |
| 51 | + |
| 52 | + /// Update accumulator. |
| 53 | + bool push(const node::query& query) NOEXCEPT; |
| 54 | + bool pop(const node::query& query) NOEXCEPT; |
| 55 | + |
| 56 | + /// Top height of accumulator. |
| 57 | + size_t top_height() const NOEXCEPT; |
| 58 | + |
| 59 | +protected: |
| 60 | + using rates = database::fee_rates; |
| 61 | + using rate_sets = database::fee_rate_sets; |
| 62 | + |
| 63 | + /// Bucket count sizing parameters. |
| 64 | + enum horizon : size_t |
| 65 | + { |
| 66 | + small = 12, |
| 67 | + medium = 48, |
| 68 | + large = 1008 |
| 69 | + }; |
| 70 | + |
| 71 | + /// Estimation confidences. |
| 72 | + struct confidence |
| 73 | + { |
| 74 | + static constexpr double low = 0.60; |
| 75 | + static constexpr double mid = 0.85; |
| 76 | + static constexpr double high = 0.95; |
| 77 | + }; |
| 78 | + |
| 79 | + /// Bucket count sizing parameters. |
| 80 | + struct sizing |
| 81 | + { |
| 82 | + static constexpr double min = 0.1; |
| 83 | + static constexpr double max = 100'000.0; |
| 84 | + static constexpr double step = 1.05; |
| 85 | + |
| 86 | + /// Derived from min/max/step above. |
| 87 | + static constexpr size_t count = 283; |
| 88 | + }; |
| 89 | + |
| 90 | + /// Accumulator (persistent, decay-weighted counters). |
| 91 | + struct accumulator |
| 92 | + { |
| 93 | + template <size_t Depth> |
| 94 | + struct bucket |
| 95 | + { |
| 96 | + /// Total scaled txs in bucket. |
| 97 | + std::atomic<size_t> total{}; |
| 98 | + |
| 99 | + /// confirmed[n]: scaled txs confirmed in > n blocks. |
| 100 | + std::array<std::atomic<size_t>, Depth> confirmed; |
| 101 | + }; |
| 102 | + |
| 103 | + /// Current block height of accumulated state. |
| 104 | + size_t top_height{}; |
| 105 | + |
| 106 | + /// Accumulated scaled fee in decayed buckets by horizon. |
| 107 | + /// Array count is the half life of the decay it implies. |
| 108 | + std::array<bucket<horizon::small>, sizing::count> small{}; |
| 109 | + std::array<bucket<horizon::medium>, sizing::count> medium{}; |
| 110 | + std::array<bucket<horizon::large>, sizing::count> large{}; |
| 111 | + }; |
| 112 | + |
| 113 | + // C++23: make consteval (std::pow). |
| 114 | + static inline double decay_rate() NOEXCEPT |
| 115 | + { |
| 116 | + static const auto rate = std::pow(0.5, 1.0 / sizing::count); |
| 117 | + return rate; |
| 118 | + } |
| 119 | + |
| 120 | + static inline double to_scale_term(size_t age) NOEXCEPT |
| 121 | + { |
| 122 | + return std::pow(decay_rate(), age); |
| 123 | + } |
| 124 | + |
| 125 | + static inline double to_scale_factor(bool push) NOEXCEPT |
| 126 | + { |
| 127 | + return std::pow(decay_rate(), push ? +1.0 : -1.0); |
| 128 | + } |
| 129 | + |
| 130 | + const accumulator& history() const NOEXCEPT; |
| 131 | + bool initialize(const rate_sets& blocks) NOEXCEPT; |
| 132 | + bool push(const rates& block) NOEXCEPT; |
| 133 | + bool pop(const rates& block) NOEXCEPT; |
| 134 | + uint64_t compute(size_t target, double confidence, |
| 135 | + bool markov=false) const NOEXCEPT; |
| 136 | + |
| 137 | +private: |
| 138 | + bool update(const rates& block, size_t height, bool push) NOEXCEPT; |
| 139 | + void decay(auto& buckets, double factor) NOEXCEPT; |
| 140 | + void decay(bool push) NOEXCEPT; |
| 141 | + |
| 142 | + accumulator fees_{}; |
| 143 | +}; |
| 144 | + |
| 145 | +} // namespace server |
| 146 | +} // namespace libbitcoin |
| 147 | + |
| 148 | +#endif |
0 commit comments