1use alloy_eips::{BlockId, BlockNumberOrTag};
2use alloy_primitives::{Address, Bytes, B256, U256, U64};
3use alloy_rpc_types_eth::{
4 state::StateOverride, BlockOverrides, EIP1186AccountProofResponse, Filter, 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;
11pub use reth_rpc_engine_api::EngineApi;
13use reth_rpc_eth_api::{
14 EngineEthFilter, FullEthApiTypes, QueryLimits, RpcBlock, RpcHeader, RpcLog, RpcReceipt,
15 RpcTransaction,
16};
17use serde_json::Value;
18use tracing_futures::Instrument;
19
20macro_rules! engine_span {
21 () => {
22 tracing::info_span!(target: "rpc", "engine")
23 };
24}
25
26#[derive(Debug, Clone)]
29pub struct EngineEthApi<Eth, EthFilter> {
30 eth: Eth,
31 eth_filter: EthFilter,
32}
33
34impl<Eth, EthFilter> EngineEthApi<Eth, EthFilter> {
35 pub const fn new(eth: Eth, eth_filter: EthFilter) -> Self {
37 Self { eth, eth_filter }
38 }
39}
40
41#[async_trait::async_trait]
42impl<Eth, EthFilter>
43 EngineEthApiServer<
44 RpcTxReq<Eth::NetworkTypes>,
45 RpcBlock<Eth::NetworkTypes>,
46 RpcReceipt<Eth::NetworkTypes>,
47 RpcLog<Eth::NetworkTypes>,
48 > for EngineEthApi<Eth, EthFilter>
49where
50 Eth: EthApiServer<
51 RpcTxReq<Eth::NetworkTypes>,
52 RpcTransaction<Eth::NetworkTypes>,
53 RpcBlock<Eth::NetworkTypes>,
54 RpcReceipt<Eth::NetworkTypes>,
55 RpcHeader<Eth::NetworkTypes>,
56 TxTy<Eth::Primitives>,
57 > + FullEthApiTypes,
58 EthFilter: EngineEthFilter<RpcLog<Eth::NetworkTypes>>,
59{
60 fn syncing(&self) -> Result<SyncStatus> {
62 let span = engine_span!();
63 let _enter = span.enter();
64 self.eth.syncing()
65 }
66
67 async fn chain_id(&self) -> Result<Option<U64>> {
69 let span = engine_span!();
70 let _enter = span.enter();
71 self.eth.chain_id().await
72 }
73
74 fn block_number(&self) -> Result<U256> {
76 let span = engine_span!();
77 let _enter = span.enter();
78 self.eth.block_number()
79 }
80
81 async fn call(
83 &self,
84 request: RpcTxReq<Eth::NetworkTypes>,
85 block_id: Option<BlockId>,
86 state_overrides: Option<StateOverride>,
87 block_overrides: Option<Box<BlockOverrides>>,
88 ) -> Result<Bytes> {
89 self.eth
90 .call(request, block_id, state_overrides, block_overrides)
91 .instrument(engine_span!())
92 .await
93 }
94
95 async fn get_code(&self, address: Address, block_id: Option<BlockId>) -> Result<Bytes> {
97 self.eth.get_code(address, block_id).instrument(engine_span!()).await
98 }
99
100 async fn block_by_hash(
102 &self,
103 hash: B256,
104 full: bool,
105 ) -> Result<Option<RpcBlock<Eth::NetworkTypes>>> {
106 self.eth.block_by_hash(hash, full).instrument(engine_span!()).await
107 }
108
109 async fn block_by_number(
111 &self,
112 number: BlockNumberOrTag,
113 full: bool,
114 ) -> Result<Option<RpcBlock<Eth::NetworkTypes>>> {
115 self.eth.block_by_number(number, full).instrument(engine_span!()).await
116 }
117
118 async fn block_receipts(
119 &self,
120 block_id: BlockId,
121 ) -> Result<Option<Vec<RpcReceipt<Eth::NetworkTypes>>>> {
122 self.eth.block_receipts(block_id).instrument(engine_span!()).await
123 }
124
125 async fn send_raw_transaction(&self, bytes: Bytes) -> Result<B256> {
127 self.eth.send_raw_transaction(bytes).instrument(engine_span!()).await
128 }
129
130 async fn transaction_receipt(
131 &self,
132 hash: B256,
133 ) -> Result<Option<RpcReceipt<Eth::NetworkTypes>>> {
134 self.eth.transaction_receipt(hash).instrument(engine_span!()).await
135 }
136
137 async fn logs(&self, filter: Filter) -> Result<Vec<RpcLog<Eth::NetworkTypes>>> {
139 self.eth_filter.logs(filter, QueryLimits::no_limits()).instrument(engine_span!()).await
140 }
141
142 async fn get_proof(
144 &self,
145 address: Address,
146 keys: Vec<JsonStorageKey>,
147 block_number: Option<BlockId>,
148 ) -> Result<EIP1186AccountProofResponse> {
149 self.eth.get_proof(address, keys, block_number).instrument(engine_span!()).await
150 }
151
152 async fn get_multi_proof(
154 &self,
155 targets: Vec<(Address, Vec<B256>)>,
156 block_number: Option<BlockId>,
157 ) -> Result<Vec<EIP1186AccountProofResponse>> {
158 self.eth.get_multi_proof(targets, block_number).instrument(engine_span!()).await
159 }
160
161 async fn block_access_list_by_block_hash(&self, hash: B256) -> Result<Option<Value>> {
163 self.eth.block_access_list_by_block_hash(hash).instrument(engine_span!()).await
164 }
165
166 async fn block_access_list_by_block_number(
168 &self,
169 block_number: BlockNumberOrTag,
170 ) -> Result<Option<Value>> {
171 self.eth.block_access_list_by_block_number(block_number).instrument(engine_span!()).await
172 }
173
174 async fn block_access_list(&self, block_id: BlockId) -> Result<Option<Value>> {
176 self.eth.block_access_list(block_id).instrument(engine_span!()).await
177 }
178
179 async fn block_access_list_raw(&self, block: BlockId) -> Result<Option<Bytes>> {
181 self.eth.block_access_list_raw(block).instrument(engine_span!()).await
182 }
183}