BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
fees.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <policy/policy.h>
9 #include <txmempool.h>
10 #include <util.h>
11 #include <validation.h>
12 #include <wallet/coincontrol.h>
13 #include <wallet/wallet.h>
14 
15 
16 CAmount GetRequiredFee(const CWallet& wallet, unsigned int nTxBytes)
17 {
18  return GetRequiredFeeRate(wallet).GetFee(nTxBytes);
19 }
20 
21 
22 CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation* feeCalc)
23 {
24  CAmount fee_needed = GetMinimumFeeRate(wallet, 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 
34 {
35  return std::max(wallet.m_min_fee, ::minRelayTxFee);
36 }
37 
38 CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation* feeCalc)
39 {
40  /* User control of how to calculate fee uses the following parameter precedence:
41  1. coin_control.m_feerate
42  2. coin_control.m_confirm_target
43  3. m_pay_tx_fee (user-set member variable of wallet)
44  4. m_confirm_target (user-set member variable of wallet)
45  The first parameter that is set is used.
46  */
47  CFeeRate feerate_needed;
48  if (coin_control.m_feerate) { // 1.
49  feerate_needed = *(coin_control.m_feerate);
50  if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
51  // Allow to override automatic min/max check over coin control instance
52  if (coin_control.fOverrideFeeRate) return feerate_needed;
53  }
54  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
55  feerate_needed = wallet.m_pay_tx_fee;
56  if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
57  }
58  else { // 2. or 4.
59  // We will use smart fee estimation
60  unsigned int target = coin_control.m_confirm_target ? *coin_control.m_confirm_target : wallet.m_confirm_target;
61  // By default estimates are economical iff we are signaling opt-in-RBF
62  bool conservative_estimate = !coin_control.m_signal_bip125_rbf.get_value_or(wallet.m_signal_rbf);
63  // Allow to override the default fee estimate mode over the CoinControl instance
64  if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
65  else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
66 
67  feerate_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate);
68  if (feerate_needed == CFeeRate(0)) {
69  // if we don't have enough data for estimateSmartFee, then use fallback fee
70  feerate_needed = wallet.m_fallback_fee;
71  if (feeCalc) feeCalc->reason = FeeReason::FALLBACK;
72 
73  // directly return if fallback fee is disabled (feerate 0 == disabled)
74  if (wallet.m_fallback_fee == CFeeRate(0)) return feerate_needed;
75  }
76  // Obey mempool min fee when using smart fee estimation
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;
80  if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
81  }
82  }
83 
84  // prevent user from paying a fee below the required fee rate
85  CFeeRate required_feerate = GetRequiredFeeRate(wallet);
86  if (required_feerate > feerate_needed) {
87  feerate_needed = required_feerate;
88  if (feeCalc) feeCalc->reason = FeeReason::REQUIRED;
89  }
90  return feerate_needed;
91 }
92 
93 CFeeRate GetDiscardRate(const CWallet& wallet, const CBlockPolicyEstimator& estimator)
94 {
95  unsigned int highest_target = estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
96  CFeeRate discard_rate = estimator.estimateSmartFee(highest_target, nullptr /* FeeCalculation */, false /* conservative */);
97  // Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
98  discard_rate = (discard_rate == CFeeRate(0)) ? wallet.m_discard_rate : std::min(discard_rate, wallet.m_discard_rate);
99  // Discard rate must be at least dustRelayFee
100  discard_rate = std::max(discard_rate, ::dustRelayFee);
101  return discard_rate;
102 }
CAmount GetMinimumFee(const CWallet &wallet, unsigned int nTxBytes, const CCoinControl &coin_control, const CTxMemPool &pool, const CBlockPolicyEstimator &estimator, FeeCalculation *feeCalc)
Estimate the minimum fee considering user set parameters and the required fee.
Definition: fees.cpp:22
boost::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:32
CAmount maxTxFee
Absolute maximum transaction fee (in satoshis) used by wallet and mempool (rejects high fee in sendra...
Definition: validation.cpp:242
CFeeRate m_pay_tx_fee
Definition: wallet.h:918
FeeReason reason
Definition: fees.h:83
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition: coincontrol.h:28
Coin Control Features.
Definition: coincontrol.h:16
CFeeRate m_min_fee
Override with -mintxfee.
Definition: wallet.h:923
boost::optional< CFeeRate > m_feerate
Override the wallet&#39;s m_pay_tx_fee if set.
Definition: coincontrol.h:30
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:929
Force estimateSmartFee to use conservative estimates.
CFeeRate m_discard_rate
Definition: wallet.h:930
CFeeRate minRelayTxFee
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) ...
Definition: validation.cpp:241
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:136
CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const
Estimate feerate needed to get be included in a block within confTarget blocks.
Definition: fees.cpp:820
CFeeRate GetMinimumFeeRate(const CWallet &wallet, const CCoinControl &coin_control, const CTxMemPool &pool, const CBlockPolicyEstimator &estimator, FeeCalculation *feeCalc)
Estimate the minimum fee rate considering user set parameters and the required fee.
Definition: fees.cpp:38
CFeeRate GetMinFee(size_t sizelimit) const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Definition: txmempool.cpp:986
boost::optional< bool > m_signal_bip125_rbf
Override the wallet&#39;s m_signal_rbf if set.
Definition: coincontrol.h:34
CFeeRate GetRequiredFeeRate(const CWallet &wallet)
Return the minimum required feerate taking into account the minimum relay feerate and user set minimu...
Definition: fees.cpp:33
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const
Calculation of highest target that estimates are tracked for.
Definition: fees.cpp:711
ArgsManager gArgs
Definition: util.cpp:88
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:441
unsigned int m_confirm_target
Definition: wallet.h:919
CFeeRate GetDiscardRate(const CWallet &wallet, const CBlockPolicyEstimator &estimator)
Return the maximum feerate for discarding change.
Definition: fees.cpp:93
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:526
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:599
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
FeeEstimateMode m_fee_mode
Fee estimation mode to control arguments to estimateSmartFee.
Definition: coincontrol.h:38
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:16
Force estimateSmartFee to use non-conservative estimates.
bool m_signal_rbf
Definition: wallet.h:921
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:23
CFeeRate dustRelayFee
Definition: policy.cpp:243