Browse Source

runtime: storage: Add extrinsic.

- add `distribution_buckets_per_bag_limit`
Shamil Gadelshin 3 years ago
parent
commit
429acda241

+ 31 - 20
runtime-modules/storage/src/lib.rs

@@ -64,6 +64,8 @@
 //! updates distribution bucket status (accepting new bags).
 //! - [update_distribution_buckets_for_bag](./struct.Module.html#method.update_distribution_buckets_for_bag) -
 //! updates distribution buckets for a bag.
+//! - [distribution_buckets_per_bag_limit](./struct.Module.html#method.distribution_buckets_per_bag_limit) -
+//! updates "Distribution buckets per bag" number limit.
 //!
 //! #### Public methods
 //! Public integration methods are exposed via the [DataObjectStorage](./trait.DataObjectStorage.html)
@@ -1046,6 +1048,11 @@ decl_event! {
             BTreeSet<DistributionBucketId>,
             BTreeSet<DistributionBucketId>
         ),
+
+        /// Emits on changing the "Distribution buckets per bag" number limit.
+        /// Params
+        /// - new limit
+        DistributionBucketsPerBagLimitUpdated(u64),
     }
 }
 
@@ -1189,6 +1196,12 @@ decl_error! {
 
         /// Distribution bucket is bound to a bag.
         DistributionBucketIsBoundToBag,
+
+        /// The new `DistributionBucketsPerBagLimit` number is too low.
+        DistributionBucketsPerBagLimitTooLow,
+
+        /// The new `DistributionBucketsPerBagLimit` number is too high.
+        DistributionBucketsPerBagLimitTooHigh,
     }
 }
 
@@ -1959,27 +1972,25 @@ decl_module! {
             );
         }
 
-        //TODO: distribution
+        /// Updates "Distribution buckets per bag" number limit.
+        #[weight = 10_000_000] // TODO: adjust weight
+        pub fn update_distribution_buckets_per_bag_limit(origin, new_limit: u64) {
+            T::ensure_distribution_working_group_leader_origin(origin)?;
 
-        //         /// Updates "Storage buckets per bag" number limit.
-        // #[weight = 10_000_000] // TODO: adjust weight
-        // pub fn update_storage_buckets_per_bag_limit(origin, new_limit: u64) {
-        //     T::ensure_storage_working_group_leader_origin(origin)?;
-        //
-        //     T::StorageBucketsPerBagValueConstraint::get().ensure_valid(
-        //         new_limit,
-        //         Error::<T>::StorageBucketsPerBagLimitTooLow,
-        //         Error::<T>::StorageBucketsPerBagLimitTooHigh,
-        //     )?;
-        //
-        //     //
-        //     // == MUTATION SAFE ==
-        //     //
-        //
-        //     StorageBucketsPerBagLimit::put(new_limit);
-        //
-        //     Self::deposit_event(RawEvent::StorageBucketsPerBagLimitUpdated(new_limit));
-        // }
+            T::DistributionBucketsPerBagValueConstraint::get().ensure_valid(
+                new_limit,
+                Error::<T>::DistributionBucketsPerBagLimitTooLow,
+                Error::<T>::DistributionBucketsPerBagLimitTooHigh,
+            )?;
+
+            //
+            // == MUTATION SAFE ==
+            //
+
+            DistributionBucketsPerBagLimit::put(new_limit);
+
+            Self::deposit_event(RawEvent::DistributionBucketsPerBagLimitUpdated(new_limit));
+        }
 
 
         //TODO: upldate distributing field

+ 43 - 0
runtime-modules/storage/src/tests/fixtures.rs

@@ -9,6 +9,7 @@ use super::mocks::{
     DEFAULT_STORAGE_PROVIDER_ACCOUNT_ID, STORAGE_WG_LEADER_ACCOUNT_ID,
 };
 
+use crate::tests::mocks::DISTRIBUTION_WG_LEADER_ACCOUNT_ID;
 use crate::{
     BagId, ContentId, DataObjectCreationParameters, DataObjectStorage, DistributionBucketFamily,
     DynamicBagId, DynamicBagType, RawEvent, StaticBagId, StorageBucketOperatorStatus,
@@ -1484,3 +1485,45 @@ impl UpdateDistributionBucketForBagsFixture {
         assert_eq!(actual_result, expected_result);
     }
 }
+
+pub struct UpdateDistributionBucketsPerBagLimitFixture {
+    origin: RawOrigin<u64>,
+    new_limit: u64,
+}
+
+impl UpdateDistributionBucketsPerBagLimitFixture {
+    pub fn default() -> Self {
+        Self {
+            origin: RawOrigin::Signed(DISTRIBUTION_WG_LEADER_ACCOUNT_ID),
+            new_limit: 0,
+        }
+    }
+
+    pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
+        Self { origin, ..self }
+    }
+
+    pub fn with_new_limit(self, new_limit: u64) -> Self {
+        Self { new_limit, ..self }
+    }
+
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
+        let old_limit = Storage::distribution_buckets_per_bag_limit();
+
+        let actual_result = Storage::update_distribution_buckets_per_bag_limit(
+            self.origin.clone().into(),
+            self.new_limit,
+        );
+
+        assert_eq!(actual_result, expected_result);
+
+        if actual_result.is_ok() {
+            assert_eq!(
+                Storage::distribution_buckets_per_bag_limit(),
+                self.new_limit
+            );
+        } else {
+            assert_eq!(old_limit, Storage::distribution_buckets_per_bag_limit());
+        }
+    }
+}

