From a2a0010fe85fc772402a81529c177a598a3e1cec Mon Sep 17 00:00:00 2001 From: "Supernovahs.eth" <91280922+supernovahs@users.noreply.github.com> Date: Thu, 5 Oct 2023 22:10:17 +0530 Subject: [PATCH] Simplify node components (#4922) Co-authored-by: Matthias Seitz --- bin/reth/src/args/rpc_server_args.rs | 10 +++--- bin/reth/src/cli/components.rs | 26 ++++++++++++++++ bin/reth/src/cli/ext.rs | 31 +++++-------------- .../src/main.rs | 26 +++++----------- 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/bin/reth/src/args/rpc_server_args.rs b/bin/reth/src/args/rpc_server_args.rs index 5ca3932e7bda..aa12adaacffd 100644 --- a/bin/reth/src/args/rpc_server_args.rs +++ b/bin/reth/src/args/rpc_server_args.rs @@ -2,7 +2,7 @@ use crate::{ args::GasPriceOracleArgs, - cli::{config::RethRpcConfig, ext::RethNodeCommandConfig}, + cli::{components::RethRpcComponents, config::RethRpcConfig, ext::RethNodeCommandConfig}, }; use clap::{ builder::{PossibleValue, RangedU64ValueParser, TypedValueParser}, @@ -193,19 +193,19 @@ impl RpcServerArgs { let module_config = self.transport_rpc_module_config(); debug!(target: "reth::cli", http=?module_config.http(), ws=?module_config.ws(), "Using RPC module config"); - let (mut rpc_modules, auth_module, mut registry) = RpcModuleBuilder::default() + let (mut modules, auth_module, mut registry) = RpcModuleBuilder::default() .with_provider(components.provider()) .with_pool(components.pool()) .with_network(components.network()) .with_events(components.events()) .with_executor(components.task_executor()) .build_with_auth_server(module_config, engine_api); - + let node_modules = RethRpcComponents { registry: &mut registry, modules: &mut modules }; // apply configured customization - conf.extend_rpc_modules(self, components, &mut registry, &mut rpc_modules)?; + conf.extend_rpc_modules(self, components, node_modules)?; let server_config = self.rpc_server_config(); - let launch_rpc = rpc_modules.start_server(server_config).map_ok(|handle| { + let launch_rpc = modules.start_server(server_config).map_ok(|handle| { if let Some(url) = handle.ipc_endpoint() { info!(target: "reth::cli", url=%url, "RPC IPC server started"); } diff --git a/bin/reth/src/cli/components.rs b/bin/reth/src/cli/components.rs index 0db43553eca4..1f51409cbce7 100644 --- a/bin/reth/src/cli/components.rs +++ b/bin/reth/src/cli/components.rs @@ -6,6 +6,7 @@ use reth_provider::{ AccountReader, BlockReaderIdExt, CanonStateSubscriptions, ChainSpecProvider, ChangeSetReader, EvmEnvProvider, StateProviderFactory, }; +use reth_rpc_builder::{RethModuleRegistry, TransportRpcModules}; use reth_tasks::TaskSpawner; use reth_transaction_pool::TransactionPool; use std::sync::Arc; @@ -71,6 +72,31 @@ pub trait RethNodeComponents { } } +/// Helper container to encapsulate [RethModuleRegistry] and [TransportRpcModules]. +/// +/// This can be used to access installed modules, or create commonly used handlers like +/// [reth_rpc::EthApi], and ultimately merge additional rpc handler into the configured transport +/// modules [TransportRpcModules]. +#[derive(Debug)] +#[allow(clippy::type_complexity)] +pub struct RethRpcComponents<'a, Reth: RethNodeComponents> { + /// A Helper type the holds instances of the configured modules. + /// + /// This provides easy access to rpc handlers, such as [RethModuleRegistry::eth_api]. + pub registry: &'a mut RethModuleRegistry< + Reth::Provider, + Reth::Pool, + Reth::Network, + Reth::Tasks, + Reth::Events, + >, + /// Holds installed modules per transport type. + /// + /// This can be used to merge additional modules into the configured transports (http, ipc, + /// ws). See [TransportRpcModules::merge_configured] + pub modules: &'a mut TransportRpcModules, +} + /// A Generic implementation of the RethNodeComponents trait. #[derive(Clone, Debug)] #[allow(missing_docs)] diff --git a/bin/reth/src/cli/ext.rs b/bin/reth/src/cli/ext.rs index f8905d037639..c61ef3a9a181 100644 --- a/bin/reth/src/cli/ext.rs +++ b/bin/reth/src/cli/ext.rs @@ -1,13 +1,12 @@ //! Support for integrating customizations into the CLI. use crate::cli::{ - components::RethNodeComponents, + components::{RethNodeComponents, RethRpcComponents}, config::{PayloadBuilderConfig, RethRpcConfig}, }; use clap::Args; use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig}; use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService}; -use reth_rpc_builder::{RethModuleRegistry, TransportRpcModules}; use reth_tasks::TaskSpawner; use std::fmt; @@ -49,21 +48,13 @@ pub trait RethNodeCommandConfig: fmt::Debug { /// Allows for registering additional RPC modules for the transports. /// - /// This is expected to call the merge functions of [TransportRpcModules], for example - /// [TransportRpcModules::merge_configured] - #[allow(clippy::type_complexity)] + /// This is expected to call the merge functions of [reth_rpc_builder::TransportRpcModules], for + /// example [reth_rpc_builder::TransportRpcModules::merge_configured] fn extend_rpc_modules( &mut self, config: &Conf, components: &Reth, - registry: &mut RethModuleRegistry< - Reth::Provider, - Reth::Pool, - Reth::Network, - Reth::Tasks, - Reth::Events, - >, - modules: &mut TransportRpcModules, + rpc_components: RethRpcComponents<'_, Reth>, ) -> eyre::Result<()> where Conf: RethRpcConfig, @@ -71,8 +62,7 @@ pub trait RethNodeCommandConfig: fmt::Debug { { let _ = config; let _ = components; - let _ = registry; - let _ = modules; + let _ = rpc_components; Ok(()) } @@ -196,21 +186,14 @@ impl RethNodeCommandConfig for NoArgs { &mut self, config: &Conf, components: &Reth, - registry: &mut RethModuleRegistry< - Reth::Provider, - Reth::Pool, - Reth::Network, - Reth::Tasks, - Reth::Events, - >, - modules: &mut TransportRpcModules, + rpc_components: RethRpcComponents<'_, Reth>, ) -> eyre::Result<()> where Conf: RethRpcConfig, Reth: RethNodeComponents, { if let Some(conf) = self.inner_mut() { - conf.extend_rpc_modules(config, components, registry, modules) + conf.extend_rpc_modules(config, components, rpc_components) } else { Ok(()) } diff --git a/examples/additional-rpc-namespace-in-cli/src/main.rs b/examples/additional-rpc-namespace-in-cli/src/main.rs index 9823fef03f1a..ce03aa5c8e3e 100644 --- a/examples/additional-rpc-namespace-in-cli/src/main.rs +++ b/examples/additional-rpc-namespace-in-cli/src/main.rs @@ -13,14 +13,11 @@ //! ``` use clap::Parser; use jsonrpsee::{core::RpcResult, proc_macros::rpc}; -use reth::{ - cli::{ - components::RethNodeComponents, - config::RethRpcConfig, - ext::{RethCliExt, RethNodeCommandConfig}, - Cli, - }, - rpc::builder::{RethModuleRegistry, TransportRpcModules}, +use reth::cli::{ + components::{RethNodeComponents, RethRpcComponents}, + config::RethRpcConfig, + ext::{RethCliExt, RethNodeCommandConfig}, + Cli, }; use reth_transaction_pool::TransactionPool; @@ -50,14 +47,7 @@ impl RethNodeCommandConfig for RethCliTxpoolExt { &mut self, _config: &Conf, _components: &Reth, - registry: &mut RethModuleRegistry< - Reth::Provider, - Reth::Pool, - Reth::Network, - Reth::Tasks, - Reth::Events, - >, - modules: &mut TransportRpcModules, + rpc_components: RethRpcComponents<'_, Reth>, ) -> eyre::Result<()> where Conf: RethRpcConfig, @@ -68,11 +58,11 @@ impl RethNodeCommandConfig for RethCliTxpoolExt { } // here we get the configured pool type from the CLI. - let pool = registry.pool().clone(); + let pool = rpc_components.registry.pool().clone(); let ext = TxpoolExt { pool }; // now we merge our extension namespace into all configured transports - modules.merge_configured(ext.into_rpc())?; + rpc_components.modules.merge_configured(ext.into_rpc())?; println!("txpool extension enabled"); Ok(())