Skip to content

Commit

Permalink
Merge pull request #10 from datachainlab/refactor
Browse files Browse the repository at this point in the history
Refactor verifier and fix to use correct fork version

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Oct 27, 2024
2 parents 823849d + e8e0825 commit a7efa6c
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 197 deletions.
41 changes: 27 additions & 14 deletions crates/light-client-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use ethereum_consensus::{
use ethereum_light_client_verifier::{
consensus::SyncProtocolVerifier,
context::{ConsensusVerificationContext, Fraction, LightClientContext},
state::apply_sync_committee_update,
updates::deneb::ConsensusUpdateInfo,
state::should_update_sync_committees,
updates::{deneb::ConsensusUpdateInfo, ConsensusUpdate},
};
use log::*;
use std::time::SystemTime;
Expand Down Expand Up @@ -267,23 +267,36 @@ impl<

self.verifier
.validate_updates(vctx, state, &updates.0, &updates.1)?;
self.verifier
.ensure_relevant_update(vctx, state, &updates.0)?;

let mut updated = false;
self.apply_light_client_update(state, updates.0)
}

fn apply_light_client_update(
&self,
state: &LightClientStore<SYNC_COMMITTEE_SIZE, BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
consensus_update: ConsensusUpdateInfo<
SYNC_COMMITTEE_SIZE,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
>,
) -> Result<
Option<LightClientStore<SYNC_COMMITTEE_SIZE, BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>>,
> {
let mut new_state = state.clone();
if apply_sync_committee_update(&self.ctx, &mut new_state, &updates.0)? {
updated = true;
let (current_committee, next_committee) =
should_update_sync_committees(&self.ctx, state, &consensus_update)?;
if let Some(current_committee) = current_committee {
new_state.current_sync_committee = current_committee.clone();
}
if updates.0.finalized_header.execution.block_number
> state.latest_execution_payload_header.block_number
{
if let Some(next_committee) = next_committee {
new_state.next_sync_committee = next_committee.cloned();
}
if consensus_update.finalized_beacon_header().slot > state.latest_finalized_header.slot {
new_state.latest_finalized_header = consensus_update.finalized_beacon_header().clone();
new_state.latest_execution_payload_header =
updates.0.finalized_header.execution.clone();
updated = true;
consensus_update.finalized_header.execution.clone();
}

if updated {
if *state != new_state {
self.ctx.store_light_client_state(&new_state)?;
Ok(Some(new_state))
} else {
Expand Down
33 changes: 2 additions & 31 deletions crates/light-client-cli/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use ethereum_consensus::{
sync_protocol::SyncCommittee,
types::{H256, U64},
};
use ethereum_light_client_verifier::{
state::{SyncCommitteeKeeper, SyncCommitteeView},
updates::ExecutionUpdate,
};
use ethereum_light_client_verifier::{state::LightClientStoreReader, updates::ExecutionUpdate};

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct LightClientStore<
Expand Down Expand Up @@ -52,7 +49,7 @@ impl<
const SYNC_COMMITTEE_SIZE: usize,
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
> SyncCommitteeView<SYNC_COMMITTEE_SIZE>
> LightClientStoreReader<SYNC_COMMITTEE_SIZE>
for LightClientStore<SYNC_COMMITTEE_SIZE, BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>
{
fn current_slot(&self) -> Slot {
Expand All @@ -68,32 +65,6 @@ impl<
}
}

impl<
const SYNC_COMMITTEE_SIZE: usize,
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
> SyncCommitteeKeeper<SYNC_COMMITTEE_SIZE>
for LightClientStore<SYNC_COMMITTEE_SIZE, BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>
{
fn set_finalized_header(&mut self, header: BeaconBlockHeader) {
self.latest_finalized_header = header;
}

fn set_current_sync_committee(
&mut self,
current_sync_committee: SyncCommittee<SYNC_COMMITTEE_SIZE>,
) {
self.current_sync_committee = current_sync_committee;
}

fn set_next_sync_committee(
&mut self,
next_sync_committee: Option<SyncCommittee<SYNC_COMMITTEE_SIZE>>,
) {
self.next_sync_committee = next_sync_committee;
}
}

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ExecutionUpdateInfo {
pub state_root: H256,
Expand Down
Loading

0 comments on commit a7efa6c

Please sign in to comment.