Browse Source

Add text constraints to the storage working group

Shamil Gadelshin 4 years ago
parent
commit
adaa2bf350

+ 1 - 1
Cargo.lock

@@ -1569,7 +1569,7 @@ dependencies = [
 
 [[package]]
 name = "joystream-node"
-version = "2.3.0"
+version = "2.4.0"
 dependencies = [
  "ctrlc",
  "derive_more 0.14.1",

+ 1 - 1
node/Cargo.toml

@@ -3,7 +3,7 @@ authors = ['Joystream']
 build = 'build.rs'
 edition = '2018'
 name = 'joystream-node'
-version = '2.3.0'
+version = '2.4.0'
 default-run = "joystream-node"
 
 [[bin]]

+ 15 - 8
node/src/chain_spec.rs

@@ -42,6 +42,8 @@ type AccountPublic = <Signature as Verify>::Signer;
 /// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
 pub type ChainSpec = substrate_service::ChainSpec<GenesisConfig>;
 
+use node_runtime::common::constraints::InputValidationLengthConstraint;
+
 /// The chain specification option. This is expected to come in from the CLI and
 /// is little more than one of a number of alternatives which can easily be converted
 /// from a string (`--chain=...`) into a `ChainSpec`.
@@ -265,6 +267,11 @@ pub fn testnet_genesis(
         data_object_storage_registry: Some(DataObjectStorageRegistryConfig {
             first_relationship_id: 1,
             storage_working_group_mint_capacity: 0,
+            opening_human_readable_text_constraint: InputValidationLengthConstraint::new(5, 1024),
+            worker_application_human_readable_text_constraint: InputValidationLengthConstraint::new(
+                5, 1024,
+            ),
+            worker_exit_rationale_text_constraint: InputValidationLengthConstraint::new(5, 1024),
         }),
         versioned_store: Some(VersionedStoreConfig {
             class_by_id: vec![],
@@ -291,14 +298,14 @@ pub fn testnet_genesis(
             next_principal_id: 0,
             channel_creation_enabled: true, // there is no extrinsic to change it so enabling at genesis
             unstaker_by_stake_id: vec![],
-            channel_handle_constraint: crate::forum_config::new_validation(5, 20),
-            channel_description_constraint: crate::forum_config::new_validation(1, 1024),
-            opening_human_readable_text: crate::forum_config::new_validation(1, 2048),
-            curator_application_human_readable_text: crate::forum_config::new_validation(1, 2048),
-            curator_exit_rationale_text: crate::forum_config::new_validation(1, 2048),
-            channel_avatar_constraint: crate::forum_config::new_validation(5, 1024),
-            channel_banner_constraint: crate::forum_config::new_validation(5, 1024),
-            channel_title_constraint: crate::forum_config::new_validation(5, 1024),
+            channel_handle_constraint: InputValidationLengthConstraint::new(5, 20),
+            channel_description_constraint: InputValidationLengthConstraint::new(1, 1024),
+            opening_human_readable_text: InputValidationLengthConstraint::new(1, 2048),
+            curator_application_human_readable_text: InputValidationLengthConstraint::new(1, 2048),
+            curator_exit_rationale_text: InputValidationLengthConstraint::new(1, 2048),
+            channel_avatar_constraint: InputValidationLengthConstraint::new(5, 1024),
+            channel_banner_constraint: InputValidationLengthConstraint::new(5, 1024),
+            channel_title_constraint: InputValidationLengthConstraint::new(5, 1024),
         }),
         migration: Some(MigrationConfig {}),
         proposals_codex: Some(ProposalsCodexConfig {

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

@@ -37,4 +37,9 @@ impl InputValidationLengthConstraint {
             Ok(())
         }
     }
+
+    /// Create default input text constraints.
+    pub fn new(min: u16, max_min_diff: u16) -> Self {
+        InputValidationLengthConstraint { min, max_min_diff }
+    }
 }

+ 19 - 1
runtime-modules/storage/src/data_object_storage_registry.rs

@@ -25,7 +25,11 @@
 use codec::{Codec, Decode, Encode};
 use rstd::prelude::*;
 use sr_primitives::traits::{MaybeSerialize, Member, SimpleArithmetic};
-use srml_support::{decl_error, decl_event, decl_module, decl_storage, ensure, print, Parameter};
+use srml_support::{
+    decl_error, decl_event, decl_module, decl_storage, ensure, print, Parameter, StorageValue,
+};
+
+use common::constraints::InputValidationLengthConstraint;
 
 use crate::data_directory::{self, ContentIdExists};
 use crate::{StorageProviderId, StorageWorkingGroup};
@@ -123,6 +127,9 @@ decl_storage! {
     }
     add_extra_genesis {
         config(storage_working_group_mint_capacity): minting::BalanceOf<T>;
+        config(opening_human_readable_text_constraint): InputValidationLengthConstraint;
+        config(worker_application_human_readable_text_constraint): InputValidationLengthConstraint;
+        config(worker_exit_rationale_text_constraint): InputValidationLengthConstraint;
         build(|config: &GenesisConfig<T>| {
             // Create a mint.
             let mint_id_result =
@@ -133,6 +140,17 @@ decl_storage! {
             } else {
                 print("Failed to create a mint for the storage working group");
             }
+
+            // Create constraints
+            <working_group::OpeningHumanReadableText::<working_group::Instance2>>::put(
+                config.opening_human_readable_text_constraint.clone()
+            );
+            <working_group::WorkerApplicationHumanReadableText::<working_group::Instance2>>::put(
+                config.worker_application_human_readable_text_constraint.clone()
+            );
+            <working_group::WorkerExitRationaleText::<working_group::Instance2>>::put(
+                config.worker_exit_rationale_text_constraint.clone()
+            );
         });
     }
 }

+ 32 - 0
runtime-modules/storage/src/tests/data_object_storage_registry.rs

@@ -1,6 +1,8 @@
 #![cfg(test)]
 
 use super::mock::*;
+use crate::StorageWorkingGroup;
+use srml_support::StorageLinkedMap;
 
 #[test]
 fn initial_state() {
@@ -155,3 +157,33 @@ fn test_toggle_ready() {
         );
     });
 }
+
+#[test]
+fn ensure_setting_genesis_storage_working_group_mint_succeeds() {
+    with_default_mock_builder(|| {
+        let mint_id = <StorageWorkingGroup<Test>>::mint();
+
+        assert!(minting::Mints::<Test>::exists(mint_id));
+
+        let mint = <minting::Module<Test>>::mints(mint_id);
+        assert_eq!(mint.capacity(), STORAGE_WORKING_GROUP_MINT_CAPACITY);
+    });
+}
+
+#[test]
+fn ensure_setting_genesis_constraints_succeeds() {
+    with_default_mock_builder(|| {
+        let default_constraint = common::constraints::InputValidationLengthConstraint::new(
+            STORAGE_WORKING_GROUP_CONSTRAINT_MIN,
+            STORAGE_WORKING_GROUP_CONSTRAINT_DIFF,
+        );
+        let opening_text_constraint = <StorageWorkingGroup<Test>>::opening_human_readable_text();
+        let worker_text_constraint =
+            <StorageWorkingGroup<Test>>::worker_application_human_readable_text();
+        let worker_exit_text_constraint = <StorageWorkingGroup<Test>>::worker_exit_rationale_text();
+
+        assert_eq!(opening_text_constraint, default_constraint);
+        assert_eq!(worker_text_constraint, default_constraint);
+        assert_eq!(worker_exit_text_constraint, default_constraint);
+    });
+}

+ 0 - 13
runtime-modules/storage/src/tests/data_object_type_registry.rs

@@ -2,7 +2,6 @@
 
 use super::mock::*;
 use crate::StorageWorkingGroup;
-use srml_support::StorageLinkedMap;
 use system::{self, EventRecord, Phase, RawOrigin};
 
 const DEFAULT_LEADER_ACCOUNT_ID: u64 = 1;
@@ -284,15 +283,3 @@ fn activate_existing() {
         assert!(!data.unwrap().active);
     });
 }
