From 44d21b676f90a2fc8b255eb9c1393e53f40c9daa Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Mon, 12 Apr 2021 05:32:53 +0300 Subject: 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 --- examples/mining.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/mining.rs (limited to 'examples/mining.rs') 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 @@ +use chrono::NaiveDate; +use gradecoin::schema::NakedBlock; +use serde_json; +use std::time::Instant; + +use blake2::{Blake2s, Digest}; + +pub fn main() { + let mut b = NakedBlock { + transaction_list: vec![ + "hash_value".to_owned(), + ], + nonce: 0, + timestamp: NaiveDate::from_ymd(2021, 04, 08).and_hms(12, 30, 30), + }; + + let now = Instant::now(); + + for nonce in 0..u32::MAX { + b.nonce = nonce; + + let j = serde_json::to_vec(&b).unwrap(); + + let result = Blake2s::digest(&j); + + let first_five = result[31] as i32 + result[30] as i32 + (result[29] << 4) as i32; + + if first_five == 0 { + println!("{} - {:x}\n{:?}", nonce, result, b); + break; + } + } + + println!("it took {} seconds", now.elapsed().as_secs()); +} -- cgit v1.2.3-70-g09d2