// 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. //! Benchmarks for Transaction Payment Pallet's transaction extension extern crate alloc; use super::*; use crate::Pallet; use pezframe_benchmarking::v2::*; use pezframe_support::dispatch::{DispatchInfo, PostDispatchInfo}; use pezframe_system::{EventRecord, RawOrigin}; use pezsp_runtime::traits::{AsTransactionAuthorizedOrigin, DispatchTransaction, Dispatchable}; fn assert_last_event(generic_event: ::RuntimeEvent) { let events = pezframe_system::Pallet::::events(); let system_event: ::RuntimeEvent = generic_event.into(); // compare to the last event record let EventRecord { event, .. } = &events[events.len() - 1]; assert_eq!(event, &system_event); } #[benchmarks(where T: Config, T::RuntimeOrigin: AsTransactionAuthorizedOrigin, T::RuntimeCall: Dispatchable, )] mod benchmarks { use super::*; #[benchmark] fn charge_transaction_payment() { let caller: T::AccountId = account("caller", 0, 0); let existential_deposit = >::minimum_balance(); let (amount_to_endow, tip) = if existential_deposit.is_zero() { let min_tip: <::OnChargeTransaction as payment::OnChargeTransaction>::Balance = 1_000_000_000u32.into(); (min_tip * 1000u32.into(), min_tip) } else { (existential_deposit * 1000u32.into(), existential_deposit) }; >::endow_account(&caller, amount_to_endow); let ext: ChargeTransactionPayment = ChargeTransactionPayment::from(tip); let inner = pezframe_system::Call::remark { remark: alloc::vec![] }; let call = T::RuntimeCall::from(inner); let extension_weight = ext.weight(&call); let info = DispatchInfo { call_weight: Weight::from_parts(100, 0), extension_weight, class: DispatchClass::Operational, pays_fee: Pays::Yes, }; let mut post_info = PostDispatchInfo { actual_weight: Some(Weight::from_parts(10, 0)), pays_fee: Pays::Yes, }; #[block] { assert!(ext .test_run(RawOrigin::Signed(caller.clone()).into(), &call, &info, 10, 0, |_| Ok( post_info )) .unwrap() .is_ok()); } post_info.actual_weight.as_mut().map(|w| w.saturating_accrue(extension_weight)); let actual_fee = Pallet::::compute_actual_fee(10, &info, &post_info, tip); assert_last_event::( Event::::TransactionFeePaid { who: caller, actual_fee, tip }.into(), ); } impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime); }