cuprate/binaries/cuprated/src/rpc/handler.rs

92 lines
2.6 KiB
Rust
Raw Normal View History

2024-09-01 20:59:33 +00:00
//! Dummy implementation of [`RpcHandler`].
use std::task::Poll;
2024-09-03 00:15:12 +00:00
use cuprate_rpc_types::{
bin::{BinRequest, BinResponse},
json::{JsonRpcRequest, JsonRpcResponse},
other::{OtherRequest, OtherResponse},
};
2024-09-05 22:06:07 +00:00
use futures::{channel::oneshot::channel, future::BoxFuture};
2024-09-01 20:59:33 +00:00
use serde::{Deserialize, Serialize};
use tower::Service;
2024-09-02 22:08:52 +00:00
use cuprate_blockchain::service::BlockchainReadHandle;
2024-09-01 20:59:33 +00:00
use cuprate_helper::asynch::InfallibleOneshotReceiver;
use cuprate_json_rpc::Id;
2024-09-03 00:34:02 +00:00
use cuprate_rpc_interface::{RpcError, RpcHandler};
2024-09-02 22:08:52 +00:00
use cuprate_txpool::service::TxpoolReadHandle;
2024-09-02 21:37:18 +00:00
use crate::rpc::{bin, json, other};
2024-09-01 20:59:33 +00:00
/// TODO
2024-09-02 22:08:52 +00:00
#[derive(Clone)]
2024-09-01 20:59:33 +00:00
pub struct CupratedRpcHandler {
/// Should this RPC server be [restricted](RpcHandler::restricted)?
pub restricted: bool,
2024-09-02 21:37:18 +00:00
/// Read handle to the blockchain database.
2024-09-02 22:08:52 +00:00
pub blockchain: BlockchainReadHandle,
2024-09-02 21:37:18 +00:00
/// Read handle to the transaction pool database.
2024-09-02 22:08:52 +00:00
pub txpool: TxpoolReadHandle,
2024-09-01 20:59:33 +00:00
}
impl RpcHandler for CupratedRpcHandler {
fn restricted(&self) -> bool {
self.restricted
}
}
2024-09-03 00:15:12 +00:00
// INVARIANT:
//
// We don't need to check for `self.is_restricted()`
// here because `cuprate-rpc-interface` handles that.
//
// We can assume the request coming has the required permissions.
impl Service<JsonRpcRequest> for CupratedRpcHandler {
type Response = JsonRpcResponse;
2024-09-01 20:59:33 +00:00
type Error = RpcError;
2024-09-05 22:06:07 +00:00
type Future = BoxFuture<'static, Result<JsonRpcResponse, RpcError>>;
2024-09-01 20:59:33 +00:00
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
2024-09-03 00:15:12 +00:00
fn call(&mut self, request: JsonRpcRequest) -> Self::Future {
2024-09-02 22:08:52 +00:00
let state = Self::clone(self);
2024-09-05 22:06:07 +00:00
Box::pin(json::map_request(state, request))
2024-09-03 00:15:12 +00:00
}
}
2024-09-01 20:59:33 +00:00
2024-09-03 00:15:12 +00:00
impl Service<BinRequest> for CupratedRpcHandler {
type Response = BinResponse;
type Error = RpcError;
2024-09-05 22:06:07 +00:00
type Future = BoxFuture<'static, Result<BinResponse, RpcError>>;
2024-09-01 20:59:33 +00:00
2024-09-03 00:15:12 +00:00
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, request: BinRequest) -> Self::Future {
let state = Self::clone(self);
2024-09-05 22:06:07 +00:00
Box::pin(bin::map_request(state, request))
2024-09-03 00:15:12 +00:00
}
}
impl Service<OtherRequest> for CupratedRpcHandler {
type Response = OtherResponse;
type Error = RpcError;
2024-09-05 22:06:07 +00:00
type Future = BoxFuture<'static, Result<OtherResponse, RpcError>>;
2024-09-03 00:15:12 +00:00
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, request: OtherRequest) -> Self::Future {
let state = Self::clone(self);
2024-09-05 22:06:07 +00:00
Box::pin(other::map_request(state, request))
2024-09-01 20:59:33 +00:00
}
}