diff options
| author | Yigit Sever | 2021-04-14 21:48:49 +0300 |
|---|---|---|
| committer | Yigit Sever | 2021-04-14 21:48:49 +0300 |
| commit | 271ac922b8e47a74fc467c2a4c54beae7a4fe60d (patch) | |
| tree | 76dd4a9e7909ca8d65f627dac5abf58b89925974 | |
| parent | 7e49712be6b887b7bc2d62d72955ab159a786ad5 (diff) | |
| download | gradecoin-271ac922b8e47a74fc467c2a4c54beae7a4fe60d.tar.gz gradecoin-271ac922b8e47a74fc467c2a4c54beae7a4fe60d.tar.bz2 gradecoin-271ac922b8e47a74fc467c2a4c54beae7a4fe60d.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 | { |
