@aztec/simulator 0.0.1-commit.592b9384 → 0.0.1-commit.59a0419c6

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 (103) hide show
  1. package/README.md +4 -4
  2. package/dest/private/circuit_recording/circuit_recorder.js +2 -2
  3. package/dest/public/avm/avm_gas.js +3 -3
  4. package/dest/public/avm/avm_simulator.js +1 -1
  5. package/dest/public/avm/calldata.d.ts +1 -1
  6. package/dest/public/avm/calldata.d.ts.map +1 -1
  7. package/dest/public/avm/calldata.js +3 -2
  8. package/dest/public/avm/fixtures/account_proof_fetcher.d.ts +2 -0
  9. package/dest/public/avm/fixtures/account_proof_fetcher.d.ts.map +1 -0
  10. package/dest/public/avm/fixtures/account_proof_fetcher.js +152 -0
  11. package/dest/public/avm/opcodes/accrued_substate.d.ts +2 -2
  12. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
  13. package/dest/public/avm/opcodes/accrued_substate.js +3 -4
  14. package/dest/public/avm/serialization/bytecode_serialization.js +3 -3
  15. package/dest/public/avm/serialization/instruction_serialization.d.ts +2 -2
  16. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -1
  17. package/dest/public/avm/serialization/instruction_serialization.js +1 -1
  18. package/dest/public/contracts_db_checkpoint.d.ts +2 -2
  19. package/dest/public/contracts_db_checkpoint.d.ts.map +1 -1
  20. package/dest/public/contracts_db_checkpoint.js +1 -1
  21. package/dest/public/fixtures/amm_test.js +2 -2
  22. package/dest/public/fixtures/bulk_test.d.ts +1 -1
  23. package/dest/public/fixtures/bulk_test.d.ts.map +1 -1
  24. package/dest/public/fixtures/bulk_test.js +4 -4
  25. package/dest/public/fixtures/custom_bytecode_tests.d.ts +3 -1
  26. package/dest/public/fixtures/custom_bytecode_tests.d.ts.map +1 -1
  27. package/dest/public/fixtures/custom_bytecode_tests.js +61 -1
  28. package/dest/public/fixtures/opcode_spammer.d.ts +1 -1
  29. package/dest/public/fixtures/opcode_spammer.d.ts.map +1 -1
  30. package/dest/public/fixtures/opcode_spammer.js +6 -6
  31. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +6 -5
  32. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  33. package/dest/public/fixtures/public_tx_simulation_tester.js +37 -10
  34. package/dest/public/fixtures/utils.d.ts +2 -2
  35. package/dest/public/fixtures/utils.d.ts.map +1 -1
  36. package/dest/public/fixtures/utils.js +10 -10
  37. package/dest/public/hinting_db_sources.d.ts +5 -5
  38. package/dest/public/hinting_db_sources.d.ts.map +1 -1
  39. package/dest/public/hinting_db_sources.js +7 -6
  40. package/dest/public/public_db_sources.d.ts +3 -3
  41. package/dest/public/public_db_sources.d.ts.map +1 -1
  42. package/dest/public/public_db_sources.js +10 -10
  43. package/dest/public/public_processor/guarded_merkle_tree.d.ts +5 -5
  44. package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -1
  45. package/dest/public/public_processor/guarded_merkle_tree.js +5 -5
  46. package/dest/public/public_processor/public_processor.d.ts +5 -3
  47. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  48. package/dest/public/public_processor/public_processor.js +68 -46
  49. package/dest/public/public_processor/public_processor_metrics.d.ts +4 -1
  50. package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -1
  51. package/dest/public/public_processor/public_processor_metrics.js +8 -0
  52. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +1 -1
  53. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts.map +1 -1
  54. package/dest/public/public_tx_simulator/contract_provider_for_cpp.js +2 -1
  55. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +1 -1
  56. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts.map +1 -1
  57. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.js +2 -3
  58. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts +1 -1
  59. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts.map +1 -1
  60. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.js +2 -2
  61. package/dest/public/public_tx_simulator/factories.d.ts +2 -2
  62. package/dest/public/public_tx_simulator/factories.d.ts.map +1 -1
  63. package/dest/public/public_tx_simulator/factories.js +2 -2
  64. package/dest/public/public_tx_simulator/public_tx_context.d.ts +7 -3
  65. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  66. package/dest/public/public_tx_simulator/public_tx_context.js +8 -10
  67. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +6 -2
  68. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  69. package/dest/public/public_tx_simulator/public_tx_simulator.js +14 -9
  70. package/dest/public/test_executor_metrics.d.ts +6 -1
  71. package/dest/public/test_executor_metrics.d.ts.map +1 -1
  72. package/dest/public/test_executor_metrics.js +22 -0
  73. package/package.json +15 -16
  74. package/src/private/circuit_recording/circuit_recorder.ts +2 -2
  75. package/src/public/avm/avm_gas.ts +2 -2
  76. package/src/public/avm/avm_simulator.ts +1 -1
  77. package/src/public/avm/calldata.ts +3 -2
  78. package/src/public/avm/fixtures/account_proof.json +553 -0
  79. package/src/public/avm/fixtures/account_proof_fetcher.ts +166 -0
  80. package/src/public/avm/opcodes/accrued_substate.ts +3 -4
  81. package/src/public/avm/opcodes/external_calls.ts +1 -1
  82. package/src/public/avm/serialization/bytecode_serialization.ts +2 -2
  83. package/src/public/avm/serialization/instruction_serialization.ts +1 -1
  84. package/src/public/contracts_db_checkpoint.ts +1 -1
  85. package/src/public/fixtures/amm_test.ts +2 -2
  86. package/src/public/fixtures/bulk_test.ts +2 -4
  87. package/src/public/fixtures/custom_bytecode_tests.ts +139 -1
  88. package/src/public/fixtures/opcode_spammer.ts +6 -10
  89. package/src/public/fixtures/public_tx_simulation_tester.ts +47 -6
  90. package/src/public/fixtures/utils.ts +17 -12
  91. package/src/public/fuzzing/avm_fuzzer_simulator.ts +1 -1
  92. package/src/public/hinting_db_sources.ts +9 -7
  93. package/src/public/public_db_sources.ts +12 -14
  94. package/src/public/public_processor/guarded_merkle_tree.ts +6 -6
  95. package/src/public/public_processor/public_processor.ts +94 -62
  96. package/src/public/public_processor/public_processor_metrics.ts +12 -0
  97. package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +2 -1
  98. package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +2 -3
  99. package/src/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.ts +2 -2
  100. package/src/public/public_tx_simulator/factories.ts +2 -1
  101. package/src/public/public_tx_simulator/public_tx_context.ts +8 -10
  102. package/src/public/public_tx_simulator/public_tx_simulator.ts +14 -10
  103. package/src/public/test_executor_metrics.ts +24 -0
