diff options
author | Yigit Sever | 2021-04-12 05:32:53 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-12 05:32:53 +0300 |
commit | 44d21b676f90a2fc8b255eb9c1393e53f40c9daa (patch) | |
tree | edfadfd279dc9fcfaa6c27f819c7f0e69d14599c /examples/mining.rs | |
parent | 6c0345ecda5e46da88bc6ca513a28c648c29833c (diff) | |
download | gradecoin-44d21b676f90a2fc8b255eb9c1393e53f40c9daa.tar.gz gradecoin-44d21b676f90a2fc8b255eb9c1393e53f40c9daa.tar.bz2 gradecoin-44d21b676f90a2fc8b255eb9c1393e53f40c9daa.zip |
Implement proof-of-work
Using blacke2s: https://docs.rs/blake2/0.9.1/blake2/
Using this guy's hash checker https://gist.github.com/gkbrk/2e4835e3a17b3fb6e1e7
blacke2s with 5 bits 0 can mine a block between 20 seconds to 359 during
my tests, hope it'll be fun
Diffstat (limited to 'examples/mining.rs')
-rw-r--r-- | examples/mining.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/examples/mining.rs b/examples/mining.rs new file mode 100644 index 0000000..56e33f3 --- /dev/null +++ b/examples/mining.rs | |||
@@ -0,0 +1,35 @@ | |||
1 | use chrono::NaiveDate; | ||
2 | use gradecoin::schema::NakedBlock; | ||
3 | use serde_json; | ||
4 | use std::time::Instant; | ||
5 | |||
6 | use blake2::{Blake2s, Digest}; | ||
7 | |||
8 | pub fn main() { | ||
9 | let mut b = NakedBlock { | ||
10 | transaction_list: vec![ | ||
11 | "hash_value".to_owned(), | ||
12 | ], | ||
13 | nonce: 0, | ||
14 | timestamp: NaiveDate::from_ymd(2021, 04, 08).and_hms(12, 30, 30), | ||
15 | }; | ||
16 | |||
17 | let now = Instant::now(); | ||
18 | |||
19 | for nonce in 0..u32::MAX { | ||
20 | b.nonce = nonce; | ||
21 | |||
22 | let j = serde_json::to_vec(&b).unwrap(); | ||
23 | |||
24 | let result = Blake2s::digest(&j); | ||
25 | |||
26 | let first_five = result[31] as i32 + result[30] as i32 + (result[29] << 4) as i32; | ||
27 | |||
28 | if first_five == 0 { | ||
29 | println!("{} - {:x}\n{:?}", nonce, result, b); | ||
30 | break; | ||
31 | } | ||
32 | } | ||
33 | |||
34 | println!("it took {} seconds", now.elapsed().as_secs()); | ||
35 | } | ||