aboutsummaryrefslogtreecommitdiffstats
path: root/src/handlers.rs
diff options
context:
space:
mode:
authorYigit Sever2021-04-17 19:47:39 +0300
committerYigit Sever2021-04-17 19:47:39 +0300
commit9ead8e1ffa7bc8619aa7a1fd5802538ed927581e (patch)
tree1bc066c9a9e82dd1d6b43b6e01f286516f53c527 /src/handlers.rs
parent1ec0207d85648c3d7bce9b71b7a22ec081b3f137 (diff)
downloadgradecoin-9ead8e1ffa7bc8619aa7a1fd5802538ed927581e.tar.gz
gradecoin-9ead8e1ffa7bc8619aa7a1fd5802538ed927581e.tar.bz2
gradecoin-9ead8e1ffa7bc8619aa7a1fd5802538ed927581e.zip
Implement block/tx bounds
Diffstat (limited to 'src/handlers.rs')
-rw-r--r--src/handlers.rs48
1 files changed, 39 insertions, 9 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index c2c8aca..9be9764 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -19,7 +19,8 @@ use warp::{http::StatusCode, reply};
19 19
20use crate::PRIVATE_KEY; 20use crate::PRIVATE_KEY;
21const BLOCK_TRANSACTION_COUNT: u8 = 10; 21const BLOCK_TRANSACTION_COUNT: u8 = 10;
22const BLOCK_REWARD: u8 = 3; 22const BLOCK_REWARD: u16 = 3;
23const TX_UPPER_LIMIT: u16 = 2;
23 24
24// Encryption primitive 25// Encryption primitive
25type Aes128Cbc = Cbc<Aes128, Pkcs7>; 26type Aes128Cbc = Cbc<Aes128, Pkcs7>;
@@ -341,7 +342,7 @@ pub async fn authorized_propose_block(
341 342
342 warn!("{:?}", &new_block); 343 warn!("{:?}", &new_block);
343 344
344 if new_block.transaction_list.is_empty() { 345 if new_block.transaction_list.len() != BLOCK_TRANSACTION_COUNT as usize {
345 let res_json = warp::reply::json(&GradeCoinResponse { 346 let res_json = warp::reply::json(&GradeCoinResponse {
346 res: ResponseType::Error, 347 res: ResponseType::Error,
347 message: format!( 348 message: format!(
@@ -480,7 +481,7 @@ pub async fn authorized_propose_block(
480 } 481 }
481 482
482 if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) { 483 if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) {
483 coinbase_user.balance += BLOCK_REWARD as i32; 484 coinbase_user.balance += BLOCK_REWARD;
484 } 485 }
485 } 486 }
486 487
@@ -565,11 +566,10 @@ pub async fn authorized_propose_transaction(
565 StatusCode::BAD_REQUEST, 566 StatusCode::BAD_REQUEST,
566 )); 567 ));
567 } 568 }
568 } else if new_transaction.by == new_transaction.target 569 }
569 && new_transaction.source 570
570 != "31415926535897932384626433832795028841971693993751058209749445923" 571 // Is transaction amount within bounds
571 { 572 if new_transaction.amount > TX_UPPER_LIMIT {
572 // Propose to transact with the bank
573 return Ok(warp::reply::with_status( 573 return Ok(warp::reply::with_status(
574 warp::reply::json(&GradeCoinResponse { 574 warp::reply::json(&GradeCoinResponse {
575 res: ResponseType::Error, 575 res: ResponseType::Error,
@@ -577,6 +577,36 @@ pub async fn authorized_propose_transaction(
577 }), 577 }),
578 StatusCode::BAD_REQUEST, 578 StatusCode::BAD_REQUEST,
579 )); 579 ));
580 }
581
582 if new_transaction.by == new_transaction.source {
583 // check if user can afford the transaction
584 if internal_user.balance < new_transaction.amount {
585 return Ok(warp::reply::with_status(
586 warp::reply::json(&GradeCoinResponse {
587 res: ResponseType::Error,
588 message:
589 "User does not have enough balance in their account for this transaction"
590 .to_owned(),
591 }),
592 StatusCode::BAD_REQUEST,
593 ));
594 }
595 } else if new_transaction.by == new_transaction.target {
596 // Only transactions FROM bank could appear here
597
598 if new_transaction.source
599 != "31415926535897932384626433832795028841971693993751058209749445923"
600 {
601 return Ok(warp::reply::with_status(
602 warp::reply::json(&GradeCoinResponse {
603 res: ResponseType::Error,
604 message: "Transactions cannot extort Gradecoin from unsuspecting users"
605 .to_owned(),
606 }),
607 StatusCode::BAD_REQUEST,
608 ));
609 }
580 } else { 610 } else {
581 return Ok(warp::reply::with_status( 611 return Ok(warp::reply::with_status(
582 warp::reply::json(&GradeCoinResponse { 612 warp::reply::json(&GradeCoinResponse {
@@ -719,7 +749,7 @@ struct UserTemplate<'a> {
719 749
720struct DisplayUsers { 750struct DisplayUsers {
721 fingerprint: String, 751 fingerprint: String,
722 balance: i32, 752 balance: u16,
723} 753}
724 754
725pub async fn user_list_handler(db: Db) -> Result<impl warp::Reply, warp::Rejection> { 755pub async fn user_list_handler(db: Db) -> Result<impl warp::Reply, warp::Rejection> {