From e1ebc2f06b781a651dea073fa479c0c367604ec1 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Fri, 12 Apr 2024 15:39:02 +0100 Subject: [PATCH] chore(node-api): move `FullNodeComponents` from builder (#7597) Co-authored-by: Matthias Seitz --- Cargo.lock | 5 + bin/reth/src/lib.rs | 5 + crates/node-api/Cargo.toml | 4 + crates/node-api/src/node.rs | 109 +++++++++++++ crates/node-builder/src/builder.rs | 9 +- crates/node-builder/src/components/builder.rs | 42 ++++- crates/node-builder/src/components/mod.rs | 2 - crates/node-builder/src/components/traits.rs | 144 ------------------ crates/node-builder/src/handle.rs | 3 +- crates/node-builder/src/hooks.rs | 3 +- crates/node-builder/src/node.rs | 3 +- crates/node-builder/src/rpc.rs | 2 +- crates/node-e2e-tests/tests/it/dev.rs | 5 +- crates/node-ethereum/Cargo.toml | 2 + crates/node-ethereum/tests/it/builder.rs | 3 +- crates/node-optimism/tests/it/builder.rs | 3 +- 16 files changed, 182 insertions(+), 162 deletions(-) delete mode 100644 crates/node-builder/src/components/traits.rs diff --git a/Cargo.lock b/Cargo.lock index ae67f44a49..8ce28f6856 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6816,7 +6816,11 @@ dependencies = [ "reth-db", "reth-engine-primitives", "reth-evm", + "reth-network", + "reth-payload-builder", "reth-provider", + "reth-tasks", + "reth-transaction-pool", ] [[package]] @@ -6930,6 +6934,7 @@ dependencies = [ "reth-evm-ethereum", "reth-exex", "reth-network", + "reth-node-api", "reth-node-builder", "reth-payload-builder", "reth-provider", diff --git a/bin/reth/src/lib.rs b/bin/reth/src/lib.rs index c1538413fb..00e2c586a2 100644 --- a/bin/reth/src/lib.rs +++ b/bin/reth/src/lib.rs @@ -39,6 +39,11 @@ pub mod payload { pub use reth_payload_validator::ExecutionPayloadValidator; } +/// Re-exported from `reth_node_api`. +pub mod api { + pub use reth_node_api::*; +} + /// Re-exported from `reth_node_core`. pub mod core { pub use reth_node_core::*; diff --git a/crates/node-api/Cargo.toml b/crates/node-api/Cargo.toml index 2d8e1aa6b3..e456f1850a 100644 --- a/crates/node-api/Cargo.toml +++ b/crates/node-api/Cargo.toml @@ -16,3 +16,7 @@ reth-evm.workspace = true reth-provider.workspace = true reth-db.workspace = true reth-engine-primitives.workspace = true +reth-transaction-pool.workspace = true +reth-network.workspace = true +reth-payload-builder.workspace = true +reth-tasks.workspace = true diff --git a/crates/node-api/src/node.rs b/crates/node-api/src/node.rs index d1ca897cb0..1304d77d18 100644 --- a/crates/node-api/src/node.rs +++ b/crates/node-api/src/node.rs @@ -2,7 +2,11 @@ use crate::{primitives::NodePrimitives, ConfigureEvm, EngineTypes}; use reth_db::database::Database; +use reth_network::NetworkHandle; +use reth_payload_builder::PayloadBuilderHandle; use reth_provider::FullProvider; +use reth_tasks::TaskExecutor; +use reth_transaction_pool::TransactionPool; use std::marker::PhantomData; /// The type that configures the essential types of an ethereum like node. @@ -72,3 +76,108 @@ where type DB = DB; type Provider = Provider; } + +/// Encapsulates all types and components of the node. +pub trait FullNodeComponents: FullNodeTypes + 'static { + /// The transaction pool of the node. + type Pool: TransactionPool; + + /// Returns the transaction pool of the node. + fn pool(&self) -> &Self::Pool; + + /// Returns the provider of the node. + fn provider(&self) -> &Self::Provider; + + /// Returns the handle to the network + fn network(&self) -> &NetworkHandle; + + /// Returns the handle to the payload builder service. + fn payload_builder(&self) -> &PayloadBuilderHandle; + + /// Returns the task executor. + fn task_executor(&self) -> &TaskExecutor; +} + +/// A type that encapsulates all the components of the node. +#[derive(Debug)] +pub struct FullNodeComponentsAdapter { + /// The EVM configuration of the node. + pub evm_config: Node::Evm, + /// The transaction pool of the node. + pub pool: Pool, + /// The network handle of the node. + pub network: NetworkHandle, + /// The provider of the node. + pub provider: Node::Provider, + /// The payload builder service handle of the node. + pub payload_builder: PayloadBuilderHandle, + /// The task executor of the node. + pub executor: TaskExecutor, +} + +impl FullNodeTypes for FullNodeComponentsAdapter +where + Node: FullNodeTypes, + Pool: TransactionPool + 'static, +{ + type DB = Node::DB; + type Provider = Node::Provider; +} + +impl NodeTypes for FullNodeComponentsAdapter +where + Node: FullNodeTypes, + Pool: TransactionPool + 'static, +{ + type Primitives = Node::Primitives; + type Engine = Node::Engine; + type Evm = Node::Evm; + + fn evm_config(&self) -> Self::Evm { + self.evm_config.clone() + } +} + +impl FullNodeComponents for FullNodeComponentsAdapter +where + Node: FullNodeTypes, + Pool: TransactionPool + 'static, +{ + type Pool = Pool; + + fn pool(&self) -> &Self::Pool { + &self.pool + } + + fn provider(&self) -> &Self::Provider { + &self.provider + } + + fn network(&self) -> &NetworkHandle { + &self.network + } + + fn payload_builder(&self) -> &PayloadBuilderHandle { + &self.payload_builder + } + + fn task_executor(&self) -> &TaskExecutor { + &self.executor + } +} + +impl Clone for FullNodeComponentsAdapter +where + Pool: Clone, +{ + fn clone(&self) -> Self { + Self { + evm_config: self.evm_config.clone(), + pool: self.pool.clone(), + network: self.network.clone(), + provider: self.provider.clone(), + payload_builder: self.payload_builder.clone(), + executor: self.executor.clone(), + } + } +} diff --git a/crates/node-builder/src/builder.rs b/crates/node-builder/src/builder.rs index a487c93d66..77f7c67afd 100644 --- a/crates/node-builder/src/builder.rs +++ b/crates/node-builder/src/builder.rs @@ -3,10 +3,7 @@ #![allow(clippy::type_complexity, missing_debug_implementations)] use crate::{ - components::{ - ComponentsBuilder, FullNodeComponents, FullNodeComponentsAdapter, NodeComponents, - NodeComponentsBuilder, PoolBuilder, - }, + components::{ComponentsBuilder, NodeComponents, NodeComponentsBuilder, PoolBuilder}, exex::BoxedLaunchExEx, hooks::NodeHooks, node::FullNode, @@ -33,7 +30,9 @@ use reth_db::{ use reth_exex::{ExExContext, ExExHandle, ExExManager}; use reth_interfaces::p2p::either::EitherDownloader; use reth_network::{NetworkBuilder, NetworkConfig, NetworkEvents, NetworkHandle}; -use reth_node_api::{FullNodeTypes, FullNodeTypesAdapter, NodeTypes}; +use reth_node_api::{ + FullNodeComponents, FullNodeComponentsAdapter, FullNodeTypes, FullNodeTypesAdapter, NodeTypes, +}; use reth_node_core::{ cli::config::{PayloadBuilderConfig, RethRpcConfig, RethTransactionPoolConfig}, dirs::{ChainPath, DataDirPath, MaybePlatformPath}, diff --git a/crates/node-builder/src/components/builder.rs b/crates/node-builder/src/components/builder.rs index f145846035..6abdca96c5 100644 --- a/crates/node-builder/src/components/builder.rs +++ b/crates/node-builder/src/components/builder.rs @@ -1,11 +1,10 @@ //! A generic [NodeComponentsBuilder] use crate::{ - components::{ - NetworkBuilder, NodeComponents, NodeComponentsBuilder, PayloadServiceBuilder, PoolBuilder, - }, + components::{NetworkBuilder, NodeComponents, PayloadServiceBuilder, PoolBuilder}, BuilderContext, FullNodeTypes, }; +use reth_transaction_pool::TransactionPool; use std::marker::PhantomData; /// A generic, customizable [`NodeComponentsBuilder`]. @@ -162,3 +161,40 @@ impl Default for ComponentsBuilder<(), (), (), ()> { } } } + +/// A type that configures all the customizable components of the node and knows how to build them. +/// +/// Implementors of this trait are responsible for building all the components of the node: See +/// [NodeComponents]. +/// +/// The [ComponentsBuilder] is a generic implementation of this trait that can be used to customize +/// certain components of the node using the builder pattern and defaults, e.g. Ethereum and +/// Optimism. +pub trait NodeComponentsBuilder { + /// The transaction pool to use. + type Pool: TransactionPool + Unpin + 'static; + + /// Builds the components of the node. + fn build_components( + self, + context: &BuilderContext, + ) -> impl std::future::Future>> + Send; +} + +impl NodeComponentsBuilder for F +where + Node: FullNodeTypes, + F: FnOnce(&BuilderContext) -> Fut + Send, + Fut: std::future::Future>> + Send, + Pool: TransactionPool + Unpin + 'static, +{ + type Pool = Pool; + + fn build_components( + self, + ctx: &BuilderContext, + ) -> impl std::future::Future>> + Send + { + self(ctx) + } +} diff --git a/crates/node-builder/src/components/mod.rs b/crates/node-builder/src/components/mod.rs index c0d2bc8906..4aa73f0fff 100644 --- a/crates/node-builder/src/components/mod.rs +++ b/crates/node-builder/src/components/mod.rs @@ -14,13 +14,11 @@ pub use payload::*; pub use pool::*; use reth_network::NetworkHandle; use reth_payload_builder::PayloadBuilderHandle; -pub use traits::*; mod builder; mod network; mod payload; mod pool; -mod traits; /// All the components of the node. /// diff --git a/crates/node-builder/src/components/traits.rs b/crates/node-builder/src/components/traits.rs deleted file mode 100644 index f73de22304..0000000000 --- a/crates/node-builder/src/components/traits.rs +++ /dev/null @@ -1,144 +0,0 @@ -//! Traits for the builder - -use crate::{components::NodeComponents, BuilderContext}; -use reth_network::NetworkHandle; -use reth_node_api::{FullNodeTypes, NodeTypes}; -use reth_payload_builder::PayloadBuilderHandle; -use reth_tasks::TaskExecutor; -use reth_transaction_pool::TransactionPool; - -/// Encapsulates all types and components of the node. -pub trait FullNodeComponents: FullNodeTypes + 'static { - /// The transaction pool of the node. - type Pool: TransactionPool; - - /// Returns the transaction pool of the node. - fn pool(&self) -> &Self::Pool; - - /// Returns the provider of the node. - fn provider(&self) -> &Self::Provider; - - /// Returns the handle to the network - fn network(&self) -> &NetworkHandle; - - /// Returns the handle to the payload builder service. - fn payload_builder(&self) -> &PayloadBuilderHandle; - - /// Returns the task executor. - fn task_executor(&self) -> &TaskExecutor; -} - -/// A type that encapsulates all the components of the node. -#[derive(Debug)] -pub struct FullNodeComponentsAdapter { - pub(crate) evm_config: Node::Evm, - pub(crate) pool: Pool, - pub(crate) network: NetworkHandle, - pub(crate) provider: Node::Provider, - pub(crate) payload_builder: PayloadBuilderHandle, - pub(crate) executor: TaskExecutor, -} - -impl FullNodeTypes for FullNodeComponentsAdapter -where - Node: FullNodeTypes, - Pool: TransactionPool + 'static, -{ - type DB = Node::DB; - type Provider = Node::Provider; -} - -impl NodeTypes for FullNodeComponentsAdapter -where - Node: FullNodeTypes, - Pool: TransactionPool + 'static, -{ - type Primitives = Node::Primitives; - type Engine = Node::Engine; - type Evm = Node::Evm; - - fn evm_config(&self) -> Self::Evm { - self.evm_config.clone() - } -} - -impl FullNodeComponents for FullNodeComponentsAdapter -where - Node: FullNodeTypes, - Pool: TransactionPool + 'static, -{ - type Pool = Pool; - - fn pool(&self) -> &Self::Pool { - &self.pool - } - - fn provider(&self) -> &Self::Provider { - &self.provider - } - - fn network(&self) -> &NetworkHandle { - &self.network - } - - fn payload_builder(&self) -> &PayloadBuilderHandle { - &self.payload_builder - } - - fn task_executor(&self) -> &TaskExecutor { - &self.executor - } -} - -impl Clone for FullNodeComponentsAdapter -where - Pool: Clone, -{ - fn clone(&self) -> Self { - Self { - evm_config: self.evm_config.clone(), - pool: self.pool.clone(), - network: self.network.clone(), - provider: self.provider.clone(), - payload_builder: self.payload_builder.clone(), - executor: self.executor.clone(), - } - } -} - -/// A type that configures all the customizable components of the node and knows how to build them. -/// -/// Implementors of this trait are responsible for building all the components of the node: See -/// [NodeComponents]. -/// -/// The [ComponentsBuilder](crate::components::builder::ComponentsBuilder) is a generic -/// implementation of this trait that can be used to customize certain components of the node using -/// the builder pattern and defaults, e.g. Ethereum and Optimism. -pub trait NodeComponentsBuilder { - /// The transaction pool to use. - type Pool: TransactionPool + Unpin + 'static; - - /// Builds the components of the node. - fn build_components( - self, - context: &BuilderContext, - ) -> impl std::future::Future>> + Send; -} - -impl NodeComponentsBuilder for F -where - Node: FullNodeTypes, - F: FnOnce(&BuilderContext) -> Fut + Send, - Fut: std::future::Future>> + Send, - Pool: TransactionPool + Unpin + 'static, -{ - type Pool = Pool; - - fn build_components( - self, - ctx: &BuilderContext, - ) -> impl std::future::Future>> + Send - { - self(ctx) - } -} diff --git a/crates/node-builder/src/handle.rs b/crates/node-builder/src/handle.rs index c5174632ec..cbdce0c8b5 100644 --- a/crates/node-builder/src/handle.rs +++ b/crates/node-builder/src/handle.rs @@ -1,4 +1,5 @@ -use crate::{components::FullNodeComponents, node::FullNode}; +use crate::node::FullNode; +use reth_node_api::FullNodeComponents; use reth_node_core::exit::NodeExitFuture; use std::fmt; diff --git a/crates/node-builder/src/hooks.rs b/crates/node-builder/src/hooks.rs index 4233e9c73f..9d2127f5a5 100644 --- a/crates/node-builder/src/hooks.rs +++ b/crates/node-builder/src/hooks.rs @@ -1,4 +1,5 @@ -use crate::{components::FullNodeComponents, node::FullNode}; +use crate::node::FullNode; +use reth_node_api::FullNodeComponents; use std::fmt; /// Container for all the configurable hook functions. diff --git a/crates/node-builder/src/node.rs b/crates/node-builder/src/node.rs index c179140021..766bae14fe 100644 --- a/crates/node-builder/src/node.rs +++ b/crates/node-builder/src/node.rs @@ -1,8 +1,9 @@ use crate::{ - components::{ComponentsBuilder, FullNodeComponents}, + components::ComponentsBuilder, rpc::{RethRpcServerHandles, RpcRegistry}, }; use reth_network::NetworkHandle; +use reth_node_api::FullNodeComponents; use reth_node_core::{ dirs::{ChainPath, DataDirPath}, node_config::NodeConfig, diff --git a/crates/node-builder/src/rpc.rs b/crates/node-builder/src/rpc.rs index 671d96ad7c..d6e2eb0f23 100644 --- a/crates/node-builder/src/rpc.rs +++ b/crates/node-builder/src/rpc.rs @@ -1,8 +1,8 @@ //! Builder support for rpc components. -use crate::components::FullNodeComponents; use futures::TryFutureExt; use reth_network::NetworkHandle; +use reth_node_api::FullNodeComponents; use reth_node_core::{ cli::config::RethRpcConfig, node_config::NodeConfig, diff --git a/crates/node-e2e-tests/tests/it/dev.rs b/crates/node-e2e-tests/tests/it/dev.rs index ef579b1a71..b096bda5aa 100644 --- a/crates/node-e2e-tests/tests/it/dev.rs +++ b/crates/node-e2e-tests/tests/it/dev.rs @@ -1,6 +1,7 @@ use futures_util::StreamExt; use reth::{ - builder::{components::FullNodeComponents, FullNode, NodeBuilder, NodeHandle}, + api::FullNodeComponents, + builder::{FullNode, NodeBuilder, NodeHandle}, providers::CanonStateSubscriptions, rpc::eth::EthTransactions, tasks::TaskManager, @@ -55,7 +56,7 @@ async fn assert_chain_advances(mut node: FullNode Arc { let custom_genesis = r#" { - + "nonce": "0x42", "timestamp": "0x0", "extraData": "0x5343", diff --git a/crates/node-ethereum/Cargo.toml b/crates/node-ethereum/Cargo.toml index d5a7c6a829..4380e57377 100644 --- a/crates/node-ethereum/Cargo.toml +++ b/crates/node-ethereum/Cargo.toml @@ -29,4 +29,6 @@ eyre.workspace = true [dev-dependencies] reth-db.workspace = true reth-exex.workspace = true +reth-node-api.workspace = true + futures.workspace = true diff --git a/crates/node-ethereum/tests/it/builder.rs b/crates/node-ethereum/tests/it/builder.rs index 1998d9bade..7cfc0d705b 100644 --- a/crates/node-ethereum/tests/it/builder.rs +++ b/crates/node-ethereum/tests/it/builder.rs @@ -1,7 +1,8 @@ //! Node builder setup tests. use reth_db::test_utils::create_test_rw_db; -use reth_node_builder::{components::FullNodeComponents, NodeBuilder, NodeConfig}; +use reth_node_api::FullNodeComponents; +use reth_node_builder::{NodeBuilder, NodeConfig}; use reth_node_ethereum::node::EthereumNode; #[test] diff --git a/crates/node-optimism/tests/it/builder.rs b/crates/node-optimism/tests/it/builder.rs index 01bed6b9e1..64f96bd2d9 100644 --- a/crates/node-optimism/tests/it/builder.rs +++ b/crates/node-optimism/tests/it/builder.rs @@ -1,7 +1,8 @@ //! Node builder setup tests. use reth_db::test_utils::create_test_rw_db; -use reth_node_builder::{components::FullNodeComponents, NodeBuilder, NodeConfig}; +use reth_node_api::FullNodeComponents; +use reth_node_builder::{NodeBuilder, NodeConfig}; use reth_node_optimism::node::OptimismNode; #[test]