reth_node_api/
node.rs

1//! Traits for configuring a node.
2
3use crate::PayloadTypes;
4use alloy_rpc_types_engine::JwtSecret;
5use reth_basic_payload_builder::PayloadBuilder;
6use reth_consensus::{ConsensusError, FullConsensus};
7use reth_db_api::{database_metrics::DatabaseMetrics, Database};
8use reth_engine_primitives::{BeaconConsensusEngineEvent, BeaconConsensusEngineHandle};
9use reth_evm::ConfigureEvm;
10use reth_network_api::FullNetwork;
11use reth_node_core::node_config::NodeConfig;
12use reth_node_types::{NodeTypes, NodeTypesWithDBAdapter, TxTy};
13use reth_payload_builder::PayloadBuilderHandle;
14use reth_provider::FullProvider;
15use reth_tasks::TaskExecutor;
16use reth_tokio_util::EventSender;
17use reth_transaction_pool::{PoolTransaction, TransactionPool};
18use std::{fmt::Debug, future::Future, marker::PhantomData};
19
20/// A helper trait that is downstream of the [`NodeTypes`] trait and adds stateful
21/// components to the node.
22///
23/// Its types are configured by node internally and are not intended to be user configurable.
24pub trait FullNodeTypes: Clone + Debug + Send + Sync + Unpin + 'static {
25    /// Node's types with the database.
26    type Types: NodeTypes;
27    /// Underlying database type used by the node to store and retrieve data.
28    type DB: Database + DatabaseMetrics + Clone + Unpin + 'static;
29    /// The provider type used to interact with the node.
30    type Provider: FullProvider<NodeTypesWithDBAdapter<Self::Types, Self::DB>>;
31}
32
33/// An adapter type that adds the builtin provider type to the user configured node types.
34#[derive(Clone, Debug)]
35pub struct FullNodeTypesAdapter<Types, DB, Provider>(PhantomData<(Types, DB, Provider)>);
36
37impl<Types, DB, Provider> FullNodeTypes for FullNodeTypesAdapter<Types, DB, Provider>
38where
39    Types: NodeTypes,
40    DB: Database + DatabaseMetrics + Clone + Unpin + 'static,
41    Provider: FullProvider<NodeTypesWithDBAdapter<Types, DB>>,
42{
43    type Types = Types;
44    type DB = DB;
45    type Provider = Provider;
46}
47
48/// Helper trait to bound [`PayloadBuilder`] to the node's engine types.
49pub trait PayloadBuilderFor<N: NodeTypes>:
50    PayloadBuilder<
51    Attributes = <N::Payload as PayloadTypes>::PayloadBuilderAttributes,
52    BuiltPayload = <N::Payload as PayloadTypes>::BuiltPayload,
53>
54{
55}
56
57impl<T, N: NodeTypes> PayloadBuilderFor<N> for T where
58    T: PayloadBuilder<
59        Attributes = <N::Payload as PayloadTypes>::PayloadBuilderAttributes,
60        BuiltPayload = <N::Payload as PayloadTypes>::BuiltPayload,
61    >
62{
63}
64
65/// Encapsulates all types and components of the node.
66pub trait FullNodeComponents: FullNodeTypes + Clone + 'static {
67    /// The transaction pool of the node.
68    type Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Self::Types>>> + Unpin;
69
70    /// The node's EVM configuration, defining settings for the Ethereum Virtual Machine.
71    type Evm: ConfigureEvm<Primitives = <Self::Types as NodeTypes>::Primitives>;
72
73    /// The consensus type of the node.
74    type Consensus: FullConsensus<<Self::Types as NodeTypes>::Primitives, Error = ConsensusError>
75        + Clone
76        + Unpin
77        + 'static;
78
79    /// Network API.
80    type Network: FullNetwork;
81
82    /// Returns the transaction pool of the node.
83    fn pool(&self) -> &Self::Pool;
84
85    /// Returns the node's evm config.
86    fn evm_config(&self) -> &Self::Evm;
87
88    /// Returns the node's consensus type.
89    fn consensus(&self) -> &Self::Consensus;
90
91    /// Returns the handle to the network
92    fn network(&self) -> &Self::Network;
93
94    /// Returns the handle to the payload builder service handling payload building requests from
95    /// the engine.
96    fn payload_builder_handle(&self) -> &PayloadBuilderHandle<<Self::Types as NodeTypes>::Payload>;
97
98    /// Returns the provider of the node.
99    fn provider(&self) -> &Self::Provider;
100
101    /// Returns an executor handle to spawn tasks.
102    ///
103    /// This can be used to spawn critical, blocking tasks or register tasks that should be
104    /// terminated gracefully. See also [`TaskSpawner`](reth_tasks::TaskSpawner).
105    fn task_executor(&self) -> &TaskExecutor;
106}
107
108/// Context passed to [`NodeAddOns::launch_add_ons`],
109#[derive(Debug, Clone)]
110pub struct AddOnsContext<'a, N: FullNodeComponents> {
111    /// Node with all configured components.
112    pub node: N,
113    /// Node configuration.
114    pub config: &'a NodeConfig<<N::Types as NodeTypes>::ChainSpec>,
115    /// Handle to the beacon consensus engine.
116    pub beacon_engine_handle: BeaconConsensusEngineHandle<<N::Types as NodeTypes>::Payload>,
117    /// Notification channel for engine API events
118    pub engine_events: EventSender<BeaconConsensusEngineEvent<<N::Types as NodeTypes>::Primitives>>,
119    /// JWT secret for the node.
120    pub jwt_secret: JwtSecret,
121}
122
123/// Customizable node add-on types.
124pub trait NodeAddOns<N: FullNodeComponents>: Send {
125    /// Handle to add-ons.
126    type Handle: Send + Sync + Clone;
127
128    /// Configures and launches the add-ons.
129    fn launch_add_ons(
130        self,
131        ctx: AddOnsContext<'_, N>,
132    ) -> impl Future<Output = eyre::Result<Self::Handle>> + Send;
133}
134
135impl<N: FullNodeComponents> NodeAddOns<N> for () {
136    type Handle = ();
137
138    async fn launch_add_ons(self, _components: AddOnsContext<'_, N>) -> eyre::Result<Self::Handle> {
139        Ok(())
140    }
141}