Skip to main content

miden_client/pswap/
observer.rs

1//! Per-note observer that collects every PSWAP-attachment note seen
2//! during sync. Lineage-scope filtering happens later, in `discovery`.
3
4use alloc::boxed::Box;
5use alloc::sync::Arc;
6use alloc::vec::Vec;
7
8use async_trait::async_trait;
9use miden_protocol::asset::AssetAmount;
10use miden_protocol::note::NoteAttachments;
11use miden_standards::note::{PswapNote, PswapNoteAttachment};
12use tracing::warn;
13
14use crate::ClientError;
15use crate::pswap::discovery::discover_pswap_rounds;
16use crate::pswap::lineage::ObservedPswapNote;
17use crate::rpc::domain::note::CommittedNote;
18use crate::store::Store;
19use crate::sync::NoteObserver;
20use crate::utils::RwLock;
21
22// PSWAP CHAIN OBSERVER
23// ================================================================================================
24
25/// Per-sync collector of PSWAP-attachment notes seen this sync.
26///
27/// - `observe()` runs per-note during sync: reads the PSWAP attachment word straight off the note's
28///   resolved attachments (carried inline on the sync window) and records a `ObservedPswapNote`. No
29///   RPC round trip, no DB write.
30/// - `apply()` runs once post-sync: drains the collector, runs the correlator, applies round
31///   updates.
32pub struct PswapChainObserver {
33    store: Arc<dyn Store>,
34    /// `observe()` writes, `apply()` drains; never concurrent. The observer is
35    /// shared via the outer `Arc<dyn NoteObserver>` and only ever touched
36    /// through `&self`, so the `RwLock` alone provides the needed interior
37    /// mutability — no inner `Arc`.
38    chain_note_updates: RwLock<Vec<ObservedPswapNote>>,
39}
40
41impl PswapChainObserver {
42    pub fn new(store: Arc<dyn Store>) -> Self {
43        Self {
44            store,
45            chain_note_updates: RwLock::new(Vec::new()),
46        }
47    }
48}
49
50#[async_trait(?Send)]
51impl NoteObserver for PswapChainObserver {
52    fn name(&self) -> &'static str {
53        "PswapChainObserver"
54    }
55
56    async fn observe(
57        &self,
58        committed_note: &CommittedNote,
59        attachments: Option<&NoteAttachments>,
60    ) -> Result<bool, ClientError> {
61        // Notes without a PSWAP attachment are the common case; `extract_pswap_attachment`
62        // fast-rejects them. Foreign-order filtering happens later in `discovery`.
63        let Some(attachments) = attachments else {
64            return Ok(false);
65        };
66        let Some(attachment) = extract_pswap_attachment(attachments) else {
67            return Ok(false);
68        };
69
70        let inclusion_proof = committed_note.inclusion_proof().clone();
71        self.chain_note_updates.write().push(ObservedPswapNote {
72            note_id: *committed_note.note_id(),
73            attachment,
74            sender: committed_note.sender(),
75            tag: committed_note.metadata().tag(),
76            block_num: inclusion_proof.location().block_num(),
77            inclusion_proof,
78        });
79        Ok(true)
80    }
81
82    /// Drains the collector, runs the correlator, applies round updates.
83    /// Per-round failures are logged, not propagated.
84    async fn apply(&self, sync_update: &crate::sync::StateSyncUpdate) -> Result<(), ClientError> {
85        let chain_note_updates = core::mem::take(&mut *self.chain_note_updates.write());
86
87        // Nothing observed AND nothing consumed — correlator has no work.
88        if chain_note_updates.is_empty()
89            && sync_update.note_updates.consumed_note_ids().next().is_none()
90        {
91            return Ok(());
92        }
93
94        let round_updates =
95            discover_pswap_rounds(self.store.clone(), sync_update, &chain_note_updates).await?;
96
97        for round_update in round_updates {
98            if let Err(err) = crate::pswap::store::apply_round(&self.store, &round_update).await {
99                warn!(
100                    order_id = round_update.order_id.as_canonical_u64(),
101                    round_depth = round_update.round_depth,
102                    error = ?err,
103                    "apply_round failed; lineage left at previous tip",
104                );
105            }
106        }
107        Ok(())
108    }
109}
110
111// ---------------------------------------------------------------------------
112// HELPERS
113// ---------------------------------------------------------------------------
114
115/// Pulls the typed [`PswapNoteAttachment`] off a note's attachment word
116/// `[amount, order_id, depth, 0]`. Returns `None` for notes without a
117/// PSWAP-scheme attachment or with malformed content.
118fn extract_pswap_attachment(attachments: &NoteAttachments) -> Option<PswapNoteAttachment> {
119    let pswap_attach = attachments.find(PswapNote::PSWAP_ATTACHMENT_SCHEME)?;
120    let word = pswap_attach.content().as_words().first()?;
121
122    let amount = AssetAmount::new(word[0].as_canonical_u64()).ok()?;
123    let order_id = word[1];
124    let depth = u32::try_from(word[2].as_canonical_u64()).ok()?;
125    Some(PswapNoteAttachment::new(amount, order_id, depth))
126}
127
128// ---------------------------------------------------------------------------
129// TESTS
130// ---------------------------------------------------------------------------
131
132#[cfg(test)]
133mod tests {
134    //! Reject-branch coverage for `extract_pswap_attachment` — the per-note
135    //! fast-path that turns a raw attachment word into a typed
136    //! [`PswapNoteAttachment`] (or rejects it).
137    use alloc::vec::Vec;
138
139    use miden_protocol::note::{NoteAttachment, NoteAttachmentScheme, NoteAttachments};
140    use miden_protocol::{Felt, Word};
141    use miden_standards::note::PswapNote;
142
143    use super::*;
144
145    /// A PSWAP attachment word `[amount, order_id, depth, 0]`.
146    fn pswap_word(amount: u64, order_id: u64, depth: u64) -> Word {
147        Word::from([
148            Felt::new(amount).unwrap(),
149            Felt::new(order_id).unwrap(),
150            Felt::new(depth).unwrap(),
151            Felt::new(0).unwrap(),
152        ])
153    }
154
155    /// Wraps `word` in a single PSWAP-scheme attachment.
156    fn pswap_attachments(word: Word) -> NoteAttachments {
157        NoteAttachments::from(NoteAttachment::with_word(PswapNote::PSWAP_ATTACHMENT_SCHEME, word))
158    }
159
160    /// Well-formed word round-trips into the typed attachment.
161    #[test]
162    fn extract_pswap_attachment_reads_wellformed_word() {
163        let parsed = extract_pswap_attachment(&pswap_attachments(pswap_word(25, 0xabcd, 3)))
164            .expect("valid PSWAP word must parse");
165        assert_eq!(u64::from(parsed.amount()), 25);
166        assert_eq!(parsed.order_id().as_canonical_u64(), 0xabcd);
167        assert_eq!(parsed.depth(), 3);
168    }
169
170    /// No PSWAP-scheme attachment present → `None`. Covers both the empty
171    /// set and the "has attachments, but none is ours" case (the common
172    /// path for unrelated notes during sync).
173    #[test]
174    fn extract_pswap_attachment_rejects_missing_scheme() {
175        let empty = NoteAttachments::new(Vec::new()).unwrap();
176        assert!(extract_pswap_attachment(&empty).is_none());
177
178        // Scheme 1 ≠ the PSWAP scheme (3).
179        let other = NoteAttachments::from(NoteAttachment::with_word(
180            NoteAttachmentScheme::new(1).unwrap(),
181            pswap_word(1, 2, 3),
182        ));
183        assert!(extract_pswap_attachment(&other).is_none());
184    }
185
186    /// `amount` above `AssetAmount::MAX` is rejected, not panicked on.
187    #[test]
188    fn extract_pswap_attachment_rejects_oversized_amount() {
189        let word = pswap_word(AssetAmount::MAX.as_u64() + 1, 7, 1);
190        assert!(extract_pswap_attachment(&pswap_attachments(word)).is_none());
191    }
192
193    /// `depth` above `u32::MAX` is rejected, not panicked on. The amount
194    /// field is valid so the parser reaches the depth check.
195    #[test]
196    fn extract_pswap_attachment_rejects_oversized_depth() {
197        let word = pswap_word(10, 7, u64::from(u32::MAX) + 1);
198        assert!(extract_pswap_attachment(&pswap_attachments(word)).is_none());
199    }
200}