Skip to content

Commit 2332f19

Browse files
committed
Initialize TxConfirmStats in constructor
and change to storing as a pointer.
1 parent 5ba81e5 commit 2332f19

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/policy/fees.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#include "txmempool.h"
1515
#include "util.h"
1616

17-
void TxConfirmStats::Initialize(std::vector<double>& defaultBuckets,
18-
unsigned int maxConfirms, double _decay)
17+
TxConfirmStats::TxConfirmStats(const std::vector<double>& defaultBuckets,
18+
unsigned int maxConfirms, double _decay)
1919
{
2020
decay = _decay;
2121
for (unsigned int i = 0; i < defaultBuckets.size(); i++) {
@@ -294,7 +294,7 @@ bool CBlockPolicyEstimator::removeTx(uint256 hash)
294294
LOCK(cs_feeEstimator);
295295
std::map<uint256, TxStatsInfo>::iterator pos = mapMemPoolTxs.find(hash);
296296
if (pos != mapMemPoolTxs.end()) {
297-
feeStats.removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex);
297+
feeStats->removeTx(pos->second.blockHeight, nBestSeenHeight, pos->second.bucketIndex);
298298
mapMemPoolTxs.erase(hash);
299299
return true;
300300
} else {
@@ -312,7 +312,12 @@ CBlockPolicyEstimator::CBlockPolicyEstimator()
312312
vfeelist.push_back(bucketBoundary);
313313
}
314314
vfeelist.push_back(INF_FEERATE);
315-
feeStats.Initialize(vfeelist, MAX_BLOCK_CONFIRMS, DEFAULT_DECAY);
315+
feeStats = new TxConfirmStats(vfeelist, MAX_BLOCK_CONFIRMS, DEFAULT_DECAY);
316+
}
317+
318+
CBlockPolicyEstimator::~CBlockPolicyEstimator()
319+
{
320+
delete feeStats;
316321
}
317322

318323
void CBlockPolicyEstimator::processTransaction(const CTxMemPoolEntry& entry, bool validFeeEstimate)
@@ -346,7 +351,7 @@ void CBlockPolicyEstimator::processTransaction(const CTxMemPoolEntry& entry, boo
346351
CFeeRate feeRate(entry.GetFee(), entry.GetTxSize());
347352

348353
mapMemPoolTxs[hash].blockHeight = txHeight;
349-
mapMemPoolTxs[hash].bucketIndex = feeStats.NewTx(txHeight, (double)feeRate.GetFeePerK());
354+
mapMemPoolTxs[hash].bucketIndex = feeStats->NewTx(txHeight, (double)feeRate.GetFeePerK());
350355
}
351356

352357
bool CBlockPolicyEstimator::processBlockTx(unsigned int nBlockHeight, const CTxMemPoolEntry* entry)
@@ -370,7 +375,7 @@ bool CBlockPolicyEstimator::processBlockTx(unsigned int nBlockHeight, const CTxM
370375
// Feerates are stored and reported as BTC-per-kb:
371376
CFeeRate feeRate(entry->GetFee(), entry->GetTxSize());
372377

373-
feeStats.Record(blocksToConfirm, (double)feeRate.GetFeePerK());
378+
feeStats->Record(blocksToConfirm, (double)feeRate.GetFeePerK());
374379
return true;
375380
}
376381

@@ -393,7 +398,7 @@ void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
393398
nBestSeenHeight = nBlockHeight;
394399

395400
// Clear the current block state and update unconfirmed circular buffer
396-
feeStats.ClearCurrent(nBlockHeight);
401+
feeStats->ClearCurrent(nBlockHeight);
397402

398403
unsigned int countedTxs = 0;
399404
// Repopulate the current block states
@@ -403,7 +408,7 @@ void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
403408
}
404409

405410
// Update all exponential averages with the current block state
406-
feeStats.UpdateMovingAverages();
411+
feeStats->UpdateMovingAverages();
407412

408413
LogPrint(BCLog::ESTIMATEFEE, "Blockpolicy after updating estimates for %u of %u txs in block, since last block %u of %u tracked, new mempool map size %u\n",
409414
countedTxs, entries.size(), trackedTxs, trackedTxs + untrackedTxs, mapMemPoolTxs.size());
@@ -417,10 +422,10 @@ CFeeRate CBlockPolicyEstimator::estimateFee(int confTarget) const
417422
LOCK(cs_feeEstimator);
418423
// Return failure if trying to analyze a target we're not tracking
419424
// It's not possible to get reasonable estimates for confTarget of 1
420-
if (confTarget <= 1 || (unsigned int)confTarget > feeStats.GetMaxConfirms())
425+
if (confTarget <= 1 || (unsigned int)confTarget > feeStats->GetMaxConfirms())
421426
return CFeeRate(0);
422427

