diff options
author | Yigit Sever | 2021-04-17 19:47:47 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-17 19:47:47 +0300 |
commit | 86eb4e6ca5f4ce8f86350c4fe66833b006317a50 (patch) | |
tree | bee2792ba6033e3cd3529d8ceb17c27ba6735b1d /src/handlers.rs | |
parent | cd4214da789df660c5d1d70f390ff164da244335 (diff) | |
download | gradecoin-86eb4e6ca5f4ce8f86350c4fe66833b006317a50.tar.gz gradecoin-86eb4e6ca5f4ce8f86350c4fe66833b006317a50.tar.bz2 gradecoin-86eb4e6ca5f4ce8f86350c4fe66833b006317a50.zip |
Rename functions and move checks
Proposals are authenticated, no need to parrot
Diffstat (limited to 'src/handlers.rs')
-rw-r--r-- | src/handlers.rs | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index 9be9764..df6e033 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -84,7 +84,7 @@ pub async fn authenticate_user( | |||
84 | request: InitialAuthRequest, | 84 | request: InitialAuthRequest, |
85 | db: Db, | 85 | db: Db, |
86 | ) -> Result<impl warp::Reply, warp::Rejection> { | 86 | ) -> Result<impl warp::Reply, warp::Rejection> { |
87 | debug!("POST request to /register, authenticate_user"); | 87 | debug!("POST /register, authenticate_user() is handling"); |
88 | 88 | ||
89 | // In essence PEM files are just base64 encoded versions of the DER encoded data. | 89 | // In essence PEM files are just base64 encoded versions of the DER encoded data. |
90 | // ~tls.mbed.org | 90 | // ~tls.mbed.org |
@@ -277,6 +277,8 @@ pub async fn authenticate_user( | |||
277 | balance: 0, | 277 | balance: 0, |
278 | }; | 278 | }; |
279 | 279 | ||
280 | debug!("New user authenticated themselves! {:?}", &new_user); | ||
281 | |||
280 | let user_at_rest_json = serde_json::to_string(&UserAtRest { | 282 | let user_at_rest_json = serde_json::to_string(&UserAtRest { |
281 | user: User { | 283 | user: User { |
282 | user_id: new_user.user_id.clone(), | 284 | user_id: new_user.user_id.clone(), |
@@ -296,7 +298,7 @@ pub async fn authenticate_user( | |||
296 | let res_json = warp::reply::json(&GradeCoinResponse { | 298 | let res_json = warp::reply::json(&GradeCoinResponse { |
297 | res: ResponseType::Success, | 299 | res: ResponseType::Success, |
298 | message: format!( | 300 | message: format!( |
299 | "User authenticated to use Gradecoin with identifier {}", | 301 | "You have authenticated to use Gradecoin with identifier {}", |
300 | fingerprint | 302 | fingerprint |
301 | ), | 303 | ), |
302 | }); | 304 | }); |
@@ -308,11 +310,10 @@ pub async fn authenticate_user( | |||
308 | /// Returns JSON array of transactions | 310 | /// Returns JSON array of transactions |
309 | /// Cannot fail | 311 | /// Cannot fail |
310 | pub async fn list_transactions(db: Db) -> Result<impl warp::Reply, Infallible> { | 312 | pub async fn list_transactions(db: Db) -> Result<impl warp::Reply, Infallible> { |
311 | debug!("GET request to /transaction, list_transactions"); | 313 | debug!("GET /transaction, list_transactions() is handling"); |
312 | let mut result = HashMap::new(); | 314 | let mut result = HashMap::new(); |
313 | 315 | ||
314 | let transactions = db.pending_transactions.read(); | 316 | let transactions = db.pending_transactions.read(); |
315 | // let transactions = transactions.clone().into_iter().collect(); | ||
316 | 317 | ||
317 | for (fp, tx) in transactions.iter() { | 318 | for (fp, tx) in transactions.iter() { |
318 | result.insert(fp, tx); | 319 | result.insert(fp, tx); |
@@ -330,17 +331,16 @@ pub async fn list_transactions(db: Db) -> Result<impl warp::Reply, Infallible> { | |||
330 | /// This is the analogue of `coinbase` in Bitcoin works | 331 | /// This is the analogue of `coinbase` in Bitcoin works |
331 | /// | 332 | /// |
332 | /// The `coinbase` transaction also gets something for their efforts. | 333 | /// The `coinbase` transaction also gets something for their efforts. |
333 | pub async fn authorized_propose_block( | 334 | pub async fn propose_block( |
334 | new_block: Block, | 335 | new_block: Block, |
335 | token: String, | 336 | token: String, |
336 | db: Db, | 337 | db: Db, |
337 | ) -> Result<impl warp::Reply, warp::Rejection> { | 338 | ) -> Result<impl warp::Reply, warp::Rejection> { |
338 | debug!("POST request to /block, authorized_propose_block"); | 339 | debug!("POST /block, propose_block() is handling"); |
339 | println!("Hi"); | ||
340 | 340 | ||
341 | let users_store = db.users.upgradable_read(); | 341 | let users_store = db.users.upgradable_read(); |
342 | 342 | ||
343 | warn!("{:?}", &new_block); | 343 | warn!("New block proposal: {:?}", &new_block); |
344 | 344 | ||
345 | if new_block.transaction_list.len() != BLOCK_TRANSACTION_COUNT as usize { | 345 | if new_block.transaction_list.len() != BLOCK_TRANSACTION_COUNT as usize { |
346 | let res_json = warp::reply::json(&GradeCoinResponse { | 346 | let res_json = warp::reply::json(&GradeCoinResponse { |
@@ -457,8 +457,9 @@ pub async fn authorized_propose_block( | |||
457 | } | 457 | } |
458 | 458 | ||
459 | // All clear, block accepted! | 459 | // All clear, block accepted! |
460 | debug!("We have a new block!"); | 460 | debug!("We have a new block! {:?}", new_block); |
461 | 461 | ||
462 | // Scope the pending_transactions | ||
462 | { | 463 | { |
463 | let pending_transactions = db.pending_transactions.read(); | 464 | let pending_transactions = db.pending_transactions.read(); |
464 | let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store); | 465 | let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store); |
@@ -521,12 +522,12 @@ pub async fn authorized_propose_block( | |||
521 | /// * `token` - An Authorization header value such as `Bearer aaa.bbb.ccc` | 522 | /// * `token` - An Authorization header value such as `Bearer aaa.bbb.ccc` |
522 | /// * `db` - Global [`Db`] instance | 523 | /// * `db` - Global [`Db`] instance |
523 | /// | 524 | /// |
524 | pub async fn authorized_propose_transaction( | 525 | pub async fn propose_transaction( |
525 | new_transaction: Transaction, | 526 | new_transaction: Transaction, |
526 | token: String, | 527 | token: String, |
527 | db: Db, | 528 | db: Db, |
528 | ) -> Result<impl warp::Reply, warp::Rejection> { | 529 | ) -> Result<impl warp::Reply, warp::Rejection> { |
529 | debug!("POST request to /transaction, authorized_propose_transaction"); | 530 | debug!("POST /transaction, authorized_propose_transaction() is handling"); |
530 | 531 | ||
531 | let users_store = db.users.read(); | 532 | let users_store = db.users.read(); |
532 | 533 | ||
@@ -552,16 +553,14 @@ pub async fn authorized_propose_transaction( | |||
552 | 553 | ||
553 | // `internal_user` is an authenticated student, can propose | 554 | // `internal_user` is an authenticated student, can propose |
554 | 555 | ||
555 | // check if user can afford the transaction | 556 | // Does this user have a pending transaction? |
556 | if new_transaction.by == new_transaction.source { | 557 | { |
557 | // Propose to transact with another user | 558 | let transactions = db.pending_transactions.read(); |
558 | if internal_user.balance < new_transaction.amount { | 559 | if transactions.contains_key(&*new_transaction.source.to_owned()) { |
559 | return Ok(warp::reply::with_status( | 560 | return Ok(warp::reply::with_status( |
560 | warp::reply::json(&GradeCoinResponse { | 561 | warp::reply::json(&GradeCoinResponse { |
561 | res: ResponseType::Error, | 562 | res: ResponseType::Error, |
562 | message: | 563 | message: "This user already has another pending transaction".to_owned(), |
563 | "User does not have enough balance in their account for this transaction" | ||
564 | .to_owned(), | ||
565 | }), | 564 | }), |
566 | StatusCode::BAD_REQUEST, | 565 | StatusCode::BAD_REQUEST, |
567 | )); | 566 | )); |
@@ -573,7 +572,7 @@ pub async fn authorized_propose_transaction( | |||
573 | return Ok(warp::reply::with_status( | 572 | return Ok(warp::reply::with_status( |
574 | warp::reply::json(&GradeCoinResponse { | 573 | warp::reply::json(&GradeCoinResponse { |
575 | res: ResponseType::Error, | 574 | res: ResponseType::Error, |
576 | message: "Transactions cannot extort Gradecoin from unsuspecting users".to_owned(), | 575 | message: format!("Transaction amount cannot exceed {}", TX_UPPER_LIMIT), |
577 | }), | 576 | }), |
578 | StatusCode::BAD_REQUEST, | 577 | StatusCode::BAD_REQUEST, |
579 | )); | 578 | )); |
@@ -656,21 +655,12 @@ pub async fn authorized_propose_transaction( | |||
656 | )); | 655 | )); |
657 | } | 656 | } |
658 | 657 | ||
659 | debug!("clear for transaction proposal"); | 658 | warn!("NEW TRANSACTION {:?}", new_transaction); |
660 | 659 | ||
661 | let mut transactions = db.pending_transactions.write(); | 660 | let mut transactions = db.pending_transactions.write(); |
662 | 661 | ||
663 | if transactions.contains_key(&*new_transaction.source.to_owned()) { | ||
664 | return Ok(warp::reply::with_status( | ||
665 | warp::reply::json(&GradeCoinResponse { | ||
666 | res: ResponseType::Error, | ||
667 | message: "This user already has another pending transaction".to_owned(), | ||
668 | }), | ||
669 | StatusCode::BAD_REQUEST, | ||
670 | )); | ||
671 | } | ||
672 | |||
673 | transactions.insert(new_transaction.source.to_owned(), new_transaction); | 662 | transactions.insert(new_transaction.source.to_owned(), new_transaction); |
663 | |||
674 | Ok(warp::reply::with_status( | 664 | Ok(warp::reply::with_status( |
675 | warp::reply::json(&GradeCoinResponse { | 665 | warp::reply::json(&GradeCoinResponse { |
676 | res: ResponseType::Success, | 666 | res: ResponseType::Success, |
@@ -685,7 +675,7 @@ pub async fn authorized_propose_transaction( | |||
685 | /// Cannot fail | 675 | /// Cannot fail |
686 | /// Mostly around for debug purposes | 676 | /// Mostly around for debug purposes |
687 | pub async fn list_blocks(db: Db) -> Result<impl warp::Reply, Infallible> { | 677 | pub async fn list_blocks(db: Db) -> Result<impl warp::Reply, Infallible> { |
688 | debug!("GET request to /block, list_blocks"); | 678 | debug!("GET /block, list_blocks() is handling"); |
689 | 679 | ||
690 | let block = db.blockchain.read(); | 680 | let block = db.blockchain.read(); |
691 | 681 | ||