Skip to content

Commit

Permalink
Release collator staking restriction (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier authored Dec 8, 2022
1 parent 97ff5cc commit e5b6409
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
36 changes: 13 additions & 23 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,8 @@ where
pub struct Exposure<AccountId> {
/// The total power backing this collator.
pub total: Power,
/// Collator's self stake power.
pub own: Power,
/// Nominators' stake power.
pub others: Vec<IndividualExposure<AccountId>>,
pub nominators: Vec<IndividualExposure<AccountId>>,
}
/// A snapshot of the staker's state.
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]
Expand Down Expand Up @@ -459,7 +457,6 @@ pub mod pallet {
pub fn collect(origin: OriginFor<T>, commission: Perbill) -> DispatchResult {
let who = ensure_signed(origin)?;

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

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

Self::ensure_staker(&who)?;

if !<Ledgers<T>>::contains_key(&who) {
Err(<Error<T>>::NotStaker)?
}
if !<Collators<T>>::contains_key(&target) {
Err(<Error<T>>::TargetNotCollator)?;
}
Expand Down Expand Up @@ -680,14 +678,6 @@ pub mod pallet {
});
}

fn ensure_staker(who: &T::AccountId) -> DispatchResult {
if <Ledgers<T>>::contains_key(who) {
Ok(())
} else {
Err(<Error<T>>::NotStaker)?
}
}

/// Add reward points to collators using their account id.
pub fn reward_by_ids(collators: &[(T::AccountId, RewardPoint)]) {
<RewardPoints<T>>::mutate(|(total, reward_map)| {
Expand Down Expand Up @@ -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::<T>::Payout { staker: c, ring_amount: c_payout });
Self::deposit_event(Event::<T>::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;

Expand Down Expand Up @@ -807,8 +798,7 @@ pub mod pallet {
pub fn elect() -> Vec<T::AccountId> {
let mut collators = <Collators<T>>::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 = <Nominators<T>>::iter()
.filter_map(|(n, c_)| {
if c_ == c {
Expand All @@ -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::<Vec<_>>();

Expand Down
2 changes: 2 additions & 0 deletions pallet/staking/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit e5b6409

Please sign in to comment.