From 2ea30741c5f5f780d4e7e6edb01942f404dd810b Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Tue, 26 Apr 2022 00:49:10 +0300 Subject: bugfix: place guy files under network directories guy files were getting written in the correct directory but were getting updated on the parent directory (/users) this moves the "update guy file" logic to its own function so this sort of thing doesn't happen again writing blocks to disk got moved to its own function as well --- src/handlers.rs | 81 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/handlers.rs b/src/handlers.rs index 09cd2a5..e45ebcd 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -335,11 +335,7 @@ pub async fn authenticate_user( }) .unwrap(); - fs::write( - format!("users/{}/{}.guy", db.config.name, new_user.user_id), - user_at_rest_json, - ) - .unwrap(); + write_guy_file(&db.config.name, &new_user.user_id, &user_at_rest_json); let mut userlist = db.users.write(); userlist.insert(fingerprint.clone(), new_user); @@ -621,34 +617,30 @@ pub async fn propose_block( } // just update everyone's .guy file - for (fp, guy) in users_store.iter() { - if !guy.is_bot { + for (fp, user) in users_store.iter() { + if !user.is_bot { let user_at_rest_json = serde_json::to_string(&UserAtRest { fingerprint: fp.clone(), user: User { - user_id: guy.user_id.clone(), - public_key: guy.public_key.clone(), - balance: guy.balance, + user_id: user.user_id.clone(), + public_key: user.public_key.clone(), + balance: user.balance, is_bot: false, }, }) .unwrap(); - fs::write(format!("users/{}.guy", guy.user_id), user_at_rest_json).unwrap(); + write_guy_file(&db.config.name, &user.user_id, &user_at_rest_json); } } } let block_json = serde_json::to_string(&new_block).unwrap(); - fs::write( - format!( - "blocks/{}/{}.block", - db.config.name, - new_block.timestamp.timestamp() - ), - block_json, - ) - .unwrap(); + write_block( + &db.config.name, + new_block.timestamp.timestamp(), + &block_json, + ); { let mut blockchain = db.blockchain.write(); @@ -928,13 +920,31 @@ pub async fn propose_transaction( /// GET /block /// Returns the last block's JSON /// Cannot fail -/// Mostly around for debug purposes pub async fn list_blocks(db: Db) -> Result { let block = db.blockchain.read(); Ok(reply::with_status(reply::json(&*block), StatusCode::OK)) } +/// GET /user +/// Returns an HTML file with the current standing of users +pub async fn user_list_handler(db: Db) -> Result { + let users = db.users.read(); + let mut sane_users = Vec::new(); + + for (fingerprint, user) in users.iter() { + sane_users.push(DisplayUsers { + fingerprint: fingerprint.clone(), + balance: user.balance, + is_bot: user.is_bot, + }); + } + + let template = UserTemplate { users: &sane_users }; + let res = template.render().unwrap(); + Ok(warp::reply::html(res)) +} + /// Handles the JWT Authorization /// /// *[`jwt_token`]: The raw JWT token, "Bearer aaa.bbb.ccc" @@ -986,6 +996,18 @@ fn authorize_proposer(jwt_token: &str, user_pem: &str) -> Result String { let long_fingerprint = format!("{}{}{}", source, target, Utc::now()); let id = format!("{:x}", Sha256::digest(long_fingerprint.as_bytes())); @@ -1004,23 +1026,6 @@ struct DisplayUsers { is_bot: bool, } -pub async fn user_list_handler(db: Db) -> Result { - let users = db.users.read(); - let mut sane_users = Vec::new(); - - for (fingerprint, user) in users.iter() { - sane_users.push(DisplayUsers { - fingerprint: fingerprint.clone(), - balance: user.balance, - is_bot: user.is_bot, - }); - } - - let template = UserTemplate { users: &sane_users }; - let res = template.render().unwrap(); - Ok(warp::reply::html(res)) -} - fn has_unique_elements(iter: T) -> bool where T: IntoIterator, -- cgit v1.2.3-70-g09d2