Browse Source

runtime: Continue with working group migration.

Shamil Gadelshin 4 years ago
parent
commit
2f04c89ecc

+ 0 - 9
runtime-modules/common/src/working_group.rs

@@ -1,7 +1,4 @@
 use codec::{Decode, Encode};
-use frame_support::sp_runtime::traits::Member;
-use frame_support::dispatch::Codec;
-use frame_support::Parameter;
 #[cfg(feature = "std")]
 use serde::{Deserialize, Serialize};
 #[cfg(feature = "std")]
@@ -20,9 +17,3 @@ pub enum WorkingGroup {
     /// Storage working group: working_group::Instance3.
     Content,
 }
-
-/// OpeningId type.
-pub type OpeningId = u64;
-
-/// ApplicationId type
-pub type ApplicationId = u64;

+ 41 - 20
runtime-modules/proposals/codex/src/lib.rs

@@ -53,35 +53,37 @@
 // Disable this lint warning because Substrate generates function without an alias for the ProposalDetailsOf type.
 #![allow(clippy::too_many_arguments)]
 
-// Do not delete! Cannot be uncommented by default, because of Parity decl_module! issue.
-// #![warn(missing_docs)]
-
 mod proposal_types;
 
 #[cfg(test)]
 mod tests;
 
+use codec::Codec;
 use frame_support::dispatch::DispatchResult;
 use frame_support::traits::{Currency, Get};
-use frame_support::{decl_error, decl_module, decl_storage, ensure, print};
+use frame_support::{decl_error, decl_module, decl_storage, ensure, print, Parameter};
 use frame_system::ensure_root;
-use sp_arithmetic::traits::Zero;
+use sp_arithmetic::traits::{BaseArithmetic, Zero};
+use sp_runtime::traits::MaybeSerialize;
 use sp_std::clone::Clone;
 use sp_std::str::from_utf8;
 use sp_std::vec::Vec;
 
+pub use crate::proposal_types::{
+    AddOpeningParameters, FillOpeningParameters, TerminateRoleParameters,
+};
 use common::origin::ActorOriginValidator;
 use common::working_group::WorkingGroup;
+use frame_support::sp_runtime::traits::Member;
 use governance::election_params::ElectionParameters;
+pub use proposal_types::{
+    ApplicationId, OpeningId, ProposalDetails, ProposalDetailsOf, ProposalEncoder,
+};
 use proposals_discussion::ThreadMode;
 use proposals_engine::{
     BalanceOf, ProposalCreationParameters, ProposalObserver, ProposalParameters,
 };
-
-pub use crate::proposal_types::{
-    AddOpeningParameters, FillOpeningParameters, TerminateRoleParameters,
-};
-pub use proposal_types::{ProposalDetails, ProposalDetailsOf, ProposalEncoder};
+use working_group::Penalty;
 
 // 'Set working group mint capacity' proposal limit
 const WORKING_GROUP_MINT_CAPACITY_MAX_VALUE: u32 = 5_000_000;
@@ -92,7 +94,7 @@ const MAX_VALIDATOR_COUNT: u32 = 100;
 
 // Data container struct to fix linter warning 'too many arguments for the function' for the
 // create_proposal() function.
