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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use aptos_crypto::HashValue;
use aptos_types::{
state_store::{state_key::StateKey, state_value::StateValue},
transaction::Version,
};
use scratchpad::SparseMerkleTree;
use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct StateDelta {
pub base: SparseMerkleTree<StateValue>,
pub base_version: Option<Version>,
pub current: SparseMerkleTree<StateValue>,
pub current_version: Option<Version>,
pub updates_since_base: HashMap<StateKey, StateValue>,
}
impl StateDelta {
pub fn new(
base: SparseMerkleTree<StateValue>,
base_version: Option<Version>,
current: SparseMerkleTree<StateValue>,
current_version: Option<Version>,
updates_since_base: HashMap<StateKey, StateValue>,
) -> Self {
assert!(base_version.map_or(0, |v| v + 1) <= current_version.map_or(0, |v| v + 1));
Self {
base,
base_version,
current,
current_version,
updates_since_base,
}
}
pub fn new_empty() -> Self {
let smt = SparseMerkleTree::new_empty();
Self::new(smt.clone(), None, smt, None, HashMap::new())
}
pub fn new_at_checkpoint(root_hash: HashValue, checkpoint_version: Option<Version>) -> Self {
let smt = SparseMerkleTree::new(root_hash);
Self::new(
smt.clone(),
checkpoint_version,
smt,
checkpoint_version,
HashMap::new(),
)
}
pub fn has_same_current_state(&self, other: &StateDelta) -> bool {
self.current_version == other.current_version
&& self.current.has_same_root_hash(&other.current)
}
pub fn base_root_hash(&self) -> HashValue {
self.base.root_hash()
}
pub fn root_hash(&self) -> HashValue {
self.current.root_hash()
}
}