Jelajahi Sumber

data_directory: give sudo ability to increase limits

Mokhtar Naamani 4 tahun lalu
induk
melakukan
f428c56aa1

+ 111 - 0
runtime-modules/storage/src/data_directory.rs

@@ -248,6 +248,14 @@ impl Voucher {
     pub fn set_new_objects_limit(&mut self, new_objects_limit: u64) {
         self.objects_limit = new_objects_limit;
     }
+
+    pub fn increment_size_limit(&mut self, increment_by: u64) {
+        self.size_limit += increment_by;
+    }
+
+    pub fn increment_objects_limit(&mut self, increment_by: u64) {
+        self.objects_limit += increment_by;
+    }
 }
 
 /// A map collection of unique DataObjects keyed by the ContentId
@@ -332,6 +340,30 @@ decl_event! {
         /// Params:
         /// - UploadingStatus bool flag.
         ContentUploadingStatusUpdated(UploadingStatus),
+
+        /// Emits when the global voucher size limit is incremented.
+        /// Params:
+        /// - Increment amount
+        /// - New limit
+        GlobalVoucherSizeLimitIncremented(u64, u64),
+
+        /// Emits when the global voucher objects limit is incremented.
+        /// Params:
+        /// - Increment amount
+        /// - New limit
+        GlobalVoucherObjectsLimitIncremented(u64, u64),
+
+        /// Emits when the size limit upper bound is incremented.
+        /// Params:
+        /// - Increment amount
+        /// - New limit
+        VoucherSizeLimitUpperBoundIncremented(u64, u64),
+
+        /// Emits when the objects limit upper bound is incremented.
+        /// Params:
+        /// - Increment amount
+        /// - New limit
+        VoucherObjectsLimitUpperBoundIncremented(u64, u64),
     }
 }
 
@@ -459,6 +491,85 @@ decl_module! {
             Self::deposit_event(RawEvent::StorageObjectOwnerVoucherSizeLimitUpdated(abstract_owner, new_voucher_size_limit));
         }
 
+        /// Increments global voucher size limit. Requires root privileges.
+        #[weight = 10_000_000] // TODO: adjust weight
+        pub fn increase_global_voucher_size_limit(
+            origin,
+            increment: u64
+        ) {
+            ensure_root(origin)?;
+
+            // ensure no overflow?
+
+            //
+            // == MUTATION SAFE ==
+            //
+
+            let mut voucher = Self::global_voucher();
+            voucher.increment_size_limit(increment);
+            GlobalVoucher::put(voucher);
+
+            Self::deposit_event(RawEvent::GlobalVoucherSizeLimitIncremented(increment, voucher.size_limit));
+        }
+
+        /// Increments global voucher objects limit. Requires root privileges.
+        #[weight = 10_000_000] // TODO: adjust weight
+        pub fn increase_global_voucher_objects_limit(
+            origin,
+            increment: u64
+        ) {
+            ensure_root(origin)?;
+            // ensure no overflow?
+
+            //
+            // == MUTATION SAFE ==
+            //
+
+            let mut voucher = Self::global_voucher();
+            voucher.increment_objects_limit(increment);
+            GlobalVoucher::put(voucher);
+
+            Self::deposit_event(RawEvent::GlobalVoucherObjectsLimitIncremented(increment, voucher.objects_limit));
+        }
+
+        /// Increments VoucherSizeLimitUpperBound. Requires root privileges.
+        #[weight = 10_000_000] // TODO: adjust weight
+        pub fn increase_voucher_size_limit_upper_bound(
+            origin,
+            increment: u64
+        ) {
+            ensure_root(origin)?;
+            // ensure no overflow?
+
+            //
+            // == MUTATION SAFE ==
+            //
+
+            let new_upper_bound = VoucherSizeLimitUpperBound::get() + increment;
+            VoucherSizeLimitUpperBound::put(new_upper_bound);
+
+            Self::deposit_event(RawEvent::VoucherSizeLimitUpperBoundIncremented(increment, new_upper_bound));
+        }
+
+        /// Increments VoucherObjectsLimitUpperBound. Requires root privileges.
+        #[weight = 10_000_000] // TODO: adjust weight
+        pub fn increase_voucher_objects_limit_upper_bound(
+            origin,
+            increment: u64
+        ) {
+            ensure_root(origin)?;
+            // ensure no overflow?
+
+            //
+            // == MUTATION SAFE ==
+            //
+
+            let new_upper_bound = VoucherObjectsLimitUpperBound::get() + increment;
+            VoucherObjectsLimitUpperBound::put(new_upper_bound);
+
+            Self::deposit_event(RawEvent::VoucherObjectsLimitUpperBoundIncremented(increment, new_upper_bound));
+        }
+
         /// Storage provider accepts a content. Requires signed storage provider account and its id.
         /// The LiaisonJudgement can be updated, but only by the liaison.
         #[weight = 10_000_000] // TODO: adjust weight

