diff options
author | Yağız Şenal | 2021-09-07 22:53:47 +0300 |
---|---|---|
committer | Yağız Şenal | 2021-09-08 20:01:47 +0300 |
commit | 90bad51a5f2645fa0179f6af589518895329421d (patch) | |
tree | f8e3003e4a197f405cb17e830244c13f2925ff34 /src | |
parent | fd24f0730c8d429730d324b02882916b3e579a2d (diff) | |
download | gradecoin-90bad51a5f2645fa0179f6af589518895329421d.tar.gz gradecoin-90bad51a5f2645fa0179f6af589518895329421d.tar.bz2 gradecoin-90bad51a5f2645fa0179f6af589518895329421d.zip |
Implements staking
Diffstat (limited to 'src')
-rw-r--r-- | src/handlers.rs | 13 |
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; | |||
9 | use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; | 9 | use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; |
10 | use lazy_static::lazy_static; | 10 | use lazy_static::lazy_static; |
11 | use log::{debug, warn}; | 11 | use log::{debug, warn}; |
12 | use math; | ||
12 | use md5::Md5; | 13 | use md5::Md5; |
13 | use parking_lot::RwLockUpgradableReadGuard; | 14 | use parking_lot::RwLockUpgradableReadGuard; |
14 | use rsa::{PaddingScheme, RSAPrivateKey}; | 15 | use rsa::{PaddingScheme, RSAPrivateKey}; |
@@ -33,6 +34,8 @@ const TX_UPPER_LIMIT: u16 = 10; | |||
33 | const TX_LOWER_LIMIT: u16 = 1; | 34 | const TX_LOWER_LIMIT: u16 = 1; |
34 | // Transaction traffic reward | 35 | // Transaction traffic reward |
35 | const TX_TRAFFIC_REWARD: u16 = 1; | 36 | const TX_TRAFFIC_REWARD: u16 = 1; |
37 | // Staking reward | ||
38 | const STAKING_REWARD: f64 = 0.1; // Percentage | ||
36 | 39 | ||
37 | // Encryption primitive | 40 | // Encryption primitive |
38 | type Aes128Cbc = Cbc<Aes128, Pkcs7>; | 41 | type 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 { |