diff options
| -rw-r--r-- | REFERENCE.md | 5 | ||||
| -rw-r--r-- | TODO.md | 4 | ||||
| -rw-r--r-- | src/handlers.rs | 20 | ||||
| -rw-r--r-- | src/schema.rs | 18 |
4 files changed, 38 insertions, 9 deletions
diff --git a/REFERENCE.md b/REFERENCE.md new file mode 100644 index 0000000..d57378d --- /dev/null +++ b/REFERENCE.md | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | # Reference | ||
| 2 | |||
| 3 | - Don't know where to start? Gradecoin uses RESTful API, simple `curl` commands or even your browser will work! [This website will help as well](https://curl.trillworks.com/). | ||
| 4 | - [JWT Debugger](https://jwt.io) and the corresponding [RFC](https://tools.ietf.org/html/rfc7519) | ||
| 5 | |||
| @@ -1,8 +1,5 @@ | |||
| 1 | # TODO | 1 | # TODO |
| 2 | 2 | ||
| 3 | ## Simulation | ||
| 4 | - [ ] Bank mechanism should be added. | ||
| 5 | |||
| 6 | ## Tests | 3 | ## Tests |
| 7 | - [ ] User Authentication/Authentication Tests | 4 | - [ ] User Authentication/Authentication Tests |
| 8 | - [ ] Route Tests | 5 | - [ ] Route Tests |
| @@ -36,3 +33,4 @@ | |||
| 36 | - [.] POST requests to /block should be authenticated as well (2021-04-13 04:50, they now are but until we make error messages **Verbose** there's not much point in testing because I honestly cannot trace the code) | 33 | - [.] POST requests to /block should be authenticated as well (2021-04-13 04:50, they now are but until we make error messages **Verbose** there's not much point in testing because I honestly cannot trace the code) |
| 37 | - [X] Blocks should "play out" the transactions and execute transactions (2021-04-14 21:29) | 34 | - [X] Blocks should "play out" the transactions and execute transactions (2021-04-14 21:29) |
| 38 | - [X] "Coinbase" ("by" of the first transaction of the block) should get rewarded for their efforts (2021-04-14 21:48) | 35 | - [X] "Coinbase" ("by" of the first transaction of the block) should get rewarded for their efforts (2021-04-14 21:48) |
| 36 | - [X] Implemented Bank Account (2021-04-14 23:28) | ||
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() |
