@aztec/pxe 0.0.1-commit.e6bd8901 → 0.0.1-commit.ee80a48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/dest/block_synchronizer/block_synchronizer.d.ts +3 -3
  2. package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
  3. package/dest/block_synchronizer/block_synchronizer.js +5 -5
  4. package/dest/contract_function_simulator/contract_function_simulator.d.ts +2 -4
  5. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  6. package/dest/contract_function_simulator/contract_function_simulator.js +7 -9
  7. package/dest/contract_function_simulator/oracle/interfaces.d.ts +8 -8
  8. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  9. package/dest/contract_function_simulator/oracle/oracle.d.ts +3 -3
  10. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  11. package/dest/contract_function_simulator/oracle/oracle.js +15 -15
  12. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +2 -3
  13. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
  14. package/dest/contract_function_simulator/oracle/private_execution_oracle.js +6 -8
  15. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +20 -16
  16. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  17. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +23 -27
  18. package/dest/contract_sync/index.d.ts +4 -3
  19. package/dest/contract_sync/index.d.ts.map +1 -1
  20. package/dest/contract_sync/index.js +12 -7
  21. package/dest/debug/pxe_debug_utils.d.ts +16 -6
  22. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  23. package/dest/debug/pxe_debug_utils.js +17 -8
  24. package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
  25. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
  26. package/dest/entrypoints/client/bundle/utils.js +12 -6
  27. package/dest/entrypoints/client/lazy/utils.d.ts +2 -2
  28. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  29. package/dest/entrypoints/client/lazy/utils.js +13 -7
  30. package/dest/entrypoints/pxe_creation_options.d.ts +3 -2
  31. package/dest/entrypoints/pxe_creation_options.d.ts.map +1 -1
  32. package/dest/entrypoints/server/utils.d.ts +1 -1
  33. package/dest/entrypoints/server/utils.d.ts.map +1 -1
  34. package/dest/entrypoints/server/utils.js +19 -8
  35. package/dest/events/event_service.d.ts +4 -5
  36. package/dest/events/event_service.d.ts.map +1 -1
  37. package/dest/events/event_service.js +5 -6
  38. package/dest/job_coordinator/job_coordinator.d.ts +3 -2
  39. package/dest/job_coordinator/job_coordinator.d.ts.map +1 -1
  40. package/dest/job_coordinator/job_coordinator.js +3 -2
  41. package/dest/logs/log_service.d.ts +6 -5
  42. package/dest/logs/log_service.d.ts.map +1 -1
  43. package/dest/logs/log_service.js +10 -16
  44. package/dest/notes/note_service.d.ts +4 -5
  45. package/dest/notes/note_service.d.ts.map +1 -1
  46. package/dest/notes/note_service.js +6 -8
  47. package/dest/oracle_version.d.ts +3 -3
  48. package/dest/oracle_version.d.ts.map +1 -1
  49. package/dest/oracle_version.js +2 -2
  50. package/dest/private_kernel/private_kernel_execution_prover.d.ts +3 -2
  51. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  52. package/dest/private_kernel/private_kernel_execution_prover.js +2 -2
  53. package/dest/private_kernel/private_kernel_oracle.d.ts +3 -3
  54. package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
  55. package/dest/pxe.d.ts +1 -1
  56. package/dest/pxe.d.ts.map +1 -1
  57. package/dest/pxe.js +12 -11
  58. package/dest/storage/address_store/address_store.d.ts +1 -1
  59. package/dest/storage/address_store/address_store.d.ts.map +1 -1
  60. package/dest/storage/address_store/address_store.js +12 -11
  61. package/dest/storage/anchor_block_store/anchor_block_store.d.ts +9 -1
  62. package/dest/storage/anchor_block_store/anchor_block_store.d.ts.map +1 -1
  63. package/dest/storage/anchor_block_store/anchor_block_store.js +8 -1
  64. package/dest/storage/capsule_store/capsule_store.js +6 -8
  65. package/dest/storage/contract_store/contract_store.d.ts +1 -1
  66. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  67. package/dest/storage/contract_store/contract_store.js +22 -13
  68. package/dest/storage/metadata.d.ts +1 -1
  69. package/dest/storage/metadata.js +1 -1
  70. package/dest/storage/note_store/note_store.d.ts +11 -1
  71. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  72. package/dest/storage/note_store/note_store.js +143 -105
  73. package/dest/storage/private_event_store/private_event_store.d.ts +1 -1
  74. package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
  75. package/dest/storage/private_event_store/private_event_store.js +84 -61
  76. package/dest/storage/private_event_store/stored_private_event.d.ts +4 -4
  77. package/dest/storage/private_event_store/stored_private_event.d.ts.map +1 -1
  78. package/dest/storage/private_event_store/stored_private_event.js +2 -2
  79. package/dest/storage/tagging_store/recipient_tagging_store.d.ts +1 -1
  80. package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
  81. package/dest/storage/tagging_store/recipient_tagging_store.js +31 -19
  82. package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
  83. package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
  84. package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
  85. package/dest/storage/tagging_store/sender_tagging_store.d.ts +1 -1
  86. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  87. package/dest/storage/tagging_store/sender_tagging_store.js +183 -113
  88. package/dest/tagging/get_all_logs_by_tags.d.ts +4 -4
  89. package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -1
  90. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +3 -3
  91. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
  92. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +3 -3
  93. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
  94. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +3 -3
  95. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
  96. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +3 -3
  97. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
  98. package/package.json +16 -16
  99. package/src/block_synchronizer/block_synchronizer.ts +17 -19
  100. package/src/contract_function_simulator/contract_function_simulator.ts +6 -6
  101. package/src/contract_function_simulator/oracle/interfaces.ts +10 -10
  102. package/src/contract_function_simulator/oracle/oracle.ts +27 -17
  103. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +4 -8
  104. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +32 -35
  105. package/src/contract_sync/index.ts +24 -5
  106. package/src/debug/pxe_debug_utils.ts +23 -9
  107. package/src/entrypoints/client/bundle/utils.ts +7 -14
  108. package/src/entrypoints/client/lazy/utils.ts +8 -14
  109. package/src/entrypoints/pxe_creation_options.ts +2 -1
  110. package/src/entrypoints/server/utils.ts +15 -19
  111. package/src/events/event_service.ts +4 -6
  112. package/src/job_coordinator/job_coordinator.ts +4 -3
  113. package/src/logs/log_service.ts +14 -15
  114. package/src/notes/note_service.ts +5 -8
  115. package/src/oracle_version.ts +2 -2
  116. package/src/private_kernel/private_kernel_execution_prover.ts +6 -3
  117. package/src/private_kernel/private_kernel_oracle.ts +2 -2
  118. package/src/pxe.ts +22 -9
  119. package/src/storage/address_store/address_store.ts +15 -15
  120. package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
  121. package/src/storage/capsule_store/capsule_store.ts +8 -8
  122. package/src/storage/contract_store/contract_store.ts +22 -11
  123. package/src/storage/metadata.ts +1 -1
  124. package/src/storage/note_store/note_store.ts +159 -129
  125. package/src/storage/private_event_store/private_event_store.ts +102 -81
  126. package/src/storage/private_event_store/stored_private_event.ts +3 -3
  127. package/src/storage/tagging_store/recipient_tagging_store.ts +31 -21
  128. package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
  129. package/src/storage/tagging_store/sender_tagging_store.ts +210 -126
  130. package/src/tagging/get_all_logs_by_tags.ts +3 -3
  131. package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +2 -2
  132. package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +2 -2
  133. package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +2 -2
  134. package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +3 -3
