Skip to main content

miden_protocol/block/
block_account_update.rs

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