reth_rpc_api/engine.rs
1//! Server traits for the engine API
2//!
3//! This contains the `engine_` namespace and the subset of the `eth_` namespace that is exposed to
4//! the consensus client.
5
6use alloy_eips::{
7 eip4844::{BlobAndProofV1, BlobAndProofV2},
8 eip7685::RequestsOrHash,
9 BlockId, BlockNumberOrTag,
10};
11use alloy_json_rpc::RpcObject;
12use alloy_primitives::{Address, BlockHash, Bytes, B256, U256, U64};
13use alloy_rpc_types_engine::{
14 ClientVersionV1, ExecutionPayloadBodiesV1, ExecutionPayloadInputV2, ExecutionPayloadV1,
15 ExecutionPayloadV3, ForkchoiceState, ForkchoiceUpdated, PayloadId, PayloadStatus,
16};
17use alloy_rpc_types_eth::{
18 state::StateOverride, transaction::TransactionRequest, BlockOverrides,
19 EIP1186AccountProofResponse, Filter, Log, SyncStatus,
20};
21use alloy_serde::JsonStorageKey;
22use jsonrpsee::{core::RpcResult, proc_macros::rpc, RpcModule};
23use reth_engine_primitives::EngineTypes;
24
25/// Helper trait for the engine api server.
26///
27/// This type-erases the concrete [`jsonrpsee`] server implementation and only returns the
28/// [`RpcModule`] that contains all the endpoints of the server.
29pub trait IntoEngineApiRpcModule {
30 /// Consumes the type and returns all the methods and subscriptions defined in the trait and
31 /// returns them as a single [`RpcModule`]
32 fn into_rpc_module(self) -> RpcModule<()>;
33}
34
35// NOTE: We can't use associated types in the `EngineApi` trait because of jsonrpsee, so we use a
36// generic here. It would be nice if the rpc macro would understand which types need to have serde.
37// By default, if the trait has a generic, the rpc macro will add e.g. `Engine: DeserializeOwned` to
38// the trait bounds, which is not what we want, because `Types` is not used directly in any of the
39// trait methods. Instead, we have to add the bounds manually. This would be disastrous if we had
40// more than one associated type used in the trait methods.
41
42#[cfg_attr(not(feature = "client"), rpc(server, namespace = "engine"), server_bounds(Engine::PayloadAttributes: jsonrpsee::core::DeserializeOwned))]
43#[cfg_attr(feature = "client", rpc(server, client, namespace = "engine", client_bounds(Engine::PayloadAttributes: jsonrpsee::core::Serialize + Clone), server_bounds(Engine::PayloadAttributes: jsonrpsee::core::DeserializeOwned)))]
44pub trait EngineApi<Engine: EngineTypes> {
45 /// See also <https://github.com/ethereum/execution-apis/blob/6709c2a795b707202e93c4f2867fa0bf2640a84f/src/engine/paris.md#engine_newpayloadv1>
46 /// Caution: This should not accept the `withdrawals` field
47 #[method(name = "newPayloadV1")]
48 async fn new_payload_v1(&self, payload: ExecutionPayloadV1) -> RpcResult<PayloadStatus>;
49
50 /// See also <https://github.com/ethereum/execution-apis/blob/584905270d8ad665718058060267061ecfd79ca5/src/engine/shanghai.md#engine_newpayloadv2>
51 #[method(name = "newPayloadV2")]
52 async fn new_payload_v2(&self, payload: ExecutionPayloadInputV2) -> RpcResult<PayloadStatus>;
53
54 /// Post Cancun payload handler
55 ///
56 /// See also <https://github.com/ethereum/execution-apis/blob/main/src/engine/cancun.md#engine_newpayloadv3>
57 #[method(name = "newPayloadV3")]
58 async fn new_payload_v3(
59 &self,
60 payload: ExecutionPayloadV3,
61 versioned_hashes: Vec<B256>,
62 parent_beacon_block_root: B256,
63 ) -> RpcResult<PayloadStatus>;
64
65 /// Post Prague payload handler
66 ///
67 /// See also <https://github.com/ethereum/execution-apis/blob/main/src/engine/prague.md#engine_newpayloadv4>
68 #[method(name = "newPayloadV4")]
69 async fn new_payload_v4(
70 &self,
71 payload: ExecutionPayloadV3,
72 versioned_hashes: Vec<B256>,
73 parent_beacon_block_root: B256,
74 execution_requests: RequestsOrHash,
75 ) -> RpcResult<PayloadStatus>;
76
77 /// See also <https://github.com/ethereum/execution-apis/blob/6709c2a795b707202e93c4f2867fa0bf2640a84f/src/engine/paris.md#engine_forkchoiceupdatedv1>
78 ///
79 /// Caution: This should not accept the `withdrawals` field in the payload attributes.
80 #[method(name = "forkchoiceUpdatedV1")]
81 async fn fork_choice_updated_v1(
82 &self,
83 fork_choice_state: ForkchoiceState,
84 payload_attributes: Option<Engine::PayloadAttributes>,
85 ) -> RpcResult<ForkchoiceUpdated>;
86
87 /// Post Shanghai forkchoice update handler
88 ///
89 /// This is the same as `forkchoiceUpdatedV1`, but expects an additional `withdrawals` field in
90 /// the `payloadAttributes`, if payload attributes are provided.
91 ///
92 /// See also <https://github.com/ethereum/execution-apis/blob/6709c2a795b707202e93c4f2867fa0bf2640a84f/src/engine/shanghai.md#engine_forkchoiceupdatedv2>
93 ///
94 /// Caution: This should not accept the `parentBeaconBlockRoot` field in the payload
95 /// attributes.
96 #[method(name = "forkchoiceUpdatedV2")]
97 async fn fork_choice_updated_v2(
98 &self,
99 fork_choice_state: ForkchoiceState,
100 payload_attributes: Option<Engine::PayloadAttributes>,
101 ) -> RpcResult<ForkchoiceUpdated>;
102
103 /// Post Cancun forkchoice update handler
104 ///
105 /// This is the same as `forkchoiceUpdatedV2`, but expects an additional
106 /// `parentBeaconBlockRoot` field in the `payloadAttributes`, if payload attributes
107 /// are provided.
108 ///
109 /// See also <https://github.com/ethereum/execution-apis/blob/main/src/engine/cancun.md#engine_forkchoiceupdatedv3>
110 #[method(name = "forkchoiceUpdatedV3")]
111 async fn fork_choice_updated_v3(
112 &self,
113 fork_choice_state: ForkchoiceState,
114 payload_attributes: Option<Engine::PayloadAttributes>,
115 ) -> RpcResult<ForkchoiceUpdated>;
116
117 /// See also <https://github.com/ethereum/execution-apis/blob/6709c2a795b707202e93c4f2867fa0bf2640a84f/src/engine/paris.md#engine_getpayloadv1>
118 ///
119 /// Returns the most recent version of the payload that is available in the corresponding
120 /// payload build process at the time of receiving this call.
121 ///
122 /// Caution: This should not return the `withdrawals` field
123 ///
124 /// Note:
125 /// > Provider software MAY stop the corresponding build process after serving this call.
126 #[method(name = "getPayloadV1")]
127 async fn get_payload_v1(
128 &self,
129 payload_id: PayloadId,
130 ) -> RpcResult<Engine::ExecutionPayloadEnvelopeV1>;
131
132 /// See also <https://github.com/ethereum/execution-apis/blob/6709c2a795b707202e93c4f2867fa0bf2640a84f/src/engine/shanghai.md#engine_getpayloadv2>
133 ///
134 /// Returns the most recent version of the payload that is available in the corresponding
135 /// payload build process at the time of receiving this call. Note:
136 /// > Provider software MAY stop the corresponding build process after serving this call.
137 #[method(name = "getPayloadV2")]
138 async fn get_payload_v2(
139 &self,
140 payload_id: PayloadId,
141 ) -> RpcResult<Engine::ExecutionPayloadEnvelopeV2>;
142
143 /// Post Cancun payload handler which also returns a blobs bundle.
144 ///
145 /// See also <https://github.com/ethereum/execution-apis/blob/main/src/engine/cancun.md#engine_getpayloadv3>
146 ///
147 /// Returns the most recent version of the payload that is available in the corresponding
148 /// payload build process at the time of receiving this call. Note:
149 /// > Provider software MAY stop the corresponding build process after serving this call.
150 #[method(name = "getPayloadV3")]
151 async fn get_payload_v3(
152 &self,
153 payload_id: PayloadId,
154 ) -> RpcResult<Engine::ExecutionPayloadEnvelopeV3>;
155
156 /// Post Prague payload handler.
157 ///
158 /// See also <https://github.com/ethereum/execution-apis/blob/main/src/engine/prague.md#engine_getpayloadv4>
159 ///
160 /// Returns the most recent version of the payload that is available in the corresponding
161 /// payload build process at the time of receiving this call. Note:
162 /// > Provider software MAY stop the corresponding build process after serving this call.
163 #[method(name = "getPayloadV4")]
164 async fn get_payload_v4(
165 &self,
166 payload_id: PayloadId,
167 ) -> RpcResult<Engine::ExecutionPayloadEnvelopeV4>;
168
169 /// See also <https://github.com/ethereum/execution-apis/blob/6452a6b194d7db269bf1dbd087a267251d3cc7f8/src/engine/shanghai.md#engine_getpayloadbodiesbyhashv1>
170 #[method(name = "getPayloadBodiesByHashV1")]
171 async fn get_payload_bodies_by_hash_v1(
172 &self,
173 block_hashes: Vec<BlockHash>,
174 ) -> RpcResult<ExecutionPayloadBodiesV1>;
175
176 /// See also <https://github.com/ethereum/execution-apis/blob/6452a6b194d7db269bf1dbd087a267251d3cc7f8/src/engine/shanghai.md#engine_getpayloadbodiesbyrangev1>
177 ///
178 /// Returns the execution payload bodies by the range starting at `start`, containing `count`
179 /// blocks.
180 ///
181 /// WARNING: This method is associated with the BeaconBlocksByRange message in the consensus
182 /// layer p2p specification, meaning the input should be treated as untrusted or potentially
183 /// adversarial.
184 ///
185 /// Implementers should take care when acting on the input to this method, specifically
186 /// ensuring that the range is limited properly, and that the range boundaries are computed
187 /// correctly and without panics.
188 #[method(name = "getPayloadBodiesByRangeV1")]
189 async fn get_payload_bodies_by_range_v1(
190 &self,
191 start: U64,
192 count: U64,
193 ) -> RpcResult<ExecutionPayloadBodiesV1>;
194
195 /// This function will return the ClientVersionV1 object.
196 /// See also:
197 /// <https://github.com/ethereum/execution-apis/blob/03911ffc053b8b806123f1fc237184b0092a485a/src/engine/identification.md#engine_getclientversionv1>make fmt
198 ///
199 ///
200 /// - When connected to a single execution client, the consensus client **MUST** receive an
201 /// array with a single `ClientVersionV1` object.
202 /// - When connected to multiple execution clients via a multiplexer, the multiplexer **MUST**
203 /// concatenate the responses from each execution client into a single,
204 /// flat array before returning the response to the consensus client.
205 #[method(name = "getClientVersionV1")]
206 async fn get_client_version_v1(
207 &self,
208 client_version: ClientVersionV1,
209 ) -> RpcResult<Vec<ClientVersionV1>>;
210
211 /// See also <https://github.com/ethereum/execution-apis/blob/6452a6b194d7db269bf1dbd087a267251d3cc7f8/src/engine/common.md#capabilities>
212 #[method(name = "exchangeCapabilities")]
213 async fn exchange_capabilities(&self, capabilities: Vec<String>) -> RpcResult<Vec<String>>;
214
215 /// Fetch blobs for the consensus layer from the in-memory blob cache.
216 #[method(name = "getBlobsV1")]
217 async fn get_blobs_v1(
218 &self,
219 versioned_hashes: Vec<B256>,
220 ) -> RpcResult<Vec<Option<BlobAndProofV1>>>;
221
222 /// Fetch blobs for the consensus layer from the blob store.
223 #[method(name = "getBlobsV2")]
224 async fn get_blobs_v2(
225 &self,
226 versioned_hashes: Vec<B256>,
227 ) -> RpcResult<Vec<Option<BlobAndProofV2>>>;
228}
229
230/// A subset of the ETH rpc interface: <https://ethereum.github.io/execution-apis/api-documentation/>
231///
232/// This also includes additional eth functions required by optimism.
233///
234/// Specifically for the engine auth server: <https://github.com/ethereum/execution-apis/blob/main/src/engine/common.md#underlying-protocol>
235#[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))]
236#[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))]
237pub trait EngineEthApi<B: RpcObject, R: RpcObject> {
238 /// Returns an object with data about the sync status or false.
239 #[method(name = "syncing")]
240 fn syncing(&self) -> RpcResult<SyncStatus>;
241
242 /// Returns the chain ID of the current network.
243 #[method(name = "chainId")]
244 async fn chain_id(&self) -> RpcResult<Option<U64>>;
245
246 /// Returns the number of most recent block.
247 #[method(name = "blockNumber")]
248 fn block_number(&self) -> RpcResult<U256>;
249
250 /// Executes a new message call immediately without creating a transaction on the block chain.
251 #[method(name = "call")]
252 async fn call(
253 &self,
254 request: TransactionRequest,
255 block_id: Option<BlockId>,
256 state_overrides: Option<StateOverride>,
257 block_overrides: Option<Box<BlockOverrides>>,
258 ) -> RpcResult<Bytes>;
259
260 /// Returns code at a given address at given block number.
261 #[method(name = "getCode")]
262 async fn get_code(&self, address: Address, block_id: Option<BlockId>) -> RpcResult<Bytes>;
263
264 /// Returns information about a block by hash.
265 #[method(name = "getBlockByHash")]
266 async fn block_by_hash(&self, hash: B256, full: bool) -> RpcResult<Option<B>>;
267
268 /// Returns information about a block by number.
269 #[method(name = "getBlockByNumber")]
270 async fn block_by_number(&self, number: BlockNumberOrTag, full: bool) -> RpcResult<Option<B>>;
271
272 /// Returns all transaction receipts for a given block.
273 #[method(name = "getBlockReceipts")]
274 async fn block_receipts(&self, block_id: BlockId) -> RpcResult<Option<Vec<R>>>;
275
276 /// Sends signed transaction, returning its hash.
277 #[method(name = "sendRawTransaction")]
278 async fn send_raw_transaction(&self, bytes: Bytes) -> RpcResult<B256>;
279
280 /// Returns the receipt of a transaction by transaction hash.
281 #[method(name = "getTransactionReceipt")]
282 async fn transaction_receipt(&self, hash: B256) -> RpcResult<Option<R>>;
283
284 /// Returns logs matching given filter object.
285 #[method(name = "getLogs")]
286 async fn logs(&self, filter: Filter) -> RpcResult<Vec<Log>>;
287
288 /// Returns the account and storage values of the specified account including the Merkle-proof.
289 /// This call can be used to verify that the data you are pulling from is not tampered with.
290 #[method(name = "getProof")]
291 async fn get_proof(
292 &self,
293 address: Address,
294 keys: Vec<JsonStorageKey>,
295 block_number: Option<BlockId>,
296 ) -> RpcResult<EIP1186AccountProofResponse>;
297}