423-
double median = feeStats.EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, MIN_SUCCESS_PCT, true, nBestSeenHeight);
428+
double median = feeStats->EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, MIN_SUCCESS_PCT, true, nBestSeenHeight);
424429

425430
if (median < 0)
426431
return CFeeRate(0);
@@ -439,15 +444,15 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, int *answerFoun
439444
LOCK(cs_feeEstimator);
440445

441446
// Return failure if trying to analyze a target we're not tracking
442-
if (confTarget <= 0 || (unsigned int)confTarget > feeStats.GetMaxConfirms())
447+
if (confTarget <= 0 || (unsigned int)confTarget > feeStats->GetMaxConfirms())
443448
return CFeeRate(0);
444449

445450
// It's not possible to get reasonable estimates for confTarget of 1
446451
if (confTarget == 1)
447452
confTarget = 2;
448453

449-
while (median < 0 && (unsigned int)confTarget <= feeStats.GetMaxConfirms()) {
450-
median = feeStats.EstimateMedianVal(confTarget++, SUFFICIENT_FEETXS, MIN_SUCCESS_PCT, true, nBestSeenHeight);
454+
while (median < 0 && (unsigned int)confTarget <= feeStats->GetMaxConfirms()) {
455+
median = feeStats->EstimateMedianVal(confTarget++, SUFFICIENT_FEETXS, MIN_SUCCESS_PCT, true, nBestSeenHeight);
451456
}
452457
} // Must unlock cs_feeEstimator before taking mempool locks
453458

@@ -472,7 +477,7 @@ bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
472477
fileout << 139900; // version required to read: 0.13.99 or later
473478
fileout << CLIENT_VERSION; // version that wrote the file
474479
fileout << nBestSeenHeight;
475-
feeStats.Write(fileout);
480+
feeStats->Write(fileout);
476481
}
477482
catch (const std::exception&) {
478483
LogPrintf("CBlockPolicyEstimator::Write(): unable to read policy estimator data (non-fatal)\n");
@@ -490,7 +495,7 @@ bool CBlockPolicyEstimator::Read(CAutoFile& filein)
490495
if (nVersionRequired > CLIENT_VERSION)
491496
return error("CBlockPolicyEstimator::Read(): up-version (%d) fee estimate file", nVersionRequired);
492497
filein >> nFileBestSeenHeight;
493-
feeStats.Read(filein);
498+
feeStats->Read(filein);
494499
nBestSeenHeight = nFileBestSeenHeight;
495500
// if nVersionThatWrote < 139900 then another TxConfirmStats (for priority) follows but can be ignored.
496501
}

src/policy/fees.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ class TxConfirmStats
108108

109109
public:
110110
/**
111-
* Initialize the data structures. This is called by BlockPolicyEstimator's
111+
* Create new TxConfirmStats. This is called by BlockPolicyEstimator's
112112
* constructor with default values.
113113
* @param defaultBuckets contains the upper limits for the bucket boundaries
114114
* @param maxConfirms max number of confirms to track
115115
* @param decay how much to decay the historical moving average per block
116116
*/
117-
void Initialize(std::vector<double>& defaultBuckets, unsigned int maxConfirms, double decay);
117+
TxConfirmStats(const std::vector<double>& defaultBuckets, unsigned int maxConfirms, double decay);
118118

119119
/** Clear the state of the curBlock variables to start counting for the new block */
120120
void ClearCurrent(unsigned int nBlockHeight);
@@ -205,6 +205,7 @@ class CBlockPolicyEstimator
205205
public:
206206
/** Create new BlockPolicyEstimator and initialize stats tracking classes with default values */
207207
CBlockPolicyEstimator();
208+
~CBlockPolicyEstimator();
208209

209210
/** Process all the transactions that have been included in a block */
210211
void processBlock(unsigned int nBlockHeight,
@@ -245,7 +246,7 @@ class CBlockPolicyEstimator
245246
std::map<uint256, TxStatsInfo> mapMemPoolTxs;
246247

247248
/** Classes to track historical data on transaction confirmations */
248-
TxConfirmStats feeStats;
249+
TxConfirmStats* feeStats;
249250

250251
unsigned int trackedTxs;
251252
unsigned int untrackedTxs;

0 commit comments

Comments
 (0)