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 : : #include <consensus/amount.h>
7 : : #include <policy/feerate.h>
8 : : #include <tinyformat.h>
9 : :
10 : : #include <cstdlib>
11 : :
12 [ + + ]: 143276 : CFeeRate::CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes)
13 : : {
14 [ + + ]: 143276 : if (virtual_bytes > 0) {
15 : 143272 : m_feerate = FeePerVSize(nFeePaid, virtual_bytes);
16 : : } else {
17 : 4 : m_feerate = FeePerVSize();
18 : : }
19 : 143276 : }
20 : :
21 : 1916660 : CAmount CFeeRate::GetFee(int32_t virtual_bytes) const
22 : : {
23 [ + - ]: 1916660 : Assume(virtual_bytes >= 0);
24 [ + - ]: 1916660 : if (m_feerate.IsEmpty()) { return CAmount(0);}
25 : 1916660 : CAmount nFee = CAmount(m_feerate.EvaluateFeeUp(virtual_bytes));
26 [ + + + + ]: 1916660 : if (nFee == 0 && virtual_bytes != 0 && m_feerate.fee < 0) return CAmount(-1);
27 : : return nFee;
28 : : }
29 : :
30 : 3228 : std::string CFeeRate::ToString(FeeRateFormat fee_rate_format) const
31 : : {
32 [ + + - ]: 3228 : const CAmount feerate_per_kvb{GetFeePerK()};
33 : 6456 : const auto format_feerate = [](const CAmount fee_rate, const CAmount divisor, const int decimals, const std::string& currency_unit, const std::string& size_unit) {
34 [ - + ]: 3228 : Assert(divisor > 0);
35 [ + + ]: 3228 : const char* sign{fee_rate < 0 ? "-" : ""};
36 : 3228 : const CAmount quotient{std::abs(fee_rate / divisor)};
37 : 3228 : const CAmount remainder{std::abs(fee_rate % divisor)};
38 : 3228 : return strprintf("%s%d.%0*d %s/%s", sign, quotient, decimals, remainder, currency_unit, size_unit);
39 : : };
40 [ + + - ]: 3228 : switch (fee_rate_format) {
41 [ + - ]: 2442 : case FeeRateFormat::BTC_KVB: return format_feerate(feerate_per_kvb, COIN, /*decimals=*/8, CURRENCY_UNIT, "kvB");
42 [ + - ]: 4014 : case FeeRateFormat::SAT_VB: return format_feerate(feerate_per_kvb, 1000, /*decimals=*/3, CURRENCY_ATOM, "vB");
43 : : } // no default case, so the compiler can warn about missing cases
44 : 0 : assert(false);
45 : : }
|