From bc7c35ebfbe7b218ab738ec24f7eb08f7bfbd12e Mon Sep 17 00:00:00 2001 From: chirag-bgh Date: Wed, 13 Sep 2023 06:04:13 +0530 Subject: [PATCH 1/2] feat: add networks type --- ethereum-consensus/src/configs/goerli.rs | 3 +- ethereum-consensus/src/configs/holesky.rs | 3 +- ethereum-consensus/src/configs/mainnet.rs | 3 +- ethereum-consensus/src/configs/minimal.rs | 3 +- ethereum-consensus/src/configs/mod.rs | 7 +++- ethereum-consensus/src/configs/sepolia.rs | 3 +- ethereum-consensus/src/networks.rs | 40 ++++++++++++++++++- .../src/state_transition/context.rs | 25 ++++++------ 8 files changed, 67 insertions(+), 20 deletions(-) diff --git a/ethereum-consensus/src/configs/goerli.rs b/ethereum-consensus/src/configs/goerli.rs index ca19e1966..67e710ac4 100644 --- a/ethereum-consensus/src/configs/goerli.rs +++ b/ethereum-consensus/src/configs/goerli.rs @@ -1,5 +1,6 @@ use crate::{ configs::Config, + networks::Network, primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256}, }; @@ -49,7 +50,7 @@ pub fn config() -> Config { Config { preset_base: "mainnet".to_string(), - name: "goerli".to_string(), + name: Network::Goerli, terminal_total_difficulty, terminal_block_hash, terminal_block_hash_activation_epoch: TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH, diff --git a/ethereum-consensus/src/configs/holesky.rs b/ethereum-consensus/src/configs/holesky.rs index e06cf5107..9098b41d1 100644 --- a/ethereum-consensus/src/configs/holesky.rs +++ b/ethereum-consensus/src/configs/holesky.rs @@ -1,5 +1,6 @@ use crate::{ configs::Config, + networks::Network, primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256}, }; @@ -52,7 +53,7 @@ pub fn config() -> Config { Config { preset_base: "mainnet".to_string(), - name: "holesky".to_string(), + name: Network::Holesky, terminal_total_difficulty, terminal_block_hash, terminal_block_hash_activation_epoch: TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH, diff --git a/ethereum-consensus/src/configs/mainnet.rs b/ethereum-consensus/src/configs/mainnet.rs index f6301b5bd..7d3e06903 100644 --- a/ethereum-consensus/src/configs/mainnet.rs +++ b/ethereum-consensus/src/configs/mainnet.rs @@ -1,5 +1,6 @@ use crate::{ configs::Config, + networks::Network, primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256}, }; @@ -47,7 +48,7 @@ pub fn config() -> Config { Config { preset_base: "mainnet".to_string(), - name: "mainnet".to_string(), + name: Network::Mainnet, terminal_total_difficulty, terminal_block_hash, terminal_block_hash_activation_epoch: TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH, diff --git a/ethereum-consensus/src/configs/minimal.rs b/ethereum-consensus/src/configs/minimal.rs index b5d957ea8..e29ba2d27 100644 --- a/ethereum-consensus/src/configs/minimal.rs +++ b/ethereum-consensus/src/configs/minimal.rs @@ -1,5 +1,6 @@ use crate::{ configs::Config, + networks::Network, primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256}, }; @@ -48,7 +49,7 @@ pub fn config() -> Config { Config { preset_base: "minimal".to_string(), - name: "minimal".to_string(), + name: Network::Custom("minimal".to_string()), terminal_total_difficulty, terminal_block_hash, terminal_block_hash_activation_epoch: TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH, diff --git a/ethereum-consensus/src/configs/mod.rs b/ethereum-consensus/src/configs/mod.rs index e6376603f..3621318f6 100644 --- a/ethereum-consensus/src/configs/mod.rs +++ b/ethereum-consensus/src/configs/mod.rs @@ -4,7 +4,10 @@ pub mod mainnet; pub mod minimal; pub mod sepolia; -use crate::primitives::{Epoch, ExecutionAddress, Gwei, Hash32, Version, U256}; +use crate::{ + networks::Network, + primitives::{Epoch, ExecutionAddress, Gwei, Hash32, Version, U256}, +}; #[derive(Debug)] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] @@ -12,7 +15,7 @@ use crate::primitives::{Epoch, ExecutionAddress, Gwei, Hash32, Version, U256}; pub struct Config { pub preset_base: String, #[cfg_attr(feature = "serde", serde(rename = "CONFIG_NAME"))] - pub name: String, + pub name: Network, pub terminal_total_difficulty: U256, pub terminal_block_hash: Hash32, diff --git a/ethereum-consensus/src/configs/sepolia.rs b/ethereum-consensus/src/configs/sepolia.rs index 905a44445..94d34653d 100644 --- a/ethereum-consensus/src/configs/sepolia.rs +++ b/ethereum-consensus/src/configs/sepolia.rs @@ -1,5 +1,6 @@ use crate::{ configs::Config, + networks::Network, primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256}, }; @@ -50,7 +51,7 @@ pub fn config() -> Config { Config { preset_base: "mainnet".to_string(), - name: "sepolia".to_string(), + name: Network::Sepolia, terminal_total_difficulty, terminal_block_hash, terminal_block_hash_activation_epoch: TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH, diff --git a/ethereum-consensus/src/networks.rs b/ethereum-consensus/src/networks.rs index bb057c642..50b9be1af 100644 --- a/ethereum-consensus/src/networks.rs +++ b/ethereum-consensus/src/networks.rs @@ -1,5 +1,43 @@ /// This module contains support for various Ethereum netowrks. -use crate::state_transition::Context; +use crate::state_transition::{Context, Error}; + +#[derive(Default, Debug, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] +pub enum Network { + #[default] + Mainnet, + Sepolia, + Goerli, + Holesky, + Custom(String), +} + +impl std::fmt::Display for Network { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Mainnet => write!(f, "mainnet"), + Self::Sepolia => write!(f, "sepolia"), + Self::Goerli => write!(f, "goerli"), + Self::Holesky => write!(f, "holesky"), + Self::Custom(config) => write!(f, "custom network with config at `{config}`"), + } + } +} + +impl TryFrom<&Network> for Context { + type Error = Error; + + fn try_from(network: &Network) -> Result { + match network { + Network::Mainnet => Ok(Context::for_mainnet()), + Network::Sepolia => Ok(Context::for_sepolia()), + Network::Goerli => Ok(Context::for_goerli()), + Network::Holesky => Ok(Context::for_holesky()), + Network::Custom(config) => Context::try_from_file(config), + } + } +} // NOTE: the default genesis time here is usually seen on testnets // where we have control over the genesis details diff --git a/ethereum-consensus/src/state_transition/context.rs b/ethereum-consensus/src/state_transition/context.rs index feb74aa77..7b78ea811 100644 --- a/ethereum-consensus/src/state_transition/context.rs +++ b/ethereum-consensus/src/state_transition/context.rs @@ -2,6 +2,7 @@ use crate::{ altair, bellatrix, clock::{self, Clock, SystemTimeProvider}, configs::{self, Config}, + networks::Network, phase0, primitives::{Epoch, ExecutionAddress, Gwei, Hash32, Slot, Version, U256}, state_transition::Error, @@ -72,7 +73,7 @@ pub struct Context { pub max_extra_data_bytes: usize, // config - pub name: String, + pub name: Network, pub terminal_total_difficulty: U256, pub terminal_block_hash: Hash32, @@ -197,7 +198,7 @@ impl Context { max_extra_data_bytes: bellatrix_preset.max_extra_data_bytes, // config - name: config.name.to_string(), + name: config.name.clone(), terminal_total_difficulty: config.terminal_total_difficulty.clone(), terminal_block_hash: config.terminal_block_hash.clone(), terminal_block_hash_activation_epoch: config.terminal_block_hash_activation_epoch, @@ -286,21 +287,21 @@ impl Context { } pub fn genesis_time(&self) -> Result { - match self.name.as_ref() { - "mainnet" => Ok(crate::clock::MAINNET_GENESIS_TIME), - "sepolia" => Ok(crate::clock::SEPOLIA_GENESIS_TIME), - "goerli" => Ok(crate::clock::GOERLI_GENESIS_TIME), - "holesky" => Ok(crate::clock::HOLESKY_GENESIS_TIME), + match &self.name { + Network::Mainnet => Ok(crate::clock::MAINNET_GENESIS_TIME), + Network::Sepolia => Ok(crate::clock::SEPOLIA_GENESIS_TIME), + Network::Goerli => Ok(crate::clock::GOERLI_GENESIS_TIME), + Network::Holesky => Ok(crate::clock::HOLESKY_GENESIS_TIME), name => Err(Error::UnknownGenesisTime(name.to_string())), } } pub fn clock(&self) -> Option> { - match self.name.as_ref() { - "mainnet" => Some(clock::for_mainnet()), - "sepolia" => Some(clock::for_sepolia()), - "goerli" => Some(clock::for_goerli()), - "holesky" => Some(clock::for_holesky()), + match self.name { + Network::Mainnet => Some(clock::for_mainnet()), + Network::Sepolia => Some(clock::for_sepolia()), + Network::Goerli => Some(clock::for_goerli()), + Network::Holesky => Some(clock::for_holesky()), _ => None, } } From f3ce0ad72efc5ec3e4befbab04df1c1e4c86e514 Mon Sep 17 00:00:00 2001 From: chirag-bgh Date: Wed, 13 Sep 2023 20:47:43 +0530 Subject: [PATCH 2/2] nit --- ethereum-consensus/src/networks.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum-consensus/src/networks.rs b/ethereum-consensus/src/networks.rs index 50b9be1af..a1a8f0e5b 100644 --- a/ethereum-consensus/src/networks.rs +++ b/ethereum-consensus/src/networks.rs @@ -20,7 +20,7 @@ impl std::fmt::Display for Network { Self::Sepolia => write!(f, "sepolia"), Self::Goerli => write!(f, "goerli"), Self::Holesky => write!(f, "holesky"), - Self::Custom(config) => write!(f, "custom network with config at `{config}`"), + Self::Custom(name) => write!(f, "{name}"), } } }