referrerpolicy=no-referrer-when-downgrade

pallet_dummy_dim/
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//! Dummy DIM pallet benchmarking.
19
20extern crate alloc;
21
22use alloc::vec::Vec;
23
24use super::*;
25use crate::Pallet as DummyDim;
26
27use frame_benchmarking::v2::{benchmarks, *};
28use frame_support::{assert_ok, traits::Get};
29use frame_system::RawOrigin;
30
31type SecretOf<T> = <<T as Config>::People as AddOnlyPeopleTrait>::Secret;
32type MemberOf<T> = <<T as Config>::People as AddOnlyPeopleTrait>::Member;
33
34fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
35	frame_system::Pallet::<T>::assert_last_event(generic_event.into());
36}
37
38fn new_member_from<T: Config>(id: PersonalId) -> (MemberOf<T>, SecretOf<T>) {
39	T::People::mock_key(id)
40}
41
42fn generate_members<T: Config>(start: u32, end: u32) -> Vec<(MemberOf<T>, SecretOf<T>)> {
43	(start..end).map(|i| new_member_from::<T>(i as PersonalId)).collect::<Vec<_>>()
44}
45
46#[benchmarks]
47mod benches {
48	use super::*;
49
50	#[benchmark]
51	fn reserve_ids(c: Linear<1, { T::MaxPersonBatchSize::get() }>) -> Result<(), BenchmarkError> {
52		#[extrinsic_call]
53		_(RawOrigin::Root, c);
54
55		assert_last_event::<T>(Event::IdsReserved { count: c }.into());
56		Ok(())
57	}
58
59	#[benchmark]
60	fn renew_id_reservation() -> Result<(), BenchmarkError> {
61		assert_ok!(DummyDim::<T>::reserve_ids(RawOrigin::Root.into(), 1));
62		assert_ok!(DummyDim::<T>::cancel_id_reservation(RawOrigin::Root.into(), 0));
63
64		#[extrinsic_call]
65		_(RawOrigin::Root, 0);
66
67		assert_last_event::<T>(Event::IdRenewed { id: 0 }.into());
68		Ok(())
69	}
70
71	#[benchmark]
72	fn cancel_id_reservation() -> Result<(), BenchmarkError> {
73		assert_ok!(DummyDim::<T>::reserve_ids(RawOrigin::Root.into(), 1));
74
75		#[extrinsic_call]
76		_(RawOrigin::Root, 0);
77
78		assert_last_event::<T>(Event::IdUnreserved { id: 0 }.into());
79		Ok(())
80	}
81
82	#[benchmark]
83	fn recognize_personhood(
84		c: Linear<1, { T::MaxPersonBatchSize::get() }>,
85	) -> Result<(), BenchmarkError> {
86		assert_ok!(DummyDim::<T>::reserve_ids(RawOrigin::Root.into(), c));
87		let keys = generate_members::<T>(0, c);
88		let keys_and_ids: Vec<_> =
89			(0..c).map(|i| (i as PersonalId, keys[i as usize].0.clone())).collect();
90
91		#[extrinsic_call]
92		_(RawOrigin::Root, keys_and_ids.try_into().unwrap());
93
94		assert_last_event::<T>(Event::PeopleRegistered { count: c }.into());
95		Ok(())
96	}
97
98	#[benchmark]
99	fn suspend_personhood(
100		c: Linear<1, { T::MaxPersonBatchSize::get() }>,
101	) -> Result<(), BenchmarkError> {
102		assert_ok!(DummyDim::<T>::reserve_ids(RawOrigin::Root.into(), c));
103		let keys = generate_members::<T>(0, c);
104		let keys_and_ids: Vec<_> =
105			(0..c).map(|i| (i as PersonalId, keys[i as usize].0.clone())).collect();
106		assert_ok!(DummyDim::<T>::recognize_personhood(
107			RawOrigin::Root.into(),
108			keys_and_ids.try_into().unwrap()
109		));
110		assert_ok!(T::People::start_people_set_mutation_session());
111		let ids: Vec<_> = (0..c as PersonalId).collect();
112
113		#[extrinsic_call]
114		_(RawOrigin::Root, ids.try_into().unwrap());
115
116		assert_last_event::<T>(Event::PeopleSuspended { count: c }.into());
117		Ok(())
118	}
119
120	#[benchmark]
121	fn resume_personhood() -> Result<(), BenchmarkError> {
122		let people_count = T::MaxPersonBatchSize::get();
123		assert_ok!(DummyDim::<T>::reserve_ids(RawOrigin::Root.into(), people_count));
124		let keys = generate_members::<T>(0, people_count);
125		let keys_and_ids: Vec<_> = (0..people_count)
126			.map(|i| (i as PersonalId, keys[i as usize].0.clone()))
127			.collect();
128		assert_ok!(DummyDim::<T>::recognize_personhood(
129			RawOrigin::Root.into(),
130			keys_and_ids.try_into().unwrap()
131		));
132		assert_ok!(T::People::start_people_set_mutation_session());
133		let ids: Vec<_> = (0..people_count as PersonalId).collect();
134		assert_ok!(DummyDim::<T>::suspend_personhood(
135			RawOrigin::Root.into(),
136			ids.clone().try_into().unwrap()
137		));
138
139		#[extrinsic_call]
140		_(RawOrigin::Root, 0);
141
142		assert_last_event::<T>(Event::PersonhoodResumed { id: 0 }.into());
143		Ok(())
144	}
145
146	#[benchmark]
147	fn start_mutation_session() -> Result<(), BenchmarkError> {
148		#[extrinsic_call]
149		_(RawOrigin::Root);
150
151		assert_last_event::<T>(Event::SuspensionsStarted.into());
152		Ok(())
153	}
154
155	#[benchmark]
156	fn end_mutation_session() -> Result<(), BenchmarkError> {
157		assert_ok!(T::People::start_people_set_mutation_session());
158
159		#[extrinsic_call]
160		_(RawOrigin::Root);
161
162		assert_last_event::<T>(Event::SuspensionsEnded.into());
163		Ok(())
164	}
165
166	// Implements a test for each benchmark. Execute with:
167	// `cargo test -p pallet-dummy-dim --features runtime-benchmarks`.
168	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
169}