@aztec/pxe 0.0.1-commit.2ed92850 → 0.0.1-commit.43597cc1

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 (144) hide show
  1. package/dest/block_synchronizer/block_synchronizer.d.ts +5 -3
  2. package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
  3. package/dest/block_synchronizer/block_synchronizer.js +11 -5
  4. package/dest/contract_function_simulator/contract_function_simulator.d.ts +5 -5
  5. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  6. package/dest/contract_function_simulator/contract_function_simulator.js +11 -11
  7. package/dest/contract_function_simulator/oracle/interfaces.d.ts +10 -10
  8. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  9. package/dest/contract_function_simulator/oracle/oracle.d.ts +4 -4
  10. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  11. package/dest/contract_function_simulator/oracle/oracle.js +38 -26
  12. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +4 -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 +7 -7
  15. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +29 -23
  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 +61 -40
  18. package/dest/contract_sync/contract_sync_service.d.ts +41 -0
  19. package/dest/contract_sync/contract_sync_service.d.ts.map +1 -0
  20. package/dest/contract_sync/contract_sync_service.js +82 -0
  21. package/dest/contract_sync/helpers.d.ts +28 -0
  22. package/dest/contract_sync/helpers.d.ts.map +1 -0
  23. package/dest/contract_sync/{index.js → helpers.js} +13 -12
  24. package/dest/debug/pxe_debug_utils.d.ts +22 -9
  25. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  26. package/dest/debug/pxe_debug_utils.js +28 -18
  27. package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
  28. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
  29. package/dest/entrypoints/client/bundle/utils.js +12 -6
  30. package/dest/entrypoints/client/lazy/utils.d.ts +2 -2
  31. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  32. package/dest/entrypoints/client/lazy/utils.js +13 -7
  33. package/dest/entrypoints/pxe_creation_options.d.ts +3 -2
  34. package/dest/entrypoints/pxe_creation_options.d.ts.map +1 -1
  35. package/dest/entrypoints/server/index.d.ts +2 -2
  36. package/dest/entrypoints/server/index.d.ts.map +1 -1
  37. package/dest/entrypoints/server/index.js +1 -1
  38. package/dest/entrypoints/server/utils.d.ts +1 -1
  39. package/dest/entrypoints/server/utils.d.ts.map +1 -1
  40. package/dest/entrypoints/server/utils.js +19 -8
  41. package/dest/events/event_service.d.ts +4 -5
  42. package/dest/events/event_service.d.ts.map +1 -1
  43. package/dest/events/event_service.js +5 -6
  44. package/dest/job_coordinator/job_coordinator.d.ts +3 -2
  45. package/dest/job_coordinator/job_coordinator.d.ts.map +1 -1
  46. package/dest/job_coordinator/job_coordinator.js +3 -2
  47. package/dest/logs/log_service.d.ts +6 -5
  48. package/dest/logs/log_service.d.ts.map +1 -1
  49. package/dest/logs/log_service.js +10 -15
  50. package/dest/notes/note_service.d.ts +4 -5
  51. package/dest/notes/note_service.d.ts.map +1 -1
  52. package/dest/notes/note_service.js +6 -7
  53. package/dest/oracle_version.d.ts +3 -3
  54. package/dest/oracle_version.d.ts.map +1 -1
  55. package/dest/oracle_version.js +2 -2
  56. package/dest/private_kernel/private_kernel_execution_prover.d.ts +3 -2
  57. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  58. package/dest/private_kernel/private_kernel_execution_prover.js +2 -2
  59. package/dest/private_kernel/private_kernel_oracle.d.ts +3 -3
  60. package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
  61. package/dest/pxe.d.ts +13 -2
  62. package/dest/pxe.d.ts.map +1 -1
  63. package/dest/pxe.js +39 -16
  64. package/dest/storage/address_store/address_store.d.ts +1 -1
  65. package/dest/storage/address_store/address_store.d.ts.map +1 -1
  66. package/dest/storage/address_store/address_store.js +12 -11
  67. package/dest/storage/anchor_block_store/anchor_block_store.d.ts +9 -1
  68. package/dest/storage/anchor_block_store/anchor_block_store.d.ts.map +1 -1
  69. package/dest/storage/anchor_block_store/anchor_block_store.js +8 -1
  70. package/dest/storage/capsule_store/capsule_store.js +6 -8
  71. package/dest/storage/contract_store/contract_store.d.ts +1 -1
  72. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  73. package/dest/storage/contract_store/contract_store.js +27 -18
  74. package/dest/storage/metadata.d.ts +1 -1
  75. package/dest/storage/metadata.js +1 -1
  76. package/dest/storage/note_store/note_store.d.ts +11 -1
  77. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  78. package/dest/storage/note_store/note_store.js +146 -105
  79. package/dest/storage/private_event_store/private_event_store.d.ts +1 -1
  80. package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
  81. package/dest/storage/private_event_store/private_event_store.js +84 -61
  82. package/dest/storage/private_event_store/stored_private_event.d.ts +4 -4
  83. package/dest/storage/private_event_store/stored_private_event.d.ts.map +1 -1
  84. package/dest/storage/private_event_store/stored_private_event.js +2 -2
  85. package/dest/storage/tagging_store/recipient_tagging_store.d.ts +1 -1
  86. package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
  87. package/dest/storage/tagging_store/recipient_tagging_store.js +31 -19
  88. package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
  89. package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
  90. package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
  91. package/dest/storage/tagging_store/sender_tagging_store.d.ts +1 -1
  92. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  93. package/dest/storage/tagging_store/sender_tagging_store.js +183 -113
  94. package/dest/tagging/get_all_logs_by_tags.d.ts +4 -4
  95. package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -1
  96. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +3 -3
  97. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
  98. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +3 -3
  99. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
  100. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +3 -3
  101. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
  102. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +3 -3
  103. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
  104. package/package.json +25 -16
  105. package/src/block_synchronizer/block_synchronizer.ts +23 -19
  106. package/src/contract_function_simulator/contract_function_simulator.ts +11 -8
  107. package/src/contract_function_simulator/oracle/interfaces.ts +12 -12
  108. package/src/contract_function_simulator/oracle/oracle.ts +41 -24
  109. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +7 -10
  110. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +73 -48
  111. package/src/contract_sync/contract_sync_service.ts +129 -0
  112. package/src/contract_sync/{index.ts → helpers.ts} +15 -20
  113. package/src/debug/pxe_debug_utils.ts +60 -18
  114. package/src/entrypoints/client/bundle/utils.ts +7 -14
  115. package/src/entrypoints/client/lazy/utils.ts +8 -14
  116. package/src/entrypoints/pxe_creation_options.ts +2 -1
  117. package/src/entrypoints/server/index.ts +1 -1
  118. package/src/entrypoints/server/utils.ts +15 -19
  119. package/src/events/event_service.ts +4 -6
  120. package/src/job_coordinator/job_coordinator.ts +4 -3
  121. package/src/logs/log_service.ts +14 -14
  122. package/src/notes/note_service.ts +5 -7
  123. package/src/oracle_version.ts +2 -2
  124. package/src/private_kernel/private_kernel_execution_prover.ts +6 -3
  125. package/src/private_kernel/private_kernel_oracle.ts +2 -2
  126. package/src/pxe.ts +61 -19
  127. package/src/storage/address_store/address_store.ts +15 -15
  128. package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
  129. package/src/storage/capsule_store/capsule_store.ts +8 -8
  130. package/src/storage/contract_store/contract_store.ts +26 -15
  131. package/src/storage/metadata.ts +1 -1
  132. package/src/storage/note_store/note_store.ts +163 -129
  133. package/src/storage/private_event_store/private_event_store.ts +102 -81
  134. package/src/storage/private_event_store/stored_private_event.ts +3 -3
  135. package/src/storage/tagging_store/recipient_tagging_store.ts +31 -21
  136. package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
  137. package/src/storage/tagging_store/sender_tagging_store.ts +210 -126
  138. package/src/tagging/get_all_logs_by_tags.ts +3 -3
  139. package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +2 -2
  140. package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +2 -2
  141. package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +2 -2
  142. package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +3 -3
  143. package/dest/contract_sync/index.d.ts +0 -23
  144. package/dest/contract_sync/index.d.ts.map +0 -1
