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: 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: TracerConfig,
45	) -> RpcResult<Trace>;
46
47	/// Dry run a call and returns the transaction's traces.
48	///
49	/// ## References
50	///
51	/// - <https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugtracecall>
52	#[method(name = "debug_traceCall")]
53	async fn trace_call(
54		&self,
55		transaction: GenericTransaction,
56		block: BlockNumberOrTagOrHash,
57		tracer_config: TracerConfig,
58	) -> RpcResult<Trace>;
59}
60
61pub struct DebugRpcServerImpl {
62	client: client::Client,
63}
64
65impl DebugRpcServerImpl {
66	pub fn new(client: client::Client) -> Self {
67		Self { client }
68	}
69}
70
71async fn with_timeout<T>(
72	timeout: Option<core::time::Duration>,
73	fut: impl std::future::Future<Output = Result<T, ClientError>>,
74) -> RpcResult<T> {
75	if let Some(timeout) = timeout {
76		match tokio::time::timeout(timeout, fut).await {
77			Ok(r) => Ok(r?),
78			Err(_) => Err(ErrorObjectOwned::owned::<String>(
79				-32000,
80				"execution timeout".to_string(),
81				None,
82			)),
83		}
84	} else {
85		Ok(fut.await?)
86	}
87}
88
89#[async_trait]
90impl DebugRpcServer for DebugRpcServerImpl {
91	async fn trace_block_by_number(
92		&self,
93		block: BlockNumberOrTag,
94		tracer_config: TracerConfig,
95	) -> RpcResult<Vec<TransactionTrace>> {
96		let TracerConfig { config, timeout } = tracer_config;
97		with_timeout(timeout, self.client.trace_block_by_number(block, config)).await
98	}
99
100	async fn trace_transaction(
101		&self,
102		transaction_hash: H256,
103		tracer_config: TracerConfig,
104	) -> RpcResult<Trace> {
105		let TracerConfig { config, timeout } = tracer_config;
106		with_timeout(timeout, self.client.trace_transaction(transaction_hash, config)).await
107	}
108
109	async fn trace_call(
110		&self,
111		transaction: GenericTransaction,
112		block: BlockNumberOrTagOrHash,
113		tracer_config: TracerConfig,
114	) -> RpcResult<Trace> {
115		let TracerConfig { config, timeout } = tracer_config;
116		with_timeout(timeout, self.client.trace_call(transaction, block, config)).await
117	}
118}