reth_node_builder/
node.rs

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