Skip to main content

reth_rpc_eth_api/
node.rs

1//! Helper trait for interfacing with node components.
2
3use reth_chain_state::CanonStateSubscriptions;
4use reth_chainspec::{ChainSpecProvider, EthChainSpec, EthereumHardforks, Hardforks};
5use reth_evm::ConfigureEvm;
6use reth_network_api::NetworkInfo;
7use reth_primitives_traits::{BlockTy, HeaderTy, NodePrimitives, ReceiptTy, TxTy};
8use reth_rpc_eth_types::EthStateCache;
9use reth_storage_api::{
10    BalProvider, BlockReader, BlockReaderIdExt, NodePrimitivesProvider, PruneCheckpointReader,
11    StageCheckpointReader, StateProviderFactory,
12};
13use reth_transaction_pool::{PoolTransaction, TransactionPool};
14
15/// Helper trait that exposes the node components needed by RPC types: the provider, pool, network
16/// and EVM config.
17///
18/// Unlike a full node components trait, it doesn't enforce the bounds required to run a node. This
19/// makes it useful for RPC types that only need these components. Use [`RpcNodeCoreAdapter`] to
20/// construct an implementation from the components.
21pub trait RpcNodeCore: Clone + Send + Sync + Unpin + 'static {
22    /// Blockchain data primitives.
23    type Primitives: NodePrimitives;
24    /// The provider type used to interact with the node.
25    type Provider: BlockReaderIdExt<
26            Block = BlockTy<Self::Primitives>,
27            Receipt = ReceiptTy<Self::Primitives>,
28            Header = HeaderTy<Self::Primitives>,
29            Transaction = TxTy<Self::Primitives>,
30        > + ChainSpecProvider<
31            ChainSpec: EthChainSpec<Header = HeaderTy<Self::Primitives>>
32                           + Hardforks
33                           + EthereumHardforks,
34        > + StateProviderFactory<Primitives = Self::Primitives>
35        + NodePrimitivesProvider<Primitives = Self::Primitives>
36        + CanonStateSubscriptions<Primitives = Self::Primitives>
37        + StageCheckpointReader
38        + PruneCheckpointReader
39        + BalProvider
40        + Clone
41        + Unpin
42        + 'static;
43    /// The transaction pool of the node.
44    type Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Self::Primitives>>>;
45    /// The node's EVM configuration, defining settings for the Ethereum Virtual Machine.
46    type Evm: ConfigureEvm<Primitives = Self::Primitives> + 'static;
47    /// Network API.
48    type Network: NetworkInfo + Clone;
49
50    /// Returns the transaction pool of the node.
51    fn pool(&self) -> &Self::Pool;
52
53    /// Returns the node's evm config.
54    fn evm_config(&self) -> &Self::Evm;
55
56    /// Returns the handle to the network
57    fn network(&self) -> &Self::Network;
58
59    /// Returns the provider of the node.
60    fn provider(&self) -> &Self::Provider;
61}
62
63/// Additional components, asides the core node components, needed to run `eth_` namespace API
64/// server.
65pub trait RpcNodeCoreExt: RpcNodeCore<Provider: BlockReader> {
66    /// Returns handle to RPC cache service.
67    fn cache(&self) -> &EthStateCache<Self::Primitives>;
68}
69
70/// An adapter that allows to construct [`RpcNodeCore`] from components.
71#[derive(Debug, Clone)]
72pub struct RpcNodeCoreAdapter<Provider, Pool, Network, Evm> {
73    provider: Provider,
74    pool: Pool,
75    network: Network,
76    evm_config: Evm,
77}
78
79impl<Provider, Pool, Network, Evm> RpcNodeCoreAdapter<Provider, Pool, Network, Evm> {
80    /// Creates a new `RpcNodeCoreAdapter` instance.
81    pub const fn new(provider: Provider, pool: Pool, network: Network, evm_config: Evm) -> Self {
82        Self { provider, pool, network, evm_config }
83    }
84}
85
86impl<Provider, Pool, Network, Evm> RpcNodeCore for RpcNodeCoreAdapter<Provider, Pool, Network, Evm>
87where
88    Provider: BlockReaderIdExt<
89            Block = BlockTy<Evm::Primitives>,
90            Receipt = ReceiptTy<Evm::Primitives>,
91            Header = HeaderTy<Evm::Primitives>,
92            Transaction = TxTy<Evm::Primitives>,
93        > + ChainSpecProvider<
94            ChainSpec: EthChainSpec<Header = HeaderTy<Evm::Primitives>>
95                           + Hardforks
96                           + EthereumHardforks,
97        > + StateProviderFactory<Primitives = Evm::Primitives>
98        + NodePrimitivesProvider<Primitives = Evm::Primitives>
99        + CanonStateSubscriptions<Primitives = Evm::Primitives>
100        + StageCheckpointReader
101        + PruneCheckpointReader
102        + BalProvider
103        + Unpin
104        + Clone
105        + 'static,
106    Evm: ConfigureEvm + 'static,
107    Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Evm::Primitives>>>
108        + Unpin
109        + 'static,
110    Network: NetworkInfo + Clone + Unpin + 'static,
111{
112    type Primitives = Evm::Primitives;
113    type Provider = Provider;
114    type Pool = Pool;
115    type Evm = Evm;
116    type Network = Network;
117
118    fn pool(&self) -> &Self::Pool {
119        &self.pool
120    }
121
122    fn evm_config(&self) -> &Self::Evm {
123        &self.evm_config
124    }
125
126    fn network(&self) -> &Self::Network {
127        &self.network
128    }
129
130    fn provider(&self) -> &Self::Provider {
131        &self.provider
132    }
133}