Skip to main content

miden_client/transaction/
store_update.rs

1use alloc::vec::Vec;
2
3use miden_protocol::block::BlockNumber;
4use miden_protocol::note::{NoteDetails, NoteTag};
5use miden_protocol::transaction::ExecutedTransaction;
6use miden_tx::utils::serde::{
7    ByteReader,
8    ByteWriter,
9    Deserializable,
10    DeserializationError,
11    Serializable,
12};
13
14use crate::note::NoteUpdateTracker;
15use crate::sync::NoteTagRecord;
16
17// TRANSACTION STORE UPDATE
18// ================================================================================================
19
20/// Represents the changes that need to be applied to the client store as a result of a
21/// transaction execution.
22#[derive(Debug, Clone)]
23pub struct TransactionStoreUpdate {
24    /// Details of the executed transaction to be inserted.
25    executed_transaction: ExecutedTransaction,
26    /// Block number at which the transaction was submitted.
27    submission_height: BlockNumber,
28    /// Future notes that are expected to be created as a result of the transaction.
29    future_notes: Vec<(NoteDetails, NoteTag)>,
30    /// Information about note changes after the transaction execution.
31    note_updates: NoteUpdateTracker,
32    /// New note tags to be tracked.
33    new_tags: Vec<NoteTagRecord>,
34}
35
36impl TransactionStoreUpdate {
37    /// Creates a new [`TransactionStoreUpdate`] instance populated with all relevant note data.
38    ///
39    /// # Arguments
40    /// - `executed_transaction`: The executed transaction details.
41    /// - `submission_height`: The block number at which the transaction was submitted.
42    /// - `note_updates`: The note updates that need to be applied to the store after the
43    ///   transaction execution.
44    /// - `future_notes`: Notes expected to be received in follow-up transactions (e.g. swap
45    ///   paybacks).
46    /// - `new_tags`: New note tags that need to be tracked because of created notes.
47    pub fn new(
48        executed_transaction: ExecutedTransaction,
49        submission_height: BlockNumber,
50        note_updates: NoteUpdateTracker,
51        future_notes: Vec<(NoteDetails, NoteTag)>,
52        new_tags: Vec<NoteTagRecord>,
53    ) -> Self {
54        Self {
55            executed_transaction,
56            submission_height,
57            future_notes,
58            note_updates,
59            new_tags,
60        }
61    }
62    /// Returns the executed transaction.
63    pub fn executed_transaction(&self) -> &ExecutedTransaction {
64        &self.executed_transaction
65    }
66
67    /// Returns the block number at which the transaction was submitted.
68    pub fn submission_height(&self) -> BlockNumber {
69        self.submission_height
70    }
71
72    /// Returns the future notes that should be tracked as a result of the transaction.
73    pub fn future_notes(&self) -> &[(NoteDetails, NoteTag)] {
74        &self.future_notes
75    }
76
77    /// Returns the note updates that need to be applied after the transaction execution.
78    pub fn note_updates(&self) -> &NoteUpdateTracker {
79        &self.note_updates
80    }
81
82    /// Returns the new tags that were created as part of the transaction.
83    pub fn new_tags(&self) -> &[NoteTagRecord] {
84        &self.new_tags
85    }
86}
87
88impl Serializable for TransactionStoreUpdate {
89    fn write_into<W: ByteWriter>(&self, target: &mut W) {
90        self.executed_transaction.write_into(target);
91        self.submission_height.write_into(target);
92        self.future_notes.write_into(target);
93    }
94}
95
96impl Deserializable for TransactionStoreUpdate {
97    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
98        let executed_transaction = ExecutedTransaction::read_from(source)?;
99        let submission_height = BlockNumber::read_from(source)?;
100        let future_notes = Vec::<(NoteDetails, NoteTag)>::read_from(source)?;
101
102        Ok(Self {
103            executed_transaction,
104            submission_height,
105            future_notes,
106            note_updates: NoteUpdateTracker::default(),
107            new_tags: Vec::new(),
108        })
109    }
110}