referrerpolicy=no-referrer-when-downgrade

pallet_registrar_para/
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-registrar-para`.
19
20use super::*;
21use frame_benchmarking::v2::*;
22use frame_support::traits::Get;
23use frame_system::RawOrigin;
24use polkadot_primitives::AsyncBackingParams;
25use registrar_primitives::{MessageToPara, MessageToParaV1};
26
27/// The host configuration every benchmark runs under.
28fn host_configuration() -> AbridgedHostConfiguration {
29	AbridgedHostConfiguration {
30		max_code_size: 3 * 1024 * 1024,
31		max_head_data_size: 32 * 1024,
32		max_upward_queue_count: 8,
33		max_upward_queue_size: 1024 * 1024,
34		max_upward_message_size: 4 * 1024,
35		max_upward_message_num_per_candidate: 2,
36		hrmp_max_message_num_per_candidate: 2,
37		validation_upgrade_cooldown: 2,
38		validation_upgrade_delay: 2,
39		async_backing_params: AsyncBackingParams {
40			allowed_ancestry_len: 0,
41			max_candidate_depth: 0,
42		},
43	}
44}
45
46/// Put [`host_configuration`] in place and return an account able to pay every consideration.
47fn funded_manager<T: Config>() -> T::AccountId {
48	let config = host_configuration();
49	T::BenchmarkHelper::set_host_configuration(config.clone());
50
51	let who: T::AccountId = account("manager", 0, 0);
52	T::ReservationConsideration::ensure_successful(&who, Pallet::<T>::reservation_footprint());
53	T::RegistrationConsideration::ensure_successful(
54		&who,
55		Pallet::<T>::registration_footprint(config.max_head_data_size, &config),
56	);
57	who
58}
59
60/// Reserve a para id for `who` and return it.
61fn reserve_for<T: Config>(who: &T::AccountId) -> Result<ParaId, BenchmarkError> {
62	Pallet::<T>::reserve(RawOrigin::Signed(who.clone()).into())?;
63	Ok(NextFreeParaId::<T>::get().saturating_sub(1))
64}
65
66/// Reserve a para id and put it into `Pending`.
67fn make_pending<T: Config>(who: &T::AccountId) -> Result<ParaId, BenchmarkError> {
68	let para_id = reserve_for::<T>(who)?;
69	let config = host_configuration();
70	Pallet::<T>::register(
71		RawOrigin::Signed(who.clone()).into(),
72		para_id,
73		alloc::vec![2u8; config.max_head_data_size as usize],
74		config.max_code_size,
75		sp_core::H256::repeat_byte(1),
76	)?;
77	Ok(para_id)
78}
79
80/// Reserve a para id and put it into `Registered`.
81fn make_registered<T: Config>(who: &T::AccountId) -> Result<ParaId, BenchmarkError> {
82	let para_id = make_pending::<T>(who)?;
83	Pallet::<T>::receive(
84		RawOrigin::Root.into(),
85		MessageToPara::V1(MessageToParaV1::RegisterResponse {
86			para_id,
87			message_id: 0,
88			outcome: Ok(()),
89		}),
90	)?;
91	Ok(para_id)
92}
93
94/// Reserve a para id, register it and ask to deregister it. Returns the id and the awaited message.
95fn make_deregistering<T: Config>(who: &T::AccountId) -> Result<(ParaId, u64), BenchmarkError> {
96	let para_id = make_registered::<T>(who)?;
97	Pallet::<T>::deregister(RawOrigin::Signed(who.clone()).into(), para_id)?;
98	match Paras::<T>::get(para_id).map(|i| i.state) {
99		Some(RegistrationState::Deregistering { message_id, .. }) => Ok((para_id, message_id)),
100		_ => Err(BenchmarkError::Stop("not deregistering")),
101	}
102}
103
104/// Reserve a para id, register it and lock it.
105fn make_locked<T: Config>(who: &T::AccountId) -> Result<ParaId, BenchmarkError> {
106	let para_id = make_registered::<T>(who)?;
107	Pallet::<T>::add_lock(RawOrigin::Signed(who.clone()).into(), para_id)?;
108	Ok(para_id)
109}
110
111#[benchmarks]
112mod benchmarks {
113	use super::*;
114
115	#[benchmark]
116	fn reserve() -> Result<(), BenchmarkError> {
117		let who = funded_manager::<T>();
118
119		#[extrinsic_call]
120		_(RawOrigin::Signed(who.clone()));
121
122		let para_id = NextFreeParaId::<T>::get().saturating_sub(1);
123		assert_eq!(Paras::<T>::get(para_id).map(|i| i.manager), Some(who));
124		Ok(())
125	}
126
127	/// Requesting a registration. Dominated by shipping the head data to the relay chain.
128	#[benchmark]
129	fn register(
130		h: Linear<0, { host_configuration().max_head_data_size }>,
131	) -> Result<(), BenchmarkError> {
132		let who = funded_manager::<T>();
133		let para_id = reserve_for::<T>(&who)?;
134
135		#[extrinsic_call]
136		_(
137			RawOrigin::Signed(who),
138			para_id,
139			alloc::vec![2u8; h as usize],
140			host_configuration().max_code_size,
141			sp_core::H256::repeat_byte(1),
142		);
143
144		assert!(matches!(
145			Paras::<T>::get(para_id).map(|i| i.state),
146			Some(RegistrationState::Pending { .. })
147		));
148		Ok(())
149	}
150
151	/// Force-registering an id nobody reserved: the worst case, since both considerations are
152	/// taken rather than just the registration's.
153	#[benchmark]
154	fn force_register(
155		h: Linear<0, { host_configuration().max_head_data_size }>,
156	) -> Result<(), BenchmarkError> {
157		let origin =
158			T::ForceOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
159		let who = funded_manager::<T>();
160		let para_id = T::FirstPublicParaId::get();
161
162		#[extrinsic_call]
163		_(
164			origin as T::RuntimeOrigin,
165			para_id,
166			who,
167			alloc::vec![2u8; h as usize],
168			host_configuration().max_code_size,
169			sp_core::H256::repeat_byte(1),
170			false,
171		);
172
173		assert!(matches!(
174			Paras::<T>::get(para_id).map(|i| i.state),
175			Some(RegistrationState::Pending { .. })
176		));
177		Ok(())
178	}
179
180	/// Asking the relay chain to drop an authorization. The deposit stays held, so this is the
181	/// state write plus the message.
182	#[benchmark]
183	fn cancel_registration() -> Result<(), BenchmarkError> {
184		let who = funded_manager::<T>();
185		let para_id = make_pending::<T>(&who)?;
186		T::BlockNumberProvider::set_block_number(
187			T::BlockNumberProvider::current_block_number()
188				.saturating_add(T::PendingDeadline::get())
189				.saturating_add(1u32.into()),
190		);
191
192		#[extrinsic_call]
193		_(RawOrigin::Signed(who), para_id);
194
195		assert!(matches!(
196			Paras::<T>::get(para_id).map(|i| i.state),
197			Some(RegistrationState::Pending { .. })
198		));
199		Ok(())
200	}
201
202	/// Worst case: a confirmed deregistration releases both considerations and removes the entry.
203	#[benchmark]
204	fn receive() -> Result<(), BenchmarkError> {
205		let who = funded_manager::<T>();
206		let (para_id, message_id) = make_deregistering::<T>(&who)?;
207		let message = MessageToPara::V1(MessageToParaV1::DeregisterResponse {
208			para_id,
209			message_id,
210			outcome: Ok(()),
211		});
212
213		#[extrinsic_call]
214		_(RawOrigin::Root, message);
215
216		assert!(Paras::<T>::get(para_id).is_none());
217		Ok(())
218	}
219
220	/// Giving up an id the relay chain never knew: the reservation goes back here and now.
221	#[benchmark]
222	fn deregister_reserved() -> Result<(), BenchmarkError> {
223		let who = funded_manager::<T>();
224		let para_id = reserve_for::<T>(&who)?;
225
226		#[extrinsic_call]
227		deregister(RawOrigin::Signed(who), para_id);
228
229		assert!(Paras::<T>::get(para_id).is_none());
230		Ok(())
231	}
232
233	/// Asking the relay chain to drop a registered para: the state write plus the message.
234	#[benchmark]
235	fn deregister_registered() -> Result<(), BenchmarkError> {
236		let who = funded_manager::<T>();
237		let para_id = make_registered::<T>(&who)?;
238
239		#[extrinsic_call]
240		deregister(RawOrigin::Signed(who), para_id);
241
242		assert!(matches!(
243			Paras::<T>::get(para_id).map(|i| i.state),
244			Some(RegistrationState::Deregistering { .. })
245		));
246		Ok(())
247	}
248
249	/// Asking again after the answer never came: the same, plus reporting the lost answer.
250	#[benchmark]
251	fn deregister_retry() -> Result<(), BenchmarkError> {
252		let who = funded_manager::<T>();
253		let (para_id, awaited) = make_deregistering::<T>(&who)?;
254		T::BlockNumberProvider::set_block_number(
255			T::BlockNumberProvider::current_block_number()
256				.saturating_add(T::PendingDeadline::get()),
257		);
258
259		#[extrinsic_call]
260		deregister(RawOrigin::Signed(who), para_id);
261
262		assert!(matches!(
263			Paras::<T>::get(para_id).map(|i| i.state),
264			Some(RegistrationState::Deregistering { message_id, .. }) if message_id != awaited
265		));
266		Ok(())
267	}
268
269	/// Locking a registered para: one state read and write.
270	#[benchmark]
271	fn add_lock() -> Result<(), BenchmarkError> {
272		let who = funded_manager::<T>();
273		let para_id = make_registered::<T>(&who)?;
274
275		#[extrinsic_call]
276		_(RawOrigin::Signed(who), para_id);
277
278		assert!(Paras::<T>::get(para_id).map(|i| i.is_locked()).unwrap_or(false));
279		Ok(())
280	}
281
282	/// Unlocking, with root standing in for the para itself: both do the same read and write.
283	#[benchmark]
284	fn remove_lock() -> Result<(), BenchmarkError> {
285		let who = funded_manager::<T>();
286		let para_id = make_locked::<T>(&who)?;
287
288		#[extrinsic_call]
289		_(RawOrigin::Root, para_id);
290
291		assert!(!Paras::<T>::get(para_id).map(|i| i.is_locked()).unwrap_or(true));
292		Ok(())
293	}
294
295	/// Asking for a new head. Dominated by shipping the head data to the relay chain.
296	#[benchmark]
297	fn set_current_head(
298		h: Linear<0, { host_configuration().max_head_data_size }>,
299	) -> Result<(), BenchmarkError> {
300		let who = funded_manager::<T>();
301		let para_id = make_registered::<T>(&who)?;
302
303		#[extrinsic_call]
304		_(RawOrigin::Signed(who), para_id, alloc::vec![2u8; h as usize]);
305
306		assert_eq!(NextMessageId::<T>::get(), 2);
307		Ok(())
308	}
309
310	/// Asking for a code upgrade: one state read and the outgoing message.
311	#[benchmark]
312	fn schedule_code_upgrade() -> Result<(), BenchmarkError> {
313		let who = funded_manager::<T>();
314		let para_id = make_registered::<T>(&who)?;
315
316		#[extrinsic_call]
317		_(
318			RawOrigin::Signed(who),
319			para_id,
320			sp_core::H256::repeat_byte(2),
321			host_configuration().max_code_size,
322		);
323
324		Ok(())
325	}
326
327	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
328}