|
| 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 | +use crate::{ |
| 19 | + benchmarking::{Contract, WasmModule}, |
| 20 | + exec::Stack, |
| 21 | + storage::meter::Meter, |
| 22 | + wasm::Runtime, |
| 23 | + BalanceOf, Config, DebugBufferVec, Determinism, ExecReturnValue, GasMeter, Origin, Schedule, |
| 24 | + TypeInfo, WasmBlob, Weight, |
| 25 | +}; |
| 26 | +use codec::{Encode, HasCompact}; |
| 27 | +use core::fmt::Debug; |
| 28 | +use sp_core::Get; |
| 29 | +use sp_std::prelude::*; |
| 30 | + |
| 31 | +type StackExt<'a, T> = Stack<'a, T, WasmBlob<T>>; |
| 32 | + |
| 33 | +/// A prepared contract call ready to be executed. |
| 34 | +pub struct PreparedCall<'a, T: Config> { |
| 35 | + func: wasmi::Func, |
| 36 | + store: wasmi::Store<Runtime<'a, StackExt<'a, T>>>, |
| 37 | +} |
| 38 | + |
| 39 | +impl<'a, T: Config> PreparedCall<'a, T> { |
| 40 | + pub fn call(mut self) -> ExecReturnValue { |
| 41 | + let result = self.func.call(&mut self.store, &[], &mut []); |
| 42 | + WasmBlob::<T>::process_result(self.store, result).unwrap() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// A builder used to prepare a contract call. |
| 47 | +pub struct CallSetup<T: Config> { |
| 48 | + contract: Contract<T>, |
| 49 | + dest: T::AccountId, |
| 50 | + origin: Origin<T>, |
| 51 | + gas_meter: GasMeter<T>, |
| 52 | + storage_meter: Meter<T>, |
| 53 | + schedule: Schedule<T>, |
| 54 | + value: BalanceOf<T>, |
| 55 | + debug_message: Option<DebugBufferVec<T>>, |
| 56 | + determinism: Determinism, |
| 57 | + data: Vec<u8>, |
| 58 | +} |
| 59 | + |
| 60 | +impl<T> CallSetup<T> |
| 61 | +where |
| 62 | + T: Config + pallet_balances::Config, |
| 63 | + <BalanceOf<T> as HasCompact>::Type: Clone + Eq + PartialEq + Debug + TypeInfo + Encode, |
| 64 | +{ |
| 65 | + /// Setup a new call for the given module. |
| 66 | + pub fn new(module: WasmModule<T>) -> Self { |
| 67 | + let contract = Contract::<T>::new(module.clone(), vec![]).unwrap(); |
| 68 | + let dest = contract.account_id.clone(); |
| 69 | + let origin = Origin::from_account_id(contract.caller.clone()); |
| 70 | + |
| 71 | + let storage_meter = Meter::new(&origin, None, 0u32.into()).unwrap(); |
| 72 | + |
| 73 | + Self { |
| 74 | + contract, |
| 75 | + dest, |
| 76 | + origin, |
| 77 | + gas_meter: GasMeter::new(Weight::MAX), |
| 78 | + storage_meter, |
| 79 | + schedule: T::Schedule::get(), |
| 80 | + value: 0u32.into(), |
| 81 | + debug_message: None, |
| 82 | + determinism: Determinism::Enforced, |
| 83 | + data: vec![], |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// Set the meter's storage deposit limit. |
| 88 | + pub fn set_storage_deposit_limit(&mut self, balance: BalanceOf<T>) { |
| 89 | + self.storage_meter = Meter::new(&self.origin, Some(balance), 0u32.into()).unwrap(); |
| 90 | + } |
| 91 | + |
| 92 | + /// Set the call's origin. |
| 93 | + pub fn set_origin(&mut self, origin: Origin<T>) { |
| 94 | + self.origin = origin; |
| 95 | + } |
| 96 | + |
| 97 | + /// Set the contract's balance. |
| 98 | + pub fn set_balance(&mut self, value: BalanceOf<T>) { |
| 99 | + self.contract.set_balance(value); |
| 100 | + } |
| 101 | + |
| 102 | + /// Set the call's input data. |
| 103 | + pub fn set_data(&mut self, value: Vec<u8>) { |
| 104 | + self.data = value; |
| 105 | + } |
| 106 | + |
| 107 | + /// Set the debug message. |
| 108 | + pub fn enable_debug_message(&mut self) { |
| 109 | + self.debug_message = Some(Default::default()); |
| 110 | + } |
| 111 | + |
| 112 | + /// Get the debug message. |
| 113 | + pub fn debug_message(&self) -> Option<DebugBufferVec<T>> { |
| 114 | + self.debug_message.clone() |
| 115 | + } |
| 116 | + |
| 117 | + /// Get the call's input data. |
| 118 | + pub fn data(&self) -> Vec<u8> { |
| 119 | + self.data.clone() |
| 120 | + } |
| 121 | + |
| 122 | + /// Get the call's contract. |
| 123 | + pub fn contract(&self) -> Contract<T> { |
| 124 | + self.contract.clone() |
| 125 | + } |
| 126 | + |
| 127 | + /// Build the call stack. |
| 128 | + pub fn ext(&mut self) -> (StackExt<'_, T>, WasmBlob<T>) { |
| 129 | + StackExt::bench_new_call( |
| 130 | + self.dest.clone(), |
| 131 | + self.origin.clone(), |
| 132 | + &mut self.gas_meter, |
| 133 | + &mut self.storage_meter, |
| 134 | + &self.schedule, |
| 135 | + self.value, |
| 136 | + self.debug_message.as_mut(), |
| 137 | + self.determinism, |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + /// Prepare a call to the module. |
| 142 | + pub fn prepare_call<'a>( |
| 143 | + ext: &'a mut StackExt<'a, T>, |
| 144 | + module: WasmBlob<T>, |
| 145 | + input: Vec<u8>, |
| 146 | + ) -> PreparedCall<'a, T> { |
| 147 | + let (func, store) = module.bench_prepare_call(ext, input); |
| 148 | + PreparedCall { func, store } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +#[macro_export] |
| 153 | +macro_rules! call_builder( |
| 154 | + ($func: ident, $module:expr) => { |
| 155 | + $crate::call_builder!($func, _contract, $module); |
| 156 | + }; |
| 157 | + ($func: ident, $contract: ident, $module:expr) => { |
| 158 | + let mut setup = CallSetup::<T>::new($module); |
| 159 | + $crate::call_builder!($func, $contract, setup: setup); |
| 160 | + }; |
| 161 | + ($func:ident, setup: $setup: ident) => { |
| 162 | + $crate::call_builder!($func, _contract, setup: $setup); |
| 163 | + }; |
| 164 | + ($func:ident, $contract: ident, setup: $setup: ident) => { |
| 165 | + let data = $setup.data(); |
| 166 | + let $contract = $setup.contract(); |
| 167 | + let (mut ext, module) = $setup.ext(); |
| 168 | + let $func = CallSetup::<T>::prepare_call(&mut ext, module, data); |
| 169 | + }; |
| 170 | +); |
0 commit comments