cuprate_rpc_interface/
rpc_handler.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! RPC handler trait.

//---------------------------------------------------------------------------------------------------- Use
use cuprate_rpc_types::{
    bin::{BinRequest, BinResponse},
    json::{JsonRpcRequest, JsonRpcResponse},
    other::{OtherRequest, OtherResponse},
};

use crate::RpcService;

//---------------------------------------------------------------------------------------------------- RpcHandler
/// An RPC handler.
///
/// This trait represents a type that can turn `Request`s into `Response`s.
///
/// Implementors of this trait must be:
/// - A [`tower::Service`] that uses [`JsonRpcRequest`] & [`JsonRpcResponse`]
/// - A [`tower::Service`] that uses [`BinRequest`] & [`BinResponse`]
/// - A [`tower::Service`] that uses [`OtherRequest`] & [`OtherResponse`]
///
/// In other words, an [`RpcHandler`] is a type that implements [`tower::Service`] 3 times,
/// one for each request/response enum type found in [`cuprate_rpc_types`].
///
/// The error type must always be [`anyhow::Error`].
///
/// See this crate's `RpcHandlerDummy` for an implementation example of this trait.
///
/// # Panics
/// Your [`RpcHandler`] must reply to `Request`s with the correct
/// `Response` or else this crate will panic during routing functions.
///
/// For example, a [`JsonRpcRequest::GetBlockCount`] must be replied with
/// [`JsonRpcResponse::GetBlockCount`]. If anything else is returned,
/// this crate may panic.
pub trait RpcHandler:
    RpcService<JsonRpcRequest, JsonRpcResponse>
    + RpcService<BinRequest, BinResponse>
    + RpcService<OtherRequest, OtherResponse>
{
    /// Is this [`RpcHandler`] restricted?
    ///
    /// If this returns `true`, restricted methods and endpoints such as:
    /// - `/json_rpc`'s `relay_tx` method
    /// - The `/pop_blocks` endpoint
    ///
    /// will automatically be denied access when using the
    /// [`axum::Router`] provided by [`RouterBuilder`](crate::RouterBuilder).
    fn restricted(&self) -> bool;
}