Moving more types from reth crate to node core crate (#6102)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Aditya Pandey
2024-01-22 20:35:46 +05:30
committed by GitHub
parent b65aea2675
commit 7ceec05e63
49 changed files with 4755 additions and 56 deletions

View File

@@ -0,0 +1,180 @@
//! Components that are used by the node command.
use reth_network::{NetworkEvents, NetworkProtocols};
use reth_network_api::{NetworkInfo, Peers};
use reth_primitives::ChainSpec;
use reth_provider::{
AccountReader, BlockReaderIdExt, CanonStateSubscriptions, ChainSpecProvider, ChangeSetReader,
EvmEnvProvider, StateProviderFactory,
};
use reth_rpc_builder::{
auth::{AuthRpcModule, AuthServerHandle},
RethModuleRegistry, RpcServerHandle, TransportRpcModules,
};
use reth_tasks::TaskSpawner;
use reth_transaction_pool::TransactionPool;
use std::sync::Arc;
/// Helper trait to unify all provider traits for simplicity.
pub trait FullProvider:
BlockReaderIdExt
+ AccountReader
+ StateProviderFactory
+ EvmEnvProvider
+ ChainSpecProvider
+ ChangeSetReader
+ Clone
+ Unpin
+ 'static
{
}
impl<T> FullProvider for T where
T: BlockReaderIdExt
+ AccountReader
+ StateProviderFactory
+ EvmEnvProvider
+ ChainSpecProvider
+ ChangeSetReader
+ Clone
+ Unpin
+ 'static
{
}
/// The trait that is implemented for the Node command.
pub trait RethNodeComponents: Clone + Send + Sync + 'static {
/// The Provider type that is provided by the node itself
type Provider: FullProvider;
/// The transaction pool type
type Pool: TransactionPool + Clone + Unpin + 'static;
/// The network type used to communicate with p2p.
type Network: NetworkInfo + Peers + NetworkProtocols + NetworkEvents + Clone + 'static;
/// The events type used to create subscriptions.
type Events: CanonStateSubscriptions + Clone + 'static;
/// The type that is used to spawn tasks.
type Tasks: TaskSpawner + Clone + Unpin + 'static;
/// Returns the instance of the provider
fn provider(&self) -> Self::Provider;
/// Returns the instance of the task executor.
fn task_executor(&self) -> Self::Tasks;
/// Returns the instance of the transaction pool.
fn pool(&self) -> Self::Pool;
/// Returns the instance of the network API.
fn network(&self) -> Self::Network;
/// Returns the instance of the events subscription handler.
fn events(&self) -> Self::Events;
/// Helper function to return the chain spec.
fn chain_spec(&self) -> Arc<ChainSpec> {
self.provider().chain_spec()
}
}
/// Helper container to encapsulate [RethModuleRegistry],[TransportRpcModules] and [AuthRpcModule].
///
/// 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] as well as configured authenticated methods [AuthRpcModule].
#[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,
/// Holds jwt authenticated rpc module.
///
/// This can be used to merge additional modules into the configured authenticated methods
pub auth_module: &'a mut AuthRpcModule,
}
/// A Generic implementation of the RethNodeComponents trait.
///
/// Represents components required for the Reth node.
#[derive(Clone, Debug)]
pub struct RethNodeComponentsImpl<Provider, Pool, Network, Events, Tasks> {
/// Represents the provider instance.
pub provider: Provider,
/// Represents the transaction pool instance.
pub pool: Pool,
/// Represents the network instance used for communication.
pub network: Network,
/// Represents the task executor instance.
pub task_executor: Tasks,
/// Represents the events subscription handler instance.
pub events: Events,
}
impl<Provider, Pool, Network, Events, Tasks> RethNodeComponents
for RethNodeComponentsImpl<Provider, Pool, Network, Events, Tasks>
where
Provider: FullProvider + Clone + 'static,
Tasks: TaskSpawner + Clone + Unpin + 'static,
Pool: TransactionPool + Clone + Unpin + 'static,
Network: NetworkInfo + Peers + NetworkProtocols + NetworkEvents + Clone + 'static,
Events: CanonStateSubscriptions + Clone + 'static,
{
type Provider = Provider;
type Pool = Pool;
type Network = Network;
type Events = Events;
type Tasks = Tasks;
fn provider(&self) -> Self::Provider {
self.provider.clone()
}
fn task_executor(&self) -> Self::Tasks {
self.task_executor.clone()
}
fn pool(&self) -> Self::Pool {
self.pool.clone()
}
fn network(&self) -> Self::Network {
self.network.clone()
}
fn events(&self) -> Self::Events {
self.events.clone()
}
}
/// Contains the handles to the spawned RPC servers.
///
/// This can be used to access the endpoints of the servers.
///
/// # Example
///
/// ```rust
/// use reth_node_core::{cli::components::RethRpcServerHandles, rpc::api::EthApiClient};
/// # async fn t(handles: RethRpcServerHandles) {
/// let client = handles.rpc.http_client().expect("http server not started");
/// let block_number = client.block_number().await.unwrap();
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct RethRpcServerHandles {
/// The regular RPC server handle.
pub rpc: RpcServerHandle,
/// The handle to the auth server (engine API)
pub auth: AuthServerHandle,
}

