Add storage size component to weights (#12277)

* Add storage size component to weights

* Rename storage_size to proof_size

* Update primitives/weights/src/weight_v2.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fixes

* cargo fmt

* Implement custom Decode and CompactAs

* Add missing import

* Fixes

* Remove CompactAs implementation

* Properly migrate from 1D weight

* Remove #[pallet::compact] from Weight parameters

* More #[pallet::compact] removals

* Add unit tests

* Set appropriate default block proof size

* cargo fmt

* Remove nonsensical weight constant

* Test only for the reference time weight in frame_system::limits

* Only check for reference time weight on idle

* Use destructuring syntax

* Update test expectations

* Fixes

* Fixes

* Fixes

* Correctly migrate from 1D weights

* cargo fmt

* Migrate using extra extrinsics instead of custom Decode

* Fixes

* Silence dispatch call warnings that were previously allowed

* Fix gas_left test

* Use OldWeight instead of u64

* Fixes

* Only check for reference time weight in election provider

* Fix test expectations

* Fix test expectations

* Use only reference time weight in grandpa test

* Use only reference time weight in examples test

* Use only reference time weight in examples test

* Fix test expectations

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Alexander Theißen <alex.theissen@me.com>
This commit is contained in:
Keith Yeung
2022-09-28 18:21:53 +08:00
committed by GitHub
parent 9d41de645f
commit 0327258566
20 changed files with 531 additions and 128 deletions
+169 -10
View File
@@ -113,7 +113,7 @@ use frame_support::{
tokens::fungible::Inspect, ConstU32, Contains, Currency, Get, Randomness,
ReservableCurrency, Time,
},
weights::Weight,
weights::{OldWeight, Weight},
BoundedVec, WeakBoundedVec,
};
use frame_system::{limits::BlockWeights, Pallet as System};
@@ -429,15 +429,18 @@ pub mod pallet {
/// * If the account is a regular account, any value will be transferred.
/// * If no account exists and the call value is not less than `existential_deposit`,
/// a regular account will be created and any value will be transferred.
#[pallet::weight(T::WeightInfo::call().saturating_add(*gas_limit))]
pub fn call(
#[pallet::weight(T::WeightInfo::call().saturating_add((*gas_limit).into()))]
#[allow(deprecated)]
#[deprecated(note = "1D weight is used in this extrinsic, please migrate to `call`")]
pub fn call_old_weight(
origin: OriginFor<T>,
dest: AccountIdLookupOf<T>,
#[pallet::compact] value: BalanceOf<T>,
#[pallet::compact] gas_limit: Weight,
#[pallet::compact] gas_limit: OldWeight,
storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
data: Vec<u8>,
) -> DispatchResultWithPostInfo {
let gas_limit: Weight = gas_limit.into();
let origin = ensure_signed(origin)?;
let dest = T::Lookup::lookup(dest)?;
let mut output = Self::internal_call(
@@ -485,17 +488,22 @@ pub mod pallet {
/// - The `deploy` function is executed in the context of the newly-created account.
#[pallet::weight(
T::WeightInfo::instantiate_with_code(code.len() as u32, salt.len() as u32)
.saturating_add(*gas_limit)
.saturating_add((*gas_limit).into())
)]
pub fn instantiate_with_code(
#[allow(deprecated)]
#[deprecated(
note = "1D weight is used in this extrinsic, please migrate to `instantiate_with_code`"
)]
pub fn instantiate_with_code_old_weight(
origin: OriginFor<T>,
#[pallet::compact] value: BalanceOf<T>,
#[pallet::compact] gas_limit: Weight,
#[pallet::compact] gas_limit: OldWeight,
storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
code: Vec<u8>,
data: Vec<u8>,
salt: Vec<u8>,
) -> DispatchResultWithPostInfo {
let gas_limit: Weight = gas_limit.into();
let origin = ensure_signed(origin)?;
let code_len = code.len() as u32;
let salt_len = salt.len() as u32;
@@ -526,17 +534,20 @@ pub mod pallet {
/// code deployment step. Instead, the `code_hash` of an on-chain deployed wasm binary
/// must be supplied.
#[pallet::weight(
T::WeightInfo::instantiate(salt.len() as u32).saturating_add(*gas_limit)
T::WeightInfo::instantiate(salt.len() as u32).saturating_add((*gas_limit).into())
)]
pub fn instantiate(
#[allow(deprecated)]
#[deprecated(note = "1D weight is used in this extrinsic, please migrate to `instantiate`")]
pub fn instantiate_old_weight(
origin: OriginFor<T>,
#[pallet::compact] value: BalanceOf<T>,
#[pallet::compact] gas_limit: Weight,
#[pallet::compact] gas_limit: OldWeight,
storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
code_hash: CodeHash<T>,
data: Vec<u8>,
salt: Vec<u8>,
) -> DispatchResultWithPostInfo {
let gas_limit: Weight = gas_limit.into();
let origin = ensure_signed(origin)?;
let salt_len = salt.len() as u32;
let mut output = Self::internal_instantiate(
@@ -639,6 +650,154 @@ pub mod pallet {
Ok(())
})
}
/// Makes a call to an account, optionally transferring some balance.
///
/// # Parameters
///
/// * `dest`: Address of the contract to call.
/// * `value`: The balance to transfer from the `origin` to `dest`.
/// * `gas_limit`: The gas limit enforced when executing the constructor.
/// * `storage_deposit_limit`: The maximum amount of balance that can be charged from the
/// caller to pay for the storage consumed.
/// * `data`: The input data to pass to the contract.
///
/// * If the account is a smart-contract account, the associated code will be
/// executed and any value will be transferred.
/// * If the account is a regular account, any value will be transferred.
/// * If no account exists and the call value is not less than `existential_deposit`,
/// a regular account will be created and any value will be transferred.
#[pallet::weight(T::WeightInfo::call().saturating_add(*gas_limit))]
pub fn call(
origin: OriginFor<T>,
dest: AccountIdLookupOf<T>,
#[pallet::compact] value: BalanceOf<T>,
gas_limit: Weight,
storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
data: Vec<u8>,
) -> DispatchResultWithPostInfo {
let gas_limit: Weight = gas_limit.into();
let origin = ensure_signed(origin)?;
let dest = T::Lookup::lookup(dest)?;
let mut output = Self::internal_call(
origin,
dest,
value,
gas_limit,
storage_deposit_limit.map(Into::into),
data,
None,
);
if let Ok(retval) = &output.result {
if retval.did_revert() {
output.result = Err(<Error<T>>::ContractReverted.into());
}
}
output.gas_meter.into_dispatch_result(output.result, T::WeightInfo::call())
}
/// Instantiates a new contract from the supplied `code` optionally transferring
/// some balance.
///
/// This dispatchable has the same effect as calling [`Self::upload_code`] +
/// [`Self::instantiate`]. Bundling them together provides efficiency gains. Please
/// also check the documentation of [`Self::upload_code`].
///
/// # Parameters
///
/// * `value`: The balance to transfer from the `origin` to the newly created contract.
/// * `gas_limit`: The gas limit enforced when executing the constructor.
/// * `storage_deposit_limit`: The maximum amount of balance that can be charged/reserved
/// from the caller to pay for the storage consumed.
/// * `code`: The contract code to deploy in raw bytes.
/// * `data`: The input data to pass to the contract constructor.
/// * `salt`: Used for the address derivation. See [`Pallet::contract_address`].
///
/// Instantiation is executed as follows:
///
/// - The supplied `code` is instrumented, deployed, and a `code_hash` is created for that
/// code.
/// - If the `code_hash` already exists on the chain the underlying `code` will be shared.
/// - The destination address is computed based on the sender, code_hash and the salt.
/// - The smart-contract account is created at the computed address.
/// - The `value` is transferred to the new account.
/// - The `deploy` function is executed in the context of the newly-created account.
#[pallet::weight(
T::WeightInfo::instantiate_with_code(code.len() as u32, salt.len() as u32)
.saturating_add(*gas_limit)
)]
pub fn instantiate_with_code(
origin: OriginFor<T>,
#[pallet::compact] value: BalanceOf<T>,
gas_limit: Weight,
storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
code: Vec<u8>,
data: Vec<u8>,
salt: Vec<u8>,
) -> DispatchResultWithPostInfo {
let origin = ensure_signed(origin)?;
let code_len = code.len() as u32;
let salt_len = salt.len() as u32;
let mut output = Self::internal_instantiate(
origin,
value,
gas_limit,
storage_deposit_limit.map(Into::into),
Code::Upload(Bytes(code)),
data,
salt,
None,
);
if let Ok(retval) = &output.result {
if retval.1.did_revert() {
output.result = Err(<Error<T>>::ContractReverted.into());
}
}
output.gas_meter.into_dispatch_result(
output.result.map(|(_address, result)| result),
T::WeightInfo::instantiate_with_code(code_len, salt_len),
)
}
/// Instantiates a contract from a previously deployed wasm binary.
///
/// This function is identical to [`Self::instantiate_with_code`] but without the
/// code deployment step. Instead, the `code_hash` of an on-chain deployed wasm binary
/// must be supplied.
#[pallet::weight(
T::WeightInfo::instantiate(salt.len() as u32).saturating_add(*gas_limit)
)]
pub fn instantiate(
origin: OriginFor<T>,
#[pallet::compact] value: BalanceOf<T>,
gas_limit: Weight,
storage_deposit_limit: Option<<BalanceOf<T> as codec::HasCompact>::Type>,
code_hash: CodeHash<T>,
data: Vec<u8>,
salt: Vec<u8>,
) -> DispatchResultWithPostInfo {
let origin = ensure_signed(origin)?;
let salt_len = salt.len() as u32;
let mut output = Self::internal_instantiate(
origin,
value,
gas_limit,
storage_deposit_limit.map(Into::into),
Code::Existing(code_hash),
data,
salt,
None,
);
if let Ok(retval) = &output.result {
if retval.1.did_revert() {
output.result = Err(<Error<T>>::ContractReverted.into());
}
}
output.gas_meter.into_dispatch_result(
output.result.map(|(_address, output)| output),
T::WeightInfo::instantiate(salt_len),
)
}
}
#[pallet::event]
+10 -5
View File
@@ -274,7 +274,11 @@ mod tests {
BalanceOf, CodeHash, Error, Pallet as Contracts,
};
use assert_matches::assert_matches;
use frame_support::{assert_ok, dispatch::DispatchResultWithPostInfo, weights::Weight};
use frame_support::{
assert_ok,
dispatch::DispatchResultWithPostInfo,
weights::{OldWeight, Weight},
};
use pallet_contracts_primitives::{ExecReturnValue, ReturnFlags};
use pretty_assertions::assert_eq;
use sp_core::{Bytes, H256};
@@ -1545,10 +1549,11 @@ mod tests {
let output = execute(CODE_GAS_LEFT, vec![], &mut ext).unwrap();
let gas_left = Weight::decode(&mut &*output.data).unwrap();
let OldWeight(gas_left) = OldWeight::decode(&mut &*output.data).unwrap();
let actual_left = ext.gas_meter.gas_left();
assert!(gas_left.all_lt(gas_limit), "gas_left must be less than initial");
assert!(gas_left.all_gt(actual_left), "gas_left must be greater than final");
// TODO: account for proof size weight
assert!(gas_left < gas_limit.ref_time(), "gas_left must be less than initial");
assert!(gas_left > actual_left.ref_time(), "gas_left must be greater than final");
}
const CODE_VALUE_TRANSFERRED: &str = r#"
@@ -1946,7 +1951,7 @@ mod tests {
)]
);
assert!(mock_ext.gas_meter.gas_left().all_gt(Weight::zero()));
assert!(mock_ext.gas_meter.gas_left().ref_time() > 0);
}
const CODE_DEPOSIT_EVENT_MAX_TOPICS: &str = r#"