From 51d82346d53a9b435fbbdb65ab0fceb8ce130b32 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Wed, 14 Apr 2021 00:09:29 +0300 Subject: Implement multithreading for example miner --- examples/mining.rs | 63 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/examples/mining.rs b/examples/mining.rs index 21fdc58..1d86a20 100644 --- a/examples/mining.rs +++ b/examples/mining.rs @@ -1,33 +1,66 @@ use chrono::NaiveDate; use gradecoin::schema::NakedBlock; use serde_json; +use std::sync::{Arc, Mutex}; +use std::thread; use std::time::Instant; use blake2::{Blake2s, Digest}; +const N: usize = 4; + pub fn main() { - let mut b = NakedBlock { - transaction_list: vec!["hash_value".to_owned()], - nonce: 0, - timestamp: NaiveDate::from_ymd(2021, 04, 08).and_hms(12, 30, 30), - }; + let counter = Arc::new(Mutex::new(0)); let now = Instant::now(); - for nonce in 0..u32::MAX { - b.nonce = nonce; + let mut threads = Vec::with_capacity(N); + + (0..N).for_each(|_| { + let counter = Arc::clone(&counter); + threads.push(thread::spawn(move || { + let mut b = NakedBlock { + transaction_list: vec!["e254275".to_owned()], + nonce: 0, + timestamp: NaiveDate::from_ymd(2021, 04, 13).and_hms(23, 38, 00), + }; + + let start: u32; + let end: u32; + { + let mut num = counter.lock().unwrap(); + + println!("Starting with 2 over {}", num); + + start = 0 + (1073741824 * *num); + end = 1073741820 * (*num + 1); + *num += 1; + } + + println!("here {} - {}", start, end); + + for nonce in start..end { + b.nonce = nonce; + + let j = serde_json::to_vec(&b).unwrap(); - let j = serde_json::to_vec(&b).unwrap(); + let result = Blake2s::digest(&j); - let result = Blake2s::digest(&j); + let first_six = result[0] as i32 + result[1] as i32 + (result[2]) as i32; - let first_six = result[31] as i32 + result[30] as i32 + result[29] as i32; + if first_six == 0 { + println!("{} - {:x}\n{:?}", nonce, result, b); + break; + } + } + })); + }); - if first_six == 0 { - println!("{} - {:x}\n{:?}", nonce, result, b); - break; - } - } + threads.into_iter().for_each(|thread| { + thread + .join() + .expect("The thread creating or execution failed !") + }); println!("it took {} seconds", now.elapsed().as_secs()); } -- cgit v1.2.3-70-g09d2