Browse Source

runtime: membership: Change staking account workflow.

Shamil Gadelshin 4 years ago
parent
commit
5a2fe475e3

+ 17 - 22
runtime-modules/membership/src/lib.rs

@@ -346,9 +346,9 @@ decl_event! {
         InitialInvitationBalanceUpdated(Balance),
         LeaderInvitationQuotaUpdated(u32),
         InitialInvitationCountUpdated(u32),
-        StakingAccountAdded(MemberId, AccountId),
-        StakingAccountRemoved(MemberId, AccountId),
-        StakingAccountConfirmed(MemberId, AccountId),
+        StakingAccountAdded(AccountId, MemberId),
+        StakingAccountRemoved(AccountId, MemberId),
+        StakingAccountConfirmed(AccountId, MemberId),
     }
 }
 
@@ -714,20 +714,18 @@ decl_module! {
         }
 
         /// Add staking account candidate for a member.
-        /// The staking account candidate must be confirmed by its owner before usage.
+        /// The membership must be confirmed before usage.
         #[weight = 10_000_000] // TODO: adjust weight
-        pub fn add_staking_account_candidate(
-            origin,
-            member_id: T::MemberId,
-            staking_account_id: T::AccountId
-        ) {
-            Self::ensure_member_controller_account_signed(origin, &member_id)?;
+        pub fn add_staking_account_candidate(origin, member_id: T::MemberId) {
+            let staking_account_id = ensure_signed(origin)?;
 
             ensure!(
                 !Self::staking_account_registered(&staking_account_id),
                 Error::<T>::StakingAccountIsAlreadyRegistered
             );
 
+            Self::ensure_membership(member_id)?;
+
             //
             // == MUTATION SAFE ==
             //
@@ -740,17 +738,15 @@ decl_module! {
                 }
             );
 
-            Self::deposit_event(RawEvent::StakingAccountAdded(member_id, staking_account_id));
+            Self::deposit_event(RawEvent::StakingAccountAdded(staking_account_id, member_id));
         }
 
         /// Remove staking account for a member.
         #[weight = 10_000_000] // TODO: adjust weight
