Skip to main content

miden_protocol/block/
block_account_update.rs

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