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