aboutsummaryrefslogtreecommitdiffstats
path: root/src/handlers.rs
diff options
context:
space:
mode:
authorYağız Şenal2021-09-07 22:53:47 +0300
committerYağız Şenal2021-09-08 20:01:47 +0300
commit90bad51a5f2645fa0179f6af589518895329421d (patch)
treef8e3003e4a197f405cb17e830244c13f2925ff34 /src/handlers.rs
parentfd24f0730c8d429730d324b02882916b3e579a2d (diff)
downloadgradecoin-90bad51a5f2645fa0179f6af589518895329421d.tar.gz
gradecoin-90bad51a5f2645fa0179f6af589518895329421d.tar.bz2
gradecoin-90bad51a5f2645fa0179f6af589518895329421d.zip
Implements staking
Diffstat (limited to 'src/handlers.rs')
-rw-r--r--src/handlers.rs13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index 5273324..2e9964c 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -9,6 +9,7 @@ use jsonwebtoken::errors::ErrorKind;
9use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; 9use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation};
10use lazy_static::lazy_static; 10use lazy_static::lazy_static;
11use log::{debug, warn}; 11use log::{debug, warn};
12use math;
12use md5::Md5; 13use md5::Md5;
13use parking_lot::RwLockUpgradableReadGuard; 14use parking_lot::RwLockUpgradableReadGuard;
14use rsa::{PaddingScheme, RSAPrivateKey}; 15use rsa::{PaddingScheme, RSAPrivateKey};
@@ -33,6 +34,8 @@ const TX_UPPER_LIMIT: u16 = 10;
33const TX_LOWER_LIMIT: u16 = 1; 34const TX_LOWER_LIMIT: u16 = 1;
34// Transaction traffic reward 35// Transaction traffic reward
35const TX_TRAFFIC_REWARD: u16 = 1; 36const TX_TRAFFIC_REWARD: u16 = 1;
37// Staking reward
38const STAKING_REWARD: f64 = 0.1; // Percentage
36 39
37// Encryption primitive 40// Encryption primitive
38type Aes128Cbc = Cbc<Aes128, Pkcs7>; 41type Aes128Cbc = Cbc<Aes128, Pkcs7>;
@@ -567,6 +570,7 @@ pub async fn propose_block(
567 if let Some(transaction) = pending_transactions.remove(fingerprint) { 570 if let Some(transaction) = pending_transactions.remove(fingerprint) {
568 let source = &transaction.source; 571 let source = &transaction.source;
569 let target = &transaction.target; 572 let target = &transaction.target;
573 let is_source_bot = users_store.get(source).unwrap().is_bot;
570 574
571 if let Some(from) = users_store.get_mut(source) { 575 if let Some(from) = users_store.get_mut(source) {
572 from.balance -= transaction.amount - TX_TRAFFIC_REWARD; 576 from.balance -= transaction.amount - TX_TRAFFIC_REWARD;
@@ -574,12 +578,17 @@ pub async fn propose_block(
574 578
575 if let Some(to) = users_store.get_mut(target) { 579 if let Some(to) = users_store.get_mut(target) {
576 to.balance += transaction.amount; 580 to.balance += transaction.amount;
581 if is_source_bot {
582 // Add staking reward
583 to.balance +=
584 math::round::ceil((transaction.amount as f64) * STAKING_REWARD, 0)
585 as u16;
586 }
577 } 587 }
578 588
579 // if the receiver is a bot, they will reciprocate 589 // if the receiver is a bot, they will reciprocate
580 if users_store.get(target).unwrap().is_bot { 590 if users_store.get(target).unwrap().is_bot {
581 let transaction_id = 591 let transaction_id = calculate_transaction_id(target, source);
582 calculate_transaction_id(&transaction.target, &transaction.source);
583 pending_transactions.insert( 592 pending_transactions.insert(
584 transaction_id, 593 transaction_id,
585 Transaction { 594 Transaction {