revm/db/states/
plain_account.rs

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