diff options
-rw-r--r-- | config.yaml | 2 | ||||
-rw-r--r-- | src/config.rs | 2 | ||||
-rw-r--r-- | 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: "" | |||
9 | preapproved_users: "students.csv" | 9 | preapproved_users: "students.csv" |
10 | # Valid blocks should have this many transactions | 10 | # Valid blocks should have this many transactions |
11 | block_transaction_count: 4 | 11 | block_transaction_count: 4 |
12 | # How many zero hexadecimal characters should a correct hash start with? | ||
13 | hash_zeros: 6 | ||
12 | # Bonus awarded after registration | 14 | # Bonus awarded after registration |
13 | register_bonus: 20 | 15 | register_bonus: 20 |
14 | # Coinbase reward | 16 | # 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 { | |||
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)); |