From 687a02f16ff2d11442446f4158b7a73a33298c31 Mon Sep 17 00:00:00 2001 From: YeahNotSewerSide Date: Wed, 24 Jul 2024 16:45:28 +0300 Subject: [PATCH] consensus change --- src/block.rs | 3 +++ src/blockchaintree.rs | 4 ++-- src/tools.rs | 11 ++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/block.rs b/src/block.rs index e2806ce..2fba3f2 100644 --- a/src/block.rs +++ b/src/block.rs @@ -319,6 +319,7 @@ impl Block for DerivativeBlock { if !check_pow( &self.get_merkle_root(), &prev_block.get_info().difficulty, + self.transactions().unwrap_or(&[]), &self.default_info.pow, ) { return Ok(false); @@ -413,6 +414,7 @@ impl Block for TransactionBlock { if !check_pow( &self.merkle_tree_root, &prev_block.get_info().difficulty, + self.transactions().unwrap_or(&[]), &self.default_info.pow, ) { return Ok(false); @@ -548,6 +550,7 @@ impl Block for SummarizeBlock { if !check_pow( &self.merkle_tree_root, &prev_block.get_info().difficulty, + self.transactions().unwrap_or(&[]), &self.default_info.pow, ) { return Ok(false); diff --git a/src/blockchaintree.rs b/src/blockchaintree.rs index 09f1a82..7d578d8 100644 --- a/src/blockchaintree.rs +++ b/src/blockchaintree.rs @@ -394,7 +394,7 @@ impl BlockChainTree { ) }; - if !tools::check_pow(&prev_hash, &difficulty, pow) { + if !tools::check_pow(&prev_hash, &difficulty, &[], pow) { return Err(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::WrongPow).into()); }; tools::recalculate_difficulty(prev_timestamp, timestamp, &mut difficulty); @@ -426,7 +426,7 @@ impl BlockChainTree { .attach_printable("failed to hash block")?; let mut difficulty = last_block.get_info().difficulty; - if !tools::check_pow(&prev_hash, &difficulty, pow) { + if !tools::check_pow(&prev_hash, &difficulty, transactions, pow) { return Err(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::WrongPow).into()); }; tools::recalculate_difficulty(last_block.get_info().timestamp, timestamp, &mut difficulty); diff --git a/src/tools.rs b/src/tools.rs index d91f98f..773e679 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,5 +1,7 @@ use crate::errors::*; +use crate::merkletree::MerkleTree; use crate::static_values::{FEE_STEP, TIME_PER_BLOCK}; +use crate::transaction::{Transaction, Transactionable}; use crate::types::Hash; use error_stack::{Report, Result, ResultExt}; use num_bigint::BigUint; @@ -195,9 +197,16 @@ pub fn count_leading_zeros(data: &[u8]) -> u32 { to_return } -pub fn check_pow(hash: &[u8; 32], difficulty: &[u8; 32], pow: &[u8]) -> bool { +pub fn check_pow( + hash: &[u8; 32], + difficulty: &[u8; 32], + transactions: &[Hash], + pow: &[u8], +) -> bool { + let merkle_tree = MerkleTree::build_tree(transactions); let mut hasher = Sha256::new(); hasher.update(hash); + hasher.update(merkle_tree.get_root()); hasher.update(pow); let result: [u8; 32] = hasher.finalize().into();