Skip to content

Commit c135549

Browse files
committed
refactor: fees: split fee rate format from fee estimate mode
- Introduce a `FeeRateFormat` enum and change `CFeeRate::ToString()` to use it for `BTC/kvB` vs `sat/vB` output formatting. - Handle all enum values, hence remove default case in `CFeeRate::ToString()` and `assert(False)` when a `FeeRateFormat` value is not handled. - Keep `FeeEstimateMode` focused on fee estimation behavior by removing fee rate format values from `FeeEstimateMode`. - Update all formatting call sites and tests to pass `FeeRateFormat` explicitly, separating fee rate format from fee-estimation mode selection.
1 parent 922ebf9 commit c135549

File tree

10 files changed

+24
-19
lines changed

10 files changed

+24
-19
lines changed

src/common/messages.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <common/messages.h>
7-
87
#include <common/types.h>
9-
#include <policy/fees/block_policy_estimator.h>
108
#include <node/types.h>
9+
#include <policy/fees/block_policy_estimator.h>
1110
#include <tinyformat.h>
11+
#include <util/fees.h>
1212
#include <util/strencodings.h>
1313
#include <util/string.h>
1414
#include <util/translation.h>
@@ -68,7 +68,6 @@ std::string FeeModeInfo(const std::pair<std::string, FeeEstimateMode>& mode, std
6868
"less responsive to short-term drops in the prevailing fee market. This mode\n"
6969
"potentially returns a higher fee rate estimate.\n", mode.first);
7070
default:
71-
// Other modes apart from the ones handled are fee rate units; they should not be clarified.
7271
assert(false);
7372
}
7473
}

src/policy/feerate.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ CAmount CFeeRate::GetFee(int32_t virtual_bytes) const
2626
return nFee;
2727
}
2828

29-
std::string CFeeRate::ToString(const FeeEstimateMode& fee_estimate_mode) const
29+
std::string CFeeRate::ToString(FeeRateFormat fee_rate_format) const
3030
{
31-
const CAmount feerate_per_kvb = GetFeePerK();
32-
switch (fee_estimate_mode) {
33-
case FeeEstimateMode::SAT_VB: return strprintf("%d.%03d %s/vB", feerate_per_kvb / 1000, feerate_per_kvb % 1000, CURRENCY_ATOM);
34-
default: return strprintf("%d.%08d %s/kvB", feerate_per_kvb / COIN, feerate_per_kvb % COIN, CURRENCY_UNIT);
35-
}
31+
const CAmount feerate_per_kvb{GetFeePerK()};
32+
switch (fee_rate_format) {
33+
case FeeRateFormat::BTC_KVB: return strprintf("%d.%08d %s/kvB", feerate_per_kvb / COIN, feerate_per_kvb % COIN, CURRENCY_UNIT);
34+
case FeeRateFormat::SAT_VB: return strprintf("%d.%03d %s/vB", feerate_per_kvb / 1000, feerate_per_kvb % 1000, CURRENCY_ATOM);
35+
} // no default case, so the compiler can warn about missing cases
36+
assert(false);
3637
}

