Simplify node components (#4922)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Supernovahs.eth
2023-10-05 22:10:17 +05:30
committed by GitHub
parent 1e7d028d53
commit 99dfad9470
4 changed files with 46 additions and 47 deletions

View File

@ -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");
}

View File

@ -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)]

View File

@ -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<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,
@ -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<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(())
}

View File

@ -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(())