mirror of
https://github.com/Cuprate/cuprate.git
synced 2025-01-26 20:36:00 +00:00
interface: extract out to RpcService
This commit is contained in:
parent
68ba5f4781
commit
7487aa4779
12 changed files with 92 additions and 60 deletions
|
@ -1,4 +1,4 @@
|
||||||
use cuprate_rpc_interface::{RpcError, RpcResponse};
|
use cuprate_rpc_interface::RpcError;
|
||||||
use cuprate_rpc_types::{
|
use cuprate_rpc_types::{
|
||||||
bin::{
|
bin::{
|
||||||
BinRequest, BinResponse, GetBlocksByHeightRequest, GetBlocksByHeightResponse,
|
BinRequest, BinResponse, GetBlocksByHeightRequest, GetBlocksByHeightResponse,
|
||||||
|
|
|
@ -15,7 +15,7 @@ use tower::Service;
|
||||||
use cuprate_blockchain::service::BlockchainReadHandle;
|
use cuprate_blockchain::service::BlockchainReadHandle;
|
||||||
use cuprate_helper::asynch::InfallibleOneshotReceiver;
|
use cuprate_helper::asynch::InfallibleOneshotReceiver;
|
||||||
use cuprate_json_rpc::Id;
|
use cuprate_json_rpc::Id;
|
||||||
use cuprate_rpc_interface::{RpcError, RpcHandler, RpcRequest, RpcResponse};
|
use cuprate_rpc_interface::{RpcError, RpcHandler};
|
||||||
use cuprate_txpool::service::TxpoolReadHandle;
|
use cuprate_txpool::service::TxpoolReadHandle;
|
||||||
|
|
||||||
use crate::rpc::{bin, json, other};
|
use crate::rpc::{bin, json, other};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use cuprate_rpc_interface::{RpcError, RpcResponse};
|
use cuprate_rpc_interface::RpcError;
|
||||||
use cuprate_rpc_types::json::{
|
use cuprate_rpc_types::json::{
|
||||||
AddAuxPowRequest, AddAuxPowResponse, BannedRequest, BannedResponse, CalcPowRequest,
|
AddAuxPowRequest, AddAuxPowResponse, BannedRequest, BannedResponse, CalcPowRequest,
|
||||||
CalcPowResponse, FlushCacheRequest, FlushCacheResponse, FlushTransactionPoolRequest,
|
CalcPowResponse, FlushCacheRequest, FlushCacheResponse, FlushTransactionPoolRequest,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use cuprate_rpc_interface::{RpcError, RpcResponse};
|
use cuprate_rpc_interface::RpcError;
|
||||||
use cuprate_rpc_types::other::{
|
use cuprate_rpc_types::other::{
|
||||||
GetAltBlocksHashesRequest, GetAltBlocksHashesResponse, GetHeightRequest, GetHeightResponse,
|
GetAltBlocksHashesRequest, GetAltBlocksHashesResponse, GetHeightRequest, GetHeightResponse,
|
||||||
GetLimitRequest, GetLimitResponse, GetNetStatsRequest, GetNetStatsResponse, GetOutsRequest,
|
GetLimitRequest, GetLimitResponse, GetNetStatsRequest, GetNetStatsResponse, GetOutsRequest,
|
||||||
|
|
|
@ -17,7 +17,7 @@ CLIENT ─► ROUTE ─► REQUEST ─► HANDLER ─► RESPONSE ─► CLIENT
|
||||||
|
|
||||||
Everything coming _in_ from a client is handled by this crate.
|
Everything coming _in_ from a client is handled by this crate.
|
||||||
|
|
||||||
This is where your [`RpcHandler`] turns this [`RpcRequest`] into a [`RpcResponse`].
|
This is where your [`RpcHandler`] turns this `Request` into a `Response`.
|
||||||
|
|
||||||
You hand this `Response` back to `cuprate-rpc-interface` and it will take care of sending it back to the client.
|
You hand this `Response` back to `cuprate-rpc-interface` and it will take care of sending it back to the client.
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ This crate simply handles:
|
||||||
- Defining handler function signatures
|
- Defining handler function signatures
|
||||||
- (De)serialization of requests/responses (JSON-RPC, binary, JSON)
|
- (De)serialization of requests/responses (JSON-RPC, binary, JSON)
|
||||||
|
|
||||||
The actual server details are all handled by the [`axum`] and [`tower`] ecosystem.
|
The actual server details are all handled by the [`axum`] and [`tower`] ecosystem.a
|
||||||
|
|
||||||
The proper usage of this crate is to:
|
The proper usage of this crate is to:
|
||||||
1. Implement a [`RpcHandler`]
|
1. Implement a [`RpcHandler`]
|
||||||
|
@ -42,16 +42,18 @@ The proper usage of this crate is to:
|
||||||
3. Do whatever with it
|
3. Do whatever with it
|
||||||
|
|
||||||
# The [`RpcHandler`]
|
# The [`RpcHandler`]
|
||||||
This is your [`tower::Service`] that converts [`RpcRequest`]s into [`RpcResponse`]s,
|
This is your [`tower::Service`] that converts `Request`s into `Response`s,
|
||||||
i.e. the "inner handler".
|
i.e. the "inner handler".
|
||||||
|
|
||||||
Said concretely, `RpcHandler` is a `tower::Service` where the associated types are from this crate:
|
Said concretely, `RpcHandler` is 3 `tower::Service`s where the associated types are
|
||||||
- [`RpcRequest`]
|
the 3 endpoint enums from [`cuprate_rpc_types`] and the error type from this crate:
|
||||||
- [`RpcResponse`]
|
- [`JsonRpcRequest`](cuprate_rpc_types::json::JsonRpcRequest) & [`JsonRpcResponse`](cuprate_rpc_types::json::JsonRpcResponse)
|
||||||
|
- [`BinRequest`](cuprate_rpc_types::bin::BinRequest) & [`BinResponse`](cuprate_rpc_types::bin::BinRequest)
|
||||||
|
- [`OtherRequest`](cuprate_rpc_types::other::OtherRequest) & [`OtherResponse`](cuprate_rpc_types::other::OtherRequest)
|
||||||
- [`RpcError`]
|
- [`RpcError`]
|
||||||
|
|
||||||
`RpcHandler`'s [`Future`](std::future::Future) is generic, _although_,
|
`RpcHandler`'s [`Future`](std::future::Future) is generic, _although_,
|
||||||
it must output `Result<RpcResponse, RpcError>`.
|
it must output `Result<$RESPONSE, RpcError>`.
|
||||||
|
|
||||||
The `RpcHandler` must also hold some state that is required
|
The `RpcHandler` must also hold some state that is required
|
||||||
for RPC server operation.
|
for RPC server operation.
|
||||||
|
@ -83,7 +85,7 @@ use cuprate_rpc_types::{
|
||||||
json::{JsonRpcRequest, JsonRpcResponse, GetBlockCountResponse},
|
json::{JsonRpcRequest, JsonRpcResponse, GetBlockCountResponse},
|
||||||
other::{OtherRequest, OtherResponse},
|
other::{OtherRequest, OtherResponse},
|
||||||
};
|
};
|
||||||
use cuprate_rpc_interface::{RouterBuilder, RpcHandlerDummy, RpcRequest};
|
use cuprate_rpc_interface::{RouterBuilder, RpcHandlerDummy, Request};
|
||||||
|
|
||||||
// Send a `/get_height` request. This endpoint has no inputs.
|
// Send a `/get_height` request. This endpoint has no inputs.
|
||||||
async fn get_height(port: u16) -> OtherResponse {
|
async fn get_height(port: u16) -> OtherResponse {
|
||||||
|
|
|
@ -7,16 +7,18 @@ mod rpc_error;
|
||||||
mod rpc_handler;
|
mod rpc_handler;
|
||||||
#[cfg(feature = "dummy")]
|
#[cfg(feature = "dummy")]
|
||||||
mod rpc_handler_dummy;
|
mod rpc_handler_dummy;
|
||||||
mod rpc_request;
|
// mod rpc_request;
|
||||||
mod rpc_response;
|
// mod rpc_response;
|
||||||
|
mod rpc_service;
|
||||||
|
|
||||||
pub use router_builder::RouterBuilder;
|
pub use router_builder::RouterBuilder;
|
||||||
pub use rpc_error::RpcError;
|
pub use rpc_error::RpcError;
|
||||||
pub use rpc_handler::RpcHandler;
|
pub use rpc_handler::RpcHandler;
|
||||||
#[cfg(feature = "dummy")]
|
#[cfg(feature = "dummy")]
|
||||||
pub use rpc_handler_dummy::RpcHandlerDummy;
|
pub use rpc_handler_dummy::RpcHandlerDummy;
|
||||||
pub use rpc_request::RpcRequest;
|
// pub use rpc_request::RpcRequest;
|
||||||
pub use rpc_response::RpcResponse;
|
// pub use rpc_response::RpcResponse;
|
||||||
|
pub use rpc_service::RpcService;
|
||||||
|
|
||||||
// false-positive: used in `README.md`'s doc-test.
|
// false-positive: used in `README.md`'s doc-test.
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -7,7 +7,7 @@ use tower::ServiceExt;
|
||||||
use cuprate_epee_encoding::from_bytes;
|
use cuprate_epee_encoding::from_bytes;
|
||||||
use cuprate_rpc_types::bin::{BinRequest, BinResponse, GetTransactionPoolHashesRequest};
|
use cuprate_rpc_types::bin::{BinRequest, BinResponse, GetTransactionPoolHashesRequest};
|
||||||
|
|
||||||
use crate::{rpc_handler::RpcHandler, rpc_request::RpcRequest, rpc_response::RpcResponse};
|
use crate::rpc_handler::RpcHandler;
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Routes
|
//---------------------------------------------------------------------------------------------------- Routes
|
||||||
/// This macro generates route functions that expect input.
|
/// This macro generates route functions that expect input.
|
||||||
|
|
|
@ -15,7 +15,7 @@ use cuprate_rpc_types::{
|
||||||
RpcCallValue,
|
RpcCallValue,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{rpc_handler::RpcHandler, rpc_request::RpcRequest, rpc_response::RpcResponse};
|
use crate::rpc_handler::RpcHandler;
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Routes
|
//---------------------------------------------------------------------------------------------------- Routes
|
||||||
/// The `/json_rpc` route function used in [`crate::RouterBuilder`].
|
/// The `/json_rpc` route function used in [`crate::RouterBuilder`].
|
||||||
|
|
|
@ -25,7 +25,7 @@ use cuprate_rpc_types::{
|
||||||
RpcCall,
|
RpcCall,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{rpc_handler::RpcHandler, rpc_request::RpcRequest, rpc_response::RpcResponse};
|
use crate::rpc_handler::RpcHandler;
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Routes
|
//---------------------------------------------------------------------------------------------------- Routes
|
||||||
/// This macro generates route functions that expect input.
|
/// This macro generates route functions that expect input.
|
||||||
|
|
|
@ -1,60 +1,42 @@
|
||||||
//! RPC handler trait.
|
//! RPC handler trait.
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Use
|
//---------------------------------------------------------------------------------------------------- Use
|
||||||
use std::future::Future;
|
|
||||||
|
|
||||||
use cuprate_rpc_types::{
|
use cuprate_rpc_types::{
|
||||||
bin::{BinRequest, BinResponse},
|
bin::{BinRequest, BinResponse},
|
||||||
json::{JsonRpcRequest, JsonRpcResponse},
|
json::{JsonRpcRequest, JsonRpcResponse},
|
||||||
other::{OtherRequest, OtherResponse},
|
other::{OtherRequest, OtherResponse},
|
||||||
};
|
};
|
||||||
use tower::Service;
|
|
||||||
|
|
||||||
use crate::{rpc_error::RpcError, rpc_request::RpcRequest, rpc_response::RpcResponse};
|
use crate::RpcService;
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- RpcHandler
|
//---------------------------------------------------------------------------------------------------- RpcHandler
|
||||||
/// An RPC handler.
|
/// An RPC handler.
|
||||||
///
|
///
|
||||||
/// This trait represents a type that can turn [`RpcRequest`]s into [`RpcResponse`]s.
|
/// This trait represents a type that can turn `Request`s into `Response`s.
|
||||||
///
|
///
|
||||||
/// Implementors of this trait must be [`tower::Service`]s that use:
|
/// Implementors of this trait must be:
|
||||||
/// - [`RpcRequest`] as the generic `Request` type
|
/// - A [`tower::Service`]s that use [`JsonRpcRequest`] & [`JsonRpcResponse`]
|
||||||
/// - [`RpcResponse`] as the associated `Response` type
|
/// - A [`tower::Service`]s that use [`BinRequest`] & [`BinResponse`]
|
||||||
/// - [`RpcError`] as the associated `Error` type
|
/// - A [`tower::Service`]s that use [`OtherRequest`] & [`OtherResponse`]
|
||||||
/// - A generic [`Future`] that outputs `Result<RpcResponse, RpcError>`
|
///
|
||||||
|
/// In other words, an [`RpcHandler`] is a type that implements [`tower::Service`] 3 times,
|
||||||
|
/// one for each endpoint enum type found in [`cuprate_rpc_types`].
|
||||||
|
///
|
||||||
|
/// The error type must always be [`RpcError`](crate::RpcError).
|
||||||
///
|
///
|
||||||
/// See this crate's `RpcHandlerDummy` for an implementation example of this trait.
|
/// See this crate's `RpcHandlerDummy` for an implementation example of this trait.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// Your [`RpcHandler`] must reply to [`RpcRequest`]s with the correct
|
/// Your [`RpcHandler`] must reply to `Request`s with the correct
|
||||||
/// [`RpcResponse`] or else this crate will panic during routing functions.
|
/// `Response` or else this crate will panic during routing functions.
|
||||||
///
|
///
|
||||||
/// For example, upon a [`RpcRequest::Binary`] must be replied with
|
/// For example, upon a [`JsonRpcRequest::GetBlockCount`] must be replied with
|
||||||
/// [`RpcRequest::Binary`]. If an [`RpcRequest::Other`] were returned instead,
|
/// [`JsonRpcResponse::GetBlockCount`]. If anything else is returned,
|
||||||
/// this crate would panic.
|
/// this crate may panic.
|
||||||
pub trait RpcHandler:
|
pub trait RpcHandler:
|
||||||
Clone
|
RpcService<JsonRpcRequest, JsonRpcResponse>
|
||||||
+ Send
|
+ RpcService<BinRequest, BinResponse>
|
||||||
+ Sync
|
+ RpcService<OtherRequest, OtherResponse>
|
||||||
+ 'static
|
|
||||||
+ Service<
|
|
||||||
JsonRpcRequest,
|
|
||||||
Response = JsonRpcResponse,
|
|
||||||
Error = RpcError,
|
|
||||||
Future: Future<Output = Result<JsonRpcResponse, RpcError>> + Send + Sync + 'static,
|
|
||||||
>
|
|
||||||
+ Service<
|
|
||||||
OtherRequest,
|
|
||||||
Response = OtherResponse,
|
|
||||||
Error = RpcError,
|
|
||||||
Future: Future<Output = Result<OtherResponse, RpcError>> + Send + Sync + 'static,
|
|
||||||
>
|
|
||||||
+ Service<
|
|
||||||
BinRequest,
|
|
||||||
Response = BinResponse,
|
|
||||||
Error = RpcError,
|
|
||||||
Future: Future<Output = Result<BinResponse, RpcError>> + Send + Sync + 'static,
|
|
||||||
>
|
|
||||||
{
|
{
|
||||||
/// Is this [`RpcHandler`] restricted?
|
/// Is this [`RpcHandler`] restricted?
|
||||||
///
|
///
|
||||||
|
|
|
@ -15,10 +15,7 @@ use tower::Service;
|
||||||
|
|
||||||
use cuprate_helper::asynch::InfallibleOneshotReceiver;
|
use cuprate_helper::asynch::InfallibleOneshotReceiver;
|
||||||
|
|
||||||
use crate::{
|
use crate::{rpc_error::RpcError, rpc_handler::RpcHandler};
|
||||||
rpc_error::RpcError, rpc_handler::RpcHandler, rpc_request::RpcRequest,
|
|
||||||
rpc_response::RpcResponse,
|
|
||||||
};
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- RpcHandlerDummy
|
//---------------------------------------------------------------------------------------------------- RpcHandlerDummy
|
||||||
/// An [`RpcHandler`] that always returns [`Default::default`].
|
/// An [`RpcHandler`] that always returns [`Default::default`].
|
||||||
|
|
49
rpc/interface/src/rpc_service.rs
Normal file
49
rpc/interface/src/rpc_service.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
//! RPC [`tower::Service`] trait.
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------- Use
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
use tower::Service;
|
||||||
|
|
||||||
|
use crate::rpc_error::RpcError;
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------- RpcService
|
||||||
|
/// An RPC [`tower::Service`].
|
||||||
|
///
|
||||||
|
/// This trait solely exists to encapsulate the traits needed
|
||||||
|
/// to handle RPC requests and respond with responses - **it is
|
||||||
|
/// not meant to be used directly.**
|
||||||
|
///
|
||||||
|
/// The `Request` and `Response` are generic and
|
||||||
|
/// are used in the [`tower::Service`] bounds.
|
||||||
|
///
|
||||||
|
/// The error type is always [`RpcError`].
|
||||||
|
///
|
||||||
|
/// See [`RpcHandler`](crate::RpcHandler) for more information.
|
||||||
|
pub trait RpcService<Request, Response>:
|
||||||
|
Clone
|
||||||
|
+ Send
|
||||||
|
+ Sync
|
||||||
|
+ 'static
|
||||||
|
+ Service<
|
||||||
|
Request,
|
||||||
|
Response = Response,
|
||||||
|
Error = RpcError,
|
||||||
|
Future: Future<Output = Result<Response, RpcError>> + Send + Sync + 'static,
|
||||||
|
>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Request, Response, T> RpcService<Request, Response> for T where
|
||||||
|
Self: Clone
|
||||||
|
+ Send
|
||||||
|
+ Sync
|
||||||
|
+ 'static
|
||||||
|
+ Service<
|
||||||
|
Request,
|
||||||
|
Response = Response,
|
||||||
|
Error = RpcError,
|
||||||
|
Future: Future<Output = Result<Response, RpcError>> + Send + Sync + 'static,
|
||||||
|
>
|
||||||
|
{
|
||||||
|
}
|
Loading…
Reference in a new issue