Jelajahi Sumber

Storage: move DefaultVoucher to the runtime storage, fix tests

iorveth 4 tahun lalu
induk
melakukan
b2eb32684a

+ 11 - 0
node/src/chain_spec/content_config.rs

@@ -25,6 +25,7 @@ struct ContentData {
     voucher_size_limit_upper_bound: u64,
     voucher_objects_limit_upper_bound: u64,
     global_voucher: Voucher,
+    default_voucher: Voucher,
     uploading_blocked: bool,
 }
 
@@ -71,6 +72,8 @@ struct EncodedContentData {
     voucher_objects_limit_upper_bound: String,
     /// hex encoded GlobalVoucher
     global_voucher: String,
+    /// hex encoded DefaultVoucher
+    default_voucher: String,
     /// hex encoded UploadingBlocked flag
     uploading_blocked: String,
 }
@@ -108,6 +111,12 @@ impl EncodedContentData {
 
                 Decode::decode(&mut encoded_global_voucher.as_slice()).unwrap()
             },
+            default_voucher: {
+                let encoded_default_voucher = hex::decode(&self.default_voucher[2..].as_bytes())
+                    .expect("failed to parse data_object hex string");
+
+                Decode::decode(&mut encoded_default_voucher.as_slice()).unwrap()
+            },
             uploading_blocked: {
                 let encoded_uploading_blocked =
                     hex::decode(&self.uploading_blocked[2..].as_bytes())
@@ -127,6 +136,7 @@ pub fn empty_data_directory_config() -> DataDirectoryConfig {
         voucher_size_limit_upper_bound: DEFAULT_VOUCHER_SIZE_LIMIT_UPPER_BOUND,
         voucher_objects_limit_upper_bound: DEFAULT_VOUCHER_OBJECTS_LIMIT_UPPER_BOUND,
         global_voucher: DEFAULT_GLOBAL_VOUCHER,
+        default_voucher: DEFAULT_VOUCHER,
         uploading_blocked: DEFAULT_UPLOADING_BLOCKED_STATUS,
     }
 }
@@ -151,6 +161,7 @@ pub fn data_directory_config_from_json(data_file: &Path) -> DataDirectoryConfig
         voucher_size_limit_upper_bound: content.voucher_size_limit_upper_bound,
         voucher_objects_limit_upper_bound: content.voucher_objects_limit_upper_bound,
         global_voucher: content.global_voucher,
+        default_voucher: content.default_voucher,
         uploading_blocked: content.uploading_blocked,
     }
 }

+ 12 - 8
runtime-modules/storage/src/data_directory.rs

@@ -23,7 +23,6 @@
 
 use codec::{Decode, Encode};
 use frame_support::dispatch::DispatchResult;
-use frame_support::traits::Get;
 use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure};
 use sp_std::collections::btree_map::BTreeMap;
 use sp_std::vec::Vec;
@@ -43,6 +42,7 @@ use crate::*;
 pub const DEFAULT_VOUCHER_SIZE_LIMIT_UPPER_BOUND: u64 = 20000;
 pub const DEFAULT_VOUCHER_OBJECTS_LIMIT_UPPER_BOUND: u64 = 200;
 pub const DEFAULT_GLOBAL_VOUCHER: Voucher = Voucher::new(2000000, 2000);
+pub const DEFAULT_VOUCHER: Voucher = Voucher::new(50000, 100);
 pub const DEFAULT_UPLOADING_BLOCKED_STATUS: bool = false;
 
 /// The _Data directory_ main _Trait_.
@@ -66,9 +66,6 @@ pub trait Trait:
 
     /// Validates member id and origin combination.
     type MemberOriginValidator: ActorOriginValidator<Self::Origin, MemberId<Self>, Self::AccountId>;
-
-    /// Default content voucher for all actors.
-    type DefaultVoucher: Get<Voucher>;
 }
 
 decl_error! {
@@ -265,11 +262,15 @@ decl_storage! {
         /// Upper bound for the Voucher objects number limit.
         pub VoucherObjectsLimitUpperBound get(fn voucher_objects_limit_upper_bound) config(): u64 = DEFAULT_VOUCHER_OBJECTS_LIMIT_UPPER_BOUND;
 
+        /// Default content voucher for all actors.
+        pub DefaultVoucher get(fn default_voucher) config(): Voucher;
+
         /// Global voucher.
         pub GlobalVoucher get(fn global_voucher) config(): Voucher = DEFAULT_GLOBAL_VOUCHER;
 
         /// If all new uploads blocked
         pub UploadingBlocked get(fn uploading_blocked) config(): bool = DEFAULT_UPLOADING_BLOCKED_STATUS;
+
     }
 }
 
