Skip to content

Commit 14e10aa

Browse files
committed
Call estimate(Smart)Fee directly from CBlockPolicyEstimator
1 parent dbb9e36 commit 14e10aa

File tree

12 files changed

+49
-60
lines changed

12 files changed

+49
-60
lines changed

src/policy/fees.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void TxConfirmStats::UpdateMovingAverages()
7777
// returns -1 on error conditions
7878
double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
7979
double successBreakPoint, bool requireGreater,
80-
unsigned int nBlockHeight)
80+
unsigned int nBlockHeight) const
8181
{
8282
// Counters for a bucket (or range of buckets)
8383
double nConf = 0; // Number of tx's confirmed within the confTarget
@@ -411,7 +411,7 @@ void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
411411
untrackedTxs = 0;
412412
}
413413

414-
CFeeRate CBlockPolicyEstimator::estimateFee(int confTarget)
414+
CFeeRate CBlockPolicyEstimator::estimateFee(int confTarget) const
415415
{
416416
LOCK(cs_feeEstimator);
417417
// Return failure if trying to analyze a target we're not tracking
@@ -427,7 +427,7 @@ CFeeRate CBlockPolicyEstimator::estimateFee(int confTarget)
427427
return CFeeRate(median);
428428
}
429429

430-
CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool)
430+
CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool) const
431431
{
432432
if (answerFoundAtTarget)
433433
*answerFoundAtTarget = confTarget;

src/policy/fees.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ class TxConfirmStats
150150
* @param nBlockHeight the current block height
151151
*/
152152
double EstimateMedianVal(int confTarget, double sufficientTxVal,
153-
double minSuccess, bool requireGreater, unsigned int nBlockHeight);
153+
double minSuccess, bool requireGreater, unsigned int nBlockHeight) const;
154154

155155
/** Return the max number of confirms we're tracking */
156-
unsigned int GetMaxConfirms() { return confAvg.size(); }
156+
unsigned int GetMaxConfirms() const { return confAvg.size(); }
157157

158158
/** Write state of estimation data to a file*/
159159
void Write(CAutoFile& fileout);
@@ -217,13 +217,13 @@ class CBlockPolicyEstimator
217217
bool removeTx(uint256 hash);
218218

219219
/** Return a feerate estimate */
220-
CFeeRate estimateFee(int confTarget);
220+
CFeeRate estimateFee(int confTarget) const;
221221

222222
/** Estimate feerate needed to get be included in a block within
223223
* confTarget blocks. If no answer can be given at confTarget, return an
224224
* estimate at the lowest target where one can be given.
225225
*/
226-
CFeeRate estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool);
226+
CFeeRate estimateSmartFee(int confTarget, int *answerFoundAtTarget, const CTxMemPool& pool) const;
227227

228228
/** Write estimation data to a file */
229229
void Write(CAutoFile& fileout);

src/qt/coincontroldialog.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "wallet/coincontrol.h"
1717
#include "init.h"
18+
#include "policy/fees.h"
1819
#include "policy/policy.h"
1920
#include "validation.h" // For mempool
2021
#include "wallet/wallet.h"
@@ -512,7 +513,7 @@ void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
512513
nBytes -= 34;
513514

514515
// Fee
515-
nPayFee = CWallet::GetMinimumFee(nBytes, nTxConfirmTarget, mempool);
516+
nPayFee = CWallet::GetMinimumFee(nBytes, nTxConfirmTarget, ::mempool, ::feeEstimator);
516517
if (nPayFee > 0 && coinControl->nMinimumTotalFee > nPayFee)
517518
nPayFee = coinControl->nMinimumTotalFee;
518519

