Bitcoin Core  0.20.99
P2P Digital Currency
fees.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_POLICY_FEES_H
6 #define BITCOIN_POLICY_FEES_H
7 
8 #include <amount.h>
9 #include <policy/feerate.h>
10 #include <uint256.h>
11 #include <random.h>
12 #include <sync.h>
13 
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 class CAutoFile;
20 class CFeeRate;
21 class CTxMemPoolEntry;
22 class CTxMemPool;
23 class TxConfirmStats;
24 
25 /* Identifier for each of the 3 different TxConfirmStats which will track
26  * history over different time horizons. */
27 enum class FeeEstimateHorizon {
28  SHORT_HALFLIFE = 0,
29  MED_HALFLIFE = 1,
30  LONG_HALFLIFE = 2
31 };
32 
34 
35 /* Enumeration of reason for returned fee estimate */
36 enum class FeeReason {
37  NONE,
43  PAYTXFEE,
44  FALLBACK,
45  REQUIRED,
46 };
47 
48 /* Used to return detailed information about a feerate bucket */
50 {
51  double start = -1;
52  double end = -1;
53  double withinTarget = 0;
54  double totalConfirmed = 0;
55  double inMempool = 0;
56  double leftMempool = 0;
57 };
58 
59 /* Used to return detailed information about a fee estimate calculation */
61 {
64  double decay = 0;
65  unsigned int scale = 0;
66 };
67 
69 {
72  int desiredTarget = 0;
73  int returnedTarget = 0;
74 };
75 
125 {
126 private:
128  static constexpr unsigned int SHORT_BLOCK_PERIODS = 12;
129  static constexpr unsigned int SHORT_SCALE = 1;
131  static constexpr unsigned int MED_BLOCK_PERIODS = 24;
132  static constexpr unsigned int MED_SCALE = 2;
134  static constexpr unsigned int LONG_BLOCK_PERIODS = 42;
135  static constexpr unsigned int LONG_SCALE = 24;
137  static const unsigned int OLDEST_ESTIMATE_HISTORY = 6 * 1008;
138 
140  static constexpr double SHORT_DECAY = .962;
142  static constexpr double MED_DECAY = .9952;
144  static constexpr double LONG_DECAY = .99931;
145 
147  static constexpr double HALF_SUCCESS_PCT = .6;
149  static constexpr double SUCCESS_PCT = .85;
151  static constexpr double DOUBLE_SUCCESS_PCT = .95;
152 
154  static constexpr double SUFFICIENT_FEETXS = 0.1;
156  static constexpr double SUFFICIENT_TXS_SHORT = 0.5;
157 
165  static constexpr double MIN_BUCKET_FEERATE = 1000;
166  static constexpr double MAX_BUCKET_FEERATE = 1e7;
167 
173  static constexpr double FEE_SPACING = 1.05;
174 
175 public:
179 
181  void processBlock(unsigned int nBlockHeight,
182  std::vector<const CTxMemPoolEntry*>& entries);
183 
185  void processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate);
186 
188  bool removeTx(uint256 hash, bool inBlock);
189 
191  CFeeRate estimateFee(int confTarget) const;
192 
198  CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const;
199 
204  CFeeRate estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult *result = nullptr) const;
205 
207  bool Write(CAutoFile& fileout) const;
208 
210  bool Read(CAutoFile& filein);
211 
213  void FlushUnconfirmed();
214 
216  unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;
217 
218 private:
220 
221  unsigned int nBestSeenHeight GUARDED_BY(m_cs_fee_estimator);
222  unsigned int firstRecordedHeight GUARDED_BY(m_cs_fee_estimator);
223  unsigned int historicalFirst GUARDED_BY(m_cs_fee_estimator);
224  unsigned int historicalBest GUARDED_BY(m_cs_fee_estimator);
225 
226  struct TxStatsInfo
227  {
228  unsigned int blockHeight;
229  unsigned int bucketIndex;
230  TxStatsInfo() : blockHeight(0), bucketIndex(0) {}
231  };
232 
233  // map of txids to information about that transaction
234  std::map<uint256, TxStatsInfo> mapMemPoolTxs GUARDED_BY(m_cs_fee_estimator);
235 
237  std::unique_ptr<TxConfirmStats> feeStats PT_GUARDED_BY(m_cs_fee_estimator);
238  std::unique_ptr<TxConfirmStats> shortStats PT_GUARDED_BY(m_cs_fee_estimator);
239  std::unique_ptr<TxConfirmStats> longStats PT_GUARDED_BY(m_cs_fee_estimator);
240 
241  unsigned int trackedTxs GUARDED_BY(m_cs_fee_estimator);
242  unsigned int untrackedTxs GUARDED_BY(m_cs_fee_estimator);
243 
244  std::vector<double> buckets GUARDED_BY(m_cs_fee_estimator); // The upper-bound of the range for the bucket (inclusive)
245  std::map<double, unsigned int> bucketMap GUARDED_BY(m_cs_fee_estimator); // Map of bucket upper-bound to index into all vectors by bucket
246 
248  bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry) EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
249 
251  double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
253  double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
255  unsigned int BlockSpan() const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
257  unsigned int HistoricalBlockSpan() const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
259  unsigned int MaxUsableEstimate() const EXCLUSIVE_LOCKS_REQUIRED(m_cs_fee_estimator);
260 };
261 
263 {
264 private:
265  static constexpr double MAX_FILTER_FEERATE = 1e7;
270  static constexpr double FEE_FILTER_SPACING = 1.1;
271 
272 public:
274  explicit FeeFilterRounder(const CFeeRate& minIncrementalFee);
275 
277  CAmount round(CAmount currentMinFee);
278 
279 private:
280  std::set<double> feeset;
282 };
283 
284 #endif // BITCOIN_POLICY_FEES_H
EstimatorBucket pass
Definition: fees.h:62
EstimationResult est
Definition: fees.h:70
We will instantiate an instance of this class to track transactions that were included in a block...
Definition: fees.cpp:36
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon)
Definition: fees.cpp:15
Force estimateSmartFee to use conservative estimates.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:78
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
EstimatorBucket fail
Definition: fees.h:63
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:124
Fast randomness source.
Definition: random.h:119
FeeReason
Definition: fees.h:36
FastRandomContext insecure_rand
Definition: fees.h:281
FeeEstimateHorizon
Definition: fees.h:27
256-bit opaque blob.
Definition: uint256.h:124
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:488
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
RecursiveMutex m_cs_fee_estimator
Definition: fees.h:219
#define GUARDED_BY(x)
Definition: threadsafety.h:38
std::set< double > feeset
Definition: fees.h:280
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:602
#define PT_GUARDED_BY(x)
Definition: threadsafety.h:39