Skip to content

Commit

Permalink
chore(rpc): add more helper functions to modle regsitry (paradigmxyz#…
Browse files Browse the repository at this point in the history
…1380)

Co-authored-by: Georgios Konstantopoulos <[email protected]>
  • Loading branch information
mattsse and gakonst authored Feb 15, 2023
1 parent a08a7b0 commit ba20759
Showing 1 changed file with 65 additions and 6 deletions.
71 changes: 65 additions & 6 deletions crates/rpc/rpc-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl RpcModuleSelection {
Network: NetworkInfo + Peers + Clone + 'static,
{
let mut registry = RethModuleRegistry::new(client, pool, network);
registry.module(self)
registry.module_for(self)
}

/// Returns an iterator over all configured [RethRpcModule]
Expand Down Expand Up @@ -357,27 +357,86 @@ pub struct RethModuleRegistry<Client, Pool, Network> {

// === impl RethModuleRegistry ===

impl<Client, Pool, Network> RethModuleRegistry<Client, Pool, Network> {
/// Creates a new, empty instance.
pub fn new(client: Client, pool: Pool, network: Network) -> Self {
Self { client, pool, network, eth_api: None, modules: Default::default() }
}

/// Returns all installed methods
pub fn methods(&self) -> Vec<Methods> {
self.modules.values().cloned().collect()
}

/// Returns a merged RpcModule
pub fn module(&self) -> RpcModule<()> {
let mut module = RpcModule::new(());
for methods in self.modules.values().cloned() {
module.merge(methods).expect("No conflicts");
}
module
}
}

impl<Client, Pool, Network> RethModuleRegistry<Client, Pool, Network>
where
Network: NetworkInfo + Peers + Clone + 'static,
{
/// Register Admin Namespace
pub fn register_admin(&mut self) -> &mut Self {
self.modules
.insert(RethRpcModule::Admin, AdminApi::new(self.network.clone()).into_rpc().into());
self
}

/// Register Web3 Namespace
pub fn register_web3(&mut self) -> &mut Self {
self.modules
.insert(RethRpcModule::Web3, Web3Api::new(self.network.clone()).into_rpc().into());
self
}
}

impl<Client, Pool, Network> RethModuleRegistry<Client, Pool, Network>
where
Client: BlockProvider + StateProviderFactory + Clone + 'static,
Pool: TransactionPool + Clone + 'static,
Network: NetworkInfo + Peers + Clone + 'static,
{
/// Creates a new, empty instance
pub fn new(client: Client, pool: Pool, network: Network) -> Self {
Self { client, pool, network, eth_api: None, modules: Default::default() }
/// Register Eth Namespace
pub fn register_eth(&mut self) -> &mut Self {
let eth_api = self.eth_api();
self.modules.insert(RethRpcModule::Eth, eth_api.into_rpc().into());
self
}

/// Register Debug Namespace
pub fn register_debug(&mut self) -> &mut Self {
let eth_api = self.eth_api();
self.modules.insert(RethRpcModule::Debug, DebugApi::new(eth_api).into_rpc().into());
self
}

/// Register Net Namespace
pub fn register_net(&mut self) -> &mut Self {
let eth_api = self.eth_api();
self.modules.insert(
RethRpcModule::Net,
NetApi::new(self.network.clone(), eth_api).into_rpc().into(),
);
self
}

/// Helper function to create a [RpcModule] if it's not `None`
fn maybe_module(&mut self, config: Option<&RpcModuleSelection>) -> Option<RpcModule<()>> {
let config = config?;
let module = self.module(config);
let module = self.module_for(config);
Some(module)
}

/// Populates a new [RpcModule] based on the selected [RethRpcModule]s in the given
/// [RpcModuleSelection]
pub fn module(&mut self, config: &RpcModuleSelection) -> RpcModule<()> {
pub fn module_for(&mut self, config: &RpcModuleSelection) -> RpcModule<()> {
let mut module = RpcModule::new(());
let all_methods = self.reth_methods(config.iter_selection());
for methods in all_methods {
Expand Down

0 comments on commit ba20759

Please sign in to comment.