aboutsummaryrefslogtreecommitdiffstats
path: root/examples/mining.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/mining.rs')
-rw-r--r--examples/mining.rs66
1 files changed, 0 insertions, 66 deletions
diff --git a/examples/mining.rs b/examples/mining.rs
deleted file mode 100644
index e3d1487..0000000
--- a/examples/mining.rs
+++ /dev/null
@@ -1,66 +0,0 @@
1use chrono::NaiveDate;
2use gradecoin::schema::NakedBlock;
3use serde_json;
4use std::sync::{Arc, Mutex};
5use std::thread;
6use std::time::Instant;
7
8use blake2::{Blake2s, Digest};
9
10const N: usize = 4;
11
12pub fn main() {
13 let counter = Arc::new(Mutex::new(0));
14
15 let now = Instant::now();
16
17 let mut threads = Vec::with_capacity(N);
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!["fingerprint_of_some_guy".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();
46
47 let result = Blake2s::digest(&j);
48
49 let first_six = result[0] as i32 + result[1] as i32 + (result[2]) as i32;
50
51 if first_six == 0 {
52 println!("{} - {:x}\n{:?}", nonce, result, b);
53 break;
54 }
55 }
56 }));
57 });
58
59 threads.into_iter().for_each(|thread| {
60 thread
61 .join()
62 .expect("The thread creating or execution failed !")
63 });
64
65 println!("it took {} seconds", now.elapsed().as_secs());
66}