diff options
author | Yigit Sever | 2021-04-14 00:09:29 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-14 00:09:29 +0300 |
commit | 51d82346d53a9b435fbbdb65ab0fceb8ce130b32 (patch) | |
tree | 6821a2ec482297c67f9a3d6d79ef7ddc95d2250c /examples/mining.rs | |
parent | f00437da19b9c57b03392b049429b18755d61da1 (diff) | |
download | gradecoin-51d82346d53a9b435fbbdb65ab0fceb8ce130b32.tar.gz gradecoin-51d82346d53a9b435fbbdb65ab0fceb8ce130b32.tar.bz2 gradecoin-51d82346d53a9b435fbbdb65ab0fceb8ce130b32.zip |
Implement multithreading for example miner
Diffstat (limited to 'examples/mining.rs')
-rw-r--r-- | examples/mining.rs | 63 |
1 files 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 @@ | |||
1 | use chrono::NaiveDate; | 1 | use chrono::NaiveDate; |
2 | use gradecoin::schema::NakedBlock; | 2 | use gradecoin::schema::NakedBlock; |
3 | use serde_json; | 3 | use serde_json; |
4 | use std::sync::{Arc, Mutex}; | ||
5 | use std::thread; | ||
4 | use std::time::Instant; | 6 | use std::time::Instant; |
5 | 7 | ||
6 | use blake2::{Blake2s, Digest}; | 8 | use blake2::{Blake2s, Digest}; |
7 | 9 | ||
10 | const N: usize = 4; | ||
11 | |||
8 | pub fn main() { | 12 | pub fn main() { |
9 | let mut b = NakedBlock { | 13 | let counter = Arc::new(Mutex::new(0)); |
10 | transaction_list: vec!["hash_value".to_owned()], | ||
11 | nonce: 0, | ||
12 | timestamp: NaiveDate::from_ymd(2021, 04, 08).and_hms(12, 30, 30), | ||
13 | }; | ||
14 | 14 | ||
15 | let now = Instant::now(); | 15 | let now = Instant::now(); |
16 | 16 | ||
17 | for nonce in 0..u32::MAX { | 17 | let mut threads = Vec::with_capacity(N); |
18 | b.nonce = nonce; | 18 | |
19 | (0..N).for_each(|_| { | ||
20 | let counter = Arc::clone(&counter); | ||
21 | threads.push(thread::spawn(move || { | ||
22 | let mut b = NakedBlock { | ||
23 | transaction_list: vec!["e254275".to_owned()], | ||
24 | nonce: 0, | ||
25 | timestamp: NaiveDate::from_ymd(2021, 04, 13).and_hms(23, 38, 00), | ||
26 | }; | ||
27 | |||
28 | let start: u32; | ||
29 | let end: u32; | ||
30 | { | ||
31 | let mut num = counter.lock().unwrap(); | ||
32 | |||
33 | println!("Starting with 2 over {}", num); | ||
34 | |||
35 | start = 0 + (1073741824 * *num); | ||
36 | end = 1073741820 * (*num + 1); | ||
37 | *num += 1; | ||
38 | } | ||
39 | |||
40 | println!("here {} - {}", start, end); | ||
41 | |||
42 | for nonce in start..end { | ||
43 | b.nonce = nonce; | ||
44 | |||
45 | let j = serde_json::to_vec(&b).unwrap(); | ||
19 | 46 | ||
20 | let j = serde_json::to_vec(&b).unwrap(); | 47 | let result = Blake2s::digest(&j); |
21 | 48 | ||
22 | let result = Blake2s::digest(&j); | 49 | let first_six = result[0] as i32 + result[1] as i32 + (result[2]) as i32; |
23 | 50 | ||
24 | let first_six = result[31] as i32 + result[30] as i32 + result[29] as i32; | 51 | if first_six == 0 { |
52 | println!("{} - {:x}\n{:?}", nonce, result, b); | ||
53 | break; | ||
54 | } | ||
55 | } | ||
56 | })); | ||
57 | }); | ||
25 | 58 | ||
26 | if first_six == 0 { | 59 | threads.into_iter().for_each(|thread| { |
27 | println!("{} - {:x}\n{:?}", nonce, result, b); | 60 | thread |
28 | break; | 61 | .join() |
29 | } | 62 | .expect("The thread creating or execution failed !") |
30 | } | 63 | }); |
31 | 64 | ||
32 | println!("it took {} seconds", now.elapsed().as_secs()); | 65 | println!("it took {} seconds", now.elapsed().as_secs()); |
33 | } | 66 | } |