diff options
author | Yigit Sever | 2021-04-17 19:47:39 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-17 19:47:39 +0300 |
commit | cd4214da789df660c5d1d70f390ff164da244335 (patch) | |
tree | 83065e654f06d876c227dae960ec7d13f329c94b /src/handlers.rs | |
parent | 240355e1df485ca869a9b0074c085d25417bf36c (diff) | |
download | gradecoin-cd4214da789df660c5d1d70f390ff164da244335.tar.gz gradecoin-cd4214da789df660c5d1d70f390ff164da244335.tar.bz2 gradecoin-cd4214da789df660c5d1d70f390ff164da244335.zip |
Implement block/tx bounds
Diffstat (limited to 'src/handlers.rs')
-rw-r--r-- | src/handlers.rs | 48 |
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 | ||
20 | use crate::PRIVATE_KEY; | 20 | use crate::PRIVATE_KEY; |
21 | const BLOCK_TRANSACTION_COUNT: u8 = 10; | 21 | const BLOCK_TRANSACTION_COUNT: u8 = 10; |
22 | const BLOCK_REWARD: u8 = 3; | 22 | const BLOCK_REWARD: u16 = 3; |
23 | const TX_UPPER_LIMIT: u16 = 2; | ||
23 | 24 | ||
24 | // Encryption primitive | 25 | // Encryption primitive |
25 | type Aes128Cbc = Cbc<Aes128, Pkcs7>; | 26 | type 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 | ||
720 | struct DisplayUsers { | 750 | struct DisplayUsers { |
721 | fingerprint: String, | 751 | fingerprint: String, |
722 | balance: i32, | 752 | balance: u16, |
723 | } | 753 | } |
724 | 754 | ||
725 | pub async fn user_list_handler(db: Db) -> Result<impl warp::Reply, warp::Rejection> { | 755 | pub async fn user_list_handler(db: Db) -> Result<impl warp::Reply, warp::Rejection> { |