extern crate crypto;
extern crate sha2;
use std::collections::HashMap;
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
pub struct Miner {
pub id: usize,
pub hash_power: usize,
pub hash_rate: AtomicUsize,
pub shares: Arc<HashMap<usize, usize>>,
pub difficulty: u64,
}
impl Miner {
pub fn new(id: usize, hash_power: usize) -> Self {
Miner {
id,
hash_power,
hash_rate: AtomicUsize::new(0),
shares: Arc::new(HashMap::new()),
difficulty: 1,
}
}
pub fn hash_rate(&self) -> u64 {
self.hash_rate.load(Ordering::SeqCst) as u64
}
pub fn mine(&self, nonce: usize) -> bool {
use sha2::{Sha256, Digest};
let mut hasher = Sha256::new();
hasher.update(format!("{}", nonce).as_bytes());
let hash_result = format!("{:x}", hasher.finalize());
let hash_as_u64 = u64::from_str_radix(&hash_result[..16], 16).unwrap_or(0);
let success = hash_as_u64 % self.difficulty == 0;
success
}}
pub mod asic {
pub struct Asic;
impl Asic {
pub fn run() {
println!("Running the ASIC miner...");
}
}
}
pub mod braidpool {
pub struct BraidPool;
impl BraidPool {
pub fn run() {
println!("Running the BraidPool miner...");
}
pub fn new() -> Self {
BraidPool
}
}
impl Default for BraidPool {
fn default() -> Self {
BraidPool::new()
}
}
}
pub mod datum {
pub struct Datum;
impl Datum {
pub fn run() {
println!("Running the Datum miner...");
}
}
}