Skip to content

Commit

Permalink
Implement custom default for Account representing a valid empty acc…
Browse files Browse the repository at this point in the history
…ount (alloy-rs#1313)

implement custom default for Account
  • Loading branch information
Wollac authored and lwedge99 committed Oct 8, 2024
1 parent 6290f55 commit c98050c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/consensus/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::constants::{EMPTY_ROOT_HASH, KECCAK_EMPTY};
use alloy_primitives::{keccak256, B256, U256};
use alloy_rlp::{RlpDecodable, RlpEncodable};

/// Represents an Account in the account trie.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, RlpDecodable, RlpEncodable)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, RlpDecodable, RlpEncodable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
Expand All @@ -18,6 +19,17 @@ pub struct Account {
pub code_hash: B256,
}

impl Default for Account {
fn default() -> Self {
Self {
nonce: 0,
balance: U256::ZERO,
storage_root: EMPTY_ROOT_HASH,
code_hash: KECCAK_EMPTY,
}
}
}

impl Account {
/// Compute hash as committed to in the MPT trie without memorizing.
pub fn trie_hash_slow(&self) -> B256 {
Expand Down

0 comments on commit c98050c

Please sign in to comment.