From aaac2855ae4e02ce2f35620eb2ff628eeb912428 Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 6 Oct 2023 16:23:09 +0000 Subject: [PATCH] Clean up the account typs with docs --- crates/primitives/src/state.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/primitives/src/state.rs b/crates/primitives/src/state.rs index b626a76cfd..a98a3694fc 100644 --- a/crates/primitives/src/state.rs +++ b/crates/primitives/src/state.rs @@ -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; + +/// 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; + #[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, + pub storage: Storage, /// Account status flags. pub status: AccountStatus, } @@ -41,13 +50,6 @@ impl Default for AccountStatus { } } -pub type State = HashMap; - -/// Structure used for EIP-1153 transient storage. -pub type TransientStorage = HashMap<(Address, U256), U256>; - -pub type Storage = HashMap; - impl Account { /// Mark account as self destructed. pub fn mark_selfdestruct(&mut self) { @@ -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