Bitcoin Core 31.99.0
P2P Digital Currency
estimator_man.cpp
Go to the documentation of this file.
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
6
7#include <logging.h>
8#include <policy/feerate.h>
11#include <util/fees.h>
12
14
15FeeRateEstimatorManager::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 : m_block_policy_estimator(std::make_unique<CBlockPolicyEstimator>(block_policy_path, read_stale_estimates)),
21 m_mempool_estimator(std::make_unique<MemPoolFeeRateEstimator>(mempool_estimator_path, mempool, chainman))
22{
23}
24
26{
27 auto block_policy_estimate = m_block_policy_estimator->EstimateFeeRate(target, conservative);
28 if (!block_policy_estimate) {
29 LogDebug(BCLog::ESTIMATEFEE, "%s", block_policy_estimate.error().reason);
30 return block_policy_estimate;
31 }
32 auto mempool_estimate = m_mempool_estimator->EstimateFeeRate(conservative);
33 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 LogDebug(BCLog::ESTIMATEFEE, "%s", mempool_estimate.error().reason);
37 return mempool_estimate;
38 }
39 auto selected_estimate = std::min(*block_policy_estimate, *mempool_estimate);
40 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 return selected_estimate;
44}
45
47{
48 switch (type) {
50 return GetFeeRateEstimate(target, conservative);
52 return m_block_policy_estimator->EstimateFeeRate(target, conservative);
54 return m_mempool_estimator->EstimateFeeRate(conservative);
55 } // no default case, so the compiler can warn about missing cases
56 assert(false);
57}
58
60{
61 m_block_policy_estimator->FlushFeeEstimates();
62 m_mempool_estimator->FlushMinedBlockStats();
63}
64
66{
68 m_mempool_estimator->FlushMinedBlockStats();
69}
70
72{
73 return m_mempool_estimator->GetPrevBlockData();
74}
75
77{
78 m_block_policy_estimator->processTransaction(tx);
79}
80
82{
83 m_block_policy_estimator->removeTx(tx->GetHash());
84}
85
86void FeeRateEstimatorManager::MempoolTransactionsRemovedForBlock(const std::shared_ptr<const CBlock>& block, const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int block_height)
87{
88 m_block_policy_estimator->processBlock(txs_removed_for_block, block_height);
89 m_mempool_estimator->MempoolTxsRemovedForBlock(block, txs_removed_for_block, block_height);
90}
91
92CFeeRate FeeRateEstimatorManager::BlockPolicyEstimateRawFee(unsigned int target, double threshold, FeeEstimateHorizon horizon, EstimationResult* buckets) const
93{
94 return m_block_policy_estimator->estimateRawFee(target, threshold, horizon, buckets);
95}
96
98{
99 return m_block_policy_estimator->HighestTargetTracked(horizon);
100}
101
103{
104 return std::max(m_block_policy_estimator->MaximumTarget(), m_mempool_estimator->MaximumTarget());
105}
FeeEstimateHorizon
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:32
CAmount GetFeePerK() const
Return the fee in satoshis for a vsize of 1000 vbytes.
Definition: feerate.h:71
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:187
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition: validation.h:945
std::unique_ptr< MemPoolFeeRateEstimator > m_mempool_estimator
Definition: estimator_man.h:99
unsigned int BlockPolicyHighestTargetTracked(FeeEstimateHorizon horizon) const
Returns the maximum supported confirmation target of block policy estimator.
virtual util::Expected< FeeRateEstimation, FeeRateEstimationError > GetFeeRateEstimate(int target, bool conservative) const
Get a fee rate estimate from the available fee rate estimators.
FeeRateEstimatorManager(const fs::path &block_policy_path, bool read_stale_estimates, const fs::path &mempool_estimator_path, const CTxMemPool &mempool, ChainstateManager &chainman)
std::vector< MinedBlockStats > MempoolPolicyEstimatorBlocksStats() const
Returns per-block weight statistics for the last MEMPOOL_HEALTH_WINDOW_BLOCKS mined blocks.
void IntervalFlush()
Flush recorded data to disk.
void MempoolTransactionsRemovedForBlock(const std::shared_ptr< const CBlock > &block, const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int block_height) override
void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason, uint64_t) override
Notifies listeners of a transaction leaving mempool.
virtual unsigned int MaximumTarget() const
Returns the maximum supported confirmation target from all fee rate estimators.
std::unique_ptr< CBlockPolicyEstimator > m_block_policy_estimator
Definition: estimator_man.h:98
CFeeRate BlockPolicyEstimateRawFee(unsigned int target, double threshold, FeeEstimateHorizon horizon, EstimationResult *buckets) const
Delegate to the block policy estimator's estimateRawFee (used by the estimaterawfee RPC).
void ShutdownFlush()
Flush recorded data to disk as part of shutdown sequence.
void TransactionAddedToMempool(const NewMempoolTransactionInfo &tx, uint64_t) override
Overridden from CValidationInterface.
virtual ~FeeRateEstimatorManager()
Estimate the fee rate required for a transaction to be included in the next block.
The util::Expected class provides a standard way for low-level functions to return either error value...
Definition: expected.h:44
const std::string CURRENCY_ATOM
Definition: feerate.h:20
#define LogDebug(category,...)
Definition: log.h:143
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
@ ESTIMATEFEE
Definition: categories.h:24
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
std::string_view FeeRateEstimatorTypeToString(FeeRateEstimatorType feerate_estimator_type)
Definition: fees.cpp:12
FeeRateEstimatorType
Identifier for fee rate estimator.
Definition: fees.h:36
assert(!tx.IsCoinBase())