Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix non-candidate slashing #1334

Merged
merged 6 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion pallets/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ pub mod pallet {

/// Destination account for slashed amount.
#[pallet::storage]
pub type SlashDestination<T> = StorageValue<_, <T as frame_system::Config>::AccountId>;
pub type SlashDestination<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;

#[pallet::genesis_config]
#[derive(DefaultNoBound)]
Expand Down Expand Up @@ -480,6 +480,22 @@ pub mod pallet {

Ok(())
}

/// Set slash destination.
/// Use `Some` to deposit slashed balance into destination or `None` to burn it.
#[pallet::call_index(6)]
#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
pub fn set_slash_destination(
origin: OriginFor<T>,
destination: Option<T::AccountId>,
) -> DispatchResult {
T::UpdateOrigin::ensure_origin(origin)?;
match destination {
Some(account) => <SlashDestination<T>>::put(account),
None => <SlashDestination<T>>::kill(),
}
Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -553,6 +569,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
70 changes: 68 additions & 2 deletions pallets/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate as collator_selection;
use crate::{
mock::*, CandidacyBond, CandidateInfo, Candidates, DesiredCandidates, Error, Invulnerables,
LastAuthoredBlock, NonCandidates,
LastAuthoredBlock, NonCandidates, SlashDestination,
};
use frame_support::{
assert_noop, assert_ok,
Expand Down 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() {
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
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 Expand Up @@ -589,3 +628,30 @@ fn cannot_set_genesis_value_twice() {
// collator selection must be initialized before session.
collator_selection.assimilate_storage(&mut t).unwrap();
}

#[test]
fn set_slash_destination() {
new_test_ext().execute_with(|| {
assert_eq!(SlashDestination::<Test>::get(), None);

// only UpdateOrigin can update
assert_noop!(
CollatorSelection::set_slash_destination(RuntimeOrigin::signed(1), Some(1)),
sp_runtime::DispatchError::BadOrigin
);

// set destination
assert_ok!(CollatorSelection::set_slash_destination(
RuntimeOrigin::signed(RootAccount::get()),
Some(1),
));
assert_eq!(SlashDestination::<Test>::get(), Some(1));

// remove destination
assert_ok!(CollatorSelection::set_slash_destination(
RuntimeOrigin::signed(RootAccount::get()),
None,
));
assert_eq!(SlashDestination::<Test>::get(), None);
});
}
Loading