Skip to main content

reth_trie_common/bundle_state/
plain_account.rs

1use alloy_primitives::{
2    map::{HashMap, U256Map},
3    U256,
4};
5use evm2::evm::AccountInfo;
6
7/// Plain account of StateDatabase.
8#[derive(Clone, Debug, Default, PartialEq, Eq)]
9pub struct PlainAccount {
10    /// Account information.
11    pub info: AccountInfo,
12    /// Account storage.
13    pub storage: PlainStorage,
14}
15
16impl PlainAccount {
17    /// Creates a new empty account with the given storage.
18    pub fn new_empty_with_storage(storage: PlainStorage) -> Self {
19        Self { info: AccountInfo::default(), storage }
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: U256,
38    /// When loaded with sload present value is set to original value
39    pub present_value: U256,
40}
41
42impl StorageSlot {
43    /// Creates a new _unchanged_ `StorageSlot` for the given value.
44    pub const fn new(original: U256) -> Self {
45        Self { previous_or_original_value: original, present_value: original }
46    }
47
48    /// Creates a new _changed_ `StorageSlot`.
49    pub const fn new_changed(previous_or_original_value: U256, present_value: U256) -> Self {
50        Self { previous_or_original_value, present_value }
51    }
52
53    /// Returns true if the present value differs from the original value
54    pub fn is_changed(&self) -> bool {
55        self.previous_or_original_value != self.present_value
56    }
57
58    /// Returns the original value of the storage slot.
59    pub const fn original_value(&self) -> U256 {
60        self.previous_or_original_value
61    }
62
63    /// Returns the current value of the storage slot.
64    pub const fn present_value(&self) -> U256 {
65        self.present_value
66    }
67}
68
69/// This storage represent values that are before block changed.
70///
71/// Note: Storage that we get EVM contains original values before block changed.
72pub type StorageWithOriginalValues = U256Map<StorageSlot>;
73
74/// Simple plain storage that does not have previous value.
75/// This is used for loading from database, cache and for bundle state.
76pub type PlainStorage = U256Map<U256>;
77
78impl From<AccountInfo> for PlainAccount {
79    fn from(info: AccountInfo) -> Self {
80        Self { info, storage: HashMap::default() }
81    }
82}