diff --git a/substrate/frame/executive/src/lib.rs b/substrate/frame/executive/src/lib.rs index 244253185d..41f679909e 100644 --- a/substrate/frame/executive/src/lib.rs +++ b/substrate/frame/executive/src/lib.rs @@ -575,15 +575,9 @@ where #[cfg(test)] mod tests { use super::*; - use frame_support::{ - assert_err, parameter_types, - traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons}, - weights::{IdentityFee, RuntimeDbWeight, Weight, WeightToFeePolynomial}, - }; - use frame_system::{Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo}; + use hex_literal::hex; - use pallet_balances::Call as BalancesCall; - use pallet_transaction_payment::CurrencyAdapter; + use sp_core::H256; use sp_runtime::{ generic::{DigestItem, Era}, @@ -594,95 +588,135 @@ mod tests { }, DispatchError, }; + + use frame_support::{ + assert_err, parameter_types, + traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons}, + weights::{IdentityFee, RuntimeDbWeight, Weight, WeightToFeePolynomial}, + }; + use frame_system::{Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo}; + use pallet_balances::Call as BalancesCall; + use pallet_transaction_payment::CurrencyAdapter; + const TEST_KEY: &[u8] = &*b":test:key:"; + #[frame_support::pallet] mod custom { - use frame_support::weights::{DispatchClass, Weight}; - use sp_runtime::transaction_validity::{ - TransactionSource, TransactionValidity, TransactionValidityError, UnknownTransaction, - }; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); + + #[pallet::config] pub trait Config: frame_system::Config {} - frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin { - #[weight = 100] - fn some_function(origin) { - // NOTE: does not make any different. - frame_system::ensure_signed(origin)?; - } - #[weight = (200, DispatchClass::Operational)] - fn some_root_operation(origin) { - frame_system::ensure_root(origin)?; - } - #[weight = 0] - fn some_unsigned_message(origin) { - frame_system::ensure_none(origin)?; - } + #[pallet::hooks] + impl Hooks> for Pallet { + // module hooks. + // one with block number arg and one without + fn on_initialize(n: T::BlockNumber) -> Weight { + println!("on_initialize({})", n); + 175 + } - #[weight = 0] - fn allowed_unsigned(origin) { - frame_system::ensure_root(origin)?; - } + fn on_idle(n: T::BlockNumber, remaining_weight: Weight) -> Weight { + println!("on_idle{}, {})", n, remaining_weight); + 175 + } - #[weight = 0] - fn unallowed_unsigned(origin) { - frame_system::ensure_root(origin)?; - } + fn on_finalize(n: T::BlockNumber) { + println!("on_finalize({})", n); + } - #[weight = 0] - fn inherent_call(origin) { - let _ = frame_system::ensure_none(origin)?; - } + fn on_runtime_upgrade() -> Weight { + sp_io::storage::set(super::TEST_KEY, "module".as_bytes()); + 200 + } - // module hooks. - // one with block number arg and one without - fn on_initialize(n: T::BlockNumber) -> Weight { - println!("on_initialize({})", n); - 175 - } - - fn on_idle(n: T::BlockNumber, remaining_weight: Weight) -> Weight { - println!("on_idle{}, {})", n, remaining_weight); - 175 - } - - fn on_finalize() { - println!("on_finalize(?)"); - } - - fn on_runtime_upgrade() -> Weight { - sp_io::storage::set(super::TEST_KEY, "module".as_bytes()); - 200 - } - - fn offchain_worker(n: T::BlockNumber) { - assert_eq!(T::BlockNumber::from(1u32), n); - } - - #[weight = 0] - fn calculate_storage_root(_origin) { - let root = sp_io::storage::root(); - sp_io::storage::set("storage_root".as_bytes(), &root); - } + fn offchain_worker(n: T::BlockNumber) { + assert_eq!(T::BlockNumber::from(1u32), n); } } - impl frame_support::inherent::ProvideInherent for Module { + #[pallet::call] + impl Pallet { + #[pallet::weight(100)] + pub fn some_function(origin: OriginFor) -> DispatchResult { + // NOTE: does not make any different. + frame_system::ensure_signed(origin)?; + Ok(()) + } + + #[pallet::weight((200, DispatchClass::Operational))] + pub fn some_root_operation(origin: OriginFor) -> DispatchResult { + frame_system::ensure_root(origin)?; + Ok(()) + } + + #[pallet::weight(0)] + pub fn some_unsigned_message(origin: OriginFor) -> DispatchResult { + frame_system::ensure_none(origin)?; + Ok(()) + } + + #[pallet::weight(0)] + pub fn allowed_unsigned(origin: OriginFor) -> DispatchResult { + frame_system::ensure_root(origin)?; + Ok(()) + } + + #[pallet::weight(0)] + pub fn unallowed_unsigned(origin: OriginFor) -> DispatchResult { + frame_system::ensure_root(origin)?; + Ok(()) + } + + #[pallet::weight(0)] + pub fn inherent_call(origin: OriginFor) -> DispatchResult { + let _ = frame_system::ensure_none(origin)?; + Ok(()) + } + + #[pallet::weight(0)] + pub fn calculate_storage_root(_origin: OriginFor) -> DispatchResult { + let root = sp_io::storage::root(); + sp_io::storage::set("storage_root".as_bytes(), &root); + Ok(()) + } + } + + #[pallet::inherent] + impl ProvideInherent for Pallet { type Call = Call; + type Error = sp_inherents::MakeFatalError<()>; + const INHERENT_IDENTIFIER: [u8; 8] = *b"test1234"; - fn create_inherent(_data: &sp_inherents::InherentData) -> Option { + + fn create_inherent(_data: &InherentData) -> Option { None } + fn is_inherent(call: &Self::Call) -> bool { *call == Call::::inherent_call {} } } - impl sp_runtime::traits::ValidateUnsigned for Module { + #[pallet::validate_unsigned] + impl ValidateUnsigned for Pallet { type Call = Call; + // Inherent call is accepted for being dispatched + fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> { + match call { + Call::allowed_unsigned { .. } => Ok(()), + Call::inherent_call { .. } => Ok(()), + _ => Err(UnknownTransaction::NoUnsignedValidator.into()), + } + } + // Inherent call is not validated as unsigned fn validate_unsigned( _source: TransactionSource, @@ -693,15 +727,6 @@ mod tests { _ => UnknownTransaction::NoUnsignedValidator.into(), } } - - // Inherent call is accepted for being dispatched - fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> { - match call { - Call::allowed_unsigned { .. } => Ok(()), - Call::inherent_call { .. } => Ok(()), - _ => Err(UnknownTransaction::NoUnsignedValidator.into()), - } - } } } diff --git a/substrate/frame/system/benches/bench.rs b/substrate/frame/system/benches/bench.rs index 97c19c5e81..c8a9d4eadf 100644 --- a/substrate/frame/system/benches/bench.rs +++ b/substrate/frame/system/benches/bench.rs @@ -16,8 +16,6 @@ // limitations under the License. use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use frame_support::{decl_event, decl_module}; -use frame_system as system; use sp_core::H256; use sp_runtime::{ testing::Header, @@ -25,24 +23,24 @@ use sp_runtime::{ Perbill, }; +#[frame_support::pallet] mod module { - use super::*; + use frame_support::pallet_prelude::*; - pub trait Config: system::Config { - type Event: From + Into<::Event>; + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config { + type Event: From + IsType<::Event>; } - decl_module! { - pub struct Module for enum Call where origin: T::Origin { - pub fn deposit_event() = default; - } + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + Complex(Vec, u32, u16, u128), } - - decl_event!( - pub enum Event { - Complex(Vec, u32, u16, u128), - } - ); } type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; @@ -55,7 +53,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Pallet, Call, Config, Storage, Event}, - Module: module::{Pallet, Call, Event}, + Module: module::{Pallet, Event}, } ); @@ -70,7 +68,7 @@ frame_support::parameter_types! { 4 * 1024 * 1024, Perbill::from_percent(75), ); } -impl system::Config for Runtime { +impl frame_system::Config for Runtime { type BaseCallFilter = frame_support::traits::Everything; type BlockWeights = (); type BlockLength = BlockLength; @@ -101,14 +99,17 @@ impl module::Config for Runtime { } fn new_test_ext() -> sp_io::TestExternalities { - system::GenesisConfig::default().build_storage::().unwrap().into() + frame_system::GenesisConfig::default() + .build_storage::() + .unwrap() + .into() } fn deposit_events(n: usize) { let mut t = new_test_ext(); t.execute_with(|| { for _ in 0..n { - module::Module::::deposit_event(module::Event::Complex( + module::Pallet::::deposit_event(module::Event::Complex( vec![1, 2, 3], 2, 3,