Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
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
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

183 changes: 124 additions & 59 deletions frame/support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3532,112 +3532,177 @@ mod tests {
#[allow(dead_code)]
mod weight_tests {
use super::{tests::*, *};
use sp_core::{parameter_types, Get};
use sp_core::parameter_types;
use sp_runtime::{generic, traits::BlakeTwo256};
use sp_weights::RuntimeDbWeight;

pub trait Config: 'static {
type RuntimeOrigin;
type Balance;
type BlockNumber;
type DbWeight: Get<RuntimeDbWeight>;
type PalletInfo: crate::traits::PalletInfo;
}
pub use self::frame_system::{Call, Config, Pallet};

pub struct TraitImpl {}
#[crate::pallet(dev_mode)]
pub mod frame_system {
use super::{frame_system, frame_system::pallet_prelude::*};
pub use crate::dispatch::RawOrigin;
use crate::pallet_prelude::*;

parameter_types! {
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
read: 100,
write: 1000,
};
}
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);

impl Config for TraitImpl {
type RuntimeOrigin = u32;
type BlockNumber = u32;
type Balance = u32;
type DbWeight = DbWeight;
type PalletInfo = crate::tests::PanicPalletInfo;
}
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: 'static {
type BlockNumber: Parameter + Default + MaxEncodedLen;
type AccountId;
type Balance;
type BaseCallFilter: crate::traits::Contains<Self::RuntimeCall>;
type RuntimeOrigin;
type RuntimeCall;
type PalletInfo: crate::traits::PalletInfo;
type DbWeight: Get<crate::weights::RuntimeDbWeight>;
}

decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {
#[pallet::error]
pub enum Error<T> {
/// Required by construct_runtime
CallFiltered,
}

#[pallet::origin]
pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;

#[pallet::call]
impl<T: Config> Pallet<T> {
// no arguments, fixed weight
#[weight = 1000]
fn f00(_origin) { unimplemented!(); }
#[pallet::weight(1000)]
pub fn f00(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}

#[weight = (1000, DispatchClass::Mandatory)]
fn f01(_origin) { unimplemented!(); }
#[pallet::weight((1000, DispatchClass::Mandatory))]
pub fn f01(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}

#[weight = (1000, Pays::No)]
fn f02(_origin) { unimplemented!(); }
#[pallet::weight((1000, Pays::No))]
pub fn f02(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}

#[weight = (1000, DispatchClass::Operational, Pays::No)]
fn f03(_origin) { unimplemented!(); }
#[pallet::weight((1000, DispatchClass::Operational, Pays::No))]
pub fn f03(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}

// weight = a x 10 + b
#[weight = ((_a * 10 + _eb * 1) as u64, DispatchClass::Normal, Pays::Yes)]
fn f11(_origin, _a: u32, _eb: u32) { unimplemented!(); }
#[pallet::weight(((_a * 10 + _eb * 1) as u64, DispatchClass::Normal, Pays::Yes))]
pub fn f11(_origin: OriginFor<T>, _a: u32, _eb: u32) -> DispatchResult {
unimplemented!();
}

#[weight = (0, DispatchClass::Operational, Pays::Yes)]
fn f12(_origin, _a: u32, _eb: u32) { unimplemented!(); }
#[pallet::weight((0, DispatchClass::Operational, Pays::Yes))]
pub fn f12(_origin: OriginFor<T>, _a: u32, _eb: u32) -> DispatchResult {
unimplemented!();
}

#[weight = T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + Weight::from_parts(10_000, 0)]
fn f20(_origin) { unimplemented!(); }
#[pallet::weight(T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + Weight::from_all(10_000))]
pub fn f20(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}

#[pallet::weight(T::DbWeight::get().reads_writes(6, 5) + Weight::from_all(40_000))]
pub fn f21(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}
}

pub mod pallet_prelude {
pub type OriginFor<T> = <T as super::Config>::RuntimeOrigin;
}
}

