Skip to content

Commit

Permalink
fix non-candidate slashing
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Aug 21, 2024
1 parent 40c4b6c commit 471f5e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pallets/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ pub mod pallet {
Self::slash_non_candidate(&who);
}
} else {
let (locked_until, _) = NonCandidates::<T>::get(&who);
if T::ValidatorSet::session_index() > locked_until {
// bond is already unlocked
continue;
}
// slash un-bonding candidate
Self::slash_non_candidate(&who);
}
Expand Down
41 changes: 40 additions & 1 deletion pallets/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ fn kick_and_slash_mechanism() {
}

#[test]
fn slash_mechanism_for_unbonding_candidates() {
fn slash_mechanism_for_unbonding_candidates_who_missed_block() {
new_test_ext().execute_with(|| {
// Define slash destination account
<crate::SlashDestination<Test>>::put(5);
Expand Down Expand Up @@ -538,6 +538,45 @@ fn slash_mechanism_for_unbonding_candidates() {
});
}

#[test]
fn should_not_slash_unbonding_candidates() {
new_test_ext().execute_with(|| {
// add a new collator
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(3)
));
assert_ok!(CollatorSelection::register_as_candidate(
RuntimeOrigin::signed(4)
));
assert_eq!(LastAuthoredBlock::<Test>::get(3), 10);
assert_eq!(LastAuthoredBlock::<Test>::get(4), 10);

assert_ok!(CollatorSelection::leave_intent(RuntimeOrigin::signed(3)));
// can withdraw on next session
assert_eq!(NonCandidates::<Test>::get(3), (1, 10));

initialize_to_block(10);
// not included next session and doesn't withdraw bond
assert_eq!(NextSessionCollators::get(), vec![1, 2, 4]);
assert_eq!(LastAuthoredBlock::<Test>::get(3), 10);
assert_eq!(LastAuthoredBlock::<Test>::get(4), 10);
assert_eq!(NonCandidates::<Test>::get(3), (1, 10));
assert_eq!(Balances::free_balance(3), 90);

initialize_to_block(20);
assert_eq!(SessionChangeBlock::get(), 20);
assert_eq!(LastAuthoredBlock::<Test>::get(3), 10);
assert_eq!(LastAuthoredBlock::<Test>::get(4), 20);

assert_eq!(NonCandidates::<Test>::get(3), (1, 10));
assert_eq!(Balances::free_balance(3), 90);

assert_ok!(CollatorSelection::withdraw_bond(RuntimeOrigin::signed(3)));
assert_eq!(NonCandidates::<Test>::get(3), (0, 0));
assert_eq!(Balances::free_balance(3), 100);
});
}

#[test]
fn should_not_kick_mechanism_too_few() {
new_test_ext().execute_with(|| {
Expand Down

0 comments on commit 471f5e1

Please sign in to comment.