Skip to content

Commit 1c1c021

Browse files
committed
fix: merkle tree algorithm
1 parent 8f2cdcc commit 1c1c021

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

primitives/src/merkle_tree.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,19 @@ impl Algorithm<MerkleItem> for KeccakAlgorithm {
5555
fn node(&mut self, left: MerkleItem, right: MerkleItem, _height: usize) -> MerkleItem {
5656
let left_vec: Vec<u8> = left.to_vec();
5757
let right_vec: Vec<u8> = right.to_vec();
58+
// This is a check for odd number of leaves items
59+
// left == right since the right is a duplicate of left
60+
// return the item unencoded as the JS impl
61+
if right_vec == left_vec {
62+
left
63+
} else {
64+
let mut node_vec = vec![left_vec, right_vec];
65+
node_vec.sort();
5866

59-
let mut node_vec = vec![left_vec, right_vec];
60-
node_vec.sort();
61-
62-
let flatten_node_vec: Vec<u8> = node_vec.into_iter().flatten().collect();
63-
64-
self.write(&flatten_node_vec);
65-
self.hash()
67+
let flatten_node_vec: Vec<u8> = node_vec.into_iter().flatten().collect();
68+
self.write(&flatten_node_vec);
69+
self.hash()
70+
}
6671
}
6772
}
6873

validator_worker/src/follower.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ async fn on_new_state<'a, A: Adapter + 'static>(
6060
) -> Result<NewStateResult, Box<dyn Error>> {
6161
let proposed_balances = new_state.balances.clone();
6262
let proposed_state_root = new_state.state_root.clone();
63-
6463
if proposed_state_root != hex::encode(get_state_root_hash(&iface, &proposed_balances)?) {
6564
return Ok(on_error(&iface, &new_state, InvalidNewState::RootHash).await);
6665
}

0 commit comments

Comments
 (0)