alloy_eip7928/
account_changes.rs

1//! Contains the [`AccountChanges`] struct, which represents storage writes, balance, nonce, code
2//! changes and read for the account. All changes for a single account, grouped by field type.
3//! This eliminates address redundancy across different change types.
4
5use crate::{
6    SlotChanges, balance_change::BalanceChange, code_change::CodeChange, nonce_change::NonceChange,
7};
8use alloc::vec::Vec;
9use alloy_primitives::{Address, U256};
10
11/// This struct is used to track the changes across accounts in a block.
12#[derive(Debug, Clone, Default, PartialEq, Eq)]
13#[cfg_attr(feature = "rlp", derive(alloy_rlp::RlpEncodable, alloy_rlp::RlpDecodable))]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
16#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
17#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
18pub struct AccountChanges {
19    /// The address of the account whoose changes are stored.
20    pub address: Address,
21    /// List of slot changes for this account.
22    pub storage_changes: Vec<SlotChanges>,
23    /// List of storage reads for this account.
24    pub storage_reads: Vec<U256>,
25    /// List of balance changes for this account.
26    pub balance_changes: Vec<BalanceChange>,
27    /// List of nonce changes for this account.
28    pub nonce_changes: Vec<NonceChange>,
29    /// List of code changes for this account.
30    pub code_changes: Vec<CodeChange>,
31}
32
33impl AccountChanges {
34    /// Creates a new [`AccountChanges`] instance for the given address with empty vectors.
35    pub const fn new(address: Address) -> Self {
36        Self {
37            address,
38            storage_changes: Vec::new(),
39            storage_reads: Vec::new(),
40            balance_changes: Vec::new(),
41            nonce_changes: Vec::new(),
42            code_changes: Vec::new(),
43        }
44    }
45
46    /// Creates a new [`AccountChanges`] instance for the given address with specified capacity.
47    pub fn with_capacity(address: Address, capacity: usize) -> Self {
48        Self {
49            address,
50            storage_changes: Vec::with_capacity(capacity),
51            storage_reads: Vec::with_capacity(capacity),
52            balance_changes: Vec::with_capacity(capacity),
53            nonce_changes: Vec::with_capacity(capacity),
54            code_changes: Vec::with_capacity(capacity),
55        }
56    }
57
58    /// Returns the address of the account.
59    #[inline]
60    pub const fn address(&self) -> Address {
61        self.address
62    }
63
64    /// Returns the storage changes for this account.
65    #[inline]
66    pub fn storage_changes(&self) -> &[SlotChanges] {
67        &self.storage_changes
68    }
69
70    /// Returns the storage reads for this account.
71    #[inline]
72    pub fn storage_reads(&self) -> &[U256] {
73        &self.storage_reads
74    }
75
76    /// Returns the balance changes for this account.
77    #[inline]
78    pub fn balance_changes(&self) -> &[BalanceChange] {
79        &self.balance_changes
80    }
81
82    /// Returns the nonce changes for this account.
83    #[inline]
84    pub fn nonce_changes(&self) -> &[NonceChange] {
85        &self.nonce_changes
86    }
87
88    /// Returns the code changes for this account.
89    #[inline]
90    pub fn code_changes(&self) -> &[CodeChange] {
91        &self.code_changes
92    }
93
94    /// Set the address.
95    pub const fn with_address(mut self, address: Address) -> Self {
96        self.address = address;
97        self
98    }
99
100    /// Add a storage read slot.
101    pub fn with_storage_read(mut self, key: U256) -> Self {
102        self.storage_reads.push(key);
103        self
104    }
105
106    /// Add a storage change (multiple writes to a slot grouped in `SlotChanges`).
107    pub fn with_storage_change(mut self, change: SlotChanges) -> Self {
108        self.storage_changes.push(change);
109        self
110    }
111
112    /// Add a balance change.
113    pub fn with_balance_change(mut self, change: BalanceChange) -> Self {
114        self.balance_changes.push(change);
115        self
116    }
117
118    /// Add a nonce change.
119    pub fn with_nonce_change(mut self, change: NonceChange) -> Self {
120        self.nonce_changes.push(change);
121        self
122    }
123
124    /// Add a code change.
125    pub fn with_code_change(mut self, change: CodeChange) -> Self {
126        self.code_changes.push(change);
127        self
128    }
129
130    /// Add multiple storage reads at once.
131    pub fn extend_storage_reads<I>(mut self, iter: I) -> Self
132    where
133        I: IntoIterator<Item = U256>,
134    {
135        self.storage_reads.extend(iter);
136        self
137    }
138
139    /// Add multiple slot changes at once.
140    pub fn extend_storage_changes<I>(mut self, iter: I) -> Self
141    where
142        I: IntoIterator<Item = SlotChanges>,
143    {
144        self.storage_changes.extend(iter);
145        self
146    }
147}