reth_node_types/
lib.rs

1//! Standalone crate for Reth configuration traits and builder types.
2
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
5    html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
6    issue_tracker_base_url = "https://github.com/SeismicSystems/seismic-reth/issues/"
7)]
8#![cfg_attr(not(test), warn(unused_crate_dependencies))]
9#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
10#![cfg_attr(not(feature = "std"), no_std)]
11
12use core::{fmt::Debug, marker::PhantomData};
13pub use reth_primitives_traits::{
14    Block, BlockBody, FullBlock, FullNodePrimitives, FullReceipt, FullSignedTx, NodePrimitives,
15};
16
17use reth_chainspec::EthChainSpec;
18use reth_db_api::{
19    database_metrics::{DatabaseMetadata, DatabaseMetrics},
20    Database,
21};
22use reth_engine_primitives::EngineTypes;
23use reth_trie_db::StateCommitment;
24
25/// The type that configures the essential types of an Ethereum-like node.
26///
27/// This includes the primitive types of a node and chain specification.
28///
29/// This trait is intended to be stateless and only define the types of the node.
30pub trait NodeTypes: Send + Sync + Unpin + 'static {
31    /// The node's primitive types, defining basic operations and structures.
32    type Primitives: NodePrimitives;
33    /// The type used for configuration of the EVM.
34    type ChainSpec: EthChainSpec<Header = <Self::Primitives as NodePrimitives>::BlockHeader>;
35    /// The type used to perform state commitment operations.
36    type StateCommitment: StateCommitment;
37    /// The type responsible for writing chain primitives to storage.
38    type Storage: Default + Send + Sync + Unpin + Debug + 'static;
39}
40
41/// The type that configures an Ethereum-like node with an engine for consensus.
42pub trait NodeTypesWithEngine: NodeTypes {
43    /// The node's engine types, defining the interaction with the consensus engine.
44    type Engine: EngineTypes;
45}
46
47/// A helper trait that is downstream of the [`NodeTypesWithEngine`] trait and adds database to the
48/// node.
49///
50/// Its types are configured by node internally and are not intended to be user configurable.
51pub trait NodeTypesWithDB: NodeTypes {
52    /// Underlying database type used by the node to store and retrieve data.
53    type DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static;
54}
55
56/// An adapter type combining [`NodeTypes`] and db into [`NodeTypesWithDB`].
57#[derive(Debug)]
58pub struct NodeTypesWithDBAdapter<Types, DB> {
59    types: PhantomData<Types>,
60    db: PhantomData<DB>,
61}
62
63impl<Types, DB> NodeTypesWithDBAdapter<Types, DB> {
64    /// Create a new adapter with the configured types.
65    pub fn new() -> Self {
66        Self { types: Default::default(), db: Default::default() }
67    }
68}
69
70impl<Types, DB> Default for NodeTypesWithDBAdapter<Types, DB> {
71    fn default() -> Self {
72        Self::new()
73    }
74}
75
76impl<Types, DB> Clone for NodeTypesWithDBAdapter<Types, DB> {
77    fn clone(&self) -> Self {
78        Self { types: self.types, db: self.db }
79    }
80}
81
82impl<Types, DB> NodeTypes for NodeTypesWithDBAdapter<Types, DB>
83where
84    Types: NodeTypes,
85    DB: Send + Sync + Unpin + 'static,
86{
87    type Primitives = Types::Primitives;
88    type ChainSpec = Types::ChainSpec;
89    type StateCommitment = Types::StateCommitment;
90    type Storage = Types::Storage;
91}
92
93impl<Types, DB> NodeTypesWithEngine for NodeTypesWithDBAdapter<Types, DB>
94where
95    Types: NodeTypesWithEngine,
96    DB: Send + Sync + Unpin + 'static,
97{
98    type Engine = Types::Engine;
99}
100
101impl<Types, DB> NodeTypesWithDB for NodeTypesWithDBAdapter<Types, DB>
102where
103    Types: NodeTypes,
104    DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static,
105{
106    type DB = DB;
107}
108
109/// A [`NodeTypes`] type builder.
110#[derive(Debug)]
111pub struct AnyNodeTypes<P = (), C = (), SC = (), S = ()>(
112    PhantomData<P>,
113    PhantomData<C>,
114    PhantomData<SC>,
115    PhantomData<S>,
116);
117
118impl<P, C, SC, S> Default for AnyNodeTypes<P, C, SC, S> {
119    fn default() -> Self {
120        Self::new()
121    }
122}
123
124impl<P, C, SC, S> AnyNodeTypes<P, C, SC, S> {
125    /// Creates a new instance of [`AnyNodeTypes`].
126    pub const fn new() -> Self {
127        Self(PhantomData, PhantomData, PhantomData, PhantomData)
128    }
129
130    /// Sets the `Primitives` associated type.
131    pub const fn primitives<T>(self) -> AnyNodeTypes<T, C, SC, S> {
132        AnyNodeTypes::new()
133    }
134
135    /// Sets the `ChainSpec` associated type.
136    pub const fn chain_spec<T>(self) -> AnyNodeTypes<P, T, SC, S> {
137        AnyNodeTypes::new()
138    }
139
140    /// Sets the `StateCommitment` associated type.
141    pub const fn state_commitment<T>(self) -> AnyNodeTypes<P, C, T, S> {
142        AnyNodeTypes::new()
143    }
144
145    /// Sets the `Storage` associated type.
146    pub const fn storage<T>(self) -> AnyNodeTypes<P, C, SC, T> {
147        AnyNodeTypes::new()
148    }
149}
150
151impl<P, C, SC, S> NodeTypes for AnyNodeTypes<P, C, SC, S>
152where
153    P: NodePrimitives + Send + Sync + Unpin + 'static,
154    C: EthChainSpec<Header = P::BlockHeader> + 'static,
155    SC: StateCommitment,
156    S: Default + Send + Sync + Unpin + Debug + 'static,
157{
158    type Primitives = P;
159    type ChainSpec = C;
160    type StateCommitment = SC;
161    type Storage = S;
162}
163
164/// A [`NodeTypesWithEngine`] type builder.
165#[derive(Debug)]
166pub struct AnyNodeTypesWithEngine<P = (), E = (), C = (), SC = (), S = ()> {
167    /// Embedding the basic node types.
168    _base: AnyNodeTypes<P, C, SC, S>,
169    /// Phantom data for the engine.
170    _engine: PhantomData<E>,
171}
172
173impl<P, E, C, SC, S> Default for AnyNodeTypesWithEngine<P, E, C, SC, S> {
174    fn default() -> Self {
175        Self::new()
176    }
177}
178
179impl<P, E, C, SC, S> AnyNodeTypesWithEngine<P, E, C, SC, S> {
180    /// Creates a new instance of [`AnyNodeTypesWithEngine`].
181    pub const fn new() -> Self {
182        Self { _base: AnyNodeTypes::new(), _engine: PhantomData }
183    }
184
185    /// Sets the `Primitives` associated type.
186    pub const fn primitives<T>(self) -> AnyNodeTypesWithEngine<T, E, C, SC, S> {
187        AnyNodeTypesWithEngine::new()
188    }
189
190    /// Sets the `Engine` associated type.
191    pub const fn engine<T>(self) -> AnyNodeTypesWithEngine<P, T, C, SC, S> {
192        AnyNodeTypesWithEngine::new()
193    }
194
195    /// Sets the `ChainSpec` associated type.
196    pub const fn chain_spec<T>(self) -> AnyNodeTypesWithEngine<P, E, T, SC, S> {
197        AnyNodeTypesWithEngine::new()
198    }
199
200    /// Sets the `StateCommitment` associated type.
201    pub const fn state_commitment<T>(self) -> AnyNodeTypesWithEngine<P, E, C, T, S> {
202        AnyNodeTypesWithEngine::new()
203    }
204
205    /// Sets the `Storage` associated type.
206    pub const fn storage<T>(self) -> AnyNodeTypesWithEngine<P, E, C, SC, T> {
207        AnyNodeTypesWithEngine::new()
208    }
209}
210
211impl<P, E, C, SC, S> NodeTypes for AnyNodeTypesWithEngine<P, E, C, SC, S>
212where
213    P: NodePrimitives + Send + Sync + Unpin + 'static,
214    E: EngineTypes + Send + Sync + Unpin,
215    C: EthChainSpec<Header = P::BlockHeader> + 'static,
216    SC: StateCommitment,
217    S: Default + Send + Sync + Unpin + Debug + 'static,
218{
219    type Primitives = P;
220    type ChainSpec = C;
221    type StateCommitment = SC;
222    type Storage = S;
223}
224
225impl<P, E, C, SC, S> NodeTypesWithEngine for AnyNodeTypesWithEngine<P, E, C, SC, S>
226where
227    P: NodePrimitives + Send + Sync + Unpin + 'static,
228    E: EngineTypes + Send + Sync + Unpin,
229    C: EthChainSpec<Header = P::BlockHeader> + 'static,
230    SC: StateCommitment,
231    S: Default + Send + Sync + Unpin + Debug + 'static,
232{
233    type Engine = E;
234}
235
236/// Helper adapter type for accessing [`NodePrimitives::Block`] on [`NodeTypes`].
237pub type BlockTy<N> = <<N as NodeTypes>::Primitives as NodePrimitives>::Block;
238
239/// Helper adapter type for accessing [`NodePrimitives::BlockHeader`] on [`NodeTypes`].
240pub type HeaderTy<N> = <<N as NodeTypes>::Primitives as NodePrimitives>::BlockHeader;
241
242/// Helper adapter type for accessing [`NodePrimitives::BlockBody`] on [`NodeTypes`].
243pub type BodyTy<N> = <<N as NodeTypes>::Primitives as NodePrimitives>::BlockBody;
244
245/// Helper adapter type for accessing [`NodePrimitives::SignedTx`] on [`NodeTypes`].
246pub type TxTy<N> = <<N as NodeTypes>::Primitives as NodePrimitives>::SignedTx;
247
248/// Helper adapter type for accessing [`NodePrimitives::Receipt`] on [`NodeTypes`].
249pub type ReceiptTy<N> = <<N as NodeTypes>::Primitives as NodePrimitives>::Receipt;