diff --git a/frame/contracts/src/benchmarking/code.rs b/frame/contracts/src/benchmarking/code.rs index cb5052042aab2..f879d2eed5548 100644 --- a/frame/contracts/src/benchmarking/code.rs +++ b/frame/contracts/src/benchmarking/code.rs @@ -29,6 +29,7 @@ use crate::Module as Contracts; use parity_wasm::elements::{Instruction, Instructions, FuncBody, ValueType, BlockType}; use pwasm_utils::stack_height::inject_limiter; +use sp_core::crypto::UncheckedFrom; use sp_runtime::traits::Hash; use sp_sandbox::{EnvironmentDefinitionBuilder, Memory}; use sp_std::{prelude::*, convert::TryFrom}; @@ -86,7 +87,11 @@ pub struct ImportedMemory { } impl ImportedMemory { - pub fn max() -> Self { + pub fn max() -> Self + where + T: Trait, + T::AccountId: UncheckedFrom + AsRef<[u8]>, + { let pages = max_pages::(); Self { min_pages: pages, max_pages: pages } } @@ -106,7 +111,11 @@ pub struct WasmModule { memory: Option, } -impl From for WasmModule { +impl From for WasmModule +where + T: Trait, + T::AccountId: UncheckedFrom + AsRef<[u8]>, +{ fn from(def: ModuleDefinition) -> Self { // internal functions start at that offset. let func_offset = u32::try_from(def.imported_functions.len()).unwrap(); @@ -216,7 +225,11 @@ impl From for WasmModule { } } -impl WasmModule { +impl WasmModule +where + T: Trait, + T::AccountId: UncheckedFrom + AsRef<[u8]>, +{ /// Creates a wasm module with an empty `call` and `deploy` function and nothing else. pub fn dummy() -> Self { ModuleDefinition::default().into() @@ -470,6 +483,10 @@ pub mod body { } /// The maximum amount of pages any contract is allowed to have according to the current `Schedule`. -pub fn max_pages() -> u32 { +pub fn max_pages() -> u32 +where + T: Trait, + T::AccountId: UncheckedFrom + AsRef<[u8]>, +{ Contracts::::current_schedule().limits.memory_pages } diff --git a/frame/contracts/src/benchmarking/mod.rs b/frame/contracts/src/benchmarking/mod.rs index dd9e89d6f35ab..2e15542368a56 100644 --- a/frame/contracts/src/benchmarking/mod.rs +++ b/frame/contracts/src/benchmarking/mod.rs @@ -25,7 +25,9 @@ mod sandbox; use crate::{ *, Module as Contracts, exec::StorageKey, + rent::Rent, schedule::{API_BENCHMARK_BATCH_SIZE, INSTR_BENCHMARK_BATCH_SIZE}, + storage::Storage, }; use self::{ code::{ @@ -75,7 +77,11 @@ impl Endow { } } -impl Contract { +impl Contract +where + T: Trait, + T::AccountId: UncheckedFrom + AsRef<[u8]>, +{ /// Create new contract and use a default account id as instantiator. fn new( module: WasmModule, @@ -123,7 +129,8 @@ impl Contract { Endow::Max => (0u32.into(), Endow::max::()), }; T::Currency::make_free_balance_be(&caller, caller_funding::()); - let addr = T::DetermineContractAddress::contract_address_for(&module.hash, &data, &caller); + let salt = vec![0xff]; + let addr = Contracts::::contract_address(&caller, &module.hash, &salt); // The default block number is zero. The benchmarking system bumps the block number // to one for the benchmarking closure when it is set to zero. In order to prevent this @@ -139,6 +146,7 @@ impl Contract { Weight::max_value(), module.hash, data, + salt, )?; let result = Contract { @@ -160,7 +168,7 @@ impl Contract { fn store(&self, items: &Vec<(StorageKey, Vec)>) -> Result<(), &'static str> { let info = self.alive_info()?; for item in items { - crate::storage::write_contract_storage::( + Storage::::write( &self.account_id, &info.trie_id, &item.0, @@ -192,7 +200,7 @@ impl Contract { /// Get the block number when this contract will be evicted. Returns an error when /// the rent collection won't happen because the contract has to much endowment. fn eviction_at(&self) -> Result { - let projection = crate::rent::compute_rent_projection::(&self.account_id) + let projection = Rent::::compute_projection(&self.account_id) .map_err(|_| "Invalid acc for rent")?; match projection { RentProjection::EvictionAt(at) => Ok(at), @@ -211,7 +219,11 @@ struct Tombstone { storage: Vec<(StorageKey, Vec)>, } -impl Tombstone { +impl Tombstone +where + T: Trait, + T::AccountId: UncheckedFrom + AsRef<[u8]>, +{ /// Create and evict a new contract with the supplied storage item count and size each. fn new(stor_num: u32, stor_size: u32) -> Result { let contract = Contract::::new(WasmModule::dummy(), vec![], Endow::CollectRent)?; @@ -220,7 +232,7 @@ impl Tombstone { System::::set_block_number( contract.eviction_at()? + T::SignedClaimHandicap::get() + 5u32.into() ); - crate::rent::collect_rent::(&contract.account_id); + Rent::::collect(&contract.account_id); contract.ensure_tombstone()?; Ok(Tombstone { @@ -250,6 +262,11 @@ fn caller_funding() -> BalanceOf { } benchmarks! { + where_clause { where + T::AccountId: UncheckedFrom, + T::AccountId: AsRef<[u8]>, + } + _ { } @@ -276,17 +293,20 @@ benchmarks! { // The size of the input data influences the runtime because it is hashed in order to determine // the contract address. // `n`: Size of the data passed to constructor in kilobytes. + // `s`: Size of the salt in kilobytes. instantiate { let n in 0 .. code::max_pages::() * 64; + let s in 0 .. code::max_pages::() * 64; let data = vec![42u8; (n * 1024) as usize]; + let salt = vec![42u8; (s * 1024) as usize]; let endowment = Config::::subsistence_threshold_uncached(); let caller = whitelisted_caller(); T::Currency::make_free_balance_be(&caller, caller_funding::()); let WasmModule { code, hash, .. } = WasmModule::::dummy_with_mem(); let origin = RawOrigin::Signed(caller.clone()); - let addr = T::DetermineContractAddress::contract_address_for(&hash, &data, &caller); + let addr = Contracts::::contract_address(&caller, &hash, &salt); Contracts::::put_code_raw(code)?; - }: _(origin, endowment, Weight::max_value(), hash, data) + }: _(origin, endowment, Weight::max_value(), hash, data, salt) verify { // endowment was removed from the caller assert_eq!(T::Currency::free_balance(&caller), caller_funding::() - endowment); @@ -1000,7 +1020,7 @@ benchmarks! { let instance = Contract::::new(code, vec![], Endow::Max)?; let trie_id = instance.alive_info()?.trie_id; for key in keys { - crate::storage::write_contract_storage::( + Storage::::write( &instance.account_id, &trie_id, key.as_slice().try_into().map_err(|e| "Key has wrong length")?, @@ -1045,7 +1065,7 @@ benchmarks! { let instance = Contract::::new(code, vec![], Endow::Max)?; let trie_id = instance.alive_info()?.trie_id; for key in keys { - crate::storage::write_contract_storage::( + Storage::::write( &instance.account_id, &trie_id, key.as_slice().try_into().map_err(|e| "Key has wrong length")?, @@ -1089,7 +1109,7 @@ benchmarks! { }); let instance = Contract::::new(code, vec![], Endow::Max)?; let trie_id = instance.alive_info()?.trie_id; - crate::storage::write_contract_storage::( + Storage::::write( &instance.account_id, &trie_id, key.as_slice().try_into().map_err(|e| "Key has wrong length")?, @@ -1341,7 +1361,9 @@ benchmarks! { ValueType::I32, ValueType::I32, ValueType::I32, - ValueType::I32 + ValueType::I32, + ValueType::I32, + ValueType::I32, ], return_type: Some(ValueType::I32), }], @@ -1371,6 +1393,8 @@ benchmarks! { Regular(Instruction::I32Const(addr_len_offset as i32)), // address_len_ptr Regular(Instruction::I32Const(u32::max_value() as i32)), // output_ptr Regular(Instruction::I32Const(0)), // output_len_ptr + Regular(Instruction::I32Const(0)), // salt_ptr + Regular(Instruction::I32Const(0)), // salt_ptr_len Regular(Instruction::Call(0)), Regular(Instruction::Drop), ])), @@ -1381,8 +1405,8 @@ benchmarks! { let callee = instance.addr.clone(); let addresses = hashes .iter() - .map(|hash| T::DetermineContractAddress::contract_address_for( - hash, &[], &instance.account_id + .map(|hash| Contracts::::contract_address( + &instance.account_id, hash, &[], )) .collect::>(); @@ -1398,9 +1422,10 @@ benchmarks! { } } - seal_instantiate_per_input_output_kb { + seal_instantiate_per_input_output_salt_kb { let i in 0 .. (code::max_pages::() - 1) * 64; let o in 0 .. (code::max_pages::() - 1) * 64; + let s in 0 .. (code::max_pages::() - 1) * 64; let callee_code = WasmModule::::from(ModuleDefinition { memory: Some(ImportedMemory::max::()), imported_functions: vec![ImportedFunction { @@ -1458,7 +1483,9 @@ benchmarks! { ValueType::I32, ValueType::I32, ValueType::I32, - ValueType::I32 + ValueType::I32, + ValueType::I32, + ValueType::I32, ], return_type: Some(ValueType::I32), }], @@ -1496,6 +1523,8 @@ benchmarks! { Regular(Instruction::I32Const(addr_len_offset as i32)), // address_len_ptr Regular(Instruction::I32Const(output_offset as i32)), // output_ptr Regular(Instruction::I32Const(output_len_offset as i32)), // output_len_ptr + Counter(input_offset as u32, input_len as u32), // salt_ptr + Regular(Instruction::I32Const((s * 1024).max(input_len as u32) as i32)), // salt_len Regular(Instruction::Call(0)), Regular(Instruction::I32Eqz), Regular(Instruction::If(BlockType::NoResult)), @@ -2401,6 +2430,8 @@ mod tests { create_test!(seal_transfer); create_test!(seal_call); create_test!(seal_call_per_transfer_input_output_kb); + create_test!(seal_instantiate); + create_test!(seal_instantiate_per_input_output_salt_kb); create_test!(seal_clear_storage); create_test!(seal_hash_sha2_256); create_test!(seal_hash_sha2_256_per_kb); diff --git a/frame/contracts/src/benchmarking/sandbox.rs b/frame/contracts/src/benchmarking/sandbox.rs index 1d93db19ee59d..76cebfaf1ed6f 100644 --- a/frame/contracts/src/benchmarking/sandbox.rs +++ b/frame/contracts/src/benchmarking/sandbox.rs @@ -19,8 +19,11 @@ ///! sandbox to execute the wasm code. This is because we do not need the full ///! environment that provides the seal interface as imported functions. -use super::code::WasmModule; -use super::Trait; +use super::{ + Trait, + code::WasmModule, +}; +use sp_core::crypto::UncheckedFrom; use sp_sandbox::{EnvironmentDefinitionBuilder, Instance, Memory}; /// Minimal execution environment without any exported functions. @@ -36,7 +39,11 @@ impl Sandbox { } } -impl From<&WasmModule> for Sandbox { +impl From<&WasmModule> for Sandbox +where + T: Trait, + T::AccountId: UncheckedFrom + AsRef<[u8]>, +{ /// Creates an instance from the supplied module and supplies as much memory /// to the instance as the module declares as imported. fn from(module: &WasmModule) -> Self {