Browse Source

Set general proposal parameters.

Shamil Gadelshin 4 years ago
parent
commit
f55dcf6070

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

@@ -96,6 +96,9 @@ pub trait Trait:
 pub type BalanceOf<T> =
     <<T as stake::Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance;
 
+/// Currency alias for `stake` module
+pub type CurrencyOf<T> = <T as stake::Trait>::Currency;
+
 /// Balance alias for GovernanceCurrency from `common` module. TODO: replace with BalanceOf
 pub type BalanceOfGovernanceCurrency<T> =
     <<T as common::currency::GovernanceCurrency>::Currency as Currency<

+ 123 - 74
runtime-modules/proposals/codex/src/proposal_types/parameters.rs

@@ -1,29 +1,62 @@
-use crate::{BalanceOf, ProposalParameters};
+use crate::{BalanceOf, CurrencyOf, ProposalParameters};
+use rstd::convert::TryInto;
+use sr_primitives::traits::SaturatedConversion;
+pub use sr_primitives::Perbill;
+use srml_support::traits::Currency;
+
+// calculates required stake value using total issuance value and stake percentage. Truncates to
+// lowest integer value.
+fn get_required_stake_by_fraction<T: crate::Trait>(
+    numerator: u32,
+    denominator: u32,
+) -> BalanceOf<T> {
+    let total_issuance: u128 = <CurrencyOf<T>>::total_issuance().try_into().unwrap_or(0) as u128;
+    let required_stake =
+        Perbill::from_rational_approximation(numerator, denominator) * total_issuance;
+
+    let balance: BalanceOf<T> = required_stake.saturated_into();
+
+    balance
+}
+
+// Proposal parameters for the 'Set validator count' proposal
+pub(crate) fn set_validator_count_proposal<T: crate::Trait>(
+) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
+    ProposalParameters {
+        voting_period: T::BlockNumber::from(43200u32),
+        grace_period: T::BlockNumber::from(0u32),
+        approval_quorum_percentage: 66,
+        approval_threshold_percentage: 80,
+        slashing_quorum_percentage: 60,
+        slashing_threshold_percentage: 80,
+        required_stake: Some(get_required_stake_by_fraction::<T>(25, 10000)),
+    }
+}
 
 // Proposal parameters for the upgrade runtime proposal
 pub(crate) fn runtime_upgrade_proposal<T: crate::Trait>(
 ) -> ProposalParameters<T::BlockNumber, BalanceOf<T>> {
     ProposalParameters {
-        voting_period: T::BlockNumber::from(50000u32),
-        grace_period: T::BlockNumber::from(10000u32),
+        voting_period: T::BlockNumber::from(72000u32),
+        grace_period: T::BlockNumber::from(72000u32),
         approval_quorum_percentage: 80,
-        approval_threshold_percentage: 80,
-        slashing_quorum_percentage: 80,
+        approval_threshold_percentage: 100,
+        slashing_quorum_percentage: 60,
         slashing_threshold_percentage: 80,
-        required_stake: Some(<BalanceOf<T>>::from(50000u32)),
+        required_stake: Some(get_required_stake_by_fraction::<T>(1, 100)),
     }
 }
 
 // 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)),
+        voting_period: T::BlockNumber::from(72000u32),
+        grace_period: T::BlockNumber::from(0u32),
+        approval_quorum_percentage: 66,
+        approval_threshold_percentage: 80,
+        slashing_quorum_percentage: 60,
+        slashing_threshold_percentage: 80,
+        required_stake: Some(get_required_stake_by_fraction::<T>(25, 10000)),
     }
 }
 
@@ -31,13 +64,13 @@ pub(crate) fn text_proposal<T: crate::Trait>() -> ProposalParameters<T::BlockNum
 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,
+        voting_period: T::BlockNumber::from(72000u32),
+        grace_period: T::BlockNumber::from(201601u32),
+        approval_quorum_percentage: 66,
+        approval_threshold_percentage: 80,
+        slashing_quorum_percentage: 60,
         slashing_threshold_percentage: 80,
-        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+        required_stake: Some(get_required_stake_by_fraction::<T>(75, 10000)),
     }
 }
 
@@ -45,13 +78,13 @@ pub(crate) fn set_election_parameters_proposal<T: crate::Trait>(
 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)),
+        voting_period: T::BlockNumber::from(43200u32),
+        grace_period: T::BlockNumber::from(0u32),
+        approval_quorum_percentage: 66,
+        approval_threshold_percentage: 80,
+        slashing_quorum_percentage: 50,
+        slashing_threshold_percentage: 50,
+        required_stake: Some(get_required_stake_by_fraction::<T>(25, 10000)),
     }
 }
 
@@ -59,13 +92,13 @@ pub(crate) fn set_council_mint_capacity_proposal<T: crate::Trait>(
 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)),
+        voting_period: T::BlockNumber::from(43200u32),
+        grace_period: T::BlockNumber::from(0u32),
+        approval_quorum_percentage: 50,
+        approval_threshold_percentage: 75,
+        slashing_quorum_percentage: 60,
+        slashing_threshold_percentage: 80,
+        required_stake: Some(get_required_stake_by_fraction::<T>(25, 10000)),
     }
 }
 
