Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/mine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{sync::Arc, time::Instant};
use std::{sync::Arc, sync::RwLock, time::Instant};

use colored::*;
use drillx::{
Expand Down Expand Up @@ -81,8 +81,10 @@ impl Miner {
// Dispatch job to each thread
let progress_bar = Arc::new(spinner::new_progress_bar());
progress_bar.set_message("Mining...");
let global_best_difficulty = Arc::new(RwLock::new(0u32));
let handles: Vec<_> = (0..threads)
.map(|i| {
let global_best_difficulty = Arc::clone(&global_best_difficulty);
std::thread::spawn({
let proof = proof.clone();
let progress_bar = progress_bar.clone();
Expand All @@ -105,19 +107,32 @@ impl Miner {
best_nonce = nonce;
best_difficulty = difficulty;
best_hash = hx;
if best_difficulty.gt(&*global_best_difficulty.read().unwrap()) {
*global_best_difficulty.write().unwrap() = best_difficulty;
}
}
}

// Exit if time has elapsed
if nonce % 100 == 0 {
let global_best_difficulty = *global_best_difficulty.read().unwrap();
if timer.elapsed().as_secs().ge(&cutoff_time) {
if best_difficulty.ge(&min_difficulty) {
if i == 0 {
progress_bar.set_message(format!(
"Mining... ({} / {} difficulty)",
global_best_difficulty,
min_difficulty,
));
}
if global_best_difficulty.ge(&min_difficulty) {
// Mine until min difficulty has been met
break;
}
} else if i == 0 {
progress_bar.set_message(format!(
"Mining... ({} sec remaining)",
"Mining... ({} / {} difficulty, {} sec remaining)",
global_best_difficulty,
min_difficulty,
cutoff_time.saturating_sub(timer.elapsed().as_secs()),
));
}
Expand Down