LCOV - code coverage report
Current view: top level - src/policy - feerate.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 13 13
Test Date: 2026-02-25 05:21:33 Functions: 100.0 % 4 4
Branches: 34.3 % 651 223

             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                 :             : #ifndef BITCOIN_POLICY_FEERATE_H
       7                 :             : #define BITCOIN_POLICY_FEERATE_H
       8                 :             : 
       9                 :             : #include <consensus/amount.h>
      10                 :             : #include <serialize.h>
      11                 :             : #include <util/feefrac.h>
      12                 :             : #include <util/fees.h>
      13                 :             : 
      14                 :             : 
      15                 :             : #include <cstdint>
      16                 :             : #include <string>
      17                 :             : #include <type_traits>
      18                 :             : 
      19                 :             : const std::string CURRENCY_UNIT = "BTC"; // One formatted unit
      20                 :             : const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit
      21                 :             : 
      22                 :             : enum class FeeRateFormat {
      23                 :             :     BTC_KVB, //!< Use BTC/kvB fee rate unit
      24                 :             :     SAT_VB,  //!< Use sat/vB fee rate unit
      25                 :             : };
      26                 :             : 
      27                 :             : /**
      28                 :             :  * Fee rate in satoshis per virtualbyte: CAmount / vB
      29                 :             :  * the feerate is represented internally as FeeFrac
      30                 :             :  */
      31                 :             : class CFeeRate
      32                 :             : {
      33                 :             : private:
      34                 :             :     /** Fee rate in sats/vB (satoshis per N virtualbytes) */
      35                 :             :     FeePerVSize m_feerate;
      36                 :             : 
      37                 :             : public:
      38                 :             :     /** Fee rate of 0 satoshis per 0 vB */
      39                 :          15 :     CFeeRate() = default;
      40                 :             :     template<std::integral I> // Disallow silent float -> int conversion
      41 [ +  + ][ +  -  :      352836 :     explicit CFeeRate(const I m_feerate_kvb) : m_feerate(FeePerVSize(m_feerate_kvb, 1000)) {}
          +  -  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  +  +  
             -  +  -  -  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  -  +  
                -  +  - ]
      42                 :             : 
      43                 :             :     /**
      44                 :             :      * Construct a fee rate from a fee in satoshis and a vsize in vB.
      45                 :             :      *
      46                 :             :      * Passing any virtual_bytes less than or equal to 0 will result in 0 fee rate per 0 size.
      47                 :             :      */
      48                 :             :     CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes);
      49                 :             : 
      50                 :             :     /**
      51                 :             :      * Return the fee in satoshis for the given vsize in vbytes.
      52                 :             :      * If the calculated fee would have fractional satoshis, then the
      53                 :             :      * returned fee will always be rounded up to the nearest satoshi.
      54                 :             :      */
      55                 :             :     CAmount GetFee(int32_t virtual_bytes) const;
      56                 :             : 
      57         [ +  + ]:         141 :     FeePerVSize GetFeePerVSize() const { return m_feerate; }
      58                 :             : 
      59                 :             :     /**
      60                 :             :      * Return the fee in satoshis for a vsize of 1000 vbytes
      61                 :             :      */
      62      [ +  +  - ]:       98869 :     CAmount GetFeePerK() const { return CAmount(m_feerate.EvaluateFeeDown(1000)); }
           [ +  +  +  + ]
         [ +  + ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
          -  -  -  +  -  
           #  # ][ -  +  
          +  -  +  -  +  
           - ][ +  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
                -  +  - ]
      63                 :        1289 :     friend std::weak_ordering operator<=>(const CFeeRate& a, const CFeeRate& b) noexcept
      64                 :             :     {
      65   [ +  +  -  +  :        1286 :         return FeeRateCompare(a.m_feerate, b.m_feerate);
           #  # ][ -  -  
             -  -  -  + ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  +  +  +  
          +  +  +  +  +  
           +  +  + ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  -  +  +  
          -  -  +  +  -  
           -  + ][ +  +  
          #  #  #  #  #  
           # ][ #  #  #  
             #  #  #  #  
                      # ]
      66                 :             :     }
      67                 :          88 :     friend bool operator==(const CFeeRate& a, const CFeeRate& b) noexcept
      68                 :             :     {
      69         [ -  + ]:          88 :         return FeeRateCompare(a.m_feerate, b.m_feerate) == std::weak_ordering::equivalent;
      70                 :             :     }
      71                 :           9 :     CFeeRate& operator+=(const CFeeRate& a) {
      72                 :           9 :         m_feerate = FeePerVSize(GetFeePerK() + a.GetFeePerK(), 1000);
      73                 :           9 :         return *this;
      74                 :             :     }
      75                 :             :     std::string ToString(FeeRateFormat fee_rate_format = FeeRateFormat::BTC_KVB) const;
      76                 :           7 :     friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }
      77                 :         131 :     friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }
      78                 :             : 
      79                 :             :     SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.m_feerate.fee, obj.m_feerate.size); }
      80                 :             : };
      81                 :             : 
      82                 :             : #endif // BITCOIN_POLICY_FEERATE_H
        

Generated by: LCOV version 2.0-1