Преглед на файлове

Storage: introduce can_remove_content & atomically_remove_content storage hooks

iorveth преди 4 години
родител
ревизия
9ee3001567
променени са 2 файла, в които са добавени 60 реда и са изтрити 1 реда
  1. 15 0
      runtime-modules/common/src/storage.rs
  2. 45 1
      runtime-modules/storage/src/data_directory.rs

+ 15 - 0
runtime-modules/common/src/storage.rs

@@ -41,4 +41,19 @@ pub trait StorageSystem<T: crate::StorageOwnership + crate::MembershipTypes> {
         owner: StorageObjectOwner<T::MemberId, T::ChannelId, T::DAOId>,
         content_parameters: Vec<ContentParameters<T::ContentId, T::DataObjectTypeId>>,
     ) -> DispatchResult;
+
+    // Should hook into call on storage system,
+    // but requires rich error (with reasons)  types.
+    // caller already knows the `ContentId`s as they are part of
+    // the ContentUploadParameters
+    fn atomically_remove_content(
+        owner: &StorageObjectOwner<T::MemberId, T::ChannelId, T::DAOId>,
+        content_ids: &[T::ContentId],
+    ) -> DispatchResult;
+
+    // Checks if given owner can remove content under givencontent ids from the storage system
+    fn can_remove_content(
+        owner: &StorageObjectOwner<T::MemberId, T::ChannelId, T::DAOId>,
+        content_ids: &[T::ContentId],
+    ) -> DispatchResult;
 }

+ 45 - 1
runtime-modules/storage/src/data_directory.rs

@@ -729,22 +729,66 @@ impl<T: Trait> common::storage::StorageSystem<T> for Module<T> {
     ) -> DispatchResult {
         Self::ensure_content_is_valid(&content)?;
 
-        let liaison = T::StorageProviderHelper::get_random_storage_provider()?;
+        Self::ensure_uploading_is_not_blocked()?;
 
         let owner_quota = Self::get_quota(&owner);
+
+        // Ensure owner quota constraints satisfied.
+        // Calculate upload voucher
         let upload_voucher = Self::ensure_owner_quota_constraints_satisfied(owner_quota, &content)?;
 
+        // Ensure global quota constraints satisfied.
+        Self::ensure_global_quota_constraints_satisfied(upload_voucher)?;
+
+        let liaison = T::StorageProviderHelper::get_random_storage_provider()?;
+
+        //
+        // == MUTATION SAFE ==
+        //
+
+        // Let's create the entry then
+
         Self::upload_content(owner_quota, upload_voucher, liaison, content, owner);
         Ok(())
     }
 
+    fn atomically_remove_content(
+        owner: &StorageObjectOwner<MemberId<T>, ChannelId<T>, DAOId<T>>,
+        content_ids: &[T::ContentId],
+    ) -> DispatchResult {
+        // Ensure content under given content ids can be successfully removed
+        let content = Self::ensure_content_can_be_removed(content_ids, owner)?;
+
+        //
+        // == MUTATION SAFE ==
+        //
+
+        // Let's remove a content
+        Self::delete_content(owner, content_ids, content);
+        Ok(())
+    }
+
     fn can_add_content(
         owner: StorageObjectOwner<MemberId<T>, ChannelId<T>, DAOId<T>>,
         content: Vec<ContentParameters<T::ContentId, DataObjectTypeId<T>>>,
     ) -> DispatchResult {
+        Self::ensure_uploading_is_not_blocked()?;
+
         T::StorageProviderHelper::get_random_storage_provider()?;
         let owner_quota = Self::get_quota(&owner);
+
+        // Ensure owner quota constraints satisfied.
         Self::ensure_owner_quota_constraints_satisfied(owner_quota, &content)?;
         Self::ensure_content_is_valid(&content)
     }
+
+    fn can_remove_content(
+        owner: &StorageObjectOwner<MemberId<T>, ChannelId<T>, DAOId<T>>,
+        content_ids: &[ContentId<T>],
+    ) -> DispatchResult {
+        // Ensure content under given content ids can be successfully removed
+        Self::ensure_content_can_be_removed(content_ids, &owner)?;
+
+        Ok(())
+    }
 }