+ 56 - 7
runtime-modules/storage/src/tests/mod.rs

@@ -360,7 +360,7 @@ fn update_storage_buckets_for_bags_succeeded_with_voucher_usage() {
     build_test_externalities().execute_with(|| {
         let bag_id = BagId::<Test>::StaticBag(StaticBagId::Council);
 
-        set_update_storage_buckets_per_bag_limit();
+        set_default_storage_buckets_per_bag_limit();
         let old_bucket_id = create_default_storage_bucket_and_assign_to_bag(bag_id.clone());
 
         let object_creation_list = create_single_data_object();
@@ -420,7 +420,7 @@ fn update_storage_buckets_for_bags_succeeded_with_voucher_usage() {
 #[test]
 fn update_storage_buckets_for_bags_fails_with_exceeding_the_voucher_objects_number_limit() {
     build_test_externalities().execute_with(|| {
-        set_update_storage_buckets_per_bag_limit();
+        set_default_storage_buckets_per_bag_limit();
 
         let bag_id = BagId::<Test>::StaticBag(StaticBagId::Council);
 
@@ -468,7 +468,7 @@ fn update_storage_buckets_for_bags_fails_with_exceeding_the_voucher_objects_tota
     build_test_externalities().execute_with(|| {
         let bag_id = BagId::<Test>::StaticBag(StaticBagId::Council);
 
-        set_update_storage_buckets_per_bag_limit();
+        set_default_storage_buckets_per_bag_limit();
         create_default_storage_bucket_and_assign_to_bag(bag_id.clone());
 
         let object_creation_list = create_single_data_object();
@@ -541,7 +541,7 @@ fn update_storage_buckets_for_working_group_static_bags_succeeded() {
 #[test]
 fn update_storage_buckets_for_dynamic_bags_succeeded() {
     build_test_externalities().execute_with(|| {
-        set_update_storage_buckets_per_bag_limit();
+        set_default_storage_buckets_per_bag_limit();
 
         let storage_provider_id = DEFAULT_STORAGE_PROVIDER_ID;
         let invite_worker = Some(storage_provider_id);
@@ -2948,7 +2948,7 @@ fn update_storage_buckets_per_bag_limit_fails_with_incorrect_value() {
     });
 }
 
-fn set_update_storage_buckets_per_bag_limit() {
+fn set_default_storage_buckets_per_bag_limit() {
     let new_limit = 7;
 
     UpdateStorageBucketsPerBagLimitFixture::default()
@@ -3936,6 +3936,55 @@ fn set_default_distribution_buckets_per_bag_limit() {
     crate::DistributionBucketsPerBagLimit::put(5);
 }
 
-fn set_default_storage_buckets_per_bag_limit() {
-    crate::StorageBucketsPerBagLimit::put(5);
+#[test]
+fn update_distribution_buckets_per_bag_limit_succeeded() {
+    build_test_externalities().execute_with(|| {
+        let starting_block = 1;
+        run_to_block(starting_block);
+
+        let new_limit = 4;
+
+        UpdateDistributionBucketsPerBagLimitFixture::default()
+            .with_origin(RawOrigin::Signed(DISTRIBUTION_WG_LEADER_ACCOUNT_ID))
+            .with_new_limit(new_limit)
+            .call_and_assert(Ok(()));
+
+        EventFixture::assert_last_crate_event(RawEvent::DistributionBucketsPerBagLimitUpdated(
+            new_limit,
+        ));
+    });
+}
+
+#[test]
+fn update_distribution_buckets_per_bag_limit_origin() {
+    build_test_externalities().execute_with(|| {
+        let non_leader_id = 1;
+
+        UpdateDistributionBucketsPerBagLimitFixture::default()
+            .with_origin(RawOrigin::Signed(non_leader_id))
+            .call_and_assert(Err(DispatchError::BadOrigin));
+    });
+}
+
+#[test]
+fn update_distribution_buckets_per_bag_limit_fails_with_incorrect_value() {
+    build_test_externalities().execute_with(|| {
+        let new_limit = 0;
+
+        UpdateDistributionBucketsPerBagLimitFixture::default()
+            .with_origin(RawOrigin::Signed(DISTRIBUTION_WG_LEADER_ACCOUNT_ID))
+            .with_new_limit(new_limit)
+            .call_and_assert(Err(
+                Error::<Test>::DistributionBucketsPerBagLimitTooLow.into()
+            ));
+
+        let new_limit = 100;
+
+        UpdateDistributionBucketsPerBagLimitFixture::default()
+            .with_origin(RawOrigin::Signed(DISTRIBUTION_WG_LEADER_ACCOUNT_ID))
+            .with_new_limit(new_limit)
+            .call_and_assert(Err(
+                Error::<Test>::DistributionBucketsPerBagLimitTooHigh.into()
+            ));
+    });
 }