diff --git a/src/dynamic_merkle_tree.rs b/src/dynamic_merkle_tree.rs index 63b54c0..0b12760 100644 --- a/src/dynamic_merkle_tree.rs +++ b/src/dynamic_merkle_tree.rs @@ -174,14 +174,15 @@ impl DynamicMerkleTree { } pub fn push(&mut self, leaf: H::Hash) { - match self.storage.get_mut(self.num_leaves + 1) { + self.num_leaves += 1; + let index = index_from_leaf(self.num_leaves); + match self.storage.get_mut(index) { Some(val) => *val = leaf, None => { self.reallocate(); self.storage[self.num_leaves + 1] = leaf; } } - self.num_leaves += 1; } pub fn set_leaf(&mut self, leaf: usize, value: H::Hash) { @@ -199,8 +200,7 @@ impl DynamicMerkleTree { let left_hash = self.storage.get(left)?; let right_hash = self.storage[right]; let parent_index = parent(index); - let parent_hash = H::hash_node(left_hash, &right_hash); - self.storage[parent_index] = parent_hash; + self.storage[parent_index] = H::hash_node(left_hash, &right_hash); index = parent_index; } }