@aztec/simulator 0.42.0 → 0.44.0

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 (191) hide show
  1. package/dest/acvm/oracle/oracle.d.ts +5 -2
  2. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  3. package/dest/acvm/oracle/oracle.js +25 -8
  4. package/dest/acvm/oracle/typed_oracle.d.ts +7 -4
  5. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/typed_oracle.js +14 -5
  7. package/dest/avm/avm_execution_environment.d.ts +2 -0
  8. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  9. package/dest/avm/avm_execution_environment.js +9 -4
  10. package/dest/avm/avm_gas.d.ts.map +1 -1
  11. package/dest/avm/avm_gas.js +3 -1
  12. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  13. package/dest/avm/avm_memory_types.js +2 -4
  14. package/dest/avm/avm_simulator.d.ts.map +1 -1
  15. package/dest/avm/avm_simulator.js +2 -3
  16. package/dest/avm/fixtures/index.d.ts +10 -3
  17. package/dest/avm/fixtures/index.d.ts.map +1 -1
  18. package/dest/avm/fixtures/index.js +9 -11
  19. package/dest/avm/journal/journal.d.ts +57 -66
  20. package/dest/avm/journal/journal.d.ts.map +1 -1
  21. package/dest/avm/journal/journal.js +97 -131
  22. package/dest/avm/journal/nullifiers.d.ts +21 -8
  23. package/dest/avm/journal/nullifiers.d.ts.map +1 -1
  24. package/dest/avm/journal/nullifiers.js +26 -8
  25. package/dest/avm/journal/public_storage.d.ts +4 -0
  26. package/dest/avm/journal/public_storage.d.ts.map +1 -1
  27. package/dest/avm/journal/public_storage.js +10 -1
  28. package/dest/avm/opcodes/accrued_substate.d.ts +2 -2
  29. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  30. package/dest/avm/opcodes/accrued_substate.js +39 -24
  31. package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
  32. package/dest/avm/opcodes/arithmetic.js +12 -9
  33. package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
  34. package/dest/avm/opcodes/bitwise.js +11 -8
  35. package/dest/avm/opcodes/comparators.d.ts.map +1 -1
  36. package/dest/avm/opcodes/comparators.js +7 -5
  37. package/dest/avm/opcodes/contract.d.ts.map +1 -1
  38. package/dest/avm/opcodes/contract.js +20 -24
  39. package/dest/avm/opcodes/control_flow.d.ts.map +1 -1
  40. package/dest/avm/opcodes/control_flow.js +4 -2
  41. package/dest/avm/opcodes/ec_add.d.ts +19 -0
  42. package/dest/avm/opcodes/ec_add.d.ts.map +1 -0
  43. package/dest/avm/opcodes/ec_add.js +78 -0
  44. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  45. package/dest/avm/opcodes/external_calls.js +19 -29
  46. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  47. package/dest/avm/opcodes/hashing.js +10 -2
  48. package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
  49. package/dest/avm/opcodes/instruction_impl.js +4 -2
  50. package/dest/avm/opcodes/memory.d.ts +1 -1
  51. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  52. package/dest/avm/opcodes/memory.js +14 -12
  53. package/dest/avm/opcodes/multi_scalar_mul.d.ts +16 -0
  54. package/dest/avm/opcodes/multi_scalar_mul.d.ts.map +1 -0
  55. package/dest/avm/opcodes/multi_scalar_mul.js +95 -0
  56. package/dest/avm/opcodes/storage.d.ts +1 -1
  57. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  58. package/dest/avm/opcodes/storage.js +11 -8
  59. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  60. package/dest/avm/serialization/bytecode_serialization.js +5 -1
  61. package/dest/avm/serialization/instruction_serialization.d.ts +3 -1
  62. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  63. package/dest/avm/serialization/instruction_serialization.js +4 -2
  64. package/dest/avm/test_utils.d.ts +14 -0
  65. package/dest/avm/test_utils.d.ts.map +1 -0
  66. package/dest/avm/test_utils.js +36 -0
  67. package/dest/client/client_execution_context.d.ts +17 -5
  68. package/dest/client/client_execution_context.d.ts.map +1 -1
  69. package/dest/client/client_execution_context.js +32 -18
  70. package/dest/client/execution_note_cache.d.ts.map +1 -1
  71. package/dest/client/execution_note_cache.js +1 -1
  72. package/dest/client/execution_result.d.ts +2 -1
  73. package/dest/client/execution_result.d.ts.map +1 -1
  74. package/dest/client/execution_result.js +1 -1
  75. package/dest/client/index.d.ts +2 -0
  76. package/dest/client/index.d.ts.map +1 -1
  77. package/dest/client/index.js +3 -1
  78. package/dest/client/simulator.d.ts +4 -3
  79. package/dest/client/simulator.d.ts.map +1 -1
  80. package/dest/client/simulator.js +17 -9
  81. package/dest/client/view_data_oracle.d.ts +2 -0
  82. package/dest/client/view_data_oracle.d.ts.map +1 -1
  83. package/dest/client/view_data_oracle.js +7 -1
  84. package/dest/mocks/fixtures.d.ts.map +1 -1
  85. package/dest/mocks/fixtures.js +3 -2
  86. package/dest/public/abstract_phase_manager.d.ts +11 -11
  87. package/dest/public/abstract_phase_manager.d.ts.map +1 -1
  88. package/dest/public/abstract_phase_manager.js +84 -59
  89. package/dest/public/app_logic_phase_manager.d.ts +3 -3
  90. package/dest/public/app_logic_phase_manager.d.ts.map +1 -1
  91. package/dest/public/app_logic_phase_manager.js +4 -3
  92. package/dest/public/db_interfaces.d.ts +1 -0
  93. package/dest/public/db_interfaces.d.ts.map +1 -1
  94. package/dest/public/execution.d.ts +28 -40
  95. package/dest/public/execution.d.ts.map +1 -1
  96. package/dest/public/execution.js +1 -51
  97. package/dest/public/executor.d.ts +9 -4
  98. package/dest/public/executor.d.ts.map +1 -1
  99. package/dest/public/executor.js +38 -27
  100. package/dest/public/hints_builder.d.ts +1 -1
  101. package/dest/public/index.d.ts +6 -6
  102. package/dest/public/index.d.ts.map +1 -1
  103. package/dest/public/index.js +7 -7
  104. package/dest/public/phase_manager_factory.d.ts +3 -3
  105. package/dest/public/phase_manager_factory.d.ts.map +1 -1
  106. package/dest/public/phase_manager_factory.js +5 -5
  107. package/dest/public/public_db_sources.d.ts +3 -1
  108. package/dest/public/public_db_sources.d.ts.map +1 -1
  109. package/dest/public/public_db_sources.js +54 -8
  110. package/dest/public/public_processor.d.ts +5 -2
  111. package/dest/public/public_processor.d.ts.map +1 -1
  112. package/dest/public/public_processor.js +143 -125
  113. package/dest/public/setup_phase_manager.d.ts +3 -3
  114. package/dest/public/setup_phase_manager.d.ts.map +1 -1
  115. package/dest/public/setup_phase_manager.js +3 -3
  116. package/dest/public/side_effect_trace.d.ts +86 -0
  117. package/dest/public/side_effect_trace.d.ts.map +1 -0
  118. package/dest/public/side_effect_trace.js +222 -0
  119. package/dest/public/side_effect_trace_interface.d.ts +36 -0
  120. package/dest/public/side_effect_trace_interface.d.ts.map +1 -0
  121. package/dest/public/side_effect_trace_interface.js +2 -0
  122. package/dest/public/tail_phase_manager.d.ts +3 -3
  123. package/dest/public/tail_phase_manager.d.ts.map +1 -1
  124. package/dest/public/tail_phase_manager.js +3 -3
  125. package/dest/public/teardown_phase_manager.d.ts +3 -3
  126. package/dest/public/teardown_phase_manager.d.ts.map +1 -1
  127. package/dest/public/teardown_phase_manager.js +4 -3
  128. package/dest/public/transitional_adaptors.d.ts +2 -6
  129. package/dest/public/transitional_adaptors.d.ts.map +1 -1
  130. package/dest/public/transitional_adaptors.js +1 -43
  131. package/package.json +18 -9
  132. package/src/acvm/oracle/oracle.ts +46 -9
  133. package/src/acvm/oracle/typed_oracle.ts +37 -7
  134. package/src/avm/avm_execution_environment.ts +10 -3
  135. package/src/avm/avm_gas.ts +2 -0
  136. package/src/avm/avm_memory_types.ts +1 -3
  137. package/src/avm/avm_simulator.ts +2 -3
  138. package/src/avm/fixtures/index.ts +19 -14
  139. package/src/avm/journal/journal.ts +127 -231
  140. package/src/avm/journal/nullifiers.ts +30 -13
  141. package/src/avm/journal/public_storage.ts +10 -0
  142. package/src/avm/opcodes/accrued_substate.ts +60 -23
  143. package/src/avm/opcodes/arithmetic.ts +17 -8
  144. package/src/avm/opcodes/bitwise.ts +13 -8
  145. package/src/avm/opcodes/comparators.ts +9 -4
  146. package/src/avm/opcodes/contract.ts +22 -26
  147. package/src/avm/opcodes/control_flow.ts +3 -1
  148. package/src/avm/opcodes/ec_add.ts +92 -0
  149. package/src/avm/opcodes/external_calls.ts +20 -36
  150. package/src/avm/opcodes/hashing.ts +11 -1
  151. package/src/avm/opcodes/instruction_impl.ts +4 -1
  152. package/src/avm/opcodes/memory.ts +18 -11
  153. package/src/avm/opcodes/multi_scalar_mul.ts +114 -0
  154. package/src/avm/opcodes/storage.ts +10 -10
  155. package/src/avm/serialization/bytecode_serialization.ts +4 -0
  156. package/src/avm/serialization/instruction_serialization.ts +2 -0
  157. package/src/avm/test_utils.ts +53 -0
  158. package/src/client/client_execution_context.ts +55 -21
  159. package/src/client/execution_note_cache.ts +0 -1
  160. package/src/client/execution_result.ts +2 -1
  161. package/src/client/index.ts +2 -0
  162. package/src/client/simulator.ts +26 -10
  163. package/src/client/view_data_oracle.ts +8 -0
  164. package/src/mocks/fixtures.ts +2 -1
  165. package/src/public/abstract_phase_manager.ts +99 -70
  166. package/src/public/app_logic_phase_manager.ts +3 -2
  167. package/src/public/db_interfaces.ts +2 -0
  168. package/src/public/execution.ts +35 -94
  169. package/src/public/executor.ts +56 -40
  170. package/src/public/index.ts +6 -12
  171. package/src/public/phase_manager_factory.ts +6 -6
  172. package/src/public/public_db_sources.ts +62 -7
  173. package/src/public/public_processor.ts +15 -9
  174. package/src/public/setup_phase_manager.ts +2 -2
  175. package/src/public/side_effect_trace.ts +323 -0
  176. package/src/public/side_effect_trace_interface.ts +41 -0
  177. package/src/public/tail_phase_manager.ts +2 -2
  178. package/src/public/teardown_phase_manager.ts +3 -2
  179. package/src/public/transitional_adaptors.ts +2 -60
  180. package/dest/avm/journal/trace.d.ts +0 -33
  181. package/dest/avm/journal/trace.d.ts.map +0 -1
  182. package/dest/avm/journal/trace.js +0 -151
  183. package/dest/avm/journal/trace_types.d.ts +0 -51
  184. package/dest/avm/journal/trace_types.d.ts.map +0 -1
  185. package/dest/avm/journal/trace_types.js +0 -6
  186. package/dest/public/utils.d.ts +0 -8
  187. package/dest/public/utils.d.ts.map +0 -1
  188. package/dest/public/utils.js +0 -38
  189. package/src/avm/journal/trace.ts +0 -184
  190. package/src/avm/journal/trace_types.ts +0 -88
  191. package/src/public/utils.ts +0 -39
