diff options
author | Yigit Sever | 2021-04-19 03:57:08 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-19 03:57:08 +0300 |
commit | 87c312b9629b3ad8da441d8ebe71d2c3a431f20b (patch) | |
tree | 3c3d535fe1cb3f34fddfd6acbd9ecee8ca5fbdcf /src/handlers.rs | |
parent | 7e16965b37a1b125994fa85b97924f0ed4ea48ad (diff) | |
download | gradecoin-87c312b9629b3ad8da441d8ebe71d2c3a431f20b.tar.gz gradecoin-87c312b9629b3ad8da441d8ebe71d2c3a431f20b.tar.bz2 gradecoin-87c312b9629b3ad8da441d8ebe71d2c3a431f20b.zip |
Bugfix
It was possible (and hilarious) to mint a new block with just one
transaction, by repeating it 5 times, lol
Diffstat (limited to 'src/handlers.rs')
-rw-r--r-- | src/handlers.rs | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index a5070a4..e12d83e 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use aes::Aes128; | ||
2 | /// API handlers, the ends of each filter chain | 1 | /// API handlers, the ends of each filter chain |
2 | use aes::Aes128; | ||
3 | use askama::Template; | 3 | use askama::Template; |
4 | use blake2::{Blake2s, Digest}; | 4 | use blake2::{Blake2s, Digest}; |
5 | use block_modes::block_padding::Pkcs7; | 5 | use block_modes::block_padding::Pkcs7; |
@@ -12,7 +12,7 @@ use parking_lot::RwLockUpgradableReadGuard; | |||
12 | use rsa::{PaddingScheme, RSAPrivateKey}; | 12 | use rsa::{PaddingScheme, RSAPrivateKey}; |
13 | use serde::Serialize; | 13 | use serde::Serialize; |
14 | use sha2::Sha256; | 14 | use sha2::Sha256; |
15 | use std::collections::HashMap; | 15 | use std::collections::{HashMap, HashSet}; |
16 | use std::convert::Infallible; | 16 | use std::convert::Infallible; |
17 | use std::fs; | 17 | use std::fs; |
18 | use warp::{http::StatusCode, reply}; | 18 | use warp::{http::StatusCode, reply}; |
@@ -404,6 +404,26 @@ pub async fn propose_block( | |||
404 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); | 404 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); |
405 | } | 405 | } |
406 | 406 | ||
407 | // scope the HashSet | ||
408 | { | ||
409 | let mut proposed_transactions = HashSet::new(); | ||
410 | for tx in new_block.transaction_list.iter() { | ||
411 | proposed_transactions.insert(tx); | ||
412 | } | ||
413 | |||
414 | if proposed_transactions.len() != BLOCK_TRANSACTION_COUNT as usize { | ||
415 | let res_json = warp::reply::json(&GradeCoinResponse { | ||
416 | res: ResponseType::Error, | ||
417 | message: format!( | ||
418 | "Block cannot contain less than {} unique transactions.", | ||
419 | BLOCK_TRANSACTION_COUNT | ||
420 | ), | ||
421 | }); | ||
422 | |||
423 | return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST)); | ||
424 | } | ||
425 | } | ||
426 | |||
407 | // Scope the RwLocks, there are hashing stuff below | 427 | // Scope the RwLocks, there are hashing stuff below |
408 | { | 428 | { |
409 | let pending_transactions = db.pending_transactions.read(); | 429 | let pending_transactions = db.pending_transactions.read(); |
@@ -457,7 +477,7 @@ pub async fn propose_block( | |||
457 | } | 477 | } |
458 | 478 | ||
459 | // All clear, block accepted! | 479 | // All clear, block accepted! |
460 | debug!("We have a new block! {:?}", new_block); | 480 | warn!("ACCEPTED BLOCK {:?}", new_block); |
461 | 481 | ||
462 | // Scope the pending_transactions | 482 | // Scope the pending_transactions |
463 | { | 483 | { |
@@ -529,6 +549,8 @@ pub async fn propose_transaction( | |||
529 | ) -> Result<impl warp::Reply, warp::Rejection> { | 549 | ) -> Result<impl warp::Reply, warp::Rejection> { |
530 | debug!("POST /transaction, propose_transaction() is handling"); | 550 | debug!("POST /transaction, propose_transaction() is handling"); |
531 | 551 | ||
552 | warn!("New transaction proposal: {:?}", &new_transaction); | ||
553 | |||
532 | let users_store = db.users.read(); | 554 | let users_store = db.users.read(); |
533 | 555 | ||
534 | // Is this transaction from an authorized source? | 556 | // Is this transaction from an authorized source? |
@@ -672,7 +694,7 @@ pub async fn propose_transaction( | |||
672 | )); | 694 | )); |
673 | } | 695 | } |
674 | 696 | ||
675 | warn!("NEW TRANSACTION {:?}", new_transaction); | 697 | warn!("ACCEPTED TRANSACTION {:?}", new_transaction); |
676 | 698 | ||
677 | let mut transactions = db.pending_transactions.write(); | 699 | let mut transactions = db.pending_transactions.write(); |
678 | 700 | ||