1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
use alloc::collections::BTreeMap;
use miden_protocol::account::AccountId;
use miden_protocol::block::{BlockHeader, BlockNumber};
use miden_protocol::note::{NoteId, NoteInclusionProof, Nullifier};
use crate::ClientError;
use crate::rpc::RpcError;
use crate::rpc::domain::note::CommittedNote;
use crate::rpc::domain::nullifier::NullifierUpdate;
use crate::store::{InputNoteRecord, OutputNoteRecord};
use crate::transaction::{TransactionRecord, TransactionStatus};
// NOTE UPDATE
// ================================================================================================
/// Represents the possible types of updates that can be applied to a note in a
/// [`NoteUpdateTracker`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NoteUpdateType {
/// Indicates that the note was already tracked but it was not updated.
None,
/// Indicates that the note is new and should be inserted in the store.
Insert,
/// Indicates that the note was already tracked and should be updated.
Update,
}
/// Represents the possible states of an input note record in a [`NoteUpdateTracker`].
#[derive(Clone, Debug)]
pub struct InputNoteUpdate {
/// Input note being updated.
note: InputNoteRecord,
/// Type of the note update.
update_type: NoteUpdateType,
}
impl InputNoteUpdate {
/// Creates a new [`InputNoteUpdate`] with the provided note with a `None` update type.
fn new_none(note: InputNoteRecord) -> Self {
Self { note, update_type: NoteUpdateType::None }
}
/// Creates a new [`InputNoteUpdate`] with the provided note with an `Insert` update type.
fn new_insert(note: InputNoteRecord) -> Self {
Self {
note,
update_type: NoteUpdateType::Insert,
}
}
/// Creates a new [`InputNoteUpdate`] with the provided note with an `Update` update type.
fn new_update(note: InputNoteRecord) -> Self {
Self {
note,
update_type: NoteUpdateType::Update,
}
}
/// Returns a reference the inner note record.
pub fn inner(&self) -> &InputNoteRecord {
&self.note
}
/// Returns a mutable reference to the inner note record. If the update type is `None` or
/// `Update`, it will be set to `Update`.
fn inner_mut(&mut self) -> &mut InputNoteRecord {
self.update_type = match self.update_type {
NoteUpdateType::None | NoteUpdateType::Update => NoteUpdateType::Update,
NoteUpdateType::Insert => NoteUpdateType::Insert,
};
&mut self.note
}
/// Returns the type of the note update.
pub fn update_type(&self) -> &NoteUpdateType {
&self.update_type
}
/// Returns the identifier of the inner note.
pub fn id(&self) -> NoteId {
self.note.id()
}
/// Returns the per-account position of the consuming transaction within the account's
/// execution chain for the block. `None` for non-consumed notes or when the order has not
/// been determined yet.
pub fn consumed_tx_order(&self) -> Option<u32> {
self.note.state().consumed_tx_order()
}
}
/// Represents the possible states of an output note record in a [`NoteUpdateTracker`].
#[derive(Clone, Debug)]
pub struct OutputNoteUpdate {
/// Output note being updated.
note: OutputNoteRecord,
/// Type of the note update.
update_type: NoteUpdateType,
}
impl OutputNoteUpdate {
/// Creates a new [`OutputNoteUpdate`] with the provided note with a `None` update type.
fn new_none(note: OutputNoteRecord) -> Self {
Self { note, update_type: NoteUpdateType::None }
}
/// Creates a new [`OutputNoteUpdate`] with the provided note with an `Insert` update type.
fn new_insert(note: OutputNoteRecord) -> Self {
Self {
note,
update_type: NoteUpdateType::Insert,
}
}
/// Creates a new [`OutputNoteUpdate`] with the provided note with an `Update` update type.
fn new_update(note: OutputNoteRecord) -> Self {
Self {
note,
update_type: NoteUpdateType::Update,
}
}
/// Returns a reference the inner note record.
pub fn inner(&self) -> &OutputNoteRecord {
&self.note
}
/// Returns a mutable reference to the inner note record. If the update type is `None` or
/// `Update`, it will be set to `Update`.
fn inner_mut(&mut self) -> &mut OutputNoteRecord {
self.update_type = match self.update_type {
NoteUpdateType::None | NoteUpdateType::Update => NoteUpdateType::Update,
NoteUpdateType::Insert => NoteUpdateType::Insert,
};
&mut self.note
}
/// Returns the type of the note update.
pub fn update_type(&self) -> &NoteUpdateType {
&self.update_type
}
/// Returns the identifier of the inner note.
pub fn id(&self) -> NoteId {
self.note.id()
}
}
// NOTE UPDATE TRACKER
// ================================================================================================
/// Contains note changes to apply to the store.
///
/// This includes new notes that have been created and existing notes that have been updated. The
/// tracker also lets state changes be applied to the contained notes, this allows for already
/// updated notes to be further updated as new information is received.
#[derive(Clone, Debug, Default)]
pub struct NoteUpdateTracker {
/// A map of new and updated input note records to be upserted in the store.
input_notes: BTreeMap<NoteId, InputNoteUpdate>,
/// A map of updated output note records to be upserted in the store.
output_notes: BTreeMap<NoteId, OutputNoteUpdate>,
/// Fast lookup map from nullifier to input note id.
input_notes_by_nullifier: BTreeMap<Nullifier, NoteId>,
/// Fast lookup map from nullifier to output note id.
output_notes_by_nullifier: BTreeMap<Nullifier, NoteId>,
/// Map from nullifier to its per-account position in the consuming transaction order.
/// Nullifiers from the same account are in execution order; ordering across different
/// accounts is not guaranteed.
nullifier_order: BTreeMap<Nullifier, u32>,
}
impl NoteUpdateTracker {
/// Creates a [`NoteUpdateTracker`] with already-tracked notes.
pub fn new(
input_notes: impl IntoIterator<Item = InputNoteRecord>,
output_notes: impl IntoIterator<Item = OutputNoteRecord>,
) -> Self {
let mut tracker = Self::default();
for note in input_notes {
tracker.insert_input_note(note, NoteUpdateType::None);
}
for note in output_notes {
tracker.insert_output_note(note, NoteUpdateType::None);
}
tracker
}
/// Creates a [`NoteUpdateTracker`] for updates related to transactions.
///
/// A transaction can:
///
/// - Create input notes
/// - Update existing input notes (by consuming them)
/// - Create output notes
pub fn for_transaction_updates(
new_input_notes: impl IntoIterator<Item = InputNoteRecord>,
updated_input_notes: impl IntoIterator<Item = InputNoteRecord>,
new_output_notes: impl IntoIterator<Item = OutputNoteRecord>,
) -> Self {
let mut tracker = Self::default();
for note in new_input_notes {
tracker.insert_input_note(note, NoteUpdateType::Insert);
}
for note in updated_input_notes {
tracker.insert_input_note(note, NoteUpdateType::Update);
}
for note in new_output_notes {
tracker.insert_output_note(note, NoteUpdateType::Insert);
}
tracker
}
// GETTERS
// --------------------------------------------------------------------------------------------
/// Returns all input note records that have been updated.
///
/// This may include:
/// - New notes that have been created that should be inserted.
/// - Existing tracked notes that should be updated.
pub fn updated_input_notes(&self) -> impl Iterator<Item = &InputNoteUpdate> {
self.input_notes.values().filter(|note| {
matches!(note.update_type, NoteUpdateType::Insert | NoteUpdateType::Update)
})
}
/// Returns all output note records that have been updated.
///
/// This may include:
/// - New notes that have been created that should be inserted.
/// - Existing tracked notes that should be updated.
pub fn updated_output_notes(&self) -> impl Iterator<Item = &OutputNoteUpdate> {
self.output_notes.values().filter(|note| {
matches!(note.update_type, NoteUpdateType::Insert | NoteUpdateType::Update)
})
}
/// Returns whether no new note-related information has been retrieved.
pub fn is_empty(&self) -> bool {
self.input_notes.is_empty() && self.output_notes.is_empty()
}
/// Returns input and output note unspent nullifiers.
pub fn unspent_nullifiers(&self) -> impl Iterator<Item = Nullifier> {
let input_note_unspent_nullifiers = self
.input_notes
.values()
.filter(|note| !note.inner().is_consumed())
.map(|note| note.inner().nullifier());
let output_note_unspent_nullifiers = self
.output_notes
.values()
.filter(|note| !note.inner().is_consumed())
.filter_map(|note| note.inner().nullifier());
input_note_unspent_nullifiers.chain(output_note_unspent_nullifiers)
}
/// Appends nullifiers to the per-account ordered nullifier list.
///
/// Nullifiers from the same account must be in execution order; ordering across different
/// accounts is not guaranteed.
pub fn extend_nullifiers(&mut self, nullifiers: impl IntoIterator<Item = Nullifier>) {
for nullifier in nullifiers {
let next_pos =
u32::try_from(self.nullifier_order.len()).expect("nullifier count exceeds u32");
self.nullifier_order.entry(nullifier).or_insert(next_pos);
}
}
// UPDATE METHODS
// --------------------------------------------------------------------------------------------
/// Inserts the new public note data into the tracker. This method doesn't check the relevance
/// of the note, so it should only be used for notes that are guaranteed to be relevant to the
/// client.
pub(crate) fn apply_new_public_note(
&mut self,
mut public_note_data: InputNoteRecord,
block_header: &BlockHeader,
) -> Result<(), ClientError> {
public_note_data.block_header_received(block_header)?;
self.insert_input_note(public_note_data, NoteUpdateType::Insert);
Ok(())
}
/// Applies the necessary state transitions to the [`NoteUpdateTracker`] when a note is
/// committed in a block and returns whether the committed note is tracked as input note.
pub(crate) fn apply_committed_note_state_transitions(
&mut self,
committed_note: &CommittedNote,
block_header: &BlockHeader,
) -> Result<bool, ClientError> {
let inclusion_proof = committed_note.inclusion_proof().clone();
let is_tracked_as_input_note =
if let Some(input_note_record) = self.get_input_note_by_id(*committed_note.note_id()) {
let metadata = committed_note.metadata().cloned().ok_or_else(|| {
ClientError::RpcError(RpcError::ExpectedDataMissing(format!(
"full metadata for committed note {}",
committed_note.note_id()
)))
})?;
input_note_record.inclusion_proof_received(inclusion_proof.clone(), metadata)?;
input_note_record.block_header_received(block_header)?;
true
} else {
false
};
self.try_commit_output_note(*committed_note.note_id(), inclusion_proof)?;
Ok(is_tracked_as_input_note)
}
/// Applies inclusion proofs from the transaction sync response to tracked output notes.
///
/// This transitions output notes from `Expected` to `Committed` state using the
/// inclusion proofs returned by `SyncTransactions`.
pub(crate) fn apply_output_note_inclusion_proofs(
&mut self,
committed_notes: &[CommittedNote],
) -> Result<(), ClientError> {
for committed_note in committed_notes {
self.try_commit_output_note(
*committed_note.note_id(),
committed_note.inclusion_proof().clone(),
)?;
}
Ok(())
}
/// Marks an erased note as consumed.
///
/// This handles notes that were erased due to same-batch note erasure: the note was
/// created and consumed within the same batch, so it never appeared in the block body.
/// The `block_num` is the block in which the creating transaction was committed.
pub(crate) fn mark_erased_note_as_consumed(
&mut self,
note_id: NoteId,
block_num: BlockNumber,
) -> Result<(), ClientError> {
if let Some(output_note) = self.get_output_note_by_id(note_id)
&& !output_note.is_consumed()
&& !output_note.is_committed()
&& let Some(nullifier) = output_note.nullifier()
{
output_note.nullifier_received(nullifier, block_num)?;
}
// Also mark the corresponding input note if tracked.
if let Some(input_note_update) = self.input_notes.get_mut(¬e_id)
&& !input_note_update.inner().is_consumed()
{
let nullifier = input_note_update.inner().nullifier();
let consumer = input_note_update.inner().metadata().and_then(|m| {
miden_standards::note::NetworkAccountTarget::try_from(m.attachment())
.ok()
.map(|t| t.target_id())
});
input_note_update
.inner_mut()
.consumed_externally(nullifier, block_num, consumer)?;
input_note_update.inner_mut().set_consumed_tx_order(Some(0));
}
Ok(())
}
/// If the note is tracked as an output note, transitions it to `Committed` with the
/// given inclusion proof. No-op if the note is not tracked.
fn try_commit_output_note(
&mut self,
note_id: NoteId,
inclusion_proof: NoteInclusionProof,
) -> Result<(), ClientError> {
if let Some(output_note) = self.get_output_note_by_id(note_id) {
output_note.inclusion_proof_received(inclusion_proof)?;
}
Ok(())
}
/// Applies the necessary state transitions to the [`NoteUpdateTracker`] when a note is
/// nullified in a block.
///
/// For input note records two possible scenarios are considered:
/// 1. The note was being processed by a local transaction that just got committed.
/// 2. The note was consumed by a transaction not submitted by this client. This includes
/// consumption by untracked accounts as well as consumption by tracked accounts whose
/// transactions were submitted by other client instances. If a local transaction was
/// processing the note and it didn't get committed, the transaction should be discarded.
pub(crate) fn apply_nullifiers_state_transitions<'a>(
&mut self,
nullifier_update: &NullifierUpdate,
mut committed_transactions: impl Iterator<Item = &'a TransactionRecord>,
external_consumer_account: Option<AccountId>,
) -> Result<(), ClientError> {
let order = self.get_nullifier_order(nullifier_update.nullifier);
if let Some(input_note_update) =
self.get_input_note_update_by_nullifier(nullifier_update.nullifier)
{
if let Some(consumer_transaction) = committed_transactions
.find(|t| input_note_update.inner().consumer_transaction_id() == Some(&t.id))
{
// The note was being processed by a local transaction that just got committed
if let TransactionStatus::Committed { block_number, .. } =
consumer_transaction.status
{
input_note_update
.inner_mut()
.transaction_committed(consumer_transaction.id, block_number)?;
}
} else {
// The note was consumed by a transaction not submitted by this client.
// If the consuming account is tracked, external_consumer_account will be Some.
input_note_update.inner_mut().consumed_externally(
nullifier_update.nullifier,
nullifier_update.block_num,
external_consumer_account,
)?;
}
input_note_update.inner_mut().set_consumed_tx_order(order);
}
if let Some(output_note_record) =
self.get_output_note_by_nullifier(nullifier_update.nullifier)
{
output_note_record
.nullifier_received(nullifier_update.nullifier, nullifier_update.block_num)?;
}
Ok(())
}
// PRIVATE HELPERS
// --------------------------------------------------------------------------------------------
/// Returns the position of the given nullifier in the consuming transaction order, or `None`
/// if it is not present.
fn get_nullifier_order(&self, nullifier: Nullifier) -> Option<u32> {
self.nullifier_order.get(&nullifier).copied()
}
/// Returns a mutable reference to the input note record with the provided ID if it exists.
fn get_input_note_by_id(&mut self, note_id: NoteId) -> Option<&mut InputNoteRecord> {
self.input_notes.get_mut(¬e_id).map(InputNoteUpdate::inner_mut)
}
/// Returns a mutable reference to the output note record with the provided ID if it exists.
fn get_output_note_by_id(&mut self, note_id: NoteId) -> Option<&mut OutputNoteRecord> {
self.output_notes.get_mut(¬e_id).map(OutputNoteUpdate::inner_mut)
}
/// Returns a mutable reference to the input note update with the provided nullifier if it
/// exists.
fn get_input_note_update_by_nullifier(
&mut self,
nullifier: Nullifier,
) -> Option<&mut InputNoteUpdate> {
let note_id = self.input_notes_by_nullifier.get(&nullifier).copied()?;
self.input_notes.get_mut(¬e_id)
}
/// Returns a mutable reference to the output note record with the provided nullifier if it
/// exists.
fn get_output_note_by_nullifier(
&mut self,
nullifier: Nullifier,
) -> Option<&mut OutputNoteRecord> {
let note_id = self.output_notes_by_nullifier.get(&nullifier).copied()?;
self.output_notes.get_mut(¬e_id).map(OutputNoteUpdate::inner_mut)
}
/// Insert an input note update
fn insert_input_note(&mut self, note: InputNoteRecord, update_type: NoteUpdateType) {
let note_id = note.id();
let nullifier = note.nullifier();
self.input_notes_by_nullifier.insert(nullifier, note_id);
let update = match update_type {
NoteUpdateType::None => InputNoteUpdate::new_none(note),
NoteUpdateType::Insert => InputNoteUpdate::new_insert(note),
NoteUpdateType::Update => InputNoteUpdate::new_update(note),
};
self.input_notes.insert(note_id, update);
}
/// Insert an output note update
fn insert_output_note(&mut self, note: OutputNoteRecord, update_type: NoteUpdateType) {
let note_id = note.id();
if let Some(nullifier) = note.nullifier() {
self.output_notes_by_nullifier.insert(nullifier, note_id);
}
let update = match update_type {
NoteUpdateType::None => OutputNoteUpdate::new_none(note),
NoteUpdateType::Insert => OutputNoteUpdate::new_insert(note),
NoteUpdateType::Update => OutputNoteUpdate::new_update(note),
};
self.output_notes.insert(note_id, update);
}
}