#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, v2::*};
use frame_support::traits::fungible::Mutate;
use sp_runtime::traits::One;
fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_has_event(generic_event.into());
}
#[benchmarks(
where
IdtyId<T>: From<u32>,
BalanceOf<T>: From<u64>,
T::AccountId: From<[u8; 32]>,
)]
mod benchmarks {
use super::*;
#[benchmark]
fn queue_refund() {
let account: T::AccountId = account("Alice", 1, 1);
let dummy_refund = Refund {
account: account.clone(),
identity: 0u32.into(),
amount: 20u64.into(),
};
let refund = Refund {
account,
identity: 1u32.into(),
amount: 10u64.into(),
};
for _ in 0..MAX_QUEUED_REFUNDS - 1 {
Pallet::<T>::queue_refund(dummy_refund.clone())
}
#[block]
{
Pallet::<T>::queue_refund(refund.clone());
}
assert_eq!(RefundQueue::<T>::get().last(), Some(refund).as_ref());
assert_eq!(RefundQueue::<T>::get().len() as u32, MAX_QUEUED_REFUNDS);
}
#[benchmark]
fn spend_quota() {
let idty_id: IdtyId<T> = 1u32.into();
let amount = 2u64;
let quota_amount = 10u64;
IdtyQuota::<T>::insert(
idty_id,
Quota {
last_use: BlockNumberFor::<T>::zero(),
amount: quota_amount.into(),
},
);
#[block]
{
Pallet::<T>::spend_quota(idty_id, amount.into());
}
let quota_growth =
sp_runtime::Perbill::from_rational(BlockNumberFor::<T>::one(), T::ReloadRate::get())
.mul_floor(T::MaxQuota::get());
assert_eq!(
IdtyQuota::<T>::get(idty_id).unwrap().amount,
quota_growth + quota_amount.into() - amount.into()
);
}
#[benchmark]
fn try_refund() {
let account: T::AccountId = account("Alice", 1, 1);
let idty_id: IdtyId<T> = 1u32.into();
IdtyQuota::<T>::insert(
idty_id,
Quota {
last_use: BlockNumberFor::<T>::zero(),
amount: 10u64.into(),
},
);
let _ = CurrencyOf::<T>::set_balance(&T::RefundAccount::get(), u32::MAX.into());
let refund = Refund {
account: account.clone(),
identity: 1u32.into(),
amount: 10u64.into(),
};
#[block]
{
Pallet::<T>::try_refund(refund);
}
assert_has_event::<T>(Event::<T>::RefundFailed(account).into());
}
#[benchmark]
fn do_refund() {
let account: T::AccountId = account("Alice", 1, 1);
let _ = CurrencyOf::<T>::set_balance(&T::RefundAccount::get(), u32::MAX.into());
let refund = Refund {
account: account.clone(),
identity: 1u32.into(),
amount: 10u64.into(),
};
#[block]
{
Pallet::<T>::try_refund(refund);
}
assert_has_event::<T>(Event::<T>::RefundFailed(account).into());
}
#[benchmark]
fn on_process_refund_queue() {
assert_eq!(RefundQueue::<T>::get().len() as u32, 0);
#[block]
{
Pallet::<T>::process_refund_queue(Weight::MAX);
}
}
#[benchmark]
fn on_process_refund_queue_elements(i: Linear<1, MAX_QUEUED_REFUNDS>) {
let account: T::AccountId = account("Alice", 1, 1);
let idty_id: IdtyId<T> = 1u32.into();
IdtyQuota::<T>::insert(
idty_id,
Quota {
last_use: BlockNumberFor::<T>::zero(),
amount: 10u64.into(),
},
);
let _ = CurrencyOf::<T>::set_balance(&T::RefundAccount::get(), u32::MAX.into());
let refund = Refund {
account: account.clone(),
identity: 1u32.into(),
amount: 10u64.into(),
};
for _ in 0..i {
Pallet::<T>::queue_refund(refund.clone());
}
assert_eq!(RefundQueue::<T>::get().len() as u32, i);
#[block]
{
Pallet::<T>::process_refund_queue(Weight::MAX);
}
assert_eq!(RefundQueue::<T>::get().len() as u32, 0);
assert_has_event::<T>(Event::<T>::RefundFailed(account).into());
}
impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext(crate::mock::QuotaConfig {
identities: vec![1, 2]
}),
crate::mock::Test
);
}