1use crate::primitives::{AccountInfo, EvmStorageSlot, HashMap, U256};
23// TODO rename this to BundleAccount. As for the block level we have original state.
4#[derive(Clone, Debug, Default, PartialEq, Eq)]
5pub struct PlainAccount {
6pub info: AccountInfo,
7pub storage: PlainStorage,
8}
910impl PlainAccount {
11pub fn new_empty_with_storage(storage: PlainStorage) -> Self {
12Self {
13 info: AccountInfo::default(),
14 storage,
15 }
16 }
1718pub fn into_components(self) -> (AccountInfo, PlainStorage) {
19 (self.info, self.storage)
20 }
21}
2223/// 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.
32pub previous_or_original_value: U256,
33/// When loaded with sload present value is set to original value
34pub present_value: U256,
35}
3637impl From<EvmStorageSlot> for StorageSlot {
38fn from(value: EvmStorageSlot) -> Self {
39Self::new_changed(value.original_value, value.present_value)
40 }
41}
4243impl StorageSlot {
44/// Creates a new _unchanged_ `StorageSlot` for the given value.
45pub fn new(original: U256) -> Self {
46Self {
47 previous_or_original_value: original,
48 present_value: original,
49 }
50 }
5152/// Creates a new _changed_ `StorageSlot`.
53pub fn new_changed(previous_or_original_value: U256, present_value: U256) -> Self {
54Self {
55 previous_or_original_value,
56 present_value,
57 }
58 }
5960/// Returns true if the present value differs from the original value
61pub fn is_changed(&self) -> bool {
62self.previous_or_original_value != self.present_value
63 }
6465/// Returns the original value of the storage slot.
66pub fn original_value(&self) -> U256 {
67self.previous_or_original_value
68 }
6970/// Returns the current value of the storage slot.
71pub fn present_value(&self) -> U256 {
72self.present_value
73 }
74}
7576/// 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>;
8081/// 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>;
8485impl From<AccountInfo> for PlainAccount {
86fn from(info: AccountInfo) -> Self {
87Self {
88 info,
89 storage: HashMap::default(),
90 }
91 }
92}