Browse Source

Set min validator count for the proposals

Shamil Gadelshin 5 years ago
parent
commit
c27a869e12

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

@@ -75,6 +75,9 @@ pub trait Trait:
     + roles::actors::Trait
     + staking::Trait
 {
+    /// Defines min allowed validator count for the 'Set validator count' proposal.
+    type SetValidatorCountProposalMinValidators: Get<u32>;
+
     /// Defines max allowed text proposal length.
     type TextProposalMaxLength: Get<u32>;
 
@@ -133,6 +136,9 @@ decl_error! {
         /// Invalid balance value for the spending proposal
         SpendingProposalZeroBalance,
 
+        /// Invalid validator count for the 'set validator count' proposal
+        LessThanMinValidatorCount,
+
         /// Require root origin in extrinsics
         RequireRootOrigin,
     }
@@ -446,6 +452,11 @@ decl_module! {
             stake_balance: Option<BalanceOf<T>>,
             new_validator_count: u32,
         ) {
+            ensure!(
+                new_validator_count >= T::SetValidatorCountProposalMinValidators::get(),
+                Error::LessThanMinValidatorCount
+            );
+
             let proposal_code =
                 <staking::Call<T>>::set_validator_count(new_validator_count);
 

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

@@ -155,6 +155,7 @@ impl VotersParameters for MockVotersParameters {
 
 parameter_types! {
     pub const TextProposalMaxLength: u32 = 20_000;
+    pub const SetValidatorCountProposalMinValidators: u32 = 4;
     pub const RuntimeUpgradeWasmProposalMaxLength: u32 = 20_000;
     pub const RuntimeUpgradeProposalAllowedProposers: Vec<u64> = vec![1];
 }
@@ -245,6 +246,7 @@ impl staking::SessionInterface<u64> for Test {
 }
 
 impl crate::Trait for Test {
+    type SetValidatorCountProposalMinValidators = SetValidatorCountProposalMinValidators;
     type TextProposalMaxLength = TextProposalMaxLength;
     type RuntimeUpgradeWasmProposalMaxLength = RuntimeUpgradeWasmProposalMaxLength;
     type RuntimeUpgradeProposalAllowedProposers = RuntimeUpgradeProposalAllowedProposers;

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

@@ -630,7 +630,7 @@ fn create_set_validator_count_proposal_common_checks_succeed() {
                     b"title".to_vec(),
                     b"body".to_vec(),
                     None,
-                    1,
+                    4,
                 )
             },
             empty_stake_call: || {
@@ -640,7 +640,7 @@ fn create_set_validator_count_proposal_common_checks_succeed() {
                     b"title".to_vec(),
                     b"body".to_vec(),
                     None,
-                    1,
+                    4,
                 )
             },
             invalid_stake_call: || {
@@ -650,7 +650,7 @@ fn create_set_validator_count_proposal_common_checks_succeed() {
                     b"title".to_vec(),
                     b"body".to_vec(),
                     Some(<BalanceOf<Test>>::from(5000u32)),
-                    1,
+                    4,
                 )
             },
             successful_call: || {
@@ -660,18 +660,35 @@ fn create_set_validator_count_proposal_common_checks_succeed() {
                     b"title".to_vec(),
                     b"body".to_vec(),
                     Some(<BalanceOf<Test>>::from(500u32)),
-                    1,
+                    4,
                 )
             },
             proposal_parameters: crate::proposal_types::parameters::set_validator_count_proposal::<
                 Test,
             >(),
-            proposal_details: ProposalDetails::SetValidatorCount(1),
+            proposal_details: ProposalDetails::SetValidatorCount(4),
         };
         proposal_fixture.check_all();
     });
 }
 
+#[test]
+fn create_set_validator_count_proposal_failed_with_invalid_validator_count() {
+    initial_test_ext().execute_with(|| {
+        assert_eq!(
+            ProposalCodex::create_set_validator_count_proposal(
+                RawOrigin::Signed(1).into(),
+                1,
+                b"title".to_vec(),
+                b"body".to_vec(),
+                Some(<BalanceOf<Test>>::from(500u32)),
+                3,
+            ),
+            Err(Error::LessThanMinValidatorCount)
+        );
+    });
+}
+
 #[test]
 fn create_set_storage_role_parameters_proposal_common_checks_succeed() {
     initial_test_ext().execute_with(|| {

+ 2 - 0
runtime/src/lib.rs

@@ -859,6 +859,7 @@ impl proposals_discussion::Trait for Runtime {
 
 parameter_types! {
     pub const TextProposalMaxLength: u32 = 60_000;
+    pub const SetValidatorCountProposalMinValidators: u32 = 4;
     pub const RuntimeUpgradeWasmProposalMaxLength: u32 = 2_000_000;
     pub const RuntimeUpgradeProposalAllowedProposers: Vec<u64> = Vec::new(); //TODO set allowed members
 }
@@ -868,6 +869,7 @@ impl proposals_codex::Trait for Runtime {
     type TextProposalMaxLength = TextProposalMaxLength;
     type RuntimeUpgradeWasmProposalMaxLength = RuntimeUpgradeWasmProposalMaxLength;
     type RuntimeUpgradeProposalAllowedProposers = RuntimeUpgradeProposalAllowedProposers;
+    type SetValidatorCountProposalMinValidators = SetValidatorCountProposalMinValidators;
 }
 
 construct_runtime!(