Skip to content

Commit e5b6409

Browse files
authored
Release collator staking restriction (#114)
1 parent 97ff5cc commit e5b6409

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

pallet/staking/src/lib.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,8 @@ where
148148
pub struct Exposure<AccountId> {
149149
/// The total power backing this collator.
150150
pub total: Power,
151-
/// Collator's self stake power.
152-
pub own: Power,
153151
/// Nominators' stake power.
154-
pub others: Vec<IndividualExposure<AccountId>>,
152+
pub nominators: Vec<IndividualExposure<AccountId>>,
155153
}
156154
/// A snapshot of the staker's state.
157155
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]
@@ -459,7 +457,6 @@ pub mod pallet {
459457
pub fn collect(origin: OriginFor<T>, commission: Perbill) -> DispatchResult {
460458
let who = ensure_signed(origin)?;
461459

462-
Self::ensure_staker(&who)?;
463460
<Collators<T>>::mutate(&who, |c| *c = Some(commission));
464461

465462
// TODO: event?
@@ -474,8 +471,9 @@ pub mod pallet {
474471
pub fn nominate(origin: OriginFor<T>, target: T::AccountId) -> DispatchResult {
475472
let who = ensure_signed(origin)?;
476473

477-
Self::ensure_staker(&who)?;
478-
474+
if !<Ledgers<T>>::contains_key(&who) {
475+
Err(<Error<T>>::NotStaker)?
476+
}
479477
if !<Collators<T>>::contains_key(&target) {
480478
Err(<Error<T>>::TargetNotCollator)?;
481479
}
@@ -680,14 +678,6 @@ pub mod pallet {
680678
});
681679
}
682680

683-
fn ensure_staker(who: &T::AccountId) -> DispatchResult {
684-
if <Ledgers<T>>::contains_key(who) {
685-
Ok(())
686-
} else {
687-
Err(<Error<T>>::NotStaker)?
688-
}
689-
}
690-
691681
/// Add reward points to collators using their account id.
692682
pub fn reward_by_ids(collators: &[(T::AccountId, RewardPoint)]) {
693683
<RewardPoints<T>>::mutate(|(total, reward_map)| {
@@ -765,16 +755,17 @@ pub mod pallet {
765755

766756
continue;
767757
};
768-
let c_payout = c_commission_payout
769-
+ Perbill::from_rational(c_exposure.own, c_exposure.total) * n_payout;
770758

771-
if let Ok(_i) = T::RingCurrency::deposit_into_existing(&c, c_payout) {
772-
actual_payout += c_payout;
759+
if let Ok(_i) = T::RingCurrency::deposit_into_existing(&c, c_commission_payout) {
760+
actual_payout += c_commission_payout;
773761

774-
Self::deposit_event(Event::<T>::Payout { staker: c, ring_amount: c_payout });
762+
Self::deposit_event(Event::<T>::Payout {
763+
staker: c,
764+
ring_amount: c_commission_payout,
765+
});
775766
}
776767

777-
for n_exposure in c_exposure.others {
768+
for n_exposure in c_exposure.nominators {
778769
let n_payout =
779770
Perbill::from_rational(n_exposure.value, c_exposure.total) * n_payout;
780771

@@ -807,8 +798,7 @@ pub mod pallet {
807798
pub fn elect() -> Vec<T::AccountId> {
808799
let mut collators = <Collators<T>>::iter_keys()
809800
.map(|c| {
810-
let c_power = Self::power_of(&c);
811-
let mut t_power = c_power;
801+
let mut t_power = 0;
812802
let i_exposures = <Nominators<T>>::iter()
813803
.filter_map(|(n, c_)| {
814804
if c_ == c {
@@ -823,7 +813,7 @@ pub mod pallet {
823813
})
824814
.collect();
825815

826-
((c, Exposure { total: t_power, own: c_power, others: i_exposures }), t_power)
816+
((c, Exposure { total: t_power, nominators: i_exposures }), t_power)
827817
})
828818
.collect::<Vec<_>>();
829819

pallet/staking/tests/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ fn elect_should_work() {
393393
Vec::new()
394394
));
395395
assert_ok!(Staking::collect(RuntimeOrigin::signed(i), Default::default()));
396+
assert_ok!(Staking::nominate(RuntimeOrigin::signed(i), i));
396397
});
397398
(6..=10).for_each(|i| {
398399
assert_ok!(Staking::stake(
@@ -441,6 +442,7 @@ fn payout_should_work() {
441442
Vec::new()
442443
));
443444
assert_ok!(Staking::collect(RuntimeOrigin::signed(i), Perbill::from_percent(i * 10)));
445+
assert_ok!(Staking::nominate(RuntimeOrigin::signed(i), i));
444446
});
445447
(6..=10).for_each(|i| {
446448
assert_ok!(Staking::stake(

0 commit comments

Comments
 (0)