@@ -174,14 +174,8 @@ export class PublicTxContext {
174
174
  }
175
175
  if (phase === TxExecutionPhase.SETUP) {
176
176
  this.log.warn(`Setup phase reverted! The transaction will be thrown out.`);
177
- } else if (phase === TxExecutionPhase.APP_LOGIC) {
178
- this.revertCode = RevertCode.APP_LOGIC_REVERTED;
179
- } else if (phase === TxExecutionPhase.TEARDOWN) {
180
- if (this.revertCode.equals(RevertCode.APP_LOGIC_REVERTED)) {
181
- this.revertCode = RevertCode.BOTH_REVERTED;
182
- } else {
183
- this.revertCode = RevertCode.TEARDOWN_REVERTED;
184
- }
177
+ } else if (phase === TxExecutionPhase.APP_LOGIC || phase === TxExecutionPhase.TEARDOWN) {
178
+ this.revertCode = RevertCode.REVERTED;
185
179
  }
186
180
  }
187
181
 
@@ -247,8 +241,12 @@ export class PublicTxContext {
247
241
  }
248
242
 
249
243
  /**
250
- * The gasUsed by public and private,
251
- * as if the entire teardown gas limit was consumed.
244
+ * The gasUsed by public and private, as if the entire teardown gas limit was consumed.
245
+ *
246
+ * This is intentional: teardown is used for gas accounting and refunds, so the transaction
247
+ * fee must be deterministic _before_ teardown executes. If fees depended on teardown's actual
248
+ * consumption there would be a circular dependency. Billing the full teardown gas limit
249
+ * (set by the user) makes the fee known in advance and available to the teardown function.
252
250
  */
