Skip to content

Commit

Permalink
Merge pull request #50 from AploCoin/dev
Browse files Browse the repository at this point in the history
update merkle tree
  • Loading branch information
YeahNotSewerSide authored Sep 6, 2023
2 parents 92b78d7 + c1c6796 commit 2994bed
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 191 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ sled = "0.34.7"
thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
zstd = "0.9"

[dev-dependencies]
rand = "0.8.5"

[profile.test]
opt-level = 3
14 changes: 7 additions & 7 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ impl TransactionBlock {
}

pub fn build_merkle_tree(&mut self) -> Result<(), BlockError> {
let mut new_merkle_tree = MerkleTree::new();
let new_merkle_tree = MerkleTree::build_tree(&self.transactions);

let res = new_merkle_tree.add_objects(&self.transactions);
if !res {
return Err(Report::new(BlockError::TransactionBlock(
TxBlockErrorKind::BuildingMerkleTree,
)));
}
// let res = new_merkle_tree.add_objects(&self.transactions);
// if !res {
// return Err(Report::new(BlockError::TransactionBlock(
// TxBlockErrorKind::BuildingMerkleTree,
// )));
// }
self.merkle_tree = Some(new_merkle_tree);
Ok(())
}
Expand Down
7 changes: 3 additions & 4 deletions src/blockchaintree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1973,8 +1973,7 @@ impl BlockChainTree {
let transactions_hashes: Vec<_> = transactions.iter().map(|trx| trx.hash()).collect();

// build merkle tree & get root
let mut merkle_tree = MerkleTree::new();
merkle_tree.add_objects(&transactions_hashes);
let merkle_tree = MerkleTree::build_tree(&transactions_hashes);
let merkle_tree_root = *merkle_tree.get_root();

let basic_info = BasicInfo::new(
Expand Down Expand Up @@ -2527,8 +2526,8 @@ impl BlockChainTree {
transactions_hashes.push(founder_transaction.hash());
transactions_hashes.extend(transactions.iter().map(|t| t.hash()));

let mut merkle_tree = MerkleTree::new();
merkle_tree.add_objects(&transactions_hashes);
let merkle_tree = MerkleTree::build_tree(&transactions_hashes);
//merkle_tree.add_objects(&transactions_hashes);
let calculated_merkle_tree_root = merkle_tree.get_root();

if !new_block.get_merkle_root().eq(calculated_merkle_tree_root) {
Expand Down
Loading

0 comments on commit 2994bed

Please sign in to comment.