1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2019-2025 PureStake Inc.
// This file is part of Moonbeam.

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

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

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

use super::blockscout::BlockscoutCallInner;
use crate::types::{
	single::{Call, Log, TransactionTrace},
	CallResult, CallType, CreateResult,
};

use crate::listeners::call_list::Listener;

use crate::types::serialization::*;
use serde::Serialize;

use crate::types::block::BlockTransactionTrace;
use ethereum_types::{H160, U256};
use parity_scale_codec::{Decode, Encode};
use sp_std::{cmp::Ordering, vec::Vec};

pub struct Formatter;

impl super::ResponseFormatter for Formatter {
	type Listener = Listener;
	type Response = Vec<BlockTransactionTrace>;

	fn format(listener: Listener) -> Option<Vec<BlockTransactionTrace>> {
		let mut traces = Vec::new();
		for (eth_tx_index, entry) in listener.entries.iter().enumerate() {
			// Skip empty BTreeMaps pushed to `entries`.
			// I.e. InvalidNonce or other pallet_evm::runner exits
			if entry.is_empty() {
				log::debug!(
					target: "tracing",
					"Empty trace entry with transaction index {}, skipping...", eth_tx_index
				);
				continue;
			}
			let mut result: Vec<Call> = entry
				.into_iter()
				.map(|(_, it)| {
					let from = it.from;
					let trace_address = it.trace_address.clone();
					let value = it.value;
					let gas = it.gas;
					let gas_used = it.gas_used;
					let inner = it.inner.clone();
					Call::CallTracer(CallTracerCall {
						from: from,
						gas: gas,
						gas_used: gas_used,
						trace_address: Some(trace_address.clone()),
						inner: match inner.clone() {
							BlockscoutCallInner::Call {
								input,
								to,
								res,
								call_type,
							} => CallTracerInner::Call {
								call_type: match call_type {
									CallType::Call => "CALL".as_bytes().to_vec(),
									CallType::CallCode => "CALLCODE".as_bytes().to_vec(),
									CallType::DelegateCall => "DELEGATECALL".as_bytes().to_vec(),
									CallType::StaticCall => "STATICCALL".as_bytes().to_vec(),
								},
								to,
								input,
								res: res.clone(),
								value: Some(value),
								logs: match res {
									CallResult::Output { .. } => it.logs.clone(),
									CallResult::Error { .. } => Vec::new(),
								},
							},
							BlockscoutCallInner::Create { init, res } => CallTracerInner::Create {
								input: init,
								error: match res {
									CreateResult::Success { .. } => None,
									CreateResult::Error { ref error } => Some(error.clone()),
								},
								to: match res {
									CreateResult::Success {
										created_contract_address_hash,
										..
									} => Some(created_contract_address_hash),
									CreateResult::Error { .. } => None,
								},
								output: match res {
									CreateResult::Success {
										created_contract_code,
										..
									} => Some(created_contract_code),
									CreateResult::Error { .. } => None,
								},
								value: value,
								call_type: "CREATE".as_bytes().to_vec(),
							},
							BlockscoutCallInner::SelfDestruct { balance, to } => {
								CallTracerInner::SelfDestruct {
									value: balance,
									to,
									call_type: "SELFDESTRUCT".as_bytes().to_vec(),
								}
							}
						},
						calls: Vec::new(),
					})
				})
				.collect();
			// Geth's `callTracer` expects a tree of nested calls and we have a stack.
			//
			// We iterate over the sorted stack, and push each children to it's
			// parent (the item which's `trace_address` matches &T[0..T.len()-1]) until there
			// is a single item on the list.
			//
			// The last remaining item is the context call with all it's descendants. I.e.
			//
			// 		# Input
			// 		[]
			// 		[0]
			// 		[0,0]
			// 		[0,0,0]
			// 		[0,1]
			// 		[0,1,0]
			// 		[0,1,1]
			// 		[0,1,2]
			// 		[1]
			// 		[1,0]
			//
			// 		# Sorted
			// 		[0,0,0] -> pop 0 and push to [0,0]
			// 		[0,1,0] -> pop 0 and push to [0,1]
			// 		[0,1,1] -> pop 1 and push to [0,1]
			// 		[0,1,2] -> pop 2 and push to [0,1]
			// 		[0,0] -> pop 0 and push to [0]
			// 		[0,1] -> pop 1 and push to [0]
			// 		[1,0] -> pop 0 and push to [1]
			// 		[0] -> pop 0 and push to root
			// 		[1] -> pop 1 and push to root
			// 		[]
			//
			// 		# Result
			// 		root {
			// 			calls: {
			// 				0 { 0 { 0 }, 1 { 0, 1, 2 }},
			// 				1 { 0 },
			// 			}
			// 		}
			if result.len() > 1 {
				// Sort the stack. Assume there is no `Ordering::Equal`, as we are
				// sorting by index.
				//
				// We consider an item to be `Ordering::Less` when:
				// 	- Is closer to the root or
				//	- Is greater than its sibling.
				result.sort_by(|a, b| match (a, b) {
					(
						Call::CallTracer(CallTracerCall {
							trace_address: Some(a),
							..
						}),
						Call::CallTracer(CallTracerCall {
							trace_address: Some(b),
							..
						}),
					) => {
						let a_len = a.len();
						let b_len = b.len();
						let sibling_greater_than = |a: &Vec<u32>, b: &Vec<u32>| -> bool {
							for (i, a_value) in a.iter().enumerate() {
								if a_value > &b[i] {
									return true;
								} else if a_value < &b[i] {
									return false;
								} else {
									continue;
								}
							}
							return false;
						};
						if b_len > a_len || (a_len == b_len && sibling_greater_than(&a, &b)) {
							Ordering::Less
						} else {
							Ordering::Greater
						}
					}
					_ => unreachable!(),
				});
				// Stack pop-and-push.
				while result.len() > 1 {
					let mut last = result
						.pop()
						.expect("result.len() > 1, so pop() necessarily returns an element");
					// Find the parent index.
					if let Some(index) =
						result
							.iter()
							.position(|current| match (last.clone(), current) {
								(
									Call::CallTracer(CallTracerCall {
										trace_address: Some(a),
										..
									}),
									Call::CallTracer(CallTracerCall {
										trace_address: Some(b),
										..
									}),
								) => {
									&b[..]
										== a.get(0..a.len() - 1).expect(
											"non-root element while traversing trace result",
										)
								}
								_ => unreachable!(),
							}) {
						// Remove `trace_address` from result.
						if let Call::CallTracer(CallTracerCall {
							ref mut trace_address,
							..
						}) = last
						{
							*trace_address = None;
						}
						// Push the children to parent.
						if let Some(Call::CallTracer(CallTracerCall { calls, .. })) =
							result.get_mut(index)
						{
							calls.push(last);
						}
					}
				}
			}
			// Remove `trace_address` from result.
			if let Some(Call::CallTracer(CallTracerCall { trace_address, .. })) = result.get_mut(0)
			{
				*trace_address = None;
			}
			if result.len() == 1 {
				traces.push(BlockTransactionTrace {
					tx_position: eth_tx_index as u32,
					// Use default, the correct value will be set upstream
					tx_hash: Default::default(),
					result: TransactionTrace::CallListNested(
						result
							.pop()
							.expect("result.len() == 1, so pop() necessarily returns this element"),
					),
				});
			}
		}
		if traces.is_empty() {
			return None;
		}
		return Some(traces);
	}
}

