use crate::worker::Worker;
use mohan::U256;
use crate::x::TigerVM;
pub struct Runner {
vm: TigerVM,
nonce: u64,
target: U256,
is_executed: bool,
last: [u8; 32]
}
impl Runner {
pub fn new() -> Self {
Self {
vm: TigerVM::new(&[]),
nonce: 0,
target: U256::zero(),
is_executed: false,
last: [0u8; 32]
}
}
}
impl Worker for Runner {
fn init(&mut self, seed: &[u8], nonce: u64, target: &U256) {
self.vm = TigerVM::new(seed);
self.nonce = nonce;
self.target = *target;
self.is_executed = false;
}
fn nonce(&mut self, nonce: u64) {
self.last = self.vm.calculate(&nonce.to_le_bytes());
}
fn proceed(&mut self) -> Option<Vec<Vec<u8>>> {
if self.is_finished() {
return None
}
self.is_executed = true;
let current_score = U256::from(self.last);
if current_score <= self.target {
info!("Solution found");
let nonce_bytes = self.nonce.to_le_bytes().to_vec();
return Some(vec![nonce_bytes])
}
trace!("Retry.\n score : {:#0128x}\n target: {:#0128x}", current_score, self.target);
None
}
fn is_finished(&self) -> bool {
self.is_executed
}
}