Browse Source

Add codex ProposalDetails for the frontend

Shamil Gadelshin 5 years ago
parent
commit
34b0e7cc9a

+ 6 - 6
runtime-modules/proposals/codex/Cargo.toml

@@ -83,6 +83,12 @@ default-features = false
 git = 'https://github.com/paritytech/substrate.git'
 rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
 
+[dependencies.runtime-io]
+default_features = false
+git = 'https://github.com/paritytech/substrate.git'
+package = 'sr-io'
+rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
+
 [dependencies.stake]
 default_features = false
 package = 'substrate-stake-module'
@@ -123,12 +129,6 @@ default_features = false
 package = 'substrate-content-working-group-module'
 path = '../../content-working-group'
 
-[dev-dependencies.runtime-io]
-default_features = false
-git = 'https://github.com/paritytech/substrate.git'
-package = 'sr-io'
-rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
-
 [dev-dependencies.hiring]
 default_features = false
 package = 'substrate-hiring-module'

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

@@ -9,7 +9,8 @@
 //! During the proposal creation `codex` also create a discussion thread using the `discussion`
 //! proposals module. `Codex` uses predefined parameters (eg.:`voting_period`) for each proposal and
 //! encodes extrinsic calls from dependency modules in order to create proposals inside the `engine`
-//! module.
+//! module. For each proposal, [its crucial details](./enum.ProposalDetails.html) are saved to the
+//! `ProposalDetailsByProposalId` map.
 //!
 //! ### Supported extrinsics (proposal types)
 //! - [create_text_proposal](./struct.Module.html#method.create_text_proposal)
@@ -50,12 +51,15 @@ use rstd::clone::Clone;
 use rstd::prelude::*;
 use rstd::str::from_utf8;
 use rstd::vec::Vec;
+use runtime_io::blake2_256;
 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};
 
+pub use proposal_types::ProposalDetails;
+
 /// 'Proposals codex' substrate module Trait
 pub trait Trait:
     system::Trait
@@ -158,6 +162,16 @@ decl_storage! {
         /// Map proposal id to its discussion thread id
         pub ThreadIdByProposalId get(fn thread_id_by_proposal_id):
             map T::ProposalId => T::ThreadId;
+
+        /// Map proposal id to proposal details
+        pub ProposalDetailsByProposalId get(fn proposal_details_by_proposal_id):
+            map T::ProposalId => ProposalDetails<
+                BalanceOfMint<T>,
+                BalanceOfGovernanceCurrency<T>,
+                T::BlockNumber,
+                T::AccountId,
+                T::MemberId
+            >;
     }
 }
 
