Skip to main content

ethrex_common/types/
account_update.rs

1use crate::{
2    Address, H256, U256,
3    types::{AccountInfo, Code},
4};
5use rustc_hash::FxHashMap;
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    pub added_storage: FxHashMap<H256, U256>,
15    /// If account was destroyed and then modified we need this for removing its storage but not the entire account.
16    pub removed_storage: bool,
17    // Matches TODO in code
18    // removed_storage_keys: Vec<H256>,
19}
20
21impl AccountUpdate {
22    /// Creates new empty update for the given account
23    pub fn new(address: Address) -> AccountUpdate {
24        AccountUpdate {
25            address,
26            ..Default::default()
27        }
28    }
29
30    /// Creates new update representing an account removal
31    pub fn removed(address: Address) -> AccountUpdate {
32        AccountUpdate {
33            address,
34            removed: true,
35            ..Default::default()
36        }
37    }
38
39    pub fn merge(&mut self, other: AccountUpdate) {
40        self.removed = other.removed;
41        self.removed_storage |= other.removed_storage;
42        if let Some(info) = other.info {
43            self.info = Some(info);
44        }
45        if let Some(code) = other.code {
46            self.code = Some(code);
47        }
48        for (key, value) in other.added_storage {
49            self.added_storage.insert(key, value);
50        }
51    }
52}