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