Skip to content

Commit

Permalink
feat: add delay and log CLI args (#442)
Browse files Browse the repository at this point in the history
* feat: add `delay` and `log` CLI args

* remove unused deps: paste

* fix tests

* parse --log correctly
  • Loading branch information
niklasad1 authored Jan 11, 2023
1 parent fcbbeef commit 50d05fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::prelude::sp_core;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to parse log directive: `{0}´")]
LogParse(#[from] tracing_subscriber::filter::ParseError),
#[error("I/O error: `{0}`")]
Io(#[from] std::io::Error),
#[error("RPC error: `{0}`")]
Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ use tracing_subscriber::EnvFilter;

#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init();
let Opt { uri, command, prometheus_port, log } = Opt::parse();
let filter = EnvFilter::from_default_env().add_directive(log.parse()?);
tracing_subscriber::fmt().with_env_filter(filter).init();

let Opt { uri, command, prometheus_port } = Opt::parse();
log::debug!(target: LOG_TARGET, "attempting to connect to {:?}", uri);

let rpc = loop {
Expand Down Expand Up @@ -213,6 +214,8 @@ mod tests {
"//Alice",
"--listen",
"head",
"--delay",
"12",
"seq-phragmen",
])
.unwrap();
Expand All @@ -222,11 +225,13 @@ mod tests {
Opt {
uri: "hi".to_string(),
prometheus_port: Some(9999),
log: "info".to_string(),
command: Command::Monitor(MonitorConfig {
listen: Listen::Head,
solver: Solver::SeqPhragmen { iterations: 10 },
submission_strategy: SubmissionStrategy::IfLeading,
seed_or_path: "//Alice".to_string(),
delay: 12,
dry_run: false,
}),
}
Expand All @@ -251,6 +256,7 @@ mod tests {
Opt {
uri: "hi".to_string(),
prometheus_port: None,
log: "info".to_string(),
command: Command::DryRun(DryRunConfig {
at: None,
solver: Solver::PhragMMS { iterations: 10 },
Expand Down Expand Up @@ -280,10 +286,11 @@ mod tests {
Opt {
uri: "hi".to_string(),
prometheus_port: None,
log: "info".to_string(),
command: Command::EmergencySolution(EmergencySolutionConfig {
take: Some(99),
at: None,
solver: Solver::PhragMMS { iterations: 1337 }
solver: Solver::PhragMMS { iterations: 1337 },
}),
}
);
Expand Down
1 change: 1 addition & 0 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ async fn mine_and_submit_solution<T>(
return
}

tokio::time::sleep(std::time::Duration::from_secs(config.delay as u64)).await;
let _lock = submit_lock.lock().await;

let (solution, score) =
Expand Down
18 changes: 18 additions & 0 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ pub struct MonitorConfig {
#[clap(long, short, env = "SEED")]
pub seed_or_path: String,

/// Delay in number seconds to wait until starting mining a solution.
///
/// At every block when a solution is attempted
/// a delay can be enforced to avoid submitting at
/// "same time" and risk potential races with other miners.
///
/// When this is enabled and there are competing solutions, your solution might not be submitted
/// if the scores are equal.
#[clap(long, default_value_t = 0)]
pub delay: usize,

/// Verify the submission by `dry-run` the extrinsic to check the validity.
/// If the extrinsic is invalid then the submission is ignored and the next block will attempted again.
///
Expand Down Expand Up @@ -288,4 +299,11 @@ pub struct Opt {
/// The prometheus endpoint TCP port.
#[clap(long, short, env = "PROMETHEUS_PORT")]
pub prometheus_port: Option<u16>,

/// Sets a custom logging filter. Syntax is `<target>=<level>`, e.g. -lstaking-miner=debug.
///
/// Log levels (least to most verbose) are error, warn, info, debug, and trace.
/// By default, all targets log `info`. The global log level can be set with `-l<level>`.
#[clap(long, short, default_value = "info")]
pub log: String,
}

0 comments on commit 50d05fb

Please sign in to comment.