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