Skip to content

Commit

Permalink
fix to validate fork parameters in ForkParameters::new()
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Oct 28, 2024
1 parent fcab95a commit c027e7d
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 33 deletions.
3 changes: 1 addition & 2 deletions crates/consensus/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ mod tests {
#[test]
fn test_compute_timestamp_at_slot() {
let ctx = DefaultChainContext::new_with_config(1729846322.into(), get_minimal_config());
assert!(ctx.validate().is_ok(), "context is invalid");
assert_eq!(compute_timestamp_at_slot(&ctx, 0.into()), 1729846322.into());
assert_eq!(compute_timestamp_at_slot(&ctx, 1.into()), 1729846328.into());
assert_eq!(compute_timestamp_at_slot(&ctx, 2.into()), 1729846334.into());
Expand All @@ -109,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
6 changes: 3 additions & 3 deletions crates/consensus/src/config/goerli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub fn get_config() -> Config {
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),
}
}
Expand All @@ -30,7 +31,6 @@ mod tests {

#[test]
fn test_config_validation() {
let config = get_config();
assert!(config.fork_parameters.validate().is_ok());
let _ = get_config();
}
}
6 changes: 3 additions & 3 deletions crates/consensus/src/config/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub fn get_config() -> Config {
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),
}
}
Expand All @@ -30,7 +31,6 @@ mod tests {

#[test]
fn test_config_validation() {
let config = get_config();
assert!(config.fork_parameters.validate().is_ok());
let _ = get_config();
}
}
6 changes: 3 additions & 3 deletions crates/consensus/src/config/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pub fn get_config() -> Config {
// deneb
ForkParameter::new(Version([4, 0, 0, 1]), U64(0)),
],
),
)
.unwrap(),
min_genesis_time: U64(1578009600),
}
}
Expand All @@ -33,7 +34,6 @@ mod tests {

#[test]
fn test_config_validation() {
let config = get_config();
assert!(config.fork_parameters.validate().is_ok());
let _ = get_config();
}
}
6 changes: 3 additions & 3 deletions crates/consensus/src/config/sepolia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub fn get_config() -> Config {
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),
}
}
Expand All @@ -30,7 +31,6 @@ mod tests {

#[test]
fn test_config_validation() {
let config = get_config();
assert!(config.fork_parameters.validate().is_ok());
let _ = get_config();
}
}
6 changes: 0 additions & 6 deletions crates/consensus/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
beacon::{Epoch, Slot},
config::Config,
errors::Error,
fork::ForkParameters,
types::U64,
};
Expand Down Expand Up @@ -48,11 +47,6 @@ impl DefaultChainContext {
config.preset.EPOCHS_PER_SYNC_COMMITTEE_PERIOD,
)
}

pub fn validate(&self) -> Result<(), Error> {
self.fork_parameters.validate()?;
Ok(())
}
}

impl ChainContext for DefaultChainContext {
Expand Down
121 changes: 116 additions & 5 deletions crates/consensus/src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,27 @@ impl Fork {
}
}

/// Fork parameters for the beacon chain
#[derive(Debug, Default, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ForkParameters {
genesis_version: Version,
/// Forks in order of increasing epoch
/// 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> {
fn validate(&self) -> Result<(), Error> {
if self.forks.windows(2).all(|f| f[0].epoch <= f[1].epoch) {
Ok(())
} else {
Expand All @@ -59,6 +64,11 @@ impl ForkParameters {
&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().rev() {
if epoch >= fork.epoch {
Expand All @@ -68,6 +78,9 @@ impl ForkParameters {
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 {
Expand All @@ -85,6 +98,8 @@ impl ForkParameters {
}
}

/// 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 @@ -96,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());
}
}
1 change: 0 additions & 1 deletion crates/consensus/src/fork/bellatrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ mod tests {
// ensure that signing_root calculation is correct

let ctx = DefaultChainContext::new_with_config(0.into(), config::mainnet::get_config());
assert!(ctx.validate().is_ok(), "context is invalid");
let fork_version =
compute_fork_version(&ctx, compute_epoch_at_slot(&ctx, update.signature_slot)).unwrap();
let domain = compute_domain(
Expand Down
4 changes: 2 additions & 2 deletions crates/light-client-verifier/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ mod tests_bellatrix {
Fraction::new(2, 3),
1729846322.into(),
);
assert!(ctx.validate().is_ok(), "context is invalid");

let updates = [
"light_client_update_period_5.json",
Expand Down Expand Up @@ -540,7 +539,8 @@ mod tests_bellatrix {
ForkParameter::new(Version([1, 0, 0, 1]), U64(0)),
ForkParameter::new(Version([2, 0, 0, 1]), U64(0)),
],
),
)
.unwrap(),
min_genesis_time: U64(1578009600),
}
}
Expand Down
5 changes: 0 additions & 5 deletions crates/light-client-verifier/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ impl LightClientContext {
current_timestamp,
)
}

pub fn validate(&self) -> Result<(), Error> {
self.fork_parameters.validate()?;
Ok(())
}
}

impl ConsensusVerificationContext for LightClientContext {
Expand Down

0 comments on commit c027e7d

Please sign in to comment.