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/check.h>
12 : : #include <util/fees.h>
13 : :
14 : 2316 : FeeRateEstimatorManager::~FeeRateEstimatorManager() = default;
15 : :
16 : 1158 : FeeRateEstimatorManager::FeeRateEstimatorManager(const fs::path& block_policy_path,
17 : : bool read_stale_estimates,
18 : : const fs::path& mempool_estimator_path,
19 : : const CTxMemPool& mempool,
20 : 1158 : ChainstateManager& chainman)
21 : 1158 : : m_block_policy_estimator(std::make_unique<CBlockPolicyEstimator>(block_policy_path, read_stale_estimates)),
22 [ + - ]: 1158 : m_mempool_estimator(std::make_unique<MemPoolFeeRateEstimator>(mempool_estimator_path, mempool, chainman))
23 : : {
24 : 1158 : }
25 : :
26 : 6869 : util::Expected<FeeRateEstimation, FeeRateEstimationError> FeeRateEstimatorManager::GetFeeRateEstimate(int target, bool conservative) const
27 : : {
28 : 6869 : auto selected_estimate = m_block_policy_estimator->EstimateFeeRate(target, conservative);
29 [ + + ]: 6869 : if (!selected_estimate) {
30 [ + - + - : 6869 : LogDebug(BCLog::ESTIMATEFEE, "%s", selected_estimate.error().reason);
+ - ]
31 : : return selected_estimate;
32 : : }
33 [ + - ]: 3191 : auto mempool_estimate = m_mempool_estimator->EstimateFeeRate(conservative);
34 [ + + ]: 3191 : if (mempool_estimate) {
35 : 3184 : selected_estimate = std::min(*selected_estimate, *mempool_estimate);
36 : : } else {
37 [ + - + - : 7 : LogDebug(BCLog::ESTIMATEFEE, "%s", mempool_estimate.error().reason);
+ - ]
38 : : }
39 [ + - + - : 6382 : LogDebug(BCLog::ESTIMATEFEE, "Fee rate estimated using %s: target=%s feerate=%s %s/kvB.",
+ - + - +
- + - ]
40 : : FeeRateEstimatorTypeToString(selected_estimate->feerate_estimator),
41 : : selected_estimate->returned_target, CFeeRate(selected_estimate->feerate).GetFeePerK(), CURRENCY_ATOM);
42 : 3191 : return selected_estimate;
43 : : }
44 : :
45 : 202 : util::Expected<FeeRateEstimation, FeeRateEstimationError> FeeRateEstimatorManager::GetFeeRateEstimate(FeeRateEstimatorType type, int target, bool conservative) const
46 : : {
47 [ + + + - ]: 202 : switch (type) {
48 : 20 : case FeeRateEstimatorType::AUTO:
49 : 20 : return GetFeeRateEstimate(target, conservative);
50 : 175 : case FeeRateEstimatorType::BLOCK_POLICY:
51 : 175 : return m_block_policy_estimator->EstimateFeeRate(target, conservative);
52 : 7 : case FeeRateEstimatorType::MEMPOOL_POLICY:
53 : 7 : return m_mempool_estimator->EstimateFeeRate(conservative);
54 : : } // no default case, so the compiler can warn about missing cases
55 : 0 : AssertUnreachable();
56 : : }
57 : :
58 : 4 : void FeeRateEstimatorManager::IntervalFlush()
59 : : {
60 : 4 : m_block_policy_estimator->FlushFeeEstimates();
61 : 4 : m_mempool_estimator->FlushMinedBlockStats();
62 : 4 : }
63 : :
64 : 1158 : void FeeRateEstimatorManager::ShutdownFlush()
65 : : {
66 : 1158 : m_block_policy_estimator->Flush();
67 : 1158 : m_mempool_estimator->FlushMinedBlockStats();
68 : 1158 : }
69 : :
70 : 606 : void FeeRateEstimatorManager::MempoolLoadFailed()
71 : : {
72 : 606 : m_mempool_estimator->MempoolLoadFailed();
73 : 606 : }
74 : :
75 : 8 : std::vector<MinedBlockStats> FeeRateEstimatorManager::MempoolPolicyEstimatorBlocksStats() const
76 : : {
77 : 8 : return m_mempool_estimator->GetPrevBlockData();
78 : : }
79 : :
80 : 28397 : void FeeRateEstimatorManager::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /*unused*/)
81 : : {
82 : 28397 : m_block_policy_estimator->processTransaction(tx);
83 : 28397 : }
84 : :
85 : 2051 : void FeeRateEstimatorManager::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason /*unused*/, uint64_t /*unused*/)
86 : : {
87 : 2051 : m_block_policy_estimator->removeTx(tx->GetHash());
88 : 2051 : }
89 : :
90 : 104316 : void FeeRateEstimatorManager::MempoolTransactionsRemovedForBlock(const std::shared_ptr<const CBlock>& block, const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int block_height)
91 : : {
92 : 104316 : m_block_policy_estimator->processBlock(txs_removed_for_block, block_height);
93 : 104316 : m_mempool_estimator->MempoolTxsRemovedForBlock(block, txs_removed_for_block, block_height);
94 : 104316 : }
95 : :
96 : 319 : CFeeRate FeeRateEstimatorManager::BlockPolicyEstimateRawFee(unsigned int target, double threshold, FeeEstimateHorizon horizon, EstimationResult* buckets) const
97 : : {
98 : 319 : return m_block_policy_estimator->estimateRawFee(target, threshold, horizon, buckets);
99 : : }
100 : :
101 : 384 : unsigned int FeeRateEstimatorManager::BlockPolicyHighestTargetTracked(FeeEstimateHorizon horizon) const
102 : : {
103 : 384 : return m_block_policy_estimator->HighestTargetTracked(horizon);
104 : : }
105 : :
106 : 4216 : unsigned int FeeRateEstimatorManager::MaximumTarget() const
107 : : {
108 [ + - ]: 4216 : return std::max(m_block_policy_estimator->MaximumTarget(), m_mempool_estimator->MaximumTarget());
109 : : }
|