#![cfg(feature = "runtime-benchmarks")]
extern crate alloc;
use super::*;
#[allow(unused)]
use crate::{extension::VerifySignature, Config, Pallet as VerifySignaturePallet};
use alloc::vec;
use frame_benchmarking::{v2::*, BenchmarkError};
use frame_support::{
dispatch::{DispatchInfo, GetDispatchInfo},
pallet_prelude::TransactionSource,
};
use frame_system::{Call as SystemCall, RawOrigin};
use sp_io::hashing::blake2_256;
use sp_runtime::{
generic::ExtensionVersion,
traits::{AsTransactionAuthorizedOrigin, DispatchTransaction, Dispatchable},
};
pub trait BenchmarkHelper<Signature, Signer> {
fn create_signature(entropy: &[u8], msg: &[u8]) -> (Signature, Signer);
}
#[benchmarks(where
T: Config + Send + Sync,
T::RuntimeCall: Dispatchable<Info = DispatchInfo> + GetDispatchInfo,
T::RuntimeOrigin: AsTransactionAuthorizedOrigin,
)]
mod benchmarks {
use super::*;
#[benchmark]
fn verify_signature() -> Result<(), BenchmarkError> {
let entropy = [42u8; 256];
let call: T::RuntimeCall = SystemCall::remark { remark: vec![] }.into();
let ext_version: ExtensionVersion = 0;
let info = call.get_dispatch_info();
let msg = (ext_version, &call).using_encoded(blake2_256).to_vec();
let (signature, signer) = T::BenchmarkHelper::create_signature(&entropy, &msg[..]);
let ext = VerifySignature::<T>::new_with_signature(signature, signer);
#[block]
{
assert!(ext
.validate_only(
RawOrigin::None.into(),
&call,
&info,
0,
TransactionSource::External,
ext_version
)
.is_ok());
}
Ok(())
}
impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
}