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};
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		_delegate_call: Option<H160>,
54		_is_read_only: bool,
55		_value: U256,
56		_input: &[u8],
57		_gas_limit: U256,
58	) {
59	}
60
61	/// Called when a contract calls terminates (selfdestructs)
62	fn terminate(
63		&mut self,
64		_contract_address: H160,
65		_beneficiary_address: H160,
66		_gas_left: U256,
67		_value: U256,
68	) {
69	}
70
71	/// Record the next code and salt to be instantiated.
72	fn instantiate_code(&mut self, _code: &Code, _salt: Option<&[u8; 32]>) {}
73
74	/// Called when a balance is read
75	fn balance_read(&mut self, _addr: &H160, _value: U256) {}
76
77	/// Called when storage read is called
78	fn storage_read(&mut self, _key: &Key, _value: Option<&[u8]>) {}
79
80	/// Called when storage write is called
81	fn storage_write(
82		&mut self,
83		_key: &Key,
84		_old_value: Option<Vec<u8>>,
85		_new_value: Option<&[u8]>,
86	) {
87	}
88
89	/// Record a log event
90	fn log_event(&mut self, _event: H160, _topics: &[H256], _data: &[u8]) {}
91
92	/// Called after a contract call is executed
93	fn exit_child_span(&mut self, _output: &ExecReturnValue, _gas_used: U256) {}
94
95	/// Called when a contract call terminates with an error
96	fn exit_child_span_with_error(&mut self, _error: DispatchError, _gas_used: U256) {}
97}