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/paradigmxyz/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::{database_metrics::DatabaseMetrics, Database};
19use reth_engine_primitives::EngineTypes;
20use reth_payload_primitives::{BuiltPayload, PayloadTypes};
21use reth_trie_db::StateCommitment;
22
23/// The type that configures the essential types of an Ethereum-like node.
24///
25/// This includes the primitive types of a node and chain specification.
26///
27/// This trait is intended to be stateless and only define the types of the node.
28pub trait NodeTypes: Clone + Debug + Send + Sync + Unpin + 'static {
29    /// The node's primitive types, defining basic operations and structures.
30    type Primitives: NodePrimitives;
31    /// The type used for configuration of the EVM.
32    type ChainSpec: EthChainSpec<Header = <Self::Primitives as NodePrimitives>::BlockHeader>;
33    /// The type used to perform state commitment operations.
34    type StateCommitment: StateCommitment;
35    /// The type responsible for writing chain primitives to storage.
36    type Storage: Default + Send + Sync + Unpin + Debug + 'static;
37    /// The node's engine types, defining the interaction with the consensus engine.
38    type Payload: PayloadTypes<BuiltPayload: BuiltPayload<Primitives = Self::Primitives>>;
39}
40
41/// A helper trait that is downstream of the [`NodeTypes`] trait and adds database to the
42/// node.
43///
44/// Its types are configured by node internally and are not intended to be user configurable.
45pub trait NodeTypesWithDB: NodeTypes {
46    /// Underlying database type used by the node to store and retrieve data.
47    type DB: Database + DatabaseMetrics + Clone + Unpin + 'static;
48}
49
50/// An adapter type combining [`NodeTypes`] and db into [`NodeTypesWithDB`].
51#[derive(Clone, Debug, Default)]
52pub struct NodeTypesWithDBAdapter<Types, DB> {
53    types: PhantomData<Types>,
54    db: PhantomData<DB>,
55}
56
57impl<Types, DB> NodeTypesWithDBAdapter<Types, DB> {
58    /// Create a new adapter with the configured types.
59    pub fn new() -> Self {
60        Self { types: Default::default(), db: Default::default() }
61    }
62}
63
64impl<Types, DB> NodeTypes for NodeTypesWithDBAdapter<Types, DB>
65where
66    Types: NodeTypes,
67    DB: Clone + Debug + Send + Sync + Unpin + 'static,
68{
69    type Primitives = Types::Primitives;
70    type ChainSpec = Types::ChainSpec;
71    type StateCommitment = Types::StateCommitment;
72    type Storage = Types::Storage;
73    type Payload = Types::Payload;
74}
75
76impl<Types, DB> NodeTypesWithDB for NodeTypesWithDBAdapter<Types, DB>
77where
78    Types: NodeTypes,
79    DB: Database + DatabaseMetrics + Clone + Unpin + 'static,
80{
81    type DB = DB;
82}
83
84/// A [`NodeTypes`] type builder.
85#[derive(Clone, Debug, Default)]
86pub struct AnyNodeTypes<P = (), C = (), SC = (), S = (), PL = ()>(
87    PhantomData<P>,
88    PhantomData<C>,
89    PhantomData<SC>,
90    PhantomData<S>,
91    PhantomData<PL>,
92);
93
94impl<P, C, SC, S, PL> AnyNodeTypes<P, C, SC, S, PL> {
95    /// Creates a new instance of [`AnyNodeTypes`].
96    pub const fn new() -> Self {
97        Self(PhantomData, PhantomData, PhantomData, PhantomData, PhantomData)
98    }
99
100    /// Sets the `Primitives` associated type.
101    pub const fn primitives<T>(self) -> AnyNodeTypes<T, C, SC, S, PL> {
102        AnyNodeTypes::new()
103    }
104
105    /// Sets the `ChainSpec` associated type.
106    pub const fn chain_spec<T>(self) -> AnyNodeTypes<P, T, SC, S, PL> {
107        AnyNodeTypes::new()
108    }
109
110    /// Sets the `StateCommitment` associated type.
111    pub const fn state_commitment<T>(self) -> AnyNodeTypes<P, C, T, S, PL> {
112        AnyNodeTypes::new()
113    }
114
115    /// Sets the `Storage` associated type.
116    pub const fn storage<T>(self) -> AnyNodeTypes<P, C, SC, T, PL> {
117        AnyNodeTypes::new()
118    }
119
120    /// Sets the `Payload` associated type.
121    pub const fn payload<T>(self) -> AnyNodeTypes<P, C, SC, S, T> {
122        AnyNodeTypes::new()
123    }
124}
125
126impl<P, C, SC, S, PL> NodeTypes for AnyNodeTypes<P, C, SC, S, PL>
127where
128    P: NodePrimitives + Send + Sync + Unpin + 'static,
129    C: EthChainSpec<Header = P::BlockHeader> + Clone + 'static,
130    SC: StateCommitment,
131    S: Default + Clone + Send + Sync + Unpin + Debug + 'static,
132    PL: PayloadTypes<BuiltPayload: BuiltPayload<Primitives = P>> + Send + Sync + Unpin + 'static,
133{
134    type Primitives = P;
135    type ChainSpec = C;
136    type StateCommitment = SC;
137    type Storage = S;
138    type Payload = PL;
139}
140
141/// A [`NodeTypes`] type builder.
142#[derive(Clone, Debug, Default)]
143pub struct AnyNodeTypesWithEngine<P = (), E = (), C = (), SC = (), S = (), PL = ()> {
144    /// Embedding the basic node types.
145    _base: AnyNodeTypes<P, C, SC, S, PL>,
146    /// Phantom data for the engine.
147    _engine: PhantomData<E>,
148}
149
150impl<P, E, C, SC, S, PL> AnyNodeTypesWithEngine<P, E, C, SC, S, PL> {
151    /// Creates a new instance of [`AnyNodeTypesWithEngine`].
152    pub const fn new() -> Self {
153        Self { _base: AnyNodeTypes::new(), _engine: PhantomData }
154    }
155
156    /// Sets the `Primitives` associated type.
157    pub const fn primitives<T>(self) -> AnyNodeTypesWithEngine<T, E, C, SC, S, PL> {
158        AnyNodeTypesWithEngine::new()
159    }
160
161    /// Sets the `Engine` associated type.
162    pub const fn engine<T>(self) -> AnyNodeTypesWithEngine<P, T, C, SC, S, PL> {
163        AnyNodeTypesWithEngine::new()
164    }
165
166    /// Sets the `ChainSpec` associated type.
167    pub const fn chain_spec<T>(self) -> AnyNodeTypesWithEngine<P, E, T, SC, S, PL> {
168        AnyNodeTypesWithEngine::new()
169    }
170
171    /// Sets the `StateCommitment` associated type.
172    pub const fn state_commitment<T>(self) -> AnyNodeTypesWithEngine<P, E, C, T, S, PL> {
173        AnyNodeTypesWithEngine::new()
174    }
175
176    /// Sets the `Storage` associated type.
177    pub const fn storage<T>(self) -> AnyNodeTypesWithEngine<P, E, C, SC, T, PL> {
178        AnyNodeTypesWithEngine::new()
179    }
180
181    /// Sets the `Payload` associated type.
182    pub const fn payload<T>(self) -> AnyNodeTypesWithEngine<P, E, C, SC, S, T> {
183        AnyNodeTypesWithEngine::new()
184    }
185}
186
187impl<P, E, C, SC, S, PL> NodeTypes for AnyNodeTypesWithEngine<P, E, C, SC, S, PL>
188where
189    P: NodePrimitives + Send + Sync + Unpin + 'static,
190    E: EngineTypes + Send + Sync + Unpin,
191    C: EthChainSpec<Header = P::BlockHeader> + Clone + 'static,
192    SC: StateCommitment,
193    S: Default + Clone + Send + Sync + Unpin + Debug + 'static,
194    PL: PayloadTypes<BuiltPayload: BuiltPayload<Primitives = P>> + Send + Sync + Unpin + 'static,
195{
196    type Primitives = P;
197    type ChainSpec = C;
198    type StateCommitment = SC;
199    type Storage = S;
200    type Payload = PL;
201}
202
203/// Helper adapter type for accessing [`NodePrimitives::Block`] on [`NodeTypes`].
204pub type BlockTy<N> = <PrimitivesTy<N> as NodePrimitives>::Block;
205
206/// Helper adapter type for accessing [`NodePrimitives::BlockHeader`] on [`NodeTypes`].
207pub type HeaderTy<N> = <PrimitivesTy<N> as NodePrimitives>::BlockHeader;
208
209/// Helper adapter type for accessing [`NodePrimitives::BlockBody`] on [`NodeTypes`].
210pub type BodyTy<N> = <PrimitivesTy<N> as NodePrimitives>::BlockBody;
211
212/// Helper adapter type for accessing [`NodePrimitives::SignedTx`] on [`NodeTypes`].
213pub type TxTy<N> = <PrimitivesTy<N> as NodePrimitives>::SignedTx;
214
215/// Helper adapter type for accessing [`NodePrimitives::Receipt`] on [`NodeTypes`].
216pub type ReceiptTy<N> = <PrimitivesTy<N> as NodePrimitives>::Receipt;
217
218/// Helper type for getting the `Primitives` associated type from a [`NodeTypes`].
219pub type PrimitivesTy<N> = <N as NodeTypes>::Primitives;
220
221/// Helper type for getting the `Primitives` associated type from a [`NodeTypes`].
222pub type KeyHasherTy<N> = <<N as NodeTypes>::StateCommitment as StateCommitment>::KeyHasher;