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

103 lines
2.9 KiB
Rust
Raw Normal View History

2024-09-01 20:59:33 +00:00
//! Dummy implementation of [`RpcHandler`].
2024-09-05 22:23:39 +00:00
use std::task::{Context, Poll};
2024-09-01 20:59:33 +00:00
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-06 20:54:28 +00:00
use cuprate_rpc_types::{
bin::{BinRequest, BinResponse},
json::{JsonRpcRequest, JsonRpcResponse},
other::{OtherRequest, OtherResponse},
};
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)?
2024-09-05 22:11:27 +00:00
//
// INVARIANT:
// We don't need to include this in `state` and check for
// `self.is_restricted()` because `cuprate-rpc-interface` handles that.
2024-09-01 20:59:33 +00:00
pub restricted: bool,
2024-09-02 21:37:18 +00:00
2024-09-05 22:11:27 +00:00
/// State needed for request -> response mapping.
pub state: CupratedRpcHandlerState,
}
/// TODO
#[derive(Clone)]
pub struct CupratedRpcHandlerState {
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-05 22:23:39 +00:00
impl CupratedRpcHandler {
/// TODO
pub fn init() {
todo!()
}
}
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
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
2024-09-05 22:23:39 +00:00
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2024-09-01 20:59:33 +00:00
Poll::Ready(Ok(()))
}
2024-09-03 00:15:12 +00:00
fn call(&mut self, request: JsonRpcRequest) -> Self::Future {
2024-09-05 22:11:27 +00:00
let state = CupratedRpcHandlerState::clone(&self.state);
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-05 22:23:39 +00:00
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2024-09-03 00:15:12 +00:00
Poll::Ready(Ok(()))
}
fn call(&mut self, request: BinRequest) -> Self::Future {
2024-09-05 22:11:27 +00:00
let state = CupratedRpcHandlerState::clone(&self.state);
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
2024-09-05 22:23:39 +00:00
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2024-09-03 00:15:12 +00:00
Poll::Ready(Ok(()))
}
fn call(&mut self, request: OtherRequest) -> Self::Future {
2024-09-05 22:11:27 +00:00
let state = CupratedRpcHandlerState::clone(&self.state);
2024-09-05 22:06:07 +00:00
Box::pin(other::map_request(state, request))
2024-09-01 20:59:33 +00:00
}
}