@aztec/simulator 0.0.1-commit.1bea0213 → 0.0.1-commit.1dcfe2301

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 (145) hide show
  1. package/README.md +4 -4
  2. package/dest/private/acvm/acvm.d.ts +4 -2
  3. package/dest/private/acvm/acvm.d.ts.map +1 -1
  4. package/dest/private/acvm/acvm.js +4 -3
  5. package/dest/private/acvm_native.d.ts +5 -3
  6. package/dest/private/acvm_native.d.ts.map +1 -1
  7. package/dest/private/acvm_native.js +8 -6
  8. package/dest/private/acvm_wasm.d.ts +4 -3
  9. package/dest/private/acvm_wasm.d.ts.map +1 -1
  10. package/dest/private/acvm_wasm.js +4 -4
  11. package/dest/private/circuit_recording/circuit_recorder.d.ts +4 -3
  12. package/dest/private/circuit_recording/circuit_recorder.d.ts.map +1 -1
  13. package/dest/private/circuit_recording/circuit_recorder.js +7 -5
  14. package/dest/private/circuit_recording/file_circuit_recorder.d.ts +3 -2
  15. package/dest/private/circuit_recording/file_circuit_recorder.d.ts.map +1 -1
  16. package/dest/private/circuit_recording/file_circuit_recorder.js +2 -2
  17. package/dest/private/circuit_recording/memory_circuit_recorder.d.ts +7 -2
  18. package/dest/private/circuit_recording/memory_circuit_recorder.d.ts.map +1 -1
  19. package/dest/private/circuit_recording/memory_circuit_recorder.js +4 -4
  20. package/dest/private/factory.d.ts +3 -3
  21. package/dest/private/factory.d.ts.map +1 -1
  22. package/dest/private/factory.js +7 -4
  23. package/dest/public/avm/avm_gas.js +3 -3
  24. package/dest/public/avm/avm_simulator.js +1 -1
  25. package/dest/public/avm/calldata.d.ts +1 -1
  26. package/dest/public/avm/calldata.d.ts.map +1 -1
  27. package/dest/public/avm/calldata.js +3 -2
  28. package/dest/public/avm/fixtures/account_proof_fetcher.d.ts +2 -0
  29. package/dest/public/avm/fixtures/account_proof_fetcher.d.ts.map +1 -0
  30. package/dest/public/avm/fixtures/account_proof_fetcher.js +152 -0
  31. package/dest/public/avm/opcodes/accrued_substate.d.ts +4 -5
  32. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
  33. package/dest/public/avm/opcodes/accrued_substate.js +11 -16
  34. package/dest/public/avm/serialization/bytecode_serialization.js +3 -3
  35. package/dest/public/avm/serialization/instruction_serialization.d.ts +2 -2
  36. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -1
  37. package/dest/public/avm/serialization/instruction_serialization.js +1 -1
  38. package/dest/public/contracts_db_checkpoint.d.ts +2 -2
  39. package/dest/public/contracts_db_checkpoint.d.ts.map +1 -1
  40. package/dest/public/contracts_db_checkpoint.js +1 -1
  41. package/dest/public/executor_metrics.d.ts +1 -1
  42. package/dest/public/executor_metrics.d.ts.map +1 -1
  43. package/dest/public/executor_metrics.js +7 -2
  44. package/dest/public/fixtures/amm_test.js +2 -2
  45. package/dest/public/fixtures/bulk_test.d.ts +1 -1
  46. package/dest/public/fixtures/bulk_test.d.ts.map +1 -1
  47. package/dest/public/fixtures/bulk_test.js +4 -4
  48. package/dest/public/fixtures/opcode_spammer.d.ts +3 -4
  49. package/dest/public/fixtures/opcode_spammer.d.ts.map +1 -1
  50. package/dest/public/fixtures/opcode_spammer.js +16 -56
  51. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +6 -5
  52. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  53. package/dest/public/fixtures/public_tx_simulation_tester.js +37 -10
  54. package/dest/public/fixtures/utils.d.ts +2 -2
  55. package/dest/public/fixtures/utils.d.ts.map +1 -1
  56. package/dest/public/fixtures/utils.js +10 -10
  57. package/dest/public/hinting_db_sources.d.ts +5 -5
  58. package/dest/public/hinting_db_sources.d.ts.map +1 -1
  59. package/dest/public/hinting_db_sources.js +7 -6
  60. package/dest/public/public_db_sources.d.ts +6 -5
  61. package/dest/public/public_db_sources.d.ts.map +1 -1
  62. package/dest/public/public_db_sources.js +14 -14
  63. package/dest/public/public_processor/guarded_merkle_tree.d.ts +5 -5
  64. package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -1
  65. package/dest/public/public_processor/guarded_merkle_tree.js +5 -5
  66. package/dest/public/public_processor/public_processor.d.ts +8 -4
  67. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  68. package/dest/public/public_processor/public_processor.js +60 -47
  69. package/dest/public/public_processor/public_processor_metrics.d.ts +2 -2
  70. package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -1
  71. package/dest/public/public_processor/public_processor_metrics.js +20 -4
  72. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +3 -2
  73. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts.map +1 -1
  74. package/dest/public/public_tx_simulator/contract_provider_for_cpp.js +4 -3
  75. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +5 -5
  76. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts.map +1 -1
  77. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.js +10 -11
  78. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts +4 -4
  79. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts.map +1 -1
  80. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.js +7 -7
  81. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts +4 -4
  82. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts.map +1 -1
  83. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.js +6 -6
  84. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts +3 -2
  85. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts.map +1 -1
  86. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.js +2 -2
  87. package/dest/public/public_tx_simulator/factories.d.ts +3 -2
  88. package/dest/public/public_tx_simulator/factories.d.ts.map +1 -1
  89. package/dest/public/public_tx_simulator/factories.js +4 -4
  90. package/dest/public/public_tx_simulator/public_tx_context.d.ts +10 -5
  91. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  92. package/dest/public/public_tx_simulator/public_tx_context.js +16 -18
  93. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +9 -4
  94. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  95. package/dest/public/public_tx_simulator/public_tx_simulator.js +19 -12
  96. package/dest/public/side_effect_trace.d.ts +4 -4
  97. package/dest/public/side_effect_trace.d.ts.map +1 -1
  98. package/dest/public/side_effect_trace.js +3 -3
  99. package/dest/public/state_manager/state_manager.d.ts +10 -4
  100. package/dest/public/state_manager/state_manager.d.ts.map +1 -1
  101. package/dest/public/state_manager/state_manager.js +12 -5
  102. package/dest/public/test_executor_metrics.d.ts +8 -2
  103. package/dest/public/test_executor_metrics.d.ts.map +1 -1
  104. package/dest/public/test_executor_metrics.js +24 -2
  105. package/package.json +15 -16
  106. package/src/private/acvm/acvm.ts +4 -3
  107. package/src/private/acvm_native.ts +11 -5
  108. package/src/private/acvm_wasm.ts +7 -3
  109. package/src/private/circuit_recording/circuit_recorder.ts +7 -5
  110. package/src/private/circuit_recording/file_circuit_recorder.ts +7 -2
  111. package/src/private/circuit_recording/memory_circuit_recorder.ts +6 -4
  112. package/src/private/factory.ts +7 -4
  113. package/src/public/avm/avm_gas.ts +2 -2
  114. package/src/public/avm/avm_simulator.ts +1 -1
  115. package/src/public/avm/calldata.ts +3 -2
  116. package/src/public/avm/fixtures/account_proof.json +553 -0
  117. package/src/public/avm/fixtures/account_proof_fetcher.ts +166 -0
  118. package/src/public/avm/opcodes/accrued_substate.ts +10 -19
  119. package/src/public/avm/opcodes/external_calls.ts +1 -1
  120. package/src/public/avm/serialization/bytecode_serialization.ts +2 -2
  121. package/src/public/avm/serialization/instruction_serialization.ts +1 -1
  122. package/src/public/contracts_db_checkpoint.ts +1 -1
  123. package/src/public/executor_metrics.ts +4 -1
  124. package/src/public/fixtures/amm_test.ts +2 -2
  125. package/src/public/fixtures/bulk_test.ts +2 -4
  126. package/src/public/fixtures/opcode_spammer.ts +18 -55
  127. package/src/public/fixtures/public_tx_simulation_tester.ts +47 -6
  128. package/src/public/fixtures/utils.ts +17 -12
  129. package/src/public/fuzzing/avm_fuzzer_simulator.ts +1 -1
  130. package/src/public/hinting_db_sources.ts +9 -7
  131. package/src/public/public_db_sources.ts +27 -19
  132. package/src/public/public_processor/guarded_merkle_tree.ts +6 -6
  133. package/src/public/public_processor/public_processor.ts +86 -60
  134. package/src/public/public_processor/public_processor_metrics.ts +10 -4
  135. package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +8 -4
  136. package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +10 -8
  137. package/src/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.ts +7 -5
  138. package/src/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.ts +7 -5
  139. package/src/public/public_tx_simulator/dumping_cpp_public_tx_simulator.ts +3 -1
  140. package/src/public/public_tx_simulator/factories.ts +6 -3
  141. package/src/public/public_tx_simulator/public_tx_context.ts +21 -16
  142. package/src/public/public_tx_simulator/public_tx_simulator.ts +20 -12
  143. package/src/public/side_effect_trace.ts +5 -2
  144. package/src/public/state_manager/state_manager.ts +27 -4
  145. package/src/public/test_executor_metrics.ts +27 -3
