Browse Source

Merge pull request #1901 from ondratra/council_reward_account

council - restriction on reward_account_id membership lifted
Bedeho Mender 4 years ago
parent
commit
6ae4885ef6

+ 1 - 6
runtime-modules/council/src/lib.rs

@@ -436,7 +436,7 @@ decl_module! {
         #[weight = 10_000_000]
         pub fn announce_candidacy(origin, membership_id: T::MembershipId, staking_account_id: T::AccountId, reward_account_id: T::AccountId, stake: Balance<T>) -> Result<(), Error<T>> {
             // ensure action can be started
-            let (stage_data, previous_staking_account_id) = EnsureChecks::<T>::can_announce_candidacy(origin, &membership_id, &staking_account_id, &reward_account_id, &stake)?;
+            let (stage_data, previous_staking_account_id) = EnsureChecks::<T>::can_announce_candidacy(origin, &membership_id, &staking_account_id, &stake)?;
 
             // prepare candidate
             let candidate = Self::prepare_new_candidate(staking_account_id, reward_account_id, stake);
@@ -1074,7 +1074,6 @@ impl<T: Trait> EnsureChecks<T> {
         origin: T::Origin,
         membership_id: &T::MembershipId,
         staking_account_id: &T::AccountId,
-        reward_account_id: &T::AccountId,
         stake: &Balance<T>,
     ) -> Result<(CouncilStageAnnouncing, Option<T::AccountId>), Error<T>> {
         // ensure user's membership
@@ -1090,10 +1089,6 @@ impl<T: Trait> EnsureChecks<T> {
             return Err(Error::ConflictingStake);
         }
 
-        if !T::is_council_member_account(&membership_id, &reward_account_id) {
-            return Err(Error::MembershipIdNotMatchAccount);
-        }
-
         let stage_data = match Stage::<T>::get().stage {
             CouncilStage::Announcing(stage_data) => stage_data,
             _ => return Err(Error::CantCandidateNow),

+ 21 - 2
runtime-modules/council/src/mock.rs

@@ -694,14 +694,33 @@ where
         member_id: T::MembershipId,
         stake: Balance<T>,
         expected_result: Result<(), Error<T>>,
+    ) {
+        // use member id as staking and reward accounts
+        Self::announce_candidacy_raw(
+            origin,
+            member_id,
+            member_id.into(),
+            member_id.into(),
+            stake,
+            expected_result,
+        );
+    }
+
+    pub fn announce_candidacy_raw(
+        origin: OriginType<T::AccountId>,
+        member_id: T::MembershipId,
+        staking_account_id: T::AccountId,
+        reward_account_id: T::AccountId,
+        stake: Balance<T>,
+        expected_result: Result<(), Error<T>>,
     ) {
         // check method returns expected result
         assert_eq!(
             Module::<T>::announce_candidacy(
                 InstanceMockUtils::<T>::mock_origin(origin),
                 member_id,
-                member_id.into(), // use member id as staking account
-                member_id.into(), // use member id as reward account
+                staking_account_id,
+                reward_account_id,
                 stake
             ),
             expected_result,

+ 45 - 0
runtime-modules/council/src/tests.rs

@@ -1288,3 +1288,48 @@ fn council_budget_auto_refill() {
         );
     });
 }
+
+/// Test that `staking_account_id` is required to be associated with `membership_id` while `reward_account_id` is not
+#[test]
+fn council_membership_checks() {
+    let config = default_genesis_config();
+
+    build_test_externalities(config).execute_with(|| {
+        let council_settings = CouncilSettings::<Runtime>::extract_settings();
+
+        // generate candidates
+        let candidate1 = MockUtils::generate_candidate(0, council_settings.min_candidate_stake);
+        let candidate2 = MockUtils::generate_candidate(1, council_settings.min_candidate_stake);
+
+        // sanity checks
+        assert_ne!(candidate1.membership_id, candidate2.membership_id,);
+        assert_ne!(
+            candidate1.candidate.reward_account_id,
+            candidate2.candidate.reward_account_id,
+        );
+        assert_ne!(
+            candidate1.candidate.staking_account_id,
+            candidate2.candidate.staking_account_id,
+        );
+
+        // test that staking_account_id has to be associated with membership_id
+        Mocks::announce_candidacy_raw(
+            candidate1.origin.clone(),
+            candidate1.account_id.clone(),
+            candidate2.candidate.staking_account_id.clone(), // second candidate's account id
+            candidate1.candidate.reward_account_id.clone(),
+            candidate1.candidate.stake.clone(),
+            Err(Error::MembershipIdNotMatchAccount),
+        );
+
+        // test that reward_account_id not associated with membership_id can be used
+        Mocks::announce_candidacy_raw(
+            candidate1.origin.clone(),
+            candidate1.account_id.clone(),
+            candidate1.candidate.staking_account_id.clone(),
+            candidate2.candidate.reward_account_id.clone(), // second candidate's account id
+            candidate1.candidate.stake.clone(),
+            Ok(()),
+        );
+    });
+}