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
//! Environmental-aware externalities for EVM tracing in Wasm runtime. This enables
18
//! capturing the - potentially large - trace output data in the host and keep
19
//! a low memory footprint in `--execution=wasm`.
20
//!
21
//! - The original trace Runtime Api call is wrapped `using` environmental (thread local).
22
//! - Arguments are scale-encoded known types in the host.
23
//! - Host functions will decode the input and emit an event `with` environmental.
24

            
25
#![cfg_attr(not(feature = "std"), no_std)]
26
use sp_runtime_interface::runtime_interface;
27

            
28
use parity_scale_codec::Decode;
29
use sp_std::vec::Vec;
30

            
31
use evm_tracing_events::{Event, EvmEvent, GasometerEvent, RuntimeEvent, StepEventFilter};
32

            
33
936
#[runtime_interface]
34
pub trait MoonbeamExt {
35
	fn raw_step(&mut self, _data: Vec<u8>) {}
36

            
37
	fn raw_gas(&mut self, _data: Vec<u8>) {}
38

            
39
	fn raw_return_value(&mut self, _data: Vec<u8>) {}
40

            
41
	fn call_list_entry(&mut self, _index: u32, _value: Vec<u8>) {}
42

            
43
	fn call_list_new(&mut self) {}
44

            
45
	// New design, proxy events.
46
	/// An `Evm` event proxied by the Moonbeam runtime to this host function.
47
	/// evm -> moonbeam_runtime -> host.
48
	fn evm_event(&mut self, event: Vec<u8>) {
49
		if let Ok(event) = EvmEvent::decode(&mut &event[..]) {
50
			Event::Evm(event).emit();
51
		}
52
	}
53

            
54
180
	/// A `Gasometer` event proxied by the Moonbeam runtime to this host function.
55
	/// evm_gasometer -> moonbeam_runtime -> host.
56
180
	fn gasometer_event(&mut self, event: Vec<u8>) {
57
180
		if let Ok(event) = GasometerEvent::decode(&mut &event[..]) {
58
180
			Event::Gasometer(event).emit();
59
180
		}
60
180
	}
61

            
62
	/// A `Runtime` event proxied by the Moonbeam runtime to this host function.
63
	/// evm_runtime -> moonbeam_runtime -> host.
64
	fn runtime_event(&mut self, event: Vec<u8>) {
65
		if let Ok(event) = RuntimeEvent::decode(&mut &event[..]) {
66
			Event::Runtime(event).emit();
67
		}
68
	}
69

            
70
240
	/// Allow the tracing module in the runtime to know how to filter Step event
71
	/// content, as cloning the entire data is expensive and most of the time
72
	/// not necessary.
73
240
	fn step_event_filter(&self) -> StepEventFilter {
74
240
		evm_tracing_events::step_event_filter().unwrap_or_default()
75
240
	}
76

            
77
120
	/// An event to create a new CallList (currently a new transaction when tracing a block).
78
	#[version(2)]
79
120
	fn call_list_new(&mut self) {
80
120
		Event::CallListNew().emit();
81
120
	}
82
}