253
251
  getTotalGasUsed(): Gas {
254
252
  return this.gasUsedByPrivate.add(this.gasUsedByPublic);
@@ -1,4 +1,4 @@
1
- import { AVM_MAX_PROCESSABLE_L2_GAS } from '@aztec/constants';
1
+ import { MAX_PROCESSABLE_L2_GAS } from '@aztec/constants';
2
2
  import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
4
4
  import { ProtocolContractAddress, ProtocolContractsList } from '@aztec/protocol-contracts';
@@ -27,7 +27,6 @@ import { type PublicContractsDB, PublicTreesDB } from '../public_db_sources.js';
27
27
  import {
28
28
  L2ToL1MessageLimitReachedError,
29
29
  NoteHashLimitReachedError,
30
- NullifierCollisionError,
31
30
  NullifierLimitReachedError,
32
31
  } from '../side_effect_errors.js';
33
32
  import type { PublicPersistableStateManager } from '../state_manager/state_manager.js';
@@ -147,7 +146,8 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
147
146
  hintingContractsDB.createCheckpoint();
148
147
 
149
148
  try {
150
- // This will throw if there is a nullifier collision or other insertion error (limit reached).
149
+ // This may throw: a side-effect limit error triggers a soft revert (caught below), while a
150
+ // nullifier collision is unrecoverable and propagates out of simulate() to throw out the tx.
151
151
  await this.insertRevertiblesFromPrivate(context);
152
152
 
153
153
  // Only proceed with app logic if there was no revert during revertible insertion.
@@ -199,8 +199,8 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
199
199
 
200
200
  // Such transactions should be filtered by GasTxValidator.
201
201
  assert(
202
- context.getActualGasUsed().l2Gas <= AVM_MAX_PROCESSABLE_L2_GAS,
203
- `Transaction consumes ${context.getActualGasUsed().l2Gas} L2 gas, which exceeds the AVM maximum processable gas of ${AVM_MAX_PROCESSABLE_L2_GAS}`,
202
+ context.getActualGasUsed().l2Gas <= MAX_PROCESSABLE_L2_GAS,
203
+ `Transaction consumes ${context.getActualGasUsed().l2Gas} L2 gas, which exceeds the maximum processable gas of ${MAX_PROCESSABLE_L2_GAS}`,
204
204
  );
205
205
  await this.payFee(context);
206
206
 
@@ -401,7 +401,7 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
401
401
  // However, things work as expected because later calls to getters on the hintingContractsDB
402
402
  // will pick up the new contracts and will generate the necessary hints.
403
403
  // So, a consumer of the hints will always see the new contracts.
404
- await this.contractsDB.addContracts(context.nonRevertibleContractDeploymentData);
404
+ this.contractsDB.addContracts(context.nonRevertibleContractDeploymentData);
405
405
  }
406
406
 
407
407
  /**
@@ -409,9 +409,13 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
409
409
  * Throws TxSimRevertibleInsertionsRevert if there is some checked error during revertible insertions.
410
410
  * This function checks for the following errors:
411
411
  * - NullifierLimitReachedError
412
- * - NullifierCollisionError
413
412
  * - NoteHashLimitReachedError
414
413
  * - L2ToL1MessageLimitReachedError
414
+ *
415
+ * Note: NullifierCollisionError is intentionally NOT caught here. A nullifier collision
416
+ * during revertible insertions is unprovable (the nullifier originated from private, so
417
+ * a collision indicates the tx should never have been proposed). It propagates as-is to
418
+ * make the transaction unrecoverable, matching the AVM circuit behavior.
415
419
  */
416
420
  protected async insertRevertiblesFromPrivate(context: PublicTxContext) {
417
421
  const stateManager = context.state.getActiveStateManager();
@@ -421,7 +425,7 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
421
425
  await stateManager.writeSiloedNullifier(siloedNullifier);
422
426
  }
423
427
  } catch (e: any) {
424
- if (e instanceof NullifierLimitReachedError || e instanceof NullifierCollisionError) {
428
+ if (e instanceof NullifierLimitReachedError) {
425
429
  context.revert(
426
430
  TxExecutionPhase.APP_LOGIC,
427
431
  new SimulationError(
@@ -431,7 +435,7 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
431
435
  );
432
436
  throw new TxSimRevertibleInsertionsRevert();
433
437
  } else {
434
- // Unchecked/unknown error - re-throw as-is
438
+ // Unchecked/unknown error or NullifierCollisionError (unrecoverable) - re-throw as-is
435
439
  throw e;
436
440
  }
437
441
  }
@@ -486,7 +490,7 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
486
490
  // However, things work as expected because later calls to getters on the hintingContractsDB
487
491
  // will pick up the new contracts and will generate the necessary hints.
488
492
  // So, a consumer of the hints will always see the new contracts.
489
- await this.contractsDB.addContracts(context.revertibleContractDeploymentData);
493
+ this.contractsDB.addContracts(context.revertibleContractDeploymentData);
490
494
  }
491
495
 
492
496
  private async payFee(context: PublicTxContext) {
@@ -21,6 +21,7 @@ export interface PublicTxMetrics {
21
21
  totalDurationMs: number;
22
22
  manaUsed: number | undefined;
23
23
  totalInstructionsExecuted: number;
24
+ bytecodeSizes: { contractName: string; sizeBytes: number }[];
24
25
  nonRevertiblePrivateInsertionsUs: number | undefined;
25
26
  revertiblePrivateInsertionsUs: number | undefined;
26
27
  enqueuedCalls: PublicEnqueuedCallMetrics[];
@@ -62,6 +63,7 @@ function createEmptyTxMetrics(): PublicTxMetrics {
62
63
  totalDurationMs: 0,
63
64
  manaUsed: 0,
64
65
  totalInstructionsExecuted: 0,
66
+ bytecodeSizes: [],
65
67
  nonRevertiblePrivateInsertionsUs: undefined,
66
68
  revertiblePrivateInsertionsUs: undefined,
67
69
  enqueuedCalls: [],
@@ -172,6 +174,12 @@ export class TestExecutorMetrics implements ExecutorMetricsInterface {
172
174
  }
173
175
  }
174
176
 
177
+ recordBytecodeSize(txLabel: string, contractName: string, sizeBytes: number) {
178
+ const txMetrics = this.txMetrics.get(txLabel);
179
+ assert(txMetrics, `Cannot record bytecode size for unknown tx label: ${txLabel}`);
180
+ txMetrics.bytecodeSizes.push({ contractName, sizeBytes });
181
+ }
182
+
175
183
  recordProverMetrics(txLabel: string, metrics: Partial<PublicTxMetrics>) {
176
184
  if (!this.txMetrics.has(txLabel)) {
177
185
  this.txMetrics.set(txLabel, createEmptyTxMetrics());
@@ -216,6 +224,15 @@ export class TestExecutorMetrics implements ExecutorMetricsInterface {
216
224
  ) {
217
225
  pretty += `${INDENT0}Total instructions executed: ${fmtNum(txMetrics.totalInstructionsExecuted)}\n`;
218
226
  }
227
+ if (
228
+ (filter === PublicTxMetricsFilter.TOTALS || filter === PublicTxMetricsFilter.ALL) &&
229
+ txMetrics.bytecodeSizes.length > 0
230
+ ) {
231
+ pretty += `${INDENT0}Bytecode sizes:\n`;
232
+ for (const { contractName, sizeBytes } of txMetrics.bytecodeSizes) {
233
+ pretty += `${INDENT1}${contractName}: ${fmtNum(sizeBytes, 'bytes')}\n`;
234
+ }
235
+ }
219
236
  if (filter === PublicTxMetricsFilter.DURATIONS || filter === PublicTxMetricsFilter.ALL) {
220
237
  pretty += `${INDENT0}Private insertions:\n`;
221
238
  pretty += `${INDENT1}Non-revertible: ${fmtNum(txMetrics.nonRevertiblePrivateInsertionsUs! / 1_000, 'ms')}\n`;
@@ -387,6 +404,13 @@ export class TestExecutorMetrics implements ExecutorMetricsInterface {
387
404
  });
388
405
  }
389
406
  }
407
+ for (const { contractName, sizeBytes } of txMetrics.bytecodeSizes) {
408
+ data.push({
409
+ name: `${txLabel}/bytecodeSizeBytes/${contractName}`,
410
+ value: sizeBytes,
411
+ unit: 'bytes',
412
+ });
413
+ }
390
414
  }
391
415
  return JSON.stringify(data, null, indent);
392
416
  }