Ver Fonte

Refactor data_directory module

- remove unused code
- move BlockAndTime to the common module
Shamil Gadelshin há 4 anos atrás
pai
commit
2644a2e8a3

+ 3 - 2
Cargo.lock

@@ -4602,13 +4602,14 @@ dependencies = [
 
 [[package]]
 name = "substrate-common-module"
-version = "1.0.0"
+version = "1.0.1"
 dependencies = [
  "parity-scale-codec",
  "serde",
  "sr-primitives",
  "srml-support",
  "srml-system",
+ "srml-timestamp",
 ]
 
 [[package]]
@@ -4828,7 +4829,7 @@ dependencies = [
 
 [[package]]
 name = "substrate-forum-module"
-version = "1.2.0"
+version = "1.2.1"
 dependencies = [
  "hex-literal 0.1.4",
  "parity-scale-codec",

+ 9 - 2
runtime-modules/common/Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = 'substrate-common-module'
-version = '1.0.0'
+version = '1.0.1'
 authors = ['Joystream contributors']
 edition = '2018'
 
@@ -10,6 +10,7 @@ std = [
 	'sr-primitives/std',
 	'srml-support/std',
 	'system/std',
+	'timestamp/std',
 	'codec/std',
 	'serde'
 ]
@@ -42,4 +43,10 @@ version = '1.0.0'
 [dependencies.serde]
 features = ['derive']
 optional = true
-version = '1.0.101'
+version = '1.0.101'
+
+[dependencies.timestamp]
+default_features = false
+git = 'https://github.com/paritytech/substrate.git'
+package = 'srml-timestamp'
+rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'

+ 24 - 0
runtime-modules/common/src/lib.rs

@@ -4,3 +4,27 @@
 pub mod constraints;
 pub mod currency;
 pub mod origin_validator;
+
+use codec::{Decode, Encode};
+#[cfg(feature = "std")]
+use serde::{Deserialize, Serialize};
+
+/// Defines time in both block number and substrate time abstraction.
+#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
+#[derive(Clone, Encode, Decode, PartialEq, Eq, Debug, Default)]
+pub struct BlockAndTime<BlockNumber, Moment> {
+    /// Defines chain block
+    pub block: BlockNumber,
+
+    /// Defines time
+    pub time: Moment,
+}
+
+/// Gathers current block and time information for the runtime
+pub fn current_block_time<T: system::Trait + timestamp::Trait>(
+) -> BlockAndTime<T::BlockNumber, T::Moment> {
+    BlockAndTime {
+        block: <system::Module<T>>::block_number(),
+        time: <timestamp::Module<T>>::now(),
+    }
+}

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

@@ -28,6 +28,7 @@ use srml_support::{decl_event, decl_module, decl_storage, dispatch, ensure, Para
 use system::{self, ensure_root};
 
 use common::origin_validator::ActorOriginValidator;
+pub(crate) use common::BlockAndTime;
 
 use crate::data_object_type_registry;
 use crate::data_object_type_registry::IsActiveDataObjectType;
@@ -64,13 +65,6 @@ static MSG_LIAISON_REQUIRED: &str = "Only the liaison for the content may modify
 static MSG_DO_TYPE_MUST_BE_ACTIVE: &str =
     "Cannot create content for inactive or missing data object type.";
 
-// TODO consider to remove it
-#[derive(Clone, Encode, Decode, PartialEq, Debug)]
-pub struct BlockAndTime<T: Trait> {
-    pub block: T::BlockNumber,
-    pub time: T::Moment,
-}
-
 /// The decision of the storage provider when it acts as liaison.
 #[derive(Clone, Encode, Decode, PartialEq, Debug)]
 pub enum LiaisonJudgement {
@@ -97,7 +91,7 @@ pub struct DataObject<T: Trait> {
     pub owner: MemberId<T>,
 
     /// Content added at.
-    pub added_at: BlockAndTime<T>,
+    pub added_at: BlockAndTime<T::BlockNumber, T::Moment>,
 
     /// Content type id.
     pub type_id: <T as data_object_type_registry::Trait>::DataObjectTypeId,
@@ -115,20 +109,6 @@ pub struct DataObject<T: Trait> {
     pub ipfs_content_id: Vec<u8>,
 }
 
-//TODO consider to remove
-
-#[derive(Clone, Encode, Decode, PartialEq, Debug)]
-pub enum ContentVisibility {
-    Draft,
-    Public,
-}
-
-impl Default for ContentVisibility {
-    fn default() -> Self {
-        ContentVisibility::Draft
-    }
-}
-
 decl_storage! {
     trait Store for Module<T: Trait> as DataDirectory {
         /// List of ids known to the system.
@@ -178,7 +158,7 @@ decl_module! {
         pub fn add_content(
             origin,
             member_id: MemberId<T>,
-            content_id: T::ContentId, // TODO generate content_id by runtime
+            content_id: T::ContentId,
             type_id: <T as data_object_type_registry::Trait>::DataObjectTypeId,
             size: u64,
             ipfs_content_id: Vec<u8>
@@ -200,7 +180,7 @@ decl_module! {
             let data: DataObject<T> = DataObject {
                 type_id,
                 size,
-                added_at: Self::current_block_and_time(),
+                added_at: common::current_block_time::<T>(),
                 owner: member_id,
                 liaison,
                 liaison_judgement: LiaisonJudgement::Pending,
@@ -275,13 +255,6 @@ decl_module! {
 }
 
 impl<T: Trait> Module<T> {
-    fn current_block_and_time() -> BlockAndTime<T> {
-        BlockAndTime {
-            block: <system::Module<T>>::block_number(),
-            time: <timestamp::Module<T>>::now(),
-        }
-    }
-
     fn update_content_judgement(
         storage_provider_id: &StorageProviderId<T>,
         content_id: T::ContentId,