Skip to main content

ethrex_common/types/
account_update.rs

1use crate::seeded_hash::SeededHashMap;
2use crate::{
3    Address, H256, U256,
4    types::{AccountInfo, Code},
5};
6use serde::{Deserialize, Serialize};
7
8#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
9pub struct AccountUpdate {
10    pub address: Address,
11    pub removed: bool,
12    pub info: Option<AccountInfo>,
13    pub code: Option<Code>,
14    /// Seeded: contracts choose these slot keys.
15    pub added_storage: SeededHashMap<H256, U256>,
16    /// If account was destroyed and then modified we need this for removing its storage but not the entire account.
17    pub removed_storage: bool,
18    // Matches TODO in code
19    // removed_storage_keys: Vec<H256>,
20}
21
22impl AccountUpdate {
23    /// Creates new empty update for the given account
24    pub fn new(address: Address) -> AccountUpdate {
25        AccountUpdate {
26            address,
27            ..Default::default()
28        }
29    }
30
31    /// Creates new update representing an account removal
32    pub fn removed(address: Address) -> AccountUpdate {
33        AccountUpdate {
34            address,
35            removed: true,
36            ..Default::default()
37        }
38    }
39
40    pub fn merge(&mut self, other: AccountUpdate) {
41        self.removed = other.removed;
42        self.removed_storage |= other.removed_storage;
43        if let Some(info) = other.info {
44            self.info = Some(info);
45        }
46        if let Some(code) = other.code {
47            self.code = Some(code);
48        }
49        for (key, value) in other.added_storage {
50            self.added_storage.insert(key, value);
51        }
52    }
53}