// This file is part of Bizinikiwi. // Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // Benchmarks for System Extensions #![cfg(feature = "runtime-benchmarks")] use alloc::vec; use pezframe_benchmarking::{account, v2::*, BenchmarkError}; use pezframe_support::{ dispatch::{DispatchClass, DispatchInfo, PostDispatchInfo}, pezpallet_prelude::Zero, weights::Weight, }; use pezframe_system::{ pezpallet_prelude::*, CheckGenesis, CheckMortality, CheckNonZeroSender, CheckNonce, CheckSpecVersion, CheckTxVersion, CheckWeight, Config, ExtensionsWeightInfo, Pezpallet as System, RawOrigin, WeightReclaim, }; use pezsp_runtime::{ generic::Era, traits::{ AsSystemOriginSigner, AsTransactionAuthorizedOrigin, DispatchTransaction, Dispatchable, Get, }, }; pub struct Pezpallet(System); #[benchmarks(where T: Send + Sync, T::RuntimeCall: Dispatchable, ::RuntimeOrigin: AsSystemOriginSigner + AsTransactionAuthorizedOrigin + Clone, )] mod benchmarks { use super::*; #[benchmark] fn check_genesis() -> Result<(), BenchmarkError> { let len = 0_usize; let caller = account("caller", 0, 0); let info = DispatchInfo { call_weight: Weight::zero(), ..Default::default() }; let call: T::RuntimeCall = pezframe_system::Call::remark { remark: vec![] }.into(); pezframe_benchmarking::benchmarking::add_to_whitelist( pezframe_system::BlockHash::::hashed_key_for(BlockNumberFor::::zero()).into(), ); #[block] { CheckGenesis::::new() .test_run(RawOrigin::Signed(caller).into(), &call, &info, len, 0, |_| Ok(().into())) .unwrap() .unwrap(); } Ok(()) } #[benchmark] fn check_mortality_mortal_transaction() -> Result<(), BenchmarkError> { let len = 0_usize; let ext = CheckMortality::::from(Era::mortal(16, 256)); let block_number: BlockNumberFor = 17u32.into(); System::::set_block_number(block_number); let prev_block: BlockNumberFor = 16u32.into(); let default_hash: T::Hash = Default::default(); pezframe_system::BlockHash::::insert(prev_block, default_hash); let caller = account("caller", 0, 0); let info = DispatchInfo { call_weight: Weight::from_parts(100, 0), class: DispatchClass::Normal, ..Default::default() }; let call: T::RuntimeCall = pezframe_system::Call::remark { remark: vec![] }.into(); pezframe_benchmarking::benchmarking::add_to_whitelist( pezframe_system::BlockHash::::hashed_key_for(prev_block).into(), ); #[block] { ext.test_run(RawOrigin::Signed(caller).into(), &call, &info, len, 0, |_| Ok(().into())) .unwrap() .unwrap(); } Ok(()) } #[benchmark] fn check_mortality_immortal_transaction() -> Result<(), BenchmarkError> { let len = 0_usize; let ext = CheckMortality::::from(Era::immortal()); let block_number: BlockNumberFor = 17u32.into(); System::::set_block_number(block_number); let prev_block: BlockNumberFor = 16u32.into(); let default_hash: T::Hash = Default::default(); pezframe_system::BlockHash::::insert(prev_block, default_hash); let genesis_block: BlockNumberFor = 0u32.into(); pezframe_system::BlockHash::::insert(genesis_block, default_hash); let caller = account("caller", 0, 0); let info = DispatchInfo { call_weight: Weight::from_parts(100, 0), class: DispatchClass::Normal, ..Default::default() }; let call: T::RuntimeCall = pezframe_system::Call::remark { remark: vec![] }.into(); pezframe_benchmarking::benchmarking::add_to_whitelist( pezframe_system::BlockHash::::hashed_key_for(BlockNumberFor::::zero()).into(), ); #[block] { ext.test_run(RawOrigin::Signed(caller).into(), &call, &info, len, 0, |_| Ok(().into())) .unwrap() .unwrap(); } Ok(()) } #[benchmark] fn check_non_zero_sender() -> Result<(), BenchmarkError> { let len = 0_usize; let ext = CheckNonZeroSender::::new(); let caller = account("caller", 0, 0); let info = DispatchInfo { call_weight: Weight::zero(), ..Default::default() }; let call: T::RuntimeCall = pezframe_system::Call::remark { remark: vec![] }.into(); #[block] { ext.test_run(RawOrigin::Signed(caller).into(), &call, &info, len, 0, |_| Ok(().into())) .unwrap() .unwrap(); } Ok(()) } #[benchmark] fn check_nonce() -> Result<(), BenchmarkError> { let caller: T::AccountId = account("caller", 0, 0); let mut info = pezframe_system::AccountInfo::default(); info.nonce = 1u32.into(); info.providers = 1; let expected_nonce = info.nonce + 1u32.into(); pezframe_system::Account::::insert(caller.clone(), info); let len = 0_usize; let ext = CheckNonce::::from(1u32.into()); let info = DispatchInfo { call_weight: Weight::zero(), ..Default::default() }; let call: T::RuntimeCall = pezframe_system::Call::remark { remark: vec![] }.into(); #[block] { ext.test_run(RawOrigin::Signed(caller.clone()).into(), &call, &info, len, 0, |_| { Ok(().into()) }) .unwrap() .unwrap(); } let updated_info = pezframe_system::Account::::get(caller.clone()); assert_eq!(updated_info.nonce, expected_nonce); Ok(()) } #[benchmark] fn check_spec_version() -> Result<(), BenchmarkError> { let len = 0_usize; let caller = account("caller", 0, 0); let info = DispatchInfo { call_weight: Weight::zero(), ..Default::default() }; let call: T::RuntimeCall = pezframe_system::Call::remark { remark: vec![] }.into(); #[block] { CheckSpecVersion::::new() .test_run(RawOrigin::Signed(caller).into(), &call, &info, len, 0, |_| Ok(().into())) .unwrap() .unwrap(); } Ok(()) } #[benchmark] fn check_tx_version() -> Result<(), BenchmarkError> { let len = 0_usize; let caller = account("caller", 0, 0); let info = DispatchInfo { call_weight: Weight::zero(), ..Default::default() }; let call: T::RuntimeCall = pezframe_system::Call::remark { remark: vec![] }.into(); #[block] { CheckTxVersion::::new() .test_run(RawOrigin::Signed(caller).into(), &call, &info, len, 0, |_| Ok(().into())) .unwrap() .unwrap(); } Ok(()) } #[benchmark] fn check_weight() -> Result<(), BenchmarkError> { let caller = account("caller", 0, 0); let base_extrinsic = ::BlockWeights::get() .get(DispatchClass::Normal) .base_extrinsic; let extension_weight = ::ExtensionsWeightInfo::check_weight(); let info = DispatchInfo { call_weight: Weight::from_parts(base_extrinsic.ref_time() * 5, 0), extension_weight, class: DispatchClass::Normal, ..Default::default() }; let call: T::RuntimeCall = pezframe_system::Call::remark { remark: vec![] }.into(); let post_info = PostDispatchInfo { actual_weight: Some(Weight::from_parts(base_extrinsic.ref_time() * 2, 0)), pays_fee: Default::default(), }; let len = 0_usize; let base_extrinsic = ::BlockWeights::get() .get(DispatchClass::Normal) .base_extrinsic; let ext = CheckWeight::::new(); let initial_block_weight = Weight::from_parts(base_extrinsic.ref_time() * 2, 0); pezframe_system::BlockWeight::::mutate(|current_weight| { current_weight.set(Weight::zero(), DispatchClass::Mandatory); current_weight.set(initial_block_weight, DispatchClass::Normal); }); #[block] { ext.test_run(RawOrigin::Signed(caller).into(), &call, &info, len, 0, |_| Ok(post_info)) .unwrap() .unwrap(); } assert_eq!( System::::block_weight().total(), initial_block_weight + base_extrinsic + post_info.actual_weight.unwrap().saturating_add(extension_weight), ); Ok(()) } #[benchmark] fn weight_reclaim() -> Result<(), BenchmarkError> { let caller = account("caller", 0, 0); let base_extrinsic = ::BlockWeights::get() .get(DispatchClass::Normal) .base_extrinsic; let extension_weight = ::ExtensionsWeightInfo::weight_reclaim(); let info = DispatchInfo { call_weight: Weight::from_parts(base_extrinsic.ref_time() * 5, 0), extension_weight, class: DispatchClass::Normal, ..Default::default() }; let call: T::RuntimeCall = pezframe_system::Call::remark { remark: vec![] }.into(); let post_info = PostDispatchInfo { actual_weight: Some(Weight::from_parts(base_extrinsic.ref_time() * 2, 0)), pays_fee: Default::default(), }; let len = 0_usize; let ext = WeightReclaim::::new(); let initial_block_weight = Weight::from_parts(base_extrinsic.ref_time() * 2, 0); pezframe_system::BlockWeight::::mutate(|current_weight| { current_weight.set(Weight::zero(), DispatchClass::Mandatory); current_weight.set(initial_block_weight, DispatchClass::Normal); current_weight.accrue(base_extrinsic + info.total_weight(), DispatchClass::Normal); }); #[block] { ext.test_run(RawOrigin::Signed(caller).into(), &call, &info, len, 0, |_| Ok(post_info)) .unwrap() .unwrap(); } assert_eq!( System::::block_weight().total(), initial_block_weight + base_extrinsic + post_info.actual_weight.unwrap().saturating_add(extension_weight), ); Ok(()) } impl_benchmark_test_suite!(Pezpallet, crate::mock::new_test_ext(), crate::mock::Test,); }