1extern crate crypto;
2extern crate sha2;
3
4use std::collections::HashMap;
6use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
7
8
9
10pub struct Miner {
11 pub id: usize,
12 pub hash_power: usize,
13 pub hash_rate: AtomicUsize,
14 pub shares: Arc<HashMap<usize, usize>>,
15 pub difficulty: u64,
16}
17
18impl Miner {
19 pub fn new(id: usize, hash_power: usize) -> Self {
20 Miner {
21 id,
22 hash_power,
23 hash_rate: AtomicUsize::new(0),
24 shares: Arc::new(HashMap::new()),
25 difficulty: 1,
26 }
27 }
28
29 pub fn hash_rate(&self) -> u64 {
30 self.hash_rate.load(Ordering::SeqCst) as u64
31 }
32
33 pub fn mine(&self, nonce: usize) -> bool {
34 use sha2::{Sha256, Digest};
36 let mut hasher = Sha256::new();
37 hasher.update(format!("{}", nonce).as_bytes());
38 let hash_result = format!("{:x}", hasher.finalize());
39
40 let hash_as_u64 = u64::from_str_radix(&hash_result[..16], 16).unwrap_or(0);
42
43 let success = hash_as_u64 % self.difficulty == 0;
45
46 success
47 }}
48
49 pub mod asic {
50 pub struct Asic;
51
52 impl Asic {
53 pub fn run() {
54 println!("Running the ASIC miner...");
55 }
56 }
57 }
58
59 pub mod braidpool {
60 pub struct BraidPool;
61 impl BraidPool {
62 pub fn run() {
63 println!("Running the BraidPool miner...");
64 }
65 pub fn new() -> Self {
66 BraidPool
67 }
68 }
69
70impl Default for BraidPool {
71 fn default() -> Self {
72 BraidPool::new()
73 }
74}
75 }
76
77pub mod datum {
78 pub struct Datum;
79
80 impl Datum {
81 pub fn run() {
82 println!("Running the Datum miner...");
83 }
84
85 }
86}