Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license. See the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <policy/fees/estimator_man.h>
6 : :
7 : : #include <logging.h>
8 : : #include <policy/feerate.h>
9 : : #include <policy/fees/block_policy_estimator.h>
10 : : #include <policy/fees/mempool_estimator.h>
11 : : #include <util/fees.h>
12 : :
13 : 2192 : FeeRateEstimatorManager::~FeeRateEstimatorManager() = default;
14 : :
15 : 1096 : FeeRateEstimatorManager::FeeRateEstimatorManager(const fs::path& block_policy_path,
16 : : bool read_stale_estimates,
17 : : const fs::path& mempool_estimator_path,
18 : : const CTxMemPool& mempool,
19 : 1096 : ChainstateManager& chainman)
20 : 1096 : : m_block_policy_estimator(std::make_unique<CBlockPolicyEstimator>(block_policy_path, read_stale_estimates)),
21 [ + - ]: 1096 : m_mempool_estimator(std::make_unique<MemPoolFeeRateEstimator>(mempool_estimator_path, mempool, chainman))
22 : : {
23 : 1096 : }
24 : :
25 : 6599 : util::Expected<FeeRateEstimation, FeeRateEstimationError> FeeRateEstimatorManager::GetFeeRateEstimate(int target, bool conservative) const
26 : : {
27 : 6599 : auto block_policy_estimate = m_block_policy_estimator->EstimateFeeRate(target, conservative);
28 [ + + ]: 6599 : if (!block_policy_estimate) {
29 [ + - + - : 3507 : LogDebug(BCLog::ESTIMATEFEE, "%s", block_policy_estimate.error().reason);
+ - ]
30 : 3507 : return block_policy_estimate;
31 : : }
32 [ + - ]: 3092 : auto mempool_estimate = m_mempool_estimator->EstimateFeeRate(conservative);
33 [ + + ]: 3092 : if (!mempool_estimate) {
34 : : // A failed mempool estimate is surfaced as a warning rather than silently returning the
35 : : // block policy estimate, which callers can still request explicitly.
36 [ + - + - : 1 : LogDebug(BCLog::ESTIMATEFEE, "%s", mempool_estimate.error().reason);
+ - ]
37 : 1 : return mempool_estimate;
38 : : }
39 : 3091 : auto selected_estimate = std::min(*block_policy_estimate, *mempool_estimate);
40 [ + - + - : 6182 : LogDebug(BCLog::ESTIMATEFEE, "Fee rate estimated using %s: target=%s feerate=%s %s/kvB.",
+ - + - +
- ]
41 : : FeeRateEstimatorTypeToString(selected_estimate.feerate_estimator),
42 : : selected_estimate.returned_target, CFeeRate(selected_estimate.feerate).GetFeePerK(), CURRENCY_ATOM);
43 : 3091 : return selected_estimate;
44 : 9691 : }
45 : :
46 : 187 : util::Expected<FeeRateEstimation, FeeRateEstimationError> FeeRateEstimatorManager::GetFeeRateEstimate(FeeRateEstimatorType type, int target, bool conservative) const
47 : : {
48 [ + + + - ]: 187 : switch (type) {
49 : 13 : case FeeRateEstimatorType::NONE:
50 : 13 : return GetFeeRateEstimate(target, conservative);
51 : 172 : case FeeRateEstimatorType::BLOCK_POLICY:
52 : 172 : return m_block_policy_estimator->EstimateFeeRate(target, conservative);
53 : 2 : case FeeRateEstimatorType::MEMPOOL_POLICY:
54 : 2 : return m_mempool_estimator->EstimateFeeRate(conservative);
55 : : } // no default case, so the compiler can warn about missing cases
56 : 0 : assert(false);
57 : : }
58 : :
59 : 4 : void FeeRateEstimatorManager::IntervalFlush()
60 : : {
61 : 4 : m_block_policy_estimator->FlushFeeEstimates();
62 : 4 : m_mempool_estimator->FlushMinedBlockStats();
63 : 4 : }
64 : :
65 : 1096 : void FeeRateEstimatorManager::ShutdownFlush()
66 : : {
67 : 1096 : m_block_policy_estimator->Flush();
68 : 1096 : m_mempool_estimator->FlushMinedBlockStats();
69 : 1096 : }
70 : :
71 : 3 : std::vector<MinedBlockStats> FeeRateEstimatorManager::MempoolPolicyEstimatorBlocksStats() const
72 : : {
73 : 3 : return m_mempool_estimator->GetPrevBlockData();
74 : : }
75 : :
76 : 27152 : void FeeRateEstimatorManager::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /*unused*/)
77 : : {
78 : 27152 : m_block_policy_estimator->processTransaction(tx);
79 : 27152 : }
80 : :
81 : 1983 : void FeeRateEstimatorManager::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason /*unused*/, uint64_t /*unused*/)
82 : : {
83 : 1983 : m_block_policy_estimator->removeTx(tx->GetHash());
84 : 1983 : }
85 : :
86 : 114371 : void FeeRateEstimatorManager::MempoolTransactionsRemovedForBlock(const std::shared_ptr<const CBlock>& block, const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int block_height)
87 : : {
88 : 114371 : m_block_policy_estimator->processBlock(txs_removed_for_block, block_height);
89 : 114371 : m_mempool_estimator->MempoolTxsRemovedForBlock(block, txs_removed_for_block, block_height);
90 : 114371 : }
91 : :
92 : 319 : CFeeRate FeeRateEstimatorManager::BlockPolicyEstimateRawFee(unsigned int target, double threshold, FeeEstimateHorizon horizon, EstimationResult* buckets) const
93 : : {
94 : 319 : return m_block_policy_estimator->estimateRawFee(target, threshold, horizon, buckets);
95 : : }
96 : :
97 : 384 : unsigned int FeeRateEstimatorManager::BlockPolicyHighestTargetTracked(FeeEstimateHorizon horizon) const
98 : : {
99 : 384 : return m_block_policy_estimator->HighestTargetTracked(horizon);
100 : : }
101 : :
102 : 4062 : unsigned int FeeRateEstimatorManager::MaximumTarget() const
103 : : {
104 [ + - ]: 4062 : return std::max(m_block_policy_estimator->MaximumTarget(), m_mempool_estimator->MaximumTarget());
105 : : }
|