From 196f395992616380e0ba357d19da08c4f88722ae Mon Sep 17 00:00:00 2001 From: Al Liu Date: Mon, 6 Nov 2023 22:14:16 +0800 Subject: [PATCH] Add `block_info` api for new HTTP service (#509) --- nodes/nomos-node-api/src/http/backend/axum.rs | 34 ++++++++++++++++++- nodes/nomos-node-api/src/http/consensus.rs | 33 ++++++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/nodes/nomos-node-api/src/http/backend/axum.rs b/nodes/nomos-node-api/src/http/backend/axum.rs index 55e1d03b..90107728 100644 --- a/nodes/nomos-node-api/src/http/backend/axum.rs +++ b/nodes/nomos-node-api/src/http/backend/axum.rs @@ -1,6 +1,10 @@ use std::{fmt::Debug, hash::Hash, net::SocketAddr}; -use axum::{extract::State, response::Response, routing, Json, Router, Server}; +use axum::{ + extract::{Query, State}, + response::Response, + routing, Json, Router, Server, +}; use overwatch_rs::overwatch::handle::OverwatchHandle; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use utoipa::OpenApi; @@ -85,6 +89,7 @@ where .route("/cl/metrics", routing::get(cl_metrics::)) .route("/cl/status", routing::post(cl_status::)) .route("/carnot/info", routing::get(carnot_info::)) + .route("/carnot/blocks", routing::get(carnot_blocks::)) .route("/network/info", routing::get(libp2p_info)) .route("/storage/block", routing::post(block::)) .route("/mempool/add/tx", routing::post(add_tx::)) @@ -215,6 +220,33 @@ where make_request_and_return_response!(consensus::carnot_info::(&store)) } +#[derive(Deserialize)] +struct QueryParams { + from: Option, + to: Option, +} + +#[utoipa::path( + get, + path = "/carnot/blocks", + responses( + (status = 200, description = "Query the block information", body = Vec), + (status = 500, description = "Internal server error", body = String), + ) +)] +async fn carnot_blocks( + State(store): State, + Query(query): Query, +) -> Response +where + Tx: Transaction + Clone + Debug + Hash + Serialize + DeserializeOwned + Send + Sync + 'static, + ::Hash: std::cmp::Ord + Debug + Send + Sync + 'static, + SS: StorageSerde + Send + Sync + 'static, +{ + let QueryParams { from, to } = query; + make_request_and_return_response!(consensus::carnot_blocks::(&store, from, to)) +} + #[utoipa::path( get, path = "/network/info", diff --git a/nodes/nomos-node-api/src/http/consensus.rs b/nodes/nomos-node-api/src/http/consensus.rs index dc61ccd2..e7d0b48b 100644 --- a/nodes/nomos-node-api/src/http/consensus.rs +++ b/nodes/nomos-node-api/src/http/consensus.rs @@ -1,6 +1,9 @@ use std::{fmt::Debug, hash::Hash}; -use consensus_engine::overlay::{RandomBeaconState, RoundRobin, TreeOverlay}; +use consensus_engine::{ + overlay::{RandomBeaconState, RoundRobin, TreeOverlay}, + Block, BlockId, +}; use full_replication::Certificate; use nomos_consensus::{ network::adapters::libp2p::Libp2pAdapter as ConsensusLibp2pAdapter, CarnotConsensus, @@ -17,6 +20,7 @@ use nomos_mempool::{ backend::mockpool::MockPool, network::adapters::libp2p::Libp2pAdapter as MempoolLibp2pAdapter, }; use nomos_storage::backends::{sled::SledBackend, StorageSerde}; +use overwatch_rs::overwatch::handle::OverwatchHandle; use serde::{de::DeserializeOwned, Serialize}; use tokio::sync::oneshot; @@ -36,8 +40,8 @@ pub type Carnot = CarnotConsensus< >; pub async fn carnot_info( - handle: &overwatch_rs::overwatch::handle::OverwatchHandle, -) -> Result + handle: &OverwatchHandle, +) -> Result where Tx: Transaction + Clone + Debug + Hash + Serialize + DeserializeOwned + Send + Sync + 'static, ::Hash: std::cmp::Ord + Debug + Send + Sync + 'static, @@ -51,3 +55,26 @@ where .map_err(|(e, _)| e)?; Ok(receiver.await?) } + +pub async fn carnot_blocks( + handle: &OverwatchHandle, + from: Option, + to: Option, +) -> Result, super::DynError> +where + Tx: Transaction + Clone + Debug + Hash + Serialize + DeserializeOwned + Send + Sync + 'static, + ::Hash: std::cmp::Ord + Debug + Send + Sync + 'static, + SS: StorageSerde + Send + Sync + 'static, +{ + let relay = handle.relay::>().connect().await?; + let (sender, receiver) = oneshot::channel(); + relay + .send(ConsensusMsg::GetBlocks { + from, + to, + tx: sender, + }) + .await + .map_err(|(e, _)| e)?; + Ok(receiver.await?) +}