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 frame_benchmarking::{account, v2::*, BenchmarkError};
26use frame_support::{
27	traits::{EnsureOrigin, Get},
28	BoundedBTreeSet,
29};
30
31#[benchmarks]
32mod benchmarks {
33	use super::*;
34
35	#[benchmark]
36	fn set_validators(n: Linear<1, { T::MaxValidators::get() }>) -> Result<(), BenchmarkError> {
37		let set = |name: &'static str, count: u32| {
38			BoundedBTreeSet::try_from(
39				(0..count).map(|i| account(name, i, 0)).collect::<BTreeSet<_>>(),
40			)
41			.map_err(|_| BenchmarkError::Stop("the set exceeds MaxValidators"))
42		};
43		Pallet::<T>::do_receive_validator_set(0, set("stored", T::MaxValidators::get())?)?;
44		let validators = set("validator", n)?;
45
46		#[block]
47		{
48			Pallet::<T>::do_receive_validator_set(1, validators)?;
49		}
50
51		assert_eq!(
52			ValidatorSet::<T>::get().map(|set| (set.era, set.validators.len() as u32)),
53			Some((1, n))
54		);
55		assert_eq!(PendingRotation::<T>::get(), RotationState::AwaitingQueue);
56		Ok(())
57	}
58
59	#[benchmark]
60	fn set_max_collators() -> Result<(), BenchmarkError> {
61		let origin =
62			T::UpdateOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
63
64		#[extrinsic_call]
65		_(origin as T::RuntimeOrigin, Some(1));
66
67		assert_eq!(MaxCollators::<T>::get(), Some(1));
68		Ok(())
69	}
70
71	impl_benchmark_test_suite!(ValidatorCollators, crate::mock::new_test_ext(), crate::mock::Test);
72}