@@ -3,12 +3,12 @@ import type { BlockNumber } from '@aztec/foundation/branded-types';
3
3
  import { Aes128 } from '@aztec/foundation/crypto/aes128';
4
4
  import { Fr } from '@aztec/foundation/curves/bn254';
5
5
  import { Point } from '@aztec/foundation/curves/grumpkin';
6
- import { LogLevels, applyStringFormatting, createLogger } from '@aztec/foundation/log';
6
+ import { LogLevels, type Logger, applyStringFormatting, createLogger } from '@aztec/foundation/log';
7
7
  import type { MembershipWitness } from '@aztec/foundation/trees';
8
8
  import type { KeyStore } from '@aztec/key-store';
9
9
  import type { AuthWitness } from '@aztec/stdlib/auth-witness';
10
10
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
11
- import { L2BlockHash } from '@aztec/stdlib/block';
11
+ import { BlockHash } from '@aztec/stdlib/block';
12
12
  import type { CompleteAddress, ContractInstance } from '@aztec/stdlib/contract';
13
13
  import { siloNullifier } from '@aztec/stdlib/hash';
14
14
  import type { AztecNode } from '@aztec/stdlib/interfaces/server';
@@ -25,7 +25,6 @@ import { LogService } from '../../logs/log_service.js';
25
25
  import { NoteService } from '../../notes/note_service.js';
26
26
  import { ORACLE_VERSION } from '../../oracle_version.js';
27
27
  import type { AddressStore } from '../../storage/address_store/address_store.js';
28
- import type { AnchorBlockStore } from '../../storage/anchor_block_store/anchor_block_store.js';
29
28
  import type { CapsuleStore } from '../../storage/capsule_store/capsule_store.js';
30
29
  import type { ContractStore } from '../../storage/contract_store/contract_store.js';
31
30
  import type { NoteStore } from '../../storage/note_store/note_store.js';
@@ -48,7 +47,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
48
47
  isMisc = true as const;
49
48
  isUtility = true as const;
50
49
 
51
- private aztecNrDebugLog = createLogger('aztec-nr:debug_log');
50
+ private contractLogger: Logger | undefined;
52
51
 
