mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 23:31:07 +00:00
frame-support: migrate some tests from decl_* macros to the new pallet macros (#12401)
* frame-support: migrate some tests from decl macros to new pallet attribute macros * Remove useless type alias * Remove useless type alias * Work around for rust issue 52234 Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Use polkadot-compatible paste version Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix crate access and add tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Typo Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: parity-processbot <>
This commit is contained in:
@@ -3532,18 +3532,110 @@ 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};
|
||||
|
||||
#[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::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
|
||||
#[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>;
|
||||
}
|
||||
|
||||
#[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
|
||||
#[pallet::weight(1000)]
|
||||
pub fn f00(_origin: OriginFor<T>) -> DispatchResult {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
#[pallet::weight((1000, DispatchClass::Mandatory))]
|
||||
pub fn f01(_origin: OriginFor<T>) -> DispatchResult {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
#[pallet::weight((1000, Pays::No))]
|
||||
pub fn f02(_origin: OriginFor<T>) -> DispatchResult {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
#[pallet::weight((1000, DispatchClass::Operational, Pays::No))]
|
||||
pub fn f03(_origin: OriginFor<T>) -> DispatchResult {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
// weight = a x 10 + b
|
||||
#[pallet::weight(((_a * 10 + _eb * 1) as u64, DispatchClass::Normal, Pays::Yes))]
|
||||
pub fn f11(_origin: OriginFor<T>, _a: u32, _eb: u32) -> DispatchResult {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
#[pallet::weight((0, DispatchClass::Operational, Pays::Yes))]
|
||||
pub fn f12(_origin: OriginFor<T>, _a: u32, _eb: u32) -> DispatchResult {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TraitImpl {}
|
||||
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 {
|
||||
@@ -3552,92 +3644,65 @@ mod weight_tests {
|
||||
};
|
||||
}
|
||||
|
||||
impl Config for TraitImpl {
|
||||
type RuntimeOrigin = u32;
|
||||
type BlockNumber = u32;
|
||||
type Balance = u32;
|
||||
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 = crate::tests::PanicPalletInfo;
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {
|
||||
// no arguments, fixed weight
|
||||
#[weight = 1000]
|
||||
fn f00(_origin) { unimplemented!(); }
|
||||
|
||||
#[weight = (1000, DispatchClass::Mandatory)]
|
||||
fn f01(_origin) { unimplemented!(); }
|
||||
|
||||
#[weight = (1000, Pays::No)]
|
||||
fn f02(_origin) { unimplemented!(); }
|
||||
|
||||
#[weight = (1000, DispatchClass::Operational, Pays::No)]
|
||||
fn f03(_origin) { 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!(); }
|
||||
|
||||
#[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) + Weight::from_parts(10_000, 0)]
|
||||
fn f20(_origin) { unimplemented!(); }
|
||||
|
||||
#[weight = T::DbWeight::get().reads_writes(6, 5) + Weight::from_parts(40_000, 0)]
|
||||
fn f21(_origin) { unimplemented!(); }
|
||||
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user