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

68 lines
2.3 KiB
Rust
Raw Normal View History

2024-09-01 20:59:33 +00:00
//! Dummy implementation of [`RpcHandler`].
//---------------------------------------------------------------------------------------------------- Use
use std::task::Poll;
use futures::channel::oneshot::channel;
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;
use cuprate_rpc_interface::{RpcError, RpcHandler, RpcRequest, RpcResponse};
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
//---------------------------------------------------------------------------------------------------- CupratedRpcHandler
/// 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
}
2024-09-02 21:37:18 +00:00
//---------------------------------------------------------------------------------------------------- RpcHandler Impl
2024-09-01 20:59:33 +00:00
impl RpcHandler for CupratedRpcHandler {
fn restricted(&self) -> bool {
self.restricted
}
}
impl Service<RpcRequest> for CupratedRpcHandler {
type Response = RpcResponse;
type Error = RpcError;
type Future = InfallibleOneshotReceiver<Result<RpcResponse, RpcError>>;
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
2024-09-02 21:47:23 +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.
2024-09-01 20:59:33 +00:00
fn call(&mut self, req: RpcRequest) -> Self::Future {
2024-09-02 22:08:52 +00:00
let state = Self::clone(self);
2024-09-01 20:59:33 +00:00
2024-09-02 21:37:18 +00:00
let resp = match req {
2024-09-02 22:08:52 +00:00
RpcRequest::JsonRpc(r) => {
RpcResponse::JsonRpc(json::map_request(state, r).expect("TODO"))
} // JSON-RPC 2.0 requests.
RpcRequest::Binary(r) => RpcResponse::Binary(bin::map_request(state, r).expect("TODO")), // Binary requests.
RpcRequest::Other(r) => RpcResponse::Other(other::map_request(state, r).expect("TODO")), // JSON (but not JSON-RPC) requests.
2024-09-01 20:59:33 +00:00
};
2024-09-02 22:08:52 +00:00
todo!()
2024-09-01 20:59:33 +00:00
}
}