Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit d26fbf8

Browse files
author
bhat
committed
Add override pool.
1 parent e11bd53 commit d26fbf8

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/main.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ struct Generate {
7272
}
7373

7474
#[derive(Clap)]
75-
struct Run {}
75+
struct Run {
76+
#[clap(short, long)]
77+
pool: Option<String>,
78+
}
7679

7780
fn generate_8hex() -> String {
7881
const HEX_ARRAY: [char; 16] = [
@@ -138,17 +141,15 @@ async fn get_pool_info() -> Result<Pool, MinerError> {
138141
Ok(pool)
139142
}
140143

141-
async fn start_miner(device: Device) -> Result<(), MinerError> {
142-
let pool = get_pool_info().await?;
144+
async fn start_miner(device: Device, pool: String) -> Result<(), MinerError> {
145+
let heatup_duration: u64 = rand::thread_rng().gen_range(10..10000);
146+
tokio::time::sleep(Duration::from_millis(heatup_duration)).await;
143147

144-
let mut stream = TcpStream::connect(format!("{}:{}", pool.ip, pool.port))
148+
let mut stream = TcpStream::connect(&pool)
145149
.await
146150
.map_err(|_| MinerError::Connection)?;
147151

148-
info!(
149-
"{} connected to pool {}:{}",
150-
device.device_name, pool.ip, pool.port
151-
);
152+
info!("{} connected to pool {}", device.device_name, pool);
152153

153154
let mut cmd_in: [u8; 200] = [0; 200];
154155
let n = stream
@@ -270,12 +271,29 @@ async fn start_miner(device: Device) -> Result<(), MinerError> {
270271
}
271272
}
272273

273-
async fn start_miner_with_watchdog(device: Device) {
274+
async fn start_miners(devices: Vec<Device>, pool: Option<String>) {
274275
loop {
275-
let heatup_duration: u64 = rand::thread_rng().gen_range(0..10000);
276-
tokio::time::sleep(Duration::from_millis(heatup_duration)).await;
276+
let pool = if let Some(pool) = pool.clone() {
277+
pool
278+
} else {
279+
let pool = get_pool_info().await.unwrap_or(Pool {
280+
name: "Default pool".to_string(),
281+
ip: "server.duinocoin.com".to_string(),
282+
port: 2813,
283+
connections: 1,
284+
});
285+
286+
format!("{}:{}", pool.ip, pool.port)
287+
};
288+
289+
let mut futures_vec = Vec::new();
290+
291+
for device in &devices {
292+
let f = start_miner(device.clone(), pool.clone());
293+
futures_vec.push(f);
294+
}
277295

278-
match start_miner(device.clone()).await {
296+
match futures::future::try_join_all(futures_vec).await {
279297
Ok(_) => error!("exited without error"),
280298
Err(e) => error!("exited with error: {:?}", e),
281299
}
@@ -285,17 +303,6 @@ async fn start_miner_with_watchdog(device: Device) {
285303
}
286304
}
287305

288-
async fn start_miners(devices: Vec<Device>) {
289-
let mut futures_vec = Vec::new();
290-
291-
for device in devices {
292-
let f = start_miner_with_watchdog(device);
293-
futures_vec.push(f);
294-
}
295-
296-
futures::future::join_all(futures_vec).await;
297-
}
298-
299306
#[tokio::main]
300307
async fn main() -> Result<(), Box<dyn std::error::Error>> {
301308
pretty_env_logger::init();
@@ -306,13 +313,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
306313
SubCommands::Generate(gen) => {
307314
generate_config(opts.config_file, &gen).await?;
308315
}
309-
SubCommands::Run(_) => {
316+
SubCommands::Run(run) => {
310317
let c_serial = tokio::fs::read_to_string(opts.config_file).await?;
311318
let c: Config = serde_yaml::from_str(c_serial.as_str())?;
312319

313320
info!("running with {} miners", c.devices.len());
314321

315-
start_miners(c.devices).await;
322+
start_miners(c.devices, run.pool).await;
316323
}
317324
}
318325

0 commit comments

Comments
 (0)