Skip to content

Commit

Permalink
consensus change
Browse files Browse the repository at this point in the history
  • Loading branch information
YeahNotSewerSide committed Jul 24, 2024
1 parent d4c0977 commit 687a02f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/blockchaintree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion src/tools.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 687a02f

Please sign in to comment.