Browse Source

runtime: blog: eliminate reactions

conectado 4 years ago
parent
commit
f7ba53d42b

+ 0 - 37
runtime-modules/blog/src/benchmarking.rs

@@ -285,29 +285,6 @@ benchmarks_instance! {
                 updated_text
             ).into());
     }
-
-    react_to_post {
-        let post_id = generate_post::<T, I>();
-        let (account_id, participant_id) = member_funded_account::<T, I>("caller", 0);
-        let origin = RawOrigin::Signed(account_id);
-    }: react(origin.clone(), participant_id, 0, post_id, None)
-    verify {
-        assert_last_event::<T, I>(
-            RawEvent::PostReactionsUpdated(participant_id, post_id, 0).into()
-        );
-    }
-
-    react_to_reply {
-        let post_id = generate_post::<T, I>();
-        let (account_id, participant_id) = member_funded_account::<T, I>("caller", 0);
-        let reply_id = generate_reply::<T, I>(account_id.clone(), participant_id, post_id.clone());
-        let origin = RawOrigin::Signed(account_id);
-    }: react(origin.clone(), participant_id, 0, post_id, Some(reply_id))
-    verify {
-        assert_last_event::<T, I>(
-            RawEvent::ReplyReactionsUpdated(participant_id, post_id, reply_id, 0).into()
-        );
-    }
 }
 
 #[cfg(test)]
@@ -364,18 +341,4 @@ mod tests {
             assert_ok!(test_benchmark_edit_reply::<Runtime>());
         })
     }
-
-    #[test]
-    fn test_react_to_post() {
-        ExtBuilder::default().build().execute_with(|| {
-            assert_ok!(test_benchmark_react_to_post::<Runtime>());
-        })
-    }
-
-    #[test]
-    fn test_react_to_reply() {
-        ExtBuilder::default().build().execute_with(|| {
-            assert_ok!(test_benchmark_react_to_reply::<Runtime>());
-        })
-    }
 }

+ 2 - 84
runtime-modules/blog/src/lib.rs

@@ -14,19 +14,14 @@
 //! - Creation and editing of posts, associated with given blog
 //! - Posts locking/unlocking
 //! - Creation and editing of replies, associated with given post
-//! - Reactions for both posts and replies
 //!
 //! ### Terminology
 //!
 //! - **Lock:** A forbiddance of mutation of any associated information related to a given post.
 //!
-//! - **Reaction:** A user can react to a post in N different ways, where N is an integer parameter configured through runtime.
-//! For each way, the reader can simply react and unreact to a given post. Think of reactions as being things like, unlike,
-//! laugh, etc. The semantics of each reaction is not present in the runtime.
-//!
 //! ## Interface
 //! The posts creation/edition/locking/unlocking are done through proposals
-//! To reply/react to posts you need to be a member
+//! To reply to posts you need to be a member
 //!
 //! ## Supported extrinsics
 //!
@@ -36,7 +31,6 @@
 //! - [edit_post](./struct.Module.html#method.edit_post)
 //! - [create_reply](./struct.Module.html#method.create_reply)
 //! - [edit_reply](./struct.Module.html#method.create_reply)
-//! - [react](./struct.Module.html#method.create_reply)
 
 #![cfg_attr(not(feature = "std"), no_std)]
 
@@ -64,12 +58,6 @@ type MaxNumber = u64;
 /// Type for post IDs
 pub type PostId = u64;
 
-/// Type, representing reactions number
-pub type ReactionsNumber = u64;
-
-/// Number of reactions, presented in runtime
-pub const REACTIONS_MAX_NUMBER: ReactionsNumber = 5;
-
 /// Blogger participant ID alias for the member of the system.
 pub type ParticipantId<T> = common::MemberId<T>;
 
@@ -83,8 +71,6 @@ pub trait WeightInfo {
     fn create_reply_to_post(t: u32) -> Weight;
     fn create_reply_to_reply(t: u32) -> Weight;
     fn edit_reply(t: u32) -> Weight;
-    fn react_to_post() -> Weight;
-    fn react_to_reply() -> Weight;
 }
 
 // The pallet's configuration trait.
