miden_protocol/batch/
account_update.rs1use alloc::boxed::Box;
2
3use crate::Word;
4use crate::account::{AccountId, AccountUpdateDetails};
5use crate::errors::BatchAccountUpdateError;
6use crate::transaction::ProvenTransaction;
7use crate::utils::serde::{
8 ByteReader,
9 ByteWriter,
10 Deserializable,
11 DeserializationError,
12 Serializable,
13};
14
15#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct BatchAccountUpdate {
21 account_id: AccountId,
23
24 initial_state_commitment: Word,
28
29 final_state_commitment: Word,
31
32 details: AccountUpdateDetails,
36}
37
38impl BatchAccountUpdate {
39 pub fn from_transaction(transaction: &ProvenTransaction) -> Self {
45 Self {
46 account_id: transaction.account_id(),
47 initial_state_commitment: transaction.account_update().initial_state_commitment(),
48 final_state_commitment: transaction.account_update().final_state_commitment(),
49 details: transaction.account_update().details().clone(),
50 }
51 }
52
53 #[cfg(any(feature = "testing", test))]
55 pub fn new_unchecked(
56 account_id: AccountId,
57 initial_state_commitment: Word,
58 final_state_commitment: Word,
59 details: AccountUpdateDetails,
60 ) -> Self {
61 Self {
62 account_id,
63 initial_state_commitment,
64 final_state_commitment,
65 details,
66 }
67 }
68
69 pub fn account_id(&self) -> AccountId {
74 self.account_id
75 }
76
77 pub fn initial_state_commitment(&self) -> Word {
81 self.initial_state_commitment
82 }
83
84 pub fn final_state_commitment(&self) -> Word {
86 self.final_state_commitment
87 }
88
89 pub fn details(&self) -> &AccountUpdateDetails {
93 &self.details
94 }
95
96 pub fn is_private(&self) -> bool {
98 self.details.is_private()
99 }
100
101 pub fn merge_proven_tx(
115 &mut self,
116 tx: &ProvenTransaction,
117 ) -> Result<(), BatchAccountUpdateError> {
118 if self.account_id != tx.account_id() {
119 return Err(BatchAccountUpdateError::AccountUpdateIdMismatch {
120 transaction: tx.id(),
121 expected_account_id: self.account_id,
122 actual_account_id: tx.account_id(),
123 });
124 }
125
126 if self.final_state_commitment != tx.account_update().initial_state_commitment() {
127 return Err(BatchAccountUpdateError::AccountUpdateInitialStateMismatch(tx.id()));
128 }
129
130 self.details = self.details.clone().merge(tx.account_update().details().clone()).map_err(
131 |source_err| {
132 BatchAccountUpdateError::TransactionUpdateMergeError(tx.id(), Box::new(source_err))
133 },
134 )?;
135 self.final_state_commitment = tx.account_update().final_state_commitment();
136
137 Ok(())
138 }
139
140 pub fn into_update(self) -> AccountUpdateDetails {
145 self.details
146 }
147}
148
149impl Serializable for BatchAccountUpdate {
153 fn write_into<W: ByteWriter>(&self, target: &mut W) {
154 self.account_id.write_into(target);
155 self.initial_state_commitment.write_into(target);
156 self.final_state_commitment.write_into(target);
157 self.details.write_into(target);
158 }
159}
160
161impl Deserializable for BatchAccountUpdate {
162 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
163 Ok(Self {
164 account_id: AccountId::read_from(source)?,
165 initial_state_commitment: Word::read_from(source)?,
166 final_state_commitment: Word::read_from(source)?,
167 details: AccountUpdateDetails::read_from(source)?,
168 })
169 }
170}