diff --git a/Cargo.lock b/Cargo.lock index 5a6e5c80eb..c18802de01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6124,6 +6124,7 @@ dependencies = [ name = "reth-node-api" version = "0.2.0-beta.4" dependencies = [ + "reth-db", "reth-evm", "reth-primitives", "reth-provider", diff --git a/crates/node-api/Cargo.toml b/crates/node-api/Cargo.toml index 2d8cf84ddc..323f730d92 100644 --- a/crates/node-api/Cargo.toml +++ b/crates/node-api/Cargo.toml @@ -16,6 +16,7 @@ reth-primitives.workspace = true reth-rpc-types.workspace = true reth-evm.workspace = true reth-provider.workspace = true +reth-db.workspace = true # misc serde.workspace = true diff --git a/crates/node-api/src/lib.rs b/crates/node-api/src/lib.rs index 15240e21b0..2f189732b6 100644 --- a/crates/node-api/src/lib.rs +++ b/crates/node-api/src/lib.rs @@ -25,7 +25,7 @@ pub use reth_evm::{ConfigureEvm, ConfigureEvmEnv}; pub mod primitives; pub mod node; -pub use node::NodeTypes; +pub use node::*; // re-export for convenience pub use reth_provider::FullProvider; diff --git a/crates/node-api/src/node.rs b/crates/node-api/src/node.rs index e1a0038969..d1ca897cb0 100644 --- a/crates/node-api/src/node.rs +++ b/crates/node-api/src/node.rs @@ -1,6 +1,9 @@ //! Traits for configuring a node use crate::{primitives::NodePrimitives, ConfigureEvm, EngineTypes}; +use reth_db::database::Database; +use reth_provider::FullProvider; +use std::marker::PhantomData; /// The type that configures the essential types of an ethereum like node. /// @@ -17,3 +20,55 @@ pub trait NodeTypes: Send + Sync + 'static { /// Returns the node's evm config. fn evm_config(&self) -> Self::Evm; } + +/// A helper type that is downstream of the [NodeTypes] trait and adds stateful components to the +/// node. +pub trait FullNodeTypes: NodeTypes + 'static { + /// Underlying database type. + type DB: Database + Clone + 'static; + /// The provider type used to interact with the node. + type Provider: FullProvider; +} + +/// An adapter type that adds the builtin provider type to the user configured node types. +#[derive(Debug)] +pub struct FullNodeTypesAdapter { + /// An instance of the user configured node types. + pub types: Types, + /// The database type used by the node. + pub db: PhantomData, + /// The provider type used by the node. + pub provider: PhantomData, +} + +impl FullNodeTypesAdapter { + /// Create a new adapter from the given node types. + pub fn new(types: Types) -> Self { + Self { types, db: Default::default(), provider: Default::default() } + } +} + +impl NodeTypes for FullNodeTypesAdapter +where + Types: NodeTypes, + DB: Send + Sync + 'static, + Provider: Send + Sync + 'static, +{ + type Primitives = Types::Primitives; + type Engine = Types::Engine; + type Evm = Types::Evm; + + fn evm_config(&self) -> Self::Evm { + self.types.evm_config() + } +} + +impl FullNodeTypes for FullNodeTypesAdapter +where + Types: NodeTypes, + Provider: FullProvider, + DB: Database + Clone + 'static, +{ + type DB = DB; + type Provider = Provider; +} diff --git a/crates/node-builder/src/builder.rs b/crates/node-builder/src/builder.rs index 74a0564d62..72372d15e4 100644 --- a/crates/node-builder/src/builder.rs +++ b/crates/node-builder/src/builder.rs @@ -9,7 +9,7 @@ use crate::{ }, exex::{BoxedLaunchExEx, ExExContext}, hooks::NodeHooks, - node::{FullNode, FullNodeTypes, FullNodeTypesAdapter}, + node::FullNode, rpc::{RethRpcServerHandles, RpcContext, RpcHooks}, Node, NodeHandle, }; @@ -30,7 +30,7 @@ use reth_db::{ }; use reth_interfaces::p2p::either::EitherDownloader; use reth_network::{NetworkBuilder, NetworkConfig, NetworkEvents, NetworkHandle}; -use reth_node_api::NodeTypes; +use reth_node_api::{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 c60a020485..f145846035 100644 --- a/crates/node-builder/src/components/builder.rs +++ b/crates/node-builder/src/components/builder.rs @@ -4,8 +4,7 @@ use crate::{ components::{ NetworkBuilder, NodeComponents, NodeComponentsBuilder, PayloadServiceBuilder, PoolBuilder, }, - node::FullNodeTypes, - BuilderContext, + BuilderContext, FullNodeTypes, }; use std::marker::PhantomData; diff --git a/crates/node-builder/src/components/mod.rs b/crates/node-builder/src/components/mod.rs index 9cb8f800de..c0d2bc8906 100644 --- a/crates/node-builder/src/components/mod.rs +++ b/crates/node-builder/src/components/mod.rs @@ -7,7 +7,7 @@ //! //! Components depend on a fully type configured node: [FullNodeTypes](crate::node::FullNodeTypes). -use crate::node::FullNodeTypes; +use crate::FullNodeTypes; pub use builder::*; pub use network::*; pub use payload::*; diff --git a/crates/node-builder/src/components/network.rs b/crates/node-builder/src/components/network.rs index e6bda16c02..f899162205 100644 --- a/crates/node-builder/src/components/network.rs +++ b/crates/node-builder/src/components/network.rs @@ -1,6 +1,6 @@ //! Network component for the node builder. -use crate::{node::FullNodeTypes, BuilderContext}; +use crate::{BuilderContext, FullNodeTypes}; use reth_network::NetworkHandle; use reth_transaction_pool::TransactionPool; use std::future::Future; diff --git a/crates/node-builder/src/components/payload.rs b/crates/node-builder/src/components/payload.rs index 7c50299bcf..bf5eb13ede 100644 --- a/crates/node-builder/src/components/payload.rs +++ b/crates/node-builder/src/components/payload.rs @@ -1,6 +1,6 @@ //! Payload service component for the node builder. -use crate::{node::FullNodeTypes, BuilderContext}; +use crate::{BuilderContext, FullNodeTypes}; use reth_payload_builder::PayloadBuilderHandle; use reth_transaction_pool::TransactionPool; use std::future::Future; diff --git a/crates/node-builder/src/components/pool.rs b/crates/node-builder/src/components/pool.rs index 8a6bd62c81..029079a999 100644 --- a/crates/node-builder/src/components/pool.rs +++ b/crates/node-builder/src/components/pool.rs @@ -1,5 +1,5 @@ //! Pool component for the node builder. -use crate::{node::FullNodeTypes, BuilderContext}; +use crate::{BuilderContext, FullNodeTypes}; use reth_transaction_pool::TransactionPool; use std::future::Future; diff --git a/crates/node-builder/src/components/traits.rs b/crates/node-builder/src/components/traits.rs index 1c2da88df5..f73de22304 100644 --- a/crates/node-builder/src/components/traits.rs +++ b/crates/node-builder/src/components/traits.rs @@ -1,8 +1,8 @@ //! Traits for the builder -use crate::{components::NodeComponents, node::FullNodeTypes, BuilderContext}; +use crate::{components::NodeComponents, BuilderContext}; use reth_network::NetworkHandle; -use reth_node_api::NodeTypes; +use reth_node_api::{FullNodeTypes, NodeTypes}; use reth_payload_builder::PayloadBuilderHandle; use reth_tasks::TaskExecutor; use reth_transaction_pool::TransactionPool; diff --git a/crates/node-builder/src/node.rs b/crates/node-builder/src/node.rs index 0cf6132690..dd32e8c0ca 100644 --- a/crates/node-builder/src/node.rs +++ b/crates/node-builder/src/node.rs @@ -1,11 +1,8 @@ use crate::{ components::{ComponentsBuilder, FullNodeComponents}, rpc::{RethRpcServerHandles, RpcRegistry}, - FullProvider, }; -use reth_db::database::Database; use reth_network::NetworkHandle; -pub use reth_node_api::NodeTypes; use reth_node_core::{ dirs::{ChainPath, DataDirPath}, node_config::NodeConfig, @@ -18,7 +15,10 @@ use reth_payload_builder::PayloadBuilderHandle; use reth_primitives::ChainSpec; use reth_provider::ChainSpecProvider; use reth_tasks::TaskExecutor; -use std::{marker::PhantomData, sync::Arc}; +use std::sync::Arc; + +// re-export the node api types +pub use reth_node_api::{FullNodeTypes, NodeTypes}; /// A [Node] is a [NodeTypes] that comes with preconfigured components. /// @@ -37,54 +37,6 @@ pub trait Node: NodeTypes + Clone { ) -> ComponentsBuilder; } -/// A helper type that is downstream of the node types and adds stateful components to the node. -pub trait FullNodeTypes: NodeTypes + 'static { - /// Underlying database type. - type DB: Database + Clone + 'static; - /// The provider type used to interact with the node. - type Provider: FullProvider; -} - -/// An adapter type that adds the builtin provider type to the user configured node types. -#[derive(Debug)] -pub struct FullNodeTypesAdapter { - pub(crate) types: Types, - _db: PhantomData, - _provider: PhantomData, -} - -impl FullNodeTypesAdapter { - /// Create a new adapter from the given node types. - pub fn new(types: Types) -> Self { - Self { types, _db: Default::default(), _provider: Default::default() } - } -} - -impl NodeTypes for FullNodeTypesAdapter -where - Types: NodeTypes, - DB: Send + Sync + 'static, - Provider: Send + Sync + 'static, -{ - type Primitives = Types::Primitives; - type Engine = Types::Engine; - type Evm = Types::Evm; - - fn evm_config(&self) -> Self::Evm { - self.types.evm_config() - } -} - -impl FullNodeTypes for FullNodeTypesAdapter -where - Types: NodeTypes, - Provider: FullProvider, - DB: Database + Clone + 'static, -{ - type DB = DB; - type Provider = Provider; -} - /// The launched node with all components including RPC handlers. /// /// This can be used to interact with the launched node.