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

98 lines
3.1 KiB
Rust
Raw Normal View History

2024-09-01 20:59:33 +00:00
//! Dummy implementation of [`RpcHandler`].
//---------------------------------------------------------------------------------------------------- Use
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-01 20:59:33 +00:00
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;
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
//---------------------------------------------------------------------------------------------------- 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
}
}
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-03 00:15:12 +00:00
type Future = InfallibleOneshotReceiver<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-03 00:15:12 +00:00
let response = json::map_request(state, request).expect("TODO");
todo!()
}
}
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;
type Future = InfallibleOneshotReceiver<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);
let response = bin::map_request(state, request).expect("TODO");
todo!()
}
}
impl Service<OtherRequest> for CupratedRpcHandler {
type Response = OtherResponse;
type Error = RpcError;
type Future = InfallibleOneshotReceiver<Result<OtherResponse, RpcError>>;
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);
let response = other::map_request(state, request).expect("TODO");
2024-09-02 22:08:52 +00:00
todo!()
2024-09-01 20:59:33 +00:00
}
}