Apply some clippy lints (#11154)

* Apply some clippy hints

* Revert clippy ci changes

* Update client/cli/src/commands/generate.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/cli/src/commands/inspect_key.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/db/src/bench.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/db/src/bench.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/service/src/client/block_rules.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/service/src/client/block_rules.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/network/src/transactions.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/network/src/protocol.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Revert due to missing `or_default` function.

* Fix compilation and simplify code

* Undo change that corrupts benchmark.

* fix clippy

* Update client/service/test/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/state-db/src/noncanonical.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/state-db/src/noncanonical.rs

remove leftovers!

* Update client/tracing/src/logging/directives.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update utils/fork-tree/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* added needed ref

* Update frame/referenda/src/benchmarking.rs

* Simplify byte-vec creation

* let's just not overlap the ranges

* Correction

* cargo fmt

* Update utils/frame/benchmarking-cli/src/shared/stats.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update utils/frame/benchmarking-cli/src/pallet/command.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update utils/frame/benchmarking-cli/src/pallet/command.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Giles Cope <gilescope@gmail.com>
This commit is contained in:
Falco Hirschenberger
2022-04-30 23:28:27 +02:00
committed by GitHub
parent a990473cf9
commit b581604aa7
368 changed files with 1927 additions and 2236 deletions
@@ -93,8 +93,7 @@ fn submit_candidates_with_self_vote<T: Config>(
let stake = default_stake::<T>(c);
let _ = candidates
.iter()
.map(|c| submit_voter::<T>(c.clone(), vec![c.clone()], stake).map(|_| ()))
.collect::<Result<_, _>>()?;
.try_for_each(|c| submit_voter::<T>(c.clone(), vec![c.clone()], stake).map(|_| ()))?;
Ok(candidates)
}
+140 -132
View File
@@ -503,7 +503,7 @@ pub mod pallet {
let had_replacement = Self::remove_and_replace_member(&who, true)?;
debug_assert_eq!(has_replacement, had_replacement);
Self::deposit_event(Event::MemberKicked { member: who.clone() });
Self::deposit_event(Event::MemberKicked { member: who });
if !had_replacement {
Self::do_phragmen();
@@ -782,7 +782,7 @@ impl<T: Config> Pallet<T> {
})?;
let remaining_member_ids_sorted =
Self::members().into_iter().map(|x| x.who.clone()).collect::<Vec<_>>();
Self::members().into_iter().map(|x| x.who).collect::<Vec<_>>();
let outgoing = &[who.clone()];
let maybe_current_prime = T::ChangeMembers::get_prime();
let return_value = match maybe_replacement {
@@ -861,7 +861,7 @@ impl<T: Config> Pallet<T> {
/// Reads Members, RunnersUp, Candidates and Voting(who) from database.
fn is_defunct_voter(votes: &[T::AccountId]) -> bool {
votes.iter().all(|v| {
!Self::is_member(v) && !Self::is_runner_up(v) && !Self::is_candidate(v).is_ok()
!Self::is_member(v) && !Self::is_runner_up(v) && Self::is_candidate(v).is_err()
})
}
@@ -923,148 +923,156 @@ impl<T: Config> Pallet<T> {
let weight_candidates = candidates_and_deposit.len() as u32;
let weight_voters = voters_and_votes.len() as u32;
let weight_edges = num_edges;
let _ = sp_npos_elections::seq_phragmen(
num_to_elect,
candidate_ids,
voters_and_votes.clone(),
None,
)
.map(|ElectionResult::<T::AccountId, Perbill> { winners, assignments: _ }| {
// this is already sorted by id.
let old_members_ids_sorted =
<Members<T>>::take().into_iter().map(|m| m.who).collect::<Vec<T::AccountId>>();
// this one needs a sort by id.
let mut old_runners_up_ids_sorted =
<RunnersUp<T>>::take().into_iter().map(|r| r.who).collect::<Vec<T::AccountId>>();
old_runners_up_ids_sorted.sort();
let _ =
sp_npos_elections::seq_phragmen(num_to_elect, candidate_ids, voters_and_votes, None)
.map(|ElectionResult::<T::AccountId, Perbill> { winners, assignments: _ }| {
// this is already sorted by id.
let old_members_ids_sorted = <Members<T>>::take()
.into_iter()
.map(|m| m.who)
.collect::<Vec<T::AccountId>>();
// this one needs a sort by id.
let mut old_runners_up_ids_sorted = <RunnersUp<T>>::take()
.into_iter()
.map(|r| r.who)
.collect::<Vec<T::AccountId>>();
old_runners_up_ids_sorted.sort();
// filter out those who end up with no backing stake.
let mut new_set_with_stake = winners
.into_iter()
.filter_map(|(m, b)| if b.is_zero() { None } else { Some((m, to_balance(b))) })
.collect::<Vec<(T::AccountId, BalanceOf<T>)>>();
// filter out those who end up with no backing stake.
let mut new_set_with_stake = winners
.into_iter()
.filter_map(
|(m, b)| if b.is_zero() { None } else { Some((m, to_balance(b))) },
)
.collect::<Vec<(T::AccountId, BalanceOf<T>)>>();
// OPTIMIZATION NOTE: we could bail out here if `new_set.len() == 0`. There isn't
// much left to do. Yet, re-arranging the code would require duplicating the
// slashing of exposed candidates, cleaning any previous members, and so on. For
// now, in favor of readability and veracity, we keep it simple.
// OPTIMIZATION NOTE: we could bail out here if `new_set.len() == 0`. There
// isn't much left to do. Yet, re-arranging the code would require duplicating
// the slashing of exposed candidates, cleaning any previous members, and so on.
// For now, in favor of readability and veracity, we keep it simple.
// split new set into winners and runners up.
let split_point = desired_seats.min(new_set_with_stake.len());
let mut new_members_sorted_by_id =
new_set_with_stake.drain(..split_point).collect::<Vec<_>>();
new_members_sorted_by_id.sort_by(|i, j| i.0.cmp(&j.0));
// split new set into winners and runners up.
let split_point = desired_seats.min(new_set_with_stake.len());
let mut new_members_sorted_by_id =
new_set_with_stake.drain(..split_point).collect::<Vec<_>>();
new_members_sorted_by_id.sort_by(|i, j| i.0.cmp(&j.0));
// all the rest will be runners-up
new_set_with_stake.reverse();
let new_runners_up_sorted_by_rank = new_set_with_stake;
let mut new_runners_up_ids_sorted =
new_runners_up_sorted_by_rank.iter().map(|(r, _)| r.clone()).collect::<Vec<_>>();
new_runners_up_ids_sorted.sort();
// all the rest will be runners-up
new_set_with_stake.reverse();
let new_runners_up_sorted_by_rank = new_set_with_stake;
let mut new_runners_up_ids_sorted = new_runners_up_sorted_by_rank
.iter()
.map(|(r, _)| r.clone())
.collect::<Vec<_>>();
new_runners_up_ids_sorted.sort();
// Now we select a prime member using a [Borda
// count](https://en.wikipedia.org/wiki/Borda_count). We weigh everyone's vote for
// that new member by a multiplier based on the order of the votes. i.e. the first
// person a voter votes for gets a 16x multiplier, the next person gets a 15x
// multiplier, an so on... (assuming `MAXIMUM_VOTE` = 16)
let mut prime_votes = new_members_sorted_by_id
.iter()
.map(|c| (&c.0, BalanceOf::<T>::zero()))
.collect::<Vec<_>>();
for (_, stake, votes) in voters_and_stakes.into_iter() {
for (vote_multiplier, who) in votes
.iter()
.enumerate()
.map(|(vote_position, who)| ((MAXIMUM_VOTE - vote_position) as u32, who))
{
if let Ok(i) = prime_votes.binary_search_by_key(&who, |k| k.0) {
prime_votes[i].1 = prime_votes[i]
.1
.saturating_add(stake.saturating_mul(vote_multiplier.into()));
// Now we select a prime member using a [Borda
// count](https://en.wikipedia.org/wiki/Borda_count). We weigh everyone's vote for
// that new member by a multiplier based on the order of the votes. i.e. the
// first person a voter votes for gets a 16x multiplier, the next person gets a
// 15x multiplier, an so on... (assuming `MAXIMUM_VOTE` = 16)
let mut prime_votes = new_members_sorted_by_id
.iter()
.map(|c| (&c.0, BalanceOf::<T>::zero()))
.collect::<Vec<_>>();
for (_, stake, votes) in voters_and_stakes.into_iter() {
for (vote_multiplier, who) in
votes.iter().enumerate().map(|(vote_position, who)| {
((MAXIMUM_VOTE - vote_position) as u32, who)
}) {
if let Ok(i) = prime_votes.binary_search_by_key(&who, |k| k.0) {
prime_votes[i].1 = prime_votes[i]
.1
.saturating_add(stake.saturating_mul(vote_multiplier.into()));
}
}
}
}
}
// We then select the new member with the highest weighted stake. In the case of a tie,
// the last person in the list with the tied score is selected. This is the person with
// the "highest" account id based on the sort above.
let prime = prime_votes.into_iter().max_by_key(|x| x.1).map(|x| x.0.clone());
// We then select the new member with the highest weighted stake. In the case of
// a tie, the last person in the list with the tied score is selected. This is
// the person with the "highest" account id based on the sort above.
let prime = prime_votes.into_iter().max_by_key(|x| x.1).map(|x| x.0.clone());
// new_members_sorted_by_id is sorted by account id.
let new_members_ids_sorted = new_members_sorted_by_id
.iter()
.map(|(m, _)| m.clone())
.collect::<Vec<T::AccountId>>();
// new_members_sorted_by_id is sorted by account id.
let new_members_ids_sorted = new_members_sorted_by_id
.iter()
.map(|(m, _)| m.clone())
.collect::<Vec<T::AccountId>>();
// report member changes. We compute diff because we need the outgoing list.
let (incoming, outgoing) = T::ChangeMembers::compute_members_diff_sorted(
&new_members_ids_sorted,
&old_members_ids_sorted,
);
T::ChangeMembers::change_members_sorted(&incoming, &outgoing, &new_members_ids_sorted);
T::ChangeMembers::set_prime(prime);
// report member changes. We compute diff because we need the outgoing list.
let (incoming, outgoing) = T::ChangeMembers::compute_members_diff_sorted(
&new_members_ids_sorted,
&old_members_ids_sorted,
);
T::ChangeMembers::change_members_sorted(
&incoming,
&outgoing,
&new_members_ids_sorted,
);
T::ChangeMembers::set_prime(prime);
// All candidates/members/runners-up who are no longer retaining a position as a
// seat holder will lose their bond.
candidates_and_deposit.iter().for_each(|(c, d)| {
if new_members_ids_sorted.binary_search(c).is_err() &&
new_runners_up_ids_sorted.binary_search(c).is_err()
{
let (imbalance, _) = T::Currency::slash_reserved(c, *d);
T::LoserCandidate::on_unbalanced(imbalance);
Self::deposit_event(Event::CandidateSlashed {
candidate: c.clone(),
amount: *d,
// All candidates/members/runners-up who are no longer retaining a position as a
// seat holder will lose their bond.
candidates_and_deposit.iter().for_each(|(c, d)| {
if new_members_ids_sorted.binary_search(c).is_err() &&
new_runners_up_ids_sorted.binary_search(c).is_err()
{
let (imbalance, _) = T::Currency::slash_reserved(c, *d);
T::LoserCandidate::on_unbalanced(imbalance);
Self::deposit_event(Event::CandidateSlashed {
candidate: c.clone(),
amount: *d,
});
}
});
}
});
// write final values to storage.
let deposit_of_candidate = |x: &T::AccountId| -> BalanceOf<T> {
// defensive-only. This closure is used against the new members and new runners-up,
// both of which are phragmen winners and thus must have deposit.
candidates_and_deposit
.iter()
.find_map(|(c, d)| if c == x { Some(*d) } else { None })
.defensive_unwrap_or_default()
};
// fetch deposits from the one recorded one. This will make sure that a candidate who
// submitted candidacy before a change to candidacy deposit will have the correct amount
// recorded.
<Members<T>>::put(
new_members_sorted_by_id
.iter()
.map(|(who, stake)| SeatHolder {
deposit: deposit_of_candidate(&who),
who: who.clone(),
stake: stake.clone(),
})
.collect::<Vec<_>>(),
);
<RunnersUp<T>>::put(
new_runners_up_sorted_by_rank
.into_iter()
.map(|(who, stake)| SeatHolder {
deposit: deposit_of_candidate(&who),
who,
stake,
})
.collect::<Vec<_>>(),
);
// write final values to storage.
let deposit_of_candidate = |x: &T::AccountId| -> BalanceOf<T> {
// defensive-only. This closure is used against the new members and new
// runners-up, both of which are phragmen winners and thus must have
// deposit.
candidates_and_deposit
.iter()
.find_map(|(c, d)| if c == x { Some(*d) } else { None })
.defensive_unwrap_or_default()
};
// fetch deposits from the one recorded one. This will make sure that a
// candidate who submitted candidacy before a change to candidacy deposit will
// have the correct amount recorded.
<Members<T>>::put(
new_members_sorted_by_id
.iter()
.map(|(who, stake)| SeatHolder {
deposit: deposit_of_candidate(who),
who: who.clone(),
stake: *stake,
})
.collect::<Vec<_>>(),
);
<RunnersUp<T>>::put(
new_runners_up_sorted_by_rank
.into_iter()
.map(|(who, stake)| SeatHolder {
deposit: deposit_of_candidate(&who),
who,
stake,
})
.collect::<Vec<_>>(),
);
// clean candidates.
<Candidates<T>>::kill();
// clean candidates.
<Candidates<T>>::kill();
Self::deposit_event(Event::NewTerm { new_members: new_members_sorted_by_id });
<ElectionRounds<T>>::mutate(|v| *v += 1);
})
.map_err(|e| {
log::error!(
target: "runtime::elections-phragmen",
"Failed to run election [{:?}].",
e,
);
Self::deposit_event(Event::ElectionError);
});
Self::deposit_event(Event::NewTerm { new_members: new_members_sorted_by_id });
<ElectionRounds<T>>::mutate(|v| *v += 1);
})
.map_err(|e| {
log::error!(
target: "runtime::elections-phragmen",
"Failed to run election [{:?}].",
e,
);
Self::deposit_event(Event::ElectionError);
});
T::WeightInfo::election_phragmen(weight_candidates, weight_voters, weight_edges)
}
@@ -12,7 +12,7 @@ pub fn migrate<T: Config>(to_migrate: Vec<T::AccountId>) -> Weight {
for who in to_migrate.iter() {
if let Ok(mut voter) = Voting::<T>::try_get(who) {
let free_balance = T::Currency::free_balance(&who);
let free_balance = T::Currency::free_balance(who);
weight = weight.saturating_add(T::DbWeight::get().reads(2));
@@ -21,7 +21,7 @@ pub fn migrate<T: Config>(to_migrate: Vec<T::AccountId>) -> Weight {
Voting::<T>::insert(&who, voter);
let pallet_id = T::PalletId::get();
T::Currency::set_lock(pallet_id, &who, free_balance, WithdrawReasons::all());
T::Currency::set_lock(pallet_id, who, free_balance, WithdrawReasons::all());
weight = weight.saturating_add(T::DbWeight::get().writes(2));
}
@@ -38,7 +38,7 @@ pub fn pre_migrate_fn<T: Config>(to_migrate: Vec<T::AccountId>) -> Box<dyn Fn()
Box::new(move || {
for who in to_migrate.iter() {
if let Ok(voter) = Voting::<T>::try_get(who) {
let free_balance = T::Currency::free_balance(&who);
let free_balance = T::Currency::free_balance(who);
if voter.stake > free_balance {
// all good