@aztec/pxe 0.0.1-commit.c80b6263 → 0.0.1-commit.cf93bcc56

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 (154) hide show
  1. package/dest/access_scopes.d.ts +9 -0
  2. package/dest/access_scopes.d.ts.map +1 -0
  3. package/dest/access_scopes.js +6 -0
  4. package/dest/block_synchronizer/block_synchronizer.d.ts +4 -2
  5. package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
  6. package/dest/block_synchronizer/block_synchronizer.js +7 -1
  7. package/dest/contract_function_simulator/contract_function_simulator.d.ts +54 -30
  8. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  9. package/dest/contract_function_simulator/contract_function_simulator.js +170 -66
  10. package/dest/contract_function_simulator/noir-structs/event_validation_request.js +1 -1
  11. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +2 -2
  12. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts.map +1 -1
  13. package/dest/contract_function_simulator/noir-structs/note_validation_request.js +1 -1
  14. package/dest/contract_function_simulator/oracle/interfaces.d.ts +9 -9
  15. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  16. package/dest/contract_function_simulator/oracle/oracle.d.ts +5 -5
  17. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  18. package/dest/contract_function_simulator/oracle/oracle.js +32 -20
  19. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +36 -36
  20. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
  21. package/dest/contract_function_simulator/oracle/private_execution_oracle.js +74 -21
  22. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +48 -24
  23. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  24. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +85 -61
  25. package/dest/contract_sync/contract_sync_service.d.ts +43 -0
  26. package/dest/contract_sync/contract_sync_service.d.ts.map +1 -0
  27. package/dest/contract_sync/contract_sync_service.js +97 -0
  28. package/dest/contract_sync/helpers.d.ts +29 -0
  29. package/dest/contract_sync/helpers.d.ts.map +1 -0
  30. package/dest/contract_sync/{index.js → helpers.js} +13 -12
  31. package/dest/debug/pxe_debug_utils.d.ts +14 -10
  32. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  33. package/dest/debug/pxe_debug_utils.js +16 -15
  34. package/dest/entrypoints/client/bundle/index.d.ts +3 -1
  35. package/dest/entrypoints/client/bundle/index.d.ts.map +1 -1
  36. package/dest/entrypoints/client/bundle/index.js +2 -0
  37. package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
  38. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
  39. package/dest/entrypoints/client/bundle/utils.js +11 -2
  40. package/dest/entrypoints/client/lazy/index.d.ts +3 -1
  41. package/dest/entrypoints/client/lazy/index.d.ts.map +1 -1
  42. package/dest/entrypoints/client/lazy/index.js +2 -0
  43. package/dest/entrypoints/client/lazy/utils.d.ts +1 -1
  44. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  45. package/dest/entrypoints/client/lazy/utils.js +11 -2
  46. package/dest/entrypoints/server/index.d.ts +4 -2
  47. package/dest/entrypoints/server/index.d.ts.map +1 -1
  48. package/dest/entrypoints/server/index.js +3 -1
  49. package/dest/entrypoints/server/utils.js +9 -1
  50. package/dest/events/event_service.d.ts +4 -5
  51. package/dest/events/event_service.d.ts.map +1 -1
  52. package/dest/events/event_service.js +5 -6
  53. package/dest/logs/log_service.d.ts +6 -5
  54. package/dest/logs/log_service.d.ts.map +1 -1
  55. package/dest/logs/log_service.js +14 -24
  56. package/dest/notes/note_service.d.ts +7 -7
  57. package/dest/notes/note_service.d.ts.map +1 -1
  58. package/dest/notes/note_service.js +9 -9
  59. package/dest/notes_filter.d.ts +25 -0
  60. package/dest/notes_filter.d.ts.map +1 -0
  61. package/dest/notes_filter.js +4 -0
  62. package/dest/oracle_version.d.ts +3 -3
  63. package/dest/oracle_version.d.ts.map +1 -1
  64. package/dest/oracle_version.js +2 -2
  65. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts +4 -0
  66. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts.map +1 -0
  67. package/dest/private_kernel/hints/{compute_tx_include_by_timestamp.js → compute_tx_expiration_timestamp.js} +12 -12
  68. package/dest/private_kernel/hints/index.d.ts +1 -1
  69. package/dest/private_kernel/hints/index.js +1 -1
  70. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.js +4 -4
  71. package/dest/private_kernel/private_kernel_execution_prover.js +6 -6
  72. package/dest/pxe.d.ts +69 -23
  73. package/dest/pxe.d.ts.map +1 -1
  74. package/dest/pxe.js +72 -44
  75. package/dest/storage/address_store/address_store.d.ts +1 -1
  76. package/dest/storage/address_store/address_store.d.ts.map +1 -1
  77. package/dest/storage/address_store/address_store.js +12 -11
  78. package/dest/storage/anchor_block_store/anchor_block_store.d.ts +9 -1
  79. package/dest/storage/anchor_block_store/anchor_block_store.d.ts.map +1 -1
  80. package/dest/storage/anchor_block_store/anchor_block_store.js +8 -1
  81. package/dest/storage/capsule_store/capsule_store.js +6 -8
  82. package/dest/storage/contract_store/contract_store.d.ts +1 -1
  83. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  84. package/dest/storage/contract_store/contract_store.js +27 -18
  85. package/dest/storage/metadata.d.ts +1 -1
  86. package/dest/storage/metadata.js +1 -1
  87. package/dest/storage/note_store/note_store.d.ts +13 -3
  88. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  89. package/dest/storage/note_store/note_store.js +147 -107
  90. package/dest/storage/private_event_store/private_event_store.d.ts +1 -1
  91. package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
  92. package/dest/storage/private_event_store/private_event_store.js +84 -61
  93. package/dest/storage/private_event_store/stored_private_event.js +1 -1
  94. package/dest/storage/tagging_store/recipient_tagging_store.d.ts +1 -1
  95. package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
  96. package/dest/storage/tagging_store/recipient_tagging_store.js +31 -19
  97. package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
  98. package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
  99. package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
  100. package/dest/storage/tagging_store/sender_tagging_store.d.ts +1 -1
  101. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  102. package/dest/storage/tagging_store/sender_tagging_store.js +183 -113
  103. package/dest/tagging/get_all_logs_by_tags.d.ts +1 -1
  104. package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -1
  105. package/dest/tagging/get_all_logs_by_tags.js +17 -3
  106. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.js +4 -4
  107. package/dest/tagging/recipient_sync/utils/find_highest_indexes.js +2 -2
  108. package/package.json +25 -16
  109. package/src/access_scopes.ts +9 -0
  110. package/src/block_synchronizer/block_synchronizer.ts +6 -0
  111. package/src/contract_function_simulator/contract_function_simulator.ts +318 -121
  112. package/src/contract_function_simulator/noir-structs/event_validation_request.ts +1 -1
  113. package/src/contract_function_simulator/noir-structs/note_validation_request.ts +1 -1
  114. package/src/contract_function_simulator/oracle/interfaces.ts +10 -10
  115. package/src/contract_function_simulator/oracle/oracle.ts +35 -18
  116. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +97 -101
  117. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +129 -63
  118. package/src/contract_sync/contract_sync_service.ts +152 -0
  119. package/src/contract_sync/{index.ts → helpers.ts} +21 -21
  120. package/src/debug/pxe_debug_utils.ts +48 -18
  121. package/src/entrypoints/client/bundle/index.ts +2 -0
  122. package/src/entrypoints/client/bundle/utils.ts +12 -2
  123. package/src/entrypoints/client/lazy/index.ts +2 -0
  124. package/src/entrypoints/client/lazy/utils.ts +12 -2
  125. package/src/entrypoints/server/index.ts +3 -1
  126. package/src/entrypoints/server/utils.ts +7 -7
  127. package/src/events/event_service.ts +4 -6
  128. package/src/logs/log_service.ts +14 -29
  129. package/src/notes/note_service.ts +9 -10
  130. package/src/notes_filter.ts +26 -0
  131. package/src/oracle_version.ts +2 -2
  132. package/src/private_kernel/hints/{compute_tx_include_by_timestamp.ts → compute_tx_expiration_timestamp.ts} +13 -13
  133. package/src/private_kernel/hints/index.ts +1 -1
  134. package/src/private_kernel/hints/private_kernel_reset_private_inputs_builder.ts +7 -7
  135. package/src/private_kernel/private_kernel_execution_prover.ts +6 -6
  136. package/src/pxe.ts +151 -88
  137. package/src/storage/address_store/address_store.ts +15 -15
  138. package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
  139. package/src/storage/capsule_store/capsule_store.ts +8 -8
  140. package/src/storage/contract_store/contract_store.ts +26 -15
  141. package/src/storage/metadata.ts +1 -1
  142. package/src/storage/note_store/note_store.ts +169 -132
  143. package/src/storage/private_event_store/private_event_store.ts +102 -81
  144. package/src/storage/private_event_store/stored_private_event.ts +1 -1
  145. package/src/storage/tagging_store/recipient_tagging_store.ts +31 -21
  146. package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
  147. package/src/storage/tagging_store/sender_tagging_store.ts +210 -126
  148. package/src/tagging/get_all_logs_by_tags.ts +28 -4
  149. package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +4 -4
  150. package/src/tagging/recipient_sync/utils/find_highest_indexes.ts +2 -2
  151. package/dest/contract_sync/index.d.ts +0 -23
  152. package/dest/contract_sync/index.d.ts.map +0 -1
  153. package/dest/private_kernel/hints/compute_tx_include_by_timestamp.d.ts +0 -4
  154. package/dest/private_kernel/hints/compute_tx_include_by_timestamp.d.ts.map +0 -1
