@aztec/simulator 0.0.1-commit.5de5ca79e → 0.0.1-commit.6201a7b05

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 (52) hide show
  1. package/dest/public/avm/avm_simulator.js +1 -1
  2. package/dest/public/avm/calldata.d.ts +1 -1
  3. package/dest/public/avm/calldata.d.ts.map +1 -1
  4. package/dest/public/avm/calldata.js +3 -2
  5. package/dest/public/contracts_db_checkpoint.d.ts +2 -2
  6. package/dest/public/contracts_db_checkpoint.d.ts.map +1 -1
  7. package/dest/public/contracts_db_checkpoint.js +1 -1
  8. package/dest/public/fixtures/bulk_test.d.ts +1 -1
  9. package/dest/public/fixtures/bulk_test.d.ts.map +1 -1
  10. package/dest/public/fixtures/bulk_test.js +4 -4
  11. package/dest/public/fixtures/custom_bytecode_tests.d.ts +3 -1
  12. package/dest/public/fixtures/custom_bytecode_tests.d.ts.map +1 -1
  13. package/dest/public/fixtures/custom_bytecode_tests.js +61 -1
  14. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +1 -1
  15. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  16. package/dest/public/fixtures/public_tx_simulation_tester.js +3 -3
  17. package/dest/public/fixtures/utils.d.ts +1 -1
  18. package/dest/public/fixtures/utils.d.ts.map +1 -1
  19. package/dest/public/fixtures/utils.js +5 -5
  20. package/dest/public/public_db_sources.d.ts +3 -3
  21. package/dest/public/public_db_sources.d.ts.map +1 -1
  22. package/dest/public/public_db_sources.js +10 -10
  23. package/dest/public/public_processor/public_processor.d.ts +1 -1
  24. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  25. package/dest/public/public_processor/public_processor.js +17 -5
  26. package/dest/public/public_processor/public_processor_metrics.d.ts +4 -1
  27. package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -1
  28. package/dest/public/public_processor/public_processor_metrics.js +8 -0
  29. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +1 -1
  30. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts.map +1 -1
  31. package/dest/public/public_tx_simulator/contract_provider_for_cpp.js +2 -1
  32. package/dest/public/public_tx_simulator/public_tx_context.d.ts +7 -3
  33. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  34. package/dest/public/public_tx_simulator/public_tx_context.js +8 -10
  35. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +6 -2
  36. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  37. package/dest/public/public_tx_simulator/public_tx_simulator.js +12 -7
  38. package/package.json +15 -15
  39. package/src/public/avm/avm_simulator.ts +1 -1
  40. package/src/public/avm/calldata.ts +3 -2
  41. package/src/public/avm/opcodes/external_calls.ts +1 -1
  42. package/src/public/contracts_db_checkpoint.ts +1 -1
  43. package/src/public/fixtures/bulk_test.ts +2 -4
  44. package/src/public/fixtures/custom_bytecode_tests.ts +139 -1
  45. package/src/public/fixtures/public_tx_simulation_tester.ts +4 -9
  46. package/src/public/fixtures/utils.ts +12 -8
  47. package/src/public/public_db_sources.ts +12 -14
  48. package/src/public/public_processor/public_processor.ts +28 -9
  49. package/src/public/public_processor/public_processor_metrics.ts +12 -0
  50. package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +2 -1
  51. package/src/public/public_tx_simulator/public_tx_context.ts +8 -10
  52. package/src/public/public_tx_simulator/public_tx_simulator.ts +11 -7
@@ -55,10 +55,10 @@ export class PublicContractsDB implements PublicContractsDBInterface {
55
55
  this.log = createLogger('simulator:contracts-data-source', bindings);
56
56
  }
57
57
 
