aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYigit Sever2021-04-18 19:31:59 +0300
committerYigit Sever2021-04-18 19:31:59 +0300
commit11dbf57a531dbb17ea5f25cc7c3616a3e4ebedcf (patch)
treea1495f82ea21cc58cad2ab1555523d7705d57909
parenta11fb97eecc4e9cbc494d0d8a2771848f6256e77 (diff)
downloadgradecoin-11dbf57a531dbb17ea5f25cc7c3616a3e4ebedcf.tar.gz
gradecoin-11dbf57a531dbb17ea5f25cc7c3616a3e4ebedcf.tar.bz2
gradecoin-11dbf57a531dbb17ea5f25cc7c3616a3e4ebedcf.zip
Bugfix and useful error messages
-rw-r--r--src/handlers.rs28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index f6e1f9f..ddcc8a3 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -556,7 +556,8 @@ pub async fn propose_transaction(
556 // Does this user have a pending transaction? 556 // Does this user have a pending transaction?
557 { 557 {
558 let transactions = db.pending_transactions.read(); 558 let transactions = db.pending_transactions.read();
559 if transactions.contains_key(&*new_transaction.source.to_owned()) { 559 if transactions.contains_key(&*new_transaction.by.to_owned()) {
560 debug!("{:?} already has a pending transaction", new_transaction.by);
560 return Ok(warp::reply::with_status( 561 return Ok(warp::reply::with_status(
561 warp::reply::json(&GradeCoinResponse { 562 warp::reply::json(&GradeCoinResponse {
562 res: ResponseType::Error, 563 res: ResponseType::Error,
@@ -569,6 +570,10 @@ pub async fn propose_transaction(
569 570
570 // Is transaction amount within bounds 571 // Is transaction amount within bounds
571 if new_transaction.amount > TX_UPPER_LIMIT { 572 if new_transaction.amount > TX_UPPER_LIMIT {
573 debug!(
574 "Transaction amount cannot exceed {}, was {}",
575 TX_UPPER_LIMIT, new_transaction.amount
576 );
572 return Ok(warp::reply::with_status( 577 return Ok(warp::reply::with_status(
573 warp::reply::json(&GradeCoinResponse { 578 warp::reply::json(&GradeCoinResponse {
574 res: ResponseType::Error, 579 res: ResponseType::Error,
@@ -581,6 +586,10 @@ pub async fn propose_transaction(
581 if new_transaction.by == new_transaction.source { 586 if new_transaction.by == new_transaction.source {
582 // check if user can afford the transaction 587 // check if user can afford the transaction
583 if internal_user.balance < new_transaction.amount { 588 if internal_user.balance < new_transaction.amount {
589 debug!(
590 "User does not have enough balance ({}) for this TX {}",
591 internal_user.balance, new_transaction.amount
592 );
584 return Ok(warp::reply::with_status( 593 return Ok(warp::reply::with_status(
585 warp::reply::json(&GradeCoinResponse { 594 warp::reply::json(&GradeCoinResponse {
586 res: ResponseType::Error, 595 res: ResponseType::Error,
@@ -597,6 +606,10 @@ pub async fn propose_transaction(
597 if new_transaction.source 606 if new_transaction.source
598 != "31415926535897932384626433832795028841971693993751058209749445923" 607 != "31415926535897932384626433832795028841971693993751058209749445923"
599 { 608 {
609 debug!(
610 "Extortion attempt - between {} and {}",
611 new_transaction.source, new_transaction.target
612 );
600 return Ok(warp::reply::with_status( 613 return Ok(warp::reply::with_status(
601 warp::reply::json(&GradeCoinResponse { 614 warp::reply::json(&GradeCoinResponse {
602 res: ResponseType::Error, 615 res: ResponseType::Error,
@@ -607,10 +620,14 @@ pub async fn propose_transaction(
607 )); 620 ));
608 } 621 }
609 } else { 622 } else {
623 debug!(
624 "Attempt to transact between two unrelated parties - {} and {}",
625 new_transaction.source, new_transaction.target
626 );
610 return Ok(warp::reply::with_status( 627 return Ok(warp::reply::with_status(
611 warp::reply::json(&GradeCoinResponse { 628 warp::reply::json(&GradeCoinResponse {
612 res: ResponseType::Error, 629 res: ResponseType::Error,
613 message: "Transactions cannot be proposed between two unrelated parties".to_owned(), 630 message: "Transactions cannot be proposed on behalf of someone else".to_owned(),
614 }), 631 }),
615 StatusCode::BAD_REQUEST, 632 StatusCode::BAD_REQUEST,
616 )); 633 ));
@@ -623,7 +640,7 @@ pub async fn propose_transaction(
623 let token_payload = match authorize_proposer(token, &proposer_public_key) { 640 let token_payload = match authorize_proposer(token, &proposer_public_key) {
624 Ok(data) => data, 641 Ok(data) => data,
625 Err(below) => { 642 Err(below) => {
626 debug!("Something went wrong below {:?}", below); 643 debug!("Something went wrong at JWT {:?}", below);
627 return Ok(warp::reply::with_status( 644 return Ok(warp::reply::with_status(
628 warp::reply::json(&GradeCoinResponse { 645 warp::reply::json(&GradeCoinResponse {
629 res: ResponseType::Error, 646 res: ResponseType::Error,
@@ -642,10 +659,6 @@ pub async fn propose_transaction(
642 let hashed_transaction = 659 let hashed_transaction =
643 Md5::digest((&serde_json::to_string(&new_transaction).unwrap()).as_ref()); 660 Md5::digest((&serde_json::to_string(&new_transaction).unwrap()).as_ref());
644 if token_payload.claims.tha != format!("{:x}", hashed_transaction) { 661 if token_payload.claims.tha != format!("{:x}", hashed_transaction) {
645 println!(
646 "the hash of the request {:x} did not match the hash given in jwt {:?}",
647 hashed_transaction, token_payload.claims.tha
648 );
649 return Ok(warp::reply::with_status( 662 return Ok(warp::reply::with_status(
650 warp::reply::json(&GradeCoinResponse { 663 warp::reply::json(&GradeCoinResponse {
651 res: ResponseType::Error, 664 res: ResponseType::Error,
@@ -690,7 +703,6 @@ pub async fn list_blocks(db: Db) -> Result<impl warp::Reply, Infallible> {
690fn authorize_proposer(jwt_token: String, user_pem: &str) -> Result<TokenData<Claims>, String> { 703fn authorize_proposer(jwt_token: String, user_pem: &str) -> Result<TokenData<Claims>, String> {
691 // Throw away the "Bearer " part 704 // Throw away the "Bearer " part
692 let raw_jwt = jwt_token.trim_start_matches(BEARER).to_owned(); 705 let raw_jwt = jwt_token.trim_start_matches(BEARER).to_owned();
693 debug!("raw_jwt: {:?}", raw_jwt);
694 706
695 // Extract a jsonwebtoken compatible decoding_key from user's public key 707 // Extract a jsonwebtoken compatible decoding_key from user's public key
696 let decoding_key = match DecodingKey::from_rsa_pem(user_pem.as_bytes()) { 708 let decoding_key = match DecodingKey::from_rsa_pem(user_pem.as_bytes()) {