From 9c52ff92230cb9e31cd9c198f7ec49f95db5cd35 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 19 Dec 2022 15:59:23 +0200 Subject: [PATCH 01/10] update RuntimeCosts to Weights V2, update tests --- frame/contracts/src/tests.rs | 6 +- frame/contracts/src/wasm/mod.rs | 4 +- frame/contracts/src/wasm/runtime.rs | 161 ++++++++++++++++------------ 3 files changed, 98 insertions(+), 73 deletions(-) diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 6121d880ca8c5..e25efcf9cd796 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -423,7 +423,7 @@ pub const BOB: AccountId32 = AccountId32::new([2u8; 32]); pub const CHARLIE: AccountId32 = AccountId32::new([3u8; 32]); pub const DJANGO: AccountId32 = AccountId32::new([4u8; 32]); -pub const GAS_LIMIT: Weight = Weight::from_ref_time(100_000_000_000).set_proof_size(256 * 1024); +pub const GAS_LIMIT: Weight = Weight::from_ref_time(100_000_000_000).set_proof_size(512 * 1024); pub struct ExtBuilder { existential_deposit: u64, @@ -2732,7 +2732,9 @@ fn gas_estimation_call_runtime() { let call = RuntimeCall::Contracts(crate::Call::call { dest: addr_callee, value: 0, - gas_limit: GAS_LIMIT.set_ref_time(GAS_LIMIT.ref_time() / 3), + gas_limit: GAS_LIMIT + .set_ref_time(GAS_LIMIT.ref_time() / 3) + .set_proof_size(GAS_LIMIT.proof_size() / 3), storage_deposit_limit: None, data: vec![], }); diff --git a/frame/contracts/src/wasm/mod.rs b/frame/contracts/src/wasm/mod.rs index d85dac95cc712..9471d9d01027b 100644 --- a/frame/contracts/src/wasm/mod.rs +++ b/frame/contracts/src/wasm/mod.rs @@ -431,7 +431,9 @@ mod tests { events: Default::default(), runtime_calls: Default::default(), schedule: Default::default(), - gas_meter: GasMeter::new(Weight::from_ref_time(10_000_000_000)), + gas_meter: GasMeter::new( + Weight::from_ref_time(10_000_000_000).set_proof_size(10 * 1024 * 1024), + ), debug_buffer: Default::default(), ecdsa_recover: Default::default(), } diff --git a/frame/contracts/src/wasm/runtime.rs b/frame/contracts/src/wasm/runtime.rs index 50ad9996e6eb6..1d11a9931f91d 100644 --- a/frame/contracts/src/wasm/runtime.rs +++ b/frame/contracts/src/wasm/runtime.rs @@ -277,81 +277,102 @@ impl RuntimeCosts { { use self::RuntimeCosts::*; let weight = match *self { - MeteringBlock(amount) => s.gas.saturating_add(amount), - CopyFromContract(len) => s.return_per_byte.saturating_mul(len.into()), - CopyToContract(len) => s.input_per_byte.saturating_mul(len.into()), - Caller => s.caller, - IsContract => s.is_contract, - CodeHash => s.code_hash, - OwnCodeHash => s.own_code_hash, - CallerIsOrigin => s.caller_is_origin, - Address => s.address, - GasLeft => s.gas_left, - Balance => s.balance, - ValueTransferred => s.value_transferred, - MinimumBalance => s.minimum_balance, - BlockNumber => s.block_number, - Now => s.now, - WeightToFee => s.weight_to_fee, - InputBase => s.input, - Return(len) => s.r#return.saturating_add(s.return_per_byte.saturating_mul(len.into())), - Terminate => s.terminate, - Random => s.random, - DepositEvent { num_topic, len } => s - .deposit_event - .saturating_add(s.deposit_event_per_topic.saturating_mul(num_topic.into())) - .saturating_add(s.deposit_event_per_byte.saturating_mul(len.into())), - DebugMessage => s.debug_message, - SetStorage { new_bytes, old_bytes } => s - .set_storage - .saturating_add(s.set_storage_per_new_byte.saturating_mul(new_bytes.into())) - .saturating_add(s.set_storage_per_old_byte.saturating_mul(old_bytes.into())), - ClearStorage(len) => s - .clear_storage - .saturating_add(s.clear_storage_per_byte.saturating_mul(len.into())), - ContainsStorage(len) => s - .contains_storage - .saturating_add(s.contains_storage_per_byte.saturating_mul(len.into())), - GetStorage(len) => + MeteringBlock(amount) => Weight::from_ref_time(s.gas.saturating_add(amount)), + CopyFromContract(len) => + Weight::from_ref_time(s.return_per_byte.saturating_mul(len.into())), + CopyToContract(len) => + Weight::from_ref_time(s.input_per_byte.saturating_mul(len.into())), + Caller => Weight::from_ref_time(s.caller), + IsContract => Weight::from_ref_time(s.is_contract), + CodeHash => Weight::from_ref_time(s.code_hash), + OwnCodeHash => Weight::from_ref_time(s.own_code_hash), + CallerIsOrigin => Weight::from_ref_time(s.caller_is_origin), + Address => Weight::from_ref_time(s.address), + GasLeft => Weight::from_ref_time(s.gas_left), + Balance => Weight::from_ref_time(s.balance), + ValueTransferred => Weight::from_ref_time(s.value_transferred), + MinimumBalance => Weight::from_ref_time(s.minimum_balance), + BlockNumber => Weight::from_ref_time(s.block_number), + Now => Weight::from_ref_time(s.now), + WeightToFee => Weight::from_ref_time(s.weight_to_fee), + InputBase => Weight::from_ref_time(s.input), + Return(len) => Weight::from_ref_time( + s.r#return.saturating_add(s.return_per_byte.saturating_mul(len.into())), + ), + Terminate => Weight::from_ref_time(s.terminate), + Random => Weight::from_ref_time(s.random), + DepositEvent { num_topic, len } => Weight::from_ref_time( + s.deposit_event + .saturating_add(s.deposit_event_per_topic.saturating_mul(num_topic.into())) + .saturating_add(s.deposit_event_per_byte.saturating_mul(len.into())), + ), + DebugMessage => Weight::from_ref_time(s.debug_message), + SetStorage { new_bytes, old_bytes } => Weight::from_ref_time( + s.set_storage + .saturating_add(s.set_storage_per_new_byte.saturating_mul(new_bytes.into())) + .saturating_add(s.set_storage_per_old_byte.saturating_mul(old_bytes.into())), + ) + .set_proof_size(old_bytes.into()), + ClearStorage(len) => Weight::from_ref_time( + s.clear_storage + .saturating_add(s.clear_storage_per_byte.saturating_mul(len.into())), + ) + .set_proof_size(len.into()), + ContainsStorage(len) => Weight::from_ref_time( + s.contains_storage + .saturating_add(s.contains_storage_per_byte.saturating_mul(len.into())), + ) + .set_proof_size(len.into()), + GetStorage(len) => Weight::from_ref_time( s.get_storage.saturating_add(s.get_storage_per_byte.saturating_mul(len.into())), - TakeStorage(len) => s - .take_storage - .saturating_add(s.take_storage_per_byte.saturating_mul(len.into())), - Transfer => s.transfer, - CallBase => s.call, - DelegateCallBase => s.delegate_call, - CallSurchargeTransfer => s.call_transfer_surcharge, - CallInputCloned(len) => s.call_per_cloned_byte.saturating_mul(len.into()), - InstantiateBase { input_data_len, salt_len } => s - .instantiate - .saturating_add(s.return_per_byte.saturating_mul(input_data_len.into())) - .saturating_add(s.instantiate_per_salt_byte.saturating_mul(salt_len.into())), - InstantiateSurchargeTransfer => s.instantiate_transfer_surcharge, - HashSha256(len) => s - .hash_sha2_256 - .saturating_add(s.hash_sha2_256_per_byte.saturating_mul(len.into())), - HashKeccak256(len) => s - .hash_keccak_256 - .saturating_add(s.hash_keccak_256_per_byte.saturating_mul(len.into())), - HashBlake256(len) => s - .hash_blake2_256 - .saturating_add(s.hash_blake2_256_per_byte.saturating_mul(len.into())), - HashBlake128(len) => s - .hash_blake2_128 - .saturating_add(s.hash_blake2_128_per_byte.saturating_mul(len.into())), - EcdsaRecovery => s.ecdsa_recover, - ChainExtension(amount) => amount, - CallRuntime(weight) => weight.ref_time(), - SetCodeHash => s.set_code_hash, - EcdsaToEthAddress => s.ecdsa_to_eth_address, - ReentrantCount => s.reentrance_count, - AccountEntranceCount => s.account_reentrance_count, - InstantationNonce => s.instantiation_nonce, + ) + .set_proof_size(len.into()), + TakeStorage(len) => Weight::from_ref_time( + s.take_storage + .saturating_add(s.take_storage_per_byte.saturating_mul(len.into())), + ) + .set_proof_size(len.into()), + Transfer => Weight::from_ref_time(s.transfer), + CallBase => Weight::from_ref_time(s.call), + DelegateCallBase => Weight::from_ref_time(s.delegate_call), + CallSurchargeTransfer => Weight::from_ref_time(s.call_transfer_surcharge), + CallInputCloned(len) => + Weight::from_ref_time(s.call_per_cloned_byte.saturating_mul(len.into())), + InstantiateBase { input_data_len, salt_len } => Weight::from_ref_time( + s.instantiate + .saturating_add(s.return_per_byte.saturating_mul(input_data_len.into())) + .saturating_add(s.instantiate_per_salt_byte.saturating_mul(salt_len.into())), + ), + InstantiateSurchargeTransfer => Weight::from_ref_time(s.instantiate_transfer_surcharge), + HashSha256(len) => Weight::from_ref_time( + s.hash_sha2_256 + .saturating_add(s.hash_sha2_256_per_byte.saturating_mul(len.into())), + ), + HashKeccak256(len) => Weight::from_ref_time( + s.hash_keccak_256 + .saturating_add(s.hash_keccak_256_per_byte.saturating_mul(len.into())), + ), + HashBlake256(len) => Weight::from_ref_time( + s.hash_blake2_256 + .saturating_add(s.hash_blake2_256_per_byte.saturating_mul(len.into())), + ), + HashBlake128(len) => Weight::from_ref_time( + s.hash_blake2_128 + .saturating_add(s.hash_blake2_128_per_byte.saturating_mul(len.into())), + ), + EcdsaRecovery => Weight::from_ref_time(s.ecdsa_recover), + ChainExtension(amount) => Weight::from_ref_time(amount), + CallRuntime(weight) => weight, + SetCodeHash => Weight::from_ref_time(s.set_code_hash), + EcdsaToEthAddress => Weight::from_ref_time(s.ecdsa_to_eth_address), + ReentrantCount => Weight::from_ref_time(s.reentrance_count), + AccountEntranceCount => Weight::from_ref_time(s.account_reentrance_count), + InstantationNonce => Weight::from_ref_time(s.instantiation_nonce), }; RuntimeToken { #[cfg(test)] _created_from: *self, - weight: Weight::from_ref_time(weight), + weight, } } } From 5da51fad0f8a3dcbe9f5986b2cfe23c5fd2afbc7 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 19 Dec 2022 18:19:13 +0200 Subject: [PATCH 02/10] improve docs --- frame/contracts/src/benchmarking/mod.rs | 2 +- frame/contracts/src/gas.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/contracts/src/benchmarking/mod.rs b/frame/contracts/src/benchmarking/mod.rs index 6b8701ac84d96..293056086f9a4 100644 --- a/frame/contracts/src/benchmarking/mod.rs +++ b/frame/contracts/src/benchmarking/mod.rs @@ -2928,7 +2928,7 @@ benchmarks! { // configured `Schedule` during benchmark development. // It can be outputed using the following command: // cargo run --manifest-path=bin/node/cli/Cargo.toml --release \ - // --features runtime-benchmarks -- benchmark --extra --dev --execution=native \ + // --features runtime-benchmarks -- benchmark pallet --extra --dev --execution=native \ // -p pallet_contracts -e print_schedule --no-median-slopes --no-min-squares #[extra] print_schedule { diff --git a/frame/contracts/src/gas.rs b/frame/contracts/src/gas.rs index c0cc2db2aa3eb..bf98dcd545fa2 100644 --- a/frame/contracts/src/gas.rs +++ b/frame/contracts/src/gas.rs @@ -157,8 +157,8 @@ where /// Returns `OutOfGas` if there is not enough gas or addition of the specified /// amount of gas has lead to overflow. On success returns `Proceed`. /// - /// NOTE that amount is always consumed, i.e. if there is not enough gas - /// then the counter will be set to zero. + /// NOTE that amount isn't consumed if there is not enough gas. This is considered + /// safe because we always charge gas before performing any resource-spending action. #[inline] pub fn charge>(&mut self, token: Tok) -> Result { #[cfg(test)] From d997429bb2e8a70836c7e514b178d44bad17afc4 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 19 Dec 2022 18:26:54 +0200 Subject: [PATCH 03/10] clearer naming and docs to compat_weight helper --- frame/contracts/src/lib.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/frame/contracts/src/lib.rs b/frame/contracts/src/lib.rs index b76acf9d1db08..953138a0634ba 100644 --- a/frame/contracts/src/lib.rs +++ b/frame/contracts/src/lib.rs @@ -391,7 +391,7 @@ pub mod pallet { { /// Deprecated version if [`Self::call`] for use in an in-storage `Call`. #[pallet::call_index(0)] - #[pallet::weight(T::WeightInfo::call().saturating_add(>::compat_weight(*gas_limit)))] + #[pallet::weight(T::WeightInfo::call().saturating_add(>::compat_weight_limit(*gas_limit)))] #[allow(deprecated)] #[deprecated(note = "1D weight is used in this extrinsic, please migrate to `call`")] pub fn call_old_weight( @@ -406,7 +406,7 @@ pub mod pallet { origin, dest, value, - >::compat_weight(gas_limit), + >::compat_weight_limit(gas_limit), storage_deposit_limit, data, ) @@ -416,7 +416,7 @@ pub mod pallet { #[pallet::call_index(1)] #[pallet::weight( T::WeightInfo::instantiate_with_code(code.len() as u32, salt.len() as u32) - .saturating_add(>::compat_weight(*gas_limit)) + .saturating_add(>::compat_weight_limit(*gas_limit)) )] #[allow(deprecated)] #[deprecated( @@ -434,7 +434,7 @@ pub mod pallet { Self::instantiate_with_code( origin, value, - >::compat_weight(gas_limit), + >::compat_weight_limit(gas_limit), storage_deposit_limit, code, data, @@ -445,7 +445,7 @@ pub mod pallet { /// Deprecated version if [`Self::instantiate`] for use in an in-storage `Call`. #[pallet::call_index(2)] #[pallet::weight( - T::WeightInfo::instantiate(salt.len() as u32).saturating_add(>::compat_weight(*gas_limit)) + T::WeightInfo::instantiate(salt.len() as u32).saturating_add(>::compat_weight_limit(*gas_limit)) )] #[allow(deprecated)] #[deprecated(note = "1D weight is used in this extrinsic, please migrate to `instantiate`")] @@ -461,7 +461,7 @@ pub mod pallet { Self::instantiate( origin, value, - >::compat_weight(gas_limit), + >::compat_weight_limit(gas_limit), storage_deposit_limit, code_hash, data, @@ -1231,11 +1231,11 @@ where >>::minimum_balance() } - /// Convert a 1D Weight to a 2D weight. + /// Convert gas_limit from 1D Weight to a 2D Weight. /// - /// Used by backwards compatible extrinsics. We cannot just set the proof to zero - /// or an old `Call` will just fail. - fn compat_weight(gas_limit: OldWeight) -> Weight { + /// Used by backwards compatible extrinsics. We cannot just set the proof_size weight limit to zero + /// or an old `Call` will just fail with OutOfGas. + fn compat_weight_limit(gas_limit: OldWeight) -> Weight { Weight::from(gas_limit).set_proof_size(u64::from(T::MaxCodeLen::get()) * 2) } } From e1139f478839bb795c9585f4c225658c9305bead Mon Sep 17 00:00:00 2001 From: Sasha Gryaznov Date: Fri, 23 Dec 2022 15:29:57 +0200 Subject: [PATCH 04/10] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Alexander Theißen --- frame/contracts/src/tests.rs | 2 +- frame/contracts/src/wasm/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index e25efcf9cd796..ea582ffe914e6 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -423,7 +423,7 @@ pub const BOB: AccountId32 = AccountId32::new([2u8; 32]); pub const CHARLIE: AccountId32 = AccountId32::new([3u8; 32]); pub const DJANGO: AccountId32 = AccountId32::new([4u8; 32]); -pub const GAS_LIMIT: Weight = Weight::from_ref_time(100_000_000_000).set_proof_size(512 * 1024); +pub const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 512 * 1024); pub struct ExtBuilder { existential_deposit: u64, diff --git a/frame/contracts/src/wasm/mod.rs b/frame/contracts/src/wasm/mod.rs index 9471d9d01027b..13fd28d7d05fe 100644 --- a/frame/contracts/src/wasm/mod.rs +++ b/frame/contracts/src/wasm/mod.rs @@ -432,7 +432,7 @@ mod tests { runtime_calls: Default::default(), schedule: Default::default(), gas_meter: GasMeter::new( - Weight::from_ref_time(10_000_000_000).set_proof_size(10 * 1024 * 1024), + Weight::from_parts(10_000_000_000, 10 * 1024 * 1024), ), debug_buffer: Default::default(), ecdsa_recover: Default::default(), From 78da4670aaafbecf9b367994b3c4ab2be96cd9e6 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 17 Jan 2023 16:23:52 +0200 Subject: [PATCH 05/10] save before master merge --- frame/contracts/src/schedule.rs | 296 +++++++++++++++++----------- frame/contracts/src/wasm/runtime.rs | 155 +++++++-------- 2 files changed, 245 insertions(+), 206 deletions(-) diff --git a/frame/contracts/src/schedule.rs b/frame/contracts/src/schedule.rs index 4f2d3b61d0176..26da75e524f35 100644 --- a/frame/contracts/src/schedule.rs +++ b/frame/contracts/src/schedule.rs @@ -21,7 +21,7 @@ use crate::{wasm::Determinism, weights::WeightInfo, Config}; use codec::{Decode, Encode}; -use frame_support::DefaultNoBound; +use frame_support::{DefaultNoBound, weights::Weight, }; use pallet_contracts_proc_macro::{ScheduleDebug, WeightDebug}; use scale_info::TypeInfo; #[cfg(feature = "std")] @@ -269,175 +269,178 @@ pub struct InstructionWeights { #[scale_info(skip_type_params(T))] pub struct HostFnWeights { /// Weight of calling `seal_caller`. - pub caller: u64, + pub caller: Weight, /// Weight of calling `seal_is_contract`. - pub is_contract: u64, + pub is_contract: Weight, /// Weight of calling `seal_code_hash`. - pub code_hash: u64, + pub code_hash: Weight, /// Weight of calling `seal_own_code_hash`. - pub own_code_hash: u64, + pub own_code_hash: Weight, /// Weight of calling `seal_caller_is_origin`. - pub caller_is_origin: u64, + pub caller_is_origin: Weight, /// Weight of calling `seal_address`. - pub address: u64, + pub address: Weight, /// Weight of calling `seal_gas_left`. - pub gas_left: u64, + pub gas_left: Weight, /// Weight of calling `seal_balance`. - pub balance: u64, + pub balance: Weight, /// Weight of calling `seal_value_transferred`. - pub value_transferred: u64, + pub value_transferred: Weight, /// Weight of calling `seal_minimum_balance`. - pub minimum_balance: u64, + pub minimum_balance: Weight, /// Weight of calling `seal_block_number`. - pub block_number: u64, + pub block_number: Weight, /// Weight of calling `seal_now`. - pub now: u64, + pub now: Weight, /// Weight of calling `seal_weight_to_fee`. - pub weight_to_fee: u64, + pub weight_to_fee: Weight, /// Weight of calling `gas`. - pub gas: u64, + pub gas: Weight, /// Weight of calling `seal_input`. - pub input: u64, + pub input: Weight, /// Weight per input byte copied to contract memory by `seal_input`. - pub input_per_byte: u64, + pub input_per_byte: Weight, /// Weight of calling `seal_return`. - pub r#return: u64, + pub r#return: Weight, /// Weight per byte returned through `seal_return`. - pub return_per_byte: u64, + pub return_per_byte: Weight, /// Weight of calling `seal_terminate`. - pub terminate: u64, + pub terminate: Weight, /// Weight of calling `seal_random`. - pub random: u64, + pub random: Weight, /// Weight of calling `seal_reposit_event`. - pub deposit_event: u64, + pub deposit_event: Weight, /// Weight per topic supplied to `seal_deposit_event`. - pub deposit_event_per_topic: u64, + pub deposit_event_per_topic: Weight, /// Weight per byte of an event deposited through `seal_deposit_event`. - pub deposit_event_per_byte: u64, + pub deposit_event_per_byte: Weight, /// Weight of calling `seal_debug_message`. - pub debug_message: u64, + pub debug_message: Weight, /// Weight of calling `seal_set_storage`. - pub set_storage: u64, + pub set_storage: Weight, /// Weight per written byten of an item stored with `seal_set_storage`. - pub set_storage_per_new_byte: u64, + pub set_storage_per_new_byte: Weight, /// Weight per overwritten byte of an item stored with `seal_set_storage`. - pub set_storage_per_old_byte: u64, + pub set_storage_per_old_byte: Weight, /// Weight of calling `seal_set_code_hash`. - pub set_code_hash: u64, + pub set_code_hash: Weight, /// Weight of calling `seal_clear_storage`. - pub clear_storage: u64, + pub clear_storage: Weight, /// Weight of calling `seal_clear_storage` per byte of the stored item. - pub clear_storage_per_byte: u64, + pub clear_storage_per_byte: Weight, /// Weight of calling `seal_contains_storage`. - pub contains_storage: u64, + pub contains_storage: Weight, /// Weight of calling `seal_contains_storage` per byte of the stored item. - pub contains_storage_per_byte: u64, + pub contains_storage_per_byte: Weight, /// Weight of calling `seal_get_storage`. - pub get_storage: u64, + pub get_storage: Weight, /// Weight per byte of an item received via `seal_get_storage`. - pub get_storage_per_byte: u64, + pub get_storage_per_byte: Weight, /// Weight of calling `seal_take_storage`. - pub take_storage: u64, + pub take_storage: Weight, /// Weight per byte of an item received via `seal_take_storage`. - pub take_storage_per_byte: u64, + pub take_storage_per_byte: Weight, /// Weight of calling `seal_transfer`. - pub transfer: u64, + pub transfer: Weight, /// Weight of calling `seal_call`. - pub call: u64, + pub call: Weight, /// Weight of calling `seal_delegate_call`. - pub delegate_call: u64, + pub delegate_call: Weight, /// Weight surcharge that is claimed if `seal_call` does a balance transfer. - pub call_transfer_surcharge: u64, + pub call_transfer_surcharge: Weight, /// Weight per byte that is cloned by supplying the `CLONE_INPUT` flag. - pub call_per_cloned_byte: u64, + pub call_per_cloned_byte: Weight, /// Weight of calling `seal_instantiate`. - pub instantiate: u64, + pub instantiate: Weight, /// Weight surcharge that is claimed if `seal_instantiate` does a balance transfer. - pub instantiate_transfer_surcharge: u64, + pub instantiate_transfer_surcharge: Weight, + + /// Weight per input byte supplied to `seal_instantiate`. + pub instantiate_per_input_byte: Weight, /// Weight per salt byte supplied to `seal_instantiate`. - pub instantiate_per_salt_byte: u64, + pub instantiate_per_salt_byte: Weight, /// Weight of calling `seal_hash_sha_256`. - pub hash_sha2_256: u64, + pub hash_sha2_256: Weight, /// Weight per byte hashed by `seal_hash_sha_256`. - pub hash_sha2_256_per_byte: u64, + pub hash_sha2_256_per_byte: Weight, /// Weight of calling `seal_hash_keccak_256`. - pub hash_keccak_256: u64, + pub hash_keccak_256: Weight, /// Weight per byte hashed by `seal_hash_keccak_256`. - pub hash_keccak_256_per_byte: u64, + pub hash_keccak_256_per_byte: Weight, /// Weight of calling `seal_hash_blake2_256`. - pub hash_blake2_256: u64, + pub hash_blake2_256: Weight, /// Weight per byte hashed by `seal_hash_blake2_256`. - pub hash_blake2_256_per_byte: u64, + pub hash_blake2_256_per_byte: Weight, /// Weight of calling `seal_hash_blake2_128`. - pub hash_blake2_128: u64, + pub hash_blake2_128: Weight, /// Weight per byte hashed by `seal_hash_blake2_128`. - pub hash_blake2_128_per_byte: u64, + pub hash_blake2_128_per_byte: Weight, /// Weight of calling `seal_ecdsa_recover`. - pub ecdsa_recover: u64, + pub ecdsa_recover: Weight, /// Weight of calling `seal_ecdsa_to_eth_address`. - pub ecdsa_to_eth_address: u64, + pub ecdsa_to_eth_address: Weight, /// Weight of calling `reentrance_count`. - pub reentrance_count: u64, + pub reentrance_count: Weight, /// Weight of calling `account_reentrance_count`. - pub account_reentrance_count: u64, + pub account_reentrance_count: Weight, /// Weight of calling `instantiation_nonce`. - pub instantiation_nonce: u64, + pub instantiation_nonce: Weight, /// The type parameter is used in the default implementation. #[codec(skip)] @@ -609,77 +612,134 @@ impl Default for InstructionWeights { } impl Default for HostFnWeights { + /// PoV should contain all trie nodes that are read during state transition (i.e. block + /// production). Hence we need to charge the `proof_size` weight for every host function which + /// reads storage, namely: + /// - get_storage, + /// - take_storage, + /// - contains_storage, + /// - clear_storage, + /// - set_storage. + /// + /// The last two functions write to storage, but they also do read storage in order to return + /// the size of the pre-existed value. Till we have PoV benchmarks implemented, we approximate + /// `proof_size` as being equal to the size of storage read. fn default() -> Self { Self { - caller: cost_batched!(seal_caller), - is_contract: cost_batched!(seal_is_contract), - code_hash: cost_batched!(seal_code_hash), - own_code_hash: cost_batched!(seal_own_code_hash), - caller_is_origin: cost_batched!(seal_caller_is_origin), - address: cost_batched!(seal_address), - gas_left: cost_batched!(seal_gas_left), - balance: cost_batched!(seal_balance), - value_transferred: cost_batched!(seal_value_transferred), - minimum_balance: cost_batched!(seal_minimum_balance), - block_number: cost_batched!(seal_block_number), - now: cost_batched!(seal_now), - weight_to_fee: cost_batched!(seal_weight_to_fee), - gas: cost_batched!(seal_gas), - input: cost_batched!(seal_input), - input_per_byte: cost_byte_batched!(seal_input_per_kb), - r#return: cost!(seal_return), - return_per_byte: cost_byte!(seal_return_per_kb), - terminate: cost!(seal_terminate), - random: cost_batched!(seal_random), - deposit_event: cost_batched!(seal_deposit_event), - deposit_event_per_topic: cost_batched_args!(seal_deposit_event_per_topic_and_kb, 1, 0), - deposit_event_per_byte: cost_byte_batched_args!( + caller: Weight::from_ref_time(cost_batched!(seal_caller)), + is_contract: Weight::from_ref_time(cost_batched!(seal_is_contract)), + code_hash: Weight::from_ref_time(cost_batched!(seal_code_hash)), + own_code_hash: Weight::from_ref_time(cost_batched!(seal_own_code_hash)), + caller_is_origin: Weight::from_ref_time(cost_batched!(seal_caller_is_origin)), + address: Weight::from_ref_time(cost_batched!(seal_address)), + gas_left: Weight::from_ref_time(cost_batched!(seal_gas_left)), + balance: Weight::from_ref_time(cost_batched!(seal_balance)), + value_transferred: Weight::from_ref_time(cost_batched!(seal_value_transferred)), + minimum_balance: Weight::from_ref_time(cost_batched!(seal_minimum_balance)), + block_number: Weight::from_ref_time(cost_batched!(seal_block_number)), + now: Weight::from_ref_time(cost_batched!(seal_now)), + weight_to_fee: Weight::from_ref_time(cost_batched!(seal_weight_to_fee)), + gas: Weight::from_ref_time(cost_batched!(seal_gas)), + input: Weight::from_ref_time(cost_batched!(seal_input)), + input_per_byte: Weight::from_ref_time(cost_byte_batched!(seal_input_per_kb)), + r#return: Weight::from_ref_time(cost!(seal_return)), + return_per_byte: Weight::from_ref_time(cost_byte!(seal_return_per_kb)), + terminate: Weight::from_ref_time(cost!(seal_terminate)), + random: Weight::from_ref_time(cost_batched!(seal_random)), + deposit_event: Weight::from_ref_time(cost_batched!(seal_deposit_event)), + deposit_event_per_topic: Weight::from_ref_time(cost_batched_args!( + seal_deposit_event_per_topic_and_kb, + 1, + 0 + )), + deposit_event_per_byte: Weight::from_ref_time(cost_byte_batched_args!( seal_deposit_event_per_topic_and_kb, 0, 1 - ), - debug_message: cost_batched!(seal_debug_message), - set_storage: cost_batched!(seal_set_storage), - set_code_hash: cost_batched!(seal_set_code_hash), - set_storage_per_new_byte: cost_byte_batched!(seal_set_storage_per_new_kb), - set_storage_per_old_byte: cost_byte_batched!(seal_set_storage_per_old_kb), - clear_storage: cost_batched!(seal_clear_storage), - clear_storage_per_byte: cost_byte_batched!(seal_clear_storage_per_kb), - contains_storage: cost_batched!(seal_contains_storage), - contains_storage_per_byte: cost_byte_batched!(seal_contains_storage_per_kb), - get_storage: cost_batched!(seal_get_storage), - get_storage_per_byte: cost_byte_batched!(seal_get_storage_per_kb), - take_storage: cost_batched!(seal_take_storage), - take_storage_per_byte: cost_byte_batched!(seal_take_storage_per_kb), - transfer: cost_batched!(seal_transfer), - call: cost_batched!(seal_call), - delegate_call: cost_batched!(seal_delegate_call), - call_transfer_surcharge: cost_batched_args!(seal_call_per_transfer_clone_kb, 1, 0), - call_per_cloned_byte: cost_batched_args!(seal_call_per_transfer_clone_kb, 0, 1), - instantiate: cost_batched!(seal_instantiate), - instantiate_transfer_surcharge: cost_byte_batched_args!( + )), + debug_message: Weight::from_ref_time(cost_batched!(seal_debug_message)), + set_storage: Weight::from_ref_time(cost_batched!(seal_set_storage)), + set_code_hash: Weight::from_ref_time(cost_batched!(seal_set_code_hash)), + set_storage_per_new_byte: Weight::from_ref_time(cost_byte_batched!( + seal_set_storage_per_new_kb + )), + set_storage_per_old_byte: Weight::from_ref_time(cost_byte_batched!( + seal_set_storage_per_old_kb + )) + .set_proof_size(1u64), + clear_storage: Weight::from_ref_time(cost_batched!(seal_clear_storage)), + clear_storage_per_byte: Weight::from_ref_time(cost_byte_batched!( + seal_clear_storage_per_kb + )) + .set_proof_size(1u64), + contains_storage: Weight::from_ref_time(cost_batched!(seal_contains_storage)), + contains_storage_per_byte: Weight::from_ref_time(cost_byte_batched!( + seal_contains_storage_per_kb + )) + .set_proof_size(1u64), + get_storage: Weight::from_ref_time(cost_batched!(seal_get_storage)), + get_storage_per_byte: Weight::from_ref_time(cost_byte_batched!( + seal_get_storage_per_kb + )) + .set_proof_size(1u64), + take_storage: Weight::from_ref_time(cost_batched!(seal_take_storage)), + take_storage_per_byte: Weight::from_ref_time(cost_byte_batched!( + seal_take_storage_per_kb + )) + .set_proof_size(1u64), + transfer: Weight::from_ref_time(cost_batched!(seal_transfer)), + call: Weight::from_ref_time(cost_batched!(seal_call)), + delegate_call: Weight::from_ref_time(cost_batched!(seal_delegate_call)), + call_transfer_surcharge: Weight::from_ref_time(cost_batched_args!( + seal_call_per_transfer_clone_kb, + 1, + 0 + )), + call_per_cloned_byte: Weight::from_ref_time(cost_batched_args!( + seal_call_per_transfer_clone_kb, + 0, + 1 + )), + instantiate: Weight::from_ref_time(cost_batched!(seal_instantiate)), + instantiate_transfer_surcharge: Weight::from_ref_time(cost_byte_batched_args!( seal_instantiate_per_transfer_salt_kb, 1, 0 - ), - instantiate_per_salt_byte: cost_byte_batched_args!( + )), + instantiate_per_input_byte: Weight::from_ref_time(cost_byte_batched_args!( + seal_instantiate_per_transfer_input_salt_kb, + 0, + 1, + 0 + )), + instantiate_per_salt_byte: Weight::from_ref_time(cost_byte_batched_args!( seal_instantiate_per_transfer_salt_kb, 0, 1 - ), - hash_sha2_256: cost_batched!(seal_hash_sha2_256), - hash_sha2_256_per_byte: cost_byte_batched!(seal_hash_sha2_256_per_kb), - hash_keccak_256: cost_batched!(seal_hash_keccak_256), - hash_keccak_256_per_byte: cost_byte_batched!(seal_hash_keccak_256_per_kb), - hash_blake2_256: cost_batched!(seal_hash_blake2_256), - hash_blake2_256_per_byte: cost_byte_batched!(seal_hash_blake2_256_per_kb), - hash_blake2_128: cost_batched!(seal_hash_blake2_128), - hash_blake2_128_per_byte: cost_byte_batched!(seal_hash_blake2_128_per_kb), - ecdsa_recover: cost_batched!(seal_ecdsa_recover), - ecdsa_to_eth_address: cost_batched!(seal_ecdsa_to_eth_address), - reentrance_count: cost_batched!(seal_reentrance_count), - account_reentrance_count: cost_batched!(seal_account_reentrance_count), - instantiation_nonce: cost_batched!(seal_instantiation_nonce), + )), + hash_sha2_256: Weight::from_ref_time(cost_batched!(seal_hash_sha2_256)), + hash_sha2_256_per_byte: Weight::from_ref_time(cost_byte_batched!( + seal_hash_sha2_256_per_kb + )), + hash_keccak_256: Weight::from_ref_time(cost_batched!(seal_hash_keccak_256)), + hash_keccak_256_per_byte: Weight::from_ref_time(cost_byte_batched!( + seal_hash_keccak_256_per_kb + )), + hash_blake2_256: Weight::from_ref_time(cost_batched!(seal_hash_blake2_256)), + hash_blake2_256_per_byte: Weight::from_ref_time(cost_byte_batched!( + seal_hash_blake2_256_per_kb + )), + hash_blake2_128: Weight::from_ref_time(cost_batched!(seal_hash_blake2_128)), + hash_blake2_128_per_byte: Weight::from_ref_time(cost_byte_batched!( + seal_hash_blake2_128_per_kb + )), + ecdsa_recover: Weight::from_ref_time(cost_batched!(seal_ecdsa_recover)), + ecdsa_to_eth_address: Weight::from_ref_time(cost_batched!(seal_ecdsa_to_eth_address)), + reentrance_count: Weight::from_ref_time(cost_batched!(seal_reentrance_count)), + account_reentrance_count: Weight::from_ref_time(cost_batched!( + seal_account_reentrance_count + )), + instantiation_nonce: Weight::from_ref_time(cost_batched!(seal_instantiation_nonce)), _phantom: PhantomData, } } diff --git a/frame/contracts/src/wasm/runtime.rs b/frame/contracts/src/wasm/runtime.rs index 1d11a9931f91d..341f3e1e713c3 100644 --- a/frame/contracts/src/wasm/runtime.rs +++ b/frame/contracts/src/wasm/runtime.rs @@ -277,97 +277,76 @@ impl RuntimeCosts { { use self::RuntimeCosts::*; let weight = match *self { - MeteringBlock(amount) => Weight::from_ref_time(s.gas.saturating_add(amount)), - CopyFromContract(len) => - Weight::from_ref_time(s.return_per_byte.saturating_mul(len.into())), - CopyToContract(len) => - Weight::from_ref_time(s.input_per_byte.saturating_mul(len.into())), - Caller => Weight::from_ref_time(s.caller), - IsContract => Weight::from_ref_time(s.is_contract), - CodeHash => Weight::from_ref_time(s.code_hash), - OwnCodeHash => Weight::from_ref_time(s.own_code_hash), - CallerIsOrigin => Weight::from_ref_time(s.caller_is_origin), - Address => Weight::from_ref_time(s.address), - GasLeft => Weight::from_ref_time(s.gas_left), - Balance => Weight::from_ref_time(s.balance), - ValueTransferred => Weight::from_ref_time(s.value_transferred), - MinimumBalance => Weight::from_ref_time(s.minimum_balance), - BlockNumber => Weight::from_ref_time(s.block_number), - Now => Weight::from_ref_time(s.now), - WeightToFee => Weight::from_ref_time(s.weight_to_fee), - InputBase => Weight::from_ref_time(s.input), - Return(len) => Weight::from_ref_time( - s.r#return.saturating_add(s.return_per_byte.saturating_mul(len.into())), - ), - Terminate => Weight::from_ref_time(s.terminate), - Random => Weight::from_ref_time(s.random), - DepositEvent { num_topic, len } => Weight::from_ref_time( - s.deposit_event - .saturating_add(s.deposit_event_per_topic.saturating_mul(num_topic.into())) - .saturating_add(s.deposit_event_per_byte.saturating_mul(len.into())), - ), - DebugMessage => Weight::from_ref_time(s.debug_message), - SetStorage { new_bytes, old_bytes } => Weight::from_ref_time( - s.set_storage - .saturating_add(s.set_storage_per_new_byte.saturating_mul(new_bytes.into())) - .saturating_add(s.set_storage_per_old_byte.saturating_mul(old_bytes.into())), - ) - .set_proof_size(old_bytes.into()), - ClearStorage(len) => Weight::from_ref_time( - s.clear_storage - .saturating_add(s.clear_storage_per_byte.saturating_mul(len.into())), - ) - .set_proof_size(len.into()), - ContainsStorage(len) => Weight::from_ref_time( - s.contains_storage - .saturating_add(s.contains_storage_per_byte.saturating_mul(len.into())), - ) - .set_proof_size(len.into()), - GetStorage(len) => Weight::from_ref_time( + MeteringBlock(amount) => s.gas.saturating_add(Weight::from_ref_time(amount)), + CopyFromContract(len) => s.return_per_byte.saturating_mul(len.into()), + CopyToContract(len) => s.input_per_byte.saturating_mul(len.into()), + Caller => s.caller, + IsContract => s.is_contract, + CodeHash => s.code_hash, + OwnCodeHash => s.own_code_hash, + CallerIsOrigin => s.caller_is_origin, + Address => s.address, + GasLeft => s.gas_left, + Balance => s.balance, + ValueTransferred => s.value_transferred, + MinimumBalance => s.minimum_balance, + BlockNumber => s.block_number, + Now => s.now, + WeightToFee => s.weight_to_fee, + InputBase => s.input, + Return(len) => s.r#return.saturating_add(s.return_per_byte.saturating_mul(len.into())), + Terminate => s.terminate, + Random => s.random, + DepositEvent { num_topic, len } => s + .deposit_event + .saturating_add(s.deposit_event_per_topic.saturating_mul(num_topic.into())) + .saturating_add(s.deposit_event_per_byte.saturating_mul(len.into())), + DebugMessage => s.debug_message, + SetStorage { new_bytes, old_bytes } => s + .set_storage + .saturating_add(s.set_storage_per_new_byte.saturating_mul(new_bytes.into())) + .saturating_add(s.set_storage_per_old_byte.saturating_mul(old_bytes.into())), + ClearStorage(len) => s + .clear_storage + .saturating_add(s.clear_storage_per_byte.saturating_mul(len.into())), + ContainsStorage(len) => s + .contains_storage + .saturating_add(s.contains_storage_per_byte.saturating_mul(len.into())), + GetStorage(len) => s.get_storage.saturating_add(s.get_storage_per_byte.saturating_mul(len.into())), - ) - .set_proof_size(len.into()), - TakeStorage(len) => Weight::from_ref_time( - s.take_storage - .saturating_add(s.take_storage_per_byte.saturating_mul(len.into())), - ) - .set_proof_size(len.into()), - Transfer => Weight::from_ref_time(s.transfer), - CallBase => Weight::from_ref_time(s.call), - DelegateCallBase => Weight::from_ref_time(s.delegate_call), - CallSurchargeTransfer => Weight::from_ref_time(s.call_transfer_surcharge), - CallInputCloned(len) => - Weight::from_ref_time(s.call_per_cloned_byte.saturating_mul(len.into())), - InstantiateBase { input_data_len, salt_len } => Weight::from_ref_time( - s.instantiate - .saturating_add(s.return_per_byte.saturating_mul(input_data_len.into())) - .saturating_add(s.instantiate_per_salt_byte.saturating_mul(salt_len.into())), - ), - InstantiateSurchargeTransfer => Weight::from_ref_time(s.instantiate_transfer_surcharge), - HashSha256(len) => Weight::from_ref_time( - s.hash_sha2_256 - .saturating_add(s.hash_sha2_256_per_byte.saturating_mul(len.into())), - ), - HashKeccak256(len) => Weight::from_ref_time( - s.hash_keccak_256 - .saturating_add(s.hash_keccak_256_per_byte.saturating_mul(len.into())), - ), - HashBlake256(len) => Weight::from_ref_time( - s.hash_blake2_256 - .saturating_add(s.hash_blake2_256_per_byte.saturating_mul(len.into())), - ), - HashBlake128(len) => Weight::from_ref_time( - s.hash_blake2_128 - .saturating_add(s.hash_blake2_128_per_byte.saturating_mul(len.into())), - ), - EcdsaRecovery => Weight::from_ref_time(s.ecdsa_recover), + TakeStorage(len) => s + .take_storage + .saturating_add(s.take_storage_per_byte.saturating_mul(len.into())), + Transfer => s.transfer, + CallBase => s.call, + DelegateCallBase => s.delegate_call, + CallSurchargeTransfer => s.call_transfer_surcharge, + CallInputCloned(len) => s.call_per_cloned_byte.saturating_mul(len.into()), + InstantiateBase { input_data_len, salt_len } => s + .instantiate + .saturating_add(s.instantiate_per_input_byte.saturating_mul(input_data_len.into())) + .saturating_add(s.instantiate_per_salt_byte.saturating_mul(salt_len.into())), + InstantiateSurchargeTransfer => s.instantiate_transfer_surcharge, + HashSha256(len) => s + .hash_sha2_256 + .saturating_add(s.hash_sha2_256_per_byte.saturating_mul(len.into())), + HashKeccak256(len) => s + .hash_keccak_256 + .saturating_add(s.hash_keccak_256_per_byte.saturating_mul(len.into())), + HashBlake256(len) => s + .hash_blake2_256 + .saturating_add(s.hash_blake2_256_per_byte.saturating_mul(len.into())), + HashBlake128(len) => s + .hash_blake2_128 + .saturating_add(s.hash_blake2_128_per_byte.saturating_mul(len.into())), + EcdsaRecovery => s.ecdsa_recover, ChainExtension(amount) => Weight::from_ref_time(amount), CallRuntime(weight) => weight, - SetCodeHash => Weight::from_ref_time(s.set_code_hash), - EcdsaToEthAddress => Weight::from_ref_time(s.ecdsa_to_eth_address), - ReentrantCount => Weight::from_ref_time(s.reentrance_count), - AccountEntranceCount => Weight::from_ref_time(s.account_reentrance_count), - InstantationNonce => Weight::from_ref_time(s.instantiation_nonce), + SetCodeHash => s.set_code_hash, + EcdsaToEthAddress => s.ecdsa_to_eth_address, + ReentrantCount => s.reentrance_count, + AccountEntranceCount => s.account_reentrance_count, + InstantationNonce => s.instantiation_nonce, }; RuntimeToken { #[cfg(test)] From 7c4c3257fbe1c4981202194a322eac8185fc767b Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 17 Jan 2023 17:34:00 +0200 Subject: [PATCH 06/10] HostFnWeights to Weight --- frame/contracts/proc-macro/src/lib.rs | 14 +++++++------- frame/contracts/src/lib.rs | 4 ++-- frame/contracts/src/schedule.rs | 9 +++------ frame/contracts/src/wasm/mod.rs | 4 +--- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/frame/contracts/proc-macro/src/lib.rs b/frame/contracts/proc-macro/src/lib.rs index a31d39f47e215..d0247d720a39a 100644 --- a/frame/contracts/proc-macro/src/lib.rs +++ b/frame/contracts/proc-macro/src/lib.rs @@ -122,23 +122,23 @@ fn iterate_fields(data: &syn::DataStruct, fmt: impl Fn(&Ident) -> TokenStream2) fn format_weight(field: &Ident) -> TokenStream2 { quote_spanned! { field.span() => - &if self.#field > 1_000_000_000 { + &if self.#field.ref_time() > 1_000_000_000 { format!( "{:.1?} ms", - Fixed::saturating_from_rational(self.#field, 1_000_000_000).to_float() + Fixed::saturating_from_rational(self.#field.ref_time(), 1_000_000_000).to_float() ) - } else if self.#field > 1_000_000 { + } else if self.#field.ref_time() > 1_000_000 { format!( "{:.1?} µs", - Fixed::saturating_from_rational(self.#field, 1_000_000).to_float() + Fixed::saturating_from_rational(self.#field.ref_time(), 1_000_000).to_float() ) - } else if self.#field > 1_000 { + } else if self.#field.ref_time() > 1_000 { format!( "{:.1?} ns", - Fixed::saturating_from_rational(self.#field, 1_000).to_float() + Fixed::saturating_from_rational(self.#field.ref_time(), 1_000).to_float() ) } else { - format!("{} ps", self.#field) + format!("{} ps", self.#field.ref_time()) } } } diff --git a/frame/contracts/src/lib.rs b/frame/contracts/src/lib.rs index 0f365fca4d4b7..58c3af16f3637 100644 --- a/frame/contracts/src/lib.rs +++ b/frame/contracts/src/lib.rs @@ -1218,8 +1218,8 @@ impl Pallet { /// Convert gas_limit from 1D Weight to a 2D Weight. /// - /// Used by backwards compatible extrinsics. We cannot just set the proof_size weight limit to zero - /// or an old `Call` will just fail with OutOfGas. + /// Used by backwards compatible extrinsics. We cannot just set the proof_size weight limit to + /// zero or an old `Call` will just fail with OutOfGas. fn compat_weight_limit(gas_limit: OldWeight) -> Weight { Weight::from(gas_limit).set_proof_size(u64::from(T::MaxCodeLen::get()) * 2) } diff --git a/frame/contracts/src/schedule.rs b/frame/contracts/src/schedule.rs index 7707657312668..750bcb59800de 100644 --- a/frame/contracts/src/schedule.rs +++ b/frame/contracts/src/schedule.rs @@ -21,7 +21,7 @@ use crate::{wasm::Determinism, weights::WeightInfo, Config}; use codec::{Decode, Encode}; -use frame_support::{DefaultNoBound, weights::Weight, }; +use frame_support::{weights::Weight, DefaultNoBound}; use pallet_contracts_proc_macro::{ScheduleDebug, WeightDebug}; use scale_info::TypeInfo; #[cfg(feature = "std")] @@ -169,7 +169,7 @@ impl Limits { /// that use them as supporting instructions. Supporting means mainly pushing arguments /// and dropping return values in order to maintain a valid module. #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[derive(Clone, Encode, Decode, PartialEq, Eq, WeightDebug, TypeInfo)] +#[derive(Clone, Encode, Decode, PartialEq, Eq, ScheduleDebug, TypeInfo)] #[scale_info(skip_type_params(T))] pub struct InstructionWeights { /// Version of the instruction weights. @@ -386,9 +386,6 @@ pub struct HostFnWeights { /// Weight per input byte supplied to `seal_instantiate`. pub instantiate_per_input_byte: Weight, - /// Weight per input byte supplied to `seal_instantiate`. - pub instantiate_per_input_byte: u64, - /// Weight per salt byte supplied to `seal_instantiate`. pub instantiate_per_salt_byte: Weight, @@ -692,7 +689,7 @@ impl Default for HostFnWeights { seal_instantiate_per_transfer_input_salt_kb, 1, 0, - 0, + 0 )), instantiate_per_input_byte: Weight::from_ref_time(cost_byte_batched_args!( seal_instantiate_per_transfer_input_salt_kb, diff --git a/frame/contracts/src/wasm/mod.rs b/frame/contracts/src/wasm/mod.rs index 5bf6aa050a998..c2b0611769429 100644 --- a/frame/contracts/src/wasm/mod.rs +++ b/frame/contracts/src/wasm/mod.rs @@ -430,9 +430,7 @@ mod tests { events: Default::default(), runtime_calls: Default::default(), schedule: Default::default(), - gas_meter: GasMeter::new( - Weight::from_parts(10_000_000_000, 10 * 1024 * 1024), - ), + gas_meter: GasMeter::new(Weight::from_parts(10_000_000_000, 10 * 1024 * 1024)), debug_buffer: Default::default(), ecdsa_recover: Default::default(), } From 7d5e5eb1ae653f3c23f4fe3e82dcaa3dbada029f Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 17 Jan 2023 18:14:14 +0200 Subject: [PATCH 07/10] added to_weight! macro --- frame/contracts/src/schedule.rs | 155 +++++++++++++++----------------- 1 file changed, 70 insertions(+), 85 deletions(-) diff --git a/frame/contracts/src/schedule.rs b/frame/contracts/src/schedule.rs index 750bcb59800de..9ef09a3bb0765 100644 --- a/frame/contracts/src/schedule.rs +++ b/frame/contracts/src/schedule.rs @@ -514,6 +514,12 @@ macro_rules! cost_byte_batched { }; } +macro_rules! to_weight { + ($ref_time:expr $(, $proof_time:expr )?) => { + Weight::from_ref_time($ref_time)$(.set_proof_size($proof_time))? + }; +} + impl Default for Limits { fn default() -> Self { Self { @@ -610,122 +616,101 @@ impl Default for HostFnWeights { /// `proof_size` as being equal to the size of storage read. fn default() -> Self { Self { - caller: Weight::from_ref_time(cost_batched!(seal_caller)), - is_contract: Weight::from_ref_time(cost_batched!(seal_is_contract)), - code_hash: Weight::from_ref_time(cost_batched!(seal_code_hash)), - own_code_hash: Weight::from_ref_time(cost_batched!(seal_own_code_hash)), - caller_is_origin: Weight::from_ref_time(cost_batched!(seal_caller_is_origin)), - address: Weight::from_ref_time(cost_batched!(seal_address)), - gas_left: Weight::from_ref_time(cost_batched!(seal_gas_left)), - balance: Weight::from_ref_time(cost_batched!(seal_balance)), - value_transferred: Weight::from_ref_time(cost_batched!(seal_value_transferred)), - minimum_balance: Weight::from_ref_time(cost_batched!(seal_minimum_balance)), - block_number: Weight::from_ref_time(cost_batched!(seal_block_number)), - now: Weight::from_ref_time(cost_batched!(seal_now)), - weight_to_fee: Weight::from_ref_time(cost_batched!(seal_weight_to_fee)), - gas: Weight::from_ref_time(cost_batched!(seal_gas)), - input: Weight::from_ref_time(cost_batched!(seal_input)), - input_per_byte: Weight::from_ref_time(cost_byte_batched!(seal_input_per_kb)), - r#return: Weight::from_ref_time(cost!(seal_return)), - return_per_byte: Weight::from_ref_time(cost_byte!(seal_return_per_kb)), - terminate: Weight::from_ref_time(cost!(seal_terminate)), - random: Weight::from_ref_time(cost_batched!(seal_random)), - deposit_event: Weight::from_ref_time(cost_batched!(seal_deposit_event)), - deposit_event_per_topic: Weight::from_ref_time(cost_batched_args!( + caller: to_weight!(cost_batched!(seal_caller)), + is_contract: to_weight!(cost_batched!(seal_is_contract)), + code_hash: to_weight!(cost_batched!(seal_code_hash)), + own_code_hash: to_weight!(cost_batched!(seal_own_code_hash)), + caller_is_origin: to_weight!(cost_batched!(seal_caller_is_origin)), + address: to_weight!(cost_batched!(seal_address)), + gas_left: to_weight!(cost_batched!(seal_gas_left)), + balance: to_weight!(cost_batched!(seal_balance)), + value_transferred: to_weight!(cost_batched!(seal_value_transferred)), + minimum_balance: to_weight!(cost_batched!(seal_minimum_balance)), + block_number: to_weight!(cost_batched!(seal_block_number)), + now: to_weight!(cost_batched!(seal_now)), + weight_to_fee: to_weight!(cost_batched!(seal_weight_to_fee)), + gas: to_weight!(cost_batched!(seal_gas)), + input: to_weight!(cost_batched!(seal_input)), + input_per_byte: to_weight!(cost_byte_batched!(seal_input_per_kb)), + r#return: to_weight!(cost!(seal_return)), + return_per_byte: to_weight!(cost_byte!(seal_return_per_kb)), + terminate: to_weight!(cost!(seal_terminate)), + random: to_weight!(cost_batched!(seal_random)), + deposit_event: to_weight!(cost_batched!(seal_deposit_event)), + deposit_event_per_topic: to_weight!(cost_batched_args!( seal_deposit_event_per_topic_and_kb, 1, 0 )), - deposit_event_per_byte: Weight::from_ref_time(cost_byte_batched_args!( + deposit_event_per_byte: to_weight!(cost_byte_batched_args!( seal_deposit_event_per_topic_and_kb, 0, 1 )), - debug_message: Weight::from_ref_time(cost_batched!(seal_debug_message)), - set_storage: Weight::from_ref_time(cost_batched!(seal_set_storage)), - set_code_hash: Weight::from_ref_time(cost_batched!(seal_set_code_hash)), - set_storage_per_new_byte: Weight::from_ref_time(cost_byte_batched!( - seal_set_storage_per_new_kb - )), - set_storage_per_old_byte: Weight::from_ref_time(cost_byte_batched!( - seal_set_storage_per_old_kb - )) - .set_proof_size(1u64), - clear_storage: Weight::from_ref_time(cost_batched!(seal_clear_storage)), - clear_storage_per_byte: Weight::from_ref_time(cost_byte_batched!( - seal_clear_storage_per_kb - )) - .set_proof_size(1u64), - contains_storage: Weight::from_ref_time(cost_batched!(seal_contains_storage)), - contains_storage_per_byte: Weight::from_ref_time(cost_byte_batched!( - seal_contains_storage_per_kb - )) - .set_proof_size(1u64), - get_storage: Weight::from_ref_time(cost_batched!(seal_get_storage)), - get_storage_per_byte: Weight::from_ref_time(cost_byte_batched!( - seal_get_storage_per_kb - )) - .set_proof_size(1u64), - take_storage: Weight::from_ref_time(cost_batched!(seal_take_storage)), - take_storage_per_byte: Weight::from_ref_time(cost_byte_batched!( - seal_take_storage_per_kb - )) - .set_proof_size(1u64), - transfer: Weight::from_ref_time(cost_batched!(seal_transfer)), - call: Weight::from_ref_time(cost_batched!(seal_call)), - delegate_call: Weight::from_ref_time(cost_batched!(seal_delegate_call)), - call_transfer_surcharge: Weight::from_ref_time(cost_batched_args!( + debug_message: to_weight!(cost_batched!(seal_debug_message)), + set_storage: to_weight!(cost_batched!(seal_set_storage)), + set_code_hash: to_weight!(cost_batched!(seal_set_code_hash)), + set_storage_per_new_byte: to_weight!(cost_byte_batched!(seal_set_storage_per_new_kb)), + set_storage_per_old_byte: to_weight!( + cost_byte_batched!(seal_set_storage_per_old_kb), + 1u64 + ), + clear_storage: to_weight!(cost_batched!(seal_clear_storage)), + clear_storage_per_byte: to_weight!(cost_byte_batched!(seal_clear_storage_per_kb), 1u64), + contains_storage: to_weight!(cost_batched!(seal_contains_storage)), + contains_storage_per_byte: to_weight!( + cost_byte_batched!(seal_contains_storage_per_kb), + 1u64 + ), + get_storage: to_weight!(cost_batched!(seal_get_storage)), + get_storage_per_byte: to_weight!(cost_byte_batched!(seal_get_storage_per_kb), 1u64), + take_storage: to_weight!(cost_batched!(seal_take_storage)), + take_storage_per_byte: to_weight!(cost_byte_batched!(seal_take_storage_per_kb), 1u64), + transfer: to_weight!(cost_batched!(seal_transfer)), + call: to_weight!(cost_batched!(seal_call)), + delegate_call: to_weight!(cost_batched!(seal_delegate_call)), + call_transfer_surcharge: to_weight!(cost_batched_args!( seal_call_per_transfer_clone_kb, 1, 0 )), - call_per_cloned_byte: Weight::from_ref_time(cost_batched_args!( + call_per_cloned_byte: to_weight!(cost_batched_args!( seal_call_per_transfer_clone_kb, 0, 1 )), - instantiate: Weight::from_ref_time(cost_batched!(seal_instantiate)), - instantiate_transfer_surcharge: Weight::from_ref_time(cost_byte_batched_args!( + instantiate: to_weight!(cost_batched!(seal_instantiate)), + instantiate_transfer_surcharge: to_weight!(cost_byte_batched_args!( seal_instantiate_per_transfer_input_salt_kb, 1, 0, 0 )), - instantiate_per_input_byte: Weight::from_ref_time(cost_byte_batched_args!( + instantiate_per_input_byte: to_weight!(cost_byte_batched_args!( seal_instantiate_per_transfer_input_salt_kb, 0, 1, 0 )), - instantiate_per_salt_byte: Weight::from_ref_time(cost_byte_batched_args!( + instantiate_per_salt_byte: to_weight!(cost_byte_batched_args!( seal_instantiate_per_transfer_input_salt_kb, 0, 0, 1 )), - hash_sha2_256: Weight::from_ref_time(cost_batched!(seal_hash_sha2_256)), - hash_sha2_256_per_byte: Weight::from_ref_time(cost_byte_batched!( - seal_hash_sha2_256_per_kb - )), - hash_keccak_256: Weight::from_ref_time(cost_batched!(seal_hash_keccak_256)), - hash_keccak_256_per_byte: Weight::from_ref_time(cost_byte_batched!( - seal_hash_keccak_256_per_kb - )), - hash_blake2_256: Weight::from_ref_time(cost_batched!(seal_hash_blake2_256)), - hash_blake2_256_per_byte: Weight::from_ref_time(cost_byte_batched!( - seal_hash_blake2_256_per_kb - )), - hash_blake2_128: Weight::from_ref_time(cost_batched!(seal_hash_blake2_128)), - hash_blake2_128_per_byte: Weight::from_ref_time(cost_byte_batched!( - seal_hash_blake2_128_per_kb - )), - ecdsa_recover: Weight::from_ref_time(cost_batched!(seal_ecdsa_recover)), - ecdsa_to_eth_address: Weight::from_ref_time(cost_batched!(seal_ecdsa_to_eth_address)), - reentrance_count: Weight::from_ref_time(cost_batched!(seal_reentrance_count)), - account_reentrance_count: Weight::from_ref_time(cost_batched!( - seal_account_reentrance_count - )), - instantiation_nonce: Weight::from_ref_time(cost_batched!(seal_instantiation_nonce)), + hash_sha2_256: to_weight!(cost_batched!(seal_hash_sha2_256)), + hash_sha2_256_per_byte: to_weight!(cost_byte_batched!(seal_hash_sha2_256_per_kb)), + hash_keccak_256: to_weight!(cost_batched!(seal_hash_keccak_256)), + hash_keccak_256_per_byte: to_weight!(cost_byte_batched!(seal_hash_keccak_256_per_kb)), + hash_blake2_256: to_weight!(cost_batched!(seal_hash_blake2_256)), + hash_blake2_256_per_byte: to_weight!(cost_byte_batched!(seal_hash_blake2_256_per_kb)), + hash_blake2_128: to_weight!(cost_batched!(seal_hash_blake2_128)), + hash_blake2_128_per_byte: to_weight!(cost_byte_batched!(seal_hash_blake2_128_per_kb)), + ecdsa_recover: to_weight!(cost_batched!(seal_ecdsa_recover)), + ecdsa_to_eth_address: to_weight!(cost_batched!(seal_ecdsa_to_eth_address)), + reentrance_count: to_weight!(cost_batched!(seal_reentrance_count)), + account_reentrance_count: to_weight!(cost_batched!(seal_account_reentrance_count)), + instantiation_nonce: to_weight!(cost_batched!(seal_instantiation_nonce)), _phantom: PhantomData, } } From d009dae49d501cda51802e1ae7315aa7154adca8 Mon Sep 17 00:00:00 2001 From: Sasha Gryaznov Date: Tue, 17 Jan 2023 21:03:31 +0200 Subject: [PATCH 08/10] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Alexander Theißen --- frame/contracts/src/lib.rs | 2 +- frame/contracts/src/schedule.rs | 4 ++-- frame/contracts/src/tests.rs | 4 +--- frame/contracts/src/wasm/runtime.rs | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/frame/contracts/src/lib.rs b/frame/contracts/src/lib.rs index 58c3af16f3637..f13b9ad3f8b61 100644 --- a/frame/contracts/src/lib.rs +++ b/frame/contracts/src/lib.rs @@ -1221,7 +1221,7 @@ impl Pallet { /// Used by backwards compatible extrinsics. We cannot just set the proof_size weight limit to /// zero or an old `Call` will just fail with OutOfGas. fn compat_weight_limit(gas_limit: OldWeight) -> Weight { - Weight::from(gas_limit).set_proof_size(u64::from(T::MaxCodeLen::get()) * 2) + Weight::from_parts(gas_limit, u64::from(T::MaxCodeLen::get()) * 2) } } diff --git a/frame/contracts/src/schedule.rs b/frame/contracts/src/schedule.rs index 9ef09a3bb0765..dabdc70135916 100644 --- a/frame/contracts/src/schedule.rs +++ b/frame/contracts/src/schedule.rs @@ -515,8 +515,8 @@ macro_rules! cost_byte_batched { } macro_rules! to_weight { - ($ref_time:expr $(, $proof_time:expr )?) => { - Weight::from_ref_time($ref_time)$(.set_proof_size($proof_time))? + ($ref_time:expr $(, $proof_size:expr )?) => { + Weight::from_ref_time($ref_time)$(.set_proof_size($proof_size))? }; } diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 8c1ed1f226cce..b9462ce45ec2f 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -2837,9 +2837,7 @@ fn gas_estimation_call_runtime() { let call = RuntimeCall::Contracts(crate::Call::call { dest: addr_callee, value: 0, - gas_limit: GAS_LIMIT - .set_ref_time(GAS_LIMIT.ref_time() / 3) - .set_proof_size(GAS_LIMIT.proof_size() / 3), + gas_limit: GAS_LIMIT / 3, storage_deposit_limit: None, data: vec![], }); diff --git a/frame/contracts/src/wasm/runtime.rs b/frame/contracts/src/wasm/runtime.rs index 890700b45dfc8..4b2e4da7b8e3a 100644 --- a/frame/contracts/src/wasm/runtime.rs +++ b/frame/contracts/src/wasm/runtime.rs @@ -335,7 +335,7 @@ impl RuntimeCosts { .hash_blake2_128 .saturating_add(s.hash_blake2_128_per_byte.saturating_mul(len.into())), EcdsaRecovery => s.ecdsa_recover, - ChainExtension(amount) => Weight::from_ref_time(amount), + ChainExtension(weight) => Weight::from_ref_time(weight), CallRuntime(weight) => weight, SetCodeHash => s.set_code_hash, EcdsaToEthAddress => s.ecdsa_to_eth_address, From 9df1be5eb8da4473a7c8df2a9f3e28c4f2ad1e49 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 17 Jan 2023 21:04:07 +0200 Subject: [PATCH 09/10] RuntimeCosts::ChainExtension to weight_v2 --- frame/contracts/src/wasm/runtime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/contracts/src/wasm/runtime.rs b/frame/contracts/src/wasm/runtime.rs index 4b2e4da7b8e3a..bac14e5f851de 100644 --- a/frame/contracts/src/wasm/runtime.rs +++ b/frame/contracts/src/wasm/runtime.rs @@ -253,7 +253,7 @@ pub enum RuntimeCosts { /// Weight of calling `seal_ecdsa_recover`. EcdsaRecovery, /// Weight charged by a chain extension through `seal_call_chain_extension`. - ChainExtension(u64), + ChainExtension(Weight), /// Weight charged for calling into the runtime. CallRuntime(Weight), /// Weight of calling `seal_set_code_hash` From 79b1ef18688f3222f7a52869c7bdf5b6c183ecca Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 17 Jan 2023 21:13:22 +0200 Subject: [PATCH 10/10] chain extension to weight v2 --- frame/contracts/src/chain_extension.rs | 7 +++---- frame/contracts/src/lib.rs | 2 +- frame/contracts/src/wasm/runtime.rs | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/frame/contracts/src/chain_extension.rs b/frame/contracts/src/chain_extension.rs index dfa9c7c2de268..3974bdba30d14 100644 --- a/frame/contracts/src/chain_extension.rs +++ b/frame/contracts/src/chain_extension.rs @@ -227,7 +227,7 @@ impl<'a, 'b, E: Ext, S: State> Environment<'a, 'b, E, S> { /// /// Weight is synonymous with gas in substrate. pub fn charge_weight(&mut self, amount: Weight) -> Result { - self.inner.runtime.charge_gas(RuntimeCosts::ChainExtension(amount.ref_time())) + self.inner.runtime.charge_gas(RuntimeCosts::ChainExtension(amount)) } /// Adjust a previously charged amount down to its actual amount. @@ -237,7 +237,7 @@ impl<'a, 'b, E: Ext, S: State> Environment<'a, 'b, E, S> { pub fn adjust_weight(&mut self, charged: ChargedAmount, actual_weight: Weight) { self.inner .runtime - .adjust_gas(charged, RuntimeCosts::ChainExtension(actual_weight.ref_time())) + .adjust_gas(charged, RuntimeCosts::ChainExtension(actual_weight)) } /// Grants access to the execution environment of the current contract call. @@ -408,8 +408,7 @@ impl<'a, 'b, E: Ext, S: BufOut> Environment<'a, 'b, E, S> { buffer, allow_skip, |len| { - weight_per_byte - .map(|w| RuntimeCosts::ChainExtension(w.ref_time().saturating_mul(len.into()))) + weight_per_byte.map(|w| RuntimeCosts::ChainExtension(w.saturating_mul(len.into()))) }, ) } diff --git a/frame/contracts/src/lib.rs b/frame/contracts/src/lib.rs index f13b9ad3f8b61..581798e4488d6 100644 --- a/frame/contracts/src/lib.rs +++ b/frame/contracts/src/lib.rs @@ -1221,7 +1221,7 @@ impl Pallet { /// Used by backwards compatible extrinsics. We cannot just set the proof_size weight limit to /// zero or an old `Call` will just fail with OutOfGas. fn compat_weight_limit(gas_limit: OldWeight) -> Weight { - Weight::from_parts(gas_limit, u64::from(T::MaxCodeLen::get()) * 2) + Weight::from_parts(gas_limit.0, u64::from(T::MaxCodeLen::get()) * 2) } } diff --git a/frame/contracts/src/wasm/runtime.rs b/frame/contracts/src/wasm/runtime.rs index bac14e5f851de..745583b337653 100644 --- a/frame/contracts/src/wasm/runtime.rs +++ b/frame/contracts/src/wasm/runtime.rs @@ -335,7 +335,7 @@ impl RuntimeCosts { .hash_blake2_128 .saturating_add(s.hash_blake2_128_per_byte.saturating_mul(len.into())), EcdsaRecovery => s.ecdsa_recover, - ChainExtension(weight) => Weight::from_ref_time(weight), + ChainExtension(weight) => weight, CallRuntime(weight) => weight, SetCodeHash => s.set_code_hash, EcdsaToEthAddress => s.ecdsa_to_eth_address,