mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-22 13:51:05 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -20,12 +20,12 @@
|
||||
#![cfg(any(test, mocks))]
|
||||
|
||||
use std::{
|
||||
collections::{HashSet, HashMap},
|
||||
collections::{HashMap, HashSet},
|
||||
convert::TryInto,
|
||||
hash::Hash,
|
||||
};
|
||||
|
||||
use rand::{self, Rng, seq::SliceRandom};
|
||||
use rand::{self, seq::SliceRandom, Rng};
|
||||
use sp_arithmetic::{
|
||||
traits::{One, SaturatedConversion, Zero},
|
||||
PerThing,
|
||||
@@ -33,7 +33,7 @@ use sp_arithmetic::{
|
||||
use sp_runtime::assert_eq_error_rate;
|
||||
use sp_std::collections::btree_map::BTreeMap;
|
||||
|
||||
use crate::{Assignment, ElectionResult, ExtendedBalance, PerThing128, VoteWeight, seq_phragmen};
|
||||
use crate::{seq_phragmen, Assignment, ElectionResult, ExtendedBalance, PerThing128, VoteWeight};
|
||||
|
||||
sp_npos_elections_compact::generate_solution_type!(
|
||||
#[compact]
|
||||
@@ -87,7 +87,7 @@ pub(crate) type _SupportMap<A> = BTreeMap<A, _Support<A>>;
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct _ElectionResult<A: Clone> {
|
||||
pub winners: Vec<(A, ExtendedBalance)>,
|
||||
pub assignments: Vec<(A, Vec<_Assignment<A>>)>
|
||||
pub assignments: Vec<(A, Vec<_Assignment<A>>)>,
|
||||
}
|
||||
|
||||
pub(crate) fn auto_generate_self_voters<A: Clone>(candidates: &[A]) -> Vec<(A, Vec<A>)> {
|
||||
@@ -99,7 +99,8 @@ pub(crate) fn elect_float<A>(
|
||||
initial_candidates: Vec<A>,
|
||||
initial_voters: Vec<(A, Vec<A>)>,
|
||||
stake_of: impl Fn(&A) -> VoteWeight,
|
||||
) -> Option<_ElectionResult<A>> where
|
||||
) -> Option<_ElectionResult<A>>
|
||||
where
|
||||
A: Default + Ord + Copy,
|
||||
{
|
||||
let mut elected_candidates: Vec<(A, ExtendedBalance)>;
|
||||
@@ -123,17 +124,10 @@ pub(crate) fn elect_float<A>(
|
||||
for v in votes {
|
||||
if let Some(idx) = c_idx_cache.get(&v) {
|
||||
candidates[*idx].approval_stake = candidates[*idx].approval_stake + voter_stake;
|
||||
edges.push(
|
||||
_Edge { who: v.clone(), candidate_index: *idx, ..Default::default() }
|
||||
);
|
||||
edges.push(_Edge { who: v.clone(), candidate_index: *idx, ..Default::default() });
|
||||
}
|
||||
}
|
||||
_Voter {
|
||||
who,
|
||||
edges: edges,
|
||||
budget: voter_stake,
|
||||
load: 0f64,
|
||||
}
|
||||
_Voter { who, edges, budget: voter_stake, load: 0f64 }
|
||||
}));
|
||||
|
||||
let to_elect = candidate_count.min(candidates.len());
|
||||
@@ -179,7 +173,9 @@ pub(crate) fn elect_float<A>(
|
||||
for n in &mut voters {
|
||||
let mut assignment = (n.who.clone(), vec![]);
|
||||
for e in &mut n.edges {
|
||||
if let Some(c) = elected_candidates.iter().cloned().map(|(c, _)| c).find(|c| *c == e.who) {
|
||||
if let Some(c) =
|
||||
elected_candidates.iter().cloned().map(|(c, _)| c).find(|c| *c == e.who)
|
||||
{
|
||||
if c != n.who {
|
||||
let ratio = e.load / n.load;
|
||||
assignment.1.push((e.who.clone(), ratio));
|
||||
@@ -191,10 +187,7 @@ pub(crate) fn elect_float<A>(
|
||||
}
|
||||
}
|
||||
|
||||
Some(_ElectionResult {
|
||||
winners: elected_candidates,
|
||||
assignments: assigned,
|
||||
})
|
||||
Some(_ElectionResult { winners: elected_candidates, assignments: assigned })
|
||||
}
|
||||
|
||||
pub(crate) fn equalize_float<A, FS>(
|
||||
@@ -211,18 +204,14 @@ pub(crate) fn equalize_float<A, FS>(
|
||||
let mut max_diff = 0.0;
|
||||
for (voter, assignment) in assignments.iter_mut() {
|
||||
let voter_budget = stake_of(&voter);
|
||||
let diff = do_equalize_float(
|
||||
voter,
|
||||
voter_budget,
|
||||
assignment,
|
||||
supports,
|
||||
tolerance,
|
||||
);
|
||||
if diff > max_diff { max_diff = diff; }
|
||||
let diff = do_equalize_float(voter, voter_budget, assignment, supports, tolerance);
|
||||
if diff > max_diff {
|
||||
max_diff = diff;
|
||||
}
|
||||
}
|
||||
|
||||
if max_diff < tolerance {
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -232,21 +221,20 @@ pub(crate) fn do_equalize_float<A>(
|
||||
budget_balance: VoteWeight,
|
||||
elected_edges: &mut Vec<_Assignment<A>>,
|
||||
support_map: &mut _SupportMap<A>,
|
||||
tolerance: f64
|
||||
) -> f64 where
|
||||
tolerance: f64,
|
||||
) -> f64
|
||||
where
|
||||
A: Ord + Clone,
|
||||
{
|
||||
let budget = budget_balance as f64;
|
||||
if elected_edges.is_empty() { return 0.0; }
|
||||
if elected_edges.is_empty() {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
let stake_used = elected_edges
|
||||
.iter()
|
||||
.fold(0.0, |s, e| s + e.1);
|
||||
let stake_used = elected_edges.iter().fold(0.0, |s, e| s + e.1);
|
||||
|
||||
let backed_stakes_iter = elected_edges
|
||||
.iter()
|
||||
.filter_map(|e| support_map.get(&e.0))
|
||||
.map(|e| e.total);
|
||||
let backed_stakes_iter =
|
||||
elected_edges.iter().filter_map(|e| support_map.get(&e.0)).map(|e| e.total);
|
||||
|
||||
let backing_backed_stake = elected_edges
|
||||
.iter()
|
||||
@@ -268,7 +256,7 @@ pub(crate) fn do_equalize_float<A>(
|
||||
difference = max_stake - min_stake;
|
||||
difference = difference + budget - stake_used;
|
||||
if difference < tolerance {
|
||||
return difference;
|
||||
return difference
|
||||
}
|
||||
} else {
|
||||
difference = budget;
|
||||
@@ -283,11 +271,12 @@ pub(crate) fn do_equalize_float<A>(
|
||||
e.1 = 0.0;
|
||||
});
|
||||
|
||||
elected_edges.sort_by(|x, y|
|
||||
support_map.get(&x.0)
|
||||
elected_edges.sort_by(|x, y| {
|
||||
support_map
|
||||
.get(&x.0)
|
||||
.and_then(|x| support_map.get(&y.0).and_then(|y| x.total.partial_cmp(&y.total)))
|
||||
.unwrap_or(sp_std::cmp::Ordering::Equal)
|
||||
);
|
||||
});
|
||||
|
||||
let mut cumulative_stake = 0.0;
|
||||
let mut last_index = elected_edges.len() - 1;
|
||||
@@ -318,20 +307,22 @@ pub(crate) fn do_equalize_float<A>(
|
||||
difference
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn create_stake_of(stakes: &[(AccountId, VoteWeight)])
|
||||
-> impl Fn(&AccountId) -> VoteWeight
|
||||
{
|
||||
pub(crate) fn create_stake_of(
|
||||
stakes: &[(AccountId, VoteWeight)],
|
||||
) -> impl Fn(&AccountId) -> VoteWeight {
|
||||
let mut storage = BTreeMap::<AccountId, VoteWeight>::new();
|
||||
stakes.iter().for_each(|s| { storage.insert(s.0, s.1); });
|
||||
stakes.iter().for_each(|s| {
|
||||
storage.insert(s.0, s.1);
|
||||
});
|
||||
move |who: &AccountId| -> VoteWeight { storage.get(who).unwrap().to_owned() }
|
||||
}
|
||||
|
||||
|
||||
pub fn check_assignments_sum<T: PerThing>(assignments: &[Assignment<AccountId, T>]) {
|
||||
for Assignment { distribution, .. } in assignments {
|
||||
let mut sum: u128 = Zero::zero();
|
||||
distribution.iter().for_each(|(_, p)| sum += p.deconstruct().saturated_into::<u128>());
|
||||
distribution
|
||||
.iter()
|
||||
.for_each(|(_, p)| sum += p.deconstruct().saturated_into::<u128>());
|
||||
assert_eq!(sum, T::ACCURACY.saturated_into(), "Assignment ratio sum is not 100%");
|
||||
}
|
||||
}
|
||||
@@ -341,8 +332,7 @@ pub(crate) fn run_and_compare<Output: PerThing128, FS>(
|
||||
voters: Vec<(AccountId, Vec<AccountId>)>,
|
||||
stake_of: FS,
|
||||
to_elect: usize,
|
||||
)
|
||||
where
|
||||
) where
|
||||
Output: PerThing128,
|
||||
FS: Fn(&AccountId) -> VoteWeight,
|
||||
{
|
||||
@@ -350,24 +340,28 @@ where
|
||||
let ElectionResult { winners, assignments } = seq_phragmen::<_, Output>(
|
||||
to_elect,
|
||||
candidates.clone(),
|
||||
voters.iter().map(|(ref v, ref vs)| (v.clone(), stake_of(v), vs.clone())).collect::<Vec<_>>(),
|
||||
None
|
||||
).unwrap();
|
||||
voters
|
||||
.iter()
|
||||
.map(|(ref v, ref vs)| (v.clone(), stake_of(v), vs.clone()))
|
||||
.collect::<Vec<_>>(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// run float poc code.
|
||||
let truth_value = elect_float(
|
||||
to_elect,
|
||||
candidates,
|
||||
voters,
|
||||
&stake_of,
|
||||
).unwrap();
|
||||
let truth_value = elect_float(to_elect, candidates, voters, &stake_of).unwrap();
|
||||
|
||||
assert_eq!(winners.iter().map(|(x, _)| x).collect::<Vec<_>>(), truth_value.winners.iter().map(|(x, _)| x).collect::<Vec<_>>());
|
||||
assert_eq!(
|
||||
winners.iter().map(|(x, _)| x).collect::<Vec<_>>(),
|
||||
truth_value.winners.iter().map(|(x, _)| x).collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
for Assignment { who, distribution } in assignments.iter() {
|
||||
if let Some(float_assignments) = truth_value.assignments.iter().find(|x| x.0 == *who) {
|
||||
for (candidate, per_thingy) in distribution {
|
||||
if let Some(float_assignment) = float_assignments.1.iter().find(|x| x.0 == *candidate ) {
|
||||
if let Some(float_assignment) =
|
||||
float_assignments.1.iter().find(|x| x.0 == *candidate)
|
||||
{
|
||||
assert_eq_error_rate!(
|
||||
Output::from_float(float_assignment.1).deconstruct(),
|
||||
per_thingy.deconstruct(),
|
||||
@@ -376,8 +370,7 @@ where
|
||||
} else {
|
||||
panic!(
|
||||
"candidate mismatch. This should never happen. could not find ({:?}, {:?})",
|
||||
candidate,
|
||||
per_thingy,
|
||||
candidate, per_thingy,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -394,13 +387,10 @@ pub(crate) fn build_support_map_float(
|
||||
stake_of: impl Fn(&AccountId) -> VoteWeight,
|
||||
) -> _SupportMap<AccountId> {
|
||||
let mut supports = <_SupportMap<AccountId>>::new();
|
||||
result.winners
|
||||
.iter()
|
||||
.map(|(e, _)| (e, stake_of(e) as f64))
|
||||
.for_each(|(e, s)| {
|
||||
let item = _Support { own: s, total: s, ..Default::default() };
|
||||
supports.insert(e.clone(), item);
|
||||
});
|
||||
result.winners.iter().map(|(e, _)| (e, stake_of(e) as f64)).for_each(|(e, s)| {
|
||||
let item = _Support { own: s, total: s, ..Default::default() };
|
||||
supports.insert(e.clone(), item);
|
||||
});
|
||||
|
||||
for (n, assignment) in result.assignments.iter_mut() {
|
||||
for (c, r) in assignment.iter_mut() {
|
||||
|
||||
Reference in New Issue
Block a user