referrerpolicy=no-referrer-when-downgrade

pallet_staking_async/
slashing.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//! A slashing implementation for NPoS systems.
19//!
20//! For the purposes of the economic model, it is easiest to think of each validator as a nominator
21//! which nominates only its own identity.
22//!
23//! The act of nomination signals intent to unify economic identity with the validator - to take
24//! part in the rewards of a job well done, and to take part in the punishment of a job done badly.
25//!
26//! There are 3 main difficulties to account for with slashing in NPoS:
27//!   - A nominator can nominate multiple validators and be slashed via any of them.
28//!   - Until slashed, stake is reused from era to era. Nominating with N coins for E eras in a row
29//!     does not mean you have N*E coins to be slashed - you've only ever had N.
30//!   - Slashable offences can be found after the fact and out of order.
31//!
32//! We only slash participants for the _maximum_ slash they receive in some time period (era),
33//! rather than the sum. This ensures a protection from overslashing.
34//!
35//! In most of the cases, thanks to validator disabling, an offender won't be able to commit more
36//! than one offence. An exception is the case when the number of offenders reaches the
37//! Byzantine threshold. In that case one or more offenders with the smallest offence will be
38//! re-enabled and they can commit another offence. But as noted previously, even in this case we
39//! slash the offender only for the biggest offence committed within an era.
40//!
41//! Based on research at <https://research.web3.foundation/Polkadot/security/slashing/npos>
42
43use crate::{
44	asset, log, session_rotation::Eras, BalanceOf, Config, NegativeImbalanceOf, OffenceQueue,
45	OffenceQueueEras, PagedExposure, Pallet, Perbill, ProcessingOffence, SlashRewardFraction,
46	UnappliedSlash, UnappliedSlashes, ValidatorSlashInEra, WeightInfo,
47};
48use alloc::vec::Vec;
49use codec::{Decode, Encode, MaxEncodedLen};
50use frame_support::traits::{Defensive, DefensiveSaturating, Get, Imbalance, OnUnbalanced};
51use scale_info::TypeInfo;
52use sp_runtime::{
53	traits::{Saturating, Zero},
54	RuntimeDebug, WeakBoundedVec, Weight,
55};
56use sp_staking::{EraIndex, StakingInterface};
57
58/// Parameters for performing a slash.
59#[derive(Clone)]
60pub(crate) struct SlashParams<'a, T: 'a + Config> {
61	/// The stash account being slashed.
62	pub(crate) stash: &'a T::AccountId,
63	/// The proportion of the slash.
64	pub(crate) slash: Perbill,
65	/// The prior slash proportion of the validator if the validator has been reported multiple
66	/// times in the same era, and a new greater slash replaces the old one.
67	/// Invariant: slash > prior_slash
68	pub(crate) prior_slash: Perbill,
69	/// The exposure of the stash and all nominators.
70	pub(crate) exposure: &'a PagedExposure<T::AccountId, BalanceOf<T>>,
71	/// The era where the offence occurred.
72	pub(crate) slash_era: EraIndex,
73	/// The maximum percentage of a slash that ever gets paid out.
74	/// This is f_inf in the paper.
75	pub(crate) reward_proportion: Perbill,
76}
77
78/// Represents an offence record within the staking system, capturing details about a slashing
79/// event.
80#[derive(Clone, Encode, Decode, TypeInfo, MaxEncodedLen, PartialEq, RuntimeDebug)]
81pub struct OffenceRecord<AccountId> {
82	/// The account ID of the entity that reported the offence.
83	pub reporter: Option<AccountId>,
84
85	/// Era at which the offence was reported.
86	pub reported_era: EraIndex,
87
88	/// The specific page of the validator's exposure currently being processed.
89	///
90	/// Since a validator's total exposure can span multiple pages, this field serves as a pointer
91	/// to the current page being evaluated. The processing order starts from the last page
92	/// and moves backward, decrementing this value with each processed page.
93	///
94	/// This ensures that all pages are systematically handled, and it helps track when
95	/// the entire exposure has been processed.
96	pub exposure_page: u32,
97
98	/// The fraction of the validator's stake to be slashed for this offence.
99	pub slash_fraction: Perbill,
100
101	/// The previous slash fraction of the validator's stake before being updated.
102	/// If a new, higher slash fraction is reported, this field stores the prior fraction
103	/// that was overwritten. This helps in tracking changes in slashes across multiple reports for
104	/// the same era.
105	pub prior_slash_fraction: Perbill,
106}
107
108/// Loads next offence in the processing offence and returns the offense record to be processed.
109///
110/// Note: this can mutate the following storage
111/// - `ProcessingOffence`
112/// - `OffenceQueue`
113/// - `OffenceQueueEras`
114fn next_offence<T: Config>() -> Option<(EraIndex, T::AccountId, OffenceRecord<T::AccountId>)> {
115	let maybe_processing_offence = ProcessingOffence::<T>::get();
116
117	if let Some((offence_era, offender, offence_record)) = maybe_processing_offence {
118		// If the exposure page is 0, then the offence has been processed.
119		if offence_record.exposure_page == 0 {
120			ProcessingOffence::<T>::kill();
121			return Some((offence_era, offender, offence_record))
122		}
123
124		// Update the next page.
125		ProcessingOffence::<T>::put((
126			offence_era,
127			&offender,
128			OffenceRecord {
129				// decrement the page index.
130				exposure_page: offence_record.exposure_page.defensive_saturating_sub(1),
131				..offence_record.clone()
132			},
133		));
134
135		return Some((offence_era, offender, offence_record))
136	}
137
138	// Nothing in processing offence. Try to enqueue the next offence.
139	let Some(mut eras) = OffenceQueueEras::<T>::get() else { return None };
140	let Some(&oldest_era) = eras.first() else { return None };
141
142	let mut offence_iter = OffenceQueue::<T>::iter_prefix(oldest_era);
143	let next_offence = offence_iter.next();
144
145	if let Some((ref validator, ref offence_record)) = next_offence {
146		// Update the processing offence if the offence is multi-page.
147		if offence_record.exposure_page > 0 {
148			// update processing offence with the next page.
149			ProcessingOffence::<T>::put((
150				oldest_era,
151				validator.clone(),
152				OffenceRecord {
153					exposure_page: offence_record.exposure_page.defensive_saturating_sub(1),
154					..offence_record.clone()
155				},
156			));
157		}
158
159		// Remove from `OffenceQueue`
160		OffenceQueue::<T>::remove(oldest_era, &validator);
161	}
162
163	// If there are no offences left for the era, remove the era from `OffenceQueueEras`.
164	if offence_iter.next().is_none() {
165		if eras.len() == 1 {
166			// If there is only one era left, remove the entire queue.
167			OffenceQueueEras::<T>::kill();
168		} else {
169			// Remove the oldest era
170			eras.remove(0);
171			OffenceQueueEras::<T>::put(eras);
172		}
173	}
174
175	next_offence.map(|(v, o)| (oldest_era, v, o))
176}
177
178/// Infallible function to process an offence.
179pub(crate) fn process_offence<T: Config>() -> Weight {
180	// We do manual weight tracking for early-returns, and use benchmarks for the final two
181	// branches.
182	let mut incomplete_consumed_weight = Weight::from_parts(0, 0);
183	let mut add_db_reads_writes = |reads, writes| {
184		incomplete_consumed_weight += T::DbWeight::get().reads_writes(reads, writes);
185	};
186
187	add_db_reads_writes(1, 1);
188	let Some((offence_era, offender, offence_record)) = next_offence::<T>() else {
189		return incomplete_consumed_weight
190	};
191
192	log!(
193		debug,
194		"🦹 Processing offence for {:?} in era {:?} with slash fraction {:?}",
195		offender,
196		offence_era,
197		offence_record.slash_fraction,
198	);
199
200	add_db_reads_writes(1, 0);
201	let reward_proportion = SlashRewardFraction::<T>::get();
202
203	add_db_reads_writes(2, 0);
204	let Some(exposure) =
205		Eras::<T>::get_paged_exposure(offence_era, &offender, offence_record.exposure_page)
206	else {
207		// this can only happen if the offence was valid at the time of reporting but became too old
208		// at the time of computing and should be discarded.
209		return incomplete_consumed_weight
210	};
211
212	let slash_page = offence_record.exposure_page;
213	let slash_defer_duration = T::SlashDeferDuration::get();
214	let slash_era = offence_era.saturating_add(slash_defer_duration);
215
216	add_db_reads_writes(3, 3);
217	let Some(mut unapplied) = compute_slash::<T>(SlashParams {
218		stash: &offender,
219		slash: offence_record.slash_fraction,
220		prior_slash: offence_record.prior_slash_fraction,
221		exposure: &exposure,
222		slash_era: offence_era,
223		reward_proportion,
224	}) else {
225		log!(
226			debug,
227			"🦹 Slash of {:?}% happened in {:?} (reported in {:?}) is discarded, as could not compute slash",
228			offence_record.slash_fraction,
229			offence_era,
230			offence_record.reported_era,
231		);
232		// No slash to apply. Discard.
233		return incomplete_consumed_weight
234	};
235
236	<Pallet<T>>::deposit_event(super::Event::<T>::SlashComputed {
237		offence_era,
238		slash_era,
239		offender: offender.clone(),
240		page: slash_page,
241	});
242
243	log!(
244		debug,
245		"🦹 Slash of {:?}% happened in {:?} (reported in {:?}) is computed",
246		offence_record.slash_fraction,
247		offence_era,
248		offence_record.reported_era,
249	);
250
251	// add the reporter to the unapplied slash.
252	unapplied.reporter = offence_record.reporter;
253
254	if slash_defer_duration == 0 {
255		// Apply right away.
256		log!(
257			debug,
258			"🦹 applying slash instantly of {:?} happened in {:?} (reported in {:?}) to {:?}",
259			offence_record.slash_fraction,
260			offence_era,
261			offence_record.reported_era,
262			offender,
263		);
264
265		apply_slash::<T>(unapplied, offence_era);
266		T::WeightInfo::apply_slash().saturating_add(T::WeightInfo::process_offence_queue())
267	} else {
268		// Historical Note: Previously, with BondingDuration = 28 and SlashDeferDuration = 27,
269		// slashes were applied at the start of the 28th era from `offence_era`.
270		// However, with paged slashing, applying slashes now takes multiple blocks.
271		// To account for this delay, slashes are now applied at the start of the 27th era from
272		// `offence_era`.
273		log!(
274			debug,
275			"🦹 deferring slash of {:?}% happened in {:?} (reported in {:?}) to {:?}",
276			offence_record.slash_fraction,
277			offence_era,
278			offence_record.reported_era,
279			slash_era,
280		);
281		UnappliedSlashes::<T>::insert(
282			slash_era,
283			(offender, offence_record.slash_fraction, slash_page),
284			unapplied,
285		);
286		T::WeightInfo::process_offence_queue()
287	}
288}
289
290/// Computes a slash of a validator and nominators. It returns an unapplied
291/// record to be applied at some later point. Slashing metadata is updated in storage,
292/// since unapplied records are only rarely intended to be dropped.
293///
294/// The pending slash record returned does not have initialized reporters. Those have
295/// to be set at a higher level, if any.
296///
297/// If `nomintors_only` is set to `true`, only the nominator slashes will be computed.
298pub(crate) fn compute_slash<T: Config>(params: SlashParams<T>) -> Option<UnappliedSlash<T>> {
299	let (val_slashed, mut reward_payout) = slash_validator::<T>(params.clone());
300
301	let mut nominators_slashed = Vec::new();
302	let (nom_slashed, nom_reward_payout) =
303		slash_nominators::<T>(params.clone(), &mut nominators_slashed);
304	reward_payout += nom_reward_payout;
305
306	(nom_slashed + val_slashed > Zero::zero()).then_some(UnappliedSlash {
307		validator: params.stash.clone(),
308		own: val_slashed,
309		others: WeakBoundedVec::force_from(
310			nominators_slashed,
311			Some("slashed nominators not expected to be larger than the bounds"),
312		),
313		reporter: None,
314		payout: reward_payout,
315	})
316}
317
318/// Compute the slash for a validator. Returns the amount slashed and the reward payout.
319fn slash_validator<T: Config>(params: SlashParams<T>) -> (BalanceOf<T>, BalanceOf<T>) {
320	let own_stake = params.exposure.exposure_metadata.own;
321	let prior_slashed = params.prior_slash * own_stake;
322	let new_total_slash = params.slash * own_stake;
323
324	let slash_due = new_total_slash.saturating_sub(prior_slashed);
325	// Audit Note: Previously, each repeated slash reduced the reward by 50% (e.g., 50% × 50% for
326	// two offences). Since repeat offences in the same era are discarded unless the new slash is
327	// higher, this reduction logic was unnecessary and removed.
328	let reward_due = params.reward_proportion * slash_due;
329	log!(
330		warn,
331		"🦹 slashing validator {:?} of stake: {:?} for {:?} in era {:?}",
332		params.stash,
333		own_stake,
334		slash_due,
335		params.slash_era,
336	);
337
338	(slash_due, reward_due)
339}
340
341/// Slash nominators. Accepts general parameters and the prior slash percentage of the validator.
342///
343/// Returns the total amount slashed and amount of reward to pay out.
344fn slash_nominators<T: Config>(
345	params: SlashParams<T>,
346	nominators_slashed: &mut Vec<(T::AccountId, BalanceOf<T>)>,
347) -> (BalanceOf<T>, BalanceOf<T>) {
348	let mut reward_payout = BalanceOf::<T>::zero();
349	let mut total_slashed = BalanceOf::<T>::zero();
350
351	nominators_slashed.reserve(params.exposure.exposure_page.others.len());
352	for nominator in &params.exposure.exposure_page.others {
353		let stash = &nominator.who;
354		let prior_slashed = params.prior_slash * nominator.value;
355		let new_slash = params.slash * nominator.value;
356		// this should always be positive since prior slash is always less than the new slash or
357		// filtered out when offence is reported (`Pallet::on_new_offences`).
358		let slash_diff = new_slash.defensive_saturating_sub(prior_slashed);
359
360		if slash_diff == Zero::zero() {
361			// nothing to do
362			continue
363		}
364
365		log!(
366			debug,
367			"🦹 slashing nominator {:?} of stake: {:?} for {:?} in era {:?}. Prior Slash: {:?}, New Slash: {:?}",
368			stash,
369			nominator.value,
370			slash_diff,
371			params.slash_era,
372			params.prior_slash,
373			params.slash,
374		);
375
376		nominators_slashed.push((stash.clone(), slash_diff));
377		total_slashed.saturating_accrue(slash_diff);
378		reward_payout.saturating_accrue(params.reward_proportion * slash_diff);
379	}
380
381	(total_slashed, reward_payout)
382}
383
384/// Clear slashing metadata for an obsolete era.
385pub(crate) fn clear_era_metadata<T: Config>(obsolete_era: EraIndex) {
386	#[allow(deprecated)]
387	ValidatorSlashInEra::<T>::remove_prefix(&obsolete_era, None);
388}
389
390// apply the slash to a stash account, deducting any missing funds from the reward
391// payout, saturating at 0. this is mildly unfair but also an edge-case that
392// can only occur when overlapping locked funds have been slashed.
393pub fn do_slash<T: Config>(
394	stash: &T::AccountId,
395	value: BalanceOf<T>,
396	reward_payout: &mut BalanceOf<T>,
397	slashed_imbalance: &mut NegativeImbalanceOf<T>,
398	slash_era: EraIndex,
399) {
400	let mut ledger =
401		match Pallet::<T>::ledger(sp_staking::StakingAccount::Stash(stash.clone())).defensive() {
402			Ok(ledger) => ledger,
403			Err(_) => return, // nothing to do.
404		};
405
406	let value = ledger.slash(value, asset::existential_deposit::<T>(), slash_era);
407	if value.is_zero() {
408		// nothing to do
409		return
410	}
411
412	// Skip slashing for virtual stakers. The pallets managing them should handle the slashing.
413	if !Pallet::<T>::is_virtual_staker(stash) {
414		let (imbalance, missing) = asset::slash::<T>(stash, value);
415		slashed_imbalance.subsume(imbalance);
416
417		if !missing.is_zero() {
418			// deduct overslash from the reward payout
419			*reward_payout = reward_payout.saturating_sub(missing);
420		}
421	}
422
423	let _ = ledger
424		.update()
425		.defensive_proof("ledger fetched from storage so it exists in storage; qed.");
426
427	// trigger the event
428	<Pallet<T>>::deposit_event(super::Event::<T>::Slashed { staker: stash.clone(), amount: value });
429}
430
431/// Apply a previously-unapplied slash.
432pub(crate) fn apply_slash<T: Config>(unapplied_slash: UnappliedSlash<T>, slash_era: EraIndex) {
433	let mut slashed_imbalance = NegativeImbalanceOf::<T>::zero();
434	let mut reward_payout = unapplied_slash.payout;
435
436	if unapplied_slash.own > Zero::zero() {
437		do_slash::<T>(
438			&unapplied_slash.validator,
439			unapplied_slash.own,
440			&mut reward_payout,
441			&mut slashed_imbalance,
442			slash_era,
443		);
444	}
445
446	for &(ref nominator, nominator_slash) in &unapplied_slash.others {
447		if nominator_slash.is_zero() {
448			continue
449		}
450
451		do_slash::<T>(
452			nominator,
453			nominator_slash,
454			&mut reward_payout,
455			&mut slashed_imbalance,
456			slash_era,
457		);
458	}
459
460	pay_reporters::<T>(
461		reward_payout,
462		slashed_imbalance,
463		&unapplied_slash.reporter.map(|v| crate::vec![v]).unwrap_or_default(),
464	);
465}
466
467/// Apply a reward payout to some reporters, paying the rewards out of the slashed imbalance.
468fn pay_reporters<T: Config>(
469	reward_payout: BalanceOf<T>,
470	slashed_imbalance: NegativeImbalanceOf<T>,
471	reporters: &[T::AccountId],
472) {
473	if reward_payout.is_zero() || reporters.is_empty() {
474		// nobody to pay out to or nothing to pay;
475		// just treat the whole value as slashed.
476		T::Slash::on_unbalanced(slashed_imbalance);
477		return
478	}
479
480	// take rewards out of the slashed imbalance.
481	let reward_payout = reward_payout.min(slashed_imbalance.peek());
482	let (mut reward_payout, mut value_slashed) = slashed_imbalance.split(reward_payout);
483
484	let per_reporter = reward_payout.peek() / (reporters.len() as u32).into();
485	for reporter in reporters {
486		let (reporter_reward, rest) = reward_payout.split(per_reporter);
487		reward_payout = rest;
488
489		// this cancels out the reporter reward imbalance internally, leading
490		// to no change in total issuance.
491		asset::deposit_slashed::<T>(reporter, reporter_reward);
492	}
493
494	// the rest goes to the on-slash imbalance handler (e.g. treasury)
495	value_slashed.subsume(reward_payout); // remainder of reward division remains.
496	T::Slash::on_unbalanced(value_slashed);
497}