Skip to content

Commit

Permalink
Merge pull request #13 from datachainlab/fork
Browse files Browse the repository at this point in the history
Remove type parameter `EXECUTION_PAYLOAD_TREE_DEPTH` from `SyncProtocolVerifier`

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Oct 28, 2024
2 parents ff98f0b + c027e7d commit b60a6c7
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 60 deletions.
4 changes: 2 additions & 2 deletions crates/consensus/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn compute_domain<C: ChainContext>(
genesis_validators_root: Option<Root>,
) -> Result<Domain, Error> {
let fork_data_root = compute_fork_data_root(
fork_version.unwrap_or(ctx.fork_parameters().genesis_version.clone()),
fork_version.unwrap_or(ctx.fork_parameters().genesis_version().clone()),
genesis_validators_root.unwrap_or_default(),
)?;
let mut domain: [u8; 32] = Default::default();
Expand Down Expand Up @@ -108,7 +108,7 @@ mod tests {
fn get_minimal_config() -> Config {
Config {
preset: preset::minimal::PRESET,
fork_parameters: ForkParameters::new(Version([0, 0, 0, 1]), vec![]),
fork_parameters: ForkParameters::new(Version([0, 0, 0, 1]), vec![]).unwrap(),
min_genesis_time: U64(1578009600),
}
}
Expand Down
21 changes: 16 additions & 5 deletions crates/consensus/src/config/goerli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ pub fn get_config() -> Config {
fork_parameters: ForkParameters::new(
Version([0, 0, 16, 32]),
vec![
ForkParameter::new(Version([5, 0, 16, 32]), U64(u64::MAX)),
ForkParameter::new(Version([4, 0, 16, 32]), U64(231680)),
ForkParameter::new(Version([3, 0, 16, 32]), U64(162304)),
ForkParameter::new(Version([2, 0, 16, 32]), U64(112260)),
ForkParameter::new(Version([1, 0, 16, 32]), U64(36660)),
ForkParameter::new(Version([2, 0, 16, 32]), U64(112260)),
ForkParameter::new(Version([3, 0, 16, 32]), U64(162304)),
ForkParameter::new(Version([4, 0, 16, 32]), U64(231680)),
ForkParameter::new(Version([5, 0, 16, 32]), U64(u64::MAX)),
],
),
)
.unwrap(),
min_genesis_time: U64(1614588812),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_config_validation() {
let _ = get_config();
}
}
21 changes: 16 additions & 5 deletions crates/consensus/src/config/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ pub fn get_config() -> Config {
fork_parameters: ForkParameters::new(
Version([0, 0, 0, 0]),
vec![
ForkParameter::new(Version([5, 0, 0, 0]), U64(u64::MAX)),
ForkParameter::new(Version([4, 0, 0, 0]), U64(269568)),
ForkParameter::new(Version([3, 0, 0, 0]), U64(194048)),
ForkParameter::new(Version([2, 0, 0, 0]), U64(144896)),
ForkParameter::new(Version([1, 0, 0, 0]), U64(74240)),
ForkParameter::new(Version([2, 0, 0, 0]), U64(144896)),
ForkParameter::new(Version([3, 0, 0, 0]), U64(194048)),
ForkParameter::new(Version([4, 0, 0, 0]), U64(269568)),
ForkParameter::new(Version([5, 0, 0, 0]), U64(u64::MAX)),
],
),
)
.unwrap(),
min_genesis_time: U64(1606824000),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_config_validation() {
let _ = get_config();
}
}
25 changes: 18 additions & 7 deletions crates/consensus/src/config/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,27 @@ pub fn get_config() -> Config {
fork_parameters: ForkParameters::new(
Version([0, 0, 0, 1]),
vec![
// deneb
ForkParameter::new(Version([4, 0, 0, 1]), U64(0)),
// capella
ForkParameter::new(Version([3, 0, 0, 1]), U64(0)),
// belatrix
ForkParameter::new(Version([2, 0, 0, 1]), U64(0)),
// altair
ForkParameter::new(Version([1, 0, 0, 1]), U64(0)),
// belatrix
ForkParameter::new(Version([2, 0, 0, 1]), U64(0)),
// capella
ForkParameter::new(Version([3, 0, 0, 1]), U64(0)),
// deneb
ForkParameter::new(Version([4, 0, 0, 1]), U64(0)),
],
),
)
.unwrap(),
min_genesis_time: U64(1578009600),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_config_validation() {
let _ = get_config();
}
}
21 changes: 16 additions & 5 deletions crates/consensus/src/config/sepolia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ pub fn get_config() -> Config {
fork_parameters: ForkParameters::new(
Version([144, 0, 0, 105]),
vec![
ForkParameter::new(Version([144, 0, 0, 116]), U64(u64::MAX)),
ForkParameter::new(Version([144, 0, 0, 115]), U64(132608)),
ForkParameter::new(Version([144, 0, 0, 114]), U64(56832)),
ForkParameter::new(Version([144, 0, 0, 113]), U64(100)),
ForkParameter::new(Version([144, 0, 0, 112]), U64(50)),
ForkParameter::new(Version([144, 0, 0, 113]), U64(100)),
ForkParameter::new(Version([144, 0, 0, 114]), U64(56832)),
ForkParameter::new(Version([144, 0, 0, 115]), U64(132608)),
ForkParameter::new(Version([144, 0, 0, 116]), U64(u64::MAX)),
],
),
)
.unwrap(),
min_genesis_time: U64(1655647200),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_config_validation() {
let _ = get_config();
}
}
12 changes: 11 additions & 1 deletion crates/consensus/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::{beacon::Root, bls::PublicKey, fork::ForkParameters, internal_prelude::*, types::H256};
use crate::{
beacon::{Root, Version},
bls::PublicKey,
fork::ForkParameters,
internal_prelude::*,
types::{H256, U64},
};
use displaydoc::Display;

