pallet_contracts_for_drink/debug.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
18pub use crate::exec::{ExecResult, ExportedFunction};
19use crate::{Config, LOG_TARGET};
20pub use pallet_contracts_primitives::ExecReturnValue;
21
22/// Umbrella trait for all interfaces that serves for debugging.
23pub trait Debugger<T: Config>: Tracing<T> + CallInterceptor<T> {}
24
25impl<T: Config, V> Debugger<T> for V where V: Tracing<T> + CallInterceptor<T> {}
26
27/// Defines methods to capture contract calls, enabling external observers to
28/// measure, trace, and react to contract interactions.
29pub trait Tracing<T: Config> {
30 /// The type of [`CallSpan`] that is created by this trait.
31 type CallSpan: CallSpan;
32
33 /// Creates a new call span to encompass the upcoming contract execution.
34 ///
35 /// This method should be invoked just before the execution of a contract and
36 /// marks the beginning of a traceable span of execution.
37 ///
38 /// # Arguments
39 ///
40 /// * `contract_address` - The address of the contract that is about to be executed.
41 /// * `entry_point` - Describes whether the call is the constructor or a regular call.
42 /// * `input_data` - The raw input data of the call.
43 fn new_call_span(
44 contract_address: &T::AccountId,
45 entry_point: ExportedFunction,
46 input_data: &[u8],
47 ) -> Self::CallSpan;
48}
49
50/// Defines a span of execution for a contract call.
51pub trait CallSpan {
52 /// Called just after the execution of a contract.
53 ///
54 /// # Arguments
55 ///
56 /// * `output` - The raw output of the call.
57 fn after_call(self, output: &ExecReturnValue);
58}
59
60impl<T: Config> Tracing<T> for () {
61 type CallSpan = ();
62
63 fn new_call_span(
64 contract_address: &T::AccountId,
65 entry_point: ExportedFunction,
66 input_data: &[u8],
67 ) {
68 log::trace!(target: LOG_TARGET, "call {entry_point:?} account: {contract_address:?}, input_data: {input_data:?}")
69 }
70}
71
72impl CallSpan for () {
73 fn after_call(self, output: &ExecReturnValue) {
74 log::trace!(target: LOG_TARGET, "call result {output:?}")
75 }
76}
77
78/// Provides an interface for intercepting contract calls.
79pub trait CallInterceptor<T: Config> {
80 /// Allows to intercept contract calls and decide whether they should be executed or not.
81 /// If the call is intercepted, the mocked result of the call is returned.
82 ///
83 /// # Arguments
84 ///
85 /// * `contract_address` - The address of the contract that is about to be executed.
86 /// * `entry_point` - Describes whether the call is the constructor or a regular call.
87 /// * `input_data` - The raw input data of the call.
88 ///
89 /// # Expected behavior
90 ///
91 /// This method should return:
92 /// * `Some(ExecResult)` - if the call should be intercepted and the mocked result of the call
93 /// is returned.
94 /// * `None` - otherwise, i.e. the call should be executed normally.
95 fn intercept_call(
96 contract_address: &T::AccountId,
97 entry_point: &ExportedFunction,
98 input_data: &[u8],
99 ) -> Option<ExecResult>;
100}
101
102impl<T: Config> CallInterceptor<T> for () {
103 fn intercept_call(
104 _contract_address: &T::AccountId,
105 _entry_point: &ExportedFunction,
106 _input_data: &[u8],
107 ) -> Option<ExecResult> {
108 None
109 }
110}