// This file is part of Bizinikiwi. // Copyright (C) Parity Technologies (UK) Ltd. // 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. #![cfg(feature = "runtime-benchmarks")] use super::*; use pezframe_benchmarking::v2::*; use pezframe_support::traits::UnfilteredDispatchable; use pezsp_runtime::impl_tx_ext_default; pub mod types { use super::*; use pezframe_support::traits::OriginTrait; use pezsp_runtime::traits::DispatchInfoOf; type CallOf = ::RuntimeCall; /// A weightless extension to facilitate the bare dispatch benchmark. #[derive(TypeInfo, Eq, PartialEq, Clone, Encode, Decode, DecodeWithMemTracking)] #[scale_info(skip_type_params(T))] pub struct WeightlessExtension(core::marker::PhantomData); impl core::fmt::Debug for WeightlessExtension { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { write!(f, "WeightlessExtension") } } impl Default for WeightlessExtension { fn default() -> Self { WeightlessExtension(Default::default()) } } impl TransactionExtension> for WeightlessExtension { const IDENTIFIER: &'static str = "WeightlessExtension"; type Implicit = (); type Pre = (); type Val = (); fn weight(&self, _call: &CallOf) -> Weight { Weight::from_all(0) } fn validate( &self, mut origin: as Dispatchable>::RuntimeOrigin, _: &CallOf, _: &DispatchInfoOf>, _: usize, _: (), _: &impl Encode, _: TransactionSource, ) -> Result< (ValidTransaction, Self::Val, as Dispatchable>::RuntimeOrigin), TransactionValidityError, > { origin.set_caller_from_signed(whitelisted_caller()); Ok((ValidTransaction::default(), (), origin)) } impl_tx_ext_default!(CallOf; prepare); } } fn assert_last_event(generic_event: ::RuntimeEvent) { pezframe_system::Pezpallet::::assert_last_event(generic_event.into()); } #[benchmarks( where T: Config, ::Extension: Default, )] mod benchmarks { use super::*; #[benchmark] fn bare_dispatch() { let meta_call = pezframe_system::Call::::remark { remark: vec![] }.into(); let meta_ext = T::Extension::default(); let meta_ext_weight = meta_ext.weight(&meta_call); #[cfg(not(test))] assert!( meta_ext_weight.is_zero(), "meta tx extension weight for the benchmarks must be zero. \ use `pezpallet_meta_tx::WeightlessExtension` as `pezpallet_meta_tx::Config::Extension` \ with the `runtime-benchmarks` feature enabled.", ); let meta_tx = MetaTxFor::::new(meta_call.clone(), 0u8, meta_ext.clone()); let caller = whitelisted_caller(); let origin: ::RuntimeOrigin = pezframe_system::RawOrigin::Signed(caller).into(); let call = Call::::dispatch { meta_tx: Box::new(meta_tx) }; #[block] { let _ = call.dispatch_bypass_filter(origin); } let info = meta_call.get_dispatch_info(); assert_last_event::( Event::Dispatched { result: Ok(PostDispatchInfo { actual_weight: Some(info.call_weight + meta_ext_weight), pays_fee: Pays::Yes, }), } .into(), ); } impl_benchmark_test_suite! { Pezpallet, crate::mock::new_test_ext(), crate::mock::Runtime, } }