#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CallTracerCall {
	pub from: H160,

	/// Indices of parent calls. Used to build the Etherscan nested response.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub trace_address: Option<Vec<u32>>,

	/// Remaining gas in the runtime.
	pub gas: U256,
	/// Gas used by this context.
	pub gas_used: U256,

	#[serde(flatten)]
	pub inner: CallTracerInner,

	#[serde(skip_serializing_if = "Vec::is_empty")]
	pub calls: Vec<Call>,
}

#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize)]
#[serde(untagged)]
pub enum CallTracerInner {
	Call {
		#[serde(rename = "type", serialize_with = "opcode_serialize")]
		call_type: Vec<u8>,
		to: H160,
		#[serde(serialize_with = "bytes_0x_serialize")]
		input: Vec<u8>,
		/// "output" or "error" field
		#[serde(flatten)]
		res: CallResult,

		#[serde(skip_serializing_if = "Option::is_none")]
		value: Option<U256>,

		#[serde(skip_serializing_if = "Vec::is_empty")]
		logs: Vec<Log>,
	},
	Create {
		#[serde(rename = "type", serialize_with = "opcode_serialize")]
		call_type: Vec<u8>,
		#[serde(serialize_with = "bytes_0x_serialize")]
		input: Vec<u8>,
		#[serde(skip_serializing_if = "Option::is_none")]
		to: Option<H160>,
		#[serde(
			skip_serializing_if = "Option::is_none",
			serialize_with = "option_bytes_0x_serialize"
		)]
		output: Option<Vec<u8>>,
		#[serde(
			skip_serializing_if = "Option::is_none",
			serialize_with = "option_string_serialize"
		)]
		error: Option<Vec<u8>>,
		value: U256,
	},
	SelfDestruct {
		#[serde(rename = "type", serialize_with = "opcode_serialize")]
		call_type: Vec<u8>,
		to: H160,
		value: U256,
	},
}