referrerpolicy=no-referrer-when-downgrade

pallet_offences_benchmarking/
inner.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
18//! Offences pallet benchmarking.
19
20use alloc::{vec, vec::Vec};
21use frame_benchmarking::v2::*;
22use frame_support::traits::Get;
23use frame_system::{Config as SystemConfig, Pallet as System, RawOrigin};
24use pallet_babe::EquivocationOffence as BabeEquivocationOffence;
25use pallet_balances::Config as BalancesConfig;
26use pallet_grandpa::{
27	EquivocationOffence as GrandpaEquivocationOffence, TimeSlot as GrandpaTimeSlot,
28};
29use pallet_offences::{Config as OffencesConfig, Pallet as Offences};
30use pallet_session::{
31	historical::{Config as HistoricalConfig, IdentificationTuple},
32	Config as SessionConfig, Pallet as Session,
33};
34use pallet_staking::{
35	Config as StakingConfig, Exposure, IndividualExposure, MaxNominationsOf, Pallet as Staking,
36	RewardDestination, ValidatorPrefs,
37};
38use sp_runtime::{
39	traits::{Convert, Saturating, StaticLookup},
40	Perbill,
41};
42use sp_staking::offence::ReportOffence;
43
44const SEED: u32 = 0;
45
46const MAX_NOMINATORS: u32 = 100;
47
48pub struct Pallet<T: Config>(Offences<T>);
49
50pub trait Config:
51	SessionConfig<ValidatorId = <Self as frame_system::Config>::AccountId>
52	+ pallet_session_benchmarking::Config
53	+ StakingConfig
54	+ OffencesConfig
55	+ HistoricalConfig
56	+ BalancesConfig
57	+ IdTupleConvert<Self>
58{
59}
60
61/// A helper trait to make sure we can convert `IdentificationTuple` coming from historical
62/// and the one required by offences.
63pub trait IdTupleConvert<T: HistoricalConfig + OffencesConfig> {
64	/// Convert identification tuple from `historical` trait to the one expected by `offences`.
65	fn convert(id: IdentificationTuple<T>) -> <T as OffencesConfig>::IdentificationTuple;
66}
67
68impl<T: HistoricalConfig + OffencesConfig> IdTupleConvert<T> for T
69where
70	<T as OffencesConfig>::IdentificationTuple: From<IdentificationTuple<T>>,
71{
72	fn convert(id: IdentificationTuple<T>) -> <T as OffencesConfig>::IdentificationTuple {
73		id.into()
74	}
75}
76
77type LookupSourceOf<T> = <<T as SystemConfig>::Lookup as StaticLookup>::Source;
78type BalanceOf<T> = <T as StakingConfig>::CurrencyBalance;
79
80struct Offender<T: Config> {
81	pub controller: T::AccountId,
82	#[allow(dead_code)]
83	pub stash: T::AccountId,
84	#[allow(dead_code)]
85	pub nominator_stashes: Vec<T::AccountId>,
86}
87
88fn bond_amount<T: Config>() -> BalanceOf<T> {
89	pallet_staking::asset::existential_deposit::<T>().saturating_mul(10_000u32.into())
90}
91
92fn create_offender<T: Config>(n: u32, nominators: u32) -> Result<Offender<T>, &'static str> {
93	let stash: T::AccountId = account("stash", n, SEED);
94	let stash_lookup: LookupSourceOf<T> = T::Lookup::unlookup(stash.clone());
95	let reward_destination = RewardDestination::Staked;
96	let amount = bond_amount::<T>();
97	// add twice as much balance to prevent the account from being killed.
98	let free_amount = amount.saturating_mul(2u32.into());
99	pallet_staking::asset::set_stakeable_balance::<T>(&stash, free_amount);
100	Staking::<T>::bond(
101		RawOrigin::Signed(stash.clone()).into(),
102		amount,
103		reward_destination.clone(),
104	)?;
105
106	let validator_prefs =
107		ValidatorPrefs { commission: Perbill::from_percent(50), ..Default::default() };
108	Staking::<T>::validate(RawOrigin::Signed(stash.clone()).into(), validator_prefs)?;
109
110	// set some fake keys for the validators.
111	let (keys, proof) = T::generate_session_keys_and_proof(stash.clone());
112	Session::<T>::ensure_can_pay_key_deposit(&stash)?;
113	Session::<T>::set_keys(RawOrigin::Signed(stash.clone()).into(), keys, proof)?;
114
115	let mut individual_exposures = vec![];
116	let mut nominator_stashes = vec![];
117	// Create n nominators
118	for i in 0..nominators {
119		let nominator_stash: T::AccountId =
120			account("nominator stash", n * MAX_NOMINATORS + i, SEED);
121		pallet_staking::asset::set_stakeable_balance::<T>(&nominator_stash, free_amount);
122
123		Staking::<T>::bond(
124			RawOrigin::Signed(nominator_stash.clone()).into(),
125			amount,
126			reward_destination.clone(),
127		)?;
128
129		let selected_validators: Vec<LookupSourceOf<T>> = vec![stash_lookup.clone()];
130		Staking::<T>::nominate(
131			RawOrigin::Signed(nominator_stash.clone()).into(),
132			selected_validators,
133		)?;
134
135		individual_exposures
136			.push(IndividualExposure { who: nominator_stash.clone(), value: amount });
137		nominator_stashes.push(nominator_stash.clone());
138	}
139
140	let exposure = Exposure { total: amount * n.into(), own: amount, others: individual_exposures };
141	let current_era = 0u32;
142	Staking::<T>::add_era_stakers(current_era, stash.clone(), exposure);
143
144	Ok(Offender { controller: stash.clone(), stash, nominator_stashes })
145}
146
147fn make_offenders<T: Config>(
148	num_offenders: u32,
149	num_nominators: u32,
150) -> Result<Vec<IdentificationTuple<T>>, &'static str> {
151	let mut offenders = vec![];
152	for i in 0..num_offenders {
153		let offender = create_offender::<T>(i + 1, num_nominators)?;
154		// add them to the session validators -- this is needed since `FullIdentificationOf` usually
155		// checks this.
156		pallet_session::Validators::<T>::mutate(|v| v.push(offender.controller.clone()));
157		offenders.push(offender);
158	}
159
160	let id_tuples = offenders
161		.iter()
162		.map(|offender| {
163			<T as SessionConfig>::ValidatorIdOf::convert(offender.controller.clone())
164				.expect("failed to get validator id from account id")
165		})
166		.map(|validator_id| {
167			<T as HistoricalConfig>::FullIdentificationOf::convert(validator_id.clone())
168				.map(|full_id| (validator_id, full_id))
169				.unwrap()
170		})
171		.collect::<Vec<IdentificationTuple<T>>>();
172
173	if pallet_staking::ActiveEra::<T>::get().is_none() {
174		pallet_staking::ActiveEra::<T>::put(pallet_staking::ActiveEraInfo {
175			index: 0,
176			start: Some(0),
177		});
178	}
179
180	Ok(id_tuples)
181}
182
183#[cfg(test)]
184fn assert_all_slashes_applied<T>(offender_count: usize)
185where
186	T: Config,
187	<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_staking::Event<T>>,
188	<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_balances::Event<T>>,
189	<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_offences::Event>,
190	<T as frame_system::Config>::RuntimeEvent: TryInto<frame_system::Event<T>>,
191{
192	// make sure that all slashes have been applied and TotalIssuance adjusted(BurnedDebt).
193	// deposit to reporter + reporter account endowed.
194	assert_eq!(System::<T>::read_events_for_pallet::<pallet_balances::Event<T>>().len(), 3);
195	// (n nominators + one validator) * slashed + Slash Reported + Slash Computed
196	assert_eq!(
197		System::<T>::read_events_for_pallet::<pallet_staking::Event<T>>().len(),
198		1 * (offender_count + 1) as usize + 1
199	);
200	// offence
201	assert_eq!(System::<T>::read_events_for_pallet::<pallet_offences::Event>().len(), 1);
202	// reporter new account
203	assert_eq!(System::<T>::read_events_for_pallet::<frame_system::Event<T>>().len(), 1);
204}
205
206#[benchmarks(
207	where
208		<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_staking::Event<T>>,
209		<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_balances::Event<T>>,
210		<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_offences::Event>,
211		<T as frame_system::Config>::RuntimeEvent: TryInto<frame_system::Event<T>>,
212)]
213mod benchmarks {
214	use super::*;
215
216	#[benchmark]
217	pub fn report_offence_grandpa(
218		n: Linear<0, { MAX_NOMINATORS.min(MaxNominationsOf::<T>::get()) }>,
219	) -> Result<(), BenchmarkError> {
220		// for grandpa equivocation reports the number of reporters
221		// and offenders is always 1
222		let reporters = vec![account("reporter", 1, SEED)];
223
224		// make sure reporters actually get rewarded
225		Staking::<T>::set_slash_reward_fraction(Perbill::one());
226
227		let mut offenders = make_offenders::<T>(1, n)?;
228		let validator_set_count = Session::<T>::validators().len() as u32;
229
230		let offence = GrandpaEquivocationOffence {
231			time_slot: GrandpaTimeSlot { set_id: 0, round: 0 },
232			session_index: 0,
233			validator_set_count,
234			offender: T::convert(offenders.pop().unwrap()),
235		};
236		assert_eq!(System::<T>::event_count(), 0);
237
238		#[block]
239		{
240			let _ = Offences::<T>::report_offence(reporters, offence);
241		}
242
243		#[cfg(test)]
244		{
245			assert_all_slashes_applied::<T>(n as usize);
246		}
247
248		Ok(())
249	}
250
251	#[benchmark]
252	fn report_offence_babe(
253		n: Linear<0, { MAX_NOMINATORS.min(MaxNominationsOf::<T>::get()) }>,
254	) -> Result<(), BenchmarkError> {
255		// for babe equivocation reports the number of reporters
256		// and offenders is always 1
257		let reporters = vec![account("reporter", 1, SEED)];
258
259		// make sure reporters actually get rewarded
260		Staking::<T>::set_slash_reward_fraction(Perbill::one());
261
262		let mut offenders = make_offenders::<T>(1, n)?;
263		let validator_set_count = Session::<T>::validators().len() as u32;
264
265		let offence = BabeEquivocationOffence {
266			slot: 0u64.into(),
267			session_index: 0,
268			validator_set_count,
269			offender: T::convert(offenders.pop().unwrap()),
270		};
271		assert_eq!(System::<T>::event_count(), 0);
272
273		#[block]
274		{
275			let _ = Offences::<T>::report_offence(reporters, offence);
276		}
277		#[cfg(test)]
278		{
279			assert_all_slashes_applied::<T>(n as usize);
280		}
281
282		Ok(())
283	}
284
285	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
286}