From 35907c5182dbe72c33dc132c855256a24da26ab7 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Sun, 21 Jul 2024 20:51:39 -0400 Subject: [PATCH] json_rpc_method: add `.is_restricted()` --- rpc/interface/src/json_rpc_method.rs | 117 +++++++++++++++++++++++++++ rpc/interface/src/lib.rs | 4 +- rpc/interface/src/method.rs | 31 ------- rpc/interface/src/route/json_rpc.rs | 5 +- 4 files changed, 122 insertions(+), 35 deletions(-) create mode 100644 rpc/interface/src/json_rpc_method.rs delete mode 100644 rpc/interface/src/method.rs diff --git a/rpc/interface/src/json_rpc_method.rs b/rpc/interface/src/json_rpc_method.rs new file mode 100644 index 0000000..095a3d1 --- /dev/null +++ b/rpc/interface/src/json_rpc_method.rs @@ -0,0 +1,117 @@ +//! TODO + +//---------------------------------------------------------------------------------------------------- Import +use serde::{Deserialize, Serialize}; + +use cuprate_rpc_types::json::{ + AddAuxPowRequest, BannedRequest, CalcPowRequest, FlushCacheRequest, + FlushTransactionPoolRequest, GenerateBlocksRequest, GetAlternateChainsRequest, GetBansRequest, + GetBlockCountRequest, GetBlockHeaderByHashRequest, GetBlockHeaderByHeightRequest, + GetBlockHeadersRangeRequest, GetBlockRequest, GetCoinbaseTxSumRequest, GetConnectionsRequest, + GetFeeEstimateRequest, GetInfoRequest, GetLastBlockHeaderRequest, GetMinerDataRequest, + GetOutputHistogramRequest, GetTransactionPoolBacklogRequest, GetVersionRequest, + HardForkInfoRequest, OnGetBlockHashRequest, PruneBlockchainRequest, RelayTxRequest, + SetBansRequest, SubmitBlockRequest, SyncInfoRequest, +}; + +//---------------------------------------------------------------------------------------------------- TODO +/// TODO +#[derive(Deserialize, Serialize)] +#[serde(tag = "method", content = "params")] +#[serde(rename_all = "snake_case")] +#[allow(missing_docs)] +pub enum JsonRpcMethod { + GetBlockCount(GetBlockCountRequest), + OnGetBlockHash(OnGetBlockHashRequest), + SubmitBlock(SubmitBlockRequest), + GenerateBlocks(GenerateBlocksRequest), + GetLastBlockHeader(GetLastBlockHeaderRequest), + GetBlockHeaderByHash(GetBlockHeaderByHashRequest), + GetBlockHeaderByHeight(GetBlockHeaderByHeightRequest), + GetBlockHeadersRange(GetBlockHeadersRangeRequest), + GetBlock(GetBlockRequest), + GetConnections(GetConnectionsRequest), + GetInfo(GetInfoRequest), + HardForkInfo(HardForkInfoRequest), + SetBans(SetBansRequest), + GetBans(GetBansRequest), + Banned(BannedRequest), + FlushTransactionPool(FlushTransactionPoolRequest), + GetOutputHistogram(GetOutputHistogramRequest), + GetCoinbaseTxSum(GetCoinbaseTxSumRequest), + GetVersion(GetVersionRequest), + GetFeeEstimate(GetFeeEstimateRequest), + GetAlternateChains(GetAlternateChainsRequest), + RelayTx(RelayTxRequest), + SyncInfo(SyncInfoRequest), + GetTransactionPoolBacklog(GetTransactionPoolBacklogRequest), + GetMinerData(GetMinerDataRequest), + PruneBlockchain(PruneBlockchainRequest), + CalcPow(CalcPowRequest), + FlushCache(FlushCacheRequest), + AddAuxPow(AddAuxPowRequest), +} + +impl JsonRpcMethod { + /// Returns `true` if this method should + /// only be allowed on local servers. + /// + /// If this returns `false`, it should be + /// okay to execute the method even on restricted + /// RPC servers. + /// + /// ```rust + /// use cuprate_rpc_interface::JsonRpcMethod; + /// + /// // Allowed method, even on restricted RPC servers (18089). + /// assert_eq!(JsonRpcMethod::GetBlockCount(()).is_restricted(), false); + /// + /// // Restricted methods, only allowed + /// // for unrestricted RPC servers (18081). + /// assert_eq!(JsonRpcMethod::GetConnections(()).is_restricted(), true); + /// ``` + pub const fn is_restricted(&self) -> bool { + match self { + // Normal methods. These are allowed + // even on restricted RPC servers (18089). + Self::GetBlockCount(()) + | Self::OnGetBlockHash(_) + | Self::SubmitBlock(_) + | Self::GetLastBlockHeader(_) + | Self::GetBlockHeaderByHash(_) + | Self::GetBlockHeaderByHeight(_) + | Self::GetBlockHeadersRange(_) + | Self::GetBlock(_) + | Self::GetInfo(()) + | Self::HardForkInfo(()) + | Self::GetOutputHistogram(_) + | Self::GetVersion(()) + | Self::GetFeeEstimate(()) + | Self::GetTransactionPoolBacklog(()) + | Self::GetMinerData(()) + | Self::AddAuxPow(_) => false, + + // Restricted methods. These are only allowed + // for unrestricted RPC servers (18081). + Self::GenerateBlocks(_) + | Self::GetConnections(()) + | Self::SetBans(_) + | Self::GetBans(()) + | Self::Banned(_) + | Self::FlushTransactionPool(_) + | Self::GetCoinbaseTxSum(_) + | Self::GetAlternateChains(()) + | Self::RelayTx(_) + | Self::SyncInfo(()) + | Self::PruneBlockchain(_) + | Self::CalcPow(_) + | Self::FlushCache(_) => true, + } + } +} + +//---------------------------------------------------------------------------------------------------- Tests +#[cfg(test)] +mod test { + // use super::*; +} diff --git a/rpc/interface/src/lib.rs b/rpc/interface/src/lib.rs index d2c91ae..d2fbda5 100644 --- a/rpc/interface/src/lib.rs +++ b/rpc/interface/src/lib.rs @@ -108,8 +108,8 @@ mod constants; mod error; mod free; +mod json_rpc_method; mod macros; -mod method; mod request; mod response; mod route; @@ -118,7 +118,7 @@ mod rpc_state; pub use error::Error; pub use free::create_router; -pub use method::Method; +pub use json_rpc_method::JsonRpcMethod; pub use request::Request; pub use response::Response; pub use rpc_handler::{ConcreteRpcHandler, RpcHandler}; diff --git a/rpc/interface/src/method.rs b/rpc/interface/src/method.rs deleted file mode 100644 index b06ad96..0000000 --- a/rpc/interface/src/method.rs +++ /dev/null @@ -1,31 +0,0 @@ -//! TODO - -//---------------------------------------------------------------------------------------------------- Import -use serde::{Deserialize, Serialize}; - -use cuprate_rpc_types::json::GetBlockRequest; - -//---------------------------------------------------------------------------------------------------- TODO -/// TODO -#[derive(Deserialize, Serialize)] -#[serde(tag = "method", content = "params")] -#[serde(rename_all = "snake_case")] -pub enum Method { - /// TODO - GetBlock(GetBlockRequest), -} - -impl Method { - /// TODO - pub const fn is_restricted(&self) -> bool { - match self { - Self::GetBlock(_) => false, - } - } -} - -//---------------------------------------------------------------------------------------------------- Tests -#[cfg(test)] -mod test { - // use super::*; -} diff --git a/rpc/interface/src/route/json_rpc.rs b/rpc/interface/src/route/json_rpc.rs index 8eb3d46..576a606 100644 --- a/rpc/interface/src/route/json_rpc.rs +++ b/rpc/interface/src/route/json_rpc.rs @@ -9,7 +9,8 @@ use axum::Json; use tower::Service; use crate::{ - error::Error, method::Method, request::Request, response::Response, rpc_handler::RpcHandler, + error::Error, json_rpc_method::JsonRpcMethod, request::Request, response::Response, + rpc_handler::RpcHandler, }; //---------------------------------------------------------------------------------------------------- Struct definitions @@ -17,7 +18,7 @@ use crate::{ // pub(crate) async fn json_rpc( pub(crate) async fn json_rpc( // handler: Arc, - Json(request): Json>, + Json(request): Json>, ) { todo!() }