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

chore(node-api): move FullNodeComponents from builder #7597

Merged
merged 7 commits into from Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions bin/reth/src/lib.rs
Expand Up @@ -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::*;
Expand Down
4 changes: 4 additions & 0 deletions crates/node-api/Cargo.toml
Expand Up @@ -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
109 changes: 109 additions & 0 deletions crates/node-api/src/node.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Self::Engine>;

/// Returns the task executor.
fn task_executor(&self) -> &TaskExecutor;
}

/// A type that encapsulates all the components of the node.
#[derive(Debug)]
pub struct FullNodeComponentsAdapter<Node: FullNodeTypes, Pool> {
/// 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<Node::Engine>,
/// The task executor of the node.
pub executor: TaskExecutor,
}

impl<Node, Pool> FullNodeTypes for FullNodeComponentsAdapter<Node, Pool>
where
Node: FullNodeTypes,
Pool: TransactionPool + 'static,
{
type DB = Node::DB;
type Provider = Node::Provider;
}

impl<Node, Pool> NodeTypes for FullNodeComponentsAdapter<Node, Pool>
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<Node, Pool> FullNodeComponents for FullNodeComponentsAdapter<Node, Pool>
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::Engine> {
&self.payload_builder
}

fn task_executor(&self) -> &TaskExecutor {
&self.executor
}
}

impl<Node: FullNodeTypes, Pool> Clone for FullNodeComponentsAdapter<Node, Pool>
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(),
}
}
}
9 changes: 4 additions & 5 deletions crates/node-builder/src/builder.rs
Expand Up @@ -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,
Expand All @@ -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},
Expand Down
42 changes: 39 additions & 3 deletions 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`].
Expand Down Expand Up @@ -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<Node: FullNodeTypes> {
/// The transaction pool to use.
type Pool: TransactionPool + Unpin + 'static;

/// Builds the components of the node.
fn build_components(
self,
context: &BuilderContext<Node>,
) -> impl std::future::Future<Output = eyre::Result<NodeComponents<Node, Self::Pool>>> + Send;
}

impl<Node, F, Fut, Pool> NodeComponentsBuilder<Node> for F
where
Node: FullNodeTypes,
F: FnOnce(&BuilderContext<Node>) -> Fut + Send,
Fut: std::future::Future<Output = eyre::Result<NodeComponents<Node, Pool>>> + Send,
Pool: TransactionPool + Unpin + 'static,
{
type Pool = Pool;

fn build_components(
self,
ctx: &BuilderContext<Node>,
) -> impl std::future::Future<Output = eyre::Result<NodeComponents<Node, Self::Pool>>> + Send
{
self(ctx)
}
}
2 changes: 0 additions & 2 deletions crates/node-builder/src/components/mod.rs
Expand Up @@ -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.
///
Expand Down
144 changes: 0 additions & 144 deletions crates/node-builder/src/components/traits.rs

This file was deleted.