diff options
| -rw-r--r-- | src/handlers.rs | 32 | ||||
| -rw-r--r-- | src/schema.rs | 2 | 
2 files changed, 27 insertions, 7 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index 349d03b..2dcdafb 100644 --- a/src/handlers.rs +++ b/src/handlers.rs  | |||
| @@ -400,7 +400,7 @@ pub async fn propose_block( | |||
| 400 | let res_json = warp::reply::json(&GradeCoinResponse { | 400 | let res_json = warp::reply::json(&GradeCoinResponse { | 
| 401 | res: ResponseType::Error, | 401 | res: ResponseType::Error, | 
| 402 | message: format!( | 402 | message: format!( | 
| 403 | "There should be at least {} transaction in the block", | 403 | "There should be at least {} transactions in the block", | 
| 404 | BLOCK_TRANSACTION_COUNT | 404 | BLOCK_TRANSACTION_COUNT | 
| 405 | ), | 405 | ), | 
| 406 | }); | 406 | }); | 
| @@ -411,17 +411,18 @@ pub async fn propose_block( | |||
| 411 | // proposer (first transaction fingerprint) checks | 411 | // proposer (first transaction fingerprint) checks | 
| 412 | let pending_transactions = db.pending_transactions.upgradable_read(); | 412 | let pending_transactions = db.pending_transactions.upgradable_read(); | 
| 413 | 413 | ||
| 414 | // we get the proposers fingerprint by finding the transaction (id) then extracting the source | ||
| 414 | let internal_user_fingerprint = match pending_transactions.get(&new_block.transaction_list[0]) { | 415 | let internal_user_fingerprint = match pending_transactions.get(&new_block.transaction_list[0]) { | 
| 415 | Some(coinbase) => &coinbase.source, | 416 | Some(coinbase) => &coinbase.source, | 
| 416 | None => { | 417 | None => { | 
| 417 | debug!( | 418 | debug!( | 
| 418 | "Block proposer with public key signature {:?} is not found in the database", | 419 | "Transaction with id {} is not found in the pending_transactions", | 
| 419 | new_block.transaction_list[0] | 420 | new_block.transaction_list[0] | 
| 420 | ); | 421 | ); | 
| 421 | 422 | ||
| 422 | let res_json = warp::reply::json(&GradeCoinResponse { | 423 | let res_json = warp::reply::json(&GradeCoinResponse { | 
| 423 | res: ResponseType::Error, | 424 | res: ResponseType::Error, | 
| 424 | message: "Proposer of the first transaction is not found in the system".to_owned(), | 425 | message: "First transaction in the block is not found in the system".to_owned(), | 
| 425 | }); | 426 | }); | 
| 426 | 427 | ||
| 427 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); | 428 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); | 
| @@ -584,6 +585,23 @@ pub async fn propose_block( | |||
| 584 | } | 585 | } | 
| 585 | } | 586 | } | 
| 586 | } | 587 | } | 
| 588 | |||
| 589 | // just update everyone's .guy file | ||
| 590 | for (fp, guy) in users_store.iter() { | ||
| 591 | if !guy.is_bot { | ||
| 592 | let user_at_rest_json = serde_json::to_string(&UserAtRest { | ||
| 593 | fingerprint: fp.clone(), | ||
| 594 | user: User { | ||
| 595 | user_id: guy.user_id.clone(), | ||
| 596 | public_key: guy.public_key.clone(), | ||
| 597 | balance: guy.balance, | ||
| 598 | is_bot: false, | ||
| 599 | }, | ||
| 600 | }) | ||
| 601 | .unwrap(); | ||
| 602 | fs::write(format!("users/{}.guy", guy.user_id), user_at_rest_json).unwrap(); | ||
| 603 | } | ||
| 604 | } | ||
| 587 | } | 605 | } | 
| 588 | 606 | ||
| 589 | let block_json = serde_json::to_string(&new_block).unwrap(); | 607 | let block_json = serde_json::to_string(&new_block).unwrap(); | 
| @@ -658,7 +676,7 @@ pub async fn propose_transaction( | |||
| 658 | )); | 676 | )); | 
| 659 | } | 677 | } | 
| 660 | 678 | ||
| 661 | // `internal_user` is an authenticated student, can propose | 679 | // `internal_user` is an authenticated student and not a bot, can propose | 
| 662 | 680 | ||
| 663 | // This public key was already written to the database, we can panic if it's not valid at | 681 | // This public key was already written to the database, we can panic if it's not valid at | 
| 664 | // *this* point | 682 | // *this* point | 
| @@ -827,8 +845,8 @@ fn authorize_proposer(jwt_token: String, user_pem: &str) -> Result<TokenData<Cla | |||
| 827 | Ok(key) => key, | 845 | Ok(key) => key, | 
| 828 | Err(j) => { | 846 | Err(j) => { | 
| 829 | warn!( | 847 | warn!( | 
| 830 | "user has invalid RSA key we should crash and burn here {:?}", | 848 | "given RSA key {} is invalid, we should crash and burn here {:?}", | 
| 831 | j | 849 | user_pem, j | 
| 832 | ); | 850 | ); | 
| 833 | return Err(String::from("This User's RSA key is invalid")); | 851 | return Err(String::from("This User's RSA key is invalid")); | 
| 834 | } | 852 | } | 
| @@ -852,7 +870,7 @@ fn authorize_proposer(jwt_token: String, user_pem: &str) -> Result<TokenData<Cla | |||
| 852 | return Err(String::from("This token has expired")); | 870 | return Err(String::from("This token has expired")); | 
| 853 | } | 871 | } | 
| 854 | _ => { | 872 | _ => { | 
| 855 | warn!("AN UNSPECIFIED ERROR: {:?}", err); | 873 | warn!("AN UNSPECIFIED ERROR: {:?} key was {}", err, user_pem); | 
| 856 | return Err(format!("JWT Error: {}", err)); | 874 | return Err(format!("JWT Error: {}", err)); | 
| 857 | } | 875 | } | 
| 858 | }, | 876 | }, | 
diff --git a/src/schema.rs b/src/schema.rs index 66d6c9c..77e22c1 100644 --- a/src/schema.rs +++ b/src/schema.rs  | |||
| @@ -275,6 +275,8 @@ impl Default for Block { | |||
| 275 | /// * [`user_id`]: Can only be one of the repopulated | 275 | /// * [`user_id`]: Can only be one of the repopulated | 
| 276 | /// * [`public_key`]: A PEM format public key "---- BEGIN" and all | 276 | /// * [`public_key`]: A PEM format public key "---- BEGIN" and all | 
| 277 | /// * [`balance`]: User's current Gradecoin amount | 277 | /// * [`balance`]: User's current Gradecoin amount | 
| 278 | /// | ||
| 279 | /// This should ideally include the fingerprint as well? | ||
| 278 | #[derive(Serialize, Deserialize, Debug, PartialEq)] | 280 | #[derive(Serialize, Deserialize, Debug, PartialEq)] | 
| 279 | pub struct User { | 281 | pub struct User { | 
| 280 | pub user_id: MetuId, | 282 | pub user_id: MetuId, | 
