Skip to content

Commit 6d81d7a

Browse files
committed
Merge #20787: Use C++17 std::array deduction for OUTPUT_TYPES, ALL_FEE_ESTIMATE_HORIZONS
aaaa987 refactor: Use C++17 std::array deduction for ALL_FEE_ESTIMATE_HORIZONS (MarcoFalke) fa39cdd refactor: Use C++17 std::array deduction for OUTPUT_TYPES (MarcoFalke) Pull request description: With the new C++17 array deduction rules, an array encompassing all values in an enum can be specified in the same header file that specifies the enum. This is useful to avoid having to repeatedly enumerate all enum values in the code. E.g. the RPC code, but also the fuzz code. ACKs for top commit: theStack: cr ACK aaaa987 ⚙️ fanquake: ACK aaaa987 Tree-SHA512: b71bd98f3ca07ddfec385735538ce89a4952e418b52dc990fb160187ccef1fc7ebc139d42988b6f7b48df24823af61f803b83d47fb7a3b82475f0c0b109bffb7
2 parents 5cce607 + aaaa987 commit 6d81d7a

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

src/outputtype.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";
1919
static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";
2020
static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
2121

22-
const std::array<OutputType, 3> OUTPUT_TYPES = {OutputType::LEGACY, OutputType::P2SH_SEGWIT, OutputType::BECH32};
23-
2422
bool ParseOutputType(const std::string& type, OutputType& output_type)
2523
{
2624
if (type == OUTPUT_TYPE_STRING_LEGACY) {

src/outputtype.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ enum class OutputType {
2020
BECH32,
2121
};
2222

23-
extern const std::array<OutputType, 3> OUTPUT_TYPES;
23+
static constexpr auto OUTPUT_TYPES = std::array{
24+
OutputType::LEGACY,
25+
OutputType::P2SH_SEGWIT,
26+
OutputType::BECH32,
27+
};
2428

2529
[[nodiscard]] bool ParseOutputType(const std::string& str, OutputType& output_type);
2630
const std::string& FormatOutputType(OutputType type);

src/policy/fees.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <random.h>
1212
#include <sync.h>
1313

14+
#include <array>
1415
#include <map>
1516
#include <memory>
1617
#include <string>
@@ -25,9 +26,15 @@ class TxConfirmStats;
2526
/* Identifier for each of the 3 different TxConfirmStats which will track
2627
* history over different time horizons. */
2728
enum class FeeEstimateHorizon {
28-
SHORT_HALFLIFE = 0,
29-
MED_HALFLIFE = 1,
30-
LONG_HALFLIFE = 2
29+
SHORT_HALFLIFE,
30+
MED_HALFLIFE,
31+
LONG_HALFLIFE,
32+
};
33+
34+
static constexpr auto ALL_FEE_ESTIMATE_HORIZONS = std::array{
35+
FeeEstimateHorizon::SHORT_HALFLIFE,
36+
FeeEstimateHorizon::MED_HALFLIFE,
37+
FeeEstimateHorizon::LONG_HALFLIFE,
3138
};
3239

3340
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon);

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ static RPCHelpMan estimaterawfee()
11601160

11611161
UniValue result(UniValue::VOBJ);
11621162

1163-
for (const FeeEstimateHorizon horizon : {FeeEstimateHorizon::SHORT_HALFLIFE, FeeEstimateHorizon::MED_HALFLIFE, FeeEstimateHorizon::LONG_HALFLIFE}) {
1163+
for (const FeeEstimateHorizon horizon : ALL_FEE_ESTIMATE_HORIZONS) {
11641164
CFeeRate feeRate;
11651165
EstimationResult buckets;
11661166

src/test/fuzz/kitchen_sink.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ constexpr TransactionError ALL_TRANSACTION_ERROR[] = {
2828
TransactionError::SIGHASH_MISMATCH,
2929
TransactionError::MAX_FEE_EXCEEDED,
3030
};
31-
32-
constexpr FeeEstimateHorizon ALL_FEE_EST_HORIZON[] = {
33-
FeeEstimateHorizon::SHORT_HALFLIFE,
34-
FeeEstimateHorizon::MED_HALFLIFE,
35-
FeeEstimateHorizon::LONG_HALFLIFE,
36-
};
3731
}; // namespace
3832

3933
// The fuzzing kitchen sink: Fuzzing harness for functions that need to be
@@ -48,7 +42,7 @@ FUZZ_TARGET(kitchen_sink)
4842
(void)RPCErrorFromTransactionError(transaction_error);
4943
(void)TransactionErrorString(transaction_error);
5044

51-
(void)StringForFeeEstimateHorizon(fuzzed_data_provider.PickValueInArray(ALL_FEE_EST_HORIZON));
45+
(void)StringForFeeEstimateHorizon(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
5246

5347
const OutputType output_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES);
5448
const std::string& output_type_string = FormatOutputType(output_type);

src/test/fuzz/policy_estimator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
6666
}
6767
(void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());
6868
EstimationResult result;
69-
(void)block_policy_estimator.estimateRawFee(fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeFloatingPoint<double>(), fuzzed_data_provider.PickValueInArray({FeeEstimateHorizon::SHORT_HALFLIFE, FeeEstimateHorizon::MED_HALFLIFE, FeeEstimateHorizon::LONG_HALFLIFE}), fuzzed_data_provider.ConsumeBool() ? &result : nullptr);
69+
(void)block_policy_estimator.estimateRawFee(fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeFloatingPoint<double>(), fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS), fuzzed_data_provider.ConsumeBool() ? &result : nullptr);
7070
FeeCalculation fee_calculation;
7171
(void)block_policy_estimator.estimateSmartFee(fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr, fuzzed_data_provider.ConsumeBool());
72-
(void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray({FeeEstimateHorizon::SHORT_HALFLIFE, FeeEstimateHorizon::MED_HALFLIFE, FeeEstimateHorizon::LONG_HALFLIFE}));
72+
(void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
7373
}
7474
{
7575
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);

0 commit comments

Comments
 (0)