From c57020d805f06528ec7092de499a8f88ef7997db Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Mon, 2 Sep 2024 19:58:38 -0400 Subject: [PATCH] interface: handle json-rpc concepts --- binaries/cuprated/src/rpc.rs | 3 ++- binaries/cuprated/src/rpc/handler.rs | 3 --- rpc/interface/src/route/json_rpc.rs | 20 +++++++++----------- rpc/interface/src/rpc_handler_dummy.rs | 1 - rpc/interface/src/rpc_response.rs | 1 + 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/binaries/cuprated/src/rpc.rs b/binaries/cuprated/src/rpc.rs index 55051e1a..43b68783 100644 --- a/binaries/cuprated/src/rpc.rs +++ b/binaries/cuprated/src/rpc.rs @@ -2,7 +2,8 @@ //! //! Will contain the code to initiate the RPC and a request handler. -#![allow(clippy::needless_pass_by_value)] // TODO: remove after impl. +// TODO: remove after impl. +#![allow(dead_code, unused_variables, clippy::needless_pass_by_value)] mod bin; mod handler; diff --git a/binaries/cuprated/src/rpc/handler.rs b/binaries/cuprated/src/rpc/handler.rs index 86897f13..ae997826 100644 --- a/binaries/cuprated/src/rpc/handler.rs +++ b/binaries/cuprated/src/rpc/handler.rs @@ -63,8 +63,5 @@ impl Service for CupratedRpcHandler { }; todo!() - // let (tx, rx) = channel(); - // drop(tx.send(Ok(resp))); - // InfallibleOneshotReceiver::from(rx) } } diff --git a/rpc/interface/src/route/json_rpc.rs b/rpc/interface/src/route/json_rpc.rs index 28ab80c1..16dc6005 100644 --- a/rpc/interface/src/route/json_rpc.rs +++ b/rpc/interface/src/route/json_rpc.rs @@ -8,7 +8,7 @@ use tower::ServiceExt; use cuprate_json_rpc::{ error::{ErrorCode, ErrorObject}, - Id, + Id, Response, }; use cuprate_rpc_types::{ json::{JsonRpcRequest, JsonRpcResponse}, @@ -22,7 +22,7 @@ use crate::{rpc_handler::RpcHandler, rpc_request::RpcRequest, rpc_response::RpcR pub(crate) async fn json_rpc( State(handler): State, Json(request): Json>, -) -> Result>, StatusCode> { +) -> Result>, StatusCode> { // TODO: // // JSON-RPC notifications (requests without `id`) @@ -30,6 +30,11 @@ pub(crate) async fn json_rpc( // must remain. How to do this considering this function will // always return and cause `axum` to respond? + // JSON-RPC 2.0 rule: + // If there was an error in detecting the `Request`'s ID, + // the `Response` must contain an `Id::Null` + let id = request.id.unwrap_or(Id::Null); + // Return early if this RPC server is restricted and // the requested method is only for non-restricted RPC. if request.body.is_restricted() && handler.restricted() { @@ -39,12 +44,7 @@ pub(crate) async fn json_rpc( data: None, }; - // JSON-RPC 2.0 rule: - // If there was an error in detecting the `Request`'s ID, - // the `Response` must contain an `Id::Null` - let id = request.id.unwrap_or(Id::Null); - - let response = cuprate_json_rpc::Response::err(id, error_object); + let response = Response::err(id, error_object); return Ok(Json(response)); } @@ -58,9 +58,7 @@ pub(crate) async fn json_rpc( panic!("RPC handler returned incorrect response"); }; - let response = todo!(); - - Ok(Json(response)) + Ok(Json(Response::ok(id, response))) } //---------------------------------------------------------------------------------------------------- Tests diff --git a/rpc/interface/src/rpc_handler_dummy.rs b/rpc/interface/src/rpc_handler_dummy.rs index 22c7fec4..6a8532f7 100644 --- a/rpc/interface/src/rpc_handler_dummy.rs +++ b/rpc/interface/src/rpc_handler_dummy.rs @@ -9,7 +9,6 @@ use serde::{Deserialize, Serialize}; use tower::Service; use cuprate_helper::asynch::InfallibleOneshotReceiver; -use cuprate_json_rpc::Id; use crate::{ rpc_error::RpcError, rpc_handler::RpcHandler, rpc_request::RpcRequest, diff --git a/rpc/interface/src/rpc_response.rs b/rpc/interface/src/rpc_response.rs index 8b615958..23a0729a 100644 --- a/rpc/interface/src/rpc_response.rs +++ b/rpc/interface/src/rpc_response.rs @@ -17,6 +17,7 @@ use cuprate_rpc_types::{bin::BinResponse, json::JsonRpcResponse, other::OtherRes /// It is the `Response` type required to be used in an [`RpcHandler`](crate::RpcHandler). #[derive(Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +#[allow(clippy::large_enum_variant)] // FIXME: maybe don't wrap internally with an enum? pub enum RpcResponse { /// JSON RPC 2.0 responses. JsonRpc(JsonRpcResponse),