@aztec/pxe 0.62.0 → 0.63.1

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 (100) hide show
  1. package/dest/config/index.d.ts +2 -3
  2. package/dest/config/index.d.ts.map +1 -1
  3. package/dest/config/index.js +4 -5
  4. package/dest/contract_data_oracle/index.d.ts +1 -0
  5. package/dest/contract_data_oracle/index.d.ts.map +1 -1
  6. package/dest/contract_data_oracle/index.js +5 -1
  7. package/dest/database/incoming_note_dao.d.ts +1 -1
  8. package/dest/database/incoming_note_dao.d.ts.map +1 -1
  9. package/dest/database/kv_pxe_database.d.ts +5 -12
  10. package/dest/database/kv_pxe_database.d.ts.map +1 -1
  11. package/dest/database/kv_pxe_database.js +31 -62
  12. package/dest/database/outgoing_note_dao.d.ts +1 -1
  13. package/dest/database/outgoing_note_dao.d.ts.map +1 -1
  14. package/dest/database/pxe_database.d.ts +17 -25
  15. package/dest/database/pxe_database.d.ts.map +1 -1
  16. package/dest/kernel_oracle/index.d.ts +3 -3
  17. package/dest/kernel_oracle/index.d.ts.map +1 -1
  18. package/dest/kernel_prover/hints/build_private_kernel_reset_private_inputs.d.ts.map +1 -1
  19. package/dest/kernel_prover/hints/build_private_kernel_reset_private_inputs.js +12 -4
  20. package/dest/kernel_prover/kernel_prover.d.ts +3 -1
  21. package/dest/kernel_prover/kernel_prover.d.ts.map +1 -1
  22. package/dest/kernel_prover/kernel_prover.js +39 -7
  23. package/dest/kernel_prover/test/test_circuit_prover.d.ts +1 -0
  24. package/dest/kernel_prover/test/test_circuit_prover.d.ts.map +1 -1
  25. package/dest/kernel_prover/test/test_circuit_prover.js +5 -1
  26. package/dest/{note_processor/utils → note_decryption_utils}/add_public_values_to_payload.d.ts +1 -1
  27. package/dest/note_decryption_utils/add_public_values_to_payload.d.ts.map +1 -0
  28. package/dest/{note_processor/utils → note_decryption_utils}/add_public_values_to_payload.js +1 -1
  29. package/dest/note_decryption_utils/brute_force_note_info.d.ts.map +1 -0
  30. package/dest/{note_processor/utils → note_decryption_utils}/brute_force_note_info.js +1 -1
  31. package/dest/note_decryption_utils/index.d.ts.map +1 -0
  32. package/dest/{note_processor/utils → note_decryption_utils}/index.js +1 -1
  33. package/dest/{note_processor/utils → note_decryption_utils}/produce_note_daos.d.ts +5 -8
  34. package/dest/note_decryption_utils/produce_note_daos.d.ts.map +1 -0
  35. package/dest/note_decryption_utils/produce_note_daos.js +47 -0
  36. package/dest/note_decryption_utils/produce_note_daos_for_key.d.ts +8 -0
  37. package/dest/note_decryption_utils/produce_note_daos_for_key.d.ts.map +1 -0
  38. package/dest/note_decryption_utils/produce_note_daos_for_key.js +17 -0
  39. package/dest/pxe_http/pxe_http_server.d.ts +1 -2
  40. package/dest/pxe_http/pxe_http_server.d.ts.map +1 -1
  41. package/dest/pxe_http/pxe_http_server.js +5 -51
  42. package/dest/pxe_service/create_pxe_service.d.ts.map +1 -1
  43. package/dest/pxe_service/create_pxe_service.js +7 -4
  44. package/dest/pxe_service/error_enriching.d.ts.map +1 -1
  45. package/dest/pxe_service/error_enriching.js +7 -6
  46. package/dest/pxe_service/pxe_service.d.ts +11 -25
  47. package/dest/pxe_service/pxe_service.d.ts.map +1 -1
  48. package/dest/pxe_service/pxe_service.js +45 -48
  49. package/dest/pxe_service/test/pxe_test_suite.d.ts.map +1 -1
  50. package/dest/pxe_service/test/pxe_test_suite.js +4 -3
  51. package/dest/simulator_oracle/index.d.ts +15 -16
  52. package/dest/simulator_oracle/index.d.ts.map +1 -1
  53. package/dest/simulator_oracle/index.js +195 -65
  54. package/dest/synchronizer/synchronizer.d.ts +0 -48
  55. package/dest/synchronizer/synchronizer.d.ts.map +1 -1
  56. package/dest/synchronizer/synchronizer.js +3 -201
  57. package/package.json +14 -14
  58. package/src/config/index.ts +4 -7
  59. package/src/contract_data_oracle/index.ts +5 -0
  60. package/src/database/incoming_note_dao.ts +1 -1
  61. package/src/database/kv_pxe_database.ts +33 -70
  62. package/src/database/outgoing_note_dao.ts +1 -1
  63. package/src/database/pxe_database.ts +19 -28
  64. package/src/kernel_prover/hints/build_private_kernel_reset_private_inputs.ts +13 -3
  65. package/src/kernel_prover/kernel_prover.ts +49 -5
  66. package/src/kernel_prover/test/test_circuit_prover.ts +8 -4
  67. package/src/{note_processor/utils → note_decryption_utils}/add_public_values_to_payload.ts +1 -1
  68. package/src/{note_processor/utils → note_decryption_utils}/produce_note_daos.ts +6 -16
  69. package/src/{note_processor/utils → note_decryption_utils}/produce_note_daos_for_key.ts +6 -15
  70. package/src/pxe_http/pxe_http_server.ts +5 -84
  71. package/src/pxe_service/create_pxe_service.ts +9 -3
  72. package/src/pxe_service/error_enriching.ts +12 -5
  73. package/src/pxe_service/pxe_service.ts +61 -78
  74. package/src/pxe_service/test/pxe_test_suite.ts +6 -2
  75. package/src/simulator_oracle/index.ts +280 -60
  76. package/src/synchronizer/synchronizer.ts +3 -253
  77. package/dest/database/deferred_note_dao.d.ts +0 -40
  78. package/dest/database/deferred_note_dao.d.ts.map +0 -1
  79. package/dest/database/deferred_note_dao.js +0 -38
  80. package/dest/note_processor/index.d.ts +0 -2
  81. package/dest/note_processor/index.d.ts.map +0 -1
  82. package/dest/note_processor/index.js +0 -2
  83. package/dest/note_processor/note_processor.d.ts +0 -83
  84. package/dest/note_processor/note_processor.d.ts.map +0 -1
  85. package/dest/note_processor/note_processor.js +0 -231
  86. package/dest/note_processor/utils/add_public_values_to_payload.d.ts.map +0 -1
  87. package/dest/note_processor/utils/brute_force_note_info.d.ts.map +0 -1
  88. package/dest/note_processor/utils/index.d.ts.map +0 -1
  89. package/dest/note_processor/utils/produce_note_daos.d.ts.map +0 -1
  90. package/dest/note_processor/utils/produce_note_daos.js +0 -51
  91. package/dest/note_processor/utils/produce_note_daos_for_key.d.ts +0 -9
  92. package/dest/note_processor/utils/produce_note_daos_for_key.d.ts.map +0 -1
  93. package/dest/note_processor/utils/produce_note_daos_for_key.js +0 -26
  94. package/src/database/deferred_note_dao.ts +0 -47
  95. package/src/note_processor/index.ts +0 -1
  96. package/src/note_processor/note_processor.ts +0 -358
  97. /package/dest/{note_processor/utils → note_decryption_utils}/brute_force_note_info.d.ts +0 -0
  98. /package/dest/{note_processor/utils → note_decryption_utils}/index.d.ts +0 -0
  99. /package/src/{note_processor/utils → note_decryption_utils}/brute_force_note_info.ts +0 -0
  100. /package/src/{note_processor/utils → note_decryption_utils}/index.ts +0 -0
