Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add network type #230

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ethereum-consensus/src/configs/goerli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
configs::Config,
networks::Network,
primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256},
};

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion ethereum-consensus/src/configs/holesky.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
configs::Config,
networks::Network,
primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256},
};

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion ethereum-consensus/src/configs/mainnet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
configs::Config,
networks::Network,
primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256},
};

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion ethereum-consensus/src/configs/minimal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
configs::Config,
networks::Network,
primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256},
};

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions ethereum-consensus/src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ 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))]
#[cfg_attr(feature = "serde", serde(rename_all = "UPPERCASE"))]
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,
Expand Down
3 changes: 2 additions & 1 deletion ethereum-consensus/src/configs/sepolia.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
configs::Config,
networks::Network,
primitives::{Epoch, ExecutionAddress, Gwei, Version, FAR_FUTURE_EPOCH, U256},
};

Expand Down Expand Up @@ -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,
Expand Down
40 changes: 39 additions & 1 deletion ethereum-consensus/src/networks.rs
Original file line number Diff line number Diff line change
@@ -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(name) => write!(f, "{name}"),
}
}
}

impl TryFrom<&Network> for Context {
type Error = Error;

fn try_from(network: &Network) -> Result<Self, Self::Error> {
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
Expand Down
25 changes: 13 additions & 12 deletions ethereum-consensus/src/state_transition/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -286,21 +287,21 @@ impl Context {
}

pub fn genesis_time(&self) -> Result<u64, Error> {
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<Clock<SystemTimeProvider>> {
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,
}
}
Expand Down