From fab62bdfc85bc2880b6fcdecdcb6b6436bcd5bb2 Mon Sep 17 00:00:00 2001 From: Revertron Date: Mon, 3 May 2021 08:42:21 +0200 Subject: [PATCH] Changed serialization of block to binary format. --- Cargo.toml | 1 + src/blockchain/block.rs | 7 +++++-- src/blockchain/hash_utils.rs | 3 +-- src/blockchain/transaction.rs | 16 ++++++++++------ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fc91025..9b6651b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ num_cpus = "1.13.0" byteorder = "1.4.3" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.64" +bincode = "1.3" num-bigint = "0.4" num-traits = "0.2.14" chrono = { version = "0.4", features = ["serde"] } diff --git a/src/blockchain/block.rs b/src/blockchain/block.rs index 9fde23a..a88353d 100644 --- a/src/blockchain/block.rs +++ b/src/blockchain/block.rs @@ -6,6 +6,7 @@ use serde::{Serialize, Deserialize}; use crate::bytes::Bytes; use crate::Transaction; use crate::blockchain::hash_utils::{hash_difficulty, key_hash_difficulty}; +use crate::blockchain::transaction::TransactionType; #[derive(Clone, Serialize, Deserialize, PartialEq, Debug)] pub struct Block { @@ -61,11 +62,13 @@ impl Block { } pub fn is_genesis(&self) -> bool { - self.index == 1 && self.transaction.is_none() && self.prev_block_hash == Bytes::default() + self.index == 1 && + matches!(Transaction::get_type(&self.transaction), TransactionType::Origin) && + self.prev_block_hash == Bytes::default() } pub fn as_bytes(&self) -> Vec { - Vec::from(serde_json::to_string(&self).unwrap().as_bytes()) + bincode::serialize(&self).unwrap() } /// Checks if this block is superior than the other diff --git a/src/blockchain/hash_utils.rs b/src/blockchain/hash_utils.rs index 5c42d77..87cf246 100644 --- a/src/blockchain/hash_utils.rs +++ b/src/blockchain/hash_utils.rs @@ -9,8 +9,7 @@ pub fn check_block_hash(block: &Block) -> bool { let mut copy: Block = block.clone(); copy.hash = Bytes::default(); copy.signature = Bytes::default(); - let data = serde_json::to_string(©).unwrap(); - blakeout_data(data.as_bytes()) == block.hash + blakeout_data(©.as_bytes()) == block.hash } /// Hashes data by given hasher diff --git a/src/blockchain/transaction.rs b/src/blockchain/transaction.rs index 270efea..2eab077 100644 --- a/src/blockchain/transaction.rs +++ b/src/blockchain/transaction.rs @@ -14,14 +14,14 @@ extern crate serde_json; #[derive(Clone, Deserialize, PartialEq)] pub struct Transaction { + pub class: String, #[serde(default, skip_serializing_if = "Bytes::is_zero")] pub identity: Bytes, #[serde(default, skip_serializing_if = "Bytes::is_zero")] pub confirmation: Bytes, - pub class: String, - pub data: String, #[serde(default, skip_serializing_if = "Bytes::is_zero")] pub owner: Bytes, + pub data: String, } impl Transaction { @@ -81,6 +81,9 @@ impl Transaction { if transaction.class == CLASS_DOMAIN { return TransactionType::Domain; } + if transaction.class == CLASS_ORIGIN { + return TransactionType::Origin; + } TransactionType::Unknown } } @@ -90,11 +93,11 @@ impl Transaction { impl fmt::Debug for Transaction { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_struct("Transaction") + .field("class", &self.class) .field("identity", &self.identity) .field("confirmation", &self.confirmation) - .field("class", &self.class) + .field("owner", &&self.owner) .field("data", &self.data) - .field("pub_key", &&self.owner) .finish() } } @@ -102,11 +105,11 @@ impl fmt::Debug for Transaction { impl Serialize for Transaction { fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> where S: Serializer { let mut structure = serializer.serialize_struct("Transaction", 5).unwrap(); + structure.serialize_field("class", &self.class)?; structure.serialize_field("identity", &self.identity)?; structure.serialize_field("confirmation", &self.confirmation)?; - structure.serialize_field("class", &self.class)?; + structure.serialize_field("owner", &self.owner)?; structure.serialize_field("data", &self.data)?; - structure.serialize_field("pub_key", &self.owner)?; structure.end() } } @@ -115,6 +118,7 @@ pub enum TransactionType { Unknown, Signing, Domain, + Origin, } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]