Skip to content

Commit cc02c79

Browse files
committed
Remove uses of fee globals in wallet code
This commit does not change behavior.
1 parent 1fb0a4a commit cc02c79

File tree

12 files changed

+54
-36
lines changed

12 files changed

+54
-36
lines changed

src/interfaces/chain.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <chain.h>
88
#include <chainparams.h>
9+
#include <policy/fees.h>
10+
#include <policy/policy.h>
911
#include <policy/rbf.h>
1012
#include <primitives/block.h>
1113
#include <sync.h>
@@ -214,6 +216,18 @@ class ChainImpl : public Chain
214216
return ::mempool.CalculateMemPoolAncestors(entry, ancestors, limit_ancestor_count, limit_ancestor_size,
215217
limit_descendant_count, limit_descendant_size, unused_error_string);
216218
}
219+
CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
220+
{
221+
return ::feeEstimator.estimateSmartFee(num_blocks, calc, conservative);
222+
}
223+
unsigned int estimateMaxBlocks() override
224+
{
225+
return ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
226+
}
227+
CFeeRate mempoolMinFee() override
228+
{
229+
return ::mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
230+
}
217231
};
218232

219233
} // namespace

src/interfaces/chain.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CScheduler;
1919
class CTransaction;
2020
class uint256;
2121
struct CBlockLocator;
22+
struct FeeCalculation;
2223

2324
namespace interfaces {
2425

@@ -145,6 +146,15 @@ class Chain
145146

146147
//! Check chain limits.
147148
virtual bool checkChainLimits(CTransactionRef tx) = 0;
149+
150+
//! Estimate smart fee.
151+
virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc = nullptr) = 0;
152+
153+
//! Fee estimator max target.
154+
virtual unsigned int estimateMaxBlocks() = 0;
155+
156+
//! Pool min fee.
157+
virtual CFeeRate mempoolMinFee() = 0;
148158
};
149159

150160
//! Interface to let node manage chain clients (wallets, or maybe tools for

src/interfaces/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ class WalletImpl : public Wallet
457457
{
458458
FeeCalculation fee_calc;
459459
CAmount result;
460-
result = GetMinimumFee(*m_wallet, tx_bytes, coin_control, ::mempool, ::feeEstimator, &fee_calc);
460+
result = GetMinimumFee(*m_wallet, tx_bytes, coin_control, &fee_calc);
461461
if (returned_target) *returned_target = fee_calc.returnedTarget;
462462
if (reason) *reason = fee_calc.reason;
463463
return result;

src/rpc/mining.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,8 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
843843

844844
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VSTR});
845845
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
846-
unsigned int conf_target = ParseConfirmTarget(request.params[0]);
846+
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
847+
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
847848
bool conservative = true;
848849
if (!request.params[1].isNull()) {
849850
FeeEstimateMode fee_mode;
@@ -915,7 +916,8 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
915916

916917
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM}, true);
917918
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
918-
unsigned int conf_target = ParseConfirmTarget(request.params[0]);
919+
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
920+
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
919921
double threshold = 0.95;
920922
if (!request.params[1].isNull()) {
921923
threshold = request.params[1].get_real();

src/rpc/util.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
#include <key_io.h>
66
#include <keystore.h>
7-
#include <policy/fees.h>
87
#include <rpc/util.h>
98
#include <tinyformat.h>
109
#include <util/strencodings.h>
11-
#include <validation.h>
1210

1311
InitInterfaces* g_rpc_interfaces = nullptr;
1412

@@ -130,10 +128,9 @@ UniValue DescribeAddress(const CTxDestination& dest)
130128
return boost::apply_visitor(DescribeAddressVisitor(), dest);
131129
}
132130

133-
unsigned int ParseConfirmTarget(const UniValue& value)
131+
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
134132
{
135133
int target = value.get_int();
136-
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
137134
if (target < 1 || (unsigned int)target > max_target) {
138135
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u - %u", 1, max_target));
139136
}

src/rpc/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ CScript CreateMultisigRedeemscript(const int required, const std::vector<CPubKey
3333
UniValue DescribeAddress(const CTxDestination& dest);
3434

3535
//! Parse a confirm target option and raise an RPC error if it is invalid.
36-
unsigned int ParseConfirmTarget(const UniValue& value);
36+
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target);
3737

3838
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr);
3939
UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string = "");

