Browse Source

runtime: working-group: get rid of penalty

conectado 4 years ago
parent
commit
3f1104ec5a

+ 1 - 4
runtime-modules/proposals/codex/src/lib.rs

@@ -422,10 +422,7 @@ impl<T: Trait> Module<T> {
                 );
             }
             ProposalDetails::SlashWorkingGroupLeaderStake(_, ref penalty, _) => {
-                ensure!(
-                    penalty.slashing_amount != Zero::zero(),
-                    Error::<T>::SlashingStakeIsZero
-                );
+                ensure!(*penalty != Zero::zero(), Error::<T>::SlashingStakeIsZero);
             }
             ProposalDetails::SetWorkingGroupLeaderReward(..) => {
                 // Note: No checks for this proposal for now

+ 5 - 25
runtime-modules/proposals/codex/src/tests/mod.rs

@@ -9,7 +9,6 @@ use frame_system::RawOrigin;
 use common::working_group::WorkingGroup;
 use proposals_engine::ProposalParameters;
 use referendum::ReferendumManager;
-use working_group::Penalty;
 
 use crate::*;
 use crate::{Error, ProposalDetails};
@@ -741,23 +740,11 @@ fn run_create_slash_working_group_leader_stake_proposal_common_checks_succeed(
             exact_execution_block: None,
         };
 
-        let slash_lead_details = ProposalDetails::SlashWorkingGroupLeaderStake(
-            0,
-            Penalty {
-                slashing_amount: 10,
-                slashing_text: Vec::new(),
-            },
-            working_group,
-        );
+        let slash_lead_details =
+            ProposalDetails::SlashWorkingGroupLeaderStake(0, 10, working_group);
 
-        let slash_lead_details_success = ProposalDetails::SlashWorkingGroupLeaderStake(
-            10,
-            Penalty {
-                slashing_amount: 10,
-                slashing_text: Vec::new(),
-            },
-            working_group,
-        );
+        let slash_lead_details_success =
+            ProposalDetails::SlashWorkingGroupLeaderStake(10, 10, working_group);
 
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
@@ -897,14 +884,7 @@ fn run_slash_stake_with_zero_staking_balance_fails(working_group: WorkingGroup)
             ProposalCodex::create_proposal(
                 RawOrigin::Signed(1).into(),
                 general_proposal_parameters.clone(),
-                ProposalDetails::SlashWorkingGroupLeaderStake(
-                    10,
-                    Penalty {
-                        slashing_amount: 0,
-                        slashing_text: Vec::new()
-                    },
-                    working_group,
-                )
+                ProposalDetails::SlashWorkingGroupLeaderStake(10, 0, working_group)
             ),
             Err(Error::<Test>::SlashingStakeIsZero.into())
         );

+ 2 - 2
runtime-modules/proposals/codex/src/types.rs

@@ -7,7 +7,7 @@ use sp_std::vec::Vec;
 
 use common::working_group::WorkingGroup;
 
-use working_group::{Penalty, StakePolicy};
+use working_group::StakePolicy;
 
 /// Encodes proposal using its details information.
 pub trait ProposalEncoder<T: crate::Trait> {
@@ -52,7 +52,7 @@ pub enum ProposalDetails<BlockNumber, AccountId, Balance, WorkerId> {
     DecreaseWorkingGroupLeaderStake(WorkerId, Balance, WorkingGroup),
 
     /// Slash the working group leader stake.
-    SlashWorkingGroupLeaderStake(WorkerId, Penalty<Balance>, WorkingGroup),
+    SlashWorkingGroupLeaderStake(WorkerId, Balance, WorkingGroup),
 
     /// Set working group leader reward balance.
     SetWorkingGroupLeaderReward(WorkerId, Option<Balance>, WorkingGroup),

+ 6 - 5
runtime-modules/working-group/src/benchmarking.rs

@@ -699,11 +699,12 @@ benchmarks_instance! {
             Some(lead_id.clone())
         );
         let slashing_amount = One::one();
-        let penalty = Penalty {
-            slashing_text: vec![0u8; i.try_into().unwrap()],
-            slashing_amount,
-        };
-    }: _(RawOrigin::Signed(lead_id.clone()), worker_id, penalty)
+    }: _(
+        RawOrigin::Signed(lead_id.clone()),
+        worker_id,
+        slashing_amount,
+        Some(vec![0u8; i.try_into().unwrap()])
+    )
     verify {
         assert_last_event::<T, I>(RawEvent::StakeSlashed(worker_id, slashing_amount).into());
     }

