miden_protocol/block/account_update_witness.rs
1use crate::Word;
2use crate::account::AccountUpdateDetails;
3use crate::block::account_tree::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 [`AccountUpdateDetails`] representing the change applied to the account by the block.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct AccountUpdateWitness {
20 /// The state commitment before the update.
21 initial_state_commitment: Word,
22 /// The state commitment after the update.
23 final_state_commitment: Word,
24 /// The account witness proving that the initial state commitment is the current state in the
25 /// account tree.
26 initial_state_proof: AccountWitness,
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}
32
33impl AccountUpdateWitness {
34 // CONSTRUCTORS
35 // --------------------------------------------------------------------------------------------
36
37 /// Constructs a new, partial [`AccountUpdateWitness`] from the provided parts.
38 pub fn new(
39 initial_state_commitment: Word,
40 final_state_commitment: Word,
41 initial_state_proof: AccountWitness,
42 details: AccountUpdateDetails,
43 ) -> Self {
44 Self {
45 initial_state_commitment,
46 final_state_commitment,
47 initial_state_proof,
48 details,
49 }
50 }
51
52 // PUBLIC ACCESSORS
53 // --------------------------------------------------------------------------------------------
54
55 /// Returns the initial state commitment of the account.
56 pub fn initial_state_commitment(&self) -> Word {
57 self.initial_state_commitment
58 }
59
60 /// Returns the final state commitment of the account.
61 pub fn final_state_commitment(&self) -> Word {
62 self.final_state_commitment
63 }
64
65 /// Returns a reference to the initial state proof of the account.
66 pub fn as_witness(&self) -> &AccountWitness {
67 &self.initial_state_proof
68 }
69
70 /// Returns the [`AccountWitness`] of this update witness.
71 pub fn to_witness(&self) -> AccountWitness {
72 self.initial_state_proof.clone()
73 }
74
75 /// Returns a reference to the underlying [`AccountUpdateDetails`] of this update, representing
76 /// the state transition of the account from the previous block to the block this witness is
77 /// for.
78 pub fn details(&self) -> &AccountUpdateDetails {
79 &self.details
80 }
81
82 // STATE MUTATORS
83 // --------------------------------------------------------------------------------------------
84
85 /// Returns a mutable reference to the initial state proof of the account.
86 pub fn initial_state_proof_mut(&mut self) -> &mut AccountWitness {
87 &mut self.initial_state_proof
88 }
89
90 /// Consumes self and returns its parts.
91 pub fn into_parts(self) -> (Word, Word, AccountWitness, AccountUpdateDetails) {
92 (
93 self.initial_state_commitment,
94 self.final_state_commitment,
95 self.initial_state_proof,
96 self.details,
97 )
98 }
99}
100
101// SERIALIZATION
102// ================================================================================================
103
104impl Serializable for AccountUpdateWitness {
105 fn write_into<W: ByteWriter>(&self, target: &mut W) {
106 target.write(self.initial_state_commitment);
107 target.write(self.final_state_commitment);
108 target.write(&self.initial_state_proof);
109 target.write(&self.details);
110 }
111}
112
113impl Deserializable for AccountUpdateWitness {
114 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
115 let initial_state_commitment = source.read()?;
116 let final_state_commitment = source.read()?;
117 let initial_state_proof = source.read()?;
118 let details = source.read()?;
119
120 Ok(AccountUpdateWitness {
121 initial_state_commitment,
122 final_state_commitment,
123 initial_state_proof,
124 details,
125 })
126 }
127}