use clap3 instead of structopt (#10632)

* use clap3 instead of structopt

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* format

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* update ss58-registry and revert some nits

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Fix clippy and doc

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* update clap to 3.0.7

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Apply review suggestions

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* remove useless option long name

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* cargo fmt

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
Qinxuan Chen
2022-01-25 00:28:46 +08:00
committed by GitHub
parent d1ff02d31e
commit e327b734bc
66 changed files with 660 additions and 768 deletions
@@ -68,7 +68,7 @@ pub fn generate_random_npos_inputs(
// always generate a sensible desired number of candidates: elections are uninteresting if we
// desire 0 candidates, or a number of candidates >= the actual number of candidates present
let rounds = rng.gen_range(1, candidate_count);
let rounds = rng.gen_range(1..candidate_count);
// candidates are easy: just a completely random set of IDs
let mut candidates: Vec<AccountId> = Vec::with_capacity(candidate_count);
@@ -95,7 +95,7 @@ pub fn generate_random_npos_inputs(
let vote_weight = rng.gen();
// it's not interesting if a voter chooses 0 or all candidates, so rule those cases out.
let n_candidates_chosen = rng.gen_range(1, candidates.len());
let n_candidates_chosen = rng.gen_range(1..candidates.len());
let mut chosen_candidates = Vec::with_capacity(n_candidates_chosen);
chosen_candidates.extend(candidates.choose_multiple(&mut rng, n_candidates_chosen));
@@ -132,25 +132,25 @@ pub fn generate_random_npos_result(
(1..=target_count).for_each(|acc| {
candidates.push(acc);
let stake_var = rng.gen_range(ed, 100 * ed);
let stake_var = rng.gen_range(ed..100 * ed);
stake_of.insert(acc, base_stake + stake_var);
});
let mut voters = Vec::with_capacity(voter_count as usize);
(prefix..=(prefix + voter_count)).for_each(|acc| {
let edge_per_this_voter = rng.gen_range(1, candidates.len());
let edge_per_this_voter = rng.gen_range(1..candidates.len());
// all possible targets
let mut all_targets = candidates.clone();
// we remove and pop into `targets` `edge_per_this_voter` times.
let targets = (0..edge_per_this_voter)
.map(|_| {
let upper = all_targets.len() - 1;
let idx = rng.gen_range(0, upper);
let idx = rng.gen_range(0..upper);
all_targets.remove(idx)
})
.collect::<Vec<AccountId>>();
let stake_var = rng.gen_range(ed, 100 * ed);
let stake_var = rng.gen_range(ed..100 * ed);
let stake = base_stake + stake_var;
stake_of.insert(acc, stake);
voters.push((acc, stake, targets));
@@ -42,7 +42,7 @@
use honggfuzz::fuzz;
#[cfg(not(fuzzing))]
use structopt::StructOpt;
use clap::Parser;
mod common;
use common::{generate_random_npos_inputs, to_range};
@@ -67,24 +67,25 @@ fn main() {
}
#[cfg(not(fuzzing))]
#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
#[clap(author, version, about)]
struct Opt {
/// How many candidates participate in this election
#[structopt(short, long)]
#[clap(short, long)]
candidates: Option<usize>,
/// How many voters participate in this election
#[structopt(short, long)]
#[clap(short, long)]
voters: Option<usize>,
/// Random seed to use in this election
#[structopt(long)]
#[clap(long)]
seed: Option<u64>,
}
#[cfg(not(fuzzing))]
fn main() {
let opt = Opt::from_args();
let opt = Opt::parse();
// candidates and voters by default use the maxima, which turn out to be one less than
// the constant.
iteration(
@@ -79,8 +79,7 @@ fn generate_random_phragmen_assignment(
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,
avg_edge_per_voter - edge_per_voter_var..avg_edge_per_voter + edge_per_voter_var,
)
} else {
avg_edge_per_voter
@@ -89,11 +88,11 @@ fn generate_random_phragmen_assignment(
let distribution = (0..targets_to_chose)
.map(|_| {
let target =
targets_to_chose_from.remove(rng.gen_range(0, targets_to_chose_from.len()));
targets_to_chose_from.remove(rng.gen_range(0..targets_to_chose_from.len()));
if winners.iter().all(|w| *w != target) {
winners.push(target.clone());
}
(target, rng.gen_range(1 * KSM, 100 * KSM))
(target, rng.gen_range(1 * KSM..100 * KSM))
})
.collect::<Vec<(AccountId, ExtendedBalance)>>();