src/policy/feerate.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
const std::string CURRENCY_UNIT = "BTC"; // One formatted unit
2020
const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit
21+
22+
enum class FeeRateFormat {
23+
BTC_KVB, //!< Use BTC/kvB fee rate unit
24+
SAT_VB, //!< Use sat/vB fee rate unit
25+
};
26+
2127
/**
2228
* Fee rate in satoshis per virtualbyte: CAmount / vB
2329
* the feerate is represented internally as FeeFrac
@@ -66,7 +72,7 @@ class CFeeRate
6672
m_feerate = FeePerVSize(GetFeePerK() + a.GetFeePerK(), 1000);
6773
return *this;
6874
}
69-
std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const;
75+
std::string ToString(FeeRateFormat fee_rate_format = FeeRateFormat::BTC_KVB) const;
7076
friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }
7177
friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }
7278

src/rpc/fees.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <rpc/util.h>
1616
#include <txmempool.h>
1717
#include <univalue.h>
18+
#include <util/fees.h>
1819
#include <validationinterface.h>
1920

2021
#include <algorithm>

src/test/amount_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ BOOST_AUTO_TEST_CASE(ToStringTest)
138138
CFeeRate feeRate;
139139
feeRate = CFeeRate(1);
140140
BOOST_CHECK_EQUAL(feeRate.ToString(), "0.00000001 BTC/kvB");
141-
BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::BTC_KVB), "0.00000001 BTC/kvB");
142-
BOOST_CHECK_EQUAL(feeRate.ToString(FeeEstimateMode::SAT_VB), "0.001 sat/vB");
141+
BOOST_CHECK_EQUAL(feeRate.ToString(FeeRateFormat::BTC_KVB), "0.00000001 BTC/kvB");
142+
BOOST_CHECK_EQUAL(feeRate.ToString(FeeRateFormat::SAT_VB), "0.001 sat/vB");
143143
}
144144

145145
BOOST_AUTO_TEST_SUITE_END()

src/test/fuzz/string.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <test/fuzz/FuzzedDataProvider.h>
2323
#include <test/fuzz/fuzz.h>
2424
#include <test/fuzz/util.h>
25+
#include <util/fees.h>
2526
#include <util/strencodings.h>
2627
#include <util/string.h>
2728
#include <util/translation.h>
@@ -34,8 +35,6 @@
3435
#include <string>
3536
#include <vector>
3637

37-
enum class FeeEstimateMode;
38-
3938
using common::AmountErrMsg;
4039
using common::AmountHighWarn;
4140
using common::FeeModeFromString;

src/util/fees.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ enum class FeeEstimateMode {
1010
UNSET, //!< Use default settings based on other criteria
1111
ECONOMICAL, //!< Force estimateSmartFee to use non-conservative estimates
1212
CONSERVATIVE, //!< Force estimateSmartFee to use conservative estimates
13-
BTC_KVB, //!< Use BTC/kvB fee rate unit
14-
SAT_VB, //!< Use sat/vB fee rate unit
1513
};
1614

1715
#endif // BITCOIN_UTIL_FEES_H

src/wallet/coincontrol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <primitives/transaction.h>
1212
#include <script/keyorigin.h>
1313
#include <script/signingprovider.h>
14+
#include <util/fees.h>
1415

1516
#include <algorithm>
1617
#include <map>

src/wallet/rpc/spend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ static std::vector<RPCArg> OutputsDoc()
10081008
static RPCHelpMan bumpfee_helper(std::string method_name)
10091009
{
10101010
const bool want_psbt = method_name == "psbtbumpfee";
1011-
const std::string incremental_fee{CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE).ToString(FeeEstimateMode::SAT_VB)};
1011+
const std::string incremental_fee{CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE).ToString(FeeRateFormat::SAT_VB)};
10121012

10131013
return RPCHelpMan{method_name,
10141014
"Bumps the fee of a transaction T, replacing it with a new transaction B.\n"
@@ -1483,7 +1483,7 @@ RPCHelpMan sendall()
14831483
// Do not, ever, assume that it's fine to change the fee rate if the user has explicitly
14841484
// provided one
14851485
if (coin_control.m_feerate && fee_rate > *coin_control.m_feerate) {
1486-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Fee rate (%s) is lower than the minimum fee rate setting (%s)", coin_control.m_feerate->ToString(FeeEstimateMode::SAT_VB), fee_rate.ToString(FeeEstimateMode::SAT_VB)));
1486+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Fee rate (%s) is lower than the minimum fee rate setting (%s)", coin_control.m_feerate->ToString(FeeRateFormat::SAT_VB), fee_rate.ToString(FeeRateFormat::SAT_VB)));
14871487
}
14881488
if (fee_calc_out.reason == FeeReason::FALLBACK && !pwallet->m_allow_fallback_fee) {
14891489
// eventually allow a fallback fee

src/wallet/spend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
11531153
// Do not, ever, assume that it's fine to change the fee rate if the user has explicitly
11541154
// provided one
11551155
if (coin_control.m_feerate && coin_selection_params.m_effective_feerate > *coin_control.m_feerate) {
1156-
return util::Error{strprintf(_("Fee rate (%s) is lower than the minimum fee rate setting (%s)"), coin_control.m_feerate->ToString(FeeEstimateMode::SAT_VB), coin_selection_params.m_effective_feerate.ToString(FeeEstimateMode::SAT_VB))};
1156+
return util::Error{strprintf(_("Fee rate (%s) is lower than the minimum fee rate setting (%s)"), coin_control.m_feerate->ToString(FeeRateFormat::SAT_VB), coin_selection_params.m_effective_feerate.ToString(FeeRateFormat::SAT_VB))};
11571157
}
11581158
if (feeCalc.reason == FeeReason::FALLBACK && !wallet.m_allow_fallback_fee) {
11591159
// eventually allow a fallback fee

0 commit comments

Comments
 (0)