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 registrar_primitives::{MessageToPara, MessageToParaV1};
25
26/// An account able to pay every consideration this pallet can ask for.
27fn funded_manager<T: Config>() -> T::AccountId {
28	let who: T::AccountId = account("manager", 0, 0);
29	T::ReservationConsideration::ensure_successful(&who, Footprint::from_parts(1, 0));
30	T::RegistrationConsideration::ensure_successful(
31		&who,
32		Pallet::<T>::registration_footprint(T::MaxHeadDataSize::get(), T::MaxCodeSize::get()),
33	);
34	who
35}
36
37/// Reserve a para id for `who` and return it.
38fn reserve_for<T: Config>(who: &T::AccountId) -> Result<ParaId, BenchmarkError> {
39	Pallet::<T>::reserve(RawOrigin::Signed(who.clone()).into())?;
40	Ok(NextFreeParaId::<T>::get().saturating_sub(1))
41}
42
43/// Reserve a para id and put it into `Pending`.
44fn make_pending<T: Config>(who: &T::AccountId) -> Result<ParaId, BenchmarkError> {
45	let para_id = reserve_for::<T>(who)?;
46	Pallet::<T>::register(
47		RawOrigin::Signed(who.clone()).into(),
48		para_id,
49		alloc::vec![2u8; T::MaxHeadDataSize::get() as usize],
50		T::MaxCodeSize::get(),
51		sp_core::H256::repeat_byte(1),
52	)?;
53	Ok(para_id)
54}
55
56#[benchmarks]
57mod benchmarks {
58	use super::*;
59
60	#[benchmark]
61	fn reserve() -> Result<(), BenchmarkError> {
62		let who = funded_manager::<T>();
63
64		#[extrinsic_call]
65		_(RawOrigin::Signed(who.clone()));
66
67		let para_id = NextFreeParaId::<T>::get().saturating_sub(1);
68		assert_eq!(Paras::<T>::get(para_id).map(|i| i.manager), Some(who));
69		Ok(())
70	}
71
72	/// Requesting a registration. Dominated by shipping the head data to the relay chain.
73	#[benchmark]
74	fn register(h: Linear<0, { T::MaxHeadDataSize::get() }>) -> Result<(), BenchmarkError> {
75		let who = funded_manager::<T>();
76		let para_id = reserve_for::<T>(&who)?;
77
78		#[extrinsic_call]
79		_(
80			RawOrigin::Signed(who),
81			para_id,
82			alloc::vec![2u8; h as usize],
83			T::MaxCodeSize::get(),
84			sp_core::H256::repeat_byte(1),
85		);
86
87		assert!(matches!(
88			Paras::<T>::get(para_id).map(|i| i.state),
89			Some(RegistrationState::Pending { .. })
90		));
91		Ok(())
92	}
93
94	/// Asking the relay chain to drop an authorization. The deposit stays held, so this is the
95	/// state write plus the message.
96	#[benchmark]
97	fn cancel_registration() -> Result<(), BenchmarkError> {
98		let who = funded_manager::<T>();
99		let para_id = make_pending::<T>(&who)?;
100		T::BlockNumberProvider::set_block_number(
101			T::BlockNumberProvider::current_block_number()
102				.saturating_add(T::PendingDeadline::get())
103				.saturating_add(1u32.into()),
104		);
105
106		#[extrinsic_call]
107		_(RawOrigin::Signed(who), para_id);
108
109		assert!(matches!(
110			Paras::<T>::get(para_id).map(|i| i.state),
111			Some(RegistrationState::Pending { .. })
112		));
113		Ok(())
114	}
115
116	/// The worst case of the messages this call serves is a confirmed cancellation, which releases
117	/// the deposit on top of writing the new state.
118	#[benchmark]
119	fn receive() -> Result<(), BenchmarkError> {
120		let who = funded_manager::<T>();
121		let para_id = make_pending::<T>(&who)?;
122		let message = MessageToPara::V1(MessageToParaV1::CancelResponse {
123			para_id,
124			message_id: 0,
125			outcome: Ok(()),
126		});
127
128		#[extrinsic_call]
129		_(RawOrigin::Root, message);
130
131		assert_eq!(Paras::<T>::get(para_id).map(|i| i.state), Some(RegistrationState::Reserved));
132		Ok(())
133	}
134
135	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
136}