Skip to main content

reth_rpc_eth_api/
node.rs

1//! Helper trait for interfacing with [`FullNodeComponents`].
2
3use reth_chain_state::CanonStateSubscriptions;
4use reth_chainspec::{ChainSpecProvider, EthChainSpec, EthereumHardforks, Hardforks};
5use reth_evm::ConfigureEvm;
6use reth_network_api::NetworkInfo;
7use reth_node_api::{FullNodeComponents, NodePrimitives, PrimitivesTy};
8use reth_primitives_traits::{BlockTy, HeaderTy, ReceiptTy, TxTy};
9use reth_rpc_eth_types::EthStateCache;
10use reth_storage_api::{
11    BalProvider, BlockReader, BlockReaderIdExt, NodePrimitivesProvider, PruneCheckpointReader,
12    StageCheckpointReader, StateProviderFactory,
13};
14use reth_transaction_pool::{PoolTransaction, TransactionPool};
15
16/// Helper trait that provides the same interface as [`FullNodeComponents`] but without requiring
17/// implementation of trait bounds.
18///
19/// This trait is structurally equivalent to [`FullNodeComponents`], exposing the same associated
20/// types and methods. However, it doesn't enforce the trait bounds required by
21/// [`FullNodeComponents`]. This makes it useful for RPC types that need access to node components
22/// where the full trait bounds of the components are not necessary.
23///
24/// Every type that is a [`FullNodeComponents`] also implements this trait.
25pub trait RpcNodeCore: Clone + Send + Sync + Unpin + 'static {
26    /// Blockchain data primitives.
27    type Primitives: NodePrimitives;
28    /// The provider type used to interact with the node.
29    type Provider: BlockReaderIdExt<
30            Block = BlockTy<Self::Primitives>,
31            Receipt = ReceiptTy<Self::Primitives>,
32            Header = HeaderTy<Self::Primitives>,
33            Transaction = TxTy<Self::Primitives>,
34        > + ChainSpecProvider<
35            ChainSpec: EthChainSpec<Header = HeaderTy<Self::Primitives>>
36                           + Hardforks
37                           + EthereumHardforks,
38        > + StateProviderFactory<Primitives = Self::Primitives>
39        + NodePrimitivesProvider<Primitives = Self::Primitives>
40        + CanonStateSubscriptions<Primitives = Self::Primitives>
41        + StageCheckpointReader
42        + PruneCheckpointReader
43        + BalProvider
44        + Send
45        + Sync
46        + Clone
47        + Unpin
48        + 'static;
49    /// The transaction pool of the node.
50    type Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Self::Primitives>>>;
51    /// The node's EVM configuration, defining settings for the Ethereum Virtual Machine.
52    type Evm: ConfigureEvm<Primitives = Self::Primitives> + Send + Sync + 'static;
53    /// Network API.
54    type Network: NetworkInfo + Clone;
55
56    /// Returns the transaction pool of the node.
57    fn pool(&self) -> &Self::Pool;
58
59    /// Returns the node's evm config.
60    fn evm_config(&self) -> &Self::Evm;
61
62    /// Returns the handle to the network
63    fn network(&self) -> &Self::Network;
64
65    /// Returns the provider of the node.
66    fn provider(&self) -> &Self::Provider;
67}
68
69impl<T> RpcNodeCore for T
70where
71    T: FullNodeComponents<Provider: ChainSpecProvider<ChainSpec: Hardforks + EthereumHardforks>>,
72{
73    type Primitives = PrimitivesTy<T::Types>;
74    type Provider = T::Provider;
75    type Pool = T::Pool;
76    type Evm = T::Evm;
77    type Network = T::Network;
78
79    #[inline]
80    fn pool(&self) -> &Self::Pool {
81        FullNodeComponents::pool(self)
82    }
83
84    #[inline]
85    fn evm_config(&self) -> &Self::Evm {
86        FullNodeComponents::evm_config(self)
87    }
88
89    #[inline]
90    fn network(&self) -> &Self::Network {
91        FullNodeComponents::network(self)
92    }
93
94    #[inline]
95    fn provider(&self) -> &Self::Provider {
96        FullNodeComponents::provider(self)
97    }
98}
99
100/// Additional components, asides the core node components, needed to run `eth_` namespace API
101/// server.
102pub trait RpcNodeCoreExt: RpcNodeCore<Provider: BlockReader> {
103    /// Returns handle to RPC cache service.
104    fn cache(&self) -> &EthStateCache<Self::Primitives>;
105}
106
107/// An adapter that allows to construct [`RpcNodeCore`] from components.
108#[derive(Debug, Clone)]
109pub struct RpcNodeCoreAdapter<Provider, Pool, Network, Evm> {
110    provider: Provider,
111    pool: Pool,
112    network: Network,
113    evm_config: Evm,
114}
115
116impl<Provider, Pool, Network, Evm> RpcNodeCoreAdapter<Provider, Pool, Network, Evm> {
117    /// Creates a new `RpcNodeCoreAdapter` instance.
118    pub const fn new(provider: Provider, pool: Pool, network: Network, evm_config: Evm) -> Self {
119        Self { provider, pool, network, evm_config }
120    }
121}
122
123impl<Provider, Pool, Network, Evm> RpcNodeCore for RpcNodeCoreAdapter<Provider, Pool, Network, Evm>
124where
125    Provider: BlockReaderIdExt<
126            Block = BlockTy<Evm::Primitives>,
127            Receipt = ReceiptTy<Evm::Primitives>,
128            Header = HeaderTy<Evm::Primitives>,
129            Transaction = TxTy<Evm::Primitives>,
130        > + ChainSpecProvider<
131            ChainSpec: EthChainSpec<Header = HeaderTy<Evm::Primitives>>
132                           + Hardforks
133                           + EthereumHardforks,
134        > + StateProviderFactory<Primitives = Evm::Primitives>
135        + NodePrimitivesProvider<Primitives = Evm::Primitives>
136        + CanonStateSubscriptions<Primitives = Evm::Primitives>
137        + StageCheckpointReader
138        + PruneCheckpointReader
139        + BalProvider
140        + Send
141        + Sync
142        + Unpin
143        + Clone
144        + 'static,
145    Evm: ConfigureEvm + Clone + 'static,
146    Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Evm::Primitives>>>
147        + Unpin
148        + 'static,
149    Network: NetworkInfo + Clone + Unpin + 'static,
150{
151    type Primitives = Evm::Primitives;
152    type Provider = Provider;
153    type Pool = Pool;
154    type Evm = Evm;
155    type Network = Network;
156
157    fn pool(&self) -> &Self::Pool {
158        &self.pool
159    }
160
161    fn evm_config(&self) -> &Self::Evm {
162        &self.evm_config
163    }
164
165    fn network(&self) -> &Self::Network {
166        &self.network
167    }
168
169    fn provider(&self) -> &Self::Provider {
170        &self.provider
171    }
172}