From 2dbe057e3a5a1008b294f1087c5b58a7da7499d3 Mon Sep 17 00:00:00 2001 From: dadepo Date: Wed, 9 Nov 2022 01:24:55 +0400 Subject: [PATCH] refactor: add count parameter to get_update and use 128 as the limit (#97) * Make get_update take count. Limit count to 128 as specified in the spec * use u8 instead of u32 * run cargo fmt to fix formating * fix compilation in tests * moved constants module into the consensus package --- consensus/src/consensus.rs | 33 +++++++++++++++++++++++++++------ consensus/src/constants.rs | 4 ++++ consensus/src/lib.rs | 1 + consensus/src/rpc/mock_rpc.rs | 2 +- consensus/src/rpc/mod.rs | 2 +- consensus/src/rpc/nimbus_rpc.rs | 9 ++++++--- 6 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 consensus/src/constants.rs diff --git a/consensus/src/consensus.rs b/consensus/src/consensus.rs index 660ba8d..b54068b 100644 --- a/consensus/src/consensus.rs +++ b/consensus/src/consensus.rs @@ -13,6 +13,7 @@ use common::types::*; use common::utils::*; use config::Config; +use crate::constants::MAX_REQUEST_LIGHT_CLIENT_UPDATES; use crate::errors::ConsensusError; use super::rpc::ConsensusRpc; @@ -96,7 +97,10 @@ impl ConsensusClient { self.bootstrap().await?; let current_period = calc_sync_period(self.store.finalized_header.slot); - let updates = self.rpc.get_updates(current_period).await?; + let updates = self + .rpc + .get_updates(current_period, MAX_REQUEST_LIGHT_CLIENT_UPDATES) + .await?; for mut update in updates { self.verify_update(&mut update)?; @@ -126,7 +130,7 @@ impl ConsensusClient { if self.store.next_sync_committee.is_none() { debug!("checking for sync committee update"); let current_period = calc_sync_period(self.store.finalized_header.slot); - let mut updates = self.rpc.get_updates(current_period).await?; + let mut updates = self.rpc.get_updates(current_period, 1).await?; if updates.len() == 1 { let mut update = updates.get_mut(0).unwrap(); @@ -558,6 +562,7 @@ fn is_current_committee_proof_valid( mod tests { use std::sync::Arc; + use crate::constants::MAX_REQUEST_LIGHT_CLIENT_UPDATES; use ssz_rs::Vector; use crate::{ @@ -593,7 +598,11 @@ mod tests { async fn test_verify_update() { let client = get_client().await; let period = calc_sync_period(client.store.finalized_header.slot); - let updates = client.rpc.get_updates(period).await.unwrap(); + let updates = client + .rpc + .get_updates(period, MAX_REQUEST_LIGHT_CLIENT_UPDATES) + .await + .unwrap(); let mut update = updates[0].clone(); client.verify_update(&mut update).unwrap(); @@ -603,7 +612,11 @@ mod tests { async fn test_verify_update_invalid_committee() { let client = get_client().await; let period = calc_sync_period(client.store.finalized_header.slot); - let updates = client.rpc.get_updates(period).await.unwrap(); + let updates = client + .rpc + .get_updates(period, MAX_REQUEST_LIGHT_CLIENT_UPDATES) + .await + .unwrap(); let mut update = updates[0].clone(); update.next_sync_committee.pubkeys[0] = Vector::default(); @@ -619,7 +632,11 @@ mod tests { async fn test_verify_update_invalid_finality() { let client = get_client().await; let period = calc_sync_period(client.store.finalized_header.slot); - let updates = client.rpc.get_updates(period).await.unwrap(); + let updates = client + .rpc + .get_updates(period, MAX_REQUEST_LIGHT_CLIENT_UPDATES) + .await + .unwrap(); let mut update = updates[0].clone(); update.finalized_header = Header::default(); @@ -635,7 +652,11 @@ mod tests { async fn test_verify_update_invalid_sig() { let client = get_client().await; let period = calc_sync_period(client.store.finalized_header.slot); - let updates = client.rpc.get_updates(period).await.unwrap(); + let updates = client + .rpc + .get_updates(period, MAX_REQUEST_LIGHT_CLIENT_UPDATES) + .await + .unwrap(); let mut update = updates[0].clone(); update.sync_aggregate.sync_committee_signature = Vector::default(); diff --git a/consensus/src/constants.rs b/consensus/src/constants.rs new file mode 100644 index 0000000..4c6310e --- /dev/null +++ b/consensus/src/constants.rs @@ -0,0 +1,4 @@ +// Consensus constants + +// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/p2p-interface.md#configuration +pub const MAX_REQUEST_LIGHT_CLIENT_UPDATES: u8 = 128; diff --git a/consensus/src/lib.rs b/consensus/src/lib.rs index 1162c64..b59d7f2 100644 --- a/consensus/src/lib.rs +++ b/consensus/src/lib.rs @@ -5,4 +5,5 @@ pub mod types; mod consensus; pub use crate::consensus::*; +mod constants; mod utils; diff --git a/consensus/src/rpc/mock_rpc.rs b/consensus/src/rpc/mock_rpc.rs index b4eef3a..8f87ba2 100644 --- a/consensus/src/rpc/mock_rpc.rs +++ b/consensus/src/rpc/mock_rpc.rs @@ -23,7 +23,7 @@ impl ConsensusRpc for MockRpc { Ok(serde_json::from_str(&bootstrap)?) } - async fn get_updates(&self, _period: u64) -> Result> { + async fn get_updates(&self, _period: u64, _count: u8) -> Result> { let updates = read_to_string(self.testdata.join("updates.json"))?; Ok(serde_json::from_str(&updates)?) } diff --git a/consensus/src/rpc/mod.rs b/consensus/src/rpc/mod.rs index a042e71..b3d9229 100644 --- a/consensus/src/rpc/mod.rs +++ b/consensus/src/rpc/mod.rs @@ -11,7 +11,7 @@ use crate::types::{BeaconBlock, Bootstrap, FinalityUpdate, OptimisticUpdate, Upd pub trait ConsensusRpc { fn new(path: &str) -> Self; async fn get_bootstrap(&self, block_root: &Vec) -> Result; - async fn get_updates(&self, period: u64) -> Result>; + async fn get_updates(&self, period: u64, count: u8) -> Result>; async fn get_finality_update(&self) -> Result; async fn get_optimistic_update(&self) -> Result; async fn get_block(&self, slot: u64) -> Result; diff --git a/consensus/src/rpc/nimbus_rpc.rs b/consensus/src/rpc/nimbus_rpc.rs index 14999b8..2f1e180 100644 --- a/consensus/src/rpc/nimbus_rpc.rs +++ b/consensus/src/rpc/nimbus_rpc.rs @@ -3,8 +3,10 @@ use common::errors::RpcError; use eyre::Result; use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware}; +use std::cmp; use super::ConsensusRpc; +use crate::constants::MAX_REQUEST_LIGHT_CLIENT_UPDATES; use crate::types::*; pub struct NimbusRpc { @@ -49,10 +51,11 @@ impl ConsensusRpc for NimbusRpc { Ok(res.data) } - async fn get_updates(&self, period: u64) -> Result> { + async fn get_updates(&self, period: u64, count: u8) -> Result> { + let count = cmp::min(count, MAX_REQUEST_LIGHT_CLIENT_UPDATES); let req = format!( - "{}/eth/v1/beacon/light_client/updates?start_period={}&count=1000", - self.rpc, period + "{}/eth/v1/beacon/light_client/updates?start_period={}&count={}", + self.rpc, period, count ); let res = self