Browse Source

Add slashing during the terminate_role() in the working group module.

Shamil Gadelshin 4 years ago
parent
commit
4e783d93e4

+ 1 - 1
runtime-modules/common/src/lib.rs

@@ -3,7 +3,7 @@
 
 pub mod constraints;
 pub mod currency;
-pub mod origin_validator;
+pub mod origin;
 
 use codec::{Decode, Encode};
 #[cfg(feature = "std")]

+ 26 - 0
runtime-modules/common/src/origin.rs

@@ -0,0 +1,26 @@
+use system::RawOrigin;
+
+/// Abstract validator for the origin(account_id) and actor_id (eg.: thread author id).
+pub trait ActorOriginValidator<Origin, ActorId, AccountId> {
+    /// Check for valid combination of origin and actor_id.
+    fn ensure_actor_origin(origin: Origin, actor_id: ActorId) -> Result<AccountId, &'static str>;
+}
+
+// Multiplies the T::Origin.
+// In our current substrate version system::Origin doesn't support clone(),
+// but it will be supported in latest up-to-date substrate version.
+// TODO: delete when T::Origin will support the clone()
+pub fn double_origin<T: system::Trait>(origin: T::Origin) -> (T::Origin, T::Origin) {
+    let coerced_origin = origin.into().ok().unwrap_or(RawOrigin::None);
+
+    let (cloned_origin1, cloned_origin2) = match coerced_origin {
+        RawOrigin::None => (RawOrigin::None, RawOrigin::None),
+        RawOrigin::Root => (RawOrigin::Root, RawOrigin::Root),
+        RawOrigin::Signed(account_id) => (
+            RawOrigin::Signed(account_id.clone()),
+            RawOrigin::Signed(account_id),
+        ),
+    };
+
+    (cloned_origin1.into(), cloned_origin2.into())
+}

+ 0 - 5
runtime-modules/common/src/origin_validator.rs

@@ -1,5 +0,0 @@
-/// Abstract validator for the origin(account_id) and actor_id (eg.: thread author id).
-pub trait ActorOriginValidator<Origin, ActorId, AccountId> {
-    /// Check for valid combination of origin and actor_id.
-    fn ensure_actor_origin(origin: Origin, actor_id: ActorId) -> Result<AccountId, &'static str>;
-}

+ 3 - 22
runtime-modules/proposals/codex/src/lib.rs

@@ -54,7 +54,7 @@ mod proposal_types;
 #[cfg(test)]
 mod tests;
 
-use common::origin_validator::ActorOriginValidator;
+use common::origin::ActorOriginValidator;
 use governance::election_params::ElectionParameters;
 use proposal_engine::ProposalParameters;
 use rstd::clone::Clone;
@@ -65,7 +65,7 @@ use sr_primitives::traits::Zero;
 use srml_support::dispatch::DispatchResult;
 use srml_support::traits::{Currency, Get};
 use srml_support::{decl_error, decl_module, decl_storage, ensure, print};
-use system::{ensure_root, RawOrigin};
+use system::ensure_root;
 
 pub use crate::proposal_types::ProposalsConfigParameters;
 pub use proposal_types::{ProposalDetails, ProposalDetailsOf, ProposalEncoder};
