Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 39 additions & 30 deletions crates/light-client/src/consensus/consensus_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use chrono::Duration;
use ethportal_api::{
consensus::{header::BeaconBlockHeader, signature::BlsSignature},
light_client::{
bootstrap::CurrentSyncCommitteeProofLen,
finality_update::{LightClientFinalityUpdate, LightClientFinalityUpdateDeneb},
optimistic_update::{LightClientOptimisticUpdate, LightClientOptimisticUpdateDeneb},
bootstrap::CurrentSyncCommitteeProofLenElectra,
finality_update::{LightClientFinalityUpdate, LightClientFinalityUpdateElectra},
optimistic_update::{LightClientOptimisticUpdate, LightClientOptimisticUpdateElectra},
store::LightClientStore,
update::{FinalizedRootProofLen, LightClientUpdateDeneb},
update::{FinalizedRootProofLenElectra, LightClientUpdateElectra},
},
types::network_spec::network_spec,
utils::bytes::hex_encode,
Expand Down Expand Up @@ -201,7 +201,7 @@ impl<R: ConsensusRpc> ConsensusLightClient<R> {
}

async fn bootstrap(&mut self) -> Result<()> {
let mut bootstrap = self
let bootstrap = self
.rpc
.get_bootstrap(&self.initial_checkpoint)
.await
Expand All @@ -219,7 +219,7 @@ impl<R: ConsensusRpc> ConsensusLightClient<R> {

let committee_valid = is_current_committee_proof_valid(
&bootstrap.header.beacon,
&mut bootstrap.current_sync_committee,
&bootstrap.current_sync_committee,
&bootstrap.current_sync_committee_branch,
);

Expand Down Expand Up @@ -247,7 +247,7 @@ impl<R: ConsensusRpc> ConsensusLightClient<R> {
Ok(())
}

fn verify_update(&self, update: &LightClientUpdateDeneb) -> Result<()> {
fn verify_update(&self, update: &LightClientUpdateElectra) -> Result<()> {
let update = GenericUpdate::from(update);
let expected_current_slot = expected_current_slot();
let genesis_root = &self.config.chain.genesis_root;
Expand All @@ -260,7 +260,7 @@ impl<R: ConsensusRpc> ConsensusLightClient<R> {
)
}

fn verify_finality_update(&self, update: &LightClientFinalityUpdateDeneb) -> Result<()> {
fn verify_finality_update(&self, update: &LightClientFinalityUpdateElectra) -> Result<()> {
let update = GenericUpdate::from(update);
let expected_current_slot = expected_current_slot();
let genesis_root = &self.config.chain.genesis_root;
Expand All @@ -273,7 +273,7 @@ impl<R: ConsensusRpc> ConsensusLightClient<R> {
)
}

fn verify_optimistic_update(&self, update: &LightClientOptimisticUpdateDeneb) -> Result<()> {
fn verify_optimistic_update(&self, update: &LightClientOptimisticUpdateElectra) -> Result<()> {
let update = GenericUpdate::from(update);
let expected_current_slot = expected_current_slot();
let genesis_root = &self.config.chain.genesis_root;
Expand Down Expand Up @@ -370,12 +370,12 @@ impl<R: ConsensusRpc> ConsensusLightClient<R> {
}
}

fn apply_update(&mut self, update: &LightClientUpdateDeneb) {
fn apply_update(&mut self, update: &LightClientUpdateElectra) {
let update = GenericUpdate::from(update);
self.apply_generic_update(&update);
}

fn apply_finality_update(&mut self, update: &LightClientFinalityUpdateDeneb) {
fn apply_finality_update(&mut self, update: &LightClientFinalityUpdateElectra) {
let update = GenericUpdate::from(update);
self.apply_generic_update(&update);
}
Expand All @@ -397,7 +397,7 @@ impl<R: ConsensusRpc> ConsensusLightClient<R> {
);
}

fn apply_optimistic_update(&mut self, update: &LightClientOptimisticUpdateDeneb) {
fn apply_optimistic_update(&mut self, update: &LightClientOptimisticUpdateElectra) {
let update = GenericUpdate::from(update);
self.apply_generic_update(&update);
}
Expand Down Expand Up @@ -523,13 +523,13 @@ pub fn verify_generic_update(
if update.finalized_header.is_some() && update.finality_branch.is_some() {
let is_valid = is_finality_proof_valid(
&update.attested_header,
&mut update
update
.finalized_header
.clone()
.as_ref()
.expect("finalized_header should be `Some`"),
&update
update
.finality_branch
.clone()
.as_ref()
.expect("finality_branch should be `Some`"),
);
ensure!(is_valid, ConsensusError::InvalidFinalityProof);
Expand All @@ -538,13 +538,13 @@ pub fn verify_generic_update(
if update.next_sync_committee.is_some() && update.next_sync_committee_branch.is_some() {
let is_valid = is_next_committee_proof_valid(
&update.attested_header,
&mut update
update
.next_sync_committee
.clone()
.as_ref()
.expect("next_sync_committee should be `Some`"),
&update
update
.next_sync_committee_branch
.clone()
.as_ref()
.expect("next_sync_committee_branch ahould be`Some`"),
);
ensure!(is_valid, ConsensusError::InvalidNextSyncCommitteeProof);
Expand Down Expand Up @@ -636,36 +636,36 @@ fn verify_sync_committee_signature(

fn is_finality_proof_valid(
attested_header: &BeaconBlockHeader,
finality_header: &mut BeaconBlockHeader,
finality_branch: &FixedVector<B256, FinalizedRootProofLen>,
finality_header: &BeaconBlockHeader,
finality_branch: &FixedVector<B256, FinalizedRootProofLenElectra>,
) -> bool {
is_proof_valid(attested_header, finality_header, finality_branch, 6, 41)
is_proof_valid(attested_header, finality_header, finality_branch, 7, 41)
}

fn is_next_committee_proof_valid(
attested_header: &BeaconBlockHeader,
next_committee: &mut SyncCommittee,
next_committee_branch: &FixedVector<B256, CurrentSyncCommitteeProofLen>,
next_committee: &SyncCommittee,
next_committee_branch: &FixedVector<B256, CurrentSyncCommitteeProofLenElectra>,
) -> bool {
is_proof_valid(
attested_header,
next_committee,
next_committee_branch,
5,
6,
23,
)
}

fn is_current_committee_proof_valid(
attested_header: &BeaconBlockHeader,
current_committee: &mut SyncCommittee,
current_committee_branch: &FixedVector<B256, CurrentSyncCommitteeProofLen>,
current_committee: &SyncCommittee,
current_committee_branch: &FixedVector<B256, CurrentSyncCommitteeProofLenElectra>,
) -> bool {
is_proof_valid(
attested_header,
current_committee,
current_committee_branch,
5,
6,
22,
)
}
Expand Down Expand Up @@ -700,7 +700,7 @@ mod tests {
};

let checkpoint =
hex::decode("c62aa0de55e6f21230fa63713715e1a6c13e73005e89f6389da271955d819bde")
hex::decode("787b52add77e871f1cdffbc7f36e84a923f95f8a75c61dc410af24030d74d45c")
.unwrap();

let mut client =
Expand All @@ -710,6 +710,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "Missing Pectra test vectors"]
async fn test_verify_update() {
let client = get_client(false).await;
let period = calc_sync_period(client.store.finalized_header.slot);
Expand All @@ -724,6 +725,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "Missing Pectra test vectors"]
async fn test_verify_update_invalid_committee() {
let client = get_client(false).await;
let period = calc_sync_period(client.store.finalized_header.slot);
Expand All @@ -743,6 +745,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "Missing Pectra test vectors"]
async fn test_verify_update_invalid_finality() {
let client = get_client(false).await;
let period = calc_sync_period(client.store.finalized_header.slot);
Expand All @@ -763,6 +766,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "Missing Pectra test vectors"]
async fn test_verify_update_invalid_sig() {
let client = get_client(false).await;
let period = calc_sync_period(client.store.finalized_header.slot);
Expand All @@ -782,6 +786,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "Missing Pectra test vectors"]
async fn test_verify_finality() {
let mut client = get_client(false).await;
client.sync().await.unwrap();
Expand All @@ -792,6 +797,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "Missing Pectra test vectors"]
async fn test_verify_finality_invalid_finality() {
let mut client = get_client(false).await;
client.sync().await.unwrap();
Expand All @@ -808,6 +814,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "Missing Pectra test vectors"]
async fn test_verify_finality_invalid_sig() {
let mut client = get_client(false).await;
client.sync().await.unwrap();
Expand All @@ -823,6 +830,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "Missing Pectra test vectors"]
async fn test_verify_optimistic() {
let mut client = get_client(false).await;
client.sync().await.unwrap();
Expand All @@ -832,6 +840,7 @@ mod tests {
}

#[tokio::test]
#[ignore = "Missing Pectra test vectors"]
async fn test_verify_optimistic_invalid_sig() {
let mut client = get_client(false).await;
client.sync().await.unwrap();
Expand Down
12 changes: 6 additions & 6 deletions crates/light-client/src/consensus/rpc/mock_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use async_trait::async_trait;

use super::ConsensusRpc;
use crate::consensus::types::{
LightClientBootstrapDeneb, LightClientFinalityUpdateDeneb, LightClientOptimisticUpdateDeneb,
LightClientUpdateDeneb,
LightClientBootstrapElectra, LightClientFinalityUpdateElectra,
LightClientOptimisticUpdateElectra, LightClientUpdateElectra,
};

#[derive(Clone, Debug)]
Expand All @@ -22,22 +22,22 @@ impl ConsensusRpc for MockRpc {
}
}

async fn get_bootstrap(&self, _block_root: &'_ [u8]) -> Result<LightClientBootstrapDeneb> {
async fn get_bootstrap(&self, _block_root: &'_ [u8]) -> Result<LightClientBootstrapElectra> {
let bootstrap = read_to_string(self.testdata.join("bootstrap.json"))?;
Ok(serde_json::from_str(&bootstrap)?)
}

async fn get_updates(&self, _period: u64, _count: u8) -> Result<Vec<LightClientUpdateDeneb>> {
async fn get_updates(&self, _period: u64, _count: u8) -> Result<Vec<LightClientUpdateElectra>> {
let updates = read_to_string(self.testdata.join("updates.json"))?;
Ok(serde_json::from_str(&updates)?)
}

async fn get_finality_update(&self) -> Result<LightClientFinalityUpdateDeneb> {
async fn get_finality_update(&self) -> Result<LightClientFinalityUpdateElectra> {
let finality = read_to_string(self.testdata.join("finality.json"))?;
Ok(serde_json::from_str(&finality)?)
}

async fn get_optimistic_update(&self) -> Result<LightClientOptimisticUpdateDeneb> {
async fn get_optimistic_update(&self) -> Result<LightClientOptimisticUpdateElectra> {
let optimistic = read_to_string(self.testdata.join("optimistic.json"))?;
Ok(serde_json::from_str(&optimistic)?)
}
Expand Down
12 changes: 6 additions & 6 deletions crates/light-client/src/consensus/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ use anyhow::Result;
use async_trait::async_trait;

use super::types::{
LightClientBootstrapDeneb, LightClientFinalityUpdateDeneb, LightClientOptimisticUpdateDeneb,
LightClientUpdateDeneb,
LightClientBootstrapElectra, LightClientFinalityUpdateElectra,
LightClientOptimisticUpdateElectra, LightClientUpdateElectra,
};

// implements https://github.com/ethereum/beacon-APIs/tree/master/apis/beacon/light_client
#[async_trait]
pub trait ConsensusRpc: Send + Sync + Clone {
fn new(path: &str) -> Self;
async fn get_bootstrap(&self, block_root: &'_ [u8]) -> Result<LightClientBootstrapDeneb>;
async fn get_updates(&self, period: u64, count: u8) -> Result<Vec<LightClientUpdateDeneb>>;
async fn get_finality_update(&self) -> Result<LightClientFinalityUpdateDeneb>;
async fn get_optimistic_update(&self) -> Result<LightClientOptimisticUpdateDeneb>;
async fn get_bootstrap(&self, block_root: &'_ [u8]) -> Result<LightClientBootstrapElectra>;
async fn get_updates(&self, period: u64, count: u8) -> Result<Vec<LightClientUpdateElectra>>;
async fn get_finality_update(&self) -> Result<LightClientFinalityUpdateElectra>;
async fn get_optimistic_update(&self) -> Result<LightClientOptimisticUpdateElectra>;
async fn chain_id(&self) -> Result<u64>;
fn name(&self) -> String;
}
20 changes: 10 additions & 10 deletions crates/light-client/src/consensus/rpc/nimbus_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
consensus::{
constants::MAX_REQUEST_LIGHT_CLIENT_UPDATES,
types::{
u64_deserialize, LightClientBootstrapDeneb, LightClientFinalityUpdateDeneb,
LightClientOptimisticUpdateDeneb, LightClientUpdateDeneb,
u64_deserialize, LightClientBootstrapElectra, LightClientFinalityUpdateElectra,
LightClientOptimisticUpdateElectra, LightClientUpdateElectra,
},
},
errors::RpcError,
Expand All @@ -28,7 +28,7 @@ impl ConsensusRpc for NimbusRpc {
}
}

async fn get_bootstrap(&self, block_root: &'_ [u8]) -> Result<LightClientBootstrapDeneb> {
async fn get_bootstrap(&self, block_root: &'_ [u8]) -> Result<LightClientBootstrapElectra> {
let root_hex = hex::encode(block_root);
let req = format!(
"{}/eth/v1/beacon/light_client/bootstrap/0x{}",
Expand All @@ -48,7 +48,7 @@ impl ConsensusRpc for NimbusRpc {
Ok(res.data)
}

async fn get_updates(&self, period: u64, count: u8) -> Result<Vec<LightClientUpdateDeneb>> {
async fn get_updates(&self, period: u64, count: u8) -> Result<Vec<LightClientUpdateElectra>> {
let count = cmp::min(count, MAX_REQUEST_LIGHT_CLIENT_UPDATES);
let req = format!(
"{}/eth/v1/beacon/light_client/updates?start_period={}&count={}",
Expand All @@ -68,7 +68,7 @@ impl ConsensusRpc for NimbusRpc {
Ok(res.iter().map(|d| d.data.clone()).collect())
}

async fn get_finality_update(&self) -> Result<LightClientFinalityUpdateDeneb> {
async fn get_finality_update(&self) -> Result<LightClientFinalityUpdateElectra> {
let req = format!("{}/eth/v1/beacon/light_client/finality_update", self.rpc);
let res = reqwest::get(req)
.await
Expand All @@ -80,7 +80,7 @@ impl ConsensusRpc for NimbusRpc {
Ok(res.data)
}

async fn get_optimistic_update(&self) -> Result<LightClientOptimisticUpdateDeneb> {
async fn get_optimistic_update(&self) -> Result<LightClientOptimisticUpdateElectra> {
let req = format!("{}/eth/v1/beacon/light_client/optimistic_update", self.rpc);
let res = reqwest::get(req)
.await
Expand Down Expand Up @@ -113,22 +113,22 @@ type UpdateResponse = Vec<UpdateData>;

#[derive(serde::Deserialize, Debug)]
struct UpdateData {
data: LightClientUpdateDeneb,
data: LightClientUpdateElectra,
}

#[derive(serde::Deserialize, Debug)]
struct FinalityUpdateResponse {
data: LightClientFinalityUpdateDeneb,
data: LightClientFinalityUpdateElectra,
}

#[derive(serde::Deserialize, Debug)]
struct OptimisticUpdateResponse {
data: LightClientOptimisticUpdateDeneb,
data: LightClientOptimisticUpdateElectra,
}

#[derive(serde::Deserialize, Debug)]
struct BootstrapResponse {
data: LightClientBootstrapDeneb,
data: LightClientBootstrapElectra,
}

#[derive(serde::Deserialize, Debug)]
Expand Down
Loading
Loading