reth_rpc/
engine.rs

1use alloy_eips::{BlockId, BlockNumberOrTag};
2use alloy_primitives::{Address, Bytes, B256, U256, U64};
3use alloy_rpc_types_eth::{
4    state::StateOverride, transaction::TransactionRequest, BlockOverrides,
5    EIP1186AccountProofResponse, Filter, Log, SyncStatus,
6};
7use alloy_serde::JsonStorageKey;
8use jsonrpsee::core::RpcResult as Result;
9use reth_rpc_api::{EngineEthApiServer, EthApiServer, EthFilterApiServer};
10/// Re-export for convenience
11pub use reth_rpc_engine_api::EngineApi;
12use reth_rpc_eth_api::{FullEthApiTypes, RpcBlock, RpcHeader, RpcReceipt, RpcTransaction};
13use tracing_futures::Instrument;
14
15macro_rules! engine_span {
16    () => {
17        tracing::trace_span!(target: "rpc", "engine")
18    };
19}
20
21/// A wrapper type for the `EthApi` and `EthFilter` implementations that only expose the required
22/// subset for the `eth_` namespace used in auth server alongside the `engine_` namespace.
23#[derive(Debug, Clone)]
24pub struct EngineEthApi<Eth, EthFilter> {
25    eth: Eth,
26    eth_filter: EthFilter,
27}
28
29impl<Eth, EthFilter> EngineEthApi<Eth, EthFilter> {
30    /// Create a new `EngineEthApi` instance.
31    pub const fn new(eth: Eth, eth_filter: EthFilter) -> Self {
32        Self { eth, eth_filter }
33    }
34}
35
36#[async_trait::async_trait]
37impl<Eth, EthFilter> EngineEthApiServer<RpcBlock<Eth::NetworkTypes>, RpcReceipt<Eth::NetworkTypes>>
38    for EngineEthApi<Eth, EthFilter>
39where
40    Eth: EthApiServer<
41            RpcTransaction<Eth::NetworkTypes>,
42            RpcBlock<Eth::NetworkTypes>,
43            RpcReceipt<Eth::NetworkTypes>,
44            RpcHeader<Eth::NetworkTypes>,
45        > + FullEthApiTypes,
46    EthFilter: EthFilterApiServer<RpcTransaction<Eth::NetworkTypes>>,
47{
48    /// Handler for: `eth_syncing`
49    fn syncing(&self) -> Result<SyncStatus> {
50        let span = engine_span!();
51        let _enter = span.enter();
52        self.eth.syncing()
53    }
54
55    /// Handler for: `eth_chainId`
56    async fn chain_id(&self) -> Result<Option<U64>> {
57        let span = engine_span!();
58        let _enter = span.enter();
59        self.eth.chain_id().await
60    }
61
62    /// Handler for: `eth_blockNumber`
63    fn block_number(&self) -> Result<U256> {
64        let span = engine_span!();
65        let _enter = span.enter();
66        self.eth.block_number()
67    }
68
69    /// Handler for: `eth_call`
70    async fn call(
71        &self,
72        request: TransactionRequest,
73        block_id: Option<BlockId>,
74        state_overrides: Option<StateOverride>,
75        block_overrides: Option<Box<BlockOverrides>>,
76    ) -> Result<Bytes> {
77        self.eth
78            .call(request, block_id, state_overrides, block_overrides)
79            .instrument(engine_span!())
80            .await
81    }
82
83    /// Handler for: `eth_getCode`
84    async fn get_code(&self, address: Address, block_id: Option<BlockId>) -> Result<Bytes> {
85        self.eth.get_code(address, block_id).instrument(engine_span!()).await
86    }
87
88    /// Handler for: `eth_getBlockByHash`
89    async fn block_by_hash(
90        &self,
91        hash: B256,
92        full: bool,
93    ) -> Result<Option<RpcBlock<Eth::NetworkTypes>>> {
94        self.eth.block_by_hash(hash, full).instrument(engine_span!()).await
95    }
96
97    /// Handler for: `eth_getBlockByNumber`
98    async fn block_by_number(
99        &self,
100        number: BlockNumberOrTag,
101        full: bool,
102    ) -> Result<Option<RpcBlock<Eth::NetworkTypes>>> {
103        self.eth.block_by_number(number, full).instrument(engine_span!()).await
104    }
105
106    async fn block_receipts(
107        &self,
108        block_id: BlockId,
109    ) -> Result<Option<Vec<RpcReceipt<Eth::NetworkTypes>>>> {
110        self.eth.block_receipts(block_id).instrument(engine_span!()).await
111    }
112
113    /// Handler for: `eth_sendRawTransaction`
114    async fn send_raw_transaction(&self, bytes: Bytes) -> Result<B256> {
115        self.eth.send_raw_transaction(bytes).instrument(engine_span!()).await
116    }
117
118    async fn transaction_receipt(
119        &self,
120        hash: B256,
121    ) -> Result<Option<RpcReceipt<Eth::NetworkTypes>>> {
122        self.eth.transaction_receipt(hash).instrument(engine_span!()).await
123    }
124
125    /// Handler for `eth_getLogs`
126    async fn logs(&self, filter: Filter) -> Result<Vec<Log>> {
127        self.eth_filter.logs(filter).instrument(engine_span!()).await
128    }
129
130    /// Handler for `eth_getProof`
131    async fn get_proof(
132        &self,
133        address: Address,
134        keys: Vec<JsonStorageKey>,
135        block_number: Option<BlockId>,
136    ) -> Result<EIP1186AccountProofResponse> {
137        self.eth.get_proof(address, keys, block_number).instrument(engine_span!()).await
138    }
139}