LCOV - code coverage report
Current view: top level - src/policy - feerate.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 96.0 % 25 24
Test Date: 2026-11-03 07:14:04 Functions: 100.0 % 5 5
Branches: 78.3 % 23 18

             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 <consensus/amount.h>
       7                 :             : #include <policy/feerate.h>
       8                 :             : #include <tinyformat.h>
       9                 :             : 
      10                 :             : #include <cstdlib>
      11                 :             : 
      12         [ +  + ]:      142257 : CFeeRate::CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes)
      13                 :             : {
      14         [ +  + ]:      142257 :     if (virtual_bytes > 0) {
      15                 :      142250 :         m_feerate = FeePerVSize(nFeePaid, virtual_bytes);
      16                 :             :     } else {
      17                 :           7 :         m_feerate = FeePerVSize();
      18                 :             :     }
      19                 :      142257 : }
      20                 :             : 
      21                 :      702923 : CAmount CFeeRate::GetFeePerK() const
      22                 :             : {
      23         [ +  + ]:      702923 :     if (m_feerate.IsEmpty()) { return CAmount(0); }
      24                 :      702920 :     return CAmount(m_feerate.EvaluateFeeDown(1000));
      25                 :             : }
      26                 :             : 
      27                 :     1906195 : CAmount CFeeRate::GetFee(int32_t virtual_bytes) const
      28                 :             : {
      29         [ +  - ]:     1906195 :     Assume(virtual_bytes >= 0);
      30         [ +  - ]:     1906195 :     if (m_feerate.IsEmpty()) { return CAmount(0);}
      31                 :     1906195 :     CAmount nFee = CAmount(m_feerate.EvaluateFeeUp(virtual_bytes));
      32   [ +  +  +  + ]:     1906195 :     if (nFee == 0 && virtual_bytes != 0 && m_feerate.fee < 0) return CAmount(-1);
      33                 :             :     return nFee;
      34                 :             : }
      35                 :             : 
      36                 :        3326 : std::string CFeeRate::ToString(FeeRateFormat fee_rate_format) const
      37                 :             : {
      38                 :        3326 :     const CAmount feerate_per_kvb{GetFeePerK()};
      39                 :        6652 :     const auto format_feerate = [](const CAmount fee_rate, const CAmount divisor, const int decimals, const std::string& currency_unit, const std::string& size_unit) {
      40                 :        3326 :         Assert(divisor > 0);
      41         [ +  + ]:        3326 :         const char* sign{fee_rate < 0 ? "-" : ""};
      42                 :        3326 :         const CAmount quotient{std::abs(fee_rate / divisor)};
      43                 :        3326 :         const CAmount remainder{std::abs(fee_rate % divisor)};
      44                 :        3326 :         return strprintf("%s%d.%0*d %s/%s", sign, quotient, decimals, remainder, currency_unit, size_unit);
      45                 :             :     };
      46      [ +  +  - ]:        3326 :     switch (fee_rate_format) {
      47         [ +  - ]:        2492 :     case FeeRateFormat::BTC_KVB: return format_feerate(feerate_per_kvb, COIN, /*decimals=*/8, CURRENCY_UNIT, "kvB");
      48         [ +  - ]:        4160 :     case FeeRateFormat::SAT_VB: return format_feerate(feerate_per_kvb, 1000, /*decimals=*/3, CURRENCY_ATOM, "vB");
      49                 :             :     } // no default case, so the compiler can warn about missing cases
      50                 :           0 :     assert(false);
      51                 :             : }
        

Generated by: LCOV version 2.0-1