Added more block supremacy checks.

This commit is contained in:
Revertron
2021-04-22 16:31:42 +02:00
parent b248d839e1
commit dee53887cb
4 changed files with 26 additions and 11 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "alfis"
version = "0.4.22"
version = "0.4.23"
authors = ["Revertron <alfis@revertron.com>"]
edition = "2018"
build = "build.rs"
+22 -7
View File
@@ -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
}
}
+1 -1
View File
@@ -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()
+2 -2
View File
@@ -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<Mutex<Context>>) {
@@ -230,7 +230,7 @@ fn generate_key(difficulty: u32, mining: Arc<AtomicBool>) -> Option<Keystore> {
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);
}