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

Migrate away from SimpleDispatchInfo #5686

Merged
merged 8 commits into from
Apr 22, 2020
6 changes: 3 additions & 3 deletions bin/node-template/pallets/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs

use frame_support::{decl_module, decl_storage, decl_event, decl_error, dispatch};
use frame_support::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
use frame_support::weights::MINIMUM_WEIGHT;
use frame_system::{self as system, ensure_signed};

#[cfg(test)]
Expand Down Expand Up @@ -76,7 +76,7 @@ decl_module! {
/// Just a dummy entry point.
/// function that can be called by the external world as an extrinsics call
/// takes a parameter of the type `AccountId`, stores it, and emits an event
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn do_something(origin, something: u32) -> dispatch::DispatchResult {
// Check it was signed and get the signer. See also: ensure_root and ensure_none
let who = ensure_signed(origin)?;
Expand All @@ -92,7 +92,7 @@ decl_module! {

/// Another dummy entry point.
/// takes no parameters, attempts to increment storage value, and possibly throws an error
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn cause_error(origin) -> dispatch::DispatchResult {
// Check it was signed and get the signer. See also: ensure_root and ensure_none
let _who = ensure_signed(origin)?;
Expand Down
8 changes: 4 additions & 4 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{Parameter, decl_module, decl_event, decl_storage, decl_error, ensure};
use frame_support::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
use frame_support::weights::MINIMUM_WEIGHT;
use sp_runtime::traits::{Member, AtLeast32Bit, Zero, StaticLookup};
use frame_system::{self as system, ensure_signed};
use sp_runtime::traits::One;
Expand All @@ -158,7 +158,7 @@ decl_module! {
/// Issue a new class of fungible assets. There are, and will only ever be, `total`
/// such assets and they'll all belong to the `origin` initially. It will have an
/// identifier `AssetId` instance: this will be specified in the `Issued` event.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
fn issue(origin, #[compact] total: T::Balance) {
let origin = ensure_signed(origin)?;

Expand All @@ -172,7 +172,7 @@ decl_module! {
}

/// Move some assets from one holder to another.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
fn transfer(origin,
#[compact] id: T::AssetId,
target: <T::Lookup as StaticLookup>::Source,
Expand All @@ -191,7 +191,7 @@ decl_module! {
}

/// Destroy any assets of `id` owned by `origin`.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
fn destroy(origin, #[compact] id: T::AssetId) {
let origin = ensure_signed(origin)?;
let balance = <Balances<T>>::take((id, &origin));
Expand Down
4 changes: 2 additions & 2 deletions frame/authorship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use frame_support::traits::{FindAuthor, VerifySeal, Get};
use codec::{Encode, Decode};
use frame_system::ensure_none;
use sp_runtime::traits::{Header as HeaderT, One, Zero};
use frame_support::weights::{Weight, MINIMUM_WEIGHT, SimpleDispatchInfo};
use frame_support::weights::{Weight, MINIMUM_WEIGHT, DispatchClass};
use sp_inherents::{InherentIdentifier, ProvideInherent, InherentData};
use sp_authorship::{INHERENT_IDENTIFIER, UnclesInherentData, InherentError};

Expand Down Expand Up @@ -207,7 +207,7 @@ decl_module! {
}

/// Provide a set of uncles.
#[weight = SimpleDispatchInfo::FixedMandatory(MINIMUM_WEIGHT)]
#[weight = (MINIMUM_WEIGHT, DispatchClass::Mandatory)]
fn set_uncles(origin, new_uncles: Vec<T::Header>) -> dispatch::DispatchResult {
ensure_none(origin)?;
ensure!(new_uncles.len() <= MAX_UNCLES, Error::<T>::TooManyUncles);
Expand Down
28 changes: 14 additions & 14 deletions frame/benchmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{decl_module, decl_storage, decl_event, decl_error};
use frame_support::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
use frame_support::weights::MINIMUM_WEIGHT;
use frame_support::traits::Currency;
use frame_system::{self as system, ensure_signed};
use codec::{Encode, Decode};
Expand Down Expand Up @@ -71,7 +71,7 @@ decl_module! {
fn deposit_event() = default;

/// Do nothing.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn do_nothing(_origin, input: u32) {
if input > 0 {
return Ok(());
Expand All @@ -83,15 +83,15 @@ decl_module! {
/// storage database, however, the `repeat` calls will all pull from the
/// storage overlay cache. You must consider this when analyzing the
/// results of the benchmark.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn read_value(_origin, repeat: u32) {
for _ in 0..repeat {
MyValue::get();
}
}

/// Put a value into a storage value.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn put_value(_origin, repeat: u32) {
for r in 0..repeat {
MyValue::put(r);
Expand All @@ -103,69 +103,69 @@ decl_module! {
/// storage database, however, the `repeat` calls will all pull from the
/// storage overlay cache. You must consider this when analyzing the
/// results of the benchmark.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn exists_value(_origin, repeat: u32) {
for _ in 0..repeat {
MyValue::exists();
}
}

/// Remove a value from storage `repeat` number of times.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn remove_value(_origin, repeat: u32) {
for r in 0..repeat {
MyMap::remove(r);
}
}

/// Read a value from storage map `repeat` number of times.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn read_map(_origin, repeat: u32) {
for r in 0..repeat {
MyMap::get(r);
}
}

/// Insert a value into a map.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn insert_map(_origin, repeat: u32) {
for r in 0..repeat {
MyMap::insert(r, r);
}
}

/// Check is a map contains a value `repeat` number of times.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn contains_key_map(_origin, repeat: u32) {
for r in 0..repeat {
MyMap::contains_key(r);
}
}

/// Read a value from storage `repeat` number of times.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn remove_prefix(_origin, repeat: u32) {
for r in 0..repeat {
MyDoubleMap::remove_prefix(r);
}
}

/// Add user to the list.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn add_member_list(origin) {
let who = ensure_signed(origin)?;
MyMemberList::<T>::mutate(|x| x.push(who));
}

/// Append user to the list.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn append_member_list(origin) {
let who = ensure_signed(origin)?;
MyMemberList::<T>::append(&[who])?;
}

/// Encode a vector of accounts to bytes.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn encode_accounts(_origin, accounts: Vec<T::AccountId>) {
let bytes = accounts.encode();

Expand All @@ -177,7 +177,7 @@ decl_module! {
}

/// Decode bytes into a vector of accounts.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn decode_accounts(_origin, bytes: Vec<u8>) {
let accounts: Vec<T::AccountId> = Decode::decode(&mut bytes.as_slice()).map_err(|_| "Could not decode")?;

Expand Down
6 changes: 3 additions & 3 deletions frame/benchmarking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use sp_std::prelude::*;
use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::{H256, Header}};
use frame_support::{
dispatch::DispatchResult,
weights::{SimpleDispatchInfo, MINIMUM_WEIGHT},
weights::MINIMUM_WEIGHT,
decl_module, decl_storage, impl_outer_origin, assert_ok, assert_err, ensure
};
use frame_system::{RawOrigin, ensure_signed, ensure_none};
Expand All @@ -37,14 +37,14 @@ decl_storage! {

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
fn set_value(origin, n: u32) -> DispatchResult {
let _sender = ensure_signed(origin)?;
Value::put(n);
Ok(())
}

#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
fn dummy(origin, _n: u32) -> DispatchResult {
let _sender = ensure_none(origin)?;
Ok(())
Expand Down
12 changes: 6 additions & 6 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ use sp_std::{prelude::*, result};
use sp_core::u32_trait::Value as U32;
use sp_runtime::RuntimeDebug;
use sp_runtime::traits::Hash;
use frame_support::weights::SimpleDispatchInfo;
use frame_support::{
dispatch::{Dispatchable, Parameter}, codec::{Encode, Decode},
traits::{Get, ChangeMembers, InitializeMembers, EnsureOrigin}, decl_module, decl_event,
decl_storage, decl_error, ensure,
weights::DispatchClass,
};
use frame_system::{self as system, ensure_signed, ensure_root};

Expand Down Expand Up @@ -187,7 +187,7 @@ decl_module! {
/// - `prime`: The prime member whose vote sets the default.
///
/// Requires root origin.
#[weight = SimpleDispatchInfo::FixedOperational(100_000_000)]
#[weight = (100_000_000, DispatchClass::Operational)]
fn set_members(origin, new_members: Vec<T::AccountId>, prime: Option<T::AccountId>) {
ensure_root(origin)?;
let mut new_members = new_members;
Expand All @@ -200,7 +200,7 @@ decl_module! {
/// Dispatch a proposal from a member using the `Member` origin.
///
/// Origin must be a member of the collective.
#[weight = SimpleDispatchInfo::FixedOperational(100_000_000)]
#[weight = (100_000_000, DispatchClass::Operational)]
fn execute(origin, proposal: Box<<T as Trait<I>>::Proposal>) {
let who = ensure_signed(origin)?;
ensure!(Self::is_member(&who), Error::<T, I>::NotMember);
Expand All @@ -214,7 +214,7 @@ decl_module! {
/// - Bounded storage reads and writes.
/// - Argument `threshold` has bearing on weight.
/// # </weight>
#[weight = SimpleDispatchInfo::FixedOperational(5_000_000_000)]
#[weight = (5_000_000_000, DispatchClass::Operational)]
fn propose(origin, #[compact] threshold: MemberCount, proposal: Box<<T as Trait<I>>::Proposal>) {
let who = ensure_signed(origin)?;
ensure!(Self::is_member(&who), Error::<T, I>::NotMember);
Expand Down Expand Up @@ -244,7 +244,7 @@ decl_module! {
/// - Bounded storage read and writes.
/// - Will be slightly heavier if the proposal is approved / disapproved after the vote.
/// # </weight>
#[weight = SimpleDispatchInfo::FixedOperational(200_000_000)]
#[weight = (200_000_000, DispatchClass::Operational)]
fn vote(origin, proposal: T::Hash, #[compact] index: ProposalIndex, approve: bool) {
let who = ensure_signed(origin)?;
ensure!(Self::is_member(&who), Error::<T, I>::NotMember);
Expand Down Expand Up @@ -303,7 +303,7 @@ decl_module! {
/// - `M` is number of members,
/// - `P` is number of active proposals,
/// - `L` is the encoded length of `proposal` preimage.
#[weight = SimpleDispatchInfo::FixedOperational(200_000_000)]
#[weight = (200_000_000, DispatchClass::Operational)]
fn close(origin, proposal: T::Hash, #[compact] index: ProposalIndex) {
let _ = ensure_signed(origin)?;

Expand Down
12 changes: 6 additions & 6 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ use sp_runtime::{
RuntimeDebug,
};
use frame_support::dispatch::{DispatchResult, Dispatchable};
use frame_support::weights::{SimpleDispatchInfo, MINIMUM_WEIGHT};
use frame_support::weights::MINIMUM_WEIGHT;
use frame_support::{
Parameter, decl_module, decl_event, decl_storage, decl_error,
parameter_types, IsSubType, storage::child::{self, ChildInfo},
Expand Down Expand Up @@ -539,7 +539,7 @@ decl_module! {
/// Updates the schedule for metering contracts.
///
/// The schedule must have a greater version than the stored schedule.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn update_schedule(origin, schedule: Schedule) -> DispatchResult {
ensure_root(origin)?;
if <Module<T>>::current_schedule().version >= schedule.version {
Expand All @@ -554,7 +554,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 = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn put_code(
origin,
#[compact] gas_limit: Gas,
Expand Down Expand Up @@ -582,7 +582,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 = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn call(
origin,
dest: <T::Lookup as StaticLookup>::Source,
Expand All @@ -608,7 +608,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 = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
pub fn instantiate(
origin,
#[compact] endowment: BalanceOf<T>,
Expand All @@ -631,7 +631,7 @@ decl_module! {
///
/// If contract is not evicted as a result of this call, no actions are taken and
/// the sender is not eligible for the reward.
#[weight = SimpleDispatchInfo::FixedNormal(MINIMUM_WEIGHT)]
#[weight = MINIMUM_WEIGHT]
fn claim_surcharge(origin, dest: T::AccountId, aux_sender: Option<T::AccountId>) {
let origin = origin.into();
let (signed, rewarded) = match (origin, aux_sender) {
Expand Down
Loading