mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 13:31:10 +00:00
Migrate the pallet macros of some tests and benches (#9853)
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
@@ -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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Event> + Into<<Self as system::Config>::Event>;
|
||||
#[pallet::pallet]
|
||||
#[pallet::generate_store(pub(super) trait Store)]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {
|
||||
type Event: From<Event> + IsType<<Self as frame_system::Config>::Event>;
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Config> 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<u8>, u32, u16, u128),
|
||||
}
|
||||
|
||||
decl_event!(
|
||||
pub enum Event {
|
||||
Complex(Vec<u8>, u32, u16, u128),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
|
||||
@@ -55,7 +53,7 @@ frame_support::construct_runtime!(
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
|
||||
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::<Runtime>().unwrap().into()
|
||||
frame_system::GenesisConfig::default()
|
||||
.build_storage::<Runtime>()
|
||||
.unwrap()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn deposit_events(n: usize) {
|
||||
let mut t = new_test_ext();
|
||||
t.execute_with(|| {
|
||||
for _ in 0..n {
|
||||
module::Module::<Runtime>::deposit_event(module::Event::Complex(
|
||||
module::Pallet::<Runtime>::deposit_event(module::Event::Complex(
|
||||
vec![1, 2, 3],
|
||||
2,
|
||||
3,
|
||||
|
||||
Reference in New Issue
Block a user