LCOV - code coverage report
Current view: top level - src/policy - feerate.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 94.1 % 17 16
Test Date: 2024-12-04 04:00:22 Functions: 100.0 % 3 3
Branches: 70.0 % 10 7

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 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 <cmath>
      11                 :             : 
      12                 :      505102 : CFeeRate::CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes)
      13                 :             : {
      14                 :      505102 :     const int64_t nSize{num_bytes};
      15                 :             : 
      16         [ +  - ]:      505102 :     if (nSize > 0) {
      17                 :      505102 :         nSatoshisPerK = nFeePaid * 1000 / nSize;
      18                 :             :     } else {
      19                 :           0 :         nSatoshisPerK = 0;
      20                 :             :     }
      21                 :      505102 : }
      22                 :             : 
      23                 :    18889588 : CAmount CFeeRate::GetFee(uint32_t num_bytes) const
      24                 :             : {
      25                 :    18889588 :     const int64_t nSize{num_bytes};
      26                 :             : 
      27                 :             :     // Be explicit that we're converting from a double to int64_t (CAmount) here.
      28                 :             :     // We've previously had issues with the silent double->int64_t conversion.
      29                 :    18889588 :     CAmount nFee{static_cast<CAmount>(std::ceil(nSatoshisPerK * nSize / 1000.0))};
      30                 :             : 
      31         [ +  + ]:    18889588 :     if (nFee == 0 && nSize != 0) {
      32         [ -  + ]:      507061 :         if (nSatoshisPerK > 0) nFee = CAmount(1);
      33         [ -  + ]:      507061 :         if (nSatoshisPerK < 0) nFee = CAmount(-1);
      34                 :             :     }
      35                 :             : 
      36                 :    18889588 :     return nFee;
      37                 :             : }
      38                 :             : 
      39                 :      120753 : std::string CFeeRate::ToString(const FeeEstimateMode& fee_estimate_mode) const
      40                 :             : {
      41         [ +  + ]:      120753 :     switch (fee_estimate_mode) {
      42                 :        9426 :     case FeeEstimateMode::SAT_VB: return strprintf("%d.%03d %s/vB", nSatoshisPerK / 1000, nSatoshisPerK % 1000, CURRENCY_ATOM);
      43                 :      111327 :     default:                      return strprintf("%d.%08d %s/kvB", nSatoshisPerK / COIN, nSatoshisPerK % COIN, CURRENCY_UNIT);
      44                 :             :     }
      45                 :             : }
        

Generated by: LCOV version 2.0-1