revm_database/states/
plain_account.rs

1use primitives::{HashMap, StorageKey, StorageValue};
2use state::{AccountInfo, EvmStorageSlot};
3
4/// Plain account of StateDatabase.
5#[derive(Clone, Debug, Default, PartialEq, Eq)]
6pub struct PlainAccount {
7    /// Account information.
8    pub info: AccountInfo,
9    /// Account storage.
10    pub storage: PlainStorage,
11}
12
13impl PlainAccount {
14    /// Creates a new empty account with the given storage.
15    pub fn new_empty_with_storage(storage: PlainStorage) -> Self {
16        Self {
17            info: AccountInfo::default(),
18            storage,
19        }
20    }
21
22    /// Consumes the account and returns its components.
23    pub fn into_components(self) -> (AccountInfo, PlainStorage) {
24        (self.info, self.storage)
25    }
26}
27
28/// This type keeps track of the current value of a storage slot.
29#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash)]
30#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
31pub struct StorageSlot {
32    /// The value of the storage slot before it was changed.
33    ///
34    /// When the slot is first loaded, this is the original value.
35    ///
36    /// If the slot was not changed, this is equal to the present value.
37    pub previous_or_original_value: StorageValue,
38    /// When loaded with sload present value is set to original value
39    pub present_value: StorageValue,
40}
41
42impl From<EvmStorageSlot> for StorageSlot {
43    fn from(value: EvmStorageSlot) -> Self {
44        Self::new_changed(value.original_value, value.present_value)
45    }
46}
47
48impl StorageSlot {
49    /// Creates a new _unchanged_ `StorageSlot` for the given value.
50    pub fn new(original: StorageValue) -> Self {
51        Self {
52            previous_or_original_value: original,
53            present_value: original,
54        }
55    }
56
57    /// Creates a new _changed_ `StorageSlot`.
58    pub fn new_changed(
59        previous_or_original_value: StorageValue,
60        present_value: StorageValue,
61    ) -> Self {
62        Self {
63            previous_or_original_value,
64            present_value,
65        }
66    }
67
68    /// Returns true if the present value differs from the original value
69    pub fn is_changed(&self) -> bool {
70        self.previous_or_original_value != self.present_value
71    }
72
73    /// Returns the original value of the storage slot.
74    pub fn original_value(&self) -> StorageValue {
75        self.previous_or_original_value
76    }
77
78    /// Returns the current value of the storage slot.
79    pub fn present_value(&self) -> StorageValue {
80        self.present_value
81    }
82}
83
84/// This storage represent values that are before block changed.
85///
86/// Note: Storage that we get EVM contains original values before block changed.
87pub type StorageWithOriginalValues = HashMap<StorageKey, StorageSlot>;
88
89/// Simple plain storage that does not have previous value.
90/// This is used for loading from database, cache and for bundle state.
91pub type PlainStorage = HashMap<StorageKey, StorageValue>;
92
93impl From<AccountInfo> for PlainAccount {
94    fn from(info: AccountInfo) -> Self {
95        Self {
96            info,
97            storage: HashMap::default(),
98        }
99    }
100}