Migrate the pallet macros of some tests and benches (#9853)

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
Qinxuan Chen
2021-09-27 11:24:25 +08:00
committed by GitHub
parent f8a228859e
commit d565382b50
2 changed files with 126 additions and 100 deletions
+106 -81
View File
@@ -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<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {}
frame_support::decl_module! {
pub struct Module<T: Config> 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<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
// 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<T: Config> frame_support::inherent::ProvideInherent for Module<T> {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(100)]
pub fn some_function(origin: OriginFor<T>) -> 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<T>) -> DispatchResult {
frame_system::ensure_root(origin)?;
Ok(())
}
#[pallet::weight(0)]
pub fn some_unsigned_message(origin: OriginFor<T>) -> DispatchResult {
frame_system::ensure_none(origin)?;
Ok(())
}
#[pallet::weight(0)]
pub fn allowed_unsigned(origin: OriginFor<T>) -> DispatchResult {
frame_system::ensure_root(origin)?;
Ok(())
}
#[pallet::weight(0)]
pub fn unallowed_unsigned(origin: OriginFor<T>) -> DispatchResult {
frame_system::ensure_root(origin)?;
Ok(())
}
#[pallet::weight(0)]
pub fn inherent_call(origin: OriginFor<T>) -> DispatchResult {
let _ = frame_system::ensure_none(origin)?;
Ok(())
}
#[pallet::weight(0)]
pub fn calculate_storage_root(_origin: OriginFor<T>) -> DispatchResult {
let root = sp_io::storage::root();
sp_io::storage::set("storage_root".as_bytes(), &root);
Ok(())
}
}
#[pallet::inherent]
impl<T: Config> ProvideInherent for Pallet<T> {
type Call = Call<T>;
type Error = sp_inherents::MakeFatalError<()>;
const INHERENT_IDENTIFIER: [u8; 8] = *b"test1234";
fn create_inherent(_data: &sp_inherents::InherentData) -> Option<Self::Call> {
fn create_inherent(_data: &InherentData) -> Option<Self::Call> {
None
}
fn is_inherent(call: &Self::Call) -> bool {
*call == Call::<T>::inherent_call {}
}
}
impl<T: Config> sp_runtime::traits::ValidateUnsigned for Module<T> {
#[pallet::validate_unsigned]
impl<T: Config> ValidateUnsigned for Pallet<T> {
type Call = Call<T>;
// 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()),
}
}
}
}