Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <consensus/amount.h>7#include <policy/feerate.h>8#include <tinyformat.h>10#include <cstdlib>12CFeeRate::CFeeRate(const CAmount& nFeePaid, int32_t virtual_bytes)13{14if (virtual_bytes > 0) {15m_feerate = FeePerVSize(nFeePaid, virtual_bytes);16} else {17m_feerate = FeePerVSize();18}19}21CAmount CFeeRate::GetFee(int32_t virtual_bytes) const22{23Assume(virtual_bytes >= 0);24if (m_feerate.IsEmpty()) { return CAmount(0);}25CAmount nFee = CAmount(m_feerate.EvaluateFeeUp(virtual_bytes));26if (nFee == 0 && virtual_bytes != 0 && m_feerate.fee < 0) return CAmount(-1);27return nFee;28}30std::string CFeeRate::ToString(FeeRateFormat fee_rate_format) const31{32const CAmount feerate_per_kvb{GetFeePerK()};33const auto format_feerate = [](const CAmount fee_rate, const CAmount divisor, const int decimals, const std::string& currency_unit, const std::string& size_unit) {34Assert(divisor > 0);35const char* sign{fee_rate < 0 ? "-" : ""};36const CAmount quotient{std::abs(fee_rate / divisor)};37const CAmount remainder{std::abs(fee_rate % divisor)};38return strprintf("%s%d.%0*d %s/%s", sign, quotient, decimals, remainder, currency_unit, size_unit);39};40switch (fee_rate_format) {41case FeeRateFormat::BTC_KVB: return format_feerate(feerate_per_kvb, COIN, /*decimals=*/8, CURRENCY_UNIT, "kvB");42case 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 cases44assert(false);45}