From e9bf8a1a85d9366e59ec7989772d4e16490f1273 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 22 Apr 2021 20:15:40 +0300 Subject: [WIP] Starting to implement realnet --- src/handlers.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src/handlers.rs') diff --git a/src/handlers.rs b/src/handlers.rs index bf554ab..ca41b61 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -19,6 +19,7 @@ use warp::{http::StatusCode, reply}; use crate::PRIVATE_KEY; const BLOCK_TRANSACTION_COUNT: u8 = 1; +const REGISTER_BONUS: u16 = 40; const BLOCK_REWARD: u16 = 3; const TX_UPPER_LIMIT: u16 = 2; @@ -274,7 +275,8 @@ pub async fn authenticate_user( let new_user = User { user_id: privileged_student_id, public_key: request.public_key, - balance: 0, + balance: REGISTER_BONUS, + is_bot: false, }; debug!("New user authenticated themselves! {:?}", &new_user); @@ -284,6 +286,7 @@ pub async fn authenticate_user( user_id: new_user.user_id.clone(), public_key: new_user.public_key.clone(), balance: 0, + is_bot: false, }, fingerprint: fingerprint.clone(), }) @@ -343,7 +346,11 @@ pub async fn propose_block( warn!("New block proposal: {:?}", &new_block); if new_block.transaction_list.len() < BLOCK_TRANSACTION_COUNT as usize { - debug!("{} transactions offered, needed {}", new_block.transaction_list.len(), BLOCK_TRANSACTION_COUNT); + debug!( + "{} transactions offered, needed {}", + new_block.transaction_list.len(), + BLOCK_TRANSACTION_COUNT + ); let res_json = warp::reply::json(&GradeCoinResponse { res: ResponseType::Error, message: format!( @@ -482,13 +489,11 @@ pub async fn propose_block( // Scope the pending_transactions { - let pending_transactions = db.pending_transactions.read(); + let mut pending_transactions = db.pending_transactions.write(); let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store); - let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap(); - for fingerprint in new_block.transaction_list.iter() { - if let Some(transaction) = pending_transactions.get(fingerprint) { + if let Some(transaction) = pending_transactions.remove(fingerprint) { let source = &transaction.source; let target = &transaction.target; @@ -502,16 +507,13 @@ pub async fn propose_block( } } + let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap(); + if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) { coinbase_user.balance += BLOCK_REWARD; } } - { - let mut pending_transactions = db.pending_transactions.write(); - pending_transactions.clear(); - } - let block_json = serde_json::to_string(&new_block).unwrap(); fs::write( -- cgit v1.2.3-70-g09d2