reth_node_builder/
node.rs

1// re-export the node api types
2pub use reth_node_api::{FullNodeTypes, NodeTypes, NodeTypesWithEngine};
3
4use std::{
5    marker::PhantomData,
6    ops::{Deref, DerefMut},
7    sync::Arc,
8};
9
10use reth_node_api::{EngineTypes, FullNodeComponents};
11use reth_node_core::{
12    dirs::{ChainPath, DataDirPath},
13    node_config::NodeConfig,
14};
15use reth_payload_builder::PayloadBuilderHandle;
16use reth_provider::ChainSpecProvider;
17use reth_rpc_api::EngineApiClient;
18use reth_rpc_builder::{auth::AuthServerHandle, RpcServerHandle};
19use reth_tasks::TaskExecutor;
20
21use crate::{components::NodeComponentsBuilder, rpc::RethRpcAddOns, NodeAdapter, NodeAddOns};
22
23/// A [`crate::Node`] is a [`NodeTypesWithEngine`] that comes with preconfigured components.
24///
25/// This can be used to configure the builder with a preset of components.
26pub trait Node<N: FullNodeTypes>: NodeTypesWithEngine + Clone {
27    /// The type that builds the node's components.
28    type ComponentsBuilder: NodeComponentsBuilder<N>;
29
30    /// Exposes the customizable node add-on types.
31    type AddOns: NodeAddOns<
32        NodeAdapter<N, <Self::ComponentsBuilder as NodeComponentsBuilder<N>>::Components>,
33    >;
34
35    /// Returns a [`NodeComponentsBuilder`] for the node.
36    fn components_builder(&self) -> Self::ComponentsBuilder;
37
38    /// Returns the node add-ons.
39    fn add_ons(&self) -> Self::AddOns;
40}
41
42/// A [`Node`] type builder
43#[derive(Clone, Default, Debug)]
44pub struct AnyNode<N = (), C = (), AO = ()>(PhantomData<N>, C, AO);
45
46impl<N, C, AO> AnyNode<N, C, AO> {
47    /// Configures the types of the node.
48    pub fn types<T>(self) -> AnyNode<T, C, AO> {
49        AnyNode(PhantomData, self.1, self.2)
50    }
51
52    /// Sets the node components builder.
53    pub fn components_builder<T>(self, value: T) -> AnyNode<N, T, AO> {
54        AnyNode(PhantomData, value, self.2)
55    }
56
57    /// Sets the node add-ons.
58    pub fn add_ons<T>(self, value: T) -> AnyNode<N, C, T> {
59        AnyNode(PhantomData, self.1, value)
60    }
61}
62
63impl<N, C, AO> NodeTypes for AnyNode<N, C, AO>
64where
65    N: FullNodeTypes,
66    C: Send + Sync + Unpin + 'static,
67    AO: Send + Sync + Unpin + Clone + 'static,
68{
69    type Primitives = <N::Types as NodeTypes>::Primitives;
70
71    type ChainSpec = <N::Types as NodeTypes>::ChainSpec;
72
73    type StateCommitment = <N::Types as NodeTypes>::StateCommitment;
74
75    type Storage = <N::Types as NodeTypes>::Storage;
76}
77
78impl<N, C, AO> NodeTypesWithEngine for AnyNode<N, C, AO>
79where
80    N: FullNodeTypes,
81    C: Send + Sync + Unpin + 'static,
82    AO: Send + Sync + Unpin + Clone + 'static,
83{
84    type Engine = <N::Types as NodeTypesWithEngine>::Engine;
85}
86
87impl<N, C, AO> Node<N> for AnyNode<N, C, AO>
88where
89    N: FullNodeTypes + Clone,
90    C: NodeComponentsBuilder<N> + Clone + Sync + Unpin + 'static,
91    AO: NodeAddOns<NodeAdapter<N, C::Components>> + Clone + Sync + Unpin + 'static,
92{
93    type ComponentsBuilder = C;
94    type AddOns = AO;
95
96    fn components_builder(&self) -> Self::ComponentsBuilder {
97        self.1.clone()
98    }
99
100    fn add_ons(&self) -> Self::AddOns {
101        self.2.clone()
102    }
103}
104
105/// The launched node with all components including RPC handlers.
106///
107/// This can be used to interact with the launched node.
108#[derive(Debug)]
109pub struct FullNode<Node: FullNodeComponents, AddOns: NodeAddOns<Node>> {
110    /// The evm configuration.
111    pub evm_config: Node::Evm,
112    /// The executor of the node.
113    pub block_executor: Node::Executor,
114    /// The node's transaction pool.
115    pub pool: Node::Pool,
116    /// Handle to the node's network.
117    pub network: Node::Network,
118    /// Provider to interact with the node's database
119    pub provider: Node::Provider,
120    /// Handle to the node's payload builder service.
121    pub payload_builder: PayloadBuilderHandle<<Node::Types as NodeTypesWithEngine>::Engine>,
122    /// Task executor for the node.
123    pub task_executor: TaskExecutor,
124    /// The initial node config.
125    pub config: NodeConfig<<Node::Types as NodeTypes>::ChainSpec>,
126    /// The data dir of the node.
127    pub data_dir: ChainPath<DataDirPath>,
128    /// The handle to launched add-ons
129    pub add_ons_handle: AddOns::Handle,
130}
131
132impl<Node: FullNodeComponents, AddOns: NodeAddOns<Node>> Clone for FullNode<Node, AddOns> {
133    fn clone(&self) -> Self {
134        Self {
135            evm_config: self.evm_config.clone(),
136            block_executor: self.block_executor.clone(),
137            pool: self.pool.clone(),
138            network: self.network.clone(),
139            provider: self.provider.clone(),
140            payload_builder: self.payload_builder.clone(),
141            task_executor: self.task_executor.clone(),
142            config: self.config.clone(),
143            data_dir: self.data_dir.clone(),
144            add_ons_handle: self.add_ons_handle.clone(),
145        }
146    }
147}
148
149impl<Engine, Node, AddOns> FullNode<Node, AddOns>
150where
151    Engine: EngineTypes,
152    Node: FullNodeComponents<Types: NodeTypesWithEngine<Engine = Engine>>,
153    AddOns: NodeAddOns<Node>,
154{
155    /// Returns the chain spec of the node.
156    pub fn chain_spec(&self) -> Arc<<Node::Types as NodeTypes>::ChainSpec> {
157        self.provider.chain_spec()
158    }
159}
160
161impl<Engine, Node, AddOns> FullNode<Node, AddOns>
162where
163    Engine: EngineTypes,
164    Node: FullNodeComponents<Types: NodeTypesWithEngine<Engine = Engine>>,
165    AddOns: RethRpcAddOns<Node>,
166{
167    /// Returns the [`RpcServerHandle`] to the started rpc server.
168    pub const fn rpc_server_handle(&self) -> &RpcServerHandle {
169        &self.add_ons_handle.rpc_server_handles.rpc
170    }
171
172    /// Returns the [`AuthServerHandle`] to the started authenticated engine API server.
173    pub const fn auth_server_handle(&self) -> &AuthServerHandle {
174        &self.add_ons_handle.rpc_server_handles.auth
175    }
176
177    /// Returns the [`EngineApiClient`] interface for the authenticated engine API.
178    ///
179    /// This will send authenticated http requests to the node's auth server.
180    pub fn engine_http_client(&self) -> impl EngineApiClient<Engine> {
181        self.auth_server_handle().http_client()
182    }
183
184    /// Returns the [`EngineApiClient`] interface for the authenticated engine API.
185    ///
186    /// This will send authenticated ws requests to the node's auth server.
187    pub async fn engine_ws_client(&self) -> impl EngineApiClient<Engine> {
188        self.auth_server_handle().ws_client().await
189    }
190
191    /// Returns the [`EngineApiClient`] interface for the authenticated engine API.
192    ///
193    /// This will send not authenticated IPC requests to the node's auth server.
194    #[cfg(unix)]
195    pub async fn engine_ipc_client(&self) -> Option<impl EngineApiClient<Engine>> {
196        self.auth_server_handle().ipc_client().await
197    }
198}
199
200impl<Node: FullNodeComponents, AddOns: NodeAddOns<Node>> Deref for FullNode<Node, AddOns> {
201    type Target = AddOns::Handle;
202
203    fn deref(&self) -> &Self::Target {
204        &self.add_ons_handle
205    }
206}
207
208impl<Node: FullNodeComponents, AddOns: NodeAddOns<Node>> DerefMut for FullNode<Node, AddOns> {
209    fn deref_mut(&mut self) -> &mut Self::Target {
210        &mut self.add_ons_handle
211    }
212}