summaryrefslogtreecommitdiffstats
path: root/examples/mining.rs
blob: 1d86a20e299e08cd74a6cfbd0f73ebc5bdf2735d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 counter = Arc::new(Mutex::new(0));

    let now = Instant::now();

    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 result = Blake2s::digest(&j);

                let first_six = result[0] as i32 + result[1] as i32 + (result[2]) as i32;

                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());
}