Bitcoin Core 31.99.0
P2P Digital Currency
mempool_estimator.h
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
5#ifndef BITCOIN_POLICY_FEES_MEMPOOL_ESTIMATOR_H
6#define BITCOIN_POLICY_FEES_MEMPOOL_ESTIMATOR_H
7
9#include <sync.h>
10#include <threadsafety.h>
11#include <uint256.h>
12#include <util/expected.h>
13#include <util/feefrac.h>
14#include <util/fees.h>
15#include <util/fs.h>
16#include <util/time.h>
17
18#include <chrono>
19#include <memory>
20#include <optional>
21#include <span>
22#include <vector>
23
24class CBlock;
25class AutoFile;
27class CTxMemPool;
28
30
31// Fee rate estimate for confirmation target above this is not reliable,
32// as mempool conditions are likely to change.
34constexpr std::chrono::seconds CACHE_LIFE{7};
35
36// Constants for mempool sanity checks.
37constexpr size_t MEMPOOL_HEALTH_WINDOW_BLOCKS = 6;
38constexpr double MEMPOOL_REPRESENTATION_THRESHOLD = 0.75;
39
43 uint64_t m_height{0};
47 uint64_t m_block_weight{0};
48};
49
56{
57public:
62 bool IsStale() const;
66 };
68 std::optional<FeeRateEstimate> GetCachedEstimate(const uint256& tip_hash) const;
70 void Update(FeePerVSize conservative, FeePerVSize economical, const uint256& tip_hash);
72 void Clear();
73
74private:
75 std::optional<FeeRateEstimate> m_fee_rate_estimation;
78};
79
88{
89public:
90 // Block percentiles fee rate (in sat/vB).
91 struct Percentiles {
94 };
95
96 MemPoolFeeRateEstimator(fs::path mempool_estimator_file_path,
97 const CTxMemPool& mempool,
98 ChainstateManager& chainman);
107 static Percentiles CalculateMaxWeightPercentiles(std::span<const FeePerVSize> chunk_feerates);
110 unsigned int MaximumTarget() const
111 {
113 }
114
115 std::vector<MinedBlockStats> GetPrevBlockData() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
116 {
117 LOCK(cs);
118 return m_prev_mined_blocks;
119 }
120
121 void MempoolTxsRemovedForBlock(const std::shared_ptr<const CBlock>& block,
122 const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block,
123 unsigned int block_height)
126 enum class MempoolHealth {
128 HEALTHY,
130 INSUFFICIENT_DATA,
132 LOW_COVERAGE,
133 };
142 bool Write(AutoFile& file) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
143
144private:
147 std::vector<MinedBlockStats> m_prev_mined_blocks GUARDED_BY(cs);
148 uint256 m_mined_blocks_tip_hash GUARDED_BY(cs);
149
152 mutable Mutex cs;
155};
156
157#endif // BITCOIN_POLICY_FEES_MEMPOOL_ESTIMATOR_H
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:395
Definition: block.h:74
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
MemPoolFeeRateEstimatorCache holds a cache of recent fee rate estimates.
std::optional< FeeRateEstimate > GetCachedEstimate(const uint256 &tip_hash) const
Returns cached estimates if not stale and computed on tip_hash, nullopt otherwise.
NodeClock::time_point m_last_updated
MemPoolFeeRateEstimatorCache(const MemPoolFeeRateEstimatorCache &)=delete
void Clear()
Clear cached fee rate estimates.
bool IsStale() const
Returns true if the cache is empty or older than CACHE_LIFE.
std::optional< FeeRateEstimate > m_fee_rate_estimation
MemPoolFeeRateEstimatorCache & operator=(const MemPoolFeeRateEstimatorCache &)=delete
void Update(FeePerVSize conservative, FeePerVSize economical, const uint256 &tip_hash)
Update the cache with new estimates computed on tip_hash.
Estimate the fee rate required for a transaction to be included in the next block.
~MemPoolFeeRateEstimator()=default
void ReadFromDisk() EXCLUSIVE_LOCKS_REQUIRED(!cs)
std::vector< MinedBlockStats > m_prev_mined_blocks GUARDED_BY(cs)
Tracks weight statistics for the last MEMPOOL_HEALTH_WINDOW_BLOCKS mined blocks.
bool Write(AutoFile &file) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Serialize mined-block stats without taking ownership of file.
MempoolHealth GetMempoolHealth() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
const CTxMemPool & m_mempool
ChainstateManager & m_chainman
unsigned int MaximumTarget() const
bool IsMempoolHealthy() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Checks if recent mined blocks indicate a healthy mempool state.
void MempoolTxsRemovedForBlock(const std::shared_ptr< const CBlock > &block, const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int block_height) EXCLUSIVE_LOCKS_REQUIRED(!cs)
void FlushMinedBlockStats() EXCLUSIVE_LOCKS_REQUIRED(!cs)
static Percentiles CalculateMaxWeightPercentiles(std::span< const FeePerVSize > chunk_feerates)
Calculate the 50th and 75th percentile fee rates from block template chunks, sorted in descending min...
const fs::path m_mempool_estimator_file_path
std::vector< MinedBlockStats > GetPrevBlockData() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
bool Read(AutoFile &file) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Deserialize mined-block stats without taking ownership of file.
util::Expected< FeeRateEstimation, FeeRateEstimationError > EstimateFeeRate(bool conservative) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
MemPoolFeeRateEstimator(fs::path mempool_estimator_file_path, const CTxMemPool &mempool, ChainstateManager &chainman)
MempoolHealth
Health of the recent mined-block window for fee rate estimation.
@ HEALTHY
Recent blocks represent the mempool well enough to estimate a fee rate.
256-bit opaque blob.
Definition: uint256.h:196
The util::Expected class provides a standard way for low-level functions to return either error value...
Definition: expected.h:44
constexpr int MEMPOOL_FEE_ESTIMATOR_MAX_TARGET
constexpr std::chrono::seconds CACHE_LIFE
constexpr double MEMPOOL_REPRESENTATION_THRESHOLD
constexpr size_t MEMPOOL_HEALTH_WINDOW_BLOCKS
Weight statistics for a recently mined block, used to assess mempool coverage.
uint64_t m_block_weight
Total non-coinbase transaction weight in the block.
uint64_t m_height
Block height.
uint64_t m_removed_block_txs_weight
Weight of mempool transactions removed for this block (excluding coinbase).
std::chrono::time_point< NodeClock > time_point
Definition: time.h:28
#define LOCK(cs)
Definition: sync.h:268
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49