diff options
Diffstat (limited to 'src/handlers.rs')
-rw-r--r-- | src/handlers.rs | 24 |
1 files changed, 13 insertions, 11 deletions
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}; | |||
19 | 19 | ||
20 | use crate::PRIVATE_KEY; | 20 | use crate::PRIVATE_KEY; |
21 | const BLOCK_TRANSACTION_COUNT: u8 = 1; | 21 | const BLOCK_TRANSACTION_COUNT: u8 = 1; |
22 | const REGISTER_BONUS: u16 = 40; | ||
22 | const BLOCK_REWARD: u16 = 3; | 23 | const BLOCK_REWARD: u16 = 3; |
23 | const TX_UPPER_LIMIT: u16 = 2; | 24 | const TX_UPPER_LIMIT: u16 = 2; |
24 | 25 | ||
@@ -274,7 +275,8 @@ pub async fn authenticate_user( | |||
274 | let new_user = User { | 275 | let new_user = User { |
275 | user_id: privileged_student_id, | 276 | user_id: privileged_student_id, |
276 | public_key: request.public_key, | 277 | public_key: request.public_key, |
277 | balance: 0, | 278 | balance: REGISTER_BONUS, |
279 | is_bot: false, | ||
278 | }; | 280 | }; |
279 | 281 | ||
280 | debug!("New user authenticated themselves! {:?}", &new_user); | 282 | debug!("New user authenticated themselves! {:?}", &new_user); |
@@ -284,6 +286,7 @@ pub async fn authenticate_user( | |||
284 | user_id: new_user.user_id.clone(), | 286 | user_id: new_user.user_id.clone(), |
285 | public_key: new_user.public_key.clone(), | 287 | public_key: new_user.public_key.clone(), |
286 | balance: 0, | 288 | balance: 0, |
289 | is_bot: false, | ||
287 | }, | 290 | }, |
288 | fingerprint: fingerprint.clone(), | 291 | fingerprint: fingerprint.clone(), |
289 | }) | 292 | }) |
@@ -343,7 +346,11 @@ pub async fn propose_block( | |||
343 | warn!("New block proposal: {:?}", &new_block); | 346 | warn!("New block proposal: {:?}", &new_block); |
344 | 347 | ||
345 | if new_block.transaction_list.len() < BLOCK_TRANSACTION_COUNT as usize { | 348 | if new_block.transaction_list.len() < BLOCK_TRANSACTION_COUNT as usize { |
346 | debug!("{} transactions offered, needed {}", new_block.transaction_list.len(), BLOCK_TRANSACTION_COUNT); | 349 | debug!( |
350 | "{} transactions offered, needed {}", | ||
351 | new_block.transaction_list.len(), | ||
352 | BLOCK_TRANSACTION_COUNT | ||
353 | ); | ||
347 | let res_json = warp::reply::json(&GradeCoinResponse { | 354 | let res_json = warp::reply::json(&GradeCoinResponse { |
348 | res: ResponseType::Error, | 355 | res: ResponseType::Error, |
349 | message: format!( | 356 | message: format!( |
@@ -482,13 +489,11 @@ pub async fn propose_block( | |||
482 | 489 | ||
483 | // Scope the pending_transactions | 490 | // Scope the pending_transactions |
484 | { | 491 | { |
485 | let pending_transactions = db.pending_transactions.read(); | 492 | let mut pending_transactions = db.pending_transactions.write(); |
486 | let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store); | 493 | let mut users_store = RwLockUpgradableReadGuard::upgrade(users_store); |
487 | 494 | ||
488 | let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap(); | ||
489 | |||
490 | for fingerprint in new_block.transaction_list.iter() { | 495 | for fingerprint in new_block.transaction_list.iter() { |
491 | if let Some(transaction) = pending_transactions.get(fingerprint) { | 496 | if let Some(transaction) = pending_transactions.remove(fingerprint) { |
492 | let source = &transaction.source; | 497 | let source = &transaction.source; |
493 | let target = &transaction.target; | 498 | let target = &transaction.target; |
494 | 499 | ||
@@ -502,16 +507,13 @@ pub async fn propose_block( | |||
502 | } | 507 | } |
503 | } | 508 | } |
504 | 509 | ||
510 | let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap(); | ||
511 | |||
505 | if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) { | 512 | if let Some(coinbase_user) = users_store.get_mut(coinbase_fingerprint) { |
506 | coinbase_user.balance += BLOCK_REWARD; | 513 | coinbase_user.balance += BLOCK_REWARD; |
507 | } | 514 | } |
508 | } | 515 | } |
509 | 516 | ||
510 | { | ||
511 | let mut pending_transactions = db.pending_transactions.write(); | ||
512 | pending_transactions.clear(); | ||
513 | } | ||
514 | |||
515 | let block_json = serde_json::to_string(&new_block).unwrap(); | 517 | let block_json = serde_json::to_string(&new_block).unwrap(); |
516 | 518 | ||
517 | fs::write( | 519 | fs::write( |