pallet_contracts/benchmarking/
sandbox.rs1use 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
29pub struct Sandbox {
31 entry_point: Func,
32 store: Store<()>,
33}
34
35impl Sandbox {
36 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 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 AllowDeprecatedInterface::No,
61 )
62 .expect("Failed to create benchmarking Sandbox instance");
63
64 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}