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

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 (200) 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/index.d.ts +2 -2
  8. package/dest/config/index.d.ts.map +1 -1
  9. package/dest/config/index.js +1 -1
  10. package/dest/config/package_info.js +1 -1
  11. package/dest/contract_function_simulator/contract_function_simulator.d.ts +54 -30
  12. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  13. package/dest/contract_function_simulator/contract_function_simulator.js +177 -73
  14. package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts +5 -5
  15. package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts.map +1 -1
  16. package/dest/contract_function_simulator/execution_tagging_index_cache.js +3 -3
  17. package/dest/contract_function_simulator/noir-structs/event_validation_request.js +1 -1
  18. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +2 -2
  19. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts.map +1 -1
  20. package/dest/contract_function_simulator/noir-structs/note_validation_request.js +1 -1
  21. package/dest/contract_function_simulator/oracle/interfaces.d.ts +49 -45
  22. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  23. package/dest/contract_function_simulator/oracle/oracle.d.ts +44 -44
  24. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  25. package/dest/contract_function_simulator/oracle/oracle.js +156 -101
  26. package/dest/contract_function_simulator/oracle/private_execution.js +1 -1
  27. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +55 -55
  28. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
  29. package/dest/contract_function_simulator/oracle/private_execution_oracle.js +101 -56
  30. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +77 -51
  31. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  32. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +116 -94
  33. package/dest/contract_logging.d.ts +22 -0
  34. package/dest/contract_logging.d.ts.map +1 -0
  35. package/dest/contract_logging.js +23 -0
  36. package/dest/contract_sync/contract_sync_service.d.ts +43 -0
  37. package/dest/contract_sync/contract_sync_service.d.ts.map +1 -0
  38. package/dest/contract_sync/contract_sync_service.js +97 -0
  39. package/dest/contract_sync/helpers.d.ts +29 -0
  40. package/dest/contract_sync/helpers.d.ts.map +1 -0
  41. package/dest/contract_sync/{index.js → helpers.js} +13 -12
  42. package/dest/debug/pxe_debug_utils.d.ts +14 -10
  43. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  44. package/dest/debug/pxe_debug_utils.js +16 -15
  45. package/dest/entrypoints/client/bundle/index.d.ts +4 -1
  46. package/dest/entrypoints/client/bundle/index.d.ts.map +1 -1
  47. package/dest/entrypoints/client/bundle/index.js +3 -0
  48. package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
  49. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
  50. package/dest/entrypoints/client/bundle/utils.js +11 -2
  51. package/dest/entrypoints/client/lazy/index.d.ts +4 -1
  52. package/dest/entrypoints/client/lazy/index.d.ts.map +1 -1
  53. package/dest/entrypoints/client/lazy/index.js +3 -0
  54. package/dest/entrypoints/client/lazy/utils.d.ts +1 -1
  55. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  56. package/dest/entrypoints/client/lazy/utils.js +11 -2
  57. package/dest/entrypoints/server/index.d.ts +4 -2
  58. package/dest/entrypoints/server/index.d.ts.map +1 -1
  59. package/dest/entrypoints/server/index.js +3 -1
  60. package/dest/entrypoints/server/utils.js +9 -1
  61. package/dest/events/event_service.d.ts +4 -5
  62. package/dest/events/event_service.d.ts.map +1 -1
  63. package/dest/events/event_service.js +5 -6
  64. package/dest/logs/log_service.d.ts +6 -5
  65. package/dest/logs/log_service.d.ts.map +1 -1
  66. package/dest/logs/log_service.js +18 -28
  67. package/dest/notes/note_service.d.ts +7 -7
  68. package/dest/notes/note_service.d.ts.map +1 -1
  69. package/dest/notes/note_service.js +9 -9
  70. package/dest/notes_filter.d.ts +25 -0
  71. package/dest/notes_filter.d.ts.map +1 -0
  72. package/dest/notes_filter.js +4 -0
  73. package/dest/oracle_version.d.ts +3 -3
  74. package/dest/oracle_version.d.ts.map +1 -1
  75. package/dest/oracle_version.js +3 -3
  76. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts +4 -0
  77. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts.map +1 -0
  78. package/dest/private_kernel/hints/{compute_tx_include_by_timestamp.js → compute_tx_expiration_timestamp.js} +12 -12
  79. package/dest/private_kernel/hints/index.d.ts +1 -1
  80. package/dest/private_kernel/hints/index.js +1 -1
  81. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts +4 -3
  82. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts.map +1 -1
  83. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.js +129 -68
  84. package/dest/private_kernel/hints/test_utils.d.ts +122 -0
  85. package/dest/private_kernel/hints/test_utils.d.ts.map +1 -0
  86. package/dest/private_kernel/hints/test_utils.js +203 -0
  87. package/dest/private_kernel/private_kernel_execution_prover.d.ts +1 -1
  88. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  89. package/dest/private_kernel/private_kernel_execution_prover.js +19 -11
  90. package/dest/private_kernel/private_kernel_oracle.d.ts +6 -2
  91. package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
  92. package/dest/private_kernel/private_kernel_oracle.js +7 -3
  93. package/dest/pxe.d.ts +71 -24
  94. package/dest/pxe.d.ts.map +1 -1
  95. package/dest/pxe.js +100 -64
  96. package/dest/storage/address_store/address_store.d.ts +1 -1
  97. package/dest/storage/address_store/address_store.d.ts.map +1 -1
  98. package/dest/storage/address_store/address_store.js +12 -11
  99. package/dest/storage/anchor_block_store/anchor_block_store.d.ts +9 -1
  100. package/dest/storage/anchor_block_store/anchor_block_store.d.ts.map +1 -1
  101. package/dest/storage/anchor_block_store/anchor_block_store.js +8 -1
  102. package/dest/storage/capsule_store/capsule_store.js +6 -8
  103. package/dest/storage/contract_store/contract_store.d.ts +42 -15
  104. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  105. package/dest/storage/contract_store/contract_store.js +157 -72
  106. package/dest/storage/metadata.d.ts +1 -1
  107. package/dest/storage/metadata.js +1 -1
  108. package/dest/storage/note_store/note_store.d.ts +13 -3
  109. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  110. package/dest/storage/note_store/note_store.js +147 -107
  111. package/dest/storage/private_event_store/private_event_store.d.ts +1 -1
  112. package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
  113. package/dest/storage/private_event_store/private_event_store.js +84 -61
  114. package/dest/storage/private_event_store/stored_private_event.js +1 -1
  115. package/dest/storage/tagging_store/recipient_tagging_store.d.ts +6 -6
  116. package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
  117. package/dest/storage/tagging_store/recipient_tagging_store.js +31 -19
  118. package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
  119. package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
  120. package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
  121. package/dest/storage/tagging_store/sender_tagging_store.d.ts +5 -5
  122. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  123. package/dest/storage/tagging_store/sender_tagging_store.js +184 -114
  124. package/dest/tagging/get_all_logs_by_tags.d.ts +1 -1
  125. package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -1
  126. package/dest/tagging/get_all_logs_by_tags.js +17 -3
  127. package/dest/tagging/index.d.ts +2 -2
  128. package/dest/tagging/index.d.ts.map +1 -1
  129. package/dest/tagging/index.js +1 -1
  130. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +4 -5
  131. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
  132. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.js +7 -7
  133. package/dest/tagging/recipient_sync/utils/find_highest_indexes.js +2 -2
  134. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +6 -7
  135. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
  136. package/dest/tagging/recipient_sync/utils/load_logs_for_range.js +12 -11
  137. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +4 -8
  138. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
  139. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +3 -6
  140. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +4 -7
  141. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
  142. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.js +14 -15
  143. package/package.json +25 -16
  144. package/src/access_scopes.ts +9 -0
  145. package/src/block_synchronizer/block_synchronizer.ts +6 -0
  146. package/src/config/index.ts +1 -1
  147. package/src/config/package_info.ts +1 -1
  148. package/src/contract_function_simulator/contract_function_simulator.ts +326 -131
  149. package/src/contract_function_simulator/execution_tagging_index_cache.ts +5 -5
  150. package/src/contract_function_simulator/noir-structs/event_validation_request.ts +1 -1
  151. package/src/contract_function_simulator/noir-structs/note_validation_request.ts +1 -1
  152. package/src/contract_function_simulator/oracle/interfaces.ts +53 -50
  153. package/src/contract_function_simulator/oracle/oracle.ts +157 -112
  154. package/src/contract_function_simulator/oracle/private_execution.ts +1 -1
  155. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +127 -137
  156. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +167 -107
  157. package/src/contract_logging.ts +39 -0
  158. package/src/contract_sync/contract_sync_service.ts +152 -0
  159. package/src/contract_sync/{index.ts → helpers.ts} +21 -21
  160. package/src/debug/pxe_debug_utils.ts +48 -18
  161. package/src/entrypoints/client/bundle/index.ts +3 -0
  162. package/src/entrypoints/client/bundle/utils.ts +12 -2
  163. package/src/entrypoints/client/lazy/index.ts +3 -0
  164. package/src/entrypoints/client/lazy/utils.ts +12 -2
  165. package/src/entrypoints/server/index.ts +3 -1
  166. package/src/entrypoints/server/utils.ts +7 -7
  167. package/src/events/event_service.ts +4 -6
  168. package/src/logs/log_service.ts +25 -35
  169. package/src/notes/note_service.ts +9 -10
  170. package/src/notes_filter.ts +26 -0
  171. package/src/oracle_version.ts +3 -3
  172. package/src/private_kernel/hints/{compute_tx_include_by_timestamp.ts → compute_tx_expiration_timestamp.ts} +13 -13
  173. package/src/private_kernel/hints/index.ts +1 -1
  174. package/src/private_kernel/hints/private_kernel_reset_private_inputs_builder.ts +164 -117
  175. package/src/private_kernel/hints/test_utils.ts +325 -0
  176. package/src/private_kernel/private_kernel_execution_prover.ts +19 -12
  177. package/src/private_kernel/private_kernel_oracle.ts +7 -7
  178. package/src/pxe.ts +184 -114
  179. package/src/storage/address_store/address_store.ts +15 -15
  180. package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
  181. package/src/storage/capsule_store/capsule_store.ts +8 -8
  182. package/src/storage/contract_store/contract_store.ts +186 -76
  183. package/src/storage/metadata.ts +1 -1
  184. package/src/storage/note_store/note_store.ts +169 -132
  185. package/src/storage/private_event_store/private_event_store.ts +102 -81
  186. package/src/storage/private_event_store/stored_private_event.ts +1 -1
  187. package/src/storage/tagging_store/recipient_tagging_store.ts +38 -24
  188. package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
  189. package/src/storage/tagging_store/sender_tagging_store.ts +214 -130
  190. package/src/tagging/get_all_logs_by_tags.ts +28 -4
  191. package/src/tagging/index.ts +1 -1
  192. package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +7 -10
  193. package/src/tagging/recipient_sync/utils/find_highest_indexes.ts +2 -2
  194. package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +10 -15
  195. package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +4 -9
  196. package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +11 -20
  197. package/dest/contract_sync/index.d.ts +0 -23
  198. package/dest/contract_sync/index.d.ts.map +0 -1
  199. package/dest/private_kernel/hints/compute_tx_include_by_timestamp.d.ts +0 -4
  200. 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,20 +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.anchorBlockStore,