53
52
  constructor(
54
53
  protected readonly contractAddress: AztecAddress,
@@ -61,7 +60,6 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
61
60
  protected readonly keyStore: KeyStore,
62
61
  protected readonly addressStore: AddressStore,
63
62
  protected readonly aztecNode: AztecNode,
64
- protected readonly anchorBlockStore: AnchorBlockStore,
65
63
  protected readonly recipientTaggingStore: RecipientTaggingStore,
66
64
  protected readonly senderAddressBookStore: SenderAddressBookStore,
67
65
  protected readonly capsuleStore: CapsuleStore,
@@ -90,35 +88,49 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
90
88
  * @param pkMHash - The master public key hash.
91
89
  * @returns A Promise that resolves to nullifier keys.
92
90
  * @throws If the keys are not registered in the key store.
91
+ * @throws If scopes are defined and the account is not in the scopes.
93
92
  */
94
- public utilityGetKeyValidationRequest(pkMHash: Fr): Promise<KeyValidationRequest> {
93
+ public async utilityGetKeyValidationRequest(pkMHash: Fr): Promise<KeyValidationRequest> {
94
+ // If scopes are defined, check that the key belongs to an account in the scopes
95
+ if (this.scopes && this.scopes.length > 0) {
96
+ const [, account] = await this.keyStore.getKeyPrefixAndAccount(pkMHash);
97
+ if (!this.scopes.some(scope => scope.equals(account))) {
98
+ throw new Error(`Key validation request denied: account ${account.toString()} is not in the allowed scopes.`);
99
+ }
100
+ }
95
101
  return this.keyStore.getKeyValidationRequest(pkMHash, this.contractAddress);
96
102
  }
97
103
 
98
104
  /**
99
105
  * Fetches the index and sibling path of a leaf at a given block from the note hash tree.
100
- * @param blockHash - The block hash at which to get the membership witness.
101
- * @param leafValue - The leaf value
106
+ * @param anchorBlockHash - The hash of a block that contains the note hash tree root in which to find the membership
107
+ * witness.
108
+ * @param noteHash - The note hash to find in the note hash tree.
102
109
  * @returns The membership witness containing the leaf index and sibling path
103
110
  */
104
111
  public utilityGetNoteHashMembershipWitness(
105
- blockHash: L2BlockHash,
106
- leafValue: Fr,
112
+ anchorBlockHash: BlockHash,
113
+ noteHash: Fr,
107
114
  ): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
108
- return this.aztecNode.getNoteHashMembershipWitness(blockHash, leafValue);
115
+ return this.aztecNode.getNoteHashMembershipWitness(anchorBlockHash, noteHash);
109
116
  }
110
117
 
111
118
  /**
112
- * Fetches the index and sibling path of a leaf at a given block from the archive tree.
113
- * @param blockHash - The block hash at which to get the membership witness.
114
- * @param leafValue - The leaf value
119
+ * Fetches the index and sibling path of a block hash in the archive tree.
120
+ *
121
+ * Block hashes are the leaves of the archive tree. Each time a new block is added to the chain,
122
+ * its block hash is appended as a new leaf to the archive tree.
123
+ *
124
+ * @param anchorBlockHash - The hash of a block that contains the archive tree root in which to find the membership
125
+ * witness.
126
+ * @param blockHash - The block hash to find in the archive tree.
115
127
  * @returns The membership witness containing the leaf index and sibling path
116
128
  */
117
- public utilityGetArchiveMembershipWitness(
118
- blockHash: L2BlockHash,
119
- leafValue: Fr,
129
+ public utilityGetBlockHashMembershipWitness(
130
+ anchorBlockHash: BlockHash,
131
+ blockHash: BlockHash,
120
132
  ): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
121
- return this.aztecNode.getArchiveMembershipWitness(blockHash, leafValue);
133
+ return this.aztecNode.getBlockHashMembershipWitness(anchorBlockHash, blockHash);
122
134
  }
123
135
 
124
136
  /**
@@ -128,7 +140,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
128
140
  * @returns The nullifier membership witness (if found).
129
141
  */
130
142
  public utilityGetNullifierMembershipWitness(
131
- blockHash: L2BlockHash,
143
+ blockHash: BlockHash,
132
144
  nullifier: Fr,
133
145
  ): Promise<NullifierMembershipWitness | undefined> {
134
146
  return this.aztecNode.getNullifierMembershipWitness(blockHash, nullifier);
@@ -144,7 +156,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
144
156
  * we are trying to prove non-inclusion for.
145
157
  */
146
158
  public utilityGetLowNullifierMembershipWitness(
147
- blockHash: L2BlockHash,
159
+ blockHash: BlockHash,
148
160
  nullifier: Fr,
149
161
  ): Promise<NullifierMembershipWitness | undefined> {
150
162
  return this.aztecNode.getLowNullifierMembershipWitness(blockHash, nullifier);
@@ -156,7 +168,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
156
168
  * @param leafSlot - The slot of the public data tree to get the witness for.
157
169
  * @returns - The witness
158
170
  */
159
- public utilityGetPublicDataWitness(blockHash: L2BlockHash, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
171
+ public utilityGetPublicDataWitness(blockHash: BlockHash, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
160
172
  return this.aztecNode.getPublicDataWitness(blockHash, leafSlot);
161
173
  }
162
174
 
@@ -166,7 +178,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
166
178
  * @returns Block extracted from a block with block number `blockNumber`.
167
179
  */
168
180
  public async utilityGetBlockHeader(blockNumber: BlockNumber): Promise<BlockHeader | undefined> {
169
- const anchorBlockNumber = (await this.anchorBlockStore.getBlockHeader()).getBlockNumber();
181
+ const anchorBlockNumber = this.anchorBlockHeader.getBlockNumber();
170
182
  if (blockNumber > anchorBlockNumber) {
171
183
  throw new Error(`Block number ${blockNumber} is higher than current block ${anchorBlockNumber}`);
172
184
  }
@@ -178,14 +190,13 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
178
190
  /**
179
191
  * Retrieve the complete address associated to a given address.
180
192
  * @param account - The account address.
181
- * @returns A complete address associated with the input address.
182
- * @throws An error if the account is not registered in the database.
193
+ * @returns A complete address associated with the input address, or `undefined` if not registered.
183
194
  */
184
- public utilityGetPublicKeysAndPartialAddress(account: AztecAddress): Promise<CompleteAddress> {
185
- return this.getCompleteAddress(account);
195
+ public utilityTryGetPublicKeysAndPartialAddress(account: AztecAddress): Promise<CompleteAddress | undefined> {
196
+ return this.addressStore.getCompleteAddress(account);
186
197
  }
187
198
 
188
- protected async getCompleteAddress(account: AztecAddress): Promise<CompleteAddress> {
199
+ protected async getCompleteAddressOrFail(account: AztecAddress): Promise<CompleteAddress> {
189
200
  const completeAddress = await this.addressStore.getCompleteAddress(account);
190
201
  if (!completeAddress) {
191
202
  throw new Error(
@@ -262,7 +273,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
262
273
  offset: number,
263
274
  status: NoteStatus,
264
275
  ): Promise<NoteData[]> {
265
- const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockStore, this.jobId);
276
+ const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockHeader, this.jobId);
266
277
 
267
278
  const dbNotes = await noteService.getNotes(this.contractAddress, owner, storageSlot, status, this.scopes);
268
279
  return pickNotes<NoteData>(dbNotes, {
@@ -286,8 +297,13 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
286
297
  * @returns A boolean indicating whether the nullifier exists in the tree or not.
287
298
  */
288
299
  public async utilityCheckNullifierExists(innerNullifier: Fr) {
289
- const nullifier = await siloNullifier(this.contractAddress, innerNullifier!);
290
- const [leafIndex] = await this.aztecNode.findLeavesIndexes('latest', MerkleTreeId.NULLIFIER_TREE, [nullifier]);
300
+ const [nullifier, anchorBlockHash] = await Promise.all([
301
+ siloNullifier(this.contractAddress, innerNullifier!),
302
+ this.anchorBlockHeader.hash(),
303
+ ]);
304
+ const [leafIndex] = await this.aztecNode.findLeavesIndexes(anchorBlockHash, MerkleTreeId.NULLIFIER_TREE, [
305
+ nullifier,
306
+ ]);
291
307
  return leafIndex?.data !== undefined;
292
308
  }
293
309
 
@@ -318,7 +334,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
318
334
  * @param numberOfElements - Number of elements to read from the starting storage slot.
319
335
  */
320
336
  public async utilityStorageRead(
321
- blockHash: L2BlockHash,
337
+ blockHash: BlockHash,
322
338
  contractAddress: AztecAddress,
323
339
  startStorageSlot: Fr,
324
340
  numberOfElements: number,
@@ -338,36 +354,44 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
338
354
  return values;
339
355
  }
340
356
 
341
- public utilityDebugLog(level: number, message: string, fields: Fr[]): void {
357
+ /**
358
+ * Returns a per-contract logger whose output is prefixed with `contract_log::<name>(<addrAbbrev>)`.
359
+ */
360
+ async #getContractLogger(): Promise<Logger> {
361
+ if (!this.contractLogger) {
362
+ const addrAbbrev = this.contractAddress.toString().slice(0, 10);
363
+ const name = await this.contractStore.getDebugContractName(this.contractAddress);
364
+ const module = name ? `contract_log::${name}(${addrAbbrev})` : `contract_log::${addrAbbrev}`;
365
+ // Purpose of instanceId is to distinguish logs from different instances of the same component. It makes sense
366
+ // to re-use jobId as instanceId here as executions of different PXE jobs are isolated.
367
+ this.contractLogger = createLogger(module, { instanceId: this.jobId });
368
+ }
369
+ return this.contractLogger;
370
+ }
371
+
372
+ public async utilityDebugLog(level: number, message: string, fields: Fr[]): Promise<void> {
342
373
  if (!LogLevels[level]) {
343
374
  throw new Error(`Invalid debug log level: ${level}`);
344
375
  }
345
376
  const levelName = LogLevels[level];
346
- this.aztecNrDebugLog[levelName](`${applyStringFormatting(message, fields)}`);
377
+ const logger = await this.#getContractLogger();
378
+ logger[levelName](`${applyStringFormatting(message, fields)}`);
347
379
  }
348
380
 
349
381
  public async utilityFetchTaggedLogs(pendingTaggedLogArrayBaseSlot: Fr) {
350
382
  const logService = new LogService(
351
383
  this.aztecNode,
352
- this.anchorBlockStore,
384
+ this.anchorBlockHeader,
353
385
  this.keyStore,
354
386
  this.capsuleStore,
355
387
  this.recipientTaggingStore,
356
388
  this.senderAddressBookStore,
357
389
  this.addressStore,
358
390
  this.jobId,
391
+ this.log.getBindings(),
359
392
  );
360
393
 
361
- const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockStore, this.jobId);
362
-
363
- // It is acceptable to run the following operations in parallel for several reasons:
364
- // 1. syncTaggedLogs does not write to the note store — it only stores the pending tagged logs in a capsule array,
365
- // which is then processed in Noir after this handler returns.
366
- // 2. Even if syncTaggedLogs did write to the note store, it would not cause inconsistent state.
367
- await Promise.all([
368
- logService.syncTaggedLogs(this.contractAddress, pendingTaggedLogArrayBaseSlot, this.scopes),
369
- noteService.syncNoteNullifiers(this.contractAddress),
370
- ]);
394
+ await logService.fetchTaggedLogs(this.contractAddress, pendingTaggedLogArrayBaseSlot, this.scopes);
371
395
  }
372
396
 
373
397
  /**
@@ -400,7 +424,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
400
424
  await this.capsuleStore.readCapsuleArray(contractAddress, eventValidationRequestsArrayBaseSlot, this.jobId)
401
425
  ).map(EventValidationRequest.fromFields);
402
426
 
403
- const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockStore, this.jobId);
427
+ const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockHeader, this.jobId);
404
428
  const noteStorePromises = noteValidationRequests.map(request =>
405
429
  noteService.validateAndStoreNote(
406
430
  request.contractAddress,
@@ -416,7 +440,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
416
440
  ),
417
441
  );
418
442
 
419
- const eventService = new EventService(this.anchorBlockStore, this.aztecNode, this.privateEventStore, this.jobId);
443
+ const eventService = new EventService(this.anchorBlockHeader, this.aztecNode, this.privateEventStore, this.jobId);
420
444
  const eventStorePromises = eventValidationRequests.map(request =>
421
445
  eventService.validateAndStoreEvent(
422
446
  request.contractAddress,
@@ -454,13 +478,14 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
454
478
 
455
479
  const logService = new LogService(
456
480
  this.aztecNode,
457
- this.anchorBlockStore,
481
+ this.anchorBlockHeader,
458
482
  this.keyStore,
459
483
  this.capsuleStore,
460
484
  this.recipientTaggingStore,
461
485
  this.senderAddressBookStore,
462
486
  this.addressStore,
463
487
  this.jobId,
488
+ this.log.getBindings(),
464
489
  );
465
490
 
466
491
  const maybeLogRetrievalResponses = await logService.bulkRetrieveLogs(logRetrievalRequests);
@@ -538,7 +563,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
538
563
 
539
564
  protected async getSharedSecret(address: AztecAddress, ephPk: Point): Promise<Point> {
540
565
  // TODO(#12656): return an app-siloed secret
541
- const recipientCompleteAddress = await this.getCompleteAddress(address);
566
+ const recipientCompleteAddress = await this.getCompleteAddressOrFail(address);
542
567
  const ivskM = await this.keyStore.getMasterSecretKey(
543
568
  recipientCompleteAddress.publicKeys.masterIncomingViewingPublicKey,
544
569
  );
@@ -0,0 +1,129 @@
1
+ import type { Logger } from '@aztec/foundation/log';
2
+ import type { FunctionCall, FunctionSelector } from '@aztec/stdlib/abi';
3
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
+ import type { AztecNode } from '@aztec/stdlib/interfaces/client';
5
+ import type { BlockHeader } from '@aztec/stdlib/tx';
6
+
7
+ import type { StagedStore } from '../job_coordinator/job_coordinator.js';
8
+ import type { ContractStore } from '../storage/contract_store/contract_store.js';
9
+ import type { NoteStore } from '../storage/note_store/note_store.js';
10
+ import { syncState, verifyCurrentClassId } from './helpers.js';
11
+
12
+ /**
13
+ * Service for syncing the private state of contracts and verifying that the PXE holds the current class artifact.
14
+ * It uses a cache to avoid redundant sync operations - the cache is wiped when the anchor block changes.
15
+ *
16
+ * TODO: The StagedStore naming is broken here. Figure out a better name.
17
+ */
18
+ export class ContractSyncService implements StagedStore {
19
+ readonly storeName = 'contract_sync';
20
+
21
+ // Tracks contracts synced since last wipe. Key is contract address string, value is a promise that resolves when
22
+ // the contract is synced.
23
+ private syncedContracts: Map<string, Promise<void>> = new Map();
24
+
25
+ // Per-job overridden contract addresses - these contracts should not be synced.
26
+ private overriddenContracts: Map<string, Set<string>> = new Map();
27
+
28
+ constructor(
29
+ private aztecNode: AztecNode,
30
+ private contractStore: ContractStore,
31
+ private noteStore: NoteStore,
32
+ private log: Logger,
33
+ ) {}
34
+
35
+ /** Sets contracts that should be skipped during sync for a specific job. */
36
+ setOverriddenContracts(jobId: string, addresses: Set<string>): void {
37
+ this.overriddenContracts.set(jobId, addresses);
38
+ }
39
+
40
+ /**
41
+ * Ensures a contract's private state is synchronized and that the PXE holds the current class artifact.
42
+ * Uses a cache to avoid redundant sync operations - the cache is wiped when the anchor block changes.
43
+ * @param contractAddress - The address of the contract to sync.
44
+ * @param functionToInvokeAfterSync - The function selector that will be called after sync (used to validate it's
45
+ * not sync_state itself).
46
+ * @param utilityExecutor - Executor function for running the sync_state utility function.
47
+ */
48
+ async ensureContractSynced(
49
+ contractAddress: AztecAddress,
50
+ functionToInvokeAfterSync: FunctionSelector | null,
51
+ utilityExecutor: (call: FunctionCall) => Promise<any>,
52
+ anchorBlockHeader: BlockHeader,
53
+ jobId: string,
54
+ ): Promise<void> {
55
+ const key = contractAddress.toString();
56
+
57
+ // Skip sync if this contract has an override for this job
58
+ const overrides = this.overriddenContracts.get(jobId);
59
+ if (overrides?.has(key)) {
60
+ return;
61
+ }
62
+
63
+ const existing = this.syncedContracts.get(key);
64
+ if (existing) {
65
+ return existing;
66
+ }
67
+
68
+ const syncPromise = this.#doSync(
69
+ contractAddress,
70
+ functionToInvokeAfterSync,
71
+ utilityExecutor,
72
+ anchorBlockHeader,
73
+ jobId,
74
+ );
75
+ this.syncedContracts.set(key, syncPromise);
76
+
77
+ try {
78
+ await syncPromise;
79
+ } catch (err) {
80
+ // There was an error syncing the contract, so we remove it from the cache so that it can be retried.
81
+ this.syncedContracts.delete(key);
82
+ throw err;
83
+ }
84
+ }
85
+
86
+ async #doSync(
87
+ contractAddress: AztecAddress,
88
+ functionToInvokeAfterSync: FunctionSelector | null,
89
+ utilityExecutor: (call: FunctionCall) => Promise<any>,
90
+ anchorBlockHeader: BlockHeader,
91
+ jobId: string,
92
+ ): Promise<void> {
93
+ this.log.debug(`Syncing contract ${contractAddress}`);
94
+ await Promise.all([
95
+ syncState(
96
+ contractAddress,
97
+ this.contractStore,
98
+ functionToInvokeAfterSync,
99
+ utilityExecutor,
100
+ this.noteStore,
101
+ this.aztecNode,
102
+ anchorBlockHeader,
103
+ jobId,
104
+ ),
105
+ verifyCurrentClassId(contractAddress, this.aztecNode, this.contractStore, anchorBlockHeader),
106
+ ]);
107
+ this.log.debug(`Contract ${contractAddress} synced`);
108
+ }
109
+
110
+ /** Clears sync cache. Called by BlockSynchronizer when anchor block changes. */
111
+ wipe(): void {
112
+ this.log.debug(`Wiping contract sync cache (${this.syncedContracts.size} entries)`);
113
+ this.syncedContracts.clear();
114
+ }
115
+
116
+ commit(jobId: string): Promise<void> {
117
+ // Clear overridden contracts for this job
118
+ this.overriddenContracts.delete(jobId);
119
+ return Promise.resolve();
120
+ }
121
+
122
+ discardStaged(jobId: string): Promise<void> {
123
+ // We clear the synced contracts cache here because, when the job is discarded, any associated database writes from
124
+ // the sync are also undone.
125
+ this.syncedContracts.clear();
126
+ this.overriddenContracts.delete(jobId);
127
+ return Promise.resolve();
128
+ }
129
+ }
@@ -6,7 +6,9 @@ import { DelayedPublicMutableValues, DelayedPublicMutableValuesWithHash } from '
6
6
  import type { AztecNode } from '@aztec/stdlib/interfaces/client';
7
7
  import type { BlockHeader } from '@aztec/stdlib/tx';
8
8
 
9
+ import { NoteService } from '../notes/note_service.js';
9
10
  import type { ContractStore } from '../storage/contract_store/contract_store.js';
11
+ import type { NoteStore } from '../storage/note_store/note_store.js';
10
12
 
11
13
  /**
12
14
  * Read the current class id of a contract from the execution data provider or AztecNode. If not found, class id
@@ -41,6 +43,10 @@ export async function syncState(
41
43
  contractStore: ContractStore,
42
44
  functionToInvokeAfterSync: FunctionSelector | null,
43
45
  utilityExecutor: (privateSyncCall: FunctionCall) => Promise<any>,
46
+ noteStore: NoteStore,
47
+ aztecNode: AztecNode,
48
+ anchorBlockHeader: BlockHeader,
49
+ jobId: string,
44
50
  ) {
45
51
  // Protocol contracts don't have private state to sync
46
52
  if (!isProtocolContract(contractAddress)) {
@@ -51,16 +57,23 @@ export async function syncState(
51
57
  );
52
58
  }
53
59
 
54
- return utilityExecutor(syncStateFunctionCall);
60
+ const noteService = new NoteService(noteStore, aztecNode, anchorBlockHeader, jobId);
61
+
62
+ // Both sync_state and syncNoteNullifiers interact with the note store, but running them in parallel is safe
63
+ // because note store is designed to handle concurrent operations.
64
+ await Promise.all([utilityExecutor(syncStateFunctionCall), noteService.syncNoteNullifiers(contractAddress)]);
55
65
  }
56
66
  }
57
67
 
58
68
  /**
59
69
  * Verify that the current class id of a contract obtained from AztecNode is the same as the one in contract data
60
70
  * provider (i.e. PXE's own storage).
71
+ * @param contractAddress - The address of the contract to verify.
72
+ * @param aztecNode - The Aztec node to query for storage.
73
+ * @param contractStore - The contract store to fetch the local instance from.
61
74
  * @param header - The header of the block at which to verify the current class id.
62
75
  */
63
- async function verifyCurrentClassId(
76
+ export async function verifyCurrentClassId(
64
77
  contractAddress: AztecAddress,
65
78
  aztecNode: AztecNode,
66
79
  contractStore: ContractStore,
@@ -78,21 +91,3 @@ async function verifyCurrentClassId(
78
91
  );
79
92
  }
80
93
  }
81
-
82
- /**
83
- * Ensures the contract's private state is synchronized and that the PXE holds the current class artifact for
84
- * the contract.
85
- */
86
- export async function ensureContractSynced(
87
- contractAddress: AztecAddress,
88
- functionToInvokeAfterSync: FunctionSelector | null,
89
- utilityExecutor: (call: FunctionCall) => Promise<any>,
90
- aztecNode: AztecNode,
91
- contractStore: ContractStore,
92
- header: BlockHeader,
93
- ): Promise<void> {
94
- await Promise.all([
95
- syncState(contractAddress, contractStore, functionToInvokeAfterSync, utilityExecutor),
96
- verifyCurrentClassId(contractAddress, aztecNode, contractStore, header),
97
- ]);
98
- }
@@ -1,8 +1,13 @@
1
- import { randomBytes } from '@aztec/foundation/crypto/random';
1
+ import type { FunctionCall } from '@aztec/stdlib/abi';
2
+ import type { AuthWitness } from '@aztec/stdlib/auth-witness';
3
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
2
4
  import type { NoteDao, NotesFilter } from '@aztec/stdlib/note';
5
+ import type { ContractOverrides } from '@aztec/stdlib/tx';
3
6
 
4
- import type { PXE } from '../pxe.js';
5
- import type { ContractStore } from '../storage/contract_store/contract_store.js';
7
+ import type { BlockSynchronizer } from '../block_synchronizer/block_synchronizer.js';
8
+ import type { ContractFunctionSimulator } from '../contract_function_simulator/contract_function_simulator.js';
9
+ import type { ContractSyncService } from '../contract_sync/contract_sync_service.js';
10
+ import type { AnchorBlockStore } from '../storage/index.js';
6
11
  import type { NoteStore } from '../storage/note_store/note_store.js';
7
12
 
8
13
  /**
@@ -10,18 +15,38 @@ import type { NoteStore } from '../storage/note_store/note_store.js';
10
15
  * No backwards compatibility or API stability should be expected. Use at your own risk.
11
16
  */
12
17
  export class PXEDebugUtils {
13
- #pxe: PXE | undefined = undefined;
18
+ #putJobInQueue!: <T>(job: (jobId: string) => Promise<T>) => Promise<T>;
19
+ #getSimulatorForTx!: (overrides?: { contracts?: ContractOverrides }) => ContractFunctionSimulator;
20
+ #simulateUtility!: (
21
+ contractFunctionSimulator: ContractFunctionSimulator,
22
+ call: FunctionCall,
23
+ authWitnesses: AuthWitness[] | undefined,
24
+ scopes: AztecAddress[] | undefined,
25
+ jobId: string,
26
+ ) => Promise<any>;
14
27
 
15
28
  constructor(
16
- private contractStore: ContractStore,
29
+ private contractSyncService: ContractSyncService,
17
30
  private noteStore: NoteStore,
31
+ private blockStateSynchronizer: BlockSynchronizer,
32
+ private anchorBlockStore: AnchorBlockStore,
18
33
  ) {}
19
34
 
20
- /**
21
- * Not injected through constructor since they're are co-dependant.
22
- */
23
- public setPXE(pxe: PXE) {
24
- this.#pxe = pxe;
35
+ /** Not injected through constructor since they're are co-dependant */
36
+ public setPXEHelpers(
37
+ putJobInQueue: <T>(job: (jobId: string) => Promise<T>) => Promise<T>,
38
+ getSimulatorForTx: (overrides?: { contracts?: ContractOverrides }) => ContractFunctionSimulator,
39
+ simulateUtility: (
40
+ contractFunctionSimulator: ContractFunctionSimulator,
41
+ call: FunctionCall,
42
+ authWitnesses: AuthWitness[] | undefined,
43
+ scopes: AztecAddress[] | undefined,
44
+ jobId: string,
45
+ ) => Promise<any>,
46
+ ) {
47
+ this.#putJobInQueue = putJobInQueue;
48
+ this.#getSimulatorForTx = getSimulatorForTx;
49
+ this.#simulateUtility = simulateUtility;
25
50
  }
26
51
 
27
52
  /**
@@ -35,15 +60,32 @@ export class PXEDebugUtils {
35
60
  * @param filter - The filter to apply to the notes.
36
61
  * @returns The requested notes.
37
62
  */
38
- public async getNotes(filter: NotesFilter): Promise<NoteDao[]> {
39
- if (!this.#pxe) {
40
- throw new Error('Cannot getNotes because no PXE is set');
41
- }
63
+ public getNotes(filter: NotesFilter): Promise<NoteDao[]> {
64
+ return this.#putJobInQueue(async (jobId: string) => {
65
+ await this.blockStateSynchronizer.sync();
66
+
67
+ const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
68
+
69
+ const contractFunctionSimulator = this.#getSimulatorForTx();
42
70
 
43
- // We need to manually trigger private state sync to have a guarantee that all the notes are available.
44
- const call = await this.contractStore.getFunctionCall('sync_state', [], filter.contractAddress);
45
- await this.#pxe.simulateUtility(call);
71
+ await this.contractSyncService.ensureContractSynced(
72
+ filter.contractAddress,
73
+ null,
74
+ async privateSyncCall =>
75
+ await this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
76
+ anchorBlockHeader,
77
+ jobId,
78
+ );
46
79
 
47
- return this.noteStore.getNotes(filter, randomBytes(8).toString('hex'));
80
+ return this.noteStore.getNotes(filter, jobId);
81
+ });
82
+ }
83
+
84
+ /**
85
+ * Triggers a sync of the PXE with the node.
86
+ * Blocks until the sync is complete.
87
+ */
88
+ public sync(): Promise<void> {
89
+ return this.#putJobInQueue(() => this.blockStateSynchronizer.sync());
48
90
  }
49
91
  }
@@ -1,6 +1,5 @@
1
1
  import { BBPrivateKernelProver } from '@aztec/bb-prover/client';
2
2
  import { BBBundlePrivateKernelProver } from '@aztec/bb-prover/client/bundle';
3
- import { randomBytes } from '@aztec/foundation/crypto/random';
4
3
  import { createLogger } from '@aztec/foundation/log';
5
4
  import { createStore } from '@aztec/kv-store/indexeddb';
6
5
  import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/providers/bundle';
@@ -9,6 +8,7 @@ import type { AztecNode } from '@aztec/stdlib/interfaces/client';
9
8
 
10
9
  import type { PXEConfig } from '../../../config/index.js';
11
10
  import { PXE } from '../../../pxe.js';
11
+ import { PXE_DATA_SCHEMA_VERSION } from '../../../storage/metadata.js';
12
12
  import type { PXECreationOptions } from '../../pxe_creation_options.js';
13
13
 
14
14
  /**
@@ -26,13 +26,7 @@ export async function createPXE(
26
26
  config: PXEConfig,
27
27
  options: PXECreationOptions = { loggers: {} },
28
28
  ) {
29
- const logSuffix =
30
- typeof options.useLogSuffix === 'boolean'
31
- ? options.useLogSuffix
32
- ? randomBytes(3).toString('hex')
33
- : undefined
34
- : options.useLogSuffix;
35
-
29
+ const actor = options.loggerActorLabel;
36
30
  const loggers = options.loggers ?? {};
37
31
 
38
32
  const l1Contracts = await aztecNode.getL1ContractAddresses();
@@ -41,14 +35,13 @@ export async function createPXE(
41
35
  l1Contracts,
42
36
  } as PXEConfig;
43
37
 
44
- const storeLogger = loggers.store ? loggers.store : createLogger('pxe:data:idb' + (logSuffix ? `:${logSuffix}` : ''));
38
+ const storeLogger = loggers.store ?? createLogger('pxe:data:idb', { actor });
45
39
 
46
- const store = options.store ?? (await createStore('pxe_data', configWithContracts, storeLogger));
40
+ const store =
41
+ options.store ?? (await createStore('pxe_data', configWithContracts, PXE_DATA_SCHEMA_VERSION, storeLogger));
47
42
 
48
43
  const simulator = options.simulator ?? new WASMSimulator();
49
- const proverLogger = loggers.prover
50
- ? loggers.prover
51
- : createLogger('pxe:bb:wasm:bundle' + (logSuffix ? `:${logSuffix}` : ''));
44
+ const proverLogger = loggers.prover ?? createLogger('pxe:bb:wasm:bundle', { actor });
52
45
 
53
46
  let prover;
54
47
  if (options.proverOrOptions instanceof BBPrivateKernelProver) {
@@ -58,7 +51,7 @@ export async function createPXE(
58
51
  }
59
52
  const protocolContractsProvider = new BundledProtocolContractsProvider();
60
53
 
61
- const pxeLogger = loggers.pxe ? loggers.pxe : createLogger('pxe:service' + (logSuffix ? `:${logSuffix}` : ''));
54
+ const pxeLogger = loggers.pxe ?? createLogger('pxe:service', { actor });
62
55
  const pxe = await PXE.create(aztecNode, store, prover, simulator, protocolContractsProvider, config, pxeLogger);
63
56
  return pxe;
64
57
  }