Changed serialization of block to binary format.

This commit is contained in:
Revertron
2021-05-03 08:42:21 +02:00
parent c3576d62d8
commit fab62bdfc8
4 changed files with 17 additions and 10 deletions
+1
View File
@@ -26,6 +26,7 @@ num_cpus = "1.13.0"
byteorder = "1.4.3" byteorder = "1.4.3"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.64" serde_json = "1.0.64"
bincode = "1.3"
num-bigint = "0.4" num-bigint = "0.4"
num-traits = "0.2.14" num-traits = "0.2.14"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
+5 -2
View File
@@ -6,6 +6,7 @@ use serde::{Serialize, Deserialize};
use crate::bytes::Bytes; use crate::bytes::Bytes;
use crate::Transaction; use crate::Transaction;
use crate::blockchain::hash_utils::{hash_difficulty, key_hash_difficulty}; use crate::blockchain::hash_utils::{hash_difficulty, key_hash_difficulty};
use crate::blockchain::transaction::TransactionType;
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)] #[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct Block { pub struct Block {
@@ -61,11 +62,13 @@ impl Block {
} }
pub fn is_genesis(&self) -> bool { 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<u8> { pub fn as_bytes(&self) -> Vec<u8> {
Vec::from(serde_json::to_string(&self).unwrap().as_bytes()) bincode::serialize(&self).unwrap()
} }
/// Checks if this block is superior than the other /// Checks if this block is superior than the other
+1 -2
View File
@@ -9,8 +9,7 @@ pub fn check_block_hash(block: &Block) -> bool {
let mut copy: Block = block.clone(); let mut copy: Block = block.clone();
copy.hash = Bytes::default(); copy.hash = Bytes::default();
copy.signature = Bytes::default(); copy.signature = Bytes::default();
let data = serde_json::to_string(&copy).unwrap(); blakeout_data(&copy.as_bytes()) == block.hash
blakeout_data(data.as_bytes()) == block.hash
} }
/// Hashes data by given hasher /// Hashes data by given hasher
+10 -6
View File
@@ -14,14 +14,14 @@ extern crate serde_json;
#[derive(Clone, Deserialize, PartialEq)] #[derive(Clone, Deserialize, PartialEq)]
pub struct Transaction { pub struct Transaction {
pub class: String,
#[serde(default, skip_serializing_if = "Bytes::is_zero")] #[serde(default, skip_serializing_if = "Bytes::is_zero")]
pub identity: Bytes, pub identity: Bytes,
#[serde(default, skip_serializing_if = "Bytes::is_zero")] #[serde(default, skip_serializing_if = "Bytes::is_zero")]
pub confirmation: Bytes, pub confirmation: Bytes,
pub class: String,
pub data: String,
#[serde(default, skip_serializing_if = "Bytes::is_zero")] #[serde(default, skip_serializing_if = "Bytes::is_zero")]
pub owner: Bytes, pub owner: Bytes,
pub data: String,
} }
impl Transaction { impl Transaction {
@@ -81,6 +81,9 @@ impl Transaction {
if transaction.class == CLASS_DOMAIN { if transaction.class == CLASS_DOMAIN {
return TransactionType::Domain; return TransactionType::Domain;
} }
if transaction.class == CLASS_ORIGIN {
return TransactionType::Origin;
}
TransactionType::Unknown TransactionType::Unknown
} }
} }
@@ -90,11 +93,11 @@ impl Transaction {
impl fmt::Debug for Transaction { impl fmt::Debug for Transaction {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Transaction") fmt.debug_struct("Transaction")
.field("class", &self.class)
.field("identity", &self.identity) .field("identity", &self.identity)
.field("confirmation", &self.confirmation) .field("confirmation", &self.confirmation)
.field("class", &self.class) .field("owner", &&self.owner)
.field("data", &self.data) .field("data", &self.data)
.field("pub_key", &&self.owner)
.finish() .finish()
} }
} }
@@ -102,11 +105,11 @@ impl fmt::Debug for Transaction {
impl Serialize for Transaction { impl Serialize for Transaction {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where S: Serializer { fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where S: Serializer {
let mut structure = serializer.serialize_struct("Transaction", 5).unwrap(); let mut structure = serializer.serialize_struct("Transaction", 5).unwrap();
structure.serialize_field("class", &self.class)?;
structure.serialize_field("identity", &self.identity)?; structure.serialize_field("identity", &self.identity)?;
structure.serialize_field("confirmation", &self.confirmation)?; 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("data", &self.data)?;
structure.serialize_field("pub_key", &self.owner)?;
structure.end() structure.end()
} }
} }
@@ -115,6 +118,7 @@ pub enum TransactionType {
Unknown, Unknown,
Signing, Signing,
Domain, Domain,
Origin,
} }
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]