miden_client/note/
note_screener.rs1use 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
26pub type NoteConsumability = (AccountId, NoteConsumptionStatus);
31
32fn is_relevant(consumption_status: &NoteConsumptionStatus) -> bool {
36 !matches!(
37 consumption_status,
38 NoteConsumptionStatus::NeverConsumable(_) | NoteConsumptionStatus::UnconsumableConditions
39 )
40}
41
42#[derive(Clone)]
49pub struct NoteScreener {
50 store: Arc<dyn Store>,
52 tx_args: Option<TransactionArgs>,
54 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 #[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 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(¬e.id())
89 .unwrap_or_default())
90 }
91
92 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 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 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#[async_trait(?Send)]
191impl OnNoteReceived for NoteScreener {
192 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 return Ok(NoteUpdateAction::Commit(committed_note));
211 }
212
213 match public_note {
214 Some(public_note) => {
215 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 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 Ok(NoteUpdateAction::Discard)
242 },
243 }
244 }
245}
246
247#[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}