+ 20 - 7
runtime-modules/working-group/src/lib.rs

@@ -50,7 +50,7 @@ use sp_std::vec::Vec;
 pub use errors::Error;
 pub use types::{
     Application, ApplicationId, ApplyOnOpeningParameters, BalanceOf, Opening, OpeningId,
-    OpeningType, Penalty, StakeParameters, StakePolicy, Worker, WorkerId,
+    OpeningType, StakeParameters, StakePolicy, Worker, WorkerId,
 };
 use types::{ApplicationInfo, WorkerInfo};
 
@@ -580,8 +580,6 @@ decl_module! {
         /// - DB:
         ///    - O(1) doesn't depend on the state or parameters
         /// # </weight>
-        // Note: We use separate penalty and rationale instead of Penalty struct to be able to have
-        // one set and not the other
         #[weight = Module::<T, I>::terminate_role_weight(&_rationale)]
         pub fn terminate_role(
             origin,
@@ -634,8 +632,13 @@ decl_module! {
         /// - DB:
         ///    - O(1) doesn't depend on the state or parameters
         /// # </weight>
-        #[weight = WeightInfoWorkingGroup::<T, I>::slash_stake(penalty.slashing_text.len().saturated_into())]
-        pub fn slash_stake(origin, worker_id: WorkerId<T>, penalty: Penalty<BalanceOf<T>>) {
+        #[weight = Module::<T, I>::slash_stake_weight(&_rationale)]
+        pub fn slash_stake(
+            origin,
+            worker_id: WorkerId<T>,
+            penalty: BalanceOf<T>,
+            _rationale: Option<Vec<u8>>
+        ) {
             // Ensure lead is set or it is the council slashing the leader.
             checks::ensure_origin_for_worker_operation::<T,I>(origin, worker_id)?;
 
@@ -649,7 +652,7 @@ decl_module! {
             );
 
             ensure!(
-                penalty.slashing_amount != <BalanceOf<T>>::zero(),
+                penalty != <BalanceOf<T>>::zero(),
                 Error::<T, I>::StakeBalanceCannotBeZero
             );
 
@@ -658,7 +661,7 @@ decl_module! {
             //
 
             if let Some(staking_account_id) = worker.staking_account_id {
-                Self::slash(worker_id, &staking_account_id, Some(penalty.slashing_amount))
+                Self::slash(worker_id, &staking_account_id, Some(penalty))
             }
         }
 
@@ -1064,6 +1067,16 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
         ))
     }
 
+    // Calculates slash_stake weight
+    fn slash_stake_weight(rationale: &Option<Vec<u8>>) -> Weight {
+        WeightInfoWorkingGroup::<T, I>::slash_stake(
+            rationale
+                .as_ref()
+                .map(|text| text.len().saturated_into())
+                .unwrap_or_default(),
+        )
+    }
+
     // Wrapper-function over frame_system::block_number()
     fn current_block() -> T::BlockNumber {
         <frame_system::Module<T>>::block_number()

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

@@ -9,8 +9,8 @@ use super::hiring_workflow::HiringWorkflow;
 use super::mock::{Balances, LockId, System, Test, TestEvent, TestWorkingGroup};
 use crate::types::StakeParameters;
 use crate::{
-    Application, ApplyOnOpeningParameters, DefaultInstance, Opening, OpeningType, Penalty,
-    RawEvent, StakePolicy, Worker,
+    Application, ApplyOnOpeningParameters, DefaultInstance, Opening, OpeningType, RawEvent,
+    StakePolicy, Worker,
 };
 
 pub struct EventFixture;
@@ -590,7 +590,8 @@ pub struct SlashWorkerStakeFixture {
     origin: RawOrigin<u64>,
     worker_id: u64,
     account_id: u64,
-    penalty: Penalty<u64>,
+    penalty: u64,
+    rationale: Option<Vec<u8>>,
 }
 
 impl SlashWorkerStakeFixture {
@@ -602,10 +603,8 @@ impl SlashWorkerStakeFixture {
         Self {
             origin: RawOrigin::Signed(lead_account_id),
             worker_id,
-            penalty: Penalty {
-                slashing_text: Vec::new(),
-                slashing_amount: 10,
-            },
+            rationale: None,
+            penalty: 10,
             account_id,
         }
     }
@@ -613,7 +612,7 @@ impl SlashWorkerStakeFixture {
         Self { origin, ..self }
     }
 
-    pub fn with_penalty(self, penalty: Penalty<u64>) -> Self {
+    pub fn with_penalty(self, penalty: u64) -> Self {
         Self { penalty, ..self }
     }
 
@@ -623,7 +622,8 @@ impl SlashWorkerStakeFixture {
         let actual_result = TestWorkingGroup::slash_stake(
             self.origin.clone().into(),
             self.worker_id,
-            self.penalty.clone(),
+            self.penalty,
+            self.rationale.clone(),
         );
 
         assert_eq!(actual_result, expected_result);
@@ -632,7 +632,7 @@ impl SlashWorkerStakeFixture {
             // stake decreased
             assert_eq!(
                 old_stake,
-                get_stake_balance(&self.account_id) + self.penalty.slashing_amount
+                get_stake_balance(&self.account_id) + self.penalty
             );
 
             let new_balance = Balances::usable_balance(&self.account_id);

+ 8 - 28
runtime-modules/working-group/src/tests/mod.rs

@@ -17,7 +17,7 @@ use crate::tests::mock::{
     STAKING_ACCOUNT_ID_FOR_ZERO_STAKE,
 };
 use crate::types::StakeParameters;
-use crate::{DefaultInstance, Error, OpeningType, Penalty, RawEvent, StakePolicy, Worker};
+use crate::{DefaultInstance, Error, OpeningType, RawEvent, StakePolicy, Worker};
 use common::working_group::WorkingGroupAuthenticator;
 use fixtures::{
     increase_total_balance_issuance_using_account_id, AddOpeningFixture, ApplyOnOpeningFixture,
@@ -1187,18 +1187,13 @@ fn slash_worker_stake_succeeds() {
         let account_id = 1;
         let total_balance = 300;
         let stake = 200;
-        let slash_stake = 100;
+        let penalty = 100;
 
         let stake_policy = Some(StakePolicy {
             stake_amount: stake,
             leaving_unstaking_period: 10,
         });
 
-        let penalty = Penalty {
-            slashing_amount: slash_stake,
-            slashing_text: Vec::new(),
-        };
-
         increase_total_balance_issuance_using_account_id(account_id, total_balance);
 
         let worker_id = HireRegularWorkerFixture::default()
@@ -1210,7 +1205,7 @@ fn slash_worker_stake_succeeds() {
 
         slash_stake_fixture.call_and_assert(Ok(()));
 
-        EventFixture::assert_last_crate_event(RawEvent::StakeSlashed(worker_id, slash_stake));
+        EventFixture::assert_last_crate_event(RawEvent::StakeSlashed(worker_id, penalty));
     });
 }
 
@@ -1219,12 +1214,7 @@ fn slash_worker_stake_fails_with_no_staking_account() {
     build_test_externalities().execute_with(|| {
         let account_id = 1;
         let total_balance = 300;
-        let slash_stake = 100;
-
-        let penalty = Penalty {
-            slashing_amount: slash_stake,
-            slashing_text: Vec::new(),
-        };
+        let penalty = 100;
 
         increase_total_balance_issuance_using_account_id(account_id, total_balance);
 
@@ -1254,18 +1244,13 @@ fn slash_leader_stake_succeeds() {
 
         let account_id = 1;
         let total_balance = 300;
-        let stake = 200;
+        let penalty = 200;
 
         let stake_policy = Some(StakePolicy {
-            stake_amount: stake,
+            stake_amount: penalty,
             leaving_unstaking_period: 10,
         });
 
-        let penalty = Penalty {
-            slashing_amount: stake,
-            slashing_text: Vec::new(),
-        };
-
         increase_total_balance_issuance_using_account_id(account_id, total_balance);
         let leader_worker_id = HireLeadFixture::default()
             .with_stake_policy(stake_policy)
@@ -1277,7 +1262,7 @@ fn slash_leader_stake_succeeds() {
 
         slash_stake_fixture.call_and_assert(Ok(()));
 
-        EventFixture::assert_last_crate_event(RawEvent::StakeSlashed(leader_worker_id, stake));
+        EventFixture::assert_last_crate_event(RawEvent::StakeSlashed(leader_worker_id, penalty));
     });
 }
 
@@ -1318,11 +1303,6 @@ fn slash_worker_stake_fails_with_zero_balance() {
             leaving_unstaking_period: 10,
         });
 
-        let penalty = Penalty {
-            slashing_amount: 0,
-            slashing_text: Vec::new(),
-        };
-
         increase_total_balance_issuance_using_account_id(account_id, total_balance);
 
         let worker_id = HireRegularWorkerFixture::default()
@@ -1330,7 +1310,7 @@ fn slash_worker_stake_fails_with_zero_balance() {
             .hire();
 
         let slash_stake_fixture =
-            SlashWorkerStakeFixture::default_for_worker_id(worker_id).with_penalty(penalty);
+            SlashWorkerStakeFixture::default_for_worker_id(worker_id).with_penalty(0);
 
         slash_stake_fixture.call_and_assert(Err(
             Error::<Test, DefaultInstance>::StakeBalanceCannotBeZero.into(),

+ 0 - 11
runtime-modules/working-group/src/types.rs

@@ -249,14 +249,3 @@ pub type ApplyOnOpeningParameters<T> = ApplyOnOpeningParams<
     <T as frame_system::Trait>::AccountId,
     BalanceOf<T>,
 >;
-
-/// Contains information for the slashing.
-#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
-#[derive(Encode, Decode, Debug, Clone, Default, PartialEq, Eq)]
-pub struct Penalty<Balance> {
-    /// Slashing rationale
-    pub slashing_text: Vec<u8>,
-
-    /// Slashing balance
-    pub slashing_amount: Balance,
-}

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

@@ -151,9 +151,9 @@ where
     // Generic call constructor for the working group 'slash stake'.
     fn create_slash_stake_call(
         worker_id: working_group::WorkerId<T>,
-        penalty: working_group::Penalty<working_group::BalanceOf<T>>,
+        penalty: working_group::BalanceOf<T>,
     ) -> working_group::Call<T, I> {
-        working_group::Call::<T, I>::slash_stake(worker_id, penalty)
+        working_group::Call::<T, I>::slash_stake(worker_id, penalty, None)
     }
 
     // Generic call constructor for the working group 'update reward amount'.

+ 2 - 5
runtime/src/tests/proposals_integration/working_group_proposals.rs

@@ -7,7 +7,7 @@ use common::working_group::WorkingGroup;
 use frame_system::RawOrigin;
 use proposals_codex::AddOpeningParameters;
 use strum::IntoEnumIterator;
-use working_group::{Penalty, StakeParameters};
+use working_group::StakeParameters;
 
 use crate::primitives::{ActorId, MemberId};
 use crate::tests::run_to_block;
@@ -206,10 +206,7 @@ fn slash_stake(
             general_proposal_parameters,
             ProposalDetails::SlashWorkingGroupLeaderStake(
                 leader_worker_id,
-                Penalty {
-                    slashing_amount: stake_amount,
-                    slashing_text: Vec::new(),
-                },
+                stake_amount,
                 working_group,
             ),
         )