-
-#[test]
-fn ensure_setting_genesis_storage_working_group_mint_succeeds() {
-    with_default_mock_builder(|| {
-        let mint_id = <StorageWorkingGroup<Test>>::mint();
-
-        assert!(minting::Mints::<Test>::exists(mint_id));
-
-        let mint = <minting::Module<Test>>::mints(mint_id);
-        assert_eq!(mint.capacity(), STORAGE_WORKING_GROUP_MINT_CAPACITY);
-    });
-}

+ 15 - 0
runtime-modules/storage/src/tests/mock.rs

@@ -1,6 +1,7 @@
 #![cfg(test)]
 
 pub use crate::{data_directory, data_object_storage_registry, data_object_type_registry};
+use common::constraints::InputValidationLengthConstraint;
 pub use common::currency::GovernanceCurrency;
 use membership::members;
 pub use system;
@@ -226,6 +227,8 @@ pub struct ExtBuilder {
 }
 
 pub(crate) const STORAGE_WORKING_GROUP_MINT_CAPACITY: u64 = 40000;
+pub(crate) const STORAGE_WORKING_GROUP_CONSTRAINT_MIN: u16 = 1;
+pub(crate) const STORAGE_WORKING_GROUP_CONSTRAINT_DIFF: u16 = 40;
 
 impl Default for ExtBuilder {
     fn default() -> Self {
@@ -270,6 +273,18 @@ impl ExtBuilder {
         data_object_storage_registry::GenesisConfig::<Test> {
             first_relationship_id: self.first_relationship_id,
             storage_working_group_mint_capacity: self.storage_working_group_mint_capacity,
+            opening_human_readable_text_constraint: InputValidationLengthConstraint::new(
+                STORAGE_WORKING_GROUP_CONSTRAINT_MIN,
+                STORAGE_WORKING_GROUP_CONSTRAINT_DIFF,
+            ),
+            worker_application_human_readable_text_constraint: InputValidationLengthConstraint::new(
+                STORAGE_WORKING_GROUP_CONSTRAINT_MIN,
+                STORAGE_WORKING_GROUP_CONSTRAINT_DIFF,
+            ),
+            worker_exit_rationale_text_constraint: InputValidationLengthConstraint::new(
+                STORAGE_WORKING_GROUP_CONSTRAINT_MIN,
+                STORAGE_WORKING_GROUP_CONSTRAINT_DIFF,
+            ),
         }
         .assimilate_storage(&mut t)
         .unwrap();

+ 2 - 0
runtime/src/lib.rs

@@ -425,7 +425,9 @@ impl finality_tracker::Trait for Runtime {
     type ReportLatency = ReportLatency;
 }
 
+pub use common;
 pub use forum;
+
 pub use governance::election_params::ElectionParameters;
 use governance::{council, election};
 use membership::members;