@@ -326,8 +312,6 @@ decl_storage! {
         /// Reply by unique blog, post and reply identificators
         ReplyById get (fn reply_by_id): double_map hasher(blake2_128_concat) PostId, hasher(blake2_128_concat) T::ReplyId => Reply<T, I>;
 
-        /// Mapping, representing AccountId -> All presented reactions state mapping by unique post or reply identificators.
-        pub Reactions get(fn reactions): double_map hasher(blake2_128_concat) (PostId, Option<T::ReplyId>), hasher(blake2_128_concat) ParticipantId<T> => [bool; REACTIONS_MAX_NUMBER as usize];
     }
 }
 
@@ -382,7 +366,7 @@ decl_module! {
         }
 
         /// Blog owner can lock posts, related to a given blog,
-        /// making post immutable to any actions (replies creation, post editing, reactions, etc.)
+        /// making post immutable to any actions (replies creation, post editing, etc.)
         ///
         /// <weight>
         ///
@@ -592,52 +576,6 @@ decl_module! {
             Ok(())
         }
 
-        /// Submit either post reaction or reply reaction
-        /// In case, when you resubmit reaction, it`s status will be changed to an opposite one
-        /// <weight>
-        ///
-        /// ## Weight
-        /// `O (1)` doesn't depends on the state or parameters
-        /// - DB:
-        ///    - O(1) doesn't depend on the state or parameters
-        /// # </weight>
-        #[weight = Module::<T, I>::react_weight()]
-        pub fn react(
-            origin,
-            participant_id: ParticipantId<T>,
-            // reaction index in array
-            index: ReactionsNumber,
-            post_id: PostId,
-            reply_id: Option<T::ReplyId>
-        ) {
-            Self::ensure_valid_participant(origin, participant_id)?;
-
-            // Ensure index is valid & reaction under given index exists
-            Self::ensure_reaction_index_is_valid(index)?;
-
-            // Ensure post with given id exists
-            let post = Self::ensure_post_exists(post_id)?;
-
-            // Ensure post unlocked, so mutations can be performed
-            Self::ensure_post_unlocked(&post)?;
-
-            // Ensure reply with given id exists
-            if let Some(reply_id) = reply_id {
-                Self::ensure_reply_exists(post_id, reply_id)?;
-            }
-
-            //
-            // == MUTATION SAFE ==
-            //
-
-            // Trigger event
-            if let Some(reply_id) = reply_id {
-                Self::deposit_event(RawEvent::ReplyReactionsUpdated(participant_id, post_id, reply_id, index));
-            } else {
-                Self::deposit_event(RawEvent::PostReactionsUpdated(participant_id, post_id, index));
-            }
-        }
-
     }
 }
 
@@ -650,11 +588,6 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
         T::WeightInfo::edit_post(title_len, body_len)
     }
 
-    // calculate react weight
-    fn react_weight() -> Weight {
-        T::WeightInfo::react_to_post().max(T::WeightInfo::react_to_reply())
-    }
-
     // calculate create_reply weight
     fn create_reply_weight(text_len: usize) -> Weight {
         let text_len: u32 = text_len.saturated_into();
@@ -742,14 +675,6 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
 
         Ok(())
     }
