/bitcoin/src/policy/feerate.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-2022 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 <cmath> |
11 | | |
12 | | CFeeRate::CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes) |
13 | 74.3k | { |
14 | 74.3k | const int64_t nSize{num_bytes}; |
15 | | |
16 | 74.3k | if (nSize > 0) { Branch (16:9): [True: 74.3k, False: 0]
|
17 | 74.3k | nSatoshisPerK = nFeePaid * 1000 / nSize; |
18 | 74.3k | } else { |
19 | 0 | nSatoshisPerK = 0; |
20 | 0 | } |
21 | 74.3k | } |
22 | | |
23 | | CAmount CFeeRate::GetFee(uint32_t num_bytes) const |
24 | 771k | { |
25 | 771k | const int64_t nSize{num_bytes}; |
26 | | |
27 | | // Be explicit that we're converting from a double to int64_t (CAmount) here. |
28 | | // We've previously had issues with the silent double->int64_t conversion. |
29 | 771k | CAmount nFee{static_cast<CAmount>(std::ceil(nSatoshisPerK * nSize / 1000.0))}; |
30 | | |
31 | 771k | if (nFee == 0 && nSize != 0) { Branch (31:9): [True: 147k, False: 624k]
Branch (31:22): [True: 147k, False: 0]
|
32 | 147k | if (nSatoshisPerK > 0) nFee = CAmount(1); Branch (32:13): [True: 0, False: 147k]
|
33 | 147k | if (nSatoshisPerK < 0) nFee = CAmount(-1); Branch (33:13): [True: 0, False: 147k]
|
34 | 147k | } |
35 | | |
36 | 771k | return nFee; |
37 | 771k | } |
38 | | |
39 | | std::string CFeeRate::ToString(const FeeEstimateMode& fee_estimate_mode) const |
40 | 52.4k | { |
41 | 52.4k | switch (fee_estimate_mode) { |
42 | 44.3k | case FeeEstimateMode::SAT_VB: return strprintf("%d.%03d %s/vB", nSatoshisPerK / 1000, nSatoshisPerK % 1000, CURRENCY_ATOM); Branch (42:5): [True: 44.3k, False: 8.11k]
|
43 | 8.11k | default: return strprintf("%d.%08d %s/kvB", nSatoshisPerK / COIN, nSatoshisPerK % COIN, CURRENCY_UNIT); Branch (43:5): [True: 8.11k, False: 44.3k]
|
44 | 52.4k | } |
45 | 52.4k | } |