referrerpolicy=no-referrer-when-downgrade

pallet_contracts/benchmarking/
sandbox.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/// ! For instruction benchmarking we do not instantiate a full contract but merely the
19/// ! sandbox to execute the Wasm code. This is because we do not need the full
20/// ! environment that provides the seal interface as imported functions.
21use super::{code::WasmModule, Config};
22use crate::wasm::{
23	AllowDeprecatedInterface, AllowUnstableInterface, Determinism, Environment, LoadedModule,
24	LoadingMode, WasmBlob,
25};
26use sp_core::Get;
27use wasmi::{errors::LinkerError, CompilationMode, Func, Linker, StackLimits, Store};
28
29/// Minimal execution environment without any imported functions.
30pub struct Sandbox {
31	entry_point: Func,
32	store: Store<()>,
33}
34
35impl Sandbox {
36	/// Invoke the `call` function of a contract code and panic on any execution error.
37	pub fn invoke(&mut self) {
38		self.entry_point.call(&mut self.store, &[], &mut []).unwrap();
39	}
40}
41
42impl<T: Config> From<&WasmModule<T>> for Sandbox {
43	/// Creates an instance from the supplied module.
44	/// Sets the execution engine fuel level to `u64::MAX`.
45	fn from(module: &WasmModule<T>) -> Self {
46		let contract = LoadedModule::new::<T>(
47			&module.code,
48			Determinism::Relaxed,
49			Some(StackLimits::default()),
50			LoadingMode::Checked,
51			CompilationMode::Eager,
52		)
53		.expect("Failed to load Wasm module");
54
55		let (mut store, _memory, instance) = WasmBlob::<T>::instantiate::<EmptyEnv, _>(
56			contract,
57			(),
58			&<T>::Schedule::get(),
59			// We are testing with an empty environment anyways
60			AllowDeprecatedInterface::No,
61		)
62		.expect("Failed to create benchmarking Sandbox instance");
63
64		// Set fuel for wasmi execution.
65		store
66			.set_fuel(u64::MAX)
67			.expect("We've set up engine to fuel consuming mode; qed");
68
69		let entry_point = instance
70			.start(&mut store)
71			.unwrap()
72			.get_export(&store, "call")
73			.unwrap()
74			.into_func()
75			.unwrap();
76		Self { entry_point, store }
77	}
78}
79
80struct EmptyEnv;
81
82impl Environment<()> for EmptyEnv {
83	fn define(
84		_: &mut Store<()>,
85		_: &mut Linker<()>,
86		_: AllowUnstableInterface,
87		_: AllowDeprecatedInterface,
88	) -> Result<(), LinkerError> {
89		Ok(())
90	}
91}