mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-20 07:09:15 +01:00
fees: Always round up fee calculated from a feerate
When calculating the fee for a given tx size from a fee rate, we should always round up to the next satoshi. Otherwise, if we round down (via truncation), the calculated fee may result in a fee with a feerate slightly less than targeted. This is particularly important for coin selection as a slightly lower feerate than expected can result in a variety of issues.
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
|
||||
#include <tinyformat.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
CFeeRate::CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes)
|
||||
{
|
||||
const int64_t nSize{num_bytes};
|
||||
@@ -22,7 +24,9 @@ CAmount CFeeRate::GetFee(uint32_t num_bytes) const
|
||||
{
|
||||
const int64_t nSize{num_bytes};
|
||||
|
||||
CAmount nFee = nSatoshisPerK * nSize / 1000;
|
||||
// Be explicit that we're converting from a double to int64_t (CAmount) here.
|
||||
// We've previously had issues with the silent double->int64_t conversion.
|
||||
CAmount nFee{static_cast<CAmount>(std::ceil(nSatoshisPerK * nSize / 1000.0))};
|
||||
|
||||
if (nFee == 0 && nSize != 0) {
|
||||
if (nSatoshisPerK > 0) nFee = CAmount(1);
|
||||
|
||||
Reference in New Issue
Block a user