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