Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Deprecate FunctionOf and remove its users #6340

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<u8>,)| Module::<T>::calc_code_put_costs(args.0),
DispatchClass::Normal,
Pays::Yes
)]
#[weight = Module::<T>::calc_code_put_costs(&code)]
pub fn put_code(
origin,
code: Vec<u8>
Expand All @@ -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: (&<T::Lookup as StaticLookup>::Source, &BalanceOf<T>, &Weight, &Vec<u8>)| *args.2,
DispatchClass::Normal,
Pays::Yes
)]
#[weight = *gas_limit]
pub fn call(
origin,
dest: <T::Lookup as StaticLookup>::Source,
Expand Down Expand Up @@ -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<T>, &Weight, &CodeHash<T>, &Vec<u8>)| *args.1,
DispatchClass::Normal,
Pays::Yes
)]
#[weight = *gas_limit]
pub fn instantiate(
origin,
#[compact] endowment: BalanceOf<T>,
Expand Down
23 changes: 4 additions & 19 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<u8>, &U256, &u32, &U256, &Option<U256>)|
(*gas_price).saturated_into::<Weight>().saturating_mul(*gas_limit as Weight),
DispatchClass::Normal,
Pays::Yes,
)]
#[weight = (*gas_price).saturated_into::<Weight>().saturating_mul(*gas_limit as Weight)]
fn call(
origin,
target: H160,
Expand Down Expand Up @@ -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<u8>, &U256, &u32, &U256, &Option<U256>)|
(*gas_price).saturated_into::<Weight>().saturating_mul(*gas_limit as Weight),
DispatchClass::Normal,
Pays::Yes,
)]
#[weight = (*gas_price).saturated_into::<Weight>().saturating_mul(*gas_limit as Weight)]
fn create(
origin,
init: Vec<u8>,
Expand Down Expand Up @@ -339,12 +329,7 @@ decl_module! {
}

/// Issue an EVM create2 operation.
#[weight = FunctionOf(
|(_, _, _, gas_limit, gas_price, _): (&Vec<u8>, &H256, &U256, &u32, &U256, &Option<U256>)|
(*gas_price).saturated_into::<Weight>().saturating_mul(*gas_limit as Weight),
DispatchClass::Normal,
Pays::Yes,
)]
#[weight = (*gas_price).saturated_into::<Weight>().saturating_mul(*gas_limit as Weight)]
fn create2(
origin,
init: Vec<u8>,
Expand Down
8 changes: 2 additions & 6 deletions frame/recovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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>
#[weight = FunctionOf(
|args: (&T::AccountId, &Box<<T as Trait>::Call>)| args.1.get_dispatch_info().weight + 10_000,
|args: (&T::AccountId, &Box<<T as Trait>::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<<T as Trait>::Call>
Expand Down
8 changes: 2 additions & 6 deletions frame/scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,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
Expand Down Expand Up @@ -441,11 +441,7 @@ mod tests {
pub struct Module<T: Trait> for enum Call where origin: <T as system::Trait>::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));
Expand Down
24 changes: 4 additions & 20 deletions frame/sudo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -126,11 +126,7 @@ decl_module! {
/// - One DB write (event).
/// - Weight of derivative `call` execution + 10,000.
/// # </weight>
#[weight = FunctionOf(
|args: (&Box<<T as Trait>::Call>,)| args.0.get_dispatch_info().weight + 10_000,
|args: (&Box<<T as Trait>::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<<T as Trait>::Call>) {
// This is a public call, so we ensure that the origin is some signed account.
let sender = ensure_signed(origin)?;
Expand All @@ -150,11 +146,7 @@ decl_module! {
/// - O(1).
/// - The weight of this call is defined by the caller.
/// # </weight>
#[weight = FunctionOf(
|(_, &weight): (&Box<<T as Trait>::Call>,&Weight,)| weight,
|(call, _): (&Box<<T as Trait>::Call>,&Weight,)| call.get_dispatch_info().class,
Pays::Yes,
)]
#[weight = (*_weight, call.get_dispatch_info().class)]
fn sudo_unchecked_weight(origin, call: Box<<T as Trait>::Call>, _weight: Weight) {
// This is a public call, so we ensure that the origin is some signed account.
let sender = ensure_signed(origin)?;
Expand Down Expand Up @@ -195,15 +187,7 @@ decl_module! {
/// - One DB write (event).
/// - Weight of derivative `call` execution + 10,000.
/// # </weight>
#[weight = FunctionOf(
|args: (&<T::Lookup as StaticLookup>::Source, &Box<<T as Trait>::Call>,)| {
args.1.get_dispatch_info().weight + 10_000
},
|args: (&<T::Lookup as StaticLookup>::Source, &Box<<T as Trait>::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: <T::Lookup as StaticLookup>::Source, call: Box<<T as Trait>::Call>) {
// This is a public call, so we ensure that the origin is some signed account.
let sender = ensure_signed(origin)?;
Expand Down
14 changes: 3 additions & 11 deletions frame/sudo/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,23 +56,15 @@ pub mod logger {
pub struct Module<T: Trait> for enum Call where origin: <T as system::Trait>::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)?;
<I32Log>::append(i);
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)?;
Expand Down
11 changes: 9 additions & 2 deletions frame/support/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,19 @@ impl<T> PaysFee<T> 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<WD, CD, PF>(pub WD, pub CD, pub PF);

// `WeighData` as a raw value
#[allow(deprecated)]
impl<Args, CD, PF> WeighData<Args> for FunctionOf<Weight, CD, PF> {
fn weigh_data(&self, _: Args) -> Weight {
self.0
}
}

// `WeighData` as a closure
#[allow(deprecated)]
impl<Args, WD, CD, PF> WeighData<Args> for FunctionOf<WD, CD, PF> where
WD : Fn(Args) -> Weight
{
Expand All @@ -444,13 +447,15 @@ impl<Args, WD, CD, PF> WeighData<Args> for FunctionOf<WD, CD, PF> where
}

// `ClassifyDispatch` as a raw value
#[allow(deprecated)]
impl<Args, WD, PF> ClassifyDispatch<Args> for FunctionOf<WD, DispatchClass, PF> {
fn classify_dispatch(&self, _: Args) -> DispatchClass {
self.1
}
}

// `ClassifyDispatch` as a raw value
#[allow(deprecated)]
impl<Args, WD, CD, PF> ClassifyDispatch<Args> for FunctionOf<WD, CD, PF> where
CD : Fn(Args) -> DispatchClass
{
Expand All @@ -460,13 +465,15 @@ impl<Args, WD, CD, PF> ClassifyDispatch<Args> for FunctionOf<WD, CD, PF> where
}

// `PaysFee` as a raw value
#[allow(deprecated)]
impl<Args, WD, CD> PaysFee<Args> for FunctionOf<WD, CD, Pays> {
fn pays_fee(&self, _: Args) -> Pays {
self.2
}
}

// `PaysFee` as a closure
#[allow(deprecated)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't all these extra allow's are already handled by the one above the struct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which struct do you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FunctionOf

Copy link
Member Author

@athei athei Jun 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FunctionOf is marked with #[deprecated] and not with #[allow(deprecated].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh🙈

impl<Args, WD, CD, PF> PaysFee<Args> for FunctionOf<WD, CD, PF> where
PF : Fn(Args) -> Pays
{
Expand Down Expand Up @@ -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]
Expand Down
37 changes: 12 additions & 25 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ use frame_support::{
},
weights::{
Weight, RuntimeDbWeight, DispatchInfo, PostDispatchInfo, DispatchClass,
FunctionOf, Pays, extract_actual_weight,
extract_actual_weight,
},
dispatch::DispatchResultWithPostInfo,
};
Expand Down Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -669,13 +665,10 @@ decl_module! {
/// - Base Weight: 0.568 * i µs
/// - Writes: Number of items
/// # </weight>
#[weight = FunctionOf(
|(items,): (&Vec<KeyValue>,)| {
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<KeyValue>) {
ensure_root(origin)?;
Expand All @@ -692,13 +685,10 @@ decl_module! {
/// - Base Weight: .378 * i µs
/// - Writes: Number of items
/// # </weight>
#[weight = FunctionOf(
|(keys,): (&Vec<Key>,)| {
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<Key>) {
ensure_root(origin)?;
Expand All @@ -718,13 +708,10 @@ decl_module! {
/// - Base Weight: 0.834 * P µs
/// - Writes: Number of subkeys + 1
/// # </weight>
#[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)?;
Expand Down Expand Up @@ -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! {
Expand Down
Loading