Skip to content

Commit

Permalink
fix to validate fork parameters in DefaultChainContext::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 27, 2024
1 parent c77d6f4 commit 299eee1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions crates/consensus/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
12 changes: 7 additions & 5 deletions crates/consensus/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ impl DefaultChainContext {
seconds_per_slot: U64,
slots_per_epoch: Slot,
epochs_per_sync_committee_period: Epoch,
) -> Self {
Self {
) -> Result<Self, Error> {
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, Error> {
Self::new(
genesis_time,
config.fork_parameters,
Expand All @@ -49,7 +51,7 @@ impl DefaultChainContext {
)
}

pub fn validate(&self) -> Result<(), Error> {
fn validate(&self) -> Result<(), Error> {
self.fork_parameters.validate()?;
Ok(())
}
Expand Down
5 changes: 3 additions & 2 deletions crates/consensus/src/fork/bellatrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 299eee1

Please sign in to comment.