@@ -182,7 +196,7 @@ decl_module! {
 
             let proposal_parameters = proposal_types::parameters::text_proposal::<T>();
             let proposal_code =
-                <Call<T>>::execute_text_proposal(title.clone(), description.clone(), text);
+                <Call<T>>::execute_text_proposal(title.clone(), description.clone(), text.clone());
 
             Self::create_proposal(
                 origin,
@@ -192,6 +206,7 @@ decl_module! {
                 stake_balance,
                 proposal_code.encode(),
                 proposal_parameters,
+                ProposalDetails::Text(text),
             )?;
         }
 
@@ -208,6 +223,8 @@ decl_module! {
             ensure!(wasm.len() as u32 <= T::RuntimeUpgradeWasmProposalMaxLength::get(),
                 Error::RuntimeProposalSizeExceeded);
 
+            let wasm_hash = blake2_256(&wasm);
+
             let proposal_code =
                 <Call<T>>::execute_runtime_upgrade_proposal(title.clone(), description.clone(), wasm);
 
@@ -221,6 +238,7 @@ decl_module! {
                 stake_balance,
                 proposal_code.encode(),
                 proposal_parameters,
+                ProposalDetails::RuntimeUpgrade(wasm_hash.to_vec()),
             )?;
         }
 
@@ -237,7 +255,7 @@ decl_module! {
             election_parameters.ensure_valid()?;
 
             let proposal_code =
-                <governance::election::Call<T>>::set_election_parameters(election_parameters);
+                <governance::election::Call<T>>::set_election_parameters(election_parameters.clone());
 
             let proposal_parameters =
                 proposal_types::parameters::set_election_parameters_proposal::<T>();
@@ -250,6 +268,7 @@ decl_module! {
                 stake_balance,
                 proposal_code.encode(),
                 proposal_parameters,
+                ProposalDetails::SetElectionParameters(election_parameters),
             )?;
         }
 
@@ -265,7 +284,7 @@ decl_module! {
             mint_balance: BalanceOfMint<T>,
         ) {
             let proposal_code =
-                <governance::council::Call<T>>::set_council_mint_capacity(mint_balance);
+                <governance::council::Call<T>>::set_council_mint_capacity(mint_balance.clone());
 
             let proposal_parameters =
                 proposal_types::parameters::set_council_mint_capacity_proposal::<T>();
@@ -278,6 +297,7 @@ decl_module! {
                 stake_balance,
                 proposal_code.encode(),
                 proposal_parameters,
+                ProposalDetails::SetCouncilMintCapacity(mint_balance),
             )?;
         }
 
@@ -292,7 +312,7 @@ decl_module! {
             mint_balance: BalanceOfMint<T>,
         ) {
             let proposal_code =
-                <content_working_group::Call<T>>::set_mint_capacity(mint_balance);
+                <content_working_group::Call<T>>::set_mint_capacity(mint_balance.clone());
 
             let proposal_parameters =
                 proposal_types::parameters::set_content_working_group_mint_capacity_proposal::<T>();
@@ -305,6 +325,7 @@ decl_module! {
                 stake_balance,
                 proposal_code.encode(),
                 proposal_parameters,
+                ProposalDetails::SetContentWorkingGroupMintCapacity(mint_balance),
             )?;
         }
 
@@ -321,8 +342,10 @@ decl_module! {
         ) {
             ensure!(balance != BalanceOfMint::<T>::zero(), Error::SpendingProposalZeroBalance);
 
-            let proposal_code =
-                <governance::council::Call<T>>::spend_from_council_mint(balance, destination);
+            let proposal_code = <governance::council::Call<T>>::spend_from_council_mint(
+                balance.clone(),
+                destination.clone()
+            );
 
             let proposal_parameters =
                 proposal_types::parameters::spending_proposal::<T>();
@@ -335,6 +358,7 @@ decl_module! {
                 stake_balance,
                 proposal_code.encode(),
                 proposal_parameters,
+                ProposalDetails::Spending(balance, destination),
             )?;
         }
 
@@ -350,7 +374,7 @@ decl_module! {
             new_lead: Option<(T::MemberId, T::AccountId)>
         ) {
             let proposal_code =
-                <content_working_group::Call<T>>::replace_lead(new_lead);
+                <content_working_group::Call<T>>::replace_lead(new_lead.clone());
 
             let proposal_parameters =
                 proposal_types::parameters::set_lead_proposal::<T>();
@@ -363,6 +387,7 @@ decl_module! {
                 stake_balance,
                 proposal_code.encode(),
                 proposal_parameters,
+                ProposalDetails::SetLead(new_lead),
             )?;
         }
 
@@ -434,6 +459,13 @@ impl<T: Trait> Module<T> {
         stake_balance: Option<BalanceOf<T>>,
         proposal_code: Vec<u8>,
         proposal_parameters: ProposalParameters<T::BlockNumber, BalanceOf<T>>,
+        proposal_details: ProposalDetails<
+            BalanceOfMint<T>,
+            BalanceOfGovernanceCurrency<T>,
+            T::BlockNumber,
+            T::AccountId,
+            T::MemberId,
+        >,
     ) -> DispatchResult<Error> {
         let account_id =
             T::MembershipOriginValidator::ensure_actor_origin(origin, member_id.clone())?;
@@ -461,6 +493,7 @@ impl<T: Trait> Module<T> {
         )?;
 
         <ThreadIdByProposalId<T>>::insert(proposal_id, discussion_thread_id);
+        <ProposalDetailsByProposalId<T>>::insert(proposal_id, proposal_details);
 
         Ok(())
     }

+ 33 - 92
runtime-modules/proposals/codex/src/proposal_types/mod.rs

@@ -1,101 +1,42 @@
-pub(crate) mod parameters {
-    use crate::{BalanceOf, ProposalParameters};
+pub(crate) mod parameters;
 
-    // Proposal parameters for the upgrade runtime proposal
-    pub(crate) fn upgrade_runtime<T: crate::Trait>(
-    ) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
-        ProposalParameters {
-            voting_period: T::BlockNumber::from(50000u32),
-            grace_period: T::BlockNumber::from(10000u32),
-            approval_quorum_percentage: 80,
-            approval_threshold_percentage: 80,
-            slashing_quorum_percentage: 80,
-            slashing_threshold_percentage: 80,
-            required_stake: Some(<BalanceOf<T>>::from(50000u32)),
-        }
-    }
+use codec::{Decode, Encode};
+use rstd::vec::Vec;
+#[cfg(feature = "std")]
+use serde::{Deserialize, Serialize};
 
-    // Proposal parameters for the text proposal
-    pub(crate) fn text_proposal<T: crate::Trait>(
-    ) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
-        ProposalParameters {
-            voting_period: T::BlockNumber::from(50000u32),
-            grace_period: T::BlockNumber::from(10000u32),
-            approval_quorum_percentage: 40,
-            approval_threshold_percentage: 51,
-            slashing_quorum_percentage: 80,
-            slashing_threshold_percentage: 82,
-            required_stake: Some(<BalanceOf<T>>::from(500u32)),
-        }
-    }
+use crate::ElectionParameters;
 
