foundry_evm_core/
state_snapshot.rs1use alloy_primitives::{U256, map::HashMap};
4
5#[derive(Clone, Debug)]
7pub struct StateSnapshots<T> {
8 id: U256,
9 state_snapshots: HashMap<U256, T>,
10}
11
12impl<T> StateSnapshots<T> {
13 fn next_id(&mut self) -> U256 {
14 let id = self.id;
15 self.id = id.saturating_add(U256::from(1));
16 id
17 }
18
19 pub fn get(&self, id: U256) -> Option<&T> {
21 self.state_snapshots.get(&id)
22 }
23
24 pub fn is_empty(&self) -> bool {
26 self.state_snapshots.is_empty()
27 }
28
29 pub fn remove(&mut self, id: U256) -> Option<T> {
34 let snapshot_state = self.state_snapshots.remove(&id);
35
36 let mut to_revert = id + U256::from(1);
38 while to_revert < self.id {
39 self.state_snapshots.remove(&to_revert);
40 to_revert += U256::from(1);
41 }
42
43 snapshot_state
44 }
45
46 pub fn clear(&mut self) {
48 self.state_snapshots.clear();
49 }
50
51 pub fn remove_at(&mut self, id: U256) -> Option<T> {
55 self.state_snapshots.remove(&id)
56 }
57
58 pub fn insert(&mut self, state_snapshot: T) -> U256 {
60 let id = self.next_id();
61 self.state_snapshots.insert(id, state_snapshot);
62 id
63 }
64
65 pub fn insert_at(&mut self, state_snapshot: T, id: U256) {
69 self.state_snapshots.insert(id, state_snapshot);
70 }
71}
72
73impl<T> Default for StateSnapshots<T> {
74 fn default() -> Self {
75 Self { id: U256::ZERO, state_snapshots: HashMap::default() }
76 }
77}