Skip to content

Commit dcd1a6a

Browse files
Adam ReifAdam Reif
authored andcommitted
kick_stale_signatures() returns Vec<> instead of Option<Vec<>>
1 parent 84844cb commit dcd1a6a

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

pallets/collator-selection/src/lib.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -560,25 +560,25 @@ pub mod pallet {
560560
/// Returns the removed AccountIds
561561
pub fn kick_stale_candidates(
562562
candidates: Vec<CandidateInfo<T::AccountId, BalanceOf<T>>>,
563-
) -> Option<Vec<T::AccountId>> {
563+
) -> Vec<T::AccountId> {
564564
use sp_runtime::PerThing;
565565

566566
// 0. Storage reads and precondition checks
567567
if candidates.is_empty() {
568-
return None; // No candidates means we're running invulnerables only
568+
return Vec::new(); // No candidates means we're running invulnerables only
569569
}
570570
let percentile_for_kick = Self::eviction_percentile();
571571
if percentile_for_kick == Percent::zero() {
572-
return None; // Selecting 0-th percentile disables kicking. Upper bound check in fn build()
572+
return Vec::new(); // Selecting 0-th percentile disables kicking. Upper bound check in fn build()
573573
}
574574
let underperformance_threshold_percent = Self::eviction_threshold();
575575
if underperformance_threshold_percent == Percent::one() {
576-
return None; // tolerating 100% underperformance disables kicking
576+
return Vec::new(); // tolerating 100% underperformance disables kicking
577577
}
578578
let mut collator_perf_this_session =
579579
<BlocksPerCollatorThisSession<T>>::iter().collect::<Vec<_>>();
580580
if collator_perf_this_session.is_empty() {
581-
return None; // no validator performance recorded ( should not happen )
581+
return Vec::new(); // no validator performance recorded ( should not happen )
582582
}
583583

584584
// 1. Ascending sort of collator performance list by number of produced blocks
@@ -625,11 +625,7 @@ pub mod pallet {
625625
}
626626
}
627627
});
628-
if removed_account_ids.is_empty() {
629-
None
630-
} else {
631-
Some(removed_account_ids)
632-
}
628+
removed_account_ids
633629
}
634630

635631
/// Reset the performance map to the currently active validators at 0 blocks
@@ -687,19 +683,16 @@ pub mod pallet {
687683
let candidates = Self::candidates();
688684
let candidates_len_before = candidates.len();
689685
let removed_candidate_ids = Self::kick_stale_candidates(candidates.clone());
690-
let active_candidate_ids: Vec<_> = match removed_candidate_ids {
691-
None => candidates.iter().map(|x| x.who.clone()).collect(),
692-
Some(removed_candidate_ids) => candidates
693-
.iter()
694-
.filter_map(|x| {
695-
if removed_candidate_ids.contains(&x.who) {
696-
None
697-
} else {
698-
Some(x.who.clone())
699-
}
700-
})
701-
.collect(),
702-
};
686+
let active_candidate_ids = candidates
687+
.iter()
688+
.filter_map(|x| {
689+
if removed_candidate_ids.contains(&x.who) {
690+
None
691+
} else {
692+
Some(x.who.clone())
693+
}
694+
})
695+
.collect::<Vec<_>>();
703696
let result = Self::assemble_collators(active_candidate_ids);
704697

705698
frame_system::Pallet::<T>::register_extra_weight_unchecked(

pallets/collator-selection/src/tests.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ fn kick_algorithm_manta() {
428428
BlocksPerCollatorThisSession::<Test>::insert(4u64, 9);
429429
BlocksPerCollatorThisSession::<Test>::insert(5u64, 0);
430430
assert_eq!(
431-
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()).unwrap(),
431+
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()),
432432
vec![5, 3]
433433
);
434434

@@ -443,11 +443,12 @@ fn kick_algorithm_manta() {
443443
BlocksPerCollatorThisSession::<Test>::insert(4u64, 9);
444444
BlocksPerCollatorThisSession::<Test>::insert(5u64, 0);
445445
assert_eq!(
446-
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()).unwrap(),
446+
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()),
447447
vec![5, 3]
448448
);
449449

450450
// Test boundary conditions
451+
let empty_vec = Vec::<<Test as frame_system::Config>::AccountId>::new();
451452
// Kick anyone not at perfect performance
452453
EvictionPercentile::<Test>::put(Percent::from_percent(100));
453454
EvictionThreshold::<Test>::put(Percent::from_percent(0));
@@ -456,21 +457,21 @@ fn kick_algorithm_manta() {
456457
BlocksPerCollatorThisSession::<Test>::insert(4u64, 10);
457458
assert_eq!(
458459
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()),
459-
Some(vec![3])
460+
vec![3]
460461
);
461462
assert_ok!(CollatorSelection::register_as_candidate(Origin::signed(3)));
462463
// Allow any underperformance => eviction disabled
463464
EvictionThreshold::<Test>::put(Percent::from_percent(100));
464465
assert_eq!(
465466
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()),
466-
None
467+
empty_vec
467468
);
468469
// 0-th percentile = use worst collator as benchmark => eviction disabled
469470
EvictionPercentile::<Test>::put(Percent::from_percent(0));
470471
EvictionThreshold::<Test>::put(Percent::from_percent(0));
471472
assert_eq!(
472473
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()),
473-
None
474+
empty_vec
474475
);
475476
// Same performance => no kick
476477
EvictionPercentile::<Test>::put(Percent::from_percent(100));
@@ -479,7 +480,7 @@ fn kick_algorithm_manta() {
479480
BlocksPerCollatorThisSession::<Test>::insert(4u64, 10);
480481
assert_eq!(
481482
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()),
482-
None
483+
empty_vec
483484
);
484485
// Exactly on threshold => no kick
485486
EvictionPercentile::<Test>::put(Percent::from_percent(100));
@@ -488,7 +489,7 @@ fn kick_algorithm_manta() {
488489
BlocksPerCollatorThisSession::<Test>::insert(4u64, 9);
489490
assert_eq!(
490491
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()),
491-
None
492+
empty_vec
492493
);
493494
// Rational threshold = 8.1, kick 8 and below
494495
EvictionPercentile::<Test>::put(Percent::from_percent(100));
@@ -497,7 +498,7 @@ fn kick_algorithm_manta() {
497498
BlocksPerCollatorThisSession::<Test>::insert(4u64, 10);
498499
assert_eq!(
499500
CollatorSelection::kick_stale_candidates(CollatorSelection::candidates()),
500-
Some(vec![3])
501+
vec![3]
501502
);
502503
});
503504
}

0 commit comments

Comments
 (0)