Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

fees.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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_POLICYESTIMATOR_H
6 #define BITCOIN_POLICYESTIMATOR_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 
70 /* Identifier for each of the 3 different TxConfirmStats which will track
71  * history over different time horizons. */
76 };
77 
79 
80 /* Enumeration of reason for returned fee estimate */
81 enum class FeeReason {
82  NONE,
88  PAYTXFEE,
89  FALLBACK,
90  REQUIRED,
91  MAXTXFEE,
92 };
93 
94 std::string StringForFeeReason(FeeReason reason);
95 
96 /* Used to determine type of fee estimation requested */
97 enum class FeeEstimateMode {
98  UNSET,
99  ECONOMICAL,
100  CONSERVATIVE,
101 };
102 
103 bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode);
104 
105 /* Used to return detailed information about a feerate bucket */
107 {
108  double start = -1;
109  double end = -1;
110  double withinTarget = 0;
111  double totalConfirmed = 0;
112  double inMempool = 0;
113  double leftMempool = 0;
114 };
115 
116 /* Used to return detailed information about a fee estimate calculation */
118 {
121  double decay = 0;
122  unsigned int scale = 0;
123 };
124 
126 {
129  int desiredTarget = 0;
130  int returnedTarget = 0;
131 };
132 
139 {
140 private:
142  static constexpr unsigned int SHORT_BLOCK_PERIODS = 12;
143  static constexpr unsigned int SHORT_SCALE = 1;
145  static constexpr unsigned int MED_BLOCK_PERIODS = 24;
146  static constexpr unsigned int MED_SCALE = 2;
148  static constexpr unsigned int LONG_BLOCK_PERIODS = 42;
149  static constexpr unsigned int LONG_SCALE = 24;
151  static const unsigned int OLDEST_ESTIMATE_HISTORY = 6 * 1008;
152 
154  static constexpr double SHORT_DECAY = .962;
156  static constexpr double MED_DECAY = .9952;
158  static constexpr double LONG_DECAY = .99931;
159 
161  static constexpr double HALF_SUCCESS_PCT = .6;
163  static constexpr double SUCCESS_PCT = .85;
165  static constexpr double DOUBLE_SUCCESS_PCT = .95;
166 
168  static constexpr double SUFFICIENT_FEETXS = 0.1;
170  static constexpr double SUFFICIENT_TXS_SHORT = 0.5;
171 
179  static constexpr double MIN_BUCKET_FEERATE = 1000;
180  static constexpr double MAX_BUCKET_FEERATE = 1e7;
181 
187  static constexpr double FEE_SPACING = 1.05;
188 
189 public:
193 
195  void processBlock(unsigned int nBlockHeight,
196  std::vector<const CTxMemPoolEntry*>& entries);
197 
199  void processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate);
200 
202  bool removeTx(uint256 hash, bool inBlock);
203 
205  CFeeRate estimateFee(int confTarget) const;
206 
212  CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const;
213 
218  CFeeRate estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult *result = nullptr) const;
219 
221  bool Write(CAutoFile& fileout) const;
222 
224  bool Read(CAutoFile& filein);
225 
227  void FlushUnconfirmed(CTxMemPool& pool);
228 
230  unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;
231 
232 private:
233  unsigned int nBestSeenHeight;
234  unsigned int firstRecordedHeight;
235  unsigned int historicalFirst;
236  unsigned int historicalBest;
237 
238  struct TxStatsInfo
239  {
240  unsigned int blockHeight;
241  unsigned int bucketIndex;
243  };
244 
245  // map of txids to information about that transaction
246  std::map<uint256, TxStatsInfo> mapMemPoolTxs;
247 
249  std::unique_ptr<TxConfirmStats> feeStats;
250  std::unique_ptr<TxConfirmStats> shortStats;
251  std::unique_ptr<TxConfirmStats> longStats;
252 
253  unsigned int trackedTxs;
254  unsigned int untrackedTxs;
255 
256  std::vector<double> buckets; // The upper-bound of the range for the bucket (inclusive)
257  std::map<double, unsigned int> bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
258 
260 
262  bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry);
263 
265  double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const;
267  double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const;
269  unsigned int BlockSpan() const;
271  unsigned int HistoricalBlockSpan() const;
273  unsigned int MaxUsableEstimate() const;
274 };
275 
276 #endif /*BITCOIN_POLICYESTIMATOR_H */
static constexpr double MED_DECAY
Decay of .998 is a half-life of 144 blocks or about 6 hours.
Definition: fees.h:156
EstimatorBucket pass
Definition: fees.h:119
bool FeeModeFromString(const std::string &mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: fees.cpp:50
EstimationResult est
Definition: fees.h:127
int returnedTarget
Definition: fees.h:130
CCriticalSection cs_feeEstimator
Definition: fees.h:259
static constexpr double MAX_BUCKET_FEERATE
Definition: fees.h:180
static constexpr double HALF_SUCCESS_PCT
Require greater than 60% of X feerate transactions to be confirmed within Y/2 blocks.
Definition: fees.h:161
unsigned int firstRecordedHeight
Definition: fees.h:234
static constexpr unsigned int MED_BLOCK_PERIODS
Track confirm delays up to 48 blocks for medium horizon.
Definition: fees.h:145
double start
Definition: fees.h:108
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
static FILE * fileout
We use boost::call_once() to make sure mutexDebugLog and vMsgsBeforeOpenLog are initialized in a thre...
Definition: util.cpp:192
bool Write(CAutoFile &fileout) const
Write estimation data to a file.
Definition: fees.cpp:898
FeeEstimateMode
Definition: fees.h:97
FeeReason reason
Definition: fees.h:128
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:257
std::unique_ptr< TxConfirmStats > shortStats
Definition: fees.h:250
std::unique_ptr< TxConfirmStats > longStats
Definition: fees.h:251
static constexpr double DOUBLE_SUCCESS_PCT
Require greater than 95% of X feerate transactions to be confirmed within 2 * Y blocks.
Definition: fees.h:165
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:187
unsigned int nBestSeenHeight
Definition: fees.h:233
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:179
double withinTarget
Definition: fees.h:110
unsigned int MaxUsableEstimate() const
Calculation of highest target that reasonable estimate can be provided for.
Definition: fees.cpp:746
int desiredTarget
Definition: fees.h:129
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:170
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:69
static constexpr double SUCCESS_PCT
Require greater than 85% of X feerate transactions to be confirmed within Y blocks.
Definition: fees.h:163
std::unique_ptr< TxConfirmStats > feeStats
Classes to track historical data on transaction confirmations.
Definition: fees.h:249
static constexpr unsigned int SHORT_SCALE
Definition: fees.h:143
static constexpr double LONG_DECAY
Decay of .9995 is a half-life of 1008 blocks or about 2 days.
Definition: fees.h:158
unsigned int HistoricalBlockSpan() const
Number of blocks of recorded fee estimate data represented in saved data file.
Definition: fees.cpp:736
double end
Definition: fees.h:109
EstimatorBucket fail
Definition: fees.h:120
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:672
We want to be able to estimate feerates that are needed on tx&#39;s to be included in a certain number of...
Definition: fees.h:138
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:819
unsigned int historicalFirst
Definition: fees.h:235
void FlushUnconfirmed(CTxMemPool &pool)
Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool...
Definition: fees.cpp:983
double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const
Helper for estimateSmartFee.
Definition: fees.cpp:795
unsigned int trackedTxs
Definition: fees.h:253
double inMempool
Definition: fees.h:112
unsigned int BlockSpan() const
Number of blocks of data recorded while fee estimates have been running.
Definition: fees.cpp:728
FeeReason
Definition: fees.h:81
std::string StringForFeeReason(FeeReason reason)
Definition: fees.cpp:30
std::map< uint256, TxStatsInfo > mapMemPoolTxs
Definition: fees.h:246
CFeeRate estimateFee(int confTarget) const
DEPRECATED.
Definition: fees.cpp:663
static constexpr unsigned int LONG_SCALE
Definition: fees.h:149
double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const
Helper for estimateSmartFee.
Definition: fees.cpp:756
FeeEstimateHorizon
Definition: fees.h:72
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const
Calculation of highest target that estimates are tracked for.
Definition: fees.cpp:710
256-bit opaque blob.
Definition: uint256.h:123
static constexpr unsigned int MED_SCALE
Definition: fees.h:146
static const unsigned int OLDEST_ESTIMATE_HISTORY
Historical estimates that are older than this aren&#39;t valid.
Definition: fees.h:151
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:442
void processBlock(unsigned int nBlockHeight, std::vector< const CTxMemPoolEntry *> &entries)
Process all the transactions that have been included in a block.
Definition: fees.cpp:614
bool Read(CAutoFile &filein)
Read estimation data from a file.
Definition: fees.cpp:923
unsigned int historicalBest
Definition: fees.h:236
double leftMempool
Definition: fees.h:113
static constexpr unsigned int LONG_BLOCK_PERIODS
Track confirm delays up to 1008 blocks for long horizon.
Definition: fees.h:148
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
std::vector< double > buckets
Definition: fees.h:256
static constexpr unsigned int SHORT_BLOCK_PERIODS
Track confirm delays up to 12 blocks for short horizon.
Definition: fees.h:142
double totalConfirmed
Definition: fees.h:111
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:168
bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry *entry)
Process a transaction confirmed in a block.
Definition: fees.cpp:587
Use default settings based on other criteria.
static constexpr double SHORT_DECAY
Decay of .962 is a half-life of 18 blocks or about 45 minutes.
Definition: fees.h:154
unsigned int untrackedTxs
Definition: fees.h:254
unsigned int scale
Definition: fees.h:122
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:410
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
void processTransaction(const CTxMemPoolEntry &entry, bool validFeeEstimate)
Process a transaction accepted to the mempool.
Definition: fees.cpp:549
double decay
Definition: fees.h:121
Released under the MIT license