Inside Bitcoin
Opening the selected file…
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#ifndef BITCOIN_POLICY_FEERATE_H7#define BITCOIN_POLICY_FEERATE_H9#include <consensus/amount.h>10#include <serialize.h>11#include <util/feefrac.h>12#include <util/fees.h>15#include <cstdint>16#include <string>17#include <type_traits>19inline const std::string CURRENCY_UNIT = "BTC"; // One formatted unit20inline const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit22enum class FeeRateFormat {23BTC_KVB, //!< Use BTC/kvB fee rate unit24SAT_VB, //!< Use sat/vB fee rate unit25};27/**28* Fee rate in satoshis per virtualbyte: CAmount / vB29* the feerate is represented internally as FeeFrac30*/31class CFeeRate32{33private:34/** Fee rate in sats/vB (satoshis per N virtualbytes) */35FeePerVSize m_feerate;37public:38/** Fee rate of 0 satoshis per 0 vB */39CFeeRate() = default;40template<std::integral I> // Disallow silent float -> int conversion41explicit constexpr CFeeRate(const I m_feerate_kvb) : m_feerate(FeePerVSize(m_feerate_kvb, 1000)) {}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*/48CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes);50/**51* Construct from a fee rate expressed as FeePerVSize.52*53* Lossless: CFeeRate is internally a FeePerVSize, so the exact54* fee/vsize fraction is preserved. A feerate whose size is less than55* or equal to 0 results in 0 fee rate per 0 size.56*/57explicit CFeeRate(const FeePerVSize& feerate) : m_feerate{feerate.size > 0 ? feerate : FeePerVSize{}} {}59/**60* Return the fee in satoshis for the given vsize in vbytes.61* If the calculated fee would have fractional satoshis, then the62* returned fee will always be rounded up to the nearest satoshi.63*/64CAmount GetFee(int32_t virtual_bytes) const;66FeePerVSize GetFeePerVSize() const { return m_feerate; }68/**69* Return the fee in satoshis for a vsize of 1000 vbytes70*/71CAmount GetFeePerK() const { return CAmount(m_feerate.EvaluateFeeDown(1000)); }72friend std::strong_ordering operator<=>(const CFeeRate& a, const CFeeRate& b) noexcept73{74return ByRatio{a.m_feerate} <=> ByRatio{b.m_feerate};75}76friend bool operator==(const CFeeRate& a, const CFeeRate& b) noexcept77{78return ByRatio{a.m_feerate} == ByRatio{b.m_feerate};79}80CFeeRate& operator+=(const CFeeRate& a) {81m_feerate = FeePerVSize(GetFeePerK() + a.GetFeePerK(), 1000);82return *this;83}84std::string ToString(FeeRateFormat fee_rate_format = FeeRateFormat::BTC_KVB) const;85friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }86friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }88SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.m_feerate.fee, obj.m_feerate.size); }89};91#endif // BITCOIN_POLICY_FEERATE_H