Procházet zdrojové kódy

Storage: Remove Option from DataByContentId representation

iorveth před 4 roky
rodič
revize
4cf40efc6d

+ 2 - 3
node/src/chain_spec/content_config.rs

@@ -1,8 +1,7 @@
 use codec::Decode;
 use node_runtime::{
-    common::storage::StorageObjectOwner,
-    data_directory::*,
-    ChannelId, ContentId, DAOId, DataDirectoryConfig, MemberId, Runtime,
+    common::storage::StorageObjectOwner, data_directory::*, ChannelId, ContentId, DAOId,
+    DataDirectoryConfig, MemberId, Runtime,
 };
 use serde::Deserialize;
 use std::{fs, path::Path};

+ 6 - 0
runtime-modules/common/src/storage.rs

@@ -24,6 +24,12 @@ pub enum StorageObjectOwner<MemberId, ChannelId, DAOId> {
     WorkingGroup(WorkingGroup), // acts through new extrinsic in working group
 }
 
+impl <MemberId, ChannelId, DAOId> Default for StorageObjectOwner<MemberId, ChannelId, DAOId> {
+    fn default() -> Self {
+        Self::Council
+    }
+}
+
 // To be implemented by current storage data_directory runtime module.
 // Defined in 'common' package
 pub trait StorageSystem<T: crate::StorageOwnership + crate::MembershipTypes> {

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

@@ -151,7 +151,7 @@ pub type DataObject<T> = DataObjectInternal<
 
 /// Manages content ids, type and storage provider decision about it.
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
-#[derive(Clone, Encode, Decode, PartialEq, Debug)]
+#[derive(Clone, Encode, Decode, PartialEq, Debug, Default)]
 pub struct DataObjectInternal<
     MemberId,
     ChannelId,
@@ -253,7 +253,7 @@ decl_storage! {
 
         /// Maps data objects by their content id.
         pub DataByContentId get(fn data_object_by_content_id) config():
-            map hasher(blake2_128_concat) T::ContentId => Option<DataObject<T>>;
+            map hasher(blake2_128_concat) T::ContentId => DataObject<T>;
 
         /// Maps storage owner to it`s quota. Created when the first upload by the new actor occured.
         pub Quotas get(fn quotas) config():
@@ -587,8 +587,7 @@ impl<T: Trait> Module<T> {
     ) -> Result<Vec<DataObject<T>>, Error<T>> {
         let mut content = Vec::new();
         for content_id in content_ids {
-            let data_object =
-                Self::data_object_by_content_id(content_id).ok_or(Error::<T>::CidNotFound)?;
+            let data_object = Self::get_data_object(content_id)?;
             ensure!(data_object.owner == *owner, Error::<T>::OwnersAreNotEqual);
             content.push(data_object);
         }
@@ -698,8 +697,7 @@ impl<T: Trait> Module<T> {
         content_id: T::ContentId,
         judgement: LiaisonJudgement,
     ) -> DispatchResult {
-        let mut data =
-            Self::data_object_by_content_id(&content_id).ok_or(Error::<T>::CidNotFound)?;
+        let mut data = Self::get_data_object(&content_id)?;
 
         // Make sure the liaison matches
         ensure!(
@@ -726,18 +724,19 @@ pub trait ContentIdExists<T: Trait> {
     fn has_content(id: &T::ContentId) -> bool;
 
     /// Returns the data object for the provided content id.
-    fn get_data_object(id: &T::ContentId) -> Result<DataObject<T>, &'static str>;
+    fn get_data_object(id: &T::ContentId) -> Result<DataObject<T>, Error<T>>;
 }
 
 impl<T: Trait> ContentIdExists<T> for Module<T> {
     fn has_content(content_id: &T::ContentId) -> bool {
-        Self::data_object_by_content_id(*content_id).is_some()
+        <DataByContentId<T>>::contains_key(content_id)
     }
 
-    fn get_data_object(content_id: &T::ContentId) -> Result<DataObject<T>, &'static str> {
-        match Self::data_object_by_content_id(*content_id) {
-            Some(data) => Ok(data),
-            None => Err(Error::<T>::LiaisonRequired.into()),
+    fn get_data_object(content_id: &T::ContentId) -> Result<DataObject<T>, Error<T>> {
+        if Self::has_content(content_id) {
+            Ok(Self::data_object_by_content_id(*content_id))
+        } else {
+            Err(Error::<T>::CidNotFound.into())
         }
     }
 }

+ 5 - 7
runtime/src/runtime_api.rs

@@ -59,13 +59,11 @@ pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<AccountId, Call, Signa
 //     frame_executive::Executive<Runtime, Block, system::ChainContext<Runtime>, Runtime, AllModules>;
 
 // Alias for the builder working group
-pub(crate) type BuilderWorkingGroup<T> =
-    working_group::Module<T, BuilderWorkingGroupInstance>;
+pub(crate) type BuilderWorkingGroup<T> = working_group::Module<T, BuilderWorkingGroupInstance>;
 
 // Alias for the gateway working group
-pub(crate) type GatewayWorkingGroup<T> =
-    working_group::Module<T, GatewayWorkingGroupInstance>;
-    
+pub(crate) type GatewayWorkingGroup<T> = working_group::Module<T, GatewayWorkingGroupInstance>;
+
 /// Custom runtime upgrade handler.
 pub struct CustomOnRuntimeUpgrade;
 impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
@@ -81,7 +79,7 @@ impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
             default_text_constraint,
             default_content_working_group_mint_capacity,
         );
-        
+
         GatewayWorkingGroup::<Runtime>::initialize_working_group(
             default_text_constraint,
             default_text_constraint,
@@ -94,7 +92,7 @@ impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
             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
+            data_directory::DEFAULT_UPLOADING_BLOCKED_STATUS,
         );
 
         // TODO: storage / data_directory migration or clear all data objects