Browse Source

Add text constraints runtime migration.

Shamil Gadelshin 4 years ago
parent
commit
eb1bf62bc8

+ 4 - 5
node/src/chain_spec.rs

@@ -189,6 +189,7 @@ pub fn testnet_genesis(
 
     // default codex proposals config parameters
     let cpcp = node_runtime::ProposalsConfigParameters::default();
+    let default_text_constraint = node_runtime::working_group::default_text_constraint();
 
     GenesisConfig {
         system: Some(SystemConfig {
@@ -267,11 +268,9 @@ 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),
+            opening_human_readable_text_constraint: default_text_constraint,
+            worker_application_human_readable_text_constraint: default_text_constraint,
+            worker_exit_rationale_text_constraint: default_text_constraint,
         }),
         versioned_store: Some(VersionedStoreConfig {
             class_by_id: vec![],

+ 3 - 3
runtime-modules/common/src/constraints.rs

@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
 
 /// Length constraint for input validation
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
-#[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
+#[derive(Encode, Decode, Default, Clone, PartialEq, Eq, Copy)]
 pub struct InputValidationLengthConstraint {
     /// Minimum length
     pub min: u16,
@@ -18,12 +18,12 @@ pub struct InputValidationLengthConstraint {
 
 impl InputValidationLengthConstraint {
     /// Helper for computing max
-    pub fn max(&self) -> u16 {
+    pub fn max(self) -> u16 {
         self.min + self.max_min_diff
     }
 
     pub fn ensure_valid(
-        &self,
+        self,
         len: usize,
         too_short_msg: &'static str,
         too_long_msg: &'static str,

+ 3 - 3
runtime-modules/storage/src/data_object_storage_registry.rs

@@ -143,13 +143,13 @@ decl_storage! {
 
             // Create constraints
             <working_group::OpeningHumanReadableText::<working_group::Instance2>>::put(
-                config.opening_human_readable_text_constraint.clone()
+                config.opening_human_readable_text_constraint
             );
             <working_group::WorkerApplicationHumanReadableText::<working_group::Instance2>>::put(
-                config.worker_application_human_readable_text_constraint.clone()
+                config.worker_application_human_readable_text_constraint
             );
             <working_group::WorkerExitRationaleText::<working_group::Instance2>>::put(
-                config.worker_exit_rationale_text_constraint.clone()
+                config.worker_exit_rationale_text_constraint
             );
         });
     }

+ 5 - 0
runtime-modules/working-group/src/lib.rs

@@ -1208,3 +1208,8 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
             .map_err(|e| e.into())
     }
 }
+
+/// Creates default text constraint.
+pub fn default_text_constraint() -> InputValidationLengthConstraint {
+    InputValidationLengthConstraint::new(1, 1024)
+}

+ 16 - 15
runtime/src/lib.rs

@@ -62,6 +62,22 @@ pub use timestamp::Call as TimestampCall;
 use integration::proposals::{CouncilManager, ExtrinsicProposalEncoder, MembershipOriginValidator};
 pub use proposals_codex::ProposalsConfigParameters;
 
+pub use common;
+pub use forum;
+pub use working_group;
+
+pub use governance::election_params::ElectionParameters;
+use governance::{council, election};
+use membership::members;
+use storage::{data_directory, data_object_storage_registry, data_object_type_registry};
+pub use versioned_store;
+
+pub use content_working_group as content_wg;
+mod migration;
+
+/// Alias for ContentId, used in various places.
+pub type ContentId = primitives::H256;
+
 /// An index to a block.
 pub type BlockNumber = u32;
 
@@ -425,21 +441,6 @@ 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;
-use storage::{data_directory, data_object_storage_registry, data_object_type_registry};
-pub use versioned_store;
-
-pub use content_working_group as content_wg;
-mod migration;
-
-/// Alias for ContentId, used in various places.
-pub type ContentId = primitives::H256;
-
 impl versioned_store::Trait for Runtime {
     type Event = Event;
 }

+ 13 - 0
runtime/src/migration.rs

@@ -20,6 +20,7 @@ impl<T: Trait> Module<T> {
         // Other tasks like resetting values, migrating values etc.
 
         Self::initialize_storage_working_group_mint();
+        Self::initialize_storage_working_group_text_constraints();
     }
 }
 
@@ -78,4 +79,16 @@ impl<T: Trait> Module<T> {
             print("Failed to create a mint for the storage working group");
         }
     }
+
+    fn initialize_storage_working_group_text_constraints() {
+        <working_group::OpeningHumanReadableText<working_group::Instance2>>::put(
+            working_group::default_text_constraint(),
+        );
+        <working_group::WorkerApplicationHumanReadableText<working_group::Instance2>>::put(
+            working_group::default_text_constraint(),
+        );
+        <working_group::WorkerExitRationaleText<working_group::Instance2>>::put(
+            working_group::default_text_constraint(),
+        );
+    }
 }