@@ -592,7 +593,7 @@ void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
592593
if (payTxFee.GetFeePerK() > 0)
593594
dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), payTxFee.GetFeePerK()) / 1000;
594595
else {
595-
dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), mempool.estimateSmartFee(nTxConfirmTarget).GetFeePerK()) / 1000;
596+
dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), ::feeEstimator.estimateSmartFee(nTxConfirmTarget, NULL, ::mempool).GetFeePerK()) / 1000;
596597
}
597598
QString toolTip4 = tr("Can vary +/- %1 satoshi(s) per input.").arg(dFeeVary);
598599

src/qt/sendcoinsdialog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "validation.h" // mempool and minRelayTxFee
2222
#include "ui_interface.h"
2323
#include "txmempool.h"
24+
#include "policy/fees.h"
2425
#include "wallet/wallet.h"
2526

2627
#include <QFontMetrics>
@@ -660,7 +661,7 @@ void SendCoinsDialog::updateSmartFeeLabel()
660661

661662
int nBlocksToConfirm = ui->sliderSmartFee->maximum() - ui->sliderSmartFee->value() + 2;
662663
int estimateFoundAtBlocks = nBlocksToConfirm;
663-
CFeeRate feeRate = mempool.estimateSmartFee(nBlocksToConfirm, &estimateFoundAtBlocks);
664+
CFeeRate feeRate = ::feeEstimator.estimateSmartFee(nBlocksToConfirm, &estimateFoundAtBlocks, ::mempool);
664665
if (feeRate <= CFeeRate(0)) // not enough data => minfee
665666
{
666667
ui->labelSmartFee->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(),

src/rpc/mining.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "validation.h"
1616
#include "miner.h"
1717
#include "net.h"
18+
#include "policy/fees.h"
1819
#include "pow.h"
1920
#include "rpc/blockchain.h"
2021
#include "rpc/server.h"
@@ -819,7 +820,7 @@ UniValue estimatefee(const JSONRPCRequest& request)
819820
if (nBlocks < 1)
820821
nBlocks = 1;
821822

822-
CFeeRate feeRate = mempool.estimateFee(nBlocks);
823+
CFeeRate feeRate = ::feeEstimator.estimateFee(nBlocks);
823824
if (feeRate == CFeeRate(0))
824825
return -1.0;
825826

@@ -857,7 +858,7 @@ UniValue estimatesmartfee(const JSONRPCRequest& request)
857858

858859
UniValue result(UniValue::VOBJ);
859860
int answerFound;
860-
CFeeRate feeRate = mempool.estimateSmartFee(nBlocks, &answerFound);
861+
CFeeRate feeRate = ::feeEstimator.estimateSmartFee(nBlocks, &answerFound, ::mempool);
861862
result.push_back(Pair("feerate", feeRate == CFeeRate(0) ? -1.0 : ValueFromAmount(feeRate.GetFeePerK())));
862863
result.push_back(Pair("blocks", answerFound));
863864
return result;

src/test/policyestimator_tests.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
7979
// At this point we should need to combine 5 buckets to get enough data points
8080
// So estimateFee(1,2,3) should fail and estimateFee(4) should return somewhere around
8181
// 8*baserate. estimateFee(4) %'s are 100,100,100,100,90 = average 98%
82-
BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0));
83-
BOOST_CHECK(mpool.estimateFee(2) == CFeeRate(0));
84-
BOOST_CHECK(mpool.estimateFee(3) == CFeeRate(0));
85-
BOOST_CHECK(mpool.estimateFee(4).GetFeePerK() < 8*baseRate.GetFeePerK() + deltaFee);
86-
BOOST_CHECK(mpool.estimateFee(4).GetFeePerK() > 8*baseRate.GetFeePerK() - deltaFee);
82+
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
83+
BOOST_CHECK(feeEst.estimateFee(2) == CFeeRate(0));
84+
BOOST_CHECK(feeEst.estimateFee(3) == CFeeRate(0));
85+
BOOST_CHECK(feeEst.estimateFee(4).GetFeePerK() < 8*baseRate.GetFeePerK() + deltaFee);
86+
BOOST_CHECK(feeEst.estimateFee(4).GetFeePerK() > 8*baseRate.GetFeePerK() - deltaFee);
8787
int answerFound;
88-
BOOST_CHECK(mpool.estimateSmartFee(1, &answerFound) == mpool.estimateFee(4) && answerFound == 4);
89-
BOOST_CHECK(mpool.estimateSmartFee(3, &answerFound) == mpool.estimateFee(4) && answerFound == 4);
90-
BOOST_CHECK(mpool.estimateSmartFee(4, &answerFound) == mpool.estimateFee(4) && answerFound == 4);
91-
BOOST_CHECK(mpool.estimateSmartFee(8, &answerFound) == mpool.estimateFee(8) && answerFound == 8);
88+
BOOST_CHECK(feeEst.estimateSmartFee(1, &answerFound, mpool) == feeEst.estimateFee(4) && answerFound == 4);
89+
BOOST_CHECK(feeEst.estimateSmartFee(3, &answerFound, mpool) == feeEst.estimateFee(4) && answerFound == 4);
90+
BOOST_CHECK(feeEst.estimateSmartFee(4, &answerFound, mpool) == feeEst.estimateFee(4) && answerFound == 4);
91+
BOOST_CHECK(feeEst.estimateSmartFee(8, &answerFound, mpool) == feeEst.estimateFee(8) && answerFound == 8);
9292
}
9393
}
9494

