This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 854
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
84 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//! Doc this | ||
use crate::{DebugWord, Error, ToBigEndian, Word}; | ||
use serde::{ser::SerializeMap, Serialize, Serializer}; | ||
use std::{collections::HashMap, fmt}; | ||
|
||
/// Represents a snapshot of the EVM stack state at a certain | ||
/// execution step height. | ||
#[derive(Clone, Eq, PartialEq)] | ||
pub struct TransientStorage(pub HashMap<Word, Word>); | ||
|
||
impl<T: Into<HashMap<Word, Word>>> From<T> for TransientStorage { | ||
fn from(map: T) -> Self { | ||
Self(map.into()) | ||
} | ||
} | ||
|
||
impl fmt::Debug for TransientStorage { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_map() | ||
.entries(self.0.iter().map(|(k, v)| (DebugWord(k), DebugWord(v)))) | ||
.finish() | ||
} | ||
} | ||
|
||
impl Serialize for TransientStorage { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut ser = serializer.serialize_map(Some(self.0.len()))?; | ||
for (k, v) in self.0.iter() { | ||
ser.serialize_entry(&hex::encode(k.to_be_bytes()), &hex::encode(v.to_be_bytes()))?; | ||
} | ||
ser.end() | ||
} | ||
} | ||
|
||
impl Default for TransientStorage { | ||
fn default() -> Self { | ||
Self::empty() | ||
} | ||
} | ||
|
||
impl TransientStorage { | ||
/// Generate an empty instance of EVM TransientStorage. | ||
pub fn empty() -> Self { | ||
TransientStorage(HashMap::new()) | ||
} | ||
|
||
/// Generate an new instance of EVM transient storage given a `HashMap<Word, Word>`. | ||
pub fn new(map: HashMap<Word, Word>) -> Self { | ||
Self::from(map) | ||
} | ||
|
||
/// Get the word for a given key in the EVM transient storage | ||
pub fn get(&self, key: &Word) -> Option<&Word> { | ||
self.0.get(key) | ||
} | ||
|
||
/// Get the word for a given key in the EVM transient storage. Returns error if key | ||
/// is not found. | ||
pub fn get_or_err(&self, key: &Word) -> Result<Word, Error> { | ||
self.get(key) | ||
.cloned() | ||
.ok_or(Error::InvalidTransientStorageKey) | ||
} | ||
} |