json_rpc_method: add .is_restricted()
Some checks are pending
Audit / audit (push) Waiting to run
Deny / audit (push) Waiting to run

This commit is contained in:
hinto.janai 2024-07-21 20:51:39 -04:00
parent 73c11a4cdf
commit 35907c5182
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
4 changed files with 122 additions and 35 deletions

View file

@ -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::*;
}

View file

@ -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};

View file

@ -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::*;
}

View file

@ -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<H: RpcHandler>(
pub(crate) async fn json_rpc(
// handler: Arc<H>,
Json(request): Json<cuprate_json_rpc::Request<Method>>,
Json(request): Json<cuprate_json_rpc::Request<JsonRpcMethod>>,
) {
todo!()
}