reth_node_builder/builder/
states.rs1use crate::{
9 components::{NodeComponents, NodeComponentsBuilder},
10 hooks::NodeHooks,
11 launch::LaunchNode,
12 rpc::{RethRpcAddOns, RethRpcServerHandles, RpcContext},
13 AddOns, FullNode,
14};
15
16use reth_exex::ExExContext;
17use reth_node_api::{FullNodeComponents, FullNodeTypes, NodeAddOns, NodeTypes};
18use reth_node_core::node_config::NodeConfig;
19use reth_tasks::TaskExecutor;
20use std::{fmt, fmt::Debug, future::Future};
21
22pub struct NodeBuilderWithTypes<T: FullNodeTypes> {
24 config: NodeConfig<<T::Types as NodeTypes>::ChainSpec>,
26 adapter: NodeTypesAdapter<T>,
28}
29
30impl<T: FullNodeTypes> NodeBuilderWithTypes<T> {
31 pub const fn new(
33 config: NodeConfig<<T::Types as NodeTypes>::ChainSpec>,
34 database: T::DB,
35 ) -> Self {
36 Self { config, adapter: NodeTypesAdapter::new(database) }
37 }
38
39 pub fn with_components<CB>(self, components_builder: CB) -> NodeBuilderWithComponents<T, CB, ()>
41 where
42 CB: NodeComponentsBuilder<T>,
43 {
44 let Self { config, adapter } = self;
45
46 NodeBuilderWithComponents {
47 config,
48 adapter,
49 components_builder,
50 add_ons: AddOns { hooks: NodeHooks::default(), exexs: Vec::new(), add_ons: () },
51 }
52 }
53}
54
55pub struct NodeTypesAdapter<T: FullNodeTypes> {
57 pub database: T::DB,
59}
60
61impl<T: FullNodeTypes> NodeTypesAdapter<T> {
62 pub(crate) const fn new(database: T::DB) -> Self {
64 Self { database }
65 }
66}
67
68impl<T: FullNodeTypes> fmt::Debug for NodeTypesAdapter<T> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 f.debug_struct("NodeTypesAdapter").field("db", &"...").field("types", &"...").finish()
71 }
72}
73
74#[derive(Debug)]
77pub struct NodeAdapter<T: FullNodeTypes, C: NodeComponents<T>> {
78 pub components: C,
80 pub task_executor: TaskExecutor,
82 pub provider: T::Provider,
84}
85
86impl<T: FullNodeTypes, C: NodeComponents<T>> FullNodeTypes for NodeAdapter<T, C> {
87 type Types = T::Types;
88 type DB = T::DB;
89 type Provider = T::Provider;
90}
91
92impl<T: FullNodeTypes, C: NodeComponents<T>> FullNodeComponents for NodeAdapter<T, C> {
93 type Pool = C::Pool;
94 type Evm = C::Evm;
95 type Consensus = C::Consensus;
96 type Network = C::Network;
97
98 fn pool(&self) -> &Self::Pool {
99 self.components.pool()
100 }
101
102 fn evm_config(&self) -> &Self::Evm {
103 self.components.evm_config()
104 }
105
106 fn consensus(&self) -> &Self::Consensus {
107 self.components.consensus()
108 }
109
110 fn network(&self) -> &Self::Network {
111 self.components.network()
112 }
113
114 fn payload_builder_handle(
115 &self,
116 ) -> &reth_payload_builder::PayloadBuilderHandle<
117 <Self::Types as reth_node_api::NodeTypes>::Payload,
118 > {
119 self.components.payload_builder_handle()
120 }
121
122 fn provider(&self) -> &Self::Provider {
123 &self.provider
124 }
125
126 fn task_executor(&self) -> &TaskExecutor {
127 &self.task_executor
128 }
129}
130
131impl<T: FullNodeTypes, C: NodeComponents<T>> Clone for NodeAdapter<T, C> {
132 fn clone(&self) -> Self {
133 Self {
134 components: self.components.clone(),
135 task_executor: self.task_executor.clone(),
136 provider: self.provider.clone(),
137 }
138 }
139}
140
141pub struct NodeBuilderWithComponents<
145 T: FullNodeTypes,
146 CB: NodeComponentsBuilder<T>,
147 AO: NodeAddOns<NodeAdapter<T, CB::Components>>,
148> {
149 pub config: NodeConfig<<T::Types as NodeTypes>::ChainSpec>,
151 pub adapter: NodeTypesAdapter<T>,
153 pub components_builder: CB,
155 pub add_ons: AddOns<NodeAdapter<T, CB::Components>, AO>,
157}
158
159impl<T, CB> NodeBuilderWithComponents<T, CB, ()>
160where
161 T: FullNodeTypes,
162 CB: NodeComponentsBuilder<T>,
163{
164 pub fn with_add_ons<AO>(self, add_ons: AO) -> NodeBuilderWithComponents<T, CB, AO>
167 where
168 AO: NodeAddOns<NodeAdapter<T, CB::Components>>,
169 {
170 let Self { config, adapter, components_builder, .. } = self;
171
172 NodeBuilderWithComponents {
173 config,
174 adapter,
175 components_builder,
176 add_ons: AddOns { hooks: NodeHooks::default(), exexs: Vec::new(), add_ons },
177 }
178 }
179}
180
181impl<T, CB, AO> NodeBuilderWithComponents<T, CB, AO>
182where
183 T: FullNodeTypes,
184 CB: NodeComponentsBuilder<T>,
185 AO: NodeAddOns<NodeAdapter<T, CB::Components>>,
186{
187 pub fn on_component_initialized<F>(mut self, hook: F) -> Self
189 where
190 F: FnOnce(NodeAdapter<T, CB::Components>) -> eyre::Result<()> + Send + 'static,
191 {
192 self.add_ons.hooks.set_on_component_initialized(hook);
193 self
194 }
195
196 pub fn on_node_started<F>(mut self, hook: F) -> Self
198 where
199 F: FnOnce(FullNode<NodeAdapter<T, CB::Components>, AO>) -> eyre::Result<()>
200 + Send
201 + 'static,
202 {
203 self.add_ons.hooks.set_on_node_started(hook);
204 self
205 }
206
207 pub fn install_exex<F, R, E>(mut self, exex_id: impl Into<String>, exex: F) -> Self
213 where
214 F: FnOnce(ExExContext<NodeAdapter<T, CB::Components>>) -> R + Send + 'static,
215 R: Future<Output = eyre::Result<E>> + Send,
216 E: Future<Output = eyre::Result<()>> + Send,
217 {
218 self.add_ons.exexs.push((exex_id.into(), Box::new(exex)));
219 self
220 }
221
222 pub fn launch_with_fn<L, R>(self, launcher: L) -> R
224 where
225 L: FnOnce(Self) -> R,
226 {
227 launcher(self)
228 }
229
230 pub const fn check_launch(self) -> Self {
234 self
235 }
236
237 pub fn map_add_ons<F>(mut self, f: F) -> Self
239 where
240 F: FnOnce(AO) -> AO,
241 {
242 self.add_ons.add_ons = f(self.add_ons.add_ons);
243 self
244 }
245}
246
247impl<T, CB, AO> NodeBuilderWithComponents<T, CB, AO>
248where
249 T: FullNodeTypes,
250 CB: NodeComponentsBuilder<T>,
251 AO: RethRpcAddOns<NodeAdapter<T, CB::Components>>,
252{
253 pub async fn launch_with<L>(self, launcher: L) -> eyre::Result<L::Node>
255 where
256 L: LaunchNode<Self>,
257 {
258 launcher.launch_node(self).await
259 }
260
261 pub fn on_rpc_started<F>(self, hook: F) -> Self
263 where
264 F: FnOnce(
265 RpcContext<'_, NodeAdapter<T, CB::Components>, AO::EthApi>,
266 RethRpcServerHandles,
267 ) -> eyre::Result<()>
268 + Send
269 + 'static,
270 {
271 self.map_add_ons(|mut add_ons| {
272 add_ons.hooks_mut().set_on_rpc_started(hook);
273 add_ons
274 })
275 }
276
277 pub fn extend_rpc_modules<F>(self, hook: F) -> Self
279 where
280 F: FnOnce(RpcContext<'_, NodeAdapter<T, CB::Components>, AO::EthApi>) -> eyre::Result<()>
281 + Send
282 + 'static,
283 {
284 self.map_add_ons(|mut add_ons| {
285 add_ons.hooks_mut().set_extend_rpc_modules(hook);
286 add_ons
287 })
288 }
289}