use mohan::{
hash::H256,
U256
};
use std::sync::atomic::{AtomicUsize, Ordering};
pub trait Worker: Send {
fn init(&mut self, message: &[u8], nonce: u64, target: &U256);
fn nonce(&mut self, nonce: u64);
fn proceed(&mut self) -> Option<Vec<Vec<u8>>>;
fn is_finished(&self) -> bool;
}
static JOB_ID: AtomicUsize = AtomicUsize::new(0);
pub fn work(hash: &H256, target: &U256, mut worker: Box<dyn Worker>, jobs: usize) -> Option<Vec<Vec<u8>>> {
let id = JOB_ID.fetch_add(1, Ordering::SeqCst);
info!("Starting a new Job {} with hash {}, target: {}", id, hash, target);
worker.init(hash.as_bytes(), 0, target);
for nonce in 0..=u64::max_value() {
worker.nonce(nonce);
while !worker.is_finished() {
if JOB_ID.load(Ordering::SeqCst) > id + jobs {
info!("A new job submitted. Stopping the job {}", id);
return None
}
match worker.proceed() {
Some(solution) => {
info!("Nonce: {}", nonce);
return Some(solution)
}
None => {}
}
}
}
info!("Could not find the solution for hash {}", hash);
None
}