From 4d4e3fb57584d9c3bba6b4fc488f23db4937ae79 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Sun, 20 Aug 2023 09:31:05 -0600 Subject: [PATCH] refactor how a user can get a `Clock` for the configured network --- ethereum-consensus/src/lib.rs | 1 + ethereum-consensus/src/networks.rs | 8 ++++++++ .../src/state_transition/context.rs | 20 +++++++++---------- 3 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 ethereum-consensus/src/networks.rs diff --git a/ethereum-consensus/src/lib.rs b/ethereum-consensus/src/lib.rs index e1121c358..0b3471424 100644 --- a/ethereum-consensus/src/lib.rs +++ b/ethereum-consensus/src/lib.rs @@ -9,6 +9,7 @@ pub mod deneb; pub mod domains; pub mod kzg; pub mod networking; +pub mod networks; pub mod phase0; pub mod primitives; #[cfg(feature = "serde")] diff --git a/ethereum-consensus/src/networks.rs b/ethereum-consensus/src/networks.rs new file mode 100644 index 000000000..bb057c642 --- /dev/null +++ b/ethereum-consensus/src/networks.rs @@ -0,0 +1,8 @@ +/// This module contains support for various Ethereum netowrks. +use crate::state_transition::Context; + +// NOTE: the default genesis time here is usually seen on testnets +// where we have control over the genesis details +pub fn typical_genesis_time(context: &Context) -> u64 { + context.min_genesis_time + context.genesis_delay +} diff --git a/ethereum-consensus/src/state_transition/context.rs b/ethereum-consensus/src/state_transition/context.rs index c228adf08..db2db2cf9 100644 --- a/ethereum-consensus/src/state_transition/context.rs +++ b/ethereum-consensus/src/state_transition/context.rs @@ -286,18 +286,16 @@ impl Context { } } - pub fn clock(&self, genesis_time: Option) -> Clock { + pub fn clock(&self) -> Option> { match self.name.as_ref() { - "mainnet" => clock::for_mainnet(), - "sepolia" => clock::for_sepolia(), - "goerli" => clock::for_goerli(), - _ => { - // NOTE: the default genesis time here is usually seen on testnets - // where we have control over the genesis details - let genesis_time = - genesis_time.unwrap_or(self.min_genesis_time + self.genesis_delay); - clock::from_system_time(genesis_time, self.seconds_per_slot, self.slots_per_epoch) - } + "mainnet" => Some(clock::for_mainnet()), + "sepolia" => Some(clock::for_sepolia()), + "goerli" => Some(clock::for_goerli()), + _ => None, } } + + pub fn clock_at(&self, genesis_time: u64) -> Clock { + clock::from_system_time(genesis_time, self.seconds_per_slot, self.slots_per_epoch) + } }