-
-    fn ensure_reaction_index_is_valid(index: ReactionsNumber) -> Result<(), DispatchError> {
-        ensure!(
-            index < REACTIONS_MAX_NUMBER,
-            Error::<T, I>::InvalidReactionIndex
-        );
-        Ok(())
-    }
 }
 
 decl_event!(
@@ -758,7 +683,6 @@ decl_event!(
         ParticipantId = ParticipantId<T>,
         PostId = PostId,
         ReplyId = <T as Trait<I>>::ReplyId,
-        ReactionIndex = ReactionsNumber,
         Title = Vec<u8>,
         Text = Vec<u8>,
         UpdatedTitle = Option<Vec<u8>>,
@@ -784,11 +708,5 @@ decl_event!(
 
         /// A reply was edited
         ReplyEdited(ParticipantId, PostId, ReplyId, Text),
-
-        /// A post reaction was created or changed
-        PostReactionsUpdated(ParticipantId, PostId, ReactionIndex),
-
-        /// A reply creation was created or changed
-        ReplyReactionsUpdated(ParticipantId, PostId, ReplyId, ReactionIndex),
     }
 );

+ 0 - 25
runtime-modules/blog/src/mock.rs

@@ -250,12 +250,6 @@ impl WeightInfo for () {
     fn edit_reply(_: u32) -> Weight {
         unimplemented!()
     }
-    fn react_to_post() -> Weight {
-        unimplemented!()
-    }
-    fn react_to_reply() -> Weight {
-        unimplemented!()
-    }
 }
 
 pub struct MockEnsureParticipant;
@@ -326,7 +320,6 @@ type RawTestEvent = RawEvent<
     ParticipantId<Runtime>,
     PostId,
     <Runtime as Trait>::ReplyId,
-    ReactionsNumber,
     Vec<u8>,
     Vec<u8>,
     Option<Vec<u8>>,
@@ -436,21 +429,3 @@ pub fn edit_reply(
         reply,
     )
 }
-
-// Reactions
-
-pub fn react(
-    origin_id: u64,
-    participant_id: u64,
-    index: ReactionsNumber,
-    post_id: PostId,
-    reply_id: Option<<Runtime as Trait>::ReplyId>,
-) -> DispatchResult {
-    TestBlogModule::react(
-        Origin::signed(origin_id),
-        participant_id,
-        index,
-        post_id,
-        reply_id,
-    )
-}

+ 0 - 213
runtime-modules/blog/src/tests.rs

@@ -819,219 +819,6 @@ fn reply_editing_participant_error() {
     })
 }
 
