referrerpolicy=no-referrer-when-downgrade

pallet_verify_signature/
benchmarking.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Benchmarks for Verify Signature Pallet
19
20#![cfg(feature = "runtime-benchmarks")]
21
22extern crate alloc;
23
24use super::*;
25
26#[allow(unused)]
27use crate::{extension::VerifySignature, Config, Pallet as VerifySignaturePallet};
28use alloc::vec;
29use frame_benchmarking::{v2::*, BenchmarkError};
30use frame_support::{
31	dispatch::{DispatchInfo, GetDispatchInfo},
32	pallet_prelude::TransactionSource,
33};
34use frame_system::{Call as SystemCall, RawOrigin};
35use sp_io::{
36	crypto::{sr25519_generate, sr25519_sign},
37	hashing::blake2_256,
38};
39use sp_runtime::{
40	generic::ExtensionVersion,
41	traits::{AsTransactionAuthorizedOrigin, DispatchTransaction, Dispatchable, IdentifyAccount},
42	AccountId32, MultiSignature, MultiSigner,
43};
44
45pub trait BenchmarkHelper<Signature, Signer> {
46	fn create_signature(entropy: &[u8], msg: &[u8]) -> (Signature, Signer);
47}
48
49impl BenchmarkHelper<MultiSignature, AccountId32> for () {
50	fn create_signature(_entropy: &[u8], msg: &[u8]) -> (MultiSignature, AccountId32) {
51		let public = sr25519_generate(0.into(), None);
52		let who_account: AccountId32 = MultiSigner::Sr25519(public).into_account().into();
53		let signature = MultiSignature::Sr25519(sr25519_sign(0.into(), &public, msg).unwrap());
54		(signature, who_account)
55	}
56}
57
58#[benchmarks(where
59	T: Config + Send + Sync,
60	T::RuntimeCall: Dispatchable<Info = DispatchInfo> + GetDispatchInfo,
61	T::RuntimeOrigin: AsTransactionAuthorizedOrigin,
62)]
63mod benchmarks {
64	use super::*;
65
66	#[benchmark]
67	fn verify_signature() -> Result<(), BenchmarkError> {
68		let entropy = [42u8; 256];
69		let call: T::RuntimeCall = SystemCall::remark { remark: vec![] }.into();
70		let ext_version: ExtensionVersion = 0;
71		let info = call.get_dispatch_info();
72		let msg = (ext_version, &call).using_encoded(blake2_256).to_vec();
73		let (signature, signer) = T::BenchmarkHelper::create_signature(&entropy, &msg[..]);
74		let ext = VerifySignature::<T>::new_with_signature(signature, signer);
75
76		#[block]
77		{
78			assert!(ext
79				.validate_only(
80					RawOrigin::None.into(),
81					&call,
82					&info,
83					0,
84					TransactionSource::External,
85					ext_version
86				)
87				.is_ok());
88		}
89
90		Ok(())
91	}
92
93	impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
94}