@@ -73,13 +106,13 @@ pub(crate) fn set_content_working_group_mint_capacity_proposal<T: crate::Trait>(
 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)),
+        voting_period: T::BlockNumber::from(43200u32),
+        grace_period: T::BlockNumber::from(0u32),
+        approval_quorum_percentage: 66,
+        approval_threshold_percentage: 80,
+        slashing_quorum_percentage: 60,
+        slashing_threshold_percentage: 80,
+        required_stake: Some(get_required_stake_by_fraction::<T>(25, 10000)),
     }
 }
 
@@ -87,13 +120,13 @@ pub(crate) fn spending_proposal<T: crate::Trait>(
 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)),
+        voting_period: T::BlockNumber::from(43200u32),
+        grace_period: T::BlockNumber::from(0u32),
+        approval_quorum_percentage: 66,
+        approval_threshold_percentage: 80,
+        slashing_quorum_percentage: 60,
+        slashing_threshold_percentage: 80,
+        required_stake: Some(get_required_stake_by_fraction::<T>(25, 10000)),
     }
 }
 
@@ -101,40 +134,56 @@ pub(crate) fn set_lead_proposal<T: crate::Trait>(
 pub(crate) fn evict_storage_provider_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: 87,
-        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+        voting_period: T::BlockNumber::from(43200u32),
+        grace_period: T::BlockNumber::from(0u32),
+        approval_quorum_percentage: 50,
+        approval_threshold_percentage: 75,
+        slashing_quorum_percentage: 60,
+        slashing_threshold_percentage: 80,
+        required_stake: Some(get_required_stake_by_fraction::<T>(1, 1000)),
     }
 }
 
-// Proposal parameters for the 'Set validator count' proposal
-pub(crate) fn set_validator_count_proposal<T: crate::Trait>(
+// Proposal parameters for the 'Set storage role parameters' proposal
+pub(crate) fn set_storage_role_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: 82,
-        slashing_threshold_percentage: 87,
-        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+        voting_period: T::BlockNumber::from(43200u32),
+        grace_period: T::BlockNumber::from(14400u32),
+        approval_quorum_percentage: 75,
+        approval_threshold_percentage: 80,
+        slashing_quorum_percentage: 60,
+        slashing_threshold_percentage: 80,
+        required_stake: Some(get_required_stake_by_fraction::<T>(25, 10000)),
     }
 }
 
-// Proposal parameters for the 'Set storage role parameters' proposal
-pub(crate) fn set_storage_role_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: 82,
-        slashing_threshold_percentage: 88,
-        required_stake: Some(<BalanceOf<T>>::from(500u32)),
+#[cfg(test)]
+mod test {
+    use crate::proposal_types::parameters::get_required_stake_by_fraction;
+    use crate::tests::{increase_total_balance_issuance, initial_test_ext, Test};
+
+    pub use sr_primitives::Perbill;
+
+    #[test]
+    fn calculate_get_required_stake_by_fraction_with_zero_issuance() {
+        initial_test_ext()
+            .execute_with(|| assert_eq!(get_required_stake_by_fraction::<Test>(5, 7), 0));
+    }
+
+    #[test]
+    fn calculate_stake_by_percentage_for_defined_issuance_succeeds() {
+        initial_test_ext().execute_with(|| {
+            increase_total_balance_issuance(50000);
+            assert_eq!(get_required_stake_by_fraction::<Test>(1, 1000), 50)
+        });
+    }
+
+    #[test]
+    fn calculate_stake_by_percentage_for_defined_issuance_with_fraction_loss() {
+        initial_test_ext().execute_with(|| {
+            increase_total_balance_issuance(1111);
+            assert_eq!(get_required_stake_by_fraction::<Test>(3, 1000), 3);
+        });
     }
 }

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

@@ -6,12 +6,21 @@ use srml_support::StorageMap;
 use system::RawOrigin;
 
 use crate::{BalanceOf, Error, ProposalDetails};
-use mock::*;
 use proposal_engine::ProposalParameters;
 use roles::actors::RoleParameters;
 use runtime_io::blake2_256;
 use srml_support::dispatch::DispatchResult;
 
+pub use mock::*;
+
+pub(crate) fn increase_total_balance_issuance(balance: u64) {
+    let initial_balance = Balances::total_issuance();
+    {
+        let _ = <Test as stake::Trait>::Currency::deposit_creating(&999, balance);
+    }
+    assert_eq!(Balances::total_issuance(), initial_balance + balance);
+}
+
 struct ProposalTestFixture<InsufficientRightsCall, EmptyStakeCall, InvalidStakeCall, SuccessfulCall>
 where
     InsufficientRightsCall: Fn() -> DispatchResult<crate::Error>,
