|
1 | 1 | use std::collections::HashMap; |
2 | | -use std::collections::HashMap; |
3 | 2 | use std::sync::Arc; |
4 | 3 | use std::sync::atomic::{AtomicUsize, Ordering}; |
5 | 4 |
|
6 | | - pub struct Miner { |
| 5 | +pub struct Miner { |
7 | 6 | pub id: usize, |
8 | | - pub struct Miner { |
9 | | - pub hash_power: usize, |
10 | | - pub hash_rate: AtomicUsize, |
11 | | - pub shares: Arc<HashMap<usize, usize>>, |
12 | | - pub difficulty: usize, |
13 | | - pub difficulty: u64, |
14 | | - } |
| 7 | + pub hash_power: usize, |
| 8 | + pub hash_rate: AtomicUsize, |
| 9 | + pub shares: Arc<HashMap<usize, usize>>, |
| 10 | + pub difficulty: u64, |
15 | 11 | } |
16 | 12 |
|
17 | | - impl Miner { |
18 | | - |
| 13 | +impl Miner { |
19 | 14 | pub fn new(id: usize, hash_power: usize) -> Self { |
20 | | - impl Miner { |
21 | | - difficulty: 1, |
22 | | - } |
| 15 | + Miner { |
| 16 | + id, |
| 17 | + hash_power, |
| 18 | + hash_rate: AtomicUsize::new(0), |
| 19 | + shares: Arc::new(HashMap::new()), |
| 20 | + difficulty: 1, |
| 21 | + } |
23 | 22 | } |
24 | | -} |
25 | | - |
26 | | - pub fn hash_rate(&self) -> usize { |
27 | | -- self.hash_rate.load(Ordering::SeqCst) |
28 | | - self.hash_rate.load(Ordering::SeqCst) as u64 |
29 | | - } |
30 | | - |
31 | | - pub fn mine(&self, nonce: usize) -> bool { |
32 | 23 |
|
33 | | - let mut hash = nonce; |
34 | | - impl Miner { |
35 | | - self.hash_rate.store(hash_rate, Ordering::SeqCst); |
36 | | - true |
37 | | - |
38 | | - } else { |
39 | | - false |
40 | | - false |
41 | | - } |
| 24 | + pub fn hash_rate(&self) -> usize { |
| 25 | + self.hash_rate.load(Ordering::SeqCst) |
| 26 | + } |
42 | 27 |
|
| 28 | + pub fn mine(&self, nonce: usize) -> bool { |
| 29 | + let hash = nonce; // Example: the actual hashing algorithm should replace this |
| 30 | + if hash < self.difficulty as usize { |
| 31 | + self.hash_rate.store(hash, Ordering::SeqCst); |
| 32 | + true |
| 33 | + } else { |
| 34 | + false |
43 | 35 | } |
44 | | - |
| 36 | + } |
| 37 | +} |
0 commit comments