+ 123 - 0
runtime-modules/storage/src/tests/data_directory.rs

@@ -2,6 +2,7 @@
 
 use crate::data_directory::Error;
 use common::storage::StorageObjectOwner;
+use frame_support::assert_ok;
 use frame_support::dispatch::DispatchError;
 use system::RawOrigin;
 
@@ -547,3 +548,125 @@ fn reject_content_as_liaison() {
         assert_eq!(res, Ok(()));
     });
 }
+
+#[test]
+fn incrementing_global_voucher_limits() {
+    with_default_mock_builder(|| {
+        /*
+           Events are not emitted on block 0.
+           So any dispatchable calls made during genesis block formation will have no events emitted.
+           https://substrate.dev/recipes/2-appetizers/4-events.html
+        */
+        run_to_block(1);
+
+        let size_limit = TestDataDirectory::global_voucher().size_limit;
+        let increment_size_limit_by = 10;
+        let expected_new_size_limit = size_limit + increment_size_limit_by;
+
+        assert_ok!(TestDataDirectory::increase_global_voucher_size_limit(
+            RawOrigin::Root.into(),
+            increment_size_limit_by
+        ));
+
+        assert_eq!(
+            System::events().last().unwrap().event,
+            MetaEvent::data_directory(data_directory::RawEvent::GlobalVoucherSizeLimitIncremented(
+                increment_size_limit_by,
+                expected_new_size_limit
+            ))
+        );
+
+        assert_eq!(
+            TestDataDirectory::global_voucher().size_limit,
+            expected_new_size_limit
+        );
+
+        let objects_limit = TestDataDirectory::global_voucher().objects_limit;
+        let increment_objects_limit_by = 10;
+        let expected_new_objects_limit = objects_limit + increment_objects_limit_by;
+
+        assert_ok!(TestDataDirectory::increase_global_voucher_objects_limit(
+            RawOrigin::Root.into(),
+            increment_objects_limit_by
+        ));
+
+        assert_eq!(
+            System::events().last().unwrap().event,
+            MetaEvent::data_directory(
+                data_directory::RawEvent::GlobalVoucherObjectsLimitIncremented(
+                    increment_objects_limit_by,
+                    expected_new_objects_limit
+                )
+            )
+        );
+
+        assert_eq!(
+            TestDataDirectory::global_voucher().objects_limit,
+            expected_new_objects_limit
+        );
+    })
+}
+
+#[test]
+fn incrementing_limit_upper_bounds() {
+    with_default_mock_builder(|| {
+        /*
+           Events are not emitted on block 0.
+           So any dispatchable calls made during genesis block formation will have no events emitted.
+           https://substrate.dev/recipes/2-appetizers/4-events.html
+        */
+        run_to_block(1);
+
+        let size_limit_upper_bound = TestDataDirectory::voucher_size_limit_upper_bound();
+        let increment_size_limit_upper_bound_by = 10;
+        let expected_new_size_limit_upper_bound =
+            size_limit_upper_bound + increment_size_limit_upper_bound_by;
+
+        assert_ok!(TestDataDirectory::increase_voucher_size_limit_upper_bound(
+            RawOrigin::Root.into(),
+            increment_size_limit_upper_bound_by
+        ));
+
+        assert_eq!(
+            System::events().last().unwrap().event,
+            MetaEvent::data_directory(
+                data_directory::RawEvent::VoucherSizeLimitUpperBoundIncremented(
+                    increment_size_limit_upper_bound_by,
+                    expected_new_size_limit_upper_bound
+                )
+            )
+        );
+
+        assert_eq!(
+            TestDataDirectory::voucher_size_limit_upper_bound(),
+            expected_new_size_limit_upper_bound
+        );
+
+        let objects_limit_upper_bound = TestDataDirectory::voucher_objects_limit_upper_bound();
+        let increment_objects_limit_upper_bound_by = 10;
+        let expected_new_objects_limit_upper_bound =
+            objects_limit_upper_bound + increment_objects_limit_upper_bound_by;
+
+        assert_ok!(
+            TestDataDirectory::increase_voucher_objects_limit_upper_bound(
+                RawOrigin::Root.into(),
+                increment_objects_limit_upper_bound_by
+            )
+        );
+
+        assert_eq!(
+            System::events().last().unwrap().event,
+            MetaEvent::data_directory(
+                data_directory::RawEvent::VoucherObjectsLimitUpperBoundIncremented(
+                    increment_objects_limit_upper_bound_by,
+                    expected_new_objects_limit_upper_bound
+                )
+            )
+        );
+
+        assert_eq!(
+            TestDataDirectory::voucher_objects_limit_upper_bound(),
+            expected_new_objects_limit_upper_bound
+        );
+    })
+}