Skip to main content

miden_client/sync/
state_sync_update.rs

1use alloc::collections::{BTreeMap, BTreeSet};
2use alloc::vec::Vec;
3
4use miden_protocol::Word;
5use miden_protocol::account::{AccountDelta, AccountHeader, AccountId};
6use miden_protocol::block::{BlockHeader, BlockNumber};
7use miden_protocol::crypto::merkle::mmr::{InOrderIndex, MmrPeaks};
8use miden_protocol::note::{NoteId, Nullifier};
9use miden_protocol::transaction::TransactionId;
10
11use super::SyncSummary;
12use crate::account::Account;
13use crate::note::{NoteUpdateTracker, NoteUpdateType};
14use crate::rpc::domain::transaction::TransactionInclusion;
15use crate::transaction::{DiscardCause, TransactionRecord, TransactionStatus};
16
17// STATE SYNC UPDATE
18// ================================================================================================
19
20/// Contains all information needed to apply the update in the store after syncing with the node.
21#[derive(Default)]
22pub struct StateSyncUpdate {
23    /// The block number of the last block that was synced.
24    pub block_num: BlockNumber,
25    /// New blocks and authentication nodes.
26    pub block_updates: BlockUpdates,
27    /// New and updated notes to be upserted in the store.
28    pub note_updates: NoteUpdateTracker,
29    /// Committed and discarded transactions after the sync.
30    pub transaction_updates: TransactionUpdateTracker,
31    /// Public account updates and mismatched private accounts after the sync.
32    pub account_updates: AccountUpdates,
33}
34
35impl From<&StateSyncUpdate> for SyncSummary {
36    fn from(value: &StateSyncUpdate) -> Self {
37        let new_public_note_ids = value
38            .note_updates
39            .updated_input_notes()
40            .filter_map(|note_update| {
41                let note = note_update.inner();
42                if let NoteUpdateType::Insert = note_update.update_type() {
43                    Some(note.id())
44                } else {
45                    None
46                }
47            })
48            .collect();
49
50        let committed_note_ids: BTreeSet<NoteId> = value
51            .note_updates
52            .updated_input_notes()
53            .filter_map(|note_update| {
54                let note = note_update.inner();
55                if let NoteUpdateType::Update = note_update.update_type() {
56                    note.is_committed().then_some(note.id())
57                } else {
58                    None
59                }
60            })
61            .chain(value.note_updates.updated_output_notes().filter_map(|note_update| {
62                let note = note_update.inner();
63                if let NoteUpdateType::Update = note_update.update_type() {
64                    note.is_committed().then_some(note.id())
65                } else {
66                    None
67                }
68            }))
69            .collect();
70
71        let consumed_note_ids: BTreeSet<NoteId> = value
72            .note_updates
73            .updated_input_notes()
74            .filter_map(|note| note.inner().is_consumed().then_some(note.inner().id()))
75            .collect();
76
77        SyncSummary::new(
78            value.block_num,
79            new_public_note_ids,
80            committed_note_ids.into_iter().collect(),
81            consumed_note_ids.into_iter().collect(),
82            value
83                .account_updates
84                .updated_public_accounts()
85                .iter()
86                .map(PublicAccountUpdate::id)
87                .collect(),
88            value
89                .account_updates
90                .mismatched_private_accounts()
91                .iter()
92                .map(|(id, _)| *id)
93                .collect(),
94            value.transaction_updates.committed_transactions().map(|t| t.id).collect(),
95        )
96    }
97}
98
99/// Contains all the block information that needs to be added in the client's store after a sync.
100#[derive(Debug, Clone, Default)]
101pub struct BlockUpdates {
102    /// New block headers to be stored, keyed by block number. The value contains the block
103    /// header, a flag indicating whether the block contains notes relevant to the client, and
104    /// the MMR peaks for the block.
105    block_headers: BTreeMap<BlockNumber, (BlockHeader, bool, MmrPeaks)>,
106    /// New authentication nodes that are meant to be stored in order to authenticate block
107    /// headers.
108    new_authentication_nodes: Vec<(InOrderIndex, Word)>,
109}
110
111impl BlockUpdates {
112    /// Adds or updates a block header and its corresponding data in this [`BlockUpdates`].
113    ///
114    /// If the block header already exists (same block number), the `has_client_notes` flag is
115    /// OR-ed and the peaks are kept from the first insertion. Otherwise a new entry is added.
116    pub fn insert(
117        &mut self,
118        block_header: BlockHeader,
119        has_client_notes: bool,
120        peaks: MmrPeaks,
121        new_authentication_nodes: Vec<(InOrderIndex, Word)>,
122    ) {
123        debug_assert_eq!(
124            peaks.forest().num_leaves(),
125            block_header.block_num().as_usize(),
126            "MMR peaks stored for a block header must use that block number as the forest",
127        );
128
129        self.block_headers
130            .entry(block_header.block_num())
131            .and_modify(|(_, existing_has_notes, _)| {
132                *existing_has_notes |= has_client_notes;
133            })
134            .or_insert((block_header, has_client_notes, peaks));
135
136        self.new_authentication_nodes.extend(new_authentication_nodes);
137    }
138
139    /// Returns the new block headers to be stored, along with a flag indicating whether the block
140    /// contains notes that are relevant to the client and the MMR peaks for the block.
141    pub fn block_headers(&self) -> impl Iterator<Item = &(BlockHeader, bool, MmrPeaks)> {
142        self.block_headers.values()
143    }
144
145    /// Adds authentication nodes without an associated block header.
146    ///
147    /// This is used when a synced block is not stored (no relevant notes and not the chain tip)
148    /// but the MMR authentication nodes it produced must still be persisted so that the on-disk
149    /// state stays consistent with the in-memory `PartialMmr`.
150    pub fn extend_authentication_nodes(&mut self, nodes: Vec<(InOrderIndex, Word)>) {
151        self.new_authentication_nodes.extend(nodes);
152    }
153
154    /// Returns the new authentication nodes that are meant to be stored in order to authenticate
155    /// block headers.
156    pub fn new_authentication_nodes(&self) -> &[(InOrderIndex, Word)] {
157        &self.new_authentication_nodes
158    }
159}
160
161/// Contains transaction changes to apply to the store.
162#[derive(Default)]
163pub struct TransactionUpdateTracker {
164    /// Transactions that were committed in the block.
165    transactions: BTreeMap<TransactionId, TransactionRecord>,
166    /// Nullifier-to-account mappings from external transactions by tracked accounts.
167    external_nullifier_accounts: BTreeMap<Nullifier, AccountId>,
168}
169
170impl TransactionUpdateTracker {
171    /// Creates a new [`TransactionUpdateTracker`]
172    pub fn new(transactions: Vec<TransactionRecord>) -> Self {
173        let transactions =
174            transactions.into_iter().map(|tx| (tx.id, tx)).collect::<BTreeMap<_, _>>();
175
176        Self {
177            transactions,
178            external_nullifier_accounts: BTreeMap::new(),
179        }
180    }
181
182    /// Returns a reference to committed transactions.
183    pub fn committed_transactions(&self) -> impl Iterator<Item = &TransactionRecord> {
184        self.transactions
185            .values()
186            .filter(|tx| matches!(tx.status, TransactionStatus::Committed { .. }))
187    }
188
189    /// Returns a reference to discarded transactions.
190    pub fn discarded_transactions(&self) -> impl Iterator<Item = &TransactionRecord> {
191        self.transactions
192            .values()
193            .filter(|tx| matches!(tx.status, TransactionStatus::Discarded(_)))
194    }
195
196    /// Returns a mutable reference to pending transactions in the tracker.
197    fn mutable_pending_transactions(&mut self) -> impl Iterator<Item = &mut TransactionRecord> {
198        self.transactions
199            .values_mut()
200            .filter(|tx| matches!(tx.status, TransactionStatus::Pending))
201    }
202
203    /// Returns transaction IDs of all transactions that have been updated.
204    pub fn updated_transaction_ids(&self) -> impl Iterator<Item = TransactionId> {
205        self.committed_transactions()
206            .chain(self.discarded_transactions())
207            .map(|tx| tx.id)
208    }
209
210    /// Returns the account ID that consumed the given nullifier in an external transaction, if
211    /// available.
212    pub fn external_nullifier_account(&self, nullifier: &Nullifier) -> Option<AccountId> {
213        self.external_nullifier_accounts.get(nullifier).copied()
214    }
215
216    /// Applies the necessary state transitions to the [`TransactionUpdateTracker`] when a
217    /// transaction is included in a block.
218    pub fn apply_transaction_inclusion(
219        &mut self,
220        transaction_inclusion: &TransactionInclusion,
221        timestamp: u64,
222    ) {
223        if let Some(transaction) = self.transactions.get_mut(&transaction_inclusion.transaction_id)
224        {
225            transaction.commit_transaction(transaction_inclusion.block_num, timestamp);
226            return;
227        }
228
229        // Fallback for transactions with unauthenticated input notes: the node
230        // authenticates these notes during processing, which changes the transaction
231        // ID. Match by account ID and pre-transaction state instead.
232        if let Some(transaction) = self.transactions.values_mut().find(|tx| {
233            tx.details.account_id == transaction_inclusion.account_id
234                && tx.details.init_account_state == transaction_inclusion.initial_state_commitment
235        }) {
236            transaction.commit_transaction(transaction_inclusion.block_num, timestamp);
237            return;
238        }
239
240        // No local transaction matched. This is an external transaction by a tracked account.
241        // Record the nullifier→account mappings so we can attribute note consumption to tracked
242        // accounts during nullifier processing.
243        for nullifier in &transaction_inclusion.nullifiers {
244            self.external_nullifier_accounts
245                .insert(*nullifier, transaction_inclusion.account_id);
246        }
247    }
248
249    /// Applies the necessary state transitions to the [`TransactionUpdateTracker`] when a the sync
250    /// height of the client is updated. This may result in stale or expired transactions.
251    pub fn apply_sync_height_update(
252        &mut self,
253        new_sync_height: BlockNumber,
254        tx_discard_delta: Option<u32>,
255    ) {
256        if let Some(tx_discard_delta) = tx_discard_delta {
257            self.discard_transaction_with_predicate(
258                |transaction| {
259                    transaction.details.submission_height
260                        < new_sync_height.checked_sub(tx_discard_delta).unwrap_or_default()
261                },
262                DiscardCause::Stale,
263            );
264        }
265
266        // NOTE: we check for <= new_sync height because at this point we would have committed the
267        // transaction otherwise
268        self.discard_transaction_with_predicate(
269            |transaction| transaction.details.expiration_block_num <= new_sync_height,
270            DiscardCause::Expired,
271        );
272    }
273
274    /// Applies the necessary state transitions to the [`TransactionUpdateTracker`] when a note is
275    /// nullified. this may result in transactions being discarded because they were processing the
276    /// nullified note.
277    pub fn apply_input_note_nullified(&mut self, input_note_nullifier: Nullifier) {
278        self.discard_transaction_with_predicate(
279            |transaction| {
280                // Check if the note was being processed by a local transaction that didn't end up
281                // being committed so it should be discarded
282                transaction
283                    .details
284                    .input_note_nullifiers
285                    .contains(&input_note_nullifier.as_word())
286            },
287            DiscardCause::InputConsumed,
288        );
289    }
290
291    /// Discards transactions that have the same initial account state as the provided one.
292    pub fn apply_invalid_initial_account_state(&mut self, invalid_account_state: Word) {
293        self.discard_transaction_with_predicate(
294            |transaction| transaction.details.init_account_state == invalid_account_state,
295            DiscardCause::DiscardedInitialState,
296        );
297    }
298
299    /// Discards transactions that match the predicate and also applies the new invalid account
300    /// states
301    fn discard_transaction_with_predicate<F>(&mut self, predicate: F, discard_cause: DiscardCause)
302    where
303        F: Fn(&TransactionRecord) -> bool,
304    {
305        let mut new_invalid_account_states = vec![];
306
307        for transaction in self.mutable_pending_transactions() {
308            // Discard transactions, and also push the invalid account state if the transaction
309            // got correctly discarded
310            // NOTE: previous updates in a chain of state syncs could have committed a transaction,
311            // so we need to check that `discard_transaction` returns `true` here (aka, it got
312            // discarded from a valid state)
313            if predicate(transaction) && transaction.discard_transaction(discard_cause) {
314                new_invalid_account_states.push(transaction.details.final_account_state);
315            }
316        }
317
318        for state in new_invalid_account_states {
319            self.apply_invalid_initial_account_state(state);
320        }
321    }
322}
323
324// PUBLIC ACCOUNT UPDATE
325// ================================================================================================
326
327/// Represents an update to a single public account's state.
328///
329/// Small accounts that fit within the node's response threshold are sent as `Full` replacements.
330/// Oversized accounts (too many storage map entries or vault assets) are sent as incremental
331/// `Delta` updates to avoid reconstructing the full account in memory.
332#[derive(Debug, Clone)]
333pub enum PublicAccountUpdate {
334    /// Full account state replacement.
335    Full(Account),
336    /// Incremental delta applied to the locally stored state.
337    Delta {
338        /// The new account header after the delta is applied.
339        new_header: AccountHeader,
340        /// The changes relative to the current locally-stored state.
341        delta: AccountDelta,
342    },
343}
344
345impl PublicAccountUpdate {
346    /// Returns the account ID for this update.
347    pub fn id(&self) -> AccountId {
348        match self {
349            Self::Full(account) => account.id(),
350            Self::Delta { new_header, .. } => new_header.id(),
351        }
352    }
353}
354
355// ACCOUNT UPDATES
356// ================================================================================================
357
358/// Contains account changes to apply to the store after a sync request.
359#[derive(Debug, Clone, Default)]
360#[allow(clippy::struct_field_names)]
361pub struct AccountUpdates {
362    /// Updated public accounts, either as full state replacements or incremental deltas.
363    updated_public_accounts: Vec<PublicAccountUpdate>,
364    /// Account commitments received from the network that don't match the currently
365    /// locally-tracked state of the private accounts.
366    ///
367    /// These updates may represent a stale account commitment (meaning that the latest local state
368    /// hasn't been committed). If this is not the case, the account may be locked until the state
369    /// is restored manually.
370    mismatched_private_accounts: Vec<(AccountId, Word)>,
371}
372
373impl AccountUpdates {
374    /// Creates a new instance of `AccountUpdates`.
375    pub fn new(
376        updated_public_accounts: Vec<PublicAccountUpdate>,
377        mismatched_private_accounts: Vec<(AccountId, Word)>,
378    ) -> Self {
379        Self {
380            updated_public_accounts,
381            mismatched_private_accounts,
382        }
383    }
384
385    /// Returns the updated public accounts.
386    pub fn updated_public_accounts(&self) -> &[PublicAccountUpdate] {
387        &self.updated_public_accounts
388    }
389
390    /// Returns the mismatched private accounts.
391    pub fn mismatched_private_accounts(&self) -> &[(AccountId, Word)] {
392        &self.mismatched_private_accounts
393    }
394
395    pub fn extend(&mut self, other: AccountUpdates) {
396        self.updated_public_accounts.extend(other.updated_public_accounts);
397        self.mismatched_private_accounts.extend(other.mismatched_private_accounts);
398    }
399}