Skip to content

Commit

Permalink
feat(rpc): add rpc_ namespace (paradigmxyz#2928)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
TechieBoy and mattsse committed Jun 2, 2023
1 parent 65b6276 commit 5b72a73
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/rpc/rpc-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod eth;
mod eth_filter;
mod eth_pubsub;
mod net;
mod rpc;
mod trace;
mod txpool;
mod web3;
Expand All @@ -33,6 +34,7 @@ pub mod servers {
eth_filter::EthFilterApiServer,
eth_pubsub::EthPubSubApiServer,
net::NetApiServer,
rpc::RpcApiServer,
trace::TraceApiServer,
txpool::TxPoolApiServer,
web3::Web3ApiServer,
Expand All @@ -52,6 +54,7 @@ pub mod clients {
engine::{EngineApiClient, EngineEthApiClient},
eth::EthApiClient,
net::NetApiClient,
rpc::RpcApiServer,
trace::TraceApiClient,
txpool::TxPoolApiClient,
web3::Web3ApiClient,
Expand Down
11 changes: 11 additions & 0 deletions crates/rpc/rpc-api/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use reth_rpc_types::RpcModules;

/// RPC namespace, used to find the versions of all rpc modules
#[cfg_attr(not(feature = "client"), rpc(server))]
#[cfg_attr(feature = "client", rpc(server, client))]
pub trait RpcApi {
/// Lists enabled APIs and the version of each.
#[method(name = "rpc_modules")]
fn rpc_modules(&self) -> RpcResult<RpcModules>;
}
19 changes: 18 additions & 1 deletion crates/rpc/rpc-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ use reth_rpc::{
gas_oracle::GasPriceOracle,
},
AdminApi, DebugApi, EngineEthApi, EthApi, EthFilter, EthPubSub, EthSubscriptionIdProvider,
NetApi, TraceApi, TracingCallGuard, TxPoolApi, Web3Api,
NetApi, RPCApi, TraceApi, TracingCallGuard, TxPoolApi, Web3Api,
};
use reth_rpc_api::{servers::*, EngineApiServer};
use reth_tasks::TaskSpawner;
Expand Down Expand Up @@ -570,6 +570,8 @@ pub enum RethRpcModule {
Txpool,
/// `web3_` module
Web3,
/// `rpc_` module
Rpc,
}

// === impl RethRpcModule ===
Expand Down Expand Up @@ -783,7 +785,13 @@ where
) -> Vec<Methods> {
let EthHandlers { api: eth_api, cache: eth_cache, filter: eth_filter, pubsub: eth_pubsub } =
self.with_eth(|eth| eth.clone());

// Create a copy, so we can list out all the methods for rpc_ api
let namespaces: Vec<_> = namespaces.collect();

namespaces
.iter()
.copied()
.map(|namespace| {
self.modules
.entry(namespace)
Expand Down Expand Up @@ -823,6 +831,14 @@ where
RethRpcModule::Txpool => {
TxPoolApi::new(self.pool.clone()).into_rpc().into()
}
RethRpcModule::Rpc => RPCApi::new(
namespaces
.iter()
.map(|module| (module.to_string(), "1.0".to_string()))
.collect(),
)
.into_rpc()
.into(),
})
.clone()
})
Expand Down Expand Up @@ -1649,6 +1665,7 @@ mod tests {
"net" => RethRpcModule::Net,
"trace" => RethRpcModule::Trace,
"web3" => RethRpcModule::Web3,
"rpc" => RethRpcModule::Rpc,
);
}

Expand Down
2 changes: 2 additions & 0 deletions crates/rpc/rpc-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

mod admin;
mod eth;
mod rpc;

pub use admin::*;
pub use eth::*;
pub use rpc::*;
41 changes: 41 additions & 0 deletions crates/rpc/rpc-types/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Represents the `rpc_modules` response, which returns the
/// list of all available modules on that transport and their version
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(transparent)]
pub struct RpcModules {
module_map: HashMap<String, String>,
}

impl RpcModules {
/// Create a new instance of RPCModules
pub fn new(module_map: HashMap<String, String>) -> Self {
Self { module_map }
}

/// Consumes self and returns the inner hashmap mapping module names to their versions
pub fn into_modules(self) -> HashMap<String, String> {
self.module_map
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_module_versions_roundtrip() {
let s = r#"{"txpool":"1.0","trace":"1.0","eth":"1.0","web3":"1.0","net":"1.0"}"#;
let module_map = HashMap::from([
("txpool".to_owned(), "1.0".to_owned()),
("trace".to_owned(), "1.0".to_owned()),
("eth".to_owned(), "1.0".to_owned()),
("web3".to_owned(), "1.0".to_owned()),
("net".to_owned(), "1.0".to_owned()),
]);
let m = RpcModules::new(module_map);
let de_serialized: RpcModules = serde_json::from_str(&s).unwrap();
assert_eq!(de_serialized, m);
}
}
2 changes: 2 additions & 0 deletions crates/rpc/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod engine;
pub mod eth;
mod layers;
mod net;
mod rpc;
mod trace;
mod txpool;
mod web3;
Expand All @@ -42,6 +43,7 @@ pub use engine::{EngineApi, EngineEthApi};
pub use eth::{EthApi, EthApiSpec, EthFilter, EthPubSub, EthSubscriptionIdProvider};
pub use layers::{AuthLayer, AuthValidator, Claims, JwtAuthValidator, JwtError, JwtSecret};
pub use net::NetApi;
pub use rpc::RPCApi;
pub use trace::TraceApi;
pub use txpool::TxPoolApi;
pub use web3::Web3Api;
Expand Down
26 changes: 26 additions & 0 deletions crates/rpc/rpc/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use jsonrpsee::core::RpcResult;
use reth_rpc_api::RpcApiServer;
use reth_rpc_types::RpcModules;
use std::{collections::HashMap, sync::Arc};

/// `rpc` API implementation.
///
/// This type provides the functionality for handling `rpc` requests
#[derive(Debug, Clone, Default)]
pub struct RPCApi {
/// The implementation of the Arc api
rpc_modules: Arc<RpcModules>,
}

impl RPCApi {
/// Return a new RPCApi struct, with given module_map
pub fn new(module_map: HashMap<String, String>) -> Self {
RPCApi { rpc_modules: Arc::new(RpcModules::new(module_map)) }
}
}

impl RpcApiServer for RPCApi {
fn rpc_modules(&self) -> RpcResult<RpcModules> {
Ok(self.rpc_modules.as_ref().clone())
}
}

0 comments on commit 5b72a73

Please sign in to comment.