@@ -81,6 +90,8 @@ where
 #[test]
 fn create_text_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_text_proposal(
@@ -118,7 +129,7 @@ fn create_text_proposal_common_checks_succeed() {
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(500u32)),
+                    Some(<BalanceOf<Test>>::from(1250u32)),
                     b"text".to_vec(),
                 )
             },
@@ -164,6 +175,8 @@ fn create_text_proposal_codex_call_fails_with_incorrect_text_size() {
 #[test]
 fn create_runtime_upgrade_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_runtime_upgrade_proposal(
@@ -201,7 +214,7 @@ fn create_runtime_upgrade_common_checks_succeed() {
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(50000u32)),
+                    Some(<BalanceOf<Test>>::from(5000u32)),
                     b"wasm".to_vec(),
                 )
             },
@@ -264,6 +277,8 @@ fn create_upgrade_runtime_proposal_codex_call_fails_with_not_allowed_member_id()
 #[test]
 fn create_set_election_parameters_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_set_election_parameters_proposal(
@@ -301,7 +316,7 @@ fn create_set_election_parameters_proposal_common_checks_succeed() {
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(500u32)),
+                    Some(<BalanceOf<Test>>::from(3750u32)),
                     get_valid_election_parameters(),
                 )
             },
@@ -411,6 +426,8 @@ fn create_set_election_parameters_call_fails_with_incorrect_parameters() {
 #[test]
 fn create_set_council_mint_capacity_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_set_council_mint_capacity_proposal(
@@ -438,7 +455,7 @@ fn create_set_council_mint_capacity_proposal_common_checks_succeed() {
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(5000u32)),
+                    Some(<BalanceOf<Test>>::from(150u32)),
                     0,
                 )
             },
@@ -448,7 +465,7 @@ fn create_set_council_mint_capacity_proposal_common_checks_succeed() {
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(500u32)),
+                    Some(<BalanceOf<Test>>::from(1250u32)),
                     10,
                 )
             },
@@ -463,6 +480,8 @@ fn create_set_council_mint_capacity_proposal_common_checks_succeed() {
 #[test]
 fn create_set_content_working_group_mint_capacity_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_set_content_working_group_mint_capacity_proposal(
@@ -500,7 +519,7 @@ fn create_set_content_working_group_mint_capacity_proposal_common_checks_succeed
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(500u32)),
+                    Some(<BalanceOf<Test>>::from(1250u32)),
                     10,
                 )
             },
@@ -514,6 +533,8 @@ fn create_set_content_working_group_mint_capacity_proposal_common_checks_succeed
 #[test]
 fn create_spending_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_spending_proposal(
@@ -554,7 +575,7 @@ fn create_spending_proposal_common_checks_succeed() {
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(500u32)),
+                    Some(<BalanceOf<Test>>::from(1250u32)),
                     100,
                     2,
                 )
@@ -587,6 +608,8 @@ fn create_spending_proposal_call_fails_with_incorrect_balance() {
 #[test]
 fn create_set_lead_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_set_lead_proposal(
@@ -624,7 +647,7 @@ fn create_set_lead_proposal_common_checks_succeed() {
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(500u32)),
+                    Some(<BalanceOf<Test>>::from(1250u32)),
                     Some((20, 10)),
                 )
             },
@@ -638,6 +661,8 @@ fn create_set_lead_proposal_common_checks_succeed() {
 #[test]
 fn create_evict_storage_provider_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_evict_storage_provider_proposal(
@@ -689,6 +714,8 @@ fn create_evict_storage_provider_proposal_common_checks_succeed() {
 #[test]
 fn create_set_validator_count_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_set_validator_count_proposal(
@@ -726,7 +753,7 @@ fn create_set_validator_count_proposal_common_checks_succeed() {
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(500u32)),
+                    Some(<BalanceOf<Test>>::from(1250u32)),
                     4,
                 )
             },
@@ -759,6 +786,8 @@ fn create_set_validator_count_proposal_failed_with_invalid_validator_count() {
 #[test]
 fn create_set_storage_role_parameters_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {
+        increase_total_balance_issuance(500000);
+
         let proposal_fixture = ProposalTestFixture {
             insufficient_rights_call: || {
                 ProposalCodex::create_set_storage_role_parameters_proposal(
@@ -796,7 +825,7 @@ fn create_set_storage_role_parameters_proposal_common_checks_succeed() {
                     1,
                     b"title".to_vec(),
                     b"body".to_vec(),
-                    Some(<BalanceOf<Test>>::from(500u32)),
+                    Some(<BalanceOf<Test>>::from(1250u32)),
                     RoleParameters::default(),
                 )
             },
@@ -828,11 +857,6 @@ fn assert_failed_set_storage_parameters_call(
 #[test]
 fn create_set_storage_role_parameters_proposal_fails_with_invalid_parameters() {
     initial_test_ext().execute_with(|| {
-        // {
-        //     let _imbalance = <Test as stake::Trait>::Currency::deposit_creating(&44, 50000);
-        // }
-        // assert_eq!(Balances::total_issuance(), 0);
-
         let mut role_parameters = RoleParameters::default();
         role_parameters.min_actors = 0;
         assert_failed_set_storage_parameters_call(