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	#[method(name = "debug_getAutomine")]
61	async fn get_automine(&self) -> RpcResult<bool>;
62}
63
64pub struct DebugRpcServerImpl {
65	client: client::Client,
66}
67
68impl DebugRpcServerImpl {
69	pub fn new(client: client::Client) -> Self {
70		Self { client }
71	}
72}
73
74async fn with_timeout<T>(
75	timeout: Option<core::time::Duration>,
76	fut: impl std::future::Future<Output = Result<T, ClientError>>,
77) -> RpcResult<T> {
78	if let Some(timeout) = timeout {
79		match tokio::time::timeout(timeout, fut).await {
80			Ok(r) => Ok(r?),
81			Err(_) => Err(ErrorObjectOwned::owned::<String>(
82				-32000,
83				"execution timeout".to_string(),
84				None,
85			)),
86		}
87	} else {
88		Ok(fut.await?)
89	}
90}
91
92#[async_trait]
93impl DebugRpcServer for DebugRpcServerImpl {
94	async fn trace_block_by_number(
95		&self,
96		block: BlockNumberOrTag,
97		tracer_config: TracerConfig,
98	) -> RpcResult<Vec<TransactionTrace>> {
99		let TracerConfig { config, timeout } = tracer_config;
100		with_timeout(timeout, self.client.trace_block_by_number(block, config)).await
101	}
102
103	async fn trace_transaction(
104		&self,
105		transaction_hash: H256,
106		tracer_config: TracerConfig,
107	) -> RpcResult<Trace> {
108		let TracerConfig { config, timeout } = tracer_config;
109		with_timeout(timeout, self.client.trace_transaction(transaction_hash, config)).await
110	}
111
112	async fn trace_call(
113		&self,
114		transaction: GenericTransaction,
115		block: BlockNumberOrTagOrHash,
116		tracer_config: TracerConfig,
117	) -> RpcResult<Trace> {
118		let TracerConfig { config, timeout } = tracer_config;
119		with_timeout(timeout, self.client.trace_call(transaction, block, config)).await
120	}
121
122	async fn get_automine(&self) -> RpcResult<bool> {
123		sc_service::Result::Ok(self.client.get_automine().await)
124	}
125}