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