@@ -100,7 +100,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
100100
// Second highest feerate has 100% chance of being included by 2 blocks,
101101
// so estimateFee(2) should return 9*baseRate etc...
102102
for (int i = 1; i < 10;i++) {
103-
origFeeEst.push_back(mpool.estimateFee(i).GetFeePerK());
103+
origFeeEst.push_back(feeEst.estimateFee(i).GetFeePerK());
104104
if (i > 2) { // Fee estimates should be monotonically decreasing
105105
BOOST_CHECK(origFeeEst[i-1] <= origFeeEst[i-2]);
106106
}
@@ -119,10 +119,10 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
119119
while (blocknum < 250)
120120
mpool.removeForBlock(block, ++blocknum);
121121

122-
BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0));
122+
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
123123
for (int i = 2; i < 10;i++) {
124-
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] + deltaFee);
125-
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
124+
BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() < origFeeEst[i-1] + deltaFee);
125+
BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
126126
}
127127

128128

@@ -142,8 +142,8 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
142142

143143
int answerFound;
144144
for (int i = 1; i < 10;i++) {
145-
BOOST_CHECK(mpool.estimateFee(i) == CFeeRate(0) || mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
146-
BOOST_CHECK(mpool.estimateSmartFee(i, &answerFound).GetFeePerK() > origFeeEst[answerFound-1] - deltaFee);
145+
BOOST_CHECK(feeEst.estimateFee(i) == CFeeRate(0) || feeEst.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
146+
BOOST_CHECK(feeEst.estimateSmartFee(i, &answerFound, mpool).GetFeePerK() > origFeeEst[answerFound-1] - deltaFee);
147147
}
148148

149149
// Mine all those transactions
@@ -158,9 +158,9 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
158158
}
159159
mpool.removeForBlock(block, 265);
160160
block.clear();
161-
BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0));
161+
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
162162
for (int i = 2; i < 10;i++) {
163-
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
163+
BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
164164
}
165165

166166
// Mine 200 more blocks where everything is mined every block
@@ -180,9 +180,9 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
180180
mpool.removeForBlock(block, ++blocknum);
181181
block.clear();
182182
}
183-
BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0));
183+
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
184184
for (int i = 2; i < 10; i++) {
185-
BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] - deltaFee);
185+
BOOST_CHECK(feeEst.estimateFee(i).GetFeePerK() < origFeeEst[i-1] - deltaFee);
186186
}
187187

