Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Implement info command
Browse files Browse the repository at this point in the history
fix #5560
  • Loading branch information
chevdor committed Jun 21, 2022
1 parent 713eb8c commit 69ff865
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 50 deletions.
11 changes: 11 additions & 0 deletions utils/staking-miner/src/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[derive(Debug, serde::Serialize)]
pub(crate) struct Info {
pub spec_name: String,
pub spec_version: u32,
}

impl Info {
pub fn new(spec_name: String, spec_version: u32) -> Self {
Self { spec_name, spec_version }
}
}
43 changes: 30 additions & 13 deletions utils/staking-miner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
mod dry_run;
mod emergency_solution;
mod info;
mod monitor;
mod opts;
mod prelude;
Expand All @@ -43,6 +44,7 @@ use crate::opts::*;
use clap::Parser;
use frame_election_provider_support::NposSolver;
use frame_support::traits::Get;
use info::*;
use jsonrpsee::ws_client::{WsClient, WsClientBuilder};
use remote_externalities::{Builder, Mode, OnlineConfig};
use rpc::{RpcApiClient, SharedRpcClient};
Expand Down Expand Up @@ -437,7 +439,7 @@ 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 { uri, seed_or_path, command } = Opt::parse();
let Opt { uri, command } = Opt::parse();
log::debug!(target: LOG_TARGET, "attempting to connect to {:?}", uri);

