Skip to content

Commit a7c29df

Browse files
committed
Merge bitcoin#34552: fees: refactor: separate feerate format from fee estimate mode
c135549 refactor: fees: split fee rate format from fee estimate mode (ismaelsadeeq) 922ebf9 refactor: move-only: move `FeeEstimateMode` enum to `util/fees.h` (ismaelsadeeq) Pull request description: ### Motivation Part of bitcoin#34075 - The `FeeEstimateMode` enum was responsible for both selecting the fee estimation algorithm and specifying the fee rate' format. #### Changes in this PR: * The `FeeEstimateMode` enum (`UNSET`, `ECONOMICAL`, `CONSERVATIVE`) is moved to a new util/fees.h header. * A new `FeeRateFormat `enum (`BTC_KVB`, `SAT_VB`) is introduced in `policy/feerate.h` for feerate formatting. * The `CFeeRate::ToString()` method is updated to use `FeeRateFormat`. * All relevant function calls have been updated to use the new `FeeRateFormat` enum for formatting and `FeeEstimateMode` for fee estimation mode. This refactoring separates these unrelated responsibilities to improve code clarity. ACKs for top commit: l0rinc: ACK c135549 furszy: utACK c135549 musaHaruna: ACK [c135549](bitcoin@c135549) — reviewed in the context of PR [34075](bitcoin#34075) willcl-ark: ACK c135549 Tree-SHA512: 7cbe36350744313d3d688d3fd282a58c441af1818b1e8ad9cddbc911c499a5205f8d4a39c36b21fed60542db1ef763eb69752d141bcef3393bf33c0922018645
2 parents c8c9c1e + c135549 commit a7c29df

File tree

10 files changed

+37
-24
lines changed

10 files changed

+37
-24
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: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <consensus/amount.h>
1010
#include <serialize.h>
1111
#include <util/feefrac.h>
12+
#include <util/fees.h>
1213

1314

1415
#include <cstdint>
@@ -18,13 +19,9 @@
1819
const std::string CURRENCY_UNIT = "BTC"; // One formatted unit
1920
const std::string CURRENCY_ATOM = "sat"; // One indivisible minimum value unit
2021

21-
/* Used to determine type of fee estimation requested */
22-
enum class FeeEstimateMode {
23-
UNSET, //!< Use default settings based on other criteria
24-
ECONOMICAL, //!< Force estimateSmartFee to use non-conservative estimates
25-
CONSERVATIVE, //!< Force estimateSmartFee to use conservative estimates
26-
BTC_KVB, //!< Use BTC/kvB fee rate unit
27-
SAT_VB, //!< Use sat/vB fee rate unit
22+
enum class FeeRateFormat {
23+
BTC_KVB, //!< Use BTC/kvB fee rate unit
24+
SAT_VB, //!< Use sat/vB fee rate unit
2825
};
2926

3027
/**
@@ -75,7 +72,7 @@ class CFeeRate
7572
m_feerate = FeePerVSize(GetFeePerK() + a.GetFeePerK(), 1000);
7673
return *this;
7774
}
78-
std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::BTC_KVB) const;
75+
std::string ToString(FeeRateFormat fee_rate_format = FeeRateFormat::BTC_KVB) const;
7976
friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }
8077
friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.m_feerate.fee, f.m_feerate.size); }
8178

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_FEES_H
6+
#define BITCOIN_UTIL_FEES_H
7+
8+
/* Used to determine type of fee estimation requested */
9+
enum class FeeEstimateMode {
10+
UNSET, //!< Use default settings based on other criteria
11+
ECONOMICAL, //!< Force estimateSmartFee to use non-conservative estimates
12+
CONSERVATIVE, //!< Force estimateSmartFee to use conservative estimates
13+
};
14+
15+
#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)