Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
policy
feerate.cpp
Go to the documentation of this file.
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
CFeeRate::CFeeRate
(
const
CAmount
& nFeePaid, int32_t virtual_bytes)
13
{
14
if
(virtual_bytes > 0) {
15
m_feerate
=
FeePerVSize
(nFeePaid, virtual_bytes);
16
}
else
{
17
m_feerate
=
FeePerVSize
();
18
}
19
}
20
21
CAmount
CFeeRate::GetFee
(int32_t virtual_bytes)
const
22
{
23
Assume
(virtual_bytes >= 0);
24
if
(
m_feerate
.
IsEmpty
()) {
return
CAmount
(0);}
25
CAmount
nFee =
CAmount
(
m_feerate
.
EvaluateFeeUp
(virtual_bytes));
26
if
(nFee == 0 && virtual_bytes != 0 &&
m_feerate
.
fee
< 0)
return
CAmount
(-1);
27
return
nFee;
28
}
29
30
std::string
CFeeRate::ToString
(
FeeRateFormat
fee_rate_format)
const
31
{
32
const
CAmount
feerate_per_kvb{
GetFeePerK
()};
33
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
Assert
(divisor > 0);
35
const
char
* sign{fee_rate < 0 ?
"-"
:
""
};
36
const
CAmount
quotient{std::abs(fee_rate / divisor)};
37
const
CAmount
remainder{std::abs(fee_rate % divisor)};
38
return
strprintf
(
"%s%d.%0*d %s/%s"
, sign, quotient, decimals, remainder, currency_unit, size_unit);
39
};
40
switch
(fee_rate_format) {
41
case
FeeRateFormat::BTC_KVB
:
return
format_feerate(feerate_per_kvb,
COIN
,
/*decimals=*/
8,
CURRENCY_UNIT
,
"kvB"
);
42
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
assert
(
false
);
45
}
amount.h
CAmount
int64_t CAmount
Amount in satoshis (Can be negative).
Definition
amount.h:12
COIN
constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition
amount.h:15
Assert
#define Assert(val)
Identity function.
Definition
check.h:116
Assume
#define Assume(val)
Assume is the identity function.
Definition
check.h:128
CFeeRate::ToString
std::string ToString(FeeRateFormat fee_rate_format=FeeRateFormat::BTC_KVB) const
Definition
feerate.cpp:30
CFeeRate::CFeeRate
CFeeRate()=default
Fee rate of 0 satoshis per 0 vB.
CFeeRate::m_feerate
FeePerVSize m_feerate
Fee rate in sats/vB (satoshis per N virtualbytes).
Definition
feerate.h:35
CFeeRate::GetFeePerK
CAmount GetFeePerK() const
Return the fee in satoshis for a vsize of 1000 vbytes.
Definition
feerate.h:71
CFeeRate::GetFee
CAmount GetFee(int32_t virtual_bytes) const
Return the fee in satoshis for the given vsize in vbytes.
Definition
feerate.cpp:21
FeePerVSize
FeePerUnit< VSizeTag > FeePerVSize
Definition
feefrac.h:204
feerate.h
CURRENCY_ATOM
const std::string CURRENCY_ATOM
Definition
feerate.h:20
FeeRateFormat
FeeRateFormat
Definition
feerate.h:22
FeeRateFormat::BTC_KVB
@ BTC_KVB
Use BTC/kvB fee rate unit.
Definition
feerate.h:23
FeeRateFormat::SAT_VB
@ SAT_VB
Use sat/vB fee rate unit.
Definition
feerate.h:24
CURRENCY_UNIT
const std::string CURRENCY_UNIT
Definition
feerate.h:19
FeeFrac::fee
int64_t fee
Definition
feefrac.h:88
FeeFrac::EvaluateFeeUp
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:175
FeeFrac::IsEmpty
bool IsEmpty() const noexcept
Check if this is empty (size and fee are 0).
Definition
feefrac.h:101
tinyformat.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition
tinyformat.h:1172
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.18.0