package/src/pxe.ts CHANGED
@@ -33,6 +33,7 @@ import type {
33
33
  PrivateKernelTailCircuitPublicInputs,
34
34
  } from '@aztec/stdlib/kernel';
35
35
  import {
36
+ BlockHeader,
36
37
  type ContractOverrides,
37
38
  type InTx,
38
39
  PrivateExecutionResult,
@@ -51,6 +52,7 @@ import {
51
52
 
52
53
  import { inspect } from 'util';
53
54
 
55
+ import type { AccessScopes } from './access_scopes.js';
54
56
  import { BlockSynchronizer } from './block_synchronizer/index.js';
55
57
  import type { PXEConfig } from './config/index.js';
56
58
  import { BenchmarkedNodeFactory } from './contract_function_simulator/benchmarked_node.js';
@@ -59,7 +61,8 @@ import {
59
61
  generateSimulatedProvingResult,
60
62
  } from './contract_function_simulator/contract_function_simulator.js';
61
63
  import { ProxiedContractStoreFactory } from './contract_function_simulator/proxied_contract_data_source.js';
62
- import { ensureContractSynced, readCurrentClassId } from './contract_sync/index.js';
64
+ import { ContractSyncService } from './contract_sync/contract_sync_service.js';
65
+ import { readCurrentClassId } from './contract_sync/helpers.js';
63
66
  import { PXEDebugUtils } from './debug/pxe_debug_utils.js';
64
67
  import { enrichPublicSimulationError, enrichSimulationError } from './error_enriching.js';
65
68
  import { PrivateEventFilterValidator } from './events/private_event_filter_validator.js';
@@ -84,6 +87,56 @@ export type PackedPrivateEvent = InTx & {
84
87
  eventSelector: EventSelector;
85
88
  };
86
89
 
90
+ /** Options for PXE.profileTx. */
91
+ export type ProfileTxOpts = {
92
+ /** The profiling mode to use. */
93
+ profileMode: 'full' | 'execution-steps' | 'gates';
94
+ /** If true, proof generation is skipped during profiling. Defaults to true. */
95
+ skipProofGeneration?: boolean;
96
+ /** Addresses whose private state and keys are accessible during private execution. */
97
+ scopes: AccessScopes;
98
+ };
99
+
100
+ /** Options for PXE.simulateTx. */
101
+ export type SimulateTxOpts = {
102
+ /** Whether to simulate the public part of the transaction. */
103
+ simulatePublic: boolean;
104
+ /** If false, this function throws if the transaction is unable to be included in a block at the current state. */
105
+ skipTxValidation?: boolean;
106
+ /** If false, fees are enforced. */
107
+ skipFeeEnforcement?: boolean;
108
+ /** State overrides for the simulation, such as contract instances and artifacts. */
109
+ overrides?: SimulationOverrides;
110
+ /** Addresses whose private state and keys are accessible during private execution */
111
+ scopes: AccessScopes;
112
+ };
113
+
114
+ /** Options for PXE.simulateUtility. */
115
+ export type SimulateUtilityOpts = {
116
+ /** The authentication witnesses required for the function call. */
117
+ authwits?: AuthWitness[];
118
+ /** The accounts whose notes we can access in this call */
119
+ scopes: AccessScopes;
120
+ };
121
+
122
+ /** Args for PXE.create. */
123
+ export type PXECreateArgs = {
124
+ /** The Aztec node to connect to. */
125
+ node: AztecNode;
126
+ /** The key-value store for persisting PXE state. */
127
+ store: AztecAsyncKVStore;
128
+ /** The prover for generating private kernel proofs. */
129
+ proofCreator: PrivateKernelProver;
130
+ /** The circuit simulator for executing ACIR circuits. */
131
+ simulator: CircuitSimulator;
132
+ /** Provider for protocol contract artifacts and instances. */
133
+ protocolContractsProvider: ProtocolContractsProvider;
134
+ /** PXE configuration options. */
135
+ config: PXEConfig;
136
+ /** Optional logger instance or string suffix for the logger name. */
137
+ loggerOrSuffix?: string | Logger;
138
+ };
139
+
87
140
  /**
88
141
  * Private eXecution Environment (PXE) is a library used by wallets to simulate private phase of transactions and to
89
142
  * manage private state of users.
@@ -102,6 +155,7 @@ export class PXE {
102
155
  private recipientTaggingStore: RecipientTaggingStore,
103
156
  private addressStore: AddressStore,
104
157
  private privateEventStore: PrivateEventStore,
158
+ private contractSyncService: ContractSyncService,
105
159
  private simulator: CircuitSimulator,
106
160
  private proverEnabled: boolean,
107
161
  private proofCreator: PrivateKernelProver,
@@ -119,15 +173,15 @@ export class PXE {
119
173
  *
120
174
  * @returns A promise that resolves PXE is ready to be used.
121
175
  */
122
- public static async create(
123
- node: AztecNode,
124
- store: AztecAsyncKVStore,
125
- proofCreator: PrivateKernelProver,
126
- simulator: CircuitSimulator,
127
- protocolContractsProvider: ProtocolContractsProvider,
128
- config: PXEConfig,
129
- loggerOrSuffix?: string | Logger,
130
- ) {
176
+ public static async create({
177
+ node,
178
+ store,
179
+ proofCreator,
180
+ simulator,
181
+ protocolContractsProvider,
182
+ config,
183
+ loggerOrSuffix,
184
+ }: PXECreateArgs) {
131
185
  // Extract bindings from the logger, or use empty bindings if a string suffix is provided.
132
186
  const bindings: LoggerBindings | undefined =
133
187
  loggerOrSuffix && typeof loggerOrSuffix !== 'string' ? loggerOrSuffix.getBindings() : undefined;
@@ -137,7 +191,9 @@ export class PXE {
137
191
  ? createLogger(loggerOrSuffix ? `pxe:service:${loggerOrSuffix}` : `pxe:service`)
138
192
  : loggerOrSuffix;
139
193
 
140
- const proverEnabled = !!config.proverEnabled;
194
+ const info = await node.getNodeInfo();
195
+
196
+ const proverEnabled = config.proverEnabled !== undefined ? config.proverEnabled : info.realProofs;
141
197
  const addressStore = new AddressStore(store);
142
198
  const privateEventStore = new PrivateEventStore(store);
143
199
  const contractStore = new ContractStore(store);
@@ -149,6 +205,12 @@ export class PXE {
149
205
  const capsuleStore = new CapsuleStore(store);
150
206
  const keyStore = new KeyStore(store);
151
207
  const tipsStore = new L2TipsKVStore(store, 'pxe');
208
+ const contractSyncService = new ContractSyncService(
209
+ node,
210
+ contractStore,
211
+ noteStore,
212
+ createLogger('pxe:contract_sync', bindings),
213
+ );
152
214
  const synchronizer = new BlockSynchronizer(
153
215
  node,
154
216
  store,
@@ -156,6 +218,7 @@ export class PXE {
156
218
  noteStore,
157
219
  privateEventStore,
158
220
  tipsStore,
221
+ contractSyncService,
159
222
  config,
160
223
  bindings,
161
224
  );
@@ -167,9 +230,10 @@ export class PXE {
167
230
  recipientTaggingStore,
168
231
  privateEventStore,
169
232
  noteStore,
233
+ contractSyncService,
170
234
  ]);
171
235
 
172
- const debugUtils = new PXEDebugUtils(contractStore, noteStore, synchronizer, anchorBlockStore);
236
+ const debugUtils = new PXEDebugUtils(contractSyncService, noteStore, synchronizer, anchorBlockStore);
173
237
 
174
238
  const jobQueue = new SerialQueue();
175
239
 
@@ -186,6 +250,7 @@ export class PXE {
186
250
  recipientTaggingStore,
187
251
  addressStore,
188
252
  privateEventStore,
253
+ contractSyncService,
189
254
  simulator,
190
255
  proverEnabled,
191
256
  proofCreator,
@@ -196,12 +261,15 @@ export class PXE {
196
261
  debugUtils,
197
262
  );
198
263
 
199
- debugUtils.setPXE(pxe, pxe.#putInJobQueue.bind(pxe));
264
+ debugUtils.setPXEHelpers(
265
+ pxe.#putInJobQueue.bind(pxe),
266
+ pxe.#getSimulatorForTx.bind(pxe),
267
+ pxe.#simulateUtility.bind(pxe),
268
+ );
200
269
 
201
270
  pxe.jobQueue.start();
202
271
 
203
272
  await pxe.#registerProtocolContracts();
204
- const info = await node.getNodeInfo();
205
273
  log.info(`Started PXE connected to chain ${info.l1ChainId} version ${info.rollupVersion}`);
206
274
  return pxe;
207
275
  }
@@ -211,20 +279,20 @@ export class PXE {
211
279
  #getSimulatorForTx(overrides?: { contracts?: ContractOverrides }) {
212
280
  const proxyContractStore = ProxiedContractStoreFactory.create(this.contractStore, overrides?.contracts);
213
281
 
214
- return new ContractFunctionSimulator(
215
- proxyContractStore,
216
- this.noteStore,
217
- this.keyStore,
218
- this.addressStore,
219
- BenchmarkedNodeFactory.create(this.node),
220
- this.anchorBlockStore,
221
- this.senderTaggingStore,
222
- this.recipientTaggingStore,
223
- this.senderAddressBookStore,
224
- this.capsuleStore,
225
- this.privateEventStore,
226
- this.simulator,
227
- );
282
+ return new ContractFunctionSimulator({
283
+ contractStore: proxyContractStore,
284
+ noteStore: this.noteStore,
285
+ keyStore: this.keyStore,
286
+ addressStore: this.addressStore,
287
+ aztecNode: BenchmarkedNodeFactory.create(this.node),
288
+ senderTaggingStore: this.senderTaggingStore,
289
+ recipientTaggingStore: this.recipientTaggingStore,
290
+ senderAddressBookStore: this.senderAddressBookStore,
291
+ capsuleStore: this.capsuleStore,
292
+ privateEventStore: this.privateEventStore,
293
+ simulator: this.simulator,
294
+ contractSyncService: this.contractSyncService,
295
+ });
228
296
  }
229
297
 
230
298
  #contextualizeError(err: Error, ...context: string[]): Error {
@@ -290,7 +358,7 @@ export class PXE {
290
358
  async #executePrivate(
291
359
  contractFunctionSimulator: ContractFunctionSimulator,
292
360
  txRequest: TxExecutionRequest,
293
- scopes: AztecAddress[] | undefined,
361
+ scopes: AccessScopes,
294
362
  jobId: string,
295
363
  ): Promise<PrivateExecutionResult> {
296
364
  const { origin: contractAddress, functionSelector } = txRequest;
@@ -298,27 +366,23 @@ export class PXE {
298
366
  try {
299
367
  const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
300
368
 
301
- await ensureContractSynced(
369
+ await this.contractSyncService.ensureContractSynced(
302
370
  contractAddress,
303
371
  functionSelector,
304
- privateSyncCall => this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
305
- this.node,
306
- this.contractStore,
372
+ (privateSyncCall, execScopes) =>
373
+ this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId),
307
374
  anchorBlockHeader,
375
+ jobId,
376
+ scopes,
308
377
  );
309
378
 
310
- const result = await contractFunctionSimulator.run(
311
- txRequest,
379
+ const result = await contractFunctionSimulator.run(txRequest, {
312
380
  contractAddress,
313
- functionSelector,
314
- undefined,
381
+ selector: functionSelector,
315
382
  anchorBlockHeader,
316
- // The sender for tags is set by contracts, typically by an account
317
- // contract entrypoint
318
- undefined, // senderForTags
319
383
  scopes,
320
384
  jobId,
321
- );
385
+ });
322
386
  this.log.debug(`Private simulation completed for ${contractAddress.toString()}:${functionSelector}`);
323
387
  return result;
324
388
  } catch (err) {
@@ -343,7 +407,7 @@ export class PXE {
343
407
  contractFunctionSimulator: ContractFunctionSimulator,
344
408
  call: FunctionCall,
345
409
  authWitnesses: AuthWitness[] | undefined,
346
- scopes: AztecAddress[] | undefined,
410
+ scopes: AccessScopes,
347
411
  jobId: string,
348
412
  ) {
349
413
  try {
@@ -416,6 +480,19 @@ export class PXE {
416
480
 
417
481
  // Public API
418
482
 
483
+ /**
484
+ * Returns the block header up to which the PXE has synced.
485
+ * @returns The synced block header
486
+ */
487
+ public getSyncedBlockHeader(): Promise<BlockHeader> {
488
+ return this.anchorBlockStore.getBlockHeader();
489
+ }
490
+
491
+ /**
492
+ * Returns the contract instance for a given address, if it's registered in the PXE.
493
+ * @param address - The contract address.
494
+ * @returns The contract instance if found, undefined otherwise.
495
+ */
419
496
  public getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
420
497
  return this.contractStore.getContractInstance(address);
421
498
  }
@@ -629,11 +706,12 @@ export class PXE {
629
706
  * (where validators prove the public portion).
630
707
  *
631
708
  * @param txRequest - An authenticated tx request ready for proving
709
+ * @param scopes - Addresses whose private state and keys are accessible during private execution.
632
710
  * @returns A result containing the proof and public inputs of the tail circuit.
633
711
  * @throws If contract code not found, or public simulation reverts.
634
712
  * Also throws if simulatePublic is true and public simulation reverts.
635
713
  */
636
- public proveTx(txRequest: TxExecutionRequest): Promise<TxProvingResult> {
714
+ public proveTx(txRequest: TxExecutionRequest, scopes: AztecAddress[]): Promise<TxProvingResult> {
637
715
  let privateExecutionResult: PrivateExecutionResult;
638
716
  // We disable proving concurrently mostly out of caution, since it accesses some of our stores. Proving is so
639
717
  // computationally demanding that it'd be rare for someone to try to do it concurrently regardless.
@@ -644,7 +722,7 @@ export class PXE {
644
722
  await this.blockStateSynchronizer.sync();
645
723
  const syncTime = syncTimer.ms();
646
724
  const contractFunctionSimulator = this.#getSimulatorForTx();
647
- privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, undefined, jobId);
725
+ privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, scopes, jobId);
648
726
 
649
727
  const {
650
728
  publicInputs,
@@ -708,17 +786,13 @@ export class PXE {
708
786
 
709
787
  /**
710
788
  * Profiles a transaction, reporting gate counts (unless disabled) and returns an execution trace.
711
- *
712
- * @param txRequest - An authenticated tx request ready for simulation
713
- * @param msgSender - (Optional) The message sender to use for the simulation.
714
- * @param skipTxValidation - (Optional) If false, this function throws if the transaction is unable to be included in a block at the current state.
789
+ * @param txRequest - An authenticated tx request ready for simulation.
715
790
  * @returns A trace of the program execution with gate counts.
716
791
  * @throws If the code for the functions executed in this transaction have not been made available via `addContracts`.
717
792
  */
718
793
  public profileTx(
719
794
  txRequest: TxExecutionRequest,
720
- profileMode: 'full' | 'execution-steps' | 'gates',
721
- skipProofGeneration: boolean = true,
795
+ { profileMode, skipProofGeneration = true, scopes }: ProfileTxOpts,
722
796
  ): Promise<TxProfileResult> {
723
797
  // We disable concurrent profiles for consistency with simulateTx.
724
798
  return this.#putInJobQueue(async jobId => {
@@ -741,12 +815,7 @@ export class PXE {
741
815
  const syncTime = syncTimer.ms();
742
816
 
743
817
  const contractFunctionSimulator = this.#getSimulatorForTx();
744
- const privateExecutionResult = await this.#executePrivate(
745
- contractFunctionSimulator,
746
- txRequest,
747
- undefined,
748
- jobId,
749
- );
818
+ const privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, scopes, jobId);
750
819
 
751
820
  const { executionSteps, timings: { proving } = {} } = await this.#prove(
752
821
  txRequest,
@@ -803,12 +872,7 @@ export class PXE {
803
872
  * In that case, the transaction returned is only potentially ready to be sent to the network for execution.
804
873
  *
805
874
  *
806
- * @param txRequest - An authenticated tx request ready for simulation
807
- * @param simulatePublic - Whether to simulate the public part of the transaction.
808
- * @param skipTxValidation - (Optional) If false, this function throws if the transaction is unable to be included in a block at the current state.
809
- * @param skipFeeEnforcement - (Optional) If false, fees are enforced.
810
- * @param overrides - (Optional) State overrides for the simulation, such as msgSender, contract instances and artifacts.
811
- * @param scopes - (Optional) The accounts whose notes we can access in this call. Currently optional and will default to all.
875
+ * @param txRequest - An authenticated tx request ready for simulation.
812
876
  * @returns A simulated transaction result object that includes public and private return values.
813
877
  * @throws If the code for the functions executed in this transaction have not been made available via `addContracts`.
814
878
  * Also throws if simulatePublic is true and public simulation reverts.
@@ -817,11 +881,7 @@ export class PXE {
817
881
  */
818
882
  public simulateTx(
819
883
  txRequest: TxExecutionRequest,
820
- simulatePublic: boolean,
821
- skipTxValidation: boolean = false,
822
- skipFeeEnforcement: boolean = false,
823
- overrides?: SimulationOverrides,
824
- scopes?: AztecAddress[],
884
+ { simulatePublic, skipTxValidation = false, skipFeeEnforcement = false, overrides, scopes }: SimulateTxOpts,
825
885
  ): Promise<TxSimulationResult> {
826
886
  // We disable concurrent simulations since those might execute oracles which read and write to the PXE stores (e.g.
827
887
  // to the capsules), and we need to prevent concurrent runs from interfering with one another (e.g. attempting to
@@ -849,7 +909,14 @@ export class PXE {
849
909
  // Temporary: in case there are overrides, we have to skip the kernels or validations
850
910
  // will fail. Consider handing control to the user/wallet on whether they want to run them
851
911
  // or not.
852
- const skipKernels = overrides?.contracts !== undefined && Object.keys(overrides.contracts ?? {}).length > 0;
912
+ const overriddenContracts = overrides?.contracts ? new Set(Object.keys(overrides.contracts)) : undefined;
913
+ const hasOverriddenContracts = overriddenContracts !== undefined && overriddenContracts.size > 0;
914
+ const skipKernels = hasOverriddenContracts;
915
+
916
+ // Set overridden contracts on the sync service so it knows to skip syncing them
917
+ if (hasOverriddenContracts) {
918
+ this.contractSyncService.setOverriddenContracts(jobId, overriddenContracts);
919
+ }
853
920
 
854
921
  // Execution of private functions only; no proving, and no kernel logic.
855
922
  const privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, scopes, jobId);
@@ -860,7 +927,8 @@ export class PXE {
860
927
  if (skipKernels) {
861
928
  ({ publicInputs, executionSteps } = await generateSimulatedProvingResult(
862
929
  privateExecutionResult,
863
- this.contractStore,
930
+ (addr, sel) => this.contractStore.getDebugFunctionName(addr, sel),
931
+ this.node,
864
932
  ));
865
933
  } else {
866
934
  // Kernel logic, plus proving of all private functions and kernels.
@@ -938,25 +1006,19 @@ export class PXE {
938
1006
  inspect(txRequest),
939
1007
  `simulatePublic=${simulatePublic}`,
940
1008
  `skipTxValidation=${skipTxValidation}`,
941
- `scopes=${scopes?.map(s => s.toString()).join(', ') ?? 'undefined'}`,
1009
+ `scopes=${scopes === 'ALL_SCOPES' ? scopes : scopes.map(s => s.toString()).join(', ')}`,
942
1010
  );
943
1011
  }
944
1012
  });
945
1013
  }
946
1014
 
947
1015
  /**
948
- * Simulate the execution of a contract utility function.
949
- *
1016
+ * Simulates the execution of a contract utility function.
950
1017
  * @param call - The function call containing the function details, arguments, and target contract address.
951
- * @param authwits - (Optional) The authentication witnesses required for the function call.
952
- * @param scopes - (Optional) The accounts whose notes we can access in this call. Currently optional and will
953
- * default to all.
954
- * @returns The result of the utility function call, structured based on the function ABI.
955
1018
  */
956
1019
  public simulateUtility(
957
1020
  call: FunctionCall,
958
- authwits?: AuthWitness[],
959
- scopes?: AztecAddress[],
1021
+ { authwits, scopes }: SimulateUtilityOpts = { scopes: 'ALL_SCOPES' },
960
1022
  ): Promise<UtilitySimulationResult> {
961
1023
  // We disable concurrent simulations since those might execute oracles which read and write to the PXE stores (e.g.
962
1024
  // to the capsules), and we need to prevent concurrent runs from interfering with one another (e.g. attempting to
@@ -971,13 +1033,14 @@ export class PXE {
971
1033
  const contractFunctionSimulator = this.#getSimulatorForTx();
972
1034
 
973
1035
  const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
974
- await ensureContractSynced(
1036
+ await this.contractSyncService.ensureContractSynced(
975
1037
  call.to,
976
1038
  call.selector,
977
- privateSyncCall => this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
978
- this.node,
979
- this.contractStore,
1039
+ (privateSyncCall, execScopes) =>
1040
+ this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId),
980
1041
  anchorBlockHeader,
1042
+ jobId,
1043
+ scopes,
981
1044
  );
982
1045
 
983
1046
  const executionResult = await this.#simulateUtility(
@@ -1008,7 +1071,7 @@ export class PXE {
1008
1071
  throw this.#contextualizeError(
1009
1072
  err,
1010
1073
  `simulateUtility ${to}:${name}(${stringifiedArgs})`,
1011
- `scopes=${scopes?.map(s => s.toString()).join(', ') ?? 'undefined'}`,
1074
+ `scopes=${scopes === 'ALL_SCOPES' ? scopes : scopes.map(s => s.toString()).join(', ')}`,
1012
1075
  );
1013
1076
  }
1014
1077
  });
@@ -1041,14 +1104,14 @@ export class PXE {
1041
1104
 
1042
1105
  const contractFunctionSimulator = this.#getSimulatorForTx();
1043
1106
 
1044
- await ensureContractSynced(
1107
+ await this.contractSyncService.ensureContractSynced(
1045
1108
  filter.contractAddress,
1046
1109
  null,
1047
- async privateSyncCall =>
1048
- await this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
1049
- this.node,
1050
- this.contractStore,
1110
+ async (privateSyncCall, execScopes) =>
1111
+ await this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId),
1051
1112
  anchorBlockHeader,
1113
+ jobId,
1114
+ filter.scopes,
1052
1115
  );
1053
1116
  });
1054
1117
 
@@ -43,23 +43,23 @@ export class AddressStore {
43
43
  });
44
44
  }
45
45
 
46
- async #getCompleteAddress(address: AztecAddress): Promise<CompleteAddress | undefined> {
47
- const index = await this.#completeAddressIndex.getAsync(address.toString());
48
- if (index === undefined) {
49
- return undefined;
50
- }
51
-
52
- const value = await this.#completeAddresses.atAsync(index);
53
- return value ? await CompleteAddress.fromBuffer(value) : undefined;
54
- }
55
-
56
46
  getCompleteAddress(account: AztecAddress): Promise<CompleteAddress | undefined> {
57
- return this.#getCompleteAddress(account);
47
+ return this.#store.transactionAsync(async () => {
48
+ const index = await this.#completeAddressIndex.getAsync(account.toString());
49
+ if (index === undefined) {
50
+ return undefined;
51
+ }
52
+
53
+ const value = await this.#completeAddresses.atAsync(index);
54
+ return value ? await CompleteAddress.fromBuffer(value) : undefined;
55
+ });
58
56
  }
59
57
 
60
- async getCompleteAddresses(): Promise<CompleteAddress[]> {
61
- return await Promise.all(
62
- (await toArray(this.#completeAddresses.valuesAsync())).map(v => CompleteAddress.fromBuffer(v)),
63
- );
58
+ getCompleteAddresses(): Promise<CompleteAddress[]> {
59
+ return this.#store.transactionAsync(async () => {
60
+ return await Promise.all(
61
+ (await toArray(this.#completeAddresses.valuesAsync())).map(v => CompleteAddress.fromBuffer(v)),
62
+ );
63
+ });
64
64
  }
65
65
  }
@@ -10,6 +10,14 @@ export class AnchorBlockStore {
10
10
  this.#synchronizedHeader = this.#store.openSingleton('header');
11
11
  }
12
12
 
13
+ /**
14
+ * Sets the currently synchronized block
15
+ *
16
+ * Important: this method is only called from BlockSynchronizer, and since we need it to run atomically with other
17
+ * stores in the case of a reorg, it MUST NOT be wrapped in a `transactionAsync` call. Doing so would result in a
18
+ * deadlock when the backend is IndexedDB, because `transactionAsync` is not designed to support reentrancy.
19
+ *
20
+ */
13
21
  async setHeader(header: BlockHeader): Promise<void> {
14
22
  await this.#synchronizedHeader.set(header.toBuffer());
15
23
  }
@@ -57,14 +57,14 @@ export class CapsuleStore implements StagedStore {
57
57
  */
58
58
  async #getFromStage(jobId: string, dbSlotKey: string): Promise<Buffer | null | undefined> {
59
59
  const jobStagedCapsules = this.#getJobStagedCapsules(jobId);
60
- let staged: Buffer | null | undefined = jobStagedCapsules.get(dbSlotKey);
61
- // Note that if staged === null, we marked it for deletion, so we don't want to
62
- // re-read it from DB
63
- if (staged === undefined) {
64
- // If we don't have a staged version of this dbSlotKey, first we check if there's one in DB
65
- staged = await this.#loadCapsuleFromDb(dbSlotKey);
66
- }
67
- return staged;
60
+ const staged: Buffer | null | undefined = jobStagedCapsules.get(dbSlotKey);
61
+
62
+ // Always issue DB read to keep IndexedDB transaction alive, even if the value is in the job staged data. This
63
+ // keeps IndexedDB transactions alive (they auto-commit when a new micro-task starts and there are no pending read
64
+ // requests). The staged value still takes precedence if it exists (including null for deletions).
65
+ const dbValue = await this.#loadCapsuleFromDb(dbSlotKey);
66
+
67
+ return staged !== undefined ? staged : dbValue;
68
68
  }
69
69
 
70
70
  /**
@@ -42,10 +42,12 @@ export class ContractStore {
42
42
  /** Map from contract address to contract class id */
43
43
  #contractClassIdMap: Map<string, Fr> = new Map();
44
44
 
45
+ #store: AztecAsyncKVStore;
45
46
  #contractArtifacts: AztecAsyncMap<string, Buffer>;
46
47
  #contractInstances: AztecAsyncMap<string, Buffer>;
47
48
 
48
49
  constructor(store: AztecAsyncKVStore) {
50
+ this.#store = store;
49
51
  this.#contractArtifacts = store.openMap('contract_artifacts');
50
52
  this.#contractInstances = store.openMap('contracts_instances');
51
53
  }
@@ -53,6 +55,7 @@ export class ContractStore {
53
55
  // Setters
54
56
 
55
57
  public async addContractArtifact(id: Fr, contract: ContractArtifact): Promise<void> {
58
+ // Validation outside transactionAsync - these are not DB operations
56
59
  const privateFunctions = contract.functions.filter(
57
60
  functionArtifact => functionArtifact.functionType === FunctionType.PRIVATE,
58
61
  );
@@ -69,7 +72,9 @@ export class ContractStore {
69
72
  throw new Error('Repeated function selectors of private functions');
70
73
  }
71
74
 
72
- await this.#contractArtifacts.set(id.toString(), contractArtifactToBuffer(contract));
75
+ await this.#store.transactionAsync(() =>
76
+ this.#contractArtifacts.set(id.toString(), contractArtifactToBuffer(contract)),
77
+ );
73
78
  }
74
79
 
75
80
  async addContractInstance(contract: ContractInstanceWithAddress): Promise<void> {
@@ -123,21 +128,27 @@ export class ContractStore {
123
128
 
124
129
  // Public getters
125
130
 
126
- async getContractsAddresses(): Promise<AztecAddress[]> {
127
- const keys = await toArray(this.#contractInstances.keysAsync());
128
- return keys.map(AztecAddress.fromString);
131
+ getContractsAddresses(): Promise<AztecAddress[]> {
132
+ return this.#store.transactionAsync(async () => {
133
+ const keys = await toArray(this.#contractInstances.keysAsync());
134
+ return keys.map(AztecAddress.fromString);
135
+ });
129
136
  }
130
137
 
131
138
  /** Returns a contract instance for a given address. Throws if not found. */
132
- public async getContractInstance(contractAddress: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
133
- const contract = await this.#contractInstances.getAsync(contractAddress.toString());
134
- return contract && SerializableContractInstance.fromBuffer(contract).withAddress(contractAddress);
139
+ public getContractInstance(contractAddress: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
140
+ return this.#store.transactionAsync(async () => {
141
+ const contract = await this.#contractInstances.getAsync(contractAddress.toString());
142
+ return contract && SerializableContractInstance.fromBuffer(contract).withAddress(contractAddress);
143
+ });
135
144
  }
136
145
 
137
- public async getContractArtifact(contractClassId: Fr): Promise<ContractArtifact | undefined> {
138
- const contract = await this.#contractArtifacts.getAsync(contractClassId.toString());
139
- // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
140
- return contract && contractArtifactFromBuffer(Buffer.from(contract));
146
+ public getContractArtifact(contractClassId: Fr): Promise<ContractArtifact | undefined> {
147
+ return this.#store.transactionAsync(async () => {
148
+ const contract = await this.#contractArtifacts.getAsync(contractClassId.toString());
149
+ // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
150
+ return contract && contractArtifactFromBuffer(Buffer.from(contract));
151
+ });
141
152
  }
142
153
 
143
154
  /** Returns a contract class for a given class id. Throws if not found. */
@@ -305,15 +316,15 @@ export class ContractStore {
305
316
  throw new Error(`Unknown function ${functionName} in contract ${contract.name}.`);
306
317
  }
307
318
 
308
- return {
319
+ return FunctionCall.from({
309
320
  name: functionDao.name,
310
- args: encodeArguments(functionDao, args),
321
+ to,
311
322
  selector: await FunctionSelector.fromNameAndParameters(functionDao.name, functionDao.parameters),
312
323
  type: functionDao.functionType,
313
- to,
314
324
  hideMsgSender: false,
315
325
  isStatic: functionDao.isStatic,
326
+ args: encodeArguments(functionDao, args),
316
327
  returnTypes: functionDao.returnTypes,
317
- };
328
+ });
318
329
  }
319
330
  }
@@ -1 +1 @@
1
- export const PXE_DATA_SCHEMA_VERSION = 2;
1
+ export const PXE_DATA_SCHEMA_VERSION = 3;