Browse Source

storage: add ipfs_content_id field to DataObject

Mokhtar Naamani 5 years ago
parent
commit
57dcb401aa
4 changed files with 50 additions and 15 deletions
  1. 1 1
      src/lib.rs
  2. 21 3
      src/migration.rs
  3. 27 11
      src/storage/data_directory.rs
  4. 1 0
      src/storage/mock.rs

+ 1 - 1
src/lib.rs

@@ -128,7 +128,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
     spec_name: create_runtime_str!("joystream-node"),
     impl_name: create_runtime_str!("joystream-node"),
     authoring_version: 5,
-    spec_version: 3,
+    spec_version: 4,
     impl_version: 0,
     apis: RUNTIME_API_VERSIONS,
 };

+ 21 - 3
src/migration.rs

@@ -1,8 +1,10 @@
 use crate::membership::members;
 use crate::roles::actors;
+use crate::storage;
 use crate::VERSION;
+use rstd::prelude::*;
 use runtime_io::print;
-use srml_support::{decl_event, decl_module, decl_storage, StorageValue};
+use srml_support::{decl_event, decl_module, decl_storage, StorageMap, StorageValue};
 use system;
 
 // When preparing a new major runtime release version bump this value to match it and update
@@ -10,7 +12,7 @@ use system;
 // the runtime doesn't need to maintain any logic for old migrations. All knowledge about state of the chain and runtime
 // prior to the new runtime taking over is implicit in the migration code implementation. If assumptions are incorrect
 // behaviour is undefined.
-const MIGRATION_FOR_SPEC_VERSION: u32 = 2;
+const MIGRATION_FOR_SPEC_VERSION: u32 = 4;
 
 impl<T: Trait> Module<T> {
     fn runtime_initialization() {
@@ -24,6 +26,16 @@ impl<T: Trait> Module<T> {
         // add initialization of other modules introduced in this runtime
         // ...
 
+        // remove all old content
+        for content_id in <storage::data_directory::KnownContentIds<T>>::get().iter() {
+            <storage::data_directory::DataObjectByContentId<T>>::remove(content_id);
+            <storage::data_directory::MetadataByContentId<T>>::remove(content_id);
+            <storage::data_object_storage_registry::RelationshipsByContentId<T>>::remove(
+                content_id,
+            );
+        }
+        <storage::data_directory::KnownContentIds<T>>::put(vec![]);
+
         Self::deposit_event(RawEvent::Migrated(
             <system::Module<T>>::block_number(),
             VERSION.spec_version,
@@ -31,7 +43,13 @@ impl<T: Trait> Module<T> {
     }
 }
 
-pub trait Trait: system::Trait + members::Trait + actors::Trait {
+pub trait Trait:
+    system::Trait
+    + members::Trait
+    + actors::Trait
+    + storage::data_directory::Trait
+    + storage::data_object_storage_registry::Trait
+{
     type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
 }
 

+ 27 - 11
src/storage/data_directory.rs

@@ -69,11 +69,12 @@ pub struct DataObject<T: Trait> {
     pub size: u64,
     pub liaison: T::AccountId,
     pub liaison_judgement: LiaisonJudgement,
-    // TODO signing_key: public key supplied by the uploader,
-    // they sigh the content with this key
+    pub ipfs_content_id: Vec<u8>, // shoule we use rust multi-format crate?
+                                  // TODO signing_key: public key supplied by the uploader,
+                                  // they sigh the content with this key
 
-    // TODO add support for this field (Some if judgment == Rejected)
-    // pub rejection_reason: Option<Vec<u8>>,
+                                  // TODO add support for this field (Some if judgment == Rejected)
+                                  // pub rejection_reason: Option<Vec<u8>>,
 }
 
 #[derive(Clone, Encode, Decode, PartialEq)]
@@ -117,12 +118,12 @@ decl_storage! {
 
         // TODO this list of ids should be moved off-chain once we have Content Indexer.
         // TODO deprecated, moved tp storage relationship
-        KnownContentIds get(known_content_ids): Vec<T::ContentId> = vec![];
+        pub KnownContentIds get(known_content_ids): Vec<T::ContentId> = vec![];
 
-        DataObjectByContentId get(data_object_by_content_id):
+        pub DataObjectByContentId get(data_object_by_content_id):
             map T::ContentId => Option<DataObject<T>>;
 
-        MetadataByContentId get(metadata_by_content_id):
+        pub MetadataByContentId get(metadata_by_content_id):
             map T::ContentId => Option<ContentMetadata<T>>;
 
         // Default storage provider address
@@ -161,7 +162,8 @@ decl_module! {
             origin,
             content_id: T::ContentId,
             type_id: <T as DOTRTrait>::DataObjectTypeId,
-            size: u64
+            size: u64,
+            ipfs_content_id: Vec<u8>
         ) {
             let who = ensure_signed(origin)?;
             ensure!(T::Members::is_active_member(&who), MSG_CREATOR_MUST_BE_MEMBER);
@@ -188,6 +190,7 @@ decl_module! {
                 owner: who.clone(),
                 liaison: liaison,
                 liaison_judgement: LiaisonJudgement::Pending,
+                ipfs_content_id: ipfs_content_id.clone(),
             };
 
             <DataObjectByContentId<T>>::insert(&content_id, data);
@@ -365,7 +368,8 @@ mod tests {
     fn succeed_adding_content() {
         with_default_mock_builder(|| {
             // Register a content with 1234 bytes of type 1, which should be recognized.
-            let res = TestDataDirectory::add_content(Origin::signed(1), 1, 1234, 0);
+            let res =
+                TestDataDirectory::add_content(Origin::signed(1), 1, 1234, 0, vec![1, 3, 3, 7]);
             assert!(res.is_ok());
         });
     }
@@ -374,7 +378,13 @@ mod tests {
     fn accept_content_as_liaison() {
         with_default_mock_builder(|| {
             let sender = 1 as u64;
-            let res = TestDataDirectory::add_content(Origin::signed(sender), 1, 1234, 0);
+            let res = TestDataDirectory::add_content(
+                Origin::signed(sender),
+                1,
+                1234,
+                0,
+                vec![1, 2, 3, 4],
+            );
             assert!(res.is_ok());
 
             // An appropriate event should have been fired.
@@ -403,7 +413,13 @@ mod tests {
     fn reject_content_as_liaison() {
         with_default_mock_builder(|| {
             let sender = 1 as u64;
-            let res = TestDataDirectory::add_content(Origin::signed(sender), 1, 1234, 0);
+            let res = TestDataDirectory::add_content(
+                Origin::signed(sender),
+                1,
+                1234,
+                0,
+                vec![1, 2, 3, 4],
+            );
             assert!(res.is_ok());
 
             // An appropriate event should have been fired.

+ 1 - 0
src/storage/mock.rs

@@ -105,6 +105,7 @@ impl traits::ContentIdExists<Test> for MockContent {
                 owner: 1,
                 liaison: TEST_MOCK_LIAISON,
                 liaison_judgement: data_directory::LiaisonJudgement::Pending,
+                ipfs_content_id: vec![],
             }),
             _ => Err("nope, missing"),
         }