reth_trie_common/bundle_state/
account_status.rs1#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20pub enum AccountStatus {
21 #[default]
23 LoadedNotExisting,
24 Loaded,
26 LoadedEmptyEIP161,
28 InMemoryChange,
30 Changed,
32 Destroyed,
34 DestroyedChanged,
36 DestroyedAgain,
38}
39
40impl AccountStatus {
41 pub const fn is_not_modified(&self) -> bool {
43 matches!(self, Self::LoadedNotExisting | Self::Loaded | Self::LoadedEmptyEIP161)
44 }
45
46 pub const fn was_destroyed(&self) -> bool {
49 matches!(self, Self::Destroyed | Self::DestroyedChanged | Self::DestroyedAgain)
50 }
51
52 pub const fn is_storage_known(&self) -> bool {
54 matches!(
55 self,
56 Self::LoadedNotExisting |
57 Self::InMemoryChange |
58 Self::Destroyed |
59 Self::DestroyedChanged |
60 Self::DestroyedAgain
61 )
62 }
63
64 pub const fn is_modified_and_not_destroyed(&self) -> bool {
68 matches!(self, Self::Changed | Self::InMemoryChange)
69 }
70
71 pub const fn on_created(&self) -> Self {
73 match self {
74 Self::DestroyedAgain
76 | Self::Destroyed
77 | Self::DestroyedChanged => Self::DestroyedChanged,
78 Self::LoadedNotExisting
80 | Self::LoadedEmptyEIP161
82 | Self::Loaded
83 | Self::Changed
84 | Self::InMemoryChange => {
85 Self::InMemoryChange
89 }
90 }
91 }
92
93 pub const fn on_touched_empty_post_eip161(&self) -> Self {
95 match self {
96 Self::LoadedNotExisting => Self::LoadedNotExisting,
98 Self::InMemoryChange | Self::Destroyed | Self::LoadedEmptyEIP161 => Self::Destroyed,
100 Self::DestroyedAgain | Self::DestroyedChanged => Self::DestroyedAgain,
102 Self::Changed | Self::Loaded => Self::Destroyed,
104 }
105 }
106
107 pub const fn on_changed(&self, had_no_nonce_and_code: bool) -> Self {
109 match self {
110 Self::LoadedNotExisting => Self::InMemoryChange,
113 Self::LoadedEmptyEIP161 => Self::InMemoryChange,
117 Self::Loaded => {
119 if had_no_nonce_and_code {
120 Self::InMemoryChange
122 } else {
123 Self::Changed
125 }
126 }
127
128 Self::Changed => Self::Changed,
131 Self::InMemoryChange => Self::InMemoryChange,
132 Self::DestroyedChanged => Self::DestroyedChanged,
133
134 Self::Destroyed | Self::DestroyedAgain => Self::DestroyedChanged,
137 }
138 }
139
140 pub const fn on_selfdestructed(&self) -> Self {
142 match self {
143 Self::LoadedNotExisting => Self::LoadedNotExisting,
145 Self::DestroyedChanged | Self::DestroyedAgain | Self::Destroyed => Self::DestroyedAgain,
149
150 _ => Self::Destroyed,
152 }
153 }
154
155 pub fn transition(&mut self, other: Self) {
168 *self = match (self.was_destroyed(), other.was_destroyed()) {
169 (true, false) => Self::DestroyedChanged,
170 (false, false) if *self == Self::InMemoryChange => Self::InMemoryChange,
171 _ => other,
172 };
173 }
174}
175
176#[cfg(test)]
177mod test {
178
179 use super::*;
180
181 #[test]
182 fn test_account_status() {
183 assert!(AccountStatus::Loaded.is_not_modified());
185 assert!(AccountStatus::LoadedEmptyEIP161.is_not_modified());
186 assert!(AccountStatus::LoadedNotExisting.is_not_modified());
187 assert!(!AccountStatus::Changed.is_not_modified());
188 assert!(!AccountStatus::InMemoryChange.is_not_modified());
189 assert!(!AccountStatus::Destroyed.is_not_modified());
190 assert!(!AccountStatus::DestroyedChanged.is_not_modified());
191 assert!(!AccountStatus::DestroyedAgain.is_not_modified());
192
193 assert!(!AccountStatus::LoadedEmptyEIP161.is_storage_known());
195 assert!(AccountStatus::LoadedNotExisting.is_storage_known());
196 assert!(AccountStatus::InMemoryChange.is_storage_known());
197 assert!(AccountStatus::Destroyed.is_storage_known());
198 assert!(AccountStatus::DestroyedChanged.is_storage_known());
199 assert!(AccountStatus::DestroyedAgain.is_storage_known());
200 assert!(!AccountStatus::Loaded.is_storage_known());
201 assert!(!AccountStatus::Changed.is_storage_known());
202
203 assert!(!AccountStatus::LoadedEmptyEIP161.was_destroyed());
205 assert!(!AccountStatus::LoadedNotExisting.was_destroyed());
206 assert!(!AccountStatus::InMemoryChange.was_destroyed());
207 assert!(AccountStatus::Destroyed.was_destroyed());
208 assert!(AccountStatus::DestroyedChanged.was_destroyed());
209 assert!(AccountStatus::DestroyedAgain.was_destroyed());
210 assert!(!AccountStatus::Loaded.was_destroyed());
211 assert!(!AccountStatus::Changed.was_destroyed());
212
213 assert!(AccountStatus::Changed.is_modified_and_not_destroyed());
215 assert!(AccountStatus::InMemoryChange.is_modified_and_not_destroyed());
216 assert!(!AccountStatus::Loaded.is_modified_and_not_destroyed());
217 assert!(!AccountStatus::LoadedEmptyEIP161.is_modified_and_not_destroyed());
218 assert!(!AccountStatus::LoadedNotExisting.is_modified_and_not_destroyed());
219 assert!(!AccountStatus::Destroyed.is_modified_and_not_destroyed());
220 assert!(!AccountStatus::DestroyedChanged.is_modified_and_not_destroyed());
221 assert!(!AccountStatus::DestroyedAgain.is_modified_and_not_destroyed());
222 }
223
224 #[test]
225 fn test_on_created() {
226 assert_eq!(AccountStatus::Destroyed.on_created(), AccountStatus::DestroyedChanged);
227 assert_eq!(AccountStatus::DestroyedAgain.on_created(), AccountStatus::DestroyedChanged);
228 assert_eq!(AccountStatus::DestroyedChanged.on_created(), AccountStatus::DestroyedChanged);
229
230 assert_eq!(AccountStatus::LoadedNotExisting.on_created(), AccountStatus::InMemoryChange);
231 assert_eq!(AccountStatus::Loaded.on_created(), AccountStatus::InMemoryChange);
232 assert_eq!(AccountStatus::LoadedEmptyEIP161.on_created(), AccountStatus::InMemoryChange);
233 assert_eq!(AccountStatus::Changed.on_created(), AccountStatus::InMemoryChange);
234 assert_eq!(AccountStatus::InMemoryChange.on_created(), AccountStatus::InMemoryChange);
235 }
236
237 #[test]
238 fn test_on_touched_empty_post_eip161() {
239 assert_eq!(
240 AccountStatus::LoadedNotExisting.on_touched_empty_post_eip161(),
241 AccountStatus::LoadedNotExisting
242 );
243 assert_eq!(
244 AccountStatus::InMemoryChange.on_touched_empty_post_eip161(),
245 AccountStatus::Destroyed
246 );
247 assert_eq!(
248 AccountStatus::Destroyed.on_touched_empty_post_eip161(),
249 AccountStatus::Destroyed
250 );
251 assert_eq!(
252 AccountStatus::LoadedEmptyEIP161.on_touched_empty_post_eip161(),
253 AccountStatus::Destroyed
254 );
255 assert_eq!(
256 AccountStatus::DestroyedAgain.on_touched_empty_post_eip161(),
257 AccountStatus::DestroyedAgain
258 );
259 assert_eq!(
260 AccountStatus::DestroyedChanged.on_touched_empty_post_eip161(),
261 AccountStatus::DestroyedAgain
262 );
263 assert_eq!(AccountStatus::Loaded.on_touched_empty_post_eip161(), AccountStatus::Destroyed);
264 assert_eq!(AccountStatus::Changed.on_touched_empty_post_eip161(), AccountStatus::Destroyed);
265 }
266
267 #[test]
268 fn test_on_changed() {
269 assert_eq!(
270 AccountStatus::LoadedNotExisting.on_changed(true),
271 AccountStatus::InMemoryChange
272 );
273 assert_eq!(
274 AccountStatus::LoadedNotExisting.on_changed(false),
275 AccountStatus::InMemoryChange
276 );
277
278 assert_eq!(
279 AccountStatus::LoadedEmptyEIP161.on_changed(true),
280 AccountStatus::InMemoryChange
281 );
282 assert_eq!(
283 AccountStatus::LoadedEmptyEIP161.on_changed(false),
284 AccountStatus::InMemoryChange
285 );
286
287 assert_eq!(AccountStatus::Loaded.on_changed(true), AccountStatus::InMemoryChange);
288 assert_eq!(AccountStatus::Loaded.on_changed(false), AccountStatus::Changed);
289
290 assert_eq!(AccountStatus::Changed.on_changed(true), AccountStatus::Changed);
291 assert_eq!(AccountStatus::Changed.on_changed(false), AccountStatus::Changed);
292
293 assert_eq!(AccountStatus::InMemoryChange.on_changed(true), AccountStatus::InMemoryChange);
294 assert_eq!(AccountStatus::InMemoryChange.on_changed(false), AccountStatus::InMemoryChange);
295
296 assert_eq!(
297 AccountStatus::DestroyedChanged.on_changed(true),
298 AccountStatus::DestroyedChanged
299 );
300 assert_eq!(
301 AccountStatus::DestroyedChanged.on_changed(false),
302 AccountStatus::DestroyedChanged
303 );
304
305 assert_eq!(AccountStatus::Destroyed.on_changed(true), AccountStatus::DestroyedChanged);
306 assert_eq!(AccountStatus::Destroyed.on_changed(false), AccountStatus::DestroyedChanged);
307
308 assert_eq!(AccountStatus::DestroyedAgain.on_changed(true), AccountStatus::DestroyedChanged);
309 assert_eq!(
310 AccountStatus::DestroyedAgain.on_changed(false),
311 AccountStatus::DestroyedChanged
312 );
313 }
314
315 #[test]
316 fn test_on_selfdestructed() {
317 assert_eq!(
318 AccountStatus::LoadedNotExisting.on_selfdestructed(),
319 AccountStatus::LoadedNotExisting
320 );
321
322 assert_eq!(
323 AccountStatus::DestroyedChanged.on_selfdestructed(),
324 AccountStatus::DestroyedAgain
325 );
326 assert_eq!(
327 AccountStatus::DestroyedAgain.on_selfdestructed(),
328 AccountStatus::DestroyedAgain
329 );
330 assert_eq!(AccountStatus::Destroyed.on_selfdestructed(), AccountStatus::DestroyedAgain);
331
332 assert_eq!(AccountStatus::Loaded.on_selfdestructed(), AccountStatus::Destroyed);
333 assert_eq!(AccountStatus::LoadedEmptyEIP161.on_selfdestructed(), AccountStatus::Destroyed);
334 assert_eq!(AccountStatus::InMemoryChange.on_selfdestructed(), AccountStatus::Destroyed);
335 assert_eq!(AccountStatus::Changed.on_selfdestructed(), AccountStatus::Destroyed);
336 }
337
338 #[test]
339 fn test_transition() {
340 let mut status = AccountStatus::Destroyed;
341 status.transition(AccountStatus::Loaded);
342 assert_eq!(status, AccountStatus::DestroyedChanged);
343
344 let mut status = AccountStatus::DestroyedChanged;
345 status.transition(AccountStatus::InMemoryChange);
346 assert_eq!(status, AccountStatus::DestroyedChanged);
347
348 let mut status = AccountStatus::DestroyedAgain;
349 status.transition(AccountStatus::Changed);
350 assert_eq!(status, AccountStatus::DestroyedChanged);
351
352 let mut status = AccountStatus::InMemoryChange;
353 status.transition(AccountStatus::Loaded);
354 assert_eq!(status, AccountStatus::InMemoryChange);
355
356 let mut status = AccountStatus::InMemoryChange;
357 status.transition(AccountStatus::Changed);
358 assert_eq!(status, AccountStatus::InMemoryChange);
359
360 let mut status = AccountStatus::Loaded;
361 status.transition(AccountStatus::Changed);
362 assert_eq!(status, AccountStatus::Changed);
363
364 let mut status = AccountStatus::LoadedNotExisting;
365 status.transition(AccountStatus::InMemoryChange);
366 assert_eq!(status, AccountStatus::InMemoryChange);
367
368 let mut status = AccountStatus::LoadedEmptyEIP161;
369 status.transition(AccountStatus::Loaded);
370 assert_eq!(status, AccountStatus::Loaded);
371
372 let mut status = AccountStatus::Destroyed;
373 status.transition(AccountStatus::DestroyedChanged);
374 assert_eq!(status, AccountStatus::DestroyedChanged);
375
376 let mut status = AccountStatus::DestroyedAgain;
377 status.transition(AccountStatus::Destroyed);
378 assert_eq!(status, AccountStatus::Destroyed);
379
380 let mut status = AccountStatus::Loaded;
381 status.transition(AccountStatus::Destroyed);
382 assert_eq!(status, AccountStatus::Destroyed);
383
384 let mut status = AccountStatus::Changed;
385 status.transition(AccountStatus::DestroyedAgain);
386 assert_eq!(status, AccountStatus::DestroyedAgain);
387 }
388}