Branch data Line data Source code
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 : :
15 : : namespace wallet {
16 : 21 : CAmount GetRequiredFee(const CWallet& wallet, unsigned int nTxBytes)
17 : : {
18 : 21 : return GetRequiredFeeRate(wallet).GetFee(static_cast<int32_t>(nTxBytes));
19 : : }
20 : :
21 : :
22 : 0 : CAmount GetMinimumFee(const MinimumFeeRateResult& min_fee_rate, unsigned int nTxBytes)
23 : : {
24 : 0 : return min_fee_rate.fee_rate.GetFee(static_cast<int32_t>(nTxBytes));
25 : : }
26 : :
27 : 3659 : CFeeRate GetRequiredFeeRate(const CWallet& wallet)
28 : : {
29 : 3659 : return std::max(wallet.m_min_fee, wallet.chain().relayMinFee());
30 : : }
31 : :
32 : 4019 : MinimumFeeRateResult GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control)
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 [ + + ]: 4019 : if (coin_control.m_feerate) { // 1.
41 [ + + ]: 1114 : CFeeRate fee_rate{*coin_control.m_feerate};
42 : : // Allow to override automatic min/max check over coin control instance
43 [ + + ]: 1114 : if (coin_control.fOverrideFeeRate) return {fee_rate, FeeReason::USER_SPECIFIED, std::nullopt};
44 : :
45 : 746 : CFeeRate required_feerate = GetRequiredFeeRate(wallet);
46 [ + + ]: 746 : if (required_feerate > fee_rate) return {required_feerate, FeeReason::REQUIRED, std::nullopt};
47 : :
48 : 724 : return {fee_rate, FeeReason::USER_SPECIFIED, std::nullopt};
49 : : }
50 : :
51 : : // We will use smart fee estimation
52 [ + + ]: 2905 : 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 [ + + ]: 2905 : 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 [ + - ]: 2905 : if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE)
57 : : conservative_estimate = true;
58 [ + + ]: 2905 : else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL)
59 : 4 : conservative_estimate = false;
60 : :
61 : 2905 : const auto fee_estimation_res = wallet.chain().getFeeRateEstimate(target, conservative_estimate);
62 : 2905 : const FeeRateEstimation& estimation{FeeRateEstimationRef(fee_estimation_res)};
63 [ + + ]: 2905 : CFeeRate fee_rate{estimation.feerate};
64 : 2905 : FeeReason fee_reason{FeeReason::FEE_RATE_ESTIMATOR};
65 : : // Only fee rate estimator results have a returned target.
66 [ + + ]: 2905 : std::optional<int> returned_target{estimation.returned_target};
67 [ + + ]: 2905 : if (fee_rate == CFeeRate(0)) {
68 : : // if we don't have enough data for getFeeRateEstimate, then use fallback fee
69 : 1591 : fee_rate = wallet.m_fallback_fee;
70 : 1591 : fee_reason = FeeReason::FALLBACK;
71 [ + + ]: 1591 : returned_target = std::nullopt;
72 : : // directly return if fallback fee is disabled (feerate 0 == disabled)
73 [ + + ]: 1591 : 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 [ + - ]: 2892 : CFeeRate min_mempool_feerate = wallet.chain().mempoolMinFee();
78 [ - + ]: 2892 : if (fee_rate < min_mempool_feerate) {
79 : 0 : fee_rate = min_mempool_feerate;
80 : 0 : fee_reason = FeeReason::MEMPOOL_MIN;
81 [ # # ]: 0 : returned_target = std::nullopt;
82 : : }
83 : :
84 [ + - ]: 2892 : CFeeRate required_feerate = GetRequiredFeeRate(wallet);
85 [ + + ]: 2892 : if (required_feerate > fee_rate) return {required_feerate, FeeReason::REQUIRED, std::nullopt};
86 : :
87 : 1555 : return {fee_rate, fee_reason, returned_target};
88 : 2905 : }
89 : :
90 : 3747 : CFeeRate GetDiscardRate(const CWallet& wallet)
91 : : {
92 : 3747 : unsigned int highest_target = wallet.chain().maximumFeeEstimationTargetBlocks();
93 : 3747 : const auto res = wallet.chain().getFeeRateEstimate(highest_target, /*conservative=*/false);
94 [ + + + - ]: 5516 : 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 [ + + ]: 3747 : 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 [ + - ]: 3747 : discard_rate = std::max(discard_rate, wallet.chain().relayDustFee());
99 : 3747 : return discard_rate;
100 : 3747 : }
101 : : } // namespace wallet
|