#[derive(Debug, Display)]
Expand All @@ -21,6 +27,10 @@ pub enum Error {
InvalidAddressLength(usize, usize),
/// invalid fork parameters order: `{0:?}`
InvalidForkParamersOrder(ForkParameters),
/// invalid fork version: `epoch={0:?} fork={1:?} index={2}`
UnknownFork(U64, U64, usize),
/// the fork version does not support execution payload: `{0:?}`
NotSupportedExecutionPayload(Version),
/// other error: `{description}`
Other { description: String },
}
Expand Down
169 changes: 161 additions & 8 deletions crates/consensus/src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,49 @@ use crate::errors::Error;
use crate::internal_prelude::*;
use crate::types::U64;

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Fork {
Genesis(Version),
Altair(ForkParameter),
Bellatrix(ForkParameter),
Capella(ForkParameter),
Deneb(ForkParameter),
}

impl Fork {
pub fn execution_payload_tree_depth(&self) -> Result<usize, Error> {
match self {
Fork::Genesis(v) => Err(Error::NotSupportedExecutionPayload(v.clone())),
Fork::Altair(f) => Err(Error::NotSupportedExecutionPayload(f.version.clone())),
Fork::Bellatrix(_) => Ok(bellatrix::EXECUTION_PAYLOAD_TREE_DEPTH),
Fork::Capella(_) => Ok(capella::EXECUTION_PAYLOAD_TREE_DEPTH),
Fork::Deneb(_) => Ok(deneb::EXECUTION_PAYLOAD_TREE_DEPTH),
}
}
}

/// Fork parameters for the beacon chain
#[derive(Debug, Default, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ForkParameters {
pub genesis_version: Version,
pub forks: Vec<ForkParameter>,
genesis_version: Version,
/// Forks in order of ascending epoch
/// The first element is the first fork after genesis
/// i.e., [Altair, Bellatrix, Capella, Deneb, ...]
forks: Vec<ForkParameter>,
}

