From 797b0c5a686df7483540f4cb6baaeed6d5147ee8 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Wed, 14 Apr 2021 23:27:41 +0300 Subject: Implement the bank account --- src/handlers.rs | 20 ++++++++++++++++---- 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( /// * `token` - An Authorization header value such as `Bearer aaa.bbb.ccc` /// * `db` - Global [`Db`] instance /// -/// TODO This method should check if the user has enough balance for the transaction pub async fn authorized_propose_transaction( new_transaction: Transaction, token: String, @@ -459,17 +458,30 @@ pub async fn authorized_propose_transaction( return Ok(warp::reply::with_status( warp::reply::json(&GradeCoinResponse { res: ResponseType::Error, - message: "User does not have enough balance in their account".to_owned(), + message: + "User does not have enough balance in their account for this transaction" + .to_owned(), }), StatusCode::BAD_REQUEST, )); } + } else if new_transaction.by == new_transaction.target + && new_transaction.source + != "31415926535897932384626433832795028841971693993751058209749445923" + { + // Propose to transact with the bank + return Ok(warp::reply::with_status( + warp::reply::json(&GradeCoinResponse { + res: ResponseType::Error, + message: "Transactions cannot extort Gradecoin from unsuspecting users".to_owned(), + }), + StatusCode::BAD_REQUEST, + )); } else { - // TODO: add bank mechanism <14-04-21, keles> // return Ok(warp::reply::with_status( warp::reply::json(&GradeCoinResponse { res: ResponseType::Error, - message: "Invalid by field for the proposed transaction".to_owned(), + message: "Transactions cannot be proposed between two unrelated parties".to_owned(), }), StatusCode::BAD_REQUEST, )); diff --git a/src/schema.rs b/src/schema.rs index 2a9e1db..1002cb8 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -93,15 +93,28 @@ pub struct Claims { pub struct Db { pub blockchain: Arc>, pub pending_transactions: Arc>>, - pub users: Arc>>, + pub users: Arc>>, } impl Db { fn new() -> Self { + let mut users: HashMap = HashMap::new(); + + let bank_acc = MetuId::new("bank".to_owned(), "P7oxDm30g1jeIId".to_owned()).unwrap(); + + users.insert( + "31415926535897932384626433832795028841971693993751058209749445923".to_owned(), + User { + user_id: bank_acc, + public_key: "null".to_owned(), + balance: 27 * 80, + }, + ); + Db { blockchain: Arc::new(RwLock::new(Block::new())), pending_transactions: Arc::new(RwLock::new(HashMap::new())), - users: Arc::new(RwLock::new(HashMap::new())), + users: Arc::new(RwLock::new(users)), } } } @@ -216,6 +229,7 @@ lazy_static! { ("e223786", "UxI6czykJfp9T9N"), ("e231060", "VJgziofQQPCoisH"), ("e223795", "pmcTCKox99NFsqp"), + ("bank", "P7oxDm30g1jeIId"), ] .iter() .cloned() -- cgit v1.2.3-70-g09d2