revm_database/states/
plain_account.rs

1use primitives::{HashMap, U256};
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: U256,
34    /// When loaded with sload present value is set to original value
35    pub present_value: U256,
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: U256) -> 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(previous_or_original_value: U256, present_value: U256) -> Self {
55        Self {
56            previous_or_original_value,
57            present_value,
58        }
59    }
60
61    /// Returns true if the present value differs from the original value
62    pub fn is_changed(&self) -> bool {
63        self.previous_or_original_value != self.present_value
64    }
65
66    /// Returns the original value of the storage slot.
67    pub fn original_value(&self) -> U256 {
68        self.previous_or_original_value
69    }
70
71    /// Returns the current value of the storage slot.
72    pub fn present_value(&self) -> U256 {
73        self.present_value
74    }
75}
76
77/// This storage represent values that are before block changed.
78///
79/// Note: Storage that we get EVM contains original values before block changed.
80pub type StorageWithOriginalValues = HashMap<U256, StorageSlot>;
81
82/// Simple plain storage that does not have previous value.
83/// This is used for loading from database, cache and for bundle state.
84pub type PlainStorage = HashMap<U256, U256>;
85
86impl From<AccountInfo> for PlainAccount {
87    fn from(info: AccountInfo) -> Self {
88        Self {
89            info,
90            storage: HashMap::default(),
91        }
92    }
93}