From 92b37178167435027bb9e4db22b5c44977f341f9 Mon Sep 17 00:00:00 2001 From: necrashter Date: Sat, 23 Apr 2022 16:42:46 +0300 Subject: Configurable hash zero hexadecimal character count --- config.yaml | 2 ++ src/config.rs | 2 ++ src/handlers.rs | 15 ++++++++++----- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/config.yaml b/config.yaml index caac122..e2f6b72 100644 --- a/config.yaml +++ b/config.yaml @@ -9,6 +9,8 @@ url_prefix: "" preapproved_users: "students.csv" # Valid blocks should have this many transactions block_transaction_count: 4 +# How many zero hexadecimal characters should a correct hash start with? +hash_zeros: 6 # Bonus awarded after registration register_bonus: 20 # Coinbase reward diff --git a/src/config.rs b/src/config.rs index 9fb268b..7a524fc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,6 +15,8 @@ pub struct Config { pub preapproved_users: String, // Valid blocks should have this many transactions pub block_transaction_count: u8, + // How many zero hexadecimal characters should a correct hash start with? + pub hash_zeros: u8, // Inital registration bonus pub register_bonus: u16, // Coinbase reward diff --git a/src/handlers.rs b/src/handlers.rs index 44c5299..6619924 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -524,14 +524,19 @@ pub async fn propose_block( return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); } - // Are the 6 leftmost characters (=24 bits) zero? - let should_zero = i32::from(hashvalue[0]) + i32::from(hashvalue[1]) + i32::from(hashvalue[2]); + // Are the n leftmost characters zero? + let hash_correct = hash_string.chars() + .take(db.config.hash_zeros.into()) + .all(|x| x == '0'); - if should_zero != 0 { - debug!("the hash does not have 6 rightmost zero bits"); + if !hash_correct { + debug!("The hash does not have {} leftmost zero characters", db.config.hash_zeros); let res_json = warp::reply::json(&GradeCoinResponse { res: ResponseType::Error, - message: "Given block hash is larger than target value".to_owned(), + message: format!( + "Given block hash does not start with {} zero hexadecimal characters", + db.config.hash_zeros + ), }); return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); -- cgit v1.2.3-70-g09d2