mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-11-16 15:58:14 +00:00
json_rpc_method: add .is_restricted()
This commit is contained in:
parent
73c11a4cdf
commit
35907c5182
4 changed files with 122 additions and 35 deletions
117
rpc/interface/src/json_rpc_method.rs
Normal file
117
rpc/interface/src/json_rpc_method.rs
Normal 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::*;
|
||||
}
|
|
@ -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};
|
||||
|
|
|
@ -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::*;
|
||||
}
|
|
@ -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!()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue