浏览代码

Merge pull request #2143 from iorveth/storage_system_blocker

Storage system blocker flag
Bedeho Mender 4 年之前
父节点
当前提交
937d3f11a9

+ 11 - 0
node/src/chain_spec/content_config.rs

@@ -26,6 +26,7 @@ struct ContentData {
     quota_size_limit_upper_bound: u64,
     quota_objects_limit_upper_bound: u64,
     global_quota: Quota,
+    uploading_blocked: bool,
 }
 
 #[derive(Deserialize)]
@@ -71,6 +72,8 @@ struct EncodedContentData {
     quota_objects_limit_upper_bound: String,
     /// hex encoded GlobalQuota
     global_quota: String,
+    /// hex encoded UploadingBlocked flag
+    uploading_blocked: String
 }
 
 fn parse_content_data(data_file: &Path) -> EncodedContentData {
@@ -106,6 +109,12 @@ impl EncodedContentData {
 
                 Decode::decode(&mut encoded_global_quota.as_slice()).unwrap()
             },
+            uploading_blocked: {
+                let encoded_uploading_blocked = hex::decode(&self.uploading_blocked[2..].as_bytes())
+                .expect("failed to parse data_object hex string");
+
+                Decode::decode(&mut encoded_uploading_blocked.as_slice()).unwrap()
+            },
         }
     }
 }
@@ -119,6 +128,7 @@ pub fn empty_data_directory_config() -> DataDirectoryConfig {
         quota_size_limit_upper_bound: 20000,
         quota_objects_limit_upper_bound: 200,
         global_quota: Quota::new(2000000, 2000),
+        uploading_blocked: false
     }
 }
 
@@ -147,5 +157,6 @@ pub fn data_directory_config_from_json(data_file: &Path) -> DataDirectoryConfig
         quota_size_limit_upper_bound: content.quota_size_limit_upper_bound,
         quota_objects_limit_upper_bound: content.quota_objects_limit_upper_bound,
         global_quota: content.global_quota,
+        uploading_blocked: content.uploading_blocked
     }
 }

+ 36 - 2
runtime-modules/storage/src/data_directory.rs

@@ -106,6 +106,9 @@ decl_error! {
 
         /// Contant uploading failed. Actor quota objects limit exceeded.
         GlobalQuotaObjectsLimitExceeded,
+
+        /// Content uploading blocked.
+        ContentUploadingBlocked,
     }
 }
 
@@ -260,6 +263,9 @@ decl_storage! {
 
         /// Global quota.
         pub GlobalQuota get(fn global_quota) config(): Quota;
+
+        /// If all new uploads blocked
+        pub UploadingBlocked get(fn uploading_blocked) config(): bool;
     }
 }
 
@@ -270,7 +276,8 @@ decl_event! {
         StorageProviderId = StorageProviderId<T>,
         Content = Vec<ContentParameters<ContentId<T>, DataObjectTypeId<T>>>,
         ContentId = ContentId<T>,
-        QuotaLimit = u64
+        QuotaLimit = u64,
+        UploadingStatus = bool
     {
         /// Emits on adding of the content.
         /// Params:
@@ -301,6 +308,11 @@ decl_event! {
         /// - StorageObjectOwner enum.
         /// - quota objects limit.
         StorageObjectOwnerQuotaObjectsLimitUpdated(StorageObjectOwner, QuotaLimit),
+
+        /// Emits when the content uploading status update performed.
+        /// Params:
+        /// - UploadingStatus bool flag.
+        ContentUploadingStatusUpdated(UploadingStatus),
     }
 }
 
@@ -324,10 +336,12 @@ decl_module! {
             owner: StorageObjectOwner<MemberId<T>, ChannelId<T>, DAOId<T>>,
             content: Vec<ContentParameters<ContentId<T>, DataObjectTypeId<T>>>
         ) {
-            
+
             // Ensure given origin can perform operation under specific storage object owner
             Self::ensure_storage_object_owner_origin(origin, &owner)?;
 
+            Self::ensure_uploading_is_not_blocked()?;
+
             Self::ensure_content_is_valid(&content)?;
 
             let owner_quota = Self::get_quota(&owner);
@@ -440,6 +454,17 @@ decl_module! {
             Self::deposit_event(RawEvent::ContentRejected(content_id, storage_provider_id));
         }
 
+        /// Locks / unlocks content uploading
+        #[weight = 10_000_000] // TODO: adjust weight
+        fn update_content_uploading_status(origin, is_blocked: bool) {
+            <StorageWorkingGroup<T>>::ensure_origin_is_active_leader(origin)?;
+
+            // == MUTATION SAFE ==
+
+            <UploadingBlocked>::put(is_blocked);
+            Self::deposit_event(RawEvent::ContentUploadingStatusUpdated(is_blocked));
+        }
+
         // Sudo methods
 
         /// Removes the content id from the list of known content ids. Requires root privileges.
@@ -507,6 +532,15 @@ impl<T: Trait> Module<T> {
         }
     }
 
+    // Ensure content uploading is not blocked
+    fn ensure_uploading_is_not_blocked() -> DispatchResult {
+        ensure!(
+            !Self::uploading_blocked(),
+            Error::<T>::ContentUploadingBlocked
+        );
+        Ok(())
+    }
+
     // Ensure owner quota constraints satisfied, returns total object length and total size voucher for this upload.
     fn ensure_owner_quota_constraints_satisfied(
         owner_quota: Quota,

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

@@ -295,6 +295,7 @@ impl ExtBuilder {
             data_object_by_content_id: vec![],
             known_content_ids: vec![],
             quotas: vec![],
+            uploading_blocked: false,
         }
         .assimilate_storage(&mut t)
         .unwrap();