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