miden_objects/block/
account_update_witness.rs

1use alloc::vec::Vec;
2
3use crate::{
4    Digest,
5    account::delta::AccountUpdateDetails,
6    crypto::merkle::MerklePath,
7    transaction::TransactionId,
8    utils::serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
9};
10
11/// This type encapsulates essentially three components:
12/// - The witness is a merkle path of the initial state commitment of the account before the block
13///   in which the witness is included, that is, in the account tree at the state of the previous
14///   block header.
15/// - The account update details represent the delta between the state of the account before the
16///   block and the state after this block.
17/// - Additionally contains a list of transaction IDs that contributed to this update.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct AccountUpdateWitness {
20    /// The state commitment before the update.
21    initial_state_commitment: Digest,
22    /// The state commitment after the update.
23    final_state_commitment: Digest,
24    /// The merkle path for the account tree proving that the initial state commitment is the
25    /// current state.
26    initial_state_proof: MerklePath,
27    /// A set of changes which can be applied to the previous account state (i.e., the state as of
28    /// the last block, equivalent to `initial_state_commitment`) to get the new account state. For
29    /// private accounts, this is set to [`AccountUpdateDetails::Private`].
30    details: AccountUpdateDetails,
31    /// All transaction IDs that contributed to this account update.
32    transactions: Vec<TransactionId>,
33}
34
35impl AccountUpdateWitness {
36    // CONSTRUCTORS
37    // --------------------------------------------------------------------------------------------
38
39    /// Constructs a new, partial [`AccountUpdateWitness`] from the provided parts.
40    pub fn new(
41        initial_state_commitment: Digest,
42        final_state_commitment: Digest,
43        initial_state_proof: MerklePath,
44        details: AccountUpdateDetails,
45        transactions: Vec<TransactionId>,
46    ) -> Self {
47        Self {
48            initial_state_commitment,
49            final_state_commitment,
50            initial_state_proof,
51            details,
52            transactions,
53        }
54    }
55
56    // PUBLIC ACCESSORS
57    // --------------------------------------------------------------------------------------------
58
59    /// Returns the initial state commitment of the account.
60    pub fn initial_state_commitment(&self) -> Digest {
61        self.initial_state_commitment
62    }
63
64    /// Returns the final state commitment of the account.
65    pub fn final_state_commitment(&self) -> Digest {
66        self.final_state_commitment
67    }
68
69    /// Returns a reference to the initial state proof of the account.
70    pub fn initial_state_proof(&self) -> &MerklePath {
71        &self.initial_state_proof
72    }
73
74    /// Returns a reference to the underlying [`AccountUpdateDetails`] of this update, representing
75    /// the state transition of the account from the previous block to the block this witness is
76    /// for.
77    pub fn details(&self) -> &AccountUpdateDetails {
78        &self.details
79    }
80
81    /// Returns the transactions that affected the account.
82    pub fn transactions(&self) -> &[TransactionId] {
83        &self.transactions
84    }
85
86    // STATE MUTATORS
87    // --------------------------------------------------------------------------------------------
88
89    /// Returns a mutable reference to the initial state proof of the account.
90    pub fn initial_state_proof_mut(&mut self) -> &mut MerklePath {
91        &mut self.initial_state_proof
92    }
93
94    /// Consumes self and returns its parts.
95    pub fn into_parts(
96        self,
97    ) -> (Digest, Digest, MerklePath, AccountUpdateDetails, Vec<TransactionId>) {
98        (
99            self.initial_state_commitment,
100            self.final_state_commitment,
101            self.initial_state_proof,
102            self.details,
103            self.transactions,
104        )
105    }
106}
107
108// SERIALIZATION
109// ================================================================================================
110
111impl Serializable for AccountUpdateWitness {
112    fn write_into<W: ByteWriter>(&self, target: &mut W) {
113        target.write(self.initial_state_commitment);
114        target.write(self.final_state_commitment);
115        target.write(&self.initial_state_proof);
116        target.write(&self.details);
117        target.write(&self.transactions);
118    }
119}
120
121impl Deserializable for AccountUpdateWitness {
122    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
123        let initial_state_commitment = source.read()?;
124        let final_state_commitment = source.read()?;
125        let initial_state_proof = source.read()?;
126        let details = source.read()?;
127        let transactions = source.read()?;
128        Ok(AccountUpdateWitness {
129            initial_state_commitment,
130            final_state_commitment,
131            initial_state_proof,
132            details,
133            transactions,
134        })
135    }
136}