1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "wallet/fees.h"
8 #include "policy/policy.h"
11 #include "validation.h"
12 #include "wallet/coincontrol.h"
13 #include "wallet/wallet.h"
16 CAmount
GetRequiredFee(unsigned int nTxBytes
)
18 return std::max(CWallet::minTxFee
.GetFee(nTxBytes
), ::minRelayTxFee
.GetFee(nTxBytes
));
22 CAmount
GetMinimumFee(unsigned int nTxBytes
, const CCoinControl
& coin_control
, const CTxMemPool
& pool
, const CBlockPolicyEstimator
& estimator
, FeeCalculation
*feeCalc
)
24 /* User control of how to calculate fee uses the following parameter precedence:
25 1. coin_control.m_feerate
26 2. coin_control.m_confirm_target
27 3. payTxFee (user-set global variable)
28 4. nTxConfirmTarget (user-set global variable)
29 The first parameter that is set is used.
32 if (coin_control
.m_feerate
) { // 1.
33 fee_needed
= coin_control
.m_feerate
->GetFee(nTxBytes
);
34 if (feeCalc
) feeCalc
->reason
= FeeReason::PAYTXFEE
;
35 // Allow to override automatic min/max check over coin control instance
36 if (coin_control
.fOverrideFeeRate
) return fee_needed
;
38 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
);
40 if (feeCalc
) feeCalc
->reason
= FeeReason::PAYTXFEE
;
43 // We will use smart fee estimation
44 unsigned int target
= coin_control
.m_confirm_target
? *coin_control
.m_confirm_target
: ::nTxConfirmTarget
;
45 // By default estimates are economical iff we are signaling opt-in-RBF
46 bool conservative_estimate
= !coin_control
.signalRbf
;
47 // Allow to override the default fee estimate mode over the CoinControl instance
48 if (coin_control
.m_fee_mode
== FeeEstimateMode::CONSERVATIVE
) conservative_estimate
= true;
49 else if (coin_control
.m_fee_mode
== FeeEstimateMode::ECONOMICAL
) conservative_estimate
= false;
51 fee_needed
= estimator
.estimateSmartFee(target
, feeCalc
, conservative_estimate
).GetFee(nTxBytes
);
52 if (fee_needed
== 0) {
53 // if we don't have enough data for estimateSmartFee, then use fallbackFee
54 fee_needed
= CWallet::fallbackFee
.GetFee(nTxBytes
);
55 if (feeCalc
) feeCalc
->reason
= FeeReason::FALLBACK
;
57 // Obey mempool min fee when using smart fee estimation
58 CAmount min_mempool_fee
= pool
.GetMinFee(gArgs
.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE
) * 1000000).GetFee(nTxBytes
);
59 if (fee_needed
< min_mempool_fee
) {
60 fee_needed
= min_mempool_fee
;
61 if (feeCalc
) feeCalc
->reason
= FeeReason::MEMPOOL_MIN
;
65 // prevent user from paying a fee below minRelayTxFee or minTxFee
66 CAmount required_fee
= GetRequiredFee(nTxBytes
);
67 if (required_fee
> fee_needed
) {
68 fee_needed
= required_fee
;
69 if (feeCalc
) feeCalc
->reason
= FeeReason::REQUIRED
;
71 // But always obey the maximum
72 if (fee_needed
> maxTxFee
) {
73 fee_needed
= maxTxFee
;
74 if (feeCalc
) feeCalc
->reason
= FeeReason::MAXTXFEE
;
80 CFeeRate
GetDiscardRate(const CBlockPolicyEstimator
& estimator
)
82 unsigned int highest_target
= estimator
.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE
);
83 CFeeRate discard_rate
= estimator
.estimateSmartFee(highest_target
, nullptr /* FeeCalculation */, false /* conservative */);
84 // Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
85 discard_rate
= (discard_rate
== CFeeRate(0)) ? CWallet::m_discard_rate
: std::min(discard_rate
, CWallet::m_discard_rate
);
86 // Discard rate must be at least dustRelayFee
87 discard_rate
= std::max(discard_rate
, ::dustRelayFee
);