Skip to content

Commit 68af651

Browse files
committed
MOVEONLY: move TxConfirmStats to cpp
1 parent 2332f19 commit 68af651

File tree

2 files changed

+107
-107
lines changed

2 files changed

+107
-107
lines changed

src/policy/fees.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,112 @@
1414
#include "txmempool.h"
1515
#include "util.h"
1616

17+
/**
18+
* We will instantiate an instance of this class to track transactions that were
19+
* included in a block. We will lump transactions into a bucket according to their
20+
* approximate feerate and then track how long it took for those txs to be included in a block
21+
*
22+
* The tracking of unconfirmed (mempool) transactions is completely independent of the
23+
* historical tracking of transactions that have been confirmed in a block.
24+
*/
25+
class TxConfirmStats
26+
{
27+
private:
28+
//Define the buckets we will group transactions into
29+
std::vector<double> buckets; // The upper-bound of the range for the bucket (inclusive)
30+
std::map<double, unsigned int> bucketMap; // Map of bucket upper-bound to index into all vectors by bucket
31+
32+
// For each bucket X:
33+
// Count the total # of txs in each bucket
34+
// Track the historical moving average of this total over blocks
35+
std::vector<double> txCtAvg;
36+
// and calculate the total for the current block to update the moving average
37+
std::vector<int> curBlockTxCt;
38+
39+
// Count the total # of txs confirmed within Y blocks in each bucket
40+
// Track the historical moving average of theses totals over blocks
41+
std::vector<std::vector<double> > confAvg; // confAvg[Y][X]
42+
// and calculate the totals for the current block to update the moving averages
43+
std::vector<std::vector<int> > curBlockConf; // curBlockConf[Y][X]
44+
45+
// Sum the total feerate of all tx's in each bucket
46+
// Track the historical moving average of this total over blocks
47+
std::vector<double> avg;
48+
// and calculate the total for the current block to update the moving average
49+
std::vector<double> curBlockVal;
50+
51+
// Combine the conf counts with tx counts to calculate the confirmation % for each Y,X
52+
// Combine the total value with the tx counts to calculate the avg feerate per bucket
53+
54+
double decay;
55+
56+
// Mempool counts of outstanding transactions
57+
// For each bucket X, track the number of transactions in the mempool
58+
// that are unconfirmed for each possible confirmation value Y
59+
std::vector<std::vector<int> > unconfTxs; //unconfTxs[Y][X]
60+
// transactions still unconfirmed after MAX_CONFIRMS for each bucket
61+
std::vector<int> oldUnconfTxs;
62+
63+
public:
64+
/**
65+
* Create new TxConfirmStats. This is called by BlockPolicyEstimator's
66+
* constructor with default values.
67+
* @param defaultBuckets contains the upper limits for the bucket boundaries
68+
* @param maxConfirms max number of confirms to track
69+
* @param decay how much to decay the historical moving average per block
70+
*/
71+
TxConfirmStats(const std::vector<double>& defaultBuckets, unsigned int maxConfirms, double decay);
72+
73+
/** Clear the state of the curBlock variables to start counting for the new block */
74+
void ClearCurrent(unsigned int nBlockHeight);
75+
76+
/**
77+
* Record a new transaction data point in the current block stats
78+
* @param blocksToConfirm the number of blocks it took this transaction to confirm
79+
* @param val the feerate of the transaction
80+
* @warning blocksToConfirm is 1-based and has to be >= 1
81+
*/
82+
void Record(int blocksToConfirm, double val);
83+
84+
/** Record a new transaction entering the mempool*/
85+
unsigned int NewTx(unsigned int nBlockHeight, double val);
86+
87+
/** Remove a transaction from mempool tracking stats*/
88+
void removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight,
89+
unsigned int bucketIndex);
90+
91+
/** Update our estimates by decaying our historical moving average and updating
92+
with the data gathered from the current block */
93+
void UpdateMovingAverages();
94+
95+
/**
96+
* Calculate a feerate estimate. Find the lowest value bucket (or range of buckets
97+
* to make sure we have enough data points) whose transactions still have sufficient likelihood
98+
* of being confirmed within the target number of confirmations
99+
* @param confTarget target number of confirmations
100+
* @param sufficientTxVal required average number of transactions per block in a bucket range
101+
* @param minSuccess the success probability we require
102+
* @param requireGreater return the lowest feerate such that all higher values pass minSuccess OR
103+
* return the highest feerate such that all lower values fail minSuccess
104+
* @param nBlockHeight the current block height
105+
*/
106+
double EstimateMedianVal(int confTarget, double sufficientTxVal,
107+
double minSuccess, bool requireGreater, unsigned int nBlockHeight) const;
108+
109+
/** Return the max number of confirms we're tracking */
110+
unsigned int GetMaxConfirms() const { return confAvg.size(); }
111+
112+
/** Write state of estimation data to a file*/
113+
void Write(CAutoFile& fileout) const;
114+
115+
/**
116+
* Read saved state of estimation data from a file and replace all internal data structures and
117+
* variables with this state.
118+
*/
119+
void Read(CAutoFile& filein);
120+
};
121+
122+
17123
TxConfirmStats::TxConfirmStats(const std::vector<double>& defaultBuckets,
18124
unsigned int maxConfirms, double _decay)
19125
{

src/policy/fees.h

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CAutoFile;
1818
class CFeeRate;
1919
class CTxMemPoolEntry;
2020
class CTxMemPool;
21+
class TxConfirmStats;
2122

2223
/** \class CBlockPolicyEstimator
2324
* The BlockPolicyEstimator is used for estimating the feerate needed
@@ -60,113 +61,6 @@ class CTxMemPool;
6061
* they've been outstanding.
6162
*/
6263

63-
/**
64-
* We will instantiate an instance of this class to track transactions that were
65-
* included in a block. We will lump transactions into a bucket according to their
66-
* approximate feerate and then track how long it took for those txs to be included in a block
67-
*
68-
* The tracking of unconfirmed (mempool) transactions is completely independent of the
69-
* historical tracking of transactions that have been confirmed in a block.
70-
*/
71-
class TxConfirmStats
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-
// For 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 theses 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;
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:
110-
/**
111-
* Create new TxConfirmStats. This is called by BlockPolicyEstimator's
112-
* constructor with default values.
113-
* @param defaultBuckets contains the upper limits for the bucket boundaries
114-
* @param maxConfirms max number of confirms to track
115-
* @param decay how much to decay the historical moving average per block
116-
*/
117-
TxConfirmStats(const std::vector<double>& defaultBuckets, unsigned int maxConfirms, double decay);
118-
119-
/** Clear the state of the curBlock variables to start counting for the new block */
120-
void ClearCurrent(unsigned int nBlockHeight);
121-
122-
/**
123-
* Record a new transaction data point in the current block stats
124-
* @param blocksToConfirm the number of blocks it took this transaction to confirm
125-
* @param val the feerate of the transaction
126-
* @warning blocksToConfirm is 1-based and has to be >= 1
127-
*/
128-
void Record(int blocksToConfirm, double val);
129-
130-
/** Record a new transaction entering the mempool*/
131-
unsigned int NewTx(unsigned int nBlockHeight, double val);
132-
133-
/** Remove a transaction from mempool tracking stats*/
134-
void removeTx(unsigned int entryHeight, unsigned int nBestSeenHeight,
135-
unsigned int bucketIndex);
136-
137-
/** Update our estimates by decaying our historical moving average and updating
138-
with the data gathered from the current block */
139-
void UpdateMovingAverages();
140-
141-
/**
142-
* Calculate a feerate estimate. Find the lowest value bucket (or range of buckets
143-
* to make sure we have enough data points) whose transactions still have sufficient likelihood
144-
* of being confirmed within the target number of confirmations
145-
* @param confTarget target number of confirmations
146-
* @param sufficientTxVal required average number of transactions per block in a bucket range
147-
* @param minSuccess the success probability we require
148-
* @param requireGreater return the lowest feerate such that all higher values pass minSuccess OR
149-
* return the highest feerate such that all lower values fail minSuccess
150-
* @param nBlockHeight the current block height
151-
*/
152-
double EstimateMedianVal(int confTarget, double sufficientTxVal,
153-
double minSuccess, bool requireGreater, unsigned int nBlockHeight) const;
154-
155-
/** Return the max number of confirms we're tracking */
156-
unsigned int GetMaxConfirms() const { return confAvg.size(); }
157-
158-
/** Write state of estimation data to a file*/
159-
void Write(CAutoFile& fileout) const;
160-
161-
/**
162-
* Read saved state of estimation data from a file and replace all internal data structures and
163-
* variables with this state.
164-
*/
165-
void Read(CAutoFile& filein);
166-
};
167-
168-
169-
17064
/** Track confirm delays up to 25 blocks, can't estimate beyond that */
17165
static const unsigned int MAX_BLOCK_CONFIRMS = 25;
17266

0 commit comments

Comments
 (0)