referrerpolicy=no-referrer-when-downgrade

pallet_election_provider_multi_block/unsigned/
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
18use crate::{
19	unsigned::{miner::OffchainWorkerMiner, Call, Config, Pallet},
20	verifier::Verifier,
21	CurrentPhase, Phase,
22};
23use frame_benchmarking::v2::*;
24use frame_election_provider_support::ElectionProvider;
25use frame_support::{assert_ok, pallet_prelude::*};
26use frame_system::RawOrigin;
27use sp_std::boxed::Box;
28
29#[benchmarks(where T: crate::Config + crate::signed::Config + crate::verifier::Config)]
30mod benchmarks {
31	use super::*;
32
33	#[benchmark(pov_mode = Measured)]
34	fn validate_unsigned() -> Result<(), BenchmarkError> {
35		#[cfg(test)]
36		crate::mock::ElectionStart::set(sp_runtime::traits::Bounded::max_value());
37		crate::Pallet::<T>::start().unwrap();
38
39		crate::Pallet::<T>::roll_until_matches(|| {
40			matches!(CurrentPhase::<T>::get(), Phase::Unsigned(_))
41		});
42		let call: Call<T> = OffchainWorkerMiner::<T>::mine_solution(T::MinerPages::get(), false)
43			.map(|solution| Call::submit_unsigned { paged_solution: Box::new(solution) })
44			.unwrap();
45
46		#[block]
47		{
48			#[allow(deprecated)]
49			let result = Pallet::<T>::validate_unsigned(TransactionSource::Local, &call);
50			assert_ok!(result);
51		}
52
53		Ok(())
54	}
55
56	#[benchmark(pov_mode = Measured)]
57	fn submit_unsigned() -> Result<(), BenchmarkError> {
58		#[cfg(test)]
59		crate::mock::ElectionStart::set(sp_runtime::traits::Bounded::max_value());
60		crate::Pallet::<T>::start().unwrap();
61
62		// roll to unsigned phase open
63		crate::Pallet::<T>::roll_until_matches(|| {
64			matches!(CurrentPhase::<T>::get(), Phase::Unsigned(_))
65		});
66		// TODO: we need to better ensure that this is actually worst case
67		let solution =
68			OffchainWorkerMiner::<T>::mine_solution(T::MinerPages::get(), false).unwrap();
69
70		// nothing is queued
71		assert!(T::Verifier::queued_score().is_none());
72		#[block]
73		{
74			assert_ok!(Pallet::<T>::submit_unsigned(RawOrigin::None.into(), Box::new(solution)));
75		}
76
77		// something is queued
78		assert!(T::Verifier::queued_score().is_some());
79		Ok(())
80	}
81
82	/// NOTE: make sure this benchmark is being run with the correct `type Solver` in `MinerConfig`.
83	#[benchmark(extra, pov_mode = Measured)]
84	fn mine_solution(p: Linear<1, { T::Pages::get() }>) -> Result<(), BenchmarkError> {
85		#[cfg(test)]
86		crate::mock::ElectionStart::set(sp_runtime::traits::Bounded::max_value());
87		crate::Pallet::<T>::start().unwrap();
88
89		// roll to unsigned phase open
90		crate::Pallet::<T>::roll_until_matches(|| {
91			matches!(CurrentPhase::<T>::get(), Phase::Unsigned(_))
92		});
93
94		#[block]
95		{
96			OffchainWorkerMiner::<T>::mine_solution(p, true).unwrap();
97		}
98
99		Ok(())
100	}
101
102	impl_benchmark_test_suite!(
103		Pallet,
104		crate::mock::ExtBuilder::full().build_unchecked(),
105		crate::mock::Runtime
106	);
107}