From 0116c8d464f9e0c968b3e776a957ca50f12ba0fa Mon Sep 17 00:00:00 2001 From: eklm Date: Thu, 20 Jan 2022 09:14:21 +0000 Subject: [PATCH] Change type of extra fields in ConfigAndPreset (#2913) ## Issue Addressed #2900 ## Proposed Changes Change type of extra_fields in ConfigAndPreset so it can contain non string values (inside serde_json::Value) --- Cargo.lock | 1 + consensus/types/Cargo.toml | 1 + consensus/types/src/config_and_preset.rs | 10 ++++++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4d487ae70..bbf8de27e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6495,6 +6495,7 @@ dependencies = [ "safe_arith", "serde", "serde_derive", + "serde_json", "serde_yaml", "slog", "state_processing", diff --git a/consensus/types/Cargo.toml b/consensus/types/Cargo.toml index ba187fb9a..bc013fe42 100644 --- a/consensus/types/Cargo.toml +++ b/consensus/types/Cargo.toml @@ -44,6 +44,7 @@ lazy_static = "1.4.0" parking_lot = "0.11.1" itertools = "0.10.0" superstruct = "0.4.0" +serde_json = "1.0.74" [dev-dependencies] criterion = "0.3.3" diff --git a/consensus/types/src/config_and_preset.rs b/consensus/types/src/config_and_preset.rs index d367cfc49..affda1a06 100644 --- a/consensus/types/src/config_and_preset.rs +++ b/consensus/types/src/config_and_preset.rs @@ -1,5 +1,6 @@ use crate::{AltairPreset, BasePreset, BellatrixPreset, ChainSpec, Config, EthSpec}; use serde_derive::{Deserialize, Serialize}; +use serde_json::Value; use std::collections::HashMap; /// Fusion of a runtime-config with the compile-time preset values. @@ -19,7 +20,7 @@ pub struct ConfigAndPreset { // pub bellatrix_preset: BellatrixPreset, /// The `extra_fields` map allows us to gracefully decode fields intended for future hard forks. #[serde(flatten)] - pub extra_fields: HashMap, + pub extra_fields: HashMap, } impl ConfigAndPreset { @@ -83,7 +84,7 @@ impl ConfigAndPreset { ), ]; for (key, value) in fields { - self.extra_fields.insert(key.to_uppercase(), value); + self.extra_fields.insert(key.to_uppercase(), value.into()); } } } @@ -107,8 +108,13 @@ mod test { let mut yamlconfig = ConfigAndPreset::from_chain_spec::(&mainnet_spec); let (k1, v1) = ("SAMPLE_HARDFORK_KEY1", "123456789"); let (k2, v2) = ("SAMPLE_HARDFORK_KEY2", "987654321"); + let (k3, v3) = ("SAMPLE_HARDFORK_KEY3", 32); + let (k4, v4) = ("SAMPLE_HARDFORK_KEY4", Value::Null); yamlconfig.extra_fields.insert(k1.into(), v1.into()); yamlconfig.extra_fields.insert(k2.into(), v2.into()); + yamlconfig.extra_fields.insert(k3.into(), v3.into()); + yamlconfig.extra_fields.insert(k4.into(), v4); + serde_yaml::to_writer(writer, &yamlconfig).expect("failed to write or serialize"); let reader = OpenOptions::new()