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
75
76
77
78
79
80
81
82
use std::borrow::Cow;
use super::{StorageSlot, TransitionAccount};
use primitives::{hash_map::Entry, Address, AddressMap, HashMap};
use state::EvmStorage;
/// State of accounts in transition between transaction executions.
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct TransitionState {
/// Block state account with account state
pub transitions: AddressMap<TransitionAccount>,
}
impl TransitionState {
/// Create new transition state containing one [`TransitionAccount`].
pub fn single(address: Address, transition: TransitionAccount) -> Self {
let mut transitions = HashMap::default();
transitions.insert(address, transition);
TransitionState { transitions }
}
/// Take the contents of this [`TransitionState`] and replace it with an
/// empty one.
///
/// See [core::mem::take].
pub fn take(&mut self) -> TransitionState {
core::mem::take(self)
}
/// Clear the transition state.
pub fn clear(&mut self) {
self.transitions.clear();
}
/// Add transitions to the transition state.
///
/// This will insert new [`TransitionAccount`]s, or update existing ones via
/// [`update`][TransitionAccount::update].
pub fn add_transitions<'a>(
&mut self,
transitions: impl IntoIterator<Item = (Address, TransitionAccount<Option<Cow<'a, EvmStorage>>>)>,
) {
let transitions = transitions.into_iter();
if let Some(upper) = transitions.size_hint().1 {
self.transitions.reserve(upper);
}
for (address, account) in transitions {
self.add_transition(address, account);
}
}
/// Add one transition to the transition state.
pub fn add_transition(
&mut self,
address: Address,
account: TransitionAccount<Option<Cow<'_, EvmStorage>>>,
) {
match self.transitions.entry(address) {
Entry::Occupied(entry) => entry.into_mut().update(account),
Entry::Vacant(entry) => {
_ = entry.insert(account.map_storage(|storage| {
storage
.map(|storage| {
storage
.iter()
.filter_map(|(key, slot)| {
slot.is_changed().then_some((
*key,
StorageSlot::new_changed(
slot.original_value,
slot.present_value,
),
))
})
.collect()
})
.unwrap_or_default()
}))
}
}
}
}