[staking miner]: add CLI tests + fix --version (#5024)

* staking miner: add CLI tests

* cargo fmt

* fix bad merge

* Update utils/staking-miner/Cargo.toml

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* grumbles: derive PartialEq only for tests

* make test `--version` a integration tests

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Niklas Adolfsson
2022-03-03 23:43:38 +01:00
committed by GitHub
parent 9eebcb8870
commit 3d5e9b3c9c
4 changed files with 118 additions and 20 deletions
+104 -19
View File
@@ -277,6 +277,7 @@ impl<T: EPM::Config> std::fmt::Display for Error<T> {
}
#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
enum Command {
/// Monitor for the phase being signed, then compute.
Monitor(MonitorConfig),
@@ -287,7 +288,8 @@ enum Command {
}
#[derive(Debug, Clone, Parser)]
enum Solvers {
#[cfg_attr(test, derive(PartialEq))]
enum Solver {
SeqPhragmen {
#[clap(long, default_value = "10")]
iterations: usize,
@@ -306,6 +308,7 @@ frame_support::parameter_types! {
}
#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
struct MonitorConfig {
/// They type of event to listen to.
///
@@ -317,10 +320,11 @@ struct MonitorConfig {
/// The solver algorithm to use.
#[clap(subcommand)]
solver: Solvers,
solver: Solver,
}
#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
struct EmergencySolutionConfig {
/// The block hash at which scraping happens. If none is provided, the latest head is used.
#[clap(long)]
@@ -328,13 +332,14 @@ struct EmergencySolutionConfig {
/// The solver algorithm to use.
#[clap(subcommand)]
solver: Solvers,
solver: Solver,
/// The number of top backed winners to take. All are taken, if not provided.
take: Option<usize>,
}
#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
struct DryRunConfig {
/// The block hash at which scraping happens. If none is provided, the latest head is used.
#[clap(long)]
@@ -342,7 +347,7 @@ struct DryRunConfig {
/// The solver algorithm to use.
#[clap(subcommand)]
solver: Solvers,
solver: Solver,
/// Force create a new snapshot, else expect one to exist onchain.
#[clap(long)]
@@ -350,7 +355,9 @@ struct DryRunConfig {
}
#[derive(Debug, Clone, Parser)]
struct SharedConfig {
#[cfg_attr(test, derive(PartialEq))]
#[clap(author, version, about)]
struct Opt {
/// The `ws` node to connect to.
#[clap(long, short, default_value = DEFAULT_URI, env = "URI")]
uri: String,
@@ -364,13 +371,6 @@ struct SharedConfig {
/// configured, it might re-try and lose funds through transaction fees/deposits.
#[clap(long, short, env = "SEED")]
seed_or_path: String,
}
#[derive(Debug, Clone, Parser)]
struct Opt {
/// The `ws` node to connect to.
#[clap(flatten)]
shared: SharedConfig,
#[clap(subcommand)]
command: Command,
@@ -433,7 +433,7 @@ where
/// Mine a solution with the given `solver`.
fn mine_with<T>(
solver: &Solvers,
solver: &Solver,
ext: &mut Ext,
do_feasibility: bool,
) -> Result<(EPM::RawSolution<EPM::SolutionOf<T>>, u32), Error<T>>
@@ -444,7 +444,7 @@ where
use frame_election_provider_support::{PhragMMS, SequentialPhragmen};
match solver {
Solvers::SeqPhragmen { iterations } => {
Solver::SeqPhragmen { iterations } => {
BalanceIterations::set(*iterations);
mine_solution::<
T,
@@ -455,7 +455,7 @@ where
>,
>(ext, do_feasibility)
},
Solvers::PhragMMS { iterations } => {
Solver::PhragMMS { iterations } => {
BalanceIterations::set(*iterations);
mine_solution::<
T,
@@ -530,11 +530,11 @@ pub(crate) async fn check_versions<T: frame_system::Config + EPM::Config>(
async fn main() {
fmt().with_env_filter(EnvFilter::from_default_env()).init();
let Opt { shared, command } = Opt::parse();
log::debug!(target: LOG_TARGET, "attempting to connect to {:?}", shared.uri);
let Opt { uri, seed_or_path, command } = Opt::parse();
log::debug!(target: LOG_TARGET, "attempting to connect to {:?}", uri);
let rpc = loop {
match SharedRpcClient::new(&shared.uri).await {
match SharedRpcClient::new(&uri).await {
Ok(client) => break client,
Err(why) => {
log::warn!(
@@ -597,7 +597,7 @@ async fn main() {
};
let signer_account = any_runtime! {
signer::signer_uri_from_string::<Runtime>(&shared.seed_or_path, &rpc)
signer::signer_uri_from_string::<Runtime>(&seed_or_path, &rpc)
.await
.expect("Provided account is invalid, terminating.")
};
@@ -644,4 +644,89 @@ mod tests {
assert_eq!(polkadot_version.spec_name, "polkadot".into());
assert_eq!(kusama_version.spec_name, "kusama".into());
}
#[test]
fn cli_monitor_works() {
let opt = Opt::try_parse_from([
env!("CARGO_PKG_NAME"),
"--uri",
"hi",
"--seed-or-path",
"//Alice",
"monitor",
"--listen",
"head",
"seq-phragmen",
])
.unwrap();
assert_eq!(
opt,
Opt {
uri: "hi".to_string(),
seed_or_path: "//Alice".to_string(),
command: Command::Monitor(MonitorConfig {
listen: "head".to_string(),
solver: Solver::SeqPhragmen { iterations: 10 }
}),
}
);
}
#[test]
fn cli_dry_run_works() {
let opt = Opt::try_parse_from([
env!("CARGO_PKG_NAME"),
"--uri",
"hi",
"--seed-or-path",
"//Alice",
"dry-run",
"phrag-mms",
])
.unwrap();
assert_eq!(
opt,
Opt {
uri: "hi".to_string(),
seed_or_path: "//Alice".to_string(),
command: Command::DryRun(DryRunConfig {
at: None,
solver: Solver::PhragMMS { iterations: 10 },
force_snapshot: false,
}),
}
);
}
#[test]
fn cli_emergency_works() {
let opt = Opt::try_parse_from([
env!("CARGO_PKG_NAME"),
"--uri",
"hi",
"--seed-or-path",
"//Alice",
"emergency-solution",
"99",
"phrag-mms",
"--iterations",
"1337",
])
.unwrap();
assert_eq!(
opt,
Opt {
uri: "hi".to_string(),
seed_or_path: "//Alice".to_string(),
command: Command::EmergencySolution(EmergencySolutionConfig {
take: Some(99),
at: None,
solver: Solver::PhragMMS { iterations: 1337 }
}),
}
);
}
}