pallet_transaction_payment/
benchmarking.rs1extern crate alloc;
21
22use super::*;
23use crate::Pallet;
24use frame_benchmarking::v2::*;
25use frame_support::dispatch::{DispatchInfo, PostDispatchInfo};
26use frame_system::{EventRecord, RawOrigin};
27use sp_runtime::traits::{AsTransactionAuthorizedOrigin, DispatchTransaction, Dispatchable};
28
29fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
30 let events = frame_system::Pallet::<T>::events();
31 let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
32 let EventRecord { event, .. } = &events[events.len() - 1];
34 assert_eq!(event, &system_event);
35}
36
37#[benchmarks(where
38 T: Config,
39 T::RuntimeOrigin: AsTransactionAuthorizedOrigin,
40 T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
41)]
42mod benchmarks {
43 use super::*;
44
45 #[benchmark]
46 fn charge_transaction_payment() {
47 let caller: T::AccountId = account("caller", 0, 0);
48 let existential_deposit =
49 <T::OnChargeTransaction as OnChargeTransaction<T>>::minimum_balance();
50
51 let (amount_to_endow, tip) = if existential_deposit.is_zero() {
52 let min_tip: <<T as pallet::Config>::OnChargeTransaction as payment::OnChargeTransaction<T>>::Balance = 1_000_000_000u32.into();
53 (min_tip * 1000u32.into(), min_tip)
54 } else {
55 (existential_deposit * 1000u32.into(), existential_deposit)
56 };
57
58 <T::OnChargeTransaction as OnChargeTransaction<T>>::endow_account(&caller, amount_to_endow);
59
60 let ext: ChargeTransactionPayment<T> = ChargeTransactionPayment::from(tip);
61 let inner = frame_system::Call::remark { remark: alloc::vec![] };
62 let call = T::RuntimeCall::from(inner);
63 let extension_weight = ext.weight(&call);
64 let info = DispatchInfo {
65 call_weight: Weight::from_parts(100, 0),
66 extension_weight,
67 class: DispatchClass::Operational,
68 pays_fee: Pays::Yes,
69 };
70 let mut post_info = PostDispatchInfo {
71 actual_weight: Some(Weight::from_parts(10, 0)),
72 pays_fee: Pays::Yes,
73 };
74
75 #[block]
76 {
77 assert!(ext
78 .test_run(RawOrigin::Signed(caller.clone()).into(), &call, &info, 10, 0, |_| Ok(
79 post_info
80 ))
81 .unwrap()
82 .is_ok());
83 }
84
85 post_info.actual_weight.as_mut().map(|w| w.saturating_accrue(extension_weight));
86 let actual_fee = Pallet::<T>::compute_actual_fee(10, &info, &post_info, tip);
87 assert_last_event::<T>(
88 Event::<T>::TransactionFeePaid { who: caller, actual_fee, tip }.into(),
89 );
90 }
91
92 impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
93}