From fa39cdd072c91eac70cda04b8b26681611f94cb7 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Sat, 26 Dec 2020 12:40:37 +0100 Subject: [PATCH 1/2] refactor: Use C++17 std::array deduction for OUTPUT_TYPES --- src/outputtype.cpp | 2 -- src/outputtype.h | 6 +++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/outputtype.cpp b/src/outputtype.cpp index e978852826..d96fb282c5 100644 --- a/src/outputtype.cpp +++ b/src/outputtype.cpp @@ -19,8 +19,6 @@ static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy"; static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit"; static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32"; -const std::array OUTPUT_TYPES = {OutputType::LEGACY, OutputType::P2SH_SEGWIT, OutputType::BECH32}; - bool ParseOutputType(const std::string& type, OutputType& output_type) { if (type == OUTPUT_TYPE_STRING_LEGACY) { diff --git a/src/outputtype.h b/src/outputtype.h index 57316b92d6..88422e5824 100644 --- a/src/outputtype.h +++ b/src/outputtype.h @@ -20,7 +20,11 @@ enum class OutputType { BECH32, }; -extern const std::array OUTPUT_TYPES; +static constexpr auto OUTPUT_TYPES = std::array{ + OutputType::LEGACY, + OutputType::P2SH_SEGWIT, + OutputType::BECH32, +}; [[nodiscard]] bool ParseOutputType(const std::string& str, OutputType& output_type); const std::string& FormatOutputType(OutputType type); From aaaa9878405f3f38f4f61c00feca110d7f9ca481 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Sun, 3 Jan 2021 18:44:02 +0100 Subject: [PATCH 2/2] refactor: Use C++17 std::array deduction for ALL_FEE_ESTIMATE_HORIZONS --- src/policy/fees.h | 13 ++++++++++--- src/rpc/mining.cpp | 2 +- src/test/fuzz/kitchen_sink.cpp | 8 +------- src/test/fuzz/policy_estimator.cpp | 4 ++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/policy/fees.h b/src/policy/fees.h index dd9f530c99..c444d71a31 100644 --- a/src/policy/fees.h +++ b/src/policy/fees.h @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -25,9 +26,15 @@ class TxConfirmStats; /* Identifier for each of the 3 different TxConfirmStats which will track * history over different time horizons. */ enum class FeeEstimateHorizon { - SHORT_HALFLIFE = 0, - MED_HALFLIFE = 1, - LONG_HALFLIFE = 2 + SHORT_HALFLIFE, + MED_HALFLIFE, + LONG_HALFLIFE, +}; + +static constexpr auto ALL_FEE_ESTIMATE_HORIZONS = std::array{ + FeeEstimateHorizon::SHORT_HALFLIFE, + FeeEstimateHorizon::MED_HALFLIFE, + FeeEstimateHorizon::LONG_HALFLIFE, }; std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 965b278bfa..52e033d0cc 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -1160,7 +1160,7 @@ static RPCHelpMan estimaterawfee() UniValue result(UniValue::VOBJ); - for (const FeeEstimateHorizon horizon : {FeeEstimateHorizon::SHORT_HALFLIFE, FeeEstimateHorizon::MED_HALFLIFE, FeeEstimateHorizon::LONG_HALFLIFE}) { + for (const FeeEstimateHorizon horizon : ALL_FEE_ESTIMATE_HORIZONS) { CFeeRate feeRate; EstimationResult buckets; diff --git a/src/test/fuzz/kitchen_sink.cpp b/src/test/fuzz/kitchen_sink.cpp index 4dfb2e3da0..fa4024fc38 100644 --- a/src/test/fuzz/kitchen_sink.cpp +++ b/src/test/fuzz/kitchen_sink.cpp @@ -28,12 +28,6 @@ constexpr TransactionError ALL_TRANSACTION_ERROR[] = { TransactionError::SIGHASH_MISMATCH, TransactionError::MAX_FEE_EXCEEDED, }; - -constexpr FeeEstimateHorizon ALL_FEE_EST_HORIZON[] = { - FeeEstimateHorizon::SHORT_HALFLIFE, - FeeEstimateHorizon::MED_HALFLIFE, - FeeEstimateHorizon::LONG_HALFLIFE, -}; }; // namespace // The fuzzing kitchen sink: Fuzzing harness for functions that need to be @@ -48,7 +42,7 @@ FUZZ_TARGET(kitchen_sink) (void)RPCErrorFromTransactionError(transaction_error); (void)TransactionErrorString(transaction_error); - (void)StringForFeeEstimateHorizon(fuzzed_data_provider.PickValueInArray(ALL_FEE_EST_HORIZON)); + (void)StringForFeeEstimateHorizon(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS)); const OutputType output_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES); const std::string& output_type_string = FormatOutputType(output_type); diff --git a/src/test/fuzz/policy_estimator.cpp b/src/test/fuzz/policy_estimator.cpp index 8a17a4b51b..35cfc5230b 100644 --- a/src/test/fuzz/policy_estimator.cpp +++ b/src/test/fuzz/policy_estimator.cpp @@ -66,10 +66,10 @@ FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator) } (void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral()); EstimationResult result; - (void)block_policy_estimator.estimateRawFee(fuzzed_data_provider.ConsumeIntegral(), fuzzed_data_provider.ConsumeFloatingPoint(), fuzzed_data_provider.PickValueInArray({FeeEstimateHorizon::SHORT_HALFLIFE, FeeEstimateHorizon::MED_HALFLIFE, FeeEstimateHorizon::LONG_HALFLIFE}), fuzzed_data_provider.ConsumeBool() ? &result : nullptr); + (void)block_policy_estimator.estimateRawFee(fuzzed_data_provider.ConsumeIntegral(), fuzzed_data_provider.ConsumeFloatingPoint(), fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS), fuzzed_data_provider.ConsumeBool() ? &result : nullptr); FeeCalculation fee_calculation; (void)block_policy_estimator.estimateSmartFee(fuzzed_data_provider.ConsumeIntegral(), fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr, fuzzed_data_provider.ConsumeBool()); - (void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray({FeeEstimateHorizon::SHORT_HALFLIFE, FeeEstimateHorizon::MED_HALFLIFE, FeeEstimateHorizon::LONG_HALFLIFE})); + (void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS)); } { FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);