cuprate_rpc_interface/
rpc_handler.rs

1//! RPC handler trait.
2
3//---------------------------------------------------------------------------------------------------- Use
4use cuprate_rpc_types::{
5    bin::{BinRequest, BinResponse},
6    json::{JsonRpcRequest, JsonRpcResponse},
7    other::{OtherRequest, OtherResponse},
8};
9
10use crate::RpcService;
11
12//---------------------------------------------------------------------------------------------------- RpcHandler
13/// An RPC handler.
14///
15/// This trait represents a type that can turn `Request`s into `Response`s.
16///
17/// Implementors of this trait must be:
18/// - A [`tower::Service`] that uses [`JsonRpcRequest`] & [`JsonRpcResponse`]
19/// - A [`tower::Service`] that uses [`BinRequest`] & [`BinResponse`]
20/// - A [`tower::Service`] that uses [`OtherRequest`] & [`OtherResponse`]
21///
22/// In other words, an [`RpcHandler`] is a type that implements [`tower::Service`] 3 times,
23/// one for each request/response enum type found in [`cuprate_rpc_types`].
24///
25/// The error type must always be [`anyhow::Error`].
26///
27/// See this crate's `RpcHandlerDummy` for an implementation example of this trait.
28///
29/// # Panics
30/// Your [`RpcHandler`] must reply to `Request`s with the correct
31/// `Response` or else this crate will panic during routing functions.
32///
33/// For example, a [`JsonRpcRequest::GetBlockCount`] must be replied with
34/// [`JsonRpcResponse::GetBlockCount`]. If anything else is returned,
35/// this crate may panic.
36pub trait RpcHandler:
37    RpcService<JsonRpcRequest, JsonRpcResponse>
38    + RpcService<BinRequest, BinResponse>
39    + RpcService<OtherRequest, OtherResponse>
40{
41    /// Is this [`RpcHandler`] restricted?
42    ///
43    /// If this returns `true`, restricted methods and endpoints such as:
44    /// - `/json_rpc`'s `relay_tx` method
45    /// - The `/pop_blocks` endpoint
46    ///
47    /// will automatically be denied access when using the
48    /// [`axum::Router`] provided by [`RouterBuilder`](crate::RouterBuilder).
49    fn restricted(&self) -> bool;
50}