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 --- .gitignore | 1 + Cargo.lock | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/handlers.rs | 13 ++++++++++-- 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 216626e..a1d6555 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ users/ public/ tags.lock tags.temp +secrets/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 22990ad..1cec047 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "aes" version = "0.6.0" @@ -349,6 +351,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -478,6 +486,7 @@ dependencies = [ "hex-literal", "jsonwebtoken", "lazy_static", + "libmath", "log", "log4rs", "md-5", @@ -739,6 +748,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" +[[package]] +name = "libmath" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfd3416934a853ae80d5c3b006f632dfcbaf320300c5167e88a469e9ac214502" +dependencies = [ + "rand 0.3.23", +] + [[package]] name = "linked-hash-map" version = "0.5.4" @@ -1191,6 +1209,29 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8" +[[package]] +name = "rand" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" +dependencies = [ + "libc", + "rand 0.4.6", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi 0.3.9", +] + [[package]] name = "rand" version = "0.7.3" @@ -1236,6 +1277,21 @@ dependencies = [ "rand_core 0.6.2", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.5.1" @@ -1272,6 +1328,15 @@ dependencies = [ "rand_core 0.6.2", ] +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "redox_syscall" version = "0.1.57" diff --git a/Cargo.toml b/Cargo.toml index 77efd25..6bae27f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ sha2 = "0.9.3" block-modes = "0.7.0" aes = "0.6.0" askama = "0.10.5" +libmath = "0.2.1" [dev-dependencies] serde_test = "1.0.117" 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