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