diff options
| author | Yiğit Sever | 2021-09-09 01:56:58 +0300 |
|---|---|---|
| committer | GitHub | 2021-09-09 01:56:58 +0300 |
| commit | 04c3b4597a995d73e6925c5391bfa27d27f298b7 (patch) | |
| tree | f8e3003e4a197f405cb17e830244c13f2925ff34 | |
| parent | fd24f0730c8d429730d324b02882916b3e579a2d (diff) | |
| parent | 90bad51a5f2645fa0179f6af589518895329421d (diff) | |
| download | gradecoin-04c3b4597a995d73e6925c5391bfa27d27f298b7.tar.gz gradecoin-04c3b4597a995d73e6925c5391bfa27d27f298b7.tar.bz2 gradecoin-04c3b4597a995d73e6925c5391bfa27d27f298b7.zip | |
Merge pull request #6 from yagizsenal/2-staking
Implements staking
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Cargo.lock | 65 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/handlers.rs | 13 |
4 files changed, 78 insertions, 2 deletions
| @@ -5,3 +5,4 @@ users/ | |||
| 5 | public/ | 5 | public/ |
| 6 | tags.lock | 6 | tags.lock |
| 7 | tags.temp | 7 | tags.temp |
| 8 | secrets/ \ No newline at end of file | ||
| @@ -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. |
| 3 | version = 3 | ||
| 4 | |||
| 3 | [[package]] | 5 | [[package]] |
| 4 | name = "aes" | 6 | name = "aes" |
| 5 | version = "0.6.0" | 7 | version = "0.6.0" |
| @@ -350,6 +352,12 @@ dependencies = [ | |||
| 350 | ] | 352 | ] |
| 351 | 353 | ||
| 352 | [[package]] | 354 | [[package]] |
| 355 | name = "fuchsia-cprng" | ||
| 356 | version = "0.1.1" | ||
| 357 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 358 | checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" | ||
| 359 | |||
| 360 | [[package]] | ||
| 353 | name = "fuchsia-zircon" | 361 | name = "fuchsia-zircon" |
| 354 | version = "0.3.3" | 362 | version = "0.3.3" |
| 355 | source = "registry+https://github.com/rust-lang/crates.io-index" | 363 | source = "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" | |||
| 740 | checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" | 749 | checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" |
| 741 | 750 | ||
| 742 | [[package]] | 751 | [[package]] |
| 752 | name = "libmath" | ||
| 753 | version = "0.2.1" | ||
| 754 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 755 | checksum = "dfd3416934a853ae80d5c3b006f632dfcbaf320300c5167e88a469e9ac214502" | ||
| 756 | dependencies = [ | ||
| 757 | "rand 0.3.23", | ||
| 758 | ] | ||
| 759 | |||
| 760 | [[package]] | ||
| 743 | name = "linked-hash-map" | 761 | name = "linked-hash-map" |
| 744 | version = "0.5.4" | 762 | version = "0.5.4" |
| 745 | source = "registry+https://github.com/rust-lang/crates.io-index" | 763 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -1193,6 +1211,29 @@ checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8" | |||
| 1193 | 1211 | ||
| 1194 | [[package]] | 1212 | [[package]] |
| 1195 | name = "rand" | 1213 | name = "rand" |
| 1214 | version = "0.3.23" | ||
| 1215 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 1216 | checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" | ||
| 1217 | dependencies = [ | ||
| 1218 | "libc", | ||
| 1219 | "rand 0.4.6", | ||
| 1220 | ] | ||
| 1221 | |||
| 1222 | [[package]] | ||
| 1223 | name = "rand" | ||
| 1224 | version = "0.4.6" | ||
| 1225 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 1226 | checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" | ||
| 1227 | dependencies = [ | ||
| 1228 | "fuchsia-cprng", | ||
| 1229 | "libc", | ||
| 1230 | "rand_core 0.3.1", | ||
| 1231 | "rdrand", | ||
| 1232 | "winapi 0.3.9", | ||
| 1233 | ] | ||
| 1234 | |||
| 1235 | [[package]] | ||
| 1236 | name = "rand" | ||
| 1196 | version = "0.7.3" | 1237 | version = "0.7.3" |
| 1197 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1238 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1198 | checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" | 1239 | checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" |
| @@ -1238,6 +1279,21 @@ dependencies = [ | |||
| 1238 | 1279 | ||
| 1239 | [[package]] | 1280 | [[package]] |
| 1240 | name = "rand_core" | 1281 | name = "rand_core" |
| 1282 | version = "0.3.1" | ||
| 1283 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 1284 | checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" | ||
| 1285 | dependencies = [ | ||
| 1286 | "rand_core 0.4.2", | ||
| 1287 | ] | ||
| 1288 | |||
| 1289 | [[package]] | ||
| 1290 | name = "rand_core" | ||
| 1291 | version = "0.4.2" | ||
| 1292 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 1293 | checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" | ||
| 1294 | |||
| 1295 | [[package]] | ||
| 1296 | name = "rand_core" | ||
| 1241 | version = "0.5.1" | 1297 | version = "0.5.1" |
| 1242 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1298 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1243 | checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" | 1299 | checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" |
| @@ -1273,6 +1329,15 @@ dependencies = [ | |||
| 1273 | ] | 1329 | ] |
| 1274 | 1330 | ||
| 1275 | [[package]] | 1331 | [[package]] |
| 1332 | name = "rdrand" | ||
| 1333 | version = "0.4.0" | ||
| 1334 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 1335 | checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" | ||
| 1336 | dependencies = [ | ||
| 1337 | "rand_core 0.3.1", | ||
| 1338 | ] | ||
| 1339 | |||
| 1340 | [[package]] | ||
| 1276 | name = "redox_syscall" | 1341 | name = "redox_syscall" |
| 1277 | version = "0.1.57" | 1342 | version = "0.1.57" |
| 1278 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1343 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -27,6 +27,7 @@ sha2 = "0.9.3" | |||
| 27 | block-modes = "0.7.0" | 27 | block-modes = "0.7.0" |
| 28 | aes = "0.6.0" | 28 | aes = "0.6.0" |
| 29 | askama = "0.10.5" | 29 | askama = "0.10.5" |
| 30 | libmath = "0.2.1" | ||
| 30 | 31 | ||
| 31 | [dev-dependencies] | 32 | [dev-dependencies] |
| 32 | serde_test = "1.0.117" | 33 | 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; | |||
| 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 { |
