diff --git a/Cargo.toml b/Cargo.toml index 08225ed..6454969 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "alfis" -version = "0.4.22" +version = "0.4.23" authors = ["Revertron "] edition = "2018" build = "build.rs" diff --git a/src/blockchain/block.rs b/src/blockchain/block.rs index 3316f0b..4ad0f88 100644 --- a/src/blockchain/block.rs +++ b/src/blockchain/block.rs @@ -5,7 +5,7 @@ use std::fmt::Debug; use serde::{Serialize, Deserialize}; use crate::bytes::Bytes; use crate::Transaction; -use crate::blockchain::hash_utils::hash_difficulty; +use crate::blockchain::hash_utils::{hash_difficulty, key_hash_difficulty}; #[derive(Clone, Serialize, Deserialize, PartialEq, Debug)] pub struct Block { @@ -68,15 +68,30 @@ impl Block { Vec::from(serde_json::to_string(&self).unwrap().as_bytes()) } + /// Checks if this block is superior than the other pub fn is_better_than(&self, other: &Block) -> bool { - if self.transaction.is_none() && other.transaction.is_some() { - return false; + if self.transaction.is_some() && other.transaction.is_none() { + return true; } - if hash_difficulty(self.hash.as_slice()) < hash_difficulty(other.hash.as_slice()) { - return false; + let my_diff = hash_difficulty(self.hash.as_slice()); + let it_diff = hash_difficulty(other.hash.as_slice()); + if my_diff > it_diff { + return true; + } + let my_diff = key_hash_difficulty(self.hash.as_slice()); + let it_diff = key_hash_difficulty(other.hash.as_slice()); + if my_diff > it_diff { + return true; + } + let my_diff = hash_difficulty(self.signature.as_slice()); + let it_diff = hash_difficulty(other.signature.as_slice()); + if my_diff > it_diff { + return true; + } + if self.nonce < other.nonce { + return true; } - // TODO add more checks - true + false } } \ No newline at end of file diff --git a/src/blockchain/hash_utils.rs b/src/blockchain/hash_utils.rs index e1a7c1f..5c42d77 100644 --- a/src/blockchain/hash_utils.rs +++ b/src/blockchain/hash_utils.rs @@ -70,7 +70,7 @@ pub fn hash_difficulty(hash: &[u8]) -> u32 { /// Returns hash difficulty for keys (only from the start) #[inline] -pub fn hash_difficulty_key(hash: &[u8]) -> u32 { +pub fn key_hash_difficulty(hash: &[u8]) -> u32 { let bytes: [u8; 8] = hash[..8].try_into().unwrap(); let int = u64::from_be_bytes(bytes); int.leading_zeros() diff --git a/src/keys.rs b/src/keys.rs index 87bdede..5aea0e7 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -166,7 +166,7 @@ impl PartialEq for Keystore { /// TODO Optimize by caching Blakeout somewhere pub fn check_public_key_strength(key: &Bytes, strength: u32) -> bool { let bytes = blakeout_data(&key); - hash_difficulty_key(&bytes) >= strength + key_hash_difficulty(&bytes) >= strength } pub fn create_key(context: Arc>) { @@ -230,7 +230,7 @@ fn generate_key(difficulty: u32, mining: Arc) -> Option { let keystore = Keystore::from_random_bytes(&buf); digest.reset(); digest.update(keystore.get_public().as_slice()); - if hash_difficulty_key(digest.result()) >= difficulty { + if key_hash_difficulty(digest.result()) >= difficulty { info!("Generated keypair with public key: {:?} and hash {:?}", &keystore.get_public(), &keystore.get_hash()); return Some(keystore); }