Skip to main content

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