LCOV - code coverage report
Current view: top level - src/wallet - fees.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 38 40 95.0 %
Date: 2020-09-26 01:30:44 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2019 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 <wallet/coincontrol.h>
       9             : #include <wallet/wallet.h>
      10             : 
      11             : 
      12           9 : CAmount GetRequiredFee(const CWallet& wallet, unsigned int nTxBytes)
      13             : {
      14           9 :     return GetRequiredFeeRate(wallet).GetFee(nTxBytes);
      15             : }
      16             : 
      17             : 
      18        3162 : CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinControl& coin_control, FeeCalculation* feeCalc)
      19             : {
      20        3162 :     return GetMinimumFeeRate(wallet, coin_control, feeCalc).GetFee(nTxBytes);
      21             : }
      22             : 
      23       22158 : CFeeRate GetRequiredFeeRate(const CWallet& wallet)
      24             : {
      25       22158 :     return std::max(wallet.m_min_fee, wallet.chain().relayMinFee());
      26             : }
      27             : 
      28       22418 : CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, FeeCalculation* feeCalc)
      29             : {
      30             :     /* User control of how to calculate fee uses the following parameter precedence:
      31             :        1. coin_control.m_feerate
      32             :        2. coin_control.m_confirm_target
      33             :        3. m_pay_tx_fee (user-set member variable of wallet)
      34             :        4. m_confirm_target (user-set member variable of wallet)
      35             :        The first parameter that is set is used.
      36             :     */
      37       22418 :     CFeeRate feerate_needed;
      38       22418 :     if (coin_control.m_feerate) { // 1.
      39         693 :         feerate_needed = *(coin_control.m_feerate);
      40         693 :         if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
      41             :         // Allow to override automatic min/max check over coin control instance
      42         693 :         if (coin_control.fOverrideFeeRate) return feerate_needed;
      43             :     }
      44       21725 :     else if (!coin_control.m_confirm_target && wallet.m_pay_tx_fee != CFeeRate(0)) { // 3. TODO: remove magic value of 0 for wallet member m_pay_tx_fee
      45         491 :         feerate_needed = wallet.m_pay_tx_fee;
      46         491 :         if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
      47             :     }
      48             :     else { // 2. or 4.
      49             :         // We will use smart fee estimation
      50       21234 :         unsigned int target = coin_control.m_confirm_target ? *coin_control.m_confirm_target : wallet.m_confirm_target;
      51             :         // By default estimates are economical iff we are signaling opt-in-RBF
      52       21234 :         bool conservative_estimate = !coin_control.m_signal_bip125_rbf.get_value_or(wallet.m_signal_rbf);
      53             :         // Allow to override the default fee estimate mode over the CoinControl instance
      54       21234 :         if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
      55       21234 :         else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
      56             : 
      57       21234 :         feerate_needed = wallet.chain().estimateSmartFee(target, conservative_estimate, feeCalc);
      58       21234 :         if (feerate_needed == CFeeRate(0)) {
      59             :             // if we don't have enough data for estimateSmartFee, then use fallback fee
      60       12431 :             feerate_needed = wallet.m_fallback_fee;
      61       12431 :             if (feeCalc) feeCalc->reason = FeeReason::FALLBACK;
      62             : 
      63             :             // directly return if fallback fee is disabled (feerate 0 == disabled)
      64       12431 :             if (wallet.m_fallback_fee == CFeeRate(0)) return feerate_needed;
      65             :         }
      66             :         // Obey mempool min fee when using smart fee estimation
      67       21095 :         CFeeRate min_mempool_feerate = wallet.chain().mempoolMinFee();
      68       21095 :         if (feerate_needed < min_mempool_feerate) {
      69           0 :             feerate_needed = min_mempool_feerate;
      70           0 :             if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
      71             :         }
      72       21095 :     }
      73             : 
      74             :     // prevent user from paying a fee below the required fee rate
      75       22149 :     CFeeRate required_feerate = GetRequiredFeeRate(wallet);
      76       22149 :     if (required_feerate > feerate_needed) {
      77          38 :         feerate_needed = required_feerate;
      78          38 :         if (feeCalc) feeCalc->reason = FeeReason::REQUIRED;
      79             :     }
      80             :     return feerate_needed;
      81       22418 : }
      82             : 
      83       19173 : CFeeRate GetDiscardRate(const CWallet& wallet)
      84             : {
      85       19173 :     unsigned int highest_target = wallet.chain().estimateMaxBlocks();
      86       19173 :     CFeeRate discard_rate = wallet.chain().estimateSmartFee(highest_target, false /* conservative */);
      87             :     // Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
      88       19173 :     discard_rate = (discard_rate == CFeeRate(0)) ? wallet.m_discard_rate : std::min(discard_rate, wallet.m_discard_rate);
      89             :     // Discard rate must be at least dustRelayFee
      90       19173 :     discard_rate = std::max(discard_rate, wallet.chain().relayDustFee());
      91             :     return discard_rate;
      92       19173 : }

Generated by: LCOV version 1.15