Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

fees.cpp
Go to the documentation of this file.
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.
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(unsigned int nTxBytes)
17 {
18  return std::max(CWallet::minTxFee.GetFee(nTxBytes), ::minRelayTxFee.GetFee(nTxBytes));
19 }
20 
21 CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc)
22 {
23  /* User control of how to calculate fee uses the following parameter precedence:
24  1. coin_control.m_feerate
25  2. coin_control.m_confirm_target
26  3. payTxFee (user-set global variable)
27  4. nTxConfirmTarget (user-set global variable)
28  The first parameter that is set is used.
29  */
30  CAmount fee_needed;
31  if (coin_control.m_feerate) { // 1.
32  fee_needed = coin_control.m_feerate->GetFee(nTxBytes);
33  if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
34  // Allow to override automatic min/max check over coin control instance
35  if (coin_control.fOverrideFeeRate) return fee_needed;
36  }
37  else if (!coin_control.m_confirm_target && ::payTxFee != CFeeRate(0)) { // 3. TODO: remove magic value of 0 for global payTxFee
38  fee_needed = ::payTxFee.GetFee(nTxBytes);
39  if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
40  }
41  else { // 2. or 4.
42  // We will use smart fee estimation
43  unsigned int target = coin_control.m_confirm_target ? *coin_control.m_confirm_target : ::nTxConfirmTarget;
44  // By default estimates are economical
45  bool conservative_estimate = true;
46  // Allow to override the default fee estimate mode over the CoinControl instance
47  if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
48  else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
49 
50  fee_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate).GetFee(nTxBytes);
51  if (fee_needed == 0) {
52  // if we don't have enough data for estimateSmartFee, then use fallbackFee
53  fee_needed = CWallet::fallbackFee.GetFee(nTxBytes);
54  if (feeCalc) feeCalc->reason = FeeReason::FALLBACK;
55  }
56  // Obey mempool min fee when using smart fee estimation
57  CAmount min_mempool_fee = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nTxBytes);
58  if (fee_needed < min_mempool_fee) {
59  fee_needed = min_mempool_fee;
60  if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
61  }
62  }
63 
64  // prevent user from paying a fee below minRelayTxFee or minTxFee
65  CAmount required_fee = GetRequiredFee(nTxBytes);
66  if (required_fee > fee_needed) {
67  fee_needed = required_fee;
68  if (feeCalc) feeCalc->reason = FeeReason::REQUIRED;
69  }
70  // But always obey the maximum
71  if (fee_needed > maxTxFee) {
72  fee_needed = maxTxFee;
73  if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
74  }
75  return fee_needed;
76 }
77 
79 {
80  unsigned int highest_target = estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
81  CFeeRate discard_rate = estimator.estimateSmartFee(highest_target, nullptr /* FeeCalculation */, false /* conservative */);
82  // Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
83  discard_rate = (discard_rate == CFeeRate(0)) ? CWallet::m_discard_rate : std::min(discard_rate, CWallet::m_discard_rate);
84  // Discard rate must be at least dustRelayFee
85  discard_rate = std::max(discard_rate, ::dustRelayFee);
86  return discard_rate;
87 }
CFeeRate GetDiscardRate(const CBlockPolicyEstimator &estimator)
Return the maximum feerate for discarding change.
Definition: fees.cpp:78
boost::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:45
static const unsigned int DEFAULT_MAX_MEMPOOL_SIZE
Default for -maxmempool, maximum megabytes of mempool memory usage.
Definition: policy.h:30
CAmount maxTxFee
Absolute maximum transaction fee (in duffs) used by wallet and mempool (rejects high fee in sendrawtr...
Definition: validation.cpp:247
FeeReason reason
Definition: fees.h:128
CAmount GetRequiredFee(unsigned int nTxBytes)
Return the minimum required fee taking into account the floating relay fee and user set minimum trans...
Definition: fees.cpp:16
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition: coincontrol.h:39
Coin Control Features.
Definition: coincontrol.h:28
boost::optional< CFeeRate > m_feerate
Override the default payTxFee if set.
Definition: coincontrol.h:41
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Force estimateSmartFee to use non-conservative estimates.
unsigned int nTxConfirmTarget
Definition: wallet.cpp:96
static CFeeRate m_discard_rate
Definition: wallet.h:1107
CFeeRate minRelayTxFee
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) ...
Definition: validation.cpp:246
We want to be able to estimate feerates that are needed on tx&#39;s to be included in a certain number of...
Definition: fees.h:138
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:819
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:1470
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const
Calculation of highest target that estimates are tracked for.
Definition: fees.cpp:710
ArgsManager gArgs
Definition: util.cpp:108
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:442
static CFeeRate fallbackFee
If fee estimation does not have enough data to provide estimates, use this fee instead.
Definition: wallet.h:1106
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:808
static CFeeRate minTxFee
Fees smaller than this (in duffs) are considered zero fee (for transaction creation) Override with -m...
Definition: wallet.h:1105
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE)
Transaction fee set by the user.
FeeEstimateMode m_fee_mode
Fee estimation mode to control arguments to estimateSmartFee.
Definition: coincontrol.h:47
Use default settings based on other criteria.
CAmount GetMinimumFee(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:21
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:177
Released under the MIT license