referrerpolicy=no-referrer-when-downgrade

pallet_validator_set_announcer/
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-set-announcer.
17
18#![cfg(feature = "runtime-benchmarks")]
19
20use super::*;
21
22#[allow(unused)]
23use crate::Pallet as ValidatorSetAnnouncer;
24use alloc::vec::Vec;
25use frame_benchmarking::{account, v2::*, BenchmarkError};
26use frame_support::traits::Get;
27use pallet_validator_collators::ValidatorSet;
28
29#[benchmarks]
30mod benchmarks {
31	use super::*;
32
33	#[benchmark]
34	fn announce(n: Linear<1, { T::MaxValidators::get() }>) -> Result<(), BenchmarkError> {
35		let stored = (0..T::MaxValidators::get())
36			.map(|i| account("stored", i, 0))
37			.collect::<Vec<_>>();
38		pallet_validator_collators::Pallet::<T>::receive_validator_set(0, stored)?;
39		let validators = (0..n).map(|i| account("validator", i, 0)).collect::<Vec<T::AccountId>>();
40
41		#[block]
42		{
43			pallet_validator_collators::Pallet::<T>::receive_validator_set(
44				1,
45				validators.iter().cloned(),
46			)?;
47		}
48
49		assert_eq!(ValidatorSet::<T>::get().map(|set| set.era), Some(1));
50		Ok(())
51	}
52
53	// Measures one send to the first entry of `Destinations`. `announce_weight` charges this
54	// weight once for every destination.
55	#[benchmark]
56	fn send_announcement(n: Linear<1, { T::MaxValidators::get() }>) -> Result<(), BenchmarkError> {
57		let validators = (0..n).map(|i| account("validator", i, 0)).collect::<Vec<T::AccountId>>();
58		let destination = T::Destinations::get()
59			.into_iter()
60			.next()
61			.ok_or(BenchmarkError::Stop("no destination is configured"))?;
62		T::Sender::ensure_successful_send(&destination);
63		// Leave a set queued for the destination, the worst case for a transport that appends
64		// to queued messages.
65		T::Sender::send(&destination, 0, &validators)
66			.map_err(|_| BenchmarkError::Stop("the sender rejected the set"))?;
67
68		#[block]
69		{
70			Pallet::<T>::send_to(destination.clone(), 1, &validators);
71		}
72
73		frame_system::Pallet::<T>::assert_last_event(
74			<T as frame_system::Config>::RuntimeEvent::from(Event::<T>::AnnouncementSent {
75				destination,
76				era: 1,
77			}),
78		);
79		Ok(())
80	}
81
82	impl_benchmark_test_suite!(
83		ValidatorSetAnnouncer,
84		crate::mock::new_test_ext(),
85		crate::mock::Test
86	);
87}