miden_objects/block/
account_update_witness.rs

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