From 299eee14259806e916c467531067f7d02a02dcfd Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Sun, 27 Oct 2024 21:26:57 +0900 Subject: [PATCH] fix to validate fork parameters in `DefaultChainContext::new()` Signed-off-by: Jun Kimura --- crates/consensus/src/compute.rs | 5 +++-- crates/consensus/src/context.rs | 12 +++++++----- crates/consensus/src/fork/bellatrix.rs | 5 +++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/consensus/src/compute.rs b/crates/consensus/src/compute.rs index c748dd1..2bd8aeb 100644 --- a/crates/consensus/src/compute.rs +++ b/crates/consensus/src/compute.rs @@ -95,8 +95,9 @@ 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"); + let res = DefaultChainContext::new_with_config(1729846322.into(), get_minimal_config()); + assert!(res.is_ok(), "{:?}", res.err()); + let ctx = res.unwrap(); 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()); diff --git a/crates/consensus/src/context.rs b/crates/consensus/src/context.rs index 8439b45..4fa736f 100644 --- a/crates/consensus/src/context.rs +++ b/crates/consensus/src/context.rs @@ -29,17 +29,19 @@ impl DefaultChainContext { seconds_per_slot: U64, slots_per_epoch: Slot, epochs_per_sync_committee_period: Epoch, - ) -> Self { - Self { + ) -> Result { + let this = Self { genesis_time, fork_parameters, seconds_per_slot, slots_per_epoch, epochs_per_sync_committee_period, - } + }; + this.validate()?; + Ok(this) } - pub fn new_with_config(genesis_time: U64, config: Config) -> Self { + pub fn new_with_config(genesis_time: U64, config: Config) -> Result { Self::new( genesis_time, config.fork_parameters, @@ -49,7 +51,7 @@ impl DefaultChainContext { ) } - pub fn validate(&self) -> Result<(), Error> { + fn validate(&self) -> Result<(), Error> { self.fork_parameters.validate()?; Ok(()) } diff --git a/crates/consensus/src/fork/bellatrix.rs b/crates/consensus/src/fork/bellatrix.rs index 6a354db..9f32961 100644 --- a/crates/consensus/src/fork/bellatrix.rs +++ b/crates/consensus/src/fork/bellatrix.rs @@ -494,8 +494,9 @@ 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 res = DefaultChainContext::new_with_config(0.into(), config::mainnet::get_config()); + assert!(res.is_ok(), "{:?}", res.err()); + let ctx = res.unwrap(); let fork_version = compute_fork_version(&ctx, compute_epoch_at_slot(&ctx, update.signature_slot)).unwrap(); let domain = compute_domain(