diff options
author | necrashter | 2022-04-23 13:16:33 +0300 |
---|---|---|
committer | Yigit Sever | 2022-04-23 18:10:12 +0300 |
commit | 7fadbabf9f6632ecc5e097058859c2eae48efc08 (patch) | |
tree | f40e1cc9bc667c421923cdf4197f9df84598812e /src | |
parent | cf0a3cf8489cd84c7b2642c0bc7b50c5dee9a095 (diff) | |
download | gradecoin-7fadbabf9f6632ecc5e097058859c2eae48efc08.tar.gz gradecoin-7fadbabf9f6632ecc5e097058859c2eae48efc08.tar.bz2 gradecoin-7fadbabf9f6632ecc5e097058859c2eae48efc08.zip |
Remove constants in handlers, use db.config
Diffstat (limited to 'src')
-rw-r--r-- | src/handlers.rs | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index 736ad29..f80aa60 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -26,18 +26,6 @@ use warp::{http::StatusCode, reply}; | |||
26 | 26 | ||
27 | use crate::PRIVATE_KEY; | 27 | use crate::PRIVATE_KEY; |
28 | 28 | ||
29 | // Valid blocks should have this many transactions | ||
30 | const BLOCK_TRANSACTION_COUNT: u8 = 4; | ||
31 | // Inital registration bonus | ||
32 | const REGISTER_BONUS: u16 = 20; | ||
33 | // Coinbase reward | ||
34 | const BLOCK_REWARD: u16 = 2; | ||
35 | // Transaction amount limit | ||
36 | const TX_UPPER_LIMIT: u16 = 4; | ||
37 | const TX_LOWER_LIMIT: u16 = 1; | ||
38 | // Transaction traffic reward | ||
39 | const TX_TRAFFIC_REWARD: u16 = 1; | ||
40 | |||
41 | // Encryption primitive | 29 | // Encryption primitive |
42 | type Aes128Cbc = Cbc<Aes128, Pkcs7>; | 30 | type Aes128Cbc = Cbc<Aes128, Pkcs7>; |
43 | 31 | ||
@@ -327,7 +315,7 @@ pub async fn authenticate_user( | |||
327 | let new_user = User { | 315 | let new_user = User { |
328 | user_id: privileged_student_id, | 316 | user_id: privileged_student_id, |
329 | public_key: request.public_key, | 317 | public_key: request.public_key, |
330 | balance: REGISTER_BONUS, | 318 | balance: db.config.register_bonus, |
331 | is_bot: false, | 319 | is_bot: false, |
332 | }; | 320 | }; |
333 | 321 | ||
@@ -392,17 +380,18 @@ pub async fn propose_block( | |||
392 | warn!("New block proposal: {:?}", &new_block); | 380 | warn!("New block proposal: {:?}", &new_block); |
393 | 381 | ||
394 | // Check if there are enough transactions in the block | 382 | // Check if there are enough transactions in the block |
395 | if new_block.transaction_list.len() < BLOCK_TRANSACTION_COUNT as usize { | 383 | let block_transaction_count = db.config.block_transaction_count; |
384 | if new_block.transaction_list.len() < block_transaction_count as usize { | ||
396 | debug!( | 385 | debug!( |
397 | "{} transactions offered, needed {}", | 386 | "{} transactions offered, needed {}", |
398 | new_block.transaction_list.len(), | 387 | new_block.transaction_list.len(), |
399 | BLOCK_TRANSACTION_COUNT | 388 | block_transaction_count |
400 | ); | 389 | ); |
401 | let res_json = warp::reply::json(&GradeCoinResponse { | 390 | let res_json = warp::reply::json(&GradeCoinResponse { |
402 | res: ResponseType::Error, | 391 | res: ResponseType::Error, |
403 | message: format!( | 392 | message: format!( |
404 | "There should be at least {} transactions in the block", | 393 | "There should be at least {} transactions in the block", |
405 | BLOCK_TRANSACTION_COUNT | 394 | block_transaction_count |
406 | ), | 395 | ), |
407 | }); | 396 | }); |
408 | 397 | ||
@@ -552,8 +541,8 @@ pub async fn propose_block( | |||
552 | // See: internal_user_fingerprint, internal_user | 541 | // See: internal_user_fingerprint, internal_user |
553 | let coinbase = pending_transactions.get(&new_block.transaction_list[0]).unwrap(); | 542 | let coinbase = pending_transactions.get(&new_block.transaction_list[0]).unwrap(); |
554 | let mut coinbase_user = users_store.get_mut(&coinbase.source).unwrap(); | 543 | let mut coinbase_user = users_store.get_mut(&coinbase.source).unwrap(); |
555 | coinbase_user.balance += BLOCK_REWARD; | 544 | coinbase_user.balance += db.config.block_reward; |
556 | debug!("{} block reward went to {:?} for mining the block", BLOCK_REWARD, coinbase_user); | 545 | debug!("{} block reward went to {:?} for mining the block", db.config.block_reward, coinbase_user); |
557 | 546 | ||
558 | let mut holding: HashMap<String, Transaction> = HashMap::new(); | 547 | let mut holding: HashMap<String, Transaction> = HashMap::new(); |
559 | 548 | ||
@@ -564,7 +553,7 @@ pub async fn propose_block( | |||
564 | let target = &transaction.target; | 553 | let target = &transaction.target; |
565 | 554 | ||
566 | if let Some(from) = users_store.get_mut(source) { | 555 | if let Some(from) = users_store.get_mut(source) { |
567 | from.balance -= transaction.amount - TX_TRAFFIC_REWARD; | 556 | from.balance -= transaction.amount - db.config.tx_traffic_reward; |
568 | } | 557 | } |
569 | 558 | ||
570 | if let Some(to) = users_store.get_mut(target) { | 559 | if let Some(to) = users_store.get_mut(target) { |
@@ -758,17 +747,19 @@ pub async fn propose_transaction( | |||
758 | } | 747 | } |
759 | 748 | ||
760 | // Is transaction amount within bounds | 749 | // Is transaction amount within bounds |
761 | if new_transaction.amount > TX_UPPER_LIMIT || new_transaction.amount < TX_LOWER_LIMIT { | 750 | let tx_upper_limit = db.config.tx_upper_limit; |
751 | let tx_lower_limit = db.config.tx_lower_limit; | ||
752 | if new_transaction.amount > tx_upper_limit || new_transaction.amount < tx_lower_limit { | ||
762 | debug!( | 753 | debug!( |
763 | "Transaction amount is not between {} and {}, was {}", | 754 | "Transaction amount is not between {} and {}, was {}", |
764 | TX_LOWER_LIMIT, TX_UPPER_LIMIT, new_transaction.amount | 755 | tx_lower_limit, tx_upper_limit, new_transaction.amount |
765 | ); | 756 | ); |
766 | return Ok(warp::reply::with_status( | 757 | return Ok(warp::reply::with_status( |
767 | warp::reply::json(&GradeCoinResponse { | 758 | warp::reply::json(&GradeCoinResponse { |
768 | res: ResponseType::Error, | 759 | res: ResponseType::Error, |
769 | message: format!( | 760 | message: format!( |
770 | "Transaction amount should be between {} and {}", | 761 | "Transaction amount should be between {} and {}", |
771 | TX_LOWER_LIMIT, TX_UPPER_LIMIT | 762 | tx_lower_limit, tx_upper_limit |
772 | ), | 763 | ), |
773 | }), | 764 | }), |
774 | StatusCode::BAD_REQUEST, | 765 | StatusCode::BAD_REQUEST, |