From afdf5ef01da479d3c662a041bc1ebcc7d1615043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Fri, 12 Jun 2020 15:21:39 +0200 Subject: [PATCH] Deprecate FunctionOf and remove its users (#6340) * Deprecate FunctionOf and remove users * Remove unused import --- frame/contracts/src/lib.rs | 20 ++++--------------- frame/evm/src/lib.rs | 23 ++++------------------ frame/recovery/src/lib.rs | 8 ++------ frame/scheduler/src/lib.rs | 8 ++------ frame/sudo/src/lib.rs | 24 ++++------------------- frame/sudo/src/mock.rs | 14 +++----------- frame/support/src/weights.rs | 11 +++++++++-- frame/system/src/lib.rs | 37 ++++++++++++------------------------ frame/utility/src/lib.rs | 35 +++++++++++++--------------------- 9 files changed, 53 insertions(+), 127 deletions(-) diff --git a/frame/contracts/src/lib.rs b/frame/contracts/src/lib.rs index 8f90f557cae13..245c95a4fa4e9 100644 --- a/frame/contracts/src/lib.rs +++ b/frame/contracts/src/lib.rs @@ -117,7 +117,7 @@ use frame_support::{ parameter_types, IsSubType, storage::child::{self, ChildInfo}, }; use frame_support::traits::{OnUnbalanced, Currency, Get, Time, Randomness}; -use frame_support::weights::{FunctionOf, DispatchClass, Weight, GetDispatchInfo, Pays}; +use frame_support::weights::GetDispatchInfo; use frame_system::{self as system, ensure_signed, RawOrigin, ensure_root}; use pallet_contracts_primitives::{RentProjection, ContractAccessError}; @@ -481,11 +481,7 @@ decl_module! { /// Stores the given binary Wasm code into the chain's storage and returns its `codehash`. /// You can instantiate contracts only with stored code. - #[weight = FunctionOf( - |args: (&Vec,)| Module::::calc_code_put_costs(args.0), - DispatchClass::Normal, - Pays::Yes - )] + #[weight = Module::::calc_code_put_costs(&code)] pub fn put_code( origin, code: Vec @@ -506,11 +502,7 @@ decl_module! { /// * 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. - #[weight = FunctionOf( - |args: (&::Source, &BalanceOf, &Weight, &Vec)| *args.2, - DispatchClass::Normal, - Pays::Yes - )] + #[weight = *gas_limit] pub fn call( origin, dest: ::Source, @@ -538,11 +530,7 @@ decl_module! { /// after the execution is saved as the `code` of the account. That code will be invoked /// upon any call received by this account. /// - The contract is initialized. - #[weight = FunctionOf( - |args: (&BalanceOf, &Weight, &CodeHash, &Vec)| *args.1, - DispatchClass::Normal, - Pays::Yes - )] + #[weight = *gas_limit] pub fn instantiate( origin, #[compact] endowment: BalanceOf, diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index c5df79fdb8791..3600b866b2d99 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -30,7 +30,7 @@ use codec::{Encode, Decode}; #[cfg(feature = "std")] use serde::{Serialize, Deserialize}; use frame_support::{ensure, decl_module, decl_storage, decl_event, decl_error}; -use frame_support::weights::{Weight, DispatchClass, FunctionOf, Pays}; +use frame_support::weights::Weight; use frame_support::traits::{Currency, WithdrawReason, ExistenceRequirement, Get}; use frame_system::{self as system, ensure_signed}; use sp_runtime::ModuleId; @@ -273,12 +273,7 @@ decl_module! { } /// Issue an EVM call operation. This is similar to a message call transaction in Ethereum. - #[weight = FunctionOf( - |(_, _, _, gas_limit, gas_price, _): (&H160, &Vec, &U256, &u32, &U256, &Option)| - (*gas_price).saturated_into::().saturating_mul(*gas_limit as Weight), - DispatchClass::Normal, - Pays::Yes, - )] + #[weight = (*gas_price).saturated_into::().saturating_mul(*gas_limit as Weight)] fn call( origin, target: H160, @@ -306,12 +301,7 @@ decl_module! { /// Issue an EVM create operation. This is similar to a contract creation transaction in /// Ethereum. - #[weight = FunctionOf( - |(_, _, gas_limit, gas_price, _): (&Vec, &U256, &u32, &U256, &Option)| - (*gas_price).saturated_into::().saturating_mul(*gas_limit as Weight), - DispatchClass::Normal, - Pays::Yes, - )] + #[weight = (*gas_price).saturated_into::().saturating_mul(*gas_limit as Weight)] fn create( origin, init: Vec, @@ -339,12 +329,7 @@ decl_module! { } /// Issue an EVM create2 operation. - #[weight = FunctionOf( - |(_, _, _, gas_limit, gas_price, _): (&Vec, &H256, &U256, &u32, &U256, &Option)| - (*gas_price).saturated_into::().saturating_mul(*gas_limit as Weight), - DispatchClass::Normal, - Pays::Yes, - )] + #[weight = (*gas_price).saturated_into::().saturating_mul(*gas_limit as Weight)] fn create2( origin, init: Vec, diff --git a/frame/recovery/src/lib.rs b/frame/recovery/src/lib.rs index cd3ba76b37012..470803d22e095 100644 --- a/frame/recovery/src/lib.rs +++ b/frame/recovery/src/lib.rs @@ -160,7 +160,7 @@ use codec::{Encode, Decode}; use frame_support::{ decl_module, decl_event, decl_storage, decl_error, ensure, - Parameter, RuntimeDebug, weights::{GetDispatchInfo, FunctionOf, Pays}, + Parameter, RuntimeDebug, weights::GetDispatchInfo, traits::{Currency, ReservableCurrency, Get, BalanceStatus}, dispatch::PostDispatchInfo, }; @@ -336,11 +336,7 @@ decl_module! { /// - The weight of the `call` + 10,000. /// - One storage lookup to check account is recovered by `who`. O(1) /// # - #[weight = FunctionOf( - |args: (&T::AccountId, &Box<::Call>)| args.1.get_dispatch_info().weight + 10_000, - |args: (&T::AccountId, &Box<::Call>)| args.1.get_dispatch_info().class, - Pays::Yes, - )] + #[weight = (call.get_dispatch_info().weight + 10_000, call.get_dispatch_info().class)] fn as_recovered(origin, account: T::AccountId, call: Box<::Call> diff --git a/frame/scheduler/src/lib.rs b/frame/scheduler/src/lib.rs index 580b3b060ebe4..cd3aba45ed5ff 100644 --- a/frame/scheduler/src/lib.rs +++ b/frame/scheduler/src/lib.rs @@ -400,7 +400,7 @@ mod tests { use frame_support::{ impl_outer_event, impl_outer_origin, impl_outer_dispatch, parameter_types, assert_ok, traits::{OnInitialize, OnFinalize}, - weights::{DispatchClass, FunctionOf, Pays, constants::RocksDbWeight}, + weights::constants::RocksDbWeight, }; use sp_core::H256; // The testing primitives are very useful for avoiding having to work with signatures @@ -439,11 +439,7 @@ mod tests { pub struct Module for enum Call where origin: ::Origin { fn deposit_event() = default; - #[weight = FunctionOf( - |args: (&u32, &Weight)| *args.1, - |_: (&u32, &Weight)| DispatchClass::Normal, - Pays::Yes, - )] + #[weight = *weight] fn log(origin, i: u32, weight: Weight) { ensure_root(origin)?; Self::deposit_event(Event::Logged(i, weight)); diff --git a/frame/sudo/src/lib.rs b/frame/sudo/src/lib.rs index 3d5d1b2582127..55c2c97d12ca6 100644 --- a/frame/sudo/src/lib.rs +++ b/frame/sudo/src/lib.rs @@ -93,7 +93,7 @@ use sp_runtime::{DispatchResult, traits::{StaticLookup, Dispatchable}}; use frame_support::{ Parameter, decl_module, decl_event, decl_storage, decl_error, ensure, }; -use frame_support::weights::{Weight, GetDispatchInfo, FunctionOf, Pays}; +use frame_support::weights::{Weight, GetDispatchInfo}; use frame_system::{self as system, ensure_signed}; #[cfg(test)] @@ -126,11 +126,7 @@ decl_module! { /// - One DB write (event). /// - Weight of derivative `call` execution + 10,000. /// # - #[weight = FunctionOf( - |args: (&Box<::Call>,)| args.0.get_dispatch_info().weight + 10_000, - |args: (&Box<::Call>,)| args.0.get_dispatch_info().class, - Pays::Yes, - )] + #[weight = (call.get_dispatch_info().weight + 10_000, call.get_dispatch_info().class)] fn sudo(origin, call: Box<::Call>) { // This is a public call, so we ensure that the origin is some signed account. let sender = ensure_signed(origin)?; @@ -150,11 +146,7 @@ decl_module! { /// - O(1). /// - The weight of this call is defined by the caller. /// # - #[weight = FunctionOf( - |(_, &weight): (&Box<::Call>,&Weight,)| weight, - |(call, _): (&Box<::Call>,&Weight,)| call.get_dispatch_info().class, - Pays::Yes, - )] + #[weight = (*_weight, call.get_dispatch_info().class)] fn sudo_unchecked_weight(origin, call: Box<::Call>, _weight: Weight) { // This is a public call, so we ensure that the origin is some signed account. let sender = ensure_signed(origin)?; @@ -195,15 +187,7 @@ decl_module! { /// - One DB write (event). /// - Weight of derivative `call` execution + 10,000. /// # - #[weight = FunctionOf( - |args: (&::Source, &Box<::Call>,)| { - args.1.get_dispatch_info().weight + 10_000 - }, - |args: (&::Source, &Box<::Call>,)| { - args.1.get_dispatch_info().class - }, - Pays::Yes, - )] + #[weight = (call.get_dispatch_info().weight + 10_000, call.get_dispatch_info().class)] fn sudo_as(origin, who: ::Source, call: Box<::Call>) { // This is a public call, so we ensure that the origin is some signed account. let sender = ensure_signed(origin)?; diff --git a/frame/sudo/src/mock.rs b/frame/sudo/src/mock.rs index a270787da66ec..54b9100d61961 100644 --- a/frame/sudo/src/mock.rs +++ b/frame/sudo/src/mock.rs @@ -20,7 +20,7 @@ use super::*; use frame_support::{ impl_outer_origin, impl_outer_dispatch, impl_outer_event, parameter_types, - weights::{Weight, DispatchClass} + weights::Weight, }; use sp_core::H256; // The testing primitives are very useful for avoiding having to work with signatures @@ -56,11 +56,7 @@ pub mod logger { pub struct Module for enum Call where origin: ::Origin { fn deposit_event() = default; - #[weight = FunctionOf( - |args: (&i32, &Weight)| *args.1, - DispatchClass::Normal, - Pays::Yes, - )] + #[weight = *weight] fn privileged_i32_log(origin, i: i32, weight: Weight){ // Ensure that the `origin` is `Root`. ensure_root(origin)?; @@ -68,11 +64,7 @@ pub mod logger { Self::deposit_event(RawEvent::AppendI32(i, weight)); } - #[weight = FunctionOf( - |args: (&i32, &Weight)| *args.1, - DispatchClass::Normal, - Pays::Yes, - )] + #[weight = *weight] fn non_privileged_log(origin, i: i32, weight: Weight){ // Ensure that the `origin` is some signed account. let sender = ensure_signed(origin)?; diff --git a/frame/support/src/weights.rs b/frame/support/src/weights.rs index dd80f8d8a8e6c..810bd2fcb6416 100644 --- a/frame/support/src/weights.rs +++ b/frame/support/src/weights.rs @@ -425,9 +425,11 @@ impl PaysFee for (Weight, Pays) { /// with the same argument list as the dispatched, wrapped in a tuple. /// - `PF`: a `Pays` variant for whether this dispatch pays fee or not or a closure that /// returns a `Pays` variant with the same argument list as the dispatched, wrapped in a tuple. +#[deprecated = "Function arguments are available directly inside the annotation now."] pub struct FunctionOf(pub WD, pub CD, pub PF); // `WeighData` as a raw value +#[allow(deprecated)] impl WeighData for FunctionOf { fn weigh_data(&self, _: Args) -> Weight { self.0 @@ -435,6 +437,7 @@ impl WeighData for FunctionOf { } // `WeighData` as a closure +#[allow(deprecated)] impl WeighData for FunctionOf where WD : Fn(Args) -> Weight { @@ -444,6 +447,7 @@ impl WeighData for FunctionOf where } // `ClassifyDispatch` as a raw value +#[allow(deprecated)] impl ClassifyDispatch for FunctionOf { fn classify_dispatch(&self, _: Args) -> DispatchClass { self.1 @@ -451,6 +455,7 @@ impl ClassifyDispatch for FunctionOf } // `ClassifyDispatch` as a raw value +#[allow(deprecated)] impl ClassifyDispatch for FunctionOf where CD : Fn(Args) -> DispatchClass { @@ -460,6 +465,7 @@ impl ClassifyDispatch for FunctionOf where } // `PaysFee` as a raw value +#[allow(deprecated)] impl PaysFee for FunctionOf { fn pays_fee(&self, _: Args) -> Pays { self.2 @@ -467,6 +473,7 @@ impl PaysFee for FunctionOf { } // `PaysFee` as a closure +#[allow(deprecated)] impl PaysFee for FunctionOf where PF : Fn(Args) -> Pays { @@ -663,10 +670,10 @@ mod tests { fn f03(_origin) { unimplemented!(); } // weight = a x 10 + b - #[weight = FunctionOf(|args: (&u32, &u32)| (args.0 * 10 + args.1) as Weight, DispatchClass::Normal, Pays::Yes)] + #[weight = ((_a * 10 + _eb * 1) as Weight, DispatchClass::Normal, Pays::Yes)] fn f11(_origin, _a: u32, _eb: u32) { unimplemented!(); } - #[weight = FunctionOf(|_: (&u32, &u32)| 0, DispatchClass::Operational, Pays::Yes)] + #[weight = (0, DispatchClass::Operational, Pays::Yes)] fn f12(_origin, _a: u32, _eb: u32) { unimplemented!(); } #[weight = T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + 10_000] diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index d702ad779a12f..1943256651a4d 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -127,7 +127,7 @@ use frame_support::{ }, weights::{ Weight, RuntimeDbWeight, DispatchInfo, PostDispatchInfo, DispatchClass, - FunctionOf, Pays, extract_actual_weight, + extract_actual_weight, }, dispatch::DispatchResultWithPostInfo, }; @@ -566,11 +566,7 @@ decl_module! { /// A dispatch that will fill the block weight up to the given ratio. // TODO: This should only be available for testing, rather than in general usage, but // that's not possible at present (since it's within the decl_module macro). - #[weight = FunctionOf( - |(ratio,): (&Perbill,)| *ratio * T::MaximumBlockWeight::get(), - DispatchClass::Operational, - Pays::Yes, - )] + #[weight = (*_ratio * T::MaximumBlockWeight::get(), DispatchClass::Operational)] fn fill_block(origin, _ratio: Perbill) { ensure_root(origin)?; } @@ -669,13 +665,10 @@ decl_module! { /// - Base Weight: 0.568 * i µs /// - Writes: Number of items /// # - #[weight = FunctionOf( - |(items,): (&Vec,)| { - T::DbWeight::get().writes(items.len() as Weight) - .saturating_add((items.len() as Weight).saturating_mul(600_000)) - }, + #[weight = ( + T::DbWeight::get().writes(items.len() as Weight) + .saturating_add((items.len() as Weight).saturating_mul(600_000)), DispatchClass::Operational, - Pays::Yes, )] fn set_storage(origin, items: Vec) { ensure_root(origin)?; @@ -692,13 +685,10 @@ decl_module! { /// - Base Weight: .378 * i µs /// - Writes: Number of items /// # - #[weight = FunctionOf( - |(keys,): (&Vec,)| { - T::DbWeight::get().writes(keys.len() as Weight) - .saturating_add((keys.len() as Weight).saturating_mul(400_000)) - }, + #[weight = ( + T::DbWeight::get().writes(keys.len() as Weight) + .saturating_add((keys.len() as Weight).saturating_mul(400_000)), DispatchClass::Operational, - Pays::Yes, )] fn kill_storage(origin, keys: Vec) { ensure_root(origin)?; @@ -718,13 +708,10 @@ decl_module! { /// - Base Weight: 0.834 * P µs /// - Writes: Number of subkeys + 1 /// # - #[weight = FunctionOf( - |(_, &subkeys): (&Key, &u32)| { - T::DbWeight::get().writes(Weight::from(subkeys) + 1) - .saturating_add((Weight::from(subkeys) + 1).saturating_mul(850_000)) - }, + #[weight = ( + T::DbWeight::get().writes(Weight::from(*_subkeys) + 1) + .saturating_add((Weight::from(*_subkeys) + 1).saturating_mul(850_000)), DispatchClass::Operational, - Pays::Yes, )] fn kill_prefix(origin, prefix: Key, _subkeys: u32) { ensure_root(origin)?; @@ -1904,7 +1891,7 @@ pub(crate) mod tests { use sp_runtime::{traits::{BlakeTwo256, IdentityLookup, SignedExtension}, testing::Header, DispatchError}; use frame_support::{ impl_outer_origin, parameter_types, assert_ok, assert_noop, assert_err, - weights::WithPostDispatchInfo, + weights::{WithPostDispatchInfo, Pays}, }; impl_outer_origin! { diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index 34385b67864c5..add1049b26c51 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -56,7 +56,7 @@ use sp_core::TypeId; use sp_io::hashing::blake2_256; use frame_support::{decl_module, decl_event, decl_error, decl_storage, Parameter, ensure}; use frame_support::{traits::{Filter, FilterStack, ClearFilterGuard}, - weights::{Weight, GetDispatchInfo, DispatchClass, FunctionOf, Pays}, dispatch::PostDispatchInfo, + weights::{Weight, GetDispatchInfo, DispatchClass}, dispatch::PostDispatchInfo, }; use frame_system::{self as system, ensure_signed, ensure_root}; use sp_runtime::{DispatchError, DispatchResult, traits::Dispatchable}; @@ -136,14 +136,12 @@ decl_module! { /// `BatchInterrupted` event is deposited, along with the number of successful calls made /// and the error of the failed call. If all were successful, then the `BatchCompleted` /// event is deposited. - #[weight = FunctionOf( - |args: (&Vec<::Call>,)| { - args.0.iter() - .map(|call| call.get_dispatch_info().weight) - .fold(15_000_000, |a: Weight, n| a.saturating_add(n).saturating_add(1_000_000)) - }, - |args: (&Vec<::Call>,)| { - let all_operational = args.0.iter() + #[weight = ( + calls.iter() + .map(|call| call.get_dispatch_info().weight) + .fold(15_000_000, |a: Weight, n| a.saturating_add(n).saturating_add(1_000_000)), + { + let all_operational = calls.iter() .map(|call| call.get_dispatch_info().class) .all(|class| class == DispatchClass::Operational); if all_operational { @@ -152,7 +150,6 @@ decl_module! { DispatchClass::Normal } }, - Pays::Yes, )] fn batch(origin, calls: Vec<::Call>) { let is_root = ensure_root(origin.clone()).is_ok(); @@ -185,12 +182,9 @@ decl_module! { /// - Base weight: 2.861 µs /// - Plus the weight of the `call` /// # - #[weight = FunctionOf( - |args: (&u16, &Box<::Call>)| { - args.1.get_dispatch_info().weight.saturating_add(3_000_000) - }, - |args: (&u16, &Box<::Call>)| args.1.get_dispatch_info().class, - Pays::Yes, + #[weight = ( + call.get_dispatch_info().weight.saturating_add(3_000_000), + call.get_dispatch_info().class, )] fn as_sub(origin, index: u16, call: Box<::Call>) -> DispatchResult { let who = ensure_signed(origin)?; @@ -217,12 +211,9 @@ decl_module! { /// - Base weight: 2.861 µs /// - Plus the weight of the `call` /// # - #[weight = FunctionOf( - |args: (&u16, &Box<::Call>)| { - args.1.get_dispatch_info().weight.saturating_add(3_000_000) - }, - |args: (&u16, &Box<::Call>)| args.1.get_dispatch_info().class, - Pays::Yes, + #[weight = ( + call.get_dispatch_info().weight.saturating_add(3_000_000), + call.get_dispatch_info().class, )] fn as_limited_sub(origin, index: u16, call: Box<::Call>) -> DispatchResult { let who = ensure_signed(origin)?;