Skip to content

Commit

Permalink
add call to update slash destination
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Aug 22, 2024
1 parent 471f5e1 commit 9384ddd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
18 changes: 17 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
29 changes: 28 additions & 1 deletion 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 @@ -628,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);
});
}

0 comments on commit 9384ddd

Please sign in to comment.