@@ -1,358 +0,0 @@
1
- import { type AztecNode, L1NotePayload, type L2Block } from '@aztec/circuit-types';
2
- import { type NoteProcessorStats } from '@aztec/circuit-types/stats';
3
- import {
4
- type CompleteAddress,
5
- INITIAL_L2_BLOCK_NUM,
6
- MAX_NOTE_HASHES_PER_TX,
7
- computeAddressSecret,
8
- computePoint,
9
- } from '@aztec/circuits.js';
10
- import { type Fr } from '@aztec/foundation/fields';
11
- import { type Logger, createDebugLogger } from '@aztec/foundation/log';
12
- import { Timer } from '@aztec/foundation/timer';
13
- import { type KeyStore } from '@aztec/key-store';
14
- import { type AcirSimulator } from '@aztec/simulator';
15
-
16
- import { type DeferredNoteDao } from '../database/deferred_note_dao.js';
17
- import { type IncomingNoteDao } from '../database/incoming_note_dao.js';
18
- import { type PxeDatabase } from '../database/index.js';
19
- import { type OutgoingNoteDao } from '../database/outgoing_note_dao.js';
20
- import { getAcirSimulator } from '../simulator/index.js';
21
- import { produceNoteDaos } from './utils/produce_note_daos.js';
22
-
23
- /**
24
- * Contains all the decrypted data in this array so that we can later batch insert it all into the database.
25
- */
26
- interface ProcessedData {
27
- /** Holds L2 block. */
28
- block: L2Block;
29
- /** DAOs of processed incoming notes. */
30
- incomingNotes: IncomingNoteDao[];
31
- /** DAOs of processed outgoing notes. */
32
- outgoingNotes: OutgoingNoteDao[];
33
- }
34
-
35
- /**
36
- * NoteProcessor is responsible for decrypting logs and converting them to notes via their originating contracts
37
- * before storing them against their owner.
38
- */
39
- export class NoteProcessor {
40
- /** Keeps track of processing time since an instance is created. */
41
- public readonly timer: Timer = new Timer();
42
-
43
- /** Stats accumulated for this processor. */
44
- public readonly stats: NoteProcessorStats = {
45
- seen: 0,
46
- decryptedIncoming: 0,
47
- decryptedOutgoing: 0,
48
- deferredIncoming: 0,
49
- deferredOutgoing: 0,
50
- failed: 0,
51
- blocks: 0,
52
- txs: 0,
53
- };
54
-
55
- private constructor(
56
- public readonly account: CompleteAddress,
57
- private keyStore: KeyStore,
58
- private db: PxeDatabase,
59
- private node: AztecNode,
60
- private startingBlock: number,
61
- private simulator: AcirSimulator,
62
- private log: Logger,
63
- ) {}
64
-
65
- public static create(
66
- account: CompleteAddress,
67
- keyStore: KeyStore,
68
- db: PxeDatabase,
69
- node: AztecNode,
70
- startingBlock: number = INITIAL_L2_BLOCK_NUM,
71
- simulator = getAcirSimulator(db, node, keyStore),
72
- log = createDebugLogger('aztec:note_processor'),
73
- ) {
74
- return new NoteProcessor(account, keyStore, db, node, startingBlock, simulator, log);
75
- }
76
-
77
- /**
78
- * Check if the NoteProcessor is synchronized with the remote block number.
79
- * The function queries the remote block number from the AztecNode and compares it with the syncedToBlock value in the NoteProcessor.
80
- * If the values are equal, then the NoteProcessor is considered to be synchronized, otherwise not.
81
- *
82
- * @returns A boolean indicating whether the NoteProcessor is synchronized with the remote block number or not.
83
- */
84
- public async isSynchronized() {
85
- const remoteBlockNumber = await this.node.getBlockNumber();
86
- return this.getSyncedToBlock() === remoteBlockNumber;
87
- }
88
-
89
- /**
90
- * Returns synchronization status (ie up to which block has been synced ) for this note processor.
91
- */
92
- public get status() {
93
- return { syncedToBlock: this.getSyncedToBlock() };
94
- }
95
-
96
- private getSyncedToBlock(): number {
97
- return this.db.getSynchedBlockNumberForAccount(this.account.address) ?? this.startingBlock - 1;
98
- }
99
-
100
- /**
101
- * Extracts new user-relevant notes from the information contained in the provided L2 blocks and encrypted logs.
102
- *
103
- * @param blocks - L2 blocks to be processed.
104
- * @returns A promise that resolves once the processing is completed.
105
- */
106
- public async process(blocks: L2Block[]): Promise<void> {
107
- if (blocks.length === 0) {
108
- return;
109
- }
110
-
111
- const blocksAndNotes: ProcessedData[] = [];
112
- // Keep track of notes that we couldn't process because the contract was not found.
113
- const deferredIncomingNotes: DeferredNoteDao[] = [];
114
- const deferredOutgoingNotes: DeferredNoteDao[] = [];
115
-
116
- const ivskM = await this.keyStore.getMasterSecretKey(this.account.publicKeys.masterIncomingViewingPublicKey);
117
- const addressSecret = computeAddressSecret(this.account.getPreaddress(), ivskM);
118
-
119
- const ovskM = await this.keyStore.getMasterSecretKey(this.account.publicKeys.masterOutgoingViewingPublicKey);
120
-
121
- // Iterate over both blocks and encrypted logs.
122
- for (const block of blocks) {
123
- this.stats.blocks++;
124
- const { txLogs: encryptedTxLogs } = block.body.noteEncryptedLogs;
125
- const { txLogs: unencryptedTxLogs } = block.body.unencryptedLogs;
126
-
127
- const dataStartIndexForBlock =
128
- block.header.state.partial.noteHashTree.nextAvailableLeafIndex -
129
- block.body.numberOfTxsIncludingPadded * MAX_NOTE_HASHES_PER_TX;
130
-
131
- // We are using set for `userPertainingTxIndices` to avoid duplicates. This would happen in case there were
132
- // multiple encrypted logs in a tx pertaining to a user.
133
- const incomingNotes: IncomingNoteDao[] = [];
134
- const outgoingNotes: OutgoingNoteDao[] = [];
135
-
136
- // Iterate over all the encrypted logs and try decrypting them. If successful, store the note.
137
- for (let indexOfTxInABlock = 0; indexOfTxInABlock < encryptedTxLogs.length; ++indexOfTxInABlock) {
138
- this.stats.txs++;
139
- const dataStartIndexForTx = dataStartIndexForBlock + indexOfTxInABlock * MAX_NOTE_HASHES_PER_TX;
140
- const noteHashes = block.body.txEffects[indexOfTxInABlock].noteHashes;
141
- // Note: Each tx generates a `TxL2Logs` object and for this reason we can rely on its index corresponding
142
- // to the index of a tx in a block.
143
- const encryptedTxFunctionLogs = encryptedTxLogs[indexOfTxInABlock].functionLogs;
144
- const unencryptedTxFunctionLogs = unencryptedTxLogs[indexOfTxInABlock].functionLogs;
145
- const excludedIndices: Set<number> = new Set();
146
-
147
- // We iterate over both encrypted and unencrypted logs to decrypt the notes since partial notes are passed
148
- // via the unencrypted logs stream.
149
- for (const txFunctionLogs of [encryptedTxFunctionLogs, unencryptedTxFunctionLogs]) {
150
- const isFromPublic = txFunctionLogs === unencryptedTxFunctionLogs;
151
- for (const functionLogs of txFunctionLogs) {
152
- for (const unprocessedLog of functionLogs.logs) {
153
- this.stats.seen++;
154
- const incomingNotePayload = L1NotePayload.decryptAsIncoming(
155
- unprocessedLog.data,
156
- addressSecret,
157
- isFromPublic,
158
- );
159
- const outgoingNotePayload = L1NotePayload.decryptAsOutgoing(unprocessedLog.data, ovskM, isFromPublic);
160
-
161
- if (incomingNotePayload || outgoingNotePayload) {
162
- if (incomingNotePayload && outgoingNotePayload && !incomingNotePayload.equals(outgoingNotePayload)) {
163
- throw new Error(
164
- `Incoming and outgoing note payloads do not match. Incoming: ${JSON.stringify(
165
- incomingNotePayload,
166
- )}, Outgoing: ${JSON.stringify(outgoingNotePayload)}`,
167
- );
168
- }
169
-
170
- const payload = incomingNotePayload || outgoingNotePayload;
171
-
172
- const txEffect = block.body.txEffects[indexOfTxInABlock];
173
- const { incomingNote, outgoingNote, incomingDeferredNote, outgoingDeferredNote } =
174
- await produceNoteDaos(
175
- this.simulator,
176
- this.db,
177
- incomingNotePayload ? computePoint(this.account.address) : undefined,
178
- outgoingNotePayload ? this.account.publicKeys.masterOutgoingViewingPublicKey : undefined,
179
- payload!,
180
- txEffect.txHash,
181
- noteHashes,
182
- dataStartIndexForTx,
183
- excludedIndices,
184
- this.log,
185
- txEffect.unencryptedLogs,
186
- );
187
-
188
- if (incomingNote) {
189
- incomingNotes.push(incomingNote);
190
- this.stats.decryptedIncoming++;
191
- }
192
- if (outgoingNote) {
193
- outgoingNotes.push(outgoingNote);
194
- this.stats.decryptedOutgoing++;
195
- }
196
- if (incomingDeferredNote) {
197
- deferredIncomingNotes.push(incomingDeferredNote);
198
- this.stats.deferredIncoming++;
199
- }
200
- if (outgoingDeferredNote) {
201
- deferredOutgoingNotes.push(outgoingDeferredNote);
202
- this.stats.deferredOutgoing++;
203
- }
204
-
205
- if (incomingNote == undefined && outgoingNote == undefined && incomingDeferredNote == undefined) {
206
- this.stats.failed++;
207
- }
208
- }
209
- }
210
- }
211
- }
212
- }
213
-
214
- blocksAndNotes.push({
215
- block,
216
- incomingNotes,
217
- outgoingNotes,
218
- });
219
- }
220
-
221
- await this.processBlocksAndNotes(blocksAndNotes);
222
- await this.processDeferredNotes(deferredIncomingNotes, deferredOutgoingNotes);
223
-
224
- const syncedToBlock = blocks[blocks.length - 1].number;
225
- await this.db.setSynchedBlockNumberForAccount(this.account.address, syncedToBlock);
226
-
227
- this.log.debug(`Synched block ${syncedToBlock}`);
228
- }
229
-
230
- /**
231
- * Process the given blocks and their associated transaction auxiliary data.
232
- * This function updates the database with information about new transactions,
233
- * user-pertaining transaction indices, and auxiliary data. It also removes nullified
234
- * transaction auxiliary data from the database. This function keeps track of new nullifiers
235
- * and ensures all other transactions are updated with newly settled block information.
236
- *
237
- * @param blocksAndNotes - Array of objects containing L2 blocks, user-pertaining transaction indices, and NoteDaos.
238
- */
239
- private async processBlocksAndNotes(blocksAndNotes: ProcessedData[]) {
240
- const incomingNotes = blocksAndNotes.flatMap(b => b.incomingNotes);
241
- const outgoingNotes = blocksAndNotes.flatMap(b => b.outgoingNotes);
242
- if (incomingNotes.length || outgoingNotes.length) {
243
- await this.db.addNotes(incomingNotes, outgoingNotes, this.account.address);
244
- incomingNotes.forEach(noteDao => {
245
- this.log.verbose(
246
- `Added incoming note for contract ${noteDao.contractAddress} at slot ${
247
- noteDao.storageSlot
248
- } with nullifier ${noteDao.siloedNullifier.toString()}`,
249
- );
250
- });
251
- outgoingNotes.forEach(noteDao => {
252
- this.log.verbose(`Added outgoing note for contract ${noteDao.contractAddress} at slot ${noteDao.storageSlot}`);
253
- });
254
- }
255
-
256
- const nullifiers: Fr[] = blocksAndNotes.flatMap(b =>
257
- b.block.body.txEffects.flatMap(txEffect => txEffect.nullifiers),
258
- );
259
- const removedNotes = await this.db.removeNullifiedNotes(nullifiers, computePoint(this.account.address));
260
- removedNotes.forEach(noteDao => {
261
- this.log.verbose(
262
- `Removed note for contract ${noteDao.contractAddress} at slot ${
263
- noteDao.storageSlot
264
- } with nullifier ${noteDao.siloedNullifier.toString()}`,
265
- );
266
- });
267
- }
268
-
269
- /**
270
- * Store the given deferred notes in the database for later decoding.
271
- *
272
- * @param deferredIncomingNotes - incoming notes that are intended for us but we couldn't process because the contract was not found.
273
- * @param deferredOutgoingNotes - outgoing notes that we couldn't process because the contract was not found.
274
- */
275
- private async processDeferredNotes(
276
- deferredIncomingNotes: DeferredNoteDao[],
277
- deferredOutgoingNotes: DeferredNoteDao[],
278
- ) {
279
- if (deferredIncomingNotes.length || deferredOutgoingNotes.length) {
280
- await this.db.addDeferredNotes([...deferredIncomingNotes, ...deferredOutgoingNotes]);
281
- deferredIncomingNotes.forEach(noteDao => {
282
- this.log.verbose(
283
- `Deferred incoming note for contract ${noteDao.payload.contractAddress} at slot ${
284
- noteDao.payload.storageSlot
285
- } in tx ${noteDao.txHash.toString()}`,
286
- );
287
- });
288
- deferredOutgoingNotes.forEach(noteDao => {
289
- this.log.verbose(
290
- `Deferred outgoing note for contract ${noteDao.payload.contractAddress} at slot ${
291
- noteDao.payload.storageSlot
292
- } in tx ${noteDao.txHash.toString()}`,
293
- );
294
- });
295
- }
296
- }
297
-
298
- /**
299
- * Retry decoding the given deferred notes because we now have the contract code.
300
- *
301
- * @param deferredNoteDaos - notes that we have previously deferred because the contract was not found
302
- * @returns An object containing arrays of incoming and outgoing notes that were successfully decoded.
303
- *
304
- * @remarks Caller is responsible for making sure that we have the contract for the
305
- * deferred notes provided: we will not retry notes that fail again.
306
- */
307
- public async decodeDeferredNotes(deferredNoteDaos: DeferredNoteDao[]): Promise<{
308
- incomingNotes: IncomingNoteDao[];
309
- outgoingNotes: OutgoingNoteDao[];
310
- }> {
311
- const excludedIndices: Set<number> = new Set();
312
- const incomingNotes: IncomingNoteDao[] = [];
313
- const outgoingNotes: OutgoingNoteDao[] = [];
314
-
315
- for (const deferredNote of deferredNoteDaos) {
316
- const { publicKey, payload, txHash, noteHashes, dataStartIndexForTx, unencryptedLogs } = deferredNote;
317
-
318
- const isIncoming = publicKey.equals(computePoint(this.account.address));
319
- const isOutgoing = publicKey.equals(this.account.publicKeys.masterOutgoingViewingPublicKey);
320
-
321
- if (!isIncoming && !isOutgoing) {
322
- // The note does not belong to this note processor
323
- continue;
324
- }
325
-
326
- const { incomingNote, outgoingNote } = await produceNoteDaos(
327
- this.simulator,
328
- this.db,
329
- isIncoming ? computePoint(this.account.address) : undefined,
330
- isOutgoing ? this.account.publicKeys.masterOutgoingViewingPublicKey : undefined,
331
- payload,
332
- txHash,
333
- noteHashes,
334
- dataStartIndexForTx,
335
- excludedIndices,
336
- this.log,
337
- unencryptedLogs,
338
- );
339
-
340
- if (isIncoming) {
341
- if (!incomingNote) {
342
- throw new Error('Deferred incoming note could not be decoded');
343
- }
344
- incomingNotes.push(incomingNote);
345
- this.stats.decryptedIncoming++;
346
- }
347
- if (outgoingNote) {
348
- if (!outgoingNote) {
349
- throw new Error('Deferred outgoing note could not be decoded');
350
- }
351
- outgoingNotes.push(outgoingNote);
352
- this.stats.decryptedOutgoing++;
353
- }
354
- }
355
-
356
- return { incomingNotes, outgoingNotes };
357
- }
358
- }