Skip to content

Commit 5b8cb35

Browse files
committed
policy/fee: remove requireGreater parameter in EstimateMedianVal()
It was always passed as true, and complicates the (already complex) logic of the function. Signed-off-by: Antoine Poinsot <[email protected]>
1 parent dba8196 commit 5b8cb35

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

src/policy/fees.cpp

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,10 @@ class TxConfirmStats
114114
* @param confTarget target number of confirmations
115115
* @param sufficientTxVal required average number of transactions per block in a bucket range
116116
* @param minSuccess the success probability we require
117-
* @param requireGreater return the lowest feerate such that all higher values pass minSuccess OR
118-
* return the highest feerate such that all lower values fail minSuccess
119117
* @param nBlockHeight the current block height
120118
*/
121119
double EstimateMedianVal(int confTarget, double sufficientTxVal,
122-
double minSuccess, bool requireGreater, unsigned int nBlockHeight,
120+
double minSuccess, unsigned int nBlockHeight,
123121
EstimationResult *result = nullptr) const;
124122

125123
/** Return the max number of confirms we're tracking */
@@ -206,34 +204,26 @@ void TxConfirmStats::UpdateMovingAverages()
206204

207205
// returns -1 on error conditions
208206
double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
209-
double successBreakPoint, bool requireGreater,
210-
unsigned int nBlockHeight, EstimationResult *result) const
207+
double successBreakPoint, unsigned int nBlockHeight,
208+
EstimationResult *result) const
211209
{
212210
// Counters for a bucket (or range of buckets)
213211
double nConf = 0; // Number of tx's confirmed within the confTarget
214212
double totalNum = 0; // Total number of tx's that were ever confirmed
215213
int extraNum = 0; // Number of tx's still in mempool for confTarget or longer
216214
double failNum = 0; // Number of tx's that were never confirmed but removed from the mempool after confTarget
217215
int periodTarget = (confTarget + scale - 1)/scale;
218-
219216
int maxbucketindex = buckets.size() - 1;
220217

221-
// requireGreater means we are looking for the lowest feerate such that all higher
222-
// values pass, so we start at maxbucketindex (highest feerate) and look at successively
223-
// smaller buckets until we reach failure. Otherwise, we are looking for the highest
224-
// feerate such that all lower values fail, and we go in the opposite direction.
225-
unsigned int startbucket = requireGreater ? maxbucketindex : 0;
226-
int step = requireGreater ? -1 : 1;
227-
228218
// We'll combine buckets until we have enough samples.
229219
// The near and far variables will define the range we've combined
230220
// The best variables are the last range we saw which still had a high
231221
// enough confirmation rate to count as success.
232222
// The cur variables are the current range we're counting.
233-
unsigned int curNearBucket = startbucket;
234-
unsigned int bestNearBucket = startbucket;
235-
unsigned int curFarBucket = startbucket;
236-
unsigned int bestFarBucket = startbucket;
223+
unsigned int curNearBucket = maxbucketindex;
224+
unsigned int bestNearBucket = maxbucketindex;
225+
unsigned int curFarBucket = maxbucketindex;
226+
unsigned int bestFarBucket = maxbucketindex;
237227

238228
bool foundAnswer = false;
239229
unsigned int bins = unconfTxs.size();
@@ -242,8 +232,8 @@ double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
242232
EstimatorBucket passBucket;
243233
EstimatorBucket failBucket;
244234

245-
// Start counting from highest(default) or lowest feerate transactions
246-
for (int bucket = startbucket; bucket >= 0 && bucket <= maxbucketindex; bucket += step) {
235+
// Start counting from highest feerate transactions
236+
for (int bucket = maxbucketindex; bucket >= 0; --bucket) {
247237
if (newBucketRange) {
248238
curNearBucket = bucket;
249239
newBucketRange = false;
@@ -263,7 +253,7 @@ double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
263253
double curPct = nConf / (totalNum + failNum + extraNum);
264254

265255
// Check to see if we are no longer getting confirmed at the success rate
266-
if ((requireGreater && curPct < successBreakPoint) || (!requireGreater && curPct > successBreakPoint)) {
256+
if (curPct < successBreakPoint) {
267257
if (passing == true) {
268258
// First time we hit a failure record the failed bucket
269259
unsigned int failMinBucket = std::min(curNearBucket, curFarBucket);
@@ -338,8 +328,8 @@ double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
338328
failBucket.leftMempool = failNum;
339329
}
340330

341-
LogPrint(BCLog::ESTIMATEFEE, "FeeEst: %d %s%.0f%% decay %.5f: feerate: %g from (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out) Fail: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out)\n",
342-
confTarget, requireGreater ? ">" : "<", 100.0 * successBreakPoint, decay,
331+
LogPrint(BCLog::ESTIMATEFEE, "FeeEst: %d > %.0f%% decay %.5f: feerate: %g from (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out) Fail: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out)\n",
332+
confTarget, 100.0 * successBreakPoint, decay,
343333
median, passBucket.start, passBucket.end,
344334
100 * passBucket.withinTarget / (passBucket.totalConfirmed + passBucket.inMempool + passBucket.leftMempool),
345335
passBucket.withinTarget, passBucket.totalConfirmed, passBucket.inMempool, passBucket.leftMempool,
@@ -664,7 +654,7 @@ CFeeRate CBlockPolicyEstimator::estimateRawFee(int confTarget, double successThr
664654
if (successThreshold > 1)
665655
return CFeeRate(0);
666656

667-
double median = stats->EstimateMedianVal(confTarget, sufficientTxs, successThreshold, true, nBestSeenHeight, result);
657+
double median = stats->EstimateMedianVal(confTarget, sufficientTxs, successThreshold, nBestSeenHeight, result);
668658

669659
if (median < 0)
670660
return CFeeRate(0);
@@ -725,26 +715,26 @@ double CBlockPolicyEstimator::estimateCombinedFee(unsigned int confTarget, doubl
725715
if (confTarget >= 1 && confTarget <= longStats->GetMaxConfirms()) {
726716
// Find estimate from shortest time horizon possible
727717
if (confTarget <= shortStats->GetMaxConfirms()) { // short horizon
728-
estimate = shortStats->EstimateMedianVal(confTarget, SUFFICIENT_TXS_SHORT, successThreshold, true, nBestSeenHeight, result);
718+
estimate = shortStats->EstimateMedianVal(confTarget, SUFFICIENT_TXS_SHORT, successThreshold, nBestSeenHeight, result);
729719
}
730720
else if (confTarget <= feeStats->GetMaxConfirms()) { // medium horizon
731-
estimate = feeStats->EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, successThreshold, true, nBestSeenHeight, result);
721+
estimate = feeStats->EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, successThreshold, nBestSeenHeight, result);
732722
}
733723
else { // long horizon
734-
estimate = longStats->EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, successThreshold, true, nBestSeenHeight, result);
724+
estimate = longStats->EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, successThreshold, nBestSeenHeight, result);
735725
}
736726
if (checkShorterHorizon) {
737727
EstimationResult tempResult;
738728
// If a lower confTarget from a more recent horizon returns a lower answer use it.
739729
if (confTarget > feeStats->GetMaxConfirms()) {
740-
double medMax = feeStats->EstimateMedianVal(feeStats->GetMaxConfirms(), SUFFICIENT_FEETXS, successThreshold, true, nBestSeenHeight, &tempResult);
730+
double medMax = feeStats->EstimateMedianVal(feeStats->GetMaxConfirms(), SUFFICIENT_FEETXS, successThreshold, nBestSeenHeight, &tempResult);
741731
if (medMax > 0 && (estimate == -1 || medMax < estimate)) {
742732
estimate = medMax;
743733
if (result) *result = tempResult;
744734
}
745735
}
746736
if (confTarget > shortStats->GetMaxConfirms()) {
747-
double shortMax = shortStats->EstimateMedianVal(shortStats->GetMaxConfirms(), SUFFICIENT_TXS_SHORT, successThreshold, true, nBestSeenHeight, &tempResult);
737+
double shortMax = shortStats->EstimateMedianVal(shortStats->GetMaxConfirms(), SUFFICIENT_TXS_SHORT, successThreshold, nBestSeenHeight, &tempResult);
748738
if (shortMax > 0 && (estimate == -1 || shortMax < estimate)) {
749739
estimate = shortMax;
750740
if (result) *result = tempResult;
@@ -763,10 +753,10 @@ double CBlockPolicyEstimator::estimateConservativeFee(unsigned int doubleTarget,
763753
double estimate = -1;
764754
EstimationResult tempResult;
765755
if (doubleTarget <= shortStats->GetMaxConfirms()) {
766-
estimate = feeStats->EstimateMedianVal(doubleTarget, SUFFICIENT_FEETXS, DOUBLE_SUCCESS_PCT, true, nBestSeenHeight, result);
756+
estimate = feeStats->EstimateMedianVal(doubleTarget, SUFFICIENT_FEETXS, DOUBLE_SUCCESS_PCT, nBestSeenHeight, result);
767757
}
768758
if (doubleTarget <= feeStats->GetMaxConfirms()) {
769-
double longEstimate = longStats->EstimateMedianVal(doubleTarget, SUFFICIENT_FEETXS, DOUBLE_SUCCESS_PCT, true, nBestSeenHeight, &tempResult);
759+
double longEstimate = longStats->EstimateMedianVal(doubleTarget, SUFFICIENT_FEETXS, DOUBLE_SUCCESS_PCT, nBestSeenHeight, &tempResult);
770760
if (longEstimate > estimate) {
771761
estimate = longEstimate;
772762
if (result) *result = tempResult;

0 commit comments

Comments
 (0)