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        + Clone
45        + Unpin
46        + 'static;
47    /// The transaction pool of the node.
48    type Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Self::Primitives>>>;
49    /// The node's EVM configuration, defining settings for the Ethereum Virtual Machine.
50    type Evm: ConfigureEvm<Primitives = Self::Primitives> + 'static;
51    /// Network API.
52    type Network: NetworkInfo + Clone;
53
54    /// Returns the transaction pool of the node.
55    fn pool(&self) -> &Self::Pool;
56
57    /// Returns the node's evm config.
58    fn evm_config(&self) -> &Self::Evm;
59
60    /// Returns the handle to the network
61    fn network(&self) -> &Self::Network;
62
63    /// Returns the provider of the node.
64    fn provider(&self) -> &Self::Provider;
65}
66
67impl<T> RpcNodeCore for T
68where
69    T: FullNodeComponents<Provider: ChainSpecProvider<ChainSpec: Hardforks + EthereumHardforks>>,
70{
71    type Primitives = PrimitivesTy<T::Types>;
72    type Provider = T::Provider;
73    type Pool = T::Pool;
74    type Evm = T::Evm;
75    type Network = T::Network;
76
77    #[inline]
78    fn pool(&self) -> &Self::Pool {
79        FullNodeComponents::pool(self)
80    }
81
82    #[inline]
83    fn evm_config(&self) -> &Self::Evm {
84        FullNodeComponents::evm_config(self)
85    }
86
87    #[inline]
88    fn network(&self) -> &Self::Network {
89        FullNodeComponents::network(self)
90    }
91
92    #[inline]
93    fn provider(&self) -> &Self::Provider {
94        FullNodeComponents::provider(self)
95    }
96}
97
98/// Additional components, asides the core node components, needed to run `eth_` namespace API
99/// server.
100pub trait RpcNodeCoreExt: RpcNodeCore<Provider: BlockReader> {
101    /// Returns handle to RPC cache service.
102    fn cache(&self) -> &EthStateCache<Self::Primitives>;
103}
104
105/// An adapter that allows to construct [`RpcNodeCore`] from components.
106#[derive(Debug, Clone)]
107pub struct RpcNodeCoreAdapter<Provider, Pool, Network, Evm> {
108    provider: Provider,
109    pool: Pool,
110    network: Network,
111    evm_config: Evm,
112}
113
114impl<Provider, Pool, Network, Evm> RpcNodeCoreAdapter<Provider, Pool, Network, Evm> {
115    /// Creates a new `RpcNodeCoreAdapter` instance.
116    pub const fn new(provider: Provider, pool: Pool, network: Network, evm_config: Evm) -> Self {
117        Self { provider, pool, network, evm_config }
118    }
119}
120
121impl<Provider, Pool, Network, Evm> RpcNodeCore for RpcNodeCoreAdapter<Provider, Pool, Network, Evm>
122where
123    Provider: BlockReaderIdExt<
124            Block = BlockTy<Evm::Primitives>,
125            Receipt = ReceiptTy<Evm::Primitives>,
126            Header = HeaderTy<Evm::Primitives>,
127            Transaction = TxTy<Evm::Primitives>,
128        > + ChainSpecProvider<
129            ChainSpec: EthChainSpec<Header = HeaderTy<Evm::Primitives>>
130                           + Hardforks
131                           + EthereumHardforks,
132        > + StateProviderFactory<Primitives = Evm::Primitives>
133        + NodePrimitivesProvider<Primitives = Evm::Primitives>
134        + CanonStateSubscriptions<Primitives = Evm::Primitives>
135        + StageCheckpointReader
136        + PruneCheckpointReader
137        + BalProvider
138        + Unpin
139        + Clone
140        + 'static,
141    Evm: ConfigureEvm + 'static,
142    Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Evm::Primitives>>>
143        + Unpin
144        + 'static,
145    Network: NetworkInfo + Clone + Unpin + 'static,
146{
147    type Primitives = Evm::Primitives;
148    type Provider = Provider;
149    type Pool = Pool;
150    type Evm = Evm;
151    type Network = Network;
152
153    fn pool(&self) -> &Self::Pool {
154        &self.pool
155    }
156
157    fn evm_config(&self) -> &Self::Evm {
158        &self.evm_config
159    }
160
161    fn network(&self) -> &Self::Network {
162        &self.network
163    }
164
165    fn provider(&self) -> &Self::Provider {
166        &self.provider
167    }
168}