View File

@@ -0,0 +1,147 @@
//! Config traits for various node components.
use alloy_rlp::Encodable;
use reth_network::protocol::IntoRlpxSubProtocol;
use reth_primitives::{Bytes, BytesMut};
use reth_rpc::{
eth::{cache::EthStateCacheConfig, gas_oracle::GasPriceOracleConfig},
JwtError, JwtSecret,
};
use reth_rpc_builder::{
auth::AuthServerConfig, error::RpcError, EthConfig, IpcServerBuilder, RpcServerConfig,
ServerBuilder, TransportRpcModuleConfig,
};
use reth_transaction_pool::PoolConfig;
use std::{borrow::Cow, path::PathBuf, time::Duration};
/// A trait that provides a configured RPC server.
///
/// This provides all basic config values for the RPC server and is implemented by the
/// [RpcServerArgs](crate::args::RpcServerArgs) type.
pub trait RethRpcConfig {
/// Returns whether ipc is enabled.
fn is_ipc_enabled(&self) -> bool;
/// Returns the path to the target ipc socket if enabled.
fn ipc_path(&self) -> &str;
/// The configured ethereum RPC settings.
fn eth_config(&self) -> EthConfig;
/// Returns state cache configuration.
fn state_cache_config(&self) -> EthStateCacheConfig;
/// Returns the max request size in bytes.
fn rpc_max_request_size_bytes(&self) -> u32;
/// Returns the max response size in bytes.
fn rpc_max_response_size_bytes(&self) -> u32;
/// Extracts the gas price oracle config from the args.
fn gas_price_oracle_config(&self) -> GasPriceOracleConfig;
/// Creates the [TransportRpcModuleConfig] from cli args.
///
/// This sets all the api modules, and configures additional settings like gas price oracle
/// settings in the [TransportRpcModuleConfig].
fn transport_rpc_module_config(&self) -> TransportRpcModuleConfig;
/// Returns the default server builder for http/ws
fn http_ws_server_builder(&self) -> ServerBuilder;
/// Returns the default ipc server builder
fn ipc_server_builder(&self) -> IpcServerBuilder;
/// Creates the [RpcServerConfig] from cli args.
fn rpc_server_config(&self) -> RpcServerConfig;
/// Creates the [AuthServerConfig] from cli args.
fn auth_server_config(&self, jwt_secret: JwtSecret) -> Result<AuthServerConfig, RpcError>;
/// The execution layer and consensus layer clients SHOULD accept a configuration parameter:
/// jwt-secret, which designates a file containing the hex-encoded 256 bit secret key to be used
/// for verifying/generating JWT tokens.
///
/// If such a parameter is given, but the file cannot be read, or does not contain a hex-encoded
/// key of 256 bits, the client SHOULD treat this as an error.
///
/// If such a parameter is not given, the client SHOULD generate such a token, valid for the
/// duration of the execution, and SHOULD store the hex-encoded secret as a jwt.hex file on
/// the filesystem. This file can then be used to provision the counterpart client.
///
/// The `default_jwt_path` provided as an argument will be used as the default location for the
/// jwt secret in case the `auth_jwtsecret` argument is not provided.
fn auth_jwt_secret(&self, default_jwt_path: PathBuf) -> Result<JwtSecret, JwtError>;
/// Returns the configured jwt secret key for the regular rpc servers, if any.
///
/// Note: this is not used for the auth server (engine API).
fn rpc_secret_key(&self) -> Option<JwtSecret>;
}
/// A trait that provides payload builder settings.
///
/// This provides all basic payload builder settings and is implemented by the
/// [PayloadBuilderArgs](crate::args::PayloadBuilderArgs) type.
pub trait PayloadBuilderConfig {
/// Block extra data set by the payload builder.
fn extradata(&self) -> Cow<'_, str>;
/// Returns the rlp-encoded extradata bytes.
fn extradata_rlp_bytes(&self) -> Bytes {
let mut extradata = BytesMut::new();
self.extradata().as_bytes().encode(&mut extradata);
extradata.freeze().into()
}
/// The interval at which the job should build a new payload after the last.
fn interval(&self) -> Duration;
/// The deadline for when the payload builder job should resolve.
fn deadline(&self) -> Duration;
/// Target gas ceiling for built blocks.
fn max_gas_limit(&self) -> u64;
/// Maximum number of tasks to spawn for building a payload.
fn max_payload_tasks(&self) -> usize;
/// Returns whether or not to construct the pending block.
#[cfg(feature = "optimism")]
fn compute_pending_block(&self) -> bool;
}
/// A trait that represents the configured network and can be used to apply additional configuration
/// to the network.
pub trait RethNetworkConfig {
/// Adds a new additional protocol to the RLPx sub-protocol list.
///
/// These additional protocols are negotiated during the RLPx handshake.
/// If both peers share the same protocol, the corresponding handler will be included alongside
/// the `eth` protocol.
///
/// See also [ProtocolHandler](reth_network::protocol::ProtocolHandler)
fn add_rlpx_sub_protocol(&mut self, protocol: impl IntoRlpxSubProtocol);
/// Returns the secret key used for authenticating sessions.
fn secret_key(&self) -> secp256k1::SecretKey;
// TODO add more network config methods here
}
impl<C> RethNetworkConfig for reth_network::NetworkManager<C> {
fn add_rlpx_sub_protocol(&mut self, protocol: impl IntoRlpxSubProtocol) {
reth_network::NetworkManager::add_rlpx_sub_protocol(self, protocol);
}
fn secret_key(&self) -> secp256k1::SecretKey {
self.secret_key()
}
}
/// A trait that provides all basic config values for the transaction pool and is implemented by the
/// [TxPoolArgs](crate::args::TxPoolArgs) type.
pub trait RethTransactionPoolConfig {
/// Returns transaction pool configuration.
fn pool_config(&self) -> PoolConfig;
}

