Bitcoin ABC 0.34.2
P2P Digital Currency
feerate.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Copyright (c) 2017-2019 The Bitcoin developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#include <feerate.h>
8
9#include <consensus/amount.h>
10#include <tinyformat.h>
11#include <util/feefrac.h>
12
13#include <cstdint>
14
15CFeeRate::CFeeRate(const Amount nFeePaid, int32_t virtual_bytes) {
16 if (virtual_bytes > 0) {
17 m_feerate = FeePerVSize(nFeePaid / SATOSHI, virtual_bytes);
18 } else {
20 }
21}
22
23Amount CFeeRate::GetFee(int32_t virtual_bytes) const {
24 Assume(virtual_bytes >= 0);
25 if (m_feerate.IsEmpty()) {
26 return Amount::zero();
27 }
28
29 // Ensure fee is rounded up
30 auto nFee = Amount(m_feerate.EvaluateFeeUp(virtual_bytes) * SATOSHI);
31
32 // Special case introduced in core#7796 for symetry around 0.
33 // A fee had to be at least + or -1 satoshi for any non-zero transaction
34 // size. The symetry was broken when the code was switched to always round
35 // up fees (core#22949), but the special case was kept. We keep it to
36 // make later backports easier.
37 if (nFee == Amount::zero() && virtual_bytes != 0 && m_feerate.fee < 0) {
38 return -SATOSHI;
39 }
40 return nFee;
41}
42
43std::string CFeeRate::ToString() const {
44 const Amount feerate_per_kvb = GetFeePerK();
45 const auto &currency = Currency::get();
46 return strprintf("%d.%0*d %s/kB", feerate_per_kvb / currency.baseunit,
47 currency.decimals,
48 (feerate_per_kvb % currency.baseunit) / currency.subunit,
50}
static constexpr Amount SATOSHI
Definition: amount.h:153
#define Assume(val)
Assume is the identity function.
Definition: check.h:100
Amount GetFee(int32_t virtual_bytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:23
CFeeRate()=delete
std::string ToString() const
Definition: feerate.cpp:43
FeePerVSize m_feerate
Definition: feerate.h:27
Amount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:55
FeePerUnit< VSizeTag > FeePerVSize
Definition: feefrac.h:254
Definition: amount.h:23
static constexpr Amount zero() noexcept
Definition: amount.h:36
static const Currency & get()
Definition: amount.cpp:25
static std::string getTicker()
Definition: amount.h:163
int64_t fee
Definition: feefrac.h:118
int64_t EvaluateFeeUp(int32_t at_size) const noexcept
Compute the fee for a given size at_size using this object's feerate, rounding up.
Definition: feefrac.h:233
bool IsEmpty() const noexcept
Check if this is empty (size and fee are 0).
Definition: feefrac.h:131
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1203