Skip to content

Commit

Permalink
Patch staking client author prediction bug (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
4meta5 authored Sep 19, 2021
1 parent ef56a04 commit 8d021e9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
9 changes: 5 additions & 4 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1912,21 +1912,23 @@ pub mod pallet {
candidates.sort_unstable_by(|a, b| a.amount.partial_cmp(&b.amount).unwrap());
let top_n = <TotalSelected<T>>::get() as usize;
// choose the top TotalSelected qualified candidates, ordered by stake
candidates
let mut collators = candidates
.into_iter()
.rev()
.take(top_n)
.filter(|x| x.amount >= T::MinCollatorStk::get())
.map(|x| x.owner)
.collect::<Vec<T::AccountId>>()
.collect::<Vec<T::AccountId>>();
collators.sort();
collators
}
/// Best as in most cumulatively supported in terms of stake
/// Returns [collator_count, nomination_count, total staked]
fn select_top_candidates(next: RoundIndex) -> (u32, u32, BalanceOf<T>) {
let (mut collator_count, mut nomination_count, mut total) =
(0u32, 0u32, BalanceOf::<T>::zero());
// choose the top TotalSelected qualified candidates, ordered by stake
let mut collators = Self::compute_top_candidates();
let collators = Self::compute_top_candidates();
// snapshot exposure for round for weighting reward distribution
for account in collators.iter() {
let state = <CollatorState2<T>>::get(&account)
Expand All @@ -1939,7 +1941,6 @@ pub mod pallet {
<AtStake<T>>::insert(next, account, exposure);
Self::deposit_event(Event::CollatorChosen(next, account.clone(), amount));
}
collators.sort();
// insert canonical collator set
<SelectedCandidates<T>>::put(collators);
(collator_count, nomination_count, total)
Expand Down
96 changes: 48 additions & 48 deletions pallets/parachain-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2706,8 +2706,8 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::ParachainBondAccountSet(0, 11),
Event::CollatorChosen(2, 1, 50),
Event::CollatorChosen(2, 2, 40),
Event::CollatorChosen(2, 4, 20),
Event::CollatorChosen(2, 3, 20),
Event::CollatorChosen(2, 4, 20),
Event::CollatorChosen(2, 5, 10),
Event::NewRound(5, 2, 5, 140),
];
Expand All @@ -2720,8 +2720,8 @@ fn parachain_bond_inflation_reserve_matches_config() {
let mut new = vec![
Event::CollatorChosen(3, 1, 50),
Event::CollatorChosen(3, 2, 40),
Event::CollatorChosen(3, 4, 20),
Event::CollatorChosen(3, 3, 20),
Event::CollatorChosen(3, 4, 20),
Event::CollatorChosen(3, 5, 10),
Event::NewRound(10, 3, 5, 140),
Event::ReservedForParachainBond(11, 15),
Expand All @@ -2731,8 +2731,8 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::Rewarded(10, 5),
Event::CollatorChosen(4, 1, 50),
Event::CollatorChosen(4, 2, 40),
Event::CollatorChosen(4, 4, 20),
Event::CollatorChosen(4, 3, 20),
Event::CollatorChosen(4, 4, 20),
Event::CollatorChosen(4, 5, 10),
Event::NewRound(15, 4, 5, 140),
];
Expand Down Expand Up @@ -2760,8 +2760,8 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::Rewarded(10, 5),
Event::CollatorChosen(5, 1, 50),
Event::CollatorChosen(5, 2, 40),
Event::CollatorChosen(5, 4, 20),
Event::CollatorChosen(5, 3, 20),
Event::CollatorChosen(5, 4, 20),
Event::CollatorChosen(5, 5, 10),
Event::NewRound(20, 5, 5, 140),
Event::ReservedForParachainBond(11, 16),
Expand All @@ -2771,10 +2771,10 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::Rewarded(10, 6),
Event::NominatorLeftCollator(6, 1, 10, 40),
Event::NominatorLeft(6, 10),
Event::CollatorChosen(6, 2, 40),
Event::CollatorChosen(6, 1, 40),
Event::CollatorChosen(6, 4, 20),
Event::CollatorChosen(6, 2, 40),
Event::CollatorChosen(6, 3, 20),
Event::CollatorChosen(6, 4, 20),
Event::CollatorChosen(6, 5, 10),
Event::NewRound(25, 6, 5, 130),
];
Expand All @@ -2799,10 +2799,10 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::Rewarded(6, 3),
Event::Rewarded(7, 3),
Event::Rewarded(10, 3),
Event::CollatorChosen(7, 2, 40),
Event::CollatorChosen(7, 1, 40),
Event::CollatorChosen(7, 4, 20),
Event::CollatorChosen(7, 2, 40),
Event::CollatorChosen(7, 3, 20),
Event::CollatorChosen(7, 4, 20),
Event::CollatorChosen(7, 5, 10),
Event::NewRound(30, 7, 5, 130),
];
Expand All @@ -2817,10 +2817,10 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::Rewarded(1, 21),
Event::Rewarded(7, 5),
Event::Rewarded(10, 5),
Event::CollatorChosen(8, 2, 40),
Event::CollatorChosen(8, 1, 40),
Event::CollatorChosen(8, 4, 20),
Event::CollatorChosen(8, 2, 40),
Event::CollatorChosen(8, 3, 20),
Event::CollatorChosen(8, 4, 20),
Event::CollatorChosen(8, 5, 10),
Event::NewRound(35, 8, 5, 130),
];
Expand All @@ -2839,8 +2839,8 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::Rewarded(10, 5),
Event::CollatorChosen(9, 1, 50),
Event::CollatorChosen(9, 2, 40),
Event::CollatorChosen(9, 4, 20),
Event::CollatorChosen(9, 3, 20),
Event::CollatorChosen(9, 4, 20),
Event::CollatorChosen(9, 5, 10),
Event::NewRound(40, 9, 5, 140),
];
Expand All @@ -2857,8 +2857,8 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::Rewarded(10, 5),
Event::CollatorChosen(10, 1, 50),
Event::CollatorChosen(10, 2, 40),
Event::CollatorChosen(10, 4, 20),
Event::CollatorChosen(10, 3, 20),
Event::CollatorChosen(10, 4, 20),
Event::CollatorChosen(10, 5, 10),
Event::NewRound(45, 10, 5, 140),
];
Expand All @@ -2875,8 +2875,8 @@ fn parachain_bond_inflation_reserve_matches_config() {
Event::Rewarded(10, 4),
Event::CollatorChosen(11, 1, 50),
Event::CollatorChosen(11, 2, 40),
Event::CollatorChosen(11, 4, 20),
Event::CollatorChosen(11, 3, 20),
Event::CollatorChosen(11, 4, 20),
Event::CollatorChosen(11, 5, 10),
Event::NewRound(50, 11, 5, 140),
];
Expand Down Expand Up @@ -2921,8 +2921,8 @@ fn paid_collator_commission_matches_config() {
Event::JoinedCollatorCandidates(4, 20, 60),
Event::Nomination(5, 10, 4, NominatorAdded::AddedToTop { new_total: 30 }),
Event::Nomination(6, 10, 4, NominatorAdded::AddedToTop { new_total: 40 }),
Event::CollatorChosen(3, 4, 40),
Event::CollatorChosen(3, 1, 40),
Event::CollatorChosen(3, 4, 40),
Event::NewRound(10, 3, 2, 80),
];
expected.append(&mut new);
Expand All @@ -2933,14 +2933,14 @@ fn paid_collator_commission_matches_config() {
// 20% of 10 is commission + due_portion (4) = 2 + 4 = 6
// all nominator payouts are 10-2 = 8 * stake_pct
let mut new2 = vec![
Event::CollatorChosen(4, 4, 40),
Event::CollatorChosen(4, 1, 40),
Event::CollatorChosen(4, 4, 40),
Event::NewRound(15, 4, 2, 80),
Event::Rewarded(4, 18),
Event::Rewarded(5, 6),
Event::Rewarded(6, 6),
Event::CollatorChosen(5, 4, 40),
Event::CollatorChosen(5, 1, 40),
Event::CollatorChosen(5, 4, 40),
Event::NewRound(20, 5, 2, 80),
];
expected.append(&mut new2);
Expand Down Expand Up @@ -3289,8 +3289,8 @@ fn multiple_nominations() {
let mut expected = vec![
Event::CollatorChosen(2, 1, 50),
Event::CollatorChosen(2, 2, 40),
Event::CollatorChosen(2, 4, 20),
Event::CollatorChosen(2, 3, 20),
Event::CollatorChosen(2, 4, 20),
Event::CollatorChosen(2, 5, 10),
Event::NewRound(5, 2, 5, 140),
];
Expand All @@ -3303,16 +3303,16 @@ fn multiple_nominations() {
Event::Nomination(6, 10, 2, NominatorAdded::AddedToTop { new_total: 50 }),
Event::Nomination(6, 10, 3, NominatorAdded::AddedToTop { new_total: 30 }),
Event::Nomination(6, 10, 4, NominatorAdded::AddedToTop { new_total: 30 }),
Event::CollatorChosen(3, 2, 50),
Event::CollatorChosen(3, 1, 50),
Event::CollatorChosen(3, 4, 30),
Event::CollatorChosen(3, 2, 50),
Event::CollatorChosen(3, 3, 30),
Event::CollatorChosen(3, 4, 30),
Event::CollatorChosen(3, 5, 10),
Event::NewRound(10, 3, 5, 170),
Event::CollatorChosen(4, 2, 50),
Event::CollatorChosen(4, 1, 50),
Event::CollatorChosen(4, 4, 30),
Event::CollatorChosen(4, 2, 50),
Event::CollatorChosen(4, 3, 30),
Event::CollatorChosen(4, 4, 30),
Event::CollatorChosen(4, 5, 10),
Event::NewRound(15, 4, 5, 170),
];
Expand All @@ -3323,18 +3323,18 @@ fn multiple_nominations() {
assert_ok!(Stake::nominate(Origin::signed(10), 2, 10, 10, 10),);
roll_to(26);
let mut new2 = vec![
Event::CollatorChosen(5, 2, 50),
Event::CollatorChosen(5, 1, 50),
Event::CollatorChosen(5, 4, 30),
Event::CollatorChosen(5, 2, 50),
Event::CollatorChosen(5, 3, 30),
Event::CollatorChosen(5, 4, 30),
Event::CollatorChosen(5, 5, 10),
Event::NewRound(20, 5, 5, 170),
Event::Nomination(7, 80, 2, NominatorAdded::AddedToTop { new_total: 130 }),
Event::Nomination(10, 10, 2, NominatorAdded::AddedToBottom),
Event::CollatorChosen(6, 2, 130),
Event::CollatorChosen(6, 1, 50),
Event::CollatorChosen(6, 4, 30),
Event::CollatorChosen(6, 2, 130),
Event::CollatorChosen(6, 3, 30),
Event::CollatorChosen(6, 4, 30),
Event::CollatorChosen(6, 5, 10),
Event::NewRound(25, 6, 5, 250),
];
Expand All @@ -3349,8 +3349,8 @@ fn multiple_nominations() {
let mut new3 = vec![
Event::CollatorScheduledExit(6, 2, 8),
Event::CollatorChosen(7, 1, 50),
Event::CollatorChosen(7, 4, 30),
Event::CollatorChosen(7, 3, 30),
Event::CollatorChosen(7, 4, 30),
Event::CollatorChosen(7, 5, 10),
Event::NewRound(30, 7, 4, 120),
];
Expand Down Expand Up @@ -3419,8 +3419,8 @@ fn payouts_follow_nomination_changes() {
let mut expected = vec![
Event::CollatorChosen(2, 1, 50),
Event::CollatorChosen(2, 2, 40),
Event::CollatorChosen(2, 4, 20),
Event::CollatorChosen(2, 3, 20),
Event::CollatorChosen(2, 4, 20),
Event::CollatorChosen(2, 5, 10),
Event::NewRound(5, 2, 5, 140),
];
Expand All @@ -3432,8 +3432,8 @@ fn payouts_follow_nomination_changes() {
let mut new = vec![
Event::CollatorChosen(3, 1, 50),
Event::CollatorChosen(3, 2, 40),
Event::CollatorChosen(3, 4, 20),
Event::CollatorChosen(3, 3, 20),
Event::CollatorChosen(3, 4, 20),
Event::CollatorChosen(3, 5, 10),
Event::NewRound(10, 3, 5, 140),
Event::Rewarded(1, 26),
Expand All @@ -3442,8 +3442,8 @@ fn payouts_follow_nomination_changes() {
Event::Rewarded(10, 8),
Event::CollatorChosen(4, 1, 50),
Event::CollatorChosen(4, 2, 40),
Event::CollatorChosen(4, 4, 20),
Event::CollatorChosen(4, 3, 20),
Event::CollatorChosen(4, 4, 20),
Event::CollatorChosen(4, 5, 10),
Event::NewRound(15, 4, 5, 140),
];
Expand All @@ -3470,8 +3470,8 @@ fn payouts_follow_nomination_changes() {
Event::Rewarded(10, 8),
Event::CollatorChosen(5, 1, 50),
Event::CollatorChosen(5, 2, 40),
Event::CollatorChosen(5, 4, 20),
Event::CollatorChosen(5, 3, 20),
Event::CollatorChosen(5, 4, 20),
Event::CollatorChosen(5, 5, 10),
Event::NewRound(20, 5, 5, 140),
Event::Rewarded(1, 29),
Expand All @@ -3480,10 +3480,10 @@ fn payouts_follow_nomination_changes() {
Event::Rewarded(10, 9),
Event::NominatorLeftCollator(6, 1, 10, 40),
Event::NominatorLeft(6, 10),
Event::CollatorChosen(6, 2, 40),
Event::CollatorChosen(6, 1, 40),
Event::CollatorChosen(6, 4, 20),
Event::CollatorChosen(6, 2, 40),
Event::CollatorChosen(6, 3, 20),
Event::CollatorChosen(6, 4, 20),
Event::CollatorChosen(6, 5, 10),
Event::NewRound(25, 6, 5, 130),
];
Expand All @@ -3498,10 +3498,10 @@ fn payouts_follow_nomination_changes() {
Event::Rewarded(6, 9),
Event::Rewarded(7, 9),
Event::Rewarded(10, 9),
Event::CollatorChosen(7, 2, 40),
Event::CollatorChosen(7, 1, 40),
Event::CollatorChosen(7, 4, 20),
Event::CollatorChosen(7, 2, 40),
Event::CollatorChosen(7, 3, 20),
Event::CollatorChosen(7, 4, 20),
Event::CollatorChosen(7, 5, 10),
Event::NewRound(30, 7, 5, 130),
];
Expand All @@ -3514,10 +3514,10 @@ fn payouts_follow_nomination_changes() {
Event::Rewarded(1, 36),
Event::Rewarded(7, 12),
Event::Rewarded(10, 12),
Event::CollatorChosen(8, 2, 40),
Event::CollatorChosen(8, 1, 40),
Event::CollatorChosen(8, 4, 20),
Event::CollatorChosen(8, 2, 40),
Event::CollatorChosen(8, 3, 20),
Event::CollatorChosen(8, 4, 20),
Event::CollatorChosen(8, 5, 10),
Event::NewRound(35, 8, 5, 130),
];
Expand All @@ -3534,8 +3534,8 @@ fn payouts_follow_nomination_changes() {
Event::Rewarded(10, 13),
Event::CollatorChosen(9, 1, 50),
Event::CollatorChosen(9, 2, 40),
Event::CollatorChosen(9, 4, 20),
Event::CollatorChosen(9, 3, 20),
Event::CollatorChosen(9, 4, 20),
Event::CollatorChosen(9, 5, 10),
Event::NewRound(40, 9, 5, 140),
];
Expand All @@ -3550,8 +3550,8 @@ fn payouts_follow_nomination_changes() {
Event::Rewarded(10, 13),
Event::CollatorChosen(10, 1, 50),
Event::CollatorChosen(10, 2, 40),
Event::CollatorChosen(10, 4, 20),
Event::CollatorChosen(10, 3, 20),
Event::CollatorChosen(10, 4, 20),
Event::CollatorChosen(10, 5, 10),
Event::NewRound(45, 10, 5, 140),
];
Expand All @@ -3566,8 +3566,8 @@ fn payouts_follow_nomination_changes() {
Event::Rewarded(10, 11),
Event::CollatorChosen(11, 1, 50),
Event::CollatorChosen(11, 2, 40),
Event::CollatorChosen(11, 4, 20),
Event::CollatorChosen(11, 3, 20),
Event::CollatorChosen(11, 4, 20),
Event::CollatorChosen(11, 5, 10),
Event::NewRound(50, 11, 5, 140),
];
Expand All @@ -3591,26 +3591,26 @@ fn nominations_merged_before_reward_payout() {
set_author(1, 4, 1);
roll_to(16);
let expected_events = vec![
Event::CollatorChosen(2, 4, 50),
Event::CollatorChosen(2, 3, 50),
Event::CollatorChosen(2, 2, 50),
Event::CollatorChosen(2, 1, 50),
Event::CollatorChosen(2, 2, 50),
Event::CollatorChosen(2, 3, 50),
Event::CollatorChosen(2, 4, 50),
Event::NewRound(5, 2, 4, 200),
Event::Rewarded(3, 1),
Event::Rewarded(4, 1),
Event::Rewarded(1, 1),
Event::Rewarded(2, 1),
// ALL REWARDS FOR 5 are merged into one payment + event
Event::Rewarded(5, 4),
Event::CollatorChosen(3, 4, 50),
Event::CollatorChosen(3, 3, 50),
Event::CollatorChosen(3, 2, 50),
Event::CollatorChosen(3, 1, 50),
Event::CollatorChosen(3, 2, 50),
Event::CollatorChosen(3, 3, 50),
Event::CollatorChosen(3, 4, 50),
Event::NewRound(10, 3, 4, 200),
Event::CollatorChosen(4, 4, 50),
Event::CollatorChosen(4, 3, 50),
Event::CollatorChosen(4, 2, 50),
Event::CollatorChosen(4, 1, 50),
Event::CollatorChosen(4, 2, 50),
Event::CollatorChosen(4, 3, 50),
Event::CollatorChosen(4, 4, 50),
Event::NewRound(15, 4, 4, 200),
];
assert_eq!(events(), expected_events);
Expand Down

0 comments on commit 8d021e9

Please sign in to comment.