let rpc = loop {
Expand Down Expand Up @@ -503,26 +505,41 @@ async fn main() {
check_versions::<Runtime>(&rpc).await
};

let signer_account = any_runtime! {
signer::signer_uri_from_string::<Runtime>(&seed_or_path, &rpc)
.await
.expect("Provided account is invalid, terminating.")
};

let outcome = any_runtime! {
match command {
Command::Monitor(cmd) => monitor_cmd(rpc, cmd, signer_account).await
Command::Monitor(monitor_config) =>
{
let signer_account = any_runtime! {
signer::signer_uri_from_string::<Runtime>(&monitor_config.seed_or_path , &rpc)
.await
.expect("Provided account is invalid, terminating.")
};
monitor_cmd(rpc, monitor_config, signer_account).await
.map_err(|e| {
log::error!(target: LOG_TARGET, "Monitor error: {:?}", e);
}),
Command::DryRun(cmd) => dry_run_cmd(rpc, cmd, signer_account).await
})},
Command::DryRun(dryrun_config) => {
let signer_account = any_runtime! {
signer::signer_uri_from_string::<Runtime>(&dryrun_config.seed_or_path , &rpc)
.await
.expect("Provided account is invalid, terminating.")
};
dry_run_cmd(rpc, dryrun_config, signer_account).await
.map_err(|e| {
log::error!(target: LOG_TARGET, "DryRun error: {:?}", e);
}),
Command::EmergencySolution(cmd) => emergency_solution_cmd(rpc, cmd).await
})},
Command::EmergencySolution(emergency_solution_config) => {
emergency_solution_cmd(rpc, emergency_solution_config).await
.map_err(|e| {
log::error!(target: LOG_TARGET, "EmergencySolution error: {:?}", e);
}),
})},
Command::Info(_info_opts) => {
let runtime_version: RuntimeVersion = rpc.runtime_version(None).await.expect("runtime_version infallible; qed.");
let info = Info::new(runtime_version.spec_name.to_string(), runtime_version.spec_version);
let info = serde_json::to_string_pretty(&info).expect("Failed serializing infos");
println!("{}", info);
Ok(())
}
}
};
log::info!(target: LOG_TARGET, "round of execution finished. outcome = {:?}", outcome);
Expand Down
93 changes: 56 additions & 37 deletions utils/staking-miner/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,47 @@ use clap::Parser;
use sp_runtime::Perbill;
use std::str::FromStr;

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
#[clap(author, version, about)]
pub(crate) struct Opt {
/// The `ws` node to connect to.
#[clap(long, short, default_value = DEFAULT_URI, env = "URI")]
pub uri: String,

#[clap(subcommand)]
pub command: Command,
}

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) enum Command {
/// Monitor for the phase being signed, then compute.
Monitor(MonitorConfig),

/// Just compute a solution now, and don't submit it.
DryRun(DryRunConfig),

/// Provide a solution that can be submitted to the chain as an emergency response.
EmergencySolution(EmergencySolutionConfig),

/// Return information about the current version
Info(InfoOpts),
}

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) struct MonitorConfig {
/// The path to a file containing the seed of the account. If the file is not found, the seed is
/// used as-is.
///
/// Can also be provided via the `SEED` environment variable.
///
/// WARNING: Don't use an account with a large stash for this. Based on how the bot is
/// configured, it might re-try and lose funds through transaction fees/deposits.
#[clap(long, short, env = "SEED")]
pub seed_or_path: String,

/// They type of event to listen to.
///
/// Typically, finalized is safer and there is no chance of anything going wrong, but it can be
Expand Down Expand Up @@ -33,7 +71,17 @@ pub(crate) struct MonitorConfig {

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) struct EmergencySolutionConfig {
pub(crate) struct DryRunConfig {
/// The path to a file containing the seed of the account. If the file is not found, the seed is
/// used as-is.
///
/// Can also be provided via the `SEED` environment variable.
///
/// WARNING: Don't use an account with a large stash for this. Based on how the bot is
/// configured, it might re-try and lose funds through transaction fees/deposits.
#[clap(long, short, env = "SEED")]
pub seed_or_path: String,

/// The block hash at which scraping happens. If none is provided, the latest head is used.
#[clap(long)]
pub at: Option<Hash>,
Expand All @@ -42,13 +90,14 @@ pub(crate) struct EmergencySolutionConfig {
#[clap(subcommand)]
pub solver: Solver,

/// The number of top backed winners to take. All are taken, if not provided.
pub take: Option<usize>,
/// Force create a new snapshot, else expect one to exist onchain.
#[clap(long)]
pub force_snapshot: bool,
}

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) struct DryRunConfig {
pub(crate) struct EmergencySolutionConfig {
/// The block hash at which scraping happens. If none is provided, the latest head is used.
#[clap(long)]
pub at: Option<Hash>,
Expand All @@ -57,32 +106,13 @@ pub(crate) struct DryRunConfig {
#[clap(subcommand)]
pub solver: Solver,

/// Force create a new snapshot, else expect one to exist onchain.
#[clap(long)]
pub force_snapshot: bool,
/// The number of top backed winners to take. All are taken, if not provided.
pub take: Option<usize>,
}

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
#[clap(author, version, about)]
pub(crate) struct Opt {
/// The `ws` node to connect to.
#[clap(long, short, default_value = DEFAULT_URI, env = "URI")]
pub uri: String,

/// The path to a file containing the seed of the account. If the file is not found, the seed is
/// used as-is.
///
/// Can also be provided via the `SEED` environment variable.
///
/// WARNING: Don't use an account with a large stash for this. Based on how the bot is
/// configured, it might re-try and lose funds through transaction fees/deposits.
#[clap(long, short, env = "SEED")]
pub seed_or_path: String,

#[clap(subcommand)]
pub command: Command,
}
pub(crate) struct InfoOpts {}

/// Submission strategy to use.
#[derive(Debug, Copy, Clone)]
Expand All @@ -97,17 +127,6 @@ pub enum SubmissionStrategy {
ClaimBetterThan(Perbill),
}

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) enum Command {
/// Monitor for the phase being signed, then compute.
Monitor(MonitorConfig),
/// Just compute a solution now, and don't submit it.
DryRun(DryRunConfig),
/// Provide a solution that can be submitted to the chain as an emergency response.
EmergencySolution(EmergencySolutionConfig),
}

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) enum Solver {
Expand Down

0 comments on commit 69ff865

Please sign in to comment.