Skip to content

Commit

Permalink
Merge pull request #9 from datachainlab/fix-lc-context
Browse files Browse the repository at this point in the history
Fix `LightClientContext` to get a timestamp instead of slot getter

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Oct 25, 2024
2 parents e18f969 + 57f0e32 commit 823849d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
36 changes: 35 additions & 1 deletion crates/consensus/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ pub fn compute_timestamp_at_slot<C: ChainContext>(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<C: ChainContext>(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<C: ChainContext>(
ctx: &C,
slot: Slot,
) -> SyncCommitteePeriod {
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<C: ChainContext>(ctx: &C, slot: Slot) -> Epoch {
slot / ctx.slots_per_epoch()
Expand Down Expand Up @@ -76,6 +83,33 @@ 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<T: ssz_rs::SimpleSerialize>(mut object: T) -> Result<Root, Error> {
Ok(H256::from_slice(object.hash_tree_root()?.as_bytes()))
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{config::Config, context::DefaultChainContext, fork::ForkParameters, preset};

#[test]
fn test_compute_timestamp_at_slot() {
let ctx = DefaultChainContext::new_with_config(1729846322.into(), get_minimal_config());
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());

assert_eq!(compute_slot_at_timestamp(&ctx, 1729846322.into()), 0.into());
assert_eq!(compute_slot_at_timestamp(&ctx, 1729846328.into()), 1.into());
assert_eq!(compute_slot_at_timestamp(&ctx, 1729846334.into()), 2.into());
}

fn get_minimal_config() -> Config {
Config {
preset: preset::minimal::PRESET,
fork_parameters: ForkParameters::new(Version([0, 0, 0, 1]), vec![]),
min_genesis_time: U64(1578009600),
}
}
}
7 changes: 6 additions & 1 deletion crates/light-client-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use ethereum_light_client_verifier::{
updates::deneb::ConsensusUpdateInfo,
};
use log::*;
use std::time::SystemTime;
type Result<T> = core::result::Result<T, Error>;

type Updates<
Expand Down Expand Up @@ -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(),
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/light-client-verifier/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
31 changes: 18 additions & 13 deletions crates/light-client-verifier/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ethereum_consensus::{
beacon::{Epoch, Root, Slot},
compute::compute_slot_at_timestamp,
config::Config,
context::ChainContext,
fork::ForkParameters,
Expand All @@ -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<F> {
pub struct LightClientContext {
fork_parameters: ForkParameters,
seconds_per_slot: U64,
slots_per_epoch: Slot,
Expand All @@ -40,10 +46,11 @@ pub struct LightClientContext<F> {
genesis_validators_root: Root,
min_sync_committee_participants: usize,
signature_threshold: Fraction,
get_current_slot: F,

current_timestamp: U64,
}

impl<F> LightClientContext<F> {
impl LightClientContext {
pub fn new(
fork_parameters: ForkParameters,
seconds_per_slot: U64,
Expand All @@ -54,7 +61,7 @@ impl<F> LightClientContext<F> {
genesis_validators_root: Root,
min_sync_committee_participants: usize,
signature_threshold: Fraction,
get_current_slot: F,
current_timestamp: U64,
) -> Self {
Self {
fork_parameters,
Expand All @@ -66,7 +73,8 @@ impl<F> LightClientContext<F> {
genesis_validators_root,
min_sync_committee_participants,
signature_threshold,
get_current_slot,

current_timestamp,
}
}

Expand All @@ -75,7 +83,7 @@ impl<F> LightClientContext<F> {
genesis_validators_root: Root,
genesis_time: U64,
signature_threshold: Fraction,
get_current_slot: F,
current_timestamp: U64,
) -> Self {
Self::new(
config.fork_parameters,
Expand All @@ -86,15 +94,12 @@ impl<F> LightClientContext<F> {
genesis_validators_root,
config.preset.MIN_SYNC_COMMITTEE_PARTICIPANTS,
signature_threshold,
get_current_slot,
current_timestamp,
)
}
}

impl<F> ConsensusVerificationContext for LightClientContext<F>
where
F: Fn() -> Slot,
{
impl ConsensusVerificationContext for LightClientContext {
fn genesis_validators_root(&self) -> Root {
self.genesis_validators_root.clone()
}
Expand All @@ -108,11 +113,11 @@ where
}

fn current_slot(&self) -> Slot {
(self.get_current_slot)()
compute_slot_at_timestamp(self, self.current_timestamp)
}
}

impl<F> ChainContext for LightClientContext<F> {
impl ChainContext for LightClientContext {
fn genesis_time(&self) -> U64 {
self.genesis_time
}
Expand Down

0 comments on commit 823849d

Please sign in to comment.