reth_node_api/
node.rs

1//! Traits for configuring a node.
2
3use crate::ConfigureEvm;
4use alloy_rpc_types_engine::JwtSecret;
5use reth_beacon_consensus::BeaconConsensusEngineHandle;
6use reth_consensus::FullConsensus;
7use reth_evm::execute::BlockExecutorProvider;
8use reth_network_api::FullNetwork;
9use reth_node_core::node_config::NodeConfig;
10use reth_node_types::{HeaderTy, NodeTypes, NodeTypesWithDB, NodeTypesWithEngine, TxTy};
11use reth_payload_builder_primitives::PayloadBuilder;
12use reth_provider::FullProvider;
13use reth_tasks::TaskExecutor;
14use reth_transaction_pool::{PoolTransaction, TransactionPool};
15use std::{future::Future, marker::PhantomData};
16
17/// A helper trait that is downstream of the [`NodeTypesWithEngine`] trait and adds stateful
18/// components to the node.
19///
20/// Its types are configured by node internally and are not intended to be user configurable.
21pub trait FullNodeTypes: Send + Sync + Unpin + 'static {
22    /// Node's types with the database.
23    type Types: NodeTypesWithDB + NodeTypesWithEngine;
24    /// The provider type used to interact with the node.
25    type Provider: FullProvider<Self::Types>;
26}
27
28/// An adapter type that adds the builtin provider type to the user configured node types.
29#[derive(Debug)]
30pub struct FullNodeTypesAdapter<Types, Provider> {
31    /// An instance of the user configured node types.
32    pub types: PhantomData<Types>,
33    /// The provider type used by the node.
34    pub provider: PhantomData<Provider>,
35}
36
37impl<Types, Provider> FullNodeTypes for FullNodeTypesAdapter<Types, Provider>
38where
39    Types: NodeTypesWithDB + NodeTypesWithEngine,
40    Provider: FullProvider<Types>,
41{
42    type Types = Types;
43    type Provider = Provider;
44}
45
46/// Encapsulates all types and components of the node.
47pub trait FullNodeComponents: FullNodeTypes + Clone + 'static {
48    /// The transaction pool of the node.
49    type Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Self::Types>>> + Unpin;
50
51    /// The node's EVM configuration, defining settings for the Ethereum Virtual Machine.
52    type Evm: ConfigureEvm<Header = HeaderTy<Self::Types>, Transaction = TxTy<Self::Types>>;
53
54    /// The type that knows how to execute blocks.
55    type Executor: BlockExecutorProvider<Primitives = <Self::Types as NodeTypes>::Primitives>;
56
57    /// The consensus type of the node.
58    type Consensus: FullConsensus<<Self::Types as NodeTypes>::Primitives> + Clone + Unpin + 'static;
59
60    /// Network API.
61    type Network: FullNetwork;
62
63    /// Builds new blocks.
64    type PayloadBuilder: PayloadBuilder<PayloadType = <Self::Types as NodeTypesWithEngine>::Engine>
65        + Clone;
66
67    /// Returns the transaction pool of the node.
68    fn pool(&self) -> &Self::Pool;
69
70    /// Returns the node's evm config.
71    fn evm_config(&self) -> &Self::Evm;
72
73    /// Returns the node's executor type.
74    fn block_executor(&self) -> &Self::Executor;
75
76    /// Returns the node's consensus type.
77    fn consensus(&self) -> &Self::Consensus;
78
79    /// Returns the handle to the network
80    fn network(&self) -> &Self::Network;
81
82    /// Returns the handle to the payload builder service.
83    fn payload_builder(&self) -> &Self::PayloadBuilder;
84
85    /// Returns the provider of the node.
86    fn provider(&self) -> &Self::Provider;
87
88    /// Returns handle to runtime.
89    fn task_executor(&self) -> &TaskExecutor;
90}
91
92/// Context passed to [`NodeAddOns::launch_add_ons`],
93#[derive(Debug, Clone)]
94pub struct AddOnsContext<'a, N: FullNodeComponents> {
95    /// Node with all configured components.
96    pub node: N,
97    /// Node configuration.
98    pub config: &'a NodeConfig<<N::Types as NodeTypes>::ChainSpec>,
99    /// Handle to the beacon consensus engine.
100    pub beacon_engine_handle:
101        BeaconConsensusEngineHandle<<N::Types as NodeTypesWithEngine>::Engine>,
102    /// JWT secret for the node.
103    pub jwt_secret: JwtSecret,
104}
105
106/// Customizable node add-on types.
107pub trait NodeAddOns<N: FullNodeComponents>: Send {
108    /// Handle to add-ons.
109    type Handle: Send + Sync + Clone;
110
111    /// Configures and launches the add-ons.
112    fn launch_add_ons(
113        self,
114        ctx: AddOnsContext<'_, N>,
115    ) -> impl Future<Output = eyre::Result<Self::Handle>> + Send;
116}
117
118impl<N: FullNodeComponents> NodeAddOns<N> for () {
119    type Handle = ();
120
121    async fn launch_add_ons(self, _components: AddOnsContext<'_, N>) -> eyre::Result<Self::Handle> {
122        Ok(())
123    }
124}