src/wallet/feebumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Result CreateTransaction(const CWallet* wallet, const uint256& txid, const CCoin
144144
new_fee = total_fee;
145145
nNewFeeRate = CFeeRate(total_fee, maxNewTxSize);
146146
} else {
147-
new_fee = GetMinimumFee(*wallet, maxNewTxSize, coin_control, mempool, ::feeEstimator, nullptr /* FeeCalculation */);
147+
new_fee = GetMinimumFee(*wallet, maxNewTxSize, coin_control, nullptr /* FeeCalculation */);
148148
nNewFeeRate = CFeeRate(new_fee, maxNewTxSize);
149149

150150
// New fee rate must be at least old rate + minimum incremental relay rate
@@ -195,7 +195,7 @@ Result CreateTransaction(const CWallet* wallet, const uint256& txid, const CCoin
195195

196196
// If the output would become dust, discard it (converting the dust to fee)
197197
poutput->nValue -= nDelta;
198-
if (poutput->nValue <= GetDustThreshold(*poutput, GetDiscardRate(*wallet, ::feeEstimator))) {
198+
if (poutput->nValue <= GetDustThreshold(*poutput, GetDiscardRate(*wallet))) {
199199
wallet->WalletLogPrintf("Bumping fee and discarding dust output\n");
200200
new_fee += poutput->nValue;
201201
mtx.vout.erase(mtx.vout.begin() + nOutput);

src/wallet/fees.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <wallet/fees.h>
77

88
#include <policy/policy.h>
9-
#include <txmempool.h>
109
#include <util/system.h>
1110
#include <validation.h>
1211
#include <wallet/coincontrol.h>
@@ -19,9 +18,9 @@ CAmount GetRequiredFee(const CWallet& wallet, unsigned int nTxBytes)
1918
}
2019

2120

22-
CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation* feeCalc)
21+
CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinControl& coin_control, FeeCalculation* feeCalc)
2322
{
24-
CAmount fee_needed = GetMinimumFeeRate(wallet, coin_control, pool, estimator, feeCalc).GetFee(nTxBytes);
23+
CAmount fee_needed = GetMinimumFeeRate(wallet, coin_control, feeCalc).GetFee(nTxBytes);
2524
// Always obey the maximum
2625
if (fee_needed > maxTxFee) {
2726
fee_needed = maxTxFee;
@@ -35,7 +34,7 @@ CFeeRate GetRequiredFeeRate(const CWallet& wallet)
3534
return std::max(wallet.m_min_fee, ::minRelayTxFee);
3635
}
3736

38-
CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation* feeCalc)
37+
CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, FeeCalculation* feeCalc)
3938
{
4039
/* User control of how to calculate fee uses the following parameter precedence:
4140
1. coin_control.m_feerate
@@ -64,7 +63,7 @@ CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_contr
6463
if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
6564
else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
6665

67-
feerate_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate);
66+
feerate_needed = wallet.chain().estimateSmartFee(target, conservative_estimate, feeCalc);
6867
if (feerate_needed == CFeeRate(0)) {
6968
// if we don't have enough data for estimateSmartFee, then use fallback fee
7069
feerate_needed = wallet.m_fallback_fee;
@@ -74,7 +73,7 @@ CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_contr
7473
if (wallet.m_fallback_fee == CFeeRate(0)) return feerate_needed;
7574
}
7675
// Obey mempool min fee when using smart fee estimation
77-
CFeeRate min_mempool_feerate = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
76+
CFeeRate min_mempool_feerate = wallet.chain().mempoolMinFee();
7877
if (feerate_needed < min_mempool_feerate) {
7978
feerate_needed = min_mempool_feerate;
8079
if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
@@ -90,10 +89,10 @@ CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_contr
9089
return feerate_needed;
9190
}
9291

93-
CFeeRate GetDiscardRate(const CWallet& wallet, const CBlockPolicyEstimator& estimator)
92+
CFeeRate GetDiscardRate(const CWallet& wallet)
9493
{
95-
unsigned int highest_target = estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
96-
CFeeRate discard_rate = estimator.estimateSmartFee(highest_target, nullptr /* FeeCalculation */, false /* conservative */);
94+
unsigned int highest_target = wallet.chain().estimateMaxBlocks();
95+
CFeeRate discard_rate = wallet.chain().estimateSmartFee(highest_target, false /* conservative */);
9796
// Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
9897
discard_rate = (discard_rate == CFeeRate(0)) ? wallet.m_discard_rate : std::min(discard_rate, wallet.m_discard_rate);
9998
// Discard rate must be at least dustRelayFee

