PIVX Core  5.6.99
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 
6 #ifndef PIVX_POLICY_FEES_H
7 #define PIVX_POLICY_FEES_H
8 
9 #include "amount.h"
10 #include "feerate.h"
11 #include "uint256.h"
12 
13 #include <map>
14 #include <string>
15 #include <vector>
16 
17 class CAutoFile;
18 class CFeeRate;
19 class CTxMemPoolEntry;
20 class CTxMemPool;
21 
72 {
73 private:
74  //Define the buckets we will group transactions into
75  std::vector<double> buckets; // The upper-bound of the range for the bucket (inclusive)
76  std::map<double, unsigned int> bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
77 
78  // feerate each bucket X:
79  // Count the total # of txs in each bucket
80  // Track the historical moving average of this total over blocks
81  std::vector<double> txCtAvg;
82  // and calculate the total for the current block to update the moving average
83  std::vector<int> curBlockTxCt;
84 
85  // Count the total # of txs confirmed within Y blocks in each bucket
86  // Track the historical moving average of these totals over blocks
87  std::vector<std::vector<double> > confAvg; // confAvg[Y][X]
88  // and calculate the totals for the current block to update the moving averages
89  std::vector<std::vector<int> > curBlockConf; // curBlockConf[Y][X]
90 
91  // Sum the total feerate of all tx's in each bucket
92  // Track the historical moving average of this total over blocks
93  std::vector<double> avg;
94  // and calculate the total for the current block to update the moving average
95  std::vector<double> curBlockVal;
96 
97  // Combine the conf counts with tx counts to calculate the confirmation % for each Y,X
98  // Combine the total value with the tx counts to calculate the avg feerate per bucket
99 
100  double decay{0.0};
101 
102  // Mempool counts of outstanding transactions
103  // For each bucket X, track the number of transactions in the mempool
104  // that are unconfirmed for each possible confirmation value Y
105  std::vector<std::vector<int> > unconfTxs; //unconfTxs[Y][X]
106  // transactions still unconfirmed after MAX_CONFIRMS for each bucket
107  std::vector<int> oldUnconfTxs;
108 
109 public:
117  void Initialize(std::vector<double>& defaultBuckets, unsigned int maxConfirms, double decay);
118 
120  void ClearCurrent(unsigned int nBlockHeight);
121 
128  void Record(int blocksToConfirm, double val);
129 
131  unsigned int NewTx(unsigned int nBlockHeight, double val);
132 
134  void removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight,
135  unsigned int bucketIndex);
136 
139  void UpdateMovingAverages();
140 
152  double EstimateMedianVal(int confTarget, double sufficientTxVal,
153  double minSuccess, bool requireGreater, unsigned int nBlockHeight);
154 
156  unsigned int GetMaxConfirms() { return confAvg.size(); }
157 
159  void Write(CAutoFile& fileout);
160 
165  void Read(CAutoFile& filein);
166 };
167 
168 
169 
171 static const unsigned int MAX_BLOCK_CONFIRMS = 25;
172 
174 static const double DEFAULT_DECAY = .998;
175 
177 static const double MIN_SUCCESS_PCT = .95;
178 static const double UNLIKELY_PCT = .5;
179 
181 static const double SUFFICIENT_FEETXS = 1;
182 
183 // Minimum and Maximum values for tracking feerates
184 static constexpr double MIN_FEERATE = 10;
185 static const double MAX_FEERATE = 1e7;
186 static const double INF_FEERATE = 1e16;
187 
188 // We have to lump transactions into buckets based on feerate, but we want to be able
189 // to give accurate estimates over a large range of potential fees and priorities
190 // Therefore it makes sense to exponentially space the buckets
192 static const double FEE_SPACING = 1.1;
193 
194 
201 {
202 public:
204  explicit CBlockPolicyEstimator(const CFeeRate& minRelayFee);
205 
207  void processBlock(unsigned int nBlockHeight,
208  std::vector<const CTxMemPoolEntry*>& entries);
209 
211  bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry);
212 
214  void processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate);
215 
217  bool removeTx(const uint256& hash);
218 
220  CFeeRate estimateFee(int confTarget);
221 
226  CFeeRate estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool);
227 
229  void Write(CAutoFile& fileout);
230 
232  void Read(CAutoFile& filein, int nFileVersion);
233 
234 private:
236  unsigned int nBestSeenHeight;
237  struct TxStatsInfo
238  {
239  unsigned int blockHeight;
240  unsigned int bucketIndex;
242  };
243 
244  // map of txids to information about that transaction
245  std::map<uint256, TxStatsInfo> mapMemPoolTxs;
246 
249 
250  unsigned int trackedTxs;
251  unsigned int untrackedTxs;
252 };
253 #endif // PIVX_POLICY_FEES_H
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:452
We want to be able to estimate feerates or priorities that are needed on tx's to be included in a cer...
Definition: fees.h:201
unsigned int untrackedTxs
Definition: fees.h:251
unsigned int nBestSeenHeight
Passed to constructor to avoid dependency on main.
Definition: fees.h:236
TxConfirmStats feeStats
Classes to track historical data on transaction confirmations.
Definition: fees.h:248
bool removeTx(const uint256 &hash)
Remove a transaction from the mempool tracking stats.
Definition: fees.cpp:287
void Write(CAutoFile &fileout)
Write estimation data to a file.
Definition: fees.cpp:457
CFeeRate estimateFee(int confTarget)
Return a feerate estimate.
Definition: fees.cpp:416
CBlockPolicyEstimator(const CFeeRate &minRelayFee)
Create new BlockPolicyEstimator and initialize stats tracking classes with default values.
Definition: fees.cpp:298
unsigned int trackedTxs
Definition: fees.h:250
CFeeRate minTrackedFee
Definition: fees.h:235
void Read(CAutoFile &filein, int nFileVersion)
Read estimation data from a file.
Definition: fees.cpp:463
CFeeRate estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool &pool)
Estimate feerate needed to get be included in a block within confTarget blocks.
Definition: fees.cpp:430
bool processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry *entry)
Process a transaction confirmed in a block.
Definition: fees.cpp:349
std::map< uint256, TxStatsInfo > mapMemPoolTxs
Definition: fees.h:245
void processTransaction(const CTxMemPoolEntry &entry, bool validFeeEstimate)
Process a transaction accepted to the mempool.
Definition: fees.cpp:311
void processBlock(unsigned int nBlockHeight, std::vector< const CTxMemPoolEntry * > &entries)
Process all the transactions that have been included in a block.
Definition: fees.cpp:379
Fee rate in PIV per kilobyte: CAmount / kB.
Definition: feerate.h:20
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:55
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:384
We will instantiate an instance of this class to track transactions that were included in a block.
Definition: fees.h:72
std::vector< double > buckets
Definition: fees.h:75
std::vector< double > curBlockVal
Definition: fees.h:95
std::map< double, unsigned int > bucketMap
Definition: fees.h:76
void Read(CAutoFile &filein)
Read saved state of estimation data from a file and replace all internal data structures and variable...
Definition: fees.cpp:183
void ClearCurrent(unsigned int nBlockHeight)
Clear the state of the curBlock variables to start counting for the new block.
Definition: fees.cpp:39
void removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight, unsigned int bucketIndex)
Remove a transaction from mempool tracking stats.
Definition: fees.cpp:254
double decay
Definition: fees.h:100
void Write(CAutoFile &fileout)
Write state of estimation data to a file.
Definition: fees.cpp:174
void Record(int blocksToConfirm, double val)
Record a new transaction data point in the current block stats.
Definition: fees.cpp:52
std::vector< double > txCtAvg
Definition: fees.h:81
double EstimateMedianVal(int confTarget, double sufficientTxVal, double minSuccess, bool requireGreater, unsigned int nBlockHeight)
Calculate a feerate estimate.
Definition: fees.cpp:76
std::vector< int > oldUnconfTxs
Definition: fees.h:107
std::vector< int > curBlockTxCt
Definition: fees.h:83
std::vector< std::vector< int > > curBlockConf
Definition: fees.h:89
void UpdateMovingAverages()
Update our estimates by decaying our historical moving average and updating with the data gathered fr...
Definition: fees.cpp:65
void Initialize(std::vector< double > &defaultBuckets, unsigned int maxConfirms, double decay)
Initialize the data structures.
Definition: fees.cpp:14
std::vector< double > avg
Definition: fees.h:93
std::vector< std::vector< double > > confAvg
Definition: fees.h:87
unsigned int GetMaxConfirms()
Return the max number of confirms we're tracking.
Definition: fees.h:156
std::vector< std::vector< int > > unconfTxs
Definition: fees.h:105
unsigned int NewTx(unsigned int nBlockHeight, double val)
Record a new transaction entering the mempool.
Definition: fees.cpp:246
256-bit opaque blob.
Definition: uint256.h:138