Dash Core  0.12.2.1
P2P Digital Currency
fees.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin 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 "uint256.h"
10 
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 class CAutoFile;
16 class CFeeRate;
17 class CTxMemPoolEntry;
18 class CTxMemPool;
19 
80 {
81 private:
82  //Define the buckets we will group transactions into (both fee buckets and priority buckets)
83  std::vector<double> buckets; // The upper-bound of the range for the bucket (inclusive)
84  std::map<double, unsigned int> bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
85 
86  // For each bucket X:
87  // Count the total # of txs in each bucket
88  // Track the historical moving average of this total over blocks
89  std::vector<double> txCtAvg;
90  // and calculate the total for the current block to update the moving average
91  std::vector<int> curBlockTxCt;
92 
93  // Count the total # of txs confirmed within Y blocks in each bucket
94  // Track the historical moving average of theses totals over blocks
95  std::vector<std::vector<double> > confAvg; // confAvg[Y][X]
96  // and calculate the totals for the current block to update the moving averages
97  std::vector<std::vector<int> > curBlockConf; // curBlockConf[Y][X]
98 
99  // Sum the total priority/fee of all tx's in each bucket
100  // Track the historical moving average of this total over blocks
101  std::vector<double> avg;
102  // and calculate the total for the current block to update the moving average
103  std::vector<double> curBlockVal;
104 
105  // Combine the conf counts with tx counts to calculate the confirmation % for each Y,X
106  // Combine the total value with the tx counts to calculate the avg fee/priority per bucket
107 
108  std::string dataTypeString;
109  double decay;
110 
111  // Mempool counts of outstanding transactions
112  // For each bucket X, track the number of transactions in the mempool
113  // that are unconfirmed for each possible confirmation value Y
114  std::vector<std::vector<int> > unconfTxs; //unconfTxs[Y][X]
115  // transactions still unconfirmed after MAX_CONFIRMS for each bucket
116  std::vector<int> oldUnconfTxs;
117 
118 public:
127  void Initialize(std::vector<double>& defaultBuckets, unsigned int maxConfirms, double decay, std::string dataTypeString);
128 
130  void ClearCurrent(unsigned int nBlockHeight);
131 
138  void Record(int blocksToConfirm, double val);
139 
141  unsigned int NewTx(unsigned int nBlockHeight, double val);
142 
144  void removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight,
145  unsigned int bucketIndex);
146 
149  void UpdateMovingAverages();
150 
162  double EstimateMedianVal(int confTarget, double sufficientTxVal,
163  double minSuccess, bool requireGreater, unsigned int nBlockHeight);
164 
166  unsigned int GetMaxConfirms() { return confAvg.size(); }
167 
169  void Write(CAutoFile& fileout);
170 
175  void Read(CAutoFile& filein);
176 };
177 
178 
179 
181 static const unsigned int MAX_BLOCK_CONFIRMS = 25;
182 
184 static const double DEFAULT_DECAY = .998;
185 
187 static const double MIN_SUCCESS_PCT = .95;
188 static const double UNLIKELY_PCT = .5;
189 
191 static const double SUFFICIENT_FEETXS = 1;
192 
194 static const double SUFFICIENT_PRITXS = .2;
195 
196 // Minimum and Maximum values for tracking fees and priorities
197 static const double MIN_FEERATE = 10;
198 static const double MAX_FEERATE = 1e7;
199 static const double INF_FEERATE = MAX_MONEY;
200 static const double MIN_PRIORITY = 10;
201 static const double MAX_PRIORITY = 1e16;
202 static const double INF_PRIORITY = 1e9 * MAX_MONEY;
203 
204 // We have to lump transactions into buckets based on fee or priority, but we want to be able
205 // to give accurate estimates over a large range of potential fees and priorities
206 // Therefore it makes sense to exponentially space the buckets
208 static const double FEE_SPACING = 1.1;
209 
211 static const double PRI_SPACING = 2;
212 
219 {
220 public:
222  CBlockPolicyEstimator(const CFeeRate& minRelayFee);
223 
225  void processBlock(unsigned int nBlockHeight,
226  std::vector<CTxMemPoolEntry>& entries, bool fCurrentEstimate);
227 
229  void processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry& entry);
230 
232  void processTransaction(const CTxMemPoolEntry& entry, bool fCurrentEstimate);
233 
235  void removeTx(uint256 hash);
236 
238  bool isFeeDataPoint(const CFeeRate &fee, double pri);
239 
241  bool isPriDataPoint(const CFeeRate &fee, double pri);
242 
244  CFeeRate estimateFee(int confTarget);
245 
250  CFeeRate estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool);
251 
253  double estimatePriority(int confTarget);
254 
259  double estimateSmartPriority(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool);
260 
262  void Write(CAutoFile& fileout);
263 
265  void Read(CAutoFile& filein);
266 
267 private:
270  unsigned int nBestSeenHeight;
271  struct TxStatsInfo
272  {
274  unsigned int blockHeight;
275  unsigned int bucketIndex;
277  };
278 
279  // map of txids to information about that transaction
280  std::map<uint256, TxStatsInfo> mapMemPoolTxs;
281 
284 
288 };
289 #endif /*BITCOIN_POLICYESTIMATOR_H */
static const double PRI_SPACING
Definition: fees.h:211
std::vector< double > txCtAvg
Definition: fees.h:89
double priUnlikely
Definition: fees.h:287
double estimateSmartPriority(int confTarget, int *answerFoundAtTarget, const CTxMemPool &pool)
Definition: fees.cpp:542
void processTransaction(const CTxMemPoolEntry &entry, bool fCurrentEstimate)
Definition: fees.cpp:344
std::vector< std::vector< int > > unconfTxs
Definition: fees.h:114
double EstimateMedianVal(int confTarget, double sufficientTxVal, double minSuccess, bool requireGreater, unsigned int nBlockHeight)
Definition: fees.cpp:78
std::vector< int > curBlockTxCt
Definition: fees.h:91
double minTrackedPriority
Passed to constructor to avoid dependency on main.
Definition: fees.h:269
CBlockPolicyEstimator(const CFeeRate &minRelayFee)
Definition: fees.cpp:301
double decay
Definition: fees.h:109
std::string dataTypeString
Definition: fees.h:108
static const CAmount MAX_MONEY
Definition: amount.h:30
static FILE * fileout
Definition: util.cpp:210
TxConfirmStats * stats
Definition: fees.h:273
void Record(int blocksToConfirm, double val)
Definition: fees.cpp:54
void Read(CAutoFile &filein)
Definition: fees.cpp:185
static const double UNLIKELY_PCT
Definition: fees.h:188
void ClearCurrent(unsigned int nBlockHeight)
Definition: fees.cpp:41
static const double MIN_FEERATE
Definition: fees.h:197
void UpdateMovingAverages()
Definition: fees.cpp:67
static const double INF_FEERATE
Definition: fees.h:199
static const double SUFFICIENT_PRITXS
Definition: fees.h:194
CFeeRate estimateFee(int confTarget)
Definition: fees.cpp:492
TxConfirmStats priStats
Definition: fees.h:283
static const double MAX_PRIORITY
Definition: fees.h:201
std::map< double, unsigned int > bucketMap
Definition: fees.h:84
void processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry &entry)
Definition: fees.cpp:397
CFeeRate estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool &pool)
Definition: fees.cpp:506
CFeeRate feeLikely
Definition: fees.h:286
unsigned int GetMaxConfirms()
Definition: fees.h:166
unsigned int NewTx(unsigned int nBlockHeight, double val)
Definition: fees.cpp:248
TxConfirmStats feeStats
Definition: fees.h:283
std::vector< double > avg
Definition: fees.h:101
void Initialize(std::vector< double > &defaultBuckets, unsigned int maxConfirms, double decay, std::string dataTypeString)
Definition: fees.cpp:15
std::vector< double > curBlockVal
Definition: fees.h:103
void removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight, unsigned int bucketIndex)
Definition: fees.cpp:257
static const double SUFFICIENT_FEETXS
Definition: fees.h:191
static const double DEFAULT_DECAY
Definition: fees.h:184
static const double MIN_SUCCESS_PCT
Definition: fees.h:187
CFeeRate feeUnlikely
Definition: fees.h:286
CFeeRate minTrackedFee
Definition: fees.h:268
void removeTx(uint256 hash)
Definition: fees.cpp:285
static const double INF_PRIORITY
Definition: fees.h:202
bool isFeeDataPoint(const CFeeRate &fee, double pri)
Definition: fees.cpp:326
static const double MIN_PRIORITY
Definition: fees.h:200
bool isPriDataPoint(const CFeeRate &fee, double pri)
Definition: fees.cpp:335
static const double MAX_FEERATE
Definition: fees.h:198
unsigned int nBestSeenHeight
Set to AllowFreeThreshold.
Definition: fees.h:270
void Read(CAutoFile &filein)
Definition: fees.cpp:573
void processBlock(unsigned int nBlockHeight, std::vector< CTxMemPoolEntry > &entries, bool fCurrentEstimate)
Definition: fees.cpp:434
void Write(CAutoFile &fileout)
Definition: fees.cpp:176
std::vector< int > oldUnconfTxs
Definition: fees.h:116
std::vector< std::vector< double > > confAvg
Definition: fees.h:95
std::vector< std::vector< int > > curBlockConf
Definition: fees.h:97
std::map< uint256, TxStatsInfo > mapMemPoolTxs
Definition: fees.h:280
void Write(CAutoFile &fileout)
Definition: fees.cpp:566
static const unsigned int MAX_BLOCK_CONFIRMS
Definition: fees.h:181
std::vector< double > buckets
Definition: fees.h:83
double estimatePriority(int confTarget)
Definition: fees.cpp:533
static const double FEE_SPACING
Definition: fees.h:208
double priLikely
Definition: fees.h:287