pallet_revive/tracing.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::{Code, DispatchError, Key, Weight, evm::Bytes, primitives::ExecReturnValue};
19use alloc::vec::Vec;
20use environmental::environmental;
21use sp_core::{H160, H256, U256};
22
23environmental!(tracer: dyn Tracing + 'static);
24
25/// Synthetic syscall name used for tracing PVM interpreter fuel consumption between real syscalls.
26pub const PVM_FUEL_NAME: &str = "pvm_fuel";
27
28/// Trace the execution of the given closure.
29///
30/// # Warning
31///
32/// Only meant to be called from off-chain code as its additional resource usage is
33/// not accounted for in the weights or memory envelope.
34pub fn trace<R, F: FnOnce() -> R>(tracer: &mut (dyn Tracing + 'static), f: F) -> R {
35 tracer::using_once(tracer, f)
36}
37
38/// Run the closure when tracing is enabled.
39///
40/// This is safe to be called from on-chain code as tracing will never be activated
41/// there. Hence the closure is not executed in this case.
42pub(crate) fn if_tracing<R, F: FnOnce(&mut (dyn Tracing + 'static)) -> R>(f: F) -> Option<R> {
43 tracer::with(f)
44}
45
46/// Interface to provide frame trace information for the current execution frame.
47pub trait FrameTraceInfo {
48 /// Get the amount of gas remaining in the current frame.
49 fn gas_left(&self) -> u64;
50
51 /// Returns how much weight was spent
52 fn weight_consumed(&self) -> Weight;
53
54 /// Get the output from the last frame.
55 fn last_frame_output(&self) -> Bytes;
56}
57
58/// Interface to provide EVM-specific trace information for the current execution frame.
59pub trait EVMFrameTraceInfo: FrameTraceInfo {
60 /// Get a snapshot of the memory at this point in execution.
61 ///
62 /// # Parameters
63 /// - `limit`: Maximum number of memory words to capture.
64 fn memory_snapshot(&self, limit: usize) -> Vec<Bytes>;
65
66 /// Get a snapshot of the stack at this point in execution.
67 fn stack_snapshot(&self) -> Vec<Bytes>;
68}
69
70/// Defines methods to trace contract interactions.
71pub trait Tracing {
72 /// Register an address that should be traced.
73 fn watch_address(&mut self, _addr: &H160) {}
74
75 /// Called before a contract call is executed.
76 ///
77 /// For CALL/DELEGATECALL opcodes:
78 /// - `gas_limit`: gas forwarded to the child call
79 fn enter_child_span(
80 &mut self,
81 _from: H160,
82 _to: H160,
83 _delegate_call: Option<H160>,
84 _is_read_only: bool,
85 _value: U256,
86 _input: &[u8],
87 _gas_limit: u64,
88 ) {
89 }
90
91 /// Called when a contract calls terminates (selfdestructs)
92 fn terminate(
93 &mut self,
94 _contract_address: H160,
95 _beneficiary_address: H160,
96 _gas_left: u64,
97 _value: U256,
98 ) {
99 }
100
101 /// Record the next code and salt to be instantiated.
102 fn instantiate_code(&mut self, _code: &Code, _salt: Option<&[u8; 32]>) {}
103
104 /// Called when a balance is read
105 fn balance_read(&mut self, _addr: &H160, _value: U256) {}
106
107 /// Called when storage read is called
108 fn storage_read(&mut self, _key: &Key, _value: Option<&[u8]>) {}
109
110 /// Called when storage write is called
111 fn storage_write(
112 &mut self,
113 _key: &Key,
114 _old_value: Option<Vec<u8>>,
115 _new_value: Option<&[u8]>,
116 ) {
117 }
118
119 /// Record a log event
120 fn log_event(&mut self, _event: H160, _topics: &[H256], _data: &[u8]) {}
121
122 /// Called after a contract call is executed
123 fn exit_child_span(
124 &mut self,
125 _output: &ExecReturnValue,
126 _gas_used: u64,
127 _weight_consumed: Weight,
128 ) {
129 }
130
131 /// Called when a contract call terminates with an error
132 fn exit_child_span_with_error(
133 &mut self,
134 _error: DispatchError,
135 _gas_used: u64,
136 _weight_consumed: Weight,
137 ) {
138 }
139
140 /// Check if the tracer is an execution tracer.
141 fn is_execution_tracer(&self) -> bool {
142 false
143 }
144
145 /// Called before an EVM opcode is executed.
146 ///
147 /// # Parameters
148 /// - `pc`: The current program counter.
149 /// - `opcode`: The opcode being executed.
150 /// - `trace_info`: Information about the current execution frame.
151 fn enter_opcode(&mut self, _pc: u64, _opcode: u8, _trace_info: &dyn EVMFrameTraceInfo) {}
152
153 /// Called before a PVM syscall is executed.
154 ///
155 /// # Parameters
156 /// - `ecall`: The name of the syscall being executed.
157 /// - `args`: The syscall arguments (register values).
158 /// - `trace_info`: Information about the current execution frame.
159 fn enter_ecall(
160 &mut self,
161 _ecall: &'static str,
162 _args: &[u64],
163 _trace_info: &dyn FrameTraceInfo,
164 ) {
165 }
166
167 /// Called after an EVM opcode or PVM syscall is executed to record the gas cost.
168 ///
169 /// # Parameters
170 /// - `trace_info`: Information about the current execution frame.
171 /// - `returned`: The syscall return value (PVM only, `None` for EVM opcodes).
172 fn exit_step(&mut self, _trace_info: &dyn FrameTraceInfo, _returned: Option<u64>) {}
173
174 /// Called once the transaction completes to report the gas consumed by the meter.
175 ///
176 /// # Parameters
177 /// - `base_call_weight`: Extrinsic base weight that is added on top of `weight_consumed` when
178 /// charging.
179 /// - `weight_consumed`: Weight used by the transaction logic, excluding the extrinsic base
180 /// weight.
181 fn dispatch_result(&mut self, _base_call_weight: Weight, _weight_consumed: Weight) {}
182}