Skip to main content

miden_client/note/
note_screener.rs

1use alloc::boxed::Box;
2use alloc::collections::BTreeMap;
3use alloc::sync::Arc;
4use alloc::vec::Vec;
5
6use async_trait::async_trait;
7use miden_protocol::account::{AccountCode, AccountId};
8use miden_protocol::note::{Note, NoteId};
9use miden_standards::note::NoteConsumptionStatus;
10use miden_tx::{
11    NoteCheckerError,
12    NoteConsumptionChecker,
13    NoteConsumptionInfo,
14    TransactionExecutor,
15};
16use thiserror::Error;
17
18use crate::ClientError;
19use crate::rpc::NodeRpcClient;
20use crate::rpc::domain::note::CommittedNote;
21use crate::store::data_store::ClientDataStore;
22use crate::store::{InputNoteRecord, NoteFilter, Store, StoreError};
23use crate::sync::{NoteUpdateAction, OnNoteReceived};
24use crate::transaction::{AdviceMap, InputNote, TransactionArgs, TransactionRequestError};
25
26/// Represents the consumability of a note by a specific account.
27///
28/// The tuple contains the account ID that may consume the note and the moment it will become
29/// relevant.
30pub type NoteConsumability = (AccountId, NoteConsumptionStatus);
31
32/// Returns `true` if the consumption status indicates that the note may be consumable by the
33/// account. A note is considered relevant unless it is permanently unconsumable (either due to
34/// a fundamental incompatibility or unconsumable conditions).
35fn is_relevant(consumption_status: &NoteConsumptionStatus) -> bool {
36    !matches!(
37        consumption_status,
38        NoteConsumptionStatus::NeverConsumable(_) | NoteConsumptionStatus::UnconsumableConditions
39    )
40}
41
42/// Provides functionality for testing whether a note is relevant to the client or not.
43///
44/// Here, relevance is based on whether the note is able to be consumed by an account that is
45/// tracked in the provided `store`. This can be derived in a number of ways, such as looking
46/// at the combination of script root and note inputs. For example, a P2ID note is relevant
47/// for a specific account ID if this ID is its first note input.
48#[derive(Clone)]
49pub struct NoteScreener {
50    /// A reference to the client's store, used to fetch necessary data to check consumability.
51    store: Arc<dyn Store>,
52    /// Optional transaction arguments to use when checking consumability.
53    tx_args: Option<TransactionArgs>,
54    /// RPC client used for lazy-loading foreign account data during note screening.
55    rpc_api: Arc<dyn NodeRpcClient>,
56}
57
58impl NoteScreener {
59    pub fn new(store: Arc<dyn Store>, rpc_api: Arc<dyn NodeRpcClient>) -> Self {
60        Self { store, tx_args: None, rpc_api }
61    }
62
63    /// Sets the transaction arguments to use when checking note consumability.
64    /// If not set, a default `TransactionArgs` with an empty advice map is used.
65    #[must_use]
66    pub fn with_transaction_args(mut self, tx_args: TransactionArgs) -> Self {
67        self.tx_args = Some(tx_args);
68        self
69    }
70
71    fn tx_args(&self) -> TransactionArgs {
72        self.tx_args
73            .clone()
74            .unwrap_or_else(|| TransactionArgs::new(AdviceMap::default()))
75    }
76
77    /// Checks whether the provided note could be consumed by any of the accounts tracked by
78    /// this screener. Convenience wrapper around [`Self::can_consume_batch`] for a single note.
79    ///
80    /// Returns the [`NoteConsumptionStatus`] for each account that could consume the note.
81    pub async fn can_consume(
82        &self,
83        note: &Note,
84    ) -> Result<Vec<NoteConsumability>, NoteScreenerError> {
85        Ok(self
86            .can_consume_batch(core::slice::from_ref(note))
87            .await?
88            .remove(&note.id())
89            .unwrap_or_default())
90    }
91
92    /// Checks whether the provided notes could be consumed by any of the accounts tracked by
93    /// this screener, by executing a transaction for each note-account pair.
94    ///
95    /// Returns a map from [`NoteId`] to a list of `(AccountId, NoteConsumptionStatus)` pairs.
96    /// Notes that are permanently unconsumable by all accounts are not included in the result.
97    pub async fn can_consume_batch(
98        &self,
99        notes: &[Note],
100    ) -> Result<BTreeMap<NoteId, Vec<NoteConsumability>>, NoteScreenerError> {
101        let account_ids = self.store.get_account_ids().await?;
102        if notes.is_empty() || account_ids.is_empty() {
103            return Ok(BTreeMap::new());
104        }
105
106        let block_ref = self.store.get_sync_height().await?;
107        let mut relevant_notes: BTreeMap<NoteId, Vec<NoteConsumability>> = BTreeMap::new();
108        let tx_args = self.tx_args();
109
110        let data_store = ClientDataStore::new(self.store.clone(), self.rpc_api.clone());
111        // Don't attach the real authenticator for consumability checks. The
112        // NoteConsumptionChecker gracefully handles a missing authenticator by
113        // returning `ConsumableWithAuthorization` instead of calling
114        // `get_signature()`. Attaching the real authenticator here causes the
115        // external signer (e.g. wallet extension) to be invoked during
116        // sync_state, producing unwanted confirmation popups on every sync.
117        let transaction_executor: TransactionExecutor<'_, '_, _, ()> =
118            TransactionExecutor::new(&data_store);
119        let consumption_checker = NoteConsumptionChecker::new(&transaction_executor);
120
121        for account_id in account_ids {
122            let account_code = self.get_account_code(account_id).await?;
123            data_store.mast_store().load_account_code(&account_code);
124
125            for note in notes {
126                let consumption_status = consumption_checker
127                    .can_consume(
128                        account_id,
129                        block_ref,
130                        InputNote::unauthenticated(note.clone()),
131                        tx_args.clone(),
132                    )
133                    .await?;
134
135                if is_relevant(&consumption_status) {
136                    relevant_notes
137                        .entry(note.id())
138                        .or_default()
139                        .push((account_id, consumption_status));
140                }
141            }
142        }
143
144        Ok(relevant_notes)
145    }
146
147    /// Checks whether the provided notes could be consumed by a specific account by attempting
148    /// to execute them together in a transaction. Notes that fail are progressively removed
149    /// until a maximal set of successfully consumable notes is found.
150    ///
151    /// Returns a [`NoteConsumptionInfo`] splitting notes into those that succeeded and those
152    /// that failed.
153    pub async fn check_notes_consumability(
154        &self,
155        account_id: AccountId,
156        notes: Vec<Note>,
157    ) -> Result<NoteConsumptionInfo, NoteScreenerError> {
158        let block_ref = self.store.get_sync_height().await?;
159        let tx_args = self.tx_args();
160        let account_code = self.get_account_code(account_id).await?;
161
162        let data_store = ClientDataStore::new(self.store.clone(), self.rpc_api.clone());
163        let transaction_executor: TransactionExecutor<'_, '_, _, ()> =
164            TransactionExecutor::new(&data_store);
165
166        let consumption_checker = NoteConsumptionChecker::new(&transaction_executor);
167
168        data_store.mast_store().load_account_code(&account_code);
169        let note_consumption_info = consumption_checker
170            .check_notes_consumability(account_id, block_ref, notes, tx_args)
171            .await?;
172
173        Ok(note_consumption_info)
174    }
175
176    async fn get_account_code(
177        &self,
178        account_id: AccountId,
179    ) -> Result<AccountCode, NoteScreenerError> {
180        self.store
181            .get_account_code(account_id)
182            .await?
183            .ok_or(NoteScreenerError::AccountDataNotFound(account_id))
184    }
185}
186
187// DEFAULT CALLBACK IMPLEMENTATIONS
188// ================================================================================================
189
190#[async_trait(?Send)]
191impl OnNoteReceived for NoteScreener {
192    /// Default implementation of the [`OnNoteReceived`] callback. It queries the store for the
193    /// committed note to check if it's relevant. If the note wasn't being tracked but it came in
194    /// the sync response it may be a new public note, in that case we use the [`NoteScreener`]
195    /// to check its relevance.
196    async fn on_note_received(
197        &self,
198        committed_note: CommittedNote,
199        public_note: Option<InputNoteRecord>,
200    ) -> Result<NoteUpdateAction, ClientError> {
201        let note_id = *committed_note.note_id();
202
203        let input_note_present =
204            !self.store.get_input_notes(NoteFilter::Unique(note_id)).await?.is_empty();
205        let output_note_present =
206            !self.store.get_output_notes(NoteFilter::Unique(note_id)).await?.is_empty();
207
208        if input_note_present || output_note_present {
209            // The note is being tracked by the client so it is relevant
210            return Ok(NoteUpdateAction::Commit(committed_note));
211        }
212
213        match public_note {
214            Some(public_note) => {
215                // If tracked by the user, keep note regardless of inputs and extra checks
216                if let Some(metadata) = public_note.metadata()
217                    && self.store.get_unique_note_tags().await?.contains(&metadata.tag())
218                {
219                    return Ok(NoteUpdateAction::Insert(public_note));
220                }
221
222                // The note is not being tracked by the client and is public so we can screen it
223                let new_note_relevance = self
224                    .can_consume(
225                        &public_note
226                            .clone()
227                            .try_into()
228                            .map_err(ClientError::NoteRecordConversionError)?,
229                    )
230                    .await?;
231                let is_relevant = !new_note_relevance.is_empty();
232                if is_relevant {
233                    Ok(NoteUpdateAction::Insert(public_note))
234                } else {
235                    Ok(NoteUpdateAction::Discard)
236                }
237            },
238            None => {
239                // The note is not being tracked by the client and is private so we can't determine
240                // if it is relevant
241                Ok(NoteUpdateAction::Discard)
242            },
243        }
244    }
245}
246
247// NOTE SCREENER ERRORS
248// ================================================================================================
249
250/// Error when screening notes to check relevance to a client.
251#[derive(Debug, Error)]
252pub enum NoteScreenerError {
253    #[error("account {0} data not found in the store")]
254    AccountDataNotFound(AccountId),
255    #[error("failed to fetch data from the store")]
256    StoreError(#[from] StoreError),
257    #[error("note consumption check failed")]
258    NoteCheckerError(#[from] NoteCheckerError),
259    #[error("failed to build transaction request")]
260    TransactionRequestError(#[from] TransactionRequestError),
261}