Skip to content

Commit

Permalink
Merge pull request #1 from dadepo/develop
Browse files Browse the repository at this point in the history
Clippy and Upgrade chores
  • Loading branch information
dadepo authored May 26, 2024
2 parents 4717aaa + 79085e3 commit 3d2f42f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 41 deletions.
21 changes: 10 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,24 @@ 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"
async-trait = "0.1.59"
axum = "0.6.1"
axum-macros = "0.3.0"
colored = "2.1.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'
Expand Down
9 changes: 4 additions & 5 deletions src/checkpoint_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand Down
41 changes: 22 additions & 19 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::errors::AppError;
use crate::processor::{process_to_displayable_format, DisplayableResult};
use async_trait::async_trait;
use futures::future::join_all;
use std::collections::HashMap;
use std::fmt;
use std::fmt::{Debug, Formatter};

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::{process_to_displayable_format, DisplayableResult};

#[derive(Debug)]
pub struct ResponsePayloadWithEndpointInfo {
Expand Down Expand Up @@ -45,7 +44,7 @@ pub struct CheckpointClient<C: HttpClient> {
#[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 {
Expand All @@ -62,12 +61,10 @@ pub struct EndpointsConfig {
pub endpoints: HashMap<String, Vec<String>>,
}

#[async_trait]
pub trait HttpClient {
async fn send_request(&self, path: String) -> Result<Response, AppError>;
fn send_request(&self, path: String) -> impl std::future::Future<Output = Result<Response, AppError>> + Send;
}

#[async_trait]
impl HttpClient for reqwest::Client {
async fn send_request(&self, path: String) -> Result<Response, AppError> {
self.get(path)
Expand All @@ -78,10 +75,17 @@ impl HttpClient for reqwest::Client {
}

impl<C: HttpClient> CheckpointClient<C> {
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,
}
}
Expand All @@ -91,11 +95,11 @@ impl<C: HttpClient> CheckpointClient<C> {
) -> Result<DisplayableResult, AppError> {
let endpoints_config = &self.endpoints_config;
let endpoints: &Vec<String> = 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 {
let raw_response = async {
async {
let path = format!(
"{}/eth/v1/beacon/states/{}/finality_checkpoints",
endpoint.clone(),
Expand All @@ -117,7 +121,7 @@ impl<C: HttpClient> CheckpointClient<C> {
payload: Err(AppError::EndpointResponseError(format!(
"Error with calling {} status code {}",
endpoint.clone(),
res.status().to_string()
res.status()
))),
endpoint: endpoint.clone(),
}
Expand All @@ -129,8 +133,7 @@ impl<C: HttpClient> CheckpointClient<C> {
},
}
}
.await;
raw_response
.await
}))
.await;

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 2 additions & 4 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate core;

use async_trait::async_trait;
use checkpointq_lib::client::{
BlockInfo, CheckpointClient, Data, EndpointsConfig, HttpClient, StateId, SuccessEndpointPayload,
};
Expand Down Expand Up @@ -43,7 +42,6 @@ impl MockClient {
}
}

#[async_trait]
impl HttpClient for MockClient {
async fn send_request(&self, path: String) -> Result<Response, AppError> {
let responses: (
Expand Down Expand Up @@ -177,7 +175,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);
Expand Down Expand Up @@ -310,7 +308,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);
Expand Down

0 comments on commit 3d2f42f

Please # to comment.