Skip to content

Commit d3e30bc

Browse files
committed
Refactor to update moving average on fly
1 parent e5007ba commit d3e30bc

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

src/policy/fees.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,16 @@ class TxConfirmStats
3535
// Count the total # of txs in each bucket
3636
// Track the historical moving average of this total over blocks
3737
std::vector<double> txCtAvg;
38-
// and calculate the total for the current block to update the moving average
39-
std::vector<int> curBlockTxCt;
4038

4139
// Count the total # of txs confirmed within Y blocks in each bucket
4240
// Track the historical moving average of theses totals over blocks
4341
std::vector<std::vector<double>> confAvg; // confAvg[Y][X]
44-
// and calculate the totals for the current block to update the moving averages
45-
std::vector<std::vector<int>> curBlockConf; // curBlockConf[Y][X]
4642

4743
std::vector<std::vector<double>> failAvg; // future use
4844

4945
// Sum the total feerate of all tx's in each bucket
5046
// Track the historical moving average of this total over blocks
5147
std::vector<double> avg;
52-
// and calculate the total for the current block to update the moving average
53-
std::vector<double> curBlockVal;
5448

5549
// Combine the conf counts with tx counts to calculate the confirmation % for each Y,X
5650
// Combine the total value with the tx counts to calculate the avg feerate per bucket
@@ -79,7 +73,7 @@ class TxConfirmStats
7973
TxConfirmStats(const std::vector<double>& defaultBuckets, const std::map<double, unsigned int>& defaultBucketMap,
8074
unsigned int maxConfirms, double decay);
8175

82-
/** Clear the state of the curBlock variables to start counting for the new block */
76+
/** Roll the circular buffer for unconfirmed txs*/
8377
void ClearCurrent(unsigned int nBlockHeight);
8478

8579
/**
@@ -148,28 +142,20 @@ TxConfirmStats::TxConfirmStats(const std::vector<double>& defaultBuckets,
148142
}
149143

150144
void TxConfirmStats::resizeInMemoryCounters(size_t newbuckets) {
151-
curBlockConf.resize(GetMaxConfirms());
152145
// newbuckets must be passed in because the buckets referred to during Read have not been updated yet.
153146
unconfTxs.resize(GetMaxConfirms());
154147
for (unsigned int i = 0; i < unconfTxs.size(); i++) {
155-
curBlockConf[i].resize(newbuckets);
156148
unconfTxs[i].resize(newbuckets);
157149
}
158150
oldUnconfTxs.resize(newbuckets);
159-
curBlockTxCt.resize(newbuckets);
160-
curBlockVal.resize(newbuckets);
161151
}
162152

163-
// Zero out the data for the current block
153+
// Roll the unconfirmed txs circular buffer
164154
void TxConfirmStats::ClearCurrent(unsigned int nBlockHeight)
165155
{
166156
for (unsigned int j = 0; j < buckets.size(); j++) {
167157
oldUnconfTxs[j] += unconfTxs[nBlockHeight%unconfTxs.size()][j];
168158
unconfTxs[nBlockHeight%unconfTxs.size()][j] = 0;
169-
for (unsigned int i = 0; i < curBlockConf.size(); i++)
170-
curBlockConf[i][j] = 0;
171-
curBlockTxCt[j] = 0;
172-
curBlockVal[j] = 0;
173159
}
174160
}
175161

@@ -180,20 +166,20 @@ void TxConfirmStats::Record(int blocksToConfirm, double val)
180166
if (blocksToConfirm < 1)
181167
return;
182168
unsigned int bucketindex = bucketMap.lower_bound(val)->second;
183-
for (size_t i = blocksToConfirm; i <= curBlockConf.size(); i++) {
184-
curBlockConf[i - 1][bucketindex]++;
169+
for (size_t i = blocksToConfirm; i <= confAvg.size(); i++) {
170+
confAvg[i - 1][bucketindex]++;
185171
}
186-
curBlockTxCt[bucketindex]++;
187-
curBlockVal[bucketindex] += val;
172+
txCtAvg[bucketindex]++;
173+
avg[bucketindex] += val;
188174
}
189175

190176
void TxConfirmStats::UpdateMovingAverages()
191177
{
192178
for (unsigned int j = 0; j < buckets.size(); j++) {
193179
for (unsigned int i = 0; i < confAvg.size(); i++)
194-
confAvg[i][j] = confAvg[i][j] * decay + curBlockConf[i][j];
195-
avg[j] = avg[j] * decay + curBlockVal[j];
196-
txCtAvg[j] = txCtAvg[j] * decay + curBlockTxCt[j];
180+
confAvg[i][j] = confAvg[i][j] * decay;
181+
avg[j] = avg[j] * decay;
182+
txCtAvg[j] = txCtAvg[j] * decay;
197183
}
198184
}
199185

@@ -521,22 +507,23 @@ void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
521507
// of unconfirmed txs to remove from tracking.
522508
nBestSeenHeight = nBlockHeight;
523509

524-
// Clear the current block state and update unconfirmed circular buffer
510+
// Update unconfirmed circular buffer
525511
feeStats->ClearCurrent(nBlockHeight);
526512
shortStats->ClearCurrent(nBlockHeight);
527513
longStats->ClearCurrent(nBlockHeight);
528514

515+
// Decay all exponential averages
516+
feeStats->UpdateMovingAverages();
517+
shortStats->UpdateMovingAverages();
518+
longStats->UpdateMovingAverages();
519+
529520
unsigned int countedTxs = 0;
530-
// Repopulate the current block states
521+
// Update averages with data points from current block
531522
for (unsigned int i = 0; i < entries.size(); i++) {
532523
if (processBlockTx(nBlockHeight, entries[i]))
533524
countedTxs++;
534525
}
535526

536-
// Update all exponential averages with the current block state
537-
feeStats->UpdateMovingAverages();
538-
shortStats->UpdateMovingAverages();
539-
longStats->UpdateMovingAverages();
540527

541528
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",
542529
countedTxs, entries.size(), trackedTxs, trackedTxs + untrackedTxs, mapMemPoolTxs.size());

0 commit comments

Comments
 (0)