From 90bad51a5f2645fa0179f6af589518895329421d Mon Sep 17 00:00:00 2001 From: Yağız Şenal Date: Tue, 7 Sep 2021 22:53:47 +0300 Subject: Implements staking --- src/handlers.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/handlers.rs') 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; use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; use lazy_static::lazy_static; use log::{debug, warn}; +use math; use md5::Md5; use parking_lot::RwLockUpgradableReadGuard; use rsa::{PaddingScheme, RSAPrivateKey}; @@ -33,6 +34,8 @@ const TX_UPPER_LIMIT: u16 = 10; const TX_LOWER_LIMIT: u16 = 1; // Transaction traffic reward const TX_TRAFFIC_REWARD: u16 = 1; +// Staking reward +const STAKING_REWARD: f64 = 0.1; // Percentage // Encryption primitive type Aes128Cbc = Cbc; @@ -567,6 +570,7 @@ pub async fn propose_block( if let Some(transaction) = pending_transactions.remove(fingerprint) { let source = &transaction.source; let target = &transaction.target; + let is_source_bot = users_store.get(source).unwrap().is_bot; if let Some(from) = users_store.get_mut(source) { from.balance -= transaction.amount - TX_TRAFFIC_REWARD; @@ -574,12 +578,17 @@ pub async fn propose_block( if let Some(to) = users_store.get_mut(target) { to.balance += transaction.amount; + if is_source_bot { + // Add staking reward + to.balance += + math::round::ceil((transaction.amount as f64) * STAKING_REWARD, 0) + as u16; + } } // if the receiver is a bot, they will reciprocate if users_store.get(target).unwrap().is_bot { - let transaction_id = - calculate_transaction_id(&transaction.target, &transaction.source); + let transaction_id = calculate_transaction_id(target, source); pending_transactions.insert( transaction_id, Transaction { -- cgit v1.2.3-70-g09d2