Skip to main content

alloy_eip7928/
code_change.rs

1//! Contains the [`CodeChange`] struct, which represents a new code for an account.
2//! Single code change: `tx_index` -> `new_code`
3use crate::BlockAccessIndex;
4use alloy_primitives::Bytes;
5
6/// This struct is used to track the new codes of accounts in a block.
7#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "rlp", derive(alloy_rlp::RlpEncodable, alloy_rlp::RlpDecodable))]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
11#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
12#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
13pub struct CodeChange {
14    /// The index of bal that stores this code change.
15    #[cfg_attr(
16        feature = "serde",
17        serde(rename = "index", alias = "blockAccessIndex", alias = "txIndex")
18    )]
19    pub block_access_index: BlockAccessIndex,
20    /// The new code of the account.
21    #[cfg_attr(feature = "serde", serde(rename = "code", alias = "newCode"))]
22    pub new_code: Bytes,
23}
24impl CodeChange {
25    /// Creates a new [`CodeChange`].
26    pub const fn new(block_access_index: BlockAccessIndex, new_code: Bytes) -> Self {
27        Self { block_access_index, new_code }
28    }
29
30    /// Returns the bal index.
31    #[inline]
32    pub const fn block_access_index(&self) -> BlockAccessIndex {
33        self.block_access_index
34    }
35
36    /// Returns the new code.
37    #[inline]
38    pub const fn new_code(&self) -> &Bytes {
39        &self.new_code
40    }
41
42    /// Consumes the change and returns the new code.
43    #[inline]
44    pub fn into_code(self) -> Bytes {
45        self.new_code
46    }
47}