Skip to content

Commit fab0488

Browse files
committed
Add a GetMinimumFeeRate function which is wrapped by GetMinimumFee
1 parent cd927ff commit fab0488

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

src/wallet/fees.cpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ CAmount GetRequiredFee(unsigned int nTxBytes)
2020

2121

2222
CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc)
23+
{
24+
CAmount fee_needed = GetMinimumFeeRate(coin_control, pool, estimator, feeCalc).GetFee(nTxBytes);
25+
// Always obey the maximum
26+
if (fee_needed > maxTxFee) {
27+
fee_needed = maxTxFee;
28+
if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
29+
}
30+
return fee_needed;
31+
}
32+
33+
CFeeRate GetRequiredFeeRate()
34+
{
35+
return std::max(CWallet::minTxFee, ::minRelayTxFee);
36+
}
37+
38+
CFeeRate GetMinimumFeeRate(const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc)
2339
{
2440
/* User control of how to calculate fee uses the following parameter precedence:
2541
1. coin_control.m_feerate
@@ -28,15 +44,15 @@ CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, c
2844
4. nTxConfirmTarget (user-set global variable)
2945
The first parameter that is set is used.
3046
*/
31-
CAmount fee_needed;
47+
CFeeRate feerate_needed ;
3248
if (coin_control.m_feerate) { // 1.
33-
fee_needed = coin_control.m_feerate->GetFee(nTxBytes);
49+
feerate_needed = *(coin_control.m_feerate);
3450
if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
3551
// Allow to override automatic min/max check over coin control instance
36-
if (coin_control.fOverrideFeeRate) return fee_needed;
52+
if (coin_control.fOverrideFeeRate) return feerate_needed;
3753
}
3854
else if (!coin_control.m_confirm_target && ::payTxFee != CFeeRate(0)) { // 3. TODO: remove magic value of 0 for global payTxFee
39-
fee_needed = ::payTxFee.GetFee(nTxBytes);
55+
feerate_needed = ::payTxFee;
4056
if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
4157
}
4258
else { // 2. or 4.
@@ -48,38 +64,32 @@ CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, c
4864
if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
4965
else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
5066

51-
fee_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate).GetFee(nTxBytes);
52-
if (fee_needed == 0) {
67+
feerate_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate);
68+
if (feerate_needed == CFeeRate(0)) {
5369
// if we don't have enough data for estimateSmartFee, then use fallbackFee
54-
fee_needed = CWallet::fallbackFee.GetFee(nTxBytes);
70+
feerate_needed = CWallet::fallbackFee;
5571
if (feeCalc) feeCalc->reason = FeeReason::FALLBACK;
5672

5773
// directly return if fallback fee is disabled (feerate 0 == disabled)
58-
if (CWallet::fallbackFee.GetFee(1000) == 0) return fee_needed;
74+
if (CWallet::fallbackFee == CFeeRate(0)) return feerate_needed;
5975
}
6076
// Obey mempool min fee when using smart fee estimation
61-
CAmount min_mempool_fee = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nTxBytes);
62-
if (fee_needed < min_mempool_fee) {
63-
fee_needed = min_mempool_fee;
77+
CFeeRate min_mempool_feerate = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
78+
if (feerate_needed < min_mempool_feerate) {
79+
feerate_needed = min_mempool_feerate;
6480
if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
6581
}
6682
}
6783

6884
// prevent user from paying a fee below minRelayTxFee or minTxFee
69-
CAmount required_fee = GetRequiredFee(nTxBytes);
70-
if (required_fee > fee_needed) {
71-
fee_needed = required_fee;
85+
CFeeRate required_feerate = GetRequiredFeeRate();
86+
if (required_feerate > feerate_needed) {
87+
feerate_needed = required_feerate;
7288
if (feeCalc) feeCalc->reason = FeeReason::REQUIRED;
7389
}
74-
// But always obey the maximum
75-
if (fee_needed > maxTxFee) {
76-
fee_needed = maxTxFee;
77-
if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
78-
}
79-
return fee_needed;
90+
return feerate_needed;
8091
}
8192

82-
8393
CFeeRate GetDiscardRate(const CBlockPolicyEstimator& estimator)
8494
{
8595
unsigned int highest_target = estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);

src/wallet/fees.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ CAmount GetRequiredFee(unsigned int nTxBytes);
2626
*/
2727
CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc);
2828

29+
/**
30+
* Return the minimum required feerate taking into account the
31+
* floating relay feerate and user set minimum transaction feerate
32+
*/
33+
CFeeRate GetRequiredFeeRate();
34+
35+
/**
36+
* Estimate the minimum fee rate considering user set parameters
37+
* and the required fee
38+
*/
39+
CFeeRate GetMinimumFeeRate(const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc);
40+
2941
/**
3042
* Return the maximum feerate for discarding change.
3143
*/

0 commit comments

Comments
 (0)