Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <wallet/fees.h>8#include <policy/feerate.h>9#include <util/fees.h>10#include <wallet/coincontrol.h>11#include <wallet/wallet.h>13#include <optional>15namespace wallet {16CAmount GetRequiredFee(const CWallet& wallet, unsigned int nTxBytes)17{18return GetRequiredFeeRate(wallet).GetFee(static_cast<int32_t>(nTxBytes));19}22CAmount GetMinimumFee(const MinimumFeeRateResult& min_fee_rate, unsigned int nTxBytes)23{24return min_fee_rate.fee_rate.GetFee(static_cast<int32_t>(nTxBytes));25}27CFeeRate GetRequiredFeeRate(const CWallet& wallet)28{29return std::max(wallet.m_min_fee, wallet.chain().relayMinFee());30}32MinimumFeeRateResult GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control)33{34/* User control of how to calculate fee uses the following parameter precedence:351. coin_control.m_feerate362. coin_control.m_confirm_target373. m_confirm_target (user-set member variable of wallet)38The first parameter that is set is used.39*/40if (coin_control.m_feerate) { // 1.41CFeeRate fee_rate{*coin_control.m_feerate};42// Allow to override automatic min/max check over coin control instance43if (coin_control.fOverrideFeeRate) return {fee_rate, FeeReason::USER_SPECIFIED, std::nullopt};45CFeeRate required_feerate = GetRequiredFeeRate(wallet);46if (required_feerate > fee_rate) return {required_feerate, FeeReason::REQUIRED, std::nullopt};48return {fee_rate, FeeReason::USER_SPECIFIED, std::nullopt};49}51// We will use smart fee estimation52unsigned 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-RBF54bool 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 instance56if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE)57conservative_estimate = true;58else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL)59conservative_estimate = false;61const auto fee_estimation_res = wallet.chain().getFeeRateEstimate(target, conservative_estimate);62const FeeRateEstimation& estimation{FeeRateEstimationRef(fee_estimation_res)};63CFeeRate fee_rate{estimation.feerate};64FeeReason fee_reason{FeeReason::FEE_RATE_ESTIMATOR};65// Only fee rate estimator results have a returned target.66std::optional<int> returned_target{estimation.returned_target};67if (fee_rate == CFeeRate(0)) {68// if we don't have enough data for getFeeRateEstimate, then use fallback fee69fee_rate = wallet.m_fallback_fee;70fee_reason = FeeReason::FALLBACK;71returned_target = std::nullopt;72// directly return if fallback fee is disabled (feerate 0 == disabled)73if (wallet.m_fallback_fee == CFeeRate(0)) return {fee_rate, FeeReason::FALLBACK, std::nullopt};74}76// Obey mempool min fee when using smart fee estimation or fallback fee77CFeeRate min_mempool_feerate = wallet.chain().mempoolMinFee();78if (fee_rate < min_mempool_feerate) {79fee_rate = min_mempool_feerate;80fee_reason = FeeReason::MEMPOOL_MIN;81returned_target = std::nullopt;82}84CFeeRate required_feerate = GetRequiredFeeRate(wallet);85if (required_feerate > fee_rate) return {required_feerate, FeeReason::REQUIRED, std::nullopt};87return {fee_rate, fee_reason, returned_target};88}90CFeeRate GetDiscardRate(const CWallet& wallet)91{92unsigned int highest_target = wallet.chain().maximumFeeEstimationTargetBlocks();93const auto res = wallet.chain().getFeeRateEstimate(highest_target, /*conservative=*/false);94auto 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 estimate96discard_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 feerate98discard_rate = std::max(discard_rate, wallet.chain().relayDustFee());99return discard_rate;100}101} // namespace wallet