Coverage Report

Created: 2026-10-07 13:27

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