referrerpolicy=no-referrer-when-downgrade

pallet_revive_eth_rpc/apis/
debug_apis.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17use crate::*;
18use jsonrpsee::{core::RpcResult, proc_macros::rpc};
19
20/// Debug Ethererum JSON-RPC apis.
21#[rpc(server, client)]
22pub trait DebugRpc {
23	/// Returns the tracing of the execution of a specific block using its number.
24	///
25	/// ## References
26	///
27	/// - <https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugtraceblockbynumber>
28	#[method(name = "debug_traceBlockByNumber")]
29	async fn trace_block_by_number(
30		&self,
31		block: BlockNumberOrTag,
32		tracer_config: Option<TracerConfig>,
33	) -> RpcResult<Vec<TransactionTrace>>;
34
35	/// Returns a transaction's traces by replaying it.
36	///
37	/// ## References
38	///
39	/// - <https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugtracetransaction>
40	#[method(name = "debug_traceTransaction")]
41	async fn trace_transaction(
42		&self,
43		transaction_hash: H256,
44		tracer_config: Option<TracerConfig>,
45	) -> RpcResult<Trace>;
46
47	/// Dry run a call and returns the transaction's traces.
48	///
49	/// Accepts an optional [`TraceCallConfig`] that extends the base tracer config with state
50	/// overrides, matching the [Geth specification](https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugtracecall).
51	///
52	/// ## References
53	///
54	/// - <https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugtracecall>
55	#[method(name = "debug_traceCall")]
56	async fn trace_call(
57		&self,
58		transaction: GenericTransaction,
59		block: BlockNumberOrTagOrHash,
60		trace_call_config: Option<TraceCallConfig>,
61	) -> RpcResult<Trace>;
62
63	#[method(name = "debug_getAutomine")]
64	async fn get_automine(&self) -> RpcResult<bool>;
65}
66
67pub struct DebugRpcServerImpl {
68	client: client::Client,
69}
70
71impl DebugRpcServerImpl {
72	pub fn new(client: client::Client) -> Self {
73		Self { client }
74	}
75}
76
77async fn with_timeout<T>(
78	timeout: Option<core::time::Duration>,
79	fut: impl std::future::Future<Output = Result<T, ClientError>>,
80) -> RpcResult<T> {
81	if let Some(timeout) = timeout {
82		match tokio::time::timeout(timeout, fut).await {
83			Ok(r) => Ok(r?),
84			Err(_) => Err(ErrorObjectOwned::owned::<String>(
85				-32000,
86				"execution timeout".to_string(),
87				None,
88			)),
89		}
90	} else {
91		Ok(fut.await?)
92	}
93}
94
95#[async_trait]
96impl DebugRpcServer for DebugRpcServerImpl {
97	async fn trace_block_by_number(
98		&self,
99		block: BlockNumberOrTag,
100		tracer_config: Option<TracerConfig>,
101	) -> RpcResult<Vec<TransactionTrace>> {
102		let TracerConfig { config, timeout } = tracer_config.unwrap_or_default();
103		with_timeout(timeout, self.client.trace_block_by_number(block, config)).await
104	}
105
106	async fn trace_transaction(
107		&self,
108		transaction_hash: H256,
109		tracer_config: Option<TracerConfig>,
110	) -> RpcResult<Trace> {
111		let TracerConfig { config, timeout } = tracer_config.unwrap_or_default();
112		with_timeout(timeout, self.client.trace_transaction(transaction_hash, config)).await
113	}
114
115	async fn trace_call(
116		&self,
117		transaction: GenericTransaction,
118		block: BlockNumberOrTagOrHash,
119		trace_call_config: Option<TraceCallConfig>,
120	) -> RpcResult<Trace> {
121		let TraceCallConfig { tracer_config, state_overrides } =
122			trace_call_config.unwrap_or_default();
123		let TracerConfig { config, timeout } = tracer_config;
124		with_timeout(timeout, self.client.trace_call(transaction, block, config, state_overrides))
125			.await
126	}
127
128	async fn get_automine(&self) -> RpcResult<bool> {
129		sc_service::Result::Ok(self.client.get_automine().await)
130	}
131}