Bitcoin Core  0.20.99
P2P Digital Currency
fees.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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.
5 
6 #include <wallet/fees.h>
7 
8 #include <wallet/coincontrol.h>
9 #include <wallet/wallet.h>
10 
11 
12 CAmount GetRequiredFee(const CWallet& wallet, unsigned int nTxBytes)
13 {
14  return GetRequiredFeeRate(wallet).GetFee(nTxBytes);
15 }
16 
17 
18 CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinControl& coin_control, FeeCalculation* feeCalc)
19 {
20  return GetMinimumFeeRate(wallet, coin_control, feeCalc).GetFee(nTxBytes);
21 }
22 
24 {
25  return std::max(wallet.m_min_fee, wallet.chain().relayMinFee());
26 }
27 
28 CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, FeeCalculation* feeCalc)
29 {
30  /* User control of how to calculate fee uses the following parameter precedence:
31  1. coin_control.m_feerate
32  2. coin_control.m_confirm_target
33  3. m_pay_tx_fee (user-set member variable of wallet)
34  4. m_confirm_target (user-set member variable of wallet)
35  The first parameter that is set is used.
36  */
37  CFeeRate feerate_needed;
38  if (coin_control.m_feerate) { // 1.
39  feerate_needed = *(coin_control.m_feerate);
40  if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
41  // Allow to override automatic min/max check over coin control instance
42  if (coin_control.fOverrideFeeRate) return feerate_needed;
43  }
44  else if (!coin_control.m_confirm_target && wallet.m_pay_tx_fee != CFeeRate(0)) { // 3. TODO: remove magic value of 0 for wallet member m_pay_tx_fee
45  feerate_needed = wallet.m_pay_tx_fee;
46  if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
47  }
48  else { // 2. or 4.
49  // We will use smart fee estimation
50  unsigned int target = coin_control.m_confirm_target ? *coin_control.m_confirm_target : wallet.m_confirm_target;
51  // By default estimates are economical iff we are signaling opt-in-RBF
52  bool conservative_estimate = !coin_control.m_signal_bip125_rbf.get_value_or(wallet.m_signal_rbf);
53  // Allow to override the default fee estimate mode over the CoinControl instance
54  if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
55  else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
56 
57  feerate_needed = wallet.chain().estimateSmartFee(target, conservative_estimate, feeCalc);
58  if (feerate_needed == CFeeRate(0)) {
59  // if we don't have enough data for estimateSmartFee, then use fallback fee
60  feerate_needed = wallet.m_fallback_fee;
61  if (feeCalc) feeCalc->reason = FeeReason::FALLBACK;
62 
63  // directly return if fallback fee is disabled (feerate 0 == disabled)
64  if (wallet.m_fallback_fee == CFeeRate(0)) return feerate_needed;
65  }
66  // Obey mempool min fee when using smart fee estimation
67  CFeeRate min_mempool_feerate = wallet.chain().mempoolMinFee();
68  if (feerate_needed < min_mempool_feerate) {
69  feerate_needed = min_mempool_feerate;
70  if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
71  }
72  }
73 
74  // prevent user from paying a fee below the required fee rate
75  CFeeRate required_feerate = GetRequiredFeeRate(wallet);
76  if (required_feerate > feerate_needed) {
77  feerate_needed = required_feerate;
78  if (feeCalc) feeCalc->reason = FeeReason::REQUIRED;
79  }
80  return feerate_needed;
81 }
82 
84 {
85  unsigned int highest_target = wallet.chain().estimateMaxBlocks();
86  CFeeRate discard_rate = wallet.chain().estimateSmartFee(highest_target, false /* conservative */);
87  // Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
88  discard_rate = (discard_rate == CFeeRate(0)) ? wallet.m_discard_rate : std::min(discard_rate, wallet.m_discard_rate);
89  // Discard rate must be at least dustRelayFee
90  discard_rate = std::max(discard_rate, wallet.chain().relayDustFee());
91  return discard_rate;
92 }
virtual CFeeRate mempoolMinFee()=0
Mempool minimum fee.
virtual CFeeRate relayDustFee()=0
Relay dust fee setting (-dustrelayfee), reflecting lowest rate it&#39;s economical to spend...
CFeeRate m_pay_tx_fee
Definition: wallet.h:998
FeeReason reason
Definition: fees.h:71
CAmount GetMinimumFee(const CWallet &wallet, unsigned int nTxBytes, const CCoinControl &coin_control, FeeCalculation *feeCalc)
Estimate the minimum fee considering user set parameters and the required fee.
Definition: fees.cpp:18
CFeeRate GetDiscardRate(const CWallet &wallet)
Return the maximum feerate for discarding change.
Definition: fees.cpp:83
Force estimateSmartFee to use non-conservative estimates.
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition: coincontrol.h:36
Coin Control Features.
Definition: coincontrol.h:22
CFeeRate GetMinimumFeeRate(const CWallet &wallet, const CCoinControl &coin_control, FeeCalculation *feeCalc)
Estimate the minimum fee rate considering user set parameters and the required fee.
Definition: fees.cpp:28
Force estimateSmartFee to use conservative estimates.
CFeeRate m_min_fee
Override with -mintxfee.
Definition: wallet.h:1003
interfaces::Chain & chain() const
Interface for accessing chain state.
Definition: wallet.h:794
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CFeeRate m_fallback_fee
If fee estimation does not have enough data to provide estimates, use this fee instead.
Definition: wallet.h:1009
Optional< bool > m_signal_bip125_rbf
Override the wallet&#39;s m_signal_rbf if set.
Definition: coincontrol.h:42
CFeeRate m_discard_rate
Definition: wallet.h:1010
virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation *calc=nullptr)=0
Estimate smart fee.
Optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:40
CFeeRate GetRequiredFeeRate(const CWallet &wallet)
Return the minimum required feerate taking into account the minimum relay feerate and user set minimu...
Definition: fees.cpp:23
unsigned int m_confirm_target
Definition: wallet.h:999
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:618
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
virtual unsigned int estimateMaxBlocks()=0
Fee estimator max target.
FeeEstimateMode m_fee_mode
Fee estimation mode to control arguments to estimateSmartFee.
Definition: coincontrol.h:48
CAmount GetRequiredFee(const CWallet &wallet, unsigned int nTxBytes)
Return the minimum required absolute fee for this size based on the required fee rate.
Definition: fees.cpp:12
Optional< CFeeRate > m_feerate
Override the wallet&#39;s m_pay_tx_fee if set.
Definition: coincontrol.h:38
bool m_signal_rbf
Definition: wallet.h:1001
virtual CFeeRate relayMinFee()=0
Relay current minimum fee (from -minrelaytxfee and -incrementalrelayfee settings).
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:21