referrerpolicy=no-referrer-when-downgrade

pallet_staking_async_points_converter/
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-staking-async-points-converter.
17
18#![cfg(feature = "runtime-benchmarks")]
19
20use super::*;
21
22#[allow(unused)]
23use crate::Pallet as StakingPointsConverter;
24use alloc::vec::Vec;
25use codec::Decode;
26use frame_benchmarking::{account, v2::*, BenchmarkError};
27use frame_support::traits::{EnsureOrigin, Get};
28use sp_runtime::traits::TrailingZeroInput;
29use sp_staking::ServiceKind;
30
31fn any_source<T: Config>() -> T::Source {
32	T::Source::decode(&mut TrailingZeroInput::zeroes())
33		.expect("a source decodes from an infinite input of zeroes; qed")
34}
35
36#[benchmarks]
37mod benchmarks {
38	use super::*;
39
40	#[benchmark]
41	fn set_points_per_service() -> Result<(), BenchmarkError> {
42		let origin =
43			T::PointsOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
44		let source = any_source::<T>();
45		let points = T::MaxPointsPerService::get();
46
47		#[extrinsic_call]
48		_(origin as T::RuntimeOrigin, source, ServiceKind::Collation, points);
49
50		assert_eq!(PointsPerService::<T>::get(source, ServiceKind::Collation), points);
51		Ok(())
52	}
53
54	/// The part of `report_authored_blocks` this pallet does, measured with a report from a
55	/// source without points so that [`Config::RewardPoints`] is not measured here.
56	#[benchmark]
57	fn report_authored_blocks() {
58		let source = any_source::<T>();
59		PointsPerService::<T>::remove(source, ServiceKind::Collation);
60		let blocks = (0..1_000)
61			.map(|i| (account::<T::AccountId>("validator", i, 0), 1))
62			.collect::<Vec<_>>();
63		let total_blocks = blocks.len() as u32;
64
65		#[block]
66		{
67			Pallet::<T>::credit_authored_blocks(source, blocks);
68		}
69
70		frame_system::Pallet::<T>::assert_last_event(
71			<T as frame_system::Config>::RuntimeEvent::from(Event::<T>::BlocksIgnored {
72				source,
73				blocks: total_blocks,
74			}),
75		);
76	}
77
78	impl_benchmark_test_suite!(
79		StakingPointsConverter,
80		crate::mock::new_test_ext(),
81		crate::mock::Test
82	);
83}