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 : 15 : CFeeRate() = default;
43 : : template<std::integral I> // Disallow silent float -> int conversion
44 [ + + # # : 352664 : 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 : : * param@[in] nFeePaid The fee paid by a transaction, in satoshis
50 : : * param@[in] virtual_bytes The vsize of a transaction, in vbytes
51 : : *
52 : : * Passing any virtual_bytes less than or equal to 0 will result in 0 fee rate per 0 size.
53 : : */
54 : : CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes);
55 : :
56 : : /**
57 : : * Return the fee in satoshis for the given vsize in vbytes.
58 : : * If the calculated fee would have fractional satoshis, then the
59 : : * returned fee will always be rounded up to the nearest satoshi.
60 : : */
61 : : CAmount GetFee(int32_t virtual_bytes) const;
62 : :
63 : : /**
64 : : * Return the fee in satoshis for a vsize of 1000 vbytes
65 : : */
66 [ + + + + ]: 99469 : CAmount GetFeePerK() const { return CAmount(m_feerate.EvaluateFeeDown(1000)); }
[ + + # # ]
[ + - + -
+ - + - +
- + - + -
+ - - - -
- + - #
# ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ + - -
- - - - -
- - + - -
- + - ][ -
+ + - + -
+ - ]
67 : 1255 : friend std::weak_ordering operator<=>(const CFeeRate& a, const CFeeRate& b) noexcept
68 : : {
69 [ + + - + ]: 1252 : return FeeRateCompare(a.m_feerate, b.m_feerate);
[ + + # #
# # # # ]
[ # # # #
# # # # ]
[ + - + -
+ - + - +
- + - + -
- + + - -
+ + - - +
+ - - + ]
[ + - + -
+ - + - +
- + + + +
+ + + + +
+ + + # #
# # # # ]
[ - - - -
- + ]
70 : : }
71 : 95 : friend bool operator==(const CFeeRate& a, const CFeeRate& b) noexcept
72 : : {
73 [ - + ]: 95 : return FeeRateCompare(a.m_feerate, b.m_feerate) == std::weak_ordering::equivalent;
74 : : }
75 : 9 : CFeeRate& operator+=(const CFeeRate& a) {
76 : 9 : m_feerate = FeePerVSize(GetFeePerK() + a.GetFeePerK(), 1000);
77 : 9 : return *this;
78 : : }
79 [ # # # # : 14 : std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::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 : 131 : 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
|