@@ -1,4 +1,3 @@
1
- import { toArray } from '@aztec/foundation/iterable';
2
1
  import { Semaphore } from '@aztec/foundation/queue';
3
2
  import type { Fr } from '@aztec/foundation/schemas';
4
3
  import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncMultiMap } from '@aztec/kv-store';
@@ -68,32 +67,26 @@ export class NoteStore implements StagedStore {
68
67
  * @param jobId - The job context for staged writes
69
68
  */
70
69
  public addNotes(notes: NoteDao[], scope: AztecAddress, jobId: string): Promise<void[]> {
71
- return this.#withJobLock(jobId, () => Promise.all(notes.map(noteDao => this.#addNote(noteDao, scope, jobId))));
72
- }
73
-
74
- async #addNote(note: NoteDao, scope: AztecAddress, jobId: string) {
75
- const noteForJob =
76
- (await this.#readNote(note.siloedNullifier.toString(), jobId)) ?? new StoredNote(note, new Set());
77
-
78
- // Make sure the note is linked to the scope and staged for this job
79
- noteForJob.addScope(scope.toString());
80
- this.#writeNote(noteForJob, jobId);
70
+ return this.#withJobLock(jobId, () =>
71
+ this.#store.transactionAsync(() =>
72
+ Promise.all(
73
+ notes.map(async note => {
74
+ const noteForJob =
75
+ (await this.#readNote(note.siloedNullifier.toString(), jobId)) ?? new StoredNote(note, new Set());
76
+ noteForJob.addScope(scope.toString());
77
+ this.#writeNote(noteForJob, jobId);
78
+ }),
79
+ ),
80
+ ),
81
+ );
81
82
  }
82
83
 
83
84
  async #readNote(nullifier: string, jobId: string): Promise<StoredNote | undefined> {
84
- // First check staged notes for this job
85
- const noteForJob = this.#getNotesForJob(jobId).get(nullifier);
86
- if (noteForJob) {
87
- return noteForJob;
88
- }
89
-
90
- // Then check persistent storage
85
+ // Always issue DB read to keep IndexedDB transaction alive (they auto-commit when a new micro-task starts and there
86
+ // are no pending read requests). The staged value still takes precedence if it exists.
91
87
  const noteBuffer = await this.#notes.getAsync(nullifier);
92
- if (noteBuffer) {
93
- return StoredNote.fromBuffer(noteBuffer);
94
- }
95
-
96
- return undefined;
88
+ const noteForJob = this.#getNotesForJob(jobId).get(nullifier);
89
+ return noteForJob ?? (noteBuffer ? StoredNote.fromBuffer(noteBuffer) : undefined);
97
90
  }
