From 54e81adac6a24291e2ee16cf5597f127122f62eb Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Wed, 14 Apr 2021 00:34:39 +0300 Subject: Add empty block test --- src/handlers.rs | 2 ++ src/schema.rs | 6 ++++-- src/validators.rs | 6 +++--- tester.sh | 11 +++++++++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/handlers.rs b/src/handlers.rs index 1189b35..362a341 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -122,6 +122,8 @@ pub async fn authorized_propose_block( let users_store = db.users.read(); + println!("{:?}", &new_block); + let internal_user = match users_store.get(&new_block.transaction_list[0]) { Some(existing_user) => existing_user, None => { diff --git a/src/schema.rs b/src/schema.rs index b07744a..6719722 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -92,6 +92,7 @@ pub struct Transaction { /// #[derive(Serialize, Deserialize, Debug)] pub struct Block { + #[serde(skip_serializing_if = "Vec::is_empty")] pub transaction_list: Vec, pub nonce: u32, pub timestamp: NaiveDateTime, @@ -101,6 +102,7 @@ pub struct Block { /// For prototyping and letting serde handle everything json #[derive(Serialize, Deserialize, Debug)] pub struct NakedBlock { + #[serde(skip_serializing_if = "Vec::is_empty", default)] pub transaction_list: Vec, pub nonce: u32, pub timestamp: NaiveDateTime, @@ -110,10 +112,10 @@ impl Block { /// Genesis block pub fn new() -> Block { Block { - transaction_list: vec![], + transaction_list: vec!["gradecoin_bank".to_owned()], nonce: 0, timestamp: NaiveDate::from_ymd(2021, 04, 11).and_hms(20, 45, 00), - hash: String::from(""), + hash: String::from("not_actually_mined"), } } } diff --git a/src/validators.rs b/src/validators.rs index dbebee8..31344d8 100644 --- a/src/validators.rs +++ b/src/validators.rs @@ -1,14 +1,14 @@ -// Custom validators incoming data +/// Custom validators incoming data use log::error; use serde::de::{Deserializer, Error as DeserializerError, Unexpected}; use serde::ser::{Error as SerializerError, Serializer}; use serde::Deserialize; -pub mod validate_game_rating { +pub mod validate_block { use super::*; - const ERROR_MESSAGE: &str = "rating must be a number between 0 and 100"; + const ERROR_MESSAGE: &str = "block cannot have an empty transaction list"; pub fn deserialize<'de, D>(deserializer: D) -> Result where diff --git a/tester.sh b/tester.sh index 0323dcb..19c1549 100755 --- a/tester.sh +++ b/tester.sh @@ -119,3 +119,14 @@ curl --request POST \ "hash": "000000a996ab57b3aff1ad1d009767278f819895b28ef860a8ec3b6560d6ed0e" }' \ http://localhost:8080/block + +curl --request POST \ + --header 'Content-Type: application/json' \ + --header 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0aGEiOiIyYjY0OGZmYWI1ZDlhZjFkNWQ1ZmMwNTJmYzllNTFiODgyZmM0ZmIwYzk5ODYwOGM5OTIzMmY5MjgyMDAwMDAwIiwiaWF0IjoxNjE4MzYwNjQxLCJleHAiOjE3MTgyNjA2NDF9.P5L_uZ9lOhRZCbsG9GDXn_rmZat3dP9Y2lbk8GY4Kg4pOxJIklBUxot-TtJzB0vEJFcjnxVnT2lFLCgfdQLHTJvURiW0KRHi94e1Kj8aDXxJ0qjlq4-c1JCZnAIbDpvkFtHNKz04yfyeSR2htJ6kOjlqVpeUhLVokHhi1x-ZUZZSpeGnlIXgi-AcmkEoyOypZGSZgQ1hjID2f18zgfbshgPK4Dr0hiN36wYMB0y0YiikRbvDuGgDzRLN2nitih46-CXTGZMqIRz3eAfM2wuUSH1yhdKi5_vavz8L3EPVCGMO-CKlPUDkYA-duQZf_q3tG2fkdaFlTAcCik_kVMprdw' \ + --data '{ + "transaction_list": [], + "nonce": 2151653522, + "timestamp": "2021-04-13T23:38:00", + "hash": "000000a996ab57b3aff1ad1d009767278f819895b28ef860a8ec3b6560d6ed0e" +}' \ + http://localhost:8080/block -- cgit v1.2.3-70-g09d2