diff options
author | Yigit Sever | 2021-04-14 21:48:49 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-14 21:48:49 +0300 |
commit | 0599e005daa03e1cf4dc8e238c1c803c1970beb2 (patch) | |
tree | 5e635c5a542c6bcb687b1a1f9e67a4d3f0dfaa31 | |
parent | 09410a41112794137e7a39fb5ffd1cffbc71f002 (diff) | |
download | gradecoin-0599e005daa03e1cf4dc8e238c1c803c1970beb2.tar.gz gradecoin-0599e005daa03e1cf4dc8e238c1c803c1970beb2.tar.bz2 gradecoin-0599e005daa03e1cf4dc8e238c1c803c1970beb2.zip |
Implement coinbase reward
-rw-r--r-- | TODO.md | 2 | ||||
-rw-r--r-- | src/handlers.rs | 24 |
2 files changed, 17 insertions, 9 deletions
@@ -1,7 +1,6 @@ | |||
1 | # TODO | 1 | # TODO |
2 | 2 | ||
3 | ## Simulation | 3 | ## Simulation |
4 | - [ ] "Coinbase" ("by" of the first transaction of the block) should get rewarded for their efforts | ||
5 | - [ ] Bank mechanism should be added. | 4 | - [ ] Bank mechanism should be added. |
6 | 5 | ||
7 | ## Tests | 6 | ## Tests |
@@ -36,3 +35,4 @@ | |||
36 | - [x] Recover database from files | 35 | - [x] Recover database from files |
37 | - [.] POST requests to /block should be authenticated as well (2021-04-13 04:50, they now are but until we make error messages **Verbose** there's not much point in testing because I honestly cannot trace the code) | 36 | - [.] POST requests to /block should be authenticated as well (2021-04-13 04:50, they now are but until we make error messages **Verbose** there's not much point in testing because I honestly cannot trace the code) |
38 | - [X] Blocks should "play out" the transactions and execute transactions (2021-04-14 21:29) | 37 | - [X] Blocks should "play out" the transactions and execute transactions (2021-04-14 21:29) |
38 | - [X] "Coinbase" ("by" of the first transaction of the block) should get rewarded for their efforts (2021-04-14 21:48) | ||
diff --git a/src/handlers.rs b/src/handlers.rs index 420e82a..0dfea15 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -19,6 +19,7 @@ use warp::{http::StatusCode, reply}; | |||
19 | 19 | ||
20 | use crate::PRIVATE_KEY; | 20 | use crate::PRIVATE_KEY; |
21 | const BLOCK_TRANSACTION_COUNT: u8 = 10; | 21 | const BLOCK_TRANSACTION_COUNT: u8 = 10; |
22 | const BLOCK_REWARD: u8 = 3; | ||
22 | 23 | ||
23 | // Encryption primitive | 24 | // Encryption primitive |
24 | type Aes128Cbc = Cbc<Aes128, Pkcs7>; | 25 | type Aes128Cbc = Cbc<Aes128, Pkcs7>; |
@@ -360,19 +361,26 @@ pub async fn authorized_propose_block( | |||
360 | let pending_transactions = db.pending_transactions.read(); | 361 | let pending_transactions = db.pending_transactions.read(); |
361 | let mut users = db.users.write(); | 362 | let mut users = db.users.write(); |
362 | 363 | ||
364 | let coinbase_fingerprint = new_block.transaction_list.get(0).unwrap(); | ||
365 | |||
363 | for fingerprint in new_block.transaction_list.iter() { | 366 | for fingerprint in new_block.transaction_list.iter() { |
364 | let transaction = pending_transactions.get(fingerprint).unwrap(); | 367 | if let Some(transaction) = pending_transactions.get(fingerprint) { |
365 | let source = &transaction.source; | 368 | let source = &transaction.source; |
366 | let target = &transaction.target; | 369 | let target = &transaction.target; |
367 | 370 | ||
368 | if let Some(from) = users.get_mut(source) { | 371 | if let Some(from) = users.get_mut(source) { |
369 | from.balance -= transaction.amount; | 372 | from.balance -= transaction.amount; |
370 | } | 373 | } |
371 | 374 | ||
372 | if let Some(to) = users.get_mut(target) { | 375 | if let Some(to) = users.get_mut(target) { |
373 | to.balance += transaction.amount; | 376 | to.balance += transaction.amount; |
377 | } | ||
374 | } | 378 | } |
375 | } | 379 | } |
380 | |||
381 | if let Some(coinbase_user) = users.get_mut(coinbase_fingerprint) { | ||
382 | coinbase_user.balance += BLOCK_REWARD as i32; | ||
383 | } | ||
376 | } | 384 | } |
377 | 385 | ||
378 | { | 386 | { |