mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-20 22:05:42 +00:00
collator-selection pallet tidy (#1093)
* One less collect + clippy suggestions. * No need to index into collection * Update pallets/collator-selection/src/lib.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -368,7 +368,7 @@ pub mod pallet {
|
|||||||
|
|
||||||
let current_count =
|
let current_count =
|
||||||
<Candidates<T>>::try_mutate(|candidates| -> Result<usize, DispatchError> {
|
<Candidates<T>>::try_mutate(|candidates| -> Result<usize, DispatchError> {
|
||||||
if candidates.into_iter().any(|candidate| candidate.who == who) {
|
if candidates.iter().any(|candidate| candidate.who == who) {
|
||||||
Err(Error::<T>::AlreadyCandidate)?
|
Err(Error::<T>::AlreadyCandidate)?
|
||||||
} else {
|
} else {
|
||||||
T::Currency::reserve(&who, deposit)?;
|
T::Currency::reserve(&who, deposit)?;
|
||||||
@@ -409,6 +409,7 @@ pub mod pallet {
|
|||||||
pub fn account_id() -> T::AccountId {
|
pub fn account_id() -> T::AccountId {
|
||||||
T::PotId::get().into_account()
|
T::PotId::get().into_account()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes a candidate if they exist and sends them back their deposit
|
/// Removes a candidate if they exist and sends them back their deposit
|
||||||
fn try_remove_candidate(who: &T::AccountId) -> Result<usize, DispatchError> {
|
fn try_remove_candidate(who: &T::AccountId) -> Result<usize, DispatchError> {
|
||||||
let current_count =
|
let current_count =
|
||||||
@@ -417,8 +418,8 @@ pub mod pallet {
|
|||||||
.iter()
|
.iter()
|
||||||
.position(|candidate| candidate.who == *who)
|
.position(|candidate| candidate.who == *who)
|
||||||
.ok_or(Error::<T>::NotCandidate)?;
|
.ok_or(Error::<T>::NotCandidate)?;
|
||||||
T::Currency::unreserve(&who, candidates[index].deposit);
|
let candidate = candidates.remove(index);
|
||||||
candidates.remove(index);
|
T::Currency::unreserve(who, candidate.deposit);
|
||||||
<LastAuthoredBlock<T>>::remove(who.clone());
|
<LastAuthoredBlock<T>>::remove(who.clone());
|
||||||
Ok(candidates.len())
|
Ok(candidates.len())
|
||||||
})?;
|
})?;
|
||||||
@@ -431,16 +432,18 @@ pub mod pallet {
|
|||||||
/// This is done on the fly, as frequent as we are told to do so, as the session manager.
|
/// This is done on the fly, as frequent as we are told to do so, as the session manager.
|
||||||
pub fn assemble_collators(candidates: Vec<T::AccountId>) -> Vec<T::AccountId> {
|
pub fn assemble_collators(candidates: Vec<T::AccountId>) -> Vec<T::AccountId> {
|
||||||
let mut collators = Self::invulnerables();
|
let mut collators = Self::invulnerables();
|
||||||
collators.extend(candidates.into_iter().collect::<Vec<_>>());
|
collators.extend(candidates);
|
||||||
collators
|
collators
|
||||||
}
|
}
|
||||||
/// Kicks out and candidates that did not produce a block in the kick threshold.
|
|
||||||
|
/// Kicks out candidates that did not produce a block in the kick threshold
|
||||||
|
/// and refund their deposits.
|
||||||
pub fn kick_stale_candidates(
|
pub fn kick_stale_candidates(
|
||||||
candidates: Vec<CandidateInfo<T::AccountId, BalanceOf<T>>>,
|
candidates: Vec<CandidateInfo<T::AccountId, BalanceOf<T>>>,
|
||||||
) -> Vec<T::AccountId> {
|
) -> Vec<T::AccountId> {
|
||||||
let now = frame_system::Pallet::<T>::block_number();
|
let now = frame_system::Pallet::<T>::block_number();
|
||||||
let kick_threshold = T::KickThreshold::get();
|
let kick_threshold = T::KickThreshold::get();
|
||||||
let new_candidates = candidates
|
candidates
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|c| {
|
.filter_map(|c| {
|
||||||
let last_block = <LastAuthoredBlock<T>>::get(c.who.clone());
|
let last_block = <LastAuthoredBlock<T>>::get(c.who.clone());
|
||||||
@@ -458,8 +461,7 @@ pub mod pallet {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect()
|
||||||
new_candidates
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -503,9 +505,8 @@ pub mod pallet {
|
|||||||
let candidates = Self::candidates();
|
let candidates = Self::candidates();
|
||||||
let candidates_len_before = candidates.len();
|
let candidates_len_before = candidates.len();
|
||||||
let active_candidates = Self::kick_stale_candidates(candidates);
|
let active_candidates = Self::kick_stale_candidates(candidates);
|
||||||
let active_candidates_len = active_candidates.len();
|
let removed = candidates_len_before - active_candidates.len();
|
||||||
let result = Self::assemble_collators(active_candidates);
|
let result = Self::assemble_collators(active_candidates);
|
||||||
let removed = candidates_len_before - active_candidates_len;
|
|
||||||
|
|
||||||
frame_system::Pallet::<T>::register_extra_weight_unchecked(
|
frame_system::Pallet::<T>::register_extra_weight_unchecked(
|
||||||
T::WeightInfo::new_session(candidates_len_before as u32, removed as u32),
|
T::WeightInfo::new_session(candidates_len_before as u32, removed as u32),
|
||||||
|
|||||||
Reference in New Issue
Block a user