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
// Copyright 2019-2022 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/>.

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

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

use parity_scale_codec::Decode;
use sp_std::vec::Vec;

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

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

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

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

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

    fn call_list_new(&mut self) {}

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

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

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

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

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