Browse Source

Move InputValidationLengthConstraint to the common module

- move InputValidationLengthConstraint to the common module from the forum
- update forum
- update content working group
Shamil Gadelshin 4 years ago
parent
commit
e0c6d24638

+ 2 - 0
Cargo.lock

@@ -4604,6 +4604,8 @@ dependencies = [
 name = "substrate-common-module"
 version = "1.0.0"
 dependencies = [
+ "parity-scale-codec",
+ "serde",
  "sr-primitives",
  "srml-support",
  "srml-system",

+ 13 - 0
runtime-modules/common/Cargo.toml

@@ -10,6 +10,8 @@ std = [
 	'sr-primitives/std',
 	'srml-support/std',
 	'system/std',
+	'codec/std',
+	'serde'
 ]
 
 
@@ -30,3 +32,14 @@ default_features = false
 git = 'https://github.com/paritytech/substrate.git'
 package = 'srml-system'
 rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
+
+[dependencies.codec]
+default-features = false
+features = ['derive']
+package = 'parity-scale-codec'
+version = '1.0.0'
+
+[dependencies.serde]
+features = ['derive']
+optional = true
+version = '1.0.101'

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

@@ -0,0 +1,40 @@
+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 - 0
runtime-modules/common/src/lib.rs

@@ -1,5 +1,6 @@
 // Ensure we're `no_std` when compiling for Wasm.
 #![cfg_attr(not(feature = "std"), no_std)]
 
+pub mod constraints;
 pub mod currency;
 pub mod origin_validator;

+ 6 - 5
runtime-modules/content-working-group/Cargo.toml

@@ -22,6 +22,7 @@ std = [
     'versioned_store/std',
     'versioned_store_permissions/std',
     'recurringrewards/std',
+    'common/std',
 ]
 
 
@@ -106,6 +107,11 @@ default_features = false
 package = 'substrate-membership-module'
 path = '../membership'
 
+[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'
@@ -123,8 +129,3 @@ default_features = false
 git = 'https://github.com/paritytech/substrate.git'
 package = 'srml-timestamp'
 rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
-
-[dev-dependencies.common]
-default_features = false
-package = 'substrate-common-module'
-path = '../common'

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

@@ -38,9 +38,7 @@ use srml_support::{
 };
 use system::{self, ensure_root, ensure_signed};
 
-/// DIRTY IMPORT BECAUSE
-/// InputValidationLengthConstraint has not been factored out yet!!!
-use forum::InputValidationLengthConstraint;
+use common::constraints::InputValidationLengthConstraint;
 
 /// Module configuration trait for this Substrate module.
 pub trait Trait:

+ 2 - 1
runtime-modules/forum/Cargo.toml

@@ -61,7 +61,7 @@ git = 'https://github.com/paritytech/substrate.git'
 package = 'srml-balances'
 rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
 
-[dev-dependencies.common]
+[dependencies.common]
 default_features = false
 package = 'substrate-common-module'
 path = '../common'
@@ -90,4 +90,5 @@ std = [
   	'balances/std',
 	'timestamp/std',
 	'bureaucracy/std',
+	'common/std',
 ]

+ 2 - 41
runtime-modules/forum/src/lib.rs

@@ -16,50 +16,11 @@ use runtime_primitives::traits::EnsureOrigin;
 use srml_support::{decl_event, decl_module, decl_storage, dispatch, ensure};
 use system::{ensure_signed, RawOrigin};
 
+pub use common::constraints::InputValidationLengthConstraint;
+
 mod mock;
 mod tests;
 
-/*
- * MOVE ALL OF THESE OUT TO COMMON LATER
- */
-
-/// 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(())
-        }
-    }
-}
-
 /// Constants
 /////////////////////////////////////////////////////////////////
 

+ 0 - 1
runtime-modules/proposals/engine/Cargo.toml

@@ -20,7 +20,6 @@ std = [
     'sr-primitives/std',
     'membership/std',
     'common/std',
-
 ]