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

reorg Fork type #282

Merged
merged 1 commit into from
Oct 9, 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
10 changes: 5 additions & 5 deletions beacon-api-client/examples/api_sketch.rs
Original file line number Diff line number Diff line change
@@ -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<T: serde::Serialize>(fork: Forks, value: T) -> serde_json::Value {
fn with_fork<T: serde::Serialize>(fork: Fork, value: T) -> serde_json::Value {
serde_json::json!( {
"version": fork,
"data": value,
Expand All @@ -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<BlindedBeaconBlock> =
serde_json::from_str(&block_with_version_repr).unwrap();
Expand All @@ -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(),
});
Expand All @@ -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"),
Expand Down
6 changes: 3 additions & 3 deletions beacon-api-client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -467,7 +467,7 @@ pub struct Value<T> {

#[derive(Debug, Serialize)]
pub struct VersionedValue<T: serde::Serialize + serde::de::DeserializeOwned> {
pub version: Forks,
pub version: Fork,
pub data: T,
pub meta: HashMap<String, serde_json::Value>,
}
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions ethereum-consensus/src/fork.rs
Original file line number Diff line number Diff line change
@@ -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,
}
3 changes: 3 additions & 0 deletions ethereum-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,3 +19,5 @@ pub mod signing;
pub mod ssz;
pub mod state_transition;
pub mod types;

pub use fork::Fork;
23 changes: 7 additions & 16 deletions ethereum-consensus/src/state_transition/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions ethereum-consensus/src/state_transition/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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")]
Expand Down
23 changes: 12 additions & 11 deletions ethereum-consensus/src/state_transition/executor.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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,
}),
}
}
Expand Down Expand Up @@ -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,
}),
}
}
Expand Down