Update logic to cancel_deferred_slash (#5186)

* Update logic to cancel_deferred_slash

* More idiomatic

* bump spec

Co-authored-by: Gavin Wood <gavin@parity.io>
This commit is contained in:
Shawn Tabrizi
2020-03-09 13:39:05 +02:00
committed by GitHub
parent 013fb6ae18
commit 0971fff470
3 changed files with 65 additions and 20 deletions
+14 -13
View File
@@ -928,6 +928,8 @@ decl_error! {
InvalidEraToReward,
/// Invalid number of nominations.
InvalidNumberOfNominations,
/// Items are not sorted and unique.
NotSortedAndUnique,
}
}
@@ -1327,21 +1329,15 @@ decl_module! {
.map(|_| ())
.or_else(ensure_root)?;
let mut slash_indices = slash_indices;
slash_indices.sort_unstable();
ensure!(!slash_indices.is_empty(), Error::<T>::EmptyTargets);
ensure!(Self::is_sorted_and_unique(&slash_indices), Error::<T>::NotSortedAndUnique);
let mut unapplied = <Self as Store>::UnappliedSlashes::get(&era);
let last_item = slash_indices[slash_indices.len() - 1];
ensure!((last_item as usize) < unapplied.len(), Error::<T>::InvalidSlashIndex);
for (removed, index) in slash_indices.into_iter().enumerate() {
let index = index as usize;
// if `index` is not duplicate, `removed` must be <= index.
ensure!(removed <= index, Error::<T>::DuplicateIndex);
// all prior removals were from before this index, since the
// list is sorted.
let index = index - removed;
ensure!(index < unapplied.len(), Error::<T>::InvalidSlashIndex);
let index = (index as usize) - removed;
unapplied.remove(index);
}
@@ -1411,7 +1407,7 @@ decl_module! {
let controller = ensure_signed(origin)?;
let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
ensure!(
ledger.unlocking.len() > 0,
!ledger.unlocking.is_empty(),
Error::<T>::NoUnlockChunk,
);
@@ -1460,6 +1456,11 @@ impl<T: Trait> Module<T> {
Self::bonded(stash).and_then(Self::ledger).map(|l| l.active).unwrap_or_default()
}
/// Check that list is sorted and has no duplicates.
fn is_sorted_and_unique(list: &Vec<u32>) -> bool {
list.windows(2).all(|w| w[0] < w[1])
}
// MUTABLES (DANGEROUS)
fn do_payout_nominator(who: T::AccountId, era: EraIndex, validators: Vec<(T::AccountId, u32)>)