use super::{Account, EvmStorageSlot};
use core::ops::{Deref, DerefMut};
use primitives::{Address, AddressMap, StorageKey, StorageKeyMap, StorageValue};
pub type EvmState = AddressMap<Account>;
pub type EvmStorage = StorageKeyMap<EvmStorageSlot>;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TransientStorage(pub AddressMap<StorageKeyMap<StorageValue>>);
impl Deref for TransientStorage {
type Target = AddressMap<StorageKeyMap<StorageValue>>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for TransientStorage {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl TransientStorage {
#[inline]
pub fn get_value(&self, address: Address, key: StorageKey) -> StorageValue {
self.0
.get(&address)
.and_then(|slots| slots.get(&key))
.copied()
.unwrap_or_default()
}
#[inline]
pub fn insert_value(
&mut self,
address: Address,
key: StorageKey,
value: StorageValue,
) -> Option<StorageValue> {
self.0.entry(address).or_default().insert(key, value)
}
#[inline]
pub fn remove_value(&mut self, address: Address, key: StorageKey) -> Option<StorageValue> {
self.0.get_mut(&address)?.remove(&key)
}
}