-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
223880
committed
Oct 1, 2024
1 parent
2ad2164
commit 004efb2
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::sync::{Arc, Mutex}; | ||
use std::collections::HashMap; | ||
pub struct ASIC { | ||
pub id: usize, | ||
pub hash_rate: usize, | ||
pub hash_rate_lock: Arc<Mutex<usize>>, | ||
pub shares: Arc<Mutex<HashMap<usize, usize>>>, | ||
pub difficulty: u64, | ||
} | ||
|
||
impl ASIC { | ||
pub fn new(id: usize, hash_rate: usize) -> Self { | ||
ASIC { | ||
id, | ||
hash_rate, | ||
hash_rate_lock: Arc::new(Mutex::new(0)), | ||
shares: Arc::new(Mutex::new(HashMap::new())), | ||
difficulty: 1, | ||
} | ||
} | ||
pub fn hash_rate(&self) -> u64 { | ||
*self.hash_rate_lock.lock().unwrap() as u64 | ||
} | ||
|
||
pub fn mine(&self, nonce: usize) -> bool { | ||
// Example placeholder implementation of mining logic | ||
use sha2::{Sha256, Digest}; | ||
let mut hasher = Sha256::new(); | ||
hasher.update(format!("{}", nonce).as_bytes()); | ||
let hash_result = format!("{:x}", hasher.finalize()); | ||
|
||
// Convert hash_result from hexadecimal string to u64 | ||
let hash_as_u64 = u64::from_str_radix(&hash_result[..16], 16).unwrap_or(0); | ||
|
||
// Check if hash_as_u64 is divisible by self.difficulty | ||
let success = hash_as_u64 % self.difficulty == 0; | ||
|
||
success | ||
} | ||
} |