98
91
 
99
92
  #writeNote(note: StoredNote, jobId: string) {
@@ -112,57 +105,98 @@ export class NoteStore implements StagedStore {
112
105
  * returned once if this is the case)
113
106
  * @throws If filtering by an empty scopes array. Scopes have to be set to undefined or to a non-empty array.
114
107
  */
115
- async getNotes(filter: NotesFilter, jobId: string): Promise<NoteDao[]> {
108
+ getNotes(filter: NotesFilter, jobId: string): Promise<NoteDao[]> {
116
109
  if (filter.scopes !== undefined && filter.scopes.length === 0) {
117
- throw new Error('Trying to get notes with an empty scopes array');
110
+ return Promise.reject(new Error('Trying to get notes with an empty scopes array'));
118
111
  }
119
112
 
120
- const targetStatus = filter.status ?? NoteStatus.ACTIVE;
113
+ return this.#store.transactionAsync(async () => {
114
+ const targetStatus = filter.status ?? NoteStatus.ACTIVE;
115
+
116
+ // The code below might read a bit unnatural, the reason is that we need to be careful in how we use `await` inside
117
+ // `transactionAsync`, otherwise browsers might choose to auto-commit the IndexedDB transaction forcing us to
118
+ // explicitly handle that condition. The rule we need to honor is: do not await unless you generate a database
119
+ // read or write or you're done using the DB for the remainder of the transaction. The following sequence is
120
+ // unsafe in IndexedDB:
121
+ //
122
+ // 1. start transactionAsync()
123
+ // 2. await readDb() <-- OK, transaction alive because we issued DB ops
124
+ // 3. run a bunch of computations (no await involved) <-- OK, tx alive because we are in the same microtask
125
+ // 4. await doSthNotInDb() <-- no DB ops issued in this task, browser's free to decide to commit the tx
126
+ // 5. await readDb() <-- BOOM, TransactionInactiveError
127
+ //
128
+ // Note that the real issue is in step number 5: we try to continue using a transaction that the browser might
129
+ // have already committed.
130
+ //
131
+ // We need to read candidate notes which are either indexed by contract address in the DB (in
132
+ // #nullifiersByContractAddress), or lie in memory for the not yet committed `jobId`.
133
+ // So we collect promises based on both sources without awaiting for them.
134
+ const noteReadPromises: Map<string, Promise<StoredNote | undefined>> = new Map();
135
+
136
+ // Awaiting the getValuesAsync iterator is fine because it's reading from the DB
137
+ for await (const nullifier of this.#nullifiersByContractAddress.getValuesAsync(
138
+ filter.contractAddress.toString(),
139
+ )) {
140
+ // Each #readNote will perform a DB read
141
+ noteReadPromises.set(nullifier, this.#readNote(nullifier, jobId));
142
+ }
121
143
 
122
- const foundNotes: Map<string, NoteDao> = new Map();
144
+ // Add staged nullifiers from job, no awaits involved, so we are fine
145
+ for (const storedNote of this.#getNotesForJob(jobId).values()) {
146
+ if (storedNote.noteDao.contractAddress.equals(filter.contractAddress)) {
147
+ const nullifier = storedNote.noteDao.siloedNullifier.toString();
148
+ if (!noteReadPromises.has(nullifier)) {
149
+ noteReadPromises.set(nullifier, Promise.resolve(storedNote));
150
+ }
151
+ }
152
+ }
123
153
 
124
- const nullifiersOfContract = await this.#nullifiersOfContract(filter.contractAddress, jobId);
125
- for (const nullifier of nullifiersOfContract) {
126
- const note = await this.#readNote(nullifier, jobId);
154
+ // By now we have pending DB requests from all the #readNote calls. Await them all together.
155
+ const notes = await Promise.all(noteReadPromises.values());
127
156
 
128
- // Defensive: hitting this case means we're mishandling contract indices or in-memory job data
129
- if (!note) {
130
- throw new Error('PXE note database is corrupted.');
131
- }
157
+ // The rest of the function is await-free, and just deals with filtering and sorting our findings.
158
+ const foundNotes: Map<string, NoteDao> = new Map();
132
159
 
133
- // Apply filters
134
- if (targetStatus === NoteStatus.ACTIVE && note.isNullified()) {
135
- continue;
136
- }
160
+ for (const note of notes) {
161
+ // Defensive: hitting this case means we're mishandling contract indices or in-memory job data
162
+ if (!note) {
163
+ throw new Error('PXE note database is corrupted.');
164
+ }
137
165
 
138
- if (filter.owner && !note.noteDao.owner.equals(filter.owner)) {
139
- continue;
140
- }
166
+ // Apply filters
167
+ if (targetStatus === NoteStatus.ACTIVE && note.isNullified()) {
168
+ continue;
169
+ }
141
170
 
142
- if (filter.storageSlot && !note.noteDao.storageSlot.equals(filter.storageSlot)) {
143
- continue;
144
- }
171
+ if (filter.owner && !note.noteDao.owner.equals(filter.owner)) {
172
+ continue;
173
+ }
145
174
 
146
- if (filter.siloedNullifier && !note.noteDao.siloedNullifier.equals(filter.siloedNullifier)) {
147
- continue;
148
- }
175
+ if (filter.storageSlot && !note.noteDao.storageSlot.equals(filter.storageSlot)) {
176
+ continue;
177
+ }
149
178
 
150
- if (filter.scopes && note.scopes.intersection(new Set(filter.scopes.map(s => s.toString()))).size === 0) {
151
- continue;
152
- }
179
+ if (filter.siloedNullifier && !note.noteDao.siloedNullifier.equals(filter.siloedNullifier)) {
180
+ continue;
181
+ }
153
182
 
154
- foundNotes.set(note.noteDao.siloedNullifier.toString(), note.noteDao);
155
- }
183
+ if (filter.scopes && note.scopes.intersection(new Set(filter.scopes.map(s => s.toString()))).size === 0) {
184
+ continue;
185
+ }
156
186
 
157
- // Sort by block number, then by tx index within block, then by note index within tx
158
- return [...foundNotes.values()].sort((a, b) => {
159
- if (a.l2BlockNumber !== b.l2BlockNumber) {
160
- return a.l2BlockNumber - b.l2BlockNumber;
161
- }
162
- if (a.txIndexInBlock !== b.txIndexInBlock) {
163
- return a.txIndexInBlock - b.txIndexInBlock;
187
+ foundNotes.set(note.noteDao.siloedNullifier.toString(), note.noteDao);
164
188
  }
165
- return a.noteIndexInTx - b.noteIndexInTx;
189
+
190
+ // Sort by block number, then by tx index within block, then by note index within tx
191
+ return [...foundNotes.values()].sort((a, b) => {
192
+ if (a.l2BlockNumber !== b.l2BlockNumber) {
193
+ return a.l2BlockNumber - b.l2BlockNumber;
194
+ }
195
+ if (a.txIndexInBlock !== b.txIndexInBlock) {
196
+ return a.txIndexInBlock - b.txIndexInBlock;
197
+ }
198
+ return a.noteIndexInTx - b.noteIndexInTx;
199
+ });
166
200
  });
167
201
  }
168
202
 
@@ -182,12 +216,12 @@ export class NoteStore implements StagedStore {
182
216
  * @throws Error if any nullifier is not found in this notes store
183
217
  */
184
218
  applyNullifiers(nullifiers: DataInBlock<Fr>[], jobId: string): Promise<NoteDao[]> {
219
+ if (nullifiers.length === 0) {
220
+ return Promise.resolve([]);
221
+ }
222
+
185
223
  return this.#withJobLock(jobId, () =>
186
224
  this.#store.transactionAsync(async () => {
187
- if (nullifiers.length === 0) {
188
- return [];
189
- }
190
-
191
225
  const notesToNullify = await Promise.all(
192
226
  nullifiers.map(async nullifierInBlock => {
193
227
  const nullifier = nullifierInBlock.data.toString();
@@ -197,14 +231,13 @@ export class NoteStore implements StagedStore {
197
231
  throw new Error(`Attempted to mark a note as nullified which does not exist in PXE DB`);
198
232
  }
199
233
 
200
- return { storedNote: await this.#readNote(nullifier, jobId), blockNumber: nullifierInBlock.l2BlockNumber };
234
+ return { storedNote, blockNumber: nullifierInBlock.l2BlockNumber };
201
235
  }),
202
236
  );
203
237
 
204
238
  const notesNullifiedInThisCall: Map<string, NoteDao> = new Map();
205
239
  for (const noteToNullify of notesToNullify) {
206
- // Safe to coerce (!) because we throw if we find any undefined above
207
- const note = noteToNullify.storedNote!;
240
+ const note = noteToNullify.storedNote;
208
241
 
209
242
  // Skip already nullified notes
210
243
  if (note.isNullified()) {
@@ -249,18 +282,23 @@ export class NoteStore implements StagedStore {
249
282
  * @param blockNumber - Notes created after this block number will be deleted
250
283
  */
251
284
  async #deleteActiveNotesAfterBlock(blockNumber: number): Promise<void> {
252
- const notes = await toArray(this.#notes.valuesAsync());
253
- for (const noteBuffer of notes) {
285
+ // Collect notes to delete during iteration to keep IndexedDB transaction alive.
286
+ const notesToDelete: { nullifier: string; contractAddress: string }[] = [];
287
+ for await (const noteBuffer of this.#notes.valuesAsync()) {
254
288
  const storedNote = StoredNote.fromBuffer(noteBuffer);
255
289
  if (storedNote.noteDao.l2BlockNumber > blockNumber) {
256
- const noteNullifier = storedNote.noteDao.siloedNullifier.toString();
257
- await this.#notes.delete(noteNullifier);
258
- await this.#nullifiersByContractAddress.deleteValue(
259
- storedNote.noteDao.contractAddress.toString(),
260
- noteNullifier,
261
- );
290
+ notesToDelete.push({
291
+ nullifier: storedNote.noteDao.siloedNullifier.toString(),
292
+ contractAddress: storedNote.noteDao.contractAddress.toString(),
293
+ });
262
294
  }
263
295
  }
296
+
297
+ // Delete all collected notes. Each delete is a DB operation that keeps the transaction alive.
298
+ for (const { nullifier, contractAddress } of notesToDelete) {
299
+ await this.#notes.delete(nullifier);
300
+ await this.#nullifiersByContractAddress.deleteValue(contractAddress, nullifier);
301
+ }
264
302
  }
265
303
 
266
304
  /**
@@ -273,62 +311,69 @@ export class NoteStore implements StagedStore {
273
311
  * @param anchorBlockNumber - Upper bound for the block range to process
274
312
  */
275
313
  async #rewindNullifiedNotesAfterBlock(blockNumber: number, anchorBlockNumber: number): Promise<void> {
276
- const currentBlockNumber = blockNumber + 1;
277
- for (let i = currentBlockNumber; i <= anchorBlockNumber; i++) {
278
- const noteNullifiersToReinsert: string[] = await toArray(
279
- this.#nullifiersByNullificationBlockNumber.getValuesAsync(i),
280
- );
281
-
282
- const nullifiedNoteBuffers = await Promise.all(
283
- noteNullifiersToReinsert.map(async noteNullifier => {
284
- const note = await this.#notes.getAsync(noteNullifier);
285
-
286
- if (!note) {
287
- throw new Error(`PXE DB integrity error: no note found with nullifier ${noteNullifier}`);
288
- }
289
-
290
- return note;
291
- }),
292
- );
293
-
294
- const storedNotes = nullifiedNoteBuffers.map(buffer => StoredNote.fromBuffer(buffer));
314
+ // First pass: collect all nullifiers for all blocks, starting reads during iteration to keep tx alive.
315
+ const nullifiersByBlock: Map<number, { nullifier: string; noteReadPromise: Promise<Buffer | undefined> }[]> =
316
+ new Map();
317
+
318
+ for (let i = blockNumber + 1; i <= anchorBlockNumber; i++) {
319
+ const blockNullifiers: { nullifier: string; noteReadPromise: Promise<Buffer | undefined> }[] = [];
320
+ for await (const nullifier of this.#nullifiersByNullificationBlockNumber.getValuesAsync(i)) {
321
+ // Start read immediately during iteration to keep IndexedDB transaction alive
322
+ blockNullifiers.push({ nullifier, noteReadPromise: this.#notes.getAsync(nullifier) });
323
+ }
324
+ if (blockNullifiers.length > 0) {
325
+ nullifiersByBlock.set(i, blockNullifiers);
326
+ }
327
+ }
295
328
 
296
- for (const storedNote of storedNotes) {
297
- const noteNullifier = storedNote.noteDao.siloedNullifier.toString();
298
- const scopes = storedNote.scopes;
329
+ // Second pass: await reads and perform writes
330
+ for (const [block, nullifiers] of nullifiersByBlock) {
331
+ for (const { nullifier, noteReadPromise } of nullifiers) {
332
+ const noteBuffer = await noteReadPromise;
333
+ if (!noteBuffer) {
334
+ throw new Error(`PXE DB integrity error: no note found with nullifier ${nullifier}`);
335
+ }
299
336
 
300
- if (scopes.size === 0) {
301
- // We should never run into this error because notes always have a scope assigned to them - either on initial
302
- // insertion via `addNotes` or when removing their nullifiers.
303
- throw new Error(`No scopes found for nullified note with nullifier ${noteNullifier}`);
337
+ const storedNote = StoredNote.fromBuffer(noteBuffer);
338
+ if (storedNote.scopes.size === 0) {
339
+ throw new Error(`No scopes found for nullified note with nullifier ${nullifier}`);
304
340
  }
305
341
 
306
342
  storedNote.markAsActive();
307
343
 
308
344
  await Promise.all([
309
- this.#notes.set(noteNullifier, storedNote.toBuffer()),
310
- this.#nullifiersByNullificationBlockNumber.deleteValue(i, noteNullifier),
345
+ this.#notes.set(nullifier, storedNote.toBuffer()),
346
+ this.#nullifiersByNullificationBlockNumber.deleteValue(block, nullifier),
311
347
  ]);
312
348
  }
313
349
  }
314
350
  }
315
351
 
316
- commit(jobId: string): Promise<void> {
317
- return this.#withJobLock(jobId, async () => {
318
- for (const [nullifier, storedNote] of this.#getNotesForJob(jobId)) {
319
- await this.#notes.set(nullifier, storedNote.toBuffer());
320
- await this.#nullifiersByContractAddress.set(storedNote.noteDao.contractAddress.toString(), nullifier);
321
- if (storedNote.nullifiedAt !== undefined) {
322
- await this.#nullifiersByNullificationBlockNumber.set(storedNote.nullifiedAt, nullifier);
323
- }
352
+ /**
353
+ * Commits in memory job data to persistent storage.
354
+ *
355
+ * Called by JobCoordinator when a job completes successfully.
356
+ *
357
+ * Note: JobCoordinator wraps all commits in a single transaction, so we don't need our own transactionAsync here
358
+ * (and using one would throw on IndexedDB as it does not support nested txs).
359
+ *
360
+ * @param jobId - The jobId identifying which staged data to commit
361
+ */
362
+ async commit(jobId: string): Promise<void> {
363
+ for (const [nullifier, storedNote] of this.#getNotesForJob(jobId)) {
364
+ await this.#notes.set(nullifier, storedNote.toBuffer());
365
+ await this.#nullifiersByContractAddress.set(storedNote.noteDao.contractAddress.toString(), nullifier);
366
+ if (storedNote.nullifiedAt !== undefined) {
367
+ await this.#nullifiersByNullificationBlockNumber.set(storedNote.nullifiedAt, nullifier);
324
368
  }
369
+ }
325
370
 
326
- this.#clearJobData(jobId);
327
- });
371
+ this.#clearJobData(jobId);
328
372
  }
329
373
 
330
374
  discardStaged(jobId: string): Promise<void> {
331
- return this.#withJobLock(jobId, () => Promise.resolve(this.#clearJobData(jobId)));
375
+ this.#clearJobData(jobId);
376
+ return Promise.resolve();
332
377
  }
333
378
 
334
379
  #clearJobData(jobId: string) {
@@ -363,19 +408,4 @@ export class NoteStore implements StagedStore {
363
408
  }
364
409
  return notesForJob;
365
410
  }
366
-
367
- async #nullifiersOfContract(contractAddress: AztecAddress, jobId: string): Promise<Set<string>> {
368
- // Collect persisted nullifiers for this contract
369
- const persistedNullifiers: string[] = await toArray(
370
- this.#nullifiersByContractAddress.getValuesAsync(contractAddress.toString()),
371
- );
372
-
373
- // Collect staged nullifiers from the job where the note's contract matches
374
- const stagedNullifiers = this.#getNotesForJob(jobId)
375
- .values()
376
- .filter(storedNote => storedNote.noteDao.contractAddress.equals(contractAddress))
377
- .map(storedNote => storedNote.noteDao.siloedNullifier.toString());
378
-
379
- return new Set([...persistedNullifiers, ...stagedNullifiers]);
380
- }
381
411
  }
@@ -1,6 +1,5 @@
1
1
  import { BlockNumber } from '@aztec/foundation/branded-types';
2
2
  import { Fr } from '@aztec/foundation/curves/bn254';
3
- import { toArray } from '@aztec/foundation/iterable';
4
3
  import { createLogger } from '@aztec/foundation/log';
5
4
  import { Semaphore } from '@aztec/foundation/queue';
6
5
  import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncMultiMap } from '@aztec/kv-store';
@@ -81,43 +80,45 @@ export class PrivateEventStore implements StagedStore {
81
80
  metadata: PrivateEventMetadata,
82
81
  jobId: string,
83
82
  ) {
84
- return this.#withJobLock(jobId, async () => {
85
- const { contractAddress, scope, txHash, l2BlockNumber, l2BlockHash, txIndexInBlock, eventIndexInTx } = metadata;
86
- const eventId = siloedEventCommitment.toString();
87
-
88
- this.logger.verbose('storing private event log (job stage)', {
89
- eventId,
90
- contractAddress,
91
- scope,
92
- msgContent,
93
- l2BlockNumber,
94
- });
95
-
96
- const existing = await this.#readEvent(eventId, jobId);
83
+ return this.#withJobLock(jobId, () =>
84
+ this.#store.transactionAsync(async () => {
85
+ const { contractAddress, scope, txHash, l2BlockNumber, l2BlockHash, txIndexInBlock, eventIndexInTx } = metadata;
86
+ const eventId = siloedEventCommitment.toString();
97
87
 
98
- if (existing) {
99
- // If we already stored this event, we still want to make sure to track it for the given scope
100
- existing.addScope(scope.toString());
101
- this.#writeEvent(eventId, existing, jobId);
102
- } else {
103
- this.#writeEvent(
88
+ this.logger.verbose('storing private event log (job stage)', {
104
89
  eventId,
105
- new StoredPrivateEvent(
106
- randomness,
107
- msgContent,
108
- l2BlockNumber,
109
- l2BlockHash,
110
- txHash,
111
- txIndexInBlock,
112
- eventIndexInTx,
113
- contractAddress,
114
- eventSelector,
115
- new Set([scope.toString()]),
116
- ),
117
- jobId,
118
- );
119
- }
120
- });
90
+ contractAddress,
91
+ scope,
92
+ msgContent,
93
+ l2BlockNumber,
94
+ });
95
+
96
+ const existing = await this.#readEvent(eventId, jobId);
97
+
98
+ if (existing) {
99
+ // If we already stored this event, we still want to make sure to track it for the given scope
100
+ existing.addScope(scope.toString());
101
+ this.#writeEvent(eventId, existing, jobId);
102
+ } else {
103
+ this.#writeEvent(
104
+ eventId,
105
+ new StoredPrivateEvent(
106
+ randomness,
107
+ msgContent,
108
+ l2BlockNumber,
109
+ l2BlockHash,
110
+ txHash,
111
+ txIndexInBlock,
112
+ eventIndexInTx,
113
+ contractAddress,
114
+ eventSelector,
115
+ new Set([scope.toString()]),
116
+ ),
117
+ jobId,
118
+ );
119
+ }
120
+ }),
121
+ );
121
122
  }
122
123
 
123
124
  /**
@@ -136,6 +137,20 @@ export class PrivateEventStore implements StagedStore {
136
137
  filter: PrivateEventStoreFilter,
137
138
  ): Promise<PackedPrivateEvent[]> {
138
139
  return this.#store.transactionAsync(async () => {
140
+ const key = this.#keyFor(filter.contractAddress, eventSelector);
141
+ const targetScopes = new Set(filter.scopes.map(s => s.toString()));
142
+
143
+ // Map from eventId to the promise that reads the event buffer.
144
+ // We start reads during iteration to keep DB requests pending and avoid IndexedDB auto-commit.
145
+ const eventReadPromises: Map<string, Promise<Buffer | undefined>> = new Map();
146
+
147
+ for await (const eventId of this.#eventsByContractAndEventSelector.getValuesAsync(key)) {
148
+ eventReadPromises.set(eventId, this.#events.getAsync(eventId));
149
+ }
150
+
151
+ const eventIds = [...eventReadPromises.keys()];
152
+ const eventBuffers = await Promise.all(eventReadPromises.values());
153
+
139
154
  const events: Array<{
140
155
  l2BlockNumber: number;
141
156
  txIndexInBlock: number;
@@ -143,13 +158,9 @@ export class PrivateEventStore implements StagedStore {
143
158
  event: PackedPrivateEvent;
144
159
  }> = [];
145
160
 
146
- const key = this.#keyFor(filter.contractAddress, eventSelector);
147
- const targetScopes = new Set(filter.scopes.map(s => s.toString()));
148
-
149
- const eventIds: string[] = await toArray(this.#eventsByContractAndEventSelector.getValuesAsync(key));
150
-
151
- for (const eventId of eventIds) {
152
- const eventBuffer = await this.#events.getAsync(eventId);
161
+ for (let i = 0; i < eventIds.length; i++) {
162
+ const eventId = eventIds[i];
163
+ const eventBuffer = eventBuffers[i];
153
164
 
154
165
  // Defensive, if it happens, there's a problem with how we're handling #eventsByContractAndEventSelector
155
166
  if (!eventBuffer) {
@@ -223,29 +234,39 @@ export class PrivateEventStore implements StagedStore {
223
234
  * IMPORTANT: This method must be called within a transaction to ensure atomicity.
224
235
  */
225
236
  public async rollback(blockNumber: number, synchedBlockNumber: number): Promise<void> {
226
- let removedCount = 0;
237
+ // First pass: collect all event IDs for all blocks, starting reads during iteration to keep tx alive.
238
+ const eventsByBlock: Map<number, { eventId: string; eventReadPromise: Promise<Buffer | undefined> }[]> = new Map();
227
239
 
228
240
  for (let block = blockNumber + 1; block <= synchedBlockNumber; block++) {
229
- const eventIds: string[] = await toArray(this.#eventsByBlockNumber.getValuesAsync(block));
241
+ const blockEvents: { eventId: string; eventReadPromise: Promise<Buffer | undefined> }[] = [];
242
+ for await (const eventId of this.#eventsByBlockNumber.getValuesAsync(block)) {
243
+ // Start read immediately during iteration to keep IndexedDB transaction alive
244
+ blockEvents.push({ eventId, eventReadPromise: this.#events.getAsync(eventId) });
245
+ }
246
+ if (blockEvents.length > 0) {
247
+ eventsByBlock.set(block, blockEvents);
248
+ }
249
+ }
230
250
 
231
- if (eventIds.length > 0) {
232
- await this.#eventsByBlockNumber.delete(block);
251
+ // Second pass: await reads and perform deletes
252
+ let removedCount = 0;
253
+ for (const [block, events] of eventsByBlock) {
254
+ await this.#eventsByBlockNumber.delete(block);
233
255
 
234
- for (const eventId of eventIds) {
235
- const buffer = await this.#events.getAsync(eventId);
236
- if (!buffer) {
237
- throw new Error(`Event not found for eventId ${eventId}`);
238
- }
256
+ for (const { eventId, eventReadPromise } of events) {
257
+ const buffer = await eventReadPromise;
258
+ if (!buffer) {
259
+ throw new Error(`Event not found for eventId ${eventId}`);
260
+ }
239
261
 
240
- const entry = StoredPrivateEvent.fromBuffer(buffer);
241
- await this.#events.delete(eventId);
242
- await this.#eventsByContractAndEventSelector.deleteValue(
243
- this.#keyFor(entry.contractAddress, entry.eventSelector),
244
- eventId,
245
- );
262
+ const entry = StoredPrivateEvent.fromBuffer(buffer);
263
+ await this.#events.delete(eventId);
264
+ await this.#eventsByContractAndEventSelector.deleteValue(
265
+ this.#keyFor(entry.contractAddress, entry.eventSelector),
266
+ eventId,
267
+ );
246
268
 
247
- removedCount++;
248
- }
269
+ removedCount++;
249
270
  }
250
271
  }
251
272
 
@@ -262,28 +283,30 @@ export class PrivateEventStore implements StagedStore {
262
283
  *
263
284
  * @param jobId - The jobId identifying which staged data to commit
264
285
  */
265
- commit(jobId: string): Promise<void> {
266
- return this.#withJobLock(jobId, async () => {
267
- for (const [eventId, entry] of this.#getEventsForJob(jobId).entries()) {
268
- const lookupKey = this.#keyFor(entry.contractAddress, entry.eventSelector);
269
- this.logger.verbose('storing private event log', { eventId, lookupKey });
270
-
271
- await Promise.all([
272
- this.#events.set(eventId, entry.toBuffer()),
273
- this.#eventsByContractAndEventSelector.set(lookupKey, eventId),
274
- this.#eventsByBlockNumber.set(entry.l2BlockNumber, eventId),
275
- ]);
276
- }
286
+ async commit(jobId: string): Promise<void> {
287
+ // Note: Don't use #withJobLock here - commit runs within JobCoordinator's transactionAsync,
288
+ // and awaiting the lock would create a microtask boundary with no pending DB request,
289
+ // causing IndexedDB to auto-commit the transaction.
290
+ for (const [eventId, entry] of this.#getEventsForJob(jobId).entries()) {
291
+ const lookupKey = this.#keyFor(entry.contractAddress, entry.eventSelector);
292
+ this.logger.verbose('storing private event log', { eventId, lookupKey });
293
+
294
+ await Promise.all([
295
+ this.#events.set(eventId, entry.toBuffer()),
296
+ this.#eventsByContractAndEventSelector.set(lookupKey, eventId),
297
+ this.#eventsByBlockNumber.set(entry.l2BlockNumber, eventId),
298
+ ]);
299
+ }
277
300
 
278
- this.#clearJobData(jobId);
279
- });
301
+ this.#clearJobData(jobId);
280
302
  }
281
303
 
282
304
  /**
283
305
  * Discards in memory job data without persisting it.
284
306
  */
285
307
  discardStaged(jobId: string): Promise<void> {
286
- return this.#withJobLock(jobId, () => Promise.resolve(this.#clearJobData(jobId)));
308
+ this.#clearJobData(jobId);
309
+ return Promise.resolve();
287
310
  }
288
311
 
289
312
  /**
@@ -292,13 +315,11 @@ export class PrivateEventStore implements StagedStore {
292
315
  * Returns undefined if the event does not exist in the store overall.
293
316
  */
294
317
  async #readEvent(eventId: string, jobId: string): Promise<StoredPrivateEvent | undefined> {
295
- const eventForJob = this.#getEventsForJob(jobId).get(eventId);
296
- if (eventForJob) {
297
- return eventForJob;
298
- }
299
-
318
+ // Always issue DB read to keep IndexedDB transaction alive (they auto-commit when a new micro-task starts and there
319
+ // are no pending read requests). The staged value still takes precedence if it exists.
300
320
  const buffer = await this.#events.getAsync(eventId);
301
- return buffer ? StoredPrivateEvent.fromBuffer(buffer) : undefined;
321
+ const eventForJob = this.#getEventsForJob(jobId).get(eventId);
322
+ return eventForJob ?? (buffer ? StoredPrivateEvent.fromBuffer(buffer) : undefined);
302
323
  }
303
324
 
304
325
  /**
@@ -2,7 +2,7 @@ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
3
3
  import { EventSelector } from '@aztec/stdlib/abi';
4
4
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
5
- import { L2BlockHash } from '@aztec/stdlib/block';
5
+ import { BlockHash } from '@aztec/stdlib/block';
6
6
  import { TxHash } from '@aztec/stdlib/tx';
7
7
 
8
8
  /** Serializable private event entry with scope tracking. */
@@ -11,7 +11,7 @@ export class StoredPrivateEvent {
11
11
  readonly randomness: Fr,
12
12
  readonly msgContent: Fr[],
13
13
  readonly l2BlockNumber: number,
14
- readonly l2BlockHash: L2BlockHash,
14
+ readonly l2BlockHash: BlockHash,
15
15
  readonly txHash: TxHash,
16
16
  readonly txIndexInBlock: number,
17
17
  readonly eventIndexInTx: number,
@@ -49,7 +49,7 @@ export class StoredPrivateEvent {
49
49
  const msgContentLength = reader.readNumber();
50
50
  const msgContent = reader.readArray(msgContentLength, Fr);
51
51
  const l2BlockNumber = reader.readNumber();
52
- const l2BlockHash = L2BlockHash.fromBuffer(reader);
52
+ const l2BlockHash = new BlockHash(Fr.fromBuffer(reader));
53
53
  const txHash = TxHash.fromBuffer(reader);
54
54
  const txIndexInBlock = reader.readNumber();
55
55
  const eventIndexInTx = reader.readNumber();