-    // Proposal parameters for the 'Set Election Parameters' proposal
-    pub(crate) fn set_election_parameters_proposal<T: crate::Trait>(
-    ) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
-        ProposalParameters {
-            voting_period: T::BlockNumber::from(50000u32),
-            grace_period: T::BlockNumber::from(10000u32),
-            approval_quorum_percentage: 40,
-            approval_threshold_percentage: 51,
-            slashing_quorum_percentage: 81,
-            slashing_threshold_percentage: 80,
-            required_stake: Some(<BalanceOf<T>>::from(500u32)),
-        }
-    }
+/// Proposal details provide voters the information required for the perceived voting.
+#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
+#[derive(Encode, Decode, Clone, PartialEq, Debug)]
+pub enum ProposalDetails<MintedBalance, CurrencyBalance, BlockNumber, AccountId, MemberId> {
+    /// The text of the `text proposal`
+    Text(Vec<u8>),
 
-    // Proposal parameters for the 'Set council mint capacity' proposal
-    pub(crate) fn set_council_mint_capacity_proposal<T: crate::Trait>(
-    ) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
-        ProposalParameters {
-            voting_period: T::BlockNumber::from(50000u32),
-            grace_period: T::BlockNumber::from(10000u32),
-            approval_quorum_percentage: 40,
-            approval_threshold_percentage: 51,
-            slashing_quorum_percentage: 81,
-            slashing_threshold_percentage: 84,
-            required_stake: Some(<BalanceOf<T>>::from(500u32)),
-        }
-    }
+    /// The hash of wasm code for the `runtime upgrade proposal`
+    RuntimeUpgrade(Vec<u8>),
 
-    // Proposal parameters for the 'Set content working group mint capacity' proposal
-    pub(crate) fn set_content_working_group_mint_capacity_proposal<T: crate::Trait>(
-    ) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
-        ProposalParameters {
-            voting_period: T::BlockNumber::from(50000u32),
-            grace_period: T::BlockNumber::from(10000u32),
-            approval_quorum_percentage: 40,
-            approval_threshold_percentage: 51,
-            slashing_quorum_percentage: 81,
-            slashing_threshold_percentage: 85,
-            required_stake: Some(<BalanceOf<T>>::from(500u32)),
-        }
-    }
+    /// Election parameters for the `set election parameters proposal`
+    SetElectionParameters(ElectionParameters<CurrencyBalance, BlockNumber>),
 
-    // Proposal parameters for the 'Spending' proposal
-    pub(crate) fn spending_proposal<T: crate::Trait>(
-    ) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
-        ProposalParameters {
-            voting_period: T::BlockNumber::from(50000u32),
-            grace_period: T::BlockNumber::from(10000u32),
-            approval_quorum_percentage: 40,
-            approval_threshold_percentage: 51,
-            slashing_quorum_percentage: 84,
-            slashing_threshold_percentage: 85,
-            required_stake: Some(<BalanceOf<T>>::from(500u32)),
-        }
-    }
+    /// Balance and destination account for the `spending proposal`
+    Spending(MintedBalance, AccountId),
+
+    /// New leader memberId and account_id for the `set lead proposal`
+    SetLead(Option<(MemberId, AccountId)>),
+
+    /// Balance for the `set council mint capacity proposal`
+    SetCouncilMintCapacity(MintedBalance),
+
+    /// Balance for the `set content working group mint capacity proposal`
+    SetContentWorkingGroupMintCapacity(MintedBalance),
+}
 
