From 72b86dec0fed37868fa34b5cd4bd4ca39c0e52ff Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Sat, 20 May 2023 09:16:58 +0400 Subject: [PATCH 1/4] Normalize the key case in the endpoint config to lowercase --- src/client.rs | 28 +++++++++++++++++----------- tests/integration_test.rs | 4 ++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/client.rs b/src/client.rs index 1b26098..8cc6072 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,15 +1,15 @@ -use crate::errors::AppError; -use crate::processor::{process_to_displayable_format, DisplayableResult}; +use std::collections::HashMap; +use std::fmt; +use std::fmt::{Debug, Formatter}; + use async_trait::async_trait; use futures::future::join_all; - use reqwest::Response; +use serde::{Deserialize, Serialize}; use crate::args::Network; -use serde::{Deserialize, Serialize}; -use std::collections::HashMap; -use std::fmt; -use std::fmt::{Debug, Formatter}; +use crate::errors::AppError; +use crate::processor::{DisplayableResult, process_to_displayable_format}; #[derive(Debug)] pub struct ResponsePayloadWithEndpointInfo { @@ -45,7 +45,7 @@ pub struct CheckpointClient { #[derive(Debug, Clone)] pub enum StateId { Finalized, - Slot(u128), // TODO is u128 to big? + Slot(u128), // TODO is u128 too big? } impl fmt::Display for StateId { @@ -78,10 +78,16 @@ impl HttpClient for reqwest::Client { } impl CheckpointClient { - pub fn new(client: C, state_id: StateId, endpoints: EndpointsConfig) -> Self { + pub fn new(client: C, state_id: StateId, mut endpoints_config: EndpointsConfig) -> Self { + + // Normalise the keys to lowercase + endpoints_config.endpoints = endpoints_config.endpoints.into_iter().map(|(k,v)| { + (k.to_lowercase(), v) + }).collect(); + Self { client, - endpoints_config: endpoints, + endpoints_config, state_id, } } @@ -91,7 +97,7 @@ impl CheckpointClient { ) -> Result { let endpoints_config = &self.endpoints_config; let endpoints: &Vec = endpoints_config.endpoints.get(&network.to_string().to_lowercase()).ok_or( - AppError::EndpointsNotFound(format!(r#"Endpoint not found for {network} network. Ensure it is present in the config file and the network name is specified in lowercase."#)), + AppError::EndpointsNotFound(format!(r#"Endpoint not found for {network} network. Ensure it is present in the config file."#)), )?; let results = join_all(endpoints.iter().map(|endpoint| async { diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 137c1d0..3c260fc 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -177,7 +177,7 @@ pub async fn test_only_non_canonical_results() { ]; let endpoint_config = EndpointsConfig { - endpoints: HashMap::from([(Sepolia.to_string().to_lowercase(), endpoints)]), + endpoints: HashMap::from([(Sepolia.to_string(), endpoints)]), }; let checkpoint_client = CheckpointClient::new(client, StateId::Finalized, endpoint_config); @@ -310,7 +310,7 @@ pub async fn test_results_but_no_canonical() { ]; let endpoint_config = EndpointsConfig { - endpoints: HashMap::from([(Sepolia.to_string().to_lowercase(), endpoints)]), + endpoints: HashMap::from([(Sepolia.to_string(), endpoints)]), }; let checkpoint_client = CheckpointClient::new(client, StateId::Finalized, endpoint_config); From 09e9c271aa59060f55f3bf656714276be95f32dd Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Sat, 20 May 2023 09:18:44 +0400 Subject: [PATCH 2/4] Applied some clippy tips --- src/client.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/client.rs b/src/client.rs index 8cc6072..3e91bd5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -101,7 +101,7 @@ impl CheckpointClient { )?; let results = join_all(endpoints.iter().map(|endpoint| async { - let raw_response = async { + async { let path = format!( "{}/eth/v1/beacon/states/{}/finality_checkpoints", endpoint.clone(), @@ -123,7 +123,7 @@ impl CheckpointClient { payload: Err(AppError::EndpointResponseError(format!( "Error with calling {} status code {}", endpoint.clone(), - res.status().to_string() + res.status() ))), endpoint: endpoint.clone(), } @@ -135,8 +135,7 @@ impl CheckpointClient { }, } } - .await; - raw_response + .await })) .await; From 8d22683e991d11566f90ed63154c5130d0fa995c Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Sun, 26 May 2024 11:30:32 +0400 Subject: [PATCH 3/4] Upgrade dependencies --- Cargo.toml | 20 ++++++++++---------- src/checkpoint_server.rs | 9 ++++----- src/client.rs | 11 ++++++----- src/main.rs | 4 ++-- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8afe199..a09f03c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,25 +14,25 @@ edition = "2021" [dependencies] clap = { version = "4.0.24", features = ["derive"] } -reqwest = { version = "0.11.12", features = ["json"] } -futures = "0.3.25" -tokio = { version = "1.21.2", features = ["full"] } +reqwest = { version = "0.12.4", features = ["json"] } +futures = "0.3.30" +tokio = { version = "1.37.0", features = ["full"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_yaml = "0.9" -colored = "2.0.0" +colored = "2.1.0" async-trait = "0.1.59" -axum = "0.6.1" -axum-macros = "0.3.0" +axum = "0.7.5" +axum-macros = "0.4.1" thiserror = "1.0.38" -tower-http = { version = "0.4.0", features = ["trace"] } +tower-http = { version = "0.5.2", features = ["trace"] } tracing-subscriber = { version = "0.3", features = ["env-filter"]} tracing = { version = "0.1.37"} -strum = { version = "0.24", features = ["derive"] } -strum_macros = "0.24" +strum = { version = "0.26.2", features = ["derive"] } +strum_macros = "0.26.2" [dev-dependencies] -http = "0.2.8" +http = "1.1.0" [profile.release] panic = 'abort' diff --git a/src/checkpoint_server.rs b/src/checkpoint_server.rs index d6ce4d5..f70b843 100644 --- a/src/checkpoint_server.rs +++ b/src/checkpoint_server.rs @@ -7,7 +7,7 @@ use axum::response::Response; use axum::{extract::State, http::StatusCode, response::IntoResponse, Json, Router}; use axum_macros::debug_handler; use serde::{Deserialize, Serialize}; -use std::{net::SocketAddr, sync::Arc}; +use std::sync::Arc; use tower_http::trace::TraceLayer; use tracing::info; use tracing::level_filters::LevelFilter; @@ -71,11 +71,10 @@ impl CheckPointMiddleware { .layer(TraceLayer::new_for_http()) .with_state(Arc::new(self)); - let addr = SocketAddr::from(([127, 0, 0, 1], port)); - axum::Server::bind(&addr) - .serve(app.into_make_service()) + let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{port}")) .await - .unwrap(); + .expect("Binding to listening address failed"); + axum::serve(listener, app).await.unwrap(); } } diff --git a/src/client.rs b/src/client.rs index 3e91bd5..9538e88 100644 --- a/src/client.rs +++ b/src/client.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use crate::args::Network; use crate::errors::AppError; -use crate::processor::{DisplayableResult, process_to_displayable_format}; +use crate::processor::{process_to_displayable_format, DisplayableResult}; #[derive(Debug)] pub struct ResponsePayloadWithEndpointInfo { @@ -79,11 +79,12 @@ impl HttpClient for reqwest::Client { impl CheckpointClient { pub fn new(client: C, state_id: StateId, mut endpoints_config: EndpointsConfig) -> Self { - // Normalise the keys to lowercase - endpoints_config.endpoints = endpoints_config.endpoints.into_iter().map(|(k,v)| { - (k.to_lowercase(), v) - }).collect(); + endpoints_config.endpoints = endpoints_config + .endpoints + .into_iter() + .map(|(k, v)| (k.to_lowercase(), v)) + .collect(); Self { client, diff --git a/src/main.rs b/src/main.rs index 6f5b281..6398543 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,11 @@ use std::path::PathBuf; use clap::Parser; -use checkpointq_lib::args::{Cli, SubCommands}; use checkpointq_lib::args::Network::Mainnet; +use checkpointq_lib::args::{Cli, SubCommands}; use checkpointq_lib::checkpoint_server; -use checkpointq_lib::client::{CheckpointClient, EndpointsConfig}; use checkpointq_lib::client::StateId; +use checkpointq_lib::client::{CheckpointClient, EndpointsConfig}; use checkpointq_lib::errors::AppError; use checkpointq_lib::processor::print_result; From 79085e32f2b952a5a9be5c6ee8b56f8b3e0b5e1a Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Sun, 26 May 2024 11:34:32 +0400 Subject: [PATCH 4/4] Remove need for async-trait --- Cargo.toml | 1 - src/client.rs | 5 +---- tests/integration_test.rs | 2 -- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a09f03c..1ceb946 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,6 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_yaml = "0.9" colored = "2.1.0" -async-trait = "0.1.59" axum = "0.7.5" axum-macros = "0.4.1" thiserror = "1.0.38" diff --git a/src/client.rs b/src/client.rs index 9538e88..5a5188e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -2,7 +2,6 @@ use std::collections::HashMap; use std::fmt; use std::fmt::{Debug, Formatter}; -use async_trait::async_trait; use futures::future::join_all; use reqwest::Response; use serde::{Deserialize, Serialize}; @@ -62,12 +61,10 @@ pub struct EndpointsConfig { pub endpoints: HashMap>, } -#[async_trait] pub trait HttpClient { - async fn send_request(&self, path: String) -> Result; + fn send_request(&self, path: String) -> impl std::future::Future> + Send; } -#[async_trait] impl HttpClient for reqwest::Client { async fn send_request(&self, path: String) -> Result { self.get(path) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 3c260fc..67c7737 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -1,6 +1,5 @@ extern crate core; -use async_trait::async_trait; use checkpointq_lib::client::{ BlockInfo, CheckpointClient, Data, EndpointsConfig, HttpClient, StateId, SuccessEndpointPayload, }; @@ -43,7 +42,6 @@ impl MockClient { } } -#[async_trait] impl HttpClient for MockClient { async fn send_request(&self, path: String) -> Result { let responses: (