mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-29 20:47:56 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -50,16 +50,19 @@
|
||||
//! Based on research at <https://w3f-research.readthedocs.io/en/latest/polkadot/slashing/npos.html>
|
||||
|
||||
use super::{
|
||||
EraIndex, Config, Pallet, Store, BalanceOf, Exposure, Perbill, SessionInterface,
|
||||
NegativeImbalanceOf, UnappliedSlash, Error,
|
||||
BalanceOf, Config, EraIndex, Error, Exposure, NegativeImbalanceOf, Pallet, Perbill,
|
||||
SessionInterface, Store, UnappliedSlash,
|
||||
};
|
||||
use sp_runtime::{traits::{Zero, Saturating}, RuntimeDebug, DispatchResult};
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::{
|
||||
ensure,
|
||||
traits::{Currency, OnUnbalanced, Imbalance},
|
||||
traits::{Currency, Imbalance, OnUnbalanced},
|
||||
};
|
||||
use sp_runtime::{
|
||||
traits::{Saturating, Zero},
|
||||
DispatchResult, RuntimeDebug,
|
||||
};
|
||||
use sp_std::vec::Vec;
|
||||
use codec::{Encode, Decode};
|
||||
|
||||
/// The proportion of the slashing reward to be paid out on the first slashing detection.
|
||||
/// This is f_1 in the paper.
|
||||
@@ -118,7 +121,9 @@ impl SlashingSpans {
|
||||
// that internal state is unchanged.
|
||||
pub(crate) fn end_span(&mut self, now: EraIndex) -> bool {
|
||||
let next_start = now + 1;
|
||||
if next_start <= self.last_start { return false }
|
||||
if next_start <= self.last_start {
|
||||
return false
|
||||
}
|
||||
|
||||
let last_length = next_start - self.last_start;
|
||||
self.prior.insert(0, last_length);
|
||||
@@ -153,7 +158,8 @@ impl SlashingSpans {
|
||||
// If this returns `Some`, then it includes a range start..end of all the span
|
||||
// indices which were pruned.
|
||||
fn prune(&mut self, window_start: EraIndex) -> Option<(SpanIndex, SpanIndex)> {
|
||||
let old_idx = self.iter()
|
||||
let old_idx = self
|
||||
.iter()
|
||||
.skip(1) // skip ongoing span.
|
||||
.position(|span| span.length.map_or(false, |len| span.start + len <= window_start));
|
||||
|
||||
@@ -163,7 +169,7 @@ impl SlashingSpans {
|
||||
self.prior.truncate(o);
|
||||
let new_earliest = self.span_index - self.prior.len() as SpanIndex;
|
||||
Some((earliest_span_index, new_earliest))
|
||||
}
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
|
||||
@@ -214,18 +220,11 @@ pub(crate) struct SlashParams<'a, T: 'a + Config> {
|
||||
///
|
||||
/// The pending slash record returned does not have initialized reporters. Those have
|
||||
/// to be set at a higher level, if any.
|
||||
pub(crate) fn compute_slash<T: Config>(params: SlashParams<T>)
|
||||
-> Option<UnappliedSlash<T::AccountId, BalanceOf<T>>>
|
||||
{
|
||||
let SlashParams {
|
||||
stash,
|
||||
slash,
|
||||
exposure,
|
||||
slash_era,
|
||||
window_start,
|
||||
now,
|
||||
reward_proportion,
|
||||
} = params.clone();
|
||||
pub(crate) fn compute_slash<T: Config>(
|
||||
params: SlashParams<T>,
|
||||
) -> Option<UnappliedSlash<T::AccountId, BalanceOf<T>>> {
|
||||
let SlashParams { stash, slash, exposure, slash_era, window_start, now, reward_proportion } =
|
||||
params.clone();
|
||||
|
||||
let mut reward_payout = Zero::zero();
|
||||
let mut val_slashed = Zero::zero();
|
||||
@@ -236,22 +235,17 @@ pub(crate) fn compute_slash<T: Config>(params: SlashParams<T>)
|
||||
// kick out the validator even if they won't be slashed,
|
||||
// as long as the misbehavior is from their most recent slashing span.
|
||||
kick_out_if_recent::<T>(params);
|
||||
return None;
|
||||
return None
|
||||
}
|
||||
|
||||
let (prior_slash_p, _era_slash) = <Pallet<T> as Store>::ValidatorSlashInEra::get(
|
||||
&slash_era,
|
||||
stash,
|
||||
).unwrap_or((Perbill::zero(), Zero::zero()));
|
||||
let (prior_slash_p, _era_slash) =
|
||||
<Pallet<T> as Store>::ValidatorSlashInEra::get(&slash_era, stash)
|
||||
.unwrap_or((Perbill::zero(), Zero::zero()));
|
||||
|
||||
// compare slash proportions rather than slash values to avoid issues due to rounding
|
||||
// error.
|
||||
if slash.deconstruct() > prior_slash_p.deconstruct() {
|
||||
<Pallet<T> as Store>::ValidatorSlashInEra::insert(
|
||||
&slash_era,
|
||||
stash,
|
||||
&(slash, own_slash),
|
||||
);
|
||||
<Pallet<T> as Store>::ValidatorSlashInEra::insert(&slash_era, stash, &(slash, own_slash));
|
||||
} else {
|
||||
// we slash based on the max in era - this new event is not the max,
|
||||
// so neither the validator or any nominators will need an update.
|
||||
@@ -260,7 +254,7 @@ pub(crate) fn compute_slash<T: Config>(params: SlashParams<T>)
|
||||
// pays out some reward even if the latest report is not max-in-era.
|
||||
// we opt to avoid the nominator lookups and edits and leave more rewards
|
||||
// for more drastic misbehavior.
|
||||
return None;
|
||||
return None
|
||||
}
|
||||
|
||||
// apply slash to validator.
|
||||
@@ -273,10 +267,7 @@ pub(crate) fn compute_slash<T: Config>(params: SlashParams<T>)
|
||||
reward_proportion,
|
||||
);
|
||||
|
||||
let target_span = spans.compare_and_update_span_slash(
|
||||
slash_era,
|
||||
own_slash,
|
||||
);
|
||||
let target_span = spans.compare_and_update_span_slash(slash_era, own_slash);
|
||||
|
||||
if target_span == Some(spans.span_index()) {
|
||||
// misbehavior occurred within the current slashing span - take appropriate
|
||||
@@ -309,9 +300,7 @@ pub(crate) fn compute_slash<T: Config>(params: SlashParams<T>)
|
||||
|
||||
// doesn't apply any slash, but kicks out the validator if the misbehavior is from
|
||||
// the most recent slashing span.
|
||||
fn kick_out_if_recent<T: Config>(
|
||||
params: SlashParams<T>,
|
||||
) {
|
||||
fn kick_out_if_recent<T: Config>(params: SlashParams<T>) {
|
||||
// these are not updated by era-span or end-span.
|
||||
let mut reward_payout = Zero::zero();
|
||||
let mut val_slashed = Zero::zero();
|
||||
@@ -343,15 +332,8 @@ fn slash_nominators<T: Config>(
|
||||
prior_slash_p: Perbill,
|
||||
nominators_slashed: &mut Vec<(T::AccountId, BalanceOf<T>)>,
|
||||
) -> BalanceOf<T> {
|
||||
let SlashParams {
|
||||
stash: _,
|
||||
slash,
|
||||
exposure,
|
||||
slash_era,
|
||||
window_start,
|
||||
now,
|
||||
reward_proportion,
|
||||
} = params;
|
||||
let SlashParams { stash: _, slash, exposure, slash_era, window_start, now, reward_proportion } =
|
||||
params;
|
||||
|
||||
let mut reward_payout = Zero::zero();
|
||||
|
||||
@@ -367,18 +349,12 @@ fn slash_nominators<T: Config>(
|
||||
let own_slash_by_validator = slash * nominator.value;
|
||||
let own_slash_difference = own_slash_by_validator.saturating_sub(own_slash_prior);
|
||||
|
||||
let mut era_slash = <Pallet<T> as Store>::NominatorSlashInEra::get(
|
||||
&slash_era,
|
||||
stash,
|
||||
).unwrap_or_else(|| Zero::zero());
|
||||
let mut era_slash = <Pallet<T> as Store>::NominatorSlashInEra::get(&slash_era, stash)
|
||||
.unwrap_or_else(|| Zero::zero());
|
||||
|
||||
era_slash += own_slash_difference;
|
||||
|
||||
<Pallet<T> as Store>::NominatorSlashInEra::insert(
|
||||
&slash_era,
|
||||
stash,
|
||||
&era_slash,
|
||||
);
|
||||
<Pallet<T> as Store>::NominatorSlashInEra::insert(&slash_era, stash, &era_slash);
|
||||
|
||||
era_slash
|
||||
};
|
||||
@@ -393,10 +369,7 @@ fn slash_nominators<T: Config>(
|
||||
reward_proportion,
|
||||
);
|
||||
|
||||
let target_span = spans.compare_and_update_span_slash(
|
||||
slash_era,
|
||||
era_slash,
|
||||
);
|
||||
let target_span = spans.compare_and_update_span_slash(slash_era, era_slash);
|
||||
|
||||
if target_span == Some(spans.span_index()) {
|
||||
// End the span, but don't chill the nominator. its nomination
|
||||
@@ -497,8 +470,8 @@ impl<'a, T: 'a + Config> InspectingSpans<'a, T> {
|
||||
span_record.slashed = slash;
|
||||
|
||||
// compute reward.
|
||||
let reward = REWARD_F1
|
||||
* (self.reward_proportion * slash).saturating_sub(span_record.paid_out);
|
||||
let reward =
|
||||
REWARD_F1 * (self.reward_proportion * slash).saturating_sub(span_record.paid_out);
|
||||
|
||||
self.add_slash(difference, slash_era);
|
||||
changed = true;
|
||||
@@ -529,7 +502,9 @@ impl<'a, T: 'a + Config> InspectingSpans<'a, T> {
|
||||
impl<'a, T: 'a + Config> Drop for InspectingSpans<'a, T> {
|
||||
fn drop(&mut self) {
|
||||
// only update on disk if we slashed this account.
|
||||
if !self.dirty { return }
|
||||
if !self.dirty {
|
||||
return
|
||||
}
|
||||
|
||||
if let Some((start, end)) = self.spans.prune(self.window_start) {
|
||||
for span_index in start..end {
|
||||
@@ -557,7 +532,10 @@ pub(crate) fn clear_stash_metadata<T: Config>(
|
||||
Some(s) => s,
|
||||
};
|
||||
|
||||
ensure!(num_slashing_spans as usize >= spans.iter().count(), Error::<T>::IncorrectSlashingSpans);
|
||||
ensure!(
|
||||
num_slashing_spans as usize >= spans.iter().count(),
|
||||
Error::<T>::IncorrectSlashingSpans
|
||||
);
|
||||
|
||||
<Pallet<T> as Store>::SlashingSpans::remove(stash);
|
||||
|
||||
@@ -606,9 +584,7 @@ pub fn do_slash<T: Config>(
|
||||
<Pallet<T>>::update_ledger(&controller, &ledger);
|
||||
|
||||
// trigger the event
|
||||
<Pallet<T>>::deposit_event(
|
||||
super::Event::<T>::Slash(stash.clone(), value)
|
||||
);
|
||||
<Pallet<T>>::deposit_event(super::Event::<T>::Slash(stash.clone(), value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -625,18 +601,12 @@ pub(crate) fn apply_slash<T: Config>(unapplied_slash: UnappliedSlash<T::AccountI
|
||||
);
|
||||
|
||||
for &(ref nominator, nominator_slash) in &unapplied_slash.others {
|
||||
do_slash::<T>(
|
||||
&nominator,
|
||||
nominator_slash,
|
||||
&mut reward_payout,
|
||||
&mut slashed_imbalance,
|
||||
);
|
||||
do_slash::<T>(&nominator, nominator_slash, &mut reward_payout, &mut slashed_imbalance);
|
||||
}
|
||||
|
||||
pay_reporters::<T>(reward_payout, slashed_imbalance, &unapplied_slash.reporters);
|
||||
}
|
||||
|
||||
|
||||
/// Apply a reward payout to some reporters, paying the rewards out of the slashed imbalance.
|
||||
fn pay_reporters<T: Config>(
|
||||
reward_payout: BalanceOf<T>,
|
||||
@@ -774,17 +744,13 @@ mod tests {
|
||||
assert_eq!(spans.prune(1000), Some((8, 10)));
|
||||
assert_eq!(
|
||||
spans.iter().collect::<Vec<_>>(),
|
||||
vec![
|
||||
SlashingSpan { index: 10, start: 1000, length: None },
|
||||
],
|
||||
vec![SlashingSpan { index: 10, start: 1000, length: None },],
|
||||
);
|
||||
|
||||
assert_eq!(spans.prune(2000), None);
|
||||
assert_eq!(
|
||||
spans.iter().collect::<Vec<_>>(),
|
||||
vec![
|
||||
SlashingSpan { index: 10, start: 2000, length: None },
|
||||
],
|
||||
vec![SlashingSpan { index: 10, start: 2000, length: None },],
|
||||
);
|
||||
|
||||
// now all in one shot.
|
||||
@@ -797,9 +763,7 @@ mod tests {
|
||||
assert_eq!(spans.prune(2000), Some((6, 10)));
|
||||
assert_eq!(
|
||||
spans.iter().collect::<Vec<_>>(),
|
||||
vec![
|
||||
SlashingSpan { index: 10, start: 2000, length: None },
|
||||
],
|
||||
vec![SlashingSpan { index: 10, start: 2000, length: None },],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user