Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify node components #4922

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 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 = &mut RethRpcComponents::<Reth> { 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
11 changes: 11 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,16 @@ pub trait RethNodeComponents {
}
}

/// Helper function to encapsulate [RethModuleRegistry] and [TransportRpcModules]
/// generic over [RethNodeComponents]
pub struct RethRpcComponents<'a, Reth: RethNodeComponents> {
/// A Helper type the holds instances of the configured modules.
pub registry:
RethModuleRegistry<Reth::Provider, Reth::Pool, Reth::Network, Reth::Tasks, Reth::Events>,
supernovahs marked this conversation as resolved.
Show resolved Hide resolved
/// Holds installed modules per transport type.
pub modules: &'a mut TransportRpcModules,
}

/// A Generic implementation of the RethNodeComponents trait.
#[derive(Clone, Debug)]
#[allow(missing_docs)]
Expand Down
31 changes: 10 additions & 21 deletions bin/reth/src/cli/ext.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
//! 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};
#[doc(hidden)]
#[allow(unused)]
/// For doc purposes
use reth_rpc_builder::TransportRpcModules;
supernovahs marked this conversation as resolved.
Show resolved Hide resolved
use reth_tasks::TaskSpawner;
use std::fmt;

Expand Down Expand Up @@ -56,23 +59,16 @@ pub trait RethNodeCommandConfig: fmt::Debug {
&mut self,
config: &Conf,
components: &Reth,
registry: &mut RethModuleRegistry<
Reth::Provider,
Reth::Pool,
Reth::Network,
Reth::Tasks,
Reth::Events,
>,
modules: &mut TransportRpcModules,
node_components: &mut RethRpcComponents<'_, Reth>,
supernovahs marked this conversation as resolved.
Show resolved Hide resolved
) -> eyre::Result<()>
where
Conf: RethRpcConfig,
Reth: RethNodeComponents,
{
let _ = config;
let _ = components;
let _ = registry;
let _ = modules;
let _ = node_components.registry;
let _ = node_components.modules;
supernovahs marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}

Expand Down Expand Up @@ -196,21 +192,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,
node_components: &mut 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, node_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,
node_components: &mut 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 = node_components.registry.pool().clone();
let ext = TxpoolExt { pool };

// now we merge our extension namespace into all configured transports
modules.merge_configured(ext.into_rpc())?;
node_components.modules.merge_configured(ext.into_rpc())?;

println!("txpool extension enabled");
Ok(())
Expand Down
Loading