Browse Source

Merge pull request #120 from mnaamani/wg-fix-leave_curator_role-normal-method

content working group: make unstaked() an non dispatchable
Bedeho Mender 5 years ago
parent
commit
562ea39a96
2 changed files with 60 additions and 65 deletions
  1. 53 54
      src/content_working_group/lib.rs
  2. 7 11
      src/content_working_group/tests.rs

+ 53 - 54
src/content_working_group/lib.rs

@@ -1936,60 +1936,6 @@ decl_module! {
             Self::deposit_event(RawEvent::LeadUnset(lead_id));
         }
 
-        /// The stake, with the given id, was unstaked.
-        pub fn unstaked(
-            origin,
-            stake_id: StakeId<T>
-        ) {
-            // Ensure its a runtime root origin
-            ensure_root(origin)?;
-
-            // Ensure its an unstaker in this group
-            let unstaker = Self::ensure_unstaker_exists(&stake_id)?;
-
-            // Get curator doing the unstaking,
-            // urrently the only possible unstaker in this module.
-            let curator_id =
-                if let WorkingGroupUnstaker::Curator(curator_id) = unstaker {
-                    curator_id
-                } else {
-                    panic!("Should not be possible, only curators unstake in this module currently");
-                };
-
-            // Grab curator from id, unwrap, because this curator _must_ exist.
-            let unstaking_curator = Self::ensure_curator_exists(&curator_id).unwrap();
-
-            //
-            // == MUTATION SAFE ==
-            //
-
-            // Update stage of curator
-            let curator_exit_summary =
-                if let CuratorRoleStage::Unstaking(summary) = unstaking_curator.stage {
-                    summary
-                } else {
-                    panic!("Curator must be in unstaking stage");
-                };
-
-            let new_curator = Curator{
-                stage: CuratorRoleStage::Exited(curator_exit_summary.clone()),
-                ..unstaking_curator
-            };
-
-            CuratorById::<T>::insert(curator_id, new_curator);
-
-            // Remove from unstaker
-            UnstakerByStakeId::<T>::remove(stake_id);
-
-            // Trigger event
-            let event = match curator_exit_summary.origin {
-                CuratorExitInitiationOrigin::Lead => RawEvent::TerminatedCurator(curator_id),
-                CuratorExitInitiationOrigin::Curator => RawEvent::CuratorExited(curator_id),
-            };
-
-            Self::deposit_event(event);
-        }
-
         /// Add an opening for a curator role.
         pub fn set_channel_creation_enabled(origin, enabled: bool)  {
 
@@ -2679,4 +2625,57 @@ impl<T: Trait> Module<T> {
         // Trigger event
         Self::deposit_event(RawEvent::ChannelUpdatedByCurationActor(*channel_id));
     }
+
+    /// The stake, with the given id, was unstaked. Infalliable. Has no side effects if stake_id is not relevant
+    /// to this module.
+    pub fn unstaked(stake_id: StakeId<T>) {
+        // Ignore if unstaked doesn't exist
+        if !<UnstakerByStakeId<T>>::exists(stake_id) {
+            return;
+        }
+
+        // Unstaker must be in this group
+        let unstaker = Self::ensure_unstaker_exists(&stake_id).unwrap();
+
+        // Get curator doing the unstaking,
+        // urrently the only possible unstaker in this module.
+        let curator_id = if let WorkingGroupUnstaker::Curator(curator_id) = unstaker {
+            curator_id
+        } else {
+            panic!("Should not be possible, only curators unstake in this module currently.");
+        };
+
+        // Grab curator from id, unwrap, because this curator _must_ exist.
+        let unstaking_curator = Self::ensure_curator_exists(&curator_id).unwrap();
+
+        //
+        // == MUTATION SAFE ==
+        //
+
+        // Update stage of curator
+        let curator_exit_summary =
+            if let CuratorRoleStage::Unstaking(summary) = unstaking_curator.stage {
+                summary
+            } else {
+                panic!("Curator must be in unstaking stage.");
+            };
+
+        let new_curator = Curator {
+            stage: CuratorRoleStage::Exited(curator_exit_summary.clone()),
+            ..unstaking_curator
+        };
+
+        CuratorById::<T>::insert(curator_id, new_curator);
+
+        // Remove from unstaker
+        UnstakerByStakeId::<T>::remove(stake_id);
+
+        // Trigger event
+        let event = match curator_exit_summary.origin {
+            CuratorExitInitiationOrigin::Lead => RawEvent::TerminatedCurator(curator_id),
+            CuratorExitInitiationOrigin::Curator => RawEvent::CuratorExited(curator_id),
+        };
+
+        Self::deposit_event(event);
+    }
 }

+ 7 - 11
src/content_working_group/tests.rs

@@ -1159,13 +1159,12 @@ fn unset_lead_success() {
 }
 
 struct UnstakedFixture {
-    pub origin: Origin,
     pub stake_id: StakeId<Test>,
 }
 
 impl UnstakedFixture {
-    fn call(&self) -> Result<(), &'static str> {
-        ContentWorkingGroup::unstaked(self.origin.clone(), self.stake_id)
+    fn call(&self) {
+        ContentWorkingGroup::unstaked(self.stake_id);
     }
 
     pub fn call_and_assert_success(&self) {
@@ -1186,9 +1185,7 @@ impl UnstakedFixture {
                 panic!("Curator not unstaking")
             };
 
-        let call_result = self.call();
-
-        assert_eq!(call_result, Ok(()));
+        self.call();
 
         let expected_curator = Curator {
             stage: CuratorRoleStage::Exited(original_exit_summary),
@@ -1208,11 +1205,11 @@ impl UnstakedFixture {
         assert!(!UnstakerByStakeId::<Test>::exists(self.stake_id));
     }
 
-    pub fn call_and_assert_failed_result(&self, error_message: &'static str) {
-        let call_result = self.call();
+    // pub fn call_and_assert_failed_result(&self, error_message: &'static str) {
+    //     let call_result = self.call();
 
-        assert_eq!(call_result, Err(error_message));
-    }
+    //     assert_eq!(call_result, Err(error_message));
+    // }
 }
 
 #[test]
@@ -1235,7 +1232,6 @@ fn unstaked_curator_success() {
                 .stake_id;
 
             UnstakedFixture {
-                origin: Origin::system(system::RawOrigin::Root),
                 stake_id: curator_role_stake_id,
             }
             .call_and_assert_success();