@aztec/simulator 0.86.0 → 0.87.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 (188) hide show
  1. package/dest/common/errors.d.ts +3 -6
  2. package/dest/common/errors.d.ts.map +1 -1
  3. package/dest/common/errors.js +50 -31
  4. package/dest/private/acvm/acvm.d.ts +0 -2
  5. package/dest/private/acvm/acvm.d.ts.map +1 -1
  6. package/dest/private/acvm/acvm.js +2 -2
  7. package/dest/private/acvm/deserialize.d.ts +0 -2
  8. package/dest/private/acvm/deserialize.d.ts.map +1 -1
  9. package/dest/private/acvm/oracle/oracle.d.ts +2 -2
  10. package/dest/private/acvm/oracle/oracle.d.ts.map +1 -1
  11. package/dest/private/acvm/oracle/oracle.js +30 -33
  12. package/dest/private/acvm/oracle/typed_oracle.d.ts +1 -3
  13. package/dest/private/acvm/oracle/typed_oracle.d.ts.map +1 -1
  14. package/dest/private/acvm/serialize.d.ts +8 -2
  15. package/dest/private/acvm/serialize.d.ts.map +1 -1
  16. package/dest/private/acvm/serialize.js +26 -0
  17. package/dest/private/index.d.ts +3 -0
  18. package/dest/private/index.d.ts.map +1 -1
  19. package/dest/private/index.js +3 -0
  20. package/dest/private/private_execution.d.ts.map +1 -1
  21. package/dest/private/private_execution.js +7 -1
  22. package/dest/private/providers/acvm_native.d.ts +0 -2
  23. package/dest/private/providers/acvm_native.d.ts.map +1 -1
  24. package/dest/private/providers/acvm_native.js +1 -1
  25. package/dest/private/providers/circuit_recording/circuit_recorder.d.ts +0 -2
  26. package/dest/private/providers/circuit_recording/circuit_recorder.d.ts.map +1 -1
  27. package/dest/private/providers/simulation_provider.d.ts.map +1 -1
  28. package/dest/private/providers/simulation_provider.js +14 -16
  29. package/dest/private/simulator.d.ts.map +1 -1
  30. package/dest/private/utility_execution_oracle.d.ts +0 -2
  31. package/dest/private/utility_execution_oracle.d.ts.map +1 -1
  32. package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -1
  33. package/dest/public/avm/avm_contract_call_result.js +2 -2
  34. package/dest/public/avm/avm_execution_environment.d.ts +2 -1
  35. package/dest/public/avm/avm_execution_environment.d.ts.map +1 -1
  36. package/dest/public/avm/avm_execution_environment.js +4 -2
  37. package/dest/public/avm/avm_gas.d.ts +0 -15
  38. package/dest/public/avm/avm_gas.d.ts.map +1 -1
  39. package/dest/public/avm/avm_gas.js +0 -18
  40. package/dest/public/avm/avm_memory_types.d.ts +84 -86
  41. package/dest/public/avm/avm_memory_types.d.ts.map +1 -1
  42. package/dest/public/avm/avm_simulator.d.ts +1 -3
  43. package/dest/public/avm/avm_simulator.d.ts.map +1 -1
  44. package/dest/public/avm/avm_simulator.js +3 -3
  45. package/dest/public/avm/avm_simulator_interface.d.ts +0 -2
  46. package/dest/public/avm/avm_simulator_interface.d.ts.map +1 -1
  47. package/dest/public/avm/fixtures/initializers.d.ts.map +1 -1
  48. package/dest/public/avm/fixtures/initializers.js +1 -1
  49. package/dest/public/avm/fixtures/utils.d.ts +0 -10
  50. package/dest/public/avm/fixtures/utils.d.ts.map +1 -1
  51. package/dest/public/avm/fixtures/utils.js +0 -42
  52. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
  53. package/dest/public/avm/opcodes/accrued_substate.js +7 -7
  54. package/dest/public/avm/opcodes/addressing_mode.d.ts +7 -6
  55. package/dest/public/avm/opcodes/addressing_mode.d.ts.map +1 -1
  56. package/dest/public/avm/opcodes/addressing_mode.js +18 -15
  57. package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -1
  58. package/dest/public/avm/opcodes/arithmetic.js +1 -1
  59. package/dest/public/avm/opcodes/bitwise.d.ts.map +1 -1
  60. package/dest/public/avm/opcodes/bitwise.js +2 -2
  61. package/dest/public/avm/opcodes/comparators.d.ts.map +1 -1
  62. package/dest/public/avm/opcodes/comparators.js +1 -1
  63. package/dest/public/avm/opcodes/contract.d.ts +1 -2
  64. package/dest/public/avm/opcodes/contract.d.ts.map +1 -1
  65. package/dest/public/avm/opcodes/contract.js +8 -9
  66. package/dest/public/avm/opcodes/control_flow.d.ts.map +1 -1
  67. package/dest/public/avm/opcodes/control_flow.js +1 -1
  68. package/dest/public/avm/opcodes/conversion.d.ts.map +1 -1
  69. package/dest/public/avm/opcodes/conversion.js +1 -1
  70. package/dest/public/avm/opcodes/ec_add.d.ts.map +1 -1
  71. package/dest/public/avm/opcodes/ec_add.js +1 -1
  72. package/dest/public/avm/opcodes/environment_getters.d.ts.map +1 -1
  73. package/dest/public/avm/opcodes/environment_getters.js +1 -1
  74. package/dest/public/avm/opcodes/external_calls.d.ts +6 -6
  75. package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -1
  76. package/dest/public/avm/opcodes/external_calls.js +22 -22
  77. package/dest/public/avm/opcodes/hashing.d.ts.map +1 -1
  78. package/dest/public/avm/opcodes/hashing.js +3 -3
  79. package/dest/public/avm/opcodes/instruction.d.ts +0 -2
  80. package/dest/public/avm/opcodes/instruction.d.ts.map +1 -1
  81. package/dest/public/avm/opcodes/memory.d.ts +4 -4
  82. package/dest/public/avm/opcodes/memory.d.ts.map +1 -1
  83. package/dest/public/avm/opcodes/memory.js +16 -16
  84. package/dest/public/avm/opcodes/misc.d.ts.map +1 -1
  85. package/dest/public/avm/opcodes/misc.js +18 -11
  86. package/dest/public/avm/opcodes/storage.d.ts.map +1 -1
  87. package/dest/public/avm/opcodes/storage.js +2 -2
  88. package/dest/public/avm/serialization/buffer_cursor.d.ts +0 -2
  89. package/dest/public/avm/serialization/buffer_cursor.d.ts.map +1 -1
  90. package/dest/public/avm/serialization/bytecode_serialization.d.ts +0 -2
  91. package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  92. package/dest/public/avm/serialization/instruction_serialization.d.ts +0 -2
  93. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -1
  94. package/dest/public/executor_metrics.d.ts +3 -3
  95. package/dest/public/executor_metrics.d.ts.map +1 -1
  96. package/dest/public/executor_metrics.js +6 -6
  97. package/dest/public/executor_metrics_interface.d.ts +1 -1
  98. package/dest/public/executor_metrics_interface.d.ts.map +1 -1
  99. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  100. package/dest/public/fixtures/public_tx_simulation_tester.js +3 -3
  101. package/dest/public/fixtures/utils.d.ts.map +1 -1
  102. package/dest/public/fixtures/utils.js +19 -26
  103. package/dest/public/hinting_db_sources.d.ts +0 -2
  104. package/dest/public/hinting_db_sources.d.ts.map +1 -1
  105. package/dest/public/hinting_db_sources.js +9 -2
  106. package/dest/public/public_processor/public_processor.d.ts +5 -7
  107. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  108. package/dest/public/public_processor/public_processor.js +10 -7
  109. package/dest/public/public_tx_simulator/apps_tests/amm_test.js +5 -5
  110. package/dest/public/public_tx_simulator/apps_tests/token_test.js +6 -6
  111. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +1 -1
  112. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts.map +1 -1
  113. package/dest/public/public_tx_simulator/measured_public_tx_simulator.js +3 -3
  114. package/dest/public/public_tx_simulator/public_tx_context.d.ts +1 -1
  115. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  116. package/dest/public/public_tx_simulator/public_tx_context.js +37 -34
  117. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +2 -1
  118. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  119. package/dest/public/public_tx_simulator/public_tx_simulator.js +15 -4
  120. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts +1 -1
  121. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts.map +1 -1
  122. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.js +2 -2
  123. package/dest/public/side_effect_trace.d.ts +2 -39
  124. package/dest/public/side_effect_trace.d.ts.map +1 -1
  125. package/dest/public/side_effect_trace.js +8 -32
  126. package/dest/public/side_effect_trace_interface.d.ts +0 -4
  127. package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
  128. package/dest/public/state_manager/nullifiers.d.ts.map +1 -1
  129. package/dest/public/state_manager/public_storage.d.ts.map +1 -1
  130. package/dest/public/state_manager/state_manager.d.ts +6 -4
  131. package/dest/public/state_manager/state_manager.d.ts.map +1 -1
  132. package/dest/public/state_manager/state_manager.js +6 -3
  133. package/dest/public/test_executor_metrics.d.ts +5 -4
  134. package/dest/public/test_executor_metrics.d.ts.map +1 -1
  135. package/dest/public/test_executor_metrics.js +47 -11
  136. package/dest/public/unique_class_ids.d.ts.map +1 -1
  137. package/dest/test/utils.d.ts.map +1 -1
  138. package/package.json +18 -17
  139. package/src/common/errors.ts +77 -41
  140. package/src/private/acvm/acvm.ts +2 -2
  141. package/src/private/acvm/oracle/oracle.ts +38 -31
  142. package/src/private/acvm/oracle/typed_oracle.ts +1 -1
  143. package/src/private/acvm/serialize.ts +35 -0
  144. package/src/private/index.ts +3 -0
  145. package/src/private/pick_notes.ts +4 -4
  146. package/src/private/private_execution.ts +8 -0
  147. package/src/private/providers/acvm_native.ts +6 -2
  148. package/src/private/providers/simulation_provider.ts +14 -16
  149. package/src/private/simulator.ts +4 -1
  150. package/src/public/avm/avm_contract_call_result.ts +2 -2
  151. package/src/public/avm/avm_execution_environment.ts +7 -1
  152. package/src/public/avm/avm_gas.ts +0 -20
  153. package/src/public/avm/avm_simulator.ts +3 -1
  154. package/src/public/avm/fixtures/initializers.ts +1 -0
  155. package/src/public/avm/fixtures/utils.ts +0 -66
  156. package/src/public/avm/opcodes/accrued_substate.ts +31 -11
  157. package/src/public/avm/opcodes/addressing_mode.ts +23 -20
  158. package/src/public/avm/opcodes/arithmetic.ts +2 -1
  159. package/src/public/avm/opcodes/bitwise.ts +9 -3
  160. package/src/public/avm/opcodes/comparators.ts +2 -1
  161. package/src/public/avm/opcodes/contract.ts +7 -6
  162. package/src/public/avm/opcodes/control_flow.ts +7 -2
  163. package/src/public/avm/opcodes/conversion.ts +2 -1
  164. package/src/public/avm/opcodes/ec_add.ts +2 -1
  165. package/src/public/avm/opcodes/environment_getters.ts +7 -2
  166. package/src/public/avm/opcodes/external_calls.ts +26 -14
  167. package/src/public/avm/opcodes/hashing.ts +17 -5
  168. package/src/public/avm/opcodes/memory.ts +33 -15
  169. package/src/public/avm/opcodes/misc.ts +23 -15
  170. package/src/public/avm/opcodes/storage.ts +9 -3
  171. package/src/public/avm/serialization/buffer_cursor.ts +4 -1
  172. package/src/public/executor_metrics.ts +11 -6
  173. package/src/public/executor_metrics_interface.ts +1 -1
  174. package/src/public/fixtures/public_tx_simulation_tester.ts +3 -2
  175. package/src/public/fixtures/utils.ts +21 -28
  176. package/src/public/hinting_db_sources.ts +17 -4
  177. package/src/public/public_processor/public_processor.ts +11 -5
  178. package/src/public/public_tx_simulator/apps_tests/amm_test.ts +5 -5
  179. package/src/public/public_tx_simulator/apps_tests/token_test.ts +6 -6
  180. package/src/public/public_tx_simulator/measured_public_tx_simulator.ts +3 -2
  181. package/src/public/public_tx_simulator/public_tx_context.ts +96 -81
  182. package/src/public/public_tx_simulator/public_tx_simulator.ts +14 -3
  183. package/src/public/public_tx_simulator/telemetry_public_tx_simulator.ts +10 -1
  184. package/src/public/side_effect_trace.ts +7 -111
  185. package/src/public/side_effect_trace_interface.ts +0 -4
  186. package/src/public/state_manager/state_manager.ts +13 -5
  187. package/src/public/test_executor_metrics.ts +60 -13
  188. package/src/public/avm/opcodes/.eslintrc.cjs +0 -8