221
- this.senderTaggingStore,
222
- this.recipientTaggingStore,
223
- this.senderAddressBookStore,
224
- this.capsuleStore,
225
- this.privateEventStore,
226
- this.simulator,
227
- );
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
+ });
228
299
  }
229
300
 
230
301
  #contextualizeError(err: Error, ...context: string[]): Error {
@@ -276,9 +347,8 @@ export class PXE {
276
347
  async #registerProtocolContracts() {
277
348
  const registered: Record<string, string> = {};
278
349
  for (const name of protocolContractNames) {
279
- const { address, contractClass, instance, artifact } =
280
- await this.protocolContractsProvider.getProtocolContractArtifact(name);
281
- await this.contractStore.addContractArtifact(contractClass.id, artifact);
350
+ const { address, instance, artifact } = await this.protocolContractsProvider.getProtocolContractArtifact(name);
351
+ await this.contractStore.addContractArtifact(artifact);
282
352
  await this.contractStore.addContractInstance(instance);
283
353
  registered[name] = address.toString();
284
354
  }
@@ -290,7 +360,7 @@ export class PXE {
290
360
  async #executePrivate(
291
361
  contractFunctionSimulator: ContractFunctionSimulator,
292
362
  txRequest: TxExecutionRequest,
293
- scopes: AztecAddress[] | undefined,
363
+ scopes: AccessScopes,
294
364
  jobId: string,
295
365
  ): Promise<PrivateExecutionResult> {
296
366
  const { origin: contractAddress, functionSelector } = txRequest;
@@ -298,27 +368,23 @@ export class PXE {
298
368
  try {
299
369
  const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
300
370
 
301
- await ensureContractSynced(
371
+ await this.contractSyncService.ensureContractSynced(
302
372
  contractAddress,
303
373
  functionSelector,
304
- privateSyncCall => this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
305
- this.node,
306
- this.contractStore,
374
+ (privateSyncCall, execScopes) =>
375
+ this.#executeUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId),
307
376
  anchorBlockHeader,
377
+ jobId,
378
+ scopes,
308
379
  );
309
380
 
310
- const result = await contractFunctionSimulator.run(
311
- txRequest,
381
+ const result = await contractFunctionSimulator.run(txRequest, {
312
382
  contractAddress,
313
- functionSelector,
314
- undefined,
383
+ selector: functionSelector,
315
384
  anchorBlockHeader,
316
- // The sender for tags is set by contracts, typically by an account
317
- // contract entrypoint
318
- undefined, // senderForTags
319
385
  scopes,
320
386
  jobId,
321
- );
387
+ });
322
388
  this.log.debug(`Private simulation completed for ${contractAddress.toString()}:${functionSelector}`);
323
389
  return result;
324
390
  } catch (err) {
@@ -330,20 +396,20 @@ export class PXE {
330
396
  }
331
397
 
332
398
  /**
333
- * Simulate a utility function call on the given contract.
399
+ * Execute a utility function call on the given contract.
334
400
  * @param contractFunctionSimulator - The simulator to use for the function call.
335
401
  * @param call - The function call to execute.
336
402
  * @param authWitnesses - Authentication witnesses required for the function call.
337
403
  * @param scopes - Optional array of account addresses whose notes can be accessed in this call. Defaults to all
338
404
  * accounts if not specified.
339
405
  * @param jobId - The job ID for staged writes.
340
- * @returns The simulation result containing the outputs of the utility function.
406
+ * @returns The execution result containing the outputs of the utility function.
341
407
  */
342
- async #simulateUtility(
408
+ async #executeUtility(
343
409
  contractFunctionSimulator: ContractFunctionSimulator,
344
410
  call: FunctionCall,
345
411
  authWitnesses: AuthWitness[] | undefined,
346
- scopes: AztecAddress[] | undefined,
412
+ scopes: AccessScopes,
347
413
  jobId: string,
348
414
  ) {
349
415
  try {
@@ -416,6 +482,19 @@ export class PXE {
416
482
 
417
483
  // Public API
418
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
+ */
419
498
  public getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
420
499
  return this.contractStore.getContractInstance(address);
421
500
  }
@@ -524,8 +603,7 @@ export class PXE {
524
603
  * @param artifact - The build artifact for the contract class.
525
604
  */
526
605
  public async registerContractClass(artifact: ContractArtifact): Promise<void> {
527
- const { id: contractClassId } = await getContractClassFromArtifact(artifact);
528
- await this.contractStore.addContractArtifact(contractClassId, artifact);
606
+ const contractClassId = await this.contractStore.addContractArtifact(artifact);
529
607
  this.log.info(`Added contract class ${artifact.name} with id ${contractClassId}`);
530
608
  }
531
609
 
@@ -544,17 +622,17 @@ export class PXE {
544
622
  if (artifact) {
545
623
  // If the user provides an artifact, validate it against the expected class id and register it
546
624
  const contractClass = await getContractClassFromArtifact(artifact);
547
- const contractClassId = contractClass.id;
548
- if (!contractClassId.equals(instance.currentContractClassId)) {
625
+ if (!contractClass.id.equals(instance.currentContractClassId)) {
549
626
  throw new Error(
550
- `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})`,
551
628
  );
552
629
  }
553
630
  const computedAddress = await computeContractAddressFromInstance(instance);
554
631
  if (!computedAddress.equals(instance.address)) {
555
632
  throw new Error('Added a contract in which the address does not match the contract instance.');
556
633
  }
557
- await this.contractStore.addContractArtifact(contractClass.id, artifact);
634
+
635
+ await this.contractStore.addContractArtifact(artifact, contractClass);
558
636
 
559
637
  const publicFunctionSignatures = artifact.functions
560
638
  .filter(fn => fn.functionType === FunctionType.PUBLIC)
@@ -603,15 +681,16 @@ export class PXE {
603
681
  throw new Error('Could not update contract to a class different from the current one.');
604
682
  }
605
683
 
606
- await this.contractStore.addContractArtifact(contractClass.id, artifact);
607
-
608
684
  const publicFunctionSignatures = artifact.functions
609
685
  .filter(fn => fn.functionType === FunctionType.PUBLIC)
610
686
  .map(fn => decodeFunctionSignature(fn.name, fn.parameters));
611
687
  await this.node.registerContractFunctionSignatures(publicFunctionSignatures);
612
688
 
613
689
  currentInstance.currentContractClassId = contractClass.id;
614
- await this.contractStore.addContractInstance(currentInstance);
690
+ await Promise.all([
691
+ this.contractStore.addContractArtifact(artifact, contractClass),
692
+ this.contractStore.addContractInstance(currentInstance),
693
+ ]);
615
694
  this.log.info(`Updated contract ${artifact.name} at ${contractAddress.toString()} to class ${contractClass.id}`);
616
695
  });
617
696
  }
@@ -629,11 +708,12 @@ export class PXE {
629
708
  * (where validators prove the public portion).
630
709
  *
631
710
  * @param txRequest - An authenticated tx request ready for proving
711
+ * @param scopes - Addresses whose private state and keys are accessible during private execution.
632
712
  * @returns A result containing the proof and public inputs of the tail circuit.
633
713
  * @throws If contract code not found, or public simulation reverts.
634
714
  * Also throws if simulatePublic is true and public simulation reverts.
635
715
  */
636
- public proveTx(txRequest: TxExecutionRequest): Promise<TxProvingResult> {
716
+ public proveTx(txRequest: TxExecutionRequest, scopes: AztecAddress[]): Promise<TxProvingResult> {
637
717
  let privateExecutionResult: PrivateExecutionResult;
638
718
  // We disable proving concurrently mostly out of caution, since it accesses some of our stores. Proving is so
639
719
  // computationally demanding that it'd be rare for someone to try to do it concurrently regardless.
@@ -644,7 +724,7 @@ export class PXE {
644
724
  await this.blockStateSynchronizer.sync();
645
725
  const syncTime = syncTimer.ms();
646
726
  const contractFunctionSimulator = this.#getSimulatorForTx();
647
- privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, undefined, jobId);
727
+ privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, scopes, jobId);
648
728
 
649
729
  const {
650
730
  publicInputs,
@@ -708,17 +788,13 @@ export class PXE {
708
788
 
709
789
  /**
710
790
  * 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.
791
+ * @param txRequest - An authenticated tx request ready for simulation.
715
792
  * @returns A trace of the program execution with gate counts.
716
793
  * @throws If the code for the functions executed in this transaction have not been made available via `addContracts`.
717
794
  */
718
795
  public profileTx(
719
796
  txRequest: TxExecutionRequest,
720
- profileMode: 'full' | 'execution-steps' | 'gates',
721
- skipProofGeneration: boolean = true,
797
+ { profileMode, skipProofGeneration = true, scopes }: ProfileTxOpts,
722
798
  ): Promise<TxProfileResult> {
723
799
  // We disable concurrent profiles for consistency with simulateTx.
724
800
  return this.#putInJobQueue(async jobId => {
@@ -741,12 +817,7 @@ export class PXE {
741
817
  const syncTime = syncTimer.ms();
742
818
 
743
819
  const contractFunctionSimulator = this.#getSimulatorForTx();
744
- const privateExecutionResult = await this.#executePrivate(
745
- contractFunctionSimulator,
746
- txRequest,
747
- undefined,
748
- jobId,
749
- );
820
+ const privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, scopes, jobId);
750
821
 
751
822
  const { executionSteps, timings: { proving } = {} } = await this.#prove(
752
823
  txRequest,
@@ -803,12 +874,7 @@ export class PXE {
803
874
  * In that case, the transaction returned is only potentially ready to be sent to the network for execution.
804
875
  *
805
876
  *
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.
877
+ * @param txRequest - An authenticated tx request ready for simulation.
812
878
  * @returns A simulated transaction result object that includes public and private return values.
813
879
  * @throws If the code for the functions executed in this transaction have not been made available via `addContracts`.
814
880
  * Also throws if simulatePublic is true and public simulation reverts.
@@ -817,11 +883,7 @@ export class PXE {
817
883
  */
818
884
  public simulateTx(
819
885
  txRequest: TxExecutionRequest,
820
- simulatePublic: boolean,
821
- skipTxValidation: boolean = false,
822
- skipFeeEnforcement: boolean = false,
823
- overrides?: SimulationOverrides,
824
- scopes?: AztecAddress[],
886
+ { simulatePublic, skipTxValidation = false, skipFeeEnforcement = false, overrides, scopes }: SimulateTxOpts,
825
887
  ): Promise<TxSimulationResult> {
826
888
  // We disable concurrent simulations since those might execute oracles which read and write to the PXE stores (e.g.
827
889
  // to the capsules), and we need to prevent concurrent runs from interfering with one another (e.g. attempting to
@@ -849,7 +911,14 @@ export class PXE {
849
911
  // Temporary: in case there are overrides, we have to skip the kernels or validations
850
912
  // will fail. Consider handing control to the user/wallet on whether they want to run them
851
913
  // or not.
852
- 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
+ }
853
922
 
854
923
  // Execution of private functions only; no proving, and no kernel logic.
855
924
  const privateExecutionResult = await this.#executePrivate(contractFunctionSimulator, txRequest, scopes, jobId);
@@ -860,7 +929,8 @@ export class PXE {
860
929
  if (skipKernels) {
861
930
  ({ publicInputs, executionSteps } = await generateSimulatedProvingResult(
862
931
  privateExecutionResult,
863
- this.contractStore,
932
+ (addr, sel) => this.contractStore.getDebugFunctionName(addr, sel),
933
+ this.node,
864
934
  ));
865
935
  } else {
866
936
  // Kernel logic, plus proving of all private functions and kernels.
@@ -879,6 +949,9 @@ export class PXE {
879
949
  const publicSimulationTimer = new Timer();
880
950
  publicOutput = await this.#simulatePublicCalls(simulatedTx, skipFeeEnforcement);
881
951
  publicSimulationTime = publicSimulationTimer.ms();
952
+ if (publicOutput?.debugLogs?.length) {
953
+ await displayDebugLogs(publicOutput.debugLogs, addr => this.contractStore.getDebugContractName(addr));
954
+ }
882
955
  }
883
956
 
884
957
  let validationTime: number | undefined;
@@ -887,7 +960,8 @@ export class PXE {
887
960
  const validationResult = await this.node.isValidTx(simulatedTx, { isSimulation: true, skipFeeEnforcement });
888
961
  validationTime = validationTimer.ms();
889
962
  if (validationResult.result === 'invalid') {
890
- 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}`);
891
965
  }
892
966
  }
893
967
 
@@ -938,29 +1012,23 @@ export class PXE {
938
1012
  inspect(txRequest),
939
1013
  `simulatePublic=${simulatePublic}`,
940
1014
  `skipTxValidation=${skipTxValidation}`,
941
- `scopes=${scopes?.map(s => s.toString()).join(', ') ?? 'undefined'}`,
1015
+ `scopes=${scopes === 'ALL_SCOPES' ? scopes : scopes.map(s => s.toString()).join(', ')}`,
942
1016
  );
943
1017
  }
944
1018
  });
945
1019
  }
946
1020
 
947
1021
  /**
948
- * Simulate the execution of a contract utility function.
949
- *
1022
+ * Executes a contract utility function.
950
1023
  * @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
1024
  */
956
- public simulateUtility(
1025
+ public executeUtility(
957
1026
  call: FunctionCall,
958
- authwits?: AuthWitness[],
959
- scopes?: AztecAddress[],
960
- ): Promise<UtilitySimulationResult> {
961
- // 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.
962
1030
  // to the capsules), and we need to prevent concurrent runs from interfering with one another (e.g. attempting to
963
- // 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).
964
1032
  return this.#putInJobQueue(async jobId => {
965
1033
  try {
966
1034
  const totalTimer = new Timer();
@@ -971,16 +1039,17 @@ export class PXE {
971
1039
  const contractFunctionSimulator = this.#getSimulatorForTx();
972
1040
 
973
1041
  const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
974
- await ensureContractSynced(
1042
+ await this.contractSyncService.ensureContractSynced(
975
1043
  call.to,
976
1044
  call.selector,
977
- privateSyncCall => this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
978
- this.node,
979
- this.contractStore,
1045
+ (privateSyncCall, execScopes) =>
1046
+ this.#executeUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId),
980
1047
  anchorBlockHeader,
1048
+ jobId,
1049
+ scopes,
981
1050
  );
982
1051
 
983
- const executionResult = await this.#simulateUtility(
1052
+ const executionResult = await this.#executeUtility(
984
1053
  contractFunctionSimulator,
985
1054
  call,
986
1055
  authwits ?? [],
@@ -1007,8 +1076,8 @@ export class PXE {
1007
1076
  const stringifiedArgs = args.map(arg => arg.toString()).join(', ');
1008
1077
  throw this.#contextualizeError(
1009
1078
  err,
1010
- `simulateUtility ${to}:${name}(${stringifiedArgs})`,
1011
- `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(', ')}`,
1012
1081
  );
1013
1082
  }
1014
1083
  });
@@ -1041,14 +1110,14 @@ export class PXE {
1041
1110
 
1042
1111
  const contractFunctionSimulator = this.#getSimulatorForTx();
1043
1112
 
1044
- await ensureContractSynced(
1113
+ await this.contractSyncService.ensureContractSynced(
1045
1114
  filter.contractAddress,
1046
1115
  null,
1047
- async privateSyncCall =>
1048
- await this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
1049
- this.node,
1050
- this.contractStore,
1116
+ async (privateSyncCall, execScopes) =>
1117
+ await this.#executeUtility(contractFunctionSimulator, privateSyncCall, [], execScopes, jobId),
1051
1118
  anchorBlockHeader,
1119
+ jobId,
1120
+ filter.scopes,
1052
1121
  );
1053
1122
  });
1054
1123
 
@@ -1063,9 +1132,10 @@ export class PXE {
1063
1132
  }
1064
1133
 
1065
1134
  /**
1066
- * Stops the PXE's job queue.
1135
+ * Stops the PXE's job queue and closes the backing store.
1067
1136
  */
1068
- public stop(): Promise<void> {
1069
- return this.jobQueue.end();
1137
+ public async stop(): Promise<void> {
1138
+ await this.jobQueue.end();
1139
+ await this.db.close();
1070
1140
  }
1071
1141
  }
@@ -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
  }