diff options
| author | Yigit Sever | 2021-04-12 05:32:53 +0300 |
|---|---|---|
| committer | Yigit Sever | 2021-04-12 05:32:53 +0300 |
| commit | e0fb91039f34204b2a5c588a95cb3f1789ad2fa7 (patch) | |
| tree | 9c38670c348f04c57185639c541a1a93b8cfde2a /examples/mining.rs | |
| parent | 62aa261e1fd531afd1e5fa7244471d051a78db38 (diff) | |
| download | gradecoin-e0fb91039f34204b2a5c588a95cb3f1789ad2fa7.tar.gz gradecoin-e0fb91039f34204b2a5c588a95cb3f1789ad2fa7.tar.bz2 gradecoin-e0fb91039f34204b2a5c588a95cb3f1789ad2fa7.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 | } | ||
