Skip to content

Commit 3688866

Browse files
committed
Disable fee estimates for a confirm target of 1 block
Backport of bitcoin#9239 without GUI changes and fixing conflicts in tests.
1 parent 29435db commit 3688866

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/policy/fees.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
495495
CFeeRate CBlockPolicyEstimator::estimateFee(int confTarget)
496496
{
497497
// Return failure if trying to analyze a target we're not tracking
498-
if (confTarget <= 0 || (unsigned int)confTarget > feeStats.GetMaxConfirms())
498+
// It's not possible to get reasonable estimates for confTarget of 1
499+
if (confTarget <= 1 || (unsigned int)confTarget > feeStats.GetMaxConfirms())
499500
return CFeeRate(0);
500501

501502
double median = feeStats.EstimateMedianVal(confTarget, SUFFICIENT_FEETXS, MIN_SUCCESS_PCT, true, nBestSeenHeight);
@@ -514,6 +515,10 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, int *answerFoun
514515
if (confTarget <= 0 || (unsigned int)confTarget > feeStats.GetMaxConfirms())
515516
return CFeeRate(0);
516517

518+
// It's not possible to get reasonable estimates for confTarget of 1
519+
if (confTarget == 1)
520+
confTarget = 2;
521+
517522
double median = -1;
518523
while (median < 0 && (unsigned int)confTarget <= feeStats.GetMaxConfirms()) {
519524
median = feeStats.EstimateMedianVal(confTarget++, SUFFICIENT_FEETXS, MIN_SUCCESS_PCT, true, nBestSeenHeight);

src/rpc/mining.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,8 @@ UniValue estimatefee(const UniValue& params, bool fHelp)
791791
"\n"
792792
"A negative value is returned if not enough transactions and blocks\n"
793793
"have been observed to make an estimate.\n"
794+
"-1 is always returned for nblocks == 1 as it is impossible to calculate\n"
795+
"a fee that is high enough to get reliably included in the next block.\n"
794796
"\nExample:\n"
795797
+ HelpExampleCli("estimatefee", "6")
796798
);

src/test/policyestimator_tests.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,26 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
105105
// Highest feerate is 10*baseRate and gets in all blocks,
106106
// second highest feerate is 9*baseRate and gets in 9/10 blocks = 90%,
107107
// third highest feerate is 8*base rate, and gets in 8/10 blocks = 80%,
108-
// so estimateFee(1) should return 10*baseRate.
108+
// so estimateFee(1) would return 10*baseRate but is hardcoded to return failure
109109
// Second highest feerate has 100% chance of being included by 2 blocks,
110110
// so estimateFee(2) should return 9*baseRate etc...
111111
for (int i = 1; i < 10;i++) {
112112
origFeeEst.push_back(mpool.estimateFee(i).GetFeePerK());
113113
origPriEst.push_back(mpool.estimatePriority(i));
114114
if (i > 1) { // Fee estimates should be monotonically decreasing
115-
BOOST_CHECK(origFeeEst[i-1] <= origFeeEst[i-2]);
115+
if (i > 2) {
116+
BOOST_CHECK(origFeeEst[i-1] <= origFeeEst[i-2]);
117+
}
116118
BOOST_CHECK(origPriEst[i-1] <= origPriEst[i-2]);
117119
}
118120
int mult = 11-i;
119-
BOOST_CHECK(origFeeEst[i-1] < mult*baseRate.GetFeePerK() + deltaFee);
120-
BOOST_CHECK(origFeeEst[i-1] > mult*baseRate.GetFeePerK() - deltaFee);
121+
if (i > 1) {
122+
BOOST_CHECK(origFeeEst[i-1] < mult*baseRate.GetFeePerK() + deltaFee);
123+
BOOST_CHECK(origFeeEst[i-1] > mult*baseRate.GetFeePerK() - deltaFee);
124+
}
125+
else {
126+
BOOST_CHECK(origFeeEst[i-1] == CFeeRate(0).GetFeePerK());
127+
}
121128
BOOST_CHECK(origPriEst[i-1] < pow(10,mult) * basepri + deltaPri);
122129
BOOST_CHECK(origPriEst[i-1] > pow(10,mult) * basepri - deltaPri);
123130
}
@@ -127,9 +134,12 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
127134
while (blocknum < 250)
128135
mpool.removeForBlock(block, ++blocknum, dummyConflicted);
129136

137+
BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0));
130138
for (int i = 1; i < 10;i++) {
131-
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] + deltaFee);
132-
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
139+
if (i > 1) {
140+
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] + deltaFee);
141+
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
142+
}
133143
BOOST_CHECK(mpool.estimatePriority(i) < origPriEst[i-1] + deltaPri);
134144
BOOST_CHECK(mpool.estimatePriority(i) > origPriEst[i-1] - deltaPri);
135145
}
@@ -169,8 +179,10 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
169179
}
170180
mpool.removeForBlock(block, 265, dummyConflicted);
171181
block.clear();
182+
BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0));
172183
for (int i = 1; i < 10;i++) {
173-
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
184+
if (i > 1)
185+
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
174186
BOOST_CHECK(mpool.estimatePriority(i) > origPriEst[i-1] - deltaPri);
175187
}
176188

@@ -190,8 +202,10 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
190202
mpool.removeForBlock(block, ++blocknum, dummyConflicted);
191203
block.clear();
192204
}
205+
BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0));
193206
for (int i = 1; i < 10; i++) {
194-
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] - deltaFee);
207+
if (i > 1)
208+
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] - deltaFee);
195209
BOOST_CHECK(mpool.estimatePriority(i) < origPriEst[i-1] - deltaPri);
196210
}
197211

0 commit comments

Comments
 (0)