src/wallet/fees.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88

99
#include <amount.h>
1010

11-
class CBlockPolicyEstimator;
1211
class CCoinControl;
1312
class CFeeRate;
14-
class CTxMemPool;
1513
class CWallet;
1614
struct FeeCalculation;
1715

@@ -25,7 +23,7 @@ CAmount GetRequiredFee(const CWallet& wallet, unsigned int nTxBytes);
2523
* Estimate the minimum fee considering user set parameters
2624
* and the required fee
2725
*/
28-
CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation* feeCalc);
26+
CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinControl& coin_control, FeeCalculation* feeCalc);
2927

3028
/**
3129
* Return the minimum required feerate taking into account the
@@ -37,11 +35,11 @@ CFeeRate GetRequiredFeeRate(const CWallet& wallet);
3735
* Estimate the minimum fee rate considering user set parameters
3836
* and the required fee
3937
*/
40-
CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation* feeCalc);
38+
CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, FeeCalculation* feeCalc);
4139

4240
/**
4341
* Return the maximum feerate for discarding change.
4442
*/
45-
CFeeRate GetDiscardRate(const CWallet& wallet, const CBlockPolicyEstimator& estimator);
43+
CFeeRate GetDiscardRate(const CWallet& wallet);
4644

4745
#endif // BITCOIN_WALLET_FEES_H

src/wallet/rpcwallet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static UniValue sendtoaddress(const JSONRPCRequest& request)
424424
}
425425

426426
if (!request.params[6].isNull()) {
427-
coin_control.m_confirm_target = ParseConfirmTarget(request.params[6]);
427+
coin_control.m_confirm_target = ParseConfirmTarget(request.params[6], pwallet->chain().estimateMaxBlocks());
428428
}
429429

430430
if (!request.params[7].isNull()) {
@@ -884,7 +884,7 @@ static UniValue sendmany(const JSONRPCRequest& request)
884884
}
885885

886886
if (!request.params[6].isNull()) {
887-
coin_control.m_confirm_target = ParseConfirmTarget(request.params[6]);
887+
coin_control.m_confirm_target = ParseConfirmTarget(request.params[6], pwallet->chain().estimateMaxBlocks());
888888
}
889889

890890
if (!request.params[7].isNull()) {
@@ -2989,7 +2989,7 @@ void FundTransaction(CWallet* const pwallet, CMutableTransaction& tx, CAmount& f
29892989
if (options.exists("feeRate")) {
29902990
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot specify both conf_target and feeRate");
29912991
}
2992-
coinControl.m_confirm_target = ParseConfirmTarget(options["conf_target"]);
2992+
coinControl.m_confirm_target = ParseConfirmTarget(options["conf_target"], pwallet->chain().estimateMaxBlocks());
29932993
}
29942994
if (options.exists("estimate_mode")) {
29952995
if (options.exists("feeRate")) {
@@ -3279,7 +3279,7 @@ static UniValue bumpfee(const JSONRPCRequest& request)
32793279
if (options.exists("confTarget") && options.exists("totalFee")) {
32803280
throw JSONRPCError(RPC_INVALID_PARAMETER, "confTarget and totalFee options should not both be set. Please provide either a confirmation target for fee estimation or an explicit total fee for the transaction.");
32813281
} else if (options.exists("confTarget")) { // TODO: alias this to conf_target
3282-
coin_control.m_confirm_target = ParseConfirmTarget(options["confTarget"]);
3282+
coin_control.m_confirm_target = ParseConfirmTarget(options["confTarget"], pwallet->chain().estimateMaxBlocks());
32833283
} else if (options.exists("totalFee")) {
32843284
totalFee = options["totalFee"].get_int64();
32853285
if (totalFee <= 0) {

0 commit comments

Comments
 (0)