-        pub fn remove_staking_account(
-            origin,
-            member_id: T::MemberId,
-            staking_account_id: T::AccountId
-        ) {
-            Self::ensure_member_controller_account_signed(origin, &member_id)?;
+        pub fn remove_staking_account(origin, member_id: T::MemberId) {
+            let staking_account_id = ensure_signed(origin)?;
+
+            Self::ensure_membership(member_id)?;
 
             ensure!(
                 Self::staking_account_registered_for_member(&staking_account_id, &member_id),
@@ -763,7 +759,7 @@ decl_module! {
 
             <StakingAccountIdMemberStatus<T>>::remove(staking_account_id.clone());
 
-            Self::deposit_event(RawEvent::StakingAccountRemoved(member_id, staking_account_id));
+            Self::deposit_event(RawEvent::StakingAccountRemoved(staking_account_id, member_id));
         }
 
         /// Confirm staking account candidate for a member.
@@ -771,10 +767,9 @@ decl_module! {
         pub fn confirm_staking_account(
             origin,
             member_id: T::MemberId,
+            staking_account_id: T::AccountId,
         ) {
-            let staking_account_id = ensure_signed(origin)?;
-
-            Self::ensure_membership(member_id)?;
+            Self::ensure_member_controller_account_signed(origin, &member_id)?;
 
             ensure!(
                 Self::staking_account_registered_for_member(&staking_account_id, &member_id),
@@ -798,7 +793,7 @@ decl_module! {
                 }
             );
 
-            Self::deposit_event(RawEvent::StakingAccountConfirmed(member_id, staking_account_id));
+            Self::deposit_event(RawEvent::StakingAccountConfirmed(staking_account_id, member_id));
         }
     }
 }

+ 15 - 16
runtime-modules/membership/src/tests/fixtures.rs

@@ -565,18 +565,15 @@ impl Default for AddStakingAccountFixture {
         Self {
             origin: RawOrigin::Signed(ALICE_ACCOUNT_ID),
             member_id: ALICE_MEMBER_ID,
-            staking_account_id: BOB_ACCOUNT_ID,
+            staking_account_id: ALICE_ACCOUNT_ID,
         }
     }
 }
 
 impl AddStakingAccountFixture {
     pub fn call_and_assert(&self, expected_result: DispatchResult) {
-        let actual_result = Membership::add_staking_account_candidate(
-            self.origin.clone().into(),
-            self.member_id,
-            self.staking_account_id,
-        );
+        let actual_result =
+            Membership::add_staking_account_candidate(self.origin.clone().into(), self.member_id);
 
         assert_eq!(expected_result, actual_result);
 
@@ -607,18 +604,15 @@ impl Default for RemoveStakingAccountFixture {
         Self {
             origin: RawOrigin::Signed(ALICE_ACCOUNT_ID),
             member_id: ALICE_MEMBER_ID,
-            staking_account_id: BOB_ACCOUNT_ID,
+            staking_account_id: ALICE_ACCOUNT_ID,
         }
     }
 }
 
 impl RemoveStakingAccountFixture {
     pub fn call_and_assert(&self, expected_result: DispatchResult) {
-        let actual_result = Membership::remove_staking_account(
-            self.origin.clone().into(),
-            self.member_id,
-            self.staking_account_id,
-        );
+        let actual_result =
+            Membership::remove_staking_account(self.origin.clone().into(), self.member_id);
 
         assert_eq!(expected_result, actual_result);
 
@@ -641,26 +635,31 @@ impl RemoveStakingAccountFixture {
 pub struct ConfirmStakingAccountFixture {
     pub origin: RawOrigin<u64>,
     pub member_id: u64,
+    pub staking_account_id: u64,
 }
 
 impl Default for ConfirmStakingAccountFixture {
     fn default() -> Self {
         Self {
-            origin: RawOrigin::Signed(BOB_ACCOUNT_ID),
+            origin: RawOrigin::Signed(ALICE_ACCOUNT_ID),
             member_id: ALICE_MEMBER_ID,
+            staking_account_id: ALICE_ACCOUNT_ID,
         }
     }
 }
 
 impl ConfirmStakingAccountFixture {
     pub fn call_and_assert(&self, expected_result: DispatchResult) {
-        let actual_result =
-            Membership::confirm_staking_account(self.origin.clone().into(), self.member_id);
+        let actual_result = Membership::confirm_staking_account(
+            self.origin.clone().into(),
+            self.member_id,
+            self.staking_account_id,
+        );
 
         assert_eq!(expected_result, actual_result);
 
         if actual_result.is_ok() {
-            assert!(<crate::StakingAccountIdMemberStatus<Test>>::get(&BOB_ACCOUNT_ID,).confirmed);
+            assert!(<crate::StakingAccountIdMemberStatus<Test>>::get(&ALICE_ACCOUNT_ID,).confirmed);
         }
     }
 

+ 10 - 12
runtime-modules/membership/src/tests/mod.rs

@@ -747,8 +747,8 @@ fn add_staking_account_candidate_succeeds() {
         AddStakingAccountFixture::default().call_and_assert(Ok(()));
 
         EventFixture::assert_last_crate_event(Event::<Test>::StakingAccountAdded(
+            ALICE_ACCOUNT_ID,
             ALICE_MEMBER_ID,
-            BOB_ACCOUNT_ID,
         ));
     });
 }
@@ -758,7 +758,7 @@ fn add_staking_account_candidate_fails_with_bad_origin() {
     build_test_externalities().execute_with(|| {
         AddStakingAccountFixture::default()
             .with_origin(RawOrigin::None)
-            .call_and_assert(Err(Error::<Test>::UnsignedOrigin.into()));
+            .call_and_assert(Err(DispatchError::BadOrigin));
     });
 }
 
@@ -802,8 +802,8 @@ fn remove_staking_account_succeeds() {
         RemoveStakingAccountFixture::default().call_and_assert(Ok(()));
 
         EventFixture::assert_last_crate_event(Event::<Test>::StakingAccountRemoved(
+            ALICE_ACCOUNT_ID,
             ALICE_MEMBER_ID,
-            BOB_ACCOUNT_ID,
         ));
     });
 }
@@ -813,7 +813,7 @@ fn remove_staking_account_fails_with_bad_origin() {
     build_test_externalities().execute_with(|| {
         RemoveStakingAccountFixture::default()
             .with_origin(RawOrigin::None)
-            .call_and_assert(Err(Error::<Test>::UnsignedOrigin.into()));
+            .call_and_assert(Err(DispatchError::BadOrigin));
     });
 }
 
@@ -854,11 +854,9 @@ fn confirm_staking_account_succeeds() {
 
         ConfirmStakingAccountFixture::default().call_and_assert(Ok(()));
 
-        assert!(<crate::StakingAccountIdMemberStatus<Test>>::get(&BOB_ACCOUNT_ID,).confirmed);
-
         EventFixture::assert_last_crate_event(Event::<Test>::StakingAccountConfirmed(
+            ALICE_ACCOUNT_ID,
             ALICE_MEMBER_ID,
-            BOB_ACCOUNT_ID,
         ));
     });
 }
