Changed serialization of block to binary format.
This commit is contained in:
@@ -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"] }
|
||||
|
||||
@@ -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<u8> {
|
||||
Vec::from(serde_json::to_string(&self).unwrap().as_bytes())
|
||||
bincode::serialize(&self).unwrap()
|
||||
}
|
||||
|
||||
/// Checks if this block is superior than the other
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<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();
|
||||
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)]
|
||||
|
||||
Reference in New Issue
Block a user