Skip to content

Commit ed68622

Browse files
committed
fix: min difficulty in multithreading
1 parent 98c801c commit ed68622

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/mine.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{sync::Arc, time::Instant};
1+
use std::{sync::Arc, sync::RwLock, time::Instant};
22

33
use colored::*;
44
use drillx::{
@@ -80,8 +80,10 @@ impl Miner {
8080
// Dispatch job to each thread
8181
let progress_bar = Arc::new(spinner::new_progress_bar());
8282
progress_bar.set_message("Mining...");
83+
let global_best_difficulty = Arc::new(RwLock::new(0u32));
8384
let handles: Vec<_> = (0..threads)
8485
.map(|i| {
86+
let global_best_difficulty = Arc::clone(&global_best_difficulty);
8587
std::thread::spawn({
8688
let proof = proof.clone();
8789
let progress_bar = progress_bar.clone();
@@ -104,13 +106,17 @@ impl Miner {
104106
best_nonce = nonce;
105107
best_difficulty = difficulty;
106108
best_hash = hx;
109+
if best_difficulty.gt(&*global_best_difficulty.read().unwrap()) {
110+
*global_best_difficulty.write().unwrap() = best_difficulty;
111+
}
107112
}
108113
}
109114

110115
// Exit if time has elapsed
111116
if nonce % 100 == 0 {
117+
let global_best_difficulty = *global_best_difficulty.read().unwrap();
112118
if timer.elapsed().as_secs().ge(&cutoff_time) {
113-
if best_difficulty.ge(&min_difficulty) {
119+
if global_best_difficulty.ge(&min_difficulty) {
114120
// Mine until min difficulty has been met
115121
break;
116122
}

0 commit comments

Comments
 (0)