referrerpolicy=no-referrer-when-downgrade

pallet_origin_restriction/
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 pallet origin restriction.
19
20use super::*;
21use frame_benchmarking::{v2::*, BenchmarkError};
22use sp_runtime::traits::DispatchTransaction;
23
24fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
25	frame_system::Pallet::<T>::assert_last_event(generic_event.into());
26}
27
28#[benchmarks]
29mod benches {
30	use super::*;
31
32	#[benchmark]
33	fn clean_usage() -> Result<(), BenchmarkError> {
34		let origin = T::RestrictedEntity::benchmarked_restricted_origin();
35		let entity = T::RestrictedEntity::restricted_entity(&origin)
36			.expect("The origin from `benchmarked_restricted_origin` must be restricted");
37
38		Usages::<T>::insert(&entity, Usage { used: 1u32.into(), at_block: 0u32.into() });
39
40		frame_system::Pallet::<T>::set_block_number(1_000u32.into());
41
42		#[extrinsic_call]
43		_(frame_system::RawOrigin::Root, entity.clone());
44
45		assert_last_event::<T>(Event::UsageCleaned { entity }.into());
46
47		Ok(())
48	}
49
50	// This benchmark may miss the cost for `OperationAllowedOneTimeExcess::contains`.
51	#[benchmark]
52	fn restrict_origin_tx_ext() -> Result<(), BenchmarkError> {
53		let tx_ext = RestrictOrigin::<T>::new(true);
54		let origin = T::RestrictedEntity::benchmarked_restricted_origin();
55		let call = frame_system::Call::remark { remark: alloc::vec![] }.into();
56
57		#[block]
58		{
59			tx_ext
60				.test_run(origin.into(), &call, &Default::default(), 0, 0, |_| {
61					Ok(Default::default())
62				})
63				.expect("Failed to allow the cheapest call, benchmark needs to be improved")
64				.expect("inner call successful");
65		}
66
67		Ok(())
68	}
69
70	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
71}