diff options
author | alpaylan | 2021-04-15 00:24:40 +0300 |
---|---|---|
committer | alpaylan | 2021-04-15 00:24:40 +0300 |
commit | bddea30ecb76f0c4805758d3b36aab52f176ff9b (patch) | |
tree | f91cc081715ed44d9354d5f6ab2f0905a0175500 /src | |
parent | 72ff7f96e18737bd90bf932c1b5f491884b2560d (diff) | |
parent | 8be38f7f9d52bf95d20fbe440cb9270b035b2cd1 (diff) | |
download | gradecoin-bddea30ecb76f0c4805758d3b36aab52f176ff9b.tar.gz gradecoin-bddea30ecb76f0c4805758d3b36aab52f176ff9b.tar.bz2 gradecoin-bddea30ecb76f0c4805758d3b36aab52f176ff9b.zip |
Merge remote-tracking branch 'origin/main'
Diffstat (limited to 'src')
-rw-r--r-- | src/handlers.rs | 20 | ||||
-rw-r--r-- | src/schema.rs | 18 |
2 files changed, 32 insertions, 6 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index 90cc8c4..a8c9947 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -420,7 +420,6 @@ pub async fn authorized_propose_block( | |||
420 | /// * `token` - An Authorization header value such as `Bearer aaa.bbb.ccc` | 420 | /// * `token` - An Authorization header value such as `Bearer aaa.bbb.ccc` |
421 | /// * `db` - Global [`Db`] instance | 421 | /// * `db` - Global [`Db`] instance |
422 | /// | 422 | /// |
423 | /// TODO This method should check if the user has enough balance for the transaction | ||
424 | pub async fn authorized_propose_transaction( | 423 | pub async fn authorized_propose_transaction( |
425 | new_transaction: Transaction, | 424 | new_transaction: Transaction, |
426 | token: String, | 425 | token: String, |
@@ -459,17 +458,30 @@ pub async fn authorized_propose_transaction( | |||
459 | return Ok(warp::reply::with_status( | 458 | return Ok(warp::reply::with_status( |
460 | warp::reply::json(&GradeCoinResponse { | 459 | warp::reply::json(&GradeCoinResponse { |
461 | res: ResponseType::Error, | 460 | res: ResponseType::Error, |
462 | message: "User does not have enough balance in their account".to_owned(), | 461 | message: |
462 | "User does not have enough balance in their account for this transaction" | ||
463 | .to_owned(), | ||
463 | }), | 464 | }), |
464 | StatusCode::BAD_REQUEST, | 465 | StatusCode::BAD_REQUEST, |
465 | )); | 466 | )); |
466 | } | 467 | } |
468 | } else if new_transaction.by == new_transaction.target | ||
469 | && new_transaction.source | ||
470 | != "31415926535897932384626433832795028841971693993751058209749445923" | ||
471 | { | ||
472 | // Propose to transact with the bank | ||
473 | return Ok(warp::reply::with_status( | ||
474 | warp::reply::json(&GradeCoinResponse { | ||
475 | res: ResponseType::Error, | ||
476 | message: "Transactions cannot extort Gradecoin from unsuspecting users".to_owned(), | ||
477 | }), | ||
478 | StatusCode::BAD_REQUEST, | ||
479 | )); | ||
467 | } else { | 480 | } else { |
468 | // TODO: add bank mechanism <14-04-21, keles> // | ||
469 | return Ok(warp::reply::with_status( | 481 | return Ok(warp::reply::with_status( |
470 | warp::reply::json(&GradeCoinResponse { | 482 | warp::reply::json(&GradeCoinResponse { |
471 | res: ResponseType::Error, | 483 | res: ResponseType::Error, |
472 | message: "Invalid by field for the proposed transaction".to_owned(), | 484 | message: "Transactions cannot be proposed between two unrelated parties".to_owned(), |
473 | }), | 485 | }), |
474 | StatusCode::BAD_REQUEST, | 486 | StatusCode::BAD_REQUEST, |
475 | )); | 487 | )); |
diff --git a/src/schema.rs b/src/schema.rs index c740bf3..af10b4d 100644 --- a/src/schema.rs +++ b/src/schema.rs | |||
@@ -93,15 +93,28 @@ pub struct Claims { | |||
93 | pub struct Db { | 93 | pub struct Db { |
94 | pub blockchain: Arc<RwLock<Block>>, | 94 | pub blockchain: Arc<RwLock<Block>>, |
95 | pub pending_transactions: Arc<RwLock<HashMap<Fingerprint, Transaction>>>, | 95 | pub pending_transactions: Arc<RwLock<HashMap<Fingerprint, Transaction>>>, |
96 | pub users: Arc<RwLock<HashMap<String, User>>>, | 96 | pub users: Arc<RwLock<HashMap<Fingerprint, User>>>, |
97 | } | 97 | } |
98 | 98 | ||
99 | impl Db { | 99 | impl Db { |
100 | fn new() -> Self { | 100 | fn new() -> Self { |
101 | let mut users: HashMap<Fingerprint, User> = HashMap::new(); | ||
102 | |||
103 | let bank_acc = MetuId::new("bank".to_owned(), "P7oxDm30g1jeIId".to_owned()).unwrap(); | ||
104 | |||
105 | users.insert( | ||
106 | "31415926535897932384626433832795028841971693993751058209749445923".to_owned(), | ||
107 | User { | ||
108 | user_id: bank_acc, | ||
109 | public_key: "null".to_owned(), | ||
110 | balance: 27 * 80, | ||
111 | }, | ||
112 | ); | ||
113 | |||
101 | Db { | 114 | Db { |
102 | blockchain: Arc::new(RwLock::new(Block::new())), | 115 | blockchain: Arc::new(RwLock::new(Block::new())), |
103 | pending_transactions: Arc::new(RwLock::new(HashMap::new())), | 116 | pending_transactions: Arc::new(RwLock::new(HashMap::new())), |
104 | users: Arc::new(RwLock::new(HashMap::new())), | 117 | users: Arc::new(RwLock::new(users)), |
105 | } | 118 | } |
106 | } | 119 | } |
107 | } | 120 | } |
@@ -216,6 +229,7 @@ lazy_static! { | |||
216 | ("e223786", "UxI6czykJfp9T9N"), | 229 | ("e223786", "UxI6czykJfp9T9N"), |
217 | ("e231060", "VJgziofQQPCoisH"), | 230 | ("e231060", "VJgziofQQPCoisH"), |
218 | ("e223795", "pmcTCKox99NFsqp"), | 231 | ("e223795", "pmcTCKox99NFsqp"), |
232 | ("bank", "P7oxDm30g1jeIId"), | ||
219 | ] | 233 | ] |
220 | .iter() | 234 | .iter() |
221 | .cloned() | 235 | .cloned() |