/tmp/bitcoin/src/policy/feerate.h
Line | Count | Source |
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 | | inline const std::string CURRENCY_UNIT = "BTC"; // One formatted unit |
20 | | inline 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 | 11.9k | CFeeRate() = default; |
40 | | template<std::integral I> // Disallow silent float -> int conversion |
41 | 1.14M | explicit constexpr CFeeRate(const I m_feerate_kvb) : m_feerate(FeePerVSize(m_feerate_kvb, 1000)) {}CFeeRate::CFeeRate<int>(int) Line | Count | Source | 41 | 510k | explicit constexpr CFeeRate(const I m_feerate_kvb) : m_feerate(FeePerVSize(m_feerate_kvb, 1000)) {} |
CFeeRate::CFeeRate<long>(long) Line | Count | Source | 41 | 166k | explicit constexpr CFeeRate(const I m_feerate_kvb) : m_feerate(FeePerVSize(m_feerate_kvb, 1000)) {} |
CFeeRate::CFeeRate<unsigned int>(unsigned int) Line | Count | Source | 41 | 105k | explicit constexpr CFeeRate(const I m_feerate_kvb) : m_feerate(FeePerVSize(m_feerate_kvb, 1000)) {} |
CFeeRate::CFeeRate<long long>(long long) Line | Count | Source | 41 | 361k | explicit constexpr 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 | | * Construct from a fee rate expressed as FeePerVSize. |
52 | | * |
53 | | * Lossless: CFeeRate is internally a FeePerVSize, so the exact |
54 | | * fee/vsize fraction is preserved. A feerate whose size is less than |
55 | | * or equal to 0 results in 0 fee rate per 0 size. |
56 | | */ |
57 | 9.08k | explicit CFeeRate(const FeePerVSize& feerate) : m_feerate{feerate.size > 0 ? feerate : FeePerVSize{}} {} |
58 | | |
59 | | /// Return the fee in satoshis for the given vsize in vbytes. (Rounds up) |
60 | | CAmount GetFee(int32_t virtual_bytes) const; |
61 | | |
62 | 85.0k | FeePerVSize GetFeePerVSize() const { return m_feerate; } |
63 | | |
64 | | /// Return the fee in satoshis for a vsize of 1000 vbytes. (Rounds down) |
65 | | CAmount GetFeePerK() const; |
66 | | |
67 | | friend std::strong_ordering operator<=>(const CFeeRate& a, const CFeeRate& b) noexcept |
68 | 84.1k | { |
69 | 84.1k | return ByRatio{a.m_feerate} <=> ByRatio{b.m_feerate}; |
70 | 84.1k | } |
71 | | friend bool operator==(const CFeeRate& a, const CFeeRate& b) noexcept |
72 | 16.0k | { |
73 | 16.0k | return ByRatio{a.m_feerate} == ByRatio{b.m_feerate}; |
74 | 16.0k | } |
75 | 232 | CFeeRate& operator+=(const CFeeRate& a) { |
76 | 232 | m_feerate = FeePerVSize(GetFeePerK() + a.GetFeePerK(), 1000); |
77 | 232 | return *this; |
78 | 232 | } |
79 | | std::string ToString(FeeRateFormat fee_rate_format = FeeRateFormat::BTC_KVB) const; |
80 | 7 | friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); } |
81 | 5.38k | friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); } |
82 | | |
83 | | SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.m_feerate.fee, obj.m_feerate.size); } |
84 | | }; |
85 | | |
86 | | #endif // BITCOIN_POLICY_FEERATE_H |