Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(primitives): State Cleanup #776

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions crates/primitives/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ use crate::{Address, Bytecode, B256, KECCAK_EMPTY, U256};
use bitflags::bitflags;
use hashbrown::HashMap;

/// EVM State is a mapping from addresses to accounts.
pub type State = HashMap<Address, Account>;

/// Structure used for EIP-1153 transient storage.
pub type TransientStorage = HashMap<(Address, U256), U256>;

/// An account's Storage is a mapping from 256-bit integer keys to [StorageSlot]s.
pub type Storage = HashMap<U256, StorageSlot>;

#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Account {
/// Balance, nonce, and code.
pub info: AccountInfo,
/// Storage cache
pub storage: HashMap<U256, StorageSlot>,
pub storage: Storage,
/// Account status flags.
pub status: AccountStatus,
}
Expand Down Expand Up @@ -41,13 +50,6 @@ impl Default for AccountStatus {
}
}

pub type State = HashMap<Address, Account>;

/// Structure used for EIP-1153 transient storage.
pub type TransientStorage = HashMap<(Address, U256), U256>;

pub type Storage = HashMap<U256, StorageSlot>;

impl Account {
/// Mark account as self destructed.
pub fn mark_selfdestruct(&mut self) {
Expand Down Expand Up @@ -213,6 +215,12 @@ impl AccountInfo {
self
}

/// Returns if an account is empty.
///
/// An account is empty if the following conditions are met.
/// - code hash is zero or set to the Keccak256 hash of the empty string `""`
/// - balance is zero
/// - nonce is zero
pub fn is_empty(&self) -> bool {
let code_empty = self.code_hash == KECCAK_EMPTY || self.code_hash == B256::ZERO;
self.balance == U256::ZERO && self.nonce == 0 && code_empty
Expand Down