summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYigit Sever2021-04-14 14:56:46 +0300
committerYigit Sever2021-04-14 14:56:46 +0300
commit14433dc3d41875dee45f2410018475683a87349e (patch)
tree961996d0e463102ec9640f7755b35c7d2e4989c1 /src
parent3a3ebcd0145707e6945abea5b29f4afb848ea309 (diff)
downloadgradecoin-14433dc3d41875dee45f2410018475683a87349e.tar.gz
gradecoin-14433dc3d41875dee45f2410018475683a87349e.tar.bz2
gradecoin-14433dc3d41875dee45f2410018475683a87349e.zip
Simplify the block validation process
2 TODOs down!
Diffstat (limited to 'src')
-rw-r--r--src/handlers.rs35
1 files changed, 16 insertions, 19 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index cc71ba7..e34abbe 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -199,27 +199,25 @@ pub async fn authorized_propose_block(
199 let hashvalue = Blake2s::digest(&naked_block_flat); 199 let hashvalue = Blake2s::digest(&naked_block_flat);
200 let hash_string = format!("{:x}", hashvalue); 200 let hash_string = format!("{:x}", hashvalue);
201 201
202 // 6 rightmost bits are zero? 202 // Does the hash claimed in block matched with the actual hash?
203 let should_zero = hashvalue[31] as i32 + hashvalue[30] as i32 + hashvalue[29] as i32; 203 if hash_string != new_block.hash {
204 // TODO: this can be offloaded to validator <13-04-21, yigit> // 204 debug!("request was not telling the truth, hash values do not match");
205
206 if should_zero != 0 {
207 debug!("the hash does not have 6 rightmost zero bits");
208 let res_json = warp::reply::json(&GradeCoinResponse { 205 let res_json = warp::reply::json(&GradeCoinResponse {
209 res: ResponseType::Error, 206 res: ResponseType::Error,
210 message: "Given block hash is larger than target value".to_owned(), 207 message: "Given hash value does not match the actual block hash".to_owned(),
211 }); 208 });
212 209
213 return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); 210 return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST));
214 } 211 }
215 212
216 // one last check to see if block is telling the truth 213 // Are the 6 rightmost characters (=24 bits) zero?
217 if hash_string != new_block.hash { 214 let should_zero = hashvalue[31] as i32 + hashvalue[30] as i32 + hashvalue[29] as i32;
218 debug!("request was not telling the truth, hash values do not match"); 215
219 // TODO: does this condition make more sense _before_ the hash 0s check? <13-04-21, yigit> // 216 if should_zero != 0 {
217 debug!("the hash does not have 6 rightmost zero bits");
220 let res_json = warp::reply::json(&GradeCoinResponse { 218 let res_json = warp::reply::json(&GradeCoinResponse {
221 res: ResponseType::Error, 219 res: ResponseType::Error,
222 message: "Given hash value does not match the actual block hash".to_owned(), 220 message: "Given block hash is larger than target value".to_owned(),
223 }); 221 });
224 222
225 return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); 223 return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST));
@@ -292,23 +290,23 @@ pub async fn authorized_propose_transaction(
292 // `user` is an authenticated student, can propose 290 // `user` is an authenticated student, can propose
293 291
294 // check if user can afford the transaction 292 // check if user can afford the transaction
295 if new_transaction.by == new_transaction.source { // Propose to transact with another user 293 if new_transaction.by == new_transaction.source {
294 // Propose to transact with another user
296 if internal_user.balance < new_transaction.amount { 295 if internal_user.balance < new_transaction.amount {
297 return Ok(warp::reply::with_status( 296 return Ok(warp::reply::with_status(
298 warp::reply::json(&GradeCoinResponse { 297 warp::reply::json(&GradeCoinResponse {
299 res: ResponseType::Error, 298 res: ResponseType::Error,
300 message: "User does not have enough balance in their account" 299 message: "User does not have enough balance in their account".to_owned(),
301 .to_owned(),
302 }), 300 }),
303 StatusCode::BAD_REQUEST, 301 StatusCode::BAD_REQUEST,
304 )); 302 ));
305 } 303 }
306 } else { // todo: add bank mechanism 304 } else {
305 // todo: add bank mechanism
307 return Ok(warp::reply::with_status( 306 return Ok(warp::reply::with_status(
308 warp::reply::json(&GradeCoinResponse { 307 warp::reply::json(&GradeCoinResponse {
309 res: ResponseType::Error, 308 res: ResponseType::Error,
310 message: "Invalid by field for the proposed transaction" 309 message: "Invalid by field for the proposed transaction".to_owned(),
311 .to_owned(),
312 }), 310 }),
313 StatusCode::BAD_REQUEST, 311 StatusCode::BAD_REQUEST,
314 )); 312 ));
@@ -354,7 +352,6 @@ pub async fn authorized_propose_transaction(
354 352
355 debug!("clear for transaction proposal"); 353 debug!("clear for transaction proposal");
356 354
357
358 let mut transactions = db.pending_transactions.write(); 355 let mut transactions = db.pending_transactions.write();
359 transactions.insert(new_transaction.source.to_owned(), new_transaction); 356 transactions.insert(new_transaction.source.to_owned(), new_transaction);
360 Ok(warp::reply::with_status( 357 Ok(warp::reply::with_status(