Skip to content

Commit 45c8234

Browse files
[commonware-storage/mmr] allow mem-mmr to access pinned nodes when updating leafs (#869)
1 parent 79eadd4 commit 45c8234

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

storage/src/mmr/mem.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,13 @@ impl<H: CHasher> Mmr<H> {
196196
Ok(self.size())
197197
}
198198

199-
/// Change the digest of an existing leaf. Panics if `pos` does not correspond to a leaf.
199+
/// Change the digest of any retained leaf. Panics if `pos` does not correspond to a leaf.
200200
///
201201
/// # Warning
202202
///
203203
/// This method will change the root hash and invalidate any previous inclusion proofs! This is
204204
/// useful if you want to use the MMR implementation as an updatable binary Merkle tree, and
205-
/// otherwise should be avoided. Returns ElementPruned if some element required to update the
206-
/// tree has been pruned.
205+
/// otherwise should be avoided.
207206
pub fn update_leaf(&mut self, hasher: &mut H, pos: u64, element: &[u8]) -> Result<(), Error> {
208207
if pos < self.pruned_to_pos {
209208
return Err(ElementPruned(pos));
@@ -225,9 +224,6 @@ impl<H: CHasher> Mmr<H> {
225224
// Traverse up to the peak, recomputing each parent node hash along the way.
226225
let path: Vec<_> = PathIterator::new(pos, peak_pos, height).collect();
227226
for (parent_pos, sibling_pos) in path.into_iter().rev() {
228-
if sibling_pos < self.pruned_to_pos {
229-
return Err(ElementPruned(sibling_pos));
230-
}
231227
if parent_pos == pos {
232228
panic!("pos was not for a leaf");
233229
}
@@ -858,15 +854,15 @@ pub(crate) mod tests {
858854

859855
// Confirm the function gracefully handles failures when the MMR is pruned.
860856
mmr.prune_to_pos(leaves[150]);
861-
assert!(matches!(
862-
mmr.update_leaf(&mut hasher, leaves[150], &element),
863-
Err(ElementPruned(_))
864-
));
865857
assert!(matches!(
866858
mmr.update_leaf(&mut hasher, leaves[149], &element),
867859
Err(ElementPruned(_))
868860
));
869-
assert!(mmr.update_leaf(&mut hasher, leaves[190], &element).is_ok());
861+
// Confirm the tree has all the hashes necessary to update any element after pruning.
862+
for &leaf_pos in &leaves[150..=190] {
863+
mmr.prune_to_pos(leaf_pos);
864+
assert!(mmr.update_leaf(&mut hasher, leaf_pos, &element).is_ok());
865+
}
870866
}
871867

872868
#[test]

0 commit comments

Comments
 (0)