referrerpolicy=no-referrer-when-downgrade

pallet_sudo/
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 Sudo Pallet
19
20use super::*;
21use crate::Pallet;
22use alloc::{boxed::Box, vec};
23use frame_benchmarking::v2::*;
24use frame_support::dispatch::{DispatchInfo, GetDispatchInfo};
25use frame_system::RawOrigin;
26use sp_runtime::traits::{
27	AsSystemOriginSigner, AsTransactionAuthorizedOrigin, DispatchTransaction, Dispatchable,
28};
29
30fn assert_last_event<T: Config>(generic_event: crate::Event<T>) {
31	let re: <T as Config>::RuntimeEvent = generic_event.into();
32	frame_system::Pallet::<T>::assert_last_event(re.into());
33}
34
35#[benchmarks(where
36	T: Send + Sync,
37	<T as Config>::RuntimeCall: From<frame_system::Call<T>>,
38	<T as frame_system::Config>::RuntimeCall: Dispatchable<Info = DispatchInfo> + GetDispatchInfo,
39	<<T as frame_system::Config>::RuntimeCall as Dispatchable>::PostInfo: Default,
40	<<T as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
41		AsSystemOriginSigner<T::AccountId> + AsTransactionAuthorizedOrigin + Clone,
42)]
43mod benchmarks {
44	use super::*;
45
46	#[benchmark]
47	fn set_key() {
48		let caller: T::AccountId = whitelisted_caller();
49		Key::<T>::put(&caller);
50
51		let new_sudoer: T::AccountId = account("sudoer", 0, 0);
52		let new_sudoer_lookup = T::Lookup::unlookup(new_sudoer.clone());
53
54		#[extrinsic_call]
55		_(RawOrigin::Signed(caller.clone()), new_sudoer_lookup);
56
57		assert_last_event::<T>(Event::KeyChanged { old: Some(caller), new: new_sudoer });
58	}
59
60	#[benchmark]
61	fn sudo() {
62		let caller: T::AccountId = whitelisted_caller();
63		Key::<T>::put(&caller);
64
65		let call = frame_system::Call::remark { remark: vec![] }.into();
66
67		#[extrinsic_call]
68		_(RawOrigin::Signed(caller), Box::new(call));
69
70		assert_last_event::<T>(Event::Sudid { sudo_result: Ok(()) })
71	}
72
73	#[benchmark]
74	fn sudo_as() {
75		let caller: T::AccountId = whitelisted_caller();
76		Key::<T>::put(caller.clone());
77
78		let call = frame_system::Call::remark { remark: vec![] }.into();
79
80		let who: T::AccountId = account("as", 0, 0);
81		let who_lookup = T::Lookup::unlookup(who);
82
83		#[extrinsic_call]
84		_(RawOrigin::Signed(caller), who_lookup, Box::new(call));
85
86		assert_last_event::<T>(Event::SudoAsDone { sudo_result: Ok(()) })
87	}
88
89	#[benchmark]
90	fn remove_key() {
91		let caller: T::AccountId = whitelisted_caller();
92		Key::<T>::put(&caller);
93
94		#[extrinsic_call]
95		_(RawOrigin::Signed(caller.clone()));
96
97		assert_last_event::<T>(Event::KeyRemoved {});
98	}
99
100	#[benchmark]
101	fn check_only_sudo_account() {
102		let caller: T::AccountId = whitelisted_caller();
103		Key::<T>::put(&caller);
104
105		let call: <T as frame_system::Config>::RuntimeCall =
106			frame_system::Call::remark { remark: vec![] }.into();
107		let info = call.get_dispatch_info();
108		let ext = CheckOnlySudoAccount::<T>::new();
109
110		#[block]
111		{
112			assert!(ext
113				.test_run(RawOrigin::Signed(caller).into(), &call, &info, 0, 0, |_| Ok(
114					Default::default()
115				))
116				.unwrap()
117				.is_ok());
118		}
119	}
120
121	impl_benchmark_test_suite!(Pallet, crate::mock::new_bench_ext(), crate::mock::Test);
122}