Raven Core  3.0.0
P2P Digital Currency
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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "wallet/fees.h"
8 
9 #include "policy/policy.h"
10 #include "txmempool.h"
11 #include "util.h"
12 #include "validation.h"
13 #include "wallet/coincontrol.h"
14 #include "wallet/wallet.h"
15 
16 
17 CAmount GetRequiredFee(unsigned int nTxBytes)
18 {
19  return std::max(CWallet::minTxFee.GetFee(nTxBytes), (AreMessagingDeployed() ? ::minRelayTxFeeV2.GetFee(nTxBytes) : ::minRelayTxFee.GetFee(nTxBytes)));
20 }
21 
22 
23 CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc)
24 {
25  /* User control of how to calculate fee uses the following parameter precedence:
26  1. coin_control.m_feerate
27  2. coin_control.m_confirm_target
28  3. payTxFee (user-set global variable)
29  4. nTxConfirmTarget (user-set global variable)
30  The first parameter that is set is used.
31  */
32  CAmount fee_needed;
33  if (coin_control.m_feerate) { // 1.
34  fee_needed = coin_control.m_feerate->GetFee(nTxBytes);
35  if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
36  // Allow to override automatic min/max check over coin control instance
37  if (coin_control.fOverrideFeeRate) return fee_needed;
38  }
39  else if (!coin_control.m_confirm_target && ::payTxFee != CFeeRate(0)) { // 3. TODO: remove magic value of 0 for global payTxFee
40  fee_needed = ::payTxFee.GetFee(nTxBytes);
41  if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
42  }
43  else { // 2. or 4.
44  // We will use smart fee estimation
45  unsigned int target = coin_control.m_confirm_target ? *coin_control.m_confirm_target : ::nTxConfirmTarget;
46  // By default estimates are economical iff we are signaling opt-in-RBF
47  bool conservative_estimate = !coin_control.signalRbf;
48  // Allow to override the default fee estimate mode over the CoinControl instance
49  if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
50  else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
51 
52  fee_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate).GetFee(nTxBytes);
53  if (fee_needed == 0) {
54  // if we don't have enough data for estimateSmartFee, then use fallbackFee
55  fee_needed = CWallet::fallbackFee.GetFee(nTxBytes);
56  if (feeCalc) feeCalc->reason = FeeReason::FALLBACK;
57  }
58  // Obey mempool min fee when using smart fee estimation
59  CAmount min_mempool_fee = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nTxBytes);
60  if (fee_needed < min_mempool_fee) {
61  fee_needed = min_mempool_fee;
62  if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
63  }
64  }
65 
66  // prevent user from paying a fee below minRelayTxFee or minTxFee
67  CAmount required_fee = GetRequiredFee(nTxBytes);
68  if (required_fee > fee_needed) {
69  fee_needed = required_fee;
70  if (feeCalc) feeCalc->reason = FeeReason::REQUIRED;
71  }
72  // But always obey the maximum
73  if (fee_needed > maxTxFee) {
74  fee_needed = maxTxFee;
75  if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
76  }
77  return fee_needed;
78 }
79 
80 
82 {
83  unsigned int highest_target = estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
84  CFeeRate discard_rate = estimator.estimateSmartFee(highest_target, nullptr /* FeeCalculation */, false /* conservative */);
85  // Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
86  discard_rate = (discard_rate == CFeeRate(0)) ? CWallet::m_discard_rate : std::min(discard_rate, CWallet::m_discard_rate);
87  // Discard rate must be at least dustRelayFee
88  discard_rate = std::max(discard_rate, ::dustRelayFee);
89  return discard_rate;
90 }
CFeeRate GetDiscardRate(const CBlockPolicyEstimator &estimator)
Return the maximum feerate for discarding change.
Definition: fees.cpp:81
boost::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:30
CAmount maxTxFee
Absolute maximum transaction fee (in satoshis) used by wallet and mempool (rejects high fee in sendra...
Definition: validation.cpp:105
FeeReason reason
Definition: fees.h:129
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:17
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition: coincontrol.h:26
Coin Control Features.
Definition: coincontrol.h:17
boost::optional< CFeeRate > m_feerate
Override the default payTxFee if set.
Definition: coincontrol.h:28
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
Force estimateSmartFee to use non-conservative estimates.
bool AreMessagingDeployed()
unsigned int nTxConfirmTarget
Definition: wallet.cpp:47
static CFeeRate m_discard_rate
Definition: wallet.h:1037
bool signalRbf
Signal BIP-125 replace by fee.
Definition: coincontrol.h:32
CFeeRate minRelayTxFee
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) ...
Definition: validation.cpp:104
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:139
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:835
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:1357
CFeeRate minRelayTxFeeV2
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) ...
Definition: validation.cpp:107
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const
Calculation of highest target that estimates are tracked for.
Definition: fees.cpp:726
ArgsManager gArgs
Definition: util.cpp:94
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:416
static CFeeRate fallbackFee
If fee estimation does not have enough data to provide estimates, use this fee instead.
Definition: wallet.h:1036
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:454
static CFeeRate minTxFee
Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) Override with ...
Definition: wallet.h:1035
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:20
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:34
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:23
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:24
CFeeRate dustRelayFee
Definition: policy.cpp:263