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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::bytes::Bytes;
use ethereum_types::{H256, U256};
use std::{cmp::*, collections::BTreeMap, fmt};
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Diff<T> {
Same,
Born(T),
Changed(T, T),
Died(T),
}
impl<T> Diff<T> {
pub fn new(pre: T, post: T) -> Self
where
T: Eq,
{
if pre == post {
Diff::Same
} else {
Diff::Changed(pre, post)
}
}
pub fn pre(&self) -> Option<&T> {
match *self {
Diff::Died(ref x) | Diff::Changed(ref x, _) => Some(x),
_ => None,
}
}
pub fn post(&self) -> Option<&T> {
match *self {
Diff::Born(ref x) | Diff::Changed(_, ref x) => Some(x),
_ => None,
}
}
pub fn is_same(&self) -> bool {
match *self {
Diff::Same => true,
_ => false,
}
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct AccountDiff {
pub balance: Diff<U256>,
pub nonce: Diff<U256>,
pub code: Diff<Bytes>,
pub storage: BTreeMap<H256, Diff<H256>>,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Existance {
Born,
Alive,
Died,
}
impl fmt::Display for Existance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Existance::Born => write!(f, "+++")?,
Existance::Alive => write!(f, "***")?,
Existance::Died => write!(f, "XXX")?,
}
Ok(())
}
}
impl AccountDiff {
pub fn existance(&self) -> Existance {
match self.balance {
Diff::Born(_) => Existance::Born,
Diff::Died(_) => Existance::Died,
_ => Existance::Alive,
}
}
}
fn interpreted_hash(u: &H256) -> String {
if u <= &H256::from_low_u64_be(0xffffffff) {
format!(
"{} = 0x{:x}",
U256::from(u.as_bytes()).low_u32(),
U256::from(u.as_bytes()).low_u32()
)
} else if u <= &H256::from_low_u64_be(u64::max_value()) {
format!(
"{} = 0x{:x}",
U256::from(u.as_bytes()).low_u64(),
U256::from(u.as_bytes()).low_u64()
)
} else {
format!("#{}", u)
}
}
impl fmt::Display for AccountDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use crate::bytes::ToPretty;
match self.nonce {
Diff::Born(ref x) => write!(f, " non {}", x)?,
Diff::Changed(ref pre, ref post) => write!(
f,
"#{} ({} {} {})",
post,
pre,
if pre > post { "-" } else { "+" },
*max(pre, post) - *min(pre, post)
)?,
_ => {}
}
match self.balance {
Diff::Born(ref x) => write!(f, " bal {}", x)?,
Diff::Changed(ref pre, ref post) => write!(
f,
"${} ({} {} {})",
post,
pre,
if pre > post { "-" } else { "+" },
*max(pre, post) - *min(pre, post)
)?,
_ => {}
}
if let Diff::Born(ref x) = self.code {
write!(f, " code {}", x.pretty())?;
}
write!(f, "\n")?;
for (k, dv) in &self.storage {
match *dv {
Diff::Born(ref v) => write!(
f,
" + {} => {}\n",
interpreted_hash(k),
interpreted_hash(v)
)?,
Diff::Changed(ref pre, ref post) => write!(
f,
" * {} => {} (was {})\n",
interpreted_hash(k),
interpreted_hash(post),
interpreted_hash(pre)
)?,
Diff::Died(_) => write!(f, " X {}\n", interpreted_hash(k))?,
_ => {}
}
}
Ok(())
}
}