@aztec/pxe 0.0.1-commit.1a99e26c → 0.0.1-commit.1bb068fb5
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.
- package/dest/access_scopes.d.ts +9 -0
- package/dest/access_scopes.d.ts.map +1 -0
- package/dest/access_scopes.js +6 -0
- package/dest/block_synchronizer/block_synchronizer.d.ts +4 -2
- package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
- package/dest/block_synchronizer/block_synchronizer.js +7 -1
- package/dest/contract_function_simulator/contract_function_simulator.d.ts +54 -30
- package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
- package/dest/contract_function_simulator/contract_function_simulator.js +169 -65
- package/dest/contract_function_simulator/oracle/interfaces.d.ts +9 -9
- package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/oracle.d.ts +5 -5
- package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/oracle.js +32 -20
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +36 -36
- package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/private_execution_oracle.js +74 -21
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +48 -24
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
- package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +85 -61
- package/dest/contract_sync/contract_sync_service.d.ts +43 -0
- package/dest/contract_sync/contract_sync_service.d.ts.map +1 -0
- package/dest/contract_sync/contract_sync_service.js +97 -0
- package/dest/contract_sync/helpers.d.ts +29 -0
- package/dest/contract_sync/helpers.d.ts.map +1 -0
- package/dest/contract_sync/{index.js → helpers.js} +13 -12
- package/dest/debug/pxe_debug_utils.d.ts +14 -10
- package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
- package/dest/debug/pxe_debug_utils.js +16 -15
- package/dest/entrypoints/client/bundle/index.d.ts +3 -1
- package/dest/entrypoints/client/bundle/index.d.ts.map +1 -1
- package/dest/entrypoints/client/bundle/index.js +2 -0
- package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
- package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
- package/dest/entrypoints/client/bundle/utils.js +11 -2
- package/dest/entrypoints/client/lazy/index.d.ts +3 -1
- package/dest/entrypoints/client/lazy/index.d.ts.map +1 -1
- package/dest/entrypoints/client/lazy/index.js +2 -0
- package/dest/entrypoints/client/lazy/utils.d.ts +1 -1
- package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
- package/dest/entrypoints/client/lazy/utils.js +11 -2
- package/dest/entrypoints/server/index.d.ts +4 -2
- package/dest/entrypoints/server/index.d.ts.map +1 -1
- package/dest/entrypoints/server/index.js +3 -1
- package/dest/entrypoints/server/utils.js +9 -1
- package/dest/events/event_service.d.ts +4 -5
- package/dest/events/event_service.d.ts.map +1 -1
- package/dest/events/event_service.js +5 -6
- package/dest/logs/log_service.d.ts +6 -5
- package/dest/logs/log_service.d.ts.map +1 -1
- package/dest/logs/log_service.js +14 -24
- package/dest/notes/note_service.d.ts +7 -7
- package/dest/notes/note_service.d.ts.map +1 -1
- package/dest/notes/note_service.js +9 -9
- package/dest/notes_filter.d.ts +25 -0
- package/dest/notes_filter.d.ts.map +1 -0
- package/dest/notes_filter.js +4 -0
- package/dest/oracle_version.d.ts +3 -3
- package/dest/oracle_version.d.ts.map +1 -1
- package/dest/oracle_version.js +2 -2
- package/dest/pxe.d.ts +69 -23
- package/dest/pxe.d.ts.map +1 -1
- package/dest/pxe.js +72 -44
- package/dest/storage/address_store/address_store.d.ts +1 -1
- package/dest/storage/address_store/address_store.d.ts.map +1 -1
- package/dest/storage/address_store/address_store.js +12 -11
- package/dest/storage/anchor_block_store/anchor_block_store.d.ts +9 -1
- package/dest/storage/anchor_block_store/anchor_block_store.d.ts.map +1 -1
- package/dest/storage/anchor_block_store/anchor_block_store.js +8 -1
- package/dest/storage/capsule_store/capsule_store.js +6 -8
- package/dest/storage/contract_store/contract_store.d.ts +1 -1
- package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
- package/dest/storage/contract_store/contract_store.js +27 -18
- package/dest/storage/metadata.d.ts +1 -1
- package/dest/storage/metadata.js +1 -1
- package/dest/storage/note_store/note_store.d.ts +13 -3
- package/dest/storage/note_store/note_store.d.ts.map +1 -1
- package/dest/storage/note_store/note_store.js +173 -131
- package/dest/storage/private_event_store/private_event_store.d.ts +1 -1
- package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
- package/dest/storage/private_event_store/private_event_store.js +126 -101
- package/dest/storage/private_event_store/stored_private_event.js +1 -1
- package/dest/storage/tagging_store/recipient_tagging_store.d.ts +1 -1
- package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/recipient_tagging_store.js +31 -19
- package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
- package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
- package/dest/storage/tagging_store/sender_tagging_store.d.ts +1 -1
- package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
- package/dest/storage/tagging_store/sender_tagging_store.js +183 -113
- package/package.json +25 -16
- package/src/access_scopes.ts +9 -0
- package/src/block_synchronizer/block_synchronizer.ts +6 -0
- package/src/contract_function_simulator/contract_function_simulator.ts +317 -124
- package/src/contract_function_simulator/oracle/interfaces.ts +10 -10
- package/src/contract_function_simulator/oracle/oracle.ts +35 -18
- package/src/contract_function_simulator/oracle/private_execution_oracle.ts +97 -101
- package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +129 -63
- package/src/contract_sync/contract_sync_service.ts +152 -0
- package/src/contract_sync/{index.ts → helpers.ts} +21 -21
- package/src/debug/pxe_debug_utils.ts +48 -18
- package/src/entrypoints/client/bundle/index.ts +2 -0
- package/src/entrypoints/client/bundle/utils.ts +12 -2
- package/src/entrypoints/client/lazy/index.ts +2 -0
- package/src/entrypoints/client/lazy/utils.ts +12 -2
- package/src/entrypoints/server/index.ts +3 -1
- package/src/entrypoints/server/utils.ts +7 -7
- package/src/events/event_service.ts +4 -6
- package/src/logs/log_service.ts +14 -29
- package/src/notes/note_service.ts +9 -10
- package/src/notes_filter.ts +26 -0
- package/src/oracle_version.ts +2 -2
- package/src/pxe.ts +151 -88
- package/src/storage/address_store/address_store.ts +15 -15
- package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
- package/src/storage/capsule_store/capsule_store.ts +8 -8
- package/src/storage/contract_store/contract_store.ts +26 -15
- package/src/storage/metadata.ts +1 -1
- package/src/storage/note_store/note_store.ts +195 -153
- package/src/storage/private_event_store/private_event_store.ts +151 -128
- package/src/storage/private_event_store/stored_private_event.ts +1 -1
- package/src/storage/tagging_store/recipient_tagging_store.ts +31 -21
- package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
- package/src/storage/tagging_store/sender_tagging_store.ts +210 -126
- package/dest/contract_sync/index.d.ts +0 -23
- package/dest/contract_sync/index.d.ts.map +0 -1
|
@@ -1,12 +1,12 @@
|
|
|
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';
|
|
5
4
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
5
|
import type { DataInBlock } from '@aztec/stdlib/block';
|
|
7
|
-
import { NoteDao, NoteStatus
|
|
6
|
+
import { NoteDao, NoteStatus } from '@aztec/stdlib/note';
|
|
8
7
|
|
|
9
8
|
import type { StagedStore } from '../../job_coordinator/job_coordinator.js';
|
|
9
|
+
import type { NotesFilter } from '../../notes_filter.js';
|
|
10
10
|
import { StoredNote } from './stored_note.js';
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -18,6 +18,8 @@ import { StoredNote } from './stored_note.js';
|
|
|
18
18
|
export class NoteStore implements StagedStore {
|
|
19
19
|
readonly storeName: string = 'note';
|
|
20
20
|
|
|
21
|
+
#store: AztecAsyncKVStore;
|
|
22
|
+
|
|
21
23
|
// Note that we use the siloedNullifier as the note id in the store as it's guaranteed to be unique.
|
|
22
24
|
|
|
23
25
|
// Main storage for notes. Avoid performing full scans on it as it contains all notes PXE knows, use
|
|
@@ -46,6 +48,7 @@ export class NoteStore implements StagedStore {
|
|
|
46
48
|
#jobLocks: Map<string, Semaphore>;
|
|
47
49
|
|
|
48
50
|
constructor(store: AztecAsyncKVStore) {
|
|
51
|
+
this.#store = store;
|
|
49
52
|
this.#notes = store.openMap('notes');
|
|
50
53
|
this.#nullifiersByContractAddress = store.openMultiMap('note_nullifiers_by_contract');
|
|
51
54
|
this.#nullifiersByNullificationBlockNumber = store.openMultiMap('note_block_number_to_nullifier');
|
|
@@ -65,32 +68,26 @@ export class NoteStore implements StagedStore {
|
|
|
65
68
|
* @param jobId - The job context for staged writes
|
|
66
69
|
*/
|
|
67
70
|
public addNotes(notes: NoteDao[], scope: AztecAddress, jobId: string): Promise<void[]> {
|
|
68
|
-
return this.#withJobLock(jobId, () =>
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
71
|
+
return this.#withJobLock(jobId, () =>
|
|
72
|
+
this.#store.transactionAsync(() =>
|
|
73
|
+
Promise.all(
|
|
74
|
+
notes.map(async note => {
|
|
75
|
+
const noteForJob =
|
|
76
|
+
(await this.#readNote(note.siloedNullifier.toString(), jobId)) ?? new StoredNote(note, new Set());
|
|
77
|
+
noteForJob.addScope(scope.toString());
|
|
78
|
+
this.#writeNote(noteForJob, jobId);
|
|
79
|
+
}),
|
|
80
|
+
),
|
|
81
|
+
),
|
|
82
|
+
);
|
|
78
83
|
}
|
|
79
84
|
|
|
80
85
|
async #readNote(nullifier: string, jobId: string): Promise<StoredNote | undefined> {
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
if (noteForJob) {
|
|
84
|
-
return noteForJob;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Then check persistent storage
|
|
86
|
+
// Always issue DB read to keep IndexedDB transaction alive (they auto-commit when a new micro-task starts and there
|
|
87
|
+
// are no pending read requests). The staged value still takes precedence if it exists.
|
|
88
88
|
const noteBuffer = await this.#notes.getAsync(nullifier);
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return undefined;
|
|
89
|
+
const noteForJob = this.#getNotesForJob(jobId).get(nullifier);
|
|
90
|
+
return noteForJob ?? (noteBuffer ? StoredNote.fromBuffer(noteBuffer) : undefined);
|
|
94
91
|
}
|
|
95
92
|
|
|
96
93
|
#writeNote(note: StoredNote, jobId: string) {
|
|
@@ -107,59 +104,102 @@ export class NoteStore implements StagedStore {
|
|
|
107
104
|
* @params jobId - the job context to read from.
|
|
108
105
|
* @returns Filtered and deduplicated notes (a note might be present in multiple scopes - we ensure it is only
|
|
109
106
|
* returned once if this is the case)
|
|
110
|
-
* @throws If filtering by an empty scopes array. Scopes have to be set to undefined or to a non-empty array.
|
|
111
107
|
*/
|
|
112
|
-
|
|
113
|
-
if (filter.scopes !==
|
|
114
|
-
|
|
108
|
+
getNotes(filter: NotesFilter, jobId: string): Promise<NoteDao[]> {
|
|
109
|
+
if (filter.scopes !== 'ALL_SCOPES' && filter.scopes.length === 0) {
|
|
110
|
+
return Promise.resolve([]);
|
|
115
111
|
}
|
|
116
112
|
|
|
117
|
-
|
|
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
|
+
}
|
|
118
143
|
|
|
119
|
-
|
|
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
|
+
}
|
|
120
153
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
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());
|
|
124
156
|
|
|
125
|
-
//
|
|
126
|
-
|
|
127
|
-
throw new Error('PXE note database is corrupted.');
|
|
128
|
-
}
|
|
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();
|
|
129
159
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
+
}
|
|
134
165
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
166
|
+
// Apply filters
|
|
167
|
+
if (targetStatus === NoteStatus.ACTIVE && note.isNullified()) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
138
170
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
171
|
+
if (filter.owner && !note.noteDao.owner.equals(filter.owner)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
142
174
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
175
|
+
if (filter.storageSlot && !note.noteDao.storageSlot.equals(filter.storageSlot)) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
146
178
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
179
|
+
if (filter.siloedNullifier && !note.noteDao.siloedNullifier.equals(filter.siloedNullifier)) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
150
182
|
|
|
151
|
-
|
|
152
|
-
|
|
183
|
+
if (
|
|
184
|
+
filter.scopes !== 'ALL_SCOPES' &&
|
|
185
|
+
note.scopes.intersection(new Set(filter.scopes.map(s => s.toString()))).size === 0
|
|
186
|
+
) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
153
189
|
|
|
154
|
-
|
|
155
|
-
return [...foundNotes.values()].sort((a, b) => {
|
|
156
|
-
if (a.l2BlockNumber !== b.l2BlockNumber) {
|
|
157
|
-
return a.l2BlockNumber - b.l2BlockNumber;
|
|
190
|
+
foundNotes.set(note.noteDao.siloedNullifier.toString(), note.noteDao);
|
|
158
191
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
192
|
+
|
|
193
|
+
// Sort by block number, then by tx index within block, then by note index within tx
|
|
194
|
+
return [...foundNotes.values()].sort((a, b) => {
|
|
195
|
+
if (a.l2BlockNumber !== b.l2BlockNumber) {
|
|
196
|
+
return a.l2BlockNumber - b.l2BlockNumber;
|
|
197
|
+
}
|
|
198
|
+
if (a.txIndexInBlock !== b.txIndexInBlock) {
|
|
199
|
+
return a.txIndexInBlock - b.txIndexInBlock;
|
|
200
|
+
}
|
|
201
|
+
return a.noteIndexInTx - b.noteIndexInTx;
|
|
202
|
+
});
|
|
163
203
|
});
|
|
164
204
|
}
|
|
165
205
|
|
|
@@ -179,41 +219,46 @@ export class NoteStore implements StagedStore {
|
|
|
179
219
|
* @throws Error if any nullifier is not found in this notes store
|
|
180
220
|
*/
|
|
181
221
|
applyNullifiers(nullifiers: DataInBlock<Fr>[], jobId: string): Promise<NoteDao[]> {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
222
|
+
if (nullifiers.length === 0) {
|
|
223
|
+
return Promise.resolve([]);
|
|
224
|
+
}
|
|
186
225
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
226
|
+
if (nullifiers.some(n => n.l2BlockNumber === 0)) {
|
|
227
|
+
return Promise.reject(new Error('applyNullifiers: nullifiers cannot have been emitted at block 0'));
|
|
228
|
+
}
|
|
190
229
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
230
|
+
return this.#withJobLock(jobId, () =>
|
|
231
|
+
this.#store.transactionAsync(async () => {
|
|
232
|
+
const notesToNullify = await Promise.all(
|
|
233
|
+
nullifiers.map(async nullifierInBlock => {
|
|
234
|
+
const nullifier = nullifierInBlock.data.toString();
|
|
195
235
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
236
|
+
const storedNote = await this.#readNote(nullifier, jobId);
|
|
237
|
+
if (!storedNote) {
|
|
238
|
+
throw new Error(`Attempted to mark a note as nullified which does not exist in PXE DB`);
|
|
239
|
+
}
|
|
199
240
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const note = noteToNullify.storedNote!;
|
|
241
|
+
return { storedNote, blockNumber: nullifierInBlock.l2BlockNumber };
|
|
242
|
+
}),
|
|
243
|
+
);
|
|
204
244
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
245
|
+
const notesNullifiedInThisCall: Map<string, NoteDao> = new Map();
|
|
246
|
+
for (const noteToNullify of notesToNullify) {
|
|
247
|
+
const note = noteToNullify.storedNote;
|
|
209
248
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
249
|
+
// Skip already nullified notes
|
|
250
|
+
if (note.isNullified()) {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
214
253
|
|
|
215
|
-
|
|
216
|
-
|
|
254
|
+
note.markAsNullified(noteToNullify.blockNumber);
|
|
255
|
+
this.#writeNote(note, jobId);
|
|
256
|
+
notesNullifiedInThisCall.set(note.noteDao.siloedNullifier.toString(), note.noteDao);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return [...notesNullifiedInThisCall.values()];
|
|
260
|
+
}),
|
|
261
|
+
);
|
|
217
262
|
}
|
|
218
263
|
|
|
219
264
|
/**
|
|
@@ -244,18 +289,23 @@ export class NoteStore implements StagedStore {
|
|
|
244
289
|
* @param blockNumber - Notes created after this block number will be deleted
|
|
245
290
|
*/
|
|
246
291
|
async #deleteActiveNotesAfterBlock(blockNumber: number): Promise<void> {
|
|
247
|
-
|
|
248
|
-
|
|
292
|
+
// Collect notes to delete during iteration to keep IndexedDB transaction alive.
|
|
293
|
+
const notesToDelete: { nullifier: string; contractAddress: string }[] = [];
|
|
294
|
+
for await (const noteBuffer of this.#notes.valuesAsync()) {
|
|
249
295
|
const storedNote = StoredNote.fromBuffer(noteBuffer);
|
|
250
296
|
if (storedNote.noteDao.l2BlockNumber > blockNumber) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
noteNullifier,
|
|
256
|
-
);
|
|
297
|
+
notesToDelete.push({
|
|
298
|
+
nullifier: storedNote.noteDao.siloedNullifier.toString(),
|
|
299
|
+
contractAddress: storedNote.noteDao.contractAddress.toString(),
|
|
300
|
+
});
|
|
257
301
|
}
|
|
258
302
|
}
|
|
303
|
+
|
|
304
|
+
// Delete all collected notes. Each delete is a DB operation that keeps the transaction alive.
|
|
305
|
+
for (const { nullifier, contractAddress } of notesToDelete) {
|
|
306
|
+
await this.#notes.delete(nullifier);
|
|
307
|
+
await this.#nullifiersByContractAddress.deleteValue(contractAddress, nullifier);
|
|
308
|
+
}
|
|
259
309
|
}
|
|
260
310
|
|
|
261
311
|
/**
|
|
@@ -268,62 +318,69 @@ export class NoteStore implements StagedStore {
|
|
|
268
318
|
* @param anchorBlockNumber - Upper bound for the block range to process
|
|
269
319
|
*/
|
|
270
320
|
async #rewindNullifiedNotesAfterBlock(blockNumber: number, anchorBlockNumber: number): Promise<void> {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return note;
|
|
286
|
-
}),
|
|
287
|
-
);
|
|
288
|
-
|
|
289
|
-
const storedNotes = nullifiedNoteBuffers.map(buffer => StoredNote.fromBuffer(buffer));
|
|
321
|
+
// First pass: collect all nullifiers for all blocks, starting reads during iteration to keep tx alive.
|
|
322
|
+
const nullifiersByBlock: Map<number, { nullifier: string; noteReadPromise: Promise<Buffer | undefined> }[]> =
|
|
323
|
+
new Map();
|
|
324
|
+
|
|
325
|
+
for (let i = blockNumber + 1; i <= anchorBlockNumber; i++) {
|
|
326
|
+
const blockNullifiers: { nullifier: string; noteReadPromise: Promise<Buffer | undefined> }[] = [];
|
|
327
|
+
for await (const nullifier of this.#nullifiersByNullificationBlockNumber.getValuesAsync(i)) {
|
|
328
|
+
// Start read immediately during iteration to keep IndexedDB transaction alive
|
|
329
|
+
blockNullifiers.push({ nullifier, noteReadPromise: this.#notes.getAsync(nullifier) });
|
|
330
|
+
}
|
|
331
|
+
if (blockNullifiers.length > 0) {
|
|
332
|
+
nullifiersByBlock.set(i, blockNullifiers);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
290
335
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
336
|
+
// Second pass: await reads and perform writes
|
|
337
|
+
for (const [block, nullifiers] of nullifiersByBlock) {
|
|
338
|
+
for (const { nullifier, noteReadPromise } of nullifiers) {
|
|
339
|
+
const noteBuffer = await noteReadPromise;
|
|
340
|
+
if (!noteBuffer) {
|
|
341
|
+
throw new Error(`PXE DB integrity error: no note found with nullifier ${nullifier}`);
|
|
342
|
+
}
|
|
294
343
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
throw new Error(`No scopes found for nullified note with nullifier ${noteNullifier}`);
|
|
344
|
+
const storedNote = StoredNote.fromBuffer(noteBuffer);
|
|
345
|
+
if (storedNote.scopes.size === 0) {
|
|
346
|
+
throw new Error(`No scopes found for nullified note with nullifier ${nullifier}`);
|
|
299
347
|
}
|
|
300
348
|
|
|
301
349
|
storedNote.markAsActive();
|
|
302
350
|
|
|
303
351
|
await Promise.all([
|
|
304
|
-
this.#notes.set(
|
|
305
|
-
this.#nullifiersByNullificationBlockNumber.deleteValue(
|
|
352
|
+
this.#notes.set(nullifier, storedNote.toBuffer()),
|
|
353
|
+
this.#nullifiersByNullificationBlockNumber.deleteValue(block, nullifier),
|
|
306
354
|
]);
|
|
307
355
|
}
|
|
308
356
|
}
|
|
309
357
|
}
|
|
310
358
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
359
|
+
/**
|
|
360
|
+
* Commits in memory job data to persistent storage.
|
|
361
|
+
*
|
|
362
|
+
* Called by JobCoordinator when a job completes successfully.
|
|
363
|
+
*
|
|
364
|
+
* Note: JobCoordinator wraps all commits in a single transaction, so we don't need our own transactionAsync here
|
|
365
|
+
* (and using one would throw on IndexedDB as it does not support nested txs).
|
|
366
|
+
*
|
|
367
|
+
* @param jobId - The jobId identifying which staged data to commit
|
|
368
|
+
*/
|
|
369
|
+
async commit(jobId: string): Promise<void> {
|
|
370
|
+
for (const [nullifier, storedNote] of this.#getNotesForJob(jobId)) {
|
|
371
|
+
await this.#notes.set(nullifier, storedNote.toBuffer());
|
|
372
|
+
await this.#nullifiersByContractAddress.set(storedNote.noteDao.contractAddress.toString(), nullifier);
|
|
373
|
+
if (storedNote.nullifiedAt !== undefined) {
|
|
374
|
+
await this.#nullifiersByNullificationBlockNumber.set(storedNote.nullifiedAt, nullifier);
|
|
319
375
|
}
|
|
376
|
+
}
|
|
320
377
|
|
|
321
|
-
|
|
322
|
-
});
|
|
378
|
+
this.#clearJobData(jobId);
|
|
323
379
|
}
|
|
324
380
|
|
|
325
381
|
discardStaged(jobId: string): Promise<void> {
|
|
326
|
-
|
|
382
|
+
this.#clearJobData(jobId);
|
|
383
|
+
return Promise.resolve();
|
|
327
384
|
}
|
|
328
385
|
|
|
329
386
|
#clearJobData(jobId: string) {
|
|
@@ -358,19 +415,4 @@ export class NoteStore implements StagedStore {
|
|
|
358
415
|
}
|
|
359
416
|
return notesForJob;
|
|
360
417
|
}
|
|
361
|
-
|
|
362
|
-
async #nullifiersOfContract(contractAddress: AztecAddress, jobId: string): Promise<Set<string>> {
|
|
363
|
-
// Collect persisted nullifiers for this contract
|
|
364
|
-
const persistedNullifiers: string[] = await toArray(
|
|
365
|
-
this.#nullifiersByContractAddress.getValuesAsync(contractAddress.toString()),
|
|
366
|
-
);
|
|
367
|
-
|
|
368
|
-
// Collect staged nullifiers from the job where the note's contract matches
|
|
369
|
-
const stagedNullifiers = this.#getNotesForJob(jobId)
|
|
370
|
-
.values()
|
|
371
|
-
.filter(storedNote => storedNote.noteDao.contractAddress.equals(contractAddress))
|
|
372
|
-
.map(storedNote => storedNote.noteDao.siloedNullifier.toString());
|
|
373
|
-
|
|
374
|
-
return new Set([...persistedNullifiers, ...stagedNullifiers]);
|
|
375
|
-
}
|
|
376
418
|
}
|