referrerpolicy=no-referrer-when-downgrade

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::{primitives::ExecReturnValue, Code, DispatchError, Key, Weight};
19use alloc::vec::Vec;
20use environmental::environmental;
21use sp_core::{H160, H256, U256};
22
23environmental!(tracer: dyn Tracing + 'static);
24
25/// Trace the execution of the given closure.
26///
27/// # Warning
28///
29/// Only meant to be called from off-chain code as its additional resource usage is
30/// not accounted for in the weights or memory envelope.
31pub fn trace<R, F: FnOnce() -> R>(tracer: &mut (dyn Tracing + 'static), f: F) -> R {
32	tracer::using_once(tracer, f)
33}
34
35/// Run the closure when tracing is enabled.
36///
37/// This is safe to be called from on-chain code as tracing will never be activated
38/// there. Hence the closure is not executed in this case.
39pub(crate) fn if_tracing<R, F: FnOnce(&mut (dyn Tracing + 'static)) -> R>(f: F) -> Option<R> {
40	tracer::with(f)
41}
42
43/// Defines methods to trace contract interactions.
44pub trait Tracing {
45	/// Register an address that should be traced.
46	fn watch_address(&mut self, _addr: &H160) {}
47
48	/// Called before a contract call is executed
49	fn enter_child_span(
50		&mut self,
51		_from: H160,
52		_to: H160,
53		_is_delegate_call: bool,
54		_is_read_only: bool,
55		_value: U256,
56		_input: &[u8],
57		_gas: Weight,
58	) {
59	}
60
61	/// Record the next code and salt to be instantiated.
62	fn instantiate_code(&mut self, _code: &Code, _salt: Option<&[u8; 32]>) {}
63
64	/// Called when a balance is read
65	fn balance_read(&mut self, _addr: &H160, _value: U256) {}
66
67	/// Called when storage read is called
68	fn storage_read(&mut self, _key: &Key, _value: Option<&[u8]>) {}
69
70	/// Called when storage write is called
71	fn storage_write(
72		&mut self,
73		_key: &Key,
74		_old_value: Option<Vec<u8>>,
75		_new_value: Option<&[u8]>,
76	) {
77	}
78
79	/// Record a log event
80	fn log_event(&mut self, _event: H160, _topics: &[H256], _data: &[u8]) {}
81
82	/// Called after a contract call is executed
83	fn exit_child_span(&mut self, _output: &ExecReturnValue, _gas_left: Weight) {}
84
85	/// Called when a contract call terminates with an error
86	fn exit_child_span_with_error(&mut self, _error: DispatchError, _gas_left: Weight) {}
87}