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