From 79821dc6a3e892c9c656f4e0296dca74dacbefae Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Mon, 9 Oct 2023 16:24:25 -0600 Subject: [PATCH] reorg `Fork` type --- beacon-api-client/examples/api_sketch.rs | 10 ++++---- beacon-api-client/src/types.rs | 6 ++--- ethereum-consensus/src/fork.rs | 10 ++++++++ ethereum-consensus/src/lib.rs | 3 +++ .../src/state_transition/context.rs | 23 ++++++------------- .../src/state_transition/error.rs | 4 ++-- .../src/state_transition/executor.rs | 23 ++++++++++--------- 7 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 ethereum-consensus/src/fork.rs diff --git a/beacon-api-client/examples/api_sketch.rs b/beacon-api-client/examples/api_sketch.rs index 9a14cb59e..7449e8660 100644 --- a/beacon-api-client/examples/api_sketch.rs +++ b/beacon-api-client/examples/api_sketch.rs @@ -1,10 +1,10 @@ use beacon_api_client::{ApiError, ApiResult, Value, VersionedValue}; use ethereum_consensus::{ - bellatrix::mainnet as bellatrix, state_transition::Forks, types::mainnet::BlindedBeaconBlock, + bellatrix::mainnet as bellatrix, types::mainnet::BlindedBeaconBlock, Fork, }; use std::collections::HashMap; -fn with_fork(fork: Forks, value: T) -> serde_json::Value { +fn with_fork(fork: Fork, value: T) -> serde_json::Value { serde_json::json!( { "version": fork, "data": value, @@ -26,7 +26,7 @@ fn main() { let block = BlindedBeaconBlock::Bellatrix(Default::default()); let block_with_version_repr = - serde_json::to_string(&with_fork(Forks::Bellatrix, &block)).unwrap(); + serde_json::to_string(&with_fork(Fork::Bellatrix, &block)).unwrap(); println!("{block_with_version_repr}"); let recovered_block: VersionedValue = serde_json::from_str(&block_with_version_repr).unwrap(); @@ -37,7 +37,7 @@ fn main() { println!("{block_with_version_repr}"); let full_success_response = ApiResult::Ok(VersionedValue { - version: Forks::Capella, + version: Fork::Capella, data: block.clone(), meta: Default::default(), }); @@ -49,7 +49,7 @@ fn main() { println!("{recovered_success:#?}"); let full_success_response = ApiResult::Ok(VersionedValue { - version: Forks::Capella, + version: Fork::Capella, data: block, meta: HashMap::from_iter([( String::from("finalized_root"), diff --git a/beacon-api-client/src/types.rs b/beacon-api-client/src/types.rs index e2432c871..965f3c8b7 100644 --- a/beacon-api-client/src/types.rs +++ b/beacon-api-client/src/types.rs @@ -8,7 +8,7 @@ use ethereum_consensus::{ Root, Slot, ValidatorIndex, Version, }, serde::try_bytes_from_hex_str, - state_transition::Forks, + Fork, }; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, fmt, marker::PhantomData, str::FromStr}; @@ -467,7 +467,7 @@ pub struct Value { #[derive(Debug, Serialize)] pub struct VersionedValue { - pub version: Forks, + pub version: Fork, pub data: T, pub meta: HashMap, } @@ -541,7 +541,7 @@ impl<'de, T: serde::Serialize + serde::de::DeserializeOwned> serde::Deserialize< return Err(serde::de::Error::duplicate_field("version")) } let version_value: serde_json::Value = map.next_value()?; - let fork: Forks = serde_json::from_value(version_value.clone()) + let fork: Fork = serde_json::from_value(version_value.clone()) .map_err(serde::de::Error::custom)?; version = Some(fork); match version_value { diff --git a/ethereum-consensus/src/fork.rs b/ethereum-consensus/src/fork.rs new file mode 100644 index 000000000..304cd2918 --- /dev/null +++ b/ethereum-consensus/src/fork.rs @@ -0,0 +1,10 @@ +// Identifies the fork of the protocol the associated object belongs to. +#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum Fork { + Phase0, + Altair, + Bellatrix, + Capella, + Deneb, +} diff --git a/ethereum-consensus/src/lib.rs b/ethereum-consensus/src/lib.rs index 05bdbb90f..a4de05dae 100644 --- a/ethereum-consensus/src/lib.rs +++ b/ethereum-consensus/src/lib.rs @@ -7,6 +7,7 @@ pub mod configs; pub mod crypto; pub mod deneb; pub mod domains; +mod fork; pub mod kzg; pub mod networking; pub mod networks; @@ -18,3 +19,5 @@ pub mod signing; pub mod ssz; pub mod state_transition; pub mod types; + +pub use fork::Fork; diff --git a/ethereum-consensus/src/state_transition/context.rs b/ethereum-consensus/src/state_transition/context.rs index 3e4cda7c4..af9b58894 100644 --- a/ethereum-consensus/src/state_transition/context.rs +++ b/ethereum-consensus/src/state_transition/context.rs @@ -7,18 +7,9 @@ use crate::{ phase0, primitives::{Epoch, ExecutionAddress, Gwei, Hash32, Slot, Version, U256}, state_transition::Error, + Fork, }; -#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)] -#[serde(rename_all = "lowercase")] -pub enum Forks { - Phase0, - Altair, - Bellatrix, - Capella, - Deneb, -} - #[derive(Debug, Default, Clone, serde::Deserialize)] pub struct Context { // phase0 preset @@ -357,18 +348,18 @@ impl Context { ) } - pub fn fork_for(&self, slot: Slot) -> Forks { + pub fn fork_for(&self, slot: Slot) -> Fork { let epoch = slot / self.slots_per_epoch; if epoch >= self.deneb_fork_epoch { - Forks::Deneb + Fork::Deneb } else if epoch >= self.capella_fork_epoch { - Forks::Capella + Fork::Capella } else if epoch >= self.bellatrix_fork_epoch { - Forks::Bellatrix + Fork::Bellatrix } else if epoch >= self.altair_fork_epoch { - Forks::Altair + Fork::Altair } else { - Forks::Phase0 + Fork::Phase0 } } diff --git a/ethereum-consensus/src/state_transition/error.rs b/ethereum-consensus/src/state_transition/error.rs index 99dda66b4..d4e303e80 100644 --- a/ethereum-consensus/src/state_transition/error.rs +++ b/ethereum-consensus/src/state_transition/error.rs @@ -4,7 +4,7 @@ use crate::{ phase0::{AttestationData, BeaconBlockHeader, Checkpoint}, primitives::{BlsPublicKey, BlsSignature, Bytes32, Epoch, Hash32, Root, Slot, ValidatorIndex}, ssz::prelude::*, - state_transition::Forks, + Fork, }; use thiserror::Error; @@ -47,7 +47,7 @@ pub enum Error { #[error( "transition requested from a later fork {destination_fork:?} to an earlier fork {source_fork:?}" )] - IncompatibleForks { source_fork: Forks, destination_fork: Forks }, + IncompatibleFork { source_fork: Fork, destination_fork: Fork }, #[error("genesis time unknown for network {0}")] UnknownGenesisTime(String), #[cfg(feature = "serde")] diff --git a/ethereum-consensus/src/state_transition/executor.rs b/ethereum-consensus/src/state_transition/executor.rs index b36b8c56b..bf37af874 100644 --- a/ethereum-consensus/src/state_transition/executor.rs +++ b/ethereum-consensus/src/state_transition/executor.rs @@ -1,9 +1,10 @@ use crate::{ altair, bellatrix, phase0, state_transition::{ - execution_engine::ExecutionEngine, BeaconState, Context, Error, Forks, Result, - SignedBeaconBlock, Validation, + execution_engine::ExecutionEngine, BeaconState, Context, Error, Result, SignedBeaconBlock, + Validation, }, + Fork, }; #[derive(Debug)] @@ -195,13 +196,13 @@ impl< BeaconState::Phase0(state) => { phase0::state_transition(state, signed_block, validation, &self.context) } - BeaconState::Altair(_) => Err(Error::IncompatibleForks { - source_fork: Forks::Altair, - destination_fork: Forks::Phase0, + BeaconState::Altair(_) => Err(Error::IncompatibleFork { + source_fork: Fork::Altair, + destination_fork: Fork::Phase0, }), - BeaconState::Bellatrix(_) => Err(Error::IncompatibleForks { - source_fork: Forks::Bellatrix, - destination_fork: Forks::Phase0, + BeaconState::Bellatrix(_) => Err(Error::IncompatibleFork { + source_fork: Fork::Bellatrix, + destination_fork: Fork::Phase0, }), } } @@ -240,9 +241,9 @@ impl< BeaconState::Altair(state) => { altair::state_transition(state, signed_block, validation, &self.context) } - BeaconState::Bellatrix(_) => Err(Error::IncompatibleForks { - source_fork: Forks::Bellatrix, - destination_fork: Forks::Altair, + BeaconState::Bellatrix(_) => Err(Error::IncompatibleFork { + source_fork: Fork::Bellatrix, + destination_fork: Fork::Altair, }), } }