@@ -22,9 +22,10 @@ export class MeasuredPublicTxSimulator extends PublicTxSimulator {
22
22
  globalVariables: GlobalVariables,
23
23
  doMerkleOperations: boolean = false,
24
24
  skipFeeEnforcement: boolean = false,
25
+ clientInitiatedSimulation: boolean = false,
25
26
  protected readonly metrics: ExecutorMetricsInterface,
26
27
  ) {
27
- super(merkleTree, contractsDB, globalVariables, doMerkleOperations, skipFeeEnforcement);
28
+ super(merkleTree, contractsDB, globalVariables, doMerkleOperations, skipFeeEnforcement, clientInitiatedSimulation);
28
29
  }
29
30
 
30
31
  public override async simulate(tx: Tx, txLabel: string = 'unlabeledTx'): Promise<PublicTxResult> {
@@ -83,7 +84,7 @@ export class MeasuredPublicTxSimulator extends PublicTxSimulator {
83
84
 
84
85
  this.log.verbose(
85
86
  result.reverted
86
- ? `Simulation of enqueued public call ${fnName} reverted with reason ${result.revertReason}.`
87
+ ? `Simulation of enqueued public call ${fnName} reverted with reason ${result.revertReason?.message}.`
87
88
  : `Simulation of enqueued public call ${fnName} completed successfully.`,
88
89
  {
89
90
  eventName: 'avm-simulation',
@@ -1,15 +1,16 @@
1
1
  import {
2
+ MAX_ENQUEUED_CALLS_PER_TX,
2
3
  MAX_L2_GAS_PER_TX_PUBLIC_PORTION,
3
4
  MAX_L2_TO_L1_MSGS_PER_TX,
4
5
  MAX_NOTE_HASHES_PER_TX,
5
6
  MAX_NULLIFIERS_PER_TX,
7
+ MAX_PUBLIC_LOGS_PER_TX,
6
8
  MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
7
9
  } from '@aztec/constants';
8
10
  import { padArrayEnd } from '@aztec/foundation/collection';
9
11
  import { Fr } from '@aztec/foundation/fields';
10
12
  import { type Logger, createLogger } from '@aztec/foundation/log';
11
- import { assertLength } from '@aztec/foundation/serialize';
12
- import { type AvmCircuitPublicInputs, PublicDataWrite, RevertCode } from '@aztec/stdlib/avm';
13
+ import { AvmAccumulatedData, AvmCircuitPublicInputs, PublicDataWrite, RevertCode } from '@aztec/stdlib/avm';
13
14
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
14
15
  import type { SimulationError } from '@aztec/stdlib/errors';
15
16
  import { computeTransactionFee } from '@aztec/stdlib/fees';
@@ -20,8 +21,9 @@ import {
20
21
  type PrivateToPublicAccumulatedData,
21
22
  PublicCallRequest,
22
23
  countAccumulatedItems,
23
- mergeAccumulatedData,
24
24
  } from '@aztec/stdlib/kernel';
25
+ import { PublicLog } from '@aztec/stdlib/logs';
26
+ import { ScopedL2ToL1Message } from '@aztec/stdlib/messaging';
25
27
  import { MerkleTreeId } from '@aztec/stdlib/trees';
26
28
  import {
27
29
  type GlobalVariables,
@@ -37,7 +39,7 @@ import { inspect } from 'util';
37
39
 
38
40
  import type { PublicContractsDBInterface } from '../db_interfaces.js';
39
41
  import type { PublicTreesDB } from '../public_db_sources.js';
40
- import { SideEffectArrayLengths, SideEffectTrace } from '../side_effect_trace.js';
42
+ import { SideEffectTrace } from '../side_effect_trace.js';
41
43
  import { PublicPersistableStateManager } from '../state_manager/state_manager.js';
42
44
  import { getCallRequestsWithCalldataByPhase } from '../utils.js';
43
45
 
@@ -86,16 +88,7 @@ export class PublicTxContext {
86
88
  ) {
87
89
  const nonRevertibleAccumulatedDataFromPrivate = tx.data.forPublic!.nonRevertibleAccumulatedData;
88
90
 
89
- const previousAccumulatedDataArrayLengths = new SideEffectArrayLengths(
90
- /*publicDataWrites*/ 0,
91
- /*protocolPublicDataWrites*/ 0,
92
- /*noteHashes*/ 0,
93
- /*nullifiers=*/ 0,
94
- countAccumulatedItems(nonRevertibleAccumulatedDataFromPrivate.l2ToL1Msgs),
95
- /*publicLogs*/ 0,
96
- );
97
-
98
- const trace = new SideEffectTrace(/*startSideEffectCounter=*/ 0, previousAccumulatedDataArrayLengths);
91
+ const trace = new SideEffectTrace();
99
92
 
100
93
  const firstNullifier = nonRevertibleAccumulatedDataFromPrivate.nullifiers[0];
101
94
 
@@ -150,7 +143,7 @@ export class PublicTxContext {
150
143
  * NOTE: this does not "halt" the entire transaction execution.
151
144
  */
152
145
  revert(phase: TxExecutionPhase, revertReason: SimulationError | undefined = undefined, culprit = '') {
153
- this.log.warn(`${TxExecutionPhase[phase]} phase reverted! ${culprit} failed with reason: ${revertReason}`);
146
+ this.log.warn(`${TxExecutionPhase[phase]} phase reverted! ${culprit} failed with reason: ${revertReason?.message}`);
154
147
 
155
148
  if (revertReason && !this.revertReason) {
156
149
  // don't override revertReason
@@ -301,82 +294,104 @@ export class PublicTxContext {
301
294
  assert(this.halted, 'Can only get AvmCircuitPublicInputs after tx execution ends');
302
295
  const stateManager = this.state.getActiveStateManager();
303
296
 
304
- // FIXME: We are first creating the PIs with the wrong endTreeSnapshots, then patching them.
305
- // This is because we need to know the lengths of the accumulated data arrays to pad them.
306
- // We should refactor this to avoid this hack.
307
- // We should just get the info we need from the trace, and create the rest of the PIs here.
308
- const avmCircuitPublicInputs = this.trace.toAvmCircuitPublicInputs(
309
- this.globalVariables,
310
- /*startGasUsed=*/ this.gasUsedByPrivate,
311
- this.gasSettings,
312
- this.feePayer,
313
- this.setupCallRequests.map(r => r.request),
314
- this.appLogicCallRequests.map(r => r.request),
315
- /*teardownCallRequest=*/ this.teardownCallRequests.length
316
- ? this.teardownCallRequests[0].request
317
- : PublicCallRequest.empty(),
318
- /*endTreeSnapshots=*/ TreeSnapshots.empty(), // Will be patched/padded at the end of this fn
319
- /*endGasUsed=*/ this.getTotalGasUsed(),
320
- /*transactionFee=*/ this.getTransactionFeeUnsafe(),
321
- /*reverted=*/ !this.revertCode.isOK(),
297
+ // We get the side effects from the AVM.
298
+ // The AVM will already have handled the conditional insertion of most private side-effects.
299
+ // (depending on the revert code of each stage). Only l2ToL1Msgs and publicLogs still need to be "merged".
300
+ const {
301
+ publicDataWrites: avmPublicDataWrites,
302
+ noteHashes: avmNoteHashes,
303
+ nullifiers: avmNullifiers,
304
+ l2ToL1Msgs: avmL2ToL1Msgs,
305
+ publicLogs: avmPublicLogs,
306
+ } = this.trace.getSideEffects();
307
+
308
+ // Private generates PrivateLogs, and public execution generates PublicLogs.
309
+ // Since these are two different categories, they should not be merged.
310
+ const finalPublicLogs = avmPublicLogs;
311
+
312
+ // We squash public data writes.
313
+ // Maps slot to value. Maps in TS are iterable in insertion order, which is exactly what we want for
314
+ // squashing "to the left", where the first occurrence of a slot uses the value of the last write to it,
315
+ // and the rest occurrences are omitted.
316
+ // Note: you can't write public state from private, so we only squash what we got from the AVM.
317
+ const finalPublicDataWrites = (() => {
318
+ const squashedPublicDataWrites: Map<bigint, Fr> = new Map();
319
+ for (const publicDataWrite of avmPublicDataWrites) {
320
+ squashedPublicDataWrites.set(publicDataWrite.leafSlot.toBigInt(), publicDataWrite.newValue);
321
+ }
322
+ return Array.from(squashedPublicDataWrites.entries()).map(
323
+ ([slot, value]) => new PublicDataWrite(new Fr(slot), value),
324
+ );
325
+ })();
326
+
327
+ const accumulatedData = new AvmAccumulatedData(
328
+ /*noteHashes=*/ padArrayEnd(
329
+ avmNoteHashes.map(n => n.value),
330
+ Fr.zero(),
331
+ MAX_NOTE_HASHES_PER_TX,
332
+ ),
333
+ /*nullifiers=*/ padArrayEnd(
334
+ avmNullifiers.map(n => n.value),
335
+ Fr.zero(),
336
+ MAX_NULLIFIERS_PER_TX,
337
+ ),
338
+ /*l2ToL1Msgs=*/ padArrayEnd(avmL2ToL1Msgs, ScopedL2ToL1Message.empty(), MAX_L2_TO_L1_MSGS_PER_TX),
339
+ /*publicLogs=*/ padArrayEnd(finalPublicLogs, PublicLog.empty(), MAX_PUBLIC_LOGS_PER_TX),
340
+ /*publicDataWrites=*/ padArrayEnd(
341
+ finalPublicDataWrites,
342
+ PublicDataWrite.empty(),
343
+ MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
344
+ ),
322
345
  );
323
- avmCircuitPublicInputs.startTreeSnapshots = this.startTreeSnapshots;
324
346
 
347
+ // Now we finally have enough information to pad the trees.
348
+ // NOTE(fcarreiro): a bit weird that a method that generates the PIs does this,
349
+ // but we do need the end tree snapshots. We can consider moving things to the caller.
350
+ const numNoteHashesToPad = MAX_NOTE_HASHES_PER_TX - avmNoteHashes.length;
351
+ const numNullifiersToPad = MAX_NULLIFIERS_PER_TX - avmNullifiers.length;
352
+ await stateManager.padTree(MerkleTreeId.NOTE_HASH_TREE, numNoteHashesToPad);
353
+ await stateManager.padTree(MerkleTreeId.NULLIFIER_TREE, numNullifiersToPad);
354
+ const endTreeSnapshots = await stateManager.getTreeSnapshots();
355
+
356
+ // This converts the private accumulated data to the avm accumulated data format.
357
+ const convertAccumulatedData = (from: PrivateToPublicAccumulatedData) =>
358
+ new PrivateToAvmAccumulatedData(from.noteHashes, from.nullifiers, from.l2ToL1Msgs);
325
359
  const getArrayLengths = (from: PrivateToPublicAccumulatedData) =>
326
360
  new PrivateToAvmAccumulatedDataArrayLengths(
327
361
  countAccumulatedItems(from.noteHashes),
328
362
  countAccumulatedItems(from.nullifiers),
329
363
  countAccumulatedItems(from.l2ToL1Msgs),
330
364
  );
331
- const convertAccumulatedData = (from: PrivateToPublicAccumulatedData) =>
332
- new PrivateToAvmAccumulatedData(from.noteHashes, from.nullifiers, from.l2ToL1Msgs);
333
- // Temporary overrides as these entries aren't yet populated in trace
334
- avmCircuitPublicInputs.previousNonRevertibleAccumulatedDataArrayLengths = getArrayLengths(
335
- this.nonRevertibleAccumulatedDataFromPrivate,
336
- );
337
- avmCircuitPublicInputs.previousRevertibleAccumulatedDataArrayLengths = getArrayLengths(
338
- this.revertibleAccumulatedDataFromPrivate,
339
- );
340
- avmCircuitPublicInputs.previousNonRevertibleAccumulatedData = convertAccumulatedData(
341
- this.nonRevertibleAccumulatedDataFromPrivate,
342
- );
343
- avmCircuitPublicInputs.previousRevertibleAccumulatedData = convertAccumulatedData(
344
- this.revertibleAccumulatedDataFromPrivate,
345
- );
346
-
347
- const msgsFromPrivate = this.revertCode.isOK()
348
- ? mergeAccumulatedData(
349
- avmCircuitPublicInputs.previousNonRevertibleAccumulatedData.l2ToL1Msgs,
350
- avmCircuitPublicInputs.previousRevertibleAccumulatedData.l2ToL1Msgs,
351
- )
352
- : avmCircuitPublicInputs.previousNonRevertibleAccumulatedData.l2ToL1Msgs;
353
- avmCircuitPublicInputs.accumulatedData.l2ToL1Msgs = assertLength(
354
- mergeAccumulatedData(msgsFromPrivate, avmCircuitPublicInputs.accumulatedData.l2ToL1Msgs),
355
- MAX_L2_TO_L1_MSGS_PER_TX,
356
- );
357
-
358
- // Maps slot to value. Maps in TS are iterable in insertion order, which is exactly what we want for
359
- // squashing "to the left", where the first occurrence of a slot uses the value of the last write to it,
360
- // and the rest occurrences are omitted
361
- const squashedPublicDataWrites: Map<bigint, Fr> = new Map();
362
- for (const publicDataWrite of avmCircuitPublicInputs.accumulatedData.publicDataWrites) {
363
- squashedPublicDataWrites.set(publicDataWrite.leafSlot.toBigInt(), publicDataWrite.value);
364
- }
365
365
 
366
- avmCircuitPublicInputs.accumulatedData.publicDataWrites = padArrayEnd(
367
- Array.from(squashedPublicDataWrites.entries()).map(([slot, value]) => new PublicDataWrite(new Fr(slot), value)),
368
- PublicDataWrite.empty(),
369
- MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
366
+ return new AvmCircuitPublicInputs(
367
+ this.globalVariables,
368
+ this.startTreeSnapshots,
369
+ /*startGasUsed=*/ this.gasUsedByPrivate,
370
+ this.gasSettings,
371
+ this.feePayer,
372
+ /*publicSetupCallRequests=*/ padArrayEnd(
373
+ this.setupCallRequests.map(r => r.request),
374
+ PublicCallRequest.empty(),
375
+ MAX_ENQUEUED_CALLS_PER_TX,
376
+ ),
377
+ /*publicAppLogicCallRequests=*/ padArrayEnd(
378
+ this.appLogicCallRequests.map(r => r.request),
379
+ PublicCallRequest.empty(),
380
+ MAX_ENQUEUED_CALLS_PER_TX,
381
+ ),
382
+ /*publicTeardownCallRequests=*/ this.teardownCallRequests.length > 0
383
+ ? this.teardownCallRequests[0].request
384
+ : PublicCallRequest.empty(),
385
+ getArrayLengths(this.nonRevertibleAccumulatedDataFromPrivate),
386
+ getArrayLengths(this.revertibleAccumulatedDataFromPrivate),
387
+ convertAccumulatedData(this.nonRevertibleAccumulatedDataFromPrivate),
388
+ convertAccumulatedData(this.revertibleAccumulatedDataFromPrivate),
389
+ endTreeSnapshots,
390
+ this.getTotalGasUsed(),
391
+ accumulatedData,
392
+ /*transactionFee=*/ this.getTransactionFeeUnsafe(),
393
+ /*isReverted=*/ !this.revertCode.isOK(),
370
394
  );
371
- const numNoteHashesToPad =
372
- MAX_NOTE_HASHES_PER_TX - countAccumulatedItems(avmCircuitPublicInputs.accumulatedData.noteHashes);
373
- await stateManager.padTree(MerkleTreeId.NOTE_HASH_TREE, numNoteHashesToPad);
374
- const numNullifiersToPad =
375
- MAX_NULLIFIERS_PER_TX - countAccumulatedItems(avmCircuitPublicInputs.accumulatedData.nullifiers);
376
- await stateManager.padTree(MerkleTreeId.NULLIFIER_TREE, numNullifiersToPad);
377
- avmCircuitPublicInputs.endTreeSnapshots = await stateManager.getTreeSnapshots();
378
-
379
- return avmCircuitPublicInputs;
380
395
  }
381
396
  }
382
397
 
@@ -60,6 +60,7 @@ export class PublicTxSimulator {
60
60
  private globalVariables: GlobalVariables,
61
61
  private doMerkleOperations: boolean = false,
62
62
  private skipFeeEnforcement: boolean = false,
63
+ private clientInitiatedSimulation: boolean = false,
63
64
  ) {
64
65
  this.log = createLogger(`simulator:public_tx_simulator`);
65
66
  }
@@ -230,7 +231,7 @@ export class PublicTxSimulator {
230
231
  const returnValues: NestedProcessReturnValues[] = [];
231
232
  let reverted = false;
232
233
  let revertReason: SimulationError | undefined;
233
- for (let i = callRequests.length - 1; i >= 0; i--) {
234
+ for (let i = 0; i < callRequests.length; i++) {
234
235
  if (reverted) {
235
236
  break;
236
237
  }
@@ -273,8 +274,6 @@ export class PublicTxSimulator {
273
274
 
274
275
  const allocatedGas = context.getGasLeftAtPhase(phase);
275
276
 
276
- stateManager.traceEnqueuedCall(callRequest.request);
277
-
278
277
  const result = await this.simulateEnqueuedCallInternal(
279
278
  stateManager,
280
279
  callRequest,
@@ -333,6 +332,7 @@ export class PublicTxSimulator {
333
332
  request.isStaticCall,
334
333
  calldata,
335
334
  allocatedGas,
335
+ this.clientInitiatedSimulation,
336
336
  );
337
337
  const avmCallResult = await simulator.execute();
338
338
  return avmCallResult.finalize();
@@ -361,6 +361,12 @@ export class PublicTxSimulator {
361
361
  await stateManager.writeUniqueNoteHash(noteHash);
362
362
  }
363
363
  }
364
+ for (const l2ToL1Message of context.nonRevertibleAccumulatedDataFromPrivate.l2ToL1Msgs) {
365
+ if (!l2ToL1Message.isEmpty()) {
366
+ stateManager.writeScopedL2ToL1Message(l2ToL1Message);
367
+ }
368
+ }
369
+
364
370
  // add new contracts to the contracts db so that their functions may be found and called
365
371
  // TODO(#6464): Should we allow emitting contracts in the private setup phase?
366
372
  // FIXME(fcarreiro): this should conceptually use the hinted contracts db.
@@ -402,6 +408,11 @@ export class PublicTxSimulator {
402
408
  await stateManager.writeSiloedNoteHash(noteHash);
403
409
  }
404
410
  }
411
+ for (const l2ToL1Message of context.revertibleAccumulatedDataFromPrivate.l2ToL1Msgs) {
412
+ if (!l2ToL1Message.isEmpty()) {
413
+ stateManager.writeScopedL2ToL1Message(l2ToL1Message);
414
+ }
415
+ }
405
416
  // add new contracts to the contracts db so that their functions may be found and called
406
417
  // FIXME(fcarreiro): this should conceptually use the hinted contracts db.
407
418
  // However things should work as they are now because the hinted db would still pick up the new contracts.
@@ -24,10 +24,19 @@ export class TelemetryPublicTxSimulator extends MeasuredPublicTxSimulator {
24
24
  globalVariables: GlobalVariables,
25
25
  doMerkleOperations: boolean = false,
26
26
  skipFeeEnforcement: boolean = false,
27
+ clientInitiatedSimulation: boolean = false,
27
28
  telemetryClient: TelemetryClient = getTelemetryClient(),
28
29
  ) {
29
30
  const metrics = new ExecutorMetrics(telemetryClient, 'PublicTxSimulator');
30
- super(merkleTree, contractsDB, globalVariables, doMerkleOperations, skipFeeEnforcement, metrics);
31
+ super(
32
+ merkleTree,
33
+ contractsDB,
34
+ globalVariables,
35
+ doMerkleOperations,
36
+ skipFeeEnforcement,
37
+ clientInitiatedSimulation,
38
+ metrics,
39
+ );
31
40
  this.tracer = metrics.tracer;
32
41
  }
33
42
 
@@ -1,38 +1,23 @@
1
1
  import {
2
- MAX_ENQUEUED_CALLS_PER_TX,
3
2
  MAX_L2_TO_L1_MSGS_PER_TX,
4
3
  MAX_NOTE_HASHES_PER_TX,
5
4
  MAX_NULLIFIERS_PER_TX,
6
5
  MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS,
7
6
  MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
8
7
  MAX_PUBLIC_LOGS_PER_TX,
9
- MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
10
8
  PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
11
- PUBLIC_LOG_DATA_SIZE_IN_FIELDS,
9
+ PUBLIC_LOG_SIZE_IN_FIELDS,
12
10
  } from '@aztec/constants';
13
11
  import { padArrayEnd } from '@aztec/foundation/collection';
14
12
  import { EthAddress } from '@aztec/foundation/eth-address';
15
13
  import { Fr } from '@aztec/foundation/fields';
16
14
  import { createLogger } from '@aztec/foundation/log';
17
- import {
18
- AvmAccumulatedData,
19
- AvmCircuitPublicInputs,
20
- PublicDataUpdateRequest,
21
- PublicDataWrite,
22
- } from '@aztec/stdlib/avm';
15
+ import { PublicDataUpdateRequest } from '@aztec/stdlib/avm';
23
16
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
24
- import type { Gas, GasSettings } from '@aztec/stdlib/gas';
25
17
  import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
26
- import {
27
- NoteHash,
28
- Nullifier,
29
- PrivateToAvmAccumulatedData,
30
- PrivateToAvmAccumulatedDataArrayLengths,
31
- PublicCallRequest,
32
- } from '@aztec/stdlib/kernel';
18
+ import { NoteHash, Nullifier } from '@aztec/stdlib/kernel';
33
19
  import { PublicLog } from '@aztec/stdlib/logs';
34
20
  import { L2ToL1Message, ScopedL2ToL1Message } from '@aztec/stdlib/messaging';
35
- import { type GlobalVariables, TreeSnapshots } from '@aztec/stdlib/tx';
36
21
 
37
22
  import { strict as assert } from 'assert';
38
23
 
@@ -46,7 +31,6 @@ import { UniqueClassIds } from './unique_class_ids.js';
46
31
  * This struct is helpful for testing and checking array lengths.
47
32
  **/
48
33
  export type SideEffects = {
49
- enqueuedCalls: PublicCallRequest[];
50
34
  publicDataWrites: PublicDataUpdateRequest[];
51
35
  noteHashes: NoteHash[];
52
36
  nullifiers: Nullifier[];
@@ -78,7 +62,6 @@ export class SideEffectTrace implements PublicSideEffectTraceInterface {
78
62
  /** The side effect counter increments with every call to the trace. */
79
63
  private sideEffectCounter: number;
80
64
 
81
- private enqueuedCalls: PublicCallRequest[] = [];
82
65
  private publicDataWrites: PublicDataUpdateRequest[] = [];
83
66
  private protocolPublicDataWritesLength: number = 0;
84
67
  private userPublicDataWritesLength: number = 0;
@@ -127,7 +110,6 @@ export class SideEffectTrace implements PublicSideEffectTraceInterface {
127
110
  forkedTrace.alreadyMergedIntoParent = true;
128
111
 
129
112
  this.sideEffectCounter = forkedTrace.sideEffectCounter;
130
- this.enqueuedCalls.push(...forkedTrace.enqueuedCalls);
131
113
  this.uniqueClassIds.acceptAndMerge(forkedTrace.uniqueClassIds);
132
114
 
133
115
  if (!reverted) {
@@ -217,9 +199,7 @@ export class SideEffectTrace implements PublicSideEffectTraceInterface {
217
199
  }
218
200
 
219
201
  const recipientAddress = EthAddress.fromField(recipient);
220
- this.l2ToL1Messages.push(
221
- new L2ToL1Message(recipientAddress, content, this.sideEffectCounter).scope(contractAddress),
222
- );
202
+ this.l2ToL1Messages.push(new L2ToL1Message(recipientAddress, content, 0).scope(contractAddress));
223
203
  this.log.trace(`Tracing new l2 to l1 message (counter=${this.sideEffectCounter})`);
224
204
  this.incrementSideEffectCounter();
225
205
  }
@@ -229,10 +209,10 @@ export class SideEffectTrace implements PublicSideEffectTraceInterface {
229
209
  throw new SideEffectLimitReachedError('public log', MAX_PUBLIC_LOGS_PER_TX);
230
210
  }
231
211
 
232
- if (log.length > PUBLIC_LOG_DATA_SIZE_IN_FIELDS) {
233
- throw new Error(`Emitted public log is too large, max: ${PUBLIC_LOG_DATA_SIZE_IN_FIELDS}, passed: ${log.length}`);
212
+ if (log.length > PUBLIC_LOG_SIZE_IN_FIELDS) {
213
+ throw new Error(`Emitted public log is too large, max: ${PUBLIC_LOG_SIZE_IN_FIELDS}, passed: ${log.length}`);
234
214
  }
235
- const publicLog = new PublicLog(contractAddress, padArrayEnd(log, Fr.ZERO, PUBLIC_LOG_DATA_SIZE_IN_FIELDS));
215
+ const publicLog = new PublicLog(contractAddress, padArrayEnd(log, Fr.ZERO, PUBLIC_LOG_SIZE_IN_FIELDS), log.length);
236
216
  this.publicLogs.push(publicLog);
237
217
  this.log.trace(`Tracing new public log (counter=${this.sideEffectCounter})`);
238
218
  this.incrementSideEffectCounter();
@@ -254,18 +234,8 @@ export class SideEffectTrace implements PublicSideEffectTraceInterface {
254
234
  }
255
235
  }
256
236
 
257
- /**
258
- * Trace an enqueued call.
259
- * Accept some results from a finished call's trace into this one.
260
- */
261
- public traceEnqueuedCall(publicCallRequest: PublicCallRequest) {
262
- // TODO(4805): check if some threshold is reached for max enqueued or nested calls (to unique contracts?)
263
- this.enqueuedCalls.push(publicCallRequest);
264
- }
265
-
266
237
  public getSideEffects(): SideEffects {
267
238
  return {
268
- enqueuedCalls: this.enqueuedCalls,
269
239
  publicDataWrites: this.publicDataWrites,
270
240
  noteHashes: this.noteHashes,
271
241
  nullifiers: this.nullifiers,
@@ -273,78 +243,4 @@ export class SideEffectTrace implements PublicSideEffectTraceInterface {
273
243
  publicLogs: this.publicLogs,
274
244
  };
275
245
  }
276
-
277
- public toAvmCircuitPublicInputs(
278
- /** Globals. */
279
- globalVariables: GlobalVariables,
280
- /** Gas used at start of TX. */
281
- startGasUsed: Gas,
282
- /** How much gas was available for this public execution. */
283
- gasLimits: GasSettings,
284
- /** Address of the fee payer. */
285
- feePayer: AztecAddress,
286
- /** Call requests for setup phase. */
287
- publicSetupCallRequests: PublicCallRequest[],
288
- /** Call requests for app logic phase. */
289
- publicAppLogicCallRequests: PublicCallRequest[],
290
- /** Call request for teardown phase. */
291
- publicTeardownCallRequest: PublicCallRequest,
292
- /** End tree snapshots. */
293
- endTreeSnapshots: TreeSnapshots,
294
- /**
295
- * Gas used by the whole transaction, assuming entire teardown limit is used.
296
- * This is the gas used when computing transaction fee.
297
- */
298
- endGasUsed: Gas,
299
- /** Transaction fee. */
300
- transactionFee: Fr,
301
- /** The call's results */
302
- reverted: boolean,
303
- ): AvmCircuitPublicInputs {
304
- return new AvmCircuitPublicInputs(
305
- globalVariables,
306
- TreeSnapshots.empty(), // will be patched later.
307
- startGasUsed,
308
- gasLimits,
309
- feePayer,
310
- padArrayEnd(publicSetupCallRequests, PublicCallRequest.empty(), MAX_ENQUEUED_CALLS_PER_TX),
311
- padArrayEnd(publicAppLogicCallRequests, PublicCallRequest.empty(), MAX_ENQUEUED_CALLS_PER_TX),
312
- publicTeardownCallRequest,
313
- /*previousNonRevertibleAccumulatedDataArrayLengths=*/ PrivateToAvmAccumulatedDataArrayLengths.empty(),
314
- /*previousRevertibleAccumulatedDataArrayLengths=*/ PrivateToAvmAccumulatedDataArrayLengths.empty(),
315
- /*previousNonRevertibleAccumulatedDataArray=*/ PrivateToAvmAccumulatedData.empty(),
316
- /*previousRevertibleAccumulatedDataArray=*/ PrivateToAvmAccumulatedData.empty(),
317
- endTreeSnapshots,
318
- endGasUsed,
319
- /*accumulatedData=*/ this.getAvmAccumulatedData(),
320
- transactionFee,
321
- reverted,
322
- );
323
- }
324
-
325
- public getPublicLogs() {
326
- return this.publicLogs;
327
- }
328
-
329
- private getAvmAccumulatedData() {
330
- return new AvmAccumulatedData(
331
- padArrayEnd(
332
- this.noteHashes.map(n => n.value),
333
- Fr.zero(),
334
- MAX_NOTE_HASHES_PER_TX,
335
- ),
336
- padArrayEnd(
337
- this.nullifiers.map(n => n.value),
338
- Fr.zero(),
339
- MAX_NULLIFIERS_PER_TX,
340
- ),
341
- padArrayEnd(this.l2ToL1Messages, ScopedL2ToL1Message.empty(), MAX_L2_TO_L1_MSGS_PER_TX),
342
- padArrayEnd(this.publicLogs, PublicLog.empty(), MAX_PUBLIC_LOGS_PER_TX),
343
- padArrayEnd(
344
- this.publicDataWrites.map(w => new PublicDataWrite(w.leafSlot, w.newValue)),
345
- PublicDataWrite.empty(),
346
- MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
347
- ),
348
- );
349
- }
350
246
  }
@@ -1,7 +1,5 @@
1
1
  import type { Fr } from '@aztec/foundation/fields';
2
2
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
3
- import type { PublicCallRequest } from '@aztec/stdlib/kernel';
4
- import type { PublicLog } from '@aztec/stdlib/logs';
5
3
 
6
4
  export interface PublicSideEffectTraceInterface {
7
5
  fork(): PublicSideEffectTraceInterface;
@@ -20,6 +18,4 @@ export interface PublicSideEffectTraceInterface {
20
18
  traceNewL2ToL1Message(contractAddress: AztecAddress, recipient: Fr, content: Fr): void;
21
19
  tracePublicLog(contractAddress: AztecAddress, log: Fr[]): void;
22
20
  traceGetContractClass(contractClassId: Fr, exists: boolean): void;
23
- traceEnqueuedCall(publicCallRequest: PublicCallRequest): void;
24
- getPublicLogs(): PublicLog[];
25
21
  }
@@ -15,7 +15,7 @@ import { AztecAddress } from '@aztec/stdlib/aztec-address';
15
15
  import type { ContractClassPublicWithCommitment, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
16
16
  import { SerializableContractInstance } from '@aztec/stdlib/contract';
17
17
  import { computeNoteHashNonce, computeUniqueNoteHash, siloNoteHash, siloNullifier } from '@aztec/stdlib/hash';
18
- import type { PublicCallRequest } from '@aztec/stdlib/kernel';
18
+ import { ScopedL2ToL1Message } from '@aztec/stdlib/messaging';
19
19
  import { SharedMutableValues, SharedMutableValuesWithHash } from '@aztec/stdlib/shared-mutable';
20
20
  import { MerkleTreeId } from '@aztec/stdlib/trees';
21
21
  import type { TreeSnapshots } from '@aztec/stdlib/tx';
@@ -302,6 +302,18 @@ export class PublicPersistableStateManager {
302
302
  this.trace.traceNewL2ToL1Message(contractAddress, recipient, content);
303
303
  }
304
304
 
305
+ /**
306
+ * Write a scoped L2 to L1 message.
307
+ * @param l2ToL1Message - The L2 to L1 message to write.
308
+ */
309
+ public writeScopedL2ToL1Message(l2ToL1Message: ScopedL2ToL1Message) {
310
+ this.writeL2ToL1Message(
311
+ l2ToL1Message.contractAddress,
312
+ l2ToL1Message.message.recipient.toField(),
313
+ l2ToL1Message.message.content,
314
+ );
315
+ }
316
+
305
317
  /**
306
318
  * Write a public log
307
319
  * @param contractAddress - address of the contract that emitted the log
@@ -460,10 +472,6 @@ export class PublicPersistableStateManager {
460
472
  return contractClass.packedBytecode;
461
473
  }
462
474
 
463
- public traceEnqueuedCall(publicCallRequest: PublicCallRequest) {
464
- this.trace.traceEnqueuedCall(publicCallRequest);
465
- }
466
-
467
475
  public async getPublicFunctionDebugName(avmEnvironment: AvmExecutionEnvironment): Promise<string> {
468
476
  return await getPublicFunctionDebugName(this.contractsDB, avmEnvironment.address, avmEnvironment.calldata);
469
477
  }