Skip to content

Commit

Permalink
cleanup and fix cli.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon1777 committed Aug 25, 2024
1 parent 24279ce commit 28d7100
Showing 1 changed file with 65 additions and 50 deletions.
115 changes: 65 additions & 50 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,59 @@ use log::LevelFilter;
use crate::Error;

#[derive(Parser, Debug)]
#[clap(name = "karlsen-miner", version, about = "A Karlsen high performance CPU/GPU miner", term_width = 0)]
#[clap(
name = "karlsen-miner",
version,
about = "A Karlsen high-performance CPU/GPU miner",
term_width = 0
)]
pub struct Opt {
#[clap(short, long, help = "Enable debug logging level")]
pub debug: bool,
#[clap(short = 'a', long = "mining-address", help = "The Karlsen address for the miner reward")]

#[clap(short = 'a', long = "mining-address", help = "The Karlsen address for miner rewards")]
pub mining_address: String,

#[clap(
short = 's',
long = "karlsend-address",
default_value = "127.0.0.1",
help = "The IP of the karlsend instance"
help = "The IP address, pool address, or node address of the karlsend instance"
)]
pub karlsend_address: String,

#[clap(long = "devfund-percent", help = "The percentage of blocks to send to the devfund (minimum 0%)", default_value = "0", parse(try_from_str = parse_devfund_percent))]
#[clap(
long = "devfund-percent",
default_value = "0",
help = "Percentage of blocks to send to the devfund (minimum 0%)",
parse(try_from_str = parse_devfund_percent)
)]
pub devfund_percent: u16,

#[clap(short, long, help = "karlsend port [default: Mainnet = 42110, Testnet = 42111, devnet = 42610]")]
#[clap(
short,
long,
help = "karlsend port [default: Mainnet = 42110, Testnet = 42111, Devnet = 42610]"
)]
port: Option<u16>,

#[clap(long, help = "Use testnet instead of mainnet [default: false]")]
testnet: bool,

#[clap(long, help = "Use devnet instead of mainnet [default: false]")]
devnet: bool,
#[clap(short = 't', long = "threads", help = "Amount of CPU miner threads to launch [default: 0]")]

#[clap(
short = 't',
long = "threads",
help = "Number of CPU miner threads to launch [default: 0]"
)]
pub num_threads: Option<u16>,

#[clap(
long = "mine-when-not-synced",
help = "Mine even when karlsend says it is not synced",
long_help = "Mine even when karlsend says it is not synced, only useful when passing `--allow-submit-block-when-not-synced` to karlsend [default: false]"
help = "Mine even when karlsend is not synced",
long_help = "Mine even when karlsend is not synced; useful when passing `--allow-submit-block-when-not-synced` to karlsend [default: false]"
)]
pub mine_when_not_synced: bool,

Expand All @@ -42,77 +65,69 @@ pub struct Opt {
}

fn parse_devfund_percent(s: &str) -> Result<u16, &'static str> {
let err = "devfund-percent should be --devfund-percent=XX.YY up to 2 numbers after the dot";
let mut splited = s.split('.');
let prefix = splited.next().ok_or(err)?;
// if there's no postfix then it's 0.
let postfix = splited.next().ok_or(err).unwrap_or("0");
// error if there's more than a single dot
if splited.next().is_some() {
return Err(err);
};
// error if there are more than 2 numbers before or after the dot
if prefix.len() > 2 || postfix.len() > 2 {
let err = "devfund-percent should be formatted as XX.YY, with up to two digits after the decimal";
let mut parts = s.split('.');

let prefix = parts.next().ok_or(err)?;
let postfix = parts.next().unwrap_or("0");

if parts.next().is_some() || prefix.len() > 2 || postfix.len() > 2 {
return Err(err);
}
let postfix: u16 = postfix.parse().map_err(|_| err)?;

let prefix: u16 = prefix.parse().map_err(|_| err)?;
// can't be more than 99.99%,
let postfix: u16 = postfix.parse().map_err(|_| err)?;

if prefix >= 100 || postfix >= 100 {
return Err(err);
}
/*
if prefix < 2 {
// Force at least 2 percent
return Ok(200u16);
}
*/
// DevFund is out of 10_000

Ok(prefix * 100 + postfix)
}

impl Opt {
pub fn process(&mut self) -> Result<(), Error> {
//self.gpus = None;
if self.karlsend_address.is_empty() {
self.karlsend_address = "127.0.0.1".to_string();
}

if !self.karlsend_address.contains("://") {
let port_str = self.port().to_string();
let (karlsend, port) = match self.karlsend_address.contains(':') {
true => self.karlsend_address.split_once(':').expect("We checked for `:`"),
false => (self.karlsend_address.as_str(), port_str.as_str()),
};
let (karlsend, port) = self.karlsend_address.split_once(':')
.unwrap_or((self.karlsend_address.as_str(), port_str.as_str()));

self.karlsend_address = format!("grpc://{}:{}", karlsend, port);
}
log::info!("karlsend address: {}", self.karlsend_address);

if self.num_threads.is_none() {
self.num_threads = Some(0);
}
self.num_threads.get_or_insert(0);

let miner_network = self.mining_address.split(':').next();
self.devfund_address = String::from("karlsen:qzrq7v5jhsc5znvtfdg6vxg7dz5x8dqe4wrh90jkdnwehp6vr8uj7csdss2l7");
self.devfund_address = "karlsen:qzrq7v5jhsc5znvtfdg6vxg7dz5x8dqe4wrh90jkdnwehp6vr8uj7csdss2l7".to_string();
let devfund_network = self.devfund_address.split(':').next();
if miner_network.is_some() && devfund_network.is_some() && miner_network != devfund_network {
self.devfund_percent = 0;
log::info!(
"Mining address ({}) and devfund ({}) are not from the same network. Disabling devfund.",
miner_network.unwrap(),
devfund_network.unwrap()
)

if let (Some(miner_net), Some(devfund_net)) = (miner_network, devfund_network) {
if miner_net != devfund_net {
self.devfund_percent = 0;
log::info!(
"Mining address ({}) and devfund ({}) are not from the same network. Disabling devfund.",
miner_net,
devfund_net
);
}
}
Ok(())
}

fn port(&mut self) -> u16 {
*self.port.get_or_insert(if self.testnet {
42210
} else if self.devnet {
42610
} else {
42110
*self.port.get_or_insert_with(|| {
if self.testnet {
42210
} else if self.devnet {
42610
} else {
42110
}
})
}

Expand Down

0 comments on commit 28d7100

Please sign in to comment.