#[weight = T::DbWeight::get().reads_writes(6, 5) + Weight::from_parts(40_000, 0)]
fn f21(_origin) { unimplemented!(); }
type BlockNumber = u32;
type AccountId = u32;
type Balance = u32;
type Header = generic::Header<BlockNumber, BlakeTwo256>;
type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, (), ()>;
type Block = generic::Block<Header, UncheckedExtrinsic>;

crate::construct_runtime!(
pub enum Runtime
where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: self::frame_system,
}
);

parameter_types! {
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
read: 100,
write: 1000,
};
}

impl Config for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Balance = Balance;
type BaseCallFilter = crate::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type DbWeight = DbWeight;
type PalletInfo = PalletInfo;
}

#[test]
fn weights_are_correct() {
// #[weight = 1000]
let info = Call::<TraitImpl>::f00 {}.get_dispatch_info();
// #[pallet::weight(1000)]
let info = Call::<Runtime>::f00 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(1000, 0));
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::Yes);

// #[weight = (1000, DispatchClass::Mandatory)]
let info = Call::<TraitImpl>::f01 {}.get_dispatch_info();
// #[pallet::weight((1000, DispatchClass::Mandatory))]
let info = Call::<Runtime>::f01 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(1000, 0));
assert_eq!(info.class, DispatchClass::Mandatory);
assert_eq!(info.pays_fee, Pays::Yes);

// #[weight = (1000, Pays::No)]
let info = Call::<TraitImpl>::f02 {}.get_dispatch_info();
// #[pallet::weight((1000, Pays::No))]
let info = Call::<Runtime>::f02 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(1000, 0));
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::No);

// #[weight = (1000, DispatchClass::Operational, Pays::No)]
let info = Call::<TraitImpl>::f03 {}.get_dispatch_info();
// #[pallet::weight((1000, DispatchClass::Operational, Pays::No))]
let info = Call::<Runtime>::f03 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(1000, 0));
assert_eq!(info.class, DispatchClass::Operational);
assert_eq!(info.pays_fee, Pays::No);

// #[weight = ((_a * 10 + _eb * 1) as Weight, DispatchClass::Normal, Pays::Yes)]
let info = Call::<TraitImpl>::f11 { _a: 13, _eb: 20 }.get_dispatch_info();
// #[pallet::weight(((_a * 10 + _eb * 1) as u64, DispatchClass::Normal, Pays::Yes))]
let info = Call::<Runtime>::f11 { a: 13, eb: 20 }.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(150, 0)); // 13*10 + 20
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::Yes);

// #[weight = (0, DispatchClass::Operational, Pays::Yes)]
let info = Call::<TraitImpl>::f12 { _a: 10, _eb: 20 }.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(0, 0));
// #[pallet::weight((0, DispatchClass::Operational, Pays::Yes))]
let info = Call::<Runtime>::f12 { a: 10, eb: 20 }.get_dispatch_info();
assert_eq!(info.weight, Weight::zero());
assert_eq!(info.class, DispatchClass::Operational);
assert_eq!(info.pays_fee, Pays::Yes);

// #[weight = T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + 10_000]
let info = Call::<TraitImpl>::f20 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(12300, 0)); // 100*3 + 1000*2 + 10_1000
// #[pallet::weight(T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) +
// Weight::from_all(10_000))]
let info = Call::<Runtime>::f20 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(12300, 10000)); // 100*3 + 1000*2 + 10_1000
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::Yes);

// #[weight = T::DbWeight::get().reads_writes(6, 5) + 40_000]
let info = Call::<TraitImpl>::f21 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(45600, 0)); // 100*6 + 1000*5 + 40_1000
// #[pallet::weight(T::DbWeight::get().reads_writes(6, 5) + Weight::from_all(40_000))]
let info = Call::<Runtime>::f21 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(45600, 40000)); // 100*6 + 1000*5 + 40_1000
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::Yes);
}
Expand Down
Loading