pallet_revive/mock.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
18//! Helper interfaces and functions, that help with controlling the execution of EVM contracts.
19//! It is mostly used to help with the implementation of foundry cheatscodes for forge test
20//! integration.
21
22use frame_system::pallet_prelude::OriginFor;
23use sp_core::{H160, U256};
24
25use crate::{DelegateInfo, ExecReturnValue, exec::Origin, pallet};
26
27/// A trait that provides hooks for mocking EVM contract calls and callers.
28/// This is useful for testing and simulating contract interactions within foundry forge tests.
29pub trait MockHandler<T: pallet::Config> {
30 /// Mock an EVM contract call.
31 ///
32 /// Returns `Some(ExecReturnValue)` if the call is mocked, otherwise `None`.
33 fn mock_call(
34 &self,
35 _callee: H160,
36 _call_data: &[u8],
37 _value_transferred: U256,
38 ) -> Option<ExecReturnValue> {
39 None
40 }
41
42 /// Mock the caller of a contract.
43 ///
44 /// # Parameters
45 ///
46 /// * `_frames_len`: The current number of frames on the call stack.
47 ///
48 /// Returns `Some(OriginFor<T>)` if the caller is mocked, otherwise `None`.
49 fn mock_caller(&self, _frames_len: usize) -> Option<OriginFor<T>> {
50 None
51 }
52
53 /// Mock the origin of a contract.
54 ///
55 /// Returns `Some(&Origin<T>)` if the caller is mocked, otherwise `None`.
56 fn mock_origin(&self) -> Option<&Origin<T>> {
57 None
58 }
59
60 /// Mock a delegated caller for a contract call.
61 ///
62 /// Returns `Some(DelegateInfo<T>)` if the delegated caller is mocked, otherwise `None`.
63 fn mock_delegated_caller(&self, _dest: H160, _input_data: &[u8]) -> Option<DelegateInfo<T>> {
64 None
65 }
66
67 /// Returns dummy code for mocked addresses.
68 ///
69 /// This method serves two purposes:
70 /// 1. Indicates whether an address has mocked calls (Some = mocked, None = not mocked)
71 /// 2. Provides the dummy bytecode for `EXTCODESIZE` and `EXTCODEHASH` opcodes
72 ///
73 /// # Returns
74 /// - `Some(bytecode)` containing dummy bytecode if the address has mocked calls
75 /// - `None` if the address is not mocked
76 fn mocked_code(&self, _address: H160) -> Option<&[u8]> {
77 None
78 }
79}