moonbeam_primitives_ext/lib.rs
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)]
26use sp_runtime_interface::{
27 pass_by::{AllocateAndReturnByCodec, PassFatPointerAndRead},
28 runtime_interface,
29};
30
31use parity_scale_codec::Decode;
32use sp_std::vec::Vec;
33
34use evm_tracing_events::{Event, EvmEvent, GasometerEvent, RuntimeEvent, StepEventFilter};
35
36#[runtime_interface]
37pub trait MoonbeamExt {
38 fn raw_step(&mut self, _data: PassFatPointerAndRead<Vec<u8>>) {}
39
40 fn raw_gas(&mut self, _data: PassFatPointerAndRead<Vec<u8>>) {}
41
42 fn raw_return_value(&mut self, _data: PassFatPointerAndRead<Vec<u8>>) {}
43
44 fn call_list_entry(&mut self, _index: u32, _value: PassFatPointerAndRead<Vec<u8>>) {}
45
46 fn call_list_new(&mut self) {}
47
48 // New design, proxy events.
49 /// An `Evm` event proxied by the Moonbeam runtime to this host function.
50 /// evm -> moonbeam_runtime -> host.
51 fn evm_event(&mut self, event: PassFatPointerAndRead<Vec<u8>>) {
52 if let Ok(event) = EvmEvent::decode(&mut &event[..]) {
53 Event::Evm(event).emit();
54 }
55 }
56
57 /// A `Gasometer` event proxied by the Moonbeam runtime to this host function.
58 /// evm_gasometer -> moonbeam_runtime -> host.
59 fn gasometer_event(&mut self, event: PassFatPointerAndRead<Vec<u8>>) {
60 if let Ok(event) = GasometerEvent::decode(&mut &event[..]) {
61 Event::Gasometer(event).emit();
62 }
63 }
64
65 /// A `Runtime` event proxied by the Moonbeam runtime to this host function.
66 /// evm_runtime -> moonbeam_runtime -> host.
67 fn runtime_event(&mut self, event: PassFatPointerAndRead<Vec<u8>>) {
68 if let Ok(event) = RuntimeEvent::decode(&mut &event[..]) {
69 Event::Runtime(event).emit();
70 }
71 }
72
73 /// Allow the tracing module in the runtime to know how to filter Step event
74 /// content, as cloning the entire data is expensive and most of the time
75 /// not necessary.
76 fn step_event_filter(&self) -> AllocateAndReturnByCodec<StepEventFilter> {
77 evm_tracing_events::step_event_filter().unwrap_or_default()
78 }
79
80 /// An event to create a new CallList (currently a new transaction when tracing a block).
81 #[version(2)]
82 fn call_list_new(&mut self) {
83 Event::CallListNew().emit();
84 }
85}