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::*, traits::Authorize};
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 authorize_submit_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			let result = call
49				.authorize(TransactionSource::Local)
50				.expect("submit_unsigned declares an authorize callback; qed");
51			assert_ok!(result);
52		}
53
54		Ok(())
55	}
56
57	#[benchmark(pov_mode = Measured)]
58	fn submit_unsigned() -> Result<(), BenchmarkError> {
59		#[cfg(test)]
60		crate::mock::ElectionStart::set(sp_runtime::traits::Bounded::max_value());
61		crate::Pallet::<T>::start().unwrap();
62
63		// roll to unsigned phase open
64		crate::Pallet::<T>::roll_until_matches(|| {
65			matches!(CurrentPhase::<T>::get(), Phase::Unsigned(_))
66		});
67		// TODO: we need to better ensure that this is actually worst case
68		let solution =
69			OffchainWorkerMiner::<T>::mine_solution(T::MinerPages::get(), false).unwrap();
70
71		// nothing is queued
72		assert!(T::Verifier::queued_score().is_none());
73		#[block]
74		{
75			assert_ok!(Pallet::<T>::submit_unsigned(
76				RawOrigin::Authorized.into(),
77				Box::new(solution)
78			));
79		}
80
81		// something is queued
82		assert!(T::Verifier::queued_score().is_some());
83		Ok(())
84	}
85
86	/// NOTE: make sure this benchmark is being run with the correct `type Solver` in `MinerConfig`.
87	#[benchmark(extra, pov_mode = Measured)]
88	fn mine_solution(p: Linear<1, { T::Pages::get() }>) -> Result<(), BenchmarkError> {
89		#[cfg(test)]
90		crate::mock::ElectionStart::set(sp_runtime::traits::Bounded::max_value());
91		crate::Pallet::<T>::start().unwrap();
92
93		// roll to unsigned phase open
94		crate::Pallet::<T>::roll_until_matches(|| {
95			matches!(CurrentPhase::<T>::get(), Phase::Unsigned(_))
96		});
97
98		#[block]
99		{
100			OffchainWorkerMiner::<T>::mine_solution(p, true).unwrap();
101		}
102
103		Ok(())
104	}
105
106	impl_benchmark_test_suite!(
107		Pallet,
108		crate::mock::ExtBuilder::full().build_unchecked(),
109		crate::mock::Runtime
110	);
111}