miden_objects/block/
block_account_update.rs

1use crate::{
2    Digest,
3    account::{AccountId, delta::AccountUpdateDetails},
4    utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
5};
6
7// BLOCK ACCOUNT UPDATE
8// ================================================================================================
9
10/// Describes the changes made to an account state resulting from executing transactions contained
11/// in a block.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct BlockAccountUpdate {
14    /// ID of the updated account.
15    account_id: AccountId,
16
17    /// Final commitment to the new state of the account after this update.
18    final_state_commitment: Digest,
19
20    /// A set of changes which can be applied to the previous account state (i.e., the state as of
21    /// the last block) to get the new account state. For private accounts, this is set to
22    /// [AccountUpdateDetails::Private].
23    details: AccountUpdateDetails,
24}
25
26impl BlockAccountUpdate {
27    /// Returns a new [BlockAccountUpdate] instantiated from the specified components.
28    pub const fn new(
29        account_id: AccountId,
30        final_state_commitment: Digest,
31        details: AccountUpdateDetails,
32    ) -> Self {
33        Self {
34            account_id,
35            final_state_commitment,
36            details,
37        }
38    }
39
40    /// Returns the ID of the updated account.
41    pub fn account_id(&self) -> AccountId {
42        self.account_id
43    }
44
45    /// Returns the state commitment of the account after this update.
46    pub fn final_state_commitment(&self) -> Digest {
47        self.final_state_commitment
48    }
49
50    /// Returns the description of the updates for on-chain accounts.
51    ///
52    /// These descriptions can be used to build the new account state from the previous account
53    /// state.
54    pub fn details(&self) -> &AccountUpdateDetails {
55        &self.details
56    }
57
58    /// Returns `true` if the account update details are for private account.
59    pub fn is_private(&self) -> bool {
60        self.details.is_private()
61    }
62}
63
64impl Serializable for BlockAccountUpdate {
65    fn write_into<W: ByteWriter>(&self, target: &mut W) {
66        self.account_id.write_into(target);
67        self.final_state_commitment.write_into(target);
68        self.details.write_into(target);
69    }
70}
71
72impl Deserializable for BlockAccountUpdate {
73    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
74        Ok(Self {
75            account_id: AccountId::read_from(source)?,
76            final_state_commitment: Digest::read_from(source)?,
77            details: AccountUpdateDetails::read_from(source)?,
78        })
79    }
80}