From 59e94935a3a0e2f6abbc4fd28ebf77c024676cae Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 18 Oct 2023 16:47:11 -0600 Subject: [PATCH 1/2] remove unnecessary call to `as_ref` --- ethereum-consensus/src/altair/spec/mod.rs | 4 ++-- ethereum-consensus/src/bellatrix/spec/mod.rs | 4 ++-- ethereum-consensus/src/capella/spec/mod.rs | 4 ++-- ethereum-consensus/src/deneb/spec/mod.rs | 4 ++-- ethereum-consensus/src/phase0/helpers.rs | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ethereum-consensus/src/altair/spec/mod.rs b/ethereum-consensus/src/altair/spec/mod.rs index c1c821bb2..00245b304 100644 --- a/ethereum-consensus/src/altair/spec/mod.rs +++ b/ethereum-consensus/src/altair/spec/mod.rs @@ -1229,7 +1229,7 @@ pub fn compute_fork_digest( genesis_validators_root: Root, ) -> Result { let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?; - let digest = &fork_data_root.as_ref()[..4]; + let digest = &fork_data_root[..4]; Ok(digest.try_into().expect("should not fail")) } pub fn compute_domain( @@ -1243,7 +1243,7 @@ pub fn compute_domain( let fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root)?; let mut domain = Domain::default(); domain[..4].copy_from_slice(&domain_type.as_bytes()); - domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]); + domain[4..].copy_from_slice(&fork_data_root[..28]); Ok(domain) } pub fn compute_fork_data_root( diff --git a/ethereum-consensus/src/bellatrix/spec/mod.rs b/ethereum-consensus/src/bellatrix/spec/mod.rs index f72b0fc6f..cc0d4bd8b 100644 --- a/ethereum-consensus/src/bellatrix/spec/mod.rs +++ b/ethereum-consensus/src/bellatrix/spec/mod.rs @@ -2178,7 +2178,7 @@ pub fn compute_fork_digest( genesis_validators_root: Root, ) -> Result { let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?; - let digest = &fork_data_root.as_ref()[..4]; + let digest = &fork_data_root[..4]; Ok(digest.try_into().expect("should not fail")) } pub fn compute_domain( @@ -2192,7 +2192,7 @@ pub fn compute_domain( let fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root)?; let mut domain = Domain::default(); domain[..4].copy_from_slice(&domain_type.as_bytes()); - domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]); + domain[4..].copy_from_slice(&fork_data_root[..28]); Ok(domain) } pub fn compute_fork_data_root( diff --git a/ethereum-consensus/src/capella/spec/mod.rs b/ethereum-consensus/src/capella/spec/mod.rs index c15c91ca5..109c9f1de 100644 --- a/ethereum-consensus/src/capella/spec/mod.rs +++ b/ethereum-consensus/src/capella/spec/mod.rs @@ -2441,7 +2441,7 @@ pub fn compute_fork_digest( genesis_validators_root: Root, ) -> Result { let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?; - let digest = &fork_data_root.as_ref()[..4]; + let digest = &fork_data_root[..4]; Ok(digest.try_into().expect("should not fail")) } pub fn compute_domain( @@ -2455,7 +2455,7 @@ pub fn compute_domain( let fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root)?; let mut domain = Domain::default(); domain[..4].copy_from_slice(&domain_type.as_bytes()); - domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]); + domain[4..].copy_from_slice(&fork_data_root[..28]); Ok(domain) } pub fn compute_fork_data_root( diff --git a/ethereum-consensus/src/deneb/spec/mod.rs b/ethereum-consensus/src/deneb/spec/mod.rs index 4fa2b019a..ffe10a1d3 100644 --- a/ethereum-consensus/src/deneb/spec/mod.rs +++ b/ethereum-consensus/src/deneb/spec/mod.rs @@ -2512,7 +2512,7 @@ pub fn compute_fork_digest( genesis_validators_root: Root, ) -> Result { let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?; - let digest = &fork_data_root.as_ref()[..4]; + let digest = &fork_data_root[..4]; Ok(digest.try_into().expect("should not fail")) } pub fn compute_domain( @@ -2526,7 +2526,7 @@ pub fn compute_domain( let fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root)?; let mut domain = Domain::default(); domain[..4].copy_from_slice(&domain_type.as_bytes()); - domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]); + domain[4..].copy_from_slice(&fork_data_root[..28]); Ok(domain) } pub fn compute_fork_data_root( diff --git a/ethereum-consensus/src/phase0/helpers.rs b/ethereum-consensus/src/phase0/helpers.rs index 0e26ad372..e13eb1855 100644 --- a/ethereum-consensus/src/phase0/helpers.rs +++ b/ethereum-consensus/src/phase0/helpers.rs @@ -366,7 +366,7 @@ pub fn compute_fork_digest( genesis_validators_root: Root, ) -> Result { let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?; - let digest = &fork_data_root.as_ref()[..4]; + let digest = &fork_data_root[..4]; Ok(digest.try_into().expect("should not fail")) } @@ -382,7 +382,7 @@ pub fn compute_domain( let mut domain = Domain::default(); domain[..4].copy_from_slice(&domain_type.as_bytes()); - domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]); + domain[4..].copy_from_slice(&fork_data_root[..28]); Ok(domain) } From 0f6c7be8db86f1bc086e1ddc1b6972a9ee5ece58 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 18 Oct 2023 17:05:38 -0600 Subject: [PATCH 2/2] support serde on `Network` --- ethereum-consensus/Cargo.toml | 1 + ethereum-consensus/src/networks.rs | 42 +++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/ethereum-consensus/Cargo.toml b/ethereum-consensus/Cargo.toml index 1b99fc01c..466ac00e4 100644 --- a/ethereum-consensus/Cargo.toml +++ b/ethereum-consensus/Cargo.toml @@ -76,6 +76,7 @@ unicode-normalization = { version = "0.1.22", optional = true } [dev-dependencies] serde_with = "1.13.0" snap = "1" +toml = "0.8.2" [[bin]] name = "ec" diff --git a/ethereum-consensus/src/networks.rs b/ethereum-consensus/src/networks.rs index 62c3907e2..7ac9dc36d 100644 --- a/ethereum-consensus/src/networks.rs +++ b/ethereum-consensus/src/networks.rs @@ -5,8 +5,8 @@ use crate::Error; /// `Network` describes one of the established networks this repository supports /// or otherwise a `Custom` variant that wraps a path to a local configuration file /// for the custom network (useful for devnets). -#[derive(Default, Debug, Clone, serde::Deserialize)] -#[serde(rename_all = "lowercase")] +#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "lowercase", into = "String", from = "String")] pub enum Network { #[default] Mainnet, @@ -23,7 +23,25 @@ impl std::fmt::Display for Network { Self::Sepolia => write!(f, "sepolia"), Self::Goerli => write!(f, "goerli"), Self::Holesky => write!(f, "holesky"), - Self::Custom(config_file) => write!(f, "custom network with config at `{config_file}`"), + Self::Custom(config_file) => write!(f, "{config_file}"), + } + } +} + +impl From for String { + fn from(value: Network) -> Self { + format!("{value}") + } +} + +impl From for Network { + fn from(value: String) -> Self { + match value.as_str() { + "mainnet" => Self::Mainnet, + "sepolia" => Self::Sepolia, + "goerli" => Self::Goerli, + "holesky" => Self::Holesky, + _ => Self::Custom(value), } } } @@ -47,3 +65,21 @@ impl TryFrom<&Network> for Context { pub fn typical_genesis_time(context: &Context) -> u64 { context.min_genesis_time + context.genesis_delay } + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] + struct File { + network: Network, + } + + #[test] + fn test_serde() { + let file = File { network: Network::Custom("/path/to/foo.yaml".to_string()) }; + let str = toml::to_string(&file).unwrap(); + let recovered_file: File = toml::from_str(&str).unwrap(); + assert_eq!(file, recovered_file); + } +}