extern crate alloc;
use super::*;
use crate::Pallet;
use frame_benchmarking::v2::*;
use frame_support::dispatch::{DispatchInfo, PostDispatchInfo};
use frame_system::{EventRecord, RawOrigin};
use sp_runtime::traits::{AsTransactionAuthorizedOrigin, DispatchTransaction, Dispatchable};
pub trait Config: crate::Config {
fn setup_benchmark_environment() {}
}
fn assert_last_event<T: crate::Config>(generic_event: <T as crate::Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}
#[benchmarks(where
T: Config,
T::RuntimeOrigin: AsTransactionAuthorizedOrigin,
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
)]
mod benchmarks {
use super::*;
#[benchmark]
fn charge_transaction_payment() {
<T as Config>::setup_benchmark_environment();
let caller: T::AccountId = account("caller", 0, 0);
let existential_deposit =
<T::OnChargeTransaction as OnChargeTransaction<T>>::minimum_balance();
let min_tip: BalanceOf<T> = 1_000_000_000u32.into();
let tip = if existential_deposit.is_zero() { min_tip } else { existential_deposit };
let ext: ChargeTransactionPayment<T> = ChargeTransactionPayment::from(tip);
let inner = frame_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,
};
let len: u32 = 10;
let expected_fee = Pallet::<T>::compute_fee(len, &info, tip);
let amount_to_endow = expected_fee.max(existential_deposit).saturating_mul(10u32.into());
<T::OnChargeTransaction as OnChargeTransaction<T>>::endow_account(&caller, amount_to_endow);
#[block]
{
assert!(ext
.test_run(
RawOrigin::Signed(caller.clone()).into(),
&call,
&info,
len as usize,
0,
|_| Ok(post_info)
)
.unwrap()
.is_ok());
}
post_info.actual_weight.as_mut().map(|w| w.saturating_accrue(extension_weight));
let actual_fee = Pallet::<T>::compute_actual_fee(len, &info, &post_info, tip);
assert_last_event::<T>(
Event::<T>::TransactionFeePaid { who: caller, actual_fee, tip }.into(),
);
}
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
}