referrerpolicy=no-referrer-when-downgrade

pallet_validator_collators/
benchmarking.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Benchmarking setup for pallet-validator-collators.
17
18#![cfg(feature = "runtime-benchmarks")]
19
20use super::*;
21
22#[allow(unused)]
23use crate::Pallet as ValidatorCollators;
24use alloc::collections::BTreeSet;
25use cumulus_pallet_parachain_system::OnSystemEvent;
26use frame_benchmarking::{account, v2::*, BenchmarkError};
27use frame_support::{
28	traits::{EnsureOrigin, Get},
29	BoundedBTreeSet, BoundedVec,
30};
31
32#[benchmarks]
33mod benchmarks {
34	use super::*;
35
36	#[benchmark]
37	fn set_validators(n: Linear<1, { T::MaxValidators::get() }>) -> Result<(), BenchmarkError> {
38		let set = |name: &'static str, count: u32| {
39			BoundedBTreeSet::try_from(
40				(0..count).map(|i| account(name, i, 0)).collect::<BTreeSet<_>>(),
41			)
42			.map_err(|_| BenchmarkError::Stop("the set exceeds MaxValidators"))
43		};
44		Pallet::<T>::do_receive_validator_set(0, set("stored", T::MaxValidators::get())?)?;
45		let validators = set("validator", n)?;
46
47		#[block]
48		{
49			Pallet::<T>::do_receive_validator_set(1, validators)?;
50		}
51
52		assert_eq!(
53			ValidatorSet::<T>::get().map(|set| (set.era, set.validators.len() as u32)),
54			Some((1, n))
55		);
56		assert_eq!(PendingRotation::<T>::get(), RotationState::AwaitingQueue);
57		Ok(())
58	}
59
60	/// Worst case: removing the cap with a stored set and drawn collators.
61	#[benchmark]
62	fn set_max_collators() -> Result<(), BenchmarkError> {
63		let origin =
64			T::UpdateOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
65		let max = T::MaxValidators::get();
66		let stored = BoundedBTreeSet::try_from(
67			(0..max).map(|i| account("stored", i, 0)).collect::<BTreeSet<_>>(),
68		)
69		.map_err(|_| BenchmarkError::Stop("the set exceeds MaxValidators"))?;
70		let drawn = BoundedVec::truncate_from(stored.iter().cloned().collect());
71		Pallet::<T>::do_receive_validator_set(0, stored)?;
72		Collators::<T>::put(DrawnCollators { era: 0, validators: drawn });
73
74		#[extrinsic_call]
75		_(origin as T::RuntimeOrigin, None);
76
77		assert_eq!(MaxCollators::<T>::get(), None);
78		assert!(!Collators::<T>::exists());
79		assert_eq!(PendingRotation::<T>::get(), RotationState::AwaitingQueue);
80		Ok(())
81	}
82
83	/// The relay chain epoch randomness changes, so the cache is written.
84	#[benchmark]
85	fn on_relay_state_proof() {
86		EpochRandomness::<T>::put([1; 32]);
87		let proof = crate::relay_proof::with_epoch_randomness([2; 32]);
88
89		#[block]
90		{
91			<Pallet<T> as OnSystemEvent>::on_relay_state_proof(&proof);
92		}
93
94		assert_eq!(EpochRandomness::<T>::get(), Some([2; 32]));
95	}
96
97	impl_benchmark_test_suite!(ValidatorCollators, crate::mock::new_test_ext(), crate::mock::Test);
98}