@aztec/pxe 0.0.1-commit.9372f48 → 0.0.1-commit.967fc6998

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 (165) 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/config/package_info.js +1 -1
  8. package/dest/contract_function_simulator/contract_function_simulator.d.ts +54 -28
  9. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  10. package/dest/contract_function_simulator/contract_function_simulator.js +174 -68
  11. package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts +5 -5
  12. package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts.map +1 -1
  13. package/dest/contract_function_simulator/execution_tagging_index_cache.js +3 -3
  14. package/dest/contract_function_simulator/noir-structs/event_validation_request.js +1 -1
  15. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +2 -2
  16. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts.map +1 -1
  17. package/dest/contract_function_simulator/noir-structs/note_validation_request.js +1 -1
  18. package/dest/contract_function_simulator/oracle/interfaces.d.ts +3 -3
  19. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  20. package/dest/contract_function_simulator/oracle/oracle.d.ts +3 -3
  21. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  22. package/dest/contract_function_simulator/oracle/oracle.js +23 -11
  23. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +36 -35
  24. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
  25. package/dest/contract_function_simulator/oracle/private_execution_oracle.js +73 -28
  26. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +35 -15
  27. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  28. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +63 -46
  29. package/dest/contract_logging.d.ts +22 -0
  30. package/dest/contract_logging.d.ts.map +1 -0
  31. package/dest/contract_logging.js +23 -0
  32. package/dest/contract_sync/contract_sync_service.d.ts +43 -0
  33. package/dest/contract_sync/contract_sync_service.d.ts.map +1 -0
  34. package/dest/contract_sync/contract_sync_service.js +97 -0
  35. package/dest/contract_sync/helpers.d.ts +29 -0
  36. package/dest/contract_sync/helpers.d.ts.map +1 -0
  37. package/dest/contract_sync/{index.js → helpers.js} +13 -12
  38. package/dest/debug/pxe_debug_utils.d.ts +14 -10
  39. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  40. package/dest/debug/pxe_debug_utils.js +16 -15
  41. package/dest/entrypoints/client/bundle/index.d.ts +4 -1
  42. package/dest/entrypoints/client/bundle/index.d.ts.map +1 -1
  43. package/dest/entrypoints/client/bundle/index.js +3 -0
  44. package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
  45. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
  46. package/dest/entrypoints/client/bundle/utils.js +9 -1
  47. package/dest/entrypoints/client/lazy/index.d.ts +4 -1
  48. package/dest/entrypoints/client/lazy/index.d.ts.map +1 -1
  49. package/dest/entrypoints/client/lazy/index.js +3 -0
  50. package/dest/entrypoints/client/lazy/utils.d.ts +1 -1
  51. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  52. package/dest/entrypoints/client/lazy/utils.js +9 -1
  53. package/dest/entrypoints/server/index.d.ts +4 -2
  54. package/dest/entrypoints/server/index.d.ts.map +1 -1
  55. package/dest/entrypoints/server/index.js +3 -1
  56. package/dest/entrypoints/server/utils.js +9 -1
  57. package/dest/logs/log_service.d.ts +3 -2
  58. package/dest/logs/log_service.d.ts.map +1 -1
  59. package/dest/logs/log_service.js +11 -18
  60. package/dest/notes/note_service.d.ts +4 -3
  61. package/dest/notes/note_service.d.ts.map +1 -1
  62. package/dest/notes/note_service.js +3 -2
  63. package/dest/notes_filter.d.ts +25 -0
  64. package/dest/notes_filter.d.ts.map +1 -0
  65. package/dest/notes_filter.js +4 -0
  66. package/dest/oracle_version.d.ts +2 -2
  67. package/dest/oracle_version.js +2 -2
  68. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts +4 -0
  69. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts.map +1 -0
  70. package/dest/private_kernel/hints/{compute_tx_include_by_timestamp.js → compute_tx_expiration_timestamp.js} +12 -12
  71. package/dest/private_kernel/hints/index.d.ts +1 -1
  72. package/dest/private_kernel/hints/index.js +1 -1
  73. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts +4 -3
  74. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts.map +1 -1
  75. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.js +129 -68
  76. package/dest/private_kernel/hints/test_utils.d.ts +122 -0
  77. package/dest/private_kernel/hints/test_utils.d.ts.map +1 -0
  78. package/dest/private_kernel/hints/test_utils.js +203 -0
  79. package/dest/private_kernel/private_kernel_execution_prover.d.ts +1 -1
  80. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  81. package/dest/private_kernel/private_kernel_execution_prover.js +19 -11
  82. package/dest/private_kernel/private_kernel_oracle.d.ts +6 -2
  83. package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
  84. package/dest/private_kernel/private_kernel_oracle.js +7 -3
  85. package/dest/pxe.d.ts +71 -24
  86. package/dest/pxe.d.ts.map +1 -1
  87. package/dest/pxe.js +100 -64
  88. package/dest/storage/contract_store/contract_store.d.ts +42 -15
  89. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  90. package/dest/storage/contract_store/contract_store.js +145 -69
  91. package/dest/storage/note_store/note_store.d.ts +3 -3
  92. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  93. package/dest/storage/note_store/note_store.js +6 -4
  94. package/dest/storage/tagging_store/recipient_tagging_store.d.ts +6 -6
  95. package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
  96. package/dest/storage/tagging_store/sender_tagging_store.d.ts +5 -5
  97. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  98. package/dest/storage/tagging_store/sender_tagging_store.js +4 -4
  99. package/dest/tagging/get_all_logs_by_tags.d.ts +1 -1
  100. package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -1
  101. package/dest/tagging/get_all_logs_by_tags.js +17 -3
  102. package/dest/tagging/index.d.ts +2 -2
  103. package/dest/tagging/index.d.ts.map +1 -1
  104. package/dest/tagging/index.js +1 -1
  105. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +4 -5
  106. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
  107. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.js +7 -7
  108. package/dest/tagging/recipient_sync/utils/find_highest_indexes.js +2 -2
  109. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +6 -7
  110. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
  111. package/dest/tagging/recipient_sync/utils/load_logs_for_range.js +12 -11
  112. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +4 -8
  113. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
  114. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +3 -6
  115. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +4 -7
  116. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
  117. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.js +14 -15
  118. package/package.json +25 -16
  119. package/src/access_scopes.ts +9 -0
  120. package/src/block_synchronizer/block_synchronizer.ts +6 -0
  121. package/src/config/package_info.ts +1 -1
  122. package/src/contract_function_simulator/contract_function_simulator.ts +323 -128
  123. package/src/contract_function_simulator/execution_tagging_index_cache.ts +5 -5
  124. package/src/contract_function_simulator/noir-structs/event_validation_request.ts +1 -1
  125. package/src/contract_function_simulator/noir-structs/note_validation_request.ts +1 -1
  126. package/src/contract_function_simulator/oracle/interfaces.ts +2 -2
  127. package/src/contract_function_simulator/oracle/oracle.ts +14 -7
  128. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +99 -105
  129. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +108 -45
  130. package/src/contract_logging.ts +39 -0
  131. package/src/contract_sync/contract_sync_service.ts +152 -0
  132. package/src/contract_sync/{index.ts → helpers.ts} +21 -21
  133. package/src/debug/pxe_debug_utils.ts +48 -18
  134. package/src/entrypoints/client/bundle/index.ts +3 -0
  135. package/src/entrypoints/client/bundle/utils.ts +9 -1
  136. package/src/entrypoints/client/lazy/index.ts +3 -0
  137. package/src/entrypoints/client/lazy/utils.ts +9 -1
  138. package/src/entrypoints/server/index.ts +3 -1
  139. package/src/entrypoints/server/utils.ts +7 -7
  140. package/src/logs/log_service.ts +18 -25
  141. package/src/notes/note_service.ts +4 -3
  142. package/src/notes_filter.ts +26 -0
  143. package/src/oracle_version.ts +2 -2
  144. package/src/private_kernel/hints/{compute_tx_include_by_timestamp.ts → compute_tx_expiration_timestamp.ts} +13 -13
  145. package/src/private_kernel/hints/index.ts +1 -1
  146. package/src/private_kernel/hints/private_kernel_reset_private_inputs_builder.ts +164 -117
  147. package/src/private_kernel/hints/test_utils.ts +325 -0
  148. package/src/private_kernel/private_kernel_execution_prover.ts +19 -12
  149. package/src/private_kernel/private_kernel_oracle.ts +7 -7
  150. package/src/pxe.ts +184 -113
  151. package/src/storage/contract_store/contract_store.ts +174 -75
  152. package/src/storage/note_store/note_store.ts +12 -5
  153. package/src/storage/tagging_store/recipient_tagging_store.ts +9 -5
  154. package/src/storage/tagging_store/sender_tagging_store.ts +8 -8
  155. package/src/tagging/get_all_logs_by_tags.ts +28 -4
  156. package/src/tagging/index.ts +1 -1
  157. package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +7 -10
  158. package/src/tagging/recipient_sync/utils/find_highest_indexes.ts +2 -2
  159. package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +10 -15
  160. package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +4 -9
  161. package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +11 -20
  162. package/dest/contract_sync/index.d.ts +0 -23
  163. package/dest/contract_sync/index.d.ts.map +0 -1
  164. package/dest/private_kernel/hints/compute_tx_include_by_timestamp.d.ts +0 -4
  165. 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,
