Browse Source

delete channel fix & updated tests

ignazio-bovo 3 years ago
parent
commit
416cfb4921

+ 4 - 0
runtime-modules/content/src/errors.rs

@@ -76,5 +76,9 @@ decl_error! {
         /// Channel Contains Assets
         ChannelContainsAssets,
 
+        /// Bag Size specified is not valid
+        InvalidBagSizeSpecified,
+
+
     }
 }

+ 21 - 3
runtime-modules/content/src/lib.rs

@@ -652,6 +652,7 @@ decl_module! {
                     &sender,
                 )?;
             }
+
             //
             // == MUTATION SAFE ==
             //
@@ -727,7 +728,7 @@ decl_module! {
             origin,
             actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
-            assets_to_remove: BTreeSet<DataObjectId<T>>,
+            num_objects_to_delete: u64,
         ) -> DispatchResult {
             // check that channel exists
             let channel = Self::ensure_channel_exists(&channel_id)?;
@@ -742,11 +743,28 @@ decl_module! {
             // check that channel videos are 0
             ensure!(channel.num_videos == 0, Error::<T>::ChannelContainsVideos);
 
+            // get bag id for the channel
+            let dyn_bag = DynamicBagIdType::<T::MemberId, T::ChannelId>::Channel(channel_id);
+            let bag_id = storage::BagIdType::from(dyn_bag.clone());
+
+            // ensure that bag size provided is valid
+            ensure!(
+                storage::Bags::<T>::get(&bag_id).objects_number == num_objects_to_delete,
+                Error::<T>::InvalidBagSizeSpecified
+            );
+
+            // construct collection of assets to be removed
+            let assets_to_remove: BTreeSet<DataObjectId<T>> =
+                storage::DataObjectsById::<T>::iter_prefix(&bag_id).map(|x| x.0).collect();
+
             // remove specified assets from storage
-            Self::remove_assets_from_storage(&assets_to_remove, &channel_id, &channel.deletion_prize_source_account_id)?;
+            Self::remove_assets_from_storage(
+                &assets_to_remove,
+                &channel_id,
+                &channel.deletion_prize_source_account_id
+            )?;
 
             // delete channel dynamic bag
-            let dyn_bag = DynamicBagIdType::<T::MemberId, T::ChannelId>::Channel(channel_id);
             Storage::<T>::delete_dynamic_bag(
                 channel.deletion_prize_source_account_id,
                 dyn_bag

+ 5 - 9
runtime-modules/content/src/tests/channels.rs

@@ -19,7 +19,6 @@ fn successful_channel_deletion() {
         );
 
         // 3 assets added at creation
-        let first_obj_id = Storage::<Test>::next_data_object_id();
         let assets = StorageAssetsRecord {
             object_creation_list: vec![
                 DataObjectCreationParameters {
@@ -51,17 +50,14 @@ fn successful_channel_deletion() {
             Ok(()),
         );
 
-        // retrieve objs id set
-        let obj_ids =
-            (first_obj_id..Storage::<Test>::next_data_object_id()).collect::<BTreeSet<_>>();
-
-        // attempt to delete channel with non zero assets should result in error
+        // attempt to delete channel with non zero assets should result in error: objects
+        // are misspecified
         delete_channel_mock(
             FIRST_MEMBER_ORIGIN,
             ContentActor::Member(FIRST_MEMBER_ID),
             channel_id,
-            BTreeSet::new(),
-            Err(storage::Error::<Test>::CannotDeleteNonEmptyDynamicBag.into()),
+            2u64,
+            Err(Error::<Test>::InvalidBagSizeSpecified.into()),
         );
 
         // successful deletion because we empty the bag first
@@ -69,7 +65,7 @@ fn successful_channel_deletion() {
             FIRST_MEMBER_ORIGIN,
             ContentActor::Member(FIRST_MEMBER_ID),
             channel_id,
-            obj_ids,
+            3u64,
             Ok(()),
         );
     })

+ 2 - 2
runtime-modules/content/src/tests/mock.rs

@@ -509,7 +509,7 @@ pub fn delete_channel_mock(
     sender: u64,
     actor: ContentActor<CuratorGroupId, CuratorId, MemberId>,
     channel_id: ChannelId,
-    assets: BTreeSet<<Test as storage::Trait>::DataObjectId>,
+    objects_num: u64,
     result: DispatchResult,
 ) {
     assert_eq!(
@@ -517,7 +517,7 @@ pub fn delete_channel_mock(
             Origin::signed(sender),
             actor.clone(),
             channel_id.clone(),
-            assets.clone()
+            objects_num,
         ),
         result.clone(),
     );