miden_objects/block/
block_account_update.rs

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