forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rpc): add rpc_ namespace (paradigmxyz#2928)
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
- Loading branch information
Showing
7 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
mod admin; | ||
mod eth; | ||
mod rpc; | ||
|
||
pub use admin::*; | ||
pub use eth::*; | ||
pub use rpc::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |