Browse Source

Storage: init configs on runtime upgrade

iorveth 4 years ago
parent
commit
9d48423b14

+ 5 - 5
node/src/chain_spec/content_config.rs

@@ -1,7 +1,7 @@
 use codec::Decode;
 use node_runtime::{
     common::storage::StorageObjectOwner,
-    data_directory::{DataObject, Quota},
+    data_directory::*,
     ChannelId, ContentId, DAOId, DataDirectoryConfig, MemberId, Runtime,
 };
 use serde::Deserialize;
@@ -125,10 +125,10 @@ pub fn empty_data_directory_config() -> DataDirectoryConfig {
     DataDirectoryConfig {
         data_object_by_content_id: vec![],
         quotas: vec![],
-        quota_size_limit_upper_bound: 20000,
-        quota_objects_limit_upper_bound: 200,
-        global_quota: Quota::new(2000000, 2000),
-        uploading_blocked: false,
+        quota_size_limit_upper_bound: DEFAULT_QUOTA_SIZE_LIMIT_UPPER_BOUND,
+        quota_objects_limit_upper_bound: DEFAULT_QUOTA_OBJECTS_LIMIT_UPPER_BOUND,
+        global_quota: DEFAULT_GLOBAL_QUOTA,
+        uploading_blocked: DEFAULT_UPLOADING_BLOCKED_STATUS,
     }
 }
 

+ 29 - 4
runtime-modules/storage/src/data_directory.rs

@@ -40,6 +40,11 @@ use crate::data_object_type_registry;
 use crate::data_object_type_registry::IsActiveDataObjectType;
 use crate::*;
 
+pub const DEFAULT_QUOTA_SIZE_LIMIT_UPPER_BOUND: u64 = 20000;
+pub const DEFAULT_QUOTA_OBJECTS_LIMIT_UPPER_BOUND: u64 = 200;
+pub const DEFAULT_GLOBAL_QUOTA: Quota = Quota::new(2000000, 2000);
+pub const DEFAULT_UPLOADING_BLOCKED_STATUS: bool = false;
+
 /// The _Data directory_ main _Trait_.
 pub trait Trait:
     pallet_timestamp::Trait
@@ -255,16 +260,16 @@ decl_storage! {
             map hasher(blake2_128_concat) StorageObjectOwner<MemberId<T>, ChannelId<T>, DAOId<T>> => Quota;
 
         /// Upper bound for the Quota size limit.
-        pub QuotaSizeLimitUpperBound get(fn quota_size_limit_upper_bound) config(): u64;
+        pub QuotaSizeLimitUpperBound get(fn quota_size_limit_upper_bound) config(): u64 = DEFAULT_QUOTA_SIZE_LIMIT_UPPER_BOUND;
 
         /// Upper bound for the Quota objects number limit.
-        pub QuotaObjectsLimitUpperBound get(fn quota_objects_limit_upper_bound) config(): u64;
+        pub QuotaObjectsLimitUpperBound get(fn quota_objects_limit_upper_bound) config(): u64 = DEFAULT_QUOTA_OBJECTS_LIMIT_UPPER_BOUND;
 
         /// Global quota.
-        pub GlobalQuota get(fn global_quota) config(): Quota;
+        pub GlobalQuota get(fn global_quota) config(): Quota = DEFAULT_GLOBAL_QUOTA;
 
         /// If all new uploads blocked
-        pub UploadingBlocked get(fn uploading_blocked) config(): bool;
+        pub UploadingBlocked get(fn uploading_blocked) config(): bool = DEFAULT_UPLOADING_BLOCKED_STATUS;
     }
 }
 
@@ -493,6 +498,26 @@ decl_module! {
 }
 
 impl<T: Trait> Module<T> {
+    pub fn initialize_data_directory(
+        quotas: Vec<(
+            StorageObjectOwner<MemberId<T>, ChannelId<T>, DAOId<T>>,
+            Quota,
+        )>,
+        quota_size_limit_upper_bound: u64,
+        quota_objects_limit_upper_bound: u64,
+        global_quota: Quota,
+        uploading_blocked: bool,
+    ) {
+        for (storage_object_owner, quota) in quotas {
+            <Quotas<T>>::insert(storage_object_owner, quota);
+        }
+
+        <QuotaSizeLimitUpperBound>::put(quota_size_limit_upper_bound);
+        <QuotaObjectsLimitUpperBound>::put(quota_objects_limit_upper_bound);
+        <GlobalQuota>::put(global_quota);
+        <UploadingBlocked>::put(uploading_blocked);
+    }
+
     // Ensure given origin can perform operation under specific storage object owner
     fn ensure_storage_object_owner_origin(
         origin: T::Origin,

+ 4 - 4
runtime-modules/storage/src/tests/mock.rs

@@ -278,14 +278,14 @@ pub struct ExtBuilder {
 impl Default for ExtBuilder {
     fn default() -> Self {
         Self {
-            quota_objects_limit_upper_bound: 200,
-            quota_size_limit_upper_bound: 20000,
-            global_quota: Quota::new(2000000, 2000),
+            quota_objects_limit_upper_bound: DEFAULT_QUOTA_SIZE_LIMIT_UPPER_BOUND,
+            quota_size_limit_upper_bound: DEFAULT_QUOTA_OBJECTS_LIMIT_UPPER_BOUND,
+            global_quota: DEFAULT_GLOBAL_QUOTA,
             first_data_object_type_id: 1,
             first_content_id: 2,
             first_relationship_id: 3,
             first_metadata_id: 4,
-            uploading_blocked: false,
+            uploading_blocked: DEFAULT_UPLOADING_BLOCKED_STATUS,
         }
     }
 }

+ 12 - 2
runtime/src/runtime_api.rs

@@ -10,10 +10,12 @@ use sp_runtime::traits::{BlakeTwo256, Block as BlockT, NumberFor};
 use sp_runtime::{generic, ApplyExtrinsicResult};
 use sp_std::vec::Vec;
 
-use crate::{BuilderWorkingGroupInstance, GatewayWorkingGroupInstance};
+use crate::{BuilderWorkingGroupInstance, DataDirectory, GatewayWorkingGroupInstance};
+
 use crate::constants::PRIMARY_PROBABILITY;
+
 use crate::{
-    content, AccountId, AuthorityDiscoveryId, Balance, BlockNumber, EpochDuration,
+    content, data_directory, AccountId, AuthorityDiscoveryId, Balance, BlockNumber, EpochDuration,
     GrandpaAuthorityList, GrandpaId, Hash, Index, RuntimeVersion, Signature, VERSION,
 };
 use crate::{
@@ -87,6 +89,14 @@ impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
             default_content_working_group_mint_capacity,
         );
 
+        DataDirectory::initialize_data_directory(
+            Vec::new(),
+            data_directory::DEFAULT_QUOTA_SIZE_LIMIT_UPPER_BOUND,
+            data_directory::DEFAULT_QUOTA_OBJECTS_LIMIT_UPPER_BOUND,
+            data_directory::DEFAULT_GLOBAL_QUOTA,
+            data_directory::DEFAULT_UPLOADING_BLOCKED_STATUS
+        );
+
         // TODO: storage / data_directory migration or clear all data objects
 
         10_000_000 // TODO: adjust weight