@@ -2,10 +2,8 @@ import {
2
2
  CONTRACT_CLASS_PUBLISHED_MAGIC_VALUE,
3
3
  CONTRACT_CLASS_REGISTRY_CONTRACT_ADDRESS,
4
4
  CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS,
5
- DEFAULT_DA_GAS_LIMIT,
6
- DEFAULT_L2_GAS_LIMIT,
7
- DEFAULT_TEARDOWN_DA_GAS_LIMIT,
8
- DEFAULT_TEARDOWN_L2_GAS_LIMIT,
5
+ MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT,
6
+ MAX_PROCESSABLE_L2_GAS,
9
7
  PRIVATE_LOG_SIZE_IN_FIELDS,
10
8
  } from '@aztec/constants';
11
9
  import { padArrayEnd } from '@aztec/foundation/collection';
@@ -14,7 +12,13 @@ import { CONTRACT_INSTANCE_PUBLISHED_EVENT_TAG } from '@aztec/protocol-contracts
14
12
  import { bufferAsFields } from '@aztec/stdlib/abi';
15
13
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
16
14
  import type { ContractClassPublic, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
17
- import { Gas, GasFees, GasSettings } from '@aztec/stdlib/gas';
15
+ import {
16
+ FALLBACK_TEARDOWN_DA_GAS_LIMIT,
17
+ FALLBACK_TEARDOWN_L2_GAS_LIMIT,
18
+ Gas,
19
+ GasFees,
20
+ GasSettings,
21
+ } from '@aztec/stdlib/gas';
18
22
  import { siloNullifier } from '@aztec/stdlib/hash';
19
23
  import {
20
24
  LogHash,
@@ -62,13 +66,14 @@ export async function createTxForPublicCalls(
62
66
  feePayer = AztecAddress.zero(),
63
67
  gasUsedByPrivate: Gas = Gas.empty(),
64
68
  globals: GlobalVariables = GlobalVariables.empty(),
69
+ gasLimits?: Gas,
65
70
  ): Promise<Tx> {
66
71
  assert(
67
72
  setupCallRequests.length > 0 || appCallRequests.length > 0 || teardownCallRequest !== undefined,
68
73
  "Can't create public tx with no enqueued calls",
69
74
  );
70
75
  // use max limits
71
- const gasLimits = new Gas(DEFAULT_DA_GAS_LIMIT, DEFAULT_L2_GAS_LIMIT);
76
+ gasLimits = gasLimits ?? new Gas(MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT, MAX_PROCESSABLE_L2_GAS);
72
77
 
73
78
  const forPublic = PartialPrivateTailPublicInputsForPublic.empty();
74
79
 
@@ -128,19 +133,19 @@ export async function createTxForPublicCalls(
128
133
 
129
134
  const maxFeesPerGas = feePayer.isZero() ? GasFees.empty() : new GasFees(10, 10);
130
135
  const teardownGasLimits = teardownCallRequest
131
- ? new Gas(DEFAULT_TEARDOWN_DA_GAS_LIMIT, DEFAULT_TEARDOWN_L2_GAS_LIMIT)
136
+ ? new Gas(FALLBACK_TEARDOWN_DA_GAS_LIMIT, FALLBACK_TEARDOWN_L2_GAS_LIMIT)
132
137
  : Gas.empty();
133
138
  const gasSettings = new GasSettings(gasLimits, teardownGasLimits, maxFeesPerGas, GasFees.empty());
134
139
  const txContext = new TxContext(Fr.zero(), Fr.zero(), gasSettings);
135
140
  const header = BlockHeader.empty({ globalVariables: globals });
136
141
  const constantData = new TxConstantData(header, txContext, Fr.zero(), Fr.zero());
137
- const includeByTimestamp = 0n; // Not used in the simulator.
142
+ const expirationTimestamp = 0n; // Not used in the simulator.
138
143
 
139
144
  const txData = new PrivateKernelTailCircuitPublicInputs(
140
145
  constantData,
141
146
  /*gasUsed=*/ gasUsedByPrivate,
142
147
  feePayer,
143
- includeByTimestamp,
148
+ expirationTimestamp,
144
149
  forPublic,
145
150
  );
146
151
 
@@ -163,7 +168,7 @@ export async function createTxForPrivateOnly(
163
168
  gasUsedByPrivate: Gas = new Gas(10, 10),
164
169
  ): Promise<Tx> {
165
170
  // use max limits
166
- const gasLimits = new Gas(DEFAULT_DA_GAS_LIMIT, DEFAULT_L2_GAS_LIMIT);
171
+ const gasLimits = new Gas(MAX_PROCESSABLE_DA_GAS_PER_CHECKPOINT, MAX_PROCESSABLE_L2_GAS);
167
172
 
168
173
  const forRollup = PartialPrivateTailPublicInputsForRollup.empty();
169
174
 
@@ -171,13 +176,13 @@ export async function createTxForPrivateOnly(
171
176
  const gasSettings = new GasSettings(gasLimits, Gas.empty(), maxFeesPerGas, GasFees.empty());
172
177
  const txContext = new TxContext(Fr.zero(), Fr.zero(), gasSettings);
173
178
  const constantData = new TxConstantData(BlockHeader.empty(), txContext, Fr.zero(), Fr.zero());
174
- const includeByTimestamp = 0n; // Not used in the simulator.
179
+ const expirationTimestamp = 0n; // Not used in the simulator.
175
180
 
176
181
  const txData = new PrivateKernelTailCircuitPublicInputs(
177
182
  constantData,
178
183
  /*gasUsed=*/ gasUsedByPrivate,
179
184
  feePayer,
180
- includeByTimestamp,
185
+ expirationTimestamp,
181
186
  /*forPublic=*/ undefined,
182
187
  forRollup,
183
188
  );
@@ -146,7 +146,7 @@ async function createTxFromHint(cppTx: AvmTxHint): Promise<Tx> {
146
146
  constants,
147
147
  cppTx.gasUsedByPrivate,
148
148
  cppTx.feePayer,
149
- 0n, // includeByTimestamp
149
+ 0n, // expirationTimestamp
150
150
  forPublic,
151
151
  undefined, // forRollup - not needed for public simulation
152
152
  );
@@ -410,12 +410,12 @@ export class HintingMerkleWriteOperations implements MerkleTreeWriteOperations {
410
410
  }
411
411
  }
412
412
 
413
- public async createCheckpoint(): Promise<void> {
413
+ public async createCheckpoint(): Promise<number> {
414
414
  const actionCounter = this.checkpointActionCounter++;
415
415
  const oldCheckpointId = this.getCurrentCheckpointId();
416
416
  const treesStateHash = await this.getTreesStateHash();
417
417
 
418
- await this.db.createCheckpoint();
418
+ const depth = await this.db.createCheckpoint();
419
419
  this.checkpointStack.push(this.nextCheckpointId++);
420
420
  const newCheckpointId = this.getCurrentCheckpointId();
421
421
 
@@ -424,14 +424,16 @@ export class HintingMerkleWriteOperations implements MerkleTreeWriteOperations {
424
424
  HintingMerkleWriteOperations.log.trace(
425
425
  `[createCheckpoint:${actionCounter}] Checkpoint evolved ${oldCheckpointId} -> ${newCheckpointId} at trees state ${treesStateHash}.`,
426
426
  );
427
+
428
+ return depth;
427
429
  }
428
430
 
429
- public commitAllCheckpoints(): Promise<void> {
430
- throw new Error('commitAllCheckpoints is not supported in HintingMerkleWriteOperations.');
431
+ public commitAllCheckpointsTo(_depth: number): Promise<void> {
432
+ throw new Error('commitAllCheckpointsTo is not supported in HintingMerkleWriteOperations.');
431
433
  }
432
434
 
433
- public revertAllCheckpoints(): Promise<void> {
434
- throw new Error('revertAllCheckpoints is not supported in HintingMerkleWriteOperations.');
435
+ public revertAllCheckpointsTo(_depth: number): Promise<void> {
436
+ throw new Error('revertAllCheckpointsTo is not supported in HintingMerkleWriteOperations.');
435
437
  }
436
438
 
437
439
  public async commitCheckpoint(): Promise<void> {
@@ -572,7 +574,7 @@ export class HintingMerkleWriteOperations implements MerkleTreeWriteOperations {
572
574
  return await this.db.close();
573
575
  }
574
576
 
575
- async [Symbol.dispose](): Promise<void> {
577
+ async [Symbol.asyncDispose](): Promise<void> {
576
578
  await this.close();
577
579
  }
578
580
 
@@ -5,7 +5,7 @@ import {
5
5
  PUBLIC_DATA_SUBTREE_HEIGHT,
6
6
  } from '@aztec/constants';
7
7
  import { Fr } from '@aztec/foundation/curves/bn254';
8
- import { createLogger } from '@aztec/foundation/log';
8
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
9
9
  import { Timer } from '@aztec/foundation/timer';
10
10
  import { ContractClassPublishedEvent } from '@aztec/protocol-contracts/class-registry';
11
11
  import { ContractInstancePublishedEvent } from '@aztec/protocol-contracts/instance-registry';
@@ -46,14 +46,19 @@ import { L1ToL2MessageIndexOutOfRangeError, NoteHashIndexOutOfRangeError } from
46
46
  export class PublicContractsDB implements PublicContractsDBInterface {
47
47
  private contractStateStack: ContractsDbCheckpoint[] = [new ContractsDbCheckpoint()];
48
48
 
49
- private log = createLogger('simulator:contracts-data-source');
49
+ private log: Logger;
50
50
 
51
- constructor(private dataSource: ContractDataSource) {}
51
+ constructor(
52
+ private dataSource: ContractDataSource,
53
+ bindings?: LoggerBindings,
54
+ ) {
55
+ this.log = createLogger('simulator:contracts-data-source', bindings);
56
+ }
52
57
 
53
- public async addContracts(contractDeploymentData: ContractDeploymentData): Promise<void> {
58
+ public addContracts(contractDeploymentData: ContractDeploymentData): void {
54
59
  const currentState = this.getCurrentState();
55
60
 
56
- await this.addContractClassesFromEvents(
61
+ this.addContractClassesFromEvents(
57
62
  ContractClassPublishedEvent.extractContractClassEvents(contractDeploymentData.getContractClassLogs()),
58
63
  currentState,
59
64
  );
@@ -64,10 +69,10 @@ export class PublicContractsDB implements PublicContractsDBInterface {
64
69
  );
65
70
  }
66
71
 
67
- public async addNewContracts(tx: Tx): Promise<void> {
72
+ public addNewContracts(tx: Tx): void {
68
73
  const contractDeploymentData = AllContractDeploymentData.fromTx(tx);
69
- await this.addContracts(contractDeploymentData.getNonRevertibleContractDeploymentData());
70
- await this.addContracts(contractDeploymentData.getRevertibleContractDeploymentData());
74
+ this.addContracts(contractDeploymentData.getNonRevertibleContractDeploymentData());
75
+ this.addContracts(contractDeploymentData.getRevertibleContractDeploymentData());
71
76
  }
72
77
 
73
78
  /**
@@ -76,7 +81,7 @@ export class PublicContractsDB implements PublicContractsDBInterface {
76
81
  */
77
82
  public createCheckpoint(): void {
78
83
  const currentState = this.getCurrentState();
79
- const newState = currentState.deepCopy();
84
+ const newState = currentState.fork();
80
85
  this.contractStateStack.push(newState);
81
86
  }
82
87
 
@@ -169,17 +174,15 @@ export class PublicContractsDB implements PublicContractsDBInterface {
169
174
  return await this.dataSource.getDebugFunctionName(address, selector);
170
175
  }
171
176
 
172
- private async addContractClassesFromEvents(
177
+ private addContractClassesFromEvents(
173
178
  contractClassEvents: ContractClassPublishedEvent[],
174
179
  state: ContractsDbCheckpoint,
175
180
  ) {
176
- await Promise.all(
177
- contractClassEvents.map(async (event: ContractClassPublishedEvent) => {
178
- this.log.debug(`Adding class ${event.contractClassId.toString()} to contract state`);
179
- const contractClass = await event.toContractClassPublic();
180
- state.addClass(event.contractClassId, contractClass);
181
- }),
182
- );
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
+ }
183
186
  }
184
187
 
185
188
  private addContractInstancesFromEvents(
@@ -208,9 +211,14 @@ export class PublicContractsDB implements PublicContractsDBInterface {
208
211
  * to decide whether to use hints or not (same with tracing, etc).
209
212
  */
210
213
  export class PublicTreesDB implements PublicStateDBInterface {
211
- private logger = createLogger('simulator:public-trees-db');
214
+ private logger: Logger;
212
215
 
213
- constructor(private readonly db: MerkleTreeWriteOperations) {}
216
+ constructor(
217
+ private readonly db: MerkleTreeWriteOperations,
218
+ bindings?: LoggerBindings,
219
+ ) {
220
+ this.logger = createLogger('simulator:public-trees-db', bindings);
221
+ }
214
222
 
215
223
  public async storageRead(contract: AztecAddress, slot: Fr): Promise<Fr> {
216
224
  const timer = new Timer();
@@ -82,7 +82,7 @@ export class GuardedMerkleTreeOperations implements MerkleTreeWriteOperations {
82
82
  return this.guardAndPush(() => this.target.close());
83
83
  }
84
84
 
85
- async [Symbol.dispose](): Promise<void> {
85
+ async [Symbol.asyncDispose](): Promise<void> {
86
86
  await this.close();
87
87
  }
88
88
  getTreeInfo(treeId: MerkleTreeId): Promise<TreeInfo> {
@@ -134,7 +134,7 @@ export class GuardedMerkleTreeOperations implements MerkleTreeWriteOperations {
134
134
  ): Promise<(BlockNumber | undefined)[]> {
135
135
  return this.guardAndPush(() => this.target.getBlockNumbersForLeafIndices(treeId, leafIndices));
136
136
  }
137
- createCheckpoint(): Promise<void> {
137
+ createCheckpoint(): Promise<number> {
138
138
  return this.guardAndPush(() => this.target.createCheckpoint());
139
139
  }
140
140
  commitCheckpoint(): Promise<void> {
@@ -143,11 +143,11 @@ export class GuardedMerkleTreeOperations implements MerkleTreeWriteOperations {
143
143
  revertCheckpoint(): Promise<void> {
144
144
  return this.guardAndPush(() => this.target.revertCheckpoint());
145
145
  }
146
- commitAllCheckpoints(): Promise<void> {
147
- return this.guardAndPush(() => this.target.commitAllCheckpoints());
146
+ commitAllCheckpointsTo(depth: number): Promise<void> {
147
+ return this.guardAndPush(() => this.target.commitAllCheckpointsTo(depth));
148
148
  }
149
- revertAllCheckpoints(): Promise<void> {
150
- return this.guardAndPush(() => this.target.revertAllCheckpoints());
149
+ revertAllCheckpointsTo(depth: number): Promise<void> {
150
+ return this.guardAndPush(() => this.target.revertAllCheckpointsTo(depth));
151
151
  }
152
152
  findSiblingPaths<ID extends MerkleTreeId>(
153
153
  treeId: ID,
@@ -1,7 +1,7 @@
1
1
  import { MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, NULLIFIER_SUBTREE_HEIGHT } from '@aztec/constants';
2
2
  import { padArrayEnd } from '@aztec/foundation/collection';
3
3
  import { Fr } from '@aztec/foundation/curves/bn254';
4
- import { createLogger } from '@aztec/foundation/log';
4
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
5
5
  import { sleep } from '@aztec/foundation/sleep';
6
6
  import { DateProvider, Timer, elapsed, executeTimeout } from '@aztec/foundation/timer';
7
7
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
@@ -25,6 +25,7 @@ import type {
25
25
  PublicProcessorValidator,
26
26
  SequencerConfig,
27
27
  } from '@aztec/stdlib/interfaces/server';
28
+ import { type DebugLog, type DebugLogStore, NullDebugLogStore } from '@aztec/stdlib/logs';
28
29
  import { ProvingRequestType } from '@aztec/stdlib/proofs';
29
30
  import { MerkleTreeId } from '@aztec/stdlib/trees';
30
31
  import {
@@ -62,11 +63,15 @@ import { PublicProcessorMetrics } from './public_processor_metrics.js';
62
63
  * Creates new instances of PublicProcessor given the provided merkle tree db and contract data source.
63
64
  */
64
65
  export class PublicProcessorFactory {
66
+ private log: Logger;
65
67
  constructor(
66
68
  private contractDataSource: ContractDataSource,
67
69
  private dateProvider: DateProvider = new DateProvider(),
68
70
  protected telemetryClient: TelemetryClient = getTelemetryClient(),
69
- ) {}
71
+ bindings?: LoggerBindings,
72
+ ) {
73
+ this.log = createLogger('simulator:public-processor-factory', bindings);
74
+ }
70
75
 
71
76
  /**
72
77
  * Creates a new instance of a PublicProcessor.
@@ -79,7 +84,8 @@ export class PublicProcessorFactory {
79
84
  globalVariables: GlobalVariables,
80
85
  config: PublicSimulatorConfig,
81
86
  ): PublicProcessor {
82
- const contractsDB = new PublicContractsDB(this.contractDataSource);
87
+ const bindings = this.log.getBindings();
88
+ const contractsDB = new PublicContractsDB(this.contractDataSource, bindings);
83
89
 
84
90
  const guardedFork = new GuardedMerkleTreeOperations(merkleTree);
85
91
  const publicTxSimulator = this.createPublicTxSimulator(guardedFork, contractsDB, globalVariables, config);
@@ -91,6 +97,7 @@ export class PublicProcessorFactory {
91
97
  publicTxSimulator,
92
98
  this.dateProvider,
93
99
  this.telemetryClient,
100
+ createLogger('simulator:public-processor', bindings),
94
101
  );
95
102
  }
96
103
 
@@ -100,7 +107,14 @@ export class PublicProcessorFactory {
100
107
  globalVariables: GlobalVariables,
101
108
  config?: Partial<PublicTxSimulatorConfig>,
102
109
  ): PublicTxSimulatorInterface {
103
- return new TelemetryCppPublicTxSimulator(merkleTree, contractsDB, globalVariables, this.telemetryClient, config);
110
+ return new TelemetryCppPublicTxSimulator(
111
+ merkleTree,
112
+ contractsDB,
113
+ globalVariables,
114
+ this.telemetryClient,
115
+ config,
116
+ this.log.getBindings(),
117
+ );
104
118
  }
105
119
  }
106
120
 
@@ -117,7 +131,6 @@ class PublicProcessorTimeoutError extends Error {
117
131
  */
118
132
  export class PublicProcessor implements Traceable {
119
133
  private metrics: PublicProcessorMetrics;
120
-
121
134
  constructor(
122
135
  protected globalVariables: GlobalVariables,
123
136
  private guardedMerkleTree: GuardedMerkleTreeOperations,
@@ -125,8 +138,9 @@ export class PublicProcessor implements Traceable {
125
138
  protected publicTxSimulator: PublicTxSimulatorInterface,
126
139
  private dateProvider: DateProvider,
127
140
  telemetryClient: TelemetryClient = getTelemetryClient(),
128
- private log = createLogger('simulator:public-processor'),
141
+ private log: Logger,
129
142
  private opts: Pick<SequencerConfig, 'fakeProcessingDelayPerTxMs' | 'fakeThrowAfterProcessingTxCount'> = {},
143
+ private debugLogStore: DebugLogStore = new NullDebugLogStore(),
130
144
  ) {
131
145
  this.metrics = new PublicProcessorMetrics(telemetryClient, 'PublicProcessor');
132
146
  }
@@ -146,12 +160,13 @@ export class PublicProcessor implements Traceable {
146
160
  txs: Iterable<Tx> | AsyncIterable<Tx>,
147
161
  limits: PublicProcessorLimits = {},
148
162
  validator: PublicProcessorValidator = {},
149
- ): Promise<[ProcessedTx[], FailedTx[], Tx[], NestedProcessReturnValues[], number]> {
150
- const { maxTransactions, maxBlockSize, deadline, maxBlockGas, maxBlobFields } = limits;
163
+ ): Promise<[ProcessedTx[], FailedTx[], Tx[], NestedProcessReturnValues[], DebugLog[]]> {
164
+ const { maxTransactions, deadline, maxBlockGas, maxBlobFields, isBuildingProposal } = limits;
151
165
  const { preprocessValidator, nullifierCache } = validator;
152
166
  const result: ProcessedTx[] = [];
153
167
  const usedTxs: Tx[] = [];
154
168
  const failed: FailedTx[] = [];
169
+ const debugLogs: DebugLog[] = [];
155
170
  const timer = new Timer();
156
171
 
157
172
  let totalSizeInBytes = 0;
@@ -173,22 +188,23 @@ export class PublicProcessor implements Traceable {
173
188
  break;
174
189
  }
175
190
 
176
- // Skip this tx if it'd exceed max block size
177
191
  const txHash = tx.getTxHash().toString();
178
- const preTxSizeInBytes = tx.getEstimatedPrivateTxEffectsSize();
179
- if (maxBlockSize !== undefined && totalSizeInBytes + preTxSizeInBytes > maxBlockSize) {
180
- this.log.warn(`Skipping processing of tx ${txHash} sized ${preTxSizeInBytes} bytes due to block size limit`, {
181
- txHash,
182
- sizeInBytes: preTxSizeInBytes,
183
- totalSizeInBytes,
184
- maxBlockSize,
185
- });
192
+
193
+ // Skip this tx if its estimated blob fields would exceed the limit.
194
+ // Only done during proposal building: during re-execution we must process the exact txs from the proposal.
195
+ const txBlobFields = tx.getPrivateTxEffectsSizeInFields();
196
+ if (isBuildingProposal && maxBlobFields !== undefined && totalBlobFields + txBlobFields > maxBlobFields) {
197
+ this.log.warn(
198
+ `Skipping tx ${txHash} with ${txBlobFields} fields from private side effects due to blob fields limit`,
199
+ { txHash, txBlobFields, totalBlobFields, maxBlobFields },
200
+ );
186
201
  continue;
187
202
  }
188
203
 
189
- // Skip this tx if its gas limit would exceed the block gas limit
204
+ // Skip this tx if its gas limit would exceed the block gas limit (either da or l2).
205
+ // Only done during proposal building: during re-execution we must process the exact txs from the proposal.
190
206
  const txGasLimit = tx.data.constants.txContext.gasSettings.gasLimits;
191
- if (maxBlockGas !== undefined && totalBlockGas.add(txGasLimit).gtAny(maxBlockGas)) {
207
+ if (isBuildingProposal && maxBlockGas !== undefined && totalBlockGas.add(txGasLimit).gtAny(maxBlockGas)) {
192
208
  this.log.warn(`Skipping processing of tx ${txHash} due to block gas limit`, {
193
209
  txHash,
194
210
  txGasLimit,
@@ -228,7 +244,7 @@ export class PublicProcessor implements Traceable {
228
244
  this.contractsDB.createCheckpoint();
229
245
 
230
246
  try {
231
- const [processedTx, returnValues] = await this.processTx(tx, deadline);
247
+ const [processedTx, returnValues, txDebugLogs] = await this.processTx(tx, deadline);
232
248
 
233
249
  // Inject a fake processing failure after N txs if requested
234
250
  const fakeThrowAfter = this.opts.fakeThrowAfterProcessingTxCount;
@@ -237,23 +253,9 @@ export class PublicProcessor implements Traceable {
237
253
  }
238
254
 
239
255
  const txBlobFields = processedTx.txEffect.getNumBlobFields();
240
-
241
- // If the actual size of this tx would exceed block size, skip it
242
256
  const txSize = txBlobFields * Fr.SIZE_IN_BYTES;
243
- if (maxBlockSize !== undefined && totalSizeInBytes + txSize > maxBlockSize) {
244
- this.log.debug(`Skipping processed tx ${txHash} sized ${txSize} due to max block size.`, {
245
- txHash,
246
- sizeInBytes: txSize,
247
- totalSizeInBytes,
248
- maxBlockSize,
249
- });
250
- // Need to revert the checkpoint here and don't go any further
251
- await checkpoint.revert();
252
- this.contractsDB.revertCheckpoint();
253
- continue;
254
- }
255
257
 
256
- // If the actual blob fields of this tx would exceed the limit, skip it
258
+ // If the actual blob fields of this tx would exceed the limit, skip it.
257
259
  // Note: maxBlobFields already accounts for block end blob fields and previous blocks in checkpoint.
258
260
  if (maxBlobFields !== undefined && totalBlobFields + txBlobFields > maxBlobFields) {
259
261
  this.log.debug(
@@ -271,17 +273,42 @@ export class PublicProcessor implements Traceable {
271
273
  continue;
272
274
  }
273
275
 
276
+ // During re-execution, check if the actual gas used by this tx would push the block over the gas limit.
277
+ // Unlike the proposal-building check (which uses declared gas limits pessimistically before processing),
278
+ // this uses actual gas and stops processing when the limit is exceeded.
279
+ if (
280
+ !isBuildingProposal &&
281
+ maxBlockGas !== undefined &&
282
+ totalBlockGas.add(processedTx.gasUsed.totalGas).gtAny(maxBlockGas)
283
+ ) {
284
+ this.log.warn(`Stopping re-execution since tx ${txHash} would push block gas over limit`, {
285
+ txHash,
286
+ txGas: processedTx.gasUsed.totalGas,
287
+ totalBlockGas,
288
+ maxBlockGas,
289
+ });
290
+ await checkpoint.revert();
291
+ this.contractsDB.revertCheckpoint();
292
+ break;
293
+ }
294
+
274
295
  // FIXME(fcarreiro): it's ugly to have to notify the validator of nullifiers.
275
296
  // I'd rather pass the validators the processedTx as well and let them deal with it.
276
297
  nullifierCache?.addNullifiers(processedTx.txEffect.nullifiers.map(n => n.toBuffer()));
277
298
  result.push(processedTx);
278
299
  usedTxs.push(tx);
279
300
  returns = returns.concat(returnValues);
301
+ debugLogs.push(...txDebugLogs);
302
+
303
+ this.debugLogStore.storeLogs(processedTx.hash.toString(), txDebugLogs);
280
304
 
281
305
  totalPublicGas = totalPublicGas.add(processedTx.gasUsed.publicGas);
282
306
  totalBlockGas = totalBlockGas.add(processedTx.gasUsed.totalGas);
283
307
  totalSizeInBytes += txSize;
284
308
  totalBlobFields += txBlobFields;
309
+
310
+ // Commit the tx-level contracts checkpoint on success
311
+ this.contractsDB.commitCheckpoint();
285
312
  } catch (err: any) {
286
313
  if (err?.name === 'PublicProcessorTimeoutError') {
287
314
  this.log.warn(`Stopping tx processing due to timeout.`);
@@ -301,14 +328,10 @@ export class PublicProcessor implements Traceable {
301
328
  // 1. At least one outstanding checkpoint that has not been committed (the one created before we processed the tx).
302
329
  // 2. Possible state updates on that checkpoint or any others created during execution.
303
330
 
304
- // First we revert a checkpoint as managed by the ForkCheckpoint. This will revert whatever is the current checkpoint
305
- // which may not be the one originally created by this object. But that is ok, we do this to fulfil the ForkCheckpoint
306
- // lifecycle expectations and ensure it doesn't attempt to commit later on.
307
- await checkpoint.revert();
308
-
309
- // Now we want to revert any/all remaining checkpoints, destroying any outstanding state updates.
310
- // This needs to be done directly on the underlying fork as the guarded fork has been stopped.
311
- await this.guardedMerkleTree.getUnderlyingFork().revertAllCheckpoints();
331
+ // Revert all checkpoints at or above this checkpoint's depth (inclusive), destroying any outstanding state
332
+ // updates from this tx and any nested checkpoints created during execution. This preserves any checkpoints
333
+ // created by callers below our depth.
334
+ await checkpoint.revertToCheckpoint();
312
335
 
313
336
  // Revert any contracts added to the DB for the tx.
314
337
  this.contractsDB.revertCheckpoint();
@@ -320,9 +343,9 @@ export class PublicProcessor implements Traceable {
320
343
  break;
321
344
  }
322
345
 
323
- // Roll back state to start of TX before proceeding to next TX
324
- await checkpoint.revert();
325
- await this.guardedMerkleTree.getUnderlyingFork().revertAllCheckpoints();
346
+ // Roll back state to start of TX before proceeding to next TX.
347
+ // Reverts all checkpoints at or above this checkpoint's depth, preserving any caller checkpoints below.
348
+ await checkpoint.revertToCheckpoint();
326
349
  this.contractsDB.revertCheckpoint();
327
350
  const errorMessage = err instanceof Error || err instanceof AssertionError ? err.message : 'Unknown error';
328
351
  this.log.warn(`Failed to process tx ${txHash.toString()}: ${errorMessage} ${err?.stack}`);
@@ -334,7 +357,6 @@ export class PublicProcessor implements Traceable {
334
357
  } finally {
335
358
  // Base case is we always commit the checkpoint. Using the ForkCheckpoint means this has no effect if the tx was previously reverted
336
359
  await checkpoint.commit();
337
- this.contractsDB.commitCheckpointOkIfNone();
338
360
  }
339
361
  }
340
362
 
@@ -350,7 +372,7 @@ export class PublicProcessor implements Traceable {
350
372
  totalSizeInBytes,
351
373
  });
352
374
 
353
- return [result, failed, usedTxs, returns, totalBlobFields];
375
+ return [result, failed, usedTxs, returns, debugLogs];
354
376
  }
355
377
 
356
378
  private async checkWorldStateUnchanged(
@@ -370,8 +392,13 @@ export class PublicProcessor implements Traceable {
370
392
  }
371
393
 
372
394
  @trackSpan('PublicProcessor.processTx', tx => ({ [Attributes.TX_HASH]: tx.getTxHash().toString() }))
373
- private async processTx(tx: Tx, deadline: Date | undefined): Promise<[ProcessedTx, NestedProcessReturnValues[]]> {
374
- const [time, [processedTx, returnValues]] = await elapsed(() => this.processTxWithinDeadline(tx, deadline));
395
+ private async processTx(
396
+ tx: Tx,
397
+ deadline: Date | undefined,
398
+ ): Promise<[ProcessedTx, NestedProcessReturnValues[], DebugLog[]]> {
399
+ const [time, [processedTx, returnValues, debugLogs]] = await elapsed(() =>
400
+ this.processTxWithinDeadline(tx, deadline),
401
+ );
375
402
 
376
403
  this.log.verbose(
377
404
  !tx.hasPublicCalls()
@@ -394,7 +421,7 @@ export class PublicProcessor implements Traceable {
394
421
  },
395
422
  );
396
423
 
397
- return [processedTx, returnValues ?? []];
424
+ return [processedTx, returnValues ?? [], debugLogs];
398
425
  }
399
426
 
400
427
  private async doTreeInsertionsForPrivateOnlyTx(processedTx: ProcessedTx): Promise<void> {
@@ -428,10 +455,9 @@ export class PublicProcessor implements Traceable {
428
455
  private async processTxWithinDeadline(
429
456
  tx: Tx,
430
457
  deadline: Date | undefined,
431
- ): Promise<[ProcessedTx, NestedProcessReturnValues[] | undefined]> {
432
- const innerProcessFn: () => Promise<[ProcessedTx, NestedProcessReturnValues[] | undefined]> = tx.hasPublicCalls()
433
- ? () => this.processTxWithPublicCalls(tx)
434
- : () => this.processPrivateOnlyTx(tx);
458
+ ): Promise<[ProcessedTx, NestedProcessReturnValues[] | undefined, DebugLog[]]> {
459
+ const innerProcessFn: () => Promise<[ProcessedTx, NestedProcessReturnValues[] | undefined, DebugLog[]]> =
460
+ tx.hasPublicCalls() ? () => this.processTxWithPublicCalls(tx) : () => this.processPrivateOnlyTx(tx);
435
461
 
436
462
  // Fake a delay per tx if instructed (used for tests)
437
463
  const fakeDelayPerTxMs = this.opts.fakeProcessingDelayPerTxMs;
@@ -499,7 +525,7 @@ export class PublicProcessor implements Traceable {
499
525
  @trackSpan('PublicProcessor.processPrivateOnlyTx', (tx: Tx) => ({
500
526
  [Attributes.TX_HASH]: tx.getTxHash().toString(),
501
527
  }))
502
- private async processPrivateOnlyTx(tx: Tx): Promise<[ProcessedTx, undefined]> {
528
+ private async processPrivateOnlyTx(tx: Tx): Promise<[ProcessedTx, undefined, DebugLog[]]> {
503
529
  const gasFees = this.globalVariables.gasFees;
504
530
  const transactionFee = computeTransactionFee(gasFees, tx.data.constants.txContext.gasSettings, tx.data.gasUsed);
505
531
 
@@ -522,15 +548,15 @@ export class PublicProcessor implements Traceable {
522
548
  // Fee payment insertion has already been done. Do the rest.
523
549
  await this.doTreeInsertionsForPrivateOnlyTx(processedTx);
524
550
 
525
- await this.contractsDB.addNewContracts(tx);
551
+ this.contractsDB.addNewContracts(tx);
526
552
 
527
- return [processedTx, undefined];
553
+ return [processedTx, undefined, []];
528
554
  }
529
555
 
530
556
  @trackSpan('PublicProcessor.processTxWithPublicCalls', tx => ({
531
557
  [Attributes.TX_HASH]: tx.getTxHash().toString(),
532
558
  }))
533
- private async processTxWithPublicCalls(tx: Tx): Promise<[ProcessedTx, NestedProcessReturnValues[]]> {
559
+ private async processTxWithPublicCalls(tx: Tx): Promise<[ProcessedTx, NestedProcessReturnValues[], DebugLog[]]> {
534
560
  const timer = new Timer();
535
561
 
536
562
  const result = await this.publicTxSimulator.simulate(tx);
@@ -568,7 +594,7 @@ export class PublicProcessor implements Traceable {
568
594
  revertReason,
569
595
  );
570
596
 
571
- return [processedTx, appLogicReturnValues];
597
+ return [processedTx, appLogicReturnValues, result.logs ?? []];
572
598
  }
573
599
 
574
600
  /**
@@ -1,6 +1,6 @@
1
1
  import type { ContractClassPublishedEvent } from '@aztec/protocol-contracts/class-registry';
2
2
  import type { Gas } from '@aztec/stdlib/gas';
3
- import type { TxExecutionPhase } from '@aztec/stdlib/tx';
3
+ import { TxExecutionPhase } from '@aztec/stdlib/tx';
4
4
  import {
5
5
  Attributes,
6
6
  type Gauge,
@@ -9,6 +9,7 @@ import {
9
9
  type TelemetryClient,
10
10
  type Tracer,
11
11
  type UpDownCounter,
12
+ createUpDownCounterWithDefault,
12
13
  } from '@aztec/telemetry-client';
13
14
 
14
15
  export class PublicProcessorMetrics {
@@ -35,13 +36,18 @@ export class PublicProcessorMetrics {
35
36
 
36
37
  this.txDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TX_DURATION);
37
38
 
38
- this.txCount = meter.createUpDownCounter(Metrics.PUBLIC_PROCESSOR_TX_COUNT);
39
+ this.txCount = createUpDownCounterWithDefault(meter, Metrics.PUBLIC_PROCESSOR_TX_COUNT, {
40
+ [Attributes.OK]: [true, false],
41
+ });
39
42
 
40
- this.txPhaseCount = meter.createUpDownCounter(Metrics.PUBLIC_PROCESSOR_TX_PHASE_COUNT);
43
+ this.txPhaseCount = createUpDownCounterWithDefault(meter, Metrics.PUBLIC_PROCESSOR_TX_PHASE_COUNT);
41
44
 
42
45
  this.phaseDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_PHASE_DURATION);
43
46
 
44
- this.phaseCount = meter.createUpDownCounter(Metrics.PUBLIC_PROCESSOR_PHASE_COUNT);
47
+ this.phaseCount = createUpDownCounterWithDefault(meter, Metrics.PUBLIC_PROCESSOR_PHASE_COUNT, {
48
+ [Attributes.TX_PHASE_NAME]: [TxExecutionPhase.SETUP, TxExecutionPhase.APP_LOGIC, TxExecutionPhase.TEARDOWN],
49
+ [Attributes.OK]: [true, false],
50
+ });
45
51
 
46
52
  this.bytecodeDeployed = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_DEPLOY_BYTECODE_SIZE);
47
53