Skip to content

Commit c2c60b1

Browse files
Rename clear history map fun and add tests
1 parent c9ef994 commit c2c60b1

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

storage_models/src/common_structs/generic_transient_storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<
4242
// Just discard old history
4343
// Note: it will reset snapshots counter, old snapshots handlers can't be used anymore
4444
// Note: We will reset it redundantly for first tx
45-
self.cache.reset_for_new_tx();
45+
self.cache.clear();
4646
self.current_tx_number += 1;
4747
}
4848

zk_ee/src/common_structs/history_map/mod.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ where
7777
}
7878

7979
/// Clears the map while reusing history record allocations.
80-
pub fn reset_for_new_tx(&mut self) {
80+
pub fn clear(&mut self) {
8181
for (_, element) in self.btree.iter_mut() {
8282
self.records_memory_pool
8383
.reuse_memory(element.head, element.initial);
@@ -623,4 +623,74 @@ mod tests {
623623
})
624624
.unwrap();
625625
}
626+
627+
#[test]
628+
fn clear_removes_elements_and_pending_changes() {
629+
let mut map = HistoryMap::<usize, usize, Global>::new(Global);
630+
631+
map.snapshot();
632+
633+
// Create one modified entry.
634+
let mut v = map.get_or_insert::<()>(&1, || Ok((1, ()))).unwrap();
635+
v.update::<_, ()>(|x| {
636+
*x = 2;
637+
Ok(())
638+
})
639+
.unwrap();
640+
641+
assert_eq!(map.iter().len(), 1);
642+
assert_eq!(map.iter_altered_since_commit().count(), 1);
643+
644+
// Drop all state.
645+
map.clear();
646+
647+
assert!(map.get(&1).is_none());
648+
assert_eq!(map.iter().len(), 0);
649+
assert_eq!(map.iter_altered_since_commit().count(), 0);
650+
map.apply_to_all_updated_elements::<_, ()>(|_, _, _| {
651+
panic!("Map is expected to be empty after clear")
652+
})
653+
.unwrap();
654+
}
655+
656+
#[test]
657+
fn clear_resets_snapshots() {
658+
let mut map = HistoryMap::<usize, usize, Global>::new(Global);
659+
660+
// Keep a pre-clear snapshot handle.
661+
let pre_clear_snapshot = map.snapshot();
662+
663+
let mut v = map.get_or_insert::<()>(&1, || Ok((1, ()))).unwrap();
664+
v.update::<_, ()>(|x| {
665+
*x = 2;
666+
Ok(())
667+
})
668+
.unwrap();
669+
670+
map.clear();
671+
672+
// Old snapshot ids are no longer valid.
673+
assert!(map.rollback(pre_clear_snapshot).is_err());
674+
675+
// Materialize key after clear with initial value.
676+
map.get_or_insert::<()>(&1, || Ok((3, ()))).unwrap();
677+
678+
// Take snapshot after clear.
679+
let post_clear_snapshot = map.snapshot();
680+
assert_eq!(post_clear_snapshot, super::CacheSnapshotId(1));
681+
682+
let mut v = map.get_or_insert::<()>(&1, || Ok((5, ()))).unwrap();
683+
v.update::<_, ()>(|x| {
684+
*x = 4;
685+
Ok(())
686+
})
687+
.unwrap();
688+
689+
// Rollback restores the post-clear initial value for this key.
690+
map.rollback(post_clear_snapshot).expect("Valid snapshot");
691+
let restored = map.get(&1).expect("Element must remain after rollback");
692+
693+
assert_eq!(*restored.initial(), 3);
694+
assert_eq!(*restored.current(), 3);
695+
}
626696
}

0 commit comments

Comments
 (0)