Merge #20787: Use C++17 std::array deduction for OUTPUT_TYPES, ALL_FEE_ESTIMATE_HORIZONS

aaaa987840 refactor: Use C++17 std::array deduction for ALL_FEE_ESTIMATE_HORIZONS (MarcoFalke)
fa39cdd072 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 aaaa987840 ⚙️
  fanquake:
    ACK aaaa987840

Tree-SHA512: b71bd98f3ca07ddfec385735538ce89a4952e418b52dc990fb160187ccef1fc7ebc139d42988b6f7b48df24823af61f803b83d47fb7a3b82475f0c0b109bffb7
pull/826/head
fanquake 4 years ago
commit 6d81d7aa87
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

@ -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_P2SH_SEGWIT = "p2sh-segwit";
static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32"; static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
const std::array<OutputType, 3> OUTPUT_TYPES = {OutputType::LEGACY, OutputType::P2SH_SEGWIT, OutputType::BECH32};
bool ParseOutputType(const std::string& type, OutputType& output_type) bool ParseOutputType(const std::string& type, OutputType& output_type)
{ {
if (type == OUTPUT_TYPE_STRING_LEGACY) { if (type == OUTPUT_TYPE_STRING_LEGACY) {

@ -20,7 +20,11 @@ enum class OutputType {
BECH32, BECH32,
}; };
extern const std::array<OutputType, 3> 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); [[nodiscard]] bool ParseOutputType(const std::string& str, OutputType& output_type);
const std::string& FormatOutputType(OutputType type); const std::string& FormatOutputType(OutputType type);

@ -11,6 +11,7 @@
#include <random.h> #include <random.h>
#include <sync.h> #include <sync.h>
#include <array>
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
@ -25,9 +26,15 @@ class TxConfirmStats;
/* Identifier for each of the 3 different TxConfirmStats which will track /* Identifier for each of the 3 different TxConfirmStats which will track
* history over different time horizons. */ * history over different time horizons. */
enum class FeeEstimateHorizon { enum class FeeEstimateHorizon {
SHORT_HALFLIFE = 0, SHORT_HALFLIFE,
MED_HALFLIFE = 1, MED_HALFLIFE,
LONG_HALFLIFE = 2 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); std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon);

@ -1160,7 +1160,7 @@ static RPCHelpMan estimaterawfee()
UniValue result(UniValue::VOBJ); 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; CFeeRate feeRate;
EstimationResult buckets; EstimationResult buckets;

@ -28,12 +28,6 @@ constexpr TransactionError ALL_TRANSACTION_ERROR[] = {
TransactionError::SIGHASH_MISMATCH, TransactionError::SIGHASH_MISMATCH,
TransactionError::MAX_FEE_EXCEEDED, TransactionError::MAX_FEE_EXCEEDED,
}; };
constexpr FeeEstimateHorizon ALL_FEE_EST_HORIZON[] = {
FeeEstimateHorizon::SHORT_HALFLIFE,
FeeEstimateHorizon::MED_HALFLIFE,
FeeEstimateHorizon::LONG_HALFLIFE,
};
}; // namespace }; // namespace
// The fuzzing kitchen sink: Fuzzing harness for functions that need to be // 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)RPCErrorFromTransactionError(transaction_error);
(void)TransactionErrorString(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 OutputType output_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES);
const std::string& output_type_string = FormatOutputType(output_type); const std::string& output_type_string = FormatOutputType(output_type);

@ -66,10 +66,10 @@ FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
} }
(void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>()); (void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());
EstimationResult result; EstimationResult result;
(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); (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);
FeeCalculation fee_calculation; FeeCalculation fee_calculation;
(void)block_policy_estimator.estimateSmartFee(fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr, fuzzed_data_provider.ConsumeBool()); (void)block_policy_estimator.estimateSmartFee(fuzzed_data_provider.ConsumeIntegral<int>(), 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); FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);

Loading…
Cancel
Save