-#[test]
-fn reaction_success() {
-    const REACTION_INDEX: ReactionsNumber = 4;
-
-    ExtBuilder::default().build().execute_with(|| {
-        // Create post for future replies
-        create_post(Origin::root()).unwrap();
-
-        let reaction_owner_id = ensure_signed(Origin::signed(SECOND_OWNER_ORIGIN)).unwrap();
-
-        // Events number before tested call
-        let number_of_events_before_call = System::events().len();
-
-        // React to a post
-        assert_ok!(react(
-            SECOND_OWNER_ORIGIN,
-            SECOND_OWNER_PARTICIPANT_ID,
-            REACTION_INDEX,
-            FIRST_ID,
-            None,
-        ));
-
-        // Event checked
-        let post_reactions_updated_event = get_test_event(RawEvent::PostReactionsUpdated(
-            reaction_owner_id,
-            FIRST_ID,
-            REACTION_INDEX,
-        ));
-
-        assert_event_success(
-            post_reactions_updated_event,
-            number_of_events_before_call + 1,
-        );
-
-        create_reply(
-            FIRST_OWNER_ORIGIN,
-            FIRST_OWNER_PARTICIPANT_ID,
-            FIRST_ID,
-            None,
-        )
-        .unwrap();
-
-        // Events number before tested call
-        let number_of_events_before_call = System::events().len();
-
-        // React to a reply twice to check, that flipping performed
-        for _ in 0..2 {
-            assert_ok!(react(
-                SECOND_OWNER_ORIGIN,
-                SECOND_OWNER_PARTICIPANT_ID,
-                REACTION_INDEX,
-                FIRST_ID,
-                Some(FIRST_ID),
-            ));
-        }
-
-        // Event checked
-        let reply_reactions_updated_event = get_test_event(RawEvent::ReplyReactionsUpdated(
-            reaction_owner_id,
-            FIRST_ID,
-            FIRST_ID,
-            REACTION_INDEX,
-        ));
-        assert_event_success(
-            reply_reactions_updated_event,
-            number_of_events_before_call + 2,
-        )
-    })
-}
-
-#[test]
-fn reaction_invalid_index() {
-    const REACTIONS_MAX_NUMBER: ReactionsNumber = 5;
-
-    ExtBuilder::default().build().execute_with(|| {
-        create_post(Origin::root()).unwrap();
-
-        // Events number before tested call
-        let number_of_events_before_call = System::events().len();
-
-        // React to a post
-        // Should fail, as last index in configured reactions array is less by one than array length
-        let react_result = react(
-            SECOND_OWNER_ORIGIN,
-            SECOND_OWNER_PARTICIPANT_ID,
-            REACTIONS_MAX_NUMBER,
-            FIRST_ID,
-            None,
-        );
-
-        // Failure checked
-        assert_failure(
-            react_result,
-            Error::InvalidReactionIndex,
-            number_of_events_before_call,
-        );
-    })
-}
-
-#[test]
-fn reaction_participant_error() {
-    const REACTIONS_MAX_NUMBER: ReactionsNumber = 5;
-
-    ExtBuilder::default().build().execute_with(|| {
-        create_post(Origin::root()).unwrap();
-
-        // Events number before tested call
-        let number_of_events_before_call = System::events().len();
-
-        // React to a post
-        // Should fail, as last index in configured reactions array is less by one than array length
-        let react_result = react(
-            SECOND_OWNER_ORIGIN,
-            BAD_MEMBER_ID,
-            REACTIONS_MAX_NUMBER,
-            FIRST_ID,
-            None,
-        );
-
-        // Failure checked
-        assert_failure(
-            react_result,
-            Error::MembershipError,
-            number_of_events_before_call,
-        );
-    })
-}
-
-#[test]
-fn reaction_post_not_found() {
-    const REACTION_INDEX: ReactionsNumber = 4;
-
-    ExtBuilder::default().build().execute_with(|| {
-        // Events number before tested call
-        let number_of_events_before_call = System::events().len();
-
-        // React to a post
-        let react_result = react(
-            SECOND_OWNER_ORIGIN,
-            SECOND_OWNER_PARTICIPANT_ID,
-            REACTION_INDEX,
-            FIRST_ID,
-            None,
-        );
-
-        // Failure checked
-        assert_failure(
-            react_result,
-            Error::PostNotFound,
-            number_of_events_before_call,
-        );
-    })
-}
-
-#[test]
-fn reaction_reply_not_found() {
-    const REACTION_INDEX: ReactionsNumber = 4;
-
-    ExtBuilder::default().build().execute_with(|| {
-        create_post(Origin::root()).unwrap();
-
-        // Events number before tested call
-        let number_of_events_before_call = System::events().len();
-
-        // React to a reply
-        let react_result = react(
-            SECOND_OWNER_ORIGIN,
-            SECOND_OWNER_PARTICIPANT_ID,
-            REACTION_INDEX,
-            FIRST_ID,
-            Some(FIRST_ID),
-        );
-
-        // Failure checked
-        assert_failure(
-            react_result,
-            Error::ReplyNotFound,
-            number_of_events_before_call,
-        );
-    })
-}
-
-#[test]
-fn reaction_post_locked_error() {
-    const REACTION_INDEX: ReactionsNumber = 4;
-
-    ExtBuilder::default().build().execute_with(|| {
-        create_post(Origin::root()).unwrap();
-
-        // Lock block to forbid mutations
-        lock_post(Origin::root(), FIRST_ID).unwrap();
-
-        // Events number before tested call
-        let number_of_events_before_call = System::events().len();
-
-        // React to a post
-        let react_result = react(
-            SECOND_OWNER_ORIGIN,
-            SECOND_OWNER_PARTICIPANT_ID,
-            REACTION_INDEX,
-            FIRST_ID,
-            None,
-        );
-
-        // Failure checked
-        assert_failure(
-            react_result,
-            Error::PostLockedError,
-            number_of_events_before_call,
-        );
-    })
-}
-
 fn replies_storage_unchanged(post_id: PostId, reply_id: <Runtime as Trait>::ReplyId) -> bool {
     match post_by_id(post_id) {
         Some(post) if post.replies_count() == 0 && reply_by_id(post_id, reply_id).is_none() => true,

+ 14 - 20
runtime/src/weights/blog.rs

@@ -8,51 +8,45 @@ use frame_support::weights::{constants::RocksDbWeight as DbWeight, Weight};
 pub struct WeightInfo;
 impl blog::WeightInfo for WeightInfo {
     fn create_post(t: u32, b: u32) -> Weight {
-        (180_427_000 as Weight)
-            .saturating_add((34_000 as Weight).saturating_mul(t as Weight))
-            .saturating_add((37_000 as Weight).saturating_mul(b as Weight))
+        (355_638_000 as Weight)
+            .saturating_add((151_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((189_000 as Weight).saturating_mul(b as Weight))
             .saturating_add(DbWeight::get().reads(1 as Weight))
             .saturating_add(DbWeight::get().writes(2 as Weight))
     }
     fn lock_post() -> Weight {
-        (174_515_000 as Weight)
+        (205_726_000 as Weight)
             .saturating_add(DbWeight::get().reads(1 as Weight))
             .saturating_add(DbWeight::get().writes(1 as Weight))
     }
     fn unlock_post() -> Weight {
-        (174_369_000 as Weight)
+        (200_216_000 as Weight)
             .saturating_add(DbWeight::get().reads(1 as Weight))
             .saturating_add(DbWeight::get().writes(1 as Weight))
     }
     fn edit_post(t: u32, b: u32) -> Weight {
-        (333_244_000 as Weight)
-            .saturating_add((38_000 as Weight).saturating_mul(t as Weight))
-            .saturating_add((31_000 as Weight).saturating_mul(b as Weight))
+        (497_924_000 as Weight)
+            .saturating_add((142_000 as Weight).saturating_mul(t as Weight))
+            .saturating_add((197_000 as Weight).saturating_mul(b as Weight))
             .saturating_add(DbWeight::get().reads(1 as Weight))
             .saturating_add(DbWeight::get().writes(1 as Weight))
     }
     fn create_reply_to_post(t: u32) -> Weight {
-        (286_102_000 as Weight)
-            .saturating_add((43_000 as Weight).saturating_mul(t as Weight))
+        (354_299_000 as Weight)
+            .saturating_add((245_000 as Weight).saturating_mul(t as Weight))
             .saturating_add(DbWeight::get().reads(2 as Weight))
             .saturating_add(DbWeight::get().writes(2 as Weight))
     }
     fn create_reply_to_reply(t: u32) -> Weight {
-        (364_116_000 as Weight)
-            .saturating_add((39_000 as Weight).saturating_mul(t as Weight))
+        (407_973_000 as Weight)
+            .saturating_add((245_000 as Weight).saturating_mul(t as Weight))
             .saturating_add(DbWeight::get().reads(3 as Weight))
             .saturating_add(DbWeight::get().writes(2 as Weight))
     }
     fn edit_reply(t: u32) -> Weight {
-        (286_073_000 as Weight)
-            .saturating_add((42_000 as Weight).saturating_mul(t as Weight))
+        (317_997_000 as Weight)
+            .saturating_add((252_000 as Weight).saturating_mul(t as Weight))
             .saturating_add(DbWeight::get().reads(3 as Weight))
             .saturating_add(DbWeight::get().writes(1 as Weight))
     }
-    fn react_to_post() -> Weight {
-        (244_186_000 as Weight).saturating_add(DbWeight::get().reads(2 as Weight))
-    }
-    fn react_to_reply() -> Weight {
-        (311_256_000 as Weight).saturating_add(DbWeight::get().reads(3 as Weight))
-    }
 }