referrerpolicy=no-referrer-when-downgrade

pallet_revive/evm/
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.
17use crate::{
18	Config,
19	evm::{CallTrace, ExecutionTrace, Trace},
20	tracing::Tracing,
21};
22
23mod call_tracing;
24pub use call_tracing::*;
25
26mod prestate_tracing;
27pub use prestate_tracing::*;
28
29mod execution_tracing;
30pub use execution_tracing::*;
31
32/// A composite tracer.
33#[derive(derive_more::From, Debug)]
34pub enum Tracer<T> {
35	/// A tracer that traces calls.
36	CallTracer(CallTracer),
37	/// A tracer that traces the prestate.
38	PrestateTracer(PrestateTracer<T>),
39	/// A tracer that traces opcodes and syscalls.
40	ExecutionTracer(ExecutionTracer),
41}
42
43impl<T: Config> Tracer<T>
44where
45	T::Nonce: Into<u32>,
46{
47	/// Returns an empty trace.
48	pub fn empty_trace(&self) -> Trace {
49		match self {
50			Tracer::CallTracer(_) => CallTrace::default().into(),
51			Tracer::PrestateTracer(tracer) => tracer.empty_trace().into(),
52			Tracer::ExecutionTracer(_) => ExecutionTrace::default().into(),
53		}
54	}
55
56	/// Get a mutable trait‐object reference to the inner tracer.
57	pub fn as_tracing(&mut self) -> &mut (dyn Tracing + 'static) {
58		match self {
59			Tracer::CallTracer(inner) => inner as &mut dyn Tracing,
60			Tracer::PrestateTracer(inner) => inner as &mut dyn Tracing,
61			Tracer::ExecutionTracer(inner) => inner as &mut dyn Tracing,
62		}
63	}
64
65	/// Collect the trace, or `None` when it is empty. A tracer can run and still produce an empty
66	/// trace (e.g. a prestate diff with no state changes), so the caller decides what `None` means.
67	pub fn collect_trace(self) -> Option<Trace> {
68		let empty = self.empty_trace();
69		let trace = match self {
70			Tracer::CallTracer(inner) => Trace::Call(inner.collect_trace().unwrap_or_default()),
71			Tracer::PrestateTracer(inner) => Trace::Prestate(inner.collect_trace()),
72			Tracer::ExecutionTracer(inner) => Trace::Execution(inner.collect_trace()),
73		};
74		(trace != empty).then_some(trace)
75	}
76
77	/// Check if this is an execution tracer.
78	pub fn is_execution_tracer(&self) -> bool {
79		matches!(self, Tracer::ExecutionTracer(_))
80	}
81}
82
83#[cfg(test)]
84mod tests {
85	use super::*;
86	use crate::tests::{ExtBuilder, Test};
87
88	// An extrinsic that runs no contract code must produce no trace, so
89	// consumers can rely on a trace's presence to mean a contract actually ran.
90	#[test]
91	fn collect_trace_is_none_when_nothing_executed() {
92		ExtBuilder::default().build().execute_with(|| {
93			let call = Tracer::<Test>::CallTracer(CallTracer::new(Default::default()));
94			let prestate = Tracer::<Test>::PrestateTracer(PrestateTracer::new(Default::default()));
95			let execution =
96				Tracer::<Test>::ExecutionTracer(ExecutionTracer::new(Default::default()));
97
98			assert_eq!(call.collect_trace(), None);
99			assert_eq!(prestate.collect_trace(), None);
100			assert_eq!(execution.collect_trace(), None);
101		});
102	}
103}