Skip to main content

neo_devpack_solidity/runtime/state/state_impl/
snapshots.rs

1use super::*;
2
3impl StateManager {
4    /// Create state snapshot
5    pub fn create_snapshot(&mut self, description: String) -> u64 {
6        let snapshot_id = self.snapshots.len() as u64;
7        let snapshot = StateSnapshot {
8            id: snapshot_id,
9            timestamp: self.current_timestamp(),
10            accounts: self.accounts.clone(),
11            description,
12        };
13
14        self.snapshots.push(snapshot);
15        snapshot_id
16    }
17
18    /// Get state snapshot
19    pub fn get_snapshot(&self) -> StateSnapshot {
20        StateSnapshot {
21            id: u64::MAX,
22            timestamp: self.current_timestamp(),
23            accounts: self.accounts.clone(),
24            description: "Current state".to_string(),
25        }
26    }
27
28    /// Restore from snapshot
29    pub fn restore_snapshot(&mut self, snapshot: StateSnapshot) -> Result<(), RuntimeError> {
30        // Record changes for each account
31        for (address, account) in &snapshot.accounts {
32            if let Some(current_account) = self.accounts.get(address) {
33                if current_account.balance != account.balance {
34                    self.record_change(StateChange {
35                        change_type: StateChangeType::BalanceChange,
36                        account: address.clone(),
37                        key: None,
38                        old_value: Some(current_account.balance.to_le_bytes().to_vec()),
39                        new_value: account.balance.to_le_bytes().to_vec(),
40                    });
41                }
42            }
43        }
44
45        self.accounts = snapshot.accounts;
46        Ok(())
47    }
48}