Browse Source

runtime: membership: Add locked balance for an invited member.

Shamil Gadelshin 4 years ago
parent
commit
824db51fae

+ 10 - 3
runtime-modules/membership/src/lib.rs

@@ -47,7 +47,7 @@ pub mod genesis;
 mod tests;
 
 use codec::{Decode, Encode};
-use frame_support::traits::{Currency, Get};
+use frame_support::traits::{Currency, Get, WithdrawReason, WithdrawReasons};
 use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure};
 use frame_system::ensure_root;
 use frame_system::ensure_signed;
@@ -588,12 +588,19 @@ decl_module! {
             let new_wg_budget = current_wg_budget - default_invitation_balance;
             T::WorkingGroup::set_budget(new_wg_budget);
 
-            // Lock invitation balance.
-            T::InvitedMemberStakingHandler::lock(
+            // Create default balance for the invited member.
+            let _ = balances::Module::<T>::deposit_creating(
                 &params.controller_account,
                 default_invitation_balance
             );
 
+            // Lock invitation balance. Allow only transaction payments.
+            T::InvitedMemberStakingHandler::lock_with_reasons(
+                &params.controller_account,
+                default_invitation_balance,
+                WithdrawReasons::except(WithdrawReason::TransactionPayment)
+            );
+
             // Fire the event.
             Self::deposit_event(RawEvent::MemberRegistered(member_id));
         }

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

@@ -496,11 +496,22 @@ fn invite_member_succeeds() {
             vec![bob_member_id]
         );
 
+        let initial_invitation_balance = <Test as Trait>::DefaultInitialInvitationBalance::get();
+        // Working group budget reduced.
         assert_eq!(
-            WORKING_GROUP_BUDGET - <Test as Trait>::DefaultInitialInvitationBalance::get(),
+            WORKING_GROUP_BUDGET - initial_invitation_balance,
             <Test as Trait>::WorkingGroup::get_budget()
         );
 
+        // Invited member account filled.
+        assert_eq!(
+            initial_invitation_balance,
+            Balances::free_balance(&profile.controller_account)
+        );
+
+        // Invited member balance locked.
+        assert_eq!(0, Balances::usable_balance(&profile.controller_account));
+
         EventFixture::assert_last_crate_event(Event::<Test>::MemberRegistered(bob_member_id));
     });
 }

+ 13 - 6
runtime-modules/staking-handler/src/lib.rs

@@ -28,8 +28,12 @@ pub trait LockComparator<Balance> {
 /// and LockIdentifier to lock balance consistently with pallet_staking.
 pub trait StakingHandler<AccountId, Balance, MemberId> {
     /// Locks the specified balance on the account using specific lock identifier.
+    /// It locks for all withdraw reasons.
     fn lock(account_id: &AccountId, amount: Balance);
 
+    /// Locks the specified balance on the account using specific lock identifier.
+    fn lock_with_reasons(account_id: &AccountId, amount: Balance, reasons: WithdrawReasons);
+
     /// Removes the specified lock on the account.
     fn unlock(account_id: &AccountId);
 
@@ -83,12 +87,15 @@ impl<
         account_id: &<T as frame_system::Trait>::AccountId,
         amount: <T as pallet_balances::Trait>::Balance,
     ) {
-        <pallet_balances::Module<T>>::set_lock(
-            LockId::get(),
-            &account_id,
-            amount,
-            WithdrawReasons::all(),
-        )
+        Self::lock_with_reasons(account_id, amount, WithdrawReasons::all())
+    }
+
+    fn lock_with_reasons(
+        account_id: &<T as frame_system::Trait>::AccountId,
+        amount: <T as pallet_balances::Trait>::Balance,
+        reasons: WithdrawReasons,
+    ) {
+        <pallet_balances::Module<T>>::set_lock(LockId::get(), &account_id, amount, reasons)
     }
 
     fn unlock(account_id: &<T as frame_system::Trait>::AccountId) {