BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
fees.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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  MAXTXFEE,
47 };
48 
49 std::string StringForFeeReason(FeeReason reason);
50 
51 /* Used to determine type of fee estimation requested */
52 enum class FeeEstimateMode {
53  UNSET,
54  ECONOMICAL,
55  CONSERVATIVE,
56 };
57 
58 bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode);
59 
60 /* Used to return detailed information about a feerate bucket */
62 {
63  double start = -1;
64  double end = -1;
65  double withinTarget = 0;
66  double totalConfirmed = 0;
67  double inMempool = 0;
68  double leftMempool = 0;
69 };
70 
71 /* Used to return detailed information about a fee estimate calculation */
73 {
76  double decay = 0;
77  unsigned int scale = 0;
78 };
79 
81 {
84  int desiredTarget = 0;
85  int returnedTarget = 0;
86 };
87 
137 {
138 private:
140  static constexpr unsigned int SHORT_BLOCK_PERIODS = 12;
141  static constexpr unsigned int SHORT_SCALE = 1;
143  static constexpr unsigned int MED_BLOCK_PERIODS = 24;
144  static constexpr unsigned int MED_SCALE = 2;
146  static constexpr unsigned int LONG_BLOCK_PERIODS = 42;
147  static constexpr unsigned int LONG_SCALE = 24;
149  static const unsigned int OLDEST_ESTIMATE_HISTORY = 6 * 1008;
150 
152  static constexpr double SHORT_DECAY = .962;
154  static constexpr double MED_DECAY = .9952;
156  static constexpr double LONG_DECAY = .99931;
157 
159  static constexpr double HALF_SUCCESS_PCT = .6;
161  static constexpr double SUCCESS_PCT = .85;
163  static constexpr double DOUBLE_SUCCESS_PCT = .95;
164 
166  static constexpr double SUFFICIENT_FEETXS = 0.1;
168  static constexpr double SUFFICIENT_TXS_SHORT = 0.5;
169 
177  static constexpr double MIN_BUCKET_FEERATE = 1000;
178  static constexpr double MAX_BUCKET_FEERATE = 1e7;
179 
185  static constexpr double FEE_SPACING = 1.05;
186 
187 public:
191 
193  void processBlock(unsigned int nBlockHeight,
194  std::vector<const CTxMemPoolEntry*>& entries);
195 
197  void processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate);
198 
200  bool removeTx(uint256 hash, bool inBlock);
201 
203  CFeeRate estimateFee(int confTarget) const;
204 
210  CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const;
211 
216  CFeeRate estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult *result = nullptr) const;
217 
219  bool Write(CAutoFile& fileout) const;
220 
222  bool Read(CAutoFile& filein);
223 
225  void FlushUnconfirmed();
226 
228  unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;
229 
230 private:
231  unsigned int nBestSeenHeight;
232  unsigned int firstRecordedHeight;
233  unsigned int historicalFirst;
234  unsigned int historicalBest;
235 
236  struct TxStatsInfo
237  {
238  unsigned int blockHeight;
239  unsigned int bucketIndex;
241  };
242 
243  // map of txids to information about that transaction
244  std::map<uint256, TxStatsInfo> mapMemPoolTxs;
245 
247  std::unique_ptr<TxConfirmStats> feeStats;
248  std::unique_ptr<TxConfirmStats> shortStats;
249  std::unique_ptr<TxConfirmStats> longStats;
250 
251  unsigned int trackedTxs;
252  unsigned int untrackedTxs;
253 
254  std::vector<double> buckets; // The upper-bound of the range for the bucket (inclusive)
255  std::map<double, unsigned int> bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
256 
258 
260  bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry);
261 
263  double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const;
265  double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const;
267  unsigned int BlockSpan() const;
269  unsigned int HistoricalBlockSpan() const;
271  unsigned int MaxUsableEstimate() const;
272 };
273 
275 {
276 private:
277  static constexpr double MAX_FILTER_FEERATE = 1e7;
282  static constexpr double FEE_FILTER_SPACING = 1.1;
283 
284 public:
286  explicit FeeFilterRounder(const CFeeRate& minIncrementalFee);
287 
289  CAmount round(CAmount currentMinFee);
290 
291 private:
292  std::set<double> feeset;
294 };
295 
296 #endif // BITCOIN_POLICY_FEES_H
static constexpr double MED_DECAY
Decay of .998 is a half-life of 144 blocks or about 1 day.
Definition: fees.h:154
EstimatorBucket pass
Definition: fees.h:74
bool FeeModeFromString(const std::string &mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: fees.cpp:50
EstimationResult est
Definition: fees.h:82
int returnedTarget
Definition: fees.h:85
CCriticalSection cs_feeEstimator
Definition: fees.h:257
static constexpr double MAX_BUCKET_FEERATE
Definition: fees.h:178
static constexpr double HALF_SUCCESS_PCT
Require greater than 60% of X feerate transactions to be confirmed within Y/2 blocks.
Definition: fees.h:159
unsigned int firstRecordedHeight
Definition: fees.h:232
static constexpr unsigned int MED_BLOCK_PERIODS
Track confirm delays up to 48 blocks for medium horizon.
Definition: fees.h:143
double start
Definition: fees.h:63
bool removeTx(uint256 hash, bool inBlock)
Remove a transaction from the mempool tracking stats.
Definition: fees.cpp:512
CBlockPolicyEstimator()
Create new BlockPolicyEstimator and initialize stats tracking classes with default values...
Definition: fees.cpp:527
bool Write(CAutoFile &fileout) const
Write estimation data to a file.
Definition: fees.cpp:899
FeeEstimateMode
Definition: fees.h:52
void FlushUnconfirmed()
Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool...
Definition: fees.cpp:984
FeeReason reason
Definition: fees.h:83
static constexpr double MAX_FILTER_FEERATE
Definition: fees.h:277
We will instantiate an instance of this class to track transactions that were included in a block...
Definition: fees.cpp:72
std::map< double, unsigned int > bucketMap
Definition: fees.h:255
std::unique_ptr< TxConfirmStats > shortStats
Definition: fees.h:248
std::unique_ptr< TxConfirmStats > longStats
Definition: fees.h:249
static constexpr double DOUBLE_SUCCESS_PCT
Require greater than 95% of X feerate transactions to be confirmed within 2 * Y blocks.
Definition: fees.h:163
static constexpr double FEE_SPACING
Spacing of FeeRate buckets We have to lump transactions into buckets based on feerate, but we want to be able to give accurate estimates over a large range of potential feerates Therefore it makes sense to exponentially space the buckets.
Definition: fees.h:185
unsigned int nBestSeenHeight
Definition: fees.h:231
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon)
Definition: fees.cpp:17
static constexpr double MIN_BUCKET_FEERATE
Minimum and Maximum values for tracking feerates The MIN_BUCKET_FEERATE should just be set to the low...
Definition: fees.h:177
double withinTarget
Definition: fees.h:65
unsigned int MaxUsableEstimate() const
Calculation of highest target that reasonable estimate can be provided for.
Definition: fees.cpp:747
int desiredTarget
Definition: fees.h:84
static constexpr double SUFFICIENT_TXS_SHORT
Require an avg of 0.5 tx when using short decay since there are fewer blocks considered.
Definition: fees.h:168
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:65
static constexpr double FEE_FILTER_SPACING
FEE_FILTER_SPACING is just used to provide some quantization of fee filter results.
Definition: fees.h:282
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr double SUCCESS_PCT
Require greater than 85% of X feerate transactions to be confirmed within Y blocks.
Definition: fees.h:161
std::unique_ptr< TxConfirmStats > feeStats
Classes to track historical data on transaction confirmations.
Definition: fees.h:247
static constexpr unsigned int SHORT_SCALE
Definition: fees.h:141
static constexpr double LONG_DECAY
Decay of .9995 is a half-life of 1008 blocks or about 1 week.
Definition: fees.h:156
unsigned int HistoricalBlockSpan() const
Number of blocks of recorded fee estimate data represented in saved data file.
Definition: fees.cpp:737
double end
Definition: fees.h:64
EstimatorBucket fail
Definition: fees.h:75
CFeeRate estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult *result=nullptr) const
Return a specific fee estimate calculation with a given success threshold and time horizon...
Definition: fees.cpp:673
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:136
CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const
Estimate feerate needed to get be included in a block within confTarget blocks.
Definition: fees.cpp:820
unsigned int historicalFirst
Definition: fees.h:233
Fast randomness source.
Definition: random.h:45
double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const
Helper for estimateSmartFee.
Definition: fees.cpp:796
unsigned int trackedTxs
Definition: fees.h:251
Use default settings based on other criteria.
double inMempool
Definition: fees.h:67
unsigned int BlockSpan() const
Number of blocks of data recorded while fee estimates have been running.
Definition: fees.cpp:729
FeeReason
Definition: fees.h:36
std::string StringForFeeReason(FeeReason reason)
Definition: fees.cpp:30
FeeFilterRounder(const CFeeRate &minIncrementalFee)
Create new FeeFilterRounder.
Definition: fees.cpp:997
std::map< uint256, TxStatsInfo > mapMemPoolTxs
Definition: fees.h:244
FastRandomContext insecure_rand
Definition: fees.h:293
CFeeRate estimateFee(int confTarget) const
DEPRECATED.
Definition: fees.cpp:664
static constexpr unsigned int LONG_SCALE
Definition: fees.h:147
double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const
Helper for estimateSmartFee.
Definition: fees.cpp:757
FeeEstimateHorizon
Definition: fees.h:27
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const
Calculation of highest target that estimates are tracked for.
Definition: fees.cpp:711
256-bit opaque blob.
Definition: uint256.h:122
static constexpr unsigned int MED_SCALE
Definition: fees.h:144
static const unsigned int OLDEST_ESTIMATE_HISTORY
Historical estimates that are older than this aren&#39;t valid.
Definition: fees.h:149
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:441
void processBlock(unsigned int nBlockHeight, std::vector< const CTxMemPoolEntry *> &entries)
Process all the transactions that have been included in a block.
Definition: fees.cpp:615
bool Read(CAutoFile &filein)
Read estimation data from a file.
Definition: fees.cpp:924
unsigned int historicalBest
Definition: fees.h:234
double leftMempool
Definition: fees.h:68
static constexpr unsigned int LONG_BLOCK_PERIODS
Track confirm delays up to 1008 blocks for long horizon.
Definition: fees.h:146
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
std::vector< double > buckets
Definition: fees.h:254
static constexpr unsigned int SHORT_BLOCK_PERIODS
Track confirm delays up to 12 blocks for short horizon.
Definition: fees.h:140
double totalConfirmed
Definition: fees.h:66
static constexpr double SUFFICIENT_FEETXS
Require an avg of 0.1 tx in the combined feerate bucket per block to have stat significance.
Definition: fees.h:166
bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry *entry)
Process a transaction confirmed in a block.
Definition: fees.cpp:588
Force estimateSmartFee to use non-conservative estimates.
CAmount round(CAmount currentMinFee)
Quantize a minimum fee for privacy purpose before broadcast.
Definition: fees.cpp:1006
static constexpr double SHORT_DECAY
Decay of .962 is a half-life of 18 blocks or about 3 hours.
Definition: fees.h:152
std::set< double > feeset
Definition: fees.h:292
unsigned int untrackedTxs
Definition: fees.h:252
unsigned int scale
Definition: fees.h:77
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:621
void processTransaction(const CTxMemPoolEntry &entry, bool validFeeEstimate)
Process a transaction accepted to the mempool.
Definition: fees.cpp:549
double decay
Definition: fees.h:76