From 5b0e56f711876ae7c60eb2b2e000cbdb81cca58a Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Sun, 1 Sep 2024 16:59:33 -0400 Subject: [PATCH] !! --- binaries/cuprated/src/main.rs | 1 - binaries/cuprated/src/rpc.rs | 4 +- binaries/cuprated/src/rpc/handler.rs | 129 +++++++++++++++++++ binaries/cuprated/src/rpc/request_handler.rs | 1 - 4 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 binaries/cuprated/src/rpc/handler.rs delete mode 100644 binaries/cuprated/src/rpc/request_handler.rs diff --git a/binaries/cuprated/src/main.rs b/binaries/cuprated/src/main.rs index a08f3d65..9c59a567 100644 --- a/binaries/cuprated/src/main.rs +++ b/binaries/cuprated/src/main.rs @@ -13,7 +13,6 @@ redundant_semicolons, unused_allocation, coherence_leak_check, - while_true, // Maybe can be put into `#[deny]`. unconditional_recursion, diff --git a/binaries/cuprated/src/rpc.rs b/binaries/cuprated/src/rpc.rs index 80b2789e..773da807 100644 --- a/binaries/cuprated/src/rpc.rs +++ b/binaries/cuprated/src/rpc.rs @@ -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; diff --git a/binaries/cuprated/src/rpc/handler.rs b/binaries/cuprated/src/rpc/handler.rs new file mode 100644 index 00000000..fd17a105 --- /dev/null +++ b/binaries/cuprated/src/rpc/handler.rs @@ -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 for CupratedRpcHandler { + type Response = RpcResponse; + type Error = RpcError; + type Future = InfallibleOneshotReceiver>; + + fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll> { + 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) + } +} diff --git a/binaries/cuprated/src/rpc/request_handler.rs b/binaries/cuprated/src/rpc/request_handler.rs deleted file mode 100644 index 8b137891..00000000 --- a/binaries/cuprated/src/rpc/request_handler.rs +++ /dev/null @@ -1 +0,0 @@ -