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
This commit is contained in:
dadepo 2022-11-09 01:24:55 +04:00 committed by GitHub
parent b5d1dbc638
commit 2dbe057e3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 11 deletions

View File

@ -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<R: ConsensusRpc> ConsensusClient<R> {
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<R: ConsensusRpc> ConsensusClient<R> {
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();

View File

@ -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;

View File

@ -5,4 +5,5 @@ pub mod types;
mod consensus;
pub use crate::consensus::*;
mod constants;
mod utils;

View File

@ -23,7 +23,7 @@ impl ConsensusRpc for MockRpc {
Ok(serde_json::from_str(&bootstrap)?)
}
async fn get_updates(&self, _period: u64) -> Result<Vec<Update>> {
async fn get_updates(&self, _period: u64, _count: u8) -> Result<Vec<Update>> {
let updates = read_to_string(self.testdata.join("updates.json"))?;
Ok(serde_json::from_str(&updates)?)
}

View File

@ -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<u8>) -> Result<Bootstrap>;
async fn get_updates(&self, period: u64) -> Result<Vec<Update>>;
async fn get_updates(&self, period: u64, count: u8) -> Result<Vec<Update>>;
async fn get_finality_update(&self) -> Result<FinalityUpdate>;
async fn get_optimistic_update(&self) -> Result<OptimisticUpdate>;
async fn get_block(&self, slot: u64) -> Result<BeaconBlock>;

View File

@ -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<Vec<Update>> {
async fn get_updates(&self, period: u64, count: u8) -> Result<Vec<Update>> {
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