From 2d94eafadfdcc85128a770e6db0cbdca37eed2da Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Fri, 25 Oct 2024 19:08:23 +0900 Subject: [PATCH] fix `LightClientContext` to get a timestamp instead of slot getter Signed-off-by: Jun Kimura --- crates/consensus/src/compute.rs | 10 +++++- crates/light-client-cli/src/client.rs | 7 ++++- crates/light-client-verifier/src/consensus.rs | 2 +- crates/light-client-verifier/src/context.rs | 31 +++++++++++-------- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/crates/consensus/src/compute.rs b/crates/consensus/src/compute.rs index 6aaf7ec..eb94d72 100644 --- a/crates/consensus/src/compute.rs +++ b/crates/consensus/src/compute.rs @@ -14,6 +14,13 @@ pub fn compute_timestamp_at_slot(ctx: &C, slot: Slot) -> U64 { ctx.genesis_time() + slots_since_genesis * ctx.seconds_per_slot() } +/// compute_slot_at_timestamp returns the slot number at the given timestamp. +pub fn compute_slot_at_timestamp(ctx: &C, timestamp: U64) -> Slot { + let slots_since_genesis = (timestamp - ctx.genesis_time()) / ctx.seconds_per_slot(); + ctx.fork_parameters().genesis_slot() + slots_since_genesis +} + +/// compute_sync_committee_period_at_slot returns the sync committee period at slot pub fn compute_sync_committee_period_at_slot( ctx: &C, slot: Slot, @@ -21,7 +28,7 @@ pub fn compute_sync_committee_period_at_slot( compute_sync_committee_period(ctx, compute_epoch_at_slot(ctx, slot)) } -/// Return the epoch number at ``slot``. +/// Return the epoch number at slot /// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#compute_epoch_at_slot pub fn compute_epoch_at_slot(ctx: &C, slot: Slot) -> Epoch { slot / ctx.slots_per_epoch() @@ -76,6 +83,7 @@ pub fn compute_signing_root(header: BeaconBlockHeader, domain: Domain) -> Result })?) } +/// hash_tree_root returns the hash tree root of the object pub fn hash_tree_root(mut object: T) -> Result { Ok(H256::from_slice(object.hash_tree_root()?.as_bytes())) } diff --git a/crates/light-client-cli/src/client.rs b/crates/light-client-cli/src/client.rs index a2e8983..b8afef5 100644 --- a/crates/light-client-cli/src/client.rs +++ b/crates/light-client-cli/src/client.rs @@ -24,6 +24,7 @@ use ethereum_light_client_verifier::{ updates::deneb::ConsensusUpdateInfo, }; use log::*; +use std::time::SystemTime; type Result = core::result::Result; type Updates< @@ -296,7 +297,11 @@ impl< self.genesis_validators_root.clone(), self.genesis_time, self.trust_level.clone(), - || U64(1000000000000), + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs() + .into(), ) } } diff --git a/crates/light-client-verifier/src/consensus.rs b/crates/light-client-verifier/src/consensus.rs index 40a87e5..9cb8c62 100644 --- a/crates/light-client-verifier/src/consensus.rs +++ b/crates/light-client-verifier/src/consensus.rs @@ -427,7 +427,7 @@ mod tests_bellatrix { // NOTE: this is workaround. we must get the correct timestamp from beacon state. minimal::get_config().min_genesis_time, Fraction::new(2, 3), - || U64(100000000000000u64.into()), + 1729846322.into(), ); let updates = [ diff --git a/crates/light-client-verifier/src/context.rs b/crates/light-client-verifier/src/context.rs index 6b9e0bd..980bf14 100644 --- a/crates/light-client-verifier/src/context.rs +++ b/crates/light-client-verifier/src/context.rs @@ -1,5 +1,6 @@ use ethereum_consensus::{ beacon::{Epoch, Root, Slot}, + compute::compute_slot_at_timestamp, config::Config, context::ChainContext, fork::ForkParameters, @@ -21,16 +22,21 @@ impl Fraction { } pub trait ConsensusVerificationContext { + /// The root of the genesis validators corresponding to the target chain + /// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#beaconstate fn genesis_validators_root(&self) -> Root; + /// A slot based on verifier's local clock fn current_slot(&self) -> Slot; + /// MIN_SYNC_COMMITTEE_PARTICIPANTS from the spec: https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#misc fn min_sync_committee_participants(&self) -> usize; + /// The threshold of sync committee participation required for valid update fn signature_threshold(&self) -> Fraction; } -pub struct LightClientContext { +pub struct LightClientContext { fork_parameters: ForkParameters, seconds_per_slot: U64, slots_per_epoch: Slot, @@ -40,10 +46,11 @@ pub struct LightClientContext { genesis_validators_root: Root, min_sync_committee_participants: usize, signature_threshold: Fraction, - get_current_slot: F, + + current_timestamp: U64, } -impl LightClientContext { +impl LightClientContext { pub fn new( fork_parameters: ForkParameters, seconds_per_slot: U64, @@ -54,7 +61,7 @@ impl LightClientContext { genesis_validators_root: Root, min_sync_committee_participants: usize, signature_threshold: Fraction, - get_current_slot: F, + current_timestamp: U64, ) -> Self { Self { fork_parameters, @@ -66,7 +73,8 @@ impl LightClientContext { genesis_validators_root, min_sync_committee_participants, signature_threshold, - get_current_slot, + + current_timestamp, } } @@ -75,7 +83,7 @@ impl LightClientContext { genesis_validators_root: Root, genesis_time: U64, signature_threshold: Fraction, - get_current_slot: F, + current_timestamp: U64, ) -> Self { Self::new( config.fork_parameters, @@ -86,15 +94,12 @@ impl LightClientContext { genesis_validators_root, config.preset.MIN_SYNC_COMMITTEE_PARTICIPANTS, signature_threshold, - get_current_slot, + current_timestamp, ) } } -impl ConsensusVerificationContext for LightClientContext -where - F: Fn() -> Slot, -{ +impl ConsensusVerificationContext for LightClientContext { fn genesis_validators_root(&self) -> Root { self.genesis_validators_root.clone() } @@ -108,11 +113,11 @@ where } fn current_slot(&self) -> Slot { - (self.get_current_slot)() + compute_slot_at_timestamp(self, self.current_timestamp) } } -impl ChainContext for LightClientContext { +impl ChainContext for LightClientContext { fn genesis_time(&self) -> U64 { self.genesis_time }