referrerpolicy=no-referrer-when-downgrade

pallet_whitelist/
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//! Whitelist pallet benchmarking.
19
20#![cfg(feature = "runtime-benchmarks")]
21
22use super::*;
23#[cfg(test)]
24use crate::Pallet as Whitelist;
25use frame::benchmarking::prelude::*;
26
27#[benchmarks]
28mod benchmarks {
29	use super::*;
30
31	#[benchmark]
32	fn whitelist_call() -> Result<(), BenchmarkError> {
33		let origin =
34			T::WhitelistOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
35		let call_hash = Default::default();
36
37		#[extrinsic_call]
38		_(origin as T::RuntimeOrigin, call_hash);
39
40		ensure!(WhitelistedCall::<T>::contains_key(call_hash), "call not whitelisted");
41		ensure!(T::Preimages::is_requested(&call_hash), "preimage not requested");
42		Ok(())
43	}
44
45	#[benchmark]
46	fn remove_whitelisted_call() -> Result<(), BenchmarkError> {
47		let origin =
48			T::WhitelistOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
49		let call_hash = Default::default();
50		Pallet::<T>::whitelist_call(origin.clone(), call_hash)
51			.expect("whitelisting call must be successful");
52
53		#[extrinsic_call]
54		_(origin as T::RuntimeOrigin, call_hash);
55
56		ensure!(!WhitelistedCall::<T>::contains_key(call_hash), "whitelist not removed");
57		ensure!(!T::Preimages::is_requested(&call_hash), "preimage still requested");
58		Ok(())
59	}
60
61	// Measured through `dispatch_whitelisted_call_with_preimage`, the worst case of the two
62	// defer sites: includes the origin check, the `WhitelistedCall` read and hashing the
63	// `n`-byte call.
64	#[benchmark]
65	fn defer_dispatch(n: Linear<1, 10_000>) -> Result<(), BenchmarkError> {
66		let origin = T::DispatchWhitelistedOrigin::try_successful_origin()
67			.map_err(|_| BenchmarkError::Weightless)?;
68		let remark = alloc::vec![1u8; n as usize];
69		let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark }.into();
70		let call_hash = T::Hashing::hash_of(&call);
71
72		#[extrinsic_call]
73		dispatch_whitelisted_call_with_preimage(origin as T::RuntimeOrigin, Box::new(call));
74
75		ensure!(DeferredDispatch::<T>::contains_key(call_hash), "dispatch not deferred");
76		Ok(())
77	}
78
79	#[benchmark]
80	fn remove_deferred_dispatch() -> Result<(), BenchmarkError> {
81		let caller: T::AccountId = whitelisted_caller();
82		let call: <T as Config>::RuntimeCall =
83			frame_system::Call::remark { remark: alloc::vec![] }.into();
84		let call_hash = T::Hashing::hash_of(&call);
85
86		// Insert an already-expired deferred entry (`expire_at == now`), so it can be removed.
87		let now = T::BlockNumberProvider::current_block_number();
88		DeferredDispatch::<T>::insert(call_hash, now);
89
90		#[extrinsic_call]
91		_(frame_system::RawOrigin::Signed(caller), call_hash);
92
93		ensure!(!DeferredDispatch::<T>::contains_key(call_hash), "deferred entry not removed");
94		Ok(())
95	}
96
97	// Worst case: the call was deferred and is relayed by a signed origin, so on top of the
98	// direct-dispatch path this also reads and removes the deferred entry and emits
99	// `DeferredDispatchExecuted`.
100	//
101	// We benchmark with the maximum possible size for a call.
102	// If the resulting weight is too big, maybe it worth having a weight which depends
103	// on the size of the call, with a new witness in parameter.
104	// Root mode is `Measured` since `T::BlockNumberProvider` may read storage without a MEL
105	// bound (e.g. `ParachainSystem::ValidationData`).
106	#[benchmark(pov_mode = Measured {
107		Whitelist: MaxEncodedLen,
108		Preimage: MaxEncodedLen,
109		// Use measured PoV size for the Preimages since we pass in a length witness.
110		Preimage::PreimageFor: Measured
111	})]
112	// NOTE: we remove `10` because we need some bytes to encode the variants and vec length
113	fn dispatch_whitelisted_call(
114		n: Linear<1, { T::Preimages::MAX_LENGTH as u32 - 10 }>,
115	) -> Result<(), BenchmarkError> {
116		let whitelist_origin = T::DispatchWhitelistedOrigin::try_successful_origin()
117			.map_err(|_| BenchmarkError::Weightless)?;
118		let caller: T::AccountId = whitelisted_caller();
119		let remark = alloc::vec![1u8; n as usize];
120		let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark }.into();
121		let call_weight = call.get_dispatch_info().call_weight;
122		let encoded_call = call.encode();
123		let call_encoded_len = encoded_call.len() as u32;
124		let call_hash = T::Hashing::hash_of(&call);
125
126		Pallet::<T>::defer_dispatch(call_hash).expect("deferring dispatch must be successful");
127		Pallet::<T>::whitelist_call(whitelist_origin, call_hash)
128			.expect("whitelisting call must be successful");
129		T::Preimages::note(encoded_call.into()).unwrap();
130
131		#[extrinsic_call]
132		_(frame_system::RawOrigin::Signed(caller), call_hash, call_encoded_len, call_weight);
133
134		ensure!(!WhitelistedCall::<T>::contains_key(call_hash), "whitelist not removed");
135		ensure!(!DeferredDispatch::<T>::contains_key(call_hash), "deferred entry not removed");
136		ensure!(!T::Preimages::is_requested(&call_hash), "preimage still requested");
137		Ok(())
138	}
139
140	#[benchmark]
141	fn dispatch_whitelisted_call_with_preimage(n: Linear<1, 10_000>) -> Result<(), BenchmarkError> {
142		let whitelist_origin = T::DispatchWhitelistedOrigin::try_successful_origin()
143			.map_err(|_| BenchmarkError::Weightless)?;
144		let caller: T::AccountId = whitelisted_caller();
145		let remark = alloc::vec![1u8; n as usize];
146
147		let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark }.into();
148		let call_hash = T::Hashing::hash_of(&call);
149
150		Pallet::<T>::defer_dispatch(call_hash).expect("deferring dispatch must be successful");
151		Pallet::<T>::whitelist_call(whitelist_origin, call_hash)
152			.expect("whitelisting call must be successful");
153
154		#[extrinsic_call]
155		_(frame_system::RawOrigin::Signed(caller), Box::new(call));
156
157		ensure!(!WhitelistedCall::<T>::contains_key(call_hash), "whitelist not removed");
158		ensure!(!DeferredDispatch::<T>::contains_key(call_hash), "deferred entry not removed");
159		ensure!(!T::Preimages::is_requested(&call_hash), "preimage still requested");
160		Ok(())
161	}
162
163	impl_benchmark_test_suite!(Whitelist, crate::mock::new_test_ext(), crate::mock::Test);
164}