@@ -577,7 +577,7 @@ decl_module! {
             origin,
             wasm: Vec<u8>,
         ) {
-            let (cloned_origin1, cloned_origin2) =  Self::double_origin(origin);
+            let (cloned_origin1, cloned_origin2) = common::origin::double_origin::<T>(origin);
             ensure_root(cloned_origin1)?;
 
             print("Runtime upgrade proposal execution started.");
@@ -590,25 +590,6 @@ decl_module! {
 }
 
 impl<T: Trait> Module<T> {
-    // Multiplies the T::Origin.
-    // In our current substrate version system::Origin doesn't support clone(),
-    // but it will be supported in latest up-to-date substrate version.
-    // TODO: delete when T::Origin will support the clone()
-    fn double_origin(origin: T::Origin) -> (T::Origin, T::Origin) {
-        let coerced_origin = origin.into().ok().unwrap_or(RawOrigin::None);
-
-        let (cloned_origin1, cloned_origin2) = match coerced_origin {
-            RawOrigin::None => (RawOrigin::None, RawOrigin::None),
-            RawOrigin::Root => (RawOrigin::Root, RawOrigin::Root),
-            RawOrigin::Signed(account_id) => (
-                RawOrigin::Signed(account_id.clone()),
-                RawOrigin::Signed(account_id),
-            ),
-        };
-
-        (cloned_origin1.into(), cloned_origin2.into())
-    }
-
     // Generic template proposal builder
     fn create_proposal(
         origin: T::Origin,

+ 1 - 1
runtime-modules/proposals/codex/src/tests/mock.rs

@@ -123,7 +123,7 @@ impl governance::council::Trait for Test {
     type CouncilTermEnded = ();
 }
 
-impl common::origin_validator::ActorOriginValidator<Origin, u64, u64> for () {
+impl common::origin::ActorOriginValidator<Origin, u64, u64> for () {
     fn ensure_actor_origin(origin: Origin, _: u64) -> Result<u64, &'static str> {
         let account_id = system::ensure_signed(origin)?;
 

+ 1 - 1
runtime-modules/proposals/discussion/src/lib.rs

@@ -56,7 +56,7 @@ use srml_support::{decl_error, decl_event, decl_module, decl_storage, ensure, Pa
 use srml_support::traits::Get;
 use types::{DiscussionPost, DiscussionThread, ThreadCounter};
 
-use common::origin_validator::ActorOriginValidator;
+use common::origin::ActorOriginValidator;
 use srml_support::dispatch::DispatchResult;
 
 type MemberId<T> = <T as membership::members::Trait>::MemberId;

+ 1 - 1
runtime-modules/proposals/engine/src/lib.rs

@@ -135,7 +135,7 @@ use srml_support::{
 use system::{ensure_root, RawOrigin};
 
 use crate::types::ApprovedProposalData;
-use common::origin_validator::ActorOriginValidator;
+use common::origin::ActorOriginValidator;
 use srml_support::dispatch::Dispatchable;
 
 type MemberId<T> = <T as membership::members::Trait>::MemberId;

+ 1 - 1
runtime-modules/proposals/engine/src/tests/mock/mod.rs

@@ -124,7 +124,7 @@ impl Default for proposals::Call<Test> {
     }
 }
 
-impl common::origin_validator::ActorOriginValidator<Origin, u64, u64> for () {
+impl common::origin::ActorOriginValidator<Origin, u64, u64> for () {
     fn ensure_actor_origin(origin: Origin, _account_id: u64) -> Result<u64, &'static str> {
         let signed_account_id = system::ensure_signed(origin)?;
 

+ 1 - 1
runtime-modules/storage/src/data_directory.rs

@@ -27,7 +27,7 @@ use sr_primitives::traits::{MaybeSerialize, Member};
 use srml_support::{decl_error, decl_event, decl_module, decl_storage, ensure, Parameter};
 use system::{self, ensure_root};
 
-use common::origin_validator::ActorOriginValidator;
+use common::origin::ActorOriginValidator;
 pub(crate) use common::BlockAndTime;
 
 use crate::data_object_type_registry;

+ 1 - 1
runtime-modules/storage/src/tests/mock.rs

@@ -169,7 +169,7 @@ impl crate::data_directory::StorageProviderHelper<Test> for () {
     }
 }
 
-impl common::origin_validator::ActorOriginValidator<Origin, u64, u64> for () {
+impl common::origin::ActorOriginValidator<Origin, u64, u64> for () {
     fn ensure_actor_origin(origin: Origin, _account_id: u64) -> Result<u64, &'static str> {
         let signed_account_id = system::ensure_signed(origin)?;
 

+ 17 - 11
runtime-modules/working-group/src/lib.rs

@@ -42,11 +42,8 @@
 //#![warn(missing_docs)]
 
 // TODO: terminate role - add 'include slashing' parameter.
-// TODO: terminate role - unset lead on success when the leader is being fired.
 // TODO: leave role - unset lead on success when the leader is leaving.
-// TODO: stakes - disallow zero stake.
 // TODO: check all that a leader can set only its own parameters as a worker.
-// TODO: get_all_worker_ids() - remove leader worker_id
 
 #[cfg(test)]
 mod tests;
@@ -58,7 +55,7 @@ use rstd::collections::btree_map::BTreeMap;
 use rstd::collections::btree_set::BTreeSet;
 use rstd::prelude::*;
 use rstd::vec::Vec;
-use sr_primitives::traits::{One, Zero};
+use sr_primitives::traits::{Bounded, One, Zero};
 use srml_support::traits::{Currency, ExistenceRequirement, WithdrawReasons};
 use srml_support::{decl_event, decl_module, decl_storage, ensure};
 use system::{ensure_root, ensure_signed};
@@ -466,13 +463,16 @@ decl_module! {
         pub fn terminate_role(
             origin,
             worker_id: WorkerId<T>,
-            rationale_text: Vec<u8>
+            rationale_text: Vec<u8>,
+            slash_stake: bool,
         ) {
-            // Ensuring worker actually exists.
-            let worker = Self::ensure_worker_exists(&worker_id)?;
+            let (cloned_origin1, cloned_origin2) = common::origin::double_origin::<T>(origin);
 
             // Ensure lead is set or it is the council terminating the leader.
-            let exit_origin = Self::ensure_origin_for_leader(origin, worker.member_id)?;
+            let exit_origin = Self::ensure_origin_for_leader(cloned_origin1, worker_id)?;
+
+            // Ensuring worker actually exists.
+            let worker = Self::ensure_worker_exists(&worker_id)?;
 
             // Ensure rationale text is valid.
             Self::ensure_worker_exit_rationale_text_is_valid(&rationale_text)?;
@@ -481,6 +481,10 @@ decl_module! {
             // == MUTATION SAFE ==
             //
 
+            if slash_stake {
+                Self::slash_stake(cloned_origin2, worker_id, BalanceOf::<T>::max_value())?;
+            }
+
             Self::deactivate_worker(
                 &worker_id,
                 &worker,
@@ -912,8 +916,10 @@ decl_module! {
         /// Slashes the worker stake, demands a leader origin. No limits, no actions on zero stake.
         /// If slashing balance greater than the existing stake - stake is slashed to zero.
         pub fn slash_stake(origin, worker_id: WorkerId<T>, balance: BalanceOf<T>) {
-            Self::ensure_origin_is_active_leader(origin)?;
+            // Ensure lead is set or it is the council terminating the leader.
+            Self::ensure_origin_for_leader(origin, worker_id)?;
 
+            // Ensuring worker actually exists.
             let worker = Self::ensure_worker_exists(&worker_id)?;
 
             ensure!(balance != <BalanceOf<T>>::zero(), Error::StakeBalanceCannotBeZero);
@@ -1042,11 +1048,11 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
 
     fn ensure_origin_for_leader(
         origin: T::Origin,
-        member_id: MemberId<T>,
+        worker_id: WorkerId<T>,
     ) -> Result<ExitInitiationOrigin, Error> {
         let lead = Self::ensure_lead_is_set()?;
 
-        let (worker_opening_type, exit_origin) = if lead.member_id == member_id {
+        let (worker_opening_type, exit_origin) = if lead.worker_id == worker_id {
             (OpeningType::Leader, ExitInitiationOrigin::Sudo)
         } else {
             (OpeningType::Worker, ExitInitiationOrigin::Lead)

+ 55 - 41
runtime-modules/working-group/src/tests/fixtures.rs

@@ -23,7 +23,7 @@ pub struct IncreaseWorkerStakeFixture {
 impl IncreaseWorkerStakeFixture {
     pub fn default_for_worker_id(worker_id: u64) -> Self {
         let account_id = 1;
-        IncreaseWorkerStakeFixture {
+        Self {
             origin: RawOrigin::Signed(1),
             worker_id,
             balance: 10,
@@ -31,11 +31,11 @@ impl IncreaseWorkerStakeFixture {
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        IncreaseWorkerStakeFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn with_balance(self, balance: u64) -> Self {
-        IncreaseWorkerStakeFixture { balance, ..self }
+        Self { balance, ..self }
     }
 
     pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
@@ -72,11 +72,12 @@ pub struct TerminateWorkerRoleFixture {
     origin: RawOrigin<u64>,
     text: Vec<u8>,
     constraint: InputValidationLengthConstraint,
+    slash_stake: bool,
 }
 
 impl TerminateWorkerRoleFixture {
     pub fn default_for_worker_id(worker_id: u64) -> Self {
-        TerminateWorkerRoleFixture {
+        Self {
             worker_id,
             origin: RawOrigin::Signed(1),
             text: b"rationale_text".to_vec(),
@@ -84,14 +85,22 @@ impl TerminateWorkerRoleFixture {
                 min: 1,
                 max_min_diff: 20,
             },
+            slash_stake: false,
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        TerminateWorkerRoleFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn with_text(self, text: Vec<u8>) -> Self {
-        TerminateWorkerRoleFixture { text, ..self }
+        Self { text, ..self }
+    }
+
+    pub fn with_slashing(self) -> Self {
+        Self {
+            slash_stake: true,
+            ..self
+        }
     }
 
     pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
@@ -101,6 +110,7 @@ impl TerminateWorkerRoleFixture {
             self.origin.clone().into(),
             self.worker_id,
             self.text.clone(),
+            self.slash_stake,
         );
         assert_eq!(actual_result, expected_result);
 
@@ -121,13 +131,13 @@ pub(crate) struct LeaveWorkerRoleFixture {
 
 impl LeaveWorkerRoleFixture {
     pub fn default_for_worker_id(worker_id: u64) -> Self {
-        LeaveWorkerRoleFixture {
+        Self {
             worker_id,
             origin: RawOrigin::Signed(1),
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        LeaveWorkerRoleFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
@@ -153,14 +163,14 @@ pub struct UpdateWorkerRewardAmountFixture {
 
 impl UpdateWorkerRewardAmountFixture {
     pub fn default_for_worker_id(worker_id: u64) -> Self {
-        UpdateWorkerRewardAmountFixture {
+        Self {
             worker_id,
             amount: 100,
             origin: RawOrigin::Signed(1),
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        UpdateWorkerRewardAmountFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
@@ -182,14 +192,14 @@ pub struct UpdateWorkerRewardAccountFixture {
 
 impl UpdateWorkerRewardAccountFixture {
     pub fn default_with_ids(worker_id: u64, new_reward_account_id: u64) -> Self {
-        UpdateWorkerRewardAccountFixture {
+        Self {
             worker_id,
             new_reward_account_id,
             origin: RawOrigin::Signed(1),
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        UpdateWorkerRewardAccountFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
@@ -212,14 +222,14 @@ pub struct UpdateWorkerRoleAccountFixture {
 
 impl UpdateWorkerRoleAccountFixture {
     pub fn default_with_ids(worker_id: u64, new_role_account_id: u64) -> Self {
-        UpdateWorkerRoleAccountFixture {
+        Self {
             worker_id,
             new_role_account_id,
             origin: RawOrigin::Signed(1),
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        UpdateWorkerRoleAccountFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
@@ -258,7 +268,7 @@ impl FillWorkerOpeningFixture {
     pub fn default_for_ids(opening_id: u64, application_ids: Vec<u64>) -> Self {
         let application_ids: BTreeSet<u64> = application_ids.iter().map(|x| *x).collect();
 
-        FillWorkerOpeningFixture {
+        Self {
             origin: RawOrigin::Signed(1),
             opening_id,
             successful_application_ids: application_ids,
@@ -268,11 +278,11 @@ impl FillWorkerOpeningFixture {
     }
 
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        FillWorkerOpeningFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn with_reward_policy(self, reward_policy: RewardPolicy<u64, u64>) -> Self {
-        FillWorkerOpeningFixture {
+        Self {
             reward_policy: Some(reward_policy),
             ..self
         }
@@ -345,13 +355,13 @@ pub struct BeginReviewWorkerApplicationsFixture {
 
 impl BeginReviewWorkerApplicationsFixture {
     pub fn default_for_opening_id(opening_id: u64) -> Self {
-        BeginReviewWorkerApplicationsFixture {
+        Self {
             origin: RawOrigin::Signed(1),
             opening_id,
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        BeginReviewWorkerApplicationsFixture { origin, ..self }
+        Self { origin, ..self }
     }
     pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
         let actual_result =
@@ -367,16 +377,16 @@ pub struct TerminateApplicationFixture {
 
 impl TerminateApplicationFixture {
     pub fn with_signer(self, account_id: u64) -> Self {
-        TerminateApplicationFixture {
+        Self {
             origin: RawOrigin::Signed(account_id),
             ..self
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        TerminateApplicationFixture { origin, ..self }
+        Self { origin, ..self }
     }
     pub fn default_for_application_id(application_id: u64) -> Self {
-        TerminateApplicationFixture {
+        Self {
             origin: RawOrigin::Signed(1),
             worker_application_id: application_id,
         }
@@ -396,16 +406,16 @@ pub struct WithdrawApplicationFixture {
 
 impl WithdrawApplicationFixture {
     pub fn with_signer(self, account_id: u64) -> Self {
-        WithdrawApplicationFixture {
+        Self {
             origin: RawOrigin::Signed(account_id),
             ..self
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        WithdrawApplicationFixture { origin, ..self }
+        Self { origin, ..self }
     }
     pub fn default_for_application_id(application_id: u64) -> Self {
-        WithdrawApplicationFixture {
+        Self {
             origin: RawOrigin::Signed(1),
             worker_application_id: application_id,
         }
@@ -424,6 +434,10 @@ pub fn increase_total_balance_issuance_using_account_id(account_id: u64, balance
         <Balances as srml_support::traits::Currency<u64>>::deposit_creating(&account_id, balance);
 }
 
+pub fn get_balance(account_id: u64) -> u64 {
+    <super::mock::Balances as srml_support::traits::Currency<u64>>::total_balance(&account_id)
+}
+
 pub fn setup_members(count: u8) {
     let authority_account_id = 1;
     Membership::set_screening_authority(RawOrigin::Root.into(), authority_account_id).unwrap();
@@ -549,7 +563,7 @@ pub struct AcceptWorkerApplicationsFixture {
 
 impl AcceptWorkerApplicationsFixture {
     pub fn default_for_opening_id(opening_id: u64) -> Self {
-        AcceptWorkerApplicationsFixture {
+        Self {
             origin: RawOrigin::Signed(1),
             opening_id,
         }
@@ -586,7 +600,7 @@ impl SetLeadFixture {
         TestWorkingGroup::set_lead(self.member_id, self.role_account, self.worker_id);
     }
     pub fn set_lead_with_ids(member_id: u64, role_account: u64, worker_id: u64) {
-        SetLeadFixture {
+        Self {
             member_id,
             role_account,
             worker_id,
@@ -601,7 +615,7 @@ pub struct HireLeadFixture {
 
 impl Default for HireLeadFixture {
     fn default() -> Self {
-        HireLeadFixture {
+        Self {
             setup_environment: true,
         }
     }
@@ -632,7 +646,7 @@ pub struct AddWorkerOpeningFixture {
 
 impl Default for AddWorkerOpeningFixture {
     fn default() -> Self {
-        AddWorkerOpeningFixture {
+        Self {
             origin: RawOrigin::Signed(1),
             activate_at: hiring::ActivateOpeningAt::CurrentBlock,
             commitment: <OpeningPolicyCommitment<u64, u64>>::default(),
@@ -647,7 +661,7 @@ impl AddWorkerOpeningFixture {
         self,
         policy_commitment: OpeningPolicyCommitment<u64, u64>,
     ) -> Self {
-        AddWorkerOpeningFixture {
+        Self {
             commitment: policy_commitment,
             ..self
         }
@@ -695,25 +709,25 @@ impl AddWorkerOpeningFixture {
     }
 
     pub fn with_text(self, text: Vec<u8>) -> Self {
-        AddWorkerOpeningFixture {
+        Self {
             human_readable_text: text,
             ..self
         }
     }
 
     pub fn with_opening_type(self, opening_type: OpeningType) -> Self {
-        AddWorkerOpeningFixture {
+        Self {
             opening_type,
             ..self
         }
     }
 
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        AddWorkerOpeningFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn with_activate_at(self, activate_at: hiring::ActivateOpeningAt<u64>) -> Self {
-        AddWorkerOpeningFixture {
+        Self {
             activate_at,
             ..self
         }
@@ -763,7 +777,7 @@ pub struct DecreaseWorkerStakeFixture {
 impl DecreaseWorkerStakeFixture {
     pub fn default_for_worker_id(worker_id: u64) -> Self {
         let account_id = 1;
-        DecreaseWorkerStakeFixture {
+        Self {
             origin: RawOrigin::Signed(account_id),
             worker_id,
             balance: 10,
@@ -771,11 +785,11 @@ impl DecreaseWorkerStakeFixture {
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        DecreaseWorkerStakeFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn with_balance(self, balance: u64) -> Self {
-        DecreaseWorkerStakeFixture { balance, ..self }
+        Self { balance, ..self }
     }
 
     pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
@@ -807,7 +821,7 @@ impl DecreaseWorkerStakeFixture {
     }
 }
 
-fn get_stake_balance(stake: stake::Stake<u64, u64, u64>) -> u64 {
+pub(crate) fn get_stake_balance(stake: stake::Stake<u64, u64, u64>) -> u64 {
     if let stake::StakingStatus::Staked(stake) = stake.staking_status {
         return stake.staked_amount;
     }
@@ -825,7 +839,7 @@ pub struct SlashWorkerStakeFixture {
 impl SlashWorkerStakeFixture {
     pub fn default_for_worker_id(worker_id: u64) -> Self {
         let account_id = 1;
-        SlashWorkerStakeFixture {
+        Self {
             origin: RawOrigin::Signed(account_id),
             worker_id,
             balance: 10,
@@ -833,11 +847,11 @@ impl SlashWorkerStakeFixture {
         }
     }
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
-        SlashWorkerStakeFixture { origin, ..self }
+        Self { origin, ..self }
     }
 
     pub fn with_balance(self, balance: u64) -> Self {
-        SlashWorkerStakeFixture { balance, ..self }
+        Self { balance, ..self }
     }
 
     pub fn call_and_assert(&self, expected_result: Result<(), Error>) {

+ 1 - 1
runtime-modules/working-group/src/tests/hiring_workflow.rs

@@ -158,7 +158,7 @@ impl HiringWorkflow {
 
         let opening_id = add_worker_opening_fixture.call()?;
 
-        // fill applications
+        // Fill applications.
         let mut application_ids = Vec::new();
         for application in self.applications.clone() {
             let apply_on_worker_opening_fixture =

+ 105 - 4
runtime-modules/working-group/src/tests/mod.rs

@@ -1161,15 +1161,18 @@ fn leave_worker_role_succeeds_with_stakes() {
 #[test]
 fn terminate_worker_role_succeeds_with_stakes() {
     build_test_externalities().execute_with(|| {
+        let total_balance = 10000;
+        let stake_balance = 100;
+
         let worker_account_id = 2;
         let worker_member_id = 2;
-        increase_total_balance_issuance_using_account_id(worker_account_id, 10000);
+        increase_total_balance_issuance_using_account_id(worker_account_id, total_balance);
 
         HireLeadFixture::default().hire_lead();
 
         let worker_id = HiringWorkflow::default()
             .disable_setup_environment()
-            .with_role_stake(Some(100))
+            .with_role_stake(Some(stake_balance))
             .add_application_with_origin(
                 b"worker_handle".to_vec(),
                 RawOrigin::Signed(worker_account_id),
@@ -1178,6 +1181,12 @@ fn terminate_worker_role_succeeds_with_stakes() {
             .execute()
             .unwrap();
 
+        // Balance was staked.
+        assert_eq!(
+            get_balance(worker_account_id),
+            total_balance - stake_balance
+        );
+
         let terminate_worker_role_fixture =
             TerminateWorkerRoleFixture::default_for_worker_id(worker_id);
 
@@ -1190,6 +1199,65 @@ fn terminate_worker_role_succeeds_with_stakes() {
     });
 }
 
+#[test]
+fn terminate_worker_role_succeeds_with_slashing() {
+    build_test_externalities().execute_with(|| {
+        let total_balance = 10000;
+        let stake_balance = 100;
+
+        let worker_account_id = 2;
+        let worker_member_id = 2;
+        increase_total_balance_issuance_using_account_id(worker_account_id, total_balance);
+
+        assert_eq!(get_balance(worker_account_id), total_balance);
+
+        HireLeadFixture::default().hire_lead();
+
+        let worker_id = HiringWorkflow::default()
+            .disable_setup_environment()
+            .with_role_stake(Some(stake_balance))
+            .add_application_with_origin(
+                b"worker_handle".to_vec(),
+                RawOrigin::Signed(worker_account_id),
+                worker_member_id,
+            )
+            .execute()
+            .unwrap();
+
+        // Balance was staked.
+
+        assert_eq!(
+            get_balance(worker_account_id),
+            total_balance - stake_balance
+        );
+
+        let stake_id = 0;
+        let old_stake = <stake::Module<Test>>::stakes(stake_id);
+
+        assert_eq!(get_stake_balance(old_stake), stake_balance);
+
+        // Terminate with slashing.
+
+        let terminate_worker_role_fixture =
+            TerminateWorkerRoleFixture::default_for_worker_id(worker_id).with_slashing();
+
+        terminate_worker_role_fixture.call_and_assert(Ok(()));
+
+        // Balance was slashed.
+
+        assert_eq!(
+            get_balance(worker_account_id),
+            total_balance - stake_balance
+        );
+
+        let new_stake = <stake::Module<Test>>::stakes(stake_id);
+        assert!(matches!(
+            new_stake.staking_status,
+            stake::StakingStatus::NotStaked
+        ));
+    });
+}
+
 #[test]
 fn terminate_worker_role_succeeds() {
     build_test_externalities().execute_with(|| {
@@ -1472,14 +1540,47 @@ fn slash_worker_stake_succeeds() {
     });
 }
 
+#[test]
+fn slash_leader_stake_succeeds() {
+    build_test_externalities().execute_with(|| {
+        let leader_worker_id = HiringWorkflow::default()
+            .with_role_stake(Some(100))
+            .with_opening_type(OpeningType::Leader)
+            .add_default_application()
+            .execute()
+            .unwrap();
+
+        let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(leader_worker_id)
+            .with_origin(RawOrigin::Root);
+
+        slash_stake_fixture.call_and_assert(Ok(()));
+
+        EventFixture::assert_last_crate_event(RawEvent::StakeSlashed(leader_worker_id));
+    });
+}
+
 #[test]
 fn slash_worker_stake_fails_with_invalid_origin() {
     build_test_externalities().execute_with(|| {
-        let worker_id = 0;
+        HireLeadFixture::default().hire_lead();
+
+        let invalid_worker_id = 22;
+        let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(invalid_worker_id)
+            .with_origin(RawOrigin::None);
+
+        slash_stake_fixture.call_and_assert(Err(Error::Other("RequireSignedOrigin")));
+    });
+}
+
+#[test]
+fn slash_leader_stake_fails_with_invalid_origin() {
+    build_test_externalities().execute_with(|| {
+        let worker_id = HireLeadFixture::default().hire_lead();
+
         let slash_stake_fixture =
             SlashWorkerStakeFixture::default_for_worker_id(worker_id).with_origin(RawOrigin::None);
 
-        slash_stake_fixture.call_and_assert(Err(Error::Other("RequireSignedOrigin")));
+        slash_stake_fixture.call_and_assert(Err(Error::RequireRootOrigin));
     });
 }
 

+ 2 - 2
runtime/src/integration/proposals/council_origin_validator.rs

@@ -2,7 +2,7 @@
 
 use rstd::marker::PhantomData;
 
-use common::origin_validator::ActorOriginValidator;
+use common::origin::ActorOriginValidator;
 use proposals_engine::VotersParameters;
 
 use super::{MemberId, MembershipOriginValidator};
@@ -44,7 +44,7 @@ impl<T: governance::council::Trait> VotersParameters for CouncilManager<T> {
 mod tests {
     use super::CouncilManager;
     use crate::Runtime;
-    use common::origin_validator::ActorOriginValidator;
+    use common::origin::ActorOriginValidator;
     use membership::members::UserInfo;
     use proposals_engine::VotersParameters;
     use sr_primitives::AccountId32;

+ 2 - 2
runtime/src/integration/proposals/membership_origin_validator.rs

@@ -2,7 +2,7 @@
 
 use rstd::marker::PhantomData;
 
-use common::origin_validator::ActorOriginValidator;
+use common::origin::ActorOriginValidator;
 use system::ensure_signed;
 
 /// Member of the Joystream organization
@@ -46,7 +46,7 @@ impl<T: crate::members::Trait>
 mod tests {
     use super::MembershipOriginValidator;
     use crate::Runtime;
-    use common::origin_validator::ActorOriginValidator;
+    use common::origin::ActorOriginValidator;
     use membership::members::UserInfo;
     use sr_primitives::AccountId32;
     use system::RawOrigin;