Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Staking] Extract pub compute top candidates from private select top candidates #745

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,23 +1865,29 @@ pub mod pallet {
exit_queue.nominator_schedule = remaining_exits;
<ExitQueue2<T>>::put(exit_queue);
}
/// 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());
/// Compute the top `TotalSelected` candidates in the CandidatePool and return
/// a vec of their AccountIds (in the order of selection)
pub fn compute_top_candidates() -> Vec<T::AccountId> {
let mut candidates = <CandidatePool<T>>::get().0;
// order candidates by stake (least to greatest so requires `rev()`)
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
let mut collators = candidates
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>>()
}
/// 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();
// snapshot exposure for round for weighting reward distribution
for account in collators.iter() {
let state = <CollatorState2<T>>::get(&account)
Expand Down