58
- public async addContracts(contractDeploymentData: ContractDeploymentData): Promise<void> {
58
+ public addContracts(contractDeploymentData: ContractDeploymentData): void {
59
59
  const currentState = this.getCurrentState();
60
60
 
61
- await this.addContractClassesFromEvents(
61
+ this.addContractClassesFromEvents(
62
62
  ContractClassPublishedEvent.extractContractClassEvents(contractDeploymentData.getContractClassLogs()),
63
63
  currentState,
64
64
  );
@@ -69,10 +69,10 @@ export class PublicContractsDB implements PublicContractsDBInterface {
69
69
  );
70
70
  }
71
71
 
72
- public async addNewContracts(tx: Tx): Promise<void> {
72
+ public addNewContracts(tx: Tx): void {
73
73
  const contractDeploymentData = AllContractDeploymentData.fromTx(tx);
74
- await this.addContracts(contractDeploymentData.getNonRevertibleContractDeploymentData());
75
- await this.addContracts(contractDeploymentData.getRevertibleContractDeploymentData());
74
+ this.addContracts(contractDeploymentData.getNonRevertibleContractDeploymentData());
75
+ this.addContracts(contractDeploymentData.getRevertibleContractDeploymentData());
76
76
  }
77
77
 
78
78
  /**
@@ -81,7 +81,7 @@ export class PublicContractsDB implements PublicContractsDBInterface {
81
81
  */
82
82
  public createCheckpoint(): void {
83
83
  const currentState = this.getCurrentState();
84
- const newState = currentState.deepCopy();
84
+ const newState = currentState.fork();
85
85
  this.contractStateStack.push(newState);
86
86
  }
87
87
 
@@ -174,17 +174,15 @@ export class PublicContractsDB implements PublicContractsDBInterface {
174
174
  return await this.dataSource.getDebugFunctionName(address, selector);
175
175
  }
176
176
 
177
- private async addContractClassesFromEvents(
177
+ private addContractClassesFromEvents(
178
178
  contractClassEvents: ContractClassPublishedEvent[],
179
179
  state: ContractsDbCheckpoint,
180
180
  ) {
181
- await Promise.all(
182
- contractClassEvents.map(async (event: ContractClassPublishedEvent) => {
183
- this.log.debug(`Adding class ${event.contractClassId.toString()} to contract state`);
184
- const contractClass = await event.toContractClassPublic();
185
- state.addClass(event.contractClassId, contractClass);
186
- }),
187
- );
181
+ for (const event of contractClassEvents) {
182
+ this.log.debug(`Adding class ${event.contractClassId.toString()} to contract state`);
183
+ const contractClass = event.toContractClassPublic();
184
+ state.addClass(event.contractClassId, contractClass);
185
+ }
188
186
  }
189
187
 
190
188
  private addContractInstancesFromEvents(
@@ -174,6 +174,8 @@ export class PublicProcessor implements Traceable {
174
174
  let totalPublicGas = new Gas(0, 0);
175
175
  let totalBlockGas = new Gas(0, 0);
176
176
  let totalBlobFields = 0;
177
+ let silentlySkippedCount = 0;
178
+ let totalSilentlySkippedDurationMs = 0;
177
179
 
178
180
  for await (const tx of txs) {
179
181
  // Only process up to the max tx limit
@@ -244,7 +246,9 @@ export class PublicProcessor implements Traceable {
244
246
  this.contractsDB.createCheckpoint();
245
247
 
246
248
  try {
247
- const [processedTx, returnValues, txDebugLogs] = await this.processTx(tx, deadline);
249
+ const [txProcessingTimeMs, [processedTx, returnValues, txDebugLogs]] = await elapsed(() =>
250
+ this.processTx(tx, deadline),
251
+ );
248
252
 
249
253
  // Inject a fake processing failure after N txs if requested
250
254
  const fakeThrowAfter = this.opts.fakeThrowAfterProcessingTxCount;
@@ -265,8 +269,12 @@ export class PublicProcessor implements Traceable {
265
269
  txBlobFields,
266
270
  totalBlobFields,
267
271
  maxBlobFields,
272
+ txProcessingTimeMs,
268
273
  },
269
274
  );
275
+ silentlySkippedCount += 1;
276
+ totalSilentlySkippedDurationMs += txProcessingTimeMs;
277
+ this.metrics.recordSilentlySkipped(txProcessingTimeMs);
270
278
  // Need to revert the checkpoint here and don't go any further
271
279
  await checkpoint.revert();
272
280
  this.contractsDB.revertCheckpoint();
@@ -364,13 +372,24 @@ export class PublicProcessor implements Traceable {
364
372
  const rate = duration > 0 ? totalPublicGas.l2Gas / duration : 0;
365
373
  this.metrics.recordAllTxs(totalPublicGas, rate);
366
374
 
367
- this.log.info(`Processed ${result.length} successful txs and ${failed.length} failed txs in ${duration}s`, {
368
- duration,
369
- rate,
370
- totalPublicGas,
371
- totalBlockGas,
372
- totalSizeInBytes,
373
- });
375
+ const silentlySkippedDurationMs = Math.round(totalSilentlySkippedDurationMs);
376
+ this.log.info(
377
+ `Processed ${result.length} successful txs and ${failed.length} failed txs ` +
378
+ `(${silentlySkippedCount} silently skipped, ${silentlySkippedDurationMs}ms wasted) ` +
379
+ `in ${duration}s`,
380
+ {
381
+ blockNumber: this.globalVariables.blockNumber,
382
+ successfulCount: result.length,
383
+ failedCount: failed.length,
384
+ duration,
385
+ rate,
386
+ totalPublicGas,
387
+ totalBlockGas,
388
+ totalSizeInBytes,
389
+ silentlySkippedCount,
390
+ silentlySkippedDurationMs,
391
+ },
392
+ );
374
393
 
375
394
  return [result, failed, usedTxs, returns, debugLogs];
376
395
  }
@@ -548,7 +567,7 @@ export class PublicProcessor implements Traceable {
548
567
  // Fee payment insertion has already been done. Do the rest.
549
568
  await this.doTreeInsertionsForPrivateOnlyTx(processedTx);
550
569
 
551
- await this.contractsDB.addNewContracts(tx);
570
+ this.contractsDB.addNewContracts(tx);
552
571
 
553
572
  return [processedTx, undefined, []];
554
573
  }
@@ -30,6 +30,9 @@ export class PublicProcessorMetrics {
30
30
 
31
31
  private treeInsertionDuration: Histogram;
32
32
 
33
+ private silentlySkippedCount: UpDownCounter;
34
+ private silentlySkippedDuration: Histogram;
35
+
33
36
  constructor(client: TelemetryClient, name = 'PublicProcessor') {
34
37
  this.tracer = client.getTracer(name);
35
38
  const meter = client.getMeter(name);
@@ -60,6 +63,10 @@ export class PublicProcessorMetrics {
60
63
  this.gasRate = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_GAS_RATE);
61
64
 
62
65
  this.treeInsertionDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TREE_INSERTION);
66
+
67
+ this.silentlySkippedCount = createUpDownCounterWithDefault(meter, Metrics.PUBLIC_PROCESSOR_SILENTLY_SKIPPED_COUNT);
68
+
69
+ this.silentlySkippedDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_SILENTLY_SKIPPED_DURATION);
63
70
  }
64
71
 
65
72
  recordPhaseDuration(phaseName: TxExecutionPhase, durationMs: number) {
@@ -123,4 +130,9 @@ export class PublicProcessorMetrics {
123
130
  recordTreeInsertions(durationUs: number) {
124
131
  this.treeInsertionDuration.record(Math.ceil(durationUs));
125
132
  }
133
+
134
+ recordSilentlySkipped(durationMs: number) {
135
+ this.silentlySkippedCount.add(1);
136
+ this.silentlySkippedDuration.record(Math.ceil(durationMs));
137
+ }
126
138
  }
@@ -52,6 +52,7 @@ export class ContractProviderForCpp implements ContractProvider {
52
52
  return serializeWithMessagePack(contractClass);
53
53
  };
54
54
 
55
+ // eslint-disable-next-line require-await
55
56
  public addContracts = async (contractDeploymentDataBuffer: Buffer): Promise<void> => {
56
57
  this.log.trace(`Contract provider callback: addContracts`);
57
58
 
@@ -62,7 +63,7 @@ export class ContractProviderForCpp implements ContractProvider {
62
63
 
63
64
  // Add contracts to the contracts DB
64
65
  this.log.trace(`Calling contractsDB.addContracts`);
65
- await this.contractsDB.addContracts(contractDeploymentData);
66
+ this.contractsDB.addContracts(contractDeploymentData);
66
67
  };
67
68
 
68
69
  public getBytecodeCommitment = async (classId: string): Promise<Buffer | undefined> => {
@@ -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);
@@ -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.
@@ -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) {