@@ -881,7 +879,7 @@ fn confirm_staking_account_fails_with_bad_origin() {
     build_test_externalities().execute_with(|| {
         ConfirmStakingAccountFixture::default()
             .with_origin(RawOrigin::None)
-            .call_and_assert(Err(DispatchError::BadOrigin));
+            .call_and_assert(Err(Error::<Test>::UnsignedOrigin.into()));
     });
 }
 
@@ -917,28 +915,28 @@ fn is_member_staking_account_works() {
     build_test_externalities_with_initial_members(initial_members.to_vec()).execute_with(|| {
         // Before adding candidate should be false.
         assert_eq!(
-            Membership::is_member_staking_account(&ALICE_MEMBER_ID, &BOB_ACCOUNT_ID),
+            Membership::is_member_staking_account(&ALICE_MEMBER_ID, &ALICE_ACCOUNT_ID),
             false
         );
         AddStakingAccountFixture::default().call_and_assert(Ok(()));
 
         // After adding but before confirmation of the candidate should be false.
         assert_eq!(
-            Membership::is_member_staking_account(&ALICE_MEMBER_ID, &BOB_ACCOUNT_ID),
+            Membership::is_member_staking_account(&ALICE_MEMBER_ID, &ALICE_ACCOUNT_ID),
             false
         );
         ConfirmStakingAccountFixture::default().call_and_assert(Ok(()));
 
         // After confirmation of the candidate should be true.
         assert_eq!(
-            Membership::is_member_staking_account(&ALICE_MEMBER_ID, &BOB_ACCOUNT_ID),
+            Membership::is_member_staking_account(&ALICE_MEMBER_ID, &ALICE_ACCOUNT_ID),
             true
         );
 
         // After removing of the staking account should be false.
         RemoveStakingAccountFixture::default().call_and_assert(Ok(()));
         assert_eq!(
-            Membership::is_member_staking_account(&ALICE_MEMBER_ID, &BOB_ACCOUNT_ID),
+            Membership::is_member_staking_account(&ALICE_MEMBER_ID, &ALICE_ACCOUNT_ID),
             false
         );
     });

+ 2 - 2
runtime/src/tests/proposals_integration/mod.rs

@@ -501,13 +501,13 @@ pub fn add_confirmed_staking_account(member_id: MemberId, account_id: AccountId3
     assert!(crate::Members::add_staking_account_candidate(
         RawOrigin::Signed(account_id.clone()).into(),
         member_id,
-        account_id.clone(),
     )
     .is_ok());
 
     assert!(crate::Members::confirm_staking_account(
-        RawOrigin::Signed(account_id).into(),
+        RawOrigin::Signed(account_id.clone()).into(),
         member_id,
+        account_id,
     )
     .is_ok());
 }