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

76 lines
2.8 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 21:37:18 +00:00
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
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 21:37:18 +00:00
use cuprate_txpool::service::{TxpoolReadHandle, TxpoolWriteHandle};
use crate::rpc::{bin, json, other};
2024-09-01 20:59:33 +00:00
//---------------------------------------------------------------------------------------------------- CupratedRpcHandler
/// TODO
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
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.
pub blockchain_read: BlockchainReadHandle,
/// Write handle to the blockchain database.
pub blockchain_write: BlockchainWriteHandle,
/// Direct handle to the blockchain database.
pub blockchain_db: Arc<ConcreteEnv>,
/// Read handle to the transaction pool database.
pub txpool_read: TxpoolReadHandle,
/// Write handle to the transaction pool database.
pub txpool_write: TxpoolWriteHandle,
/// Direct handle to the transaction pool database.
pub txpool_db: Arc<ConcreteEnv>,
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 21:37:18 +00:00
let state = CupratedRpcHandler::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 21:47:23 +00:00
RpcRequest::JsonRpc(r) => json::map_request(state, r), // JSON-RPC 2.0 requests.
RpcRequest::Binary(r) => bin::map_request(state, r), // Binary requests.
RpcRequest::Other(o) => other::map_request(state, r), // JSON (but not JSON-RPC) requests.
2024-09-01 20:59:33 +00:00
};
let (tx, rx) = channel();
drop(tx.send(Ok(resp)));
InfallibleOneshotReceiver::from(rx)
}
}