View File

@@ -0,0 +1,360 @@
//! Support for integrating customizations into the CLI.
use crate::cli::{
components::{RethNodeComponents, RethRpcComponents, RethRpcServerHandles},
config::{PayloadBuilderConfig, RethNetworkConfig, RethRpcConfig},
};
use clap::Args;
use reth_basic_payload_builder::{
BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig, PayloadBuilder,
};
use reth_node_api::EngineTypes;
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_provider::CanonStateSubscriptions;
use reth_tasks::TaskSpawner;
use std::{fmt, marker::PhantomData};
/// A trait that allows for extending parts of the CLI with additional functionality.
///
/// This is intended as a way to allow to _extend_ the node command. For example, to register
/// additional RPC namespaces.
pub trait RethCliExt {
/// Provides additional configuration for the node CLI command.
///
/// This supports additional CLI arguments that can be used to modify the node configuration.
///
/// If no additional CLI arguments are required, the [NoArgs] wrapper type can be used.
type Node: RethNodeCommandExt;
}
/// The default CLI extension.
impl RethCliExt for () {
type Node = DefaultRethNodeCommandConfig;
}
/// A trait that allows for extending and customizing parts of the rethr node command.
///
/// The functions are invoked during the initialization of the node command in the following order:
///
/// 1. [configure_network](RethNodeCommandConfig::configure_network)
/// 2. [on_components_initialized](RethNodeCommandConfig::on_components_initialized)
/// 3. [spawn_payload_builder_service](RethNodeCommandConfig::spawn_payload_builder_service)
/// 4. [extend_rpc_modules](RethNodeCommandConfig::extend_rpc_modules)
/// 5. [on_rpc_server_started](RethNodeCommandConfig::on_rpc_server_started)
/// 6. [on_node_started](RethNodeCommandConfig::on_node_started)
pub trait RethNodeCommandConfig: fmt::Debug {
/// Invoked with the network configuration before the network is configured.
///
/// This allows additional configuration of the network before it is launched.
fn configure_network<Conf, Reth>(
&mut self,
config: &mut Conf,
components: &Reth,
) -> eyre::Result<()>
where
Conf: RethNetworkConfig,
Reth: RethNodeComponents,
{
let _ = config;
let _ = components;
Ok(())
}
/// Event hook called once all components have been initialized.
///
/// This is called as soon as the node components have been initialized.
fn on_components_initialized<Reth: RethNodeComponents>(
&mut self,
components: &Reth,
) -> eyre::Result<()> {
let _ = components;
Ok(())
}
/// Event hook called once the node has been launched.
///
/// This is called last after the node has been launched.
fn on_node_started<Reth: RethNodeComponents>(&mut self, components: &Reth) -> eyre::Result<()> {
let _ = components;
Ok(())
}
/// Event hook called once the rpc servers has been started.
///
/// This is called after the rpc server has been started.
fn on_rpc_server_started<Conf, Reth>(
&mut self,
config: &Conf,
components: &Reth,
rpc_components: RethRpcComponents<'_, Reth>,
handles: RethRpcServerHandles,
) -> eyre::Result<()>
where
Conf: RethRpcConfig,
Reth: RethNodeComponents,
{
let _ = config;
let _ = components;
let _ = rpc_components;
let _ = handles;
Ok(())
}
/// Allows for registering additional RPC modules for the transports.
///
/// This is expected to call the merge functions of [reth_rpc_builder::TransportRpcModules], for
/// example [reth_rpc_builder::TransportRpcModules::merge_configured].
///
/// This is called before the rpc server will be started [Self::on_rpc_server_started].
fn extend_rpc_modules<Conf, Reth>(
&mut self,
config: &Conf,
components: &Reth,
rpc_components: RethRpcComponents<'_, Reth>,
) -> eyre::Result<()>
where
Conf: RethRpcConfig,
Reth: RethNodeComponents,
{
let _ = config;
let _ = components;
let _ = rpc_components;
Ok(())
}
/// Configures the [PayloadBuilderService] for the node, spawns it and returns the
/// [PayloadBuilderHandle].
///
/// By default this spawns a [BasicPayloadJobGenerator] with the default configuration
/// [BasicPayloadJobGeneratorConfig].
fn spawn_payload_builder_service<Conf, Reth, Builder, Engine>(
&mut self,
conf: &Conf,
components: &Reth,
payload_builder: Builder,
) -> eyre::Result<PayloadBuilderHandle<Engine>>
where
Conf: PayloadBuilderConfig,
Reth: RethNodeComponents,
Engine: EngineTypes + 'static,
Builder: PayloadBuilder<
Reth::Pool,
Reth::Provider,
Attributes = Engine::PayloadBuilderAttributes,
BuiltPayload = Engine::BuiltPayload,
> + Unpin
+ 'static,
{
let payload_job_config = BasicPayloadJobGeneratorConfig::default()
.interval(conf.interval())
.deadline(conf.deadline())
.max_payload_tasks(conf.max_payload_tasks())
.extradata(conf.extradata_rlp_bytes())
.max_gas_limit(conf.max_gas_limit());
// no extradata for optimism
#[cfg(feature = "optimism")]
let payload_job_config = payload_job_config.extradata(Default::default());
let payload_generator = BasicPayloadJobGenerator::with_builder(
components.provider(),
components.pool(),
components.task_executor(),
payload_job_config,
components.chain_spec(),
payload_builder,
);
let (payload_service, payload_builder) = PayloadBuilderService::new(
payload_generator,
components.events().canonical_state_stream(),
);
components
.task_executor()
.spawn_critical("payload builder service", Box::pin(payload_service));
Ok(payload_builder)
}
}
/// A trait that allows for extending parts of the CLI with additional functionality.
pub trait RethNodeCommandExt: RethNodeCommandConfig + fmt::Debug + clap::Args {}
// blanket impl for all types that implement the required traits.
impl<T> RethNodeCommandExt for T where T: RethNodeCommandConfig + fmt::Debug + clap::Args {}
/// The default configuration for the reth node command.
///
/// This is a convenience type for [NoArgs<()>].
#[derive(Debug, Clone, Copy, Default, Args)]
#[non_exhaustive]
pub struct DefaultRethNodeCommandConfig;
impl RethNodeCommandConfig for DefaultRethNodeCommandConfig {}
impl RethNodeCommandConfig for () {}
/// A helper type for [RethCliExt] extension that don't require any additional clap Arguments.
#[derive(Debug, Clone, Copy)]
pub struct NoArgsCliExt<Conf>(PhantomData<Conf>);
impl<Conf: RethNodeCommandConfig> RethCliExt for NoArgsCliExt<Conf> {
type Node = NoArgs<Conf>;
}
/// A helper struct that allows for wrapping a [RethNodeCommandConfig] value without providing
/// additional CLI arguments.
///
/// Note: This type must be manually filled with a [RethNodeCommandConfig] manually before executing
/// the reth node command.
#[derive(Debug, Clone, Copy, Default, Args)]
pub struct NoArgs<T = ()> {
#[clap(skip)]
inner: Option<T>,
}
impl<T> NoArgs<T> {
/// Creates a new instance of the wrapper type.
pub fn with(inner: T) -> Self {
Self { inner: Some(inner) }
}
/// Sets the inner value.
pub fn set(&mut self, inner: T) {
self.inner = Some(inner)
}
/// Transforms the configured value.
pub fn map<U>(self, inner: U) -> NoArgs<U> {
NoArgs::with(inner)
}
/// Returns the inner value if it exists.
pub fn inner(&self) -> Option<&T> {
self.inner.as_ref()
}
/// Returns a mutable reference to the inner value if it exists.
pub fn inner_mut(&mut self) -> Option<&mut T> {
self.inner.as_mut()
}
/// Consumes the wrapper and returns the inner value if it exists.
pub fn into_inner(self) -> Option<T> {
self.inner
}
}
impl<T: RethNodeCommandConfig> RethNodeCommandConfig for NoArgs<T> {
fn configure_network<Conf, Reth>(
&mut self,
config: &mut Conf,
components: &Reth,
) -> eyre::Result<()>
where
Conf: RethNetworkConfig,
Reth: RethNodeComponents,
{
if let Some(conf) = self.inner_mut() {
conf.configure_network(config, components)
} else {
Ok(())
}
}
fn on_components_initialized<Reth: RethNodeComponents>(
&mut self,
components: &Reth,
) -> eyre::Result<()> {
if let Some(conf) = self.inner_mut() {
conf.on_components_initialized(components)
} else {
Ok(())
}
}
fn on_node_started<Reth: RethNodeComponents>(&mut self, components: &Reth) -> eyre::Result<()> {
if let Some(conf) = self.inner_mut() {
conf.on_node_started(components)
} else {
Ok(())
}
}
fn on_rpc_server_started<Conf, Reth>(
&mut self,
config: &Conf,
components: &Reth,
rpc_components: RethRpcComponents<'_, Reth>,
handles: RethRpcServerHandles,
) -> eyre::Result<()>
where
Conf: RethRpcConfig,
Reth: RethNodeComponents,
{
if let Some(conf) = self.inner_mut() {
conf.on_rpc_server_started(config, components, rpc_components, handles)
} else {
Ok(())
}
}
fn extend_rpc_modules<Conf, Reth>(
&mut self,
config: &Conf,
components: &Reth,
rpc_components: RethRpcComponents<'_, Reth>,
) -> eyre::Result<()>
where
Conf: RethRpcConfig,
Reth: RethNodeComponents,
{
if let Some(conf) = self.inner_mut() {
conf.extend_rpc_modules(config, components, rpc_components)
} else {
Ok(())
}
}
fn spawn_payload_builder_service<Conf, Reth, Builder, Engine>(
&mut self,
conf: &Conf,
components: &Reth,
payload_builder: Builder,
) -> eyre::Result<PayloadBuilderHandle<Engine>>
where
Conf: PayloadBuilderConfig,
Reth: RethNodeComponents,
Engine: EngineTypes + 'static,
Builder: PayloadBuilder<
Reth::Pool,
Reth::Provider,
Attributes = Engine::PayloadBuilderAttributes,
BuiltPayload = Engine::BuiltPayload,
> + Unpin
+ 'static,
{
self.inner_mut()
.ok_or_else(|| eyre::eyre!("config value must be set"))?
.spawn_payload_builder_service(conf, components, payload_builder)
}
}
impl<T> From<T> for NoArgs<T> {
fn from(value: T) -> Self {
Self::with(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn assert_ext<T: RethNodeCommandExt>() {}
#[test]
fn ensure_ext() {
assert_ext::<DefaultRethNodeCommandConfig>();
assert_ext::<NoArgs<()>>();
}
}

View File

@@ -0,0 +1,3 @@
pub mod components;
pub mod config;
pub mod ext;