summaryrefslogtreecommitdiffstats
path: root/src/handlers.rs
diff options
context:
space:
mode:
authorYigit Sever2021-04-14 21:48:49 +0300
committerYigit Sever2021-04-14 21:48:49 +0300
commit271ac922b8e47a74fc467c2a4c54beae7a4fe60d (patch)
tree76dd4a9e7909ca8d65f627dac5abf58b89925974 /src/handlers.rs
parent7e49712be6b887b7bc2d62d72955ab159a786ad5 (diff)
downloadgradecoin-271ac922b8e47a74fc467c2a4c54beae7a4fe60d.tar.gz
gradecoin-271ac922b8e47a74fc467c2a4c54beae7a4fe60d.tar.bz2
gradecoin-271ac922b8e47a74fc467c2a4c54beae7a4fe60d.zip
Implement coinbase reward
Diffstat (limited to 'src/handlers.rs')
-rw-r--r--src/handlers.rs24
1 files changed, 16 insertions, 8 deletions
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
20use crate::PRIVATE_KEY; 20use crate::PRIVATE_KEY;
21const BLOCK_TRANSACTION_COUNT: u8 = 10; 21const BLOCK_TRANSACTION_COUNT: u8 = 10;
22const BLOCK_REWARD: u8 = 3;
22 23
23// Encryption primitive 24// Encryption primitive
24type Aes128Cbc = Cbc<Aes128, Pkcs7>; 25type 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 {