Skip to main content

miden_client/sync/
state_sync.rs

1use alloc::boxed::Box;
2use alloc::collections::{BTreeMap, BTreeSet};
3use alloc::sync::Arc;
4use alloc::vec::Vec;
5use core::cmp::Ordering;
6
7use async_trait::async_trait;
8use futures::{StreamExt, TryStreamExt};
9use miden_protocol::Word;
10use miden_protocol::account::{Account, AccountHeader, AccountId, StorageSlotType};
11use miden_protocol::block::account_tree::AccountIdKey;
12use miden_protocol::block::{BlockHeader, BlockNumber, ValidatorConfig};
13use miden_protocol::crypto::merkle::MerklePath;
14use miden_protocol::crypto::merkle::mmr::{InOrderIndex, MmrDelta, PartialMmr};
15use miden_protocol::note::{NoteId, NoteTag, Nullifier};
16use miden_protocol::protocol_config::ProtocolConfig;
17use tracing::info;
18
19use super::state_sync_update::{TransactionUpdateTracker, build_account_patch};
20use super::{
21    AccountUpdates,
22    NoteObserver,
23    PartialBlockchainUpdates,
24    PublicAccountUpdate,
25    StateSyncUpdate,
26};
27use crate::ClientError;
28use crate::note::{NoteConsumption, NoteUpdateTracker};
29use crate::rpc::domain::account::{
30    AccountDetails,
31    AccountProof,
32    AccountStorageMapDetails,
33    GetAccountRequest,
34    StorageMapFetch,
35    VaultFetch,
36};
37use crate::rpc::domain::note::{CommittedNote, FetchedNote, ResolvedSyncNotesBlock, SyncedNote};
38use crate::rpc::domain::sync::{ChainMmrInfo, SyncTarget};
39use crate::rpc::domain::transaction::TransactionRecord as RpcTransactionRecord;
40use crate::rpc::{AccountStateAt, NodeRpcClient, NoteContentFetch, RpcError};
41use crate::store::input_note_states::UnverifiedNoteState;
42use crate::store::{InputNoteRecord, OutputNoteRecord, StoreError};
43use crate::transaction::TransactionRecord;
44
45/// Maximum number of `get_account` requests kept in flight while syncing the state.
46const MAX_CONCURRENT_ACCOUNT_FETCHES: usize = 4;
47
48// STATE UPDATE DATA
49// ================================================================================================
50
51/// How a node snapshot of a public account should be reconciled against the local state.
52enum PublicAccountSync {
53    /// Node is newer — apply its state to the store.
54    Apply(Box<PublicAccountUpdate>),
55    /// Same nonce but different state — the local transaction lost the race and must be discarded.
56    Superseded,
57    /// Node is behind the local (potentially optimistic) state — leave the local state untouched.
58    Ignore,
59}
60
61/// Data fetched from the node needed to sync the client to the chain tip.
62///
63/// Aggregates the responses of `sync_chain_mmr`, `sync_notes`, `get_notes_by_id`, and
64/// `sync_transactions`. This may contain more data than a particular client needs to store — it is
65/// filtered and transformed into a [`StateSyncUpdate`] before being applied.
66struct FetchedSyncData {
67    /// MMR delta covering the full range from `current_block` to `chain_tip`.
68    mmr_delta: MmrDelta,
69    /// Chain tip block header.
70    chain_tip_header: BlockHeader,
71    /// Blocks with matching notes that the client is interested in, each note carrying its
72    /// attachments and, for a fetched public note, its body.
73    note_blocks: Vec<ResolvedSyncNotesBlock>,
74    /// Transaction records for the synced range, as returned by `sync_transactions`.
75    transactions: Vec<RpcTransactionRecord>,
76    /// The protocol configuration active at the chain tip. The node sends it when the sync starts
77    /// at genesis, or when the starting block and the chain tip commit to different configurations.
78    protocol_config: Option<ProtocolConfig>,
79}
80
81/// A note a watched account consumed, carrying what recovery needs to validate and attribute it.
82///
83/// Complements the note id (under which recovery keys these entries) from the node's `(nullifier,
84/// note_id)` reference with the consuming account and block.
85struct RecoverableConsumedNote {
86    nullifier: Nullifier,
87    consumer: AccountId,
88    block_num: BlockNumber,
89}
90
91/// A note block that must be authenticated after screening.
92///
93/// `observer_requires_block` preserves the [`NoteObserver::observe`] contract independently of
94/// whether any normally-tracked note in the block remains unspent at the end of the sync.
95struct RelevantNoteBlock {
96    block_header: BlockHeader,
97    mmr_path: MerklePath,
98    observer_requires_block: bool,
99}
100
101/// The two independent reasons a screened note block may be relevant.
102#[derive(Default)]
103struct NoteBlockRelevance {
104    has_client_note: bool,
105    observer_requires_block: bool,
106}
107
108impl NoteBlockRelevance {
109    fn is_relevant(&self) -> bool {
110        self.has_client_note || self.observer_requires_block
111    }
112}
113
114// SYNC REQUEST
115// ================================================================================================
116
117/// Bundles the client state needed to perform a sync operation.
118///
119/// The sync process uses these inputs to:
120/// - Request account commitment updates from the node for the provided accounts.
121/// - Filter which note inclusions the node returns based on the provided note tags.
122/// - Follow the lifecycle of every tracked note (input and output), transitioning them from pending
123///   to committed to consumed as the network state advances.
124/// - Track uncommitted transactions so they can be marked as committed when the node confirms them,
125///   or discarded when they become stale.
126///
127/// Use [`Client::build_sync_input()`](`crate::Client::build_sync_input()`) to build a default input
128/// from the client state, or construct this struct manually for custom sync scenarios.
129pub struct StateSyncInput {
130    /// Headers of the tracked accounts to follow during the sync.
131    pub accounts: Vec<AccountHeader>,
132    /// Note tags that the node uses to filter which note inclusions to return.
133    pub note_tags: BTreeSet<NoteTag>,
134    /// Input notes whose lifecycle should be followed during sync.
135    pub input_notes: Vec<InputNoteRecord>,
136    /// Output notes whose lifecycle should be followed during sync.
137    ///
138    /// Inclusion (committed) updates are derived from transaction sync, so the account that created
139    /// a note must be present in `accounts` for the note to transition to committed. The consumed
140    /// transition does not depend on this: nullifier sync detects it regardless.
141    pub output_notes: Vec<OutputNoteRecord>,
142    /// Transactions to track for commitment or discard during sync.
143    pub uncommitted_transactions: Vec<TransactionRecord>,
144}
145
146// SYNC CALLBACKS
147// ================================================================================================
148
149/// The action to be taken when a note update is received as part of the sync response.
150#[allow(clippy::large_enum_variant)]
151pub enum NoteUpdateAction {
152    /// The note commit update is relevant and the specified note should be marked as committed in
153    /// the store, storing its inclusion proof.
154    Commit(CommittedNote),
155    /// The public note is relevant and should be inserted into the store.
156    Insert(InputNoteRecord),
157    /// The note update is not relevant and should be discarded.
158    Discard,
159}
160
161#[async_trait(?Send)]
162pub trait OnNoteReceived {
163    /// Callback that gets executed when a new note is received as part of the sync response.
164    ///
165    /// It receives:
166    ///
167    /// - The committed note received from the network.
168    /// - An optional note record that corresponds to the state of the note in the network (only if
169    ///   the note is public).
170    ///
171    /// It returns an enum indicating the action to be taken for the received note update. Whether
172    /// the note updated should be committed, new public note inserted, or ignored.
173    async fn on_note_received(
174        &self,
175        committed_note: CommittedNote,
176        public_note: Option<InputNoteRecord>,
177    ) -> Result<NoteUpdateAction, ClientError>;
178}
179// STATE SYNC
180// ================================================================================================
181
182/// The state sync component encompasses the client's sync logic. It is then used to request updates
183/// from the node and apply them to the relevant elements. The updates are then returned and can be
184/// applied to the store to persist the changes.
185#[derive(Clone)]
186pub struct StateSync {
187    /// The RPC client used to communicate with the node.
188    rpc_api: Arc<dyn NodeRpcClient>,
189    /// Responsible for checking the relevance of notes and executing the [`OnNoteReceived`]
190    /// callback when a new note inclusion is received.
191    note_screener: Arc<dyn OnNoteReceived>,
192    /// Per-note observers (see [`NoteObserver`]), invoked *before* the screener verdict in
193    /// `note_state_sync`. Empty by default.
194    note_observers: Vec<Arc<dyn NoteObserver>>,
195    /// Number of blocks after which pending transactions are considered stale and discarded. If
196    /// `None`, there is no limit and transactions will be kept indefinitely.
197    tx_discard_delta: Option<u32>,
198    /// If true, queries the node for consumption of tracked unspent-note nullifiers each sync and
199    /// discards local transactions whose inputs were nullified.
200    sync_nullifiers: bool,
201    /// The validator set and quorum that the chain tip block header must be signed by. A sync
202    /// validates every `sync_chain_mmr` response against it.
203    validator_config: ValidatorConfig,
204}
205
206impl StateSync {
207    /// Creates a new instance of the state sync component.
208    ///
209    /// The nullifiers sync is enabled by default. To disable it, see
210    /// [`Self::disable_nullifier_sync`].
211    ///
212    /// # Arguments
213    ///
214    /// * `rpc_api` - The RPC client used to communicate with the node.
215    /// * `note_screener` - The note screener used to check the relevance of notes.
216    /// * `tx_discard_delta` - Number of blocks after which pending transactions are discarded.
217    /// * `validator_config` - The validator set and quorum the chain tip header must be signed by.
218    pub fn new(
219        rpc_api: Arc<dyn NodeRpcClient>,
220        note_screener: Arc<dyn OnNoteReceived>,
221        tx_discard_delta: Option<u32>,
222        validator_config: ValidatorConfig,
223    ) -> Self {
224        Self {
225            rpc_api,
226            note_screener,
227            note_observers: Vec::new(),
228            tx_discard_delta,
229            sync_nullifiers: true,
230            validator_config,
231        }
232    }
233
234    /// Attaches a [`NoteObserver`] to this sync component. Observers run in attachment order
235    /// *before* the screener verdict; failures are logged (tagged with [`NoteObserver::name`]) and
236    /// never abort sync.
237    #[must_use]
238    pub fn with_note_observer(mut self, observer: Arc<dyn NoteObserver>) -> Self {
239        self.note_observers.push(observer);
240        self
241    }
242
243    /// Disables the nullifier sync.
244    ///
245    /// When disabled, the component will not query the node for new nullifiers after each sync
246    /// step. This is useful for clients that don't need to track note consumption, such as faucets.
247    pub fn disable_nullifier_sync(&mut self) {
248        self.sync_nullifiers = false;
249    }
250
251    /// Enables the nullifier sync.
252    pub fn enable_nullifier_sync(&mut self) {
253        self.sync_nullifiers = true;
254    }
255
256    /// Runs each attached observer's `apply()` hook against `state_sync_update`. Called by the
257    /// orchestrator after [`Self::sync_state`] returns but before the caller persists the sync
258    /// update. Per-observer failures are logged (tagged with the observer's [`NoteObserver::name`])
259    /// and never abort the rest of the pass — symmetric with the per-note `observe()` dispatcher.
260    pub(crate) async fn run_apply_hooks(
261        &self,
262        state_sync_update: &StateSyncUpdate,
263    ) -> Result<(), ClientError> {
264        for observer in &self.note_observers {
265            crate::errors::log_observer_failure(
266                observer.name(),
267                "NoteObserver::apply",
268                observer.apply(state_sync_update).await,
269            );
270        }
271        Ok(())
272    }
273
274    /// Syncs the state of the client with the chain tip of the node, returning the updates that
275    /// should be applied to the store.
276    ///
277    /// Use [`Client::build_sync_input()`](`crate::Client::build_sync_input()`) to build the default
278    /// input, or assemble it manually for custom sync. The `current_partial_mmr` is taken by
279    /// mutable reference so callers can keep it in memory across syncs; it is only modified once
280    /// every check has passed.
281    ///
282    /// During the sync process, the following steps are performed:
283    /// 1. Fetch sync data from the node (MMR delta, note inclusions, transactions).
284    /// 2. Update account states (fetch updated public accounts, flag mismatched private ones).
285    /// 3. Screen note inclusions via the configured [`OnNoteReceived`] callback.
286    /// 4. Process transaction inclusions (commit local txs, record external consumers, discard
287    ///    stale/expired txs, commit output notes).
288    /// 5. Recover the public notes a tracked account consumed but the client never tracked.
289    /// 6. Detect consumed notes via nullifier sync (optional, see
290    ///    [`Self::disable_nullifier_sync`]).
291    /// 7. Advance the partial MMR to the chain tip and track the screened blocks that still hold an
292    ///    unspent note.
293    ///
294    /// Steps 1-2 are [`Self::fetch_state`], 3-5 [`Self::derive_state_updates`], 6
295    /// [`Self::fetch_nullifiers`] and 7 [`Self::build_update`]; each can be driven separately.
296    pub async fn sync_state(
297        &self,
298        current_partial_mmr: &mut PartialMmr,
299        input: StateSyncInput,
300    ) -> Result<StateSyncUpdate, ClientError> {
301        let block_num = block_num_from_forest(current_partial_mmr)?;
302
303        let mut chain_sync_data = self.fetch_state(block_num, input).await?;
304        self.derive_state_updates(&mut chain_sync_data).await?;
305        self.fetch_nullifiers(&mut chain_sync_data).await?;
306
307        // Work on a clone so any validation failure leaves `current_partial_mmr` untouched.
308        let mut working_mmr = current_partial_mmr.clone();
309        let update = Self::build_update(chain_sync_data, &mut working_mmr)?;
310        *current_partial_mmr = working_mmr;
311
312        Ok(update)
313    }
314
315    /// Fetches the node's view of everything that changed since `block_from`: the MMR delta, the
316    /// note inclusions, the transactions, and the account states.
317    ///
318    /// Every node call that does not depend on note screening happens here, so a caller can run
319    /// this concurrently with another sync's fetch. Interpreting the response is
320    /// [`Self::derive_state_updates`]'s, and the nullifier check [`Self::fetch_nullifiers`]'s. Both
321    /// run afterwards so a caller syncing more than one source can write the other source first,
322    /// and check nullifiers once across all of them.
323    pub async fn fetch_state(
324        &self,
325        block_from: BlockNumber,
326        input: StateSyncInput,
327    ) -> Result<ChainSyncData, ClientError> {
328        let StateSyncInput {
329            accounts,
330            note_tags,
331            input_notes,
332            output_notes,
333            uncommitted_transactions,
334        } = input;
335
336        let note_tags = Arc::new(note_tags);
337        let account_ids: Vec<AccountId> = accounts.iter().map(AccountHeader::id).collect();
338
339        let note_updates = NoteUpdateTracker::new(input_notes, output_notes);
340        let transaction_updates = TransactionUpdateTracker::new(uncommitted_transactions);
341        let mut account_updates = AccountUpdates::default();
342
343        let Some(sync_data) = self.fetch_sync_data(block_from, &account_ids, &note_tags).await?
344        else {
345            // No progress — already at the tip.
346            return Ok(ChainSyncData {
347                block_from,
348                advance: None,
349                superseded_states: Vec::new(),
350                note_updates,
351                transaction_updates,
352                account_updates,
353            });
354        };
355
356        let FetchedSyncData {
357            mmr_delta,
358            chain_tip_header,
359            note_blocks,
360            transactions,
361            protocol_config,
362        } = sync_data;
363
364        let new_commitments = derive_account_commitments(&transactions);
365        let superseded_states = self
366            .account_state_sync(
367                &mut account_updates,
368                &accounts,
369                &new_commitments,
370                block_from,
371                &chain_tip_header,
372            )
373            .await?;
374
375        Ok(ChainSyncData {
376            block_from,
377            advance: Some(ChainAdvance {
378                chain_tip_header,
379                mmr_delta,
380                note_blocks_awaiting_screening: note_blocks,
381                transactions,
382                relevant_note_blocks: Vec::new(),
383                protocol_config,
384            }),
385            superseded_states,
386            note_updates,
387            transaction_updates,
388            account_updates,
389        })
390    }
391
392    /// Turns the node's raw response into note and transaction updates.
393    ///
394    /// Discards the local transactions the node superseded, screens the received notes for
395    /// relevance, applies the transaction inclusions, and recovers the public notes the tracked
396    /// accounts consumed, fetching those by id.
397    pub async fn derive_state_updates(
398        &self,
399        chain_sync_data: &mut ChainSyncData,
400    ) -> Result<(), ClientError> {
401        let ChainSyncData {
402            advance,
403            superseded_states,
404            note_updates,
405            transaction_updates,
406            ..
407        } = chain_sync_data;
408
409        let Some(advance) = advance.as_mut() else {
410            return Ok(());
411        };
412
413        // Discard the local transactions whose result lost a same-nonce race against the network.
414        for superseded_state in core::mem::take(superseded_states) {
415            transaction_updates.apply_superseded_account_state(superseded_state);
416        }
417
418        advance.relevant_note_blocks = self
419            .screen_note_blocks(
420                core::mem::take(&mut advance.note_blocks_awaiting_screening),
421                note_updates,
422            )
423            .await?;
424
425        self.apply_transactions_and_nullifiers(
426            &advance.chain_tip_header,
427            &advance.transactions,
428            note_updates,
429            transaction_updates,
430        )?;
431
432        self.recover_consumed_public_notes(note_updates, &advance.transactions).await?;
433
434        Ok(())
435    }
436
437    /// Verifies the fetched chain data against `partial_mmr` and turns it into the update to apply
438    /// to the store.
439    ///
440    /// It applies the node's delta, checks the resulting peaks against the chain tip header's chain
441    /// commitment, and tracks the screened note blocks that still hold an unspent note.
442    pub fn build_update(
443        chain_sync_data: ChainSyncData,
444        partial_mmr: &mut PartialMmr,
445    ) -> Result<StateSyncUpdate, ClientError> {
446        let ChainSyncData {
447            block_from,
448            advance,
449            note_updates,
450            transaction_updates,
451            account_updates,
452            ..
453        } = chain_sync_data;
454
455        let mut partial_blockchain_updates = PartialBlockchainUpdates::default();
456
457        let Some(ChainAdvance {
458            chain_tip_header,
459            mmr_delta,
460            note_blocks_awaiting_screening,
461            relevant_note_blocks,
462            protocol_config,
463            ..
464        }) = advance
465        else {
466            // No progress — already at the tip.
467            return Ok(StateSyncUpdate::from_parts(
468                block_from,
469                partial_blockchain_updates,
470                note_updates,
471                transaction_updates,
472                account_updates,
473                None,
474            ));
475        };
476        // Check the note blocks have been screened before building the update
477        if !note_blocks_awaiting_screening.is_empty() {
478            return Err(ClientError::UnscreenedNoteBlocks);
479        }
480
481        let chain_tip = chain_tip_header.block_num();
482
483        Self::advance_mmr(
484            mmr_delta,
485            &chain_tip_header,
486            partial_mmr,
487            &mut partial_blockchain_updates,
488        )?;
489
490        let blocks_with_unspent_notes: BTreeSet<BlockNumber> =
491            note_updates.unspent_input_note_block_numbers().collect();
492
493        Self::validate_and_track_note_blocks(
494            relevant_note_blocks,
495            &blocks_with_unspent_notes,
496            partial_mmr,
497            &mut partial_blockchain_updates,
498        )?;
499
500        Ok(StateSyncUpdate::from_parts(
501            chain_tip,
502            partial_blockchain_updates,
503            note_updates,
504            transaction_updates,
505            account_updates,
506            protocol_config,
507        ))
508    }
509
510    /// Checks the node for nullifiers of every note `chain_sync_data` could have consumed.
511    ///
512    /// The query covers every note the tracker holds, including any the caller added from another
513    /// sync path — so a transport-delivered note consumed within one sync is reported as consumed
514    /// by that same sync.
515    ///
516    /// No-op when the nullifier sync is disabled (see [`Self::disable_nullifier_sync`]) or when the
517    /// node reported no progress, since there is no block range to query.
518    pub async fn fetch_nullifiers(
519        &self,
520        chain_sync_data: &mut ChainSyncData,
521    ) -> Result<(), ClientError> {
522        if !self.sync_nullifiers {
523            return Ok(());
524        }
525
526        let Some(chain_tip) = chain_sync_data
527            .advance
528            .as_ref()
529            .map(|advance| advance.chain_tip_header.block_num())
530        else {
531            return Ok(());
532        };
533
534        self.nullifiers_state_sync(
535            &mut chain_sync_data.note_updates,
536            &mut chain_sync_data.transaction_updates,
537            chain_tip,
538            chain_sync_data.block_from,
539        )
540        .await
541    }
542
543    /// Recovers public notes a watched account consumed, from the `consumed_note_refs` the node
544    /// attaches to its transactions. Fetches the body of each not-yet-tracked note by id and hands
545    /// it to [`NoteUpdateTracker::insert_consumed_public_note`]. Notes the node doesn't return are
546    /// skipped; a reference the node resolves to a private note is rejected as an invalid response.
547    async fn recover_consumed_public_notes(
548        &self,
549        note_updates: &mut NoteUpdateTracker,
550        transactions: &[RpcTransactionRecord],
551    ) -> Result<(), ClientError> {
552        let mut recoverable_consumed_notes: BTreeMap<NoteId, RecoverableConsumedNote> =
553            BTreeMap::new();
554        for tx in transactions {
555            for (nullifier, note_id) in tx.trusted_consumed_note_refs() {
556                recoverable_consumed_notes.insert(
557                    note_id,
558                    RecoverableConsumedNote {
559                        nullifier,
560                        consumer: tx.transaction_header.account_id(),
561                        block_num: tx.block_num,
562                    },
563                );
564            }
565        }
566        // Skip references whose note the client already tracks (e.g. discovered by tag), to avoid
567        // clobbering full-detail records and fetching bodies we already hold.
568        recoverable_consumed_notes.retain(|note_id, _| !note_updates.tracks_note(*note_id));
569
570        let note_ids: Vec<NoteId> = recoverable_consumed_notes.keys().copied().collect();
571        if note_ids.is_empty() {
572            return Ok(());
573        }
574
575        for fetched in self.rpc_api.get_notes_by_id(&note_ids).await? {
576            match fetched {
577                FetchedNote::Public(note, _) => {
578                    let Some(reference) = recoverable_consumed_notes.get(&note.id()) else {
579                        continue;
580                    };
581                    // Make sure the fetched body actually hashes to the nullifier the transaction
582                    // consumed, so a byzantine node can't attribute an unrelated note here.
583                    if note.nullifier() != reference.nullifier {
584                        return Err(RpcError::InvalidResponse(format!(
585                            "node returned note {} whose nullifier doesn't match the consumed reference",
586                            note.id()
587                        ))
588                        .into());
589                    }
590                    note_updates.insert_consumed_public_note(
591                        note,
592                        reference.consumer,
593                        reference.block_num,
594                    )?;
595                },
596                FetchedNote::Private(note_id, ..) => {
597                    return Err(RpcError::InvalidResponse(format!(
598                        "node returned private note {note_id} for a public consumed-note reference"
599                    ))
600                    .into());
601                },
602            }
603        }
604
605        Ok(())
606    }
607
608    /// Fetches the sync data from the node by calling the following endpoints:
609    /// 1. `sync_chain_mmr` — discovers the chain tip, gets the MMR delta and chain tip header.
610    /// 2. `sync_notes` — loops until the full range to the chain tip is covered (handles paginated
611    ///    responses).
612    /// 3. `get_notes_by_id` — fetches public note bodies, plus attachment content the sync response
613    ///    did not already carry.
614    /// 4. `sync_transactions` — gets transaction data for the full range.
615    ///
616    /// Returns `None` when the client is already at the chain tip (no progress).
617    async fn fetch_sync_data(
618        &self,
619        current_block_num: BlockNumber,
620        account_ids: &[AccountId],
621        note_tags: &Arc<BTreeSet<NoteTag>>,
622    ) -> Result<Option<FetchedSyncData>, ClientError> {
623        // Step 1: Fetch the MMR delta and chain tip header.
624        let chain_mmr_info = self
625            .rpc_api
626            .sync_chain_mmr(current_block_num, SyncTarget::CommittedChainTip)
627            .await?;
628        let chain_tip = chain_mmr_info.block_to;
629
630        // Validate the response covers the range we requested and is signed by the validator set.
631        Self::validate_chain_mmr_response(
632            &chain_mmr_info,
633            current_block_num,
634            &self.validator_config,
635        )?;
636
637        // No progress — already at the tip.
638        if chain_tip == current_block_num {
639            info!(block_num = %current_block_num, "Already at chain tip, nothing to sync.");
640            return Ok(None);
641        }
642
643        info!(
644            block_from = %current_block_num,
645            block_to = %chain_tip,
646            "Syncing state.",
647        );
648
649        // Steps 2 and 3: note inclusions and transaction records are independent given the chain
650        // tip, so both are driven concurrently and the first failure aborts the pass.
651        let (note_blocks, transaction_records) = futures::try_join!(
652            self.fetch_note_blocks(current_block_num + 1, chain_tip, note_tags),
653            self.fetch_transactions(current_block_num + 1, chain_tip, account_ids),
654        )?;
655
656        // Validate every returned note block falls in (current_block_num, chain_tip].
657        Self::validate_note_blocks_range(&note_blocks, current_block_num, chain_tip)?;
658
659        let note_count: usize = note_blocks.iter().map(|b| b.notes.len()).sum();
660        info!(
661            blocks_with_notes = note_blocks.len(),
662            notes = note_count,
663            "Fetched note sync data.",
664        );
665
666        Self::validate_transaction_records_range(
667            &transaction_records,
668            current_block_num,
669            chain_tip,
670        )?;
671
672        Ok(Some(FetchedSyncData {
673            mmr_delta: chain_mmr_info.mmr_delta,
674            chain_tip_header: chain_mmr_info.block_header,
675            note_blocks,
676            transactions: transaction_records,
677            protocol_config: chain_mmr_info.protocol_config,
678        }))
679    }
680
681    // HELPERS
682    // --------------------------------------------------------------------------------------------
683
684    /// Syncs the note inclusions matching `note_tags` over `[block_from, block_to]`, resolving the
685    /// body of every public note and the attachment content of every note that carries attachments.
686    async fn fetch_note_blocks(
687        &self,
688        block_from: BlockNumber,
689        block_to: BlockNumber,
690        note_tags: &BTreeSet<NoteTag>,
691    ) -> Result<Vec<ResolvedSyncNotesBlock>, ClientError> {
692        if note_tags.is_empty() {
693            return Ok(Vec::new());
694        }
695
696        self.rpc_api
697            .sync_notes_with_content(
698                block_from,
699                block_to,
700                note_tags,
701                NoteContentFetch::PublicDetailsAndAttachments,
702            )
703            .await
704            .map_err(ClientError::RpcError)
705    }
706
707    /// Syncs the transaction records of `account_ids` over `[block_from, block_to]`.
708    async fn fetch_transactions(
709        &self,
710        block_from: BlockNumber,
711        block_to: BlockNumber,
712        account_ids: &[AccountId],
713    ) -> Result<Vec<RpcTransactionRecord>, ClientError> {
714        if account_ids.is_empty() {
715            return Ok(Vec::new());
716        }
717
718        self.rpc_api
719            .sync_transactions(block_from, block_to, account_ids.to_vec())
720            .await
721            .map_err(ClientError::RpcError)
722    }
723
724    /// Validates that a `sync_chain_mmr` response covers the requested range and that the chain tip
725    /// block header carries the signatures of `validator_config`.
726    fn validate_chain_mmr_response(
727        chain_mmr_info: &ChainMmrInfo,
728        current_block_num: BlockNumber,
729        validator_config: &ValidatorConfig,
730    ) -> Result<(), ClientError> {
731        if chain_mmr_info.block_header.block_num() != chain_mmr_info.block_to {
732            return Err(ClientError::ChainValidationError(format!(
733                "sync_chain_mmr block_header.block_num ({}) does not match block_to ({})",
734                chain_mmr_info.block_header.block_num(),
735                chain_mmr_info.block_to
736            )));
737        }
738        if chain_mmr_info.block_from != current_block_num {
739            return Err(ClientError::ChainValidationError(format!(
740                "sync_chain_mmr block_from mismatch: expected {current_block_num}, got {}",
741                chain_mmr_info.block_from
742            )));
743        }
744        if chain_mmr_info.block_to < current_block_num {
745            return Err(ClientError::ChainValidationError(format!(
746                "sync_chain_mmr block_to ({}) is behind current block {current_block_num}",
747                chain_mmr_info.block_to
748            )));
749        }
750
751        // Check that the validator set signed the chain tip block header.
752        chain_mmr_info
753            .block_signatures
754            .verify_against(chain_mmr_info.block_header.commitment(), validator_config)
755            .map_err(|err| {
756                ClientError::ChainValidationError(format!(
757                    "chain tip block header {} does not carry valid validator signatures: {err}",
758                    chain_mmr_info.block_header.block_num()
759                ))
760            })?;
761
762        Ok(())
763    }
764
765    /// Validates that every block returned by `sync_notes` falls in the requested range
766    /// `(current_block_num, chain_tip]`.
767    fn validate_note_blocks_range(
768        note_blocks: &[ResolvedSyncNotesBlock],
769        current_block_num: BlockNumber,
770        chain_tip: BlockNumber,
771    ) -> Result<(), ClientError> {
772        for block in note_blocks {
773            let block_num = block.block_header.block_num();
774            if block_num <= current_block_num || block_num > chain_tip {
775                return Err(ClientError::ChainValidationError(format!(
776                    "sync_notes returned block {block_num} outside requested range ({current_block_num}, {chain_tip}]"
777                )));
778            }
779        }
780        Ok(())
781    }
782
783    /// Validates that every record returned by `sync_transactions` falls in the requested range
784    /// `(current_block_num, chain_tip]`.
785    fn validate_transaction_records_range(
786        records: &[RpcTransactionRecord],
787        current_block_num: BlockNumber,
788        chain_tip: BlockNumber,
789    ) -> Result<(), ClientError> {
790        for record in records {
791            let block_num = record.block_num;
792            if block_num <= current_block_num || block_num > chain_tip {
793                return Err(ClientError::ChainValidationError(format!(
794                    "sync_transactions returned block {block_num} outside requested range ({current_block_num}, {chain_tip}]"
795                )));
796            }
797        }
798        Ok(())
799    }
800
801    /// Applies the MMR delta and inserts the chain-tip leaf into the partial blockchain updates.
802    /// The delta excludes the chain-tip leaf because of the one-block lag in block header MMR
803    /// commitments, so the tip leaf has to be added separately.
804    ///
805    /// Before adding the chain-tip leaf, the post-delta peaks are checked against the chain tip
806    /// header's chain commitment to ensure the delta advanced the MMR to the expected state.
807    fn advance_mmr(
808        mmr_delta: MmrDelta,
809        chain_tip_header: &BlockHeader,
810        current_partial_mmr: &mut PartialMmr,
811        partial_blockchain_updates: &mut PartialBlockchainUpdates,
812    ) -> Result<(), ClientError> {
813        let mut new_authentication_nodes =
814            current_partial_mmr.apply(mmr_delta).map_err(StoreError::MmrError)?;
815        let new_peaks = current_partial_mmr.peaks();
816
817        // Verify that post-delta peaks match the block header's chain commitment. chain_commitment
818        // is the hash of MMR peaks for blocks 0..block_num-1, which is exactly the state after
819        // applying the delta.
820        let peaks_commitment = new_peaks.hash_peaks();
821        if peaks_commitment != chain_tip_header.chain_commitment() {
822            return Err(ClientError::ChainValidationError(format!(
823                "MMR peaks commitment is {} and does not match block header chain commitment {}",
824                peaks_commitment.to_hex(),
825                chain_tip_header.chain_commitment().to_hex()
826            )));
827        }
828
829        partial_blockchain_updates.new_peaks = new_peaks;
830
831        // Note: we add the chain tip leaf to our MMR, but we cannot prove that it is effectively
832        // the chain tip. In the current context of centralized trusted node, we assume it is valid.
833        // Eventually, we will be able to validate that the resulting MMR root is "canonical".
834        new_authentication_nodes.append(
835            &mut current_partial_mmr
836                .add(chain_tip_header.commitment(), false)
837                .map_err(StoreError::MmrError)?,
838        );
839
840        partial_blockchain_updates.insert(chain_tip_header.clone(), false);
841        partial_blockchain_updates.extend_authentication_nodes(new_authentication_nodes);
842
843        Ok(())
844    }
845
846    /// Screens each note block for relevance, returning those with client-relevant notes and their
847    /// authentication path from the `sync_notes` response.
848    ///
849    /// These are candidates only — whether a normally-tracked note survives unspent isn't known
850    /// until nullifiers are processed, so tracking is deferred to
851    /// [`Self::validate_and_track_note_blocks`]. Blocks explicitly requested by an observer retain
852    /// that requirement separately.
853    async fn screen_note_blocks(
854        &self,
855        note_blocks: Vec<ResolvedSyncNotesBlock>,
856        note_updates: &mut NoteUpdateTracker,
857    ) -> Result<Vec<RelevantNoteBlock>, ClientError> {
858        let mut relevant_blocks = Vec::new();
859
860        for block in note_blocks {
861            let relevance =
862                self.note_state_sync(note_updates, block.notes, &block.block_header).await?;
863
864            if relevance.is_relevant() {
865                relevant_blocks.push(RelevantNoteBlock {
866                    block_header: block.block_header,
867                    mmr_path: block.mmr_path,
868                    observer_requires_block: relevance.observer_requires_block,
869                });
870            }
871        }
872
873        Ok(relevant_blocks)
874    }
875
876    /// Authenticates every relevant note block, then retains only blocks holding an unspent note or
877    /// explicitly requested by an observer.
878    ///
879    /// A block which does not need to be retained is temporarily tracked so its header and MMR path
880    /// are still validated against the current peaks, then immediately untracked. This avoids
881    /// persisting its header and authentication nodes without accepting unauthenticated sync data.
882    ///
883    /// Requires `partial_mmr` to be at the chain tip forest that `relevant_blocks`' paths are
884    /// relative to, which [`Self::advance_mmr`] establishes and nothing else in the pass changes.
885    fn validate_and_track_note_blocks(
886        relevant_blocks: Vec<RelevantNoteBlock>,
887        blocks_with_unspent_notes: &BTreeSet<BlockNumber>,
888        partial_mmr: &mut PartialMmr,
889        partial_blockchain_updates: &mut PartialBlockchainUpdates,
890    ) -> Result<(), ClientError> {
891        let nodes_before: BTreeSet<InOrderIndex> = partial_mmr.nodes().map(|(k, _)| *k).collect();
892
893        for RelevantNoteBlock {
894            block_header,
895            mmr_path,
896            observer_requires_block,
897        } in relevant_blocks
898        {
899            let block_pos = block_header.block_num().as_usize();
900            let was_tracked = partial_mmr.is_tracked(block_pos);
901
902            // `track` is also the authentication step: it verifies the supplied path against the
903            // current peaks before mutating the partial MMR.
904            partial_mmr
905                .track(block_pos, block_header.commitment(), &mmr_path)
906                .map_err(StoreError::MmrError)?;
907
908            if observer_requires_block
909                || blocks_with_unspent_notes.contains(&block_header.block_num())
910            {
911                partial_blockchain_updates.insert(block_header, true);
912            } else if !was_tracked {
913                partial_mmr.untrack(block_pos);
914            }
915        }
916
917        // Diffed once for the whole batch, since tracked paths share internal nodes.
918        partial_blockchain_updates.extend_authentication_nodes(
919            partial_mmr
920                .nodes()
921                .filter(|(index, _)| !nodes_before.contains(index))
922                .map(|(index, value)| (*index, *value)),
923        );
924
925        Ok(())
926    }
927
928    /// Extends the note tracker with newly-observed nullifiers, applies transaction inclusions, and
929    /// walks each transaction to apply output-note inclusion proofs and mark same-batch-erased
930    /// output notes as consumed.
931    fn apply_transactions_and_nullifiers(
932        &self,
933        chain_tip_header: &BlockHeader,
934        transactions: &[RpcTransactionRecord],
935        note_updates: &mut NoteUpdateTracker,
936        transaction_updates: &mut TransactionUpdateTracker,
937    ) -> Result<(), ClientError> {
938        note_updates.extend_nullifiers(compute_ordered_nullifiers(transactions));
939
940        for record in transactions {
941            transaction_updates
942                .apply_transaction_inclusion(record, u64::from(chain_tip_header.timestamp())); //TODO: Change timestamps from u64 to u32
943        }
944        transaction_updates
945            .apply_sync_height_update(chain_tip_header.block_num(), self.tx_discard_delta);
946
947        for transaction in transactions {
948            // Transition tracked output notes to Committed using inclusion proofs from the
949            // transaction sync response. This covers output notes regardless of whether their tags
950            // were tracked in the note sync.
951            note_updates.apply_output_note_inclusion_proofs(&transaction.output_notes)?;
952
953            // Detect output notes erased by same-batch note erasure.
954            Self::mark_erased_notes_as_consumed(note_updates, transaction);
955        }
956
957        Ok(())
958    }
959
960    /// Marks output notes that were erased by same-batch note erasure as consumed.
961    ///
962    /// When a note is created and consumed in the same batch, note erasure removes it from the
963    /// block body. The node reports these as erased output notes in the transaction record (note ID
964    /// only, no inclusion proof). We mark them as consumed.
965    fn mark_erased_notes_as_consumed(
966        note_updates: &mut NoteUpdateTracker,
967        transaction: &RpcTransactionRecord,
968    ) {
969        for note_header in &transaction.erased_output_notes {
970            // Best-effort: ignore errors for notes not tracked by this client.
971            let _ = note_updates.mark_erased_note_as_consumed(note_header, transaction.block_num);
972        }
973    }
974
975    /// Compares the state of tracked accounts with the updates received from the node. The method
976    /// Updates the `account_updates` with the details of the accounts that need to be updated.
977    ///
978    /// The account updates might include:
979    /// * Public accounts that have been updated in the node (full or delta-based).
980    /// * Network accounts that have been updated in the node and are being tracked by the client.
981    /// * Private accounts that have been marked as mismatched because the current commitment
982    ///   doesn't match the one received from the node. The client will need to handle these cases
983    ///   as they could be a stale account state or a reason to lock the account.
984    ///
985    /// Returns the local states that were superseded by a same-nonce network transaction; the
986    /// caller must discard the transactions that produced them.
987    async fn account_state_sync(
988        &self,
989        account_updates: &mut AccountUpdates,
990        accounts: &[AccountHeader],
991        account_commitment_updates: &[(AccountId, Word)],
992        block_from: BlockNumber,
993        chain_tip_header: &BlockHeader,
994    ) -> Result<Vec<Word>, ClientError> {
995        // "Public" here includes both Public and Network accounts, since both have their state
996        // stored on-chain and follow the same sync path.
997        let (public_accounts, private_accounts): (Vec<_>, Vec<_>) =
998            accounts.iter().partition(|header| !header.id().is_private());
999
1000        let superseded_states = self
1001            .sync_public_accounts(
1002                account_updates,
1003                account_commitment_updates,
1004                &public_accounts,
1005                block_from,
1006                chain_tip_header,
1007            )
1008            .await?;
1009
1010        // If a private account commitment differs between the node and local then we verify the
1011        // commitment from the node before flagging the account as mismatched.
1012        let diverging_private_accounts: Vec<&AccountHeader> = private_accounts
1013            .into_iter()
1014            .filter(|header| {
1015                let local_commitment = header.to_commitment();
1016                account_commitment_updates
1017                    .iter()
1018                    .any(|(id, digest)| *id == header.id() && *digest != local_commitment)
1019            })
1020            .collect();
1021
1022        let proven_commitments: Vec<Option<Word>> =
1023            futures::stream::iter(diverging_private_accounts.iter().map(|header| {
1024                self.verify_private_account_mismatch(
1025                    header.id(),
1026                    header.to_commitment(),
1027                    chain_tip_header,
1028                )
1029            }))
1030            .buffered(MAX_CONCURRENT_ACCOUNT_FETCHES)
1031            .try_collect()
1032            .await?;
1033
1034        let mismatched_private_accounts: Vec<(AccountId, Word)> = diverging_private_accounts
1035            .iter()
1036            .zip(proven_commitments)
1037            .filter_map(|(header, proven_commitment)| {
1038                proven_commitment.map(|commitment| (header.id(), commitment))
1039            })
1040            .collect();
1041
1042        account_updates.extend(AccountUpdates::new(Vec::new(), mismatched_private_accounts));
1043
1044        Ok(superseded_states)
1045    }
1046
1047    /// Verifies a private account commitment against an account witness from the node.
1048    ///
1049    /// Assumes `local_commitment` is a private account commitment that diverges from the
1050    /// `sync_transactions` records.
1051    ///
1052    /// Fetches the account witness via `get_account` at `chain_tip_header`'s block and checks the
1053    /// root it computes against `chain_tip_header`'s account root.
1054    ///
1055    /// Returns `Some(proven_commitment)` only when the proven on-chain commitment differs from
1056    /// `local_commitment`.
1057    async fn verify_private_account_mismatch(
1058        &self,
1059        account_id: AccountId,
1060        local_commitment: Word,
1061        chain_tip_header: &BlockHeader,
1062    ) -> Result<Option<Word>, ClientError> {
1063        let chain_tip = chain_tip_header.block_num();
1064        let (proof_block_num, proof) = self
1065            .rpc_api
1066            .get_account(account_id, GetAccountRequest::new().at(AccountStateAt::Block(chain_tip)))
1067            .await?;
1068
1069        if proof_block_num != chain_tip {
1070            return Err(ClientError::ChainValidationError(format!(
1071                "get_account returned a proof at block {proof_block_num}, expected chain tip {chain_tip}"
1072            )));
1073        }
1074
1075        let (witness, _) = proof.into_parts();
1076        let witness_id = witness.id();
1077        let proven_commitment = witness.state_commitment();
1078        // Verifying the witness against the chain tip's account root ties the proven commitment to
1079        // the synced block.
1080        if witness.into_proof().compute_root() != chain_tip_header.account_root() {
1081            return Err(ClientError::ChainValidationError(format!(
1082                "account witness for {account_id} does not verify against the chain tip account root"
1083            )));
1084        }
1085
1086        // Check if the witness is for a different account at this prefix, the account is absent on
1087        // chain, or the proven commitment matches local.
1088        if witness_id != account_id
1089            || proven_commitment == Word::empty()
1090            || proven_commitment == local_commitment
1091        {
1092            return Ok(None);
1093        }
1094
1095        Ok(Some(proven_commitment))
1096    }
1097
1098    /// Queries the node for updated public accounts and populates `account_updates`.
1099    ///
1100    /// For each public account whose commitment changed, an updated snapshot is fetched with a
1101    /// single `get_account` call that requests every storage map and the vault.
1102    ///
1103    /// Accounts whose vault or maps are too large to fit in a single response fall back to the
1104    /// incremental [`PublicAccountUpdate::Delta`] path, which fetches vault and storage map updates
1105    /// over the synced block range.
1106    async fn sync_public_accounts(
1107        &self,
1108        account_updates: &mut AccountUpdates,
1109        commitment_updates: &[(AccountId, Word)],
1110        current_public_accounts: &[&AccountHeader],
1111        block_from: BlockNumber,
1112        chain_tip_header: &BlockHeader,
1113    ) -> Result<Vec<Word>, ClientError> {
1114        let local_headers: BTreeMap<AccountId, &AccountHeader> =
1115            current_public_accounts.iter().map(|header| (header.id(), *header)).collect();
1116
1117        // Tracked accounts whose local commitment diverges from the network's. `commitment_updates`
1118        // holds at most one entry per account, so no account is fetched twice.
1119        let diverging_accounts: Vec<(AccountId, &AccountHeader)> = commitment_updates
1120            .iter()
1121            .filter_map(|(id, commitment)| {
1122                let local_header = local_headers.get(id).copied()?;
1123                (local_header.to_commitment() != *commitment).then_some((*id, local_header))
1124            })
1125            .collect();
1126
1127        // Ordered fan-out: responses are folded in `commitment_updates` order regardless of
1128        // completion order, so the resulting updates do not depend on response timing.
1129        let synced_accounts: Vec<PublicAccountSync> =
1130            futures::stream::iter(diverging_accounts.iter().map(|(id, local_header)| {
1131                self.sync_public_account(*id, local_header, block_from, chain_tip_header)
1132            }))
1133            .buffered(MAX_CONCURRENT_ACCOUNT_FETCHES)
1134            .try_collect()
1135            .await?;
1136
1137        // Local states that lost a same-nonce race; their transactions must be discarded.
1138        let mut superseded_states = Vec::new();
1139        for ((_, local_header), synced_account) in diverging_accounts.iter().zip(synced_accounts) {
1140            match synced_account {
1141                PublicAccountSync::Apply(public_update) => {
1142                    account_updates.extend(AccountUpdates::new(vec![*public_update], Vec::new()));
1143                },
1144                PublicAccountSync::Superseded => {
1145                    superseded_states.push(local_header.to_commitment());
1146                },
1147                PublicAccountSync::Ignore => {},
1148            }
1149        }
1150
1151        Ok(superseded_states)
1152    }
1153
1154    // SYNC PUBLIC ACCOUNTS HELPERS
1155    // --------------------------------------------------------------------------------------------
1156
1157    /// Fetches an updated snapshot for a single public account and decides how to reconcile it
1158    /// against the local state.
1159    ///
1160    /// Must only be called when the local commitment for the account is known to differ from the
1161    /// network's, so an equal nonce always means a genuine fork.
1162    async fn sync_public_account(
1163        &self,
1164        account_id: AccountId,
1165        local_header: &AccountHeader,
1166        block_from: BlockNumber,
1167        chain_tip_header: &BlockHeader,
1168    ) -> Result<PublicAccountSync, ClientError> {
1169        let target_block_num = chain_tip_header.block_num();
1170
1171        // A single request fetches the full snapshot: every storage map's entries plus the vault,
1172        // with the storage layout discovered server-side.
1173        let (proof_block_num, proof) = self
1174            .rpc_api
1175            .get_account(
1176                account_id,
1177                GetAccountRequest::new()
1178                    .at(AccountStateAt::Block(target_block_num))
1179                    .with_storage(StorageMapFetch::All)
1180                    .with_vault(VaultFetch::Always),
1181            )
1182            .await
1183            .map_err(ClientError::RpcError)?;
1184
1185        let details =
1186            Self::validate_account_proof(proof, proof_block_num, account_id, chain_tip_header)?;
1187
1188        match details
1189            .header
1190            .nonce()
1191            .as_canonical_u64()
1192            .cmp(&local_header.nonce().as_canonical_u64())
1193        {
1194            // Node is behind us: our own transaction was committed yet (will expire naturally
1195            // eventually).
1196            Ordering::Less => return Ok(PublicAccountSync::Ignore),
1197            // Same height but different state: our transaction definitively lost, drop it.
1198            Ordering::Equal => return Ok(PublicAccountSync::Superseded),
1199            // Node moved past us: adopt its state, built below.
1200            Ordering::Greater => {},
1201        }
1202
1203        let vault_oversized = details.vault_details.too_many_assets;
1204        let any_map_oversized = details
1205            .storage_details
1206            .map_details
1207            .iter()
1208            .any(AccountStorageMapDetails::is_limit_exceeded);
1209
1210        // TODO: we can handle vault and storage-map oversize independently. Today any oversize
1211        // routes the whole account through the incremental patch path, which always fetches both
1212        // `sync_storage_maps` and `sync_account_vault`, even if not needed.
1213        let public_update = if vault_oversized || any_map_oversized {
1214            // Some part of the account is oversized — use incremental endpoints.
1215            self.build_patch_update(account_id, &details, block_from, proof_block_num)
1216                .await?
1217        } else {
1218            // The single response carries the full vault and every map's entries.
1219            let account = Account::try_from(&details).map_err(ClientError::RpcError)?;
1220            PublicAccountUpdate::Full(account)
1221        };
1222
1223        Ok(PublicAccountSync::Apply(Box::new(public_update)))
1224    }
1225
1226    /// Validates that a `get_account` proof is bound to the sync target `chain_tip_header`: it must
1227    /// be for the requested `account_id`, at the target block, and its witness must open under the
1228    /// target header's account root. Returns the account details on success.
1229    ///
1230    /// # Errors
1231    ///
1232    /// Returns [`ClientError::ChainValidationError`] if:
1233    /// - the proof is for a different block than the sync target.
1234    /// - the witness is for a different account than the requested one.
1235    /// - the witness does not open under the sync target header's account root.
1236    /// - the proof carries no account details. The node returns details for every public account,
1237    ///   so a missing value means the response is malformed.
1238    fn validate_account_proof(
1239        proof: AccountProof,
1240        proof_block_num: BlockNumber,
1241        account_id: AccountId,
1242        chain_tip_header: &BlockHeader,
1243    ) -> Result<AccountDetails, ClientError> {
1244        let target_block_num = chain_tip_header.block_num();
1245
1246        if proof_block_num != target_block_num {
1247            return Err(ClientError::ChainValidationError(format!(
1248                "get_account returned block {proof_block_num} but {target_block_num} was requested"
1249            )));
1250        }
1251
1252        let (witness, details) = proof.into_parts();
1253
1254        // The witness is internally consistent but not yet tied to the account we requested.
1255        if witness.id() != account_id {
1256            return Err(ClientError::ChainValidationError(format!(
1257                "get_account returned account {} but {account_id} was requested",
1258                witness.id()
1259            )));
1260        }
1261
1262        let account_key = AccountIdKey::from(account_id).as_word();
1263        let state_commitment = witness.state_commitment();
1264        witness
1265            .into_proof()
1266            .verify_presence(&account_key, &state_commitment, &chain_tip_header.account_root())
1267            .map_err(|err| {
1268                ClientError::ChainValidationError(format!(
1269                    "get_account witness for account {account_id} does not open under block \
1270                     {target_block_num} account root: {err}"
1271                ))
1272            })?;
1273
1274        details.ok_or_else(|| {
1275            ClientError::ChainValidationError(format!(
1276                "get_account returned no details for public account {account_id}"
1277            ))
1278        })
1279    }
1280
1281    /// Builds a [`PublicAccountUpdate::Patch`] by fetching incremental storage map and vault
1282    /// updates over the synced range and assembling the absolute [`AccountPatch`] from them.
1283    async fn build_patch_update(
1284        &self,
1285        account_id: AccountId,
1286        details: &AccountDetails,
1287        block_from: BlockNumber,
1288        block_to: BlockNumber,
1289    ) -> Result<PublicAccountUpdate, ClientError> {
1290        let value_slot_updates: Vec<(_, Word)> = details
1291            .storage_details
1292            .header
1293            .slots()
1294            .filter(|slot| slot.slot_type() == StorageSlotType::Value)
1295            .map(|slot| (slot.name().clone(), slot.value()))
1296            .collect();
1297
1298        // The lower bound is inclusive at the node, so request from `block_from + 1` to skip the
1299        // block whose state we already have.
1300        let map_info = self
1301            .rpc_api
1302            .sync_storage_maps(block_from + 1, block_to, account_id)
1303            .await
1304            .map_err(ClientError::RpcError)?;
1305        let vault_info = self
1306            .rpc_api
1307            .sync_account_vault(block_from + 1, block_to, account_id)
1308            .await
1309            .map_err(ClientError::RpcError)?;
1310
1311        let patch = build_account_patch(
1312            &details.header,
1313            value_slot_updates,
1314            map_info.map_entries,
1315            vault_info.vault_patch,
1316            details.code.clone(),
1317        )
1318        .map_err(StoreError::AccountPatchError)?;
1319
1320        Ok(PublicAccountUpdate::Patch {
1321            new_header: details.header.clone(),
1322            patch,
1323        })
1324    }
1325
1326    /// Applies the changes received from the sync response to the notes and transactions tracked by
1327    /// the client and updates the `note_updates` accordingly.
1328    ///
1329    /// This method uses the callbacks provided to the [`StateSync`] component to check if the
1330    /// updates received are relevant to the client.
1331    ///
1332    /// The note updates might include:
1333    /// * New notes that we received from the node and might be relevant to the client.
1334    /// * Tracked expected notes that were committed in the block.
1335    /// * Tracked notes that were being processed by a transaction that got committed.
1336    /// * Tracked notes that were nullified by an external transaction.
1337    ///
1338    /// Each [`SyncedNote`] is self-contained: inclusion proof and metadata from the sync record,
1339    /// attachments from the sync record or a `GetNotesById` follow-up, and the body from `details`.
1340    ///
1341    /// Attachments are stored on-chain for private and public notes alike, so they are applied to
1342    /// the record regardless of note type.
1343    async fn note_state_sync(
1344        &self,
1345        note_updates: &mut NoteUpdateTracker,
1346        notes: BTreeMap<NoteId, SyncedNote>,
1347        block_header: &BlockHeader,
1348    ) -> Result<NoteBlockRelevance, ClientError> {
1349        let mut relevance = NoteBlockRelevance::default();
1350
1351        for (_, mut note) in notes {
1352            // Observers run BEFORE the screener: they are a side-effect channel independent of the
1353            // Commit/Insert/Discard decision, and a failing screener must not rob them of the note.
1354            if !self.note_observers.is_empty() {
1355                for obs in &self.note_observers {
1356                    match obs.observe(&note).await {
1357                        Ok(true) => relevance.observer_requires_block = true,
1358                        Ok(false) => {},
1359                        Err(err) => {
1360                            tracing::warn!(
1361                                observer = obs.name(),
1362                                error = ?err,
1363                                "note observer failed; sync continues",
1364                            );
1365                        },
1366                    }
1367                }
1368            }
1369
1370            // For a public note, pair its fetched body with the inclusion proof and metadata from
1371            // the sync record (the single source of truth) to build the candidate record.
1372            let public_note = note.details.take().map(|details| {
1373                let state = UnverifiedNoteState {
1374                    metadata: note.metadata,
1375                    inclusion_proof: note.inclusion_proof.clone(),
1376                }
1377                .into();
1378                InputNoteRecord::new(details, note.attachments.clone(), None, state)
1379            });
1380
1381            let committed = note.into_committed_note();
1382
1383            match self.note_screener.on_note_received(committed, public_note).await? {
1384                NoteUpdateAction::Commit(committed_note) => {
1385                    // Only mark the downloaded block header as relevant if we are talking about an
1386                    // input note (output notes get marked as committed but we don't need the block
1387                    // for anything there)
1388                    relevance.has_client_note |= note_updates
1389                        .apply_committed_note_state_transitions(&committed_note, block_header)?;
1390                },
1391                NoteUpdateAction::Insert(public_note) => {
1392                    relevance.has_client_note = true;
1393
1394                    note_updates.apply_new_public_note(public_note, block_header)?;
1395                },
1396                NoteUpdateAction::Discard => {},
1397            }
1398        }
1399
1400        Ok(relevance)
1401    }
1402
1403    /// Collects the nullifier tags for the notes that were updated in the sync response and uses
1404    /// the `sync_nullifiers` endpoint to check if there are new nullifiers for these notes. It then
1405    /// processes the nullifiers to apply the state transitions on the note updates.
1406    ///
1407    /// The `transaction_updates` parameter will be updated to track the new discarded transactions.
1408    async fn nullifiers_state_sync(
1409        &self,
1410        note_updates: &mut NoteUpdateTracker,
1411        transaction_updates: &mut TransactionUpdateTracker,
1412        chain_tip: BlockNumber,
1413        current_block_num: BlockNumber,
1414    ) -> Result<(), ClientError> {
1415        // To receive information about added nullifiers, we reduce them to the higher 16 bits. Note
1416        // that besides filtering by nullifier prefixes, the node also filters by block number (it
1417        // only returns nullifiers from current_block_num + 1 until chain_tip).
1418
1419        // Check for new nullifiers for input notes that were updated
1420        let nullifiers_tags: Vec<u16> =
1421            note_updates.unspent_nullifiers().map(|nullifier| nullifier.prefix()).collect();
1422
1423        let mut new_nullifiers = self
1424            .rpc_api
1425            .sync_nullifiers(&nullifiers_tags, current_block_num + 1, chain_tip)
1426            .await?;
1427
1428        // Discard nullifiers that are newer than the current block (this might happen if the block
1429        // changes between the sync_state and the check_nullifier calls)
1430        new_nullifiers.retain(|update| update.block_num <= chain_tip);
1431
1432        // Match each nullifier update with the externally-tracked consumer account.
1433        let consumptions: Vec<NoteConsumption> = new_nullifiers
1434            .into_iter()
1435            .map(|update| NoteConsumption {
1436                external_consumer: transaction_updates
1437                    .external_nullifier_account(&update.nullifier),
1438                nullifier: update.nullifier,
1439                block_num: update.block_num,
1440            })
1441            .collect();
1442
1443        for consumption in consumptions {
1444            note_updates.apply_note_consumption(
1445                &consumption,
1446                transaction_updates.committed_transactions(),
1447            )?;
1448
1449            // Process nullifiers and track the updates of local tracked transactions that were
1450            // discarded because the notes that they were processing were nullified by an another
1451            // transaction.
1452            transaction_updates.apply_input_note_nullified(consumption.nullifier);
1453        }
1454
1455        Ok(())
1456    }
1457}
1458
1459// CHAIN SYNC DATA
1460// ================================================================================================
1461
1462/// The chain data a sync fetched from the node, before any of it has been verified against the
1463/// client's MMR or to the store.
1464///
1465/// Built by [`StateSync::fetch_state`], extended by [`StateSync::fetch_nullifiers`] and turned into
1466/// a [`StateSyncUpdate`] by [`StateSync::build_update`].
1467pub struct ChainSyncData {
1468    /// The chain tip the sync started from.
1469    pub(crate) block_from: BlockNumber,
1470    /// What the node reported beyond `block_from`, or `None` when the client was already at the
1471    /// chain tip.
1472    advance: Option<ChainAdvance>,
1473    /// Account states the node superseded, to be applied to the transaction updates.
1474    superseded_states: Vec<Word>,
1475    /// Notes as the sync found them. A caller that wrote notes of its own after the sync input was
1476    /// built has to track them here, or this sync's verdicts have no record to apply to.
1477    pub(crate) note_updates: NoteUpdateTracker,
1478    transaction_updates: TransactionUpdateTracker,
1479    account_updates: AccountUpdates,
1480}
1481
1482/// The part of a [`ChainSyncData`] that only exists when the node reported progress.
1483struct ChainAdvance {
1484    /// Header of the chain tip the sync advanced to.
1485    chain_tip_header: BlockHeader,
1486    /// MMR delta from `block_from` to the chain tip, excluding the chain-tip leaf.
1487    mmr_delta: MmrDelta,
1488    /// Note blocks as the node returned them. [`StateSync::derive_state_updates`] drains these into
1489    /// `relevant_note_blocks`, so this is empty by the time the update is built.
1490    note_blocks_awaiting_screening: Vec<ResolvedSyncNotesBlock>,
1491    /// Transaction records as the node returned them, read by [`StateSync::derive_state_updates`].
1492    transactions: Vec<RpcTransactionRecord>,
1493    /// Screened blocks holding a client-relevant note, each with its `sync_notes` MMR path.
1494    relevant_note_blocks: Vec<RelevantNoteBlock>,
1495    /// The protocol configuration active at `chain_tip_header`, when the node sent it.
1496    protocol_config: Option<ProtocolConfig>,
1497}
1498
1499// HELPERS
1500// ================================================================================================
1501
1502/// Returns the block number the given partial MMR is synced to.
1503pub(crate) fn block_num_from_forest(partial_mmr: &PartialMmr) -> Result<BlockNumber, ClientError> {
1504    Ok(u32::try_from(partial_mmr.forest().num_leaves().saturating_sub(1))
1505        .map_err(|_| ClientError::InvalidPartialMmrForest)?
1506        .into())
1507}
1508
1509/// Groups transaction records by `(account_id, block_num)`.
1510fn group_txs_by_account_block(
1511    transaction_records: &[RpcTransactionRecord],
1512) -> BTreeMap<(AccountId, BlockNumber), Vec<&RpcTransactionRecord>> {
1513    let mut groups: BTreeMap<(AccountId, BlockNumber), Vec<&RpcTransactionRecord>> =
1514        BTreeMap::new();
1515    for record in transaction_records {
1516        let account_id = record.transaction_header.account_id();
1517        groups.entry((account_id, record.block_num)).or_default().push(record);
1518    }
1519    groups
1520}
1521
1522/// Walks a group of transaction records in execution order.
1523///
1524/// Same-block transactions for the same account form an execution chain: each tx's
1525/// `final_state_commitment` is the next tx's `initial_state_commitment`. This finds the chain start
1526/// and walks forward, yielding each tx in execution order.
1527fn walk_execution_chain<'a>(
1528    txs: &'a [&'a RpcTransactionRecord],
1529) -> impl Iterator<Item = &'a RpcTransactionRecord> + 'a {
1530    let (self_loops, chained): (Vec<&RpcTransactionRecord>, Vec<&RpcTransactionRecord>) =
1531        txs.iter().copied().partition(|tx| {
1532            tx.transaction_header.initial_state_commitment()
1533                == tx.transaction_header.final_state_commitment()
1534        });
1535
1536    let final_states: BTreeSet<Word> = chained
1537        .iter()
1538        .map(|tx| tx.transaction_header.final_state_commitment())
1539        .collect();
1540
1541    let mut init_to_tx: BTreeMap<Word, &RpcTransactionRecord> = chained
1542        .iter()
1543        .map(|tx| (tx.transaction_header.initial_state_commitment(), *tx))
1544        .collect();
1545
1546    let start = chained
1547        .iter()
1548        .find(|tx| !final_states.contains(&tx.transaction_header.initial_state_commitment()))
1549        .copied();
1550
1551    assert!(start.is_some() || chained.is_empty(), "cannot walk cyclic execution chain");
1552
1553    let mut current =
1554        start.and_then(|tx| init_to_tx.remove(&tx.transaction_header.initial_state_commitment()));
1555    let mut self_loops_iter = self_loops.into_iter();
1556
1557    core::iter::from_fn(move || {
1558        if let Some(tx) = current {
1559            current = init_to_tx.remove(&tx.transaction_header.final_state_commitment());
1560            return Some(tx);
1561        }
1562        self_loops_iter.next()
1563    })
1564}
1565
1566/// Derives account commitment updates from transaction records.
1567///
1568/// For each unique account, returns the `final_state_commitment` from the final transaction with
1569/// the highest `block_num`.
1570fn derive_account_commitments(
1571    transaction_records: &[RpcTransactionRecord],
1572) -> Vec<(AccountId, Word)> {
1573    let mut latest_by_account: BTreeMap<AccountId, (BlockNumber, Word)> = BTreeMap::new();
1574
1575    for ((account_id, block_num), txs) in &group_txs_by_account_block(transaction_records) {
1576        let terminal_state = walk_execution_chain(txs)
1577            .last()
1578            .expect("account must have a final state")
1579            .transaction_header
1580            .final_state_commitment();
1581
1582        latest_by_account
1583            .entry(*account_id)
1584            .and_modify(|(existing_block, existing_state)| {
1585                if *block_num > *existing_block {
1586                    *existing_block = *block_num;
1587                    *existing_state = terminal_state;
1588                }
1589            })
1590            .or_insert((*block_num, terminal_state));
1591    }
1592
1593    latest_by_account
1594        .into_iter()
1595        .map(|(account_id, (_, state))| (account_id, state))
1596        .collect()
1597}
1598
1599/// Returns nullifiers ordered by consuming transaction position, per account.
1600///
1601/// Groups RPC transaction records by (`account_id`, `block_num`), chains them using
1602/// `initial_state_commitment` / `final_state_commitment`, and collects each transaction's input
1603/// note nullifiers in execution order. Nullifiers from the same account are in execution order;
1604/// ordering across different accounts is arbitrary.
1605fn compute_ordered_nullifiers(transaction_records: &[RpcTransactionRecord]) -> Vec<Nullifier> {
1606    let mut result = Vec::new();
1607
1608    for txs in group_txs_by_account_block(transaction_records).values() {
1609        for tx in walk_execution_chain(txs) {
1610            for commitment in tx.transaction_header.input_notes().iter() {
1611                result.push(commitment.nullifier());
1612            }
1613        }
1614    }
1615
1616    result
1617}
1618
1619#[cfg(all(test, feature = "testing"))]
1620mod tests {
1621    use alloc::collections::BTreeSet;
1622    use alloc::sync::Arc;
1623
1624    use async_trait::async_trait;
1625    use miden_protocol::account::Account;
1626    use miden_protocol::assembly::DefaultSourceManager;
1627    use miden_protocol::asset::{Asset, FungibleAsset};
1628    use miden_protocol::block::{BlockNumber, BlockSignatures};
1629    use miden_protocol::crypto::merkle::MerklePath;
1630    use miden_protocol::crypto::merkle::mmr::{Forest, InOrderIndex, PartialMmr};
1631    use miden_protocol::note::{
1632        Note,
1633        NoteAssets,
1634        NoteAttachment,
1635        NoteAttachments,
1636        NoteDetails,
1637        NoteHeader,
1638        NoteMetadata,
1639        NoteRecipient,
1640        NoteStorage,
1641        NoteTag,
1642        NoteType,
1643        PartialNoteMetadata,
1644    };
1645    use miden_protocol::testing::account_id::{
1646        ACCOUNT_ID_PRIVATE_FUNGIBLE_FAUCET,
1647        ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET,
1648        ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE,
1649        ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE,
1650        ACCOUNT_ID_SENDER,
1651    };
1652    use miden_protocol::transaction::{InputNotes, TransactionArgs, TransactionHeader};
1653    use miden_protocol::vm::AdviceMap;
1654    use miden_protocol::{EMPTY_WORD, Felt, Word, ZERO};
1655    use miden_standards::code_builder::CodeBuilder;
1656    use miden_standards::note::{NetworkAccountTarget, NoteExecutionHint};
1657    use miden_testing::{MockChainBuilder, MockTransactionInput};
1658
1659    use super::*;
1660    use crate::store::{OutputNoteRecord, OutputNoteState};
1661    use crate::test_utils::mock::MockRpcApi;
1662
1663    /// Mock note screener that discards all notes, for minimal test setup.
1664    struct MockScreener;
1665
1666    #[async_trait(?Send)]
1667    impl OnNoteReceived for MockScreener {
1668        async fn on_note_received(
1669            &self,
1670            _committed_note: CommittedNote,
1671            _public_note: Option<InputNoteRecord>,
1672        ) -> Result<NoteUpdateAction, ClientError> {
1673            Ok(NoteUpdateAction::Discard)
1674        }
1675    }
1676
1677    /// Observer that requires every matching note's block to remain tracked.
1678    struct AlwaysRelevantObserver;
1679
1680    #[async_trait(?Send)]
1681    impl NoteObserver for AlwaysRelevantObserver {
1682        fn name(&self) -> &'static str {
1683            "always-relevant"
1684        }
1685
1686        async fn observe(&self, _note: &SyncedNote) -> Result<bool, ClientError> {
1687            Ok(true)
1688        }
1689    }
1690
1691    /// The validator configuration committed by the mock chain's genesis block header.
1692    fn genesis_validator_config(mock_rpc: &MockRpcApi) -> ValidatorConfig {
1693        mock_rpc.mock_chain.read().block_header(0).validator_config().clone()
1694    }
1695
1696    /// The signatures the mock chain produced for `block_num`.
1697    fn block_signatures(mock_rpc: &MockRpcApi, block_num: BlockNumber) -> BlockSignatures {
1698        mock_rpc
1699            .mock_chain
1700            .read()
1701            .proven_blocks()
1702            .iter()
1703            .find(|block| block.header().block_num() == block_num)
1704            .expect("the mock chain contains the block")
1705            .signatures()
1706            .clone()
1707    }
1708
1709    fn empty() -> StateSyncInput {
1710        StateSyncInput {
1711            accounts: vec![],
1712            note_tags: BTreeSet::new(),
1713            input_notes: vec![],
1714            output_notes: vec![],
1715            uncommitted_transactions: vec![],
1716        }
1717    }
1718
1719    fn word(n: u64) -> miden_protocol::Word {
1720        [
1721            Felt::new(n).expect("test value should fit into the base field"),
1722            ZERO,
1723            ZERO,
1724            ZERO,
1725        ]
1726        .into()
1727    }
1728
1729    fn header_with_account_root(header: &BlockHeader, account_root: Word) -> BlockHeader {
1730        BlockHeader::new(
1731            header.prev_block_commitment(),
1732            header.block_num(),
1733            header.chain_commitment(),
1734            account_root,
1735            header.nullifier_root(),
1736            header.note_root(),
1737            header.tx_commitment(),
1738            header.validator_config().clone(),
1739            header.fee_parameters().clone(),
1740            header.protocol_config_commitment(),
1741            header.next_protocol_config().cloned(),
1742            header.timestamp(),
1743        )
1744    }
1745
1746    #[tokio::test]
1747    async fn sync_public_accounts_ignores_older_node_snapshot() {
1748        let mut builder = MockChainBuilder::new();
1749        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
1750        let rpc_api = MockRpcApi::new(builder.build().unwrap());
1751        let chain_tip_header = rpc_api.mock_chain.read().latest_block_header();
1752        let validator_config = genesis_validator_config(&rpc_api);
1753        let state_sync =
1754            StateSync::new(Arc::new(rpc_api), Arc::new(MockScreener), None, validator_config);
1755
1756        // Local state is at a higher nonce than the node's snapshot (our own tx isn't committed
1757        // there yet), so the node snapshot must be ignored.
1758        let local_header =
1759            AccountHeader::new(account.id(), Felt::from(2u32), EMPTY_WORD, EMPTY_WORD, EMPTY_WORD);
1760        let current_public_accounts = vec![&local_header];
1761        let commitment_updates = vec![(account.id(), account.to_commitment())];
1762        let mut account_updates = AccountUpdates::default();
1763
1764        let superseded = state_sync
1765            .sync_public_accounts(
1766                &mut account_updates,
1767                &commitment_updates,
1768                &current_public_accounts,
1769                BlockNumber::GENESIS,
1770                &chain_tip_header,
1771            )
1772            .await
1773            .unwrap();
1774
1775        assert!(
1776            account_updates.updated_public_accounts().is_empty(),
1777            "public account sync should ignore node snapshots that are older than local"
1778        );
1779        assert!(
1780            superseded.is_empty(),
1781            "an older node snapshot must not supersede the local state"
1782        );
1783    }
1784
1785    #[tokio::test]
1786    async fn sync_public_accounts_marks_same_nonce_mismatch_as_superseded() {
1787        let mut builder = MockChainBuilder::new();
1788        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
1789        let rpc_api = MockRpcApi::new(builder.build().unwrap());
1790        let chain_tip_header = rpc_api.mock_chain.read().latest_block_header();
1791        let validator_config = genesis_validator_config(&rpc_api);
1792        let state_sync =
1793            StateSync::new(Arc::new(rpc_api), Arc::new(MockScreener), None, validator_config);
1794
1795        // Local state is at the same nonce as the node's but with a different commitment: a fork
1796        // where the local transaction lost the race and must be discarded.
1797        let local_header =
1798            AccountHeader::new(account.id(), account.nonce(), EMPTY_WORD, EMPTY_WORD, EMPTY_WORD);
1799        let current_public_accounts = vec![&local_header];
1800        let commitment_updates = vec![(account.id(), account.to_commitment())];
1801        let mut account_updates = AccountUpdates::default();
1802
1803        let superseded = state_sync
1804            .sync_public_accounts(
1805                &mut account_updates,
1806                &commitment_updates,
1807                &current_public_accounts,
1808                BlockNumber::GENESIS,
1809                &chain_tip_header,
1810            )
1811            .await
1812            .unwrap();
1813
1814        assert!(
1815            account_updates.updated_public_accounts().is_empty(),
1816            "a same-nonce fork must not overwrite the account while its tx is still pending"
1817        );
1818        assert_eq!(
1819            superseded,
1820            vec![local_header.to_commitment()],
1821            "the superseded local state should be reported so its transaction is discarded"
1822        );
1823    }
1824
1825    // PRIVATE ACCOUNT LOCK VERIFICATION TESTS
1826    // --------------------------------------------------------------------------------------------
1827
1828    /// Verifies that `sync_transactions` records outside the requested range `(current, chain_tip]`
1829    /// are rejected with a `ChainValidationError`.
1830    #[test]
1831    fn validate_transaction_records_range_rejects_out_of_range_blocks() {
1832        let account_id: AccountId = ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET.try_into().unwrap();
1833        let current = BlockNumber::from(5u32);
1834        let chain_tip = BlockNumber::from(10u32);
1835
1836        StateSync::validate_transaction_records_range(
1837            &[make_tx_record(account_id, 7)],
1838            current,
1839            chain_tip,
1840        )
1841        .unwrap();
1842
1843        let result = StateSync::validate_transaction_records_range(
1844            &[make_tx_record(account_id, 11)],
1845            current,
1846            chain_tip,
1847        );
1848        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
1849
1850        let result = StateSync::validate_transaction_records_range(
1851            &[make_tx_record(account_id, 5)],
1852            current,
1853            chain_tip,
1854        );
1855        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
1856    }
1857
1858    /// A forged `sync_transactions` commitment must not lock the account when the witness proves
1859    /// the on-chain commitment still matches the local one.
1860    #[tokio::test]
1861    async fn verify_private_account_mismatch_ignores_forged_commitment() {
1862        let mut builder = MockChainBuilder::new();
1863        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
1864        let rpc_api = MockRpcApi::new(builder.build().unwrap());
1865        let chain_tip_header = rpc_api.mock_chain.read().latest_block_header();
1866        let on_chain_commitment = account.to_commitment();
1867        let validator_config = genesis_validator_config(&rpc_api);
1868        let state_sync =
1869            StateSync::new(Arc::new(rpc_api), Arc::new(MockScreener), None, validator_config);
1870
1871        let result = state_sync
1872            .verify_private_account_mismatch(account.id(), on_chain_commitment, &chain_tip_header)
1873            .await
1874            .unwrap();
1875
1876        assert!(
1877            result.is_none(),
1878            "an unproven commitment must not lock an account whose on-chain state matches local"
1879        );
1880    }
1881
1882    /// When the witness proves a commitment that differs from the local one, the account is
1883    /// reported as mismatched with the proven commitment.
1884    #[tokio::test]
1885    async fn verify_private_account_mismatch_reports_proven_divergence() {
1886        let mut builder = MockChainBuilder::new();
1887        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
1888        let rpc_api = MockRpcApi::new(builder.build().unwrap());
1889        let chain_tip_header = rpc_api.mock_chain.read().latest_block_header();
1890        let on_chain_commitment = account.to_commitment();
1891        let validator_config = genesis_validator_config(&rpc_api);
1892        let state_sync =
1893            StateSync::new(Arc::new(rpc_api), Arc::new(MockScreener), None, validator_config);
1894        let stale_local_commitment = word(0xdead_beef);
1895
1896        let result = state_sync
1897            .verify_private_account_mismatch(
1898                account.id(),
1899                stale_local_commitment,
1900                &chain_tip_header,
1901            )
1902            .await
1903            .unwrap();
1904
1905        assert_eq!(
1906            result,
1907            Some(on_chain_commitment),
1908            "a proven divergence should return the proven commitment to lock with"
1909        );
1910    }
1911
1912    /// A witness that doesn't verify against the chain tip's account root is a misbehaving node and
1913    /// must abort the sync rather than lock the account.
1914    #[tokio::test]
1915    async fn verify_private_account_mismatch_rejects_unverifiable_proof() {
1916        let mut builder = MockChainBuilder::new();
1917        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
1918        let rpc_api = MockRpcApi::new(builder.build().unwrap());
1919        let real_header = rpc_api.mock_chain.read().latest_block_header();
1920        let validator_config = genesis_validator_config(&rpc_api);
1921        let state_sync =
1922            StateSync::new(Arc::new(rpc_api), Arc::new(MockScreener), None, validator_config);
1923
1924        // Same block number so the request resolves, but a tampered account root the witness cannot
1925        // verify against.
1926        let tampered_header = BlockHeader::new(
1927            real_header.prev_block_commitment(),
1928            real_header.block_num(),
1929            real_header.chain_commitment(),
1930            word(0xbad0_bad0),
1931            real_header.nullifier_root(),
1932            real_header.note_root(),
1933            real_header.tx_commitment(),
1934            real_header.validator_config().clone(),
1935            real_header.fee_parameters().clone(),
1936            real_header.protocol_config_commitment(),
1937            real_header.next_protocol_config().cloned(),
1938            real_header.timestamp(),
1939        );
1940
1941        let result = state_sync
1942            .verify_private_account_mismatch(
1943                account.id(),
1944                account.to_commitment(),
1945                &tampered_header,
1946            )
1947            .await;
1948        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
1949    }
1950
1951    /// A transaction committed after the sync target must remain pending. The account fetch is
1952    /// pinned to the target's pre-commit state.
1953    #[tokio::test]
1954    async fn sync_public_accounts_pins_account_fetch_to_sync_target() {
1955        let mut builder = MockChainBuilder::new();
1956        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
1957        let mut chain = builder.build().unwrap();
1958
1959        // The sync target predates this transaction.
1960        let sync_target_header = chain.latest_block_header();
1961        let tx = Box::pin(
1962            chain
1963                .build_transaction(MockTransactionInput::AccountId(account.id()))
1964                .build()
1965                .unwrap()
1966                .execute(),
1967        )
1968        .await
1969        .unwrap();
1970        let local_header = tx.final_account().clone();
1971        assert_ne!(local_header.to_commitment(), account.to_commitment());
1972        chain.add_pending_executed_transaction(&tx).unwrap();
1973
1974        let rpc_api = MockRpcApi::new(chain);
1975        // Commit the transaction after the target.
1976        rpc_api.prove_block();
1977        assert_eq!(
1978            rpc_api
1979                .mock_chain
1980                .read()
1981                .committed_account(account.id())
1982                .unwrap()
1983                .to_commitment(),
1984            local_header.to_commitment()
1985        );
1986        let validator_config = genesis_validator_config(&rpc_api);
1987        let state_sync =
1988            StateSync::new(Arc::new(rpc_api), Arc::new(MockScreener), None, validator_config);
1989
1990        let current_public_accounts = vec![&local_header];
1991        let commitment_updates = vec![(account.id(), account.to_commitment())];
1992        let mut account_updates = AccountUpdates::default();
1993
1994        let superseded = state_sync
1995            .sync_public_accounts(
1996                &mut account_updates,
1997                &commitment_updates,
1998                &current_public_accounts,
1999                BlockNumber::GENESIS,
2000                &sync_target_header,
2001            )
2002            .await
2003            .unwrap();
2004
2005        assert!(superseded.is_empty(), "the transaction must not be superseded");
2006        assert!(
2007            account_updates.updated_public_accounts().is_empty(),
2008            "the target state must not overwrite the local account"
2009        );
2010    }
2011
2012    /// Builds an honest `get_account` response for `account_id`.
2013    async fn get_account_proof(
2014        rpc_api: &MockRpcApi,
2015        account_id: AccountId,
2016    ) -> (BlockNumber, AccountProof) {
2017        rpc_api
2018            .get_account(
2019                account_id,
2020                GetAccountRequest::new()
2021                    .with_storage(StorageMapFetch::All)
2022                    .with_vault(VaultFetch::Always),
2023            )
2024            .await
2025            .unwrap()
2026    }
2027
2028    /// `validate_account_proof` rejects a proof whose account differs from the requested one.
2029    #[tokio::test]
2030    async fn validate_account_proof_rejects_mismatched_account() {
2031        let mut builder = MockChainBuilder::new();
2032        let account_a = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
2033        let account_b = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
2034        let rpc_api = MockRpcApi::new(builder.build().unwrap());
2035        let chain_tip_header = rpc_api.mock_chain.read().latest_block_header();
2036
2037        // An honest proof for B, validated as if A had been requested.
2038        let (proof_block_num, proof) = get_account_proof(&rpc_api, account_b.id()).await;
2039        let result = StateSync::validate_account_proof(
2040            proof,
2041            proof_block_num,
2042            account_a.id(),
2043            &chain_tip_header,
2044        );
2045
2046        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
2047    }
2048
2049    /// `validate_account_proof` rejects a witness that doesn't open under the target account root.
2050    #[tokio::test]
2051    async fn validate_account_proof_rejects_wrong_account_root() {
2052        let mut builder = MockChainBuilder::new();
2053        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
2054        let rpc_api = MockRpcApi::new(builder.build().unwrap());
2055        let chain_tip_header = rpc_api.mock_chain.read().latest_block_header();
2056        let wrong_header = header_with_account_root(&chain_tip_header, word(999));
2057
2058        // An honest proof for the account, validated against a header with a bogus account root.
2059        let (proof_block_num, proof) = get_account_proof(&rpc_api, account.id()).await;
2060        let result =
2061            StateSync::validate_account_proof(proof, proof_block_num, account.id(), &wrong_header);
2062
2063        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
2064    }
2065
2066    /// `validate_account_proof` rejects a proof that carries no account details.
2067    #[tokio::test]
2068    async fn validate_account_proof_rejects_missing_details() {
2069        let mut builder = MockChainBuilder::new();
2070        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
2071        let rpc_api = MockRpcApi::new(builder.build().unwrap());
2072        let chain_tip_header = rpc_api.mock_chain.read().latest_block_header();
2073
2074        // An otherwise honest proof, but with the account details stripped.
2075        let (proof_block_num, proof) = get_account_proof(&rpc_api, account.id()).await;
2076        let (witness, _) = proof.into_parts();
2077        let proof = AccountProof::new(witness, None).unwrap();
2078        let result = StateSync::validate_account_proof(
2079            proof,
2080            proof_block_num,
2081            account.id(),
2082            &chain_tip_header,
2083        );
2084
2085        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
2086    }
2087
2088    /// `validate_account_proof` rejects a proof reported for a block other than the sync target.
2089    #[tokio::test]
2090    async fn validate_account_proof_rejects_wrong_block() {
2091        let mut builder = MockChainBuilder::new();
2092        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
2093        let rpc_api = MockRpcApi::new(builder.build().unwrap());
2094        let chain_tip_header = rpc_api.mock_chain.read().latest_block_header();
2095
2096        // An honest proof, but reported at a block other than the target.
2097        let (proof_block_num, proof) = get_account_proof(&rpc_api, account.id()).await;
2098        let result = StateSync::validate_account_proof(
2099            proof,
2100            proof_block_num + 1,
2101            account.id(),
2102            &chain_tip_header,
2103        );
2104
2105        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
2106    }
2107
2108    // COMPUTE NULLIFIER TX ORDER TESTS
2109    // --------------------------------------------------------------------------------------------
2110
2111    mod compute_nullifiers_tests {
2112        use alloc::vec;
2113
2114        use miden_protocol::block::BlockNumber;
2115        use miden_protocol::note::Nullifier;
2116        use miden_protocol::transaction::{InputNoteCommitment, InputNotes, TransactionHeader};
2117
2118        use super::word;
2119        use crate::rpc::domain::transaction::TransactionRecord as RpcTransactionRecord;
2120
2121        fn make_rpc_tx(
2122            init_state: u64,
2123            final_state: u64,
2124            nullifier_vals: &[u64],
2125            block_number: u32,
2126        ) -> RpcTransactionRecord {
2127            let account_id = miden_protocol::account::AccountId::try_from(
2128                miden_protocol::testing::account_id::ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE,
2129            )
2130            .unwrap();
2131
2132            let input_notes = InputNotes::new_unchecked(
2133                nullifier_vals
2134                    .iter()
2135                    .map(|v| InputNoteCommitment::from(Nullifier::from_raw(word(*v))))
2136                    .collect(),
2137            );
2138
2139            RpcTransactionRecord {
2140                block_num: BlockNumber::from(block_number),
2141                transaction_header: TransactionHeader::new(
2142                    account_id,
2143                    word(init_state),
2144                    word(final_state),
2145                    input_notes,
2146                    vec![],
2147                )
2148                .unwrap(),
2149                output_notes: vec![],
2150                erased_output_notes: vec![],
2151                consumed_note_refs: vec![],
2152            }
2153        }
2154
2155        #[test]
2156        fn chains_rpc_transactions_by_state_commitment() {
2157            // Chain: tx_a (state 1->2) -> tx_b (state 2->3) -> tx_c (state 3->4)
2158            //
2159            // Passed in reverse order to verify chaining uses state, not insertion order.
2160            let tx_a = make_rpc_tx(1, 2, &[10], 5);
2161            let tx_b = make_rpc_tx(2, 3, &[20], 5);
2162            let tx_c = make_rpc_tx(3, 4, &[30], 5);
2163
2164            let result = super::super::compute_ordered_nullifiers(&[tx_c, tx_a, tx_b]);
2165
2166            assert_eq!(result[0], Nullifier::from_raw(word(10)));
2167            assert_eq!(result[1], Nullifier::from_raw(word(20)));
2168            assert_eq!(result[2], Nullifier::from_raw(word(30)));
2169        }
2170
2171        #[test]
2172        fn groups_independently_by_account_and_block() {
2173            // Account A, block 5: two chained txs.
2174            let tx_a1 = make_rpc_tx(1, 2, &[10], 5);
2175            let tx_a2 = make_rpc_tx(2, 3, &[20], 5);
2176
2177            // Account A, block 6: independent chain.
2178            let tx_a3 = make_rpc_tx(3, 4, &[30], 6);
2179
2180            // Account B, block 5: independent chain.
2181            let account_b = miden_protocol::account::AccountId::try_from(
2182                miden_protocol::testing::account_id::ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET,
2183            )
2184            .unwrap();
2185
2186            let tx_b1 = RpcTransactionRecord {
2187                block_num: BlockNumber::from(5u32),
2188                transaction_header: TransactionHeader::new(
2189                    account_b,
2190                    word(100),
2191                    word(200),
2192                    InputNotes::new_unchecked(vec![InputNoteCommitment::from(
2193                        Nullifier::from_raw(word(40)),
2194                    )]),
2195                    vec![],
2196                )
2197                .unwrap(),
2198                output_notes: vec![],
2199                erased_output_notes: vec![],
2200                consumed_note_refs: vec![],
2201            };
2202
2203            let result = super::super::compute_ordered_nullifiers(&[tx_a2, tx_b1, tx_a3, tx_a1]);
2204
2205            // Nullifiers are ordered by chain position within each (account, block) group. The
2206            // exact global indices depend on BTreeMap iteration order of the groups.
2207            let pos = |val: u64| -> usize {
2208                result.iter().position(|n| *n == Nullifier::from_raw(word(val))).unwrap()
2209            };
2210
2211            // Within the same group, chain order is preserved.
2212            assert!(pos(10) < pos(20)); // A, block 5: pos 0 < pos 1
2213            // Nullifiers from different groups are all present.
2214            assert!(result.contains(&Nullifier::from_raw(word(30)))); // A, block 6
2215            assert!(result.contains(&Nullifier::from_raw(word(40)))); // B, block 5
2216        }
2217
2218        #[test]
2219        fn multiple_nullifiers_per_transaction_are_consecutive() {
2220            // Single tx consuming 3 notes — all should appear consecutively.
2221            let tx = make_rpc_tx(1, 2, &[10, 20, 30], 5);
2222
2223            let result = super::super::compute_ordered_nullifiers(&[tx]);
2224
2225            assert_eq!(result.len(), 3);
2226            assert!(result.contains(&Nullifier::from_raw(word(10))));
2227            assert!(result.contains(&Nullifier::from_raw(word(20))));
2228            assert!(result.contains(&Nullifier::from_raw(word(30))));
2229        }
2230
2231        #[test]
2232        fn empty_input_returns_empty_vec() {
2233            let result = super::super::compute_ordered_nullifiers(&[]);
2234            assert!(result.is_empty());
2235        }
2236    }
2237
2238    // DERIVE ACCOUNT COMMITMENTS TESTS
2239    // --------------------------------------------------------------------------------------------
2240
2241    /// `derive_account_commitments` must walk the execution chain to get the final commitment when
2242    /// several transactions for the same account land in the same block.
2243    ///
2244    /// Test scenario:
2245    /// - Account A, block 5: chain 1 - 2 - 3 (older group; must be dominated by block 6).
2246    /// - Account A, block 6: chain 3 - 4 - 5 (final state = 5).
2247    /// - Account B, block 6: single tx 10 - 20 (final state = 20).
2248    #[test]
2249    fn derive_account_commitments_walks_chains_per_account() {
2250        let make_tx = |account: AccountId, init_state: u64, final_state: u64, block_num: u32| {
2251            RpcTransactionRecord {
2252                block_num: BlockNumber::from(block_num),
2253                transaction_header: TransactionHeader::new(
2254                    account,
2255                    word(init_state),
2256                    word(final_state),
2257                    InputNotes::new_unchecked(vec![]),
2258                    vec![],
2259                )
2260                .unwrap(),
2261                output_notes: vec![],
2262                erased_output_notes: vec![],
2263                consumed_note_refs: vec![],
2264            }
2265        };
2266
2267        let account_a: AccountId =
2268            ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE.try_into().unwrap();
2269        let account_b: AccountId = ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET.try_into().unwrap();
2270
2271        let tx_a_b5_1 = make_tx(account_a, 1, 2, 5);
2272        let tx_a_b5_2 = make_tx(account_a, 2, 3, 5);
2273        let tx_a_b6_1 = make_tx(account_a, 3, 4, 6);
2274        let tx_a_b6_2 = make_tx(account_a, 4, 5, 6);
2275        let tx_b_b6 = make_tx(account_b, 10, 20, 6);
2276
2277        // Insert transactions not ordered by execution order.
2278        let result = super::derive_account_commitments(&[
2279            tx_a_b6_1, tx_b_b6, tx_a_b5_2, tx_a_b6_2, tx_a_b5_1,
2280        ]);
2281
2282        assert_eq!(result.len(), 2, "one entry per account");
2283        assert!(
2284            result.contains(&(account_a, word(5))),
2285            "account A: must walk block 6's chain, not return block 5 or an intermediate",
2286        );
2287        assert!(
2288            result.contains(&(account_b, word(20))),
2289            "account B: must be resolved independently of account A",
2290        );
2291    }
2292
2293    // CONSUMED NOTE ORDERING INTEGRATION TESTS
2294    // --------------------------------------------------------------------------------------------
2295
2296    /// Mock note screener that commits all notes matching tracked input notes. This ensures
2297    /// committed notes get their inclusion proofs set during sync.
2298    struct CommitAllScreener;
2299
2300    #[async_trait(?Send)]
2301    impl OnNoteReceived for CommitAllScreener {
2302        async fn on_note_received(
2303            &self,
2304            committed_note: CommittedNote,
2305            _public_note: Option<InputNoteRecord>,
2306        ) -> Result<NoteUpdateAction, ClientError> {
2307            Ok(NoteUpdateAction::Commit(committed_note))
2308        }
2309    }
2310
2311    /// Builds a `MockChain` where 3 notes are consumed by chained transactions in the same block.
2312    ///
2313    /// Returns the chain, the account, and the 3 notes (in consumption order).
2314    async fn build_chain_with_chained_consume_txs() -> (miden_testing::MockChain, Account, [Note; 3])
2315    {
2316        let sender_id: AccountId = ACCOUNT_ID_SENDER.try_into().unwrap();
2317        let faucet_id: AccountId = ACCOUNT_ID_PRIVATE_FUNGIBLE_FAUCET.try_into().unwrap();
2318
2319        let mut builder = MockChainBuilder::new();
2320        let account = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
2321        let account_id = account.id();
2322
2323        let asset = Asset::from(FungibleAsset::new(faucet_id, 100u64).unwrap());
2324        let note1 = builder
2325            .add_p2id_note(sender_id, account_id, &[asset], NoteType::Public)
2326            .unwrap();
2327        let note2 = builder
2328            .add_p2id_note(sender_id, account_id, &[asset], NoteType::Public)
2329            .unwrap();
2330        let note3 = builder
2331            .add_p2id_note(sender_id, account_id, &[asset], NoteType::Public)
2332            .unwrap();
2333
2334        let mut chain = builder.build().unwrap();
2335        chain.prove_next_block().unwrap(); // block 1: makes genesis notes consumable
2336
2337        // Execute 3 chained consume transactions (state S0→S1→S2→S3).
2338        let mut current_account = account.clone();
2339        for note in [&note1, &note2, &note3] {
2340            let tx = Box::pin(
2341                chain
2342                    .build_transaction(MockTransactionInput::Account(current_account.clone()))
2343                    .unauthenticated_input_note(note.clone())
2344                    .build()
2345                    .unwrap()
2346                    .execute(),
2347            )
2348            .await
2349            .unwrap();
2350            current_account.apply_patch(tx.account_patch()).unwrap();
2351            chain.add_pending_executed_transaction(&tx).unwrap();
2352        }
2353
2354        chain.prove_next_block().unwrap(); // block 2: all 3 txs in one block
2355        (chain, account, [note1, note2, note3])
2356    }
2357
2358    /// Verifies that `consumed_tx_order` is correctly set when multiple chained transactions for
2359    /// the same account consume notes in the same block.
2360    #[tokio::test]
2361    async fn sync_state_sets_consumed_tx_order_for_chained_transactions() {
2362        use miden_protocol::note::NoteMetadata;
2363
2364        let (chain, account, [note1, note2, note3]) = build_chain_with_chained_consume_txs().await;
2365
2366        let mock_rpc = MockRpcApi::new(chain);
2367        let state_sync = StateSync::new(
2368            Arc::new(mock_rpc.clone()),
2369            Arc::new(CommitAllScreener),
2370            None,
2371            genesis_validator_config(&mock_rpc),
2372        );
2373
2374        let genesis_peaks =
2375            mock_rpc.get_mmr().peaks_at(Forest::new(1).expect("valid forest")).unwrap();
2376        let mut partial_mmr = PartialMmr::from_peaks(genesis_peaks);
2377
2378        let input_notes: Vec<InputNoteRecord> = [&note1, &note2, &note3]
2379            .into_iter()
2380            .map(|n| InputNoteRecord::from(n.clone()))
2381            .collect();
2382
2383        let note_tags: BTreeSet<NoteTag> =
2384            input_notes.iter().filter_map(|n| n.metadata().map(NoteMetadata::tag)).collect();
2385
2386        let account_id = account.id();
2387        let sync_input = StateSyncInput {
2388            accounts: vec![AccountHeader::from(&account)],
2389            note_tags,
2390            input_notes,
2391            output_notes: vec![],
2392            uncommitted_transactions: vec![],
2393        };
2394
2395        let update = state_sync.sync_state(&mut partial_mmr, sync_input).await.unwrap();
2396
2397        let updated_notes: Vec<_> = update.note_updates().updated_input_notes().collect();
2398
2399        let find_order = |details_commitment| -> Option<u32> {
2400            updated_notes
2401                .iter()
2402                .find(|n| n.inner().details_commitment() == details_commitment)
2403                .and_then(|n| n.consumed_tx_order())
2404        };
2405
2406        assert_eq!(find_order(note1.details_commitment()), Some(0), "note1 should have tx_order 0");
2407        assert_eq!(find_order(note2.details_commitment()), Some(1), "note2 should have tx_order 1");
2408        assert_eq!(find_order(note3.details_commitment()), Some(2), "note3 should have tx_order 2");
2409
2410        // Since there are no uncommitted_transactions, these notes were consumed by a tracked
2411        // account via external transactions. Verify that consumer_account is populated.
2412        for note in &updated_notes {
2413            let record = note.inner();
2414            assert!(record.is_consumed(), "note should be in a consumed state");
2415            assert_eq!(
2416                record.consumer_account(),
2417                Some(account_id),
2418                "externally-consumed notes by a tracked account should have consumer_account set",
2419            );
2420        }
2421    }
2422
2423    #[tokio::test]
2424    async fn sync_state_across_multiple_iterations_with_same_mmr() {
2425        // Setup: create a mock chain and advance it so there are blocks to sync.
2426        let mock_rpc = MockRpcApi::default();
2427        mock_rpc.advance_blocks(3);
2428        let chain_tip_1 = mock_rpc.get_chain_tip_block_num();
2429
2430        let state_sync = StateSync::new(
2431            Arc::new(mock_rpc.clone()),
2432            Arc::new(MockScreener),
2433            None,
2434            genesis_validator_config(&mock_rpc),
2435        );
2436
2437        // Build the initial PartialMmr from genesis (only 1 leaf).
2438        let genesis_peaks =
2439            mock_rpc.get_mmr().peaks_at(Forest::new(1).expect("valid forest")).unwrap();
2440        let mut partial_mmr = PartialMmr::from_peaks(genesis_peaks);
2441        assert_eq!(partial_mmr.forest().num_leaves(), 1);
2442
2443        // First sync
2444        let update = state_sync.sync_state(&mut partial_mmr, empty()).await.unwrap();
2445
2446        assert_eq!(update.block_num(), chain_tip_1);
2447        let forest_1 = partial_mmr.forest();
2448        // The MMR should contain one leaf per block (genesis + the new blocks).
2449        assert_eq!(forest_1.num_leaves(), chain_tip_1.as_u32() as usize + 1);
2450
2451        // Second sync
2452        mock_rpc.advance_blocks(2);
2453        let chain_tip_2 = mock_rpc.get_chain_tip_block_num();
2454
2455        let update = state_sync.sync_state(&mut partial_mmr, empty()).await.unwrap();
2456
2457        assert_eq!(update.block_num(), chain_tip_2);
2458        let forest_2 = partial_mmr.forest();
2459        assert!(forest_2 > forest_1);
2460        assert_eq!(forest_2.num_leaves(), chain_tip_2.as_u32() as usize + 1);
2461
2462        // Third sync (no new blocks)
2463        let update = state_sync.sync_state(&mut partial_mmr, empty()).await.unwrap();
2464
2465        assert_eq!(update.block_num(), chain_tip_2);
2466        assert_eq!(partial_mmr.forest(), forest_2);
2467    }
2468
2469    /// Builds a mock chain with a faucet that mints `num_blocks` notes, one per block. Returns the
2470    /// chain and the set of note tags for filtering.
2471    async fn build_chain_with_mint_notes(
2472        num_blocks: u64,
2473    ) -> (miden_testing::MockChain, BTreeSet<NoteTag>) {
2474        let mut builder = MockChainBuilder::new();
2475        let faucet = builder
2476            .add_existing_basic_faucet(
2477                miden_testing::Auth::BasicAuth {
2478                    auth_scheme: miden_protocol::account::auth::AuthScheme::Falcon512Poseidon2,
2479                },
2480                "TST",
2481                10_000,
2482                None,
2483            )
2484            .unwrap();
2485        let _target = builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
2486        let mut chain = builder.build().unwrap();
2487
2488        // Build a real recipient so its digest has a registered preimage in the advice map;
2489        // `mint_and_send` → `output_note::create` emits `NOTE_BEFORE_CREATED_EVENT`, whose host
2490        // handler decomposes the recipient digest through the advice map and fails with
2491        // `MalformedRecipientData` if the preimage isn't present.
2492        let note_script = CodeBuilder::new()
2493            .compile_note_script("@note_script\npub proc main\n    nop\nend")
2494            .unwrap();
2495        let note_recipient = NoteRecipient::new(
2496            Word::from([1u32, 2, 3, 4]),
2497            note_script,
2498            NoteStorage::new(vec![]).unwrap(),
2499        );
2500        let recipient = note_recipient.digest();
2501        // `add_output_note_recipient` populates the advice map with the recipient's preimage chain
2502        // (RECIPIENT → [SERIAL_SCRIPT_HASH, STORAGE_COMMITMENT], etc.).
2503        let note_details = NoteDetails::new(NoteAssets::new(vec![]).unwrap(), note_recipient);
2504        let mut recipient_args = TransactionArgs::new(AdviceMap::default());
2505        recipient_args.add_output_note_recipient(&note_details);
2506        let recipient_advice = recipient_args.advice_inputs().clone();
2507
2508        let tag = NoteTag::default();
2509        let mut faucet_account = faucet.clone();
2510        let mut note_tags = BTreeSet::new();
2511
2512        for i in 0..num_blocks {
2513            let amount = 100 + i;
2514            let source_manager = Arc::new(DefaultSourceManager::default());
2515            // `mint_and_send` consumes the fungible asset's ID and value words directly:
2516            // `[ASSET_ID, ASSET_VALUE, tag, note_type, RECIPIENT, pad(2)]`. Both words are derived
2517            // in Rust from the faucet's `AssetId`, which intrinsically carries the callback flag.
2518            let mint_asset = FungibleAsset::new(faucet_account.id(), amount).unwrap();
2519            let asset_id_word = mint_asset.id().to_word();
2520            let asset_value_word = mint_asset.to_value_word();
2521            let tx_script_code = format!(
2522                "
2523                @transaction_script
2524                pub proc main
2525                    push.{recipient}
2526                    push.{note_type}
2527                    push.{tag}
2528                    push.{asset_value}
2529                    push.{asset_id}
2530                    call.::miden::standards::faucets::fungible::mint_and_send
2531                    dropw dropw dropw dropw
2532                end
2533                ",
2534                recipient = recipient,
2535                note_type = NoteType::Private as u8,
2536                tag = u32::from(tag),
2537                asset_value = asset_value_word,
2538                asset_id = asset_id_word,
2539            );
2540            let tx_script = CodeBuilder::with_source_manager(source_manager.clone())
2541                .compile_tx_script(tx_script_code)
2542                .unwrap();
2543            let tx = Box::pin(
2544                chain
2545                    .build_transaction(miden_testing::MockTransactionInput::Account(
2546                        faucet_account.clone(),
2547                    ))
2548                    .extend_advice_inputs(recipient_advice.clone())
2549                    .tx_script(tx_script)
2550                    .with_source_manager(source_manager)
2551                    .build()
2552                    .unwrap()
2553                    .execute(),
2554            )
2555            .await
2556            .unwrap();
2557
2558            for output_note in tx.output_notes().iter() {
2559                note_tags.insert(output_note.metadata().tag());
2560            }
2561
2562            faucet_account.apply_patch(tx.account_patch()).unwrap();
2563            chain.add_pending_executed_transaction(&tx).unwrap();
2564            chain.prove_next_block().unwrap();
2565        }
2566
2567        (chain, note_tags)
2568    }
2569
2570    /// An observer's `true` result retains a note block even when the screener discards the note
2571    /// and the regular note tracker therefore has no live note in that block.
2572    #[tokio::test]
2573    async fn observer_relevance_persists_discarded_note_block() {
2574        let (chain, note_tags) = build_chain_with_mint_notes(2).await;
2575        let mock_rpc = MockRpcApi::new(chain);
2576        let chain_tip = mock_rpc.get_chain_tip_block_num();
2577
2578        let genesis_peaks =
2579            mock_rpc.get_mmr().peaks_at(Forest::new(1).expect("valid forest")).unwrap();
2580        let mut partial_mmr = PartialMmr::from_peaks(genesis_peaks);
2581
2582        let validator_config = genesis_validator_config(&mock_rpc);
2583        let mut input = empty();
2584        let state_sync =
2585            StateSync::new(Arc::new(mock_rpc), Arc::new(MockScreener), None, validator_config)
2586                .with_note_observer(Arc::new(AlwaysRelevantObserver));
2587        input.note_tags = note_tags;
2588
2589        let update = state_sync.sync_state(&mut partial_mmr, input).await.unwrap();
2590        let observed_non_tip_block = BlockNumber::from(1u32);
2591
2592        assert!(
2593            update.partial_blockchain_updates().block_headers_to_store(chain_tip).any(
2594                |(header, is_relevant)| {
2595                    header.block_num() == observed_non_tip_block && *is_relevant
2596                }
2597            ),
2598            "an observer-relevant block must be staged as relevant"
2599        );
2600        assert!(
2601            partial_mmr.is_tracked(observed_non_tip_block.as_usize()),
2602            "an observer-relevant block must remain tracked in the partial MMR"
2603        );
2604    }
2605
2606    /// Verifies that the sync correctly processes notes committed in multiple blocks (batched
2607    /// `SyncNotes` response) and tracks their blocks in the partial MMR.
2608    ///
2609    /// This test creates a faucet and mints notes in separate blocks (blocks 1, 2, 3),
2610    /// so `sync_notes` returns multiple `SyncNotesBlock`s. It then verifies:
2611    /// - The MMR is advanced to the chain tip
2612    /// - Blocks containing relevant notes are tracked in the partial MMR via `track()`
2613    /// - Note inclusion proofs are set correctly
2614    /// - Block headers for note blocks are stored
2615    #[tokio::test]
2616    async fn sync_state_tracks_note_blocks_in_mmr() {
2617        let (chain, note_tags) = build_chain_with_mint_notes(3).await;
2618        let mock_rpc = MockRpcApi::new(chain);
2619        let chain_tip = mock_rpc.get_chain_tip_block_num();
2620
2621        // Verify the mock returns notes across multiple blocks.
2622        let note_blocks = mock_rpc
2623            .sync_notes(BlockNumber::from(0u32), chain_tip, &note_tags)
2624            .await
2625            .unwrap();
2626        assert!(
2627            note_blocks.len() >= 2,
2628            "expected notes in multiple blocks, got {}",
2629            note_blocks.len()
2630        );
2631
2632        // Collect the block numbers that have notes.
2633        let note_block_nums: BTreeSet<BlockNumber> =
2634            note_blocks.iter().map(|b| b.block_header.block_num()).collect();
2635
2636        // Test that fetch_sync_data returns note blocks with valid MMR paths that can be used to
2637        // track blocks in the partial MMR.
2638        let state_sync = StateSync::new(
2639            Arc::new(mock_rpc.clone()),
2640            Arc::new(MockScreener),
2641            None,
2642            genesis_validator_config(&mock_rpc),
2643        );
2644
2645        let genesis_peaks =
2646            mock_rpc.get_mmr().peaks_at(Forest::new(1).expect("valid forest")).unwrap();
2647        let mut partial_mmr = PartialMmr::from_peaks(genesis_peaks);
2648
2649        let sync_data = state_sync
2650            .fetch_sync_data(BlockNumber::GENESIS, &[], &Arc::new(note_tags.clone()))
2651            .await
2652            .unwrap()
2653            .expect("should have progressed past genesis");
2654
2655        // Should have advanced to the chain tip.
2656        assert_eq!(sync_data.chain_tip_header.block_num(), chain_tip);
2657        assert!(!sync_data.note_blocks.is_empty(), "should have note blocks");
2658
2659        // Apply the MMR delta and add the chain tip block.
2660        let _auth_nodes: Vec<(InOrderIndex, Word)> =
2661            partial_mmr.apply(sync_data.mmr_delta).map_err(StoreError::MmrError).unwrap();
2662        partial_mmr
2663            .add(sync_data.chain_tip_header.commitment(), false)
2664            .expect("chain tip should append to the partial MMR");
2665
2666        assert_eq!(partial_mmr.forest().num_leaves(), chain_tip.as_u32() as usize + 1);
2667
2668        // Track each note block using the MMR path from the sync_notes response.
2669        for block in &sync_data.note_blocks {
2670            let bn = block.block_header.block_num();
2671            partial_mmr
2672                .track(bn.as_usize(), block.block_header.commitment(), &block.mmr_path)
2673                .map_err(StoreError::MmrError)
2674                .unwrap();
2675
2676            assert!(
2677                partial_mmr.is_tracked(bn.as_usize()),
2678                "block {bn} should be tracked after calling track()"
2679            );
2680        }
2681
2682        // Verify the tracked blocks match the note blocks.
2683        for &bn in &note_block_nums {
2684            assert!(
2685                partial_mmr.is_tracked(bn.as_usize()),
2686                "block {bn} with notes should be tracked in partial MMR"
2687            );
2688        }
2689    }
2690
2691    #[tokio::test]
2692    async fn sync_notes_with_content_fetches_inclusive_upper_bound_page() {
2693        let (chain, note_tags) = build_chain_with_mint_notes(10).await;
2694        let mock_rpc = MockRpcApi::new(chain);
2695
2696        let blocks = mock_rpc
2697            .sync_notes_with_content(
2698                4_u32.into(),
2699                10_u32.into(),
2700                &note_tags,
2701                NoteContentFetch::PublicDetailsAndAttachments,
2702            )
2703            .await
2704            .expect("sync notes should succeed");
2705
2706        assert_eq!(blocks.last().unwrap().block_header.block_num(), BlockNumber::from(10u32));
2707        assert!(
2708            blocks
2709                .iter()
2710                .any(|block| block.block_header.block_num() == BlockNumber::from(9u32))
2711        );
2712    }
2713
2714    /// Tests that erased notes are marked as consumed when a committed transaction reports output
2715    /// notes that were erased by same-batch note erasure.
2716    ///
2717    /// This simulates same-batch note erasure: the transaction was committed, its header says it
2718    /// produced a note, but the note was erased and doesn't exist on the node.
2719    #[tokio::test]
2720    async fn erased_notes_are_marked_as_consumed() {
2721        // Create a public output note. It won't be in the mock chain (simulating erasure).
2722        let sender_id: AccountId = ACCOUNT_ID_SENDER.try_into().unwrap();
2723        let partial_metadata = PartialNoteMetadata::new(sender_id, NoteType::Public);
2724        let metadata = NoteMetadata::new(partial_metadata, &NoteAttachments::empty());
2725        let script = CodeBuilder::new()
2726            .compile_note_script("@note_script\npub proc main\n    nop\nend")
2727            .unwrap();
2728        let recipient = NoteRecipient::new(
2729            Word::from([1u32, 2, 3, 4]),
2730            script,
2731            NoteStorage::new(vec![]).unwrap(),
2732        );
2733        let output_note = OutputNoteRecord::new(
2734            recipient.digest(),
2735            NoteAssets::new(vec![]).unwrap(),
2736            metadata,
2737            OutputNoteState::ExpectedFull { recipient },
2738            BlockNumber::from(1u32),
2739            NoteAttachments::default(),
2740        );
2741        let note_id = output_note.id();
2742        let note_header = NoteHeader::new(output_note.details_commitment(), metadata);
2743
2744        // Build a NoteUpdateTracker with the output note.
2745        let mut note_updates = NoteUpdateTracker::new(vec![], vec![output_note]);
2746
2747        // Mark the note as erased (created and consumed in the same batch).
2748        let block_num = BlockNumber::from(3u32);
2749        note_updates
2750            .mark_erased_note_as_consumed(&note_header, block_num)
2751            .expect("marking erased note should succeed");
2752
2753        let updated = note_updates
2754            .updated_output_notes()
2755            .find(|n| n.id() == note_id)
2756            .expect("output note should be in the update");
2757
2758        assert!(
2759            updated.inner().is_consumed(),
2760            "output note should be consumed after erasure detection, but state is: {}",
2761            updated.inner().state()
2762        );
2763    }
2764
2765    /// Tests that erased notes targeting a tracked network account are marked as consumed by that
2766    /// account through the full sync flow.
2767    ///
2768    /// Same-batch erasure scenario: a sender's transaction creates an output note targeting a
2769    /// network account that consumes it in the same batch, so the note never appears in the block
2770    /// body and the mock RPC surfaces it as erased in the transaction sync response.
2771    ///
2772    /// When the client tracks the network account, the expected end state is that an input note
2773    /// record is created for the erased note in a consumed state with the network account as the
2774    /// consumer.
2775    ///
2776    /// Ignored because the consumer extraction from an erased note's attachments is no longer wired
2777    /// through `mark_erased_note_as_consumed` — the RPC sync stream delivers only a bare
2778    /// `NoteHeader`, so the consumer is left unknown. Re-enable once attachments are delivered
2779    /// alongside erased notes (or the test is reworked against the new model).
2780    #[allow(clippy::too_many_lines)]
2781    #[ignore = "consumer derivation removed; see comment above"]
2782    #[tokio::test]
2783    async fn erased_notes_are_marked_as_consumed_by_network_account() {
2784        // Build a chain with a sender that executes one tx so `sync_transactions` returns a record.
2785        // The mock attaches the registered erased note header to that record.
2786        let mut builder = MockChainBuilder::new();
2787        let p2id_sender: AccountId = ACCOUNT_ID_SENDER.try_into().unwrap();
2788        let faucet_id: AccountId = ACCOUNT_ID_PRIVATE_FUNGIBLE_FAUCET.try_into().unwrap();
2789        let sender_account =
2790            builder.add_existing_mock_account(miden_testing::Auth::IncrNonce).unwrap();
2791        let sender_id = sender_account.id();
2792
2793        let asset = Asset::from(FungibleAsset::new(faucet_id, 100u64).unwrap());
2794        let note = builder
2795            .add_p2id_note(p2id_sender, sender_id, &[asset], NoteType::Public)
2796            .unwrap();
2797
2798        let mut chain = builder.build().unwrap();
2799        chain.prove_next_block().unwrap();
2800
2801        let tx = Box::pin(
2802            chain
2803                .build_transaction(MockTransactionInput::Account(sender_account.clone()))
2804                .unauthenticated_input_note(note.clone())
2805                .build()
2806                .unwrap()
2807                .execute(),
2808        )
2809        .await
2810        .unwrap();
2811        chain.add_pending_executed_transaction(&tx).unwrap();
2812        chain.prove_next_block().unwrap();
2813
2814        // Construct the erased note that will be marked as consumed by the network account.
2815        let network_account_id: AccountId =
2816            ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE.try_into().unwrap();
2817        let target =
2818            NetworkAccountTarget::new(network_account_id, NoteExecutionHint::Always).unwrap();
2819        let attachment: NoteAttachment = target.into();
2820        let attachments = NoteAttachments::new(vec![attachment]).unwrap();
2821        let partial_metadata = PartialNoteMetadata::new(sender_id, NoteType::Public);
2822        let metadata = NoteMetadata::new(partial_metadata, &attachments);
2823        let script = CodeBuilder::new()
2824            .compile_note_script("@note_script\npub proc main\n    nop\nend")
2825            .unwrap();
2826        let recipient = NoteRecipient::new(
2827            Word::from([7u32, 8, 9, 10]),
2828            script,
2829            NoteStorage::new(vec![]).unwrap(),
2830        );
2831        let recipient_digest = recipient.digest();
2832        let assets = NoteAssets::new(vec![]).unwrap();
2833
2834        // Output note record tracked by the sender prior to sync. The flow that builds the input
2835        // record from the erased header relies on this output entry being present.
2836        let output_note = OutputNoteRecord::new(
2837            recipient_digest,
2838            assets.clone(),
2839            metadata,
2840            OutputNoteState::ExpectedFull { recipient },
2841            BlockNumber::from(1u32),
2842            NoteAttachments::default(),
2843        );
2844        let erased_note_id = output_note.id();
2845        let erased_note_header = NoteHeader::new(output_note.details_commitment(), metadata);
2846
2847        let mock_rpc = MockRpcApi::new(chain);
2848        mock_rpc.mark_note_as_erased(erased_note_header);
2849
2850        // Track both the sender (so its tx is returned) and the network account (so the gating in
2851        // `mark_erased_note_as_consumed` allows creating the input record).
2852        let network_header =
2853            AccountHeader::new(network_account_id, ZERO, EMPTY_WORD, EMPTY_WORD, EMPTY_WORD);
2854
2855        let state_sync = StateSync::new(
2856            Arc::new(mock_rpc.clone()),
2857            Arc::new(MockScreener),
2858            None,
2859            genesis_validator_config(&mock_rpc),
2860        );
2861
2862        let genesis_peaks =
2863            mock_rpc.get_mmr().peaks_at(Forest::new(1).expect("valid forest")).unwrap();
2864        let mut partial_mmr = PartialMmr::from_peaks(genesis_peaks);
2865
2866        let sync_input = StateSyncInput {
2867            accounts: vec![AccountHeader::from(&sender_account), network_header],
2868            note_tags: BTreeSet::new(),
2869            input_notes: vec![],
2870            output_notes: vec![output_note],
2871            uncommitted_transactions: vec![],
2872        };
2873
2874        let update = state_sync.sync_state(&mut partial_mmr, sync_input).await.unwrap();
2875
2876        // The output note record should transition to consumed.
2877        let updated_output = update
2878            .note_updates()
2879            .updated_output_notes()
2880            .find(|n| n.id() == erased_note_id)
2881            .expect("output note should be in the update");
2882        assert!(
2883            updated_output.inner().is_consumed(),
2884            "output note should be consumed, got: {}",
2885            updated_output.inner().state()
2886        );
2887
2888        // A new input note record should be created with the network account as consumer.
2889        let input_note_update = update
2890            .note_updates()
2891            .updated_input_notes()
2892            .find(|n| n.id() == Some(erased_note_id))
2893            .expect("input note should be created from the erased output note");
2894
2895        let inner = input_note_update.inner();
2896        assert!(
2897            inner.is_consumed(),
2898            "input note should be in a consumed state, got: {}",
2899            inner.state()
2900        );
2901        assert_eq!(
2902            inner.consumer_account(),
2903            Some(network_account_id),
2904            "consumer should be the tracked network account"
2905        );
2906    }
2907
2908    /// Verifies the validations performed on `sync_chain_mmr` responses: a genuine mock-chain
2909    /// response passes, while each tampered variant is rejected with a `ChainValidationError`.
2910    #[tokio::test]
2911    async fn validate_chain_mmr_response_rejects_tampered_responses() {
2912        let mock_rpc = MockRpcApi::default();
2913        mock_rpc.advance_blocks(3);
2914        let chain_tip = mock_rpc.get_chain_tip_block_num();
2915        let current = BlockNumber::GENESIS;
2916        let validator_config = genesis_validator_config(&mock_rpc);
2917
2918        let header_of =
2919            |block_num: u32| mock_rpc.mock_chain.read().block_header(block_num as usize);
2920        let chain_mmr_response = || async {
2921            mock_rpc.sync_chain_mmr(current, SyncTarget::CommittedChainTip).await.unwrap()
2922        };
2923
2924        // Sanity check: the untampered response passes validation.
2925        let response = chain_mmr_response().await;
2926        StateSync::validate_chain_mmr_response(&response, current, &validator_config).unwrap();
2927
2928        // The returned block header doesn't correspond to `block_to`.
2929        let mut response = chain_mmr_response().await;
2930        response.block_header = header_of(chain_tip.as_u32() - 1);
2931        let result = StateSync::validate_chain_mmr_response(&response, current, &validator_config);
2932        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
2933
2934        // `block_from` doesn't match the block the sync was requested from.
2935        let mut response = chain_mmr_response().await;
2936        response.block_from = current + 1;
2937        let result = StateSync::validate_chain_mmr_response(&response, current, &validator_config);
2938        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
2939
2940        // `block_to` (and its header) regress behind the client's current block.
2941        let mut response = chain_mmr_response().await;
2942        response.block_from = chain_tip;
2943        response.block_to = BlockNumber::GENESIS;
2944        response.block_header = header_of(0);
2945        let result =
2946            StateSync::validate_chain_mmr_response(&response, chain_tip, &validator_config);
2947        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
2948    }
2949
2950    /// Verifies that `sync_notes` blocks outside the requested range `(current, chain_tip]` are
2951    /// rejected with a `ChainValidationError`.
2952    #[test]
2953    fn validate_note_blocks_range_rejects_out_of_range_blocks() {
2954        let mock_rpc = MockRpcApi::default();
2955        mock_rpc.advance_blocks(3);
2956        let chain_tip = mock_rpc.get_chain_tip_block_num();
2957        let current = BlockNumber::GENESIS;
2958
2959        // Sanity check: an empty block list passes validation.
2960        StateSync::validate_note_blocks_range(&[], current, chain_tip).unwrap();
2961
2962        // A note block outside the requested range: genesis is always outside it.
2963        let genesis_note_block = ResolvedSyncNotesBlock {
2964            block_header: mock_rpc.mock_chain.read().block_header(0),
2965            mmr_path: MerklePath::new(Vec::new()),
2966            notes: BTreeMap::new(),
2967        };
2968        let result =
2969            StateSync::validate_note_blocks_range(&[genesis_note_block], current, chain_tip);
2970        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
2971    }
2972
2973    /// Verifies that `validate_chain_mmr_response` authenticates the chain tip header against the
2974    /// validator set, so a header served without the signatures of that set is rejected.
2975    #[tokio::test]
2976    async fn validate_chain_mmr_response_rejects_chain_tip_without_valid_signatures() {
2977        let mock_rpc = MockRpcApi::default();
2978        mock_rpc.advance_blocks(3);
2979        let chain_tip = mock_rpc.get_chain_tip_block_num();
2980        let current = BlockNumber::GENESIS;
2981
2982        let validator_config = genesis_validator_config(&mock_rpc);
2983        assert!(!validator_config.is_empty(), "the mock chain must commit a validator set");
2984
2985        // The signatures the validator set produced for the chain tip are accepted.
2986        let mut chain_mmr_info =
2987            mock_rpc.sync_chain_mmr(current, SyncTarget::CommittedChainTip).await.unwrap();
2988        StateSync::validate_chain_mmr_response(&chain_mmr_info, current, &validator_config)
2989            .unwrap();
2990
2991        let parent = BlockNumber::from(chain_tip.as_u32() - 1);
2992        // Override the chain_mmr_info with the signatures of a different block
2993        chain_mmr_info.block_signatures = block_signatures(&mock_rpc, parent);
2994        // `validate_chain_mmr_response` returns an error when the signature verification fails
2995        let result =
2996            StateSync::validate_chain_mmr_response(&chain_mmr_info, current, &validator_config);
2997        assert!(
2998            matches!(result, Err(ClientError::ChainValidationError(_))),
2999            "signatures of another block must be rejected, got {result:?}"
3000        );
3001    }
3002
3003    /// Verifies that `advance_mmr` rejects an MMR delta whose post-apply peaks don't match the
3004    /// chain tip header's chain commitment.
3005    #[test]
3006    fn advance_mmr_rejects_delta_inconsistent_with_chain_commitment() {
3007        let mock_rpc = MockRpcApi::default();
3008        mock_rpc.advance_blocks(3);
3009        let chain_tip = mock_rpc.get_chain_tip_block_num();
3010
3011        let chain_tip_header = mock_rpc.mock_chain.read().block_header(chain_tip.as_usize());
3012        let genesis_partial_mmr = || {
3013            let peaks = mock_rpc.get_mmr().peaks_at(Forest::new(1).expect("valid forest")).unwrap();
3014            PartialMmr::from_peaks(peaks)
3015        };
3016
3017        // An MMR delta consistent with the chain tip header advances the MMR...
3018        let full_delta = mock_rpc
3019            .get_mmr()
3020            .get_delta(Forest::new(1).unwrap(), Forest::new(chain_tip.as_usize()).unwrap())
3021            .unwrap();
3022        StateSync::advance_mmr(
3023            full_delta,
3024            &chain_tip_header,
3025            &mut genesis_partial_mmr(),
3026            &mut PartialBlockchainUpdates::default(),
3027        )
3028        .unwrap();
3029
3030        // ...but one that stops short of the chain tip fails the chain commitment check.
3031        let truncated_delta = mock_rpc
3032            .get_mmr()
3033            .get_delta(Forest::new(1).unwrap(), Forest::new(chain_tip.as_usize() - 1).unwrap())
3034            .unwrap();
3035        let result = StateSync::advance_mmr(
3036            truncated_delta,
3037            &chain_tip_header,
3038            &mut genesis_partial_mmr(),
3039            &mut PartialBlockchainUpdates::default(),
3040        );
3041        assert!(matches!(result, Err(ClientError::ChainValidationError(_))));
3042    }
3043
3044    /// Builds a minimal RPC transaction record at `block_num`, for range-validation tests.
3045    fn make_tx_record(account_id: AccountId, block_num: u32) -> RpcTransactionRecord {
3046        RpcTransactionRecord {
3047            block_num: BlockNumber::from(block_num),
3048            transaction_header: TransactionHeader::new(
3049                account_id,
3050                word(1),
3051                word(2),
3052                InputNotes::new_unchecked(vec![]),
3053                vec![],
3054            )
3055            .unwrap(),
3056            output_notes: vec![],
3057            erased_output_notes: vec![],
3058            consumed_note_refs: vec![],
3059        }
3060    }
3061}