Skip to content

Commit

Permalink
Simplify node components (#4922)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
supernovahs and mattsse committed Nov 8, 2023
1 parent 77eeaa1 commit a2a0010
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
10 changes: 5 additions & 5 deletions bin/reth/src/args/rpc_server_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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");
}
Expand Down
26 changes: 26 additions & 0 deletions bin/reth/src/cli/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down
31 changes: 7 additions & 24 deletions bin/reth/src/cli/ext.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -49,30 +48,21 @@ 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<Conf, Reth>(
&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,
{
let _ = config;
let _ = components;
let _ = registry;
let _ = modules;
let _ = rpc_components;
Ok(())
}

Expand Down Expand Up @@ -196,21 +186,14 @@ impl<T: RethNodeCommandConfig> RethNodeCommandConfig for NoArgs<T> {
&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(())
}
Expand Down
26 changes: 8 additions & 18 deletions examples/additional-rpc-namespace-in-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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(())
Expand Down

0 comments on commit a2a0010

Please sign in to comment.