Browse Source

refactor using DataStorageObject

ignazio-bovo 3 years ago
parent
commit
aa852fad2a

+ 6 - 5
runtime-modules/content/src/lib.rs

@@ -59,7 +59,9 @@ pub trait NumericIdentifier:
     + Eq
     + PartialEq
     + Ord
-    + Zero { }
+    + Zero
+{
+}
 
 impl NumericIdentifier for u64 {}
 
@@ -749,7 +751,7 @@ decl_module! {
             let bag_id = storage::BagIdType::from(dyn_bag.clone());
 
             // channel has a dynamic bag associated to it -> remove assets from storage
-            if let Ok(bag) = Storage::<T>::ensure_bag_exists(&bag_id) {
+            if let Ok(bag) = <T as Trait>::DataObjectStorage::ensure_bag_exists(&bag_id) {
                 // ensure that bag size provided is valid
                 ensure!(
                     bag.objects_number == num_objects_to_delete,
@@ -757,8 +759,7 @@ decl_module! {
                 );
 
                 // 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();
+                let assets_to_remove = T::DataObjectStorage::get_data_objects_id(&bag_id);
 
                 // remove specified assets from storage
                 Self::remove_assets_from_storage(
@@ -1340,7 +1341,7 @@ impl<T: Trait> Module<T> {
         let dyn_bag = DynamicBagIdType::<T::MemberId, T::ChannelId>::Channel(*channel_id);
         let bag_id = BagIdType::from(dyn_bag.clone());
 
-        if Storage::<T>::ensure_bag_exists(&bag_id).is_err() {
+        if <T as Trait>::DataObjectStorage::ensure_bag_exists(&bag_id).is_err() {
             // create_dynamic_bag checks automatically satifsfied with None as second parameter
             Storage::<T>::create_dynamic_bag(dyn_bag, None).unwrap();
         }

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

@@ -359,7 +359,7 @@ impl Trait for Test {
     /// The maximum number of curators per group constraint
     type MaxNumberOfCuratorsPerGroup = MaxNumberOfCuratorsPerGroup;
 
-    /// The data object used in storage 
+    /// The data object used in storage
     type DataObjectStorage = storage::Module<Self>;
 }
 

+ 17 - 1
runtime-modules/storage/src/lib.rs

@@ -211,6 +211,12 @@ pub trait DataObjectStorage<T: Trait> {
         bag_id: &DynamicBagId<T>,
         deletion_prize: &Option<DynamicBagDeletionPrize<T>>,
     ) -> DispatchResult;
+
+    /// Checks if a bag does exists and returns it. Static Always exists
+    fn ensure_bag_exists(bag_id: &BagId<T>) -> Result<Bag<T>, DispatchError>;
+
+    /// Get all objects id in a bag, without checking its existence
+    fn get_data_objects_id(bag_id: &BagId<T>) -> BTreeSet<T::DataObjectId>;
 }
 
 /// Storage trait.
@@ -2757,6 +2763,16 @@ impl<T: Trait> DataObjectStorage<T> for Module<T> {
     ) -> DispatchResult {
         Self::validate_create_dynamic_bag_params(bag_id, deletion_prize)
     }
+
+    fn ensure_bag_exists(bag_id: &BagId<T>) -> Result<Bag<T>, DispatchError> {
+        Self::ensure_bag_exists(bag_id)
+    }
+
+    fn get_data_objects_id(bag_id: &BagId<T>) -> BTreeSet<T::DataObjectId> {
+        DataObjectsById::<T>::iter_prefix(&bag_id)
+            .map(|x| x.0)
+            .collect()
+    }
 }
 
 impl<T: Trait> Module<T> {
@@ -3419,7 +3435,7 @@ impl<T: Trait> Module<T> {
     }
 
     // Check the dynamic bag existence. Static bags always exist.
-    pub fn ensure_bag_exists(bag_id: &BagId<T>) -> Result<Bag<T>, DispatchError> {
+    fn ensure_bag_exists(bag_id: &BagId<T>) -> Result<Bag<T>, DispatchError> {
         if let BagId::<T>::Dynamic(_) = &bag_id {
             ensure!(
                 <Bags<T>>::contains_key(&bag_id),

+ 1 - 0
runtime/src/lib.rs

@@ -442,6 +442,7 @@ impl content::Trait for Runtime {
     type SeriesId = SeriesId;
     type ChannelOwnershipTransferRequestId = ChannelOwnershipTransferRequestId;
     type MaxNumberOfCuratorsPerGroup = MaxNumberOfCuratorsPerGroup;
+    type DataObjectStorage = storage::Module<Runtime>;
 }
 
 impl hiring::Trait for Runtime {