1// This file is part of Substrate.23// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.4// SPDX-License-Identifier: Apache-2.056// 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 at9//10// http://www.apache.org/licenses/LICENSE-2.011//12// Unless required by applicable law or agreed to in writing, software13// 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 and16// limitations under the License.1718///! For instruction benchmarking we do no instantiate a full contract but merely the19///! sandbox to execute the wasm code. This is because we do not need the full20///! environment that provides the seal interface as imported functions.21use super::{Config, code::WasmModule};22use sp_core::crypto::UncheckedFrom;23use sp_sandbox::{EnvironmentDefinitionBuilder, Instance, Memory};2425/// Minimal execution environment without any exported functions.26pub struct Sandbox {27 instance: Instance<()>,28 _memory: Option<Memory>,29}3031impl Sandbox {32 /// Invoke the `call` function of a contract code and panic on any execution error.33 pub fn invoke(&mut self) {34 self.instance.invoke("call", &[], &mut ()).unwrap();35 }36}3738impl<T: Config> From<&WasmModule<T>> for Sandbox39where40 T: Config,41 T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,42{43 /// Creates an instance from the supplied module and supplies as much memory44 /// to the instance as the module declares as imported.45 fn from(module: &WasmModule<T>) -> Self {46 let mut env_builder = EnvironmentDefinitionBuilder::new();47 let memory = module.add_memory(&mut env_builder);48 let instance = Instance::new(&module.code, &env_builder, &mut ())49 .expect("Failed to create benchmarking Sandbox instance");50 Self {51 instance,52 _memory: memory,53 }54 }55}difftreelog
source
pallets/contracts/src/benchmarking/sandbox.rs1.9 KiBsourcehistory