interface: handle json-rpc concepts

This commit is contained in:
hinto.janai 2024-09-02 19:58:38 -04:00
parent 5f37771729
commit c57020d805
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
5 changed files with 12 additions and 16 deletions

View file

@ -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;

View file

@ -63,8 +63,5 @@ impl Service<RpcRequest> for CupratedRpcHandler {
};
todo!()
// let (tx, rx) = channel();
// drop(tx.send(Ok(resp)));
// InfallibleOneshotReceiver::from(rx)
}
}

View file

@ -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<H: RpcHandler>(
State(handler): State<H>,
Json(request): Json<cuprate_json_rpc::Request<JsonRpcRequest>>,
) -> Result<Json<cuprate_json_rpc::Response<JsonRpcResponse>>, StatusCode> {
) -> Result<Json<Response<JsonRpcResponse>>, StatusCode> {
// TODO: <https://www.jsonrpc.org/specification#notification>
//
// JSON-RPC notifications (requests without `id`)
@ -30,6 +30,11 @@ pub(crate) async fn json_rpc<H: RpcHandler>(
// 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<H: RpcHandler>(
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<H: RpcHandler>(
panic!("RPC handler returned incorrect response");
};
let response = todo!();
Ok(Json(response))
Ok(Json(Response::ok(id, response)))
}
//---------------------------------------------------------------------------------------------------- Tests

View file

@ -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,

View file

@ -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),