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: inline more AccountInfo fns and add docs #1819

Merged
merged 1 commit into from
Oct 9, 2024
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
14 changes: 14 additions & 0 deletions crates/state/src/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ impl Hash for AccountInfo {
}

impl AccountInfo {
/// Creates a new [`AccountInfo`] with the given fields.
#[inline]
pub fn new(balance: U256, nonce: u64, code_hash: B256, code: Bytecode) -> Self {
Self {
balance,
Expand All @@ -63,6 +65,7 @@ impl AccountInfo {
/// This is distinct from [`AccountInfo::without_code`] in that it returns
/// a new `AccountInfo` instance with the code removed.
/// [`AccountInfo::without_code`] will modify and return the same instance.
#[inline]
pub fn copy_without_code(&self) -> Self {
Self {
balance: self.balance,
Expand Down Expand Up @@ -92,23 +95,27 @@ impl AccountInfo {
/// - code hash is zero or set to the Keccak256 hash of the empty string `""`
/// - balance is zero
/// - nonce is zero
#[inline]
pub fn is_empty(&self) -> bool {
let code_empty = self.is_empty_code_hash() || self.code_hash.is_zero();
code_empty && self.balance.is_zero() && self.nonce == 0
}

/// Returns `true` if the account is not empty.
#[inline]
pub fn exists(&self) -> bool {
!self.is_empty()
}

/// Returns `true` if account has no nonce and code.
#[inline]
pub fn has_no_code_and_nonce(&self) -> bool {
self.is_empty_code_hash() && self.nonce == 0
}

/// Return bytecode hash associated with this account.
/// If account does not have code, it returns `KECCAK_EMPTY` hash.
#[inline]
pub fn code_hash(&self) -> B256 {
self.code_hash
}
Expand All @@ -120,17 +127,24 @@ impl AccountInfo {
}

/// Take bytecode from account. Code will be set to None.
#[inline]
pub fn take_bytecode(&mut self) -> Option<Bytecode> {
self.code.take()
}

/// Initialize an [`AccountInfo`] with the given balance, setting all other fields to their
/// default values.
#[inline]
pub fn from_balance(balance: U256) -> Self {
AccountInfo {
balance,
..Default::default()
}
}

/// Initialize an [`AccountInfo`] with the given bytecode, setting its balance to zero, its
/// nonce to `1`, and calculating the code hash from the given bytecode.
#[inline]
pub fn from_bytecode(bytecode: Bytecode) -> Self {
let hash = bytecode.hash_slow();

Expand Down
Loading