miden_objects/block/
account_update_witness.rs

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