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
//! Runtime API allowing to debug/trace Ethereum
18

            
19
extern crate alloc;
20

            
21
use ethereum_types::{H160, H256};
22
use parity_scale_codec::{Decode, Encode};
23
use sp_std::vec::Vec;
24

            
25
pub mod block;
26
pub mod serialization;
27
pub mod single;
28

            
29
use serde::Serialize;
30
use serialization::*;
31

            
32
pub const MANUAL_BLOCK_INITIALIZATION_RUNTIME_VERSION: u32 = 159;
33

            
34
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize)]
35
#[serde(rename_all = "camelCase")]
36
pub enum CallResult {
37
	Output(#[serde(serialize_with = "bytes_0x_serialize")] Vec<u8>),
38
	// field "error"
39
	Error(#[serde(serialize_with = "string_serialize")] Vec<u8>),
40
}
41

            
42
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize)]
43
#[serde(rename_all = "camelCase", untagged)]
44
pub enum CreateResult {
45
	Error {
46
		#[serde(serialize_with = "string_serialize")]
47
		error: Vec<u8>,
48
	},
49
	Success {
50
		#[serde(rename = "createdContractAddressHash")]
51
		created_contract_address_hash: H160,
52
		#[serde(serialize_with = "bytes_0x_serialize", rename = "createdContractCode")]
53
		created_contract_code: Vec<u8>,
54
	},
55
}
56

            
57
#[derive(Clone, Copy, Eq, PartialEq, Debug, Encode, Decode, Serialize)]
58
#[serde(rename_all = "lowercase")]
59
pub enum CallType {
60
	Call,
61
	CallCode,
62
	DelegateCall,
63
	StaticCall,
64
}
65

            
66
#[derive(Clone, Copy, Eq, PartialEq, Debug, Encode, Decode, Serialize)]
67
#[serde(rename_all = "lowercase")]
68
pub enum CreateType {
69
	Create,
70
}
71

            
72
#[derive(Debug)]
73
pub enum ContextType {
74
	Call(CallType),
75
	Create,
76
}
77

            
78
impl ContextType {
79
	pub fn from(opcode: Vec<u8>) -> Option<Self> {
80
		let opcode = match alloc::str::from_utf8(&opcode[..]) {
81
			Ok(op) => op.to_uppercase(),
82
			_ => return None,
83
		};
84
		match &opcode[..] {
85
			"CREATE" | "CREATE2" => Some(ContextType::Create),
86
			"CALL" => Some(ContextType::Call(CallType::Call)),
87
			"CALLCODE" => Some(ContextType::Call(CallType::CallCode)),
88
			"DELEGATECALL" => Some(ContextType::Call(CallType::DelegateCall)),
89
			"STATICCALL" => Some(ContextType::Call(CallType::StaticCall)),
90
			_ => None,
91
		}
92
	}
93
}
94

            
95
pub fn convert_memory(memory: Vec<u8>) -> Vec<H256> {
96
	let size = 32;
97
	memory
98
		.chunks(size)
99
		.map(|c| {
100
			let mut msg = [0u8; 32];
101
			let chunk = c.len();
102
			if chunk < size {
103
				let left = size - chunk;
104
				let remainder = vec![0; left];
105
				msg[0..left].copy_from_slice(&remainder[..]);
106
				msg[left..size].copy_from_slice(c);
107
			} else {
108
				msg[0..size].copy_from_slice(c)
109
			}
110
			H256::from_slice(&msg[..])
111
		})
112
		.collect()
113
}