188188
// Test that if the mempool is limited, estimateSmartFee won't return a value below the mempool min fee
@@ -191,8 +191,8 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
191191
mpool.TrimToSize(1);
192192
BOOST_CHECK(mpool.GetMinFee(1).GetFeePerK() > feeV[5]);
193193
for (int i = 1; i < 10; i++) {
194-
BOOST_CHECK(mpool.estimateSmartFee(i).GetFeePerK() >= mpool.estimateFee(i).GetFeePerK());
195-
BOOST_CHECK(mpool.estimateSmartFee(i).GetFeePerK() >= mpool.GetMinFee(1).GetFeePerK());
194+
BOOST_CHECK(feeEst.estimateSmartFee(i, NULL, mpool).GetFeePerK() >= feeEst.estimateFee(i).GetFeePerK());
195+
BOOST_CHECK(feeEst.estimateSmartFee(i, NULL, mpool).GetFeePerK() >= mpool.GetMinFee(1).GetFeePerK());
196196
}
197197
}
198198

src/txmempool.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -843,15 +843,6 @@ TxMempoolInfo CTxMemPool::info(const uint256& hash) const
843843
return GetInfo(i);
844844
}
845845

846-
CFeeRate CTxMemPool::estimateFee(int nBlocks) const
847-
{
848-
return minerPolicyEstimator->estimateFee(nBlocks);
849-
}
850-
CFeeRate CTxMemPool::estimateSmartFee(int nBlocks, int *answerFoundAtBlocks) const
851-
{
852-
return minerPolicyEstimator->estimateSmartFee(nBlocks, answerFoundAtBlocks, *this);
853-
}
854-
855846
bool
856847
CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
857848
{

src/txmempool.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,6 @@ class CTxMemPool
617617
TxMempoolInfo info(const uint256& hash) const;
618618
std::vector<TxMempoolInfo> infoAll() const;
619619

620-
/** Estimate fee rate needed to get into the next nBlocks
621-
* If no answer can be given at nBlocks, return an estimate
622-
* at the lowest number of blocks where one can be given
623-
*/
624-
CFeeRate estimateSmartFee(int nBlocks, int *answerFoundAtBlocks = NULL) const;
625-
626-
/** Estimate fee rate needed to get into the next nBlocks */
627-
CFeeRate estimateFee(int nBlocks) const;
628-
629620
/** Write/Read estimates to disk */
630621
bool WriteFeeEstimates(CAutoFile& fileout) const;
631622
bool ReadFeeEstimates(CAutoFile& filein);

src/wallet/feebumper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "consensus/validation.h"
66
#include "wallet/feebumper.h"
77
#include "wallet/wallet.h"
8+
#include "policy/fees.h"
89
#include "policy/policy.h"
910
#include "policy/rbf.h"
1011
#include "validation.h" //for mempool access
@@ -159,11 +160,11 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, int newConf
159160
} else {
160161
// if user specified a confirm target then don't consider any global payTxFee
161162
if (specifiedConfirmTarget) {
162-
nNewFee = CWallet::GetMinimumFee(maxNewTxSize, newConfirmTarget, mempool, CAmount(0));
163+
nNewFee = CWallet::GetMinimumFee(maxNewTxSize, newConfirmTarget, mempool, ::feeEstimator, CAmount(0));
163164
}
164165
// otherwise use the regular wallet logic to select payTxFee or default confirm target
165166
else {
166-
nNewFee = CWallet::GetMinimumFee(maxNewTxSize, newConfirmTarget, mempool);
167+
nNewFee = CWallet::GetMinimumFee(maxNewTxSize, newConfirmTarget, mempool, ::feeEstimator);
167168
}
168169

169170
nNewFeeRate = CFeeRate(nNewFee, maxNewTxSize);

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "init.h"
1212
#include "validation.h"
1313
#include "net.h"
14+
#include "policy/fees.h"
1415
#include "policy/policy.h"
1516
#include "policy/rbf.h"
1617
#include "rpc/server.h"

0 commit comments

Comments
 (0)