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

Place demo_offence dispatchable behind root origin check #426

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions pallets/slashing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ pub mod pallet {
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {
/// An example dispatchable that may throw a custom error.
/// A helper to trigger an offence.
///
// # Note
//
// This is only used for demo purposes and should be removed before launch.
#[pallet::call_index(0)]
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
pub fn demo_offence(origin: OriginFor<T>, offenders: Vec<T::AccountId>) -> DispatchResult {
// TODO remove this function, it is for demo purposes only
let who = ensure_signed(origin)?;
Self::do_offence(who, offenders)?;
pub fn demo_offence(origin: OriginFor<T>, reporter: T::AccountId, offenders: Vec<T::AccountId>) -> DispatchResult {
let _ = ensure_root(origin)?;
Self::do_offence(reporter, offenders)?;
Ok(())
}
}
Expand Down
19 changes: 16 additions & 3 deletions pallets/slashing/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use frame_support::assert_ok;
use frame_support::{assert_ok, assert_err};
use sp_runtime::Perbill;
use sp_staking::offence::Offence;

Expand All @@ -18,13 +18,26 @@ fn offence_test() {
new_test_ext().execute_with(|| {
assert_ok!(Staking::force_new_era_always(RuntimeOrigin::root()));
assert!(Session::validators().contains(&1));

// slash would cause min validators to drop below min validators no offence
assert_ok!(Slashing::demo_offence(RuntimeOrigin::signed(1), vec![1u64, 2u64]));
assert_ok!(Slashing::demo_offence(RuntimeOrigin::root(), 1, vec![1u64, 2u64]));
let mut offences = OFFENCES.with(|l| l.replace(vec![]));
assert_eq!(offences.len(), 0);

// causes offence
assert_ok!(Slashing::demo_offence(RuntimeOrigin::signed(1), vec![1u64]));
assert_ok!(Slashing::demo_offence(RuntimeOrigin::root(), 1, vec![1u64]));
offences = OFFENCES.with(|l| l.replace(vec![]));
assert_eq!(offences.len(), 1);
});
}

#[test]
fn signed_origin_cannot_initiate_demo_offence() {
new_test_ext().execute_with(|| {
assert_err!(
Slashing::demo_offence(RuntimeOrigin::signed(1), 1, vec![1u64, 2u64]),
sp_runtime::DispatchError::BadOrigin
);
})
}