diff options
author | necrashter | 2022-04-23 16:42:46 +0300 |
---|---|---|
committer | Yigit Sever | 2022-04-23 18:10:12 +0300 |
commit | 92b37178167435027bb9e4db22b5c44977f341f9 (patch) | |
tree | 7170010cd9693a9976c5f71cfbf648ea7abef80f /src | |
parent | 8d7a1c4337af570c691c3c16527b724ae57ed80f (diff) | |
download | gradecoin-92b37178167435027bb9e4db22b5c44977f341f9.tar.gz gradecoin-92b37178167435027bb9e4db22b5c44977f341f9.tar.bz2 gradecoin-92b37178167435027bb9e4db22b5c44977f341f9.zip |
Configurable hash zero hexadecimal character count
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 2 | ||||
-rw-r--r-- | src/handlers.rs | 15 |
2 files changed, 12 insertions, 5 deletions
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 { | |||
15 | pub preapproved_users: String, | 15 | pub preapproved_users: String, |
16 | // Valid blocks should have this many transactions | 16 | // Valid blocks should have this many transactions |
17 | pub block_transaction_count: u8, | 17 | pub block_transaction_count: u8, |
18 | // How many zero hexadecimal characters should a correct hash start with? | ||
19 | pub hash_zeros: u8, | ||
18 | // Inital registration bonus | 20 | // Inital registration bonus |
19 | pub register_bonus: u16, | 21 | pub register_bonus: u16, |
20 | // Coinbase reward | 22 | // 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( | |||
524 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); | 524 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); |
525 | } | 525 | } |
526 | 526 | ||
527 | // Are the 6 leftmost characters (=24 bits) zero? | 527 | // Are the n leftmost characters zero? |
528 | let should_zero = i32::from(hashvalue[0]) + i32::from(hashvalue[1]) + i32::from(hashvalue[2]); | 528 | let hash_correct = hash_string.chars() |
529 | .take(db.config.hash_zeros.into()) | ||
530 | .all(|x| x == '0'); | ||
529 | 531 | ||
530 | if should_zero != 0 { | 532 | if !hash_correct { |
531 | debug!("the hash does not have 6 rightmost zero bits"); | 533 | debug!("The hash does not have {} leftmost zero characters", db.config.hash_zeros); |
532 | let res_json = warp::reply::json(&GradeCoinResponse { | 534 | let res_json = warp::reply::json(&GradeCoinResponse { |
533 | res: ResponseType::Error, | 535 | res: ResponseType::Error, |
534 | message: "Given block hash is larger than target value".to_owned(), | 536 | message: format!( |
537 | "Given block hash does not start with {} zero hexadecimal characters", | ||
538 | db.config.hash_zeros | ||
539 | ), | ||
535 | }); | 540 | }); |
536 | 541 | ||
537 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); | 542 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); |