aboutsummaryrefslogtreecommitdiffstats
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
parentfd24f0730c8d429730d324b02882916b3e579a2d (diff)
downloadgradecoin-90bad51a5f2645fa0179f6af589518895329421d.tar.gz
gradecoin-90bad51a5f2645fa0179f6af589518895329421d.tar.bz2
gradecoin-90bad51a5f2645fa0179f6af589518895329421d.zip
Implements staking
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock65
-rw-r--r--Cargo.toml1
-rw-r--r--src/handlers.rs13
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/
5public/ 5public/
6tags.lock 6tags.lock
7tags.temp 7tags.temp
8secrets/ \ 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 @@
1# This file is automatically @generated by Cargo. 1# This file is automatically @generated by Cargo.
2# It is not intended for manual editing. 2# It is not intended for manual editing.
3version = 3
4
3[[package]] 5[[package]]
4name = "aes" 6name = "aes"
5version = "0.6.0" 7version = "0.6.0"
@@ -350,6 +352,12 @@ dependencies = [
350] 352]
351 353
352[[package]] 354[[package]]
355name = "fuchsia-cprng"
356version = "0.1.1"
357source = "registry+https://github.com/rust-lang/crates.io-index"
358checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
359
360[[package]]
353name = "fuchsia-zircon" 361name = "fuchsia-zircon"
354version = "0.3.3" 362version = "0.3.3"
355source = "registry+https://github.com/rust-lang/crates.io-index" 363source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -478,6 +486,7 @@ dependencies = [
478 "hex-literal", 486 "hex-literal",
479 "jsonwebtoken", 487 "jsonwebtoken",
480 "lazy_static", 488 "lazy_static",
489 "libmath",
481 "log", 490 "log",
482 "log4rs", 491 "log4rs",
483 "md-5", 492 "md-5",
@@ -740,6 +749,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
740checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" 749checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a"
741 750
742[[package]] 751[[package]]
752name = "libmath"
753version = "0.2.1"
754source = "registry+https://github.com/rust-lang/crates.io-index"
755checksum = "dfd3416934a853ae80d5c3b006f632dfcbaf320300c5167e88a469e9ac214502"
756dependencies = [
757 "rand 0.3.23",
758]
759
760[[package]]
743name = "linked-hash-map" 761name = "linked-hash-map"
744version = "0.5.4" 762version = "0.5.4"
745source = "registry+https://github.com/rust-lang/crates.io-index" 763source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1193,6 +1211,29 @@ checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8"
1193 1211
1194[[package]] 1212[[package]]
1195name = "rand" 1213name = "rand"
1214version = "0.3.23"
1215source = "registry+https://github.com/rust-lang/crates.io-index"
1216checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
1217dependencies = [
1218 "libc",
1219 "rand 0.4.6",
1220]
1221
1222[[package]]
1223name = "rand"
1224version = "0.4.6"
1225source = "registry+https://github.com/rust-lang/crates.io-index"
1226checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
1227dependencies = [
1228 "fuchsia-cprng",
1229 "libc",
1230 "rand_core 0.3.1",
1231 "rdrand",
1232 "winapi 0.3.9",
1233]
1234
1235[[package]]
1236name = "rand"
1196version = "0.7.3" 1237version = "0.7.3"
1197source = "registry+https://github.com/rust-lang/crates.io-index" 1238source = "registry+https://github.com/rust-lang/crates.io-index"
1198checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" 1239checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
@@ -1238,6 +1279,21 @@ dependencies = [
1238 1279
1239[[package]] 1280[[package]]
1240name = "rand_core" 1281name = "rand_core"
1282version = "0.3.1"
1283source = "registry+https://github.com/rust-lang/crates.io-index"
1284checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
1285dependencies = [
1286 "rand_core 0.4.2",
1287]
1288
1289[[package]]
1290name = "rand_core"
1291version = "0.4.2"
1292source = "registry+https://github.com/rust-lang/crates.io-index"
1293checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
1294
1295[[package]]
1296name = "rand_core"
1241version = "0.5.1" 1297version = "0.5.1"
1242source = "registry+https://github.com/rust-lang/crates.io-index" 1298source = "registry+https://github.com/rust-lang/crates.io-index"
1243checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" 1299checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
@@ -1273,6 +1329,15 @@ dependencies = [
1273] 1329]
1274 1330
1275[[package]] 1331[[package]]
1332name = "rdrand"
1333version = "0.4.0"
1334source = "registry+https://github.com/rust-lang/crates.io-index"
1335checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
1336dependencies = [
1337 "rand_core 0.3.1",
1338]
1339
1340[[package]]
1276name = "redox_syscall" 1341name = "redox_syscall"
1277version = "0.1.57" 1342version = "0.1.57"
1278source = "registry+https://github.com/rust-lang/crates.io-index" 1343source = "registry+https://github.com/rust-lang/crates.io-index"
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"
27block-modes = "0.7.0" 27block-modes = "0.7.0"
28aes = "0.6.0" 28aes = "0.6.0"
29askama = "0.10.5" 29askama = "0.10.5"
30libmath = "0.2.1"
30 31
31[dev-dependencies] 32[dev-dependencies]
32serde_test = "1.0.117" 33serde_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;
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 {