@@ -46,11 +47,12 @@ import {
46
47
  TxProfileResult,
47
48
  TxProvingResult,
48
49
  TxSimulationResult,
49
- UtilitySimulationResult,
50
+ UtilityExecutionResult,
50
51
  } from '@aztec/stdlib/tx';
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,9 @@ 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 { displayDebugLogs } from './contract_logging.js';
65
+ import { ContractSyncService } from './contract_sync/contract_sync_service.js';
66
+ import { readCurrentClassId } from './contract_sync/helpers.js';
63
67
  import { PXEDebugUtils } from './debug/pxe_debug_utils.js';
64
68
  import { enrichPublicSimulationError, enrichSimulationError } from './error_enriching.js';
65
69
  import { PrivateEventFilterValidator } from './events/private_event_filter_validator.js';
@@ -84,6 +88,56 @@ export type PackedPrivateEvent = InTx & {
84
88
  eventSelector: EventSelector;
85
89
  };
86
90
 
91
+ /** Options for PXE.profileTx. */
92
+ export type ProfileTxOpts = {
93
+ /** The profiling mode to use. */
94
+ profileMode: 'full' | 'execution-steps' | 'gates';
95
+ /** If true, proof generation is skipped during profiling. Defaults to true. */
96
+ skipProofGeneration?: boolean;
97
+ /** Addresses whose private state and keys are accessible during private execution. */
98
+ scopes: AccessScopes;
99
+ };
100
+
101
+ /** Options for PXE.simulateTx. */
102
+ export type SimulateTxOpts = {
103
+ /** Whether to simulate the public part of the transaction. */
104
+ simulatePublic: boolean;
105
+ /** If false, this function throws if the transaction is unable to be included in a block at the current state. */
106
+ skipTxValidation?: boolean;
107
+ /** If false, fees are enforced. */
108
+ skipFeeEnforcement?: boolean;
109
+ /** State overrides for the simulation, such as contract instances and artifacts. */
110
+ overrides?: SimulationOverrides;
111
+ /** Addresses whose private state and keys are accessible during private execution */
112
+ scopes: AccessScopes;
113
+ };
114
+
115
+ /** Options for PXE.executeUtility. */
116
+ export type ExecuteUtilityOpts = {
117
+ /** The authentication witnesses required for the function call. */
118
+ authwits?: AuthWitness[];
119
+ /** The accounts whose notes we can access in this call */
120
+ scopes: AccessScopes;
121
+ };
122
+
123
+ /** Args for PXE.create. */
124
+ export type PXECreateArgs = {
125
+ /** The Aztec node to connect to. */
126
+ node: AztecNode;
127
+ /** The key-value store for persisting PXE state. */
128
+ store: AztecAsyncKVStore;
129
+ /** The prover for generating private kernel proofs. */
130
+ proofCreator: PrivateKernelProver;
131
+ /** The circuit simulator for executing ACIR circuits. */
132
+ simulator: CircuitSimulator;
133
+ /** Provider for protocol contract artifacts and instances. */
134
+ protocolContractsProvider: ProtocolContractsProvider;
135
+ /** PXE configuration options. */
136
+ config: PXEConfig;
137
+ /** Optional logger instance or string suffix for the logger name. */
138
+ loggerOrSuffix?: string | Logger;
139
+ };
140
+
87
141
  /**
88
142
  * Private eXecution Environment (PXE) is a library used by wallets to simulate private phase of transactions and to
89
143
  * manage private state of users.
@@ -91,6 +145,7 @@ export type PackedPrivateEvent = InTx & {
91
145
  export class PXE {
92
146
  private constructor(
93
147
  private node: AztecNode,
148
+ private db: AztecAsyncKVStore,
94
149
  private blockStateSynchronizer: BlockSynchronizer,
95
150
  private keyStore: KeyStore,
96
151
  private contractStore: ContractStore,
@@ -102,6 +157,7 @@ export class PXE {
102
157
  private recipientTaggingStore: RecipientTaggingStore,
103
158
  private addressStore: AddressStore,
104
159
  private privateEventStore: PrivateEventStore,
160
+ private contractSyncService: ContractSyncService,
105
161
  private simulator: CircuitSimulator,
106
162
  private proverEnabled: boolean,
107
163
  private proofCreator: PrivateKernelProver,
@@ -119,15 +175,15 @@ export class PXE {
119
175
  *
120
176
  * @returns A promise that resolves PXE is ready to be used.
121
177
  */
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
- ) {
178
+ public static async create({
179
+ node,
180
+ store,
181
+ proofCreator,
182
+ simulator,
183
+ protocolContractsProvider,
184
+ config,
185
+ loggerOrSuffix,
186
+ }: PXECreateArgs) {
131
187
  // Extract bindings from the logger, or use empty bindings if a string suffix is provided.
132
188
  const bindings: LoggerBindings | undefined =
133
189
  loggerOrSuffix && typeof loggerOrSuffix !== 'string' ? loggerOrSuffix.getBindings() : undefined;
@@ -137,7 +193,9 @@ export class PXE {
137
193
  ? createLogger(loggerOrSuffix ? `pxe:service:${loggerOrSuffix}` : `pxe:service`)
138
194
  : loggerOrSuffix;
139
195
 
140
- const proverEnabled = !!config.proverEnabled;
196
+ const info = await node.getNodeInfo();
197
+
198
+ const proverEnabled = config.proverEnabled !== undefined ? config.proverEnabled : info.realProofs;
141
199
  const addressStore = new AddressStore(store);
142
200
  const privateEventStore = new PrivateEventStore(store);
143
201
  const contractStore = new ContractStore(store);
@@ -149,6 +207,12 @@ export class PXE {
149
207
  const capsuleStore = new CapsuleStore(store);
150
208
  const keyStore = new KeyStore(store);
151
209
  const tipsStore = new L2TipsKVStore(store, 'pxe');
210
+ const contractSyncService = new ContractSyncService(
211
+ node,
212
+ contractStore,
213
+ noteStore,
214
+ createLogger('pxe:contract_sync', bindings),
215
+ );
152
216
  const synchronizer = new BlockSynchronizer(
153
217
  node,
154
218
  store,
@@ -156,6 +220,7 @@ export class PXE {
156
220
  noteStore,
157
221
  privateEventStore,
158
222
  tipsStore,
223
+ contractSyncService,
159
224
  config,
160
225
  bindings,
161
226
  );
@@ -167,14 +232,16 @@ export class PXE {
167
232
  recipientTaggingStore,
168
233
  privateEventStore,
169
234
  noteStore,
235
+ contractSyncService,
170
236
  ]);
171
237
 
172
- const debugUtils = new PXEDebugUtils(contractStore, noteStore, synchronizer, anchorBlockStore);
238
+ const debugUtils = new PXEDebugUtils(contractSyncService, noteStore, synchronizer, anchorBlockStore);
173
239
 
174
240
  const jobQueue = new SerialQueue();
175
241
 
176
242
  const pxe = new PXE(
177
243
  node,
244
+ store,
178
245
  synchronizer,
179
246
  keyStore,
180
247
  contractStore,
@@ -186,6 +253,7 @@ export class PXE {
186
253
  recipientTaggingStore,
187
254
  addressStore,
188
255
  privateEventStore,
256
+ contractSyncService,
189
257
  simulator,
190
258
  proverEnabled,
191
259
  proofCreator,
@@ -196,12 +264,15 @@ export class PXE {
196
264
  debugUtils,
197
265
  );
198
266
 
199
- debugUtils.setPXE(pxe, pxe.#putInJobQueue.bind(pxe));
267
+ debugUtils.setPXEHelpers(
268
+ pxe.#putInJobQueue.bind(pxe),
269
+ pxe.#getSimulatorForTx.bind(pxe),
270
+ pxe.#executeUtility.bind(pxe),
271
+ );
200
272
 
201
273
  pxe.jobQueue.start();
202
274
 
203
275
  await pxe.#registerProtocolContracts();
204
- const info = await node.getNodeInfo();
205
276
  log.info(`Started PXE connected to chain ${info.l1ChainId} version ${info.rollupVersion}`);
206
277
  return pxe;
207
278
  }
@@ -211,19 +282,20 @@ export class PXE {
211
282
  #getSimulatorForTx(overrides?: { contracts?: ContractOverrides }) {
212
283
  const proxyContractStore = ProxiedContractStoreFactory.create(this.contractStore, overrides?.contracts);
213
284
 
214
- return new ContractFunctionSimulator(
215
- proxyContractStore,
216
- this.noteStore,
217
- this.keyStore,
218
- this.addressStore,
219
- BenchmarkedNodeFactory.create(this.node),
220
- this.senderTaggingStore,
221
- this.recipientTaggingStore,
222
- this.senderAddressBookStore,
223
- this.capsuleStore,
224
- this.privateEventStore,
225
- this.simulator,
226
- );
285
+ return new ContractFunctionSimulator({
286
+ contractStore: proxyContractStore,
287
+ noteStore: this.noteStore,
288
+ keyStore: this.keyStore,
289
+ addressStore: this.addressStore,
290
+ aztecNode: BenchmarkedNodeFactory.create(this.node),
291
+ senderTaggingStore: this.senderTaggingStore,
292
+ recipientTaggingStore: this.recipientTaggingStore,
293
+ senderAddressBookStore: this.senderAddressBookStore,
294
+ capsuleStore: this.capsuleStore,
295
+ privateEventStore: this.privateEventStore,
296
+ simulator: this.simulator,
297
+ contractSyncService: this.contractSyncService,
298
+ });
227
299
  }
228
300
 
229
301
  #contextualizeError(err: Error, ...context: string[]): Error {
@@ -275,9 +347,8 @@ export class PXE {
275
347
  async #registerProtocolContracts() {
276
348
  const registered: Record<string, string> = {};
277
349
  for (const name of protocolContractNames) {
278
- const { address, contractClass, instance, artifact } =
279
- await this.protocolContractsProvider.getProtocolContractArtifact(name);
280
- await this.contractStore.addContractArtifact(contractClass.id, artifact);
350
+ const { address, instance, artifact } = await this.protocolContractsProvider.getProtocolContractArtifact(name);
351
+ await this.contractStore.addContractArtifact(artifact);
281
352
  await this.contractStore.addContractInstance(instance);
282
353
  registered[name] = address.toString();
283
354
  }
@@ -289,7 +360,7 @@ export class PXE {
289
360
  async #executePrivate(
290
361
  contractFunctionSimulator: ContractFunctionSimulator,
291
362
  txRequest: TxExecutionRequest,
292
- scopes: AztecAddress[] | undefined,
363
+ scopes: AccessScopes,
293
364
  jobId: string,
294
365
  ): Promise<PrivateExecutionResult> {
295
366
  const { origin: contractAddress, functionSelector } = txRequest;
@@ -297,27 +368,23 @@ export class PXE {
297
368
  try {
298
369
  const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
299
370
 
300
- await ensureContractSynced(
371
+ await this.contractSyncService.ensureContractSynced(
301
372
  contractAddress,
302
373
  functionSelector,
303
- privateSyncCall => this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
304
- this.node,
305
- this.contractStore,
374
+ (privateSyncCall, execScopes) =>
375
+ this.#executeUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId),
306
376
  anchorBlockHeader,
377
+ jobId,
378
+ scopes,
307
379
  );
308
380
 
309
- const result = await contractFunctionSimulator.run(
310
- txRequest,
381
+ const result = await contractFunctionSimulator.run(txRequest, {
311
382
  contractAddress,
312
- functionSelector,
313
- undefined,
383
+ selector: functionSelector,
314
384
  anchorBlockHeader,
315
- // The sender for tags is set by contracts, typically by an account
316
- // contract entrypoint
317
- undefined, // senderForTags
318
385
  scopes,
319
386
  jobId,
320
- );
387
+ });
321
388
  this.log.debug(`Private simulation completed for ${contractAddress.toString()}:${functionSelector}`);
322
389
  return result;
323
390
  } catch (err) {
@@ -329,20 +396,20 @@ export class PXE {
329
396
  }
330
397
 
331
398
  /**
332
- * Simulate a utility function call on the given contract.
399
+ * Execute a utility function call on the given contract.
333
400
  * @param contractFunctionSimulator - The simulator to use for the function call.
334
401
  * @param call - The function call to execute.
335
402
  * @param authWitnesses - Authentication witnesses required for the function call.
336
403
  * @param scopes - Optional array of account addresses whose notes can be accessed in this call. Defaults to all
337
404
  * accounts if not specified.
338
405
  * @param jobId - The job ID for staged writes.
339
- * @returns The simulation result containing the outputs of the utility function.
406
+ * @returns The execution result containing the outputs of the utility function.
340
407
  */
341
- async #simulateUtility(
408
+ async #executeUtility(
342
409
  contractFunctionSimulator: ContractFunctionSimulator,
343
410
  call: FunctionCall,
344
411
  authWitnesses: AuthWitness[] | undefined,
345
- scopes: AztecAddress[] | undefined,
412
+ scopes: AccessScopes,
346
413
  jobId: string,
347
414
  ) {
348
415
  try {
@@ -415,6 +482,19 @@ export class PXE {
415
482
 
416
483
  // Public API
417
484
 
485
+ /**
486
+ * Returns the block header up to which the PXE has synced.
487
+ * @returns The synced block header
488
+ */
489
+ public getSyncedBlockHeader(): Promise<BlockHeader> {
490
+ return this.anchorBlockStore.getBlockHeader();
491
+ }
492
+
493
+ /**
494
+ * Returns the contract instance for a given address, if it's registered in the PXE.
495
+ * @param address - The contract address.
496
+ * @returns The contract instance if found, undefined otherwise.
497
+ */
418
498
  public getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
419
499
  return this.contractStore.getContractInstance(address);
420
500
  }
@@ -523,8 +603,7 @@ export class PXE {
523
603
  * @param artifact - The build artifact for the contract class.
524
604
  */
525
605
  public async registerContractClass(artifact: ContractArtifact): Promise<void> {
526
- const { id: contractClassId } = await getContractClassFromArtifact(artifact);
527
- await this.contractStore.addContractArtifact(contractClassId, artifact);
606
+ const contractClassId = await this.contractStore.addContractArtifact(artifact);
528
607
  this.log.info(`Added contract class ${artifact.name} with id ${contractClassId}`);
529
608
  }
530
609
 
@@ -543,17 +622,17 @@ export class PXE {
543
622
  if (artifact) {
544
623
  // If the user provides an artifact, validate it against the expected class id and register it
545
624
  const contractClass = await getContractClassFromArtifact(artifact);
546
- const contractClassId = contractClass.id;
547
- if (!contractClassId.equals(instance.currentContractClassId)) {
625
+ if (!contractClass.id.equals(instance.currentContractClassId)) {
548
626
  throw new Error(
549
- `Artifact does not match expected class id (computed ${contractClassId} but instance refers to ${instance.currentContractClassId})`,
627
+ `Artifact does not match expected class id (computed ${contractClass.id} but instance refers to ${instance.currentContractClassId})`,
550
628
  );
551
629
  }
552
630
  const computedAddress = await computeContractAddressFromInstance(instance);
553
631
  if (!computedAddress.equals(instance.address)) {
554
632
  throw new Error('Added a contract in which the address does not match the contract instance.');
555
633
  }
556
- await this.contractStore.addContractArtifact(contractClass.id, artifact);
634
+
635
+ await this.contractStore.addContractArtifact(artifact, contractClass);
557
636
 
558
637
  const publicFunctionSignatures = artifact.functions
559
638
  .filter(fn => fn.functionType === FunctionType.PUBLIC)
@@ -602,15 +681,16 @@ export class PXE {
602
681
  throw new Error('Could not update contract to a class different from the current one.');
603
682
  }
604
683
 
605
- await this.contractStore.addContractArtifact(contractClass.id, artifact);
606
-
607
684
  const publicFunctionSignatures = artifact.functions
608
685
  .filter(fn => fn.functionType === FunctionType.PUBLIC)
609
686
  .map(fn => decodeFunctionSignature(fn.name, fn.parameters));
610
687
  await this.node.registerContractFunctionSignatures(publicFunctionSignatures);
611
688
 
612
689
  currentInstance.currentContractClassId = contractClass.id;
613
- await this.contractStore.addContractInstance(currentInstance);
690
+ await Promise.all([
691
+ this.contractStore.addContractArtifact(artifact, contractClass),
692
+ this.contractStore.addContractInstance(currentInstance),
693
+ ]);
614
694
  this.log.info(`Updated contract ${artifact.name} at ${contractAddress.toString()} to class ${contractClass.id}`);
615
695
  });
616
696
  }
@@ -628,11 +708,12 @@ export class PXE {
628
708
  * (where validators prove the public portion).
629
709
  *
630
710
  * @param txRequest - An authenticated tx request ready for proving
711
+ * @param scopes - Addresses whose private state and keys are accessible during private execution.
631
712
  * @returns A result containing the proof and public inputs of the tail circuit.
632
713
  * @throws If contract code not found, or public simulation reverts.
633
714
  * Also throws if simulatePublic is true and public simulation reverts.
634
715
  */
635
- public proveTx(txRequest: TxExecutionRequest): Promise<TxProvingResult> {
716
+ public proveTx(txRequest: TxExecutionRequest, scopes: AztecAddress[]): Promise<TxProvingResult> {
636
717
  let privateExecutionResult: PrivateExecutionResult;
637
718
  // We disable proving concurrently mostly out of caution, since it accesses some of our stores. Proving is so
638
719
  // computationally demanding that it'd be rare for someone to try to do it concurrently regardless.
@@ -643,7 +724,7 @@ export class PXE {
643
724
  await this.blockStateSynchronizer.sync();
644
725
  const syncTime = syncTimer.ms();
645
726
  const contractFunctionSimulator = this.#getSimulatorForTx();
646
- privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, undefined, jobId);
727
+ privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, scopes, jobId);
647
728
 
648
729
  const {
649
730
  publicInputs,
@@ -707,17 +788,13 @@ export class PXE {
707
788
 
708
789
  /**
709
790
  * Profiles a transaction, reporting gate counts (unless disabled) and returns an execution trace.
710
- *
711
- * @param txRequest - An authenticated tx request ready for simulation
712
- * @param msgSender - (Optional) The message sender to use for the simulation.
713
- * @param skipTxValidation - (Optional) If false, this function throws if the transaction is unable to be included in a block at the current state.
791
+ * @param txRequest - An authenticated tx request ready for simulation.
714
792
  * @returns A trace of the program execution with gate counts.
715
793
  * @throws If the code for the functions executed in this transaction have not been made available via `addContracts`.
716
794
  */
717
795
  public profileTx(
718
796
  txRequest: TxExecutionRequest,
719
- profileMode: 'full' | 'execution-steps' | 'gates',
720
- skipProofGeneration: boolean = true,
797
+ { profileMode, skipProofGeneration = true, scopes }: ProfileTxOpts,
721
798
  ): Promise<TxProfileResult> {
722
799
  // We disable concurrent profiles for consistency with simulateTx.
723
800
  return this.#putInJobQueue(async jobId => {
@@ -740,12 +817,7 @@ export class PXE {
740
817
  const syncTime = syncTimer.ms();
741
818
 
742
819
  const contractFunctionSimulator = this.#getSimulatorForTx();
743
- const privateExecutionResult = await this.#executePrivate(
744
- contractFunctionSimulator,
745
- txRequest,
746
- undefined,
747
- jobId,
748
- );
820
+ const privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, scopes, jobId);
749
821
 
750
822
  const { executionSteps, timings: { proving } = {} } = await this.#prove(
751
823
  txRequest,
@@ -802,12 +874,7 @@ export class PXE {
802
874
  * In that case, the transaction returned is only potentially ready to be sent to the network for execution.
803
875
  *
804
876
  *
805
- * @param txRequest - An authenticated tx request ready for simulation
806
- * @param simulatePublic - Whether to simulate the public part of the transaction.
807
- * @param skipTxValidation - (Optional) If false, this function throws if the transaction is unable to be included in a block at the current state.
808
- * @param skipFeeEnforcement - (Optional) If false, fees are enforced.
809
- * @param overrides - (Optional) State overrides for the simulation, such as msgSender, contract instances and artifacts.
810
- * @param scopes - (Optional) The accounts whose notes we can access in this call. Currently optional and will default to all.
877
+ * @param txRequest - An authenticated tx request ready for simulation.
811
878
  * @returns A simulated transaction result object that includes public and private return values.
812
879
  * @throws If the code for the functions executed in this transaction have not been made available via `addContracts`.
813
880
  * Also throws if simulatePublic is true and public simulation reverts.
@@ -816,11 +883,7 @@ export class PXE {
816
883
  */
817
884
  public simulateTx(
818
885
  txRequest: TxExecutionRequest,
819
- simulatePublic: boolean,
820
- skipTxValidation: boolean = false,
821
- skipFeeEnforcement: boolean = false,
822
- overrides?: SimulationOverrides,
823
- scopes?: AztecAddress[],
886
+ { simulatePublic, skipTxValidation = false, skipFeeEnforcement = false, overrides, scopes }: SimulateTxOpts,
824
887
  ): Promise<TxSimulationResult> {
825
888
  // We disable concurrent simulations since those might execute oracles which read and write to the PXE stores (e.g.
826
889
  // to the capsules), and we need to prevent concurrent runs from interfering with one another (e.g. attempting to
@@ -848,7 +911,14 @@ export class PXE {
848
911
  // Temporary: in case there are overrides, we have to skip the kernels or validations
849
912
  // will fail. Consider handing control to the user/wallet on whether they want to run them
850
913
  // or not.
851
- const skipKernels = overrides?.contracts !== undefined && Object.keys(overrides.contracts ?? {}).length > 0;
914
+ const overriddenContracts = overrides?.contracts ? new Set(Object.keys(overrides.contracts)) : undefined;
915
+ const hasOverriddenContracts = overriddenContracts !== undefined && overriddenContracts.size > 0;
916
+ const skipKernels = hasOverriddenContracts;
917
+
918
+ // Set overridden contracts on the sync service so it knows to skip syncing them
919
+ if (hasOverriddenContracts) {
920
+ this.contractSyncService.setOverriddenContracts(jobId, overriddenContracts);
921
+ }
852
922
 
853
923
  // Execution of private functions only; no proving, and no kernel logic.
854
924
  const privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, scopes, jobId);
@@ -859,7 +929,8 @@ export class PXE {
859
929
  if (skipKernels) {
860
930
  ({ publicInputs, executionSteps } = await generateSimulatedProvingResult(
861
931
  privateExecutionResult,
862
- this.contractStore,
932
+ (addr, sel) => this.contractStore.getDebugFunctionName(addr, sel),
933
+ this.node,
863
934
  ));
864
935
  } else {
865
936
  // Kernel logic, plus proving of all private functions and kernels.
@@ -878,6 +949,9 @@ export class PXE {
878
949
  const publicSimulationTimer = new Timer();
879
950
  publicOutput = await this.#simulatePublicCalls(simulatedTx, skipFeeEnforcement);
880
951
  publicSimulationTime = publicSimulationTimer.ms();
952
+ if (publicOutput?.debugLogs?.length) {
953
+ await displayDebugLogs(publicOutput.debugLogs, addr => this.contractStore.getDebugContractName(addr));
954
+ }
881
955
  }
882
956
 
883
957
  let validationTime: number | undefined;
@@ -886,7 +960,8 @@ export class PXE {
886
960
  const validationResult = await this.node.isValidTx(simulatedTx, { isSimulation: true, skipFeeEnforcement });
887
961
  validationTime = validationTimer.ms();
888
962
  if (validationResult.result === 'invalid') {
889
- throw new Error('The simulated transaction is unable to be added to state and is invalid.');
963
+ const reason = validationResult.reason.length > 0 ? ` Reason: ${validationResult.reason.join(', ')}` : '';
964
+ throw new Error(`The simulated transaction is unable to be added to state and is invalid.${reason}`);
890
965
  }
891
966
  }
892
967
 
@@ -937,29 +1012,23 @@ export class PXE {
937
1012
  inspect(txRequest),
938
1013
  `simulatePublic=${simulatePublic}`,
939
1014
  `skipTxValidation=${skipTxValidation}`,
940
- `scopes=${scopes?.map(s => s.toString()).join(', ') ?? 'undefined'}`,
1015
+ `scopes=${scopes === 'ALL_SCOPES' ? scopes : scopes.map(s => s.toString()).join(', ')}`,
941
1016
  );
942
1017
  }
943
1018
  });
944
1019
  }
945
1020
 
946
1021
  /**
947
- * Simulate the execution of a contract utility function.
948
- *
1022
+ * Executes a contract utility function.
949
1023
  * @param call - The function call containing the function details, arguments, and target contract address.
950
- * @param authwits - (Optional) The authentication witnesses required for the function call.
951
- * @param scopes - (Optional) The accounts whose notes we can access in this call. Currently optional and will
952
- * default to all.
953
- * @returns The result of the utility function call, structured based on the function ABI.
954
1024
  */
955
- public simulateUtility(
1025
+ public executeUtility(
956
1026
  call: FunctionCall,
957
- authwits?: AuthWitness[],
958
- scopes?: AztecAddress[],
959
- ): Promise<UtilitySimulationResult> {
960
- // We disable concurrent simulations since those might execute oracles which read and write to the PXE stores (e.g.
1027
+ { authwits, scopes }: ExecuteUtilityOpts = { scopes: 'ALL_SCOPES' },
1028
+ ): Promise<UtilityExecutionResult> {
1029
+ // We disable concurrent executions since those might execute oracles which read and write to the PXE stores (e.g.
961
1030
  // to the capsules), and we need to prevent concurrent runs from interfering with one another (e.g. attempting to
962
- // delete the same read value, or reading values that another simulation is currently modifying).
1031
+ // delete the same read value, or reading values that another execution is currently modifying).
963
1032
  return this.#putInJobQueue(async jobId => {
964
1033
  try {
965
1034
  const totalTimer = new Timer();
@@ -970,16 +1039,17 @@ export class PXE {
970
1039
  const contractFunctionSimulator = this.#getSimulatorForTx();
971
1040
 
972
1041
  const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
973
- await ensureContractSynced(
1042
+ await this.contractSyncService.ensureContractSynced(
974
1043
  call.to,
975
1044
  call.selector,
976
- privateSyncCall => this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
977
- this.node,
978
- this.contractStore,
1045
+ (privateSyncCall, execScopes) =>
1046
+ this.#executeUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId),
979
1047
  anchorBlockHeader,
1048
+ jobId,
1049
+ scopes,
980
1050
  );
981
1051
 
982
- const executionResult = await this.#simulateUtility(
1052
+ const executionResult = await this.#executeUtility(
983
1053
  contractFunctionSimulator,
984
1054
  call,
985
1055
  authwits ?? [],
@@ -1006,8 +1076,8 @@ export class PXE {
1006
1076
  const stringifiedArgs = args.map(arg => arg.toString()).join(', ');
1007
1077
  throw this.#contextualizeError(
1008
1078
  err,
1009
- `simulateUtility ${to}:${name}(${stringifiedArgs})`,
1010
- `scopes=${scopes?.map(s => s.toString()).join(', ') ?? 'undefined'}`,
1079
+ `executeUtility ${to}:${name}(${stringifiedArgs})`,
1080
+ `scopes=${scopes === 'ALL_SCOPES' ? scopes : scopes.map(s => s.toString()).join(', ')}`,
1011
1081
  );
1012
1082
  }
1013
1083
  });
@@ -1040,14 +1110,14 @@ export class PXE {
1040
1110
 
1041
1111
  const contractFunctionSimulator = this.#getSimulatorForTx();
1042
1112
 
1043
- await ensureContractSynced(
1113
+ await this.contractSyncService.ensureContractSynced(
1044
1114
  filter.contractAddress,
1045
1115
  null,
1046
- async privateSyncCall =>
1047
- await this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
1048
- this.node,
1049
- this.contractStore,
1116
+ async (privateSyncCall, execScopes) =>
1117
+ await this.#executeUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId),
1050
1118
  anchorBlockHeader,
1119
+ jobId,
1120
+ filter.scopes,
1051
1121
  );
1052
1122
  });
1053
1123
 
@@ -1062,9 +1132,10 @@ export class PXE {
1062
1132
  }
1063
1133
 
1064
1134
  /**
1065
- * Stops the PXE's job queue.
1135
+ * Stops the PXE's job queue and closes the backing store.
1066
1136
  */
1067
- public stop(): Promise<void> {
1068
- return this.jobQueue.end();
1137
+ public async stop(): Promise<void> {
1138
+ await this.jobQueue.end();
1139
+ await this.db.close();
1069
1140
  }
1070
1141
  }