Fabcoin Core  0.16.2
P2P Digital Currency
fees.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_POLICYESTIMATOR_H
6 #define FABCOIN_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 <string>
16 #include <vector>
17 
18 class CAutoFile;
19 class CFeeRate;
20 class CTxMemPoolEntry;
21 class CTxMemPool;
22 class TxConfirmStats;
23 
69 /* Identifier for each of the 3 different TxConfirmStats which will track
70  * history over different time horizons. */
75 };
76 
78 
79 /* Enumeration of reason for returned fee estimate */
80 enum class FeeReason {
81  NONE,
87  PAYTXFEE,
88  FALLBACK,
89  REQUIRED,
90  MAXTXFEE,
91 };
92 
93 std::string StringForFeeReason(FeeReason reason);
94 
95 /* Used to determine type of fee estimation requested */
96 enum class FeeEstimateMode {
97  UNSET,
98  ECONOMICAL,
99  CONSERVATIVE,
100 };
101 
102 bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode);
103 
104 /* Used to return detailed information about a feerate bucket */
106 {
107  double start = -1;
108  double end = -1;
109  double withinTarget = 0;
110  double totalConfirmed = 0;
111  double inMempool = 0;
112  double leftMempool = 0;
113 };
114 
115 /* Used to return detailed information about a fee estimate calculation */
117 {
120  double decay = 0;
121  unsigned int scale = 0;
122 };
123 
125 {
128  int desiredTarget = 0;
129  int returnedTarget = 0;
130 };
131 
133 static const unsigned int MAX_BLOCK_CONFIRMS = 25;
134 
136 static const double DEFAULT_DECAY = .998;
137 
139 static const double MIN_SUCCESS_PCT = .95;
140 
142 static const double SUFFICIENT_FEETXS = 1;
143 
144 // Minimum and Maximum values for tracking feerates
145 static constexpr double MIN_FEERATE = 10;
146 static const double MAX_FEERATE = 1e7;
147 static const double INF_FEERATE = MAX_MONEY;
148 
149 static const double INF_PRIORITY = 1e9 * MAX_MONEY;
150 
151 // We have to lump transactions into buckets based on feerate, but we want to be able
152 // to give accurate estimates over a large range of potential feerates
153 // Therefore it makes sense to exponentially space the buckets
155 static const double FEE_SPACING = 1.1;
156 
163 {
164 private:
166  static constexpr unsigned int SHORT_BLOCK_PERIODS = 12;
167  static constexpr unsigned int SHORT_SCALE = 1;
169  static constexpr unsigned int MED_BLOCK_PERIODS = 24;
170  static constexpr unsigned int MED_SCALE = 2;
172  static constexpr unsigned int LONG_BLOCK_PERIODS = 42;
173  static constexpr unsigned int LONG_SCALE = 24;
175  static const unsigned int OLDEST_ESTIMATE_HISTORY = 6 * 1008;
176 
178  static constexpr double SHORT_DECAY = .962;
180  static constexpr double MED_DECAY = .9952;
182  static constexpr double LONG_DECAY = .99931;
183 
185  static constexpr double HALF_SUCCESS_PCT = .6;
187  static constexpr double SUCCESS_PCT = .85;
189  static constexpr double DOUBLE_SUCCESS_PCT = .95;
190 
192  static constexpr double SUFFICIENT_FEETXS = 0.1;
194  static constexpr double SUFFICIENT_TXS_SHORT = 0.5;
195 
203  static constexpr double MIN_BUCKET_FEERATE = 1000;
204  static constexpr double MAX_BUCKET_FEERATE = 1e7;
205 
211  static constexpr double FEE_SPACING = 1.05;
212 
213 public:
216  CBlockPolicyEstimator(const CFeeRate& minRelayFee);
218 
220  void processBlock(unsigned int nBlockHeight,
221  std::vector<const CTxMemPoolEntry*>& entries);
222 
224  bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry);
225 
227  void processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate);
228 
230  bool removeTx(uint256 hash, bool inBlock);
231 
233  CFeeRate estimateFee(int confTarget) const;
234 
240  CFeeRate estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool);
241  CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const;
242 
243 
244 
249  double estimatePriority(int confTarget);
250 
257  double estimateSmartPriority(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool);
258 
263  CFeeRate estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult *result = nullptr) const;
264 
266  bool Write(CAutoFile& fileout) const;
267 
269  bool Read(CAutoFile& filein);
270 
272  void FlushUnconfirmed(CTxMemPool& pool);
273 
275  unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;
276 
277 private:
279  unsigned int nBestSeenHeight;
280  unsigned int firstRecordedHeight;
281  unsigned int historicalFirst;
282  unsigned int historicalBest;
283 
284  struct TxStatsInfo
285  {
286  unsigned int blockHeight;
287  unsigned int bucketIndex;
288  TxStatsInfo() : blockHeight(0), bucketIndex(0) {}
289  };
290 
291  // map of txids to information about that transaction
292  std::map<uint256, TxStatsInfo> mapMemPoolTxs;
293 
298 
299  unsigned int trackedTxs;
300  unsigned int untrackedTxs;
301 
302  std::vector<double> buckets; // The upper-bound of the range for the bucket (inclusive)
303  std::map<double, unsigned int> bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
304 
306 
307 
309  double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const;
311  double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const;
313  unsigned int BlockSpan() const;
315  unsigned int HistoricalBlockSpan() const;
317  unsigned int MaxUsableEstimate() const;
318 };
319 
321 {
322 private:
323  static constexpr double MAX_FILTER_FEERATE = 1e7;
328  static constexpr double FEE_FILTER_SPACING = 1.1;
329 
330 public:
332  FeeFilterRounder(const CFeeRate& minIncrementalFee);
333 
335  CAmount round(CAmount currentMinFee);
336 
337 private:
338  std::set<double> feeset;
340 };
341 
342 #endif /*FABCOIN_POLICYESTIMATOR_H */
EstimatorBucket pass
Definition: fees.h:118
EstimationResult est
Definition: fees.h:126
CCriticalSection cs_feeEstimator
Definition: fees.h:305
unsigned int firstRecordedHeight
Definition: fees.h:280
FeeEstimateMode
Definition: fees.h:96
We will instantiate an instance of this class to track transactions that were included in a block...
Definition: fees.cpp:75
std::map< double, unsigned int > bucketMap
Definition: fees.h:303
unsigned int nBestSeenHeight
Definition: fees.h:279
FeeEstimateHorizon
Definition: fees.h:71
CFeeRate minTrackedFee
Passed to constructor to avoid dependency on main.
Definition: fees.h:278
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:66
TxConfirmStats * longStats
Definition: fees.h:297
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
EstimatorBucket fail
Definition: fees.h:119
TxConfirmStats * feeStats
Classes to track historical data on transaction confirmations.
Definition: fees.h:295
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:162
unsigned int historicalFirst
Definition: fees.h:281
Fast randomness source.
Definition: random.h:44
bool FeeModeFromString(const std::string &mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: fees.cpp:53
unsigned int trackedTxs
Definition: fees.h:299
std::map< uint256, TxStatsInfo > mapMemPoolTxs
Definition: fees.h:292
FastRandomContext insecure_rand
Definition: fees.h:339
256-bit opaque blob.
Definition: uint256.h:132
std::string StringForFeeReason(FeeReason reason)
Definition: fees.cpp:33
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:465
FeeReason
Definition: fees.h:80
unsigned int historicalBest
Definition: fees.h:282
#define round(a, b, c, x, mul)
Fee rate in liu per kilobyte: CAmount / kB.
Definition: feerate.h:20
std::vector< double > buckets
Definition: fees.h:302
Use default settings based on other criteria.
TxConfirmStats * shortStats
Definition: fees.h:296
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon)
Definition: fees.cpp:20
std::set< double > feeset
Definition: fees.h:338
unsigned int untrackedTxs
Definition: fees.h:300
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:455
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Definition: sync.h:91