diff --git a/pallets/slashing/src/lib.rs b/pallets/slashing/src/lib.rs index bfb03a4d7..5f8956f48 100644 --- a/pallets/slashing/src/lib.rs +++ b/pallets/slashing/src/lib.rs @@ -85,13 +85,20 @@ pub mod pallet { // Dispatchable functions must be annotated with a weight and must return a DispatchResult. #[pallet::call] impl Pallet { - /// 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, offenders: Vec) -> 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, + reporter: T::AccountId, + offenders: Vec, + ) -> DispatchResult { + ensure_root(origin)?; + Self::do_offence(reporter, offenders)?; Ok(()) } } diff --git a/pallets/slashing/src/tests.rs b/pallets/slashing/src/tests.rs index c24302c1c..e24401b3f 100644 --- a/pallets/slashing/src/tests.rs +++ b/pallets/slashing/src/tests.rs @@ -1,4 +1,4 @@ -use frame_support::assert_ok; +use frame_support::{assert_err, assert_ok}; use sp_runtime::Perbill; use sp_staking::offence::Offence; @@ -18,13 +18,25 @@ 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 + ); + }) +}