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 3 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
4 changes: 2 additions & 2 deletions pallets/collator-selection/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ benchmarks! {
assert_ok!(CollatorSelection::<T>::leave_intent(RawOrigin::Signed(leaving.clone()).into()));
let session_length = <T as session::Config>::NextSessionRotation::average_session_length();
session::Pallet::<T>::on_initialize(session_length);
assert_eq!(<NonCandidates<T>>::get(&leaving), (1u32, T::Currency::minimum_balance()));
assert_eq!(<NonCandidates<T>>::get(&leaving), Some((1u32, T::Currency::minimum_balance())));
}: _(RawOrigin::Signed(leaving.clone()))
verify {
assert_eq!(<NonCandidates<T>>::get(&leaving), (0u32, BalanceOf::<T>::default()));
assert_eq!(<NonCandidates<T>>::get(&leaving), None);
}

// worse case is paying a non-existing candidate account.
Expand Down
7 changes: 3 additions & 4 deletions pallets/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub mod pallet {
/// Candidates who initiated leave intent or kicked.
#[pallet::storage]
pub type NonCandidates<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, (SessionIndex, BalanceOf<T>), ValueQuery>;
StorageMap<_, Twox64Concat, T::AccountId, (SessionIndex, BalanceOf<T>), OptionQuery>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of this during my initial review. Better to use Option here, as a default value could have otherwise misled the system.


/// Last block authored by collator.
#[pallet::storage]
Expand Down Expand Up @@ -561,15 +561,14 @@ pub mod pallet {
if now.saturating_sub(last_authored) < kick_threshold {
continue;
}
// still candidate, kick and slash
// stale candidate, kick and slash
if Self::is_account_candidate(&who) {
if Candidates::<T>::get().len() > T::MinCandidates::get() as usize {
// no error, who is a candidate
let _ = Self::try_remove_candidate(&who);
Self::slash_non_candidate(&who);
}
} else {
let (locked_until, _) = NonCandidates::<T>::get(&who);
} else if let Some((locked_until, _)) = NonCandidates::<T>::get(&who) {
if T::ValidatorSet::session_index() > locked_until {
// bond is already unlocked
<LastAuthoredBlock<T>>::remove(who);
Expand Down
12 changes: 6 additions & 6 deletions pallets/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ fn leave_intent() {
assert_eq!(Balances::reserved_balance(3), 10);
assert_eq!(LastAuthoredBlock::<Test>::get(3), 10);
// 10 unbonding from session 1
assert_eq!(NonCandidates::<Test>::get(3), (1, 10));
assert_eq!(NonCandidates::<Test>::get(3), Some((1, 10)));
});
}

Expand Down Expand Up @@ -313,7 +313,7 @@ fn withdraw_unbond() {

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

Expand Down Expand Up @@ -553,26 +553,26 @@ fn should_not_slash_unbonding_candidates() {

assert_ok!(CollatorSelection::leave_intent(RuntimeOrigin::signed(3)));
// can withdraw on next session
assert_eq!(NonCandidates::<Test>::get(3), (1, 10));
assert_eq!(NonCandidates::<Test>::get(3), Some((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!(NonCandidates::<Test>::get(3), Some((1, 10)));
assert_eq!(Balances::free_balance(3), 90);

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

assert_eq!(NonCandidates::<Test>::get(3), (1, 10));
assert_eq!(NonCandidates::<Test>::get(3), Some((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!(NonCandidates::<Test>::get(3), None);
assert_eq!(Balances::free_balance(3), 100);
});
}
Expand Down
Loading