mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-11-16 15:58:14 +00:00
interface: call handler in routes
This commit is contained in:
parent
35d1cebd7c
commit
d57a5c9a3f
4 changed files with 46 additions and 25 deletions
|
@ -2,10 +2,20 @@
|
|||
|
||||
//---------------------------------------------------------------------------------------------------- Import
|
||||
|
||||
use axum::http::StatusCode;
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- TODO
|
||||
/// TODO
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum Error {}
|
||||
|
||||
impl From<Error> for StatusCode {
|
||||
fn from(value: Error) -> Self {
|
||||
// TODO
|
||||
Self::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Tests
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
|
|
@ -3,11 +3,12 @@
|
|||
|
||||
//---------------------------------------------------------------------------------------------------- Import
|
||||
use axum::{body::Bytes, extract::State, http::StatusCode};
|
||||
use tower::ServiceExt;
|
||||
|
||||
use cuprate_epee_encoding::from_bytes;
|
||||
use cuprate_rpc_types::bin::{BinRequest, BinResponse};
|
||||
|
||||
use crate::{response::Response, rpc_handler::RpcHandler};
|
||||
use crate::{request::Request, response::Response, rpc_handler::RpcHandler};
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Routes
|
||||
/// TODO
|
||||
|
@ -27,12 +28,14 @@ macro_rules! generate_endpoints {
|
|||
from_bytes(&mut request).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
);
|
||||
|
||||
// TODO: call handler
|
||||
// Send request.
|
||||
let request = Request::Binary(request);
|
||||
let channel = handler.oneshot(request).await?;
|
||||
|
||||
// Assert the response from the inner handler is correct.
|
||||
let Response::Binary(response) = todo!() else {
|
||||
panic!("RPC handler did not return a binary response");
|
||||
};
|
||||
|
||||
// Assert the response from the inner handler is correct.
|
||||
let BinResponse::$variant(response) = response else {
|
||||
panic!("RPC handler returned incorrect response");
|
||||
};
|
||||
|
|
|
@ -5,17 +5,18 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use axum::{extract::State, http::StatusCode, Json};
|
||||
use tower::ServiceExt;
|
||||
|
||||
use cuprate_json_rpc::{
|
||||
error::{ErrorCode, ErrorObject},
|
||||
Id,
|
||||
};
|
||||
|
||||
use cuprate_rpc_types::{
|
||||
json::{JsonRpcRequest, JsonRpcResponse},
|
||||
RpcRequest,
|
||||
};
|
||||
|
||||
use crate::{response::Response, rpc_handler::RpcHandler};
|
||||
use crate::{request::Request, response::Response, rpc_handler::RpcHandler};
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Routes
|
||||
/// TODO
|
||||
|
@ -28,7 +29,7 @@ pub(crate) async fn json_rpc<H: RpcHandler>(
|
|||
if request.body.is_restricted() && handler.restricted() {
|
||||
let error_object = ErrorObject {
|
||||
code: ErrorCode::ServerError(-1 /* TODO */),
|
||||
message: Cow::Borrowed("Restricted. TODO"),
|
||||
message: Cow::Borrowed("Restricted. TODO: mimic monerod message"),
|
||||
data: None,
|
||||
};
|
||||
|
||||
|
@ -39,12 +40,15 @@ pub(crate) async fn json_rpc<H: RpcHandler>(
|
|||
|
||||
let response = cuprate_json_rpc::Response::err(id, error_object);
|
||||
|
||||
// TODO
|
||||
return Ok(Json(response));
|
||||
}
|
||||
|
||||
// TODO: call handler
|
||||
let Response::JsonRpc(response) = todo!() else {
|
||||
// Send request.
|
||||
let request = Request::JsonRpc(request);
|
||||
let channel = handler.oneshot(request).await?;
|
||||
|
||||
// Assert the response from the inner handler is correct.
|
||||
let Response::JsonRpc(response) = channel else {
|
||||
panic!("RPC handler returned incorrect response");
|
||||
};
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
#![allow(clippy::unused_async)] // TODO: remove after impl
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Import
|
||||
|
||||
use axum::{extract::State, http::StatusCode, Json};
|
||||
use tower::ServiceExt;
|
||||
|
||||
use cuprate_rpc_types::{
|
||||
other::{
|
||||
|
@ -15,18 +15,18 @@ use cuprate_rpc_types::{
|
|||
GetTransactionPoolStatsResponse, GetTransactionsRequest, GetTransactionsResponse,
|
||||
GetTxIdsLooseRequest, GetTxIdsLooseResponse, InPeersRequest, InPeersResponse,
|
||||
IsKeyImageSpentRequest, IsKeyImageSpentResponse, MiningStatusRequest, MiningStatusResponse,
|
||||
OtherResponse, OutPeersRequest, OutPeersResponse, PopBlocksRequest, PopBlocksResponse,
|
||||
SaveBcRequest, SaveBcResponse, SendRawTransactionRequest, SendRawTransactionResponse,
|
||||
SetBootstrapDaemonRequest, SetBootstrapDaemonResponse, SetLimitRequest, SetLimitResponse,
|
||||
SetLogCategoriesRequest, SetLogCategoriesResponse, SetLogHashRateRequest,
|
||||
SetLogHashRateResponse, SetLogLevelRequest, SetLogLevelResponse, StartMiningRequest,
|
||||
StartMiningResponse, StopDaemonRequest, StopDaemonResponse, StopMiningRequest,
|
||||
StopMiningResponse, UpdateRequest, UpdateResponse,
|
||||
OtherRequest, OtherResponse, OutPeersRequest, OutPeersResponse, PopBlocksRequest,
|
||||
PopBlocksResponse, SaveBcRequest, SaveBcResponse, SendRawTransactionRequest,
|
||||
SendRawTransactionResponse, SetBootstrapDaemonRequest, SetBootstrapDaemonResponse,
|
||||
SetLimitRequest, SetLimitResponse, SetLogCategoriesRequest, SetLogCategoriesResponse,
|
||||
SetLogHashRateRequest, SetLogHashRateResponse, SetLogLevelRequest, SetLogLevelResponse,
|
||||
StartMiningRequest, StartMiningResponse, StopDaemonRequest, StopDaemonResponse,
|
||||
StopMiningRequest, StopMiningResponse, UpdateRequest, UpdateResponse,
|
||||
},
|
||||
RpcRequest,
|
||||
};
|
||||
|
||||
use crate::{response::Response, rpc_handler::RpcHandler};
|
||||
use crate::{request::Request, response::Response, rpc_handler::RpcHandler};
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Routes
|
||||
/// TODO
|
||||
|
@ -38,19 +38,23 @@ macro_rules! generate_endpoints {
|
|||
/// TODO
|
||||
#[allow(unused_mut)]
|
||||
pub(crate) async fn $endpoint<H: RpcHandler>(
|
||||
State(handler): State<H>,
|
||||
State(mut handler): State<H>,
|
||||
Json(request): Json<[<$variant Request>]>,
|
||||
) -> Result<Json<[<$variant Response>]>, StatusCode> {
|
||||
// Check if restricted.
|
||||
if request.is_restricted() && handler.restricted() {
|
||||
todo!();
|
||||
// TODO: mimic `monerod` behavior.
|
||||
return Err(StatusCode::FORBIDDEN);
|
||||
}
|
||||
|
||||
// TODO: call handler
|
||||
let Response::Other(response) = todo!() else {
|
||||
panic!("RPC handler did not return a binary response");
|
||||
};
|
||||
// Send request.
|
||||
let request = Request::Other(OtherRequest::$variant(request));
|
||||
let channel = handler.oneshot(request).await?;
|
||||
|
||||
// Assert the response from the inner handler is correct.
|
||||
let Response::Other(response) = channel else {
|
||||
panic!("RPC handler did not return a binary response");
|
||||
};
|
||||
let OtherResponse::$variant(response) = response else {
|
||||
panic!("RPC handler returned incorrect response")
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue