1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::collections::HashMap;

use crate::state::{global_stat::GlobalStat, overlay_account::AccountEntry};
use cfx_types::AddressWithSpace;

use super::State;

pub struct SavedState {
    committed_cache: HashMap<AddressWithSpace, AccountEntry>,
    global_stat: GlobalStat,
}

impl State {
    pub fn save(&mut self) -> SavedState {
        self.commit_cache(false);
        let committed_cache = self
            .committed_cache
            .iter()
            .map(|(k, v)| (*k, v.clone_account()))
            .collect();
        SavedState {
            committed_cache,
            global_stat: self.global_stat.clone(),
        }
    }

    pub fn restore(&mut self, saved: SavedState) {
        assert!(self.no_checkpoint());
        self.cache = Default::default();
        self.committed_cache = saved.committed_cache;
        self.global_stat = saved.global_stat;
    }
}