This commit is contained in:
hinto.janai 2024-09-01 16:59:33 -04:00
parent d83a93fc31
commit 5b0e56f711
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
4 changed files with 132 additions and 3 deletions

View file

@ -13,7 +13,6 @@
redundant_semicolons,
unused_allocation,
coherence_leak_check,
while_true,
// Maybe can be put into `#[deny]`.
unconditional_recursion,

View file

@ -2,4 +2,6 @@
//!
//! Will contain the code to initiate the RPC and a request handler.
mod request_handler;
mod handler;
pub use handler::CupratedRpcHandler;

View file

@ -0,0 +1,129 @@
//! Dummy implementation of [`RpcHandler`].
//---------------------------------------------------------------------------------------------------- Use
use std::task::Poll;
use futures::channel::oneshot::channel;
use serde::{Deserialize, Serialize};
use tower::Service;
use cuprate_helper::asynch::InfallibleOneshotReceiver;
use cuprate_json_rpc::Id;
use cuprate_rpc_interface::{RpcError, RpcHandler, RpcRequest, RpcResponse};
//---------------------------------------------------------------------------------------------------- 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,
}
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(()))
}
fn call(&mut self, req: RpcRequest) -> Self::Future {
use cuprate_rpc_types::bin::BinRequest as BReq;
use cuprate_rpc_types::bin::BinResponse as BResp;
use cuprate_rpc_types::json::JsonRpcRequest as JReq;
use cuprate_rpc_types::json::JsonRpcResponse as JResp;
use cuprate_rpc_types::other::OtherRequest as OReq;
use cuprate_rpc_types::other::OtherResponse as OResp;
#[rustfmt::skip]
#[allow(clippy::default_trait_access)]
let resp = match req {
// JSON-RPC 2.0 requests.
RpcRequest::JsonRpc(j) => RpcResponse::JsonRpc(cuprate_json_rpc::Response::ok(Id::Null, match j.body {
JReq::GetBlockCount(_) => JResp::GetBlockCount(Default::default()),
JReq::OnGetBlockHash(_) => JResp::OnGetBlockHash(Default::default()),
JReq::SubmitBlock(_) => JResp::SubmitBlock(Default::default()),
JReq::GenerateBlocks(_) => JResp::GenerateBlocks(Default::default()),
JReq::GetLastBlockHeader(_) => JResp::GetLastBlockHeader(Default::default()),
JReq::GetBlockHeaderByHash(_) => JResp::GetBlockHeaderByHash(Default::default()),
JReq::GetBlockHeaderByHeight(_) => JResp::GetBlockHeaderByHeight(Default::default()),
JReq::GetBlockHeadersRange(_) => JResp::GetBlockHeadersRange(Default::default()),
JReq::GetBlock(_) => JResp::GetBlock(Default::default()),
JReq::GetConnections(_) => JResp::GetConnections(Default::default()),
JReq::GetInfo(_) => JResp::GetInfo(Default::default()),
JReq::HardForkInfo(_) => JResp::HardForkInfo(Default::default()),
JReq::SetBans(_) => JResp::SetBans(Default::default()),
JReq::GetBans(_) => JResp::GetBans(Default::default()),
JReq::Banned(_) => JResp::Banned(Default::default()),
JReq::FlushTransactionPool(_) => JResp::FlushTransactionPool(Default::default()),
JReq::GetOutputHistogram(_) => JResp::GetOutputHistogram(Default::default()),
JReq::GetCoinbaseTxSum(_) => JResp::GetCoinbaseTxSum(Default::default()),
JReq::GetVersion(_) => JResp::GetVersion(Default::default()),
JReq::GetFeeEstimate(_) => JResp::GetFeeEstimate(Default::default()),
JReq::GetAlternateChains(_) => JResp::GetAlternateChains(Default::default()),
JReq::RelayTx(_) => JResp::RelayTx(Default::default()),
JReq::SyncInfo(_) => JResp::SyncInfo(Default::default()),
JReq::GetTransactionPoolBacklog(_) => JResp::GetTransactionPoolBacklog(Default::default()),
JReq::GetMinerData(_) => JResp::GetMinerData(Default::default()),
JReq::PruneBlockchain(_) => JResp::PruneBlockchain(Default::default()),
JReq::CalcPow(_) => JResp::CalcPow(Default::default()),
JReq::FlushCache(_) => JResp::FlushCache(Default::default()),
JReq::AddAuxPow(_) => JResp::AddAuxPow(Default::default()),
JReq::GetTxIdsLoose(_) => JResp::GetTxIdsLoose(Default::default()),
})),
// Binary requests.
RpcRequest::Binary(b) => RpcResponse::Binary(match b {
BReq::GetBlocks(_) => BResp::GetBlocks(Default::default()),
BReq::GetBlocksByHeight(_) => BResp::GetBlocksByHeight(Default::default()),
BReq::GetHashes(_) => BResp::GetHashes(Default::default()),
BReq::GetOutputIndexes(_) => BResp::GetOutputIndexes(Default::default()),
BReq::GetOuts(_) => BResp::GetOuts(Default::default()),
BReq::GetTransactionPoolHashes(_) => BResp::GetTransactionPoolHashes(Default::default()),
BReq::GetOutputDistribution(_) => BResp::GetOutputDistribution(Default::default()),
}),
// JSON (but not JSON-RPC) requests.
RpcRequest::Other(o) => RpcResponse::Other(match o {
OReq::GetHeight(_) => OResp::GetHeight(Default::default()),
OReq::GetTransactions(_) => OResp::GetTransactions(Default::default()),
OReq::GetAltBlocksHashes(_) => OResp::GetAltBlocksHashes(Default::default()),
OReq::IsKeyImageSpent(_) => OResp::IsKeyImageSpent(Default::default()),
OReq::SendRawTransaction(_) => OResp::SendRawTransaction(Default::default()),
OReq::StartMining(_) => OResp::StartMining(Default::default()),
OReq::StopMining(_) => OResp::StopMining(Default::default()),
OReq::MiningStatus(_) => OResp::MiningStatus(Default::default()),
OReq::SaveBc(_) => OResp::SaveBc(Default::default()),
OReq::GetPeerList(_) => OResp::GetPeerList(Default::default()),
OReq::SetLogHashRate(_) => OResp::SetLogHashRate(Default::default()),
OReq::SetLogLevel(_) => OResp::SetLogLevel(Default::default()),
OReq::SetLogCategories(_) => OResp::SetLogCategories(Default::default()),
OReq::SetBootstrapDaemon(_) => OResp::SetBootstrapDaemon(Default::default()),
OReq::GetTransactionPool(_) => OResp::GetTransactionPool(Default::default()),
OReq::GetTransactionPoolStats(_) => OResp::GetTransactionPoolStats(Default::default()),
OReq::StopDaemon(_) => OResp::StopDaemon(Default::default()),
OReq::GetLimit(_) => OResp::GetLimit(Default::default()),
OReq::SetLimit(_) => OResp::SetLimit(Default::default()),
OReq::OutPeers(_) => OResp::OutPeers(Default::default()),
OReq::InPeers(_) => OResp::InPeers(Default::default()),
OReq::GetNetStats(_) => OResp::GetNetStats(Default::default()),
OReq::GetOuts(_) => OResp::GetOuts(Default::default()),
OReq::Update(_) => OResp::Update(Default::default()),
OReq::PopBlocks(_) => OResp::PopBlocks(Default::default()),
OReq::GetTransactionPoolHashes(_) => OResp::GetTransactionPoolHashes(Default::default()),
OReq::GetPublicNodes(_) => OResp::GetPublicNodes(Default::default()),
})
};
let (tx, rx) = channel();
drop(tx.send(Ok(resp)));
InfallibleOneshotReceiver::from(rx)
}
}