-struct CreateProposalParameters<T: Trait + working_group::Trait> {
+struct CreateProposalParameters<T: Trait> {
     pub origin: T::Origin,
     pub member_id: MemberId<T>,
     pub title: Vec<u8>,
@@ -113,7 +115,6 @@ pub trait Trait:
     + governance::election::Trait
     + hiring::Trait
     + staking::Trait
-    + working_group::Trait
 {
     /// Defines max allowed text proposal length.
     type TextProposalMaxLength: Get<u32>;
@@ -131,6 +132,26 @@ pub trait Trait:
     /// Encodes the proposal usint its details.
     type ProposalEncoder: ProposalEncoder<Self>;
 
+    /// Working group OpeningId type
+    type WorkingGroupOpeningId: Parameter
+        + Member
+        + BaseArithmetic
+        + Codec
+        + Default
+        + Copy
+        + MaybeSerialize
+        + PartialEq;
+
+    /// Working group ApplicationId type
+    type WorkingGroupApplicationId: Parameter
+        + Member
+        + BaseArithmetic
+        + Codec
+        + Default
+        + Copy
+        + MaybeSerialize
+        + PartialEq;
+
     /// 'Set validator count' proposal parameters.
     type SetValidatorCountProposalParameters: Get<
         ProposalParameters<Self::BlockNumber, BalanceOf<Self>>,
@@ -520,8 +541,8 @@ decl_module! {
             description: Vec<u8>,
             staking_account_id: Option<T::AccountId>,
             fill_opening_parameters: FillOpeningParameters<
-                working_group::OpeningId<T>,
-                working_group::ApplicationId<T>
+                OpeningId<T>,
+                ApplicationId<T>
             >,
             exact_execution_block: Option<T::BlockNumber>,
         ) {
@@ -559,7 +580,7 @@ decl_module! {
                 Error::<T>::InvalidWorkingGroupMintCapacity
             );
 
-            let proposal_details = ProposalDetails::SetWorkingGroupMintCapacity(mint_balance, working_group);
+            let proposal_details = ProposalDetails::SetWorkingGroupBudgetCapacity(mint_balance, working_group);
             let params = CreateProposalParameters{
                 origin,
                 member_id,
@@ -622,15 +643,15 @@ decl_module! {
             description: Vec<u8>,
             staking_account_id: Option<T::AccountId>,
             worker_id: working_group::WorkerId<T>,
-            slashing_stake: BalanceOf<T>,
+            penalty: Penalty<BalanceOf<T>>,
             working_group: WorkingGroup,
             exact_execution_block: Option<T::BlockNumber>,
         ) {
-            ensure!(slashing_stake != Zero::zero(), Error::<T>::SlashingStakeIsZero);
+            ensure!(penalty.slashing_amount != Zero::zero(), Error::<T>::SlashingStakeIsZero);
 
             let proposal_details = ProposalDetails::SlashWorkingGroupLeaderStake(
                 worker_id,
-                slashing_stake,
+                penalty,
                 working_group
             );
 
@@ -659,7 +680,7 @@ decl_module! {
             description: Vec<u8>,
             staking_account_id: Option<T::AccountId>,
             worker_id: working_group::WorkerId<T>,
-            reward_amount: BalanceOfMint<T>,
+            reward_amount: Option<BalanceOfMint<T>>,
             working_group: WorkingGroup,
             exact_execution_block: Option<T::BlockNumber>,
         ) {
@@ -693,7 +714,7 @@ decl_module! {
             title: Vec<u8>,
             description: Vec<u8>,
             staking_account_id: Option<T::AccountId>,
-            terminate_role_parameters: TerminateRoleParameters<working_group::WorkerId<T>>,
+            terminate_role_parameters: TerminateRoleParameters<working_group::WorkerId<T>, BalanceOf<T>>,
             exact_execution_block: Option<T::BlockNumber>,
         ) {
             let proposal_details = ProposalDetails::TerminateWorkingGroupLeaderRole(terminate_role_parameters);

+ 20 - 15
runtime-modules/proposals/codex/src/proposal_types/mod.rs

@@ -8,7 +8,7 @@ use sp_std::vec::Vec;
 use crate::ElectionParameters;
 use common::working_group::WorkingGroup;
 
-use working_group::{RewardPolicy, StakePolicy};
+use working_group::{Penalty, RewardPolicy, StakePolicy};
 
 /// Encodes proposal using its details information.
 pub trait ProposalEncoder<T: crate::Trait> {
@@ -16,14 +16,20 @@ pub trait ProposalEncoder<T: crate::Trait> {
     fn encode_proposal(proposal_details: ProposalDetailsOf<T>) -> Vec<u8>;
 }
 
+/// Type alias for an application id.
+pub type ApplicationId<T> = <T as crate::Trait>::WorkingGroupApplicationId;
+
+/// Type alias for an opening id.
+pub type OpeningId<T> = <T as crate::Trait>::WorkingGroupOpeningId;
+
 /// _ProposalDetails_ alias for type simplification
 pub type ProposalDetailsOf<T> = ProposalDetails<
     crate::BalanceOfMint<T>,
     crate::BalanceOfGovernanceCurrency<T>,
     <T as frame_system::Trait>::BlockNumber,
     <T as frame_system::Trait>::AccountId,
-    working_group::OpeningId<T>,
-    working_group::ApplicationId<T>,
+    OpeningId<T>,
+    ApplicationId<T>,
     crate::BalanceOf<T>,
     working_group::WorkerId<T>,
     crate::MemberId<T>,
@@ -83,26 +89,28 @@ pub enum ProposalDetails<
     /// Add opening for the working group leader position.
     AddWorkingGroupLeaderOpening(AddOpeningParameters<BlockNumber, CurrencyBalance>),
 
+    /// ********** Deprecated during the Olympia release.
+    /// It is kept only for backward compatibility in the Pioneer. **********
     /// Begin review applications for the working group leader position.
-    BeginReviewWorkingGroupLeaderApplications(OpeningId, WorkingGroup),
+    DeprecatedBeginReviewWorkingGroupLeaderApplications(OpeningId, WorkingGroup),
 
     /// Fill opening for the working group leader position.
     FillWorkingGroupLeaderOpening(FillOpeningParameters<OpeningId, ApplicationId>),
 
-    /// Set working group mint capacity.
-    SetWorkingGroupMintCapacity(MintedBalance, WorkingGroup),
+    /// Set working group budget capacity.
+    SetWorkingGroupBudgetCapacity(MintedBalance, WorkingGroup),
 
     /// Decrease the working group leader stake.
     DecreaseWorkingGroupLeaderStake(WorkerId, StakeBalance, WorkingGroup),
 
     /// Slash the working group leader stake.
-    SlashWorkingGroupLeaderStake(WorkerId, StakeBalance, WorkingGroup),
+    SlashWorkingGroupLeaderStake(WorkerId, Penalty<StakeBalance>, WorkingGroup),
 
     /// Set working group leader reward balance.
-    SetWorkingGroupLeaderReward(WorkerId, MintedBalance, WorkingGroup),
+    SetWorkingGroupLeaderReward(WorkerId, Option<MintedBalance>, WorkingGroup),
 
     /// Fire the working group leader with possible slashing.
-    TerminateWorkingGroupLeaderRole(TerminateRoleParameters<WorkerId>),
+    TerminateWorkingGroupLeaderRole(TerminateRoleParameters<WorkerId, StakeBalance>),
 
     /// Amend constitution.
     AmendConstitution(Vec<u8>),
@@ -139,15 +147,12 @@ impl<
 /// Parameters for the 'terminate the leader position' proposal.
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 #[derive(Encode, Decode, Clone, PartialEq, Debug)]
-pub struct TerminateRoleParameters<WorkerId> {
+pub struct TerminateRoleParameters<WorkerId, Balance> {
     /// Leader worker id to fire.
     pub worker_id: WorkerId,
 
-    /// Terminate role rationale.
-    pub rationale: Vec<u8>,
-
-    /// Slash the leader stake on terminating.
-    pub slash: bool,
+    /// Terminate role slash penalty.
+    pub penalty: Option<Penalty<Balance>>,
 
     /// Defines working group with the open position.
     pub working_group: WorkingGroup,

+ 2 - 2
runtime-modules/working-group/src/checks.rs

@@ -132,8 +132,8 @@ pub fn ensure_origin_is_active_leader<T: Trait<I>, I: Instance>(
     ensure_is_lead_account::<T, I>(signer)
 }
 
-// Check worker: ensures the worker was already created.
-pub(crate) fn ensure_worker_exists<T: Trait<I>, I: Instance>(
+/// Check worker: ensures the worker was already created.
+pub fn ensure_worker_exists<T: Trait<I>, I: Instance>(
     worker_id: &WorkerId<T>,
 ) -> Result<GroupWorker<T>, Error<T, I>> {
     ensure!(

+ 8 - 1
runtime-modules/working-group/src/lib.rs

@@ -54,7 +54,7 @@ pub use types::{
 };
 use types::{ApplicationInfo, GroupWorker, MemberId, WorkerInfo};
 
-pub use checks::{ensure_origin_is_active_leader, ensure_worker_signed};
+pub use checks::{ensure_origin_is_active_leader, ensure_worker_exists, ensure_worker_signed};
 
 use common::origin::ActorOriginValidator;
 use membership::staking_handler::StakingHandler;
@@ -1119,4 +1119,11 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
 
         false
     }
+
+    /// Returns all existing worker id list.
+    pub fn get_all_worker_ids() -> Vec<WorkerId<T>> {
+        <WorkerById<T, I>>::iter()
+            .map(|(worker_id, _)| worker_id)
+            .collect()
+    }
 }

+ 8 - 4
runtime/src/integration/content_directory.rs

@@ -13,8 +13,10 @@ impl content_directory::ActorAuthenticator for Runtime {
         // get current lead id
         let maybe_current_lead_id = ContentDirectoryWorkingGroup::<Runtime>::current_lead();
         if let Some(ref current_lead_id) = maybe_current_lead_id {
-            if let Ok(worker) =
-                ContentDirectoryWorkingGroup::<Runtime>::ensure_worker_exists(current_lead_id)
+            if let Ok(worker) = working_group::ensure_worker_exists::<
+                Runtime,
+                ContentDirectoryWorkingGroupInstance,
+            >(current_lead_id)
             {
                 *account_id == worker.role_account_id
             } else {
@@ -26,8 +28,10 @@ impl content_directory::ActorAuthenticator for Runtime {
     }
 
     fn is_curator(curator_id: &Self::CuratorId, account_id: &AccountId) -> bool {
-        if let Ok(worker) =
-            ContentDirectoryWorkingGroup::<Runtime>::ensure_worker_exists(curator_id)
+        if let Ok(worker) = working_group::ensure_worker_exists::<
+            Runtime,
+            ContentDirectoryWorkingGroupInstance,
+        >(curator_id)
         {
             *account_id == worker.role_account_id
         } else {

+ 30 - 38
runtime/src/integration/proposals/proposal_encoder.rs

@@ -3,7 +3,6 @@ use common::working_group::WorkingGroup;
 use proposals_codex::{ProposalDetails, ProposalDetailsOf, ProposalEncoder};
 use working_group::OpeningType;
 
-use codec::Encode;
 use frame_support::print;
 use sp_std::collections::btree_set::BTreeSet;
 use sp_std::marker::PhantomData;
@@ -77,24 +76,23 @@ impl ProposalEncoder<Runtime> for ExtrinsicProposalEncoder {
                     Wg::create_add_opening_call(add_opening_params)
                 )
             }
-            ProposalDetails::BeginReviewWorkingGroupLeaderApplications(
+            ProposalDetails::DeprecatedBeginReviewWorkingGroupLeaderApplications(
                 opening_id,
                 working_group,
-            ) => wrap_working_group_call!(
-                working_group,
-                Wg::create_begin_review_applications_call(opening_id)
-            ),
+            ) => {
+                print(
+                    "Error: Calling deprecated BeginReviewWorkingGroupLeaderApplications encoding option.",
+                );
+                return Vec::new();
+            }
             ProposalDetails::FillWorkingGroupLeaderOpening(fill_opening_params) => {
                 wrap_working_group_call!(
                     fill_opening_params.working_group,
                     Wg::create_fill_opening_call(fill_opening_params)
                 )
             }
-            ProposalDetails::SetWorkingGroupMintCapacity(mint_balance, working_group) => {
-                wrap_working_group_call!(
-                    working_group,
-                    Wg::create_set_mint_capacity_call(mint_balance)
-                )
+            ProposalDetails::SetWorkingGroupBudgetCapacity(budget, working_group) => {
+                wrap_working_group_call!(working_group, Wg::create_set_budget_capacity_call(budget))
             }
             ProposalDetails::DecreaseWorkingGroupLeaderStake(
                 worker_id,
@@ -143,7 +141,7 @@ struct Wg<T, I> {
 
 impl<T, I> Wg<T, I>
 where
-    T: working_group::Trait<I>,
+    T: working_group::Trait<I> + proposals_codex::Trait,
     I: frame_support::traits::Instance,
 {
     // Generic call constructor for the add working group opening.
@@ -154,25 +152,18 @@ where
         >,
     ) -> working_group::Call<T, I> {
         working_group::Call::<T, I>::add_opening(
-            add_opening_params.activate_at,
-            add_opening_params.commitment,
-            add_opening_params.human_readable_text,
+            add_opening_params.description,
             OpeningType::Leader,
+            add_opening_params.stake_policy,
+            add_opening_params.reward_policy,
         )
     }
 
-    // Generic call constructor for the begin review working group applications.
-    fn create_begin_review_applications_call(
-        opening_id: working_group::OpeningId<T>,
-    ) -> working_group::Call<T, I> {
-        working_group::Call::<T, I>::begin_applicant_review(opening_id)
-    }
-
     // Generic call constructor for the add working group opening.
     fn create_fill_opening_call(
         fill_opening_params: proposals_codex::FillOpeningParameters<
-            working_group::OpeningId<T>,
-            working_group::ApplicationId<T>,
+            proposals_codex::OpeningId<T>,
+            proposals_codex::ApplicationId<T>,
         >,
     ) -> working_group::Call<T, I> {
         let mut successful_application_ids = BTreeSet::new();
@@ -181,17 +172,9 @@ where
         working_group::Call::<T, I>::fill_opening(
             fill_opening_params.opening_id,
             successful_application_ids,
-            fill_opening_params.reward_policy,
         )
     }
 
-    // Generic call constructor for the working group 'set mit capacity'.
-    fn create_set_mint_capacity_call(
-        mint_balance: working_group::BalanceOf<T>,
-    ) -> working_group::Call<T, I> {
-        working_group::Call::<T, I>::set_mint_capacity(mint_balance)
-    }
-
     // Generic call constructor for the working group 'decrease stake'.
     fn create_decrease_stake_call(
         worker_id: working_group::WorkerId<T>,
@@ -203,27 +186,36 @@ where
     // Generic call constructor for the working group 'slash stake'.
     fn create_slash_stake_call(
         worker_id: working_group::WorkerId<T>,
-        slashing_stake: working_group::BalanceOf<T>,
+        penalty: working_group::Penalty<working_group::BalanceOf<T>>,
     ) -> working_group::Call<T, I> {
-        working_group::Call::<T, I>::slash_stake(worker_id, slashing_stake)
+        working_group::Call::<T, I>::slash_stake(worker_id, penalty)
     }
 
     // Generic call constructor for the working group 'update reward amount'.
     fn create_set_reward_call(
         worker_id: working_group::WorkerId<T>,
-        reward_amount: working_group::BalanceOf<T>,
+        reward_amount: Option<working_group::BalanceOf<T>>,
     ) -> working_group::Call<T, I> {
         working_group::Call::<T, I>::update_reward_amount(worker_id, reward_amount)
     }
 
     // Generic call constructor for the working group 'terminate role'.
     fn terminate_role_call(
-        terminate_role_params: proposals_codex::TerminateRoleParameters<working_group::WorkerId<T>>,
+        terminate_role_params: proposals_codex::TerminateRoleParameters<
+            working_group::WorkerId<T>,
+            working_group::BalanceOf<T>,
+        >,
     ) -> working_group::Call<T, I> {
         working_group::Call::<T, I>::terminate_role(
             terminate_role_params.worker_id,
-            terminate_role_params.rationale,
-            terminate_role_params.slash,
+            terminate_role_params.penalty,
         )
     }
+
+    // Generic call constructor for the working group 'set budget'.
+    fn create_set_budget_capacity_call(
+        budget: working_group::BalanceOf<T>,
+    ) -> working_group::Call<T, I> {
+        working_group::Call::<T, I>::set_budget(budget)
+    }
 }

+ 2 - 0
runtime/src/lib.rs

@@ -652,6 +652,8 @@ impl proposals_codex::Trait for Runtime {
     type RuntimeUpgradeWasmProposalMaxLength = RuntimeUpgradeWasmProposalMaxLength;
     type MembershipOriginValidator = MembershipOriginValidator<Self>;
     type ProposalEncoder = ExtrinsicProposalEncoder;
+    type WorkingGroupOpeningId = OpeningId;
+    type WorkingGroupApplicationId = ApplicationId;
     type SetValidatorCountProposalParameters = SetValidatorCountProposalParameters;
     type RuntimeUpgradeProposalParameters = RuntimeUpgradeProposalParameters;
     type TextProposalParameters = TextProposalParameters;