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 : :
13 : :
14 : : #include <cstdint>
15 : : #include <string>
16 : : #include <type_traits>
17 : :
18 : : const std::string CURRENCY_UNIT = "BTC"; // One formatted unit
19 : : const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit
20 : :
21 : : /* Used to determine type of fee estimation requested */
22 : : enum class FeeEstimateMode {
23 : : UNSET, //!< Use default settings based on other criteria
24 : : ECONOMICAL, //!< Force estimateSmartFee to use non-conservative estimates
25 : : CONSERVATIVE, //!< Force estimateSmartFee to use conservative estimates
26 : : BTC_KVB, //!< Use BTC/kvB fee rate unit
27 : : SAT_VB, //!< Use sat/vB fee rate unit
28 : : };
29 : :
30 : : /**
31 : : * Fee rate in satoshis per virtualbyte: CAmount / vB
32 : : * the feerate is represented internally as FeeFrac
33 : : */
34 : : class CFeeRate
35 : : {
36 : : private:
37 : : /** Fee rate in sats/vB (satoshis per N virtualbytes) */
38 : : FeePerVSize m_feerate;
39 : :
40 : : public:
41 : : /** Fee rate of 0 satoshis per 0 vB */
42 : 3484 : CFeeRate() = default;
43 : : template<std::integral I> // Disallow silent float -> int conversion
44 [ + + - - : 1104747 : explicit CFeeRate(const I m_feerate_kvb) : m_feerate(FeePerVSize(m_feerate_kvb, 1000)) {}
- - ][ + +
+ + + + +
- ][ + + +
+ + + +
+ ]
[ + + + + ]
[ + - + -
+ - + - +
- + - ][ +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ + + - +
- - - ][ +
- + - + -
+ - + - +
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
45 : :
46 : : /**
47 : : * Construct a fee rate from a fee in satoshis and a vsize in vB.
48 : : *
49 : : * Passing any virtual_bytes less than or equal to 0 will result in 0 fee rate per 0 size.
50 : : */
51 : : CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes);
52 : :
53 : : /**
54 : : * Return the fee in satoshis for the given vsize in vbytes.
55 : : * If the calculated fee would have fractional satoshis, then the
56 : : * returned fee will always be rounded up to the nearest satoshi.
57 : : */
58 : : CAmount GetFee(int32_t virtual_bytes) const;
59 : :
60 : : /**
61 : : * Return the fee in satoshis for a vsize of 1000 vbytes
62 : : */
63 [ + + # # ]: 651084 : CAmount GetFeePerK() const { return CAmount(m_feerate.EvaluateFeeDown(1000)); }
[ + + + + ]
[ - + + -
+ - + - ]
[ + - + -
+ - + - +
- + - + -
+ - ][ + -
+ - + - +
- + - + -
+ - + - -
- - - + -
# # ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
64 : 53561 : friend std::weak_ordering operator<=>(const CFeeRate& a, const CFeeRate& b) noexcept
65 : : {
66 [ + + + + ]: 53529 : return FeeRateCompare(a.m_feerate, b.m_feerate);
[ + + + +
+ + + + ]
[ + + # #
# # # # ]
[ + + + +
+ + ][ + -
+ - + - +
- + - + +
+ + + + +
+ + + +
+ ][ + - +
- + - + -
+ - + - +
- - + + -
- + + - -
+ + - -
+ ]
67 : : }
68 : 11169 : friend bool operator==(const CFeeRate& a, const CFeeRate& b) noexcept
69 : : {
70 [ + + ]: 11169 : return FeeRateCompare(a.m_feerate, b.m_feerate) == std::weak_ordering::equivalent;
71 : : }
72 : 236 : CFeeRate& operator+=(const CFeeRate& a) {
73 : 236 : m_feerate = FeePerVSize(GetFeePerK() + a.GetFeePerK(), 1000);
74 : 236 : return *this;
75 : : }
76 [ + - + - : 1219 : std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const;
+ - ][ + -
+ - # # ]
[ + - ]
77 : 7 : friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }
78 : 5672 : friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }
79 : :
80 : : SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.m_feerate.fee, obj.m_feerate.size); }
81 : : };
82 : :
83 : : #endif // BITCOIN_POLICY_FEERATE_H
|