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:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
@@ -62,11 +62,7 @@ pub fn generate_random_npos_inputs(
candidate_count: usize,
voter_count: usize,
mut rng: impl Rng,
) -> (
usize,
Vec<AccountId>,
Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
) {
) -> (usize, Vec<AccountId>, Vec<(AccountId, VoteWeight, Vec<AccountId>)>) {
// cache for fast generation of unique candidate and voter ids
let mut used_ids = HashSet::with_capacity(candidate_count + voter_count);
@@ -1,6 +1,5 @@
use honggfuzz::fuzz;
use sp_npos_elections::generate_solution_type;
use sp_npos_elections::sp_arithmetic::Percent;
use sp_npos_elections::{generate_solution_type, sp_arithmetic::Percent};
use sp_runtime::codec::{Encode, Error};
fn main() {
@@ -26,9 +25,8 @@ fn main() {
// The reencoded value should definitely be decodable (if unwrap() fails that is a valid
// panic/finding for the fuzzer):
let decoded2: InnerTestSolutionCompact =
<InnerTestSolutionCompact as codec::Decode>::decode(
&mut reencoded.as_slice(),
).unwrap();
<InnerTestSolutionCompact as codec::Decode>::decode(&mut reencoded.as_slice())
.unwrap();
// And it should be equal to the original decoded object (resulting from directly
// decoding fuzzer_data):
assert_eq!(decoded, decoded2);
@@ -21,23 +21,17 @@ mod common;
use common::*;
use honggfuzz::fuzz;
use rand::{self, SeedableRng};
use sp_npos_elections::{
assignment_ratio_to_staked_normalized, is_score_better, seq_phragmen, to_supports,
to_without_backing, EvaluateSupport, VoteWeight,
};
use sp_runtime::Perbill;
use rand::{self, SeedableRng};
fn main() {
loop {
fuzz!(|data: (usize, usize, usize, usize, u64)| {
let (
mut target_count,
mut voter_count,
mut iterations,
mut to_elect,
seed,
) = data;
let (mut target_count, mut voter_count, mut iterations, mut to_elect, seed) = data;
let rng = rand::rngs::SmallRng::seed_from_u64(seed);
target_count = to_range(target_count, 100, 200);
voter_count = to_range(voter_count, 100, 200);
@@ -48,12 +42,7 @@ fn main() {
"++ [voter_count: {} / target_count:{} / to_elect:{} / iterations:{}]",
voter_count, target_count, to_elect, iterations,
);
let (
unbalanced,
candidates,
voters,
stake_of_tree,
) = generate_random_npos_result(
let (unbalanced, candidates, voters, stake_of_tree) = generate_random_npos_result(
voter_count as u64,
target_count as u64,
to_elect,
@@ -61,9 +50,7 @@ fn main() {
ElectionType::Phragmen(None),
);
let stake_of = |who: &AccountId| -> VoteWeight {
*stake_of_tree.get(who).unwrap()
};
let stake_of = |who: &AccountId| -> VoteWeight { *stake_of_tree.get(who).unwrap() };
let unbalanced_score = {
let staked = assignment_ratio_to_staked_normalized(
@@ -76,7 +63,7 @@ fn main() {
if score[0] == 0 {
// such cases cannot be improved by balancing.
return;
return
}
score
};
@@ -87,34 +74,32 @@ fn main() {
candidates,
voters,
Some((iterations, 0)),
).unwrap();
)
.unwrap();
let balanced_score = {
let staked = assignment_ratio_to_staked_normalized(
balanced.assignments.clone(),
&stake_of,
).unwrap();
)
.unwrap();
let winners = to_without_backing(balanced.winners);
to_supports(winners.as_ref(), staked.as_ref()).unwrap().evaluate()
};
let enhance = is_score_better(balanced_score, unbalanced_score, Perbill::zero());
println!(
"iter = {} // {:?} -> {:?} [{}]",
iterations,
unbalanced_score,
balanced_score,
enhance,
iterations, unbalanced_score, balanced_score, enhance,
);
// The only guarantee of balancing is such that the first and third element of the score
// cannot decrease.
assert!(
balanced_score[0] >= unbalanced_score[0] &&
balanced_score[1] == unbalanced_score[1] &&
balanced_score[2] <= unbalanced_score[2]
balanced_score[1] == unbalanced_score[1] &&
balanced_score[2] <= unbalanced_score[2]
);
}
});
@@ -37,7 +37,6 @@
//!
//! Once a panic is found, it can be debugged with
//! `HFUZZ_RUN_ARGS="-t 10" cargo hfuzz run-debug phragmen_pjr hfuzz_workspace/phragmen_pjr/*.fuzz`.
//!
#[cfg(fuzzing)]
use honggfuzz::fuzz;
@@ -21,23 +21,17 @@ mod common;
use common::*;
use honggfuzz::fuzz;
use rand::{self, SeedableRng};
use sp_npos_elections::{
assignment_ratio_to_staked_normalized, is_score_better, phragmms, to_supports,
to_without_backing, EvaluateSupport, VoteWeight,
};
use sp_runtime::Perbill;
use rand::{self, SeedableRng};
fn main() {
loop {
fuzz!(|data: (usize, usize, usize, usize, u64)| {
let (
mut target_count,
mut voter_count,
mut iterations,
mut to_elect,
seed,
) = data;
let (mut target_count, mut voter_count, mut iterations, mut to_elect, seed) = data;
let rng = rand::rngs::SmallRng::seed_from_u64(seed);
target_count = to_range(target_count, 100, 200);
voter_count = to_range(voter_count, 100, 200);
@@ -48,12 +42,7 @@ fn main() {
"++ [voter_count: {} / target_count:{} / to_elect:{} / iterations:{}]",
voter_count, target_count, to_elect, iterations,
);
let (
unbalanced,
candidates,
voters,
stake_of_tree,
) = generate_random_npos_result(
let (unbalanced, candidates, voters, stake_of_tree) = generate_random_npos_result(
voter_count as u64,
target_count as u64,
to_elect,
@@ -61,9 +50,7 @@ fn main() {
ElectionType::Phragmms(None),
);
let stake_of = |who: &AccountId| -> VoteWeight {
*stake_of_tree.get(who).unwrap()
};
let stake_of = |who: &AccountId| -> VoteWeight { *stake_of_tree.get(who).unwrap() };
let unbalanced_score = {
let staked = assignment_ratio_to_staked_normalized(
@@ -76,7 +63,7 @@ fn main() {
if score[0] == 0 {
// such cases cannot be improved by balancing.
return;
return
}
score
};
@@ -86,34 +73,30 @@ fn main() {
candidates,
voters,
Some((iterations, 0)),
).unwrap();
)
.unwrap();
let balanced_score = {
let staked =
assignment_ratio_to_staked_normalized(balanced.assignments.clone(), &stake_of)
.unwrap();
let winners = to_without_backing(balanced.winners);
to_supports(winners.as_ref(), staked.as_ref())
.unwrap()
.evaluate()
to_supports(winners.as_ref(), staked.as_ref()).unwrap().evaluate()
};
let enhance = is_score_better(balanced_score, unbalanced_score, Perbill::zero());
println!(
"iter = {} // {:?} -> {:?} [{}]",
iterations,
unbalanced_score,
balanced_score,
enhance,
iterations, unbalanced_score, balanced_score, enhance,
);
// The only guarantee of balancing is such that the first and third element of the score
// cannot decrease.
assert!(
balanced_score[0] >= unbalanced_score[0] &&
balanced_score[1] == unbalanced_score[1] &&
balanced_score[2] <= unbalanced_score[2]
balanced_score[1] == unbalanced_score[1] &&
balanced_score[2] <= unbalanced_score[2]
);
});
}
@@ -34,8 +34,8 @@ use honggfuzz::fuzz;
mod common;
use common::to_range;
use sp_npos_elections::{reduce, to_support_map, ExtendedBalance, StakedAssignment};
use rand::{self, Rng, RngCore, SeedableRng};
use sp_npos_elections::{reduce, to_support_map, ExtendedBalance, StakedAssignment};
type Balance = u128;
type AccountId = u64;
@@ -50,13 +50,8 @@ fn main() {
let rng = rand::rngs::SmallRng::seed_from_u64(seed);
target_count = to_range(target_count, 100, 1000);
voter_count = to_range(voter_count, 100, 2000);
let (assignments, winners) = generate_random_phragmen_assignment(
voter_count,
target_count,
8,
8,
rng
);
let (assignments, winners) =
generate_random_phragmen_assignment(voter_count, target_count, 8, 8, rng);
reduce_and_compare(&assignments, &winners);
});
}
@@ -82,23 +77,27 @@ fn generate_random_phragmen_assignment(
(1..=voter_count).for_each(|acc| {
let mut targets_to_chose_from = all_targets.clone();
let targets_to_chose = if edge_per_voter_var > 0 { rng.gen_range(
avg_edge_per_voter - edge_per_voter_var,
avg_edge_per_voter + edge_per_voter_var,
) } else { avg_edge_per_voter };
let targets_to_chose = if edge_per_voter_var > 0 {
rng.gen_range(
avg_edge_per_voter - edge_per_voter_var,
avg_edge_per_voter + edge_per_voter_var,
)
} else {
avg_edge_per_voter
};
let distribution = (0..targets_to_chose).map(|_| {
let target = targets_to_chose_from.remove(rng.gen_range(0, targets_to_chose_from.len()));
if winners.iter().find(|w| **w == target).is_none() {
winners.push(target.clone());
}
(target, rng.gen_range(1 * KSM, 100 * KSM))
}).collect::<Vec<(AccountId, ExtendedBalance)>>();
let distribution = (0..targets_to_chose)
.map(|_| {
let target =
targets_to_chose_from.remove(rng.gen_range(0, targets_to_chose_from.len()));
if winners.iter().find(|w| **w == target).is_none() {
winners.push(target.clone());
}
(target, rng.gen_range(1 * KSM, 100 * KSM))
})
.collect::<Vec<(AccountId, ExtendedBalance)>>();
assignments.push(StakedAssignment {
who: (acc as AccountId),
distribution,
});
assignments.push(StakedAssignment { who: (acc as AccountId), distribution });
});
(assignments, winners)
@@ -117,10 +116,7 @@ fn assert_assignments_equal(
}
}
fn reduce_and_compare(
assignment: &Vec<StakedAssignment<AccountId>>,
winners: &Vec<AccountId>,
) {
fn reduce_and_compare(assignment: &Vec<StakedAssignment<AccountId>>, winners: &Vec<AccountId>) {
let mut altered_assignment = assignment.clone();
let n = assignment.len() as u32;
let m = winners.len() as u32;
@@ -138,15 +134,13 @@ fn reduce_and_compare(
num_changed,
);
assert_assignments_equal(
winners,
&assignment,
&altered_assignment,
);
assert_assignments_equal(winners, &assignment, &altered_assignment);
}
fn assignment_len(assignments: &[StakedAssignment<AccountId>]) -> u32 {
let mut counter = 0;
assignments.iter().for_each(|x| x.distribution.iter().for_each(|_| counter += 1));
assignments
.iter()
.for_each(|x| x.distribution.iter().for_each(|_| counter += 1));
counter
}