Skip to content

Commit

Permalink
add get_sync_committee_at_period function
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Nov 10, 2024
1 parent 47c3ddd commit c0fba47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
8 changes: 5 additions & 3 deletions crates/light-client-verifier/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::context::{ChainConsensusVerificationContext, ConsensusVerificationCon
use crate::errors::Error;
use crate::internal_prelude::*;
use crate::misbehaviour::Misbehaviour;
use crate::state::LightClientStoreReader;
use crate::state::{get_sync_committee_at_period, LightClientStoreReader};
use crate::updates::{ConsensusUpdate, ExecutionUpdate, LightClientBootstrap};
use core::marker::PhantomData;
use ethereum_consensus::beacon::{BeaconBlockHeader, Root, DOMAIN_SYNC_COMMITTEE};
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<const SYNC_COMMITTEE_SIZE: usize, ST: LightClientStoreReader<SYNC_COMMITTEE
) -> Result<SyncCommittee<SYNC_COMMITTEE_SIZE>, Error> {
let update_signature_period =
compute_sync_committee_period_at_slot(ctx, update.signature_slot());
if let Some(committee) = store.get_sync_committee(ctx, update_signature_period) {
if let Some(committee) = get_sync_committee_at_period(ctx, store, update_signature_period) {
Ok(committee)
} else {
Err(Error::UnexpectedSingaturePeriod(
Expand Down Expand Up @@ -331,7 +331,9 @@ pub fn validate_light_client_update<
ctx,
consensus_update.attested_beacon_header().slot,
);
if let Some(committee) = store.get_sync_committee(ctx, update_attested_period + 1) {
if let Some(committee) =
get_sync_committee_at_period(ctx, store, update_attested_period + 1)
{
if committee != *update_next_sync_committee {
return Err(Error::InconsistentNextSyncCommittee(
committee.aggregate_pubkey.clone(),
Expand Down
39 changes: 22 additions & 17 deletions crates/light-client-verifier/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ethereum_consensus::{
sync_protocol::{SyncCommittee, SyncCommitteePeriod},
};

/// A trait for reading the light client store
pub trait LightClientStoreReader<const SYNC_COMMITTEE_SIZE: usize> {
/// Returns the current sync committee period
fn current_period<CC: ChainContext>(&self, ctx: &CC) -> SyncCommitteePeriod;
Expand All @@ -14,23 +15,6 @@ pub trait LightClientStoreReader<const SYNC_COMMITTEE_SIZE: usize> {
/// Returns the next sync committee corresponding to the `current_period() + 1` if available
fn next_sync_committee(&self) -> Option<SyncCommittee<SYNC_COMMITTEE_SIZE>>;

/// Returns the sync committee corresponding to the given signature period if available
fn get_sync_committee<CC: ChainContext>(
&self,
ctx: &CC,
signature_period: SyncCommitteePeriod,
) -> Option<SyncCommittee<SYNC_COMMITTEE_SIZE>> {
let current_period = self.current_period(ctx);
let next_period = current_period + 1;
if signature_period == current_period {
self.current_sync_committee()
} else if signature_period == next_period {
self.next_sync_committee()
} else {
None
}
}

/// Returns a error indicating whether the update is relevant to this store.
///
/// This method should be used to determine whether the update should be applied to the store.
Expand All @@ -40,3 +24,24 @@ pub trait LightClientStoreReader<const SYNC_COMMITTEE_SIZE: usize> {
update: &C,
) -> Result<(), Error>;
}

/// Returns the sync committee corresponding to the given signature period if available
pub fn get_sync_committee_at_period<
CC: ChainContext,
const SYNC_COMMITTEE_SIZE: usize,
ST: LightClientStoreReader<SYNC_COMMITTEE_SIZE>,
>(
ctx: &CC,
store: &ST,
signature_period: SyncCommitteePeriod,
) -> Option<SyncCommittee<SYNC_COMMITTEE_SIZE>> {
let current_period = store.current_period(ctx);
let next_period = current_period + 1;
if signature_period == current_period {
store.current_sync_committee()
} else if signature_period == next_period {
store.next_sync_committee()
} else {
None
}
}

0 comments on commit c0fba47

Please sign in to comment.