From e5b640911ea6097a88cb266effff9f1ef25e151e Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Thu, 8 Dec 2022 11:53:18 +0800 Subject: [PATCH] Release collator staking restriction (#114) --- pallet/staking/src/lib.rs | 36 +++++++++++++---------------------- pallet/staking/tests/tests.rs | 2 ++ 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/pallet/staking/src/lib.rs b/pallet/staking/src/lib.rs index e095e9e11..039c90602 100644 --- a/pallet/staking/src/lib.rs +++ b/pallet/staking/src/lib.rs @@ -148,10 +148,8 @@ where pub struct Exposure { /// The total power backing this collator. pub total: Power, - /// Collator's self stake power. - pub own: Power, /// Nominators' stake power. - pub others: Vec>, + pub nominators: Vec>, } /// A snapshot of the staker's state. #[derive(Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)] @@ -459,7 +457,6 @@ pub mod pallet { pub fn collect(origin: OriginFor, commission: Perbill) -> DispatchResult { let who = ensure_signed(origin)?; - Self::ensure_staker(&who)?; >::mutate(&who, |c| *c = Some(commission)); // TODO: event? @@ -474,8 +471,9 @@ pub mod pallet { pub fn nominate(origin: OriginFor, target: T::AccountId) -> DispatchResult { let who = ensure_signed(origin)?; - Self::ensure_staker(&who)?; - + if !>::contains_key(&who) { + Err(>::NotStaker)? + } if !>::contains_key(&target) { Err(>::TargetNotCollator)?; } @@ -680,14 +678,6 @@ pub mod pallet { }); } - fn ensure_staker(who: &T::AccountId) -> DispatchResult { - if >::contains_key(who) { - Ok(()) - } else { - Err(>::NotStaker)? - } - } - /// Add reward points to collators using their account id. pub fn reward_by_ids(collators: &[(T::AccountId, RewardPoint)]) { >::mutate(|(total, reward_map)| { @@ -765,16 +755,17 @@ pub mod pallet { continue; }; - let c_payout = c_commission_payout - + Perbill::from_rational(c_exposure.own, c_exposure.total) * n_payout; - if let Ok(_i) = T::RingCurrency::deposit_into_existing(&c, c_payout) { - actual_payout += c_payout; + if let Ok(_i) = T::RingCurrency::deposit_into_existing(&c, c_commission_payout) { + actual_payout += c_commission_payout; - Self::deposit_event(Event::::Payout { staker: c, ring_amount: c_payout }); + Self::deposit_event(Event::::Payout { + staker: c, + ring_amount: c_commission_payout, + }); } - for n_exposure in c_exposure.others { + for n_exposure in c_exposure.nominators { let n_payout = Perbill::from_rational(n_exposure.value, c_exposure.total) * n_payout; @@ -807,8 +798,7 @@ pub mod pallet { pub fn elect() -> Vec { let mut collators = >::iter_keys() .map(|c| { - let c_power = Self::power_of(&c); - let mut t_power = c_power; + let mut t_power = 0; let i_exposures = >::iter() .filter_map(|(n, c_)| { if c_ == c { @@ -823,7 +813,7 @@ pub mod pallet { }) .collect(); - ((c, Exposure { total: t_power, own: c_power, others: i_exposures }), t_power) + ((c, Exposure { total: t_power, nominators: i_exposures }), t_power) }) .collect::>(); diff --git a/pallet/staking/tests/tests.rs b/pallet/staking/tests/tests.rs index 917857398..7c3bbf958 100644 --- a/pallet/staking/tests/tests.rs +++ b/pallet/staking/tests/tests.rs @@ -393,6 +393,7 @@ fn elect_should_work() { Vec::new() )); assert_ok!(Staking::collect(RuntimeOrigin::signed(i), Default::default())); + assert_ok!(Staking::nominate(RuntimeOrigin::signed(i), i)); }); (6..=10).for_each(|i| { assert_ok!(Staking::stake( @@ -441,6 +442,7 @@ fn payout_should_work() { Vec::new() )); assert_ok!(Staking::collect(RuntimeOrigin::signed(i), Perbill::from_percent(i * 10))); + assert_ok!(Staking::nominate(RuntimeOrigin::signed(i), i)); }); (6..=10).for_each(|i| { assert_ok!(Staking::stake(