Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
223880 committed Oct 1, 2024
1 parent 2ad2164 commit 004efb2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/asic/asic/mod.rs
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
}
}

0 comments on commit 004efb2

Please sign in to comment.