Bitcoin Core 31.99.0
P2P Digital Currency
fees.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present 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/feerate.h>
9#include <util/fees.h>
10#include <wallet/coincontrol.h>
11#include <wallet/wallet.h>
12
13#include <optional>
14
15namespace wallet {
16CAmount GetRequiredFee(const CWallet& wallet, unsigned int nTxBytes)
17{
18 return GetRequiredFeeRate(wallet).GetFee(static_cast<int32_t>(nTxBytes));
19}
20
21
22CAmount GetMinimumFee(const MinimumFeeRateResult& min_fee_rate, unsigned int nTxBytes)
23{
24 return min_fee_rate.fee_rate.GetFee(static_cast<int32_t>(nTxBytes));
25}
26
28{
29 return std::max(wallet.m_min_fee, wallet.chain().relayMinFee());
30}
31
33{
34 /* User control of how to calculate fee uses the following parameter precedence:
35 1. coin_control.m_feerate
36 2. coin_control.m_confirm_target
37 3. m_confirm_target (user-set member variable of wallet)
38 The first parameter that is set is used.
39 */
40 if (coin_control.m_feerate) { // 1.
41 CFeeRate fee_rate{*coin_control.m_feerate};
42 // Allow to override automatic min/max check over coin control instance
43 if (coin_control.fOverrideFeeRate) return {fee_rate, FeeReason::USER_SPECIFIED, std::nullopt};
44
45 CFeeRate required_feerate = GetRequiredFeeRate(wallet);
46 if (required_feerate > fee_rate) return {required_feerate, FeeReason::REQUIRED, std::nullopt};
47
48 return {fee_rate, FeeReason::USER_SPECIFIED, std::nullopt};
49 }
50
51 // We will use smart fee estimation
52 unsigned int target = coin_control.m_confirm_target ? *coin_control.m_confirm_target : wallet.m_confirm_target;
53 // By default estimates are economical iff we are signaling opt-in-RBF
54 bool conservative_estimate = !coin_control.m_signal_bip125_rbf.value_or(wallet.m_signal_rbf);
55 // Allow to override the default fee estimate mode over the CoinControl instance
56 if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE)
57 conservative_estimate = true;
58 else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL)
59 conservative_estimate = false;
60
61 const auto fee_estimation_res = wallet.chain().getFeeRateEstimate(target, conservative_estimate);
62 const FeeRateEstimation& estimation{FeeRateEstimationRef(fee_estimation_res)};
63 CFeeRate fee_rate{estimation.feerate};
65 // Only fee rate estimator results have a returned target.
66 std::optional<int> returned_target{estimation.returned_target};
67 if (fee_rate == CFeeRate(0)) {
68 // if we don't have enough data for getFeeRateEstimate, then use fallback fee
69 fee_rate = wallet.m_fallback_fee;
70 fee_reason = FeeReason::FALLBACK;
71 returned_target = std::nullopt;
72 // directly return if fallback fee is disabled (feerate 0 == disabled)
73 if (wallet.m_fallback_fee == CFeeRate(0)) return {fee_rate, FeeReason::FALLBACK, std::nullopt};
74 }
75
76 // Obey mempool min fee when using smart fee estimation or fallback fee
77 CFeeRate min_mempool_feerate = wallet.chain().mempoolMinFee();
78 if (fee_rate < min_mempool_feerate) {
79 fee_rate = min_mempool_feerate;
80 fee_reason = FeeReason::MEMPOOL_MIN;
81 returned_target = std::nullopt;
82 }
83
84 CFeeRate required_feerate = GetRequiredFeeRate(wallet);
85 if (required_feerate > fee_rate) return {required_feerate, FeeReason::REQUIRED, std::nullopt};
86
87 return {fee_rate, fee_reason, returned_target};
88}
89
91{
92 unsigned int highest_target = wallet.chain().maximumFeeEstimationTargetBlocks();
93 const auto res = wallet.chain().getFeeRateEstimate(highest_target, /*conservative=*/false);
94 auto discard_rate = res ? CFeeRate(res->feerate) : CFeeRate(0);
95 // Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
96 discard_rate = (discard_rate == CFeeRate(0)) ? wallet.m_discard_rate : std::min(discard_rate, wallet.m_discard_rate);
97 // Discard rate must be at least dust relay feerate
98 discard_rate = std::max(discard_rate, wallet.chain().relayDustFee());
99 return discard_rate;
100}
101} // namespace wallet
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:32
CAmount GetFee(int32_t virtual_bytes) const
Return the fee in satoshis for the given vsize in vbytes.
Definition: feerate.cpp:20
Coin Control Features.
Definition: coincontrol.h:83
FeeEstimateMode m_fee_mode
Fee estimation mode.
Definition: coincontrol.h:107
std::optional< bool > m_signal_bip125_rbf
Override the wallet's m_signal_rbf if set.
Definition: coincontrol.h:101
std::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:99
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition: coincontrol.h:95
std::optional< CFeeRate > m_feerate
Override the wallet's fee rate if set.
Definition: coincontrol.h:97
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:310
CFeeRate GetRequiredFeeRate(const CWallet &wallet)
Return the minimum required feerate taking into account the minimum relay feerate and user set minimu...
Definition: fees.cpp:27
MinimumFeeRateResult GetMinimumFeeRate(const CWallet &wallet, const CCoinControl &coin_control)
Estimate the minimum fee rate considering user set parameters and the required fee.
Definition: fees.cpp:32
CAmount GetMinimumFee(const MinimumFeeRateResult &min_fee_rate, unsigned int nTxBytes)
Return the minimum fee for this size given a fee rate result.
Definition: fees.cpp:22
CFeeRate GetDiscardRate(const CWallet &wallet)
Return the maximum feerate for discarding change.
Definition: fees.cpp:90
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
A successful fee rate estimate returned by a fee rate estimator.
Definition: fees.h:46
const FeeRateEstimation & FeeRateEstimationRef(const util::Expected< FeeRateEstimation, FeeRateEstimationError > &result LIFETIMEBOUND)
Return the estimation carried by a fee rate estimate result: the successful estimation,...
Definition: fees.h:87
FeeReason
Definition: fees.h:24
@ FEE_RATE_ESTIMATOR
@ CONSERVATIVE
Force Fee rate estimator to return conservative estimates.
@ ECONOMICAL
Force Fee rate estimator to return non-conservative estimates.