impl ForkParameters {
pub const fn new(genesis_version: Version, forks: Vec<ForkParameter>) -> Self {
Self {
pub fn new(genesis_version: Version, forks: Vec<ForkParameter>) -> Result<Self, Error> {
let this = Self {
genesis_version,
forks,
}
};
this.validate()?;
Ok(this)
}

pub fn validate(&self) -> Result<(), Error> {
if self.forks.windows(2).all(|f| f[0].epoch >= f[1].epoch) {
fn validate(&self) -> Result<(), Error> {
if self.forks.windows(2).all(|f| f[0].epoch <= f[1].epoch) {
Ok(())
} else {
Err(Error::InvalidForkParamersOrder(self.clone()))
Expand All @@ -33,16 +60,46 @@ impl ForkParameters {
U64(0)
}

pub fn genesis_version(&self) -> &Version {
&self.genesis_version
}

pub fn forks(&self) -> &[ForkParameter] {
&self.forks
}

/// Compute the fork version for the given epoch
pub fn compute_fork_version(&self, epoch: Epoch) -> Result<&Version, Error> {
for fork in self.forks.iter() {
for fork in self.forks.iter().rev() {
if epoch >= fork.epoch {
return Ok(&fork.version);
}
}
Ok(&self.genesis_version)
}

/// Compute the fork for the given epoch
///
/// If `forks` does not contain a fork for the given epoch, it returns an error.
pub fn compute_fork(&self, epoch: Epoch) -> Result<Fork, Error> {
for (i, fork) in self.forks.iter().enumerate().rev() {
if epoch >= fork.epoch {
let fork = fork.clone();
return Ok(match i {
0 => Fork::Altair(fork),
1 => Fork::Bellatrix(fork),
2 => Fork::Capella(fork),
3 => Fork::Deneb(fork),
_ => return Err(Error::UnknownFork(epoch, fork.epoch, i)),
});
}
}
Ok(Fork::Genesis(self.genesis_version.clone()))
}
}

/// Fork parameters for each fork
/// In the mainnet, you can find the parameters here: https://github.com/ethereum/consensus-specs/blob/9849fb39e75e6228ebd610ef0ad22f5b41543cd5/configs/mainnet.yaml#L35
#[derive(Debug, Default, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ForkParameter {
pub version: Version,
Expand All @@ -54,3 +111,99 @@ impl ForkParameter {
Self { version, epoch }
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
pub fn test_fork_parameters() {
let res = ForkParameters::new(
Version([0, 0, 0, 1]),
vec![
ForkParameter::new(Version([1, 0, 0, 1]), U64(0)),
ForkParameter::new(Version([2, 0, 0, 1]), U64(0)),
ForkParameter::new(Version([3, 0, 0, 1]), U64(0)),
ForkParameter::new(Version([4, 0, 0, 1]), U64(0)),
],
);
assert!(res.is_ok());
let params = res.unwrap();
let res = params.compute_fork(0.into());
assert!(res.is_ok());
assert_eq!(
res.unwrap(),
Fork::Deneb(ForkParameter::new(Version([4, 0, 0, 1]), U64(0)))
);

let res = ForkParameters::new(Version([0, 0, 0, 1]), vec![]);
assert!(res.is_ok());
let params = res.unwrap();
let res = params.compute_fork(0.into());
assert!(res.is_ok());
assert_eq!(res.unwrap(), Fork::Genesis(Version([0, 0, 0, 1])));

let res = ForkParameters::new(
Version([0, 0, 0, 1]),
vec![ForkParameter::new(Version([1, 0, 0, 1]), U64(0))],
);
let params = res.unwrap();
let res = params.compute_fork(0.into());
assert!(res.is_ok());
assert_eq!(
res.unwrap(),
Fork::Altair(ForkParameter::new(Version([1, 0, 0, 1]), U64(0)))
);

let res = ForkParameters::new(
Version([0, 0, 0, 1]),
vec![
ForkParameter::new(Version([1, 0, 0, 1]), U64(0)),
ForkParameter::new(Version([2, 0, 0, 1]), U64(1)),
ForkParameter::new(Version([3, 0, 0, 1]), U64(2)),
ForkParameter::new(Version([4, 0, 0, 1]), U64(3)),
],
);
assert!(res.is_ok());
let params = res.unwrap();
let res = params.compute_fork(0.into());
assert!(res.is_ok());
assert_eq!(
res.unwrap(),
Fork::Altair(ForkParameter::new(Version([1, 0, 0, 1]), U64(0)))
);
let res = params.compute_fork(1.into());
assert!(res.is_ok());
assert_eq!(
res.unwrap(),
Fork::Bellatrix(ForkParameter::new(Version([2, 0, 0, 1]), U64(1)))
);
let res = params.compute_fork(2.into());
assert!(res.is_ok());
assert_eq!(
res.unwrap(),
Fork::Capella(ForkParameter::new(Version([3, 0, 0, 1]), U64(2)))
);
let res = params.compute_fork(3.into());
assert!(res.is_ok());
assert_eq!(
res.unwrap(),
Fork::Deneb(ForkParameter::new(Version([4, 0, 0, 1]), U64(3)))
);
let res = params.compute_fork(4.into());
assert!(res.is_ok());
assert_eq!(
res.unwrap(),
Fork::Deneb(ForkParameter::new(Version([4, 0, 0, 1]), U64(3)))
);

let res = ForkParameters::new(
Version([0, 0, 0, 1]),
vec![
ForkParameter::new(Version([2, 0, 0, 1]), U64(1)),
ForkParameter::new(Version([1, 0, 0, 1]), U64(0)),
],
);
assert!(res.is_err());
}
}
2 changes: 1 addition & 1 deletion crates/consensus/src/fork/bellatrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub fn gen_execution_payload_field_proof<
}

#[cfg(test)]
mod test {
mod tests {
use super::{
gen_execution_payload_field_proof, gen_execution_payload_proof, BeaconBlockHeader,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/fork/capella.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ pub fn gen_execution_payload_field_proof<
}

#[cfg(test)]
mod test {
mod tests {
use super::*;
use crate::beacon::BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX;
use crate::merkle::is_valid_merkle_branch;
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/fork/deneb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ pub fn gen_execution_payload_field_proof<
}

#[cfg(test)]
mod test {
mod tests {
use super::*;
use crate::beacon::BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX;
use crate::merkle::is_valid_merkle_branch;
Expand Down
Loading

0 comments on commit b60a6c7

Please sign in to comment.