-    // Proposal parameters for the 'Set lead' proposal
-    pub(crate) fn set_lead_proposal<T: crate::Trait>(
-    ) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
-        ProposalParameters {
-            voting_period: T::BlockNumber::from(50000u32),
-            grace_period: T::BlockNumber::from(10000u32),
-            approval_quorum_percentage: 40,
-            approval_threshold_percentage: 51,
-            slashing_quorum_percentage: 81,
-            slashing_threshold_percentage: 86,
-            required_stake: Some(<BalanceOf<T>>::from(500u32)),
-        }
+impl<MintedBalance, CurrencyBalance, BlockNumber, AccountId, MemberId> Default
+    for ProposalDetails<MintedBalance, CurrencyBalance, BlockNumber, AccountId, MemberId>
+{
+    fn default() -> Self {
+        ProposalDetails::Text(b"invalid proposal details".to_vec())
     }
 }

+ 98 - 0
runtime-modules/proposals/codex/src/proposal_types/parameters.rs

@@ -0,0 +1,98 @@
+use crate::{BalanceOf, ProposalParameters};
+
+// Proposal parameters for the upgrade runtime proposal
+pub(crate) fn upgrade_runtime<T: crate::Trait>() -> ProposalParameters<T::BlockNumber, BalanceOf<T>>
+{
+    ProposalParameters {
+        voting_period: T::BlockNumber::from(50000u32),
+        grace_period: T::BlockNumber::from(10000u32),
+        approval_quorum_percentage: 80,
+        approval_threshold_percentage: 80,
+        slashing_quorum_percentage: 80,
+        slashing_threshold_percentage: 80,
+        required_stake: Some(<BalanceOf<T>>::from(50000u32)),
+    }
+}
+
+// Proposal parameters for the text proposal
+pub(crate) fn text_proposal<T: crate::Trait>() -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
+    ProposalParameters {
+        voting_period: T::BlockNumber::from(50000u32),
+        grace_period: T::BlockNumber::from(10000u32),
+        approval_quorum_percentage: 40,
+        approval_threshold_percentage: 51,
+        slashing_quorum_percentage: 80,
+        slashing_threshold_percentage: 82,
+        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+    }
+}
+
+// Proposal parameters for the 'Set Election Parameters' proposal
+pub(crate) fn set_election_parameters_proposal<T: crate::Trait>(
+) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
+    ProposalParameters {
+        voting_period: T::BlockNumber::from(50000u32),
+        grace_period: T::BlockNumber::from(10000u32),
+        approval_quorum_percentage: 40,
+        approval_threshold_percentage: 51,
+        slashing_quorum_percentage: 81,
+        slashing_threshold_percentage: 80,
+        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+    }
+}
+
+// Proposal parameters for the 'Set council mint capacity' proposal
+pub(crate) fn set_council_mint_capacity_proposal<T: crate::Trait>(
+) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
+    ProposalParameters {
+        voting_period: T::BlockNumber::from(50000u32),
+        grace_period: T::BlockNumber::from(10000u32),
+        approval_quorum_percentage: 40,
+        approval_threshold_percentage: 51,
+        slashing_quorum_percentage: 81,
+        slashing_threshold_percentage: 84,
+        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+    }
+}
+
+// Proposal parameters for the 'Set content working group mint capacity' proposal
+pub(crate) fn set_content_working_group_mint_capacity_proposal<T: crate::Trait>(
+) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
+    ProposalParameters {
+        voting_period: T::BlockNumber::from(50000u32),
+        grace_period: T::BlockNumber::from(10000u32),
+        approval_quorum_percentage: 40,
+        approval_threshold_percentage: 51,
+        slashing_quorum_percentage: 81,
+        slashing_threshold_percentage: 85,
+        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+    }
+}
+
+// Proposal parameters for the 'Spending' proposal
+pub(crate) fn spending_proposal<T: crate::Trait>(
+) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
+    ProposalParameters {
+        voting_period: T::BlockNumber::from(50000u32),
+        grace_period: T::BlockNumber::from(10000u32),
+        approval_quorum_percentage: 40,
+        approval_threshold_percentage: 51,
+        slashing_quorum_percentage: 84,
+        slashing_threshold_percentage: 85,
+        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+    }
+}
+
+// Proposal parameters for the 'Set lead' proposal
+pub(crate) fn set_lead_proposal<T: crate::Trait>(
+) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
+    ProposalParameters {
+        voting_period: T::BlockNumber::from(50000u32),
+        grace_period: T::BlockNumber::from(10000u32),
+        approval_quorum_percentage: 40,
+        approval_threshold_percentage: 51,
+        slashing_quorum_percentage: 81,
+        slashing_threshold_percentage: 86,
+        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+    }
+}

