Add `block_info` api for new HTTP service (#509)

This commit is contained in:
Al Liu 2023-11-06 22:14:16 +08:00 committed by GitHub
parent 56e8506704
commit 196f395992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 4 deletions

View File

@ -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::<T>))
.route("/cl/status", routing::post(cl_status::<T>))
.route("/carnot/info", routing::get(carnot_info::<T, S, SIZE>))
.route("/carnot/blocks", routing::get(carnot_blocks::<T, S, SIZE>))
.route("/network/info", routing::get(libp2p_info))
.route("/storage/block", routing::post(block::<S, T>))
.route("/mempool/add/tx", routing::post(add_tx::<T>))
@ -215,6 +220,33 @@ where
make_request_and_return_response!(consensus::carnot_info::<Tx, SS, SIZE>(&store))
}
#[derive(Deserialize)]
struct QueryParams {
from: Option<BlockId>,
to: Option<BlockId>,
}
#[utoipa::path(
get,
path = "/carnot/blocks",
responses(
(status = 200, description = "Query the block information", body = Vec<consensus_engine::Block>),
(status = 500, description = "Internal server error", body = String),
)
)]
async fn carnot_blocks<Tx, SS, const SIZE: usize>(
State(store): State<OverwatchHandle>,
Query(query): Query<QueryParams>,
) -> Response
where
Tx: Transaction + Clone + Debug + Hash + Serialize + DeserializeOwned + Send + Sync + 'static,
<Tx as Transaction>::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::<Tx, SS, SIZE>(&store, from, to))
}
#[utoipa::path(
get,
path = "/network/info",

View File

@ -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<Tx, SS, const SIZE: usize> = CarnotConsensus<
>;
pub async fn carnot_info<Tx, SS, const SIZE: usize>(
handle: &overwatch_rs::overwatch::handle::OverwatchHandle,
) -> Result<CarnotInfo, overwatch_rs::DynError>
handle: &OverwatchHandle,
) -> Result<CarnotInfo, super::DynError>
where
Tx: Transaction + Clone + Debug + Hash + Serialize + DeserializeOwned + Send + Sync + 'static,
<Tx as Transaction>::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<Tx, SS, const SIZE: usize>(
handle: &OverwatchHandle,
from: Option<BlockId>,
to: Option<BlockId>,
) -> Result<Vec<Block>, super::DynError>
where
Tx: Transaction + Clone + Debug + Hash + Serialize + DeserializeOwned + Send + Sync + 'static,
<Tx as Transaction>::Hash: std::cmp::Ord + Debug + Send + Sync + 'static,
SS: StorageSerde + Send + Sync + 'static,
{
let relay = handle.relay::<Carnot<Tx, SS, SIZE>>().connect().await?;
let (sender, receiver) = oneshot::channel();
relay
.send(ConsensusMsg::GetBlocks {
from,
to,
tx: sender,
})
.await
.map_err(|(e, _)| e)?;
Ok(receiver.await?)
}