@@ -67,37 +67,17 @@ import { type MerkleTreeOperations } from '@aztec/world-state';
67
67
 
68
68
  import { HintsBuilder } from './hints_builder.js';
69
69
  import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
70
- import { lastSideEffectCounter } from './utils.js';
71
70
 
72
- export enum PublicKernelPhase {
73
- SETUP = 'setup',
74
- APP_LOGIC = 'app-logic',
75
- TEARDOWN = 'teardown',
76
- TAIL = 'tail',
77
- }
78
-
79
- export const PhaseIsRevertible: Record<PublicKernelPhase, boolean> = {
80
- [PublicKernelPhase.SETUP]: false,
81
- [PublicKernelPhase.APP_LOGIC]: true,
82
- [PublicKernelPhase.TEARDOWN]: true,
83
- [PublicKernelPhase.TAIL]: false,
71
+ export const PhaseIsRevertible: Record<PublicKernelType, boolean> = {
72
+ [PublicKernelType.NON_PUBLIC]: false,
73
+ [PublicKernelType.SETUP]: false,
74
+ [PublicKernelType.APP_LOGIC]: true,
75
+ [PublicKernelType.TEARDOWN]: true,
76
+ [PublicKernelType.TAIL]: false,
84
77
  };
85
78
 
86
- // REFACTOR: Unify both enums and move to types or circuit-types.
87
- export function publicKernelPhaseToKernelType(phase: PublicKernelPhase): PublicKernelType {
88
- switch (phase) {
89
- case PublicKernelPhase.SETUP:
90
- return PublicKernelType.SETUP;
91
- case PublicKernelPhase.APP_LOGIC:
92
- return PublicKernelType.APP_LOGIC;
93
- case PublicKernelPhase.TEARDOWN:
94
- return PublicKernelType.TEARDOWN;
95
- case PublicKernelPhase.TAIL:
96
- return PublicKernelType.TAIL;
97
- }
98
- }
99
-
100
79
  export type PublicProvingInformation = {
80
+ functionName: string; // informational only
101
81
  calldata: Fr[];
102
82
  bytecode: Buffer;
103
83
  inputs: PublicKernelCircuitPrivateInputs;
@@ -110,6 +90,7 @@ export function makeAvmProvingRequest(
110
90
  ): AvmProvingRequest {
111
91
  return {
112
92
  type: AVM_REQUEST,
93
+ functionName: info.functionName,
113
94
  bytecode: info.bytecode,
114
95
  calldata: info.calldata,
115
96
  avmHints: info.avmHints,
@@ -159,7 +140,7 @@ export abstract class AbstractPhaseManager {
159
140
  protected publicKernel: PublicKernelCircuitSimulator,
160
141
  protected globalVariables: GlobalVariables,
161
142
  protected historicalHeader: Header,
162
- public phase: PublicKernelPhase,
143
+ public phase: PublicKernelType,
163
144
  ) {
164
145
  this.hintsBuilder = new HintsBuilder(db);
165
146
  this.log = createDebugLogger(`aztec:sequencer:${phase}`);
@@ -171,14 +152,15 @@ export abstract class AbstractPhaseManager {
171
152
  */
172
153
  abstract handle(tx: Tx, publicKernelPublicInputs: PublicKernelCircuitPublicInputs): Promise<PhaseResult>;
173
154
 
174
- public static extractEnqueuedPublicCallsByPhase(tx: Tx): Record<PublicKernelPhase, PublicCallRequest[]> {
155
+ public static extractEnqueuedPublicCallsByPhase(tx: Tx): Record<PublicKernelType, PublicCallRequest[]> {
175
156
  const data = tx.data.forPublic;
176
157
  if (!data) {
177
158
  return {
178
- [PublicKernelPhase.SETUP]: [],
179
- [PublicKernelPhase.APP_LOGIC]: [],
180
- [PublicKernelPhase.TEARDOWN]: [],
181
- [PublicKernelPhase.TAIL]: [],
159
+ [PublicKernelType.NON_PUBLIC]: [],
160
+ [PublicKernelType.SETUP]: [],
161
+ [PublicKernelType.APP_LOGIC]: [],
162
+ [PublicKernelType.TEARDOWN]: [],
163
+ [PublicKernelType.TAIL]: [],
182
164
  };
183
165
  }
184
166
  const publicCallsStack = tx.enqueuedPublicFunctionCalls.slice().reverse();
@@ -196,10 +178,11 @@ export abstract class AbstractPhaseManager {
196
178
 
197
179
  if (callRequestsStack.length === 0) {
198
180
  return {
199
- [PublicKernelPhase.SETUP]: [],
200
- [PublicKernelPhase.APP_LOGIC]: [],
201
- [PublicKernelPhase.TEARDOWN]: [],
202
- [PublicKernelPhase.TAIL]: [],
181
+ [PublicKernelType.NON_PUBLIC]: [],
182
+ [PublicKernelType.SETUP]: [],
183
+ [PublicKernelType.APP_LOGIC]: [],
184
+ [PublicKernelType.TEARDOWN]: [],
185
+ [PublicKernelType.TAIL]: [],
203
186
  };
204
187
  }
205
188
 
@@ -212,25 +195,28 @@ export abstract class AbstractPhaseManager {
212
195
 
213
196
  if (firstRevertibleCallIndex === 0) {
214
197
  return {
215
- [PublicKernelPhase.SETUP]: [],
216
- [PublicKernelPhase.APP_LOGIC]: publicCallsStack,
217
- [PublicKernelPhase.TEARDOWN]: teardownCallStack,
218
- [PublicKernelPhase.TAIL]: [],
198
+ [PublicKernelType.NON_PUBLIC]: [],
199
+ [PublicKernelType.SETUP]: [],
200
+ [PublicKernelType.APP_LOGIC]: publicCallsStack,
201
+ [PublicKernelType.TEARDOWN]: teardownCallStack,
202
+ [PublicKernelType.TAIL]: [],
219
203
  };
220
204
  } else if (firstRevertibleCallIndex === -1) {
221
205
  // there's no app logic, split the functions between setup (many) and teardown (just one function call)
222
206
  return {
223
- [PublicKernelPhase.SETUP]: publicCallsStack,
224
- [PublicKernelPhase.APP_LOGIC]: [],
225
- [PublicKernelPhase.TEARDOWN]: teardownCallStack,
226
- [PublicKernelPhase.TAIL]: [],
207
+ [PublicKernelType.NON_PUBLIC]: [],
208
+ [PublicKernelType.SETUP]: publicCallsStack,
209
+ [PublicKernelType.APP_LOGIC]: [],
210
+ [PublicKernelType.TEARDOWN]: teardownCallStack,
211
+ [PublicKernelType.TAIL]: [],
227
212
  };
228
213
  } else {
229
214
  return {
230
- [PublicKernelPhase.SETUP]: publicCallsStack.slice(0, firstRevertibleCallIndex),
231
- [PublicKernelPhase.APP_LOGIC]: publicCallsStack.slice(firstRevertibleCallIndex),
232
- [PublicKernelPhase.TEARDOWN]: teardownCallStack,
233
- [PublicKernelPhase.TAIL]: [],
215
+ [PublicKernelType.NON_PUBLIC]: [],
216
+ [PublicKernelType.SETUP]: publicCallsStack.slice(0, firstRevertibleCallIndex),
217
+ [PublicKernelType.APP_LOGIC]: publicCallsStack.slice(firstRevertibleCallIndex),
218
+ [PublicKernelType.TEARDOWN]: teardownCallStack,
219
+ [PublicKernelType.TAIL]: [],
234
220
  };
235
221
  }
236
222
  }
@@ -278,19 +264,17 @@ export abstract class AbstractPhaseManager {
278
264
  while (executionStack.length) {
279
265
  const current = executionStack.pop()!;
280
266
  const isExecutionRequest = !isPublicExecutionResult(current);
281
- const sideEffectCounter = lastSideEffectCounter(kernelPublicOutput) + 1;
282
- const availableGas = this.getAvailableGas(tx, kernelPublicOutput);
283
- const pendingNullifiers = this.getSiloedPendingNullifiers(kernelPublicOutput);
284
-
285
267
  const result = isExecutionRequest
286
268
  ? await this.publicExecutor.simulate(
287
- current,
269
+ /*executionRequest=*/ current,
288
270
  this.globalVariables,
289
- availableGas,
271
+ /*availableGas=*/ this.getAvailableGas(tx, kernelPublicOutput),
290
272
  tx.data.constants.txContext,
291
- pendingNullifiers,
273
+ /*pendingNullifiers=*/ this.getSiloedPendingNullifiers(kernelPublicOutput),
292
274
  transactionFee,
293
- sideEffectCounter,
275
+ /*startSideEffectCounter=*/ AbstractPhaseManager.getMaxSideEffectCounter(kernelPublicOutput) + 1,
276
+ // NOTE: startSideEffectCounter is not the same as the executionRequest's sideEffectCounter
277
+ // (which counts the request itself)
294
278
  )
295
279
  : current;
296
280
 
@@ -302,15 +286,17 @@ export abstract class AbstractPhaseManager {
302
286
  const functionSelector = result.execution.functionSelector.toString();
303
287
  if (result.reverted && !result.revertReason) {
304
288
  throw new Error(
305
- `Simulation of ${result.execution.contractAddress.toString()}:${functionSelector} reverted with no reason.`,
289
+ `Simulation of ${result.execution.contractAddress.toString()}:${functionSelector}(${
290
+ result.functionName
291
+ }) reverted with no reason.`,
306
292
  );
307
293
  }
308
294
 
309
295
  if (result.reverted && !PhaseIsRevertible[this.phase]) {
310
296
  this.log.debug(
311
- `Simulation error on ${result.execution.contractAddress.toString()}:${functionSelector} with reason: ${
312
- result.revertReason
313
- }`,
297
+ `Simulation error on ${result.execution.contractAddress.toString()}:${functionSelector}(${
298
+ result.functionName
299
+ }) with reason: ${result.revertReason}`,
314
300
  );
315
301
  throw result.revertReason;
316
302
  }
@@ -322,7 +308,9 @@ export abstract class AbstractPhaseManager {
322
308
 
323
309
  // Simulate the public kernel circuit.
324
310
  this.log.debug(
325
- `Running public kernel circuit for ${result.execution.contractAddress.toString()}:${functionSelector}`,
311
+ `Running public kernel circuit for ${result.execution.contractAddress.toString()}:${functionSelector}(${
312
+ result.functionName
313
+ })`,
326
314
  );
327
315
  const callData = await this.getPublicCallData(result, isExecutionRequest);
328
316
  const [privateInputs, publicInputs] = await this.runKernelCircuit(kernelPublicOutput, callData);
@@ -330,10 +318,11 @@ export abstract class AbstractPhaseManager {
330
318
 
331
319
  // Capture the inputs for later proving in the AVM and kernel.
332
320
  const publicProvingInformation: PublicProvingInformation = {
321
+ functionName: result.functionName,
333
322
  calldata: result.calldata,
334
323
  bytecode: result.bytecode!,
335
324
  inputs: privateInputs,
336
- avmHints: result.avmHints,
325
+ avmHints: result.avmCircuitHints,
337
326
  };
338
327
  provingInformationList.push(publicProvingInformation);
339
328
 
@@ -342,7 +331,9 @@ export abstract class AbstractPhaseManager {
342
331
  // but the kernel carries the reverted flag forward. But if the simulator reverts, so should the kernel.
343
332
  if (result.reverted && kernelPublicOutput.revertCode.isOK()) {
344
333
  throw new Error(
345
- `Public kernel circuit did not revert on ${result.execution.contractAddress.toString()}:${functionSelector}, but simulator did.`,
334
+ `Public kernel circuit did not revert on ${result.execution.contractAddress.toString()}:${functionSelector}(${
335
+ result.functionName
336
+ }), but simulator did.`,
346
337
  );
347
338
  }
348
339
 
@@ -350,9 +341,9 @@ export abstract class AbstractPhaseManager {
350
341
  // So safely return the revert reason and the kernel output (which has had its revertible side effects dropped)
351
342
  if (result.reverted) {
352
343
  this.log.debug(
353
- `Reverting on ${result.execution.contractAddress.toString()}:${functionSelector} with reason: ${
354
- result.revertReason
355
- }`,
344
+ `Reverting on ${result.execution.contractAddress.toString()}:${functionSelector}(${
345
+ result.functionName
346
+ }) with reason: ${result.revertReason}`,
356
347
  );
357
348
  // TODO(@spalladino): Check gasUsed is correct. The AVM should take care of setting gasLeft to zero upon a revert.
358
349
  return {
@@ -407,11 +398,11 @@ export abstract class AbstractPhaseManager {
407
398
  // We take a deep copy (clone) of these inputs to be passed to the prover
408
399
  const inputs = new PublicKernelCircuitPrivateInputs(previousKernel, callData);
409
400
  switch (this.phase) {
410
- case PublicKernelPhase.SETUP:
401
+ case PublicKernelType.SETUP:
411
402
  return [inputs.clone(), await this.publicKernel.publicKernelCircuitSetup(inputs)];
412
- case PublicKernelPhase.APP_LOGIC:
403
+ case PublicKernelType.APP_LOGIC:
413
404
  return [inputs.clone(), await this.publicKernel.publicKernelCircuitAppLogic(inputs)];
414
- case PublicKernelPhase.TEARDOWN:
405
+ case PublicKernelType.TEARDOWN:
415
406
  return [inputs.clone(), await this.publicKernel.publicKernelCircuitTeardown(inputs)];
416
407
  default:
417
408
  throw new Error(`No public kernel circuit for inputs`);
@@ -508,6 +499,44 @@ export abstract class AbstractPhaseManager {
508
499
  return await Promise.all(nested.map(n => this.getPublicCallStackItem(n)));
509
500
  }
510
501
 
502
+ /**
503
+ * Looks at the side effects of a transaction and returns the highest counter
504
+ * @param tx - A transaction
505
+ * @returns The highest side effect counter in the transaction so far
506
+ */
507
+ static getMaxSideEffectCounter(inputs: PublicKernelCircuitPublicInputs): number {
508
+ const sideEffectCounters = [
509
+ ...inputs.endNonRevertibleData.newNoteHashes,
510
+ ...inputs.endNonRevertibleData.newNullifiers,
511
+ ...inputs.endNonRevertibleData.noteEncryptedLogsHashes,
512
+ ...inputs.endNonRevertibleData.encryptedLogsHashes,
513
+ ...inputs.endNonRevertibleData.unencryptedLogsHashes,
514
+ ...inputs.endNonRevertibleData.publicCallStack,
515
+ ...inputs.endNonRevertibleData.publicDataUpdateRequests,
516
+ ...inputs.end.newNoteHashes,
517
+ ...inputs.end.newNullifiers,
518
+ ...inputs.end.noteEncryptedLogsHashes,
519
+ ...inputs.end.encryptedLogsHashes,
520
+ ...inputs.end.unencryptedLogsHashes,
521
+ ...inputs.end.publicCallStack,
522
+ ...inputs.end.publicDataUpdateRequests,
523
+ ];
524
+
525
+ let max = 0;
526
+ for (const sideEffect of sideEffectCounters) {
527
+ if ('startSideEffectCounter' in sideEffect) {
528
+ // look at both start and end counters because for enqueued public calls start > 0 while end === 0
529
+ max = Math.max(max, sideEffect.startSideEffectCounter.toNumber(), sideEffect.endSideEffectCounter.toNumber());
530
+ } else if ('counter' in sideEffect) {
531
+ max = Math.max(max, sideEffect.counter);
532
+ } else {
533
+ throw new Error('Unknown side effect type');
534
+ }
535
+ }
536
+
537
+ return max;
538
+ }
539
+
511
540
  protected getBytecodeHash(_result: PublicExecutionResult) {
512
541
  // TODO: Determine how to calculate bytecode hash. Circuits just check it isn't zero for now.
513
542
  // See https://github.com/AztecProtocol/aztec3-packages/issues/378
@@ -3,7 +3,7 @@ import { type GlobalVariables, type Header, type PublicKernelCircuitPublicInputs
3
3
  import { type PublicExecutor, type PublicStateDB } from '@aztec/simulator';
4
4
  import { type MerkleTreeOperations } from '@aztec/world-state';
5
5
 
6
- import { AbstractPhaseManager, PublicKernelPhase, makeAvmProvingRequest } from './abstract_phase_manager.js';
6
+ import { AbstractPhaseManager, makeAvmProvingRequest } from './abstract_phase_manager.js';
7
7
  import { type ContractsDataSourcePublicDB } from './public_db_sources.js';
8
8
  import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
9
9
 
@@ -19,7 +19,7 @@ export class AppLogicPhaseManager extends AbstractPhaseManager {
19
19
  historicalHeader: Header,
20
20
  protected publicContractsDB: ContractsDataSourcePublicDB,
21
21
  protected publicStateDB: PublicStateDB,
22
- phase: PublicKernelPhase = PublicKernelPhase.APP_LOGIC,
22
+ phase: PublicKernelType = PublicKernelType.APP_LOGIC,
23
23
  ) {
24
24
  super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
25
25
  }
@@ -47,6 +47,7 @@ export class AppLogicPhaseManager extends AbstractPhaseManager {
47
47
  // if so, this is removing contracts deployed in private setup
48
48
  await this.publicContractsDB.removeNewContracts(tx);
49
49
  await this.publicStateDB.rollbackToCheckpoint();
50
+ tx.filterRevertedLogs(kernelOutput);
50
51
  } else {
51
52
  tx.unencryptedLogs.addFunctionLogs(newUnencryptedLogs);
52
53
  // TODO(#6470): we should be adding contracts deployed in those logs to the publicContractsDB
@@ -67,6 +67,8 @@ export interface PublicContractsDB {
67
67
  * @returns The contract instance or undefined if not found.
68
68
  */
69
69
  getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined>;
70
+
71
+ getDebugFunctionName(contractAddress: AztecAddress, selector: FunctionSelector): Promise<string | undefined>;
70
72
  }
71
73
 
72
74
  /** Database interface for providing access to commitment tree, l1 to l2 message tree, and nullifier tree. */
@@ -9,11 +9,8 @@ import {
9
9
  type NoteHash,
10
10
  type Nullifier,
11
11
  type PublicCallRequest,
12
- PublicDataRead,
13
- PublicDataUpdateRequest,
14
12
  type ReadRequest,
15
13
  } from '@aztec/circuits.js';
16
- import { computePublicDataTreeLeafSlot, computePublicDataTreeValue } from '@aztec/circuits.js/hash';
17
14
 
18
15
  import { type Gas } from '../avm/avm_gas.js';
19
16
 
@@ -23,16 +20,37 @@ import { type Gas } from '../avm/avm_gas.js';
23
20
  export interface PublicExecutionResult {
24
21
  /** The execution that triggered this result. */
25
22
  execution: PublicExecution;
23
+
24
+ /** The side effect counter at the start of the function call. */
25
+ startSideEffectCounter: Fr;
26
+ /** The side effect counter after executing this function call */
27
+ endSideEffectCounter: Fr;
28
+ /** How much gas was available for this public execution. */
29
+ startGasLeft: Gas;
30
+ /** How much gas was left after this public execution. */
31
+ endGasLeft: Gas;
32
+ /** Transaction fee set for this tx. */
33
+ transactionFee: Fr;
34
+
35
+ /** Bytecode used for this execution. */
36
+ bytecode?: Buffer;
37
+ /** Calldata used for this execution. */
38
+ calldata: Fr[];
26
39
  /** The return values of the function. */
27
40
  returnValues: Fr[];
41
+ /** Whether the execution reverted. */
42
+ reverted: boolean;
43
+ /** The revert reason if the execution reverted. */
44
+ revertReason?: SimulationError;
45
+
46
+ /** The contract storage reads performed by the function. */
47
+ contractStorageReads: ContractStorageRead[];
48
+ /** The contract storage update requests performed by the function. */
49
+ contractStorageUpdateRequests: ContractStorageUpdateRequest[];
28
50
  /** The new note hashes to be inserted into the note hashes tree. */
29
51
  newNoteHashes: NoteHash[];
30
52
  /** The new l2 to l1 messages generated in this call. */
31
53
  newL2ToL1Messages: L2ToL1Message[];
32
- /** The side effect counter at the start of the function call. */
33
- startSideEffectCounter: Fr;
34
- /** The side effect counter after executing this function call */
35
- endSideEffectCounter: Fr;
36
54
  /** The new nullifiers to be inserted into the nullifier tree. */
37
55
  newNullifiers: Nullifier[];
38
56
  /** The note hash read requests emitted in this call. */
@@ -43,12 +61,6 @@ export interface PublicExecutionResult {
43
61
  nullifierNonExistentReadRequests: ReadRequest[];
44
62
  /** L1 to L2 message read requests emitted in this call. */
45
63
  l1ToL2MsgReadRequests: ReadRequest[];
46
- /** The contract storage reads performed by the function. */
47
- contractStorageReads: ContractStorageRead[];
48
- /** The contract storage update requests performed by the function. */
49
- contractStorageUpdateRequests: ContractStorageUpdateRequest[];
50
- /** The results of nested calls. */
51
- nestedExecutions: this[];
52
64
  /**
53
65
  * The hashed logs with side effect counter.
54
66
  * Note: required as we don't track the counter anywhere else.
@@ -64,22 +76,17 @@ export interface PublicExecutionResult {
64
76
  * Useful for maintaining correct ordering in ts.
65
77
  */
66
78
  allUnencryptedLogs: UnencryptedFunctionL2Logs;
67
- /** Whether the execution reverted. */
68
- reverted: boolean;
69
- /** The revert reason if the execution reverted. */
70
- revertReason?: SimulationError;
71
- /** How much gas was available for this public execution. */
72
- startGasLeft: Gas;
73
- /** How much gas was left after this public execution. */
74
- endGasLeft: Gas;
75
- /** Transaction fee set for this tx. */
76
- transactionFee: Fr;
77
- /** Bytecode used for this execution. */
78
- bytecode?: Buffer;
79
- /** Calldata used for this execution. */
80
- calldata: Fr[];
79
+
80
+ // TODO(dbanks12): add contract instance read requests
81
+
82
+ /** The results of nested calls. */
83
+ nestedExecutions: this[];
84
+
81
85
  /** Hints for proving AVM execution. */
82
- avmHints: AvmExecutionHints;
86
+ avmCircuitHints: AvmExecutionHints;
87
+
88
+ /** The name of the function that was executed. Only used for logging. */
89
+ functionName: string;
83
90
  }
84
91
 
85
92
  /**
@@ -98,72 +105,6 @@ export function isPublicExecutionResult(
98
105
  return 'execution' in input && input.execution !== undefined;
99
106
  }
100
107
 
101
- /**
102
- * Collect all public storage reads across all nested executions
103
- * and convert them to PublicDataReads (to match kernel output).
104
- * @param execResult - The topmost execution result.
105
- * @returns All public data reads (in execution order).
106
- */
107
- export function collectPublicDataReads(execResult: PublicExecutionResult): PublicDataRead[] {
108
- // HACK(#1622): part of temporary hack - may be able to remove this function after public state ordering is fixed
109
- const thisExecPublicDataReads = execResult.contractStorageReads.map(read =>
110
- contractStorageReadToPublicDataRead(read),
111
- );
112
- const unsorted = [
113
- ...thisExecPublicDataReads,
114
- ...[...execResult.nestedExecutions].flatMap(result => collectPublicDataReads(result)),
115
- ];
116
- return unsorted.sort((a, b) => a.sideEffectCounter! - b.sideEffectCounter!);
117
- }
118
-
119
- /**
120
- * Collect all public storage update requests across all nested executions
121
- * and convert them to PublicDataUpdateRequests (to match kernel output).
122
- * @param execResult - The topmost execution result.
123
- * @returns All public data reads (in execution order).
124
- */
125
- export function collectPublicDataUpdateRequests(execResult: PublicExecutionResult): PublicDataUpdateRequest[] {
126
- // HACK(#1622): part of temporary hack - may be able to remove this function after public state ordering is fixed
127
- const thisExecPublicDataUpdateRequests = execResult.contractStorageUpdateRequests.map(update =>
128
- contractStorageUpdateRequestToPublicDataUpdateRequest(update),
129
- );
130
- const unsorted = [
131
- ...thisExecPublicDataUpdateRequests,
132
- ...[...execResult.nestedExecutions].flatMap(result => collectPublicDataUpdateRequests(result)),
133
- ];
134
- return unsorted.sort((a, b) => a.sideEffectCounter! - b.sideEffectCounter!);
135
- }
136
-
137
- /**
138
- * Convert a Contract Storage Read to a Public Data Read.
139
- * @param read - the contract storage read to convert
140
- * @param contractAddress - the contract address of the read
141
- * @returns The public data read.
142
- */
143
- function contractStorageReadToPublicDataRead(read: ContractStorageRead): PublicDataRead {
144
- return new PublicDataRead(
145
- computePublicDataTreeLeafSlot(read.contractAddress!, read.storageSlot),
146
- computePublicDataTreeValue(read.currentValue),
147
- read.sideEffectCounter!,
148
- );
149
- }
150
-
151
- /**
152
- * Convert a Contract Storage Update Request to a Public Data Update Request.
153
- * @param update - the contract storage update request to convert
154
- * @param contractAddress - the contract address of the data update request.
155
- * @returns The public data update request.
156
- */
157
- function contractStorageUpdateRequestToPublicDataUpdateRequest(
158
- update: ContractStorageUpdateRequest,
159
- ): PublicDataUpdateRequest {
160
- return new PublicDataUpdateRequest(
161
- computePublicDataTreeLeafSlot(update.contractAddress!, update.storageSlot),
162
- computePublicDataTreeValue(update.newValue),
163
- update.sideEffectCounter!,
164
- );
165
- }
166
-
167
108
  /**
168
109
  * Checks whether the child execution result is valid for a static call (no state modifications).
169
110
  * @param executionResult - The execution result of a public function
@@ -1,5 +1,7 @@
1
- import { Fr, type Gas, type GlobalVariables, type Header, type Nullifier, type TxContext } from '@aztec/circuits.js';
1
+ import { type AvmSimulationStats } from '@aztec/circuit-types/stats';
2
+ import { Fr, Gas, type GlobalVariables, type Header, type Nullifier, type TxContext } from '@aztec/circuits.js';
2
3
  import { createDebugLogger } from '@aztec/foundation/log';
4
+ import { Timer } from '@aztec/foundation/timer';
3
5
 
4
6
  import { AvmContext } from '../avm/avm_context.js';
5
7
  import { AvmMachineState } from '../avm/avm_machine_state.js';
@@ -8,7 +10,8 @@ import { HostStorage } from '../avm/journal/host_storage.js';
8
10
  import { AvmPersistableStateManager } from '../avm/journal/index.js';
9
11
  import { type CommitmentsDB, type PublicContractsDB, type PublicStateDB } from './db_interfaces.js';
10
12
  import { type PublicExecution, type PublicExecutionResult, checkValidStaticCall } from './execution.js';
11
- import { convertAvmResultsToPxResult, createAvmExecutionEnvironment } from './transitional_adaptors.js';
13
+ import { PublicSideEffectTrace } from './side_effect_trace.js';
14
+ import { createAvmExecutionEnvironment } from './transitional_adaptors.js';
12
15
 
13
16
  /**
14
17
  * Handles execution of public functions.
@@ -25,82 +28,95 @@ export class PublicExecutor {
25
28
 
26
29
  /**
27
30
  * Executes a public execution request.
28
- * @param execution - The execution to run.
31
+ * @param executionRequest - The execution to run.
29
32
  * @param globalVariables - The global variables to use.
30
- * @returns The result of the run plus all nested runs.
33
+ * @param availableGas - The gas available at the start of this enqueued call.
34
+ * @param txContext - Transaction context.
35
+ * @param pendingSiloedNullifiers - The pending nullifier set from earlier parts of this TX.
36
+ * @param transactionFee - Fee offered for this TX.
37
+ * @param startSideEffectCounter - The counter of the first side-effect generated by this simulation.
38
+ * @returns The result of execution, including the results of all nested calls.
31
39
  */
32
40
  public async simulate(
33
- execution: PublicExecution,
41
+ executionRequest: PublicExecution,
34
42
  globalVariables: GlobalVariables,
35
43
  availableGas: Gas,
36
44
  txContext: TxContext,
37
- pendingNullifiers: Nullifier[],
45
+ pendingSiloedNullifiers: Nullifier[],
38
46
  transactionFee: Fr = Fr.ZERO,
39
- sideEffectCounter: number = 0,
47
+ startSideEffectCounter: number = 0,
40
48
  ): Promise<PublicExecutionResult> {
41
- const address = execution.contractAddress;
42
- const selector = execution.functionSelector;
43
- const startGas = availableGas;
49
+ const address = executionRequest.contractAddress;
50
+ const selector = executionRequest.functionSelector;
51
+ const fnName = (await this.contractsDb.getDebugFunctionName(address, selector)) ?? `${address}:${selector}`;
44
52
 
45
- PublicExecutor.log.verbose(`[AVM] Executing public external function ${address.toString()}:${selector}.`);
53
+ PublicExecutor.log.verbose(`[AVM] Executing public external function ${fnName}.`);
54
+ const timer = new Timer();
46
55
 
47
- // Temporary code to construct the AVM context
48
- // These data structures will permeate across the simulator when the public executor is phased out
49
56
  const hostStorage = new HostStorage(this.stateDb, this.contractsDb, this.commitmentsDb);
57
+ const trace = new PublicSideEffectTrace(startSideEffectCounter);
58
+ const avmPersistableState = AvmPersistableStateManager.newWithPendingSiloedNullifiers(
59
+ hostStorage,
60
+ trace,
61
+ pendingSiloedNullifiers.map(n => n.value),
62
+ );
50
63
 
51
- const startSideEffectCounter = sideEffectCounter;
52
- const worldStateJournal = new AvmPersistableStateManager(hostStorage);
53
- for (const nullifier of pendingNullifiers) {
54
- worldStateJournal.nullifiers.cache.appendSiloed(nullifier.value);
55
- }
56
- // All the subsequent side effects will have a counter larger than the call's start counter.
57
- worldStateJournal.trace.accessCounter = startSideEffectCounter + 1;
58
-
59
- const executionEnv = createAvmExecutionEnvironment(
60
- execution,
64
+ const avmExecutionEnv = createAvmExecutionEnvironment(
65
+ executionRequest,
61
66
  this.header,
62
67
  globalVariables,
63
68
  txContext.gasSettings,
64
69
  transactionFee,
65
70
  );
66
71
 
67
- const machineState = new AvmMachineState(startGas);
68
- const avmContext = new AvmContext(worldStateJournal, executionEnv, machineState);
72
+ const avmMachineState = new AvmMachineState(availableGas);
73
+ const avmContext = new AvmContext(avmPersistableState, avmExecutionEnv, avmMachineState);
69
74
  const simulator = new AvmSimulator(avmContext);
70
75
  const avmResult = await simulator.execute();
76
+ const bytecode = simulator.getBytecode()!;
71
77
 
72
78
  // Commit the journals state to the DBs since this is a top-level execution.
73
79
  // Observe that this will write all the state changes to the DBs, not only the latest for each slot.
74
80
  // However, the underlying DB keep a cache and will only write the latest state to disk.
81
+ // TODO(dbanks12): this should be unnecessary here or should be exposed by state manager
75
82
  await avmContext.persistableState.publicStorage.commitToDB();
76
83
 
77
84
  PublicExecutor.log.verbose(
78
- `[AVM] ${address.toString()}:${selector} returned, reverted: ${avmResult.reverted}, reason: ${
79
- avmResult.revertReason
85
+ `[AVM] ${fnName} returned, reverted: ${avmResult.reverted}${
86
+ avmResult.reverted ? ', reason: ' + avmResult.revertReason : ''
80
87
  }.`,
88
+ {
89
+ eventName: 'avm-simulation',
90
+ appCircuitName: fnName,
91
+ duration: timer.ms(),
92
+ bytecodeSize: bytecode!.length,
93
+ } satisfies AvmSimulationStats,
81
94
  );
82
95
 
83
- const executionResult = convertAvmResultsToPxResult(
96
+ const publicExecutionResult = trace.toPublicExecutionResult(
97
+ avmExecutionEnv,
98
+ /*startGasLeft=*/ availableGas,
99
+ /*endGasLeft=*/ Gas.from(avmContext.machineState.gasLeft),
100
+ bytecode,
84
101
  avmResult,
85
- startSideEffectCounter,
86
- execution,
87
- startGas,
88
- avmContext,
89
- simulator.getBytecode(),
102
+ fnName,
103
+ /*requestSideEffectCounter=*/ executionRequest.callContext.sideEffectCounter,
104
+ // NOTE: startSideEffectCounter is not the same as the executionRequest's sideEffectCounter
105
+ // (which counts the request itself)
90
106
  );
91
107
 
92
108
  // TODO(https://github.com/AztecProtocol/aztec-packages/issues/5818): is this really needed?
93
109
  // should already be handled in simulation.
94
- if (execution.callContext.isStaticCall) {
110
+ if (executionRequest.callContext.isStaticCall) {
95
111
  checkValidStaticCall(
96
- executionResult.newNoteHashes,
97
- executionResult.newNullifiers,
98
- executionResult.contractStorageUpdateRequests,
99
- executionResult.newL2ToL1Messages,
100
- executionResult.unencryptedLogs,
112
+ publicExecutionResult.newNoteHashes,
113
+ publicExecutionResult.newNullifiers,
114
+ publicExecutionResult.contractStorageUpdateRequests,
115
+ publicExecutionResult.newL2ToL1Messages,
116
+ publicExecutionResult.unencryptedLogs,
101
117
  );
102
118
  }
103
119
 
104
- return executionResult;
120
+ return publicExecutionResult;
105
121
  }
106
122
  }