Raven Core  3.0.0
P2P Digital Currency
fees.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef RAVEN_POLICYESTIMATOR_H
7 #define RAVEN_POLICYESTIMATOR_H
8 
9 #include "amount.h"
10 #include "feerate.h"
11 #include "uint256.h"
12 #include "random.h"
13 #include "sync.h"
14 
15 #include <map>
16 #include <string>
17 #include <vector>
18 #include <array>
19 
20 class CAutoFile;
21 class CFeeRate;
22 class CTxMemPoolEntry;
23 class CTxMemPool;
24 class TxConfirmStats;
25 
71 /* Identifier for each of the 3 different TxConfirmStats which will track
72  * history over different time horizons. */
77 };
78 
80 
81 /* Enumeration of reason for returned fee estimate */
82 enum class FeeReason {
83  NONE,
89  PAYTXFEE,
90  FALLBACK,
91  REQUIRED,
92  MAXTXFEE,
93 };
94 
95 std::string StringForFeeReason(FeeReason reason);
96 
97 /* Used to determine type of fee estimation requested */
98 enum class FeeEstimateMode {
99  UNSET,
100  ECONOMICAL,
101  CONSERVATIVE,
102 };
103 
104 bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode);
105 
106 /* Used to return detailed information about a feerate bucket */
108 {
109  double start = -1;
110  double end = -1;
111  double withinTarget = 0;
112  double totalConfirmed = 0;
113  double inMempool = 0;
114  double leftMempool = 0;
115 };
116 
117 /* Used to return detailed information about a fee estimate calculation */
119 {
122  double decay = 0;
123  unsigned int scale = 0;
124 };
125 
127 {
130  int desiredTarget = 0;
131  int returnedTarget = 0;
132 };
133 
140 {
141 private:
143  static constexpr unsigned int SHORT_BLOCK_PERIODS = 12;
144  static constexpr unsigned int SHORT_SCALE = 1;
146  static constexpr unsigned int MED_BLOCK_PERIODS = 24;
147  static constexpr unsigned int MED_SCALE = 2;
149  static constexpr unsigned int LONG_BLOCK_PERIODS = 42;
150  static constexpr unsigned int LONG_SCALE = 24;
152  static const unsigned int OLDEST_ESTIMATE_HISTORY = 6 * 1008;
153 
155  static constexpr double SHORT_DECAY = .962;
157  static constexpr double MED_DECAY = .9952;
159  static constexpr double LONG_DECAY = .99931;
160 
162  static constexpr double HALF_SUCCESS_PCT = .6;
164  static constexpr double SUCCESS_PCT = .85;
166  static constexpr double DOUBLE_SUCCESS_PCT = .95;
167 
169  static constexpr double SUFFICIENT_FEETXS = 0.1;
171  static constexpr double SUFFICIENT_TXS_SHORT = 0.5;
172 
180  static constexpr double MIN_BUCKET_FEERATE = 1000;
181  static constexpr double MAX_BUCKET_FEERATE = 1e7;
182 
188  static constexpr double FEE_SPACING = 1.05;
189 
190 public:
194 
196  void processBlock(unsigned int nBlockHeight,
197  std::vector<const CTxMemPoolEntry*>& entries);
198 
200  void processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate);
201 
203  bool removeTx(uint256 hash, bool inBlock);
204 
206  CFeeRate estimateFee(int confTarget) const;
207 
213  CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const;
214 
219  CFeeRate estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult *result = nullptr) const;
220 
222  bool Write(CAutoFile& fileout) const;
223 
225  bool Read(CAutoFile& filein);
226 
228  void FlushUnconfirmed(CTxMemPool& pool);
229 
231  unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;
232 
233 private:
234  unsigned int nBestSeenHeight;
235  unsigned int firstRecordedHeight;
236  unsigned int historicalFirst;
237  unsigned int historicalBest;
238 
239  struct TxStatsInfo
240  {
241  unsigned int blockHeight;
242  unsigned int bucketIndex;
243  TxStatsInfo() : blockHeight(0), bucketIndex(0) {}
244  };
245 
246  // map of txids to information about that transaction
247  std::map<uint256, TxStatsInfo> mapMemPoolTxs;
248 
253 
254  unsigned int trackedTxs;
255  unsigned int untrackedTxs;
256 
257  std::vector<double> buckets; // The upper-bound of the range for the bucket (inclusive)
258  std::map<double, unsigned int> bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
259 
261 
263  bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry);
264 
266  double estimateCombinedFee(unsigned int confTarget, double successThreshold, bool checkShorterHorizon, EstimationResult *result) const;
268  double estimateConservativeFee(unsigned int doubleTarget, EstimationResult *result) const;
270  unsigned int BlockSpan() const;
272  unsigned int HistoricalBlockSpan() const;
274  unsigned int MaxUsableEstimate() const;
275 };
276 
278 {
279 private:
280  static constexpr double MAX_FILTER_FEERATE = 1e7;
285  static constexpr double FEE_FILTER_SPACING = 1.1;
286 
287 public:
289  explicit FeeFilterRounder(const CFeeRate& minIncrementalFee);
290 
292  CAmount round(CAmount currentMinFee);
293 
294 private:
295  std::set<double> feeset;
297 };
298 
299 
300 static const std::array<int, 9> confTargets = { {2, 4, 6, 12, 24, 48, 144, 504, 1008} };
301 int getConfTargetForIndex(int index);
302 int getIndexForConfTarget(int target);
303 
304 #endif /*RAVEN_POLICYESTIMATOR_H */
EstimatorBucket pass
Definition: fees.h:120
bool FeeModeFromString(const std::string &mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: fees.cpp:53
EstimationResult est
Definition: fees.h:128
CCriticalSection cs_feeEstimator
Definition: fees.h:260
unsigned int firstRecordedHeight
Definition: fees.h:235
FeeEstimateMode
Definition: fees.h:98
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:258
int getIndexForConfTarget(int target)
Definition: fees.cpp:1066
unsigned int nBestSeenHeight
Definition: fees.h:234
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon)
Definition: fees.cpp:20
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:68
TxConfirmStats * longStats
Definition: fees.h:252
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
EstimatorBucket fail
Definition: fees.h:121
TxConfirmStats * feeStats
Classes to track historical data on transaction confirmations.
Definition: fees.h:250
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:139
unsigned int historicalFirst
Definition: fees.h:236
Fast randomness source.
Definition: random.h:45
unsigned int trackedTxs
Definition: fees.h:254
FeeReason
Definition: fees.h:82
std::string StringForFeeReason(FeeReason reason)
Definition: fees.cpp:33
std::map< uint256, TxStatsInfo > mapMemPoolTxs
Definition: fees.h:247
FastRandomContext insecure_rand
Definition: fees.h:296
FeeEstimateHorizon
Definition: fees.h:73
256-bit opaque blob.
Definition: uint256.h:123
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:416
unsigned int historicalBest
Definition: fees.h:237
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:20
std::vector< double > buckets
Definition: fees.h:257
Use default settings based on other criteria.
TxConfirmStats * shortStats
Definition: fees.h:251
std::set< double > feeset
Definition: fees.h:295
unsigned int untrackedTxs
Definition: fees.h:255
int getConfTargetForIndex(int index)
Definition: fees.cpp:1056
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:456
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Definition: sync.h:92