1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! Types for the tracing of a single Ethereum transaction.
18
//! Structure from "raw" debug_trace and a "call list" matching
19
//! Blockscout formatter. This "call list" is also used to build
20
//! the whole block tracing output.
21

            
22
use super::serialization::*;
23
use serde::{Deserialize, Serialize};
24

            
25
use ethereum_types::{H160, H256, U256};
26
use parity_scale_codec::{Decode, Encode};
27
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};
28

            
29
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize)]
30
#[serde(rename_all = "camelCase", untagged)]
31
pub enum Call {
32
	Blockscout(crate::formatters::blockscout::BlockscoutCall),
33
	CallTracer(crate::formatters::call_tracer::CallTracerCall),
34
}
35

            
36
#[derive(Clone, Copy, Eq, PartialEq, Debug, Encode, Decode)]
37
pub enum TraceType {
38
	/// Classic geth with no javascript based tracing.
39
	Raw {
40
		disable_storage: bool,
41
		disable_memory: bool,
42
		disable_stack: bool,
43
	},
44
	/// List of calls and subcalls formatted with an input tracer (i.e. callTracer or Blockscout).
45
	CallList,
46
	/// A single block trace. Use in `debug_traceTransactionByNumber` / `traceTransactionByHash`.
47
	Block,
48
}
49

            
50
/// Single transaction trace.
51
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize)]
52
#[serde(rename_all = "camelCase", untagged)]
53
pub enum TransactionTrace {
54
	/// Classical output of `debug_trace`.
55
	#[serde(rename_all = "camelCase")]
56
	Raw {
57
		gas: U256,
58
		#[serde(with = "hex")]
59
		return_value: Vec<u8>,
60
		struct_logs: Vec<RawStepLog>,
61
	},
62
	/// Matches the formatter used by Blockscout.
63
	/// Is also used to built output of OpenEthereum's `trace_filter`.
64
	CallList(Vec<Call>),
65
	/// Used by Geth's callTracer.
66
	CallListNested(Call),
67
}
68

            
69
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize)]
70
#[serde(rename_all = "camelCase")]
71
pub struct RawStepLog {
72
	#[serde(serialize_with = "u256_serialize")]
73
	pub depth: U256,
74

            
75
	//error: TODO
76
	#[serde(serialize_with = "u256_serialize")]
77
	pub gas: U256,
78

            
79
	#[serde(serialize_with = "u256_serialize")]
80
	pub gas_cost: U256,
81

            
82
	#[serde(
83
		serialize_with = "seq_h256_serialize",
84
		skip_serializing_if = "Option::is_none"
85
	)]
86
	pub memory: Option<Vec<H256>>,
87

            
88
	#[serde(serialize_with = "opcode_serialize")]
89
	pub op: Vec<u8>,
90

            
91
	#[serde(serialize_with = "u256_serialize")]
92
	pub pc: U256,
93

            
94
	#[serde(
95
		serialize_with = "seq_h256_serialize",
96
		skip_serializing_if = "Option::is_none"
97
	)]
98
	pub stack: Option<Vec<H256>>,
99

            
100
	#[serde(skip_serializing_if = "Option::is_none")]
101
	pub storage: Option<BTreeMap<H256, H256>>,
102
}
103

            
104
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize, Deserialize)]
105
#[serde(rename_all = "camelCase")]
106
pub struct TraceCallConfig {
107
	pub with_log: bool,
108
}
109

            
110
impl Default for TraceCallConfig {
111
	fn default() -> Self {
112
		Self { with_log: false }
113
	}
114
}
115

            
116
#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, Serialize)]
117
pub struct Log {
118
	/// Event address.
119
	pub address: H160,
120
	/// Event topics
121
	pub topics: Vec<H256>,
122
	/// Event data
123
	#[serde(serialize_with = "bytes_0x_serialize")]
124
	pub data: Vec<u8>,
125
}