Browse Source

Refactor InputValidationLengthConstraint in the bureaucracy

- delete constraints module
- use common::constraints::InputValidationLengthConstraint instead
Shamil Gadelshin 4 years ago
parent
commit
0a2ee72cd3

+ 6 - 5
runtime-modules/bureaucracy/Cargo.toml

@@ -18,6 +18,7 @@ std = [
     'membership/std',
     'minting/std',
     'recurringrewards/std',
+    'common/std',
 ]
 
 [dependencies.primitives]
@@ -86,6 +87,11 @@ default_features = false
 package = 'substrate-recurring-reward-module'
 path = '../recurring-reward'
 
+[dependencies.common]
+default_features = false
+package = 'substrate-common-module'
+path = '../common'
+
 [dev-dependencies.runtime-io]
 default_features = false
 git = 'https://github.com/paritytech/substrate.git'
@@ -98,11 +104,6 @@ git = 'https://github.com/paritytech/substrate.git'
 package = 'srml-balances'
 rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
 
-[dev-dependencies.common]
-default_features = false
-package = 'substrate-common-module'
-path = '../common'
-
 [dev-dependencies.timestamp]
 default_features = false
 git = 'https://github.com/paritytech/substrate.git'

+ 0 - 40
runtime-modules/bureaucracy/src/constraints.rs

@@ -1,40 +0,0 @@
-use codec::{Decode, Encode};
-#[cfg(feature = "std")]
-use serde::{Deserialize, Serialize};
-
-/// Length constraint for input validation
-#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
-#[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
-pub struct InputValidationLengthConstraint {
-    /// Minimum length
-    pub min: u16,
-
-    /// Difference between minimum length and max length.
-    /// While having max would have been more direct, this
-    /// way makes max < min unrepresentable semantically,
-    /// which is safer.
-    pub max_min_diff: u16,
-}
-
-impl InputValidationLengthConstraint {
-    /// Helper for computing max
-    pub fn max(&self) -> u16 {
-        self.min + self.max_min_diff
-    }
-
-    pub fn ensure_valid(
-        &self,
-        len: usize,
-        too_short_msg: &'static str,
-        too_long_msg: &'static str,
-    ) -> Result<(), &'static str> {
-        let length = len as u16;
-        if length < self.min {
-            Err(too_short_msg)
-        } else if length > self.max() {
-            Err(too_long_msg)
-        } else {
-            Ok(())
-        }
-    }
-}

+ 1 - 2
runtime-modules/bureaucracy/src/lib.rs

@@ -1,7 +1,6 @@
 // Ensure we're `no_std` when compiling for Wasm.
 #![cfg_attr(not(feature = "std"), no_std)]
 
-mod constraints;
 #[cfg(test)]
 mod tests;
 mod types;
@@ -17,7 +16,7 @@ use srml_support::traits::{Currency, ExistenceRequirement, WithdrawReasons};
 use srml_support::{decl_event, decl_module, decl_storage, dispatch, ensure};
 use system::{ensure_root, ensure_signed, RawOrigin};
 
-use constraints::InputValidationLengthConstraint;
+use common::constraints::InputValidationLengthConstraint;
 use errors::bureaucracy_errors::*;
 use errors::WrappedError;
 use types::{

+ 1 - 1
runtime-modules/bureaucracy/src/tests/mod.rs

@@ -1,12 +1,12 @@
 mod mock;
 
-use crate::constraints::InputValidationLengthConstraint;
 use crate::tests::mock::Test;
 use crate::types::{
     Lead, OpeningPolicyCommitment, RewardPolicy, Worker, WorkerApplication, WorkerOpening,
     WorkerRoleStage, WorkerRoleStakeProfile,
 };
 use crate::{Instance1, RawEvent};
+use common::constraints::InputValidationLengthConstraint;
 use mock::{build_test_externalities, Balances, Bureaucracy1, Membership, System, TestEvent};
 use srml_support::StorageValue;
 use std::collections::{BTreeMap, BTreeSet};