#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::v2::*;
use frame_support::traits::EnsureOrigin;
#[cfg(test)]
use crate::Pallet as Whitelist;
#[benchmarks]
mod benchmarks {
use super::*;
#[benchmark]
fn whitelist_call() -> Result<(), BenchmarkError> {
let origin =
T::WhitelistOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let call_hash = Default::default();
#[extrinsic_call]
_(origin as T::RuntimeOrigin, call_hash);
ensure!(WhitelistedCall::<T>::contains_key(call_hash), "call not whitelisted");
ensure!(T::Preimages::is_requested(&call_hash), "preimage not requested");
Ok(())
}
#[benchmark]
fn remove_whitelisted_call() -> Result<(), BenchmarkError> {
let origin =
T::WhitelistOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let call_hash = Default::default();
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
.expect("whitelisting call must be successful");
#[extrinsic_call]
_(origin as T::RuntimeOrigin, call_hash);
ensure!(!WhitelistedCall::<T>::contains_key(call_hash), "whitelist not removed");
ensure!(!T::Preimages::is_requested(&call_hash), "preimage still requested");
Ok(())
}
#[benchmark(pov_mode = MaxEncodedLen {
Preimage::PreimageFor: Measured
})]
fn dispatch_whitelisted_call(
n: Linear<1, { T::Preimages::MAX_LENGTH as u32 - 10 }>,
) -> Result<(), BenchmarkError> {
let origin = T::DispatchWhitelistedOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)?;
let remark = alloc::vec![1u8; n as usize];
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark }.into();
let call_weight = call.get_dispatch_info().call_weight;
let encoded_call = call.encode();
let call_encoded_len = encoded_call.len() as u32;
let call_hash = T::Hashing::hash_of(&call);
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
.expect("whitelisting call must be successful");
T::Preimages::note(encoded_call.into()).unwrap();
#[extrinsic_call]
_(origin as T::RuntimeOrigin, call_hash, call_encoded_len, call_weight);
ensure!(!WhitelistedCall::<T>::contains_key(call_hash), "whitelist not removed");
ensure!(!T::Preimages::is_requested(&call_hash), "preimage still requested");
Ok(())
}
#[benchmark]
fn dispatch_whitelisted_call_with_preimage(n: Linear<1, 10_000>) -> Result<(), BenchmarkError> {
let origin = T::DispatchWhitelistedOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)?;
let remark = alloc::vec![1u8; n as usize];
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark }.into();
let call_hash = T::Hashing::hash_of(&call);
Pallet::<T>::whitelist_call(origin.clone(), call_hash)
.expect("whitelisting call must be successful");
#[extrinsic_call]
_(origin as T::RuntimeOrigin, Box::new(call));
ensure!(!WhitelistedCall::<T>::contains_key(call_hash), "whitelist not removed");
ensure!(!T::Preimages::is_requested(&call_hash), "preimage still requested");
Ok(())
}
impl_benchmark_test_suite!(Whitelist, crate::mock::new_test_ext(), crate::mock::Test);
}