+ 16 - 3
runtime-modules/proposals/codex/src/tests/mod.rs

@@ -5,9 +5,10 @@ use srml_support::traits::Currency;
 use srml_support::StorageMap;
 use system::RawOrigin;
 
-use crate::{BalanceOf, Error};
+use crate::{BalanceOf, Error, ProposalDetails};
 use mock::*;
 use proposal_engine::ProposalParameters;
+use runtime_io::blake2_256;
 use srml_support::dispatch::DispatchResult;
 
 struct ProposalTestFixture<InsufficientRightsCall, EmptyStakeCall, InvalidStakeCall, SuccessfulCall>
@@ -22,6 +23,7 @@ where
     invalid_stake_call: InvalidStakeCall,
     successful_call: SuccessfulCall,
     proposal_parameters: ProposalParameters<u64, u64>,
+    proposal_details: ProposalDetails<u64, u64, u64, u64, u64>,
 }
 
 impl<InsufficientRightsCall, EmptyStakeCall, InvalidStakeCall, SuccessfulCall>
@@ -59,6 +61,10 @@ where
         let proposal = ProposalsEngine::proposals(proposal_id);
         // check for correct proposal parameters
         assert_eq!(proposal.parameters, self.proposal_parameters);
+
+        // proposal details was set
+        let details = <crate::ProposalDetailsByProposalId<Test>>::get(proposal_id);
+        assert_eq!(details, self.proposal_details);
     }
 
     pub fn check_all(&self) {
@@ -113,6 +119,7 @@ fn create_text_proposal_common_checks_succeed() {
                 )
             },
             proposal_parameters: crate::proposal_types::parameters::text_proposal::<Test>(),
+            proposal_details: ProposalDetails::Text(b"text".to_vec()),
         };
         proposal_fixture.check_all();
     });
@@ -195,6 +202,7 @@ fn create_runtime_upgrade_common_checks_succeed() {
                 )
             },
             proposal_parameters: crate::proposal_types::parameters::upgrade_runtime::<Test>(),
+            proposal_details: ProposalDetails::RuntimeUpgrade(blake2_256(b"wasm").to_vec()),
         };
         proposal_fixture.check_all();
     });
@@ -289,6 +297,7 @@ fn create_set_election_parameters_proposal_common_checks_succeed() {
             },
             proposal_parameters:
                 crate::proposal_types::parameters::set_election_parameters_proposal::<Test>(),
+            proposal_details: ProposalDetails::SetElectionParameters(election_parameters),
         };
         proposal_fixture.check_all();
     });
@@ -355,11 +364,12 @@ fn create_set_council_mint_capacity_proposal_common_checks_succeed() {
                     b"title".to_vec(),
                     b"body".to_vec(),
                     Some(<BalanceOf<Test>>::from(500u32)),
-                    0,
+                    10,
                 )
             },
             proposal_parameters:
                 crate::proposal_types::parameters::set_council_mint_capacity_proposal::<Test>(),
+            proposal_details: ProposalDetails::SetCouncilMintCapacity(10),
         };
         proposal_fixture.check_all();
     });
@@ -406,10 +416,11 @@ fn create_set_content_working_group_mint_capacity_proposal_common_checks_succeed
                     b"title".to_vec(),
                     b"body".to_vec(),
                     Some(<BalanceOf<Test>>::from(500u32)),
-                    0,
+                    10,
                 )
             },
             proposal_parameters: crate::proposal_types::parameters::set_content_working_group_mint_capacity_proposal::<Test>(),
+            proposal_details: ProposalDetails::SetContentWorkingGroupMintCapacity(10),
         };
         proposal_fixture.check_all();
     });
@@ -464,6 +475,7 @@ fn create_spending_proposal_common_checks_succeed() {
                 )
             },
             proposal_parameters: crate::proposal_types::parameters::spending_proposal::<Test>(),
+            proposal_details: ProposalDetails::Spending(100, 2),
         };
         proposal_fixture.check_all();
     });
@@ -532,6 +544,7 @@ fn create_set_lead_proposal_common_checks_succeed() {
                 )
             },
             proposal_parameters: crate::proposal_types::parameters::set_lead_proposal::<Test>(),
+            proposal_details: ProposalDetails::SetLead(Some((20, 10))),
         };
         proposal_fixture.check_all();
     });