@@ -416,7 +417,7 @@ decl_module! {
                     voucher.set_new_objects_limit(new_voucher_objects_limit);
                 });
             } else {
-                let mut voucher = T::DefaultVoucher::get();
+                let mut voucher = Self::default_voucher();
                 voucher.set_new_objects_limit(new_voucher_objects_limit);
                 <Vouchers<T>>::insert(&abstract_owner, voucher);
             };
@@ -443,7 +444,7 @@ decl_module! {
                     voucher.set_new_size_limit(new_voucher_size_limit);
                 });
             } else {
-                let mut voucher = T::DefaultVoucher::get();
+                let mut voucher = Self::default_voucher();
                 voucher.set_new_size_limit(new_voucher_size_limit);
                 <Vouchers<T>>::insert(&abstract_owner, voucher);
             };
@@ -498,6 +499,7 @@ decl_module! {
 }
 
 impl<T: Trait> Module<T> {
+    // Used to initialize data_directory runtime storage on runtime upgrade
     pub fn initialize_data_directory(
         vouchers: Vec<(
             StorageObjectOwner<MemberId<T>, ChannelId<T>, DAOId<T>>,
@@ -506,6 +508,7 @@ impl<T: Trait> Module<T> {
         voucher_size_limit_upper_bound: u64,
         voucher_objects_limit_upper_bound: u64,
         global_voucher: Voucher,
+        default_voucher: Voucher,
         uploading_blocked: bool,
     ) {
         for (storage_object_owner, voucher) in vouchers {
@@ -515,6 +518,7 @@ impl<T: Trait> Module<T> {
         <VoucherSizeLimitUpperBound>::put(voucher_size_limit_upper_bound);
         <VoucherObjectsLimitUpperBound>::put(voucher_objects_limit_upper_bound);
         <GlobalVoucher>::put(global_voucher);
+        <DefaultVoucher>::put(default_voucher);
         <UploadingBlocked>::put(uploading_blocked);
     }
 
@@ -536,7 +540,7 @@ impl<T: Trait> Module<T> {
         if <Vouchers<T>>::contains_key(owner) {
             Self::vouchers(owner)
         } else {
-            T::DefaultVoucher::get()
+            Self::default_voucher()
         }
     }
 
@@ -737,7 +741,7 @@ impl<T: Trait> ContentIdExists<T> for Module<T> {
         if Self::has_content(content_id) {
             Ok(Self::data_object_by_content_id(*content_id))
         } else {
-            Err(Error::<T>::CidNotFound.into())
+            Err(Error::<T>::CidNotFound)
         }
     }
 }

+ 3 - 3
runtime-modules/storage/src/tests/data_directory.rs

@@ -91,7 +91,7 @@ fn add_content_size_limit_reached() {
         let content_parameters = ContentParameters {
             content_id: 1,
             type_id: 1234,
-            size: DefaultVoucher::get().size_limit + 1,
+            size: DEFAULT_VOUCHER.size_limit + 1,
             ipfs_content_id: vec![1, 2, 3, 4],
         };
 
@@ -111,7 +111,7 @@ fn add_content_objects_limit_reached() {
 
         let mut content = vec![];
 
-        for i in 0..=DefaultVoucher::get().objects_limit {
+        for i in 0..=DEFAULT_VOUCHER.objects_limit {
             let content_parameters = ContentParameters {
                 content_id: i + 1,
                 type_id: 1234,
@@ -356,7 +356,7 @@ fn update_storage_object_owner_voucher_size_limit() {
 
         let owner = StorageObjectOwner::Member(1u64);
 
-        let new_objects_total_size_limit = 1500;
+        let new_objects_total_size_limit = 100;
 
         let res = TestDataDirectory::update_storage_object_owner_voucher_size_limit(
             Origin::signed(DEFAULT_LEADER_ACCOUNT_ID),

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

@@ -21,6 +21,11 @@ use common::currency::GovernanceCurrency;
 use frame_support::StorageValue;
 use membership;
 
+pub use crate::data_directory::{
+    DEFAULT_GLOBAL_VOUCHER, DEFAULT_UPLOADING_BLOCKED_STATUS, DEFAULT_VOUCHER,
+    DEFAULT_VOUCHER_OBJECTS_LIMIT_UPPER_BOUND, DEFAULT_VOUCHER_SIZE_LIMIT_UPPER_BOUND,
+};
+
 mod working_group_mod {
     pub use super::StorageWorkingGroupInstance;
     pub use working_group::Event;
@@ -96,7 +101,7 @@ impl ContentIdExists<Test> for MockContent {
 
     fn get_data_object(
         which: &ContentId<Test>,
-    ) -> Result<data_directory::DataObject<Test>, &'static str> {
+    ) -> Result<data_directory::DataObject<Test>, data_directory::Error<Test>> {
         match *which {
             TEST_MOCK_EXISTING_CID => Ok(data_directory::DataObjectInternal {
                 type_id: 1,
@@ -110,7 +115,7 @@ impl ContentIdExists<Test> for MockContent {
                 liaison_judgement: data_directory::LiaisonJudgement::Pending,
                 ipfs_content_id: vec![],
             }),
-            _ => Err("nope, missing"),
+            _ => Err(data_directory::Error::<Test>::CidNotFound),
         }
     }
 }
@@ -124,7 +129,6 @@ parameter_types! {
     pub const MaximumBlockLength: u32 = 2 * 1024;
     pub const AvailableBlockRatio: Perbill = Perbill::one();
     pub const MinimumPeriod: u64 = 5;
-    pub const DefaultVoucher: Voucher = Voucher::new(5000, 50);
 }
 
 impl system::Trait for Test {
@@ -207,7 +211,6 @@ impl data_directory::Trait for Test {
     type StorageProviderHelper = ();
     type IsActiveDataObjectType = AnyDataObjectTypeIsActive;
     type MemberOriginValidator = ();
-    type DefaultVoucher = DefaultVoucher;
 }
 
 impl crate::data_directory::StorageProviderHelper<Test> for () {
@@ -268,6 +271,7 @@ pub struct ExtBuilder {
     voucher_objects_limit_upper_bound: u64,
     voucher_size_limit_upper_bound: u64,
     global_voucher: Voucher,
+    default_voucher: Voucher,
     first_data_object_type_id: u64,
     first_content_id: u64,
     first_relationship_id: u64,
@@ -281,6 +285,7 @@ impl Default for ExtBuilder {
             voucher_objects_limit_upper_bound: DEFAULT_VOUCHER_SIZE_LIMIT_UPPER_BOUND,
             voucher_size_limit_upper_bound: DEFAULT_VOUCHER_OBJECTS_LIMIT_UPPER_BOUND,
             global_voucher: DEFAULT_GLOBAL_VOUCHER,
+            default_voucher: DEFAULT_VOUCHER,
             first_data_object_type_id: 1,
             first_content_id: 2,
             first_relationship_id: 3,
@@ -330,6 +335,7 @@ impl ExtBuilder {
             voucher_size_limit_upper_bound: self.voucher_size_limit_upper_bound,
             voucher_objects_limit_upper_bound: self.voucher_objects_limit_upper_bound,
             global_voucher: self.global_voucher,
+            default_voucher: self.default_voucher,
             data_object_by_content_id: vec![],
             vouchers: vec![],
             uploading_blocked: self.uploading_blocked,

+ 0 - 1
runtime/src/lib.rs

@@ -465,7 +465,6 @@ impl storage::data_directory::Trait for Runtime {
     type StorageProviderHelper = integration::storage::StorageProviderHelper;
     type IsActiveDataObjectType = DataObjectTypeRegistry;
     type MemberOriginValidator = MembershipOriginValidator<Self>;
-    type DefaultVoucher = DefaultVoucher;
 }
 
 impl storage::data_object_storage_registry::Trait for Runtime {

+ 1 - 0
runtime/src/runtime_api.rs

@@ -92,6 +92,7 @@ impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
             data_directory::DEFAULT_VOUCHER_SIZE_LIMIT_UPPER_BOUND,
             data_directory::DEFAULT_VOUCHER_OBJECTS_LIMIT_UPPER_BOUND,
             data_directory::DEFAULT_GLOBAL_VOUCHER,
+            data_directory::DEFAULT_VOUCHER,
             data_directory::DEFAULT_UPLOADING_BLOCKED_STATUS,
         );