diff options
author | Yigit Sever | 2021-04-19 03:57:08 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-19 03:57:08 +0300 |
commit | 9ad288e25973488a3cfc83533456d5d741e08e3b (patch) | |
tree | 72495798bf4dca34a75c4cd7687446c3299dd0e5 | |
parent | 4c855e5bb82cdc470d8dd915121a2e033324d5b1 (diff) | |
download | gradecoin-9ad288e25973488a3cfc83533456d5d741e08e3b.tar.gz gradecoin-9ad288e25973488a3cfc83533456d5d741e08e3b.tar.bz2 gradecoin-9ad288e25973488a3cfc83533456d5d741e08e3b.zip |
Bugfix
It was possible (and hilarious) to mint a new block with just one
transaction, by repeating it 5 times, lol
-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 | ||