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