Skip to content

Commit 68ba5f4

Browse files
committed
split rpc calls into 3 Services
1 parent c57020d commit 68ba5f4

File tree

6 files changed

+194
-120
lines changed

6 files changed

+194
-120
lines changed

binaries/cuprated/src/rpc/handler.rs

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
//---------------------------------------------------------------------------------------------------- Use
44
use std::task::Poll;
55

6+
use cuprate_rpc_types::{
7+
bin::{BinRequest, BinResponse},
8+
json::{JsonRpcRequest, JsonRpcResponse},
9+
other::{OtherRequest, OtherResponse},
10+
};
611
use futures::channel::oneshot::channel;
712
use serde::{Deserialize, Serialize};
813
use tower::Service;
@@ -36,32 +41,57 @@ impl RpcHandler for CupratedRpcHandler {
3641
}
3742
}
3843

39-
impl Service<RpcRequest> for CupratedRpcHandler {
40-
type Response = RpcResponse;
44+
// INVARIANT:
45+
//
46+
// We don't need to check for `self.is_restricted()`
47+
// here because `cuprate-rpc-interface` handles that.
48+
//
49+
// We can assume the request coming has the required permissions.
50+
51+
impl Service<JsonRpcRequest> for CupratedRpcHandler {
52+
type Response = JsonRpcResponse;
4153
type Error = RpcError;
42-
type Future = InfallibleOneshotReceiver<Result<RpcResponse, RpcError>>;
54+
type Future = InfallibleOneshotReceiver<Result<JsonRpcResponse, RpcError>>;
4355

4456
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
4557
Poll::Ready(Ok(()))
4658
}
4759

48-
/// INVARIANT:
49-
///
50-
/// We don't need to check for `self.is_restricted()`
51-
/// here because `cuprate-rpc-interface` handles that.
52-
///
53-
/// We can assume the request coming has the required permissions.
54-
fn call(&mut self, req: RpcRequest) -> Self::Future {
60+
fn call(&mut self, request: JsonRpcRequest) -> Self::Future {
5561
let state = Self::clone(self);
62+
let response = json::map_request(state, request).expect("TODO");
63+
todo!()
64+
}
65+
}
5666

57-
let resp = match req {
58-
RpcRequest::JsonRpc(r) => {
59-
RpcResponse::JsonRpc(json::map_request(state, r).expect("TODO"))
60-
} // JSON-RPC 2.0 requests.
61-
RpcRequest::Binary(r) => RpcResponse::Binary(bin::map_request(state, r).expect("TODO")), // Binary requests.
62-
RpcRequest::Other(r) => RpcResponse::Other(other::map_request(state, r).expect("TODO")), // JSON (but not JSON-RPC) requests.
63-
};
67+
impl Service<BinRequest> for CupratedRpcHandler {
68+
type Response = BinResponse;
69+
type Error = RpcError;
70+
type Future = InfallibleOneshotReceiver<Result<BinResponse, RpcError>>;
6471

72+
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
73+
Poll::Ready(Ok(()))
74+
}
75+
76+
fn call(&mut self, request: BinRequest) -> Self::Future {
77+
let state = Self::clone(self);
78+
let response = bin::map_request(state, request).expect("TODO");
79+
todo!()
80+
}
81+
}
82+
83+
impl Service<OtherRequest> for CupratedRpcHandler {
84+
type Response = OtherResponse;
85+
type Error = RpcError;
86+
type Future = InfallibleOneshotReceiver<Result<OtherResponse, RpcError>>;
87+
88+
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
89+
Poll::Ready(Ok(()))
90+
}
91+
92+
fn call(&mut self, request: OtherRequest) -> Self::Future {
93+
let state = Self::clone(self);
94+
let response = other::map_request(state, request).expect("TODO");
6595
todo!()
6696
}
6797
}

rpc/interface/src/route/bin.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,8 @@ macro_rules! generate_endpoints_inner {
6767
paste::paste! {
6868
{
6969
// Send request.
70-
let request = RpcRequest::Binary($request);
71-
let channel = $handler.oneshot(request).await?;
70+
let response = $handler.oneshot($request).await?;
7271

73-
// Assert the response from the inner handler is correct.
74-
let RpcResponse::Binary(response) = channel else {
75-
panic!("RPC handler did not return a binary response");
76-
};
7772
let BinResponse::$variant(response) = response else {
7873
panic!("RPC handler returned incorrect response");
7974
};

rpc/interface/src/route/json_rpc.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@ pub(crate) async fn json_rpc<H: RpcHandler>(
5050
}
5151

5252
// Send request.
53-
let request = RpcRequest::JsonRpc(request.body);
54-
let channel = handler.oneshot(request).await?;
55-
56-
// Assert the response from the inner handler is correct.
57-
let RpcResponse::JsonRpc(response) = channel else {
58-
panic!("RPC handler returned incorrect response");
59-
};
53+
let response = handler.oneshot(request.body).await?;
6054

6155
Ok(Json(Response::ok(id, response)))
6256
}

rpc/interface/src/route/other.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,9 @@ macro_rules! generate_endpoints_inner {
8181
}
8282

8383
// Send request.
84-
let request = RpcRequest::Other(OtherRequest::$variant($request));
85-
let channel = $handler.oneshot(request).await?;
84+
let request = OtherRequest::$variant($request);
85+
let response = $handler.oneshot(request).await?;
8686

87-
// Assert the response from the inner handler is correct.
88-
let RpcResponse::Other(response) = channel else {
89-
panic!("RPC handler did not return a binary response");
90-
};
9187
let OtherResponse::$variant(response) = response else {
9288
panic!("RPC handler returned incorrect response")
9389
};

rpc/interface/src/rpc_handler.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
//---------------------------------------------------------------------------------------------------- Use
44
use std::future::Future;
55

6+
use cuprate_rpc_types::{
7+
bin::{BinRequest, BinResponse},
8+
json::{JsonRpcRequest, JsonRpcResponse},
9+
other::{OtherRequest, OtherResponse},
10+
};
611
use tower::Service;
712

813
use crate::{rpc_error::RpcError, rpc_request::RpcRequest, rpc_response::RpcResponse};
@@ -33,10 +38,22 @@ pub trait RpcHandler:
3338
+ Sync
3439
+ 'static
3540
+ Service<
36-
RpcRequest,
37-
Response = RpcResponse,
41+
JsonRpcRequest,
42+
Response = JsonRpcResponse,
3843
Error = RpcError,
39-
Future: Future<Output = Result<RpcResponse, RpcError>> + Send + Sync + 'static,
44+
Future: Future<Output = Result<JsonRpcResponse, RpcError>> + Send + Sync + 'static,
45+
>
46+
+ Service<
47+
OtherRequest,
48+
Response = OtherResponse,
49+
Error = RpcError,
50+
Future: Future<Output = Result<OtherResponse, RpcError>> + Send + Sync + 'static,
51+
>
52+
+ Service<
53+
BinRequest,
54+
Response = BinResponse,
55+
Error = RpcError,
56+
Future: Future<Output = Result<BinResponse, RpcError>> + Send + Sync + 'static,
4057
>
4158
{
4259
/// Is this [`RpcHandler`] restricted?

0 commit comments

Comments
 (0)