@aztec/simulator 3.0.3 → 3.9.9-nightly.20260312

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 (256) hide show
  1. package/README.md +6 -4
  2. package/dest/common/errors.d.ts +7 -1
  3. package/dest/common/errors.d.ts.map +1 -1
  4. package/dest/private/acvm/acvm.d.ts +4 -2
  5. package/dest/private/acvm/acvm.d.ts.map +1 -1
  6. package/dest/private/acvm/acvm.js +4 -3
  7. package/dest/private/acvm_native.d.ts +5 -3
  8. package/dest/private/acvm_native.d.ts.map +1 -1
  9. package/dest/private/acvm_native.js +8 -6
  10. package/dest/private/acvm_wasm.d.ts +4 -3
  11. package/dest/private/acvm_wasm.d.ts.map +1 -1
  12. package/dest/private/acvm_wasm.js +4 -4
  13. package/dest/private/circuit_recording/circuit_recorder.d.ts +4 -3
  14. package/dest/private/circuit_recording/circuit_recorder.d.ts.map +1 -1
  15. package/dest/private/circuit_recording/circuit_recorder.js +20 -17
  16. package/dest/private/circuit_recording/file_circuit_recorder.d.ts +3 -2
  17. package/dest/private/circuit_recording/file_circuit_recorder.d.ts.map +1 -1
  18. package/dest/private/circuit_recording/file_circuit_recorder.js +2 -2
  19. package/dest/private/circuit_recording/memory_circuit_recorder.d.ts +7 -2
  20. package/dest/private/circuit_recording/memory_circuit_recorder.d.ts.map +1 -1
  21. package/dest/private/circuit_recording/memory_circuit_recorder.js +4 -4
  22. package/dest/private/factory.d.ts +3 -3
  23. package/dest/private/factory.d.ts.map +1 -1
  24. package/dest/private/factory.js +7 -4
  25. package/dest/public/avm/avm_context.d.ts +3 -3
  26. package/dest/public/avm/avm_context.d.ts.map +1 -1
  27. package/dest/public/avm/avm_contract_call_result.d.ts +6 -6
  28. package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -1
  29. package/dest/public/avm/avm_contract_call_result.js +3 -3
  30. package/dest/public/avm/avm_execution_environment.d.ts +6 -5
  31. package/dest/public/avm/avm_execution_environment.d.ts.map +1 -1
  32. package/dest/public/avm/avm_gas.js +3 -3
  33. package/dest/public/avm/avm_machine_state.d.ts +6 -5
  34. package/dest/public/avm/avm_machine_state.d.ts.map +1 -1
  35. package/dest/public/avm/avm_machine_state.js +3 -2
  36. package/dest/public/avm/avm_memory_types.d.ts +1 -1
  37. package/dest/public/avm/avm_memory_types.d.ts.map +1 -1
  38. package/dest/public/avm/avm_memory_types.js +3 -0
  39. package/dest/public/avm/avm_simulator.d.ts +3 -2
  40. package/dest/public/avm/avm_simulator.d.ts.map +1 -1
  41. package/dest/public/avm/avm_simulator.js +5 -4
  42. package/dest/public/avm/calldata.d.ts +51 -0
  43. package/dest/public/avm/calldata.d.ts.map +1 -0
  44. package/dest/public/avm/calldata.js +63 -0
  45. package/dest/public/avm/fixtures/account_proof_fetcher.d.ts +2 -0
  46. package/dest/public/avm/fixtures/account_proof_fetcher.d.ts.map +1 -0
  47. package/dest/public/avm/fixtures/account_proof_fetcher.js +152 -0
  48. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts +1 -1
  49. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -1
  50. package/dest/public/avm/fixtures/avm_simulation_tester.js +3 -2
  51. package/dest/public/avm/fixtures/initializers.d.ts +1 -1
  52. package/dest/public/avm/fixtures/initializers.d.ts.map +1 -1
  53. package/dest/public/avm/fixtures/initializers.js +2 -1
  54. package/dest/public/avm/opcodes/accrued_substate.d.ts +17 -18
  55. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
  56. package/dest/public/avm/opcodes/accrued_substate.js +42 -43
  57. package/dest/public/avm/opcodes/arithmetic.d.ts +8 -8
  58. package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -1
  59. package/dest/public/avm/opcodes/arithmetic.js +1 -1
  60. package/dest/public/avm/opcodes/bitwise.d.ts +7 -7
  61. package/dest/public/avm/opcodes/bitwise.d.ts.map +1 -1
  62. package/dest/public/avm/opcodes/bitwise.js +5 -5
  63. package/dest/public/avm/opcodes/comparators.d.ts +4 -4
  64. package/dest/public/avm/opcodes/comparators.d.ts.map +1 -1
  65. package/dest/public/avm/opcodes/comparators.js +1 -1
  66. package/dest/public/avm/opcodes/contract.d.ts +3 -3
  67. package/dest/public/avm/opcodes/contract.d.ts.map +1 -1
  68. package/dest/public/avm/opcodes/contract.js +8 -8
  69. package/dest/public/avm/opcodes/control_flow.d.ts +3 -3
  70. package/dest/public/avm/opcodes/control_flow.d.ts.map +1 -1
  71. package/dest/public/avm/opcodes/control_flow.js +4 -4
  72. package/dest/public/avm/opcodes/conversion.d.ts +3 -3
  73. package/dest/public/avm/opcodes/conversion.d.ts.map +1 -1
  74. package/dest/public/avm/opcodes/conversion.js +4 -4
  75. package/dest/public/avm/opcodes/ec_add.d.ts +4 -4
  76. package/dest/public/avm/opcodes/ec_add.d.ts.map +1 -1
  77. package/dest/public/avm/opcodes/ec_add.js +15 -6
  78. package/dest/public/avm/opcodes/environment_getters.d.ts +5 -5
  79. package/dest/public/avm/opcodes/environment_getters.d.ts.map +1 -1
  80. package/dest/public/avm/opcodes/environment_getters.js +6 -6
  81. package/dest/public/avm/opcodes/external_calls.d.ts +9 -9
  82. package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -1
  83. package/dest/public/avm/opcodes/external_calls.js +23 -22
  84. package/dest/public/avm/opcodes/hashing.d.ts +8 -8
  85. package/dest/public/avm/opcodes/hashing.d.ts.map +1 -1
  86. package/dest/public/avm/opcodes/hashing.js +18 -15
  87. package/dest/public/avm/opcodes/instruction_impl.d.ts +4 -4
  88. package/dest/public/avm/opcodes/instruction_impl.d.ts.map +1 -1
  89. package/dest/public/avm/opcodes/instruction_impl.js +4 -4
  90. package/dest/public/avm/opcodes/memory.d.ts +14 -14
  91. package/dest/public/avm/opcodes/memory.d.ts.map +1 -1
  92. package/dest/public/avm/opcodes/memory.js +25 -25
  93. package/dest/public/avm/opcodes/misc.d.ts +3 -3
  94. package/dest/public/avm/opcodes/misc.d.ts.map +1 -1
  95. package/dest/public/avm/opcodes/misc.js +4 -4
  96. package/dest/public/avm/opcodes/storage.d.ts +16 -15
  97. package/dest/public/avm/opcodes/storage.d.ts.map +1 -1
  98. package/dest/public/avm/opcodes/storage.js +34 -24
  99. package/dest/public/avm/serialization/bytecode_serialization.js +3 -3
  100. package/dest/public/avm/serialization/instruction_serialization.d.ts +2 -2
  101. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -1
  102. package/dest/public/avm/serialization/instruction_serialization.js +1 -1
  103. package/dest/public/debug_fn_name.d.ts +4 -4
  104. package/dest/public/debug_fn_name.d.ts.map +1 -1
  105. package/dest/public/debug_fn_name.js +14 -5
  106. package/dest/public/executor_metrics.d.ts +1 -1
  107. package/dest/public/executor_metrics.d.ts.map +1 -1
  108. package/dest/public/executor_metrics.js +12 -33
  109. package/dest/public/fixtures/amm_test.js +2 -2
  110. package/dest/public/fixtures/bulk_test.d.ts +3 -3
  111. package/dest/public/fixtures/bulk_test.d.ts.map +1 -1
  112. package/dest/public/fixtures/bulk_test.js +4 -68
  113. package/dest/public/fixtures/custom_bytecode_tester.d.ts +28 -6
  114. package/dest/public/fixtures/custom_bytecode_tester.d.ts.map +1 -1
  115. package/dest/public/fixtures/custom_bytecode_tester.js +36 -12
  116. package/dest/public/fixtures/custom_bytecode_tests.d.ts +11 -9
  117. package/dest/public/fixtures/custom_bytecode_tests.d.ts.map +1 -1
  118. package/dest/public/fixtures/custom_bytecode_tests.js +66 -22
  119. package/dest/public/fixtures/index.d.ts +4 -2
  120. package/dest/public/fixtures/index.d.ts.map +1 -1
  121. package/dest/public/fixtures/index.js +3 -1
  122. package/dest/public/fixtures/minimal_public_tx.d.ts +2 -7
  123. package/dest/public/fixtures/minimal_public_tx.d.ts.map +1 -1
  124. package/dest/public/fixtures/minimal_public_tx.js +4 -14
  125. package/dest/public/fixtures/opcode_spammer.d.ts +122 -0
  126. package/dest/public/fixtures/opcode_spammer.d.ts.map +1 -0
  127. package/dest/public/fixtures/opcode_spammer.js +1653 -0
  128. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +20 -6
  129. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  130. package/dest/public/fixtures/public_tx_simulation_tester.js +43 -16
  131. package/dest/public/fixtures/utils.d.ts +2 -2
  132. package/dest/public/fixtures/utils.d.ts.map +1 -1
  133. package/dest/public/fixtures/utils.js +9 -8
  134. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts +21 -7
  135. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts.map +1 -1
  136. package/dest/public/fuzzing/avm_fuzzer_simulator.js +46 -11
  137. package/dest/public/fuzzing/avm_simulator_bin.js +47 -13
  138. package/dest/public/hinting_db_sources.d.ts +2 -1
  139. package/dest/public/hinting_db_sources.d.ts.map +1 -1
  140. package/dest/public/hinting_db_sources.js +5 -0
  141. package/dest/public/public_db_sources.d.ts +4 -3
  142. package/dest/public/public_db_sources.d.ts.map +1 -1
  143. package/dest/public/public_db_sources.js +4 -4
  144. package/dest/public/public_processor/guarded_merkle_tree.d.ts +2 -1
  145. package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -1
  146. package/dest/public/public_processor/guarded_merkle_tree.js +5 -0
  147. package/dest/public/public_processor/public_processor.d.ts +8 -4
  148. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  149. package/dest/public/public_processor/public_processor.js +468 -62
  150. package/dest/public/public_processor/public_processor_metrics.d.ts +2 -2
  151. package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -1
  152. package/dest/public/public_processor/public_processor_metrics.js +28 -45
  153. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +3 -2
  154. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts.map +1 -1
  155. package/dest/public/public_tx_simulator/contract_provider_for_cpp.js +17 -13
  156. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +20 -5
  157. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts.map +1 -1
  158. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.js +50 -12
  159. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts +4 -4
  160. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.d.ts.map +1 -1
  161. package/dest/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.js +7 -7
  162. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts +4 -4
  163. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts.map +1 -1
  164. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.js +8 -7
  165. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts +3 -2
  166. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts.map +1 -1
  167. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.js +2 -2
  168. package/dest/public/public_tx_simulator/factories.d.ts +3 -2
  169. package/dest/public/public_tx_simulator/factories.d.ts.map +1 -1
  170. package/dest/public/public_tx_simulator/factories.js +4 -4
  171. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +1 -1
  172. package/dest/public/public_tx_simulator/public_tx_context.d.ts +4 -3
  173. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  174. package/dest/public/public_tx_simulator/public_tx_context.js +8 -8
  175. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +6 -4
  176. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  177. package/dest/public/public_tx_simulator/public_tx_simulator.js +14 -9
  178. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts +24 -1
  179. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts.map +1 -1
  180. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.js +395 -19
  181. package/dest/public/side_effect_trace.d.ts +5 -4
  182. package/dest/public/side_effect_trace.d.ts.map +1 -1
  183. package/dest/public/side_effect_trace.js +3 -3
  184. package/dest/public/state_manager/state_manager.d.ts +10 -4
  185. package/dest/public/state_manager/state_manager.d.ts.map +1 -1
  186. package/dest/public/state_manager/state_manager.js +14 -7
  187. package/dest/public/test_executor_metrics.d.ts +3 -2
  188. package/dest/public/test_executor_metrics.d.ts.map +1 -1
  189. package/dest/public/test_executor_metrics.js +2 -2
  190. package/package.json +17 -17
  191. package/src/private/acvm/acvm.ts +4 -3
  192. package/src/private/acvm_native.ts +11 -5
  193. package/src/private/acvm_wasm.ts +7 -3
  194. package/src/private/circuit_recording/circuit_recorder.ts +21 -18
  195. package/src/private/circuit_recording/file_circuit_recorder.ts +7 -2
  196. package/src/private/circuit_recording/memory_circuit_recorder.ts +6 -4
  197. package/src/private/factory.ts +7 -4
  198. package/src/public/avm/avm_context.ts +2 -2
  199. package/src/public/avm/avm_contract_call_result.ts +8 -6
  200. package/src/public/avm/avm_execution_environment.ts +9 -4
  201. package/src/public/avm/avm_gas.ts +2 -2
  202. package/src/public/avm/avm_machine_state.ts +6 -5
  203. package/src/public/avm/avm_memory_types.ts +4 -0
  204. package/src/public/avm/avm_simulator.ts +8 -5
  205. package/src/public/avm/calldata.ts +100 -0
  206. package/src/public/avm/fixtures/account_proof.json +553 -0
  207. package/src/public/avm/fixtures/account_proof_fetcher.ts +166 -0
  208. package/src/public/avm/fixtures/avm_simulation_tester.ts +8 -2
  209. package/src/public/avm/fixtures/initializers.ts +2 -1
  210. package/src/public/avm/opcodes/accrued_substate.ts +31 -34
  211. package/src/public/avm/opcodes/arithmetic.ts +1 -1
  212. package/src/public/avm/opcodes/bitwise.ts +3 -3
  213. package/src/public/avm/opcodes/comparators.ts +1 -1
  214. package/src/public/avm/opcodes/contract.ts +4 -7
  215. package/src/public/avm/opcodes/control_flow.ts +2 -2
  216. package/src/public/avm/opcodes/conversion.ts +3 -3
  217. package/src/public/avm/opcodes/ec_add.ts +13 -4
  218. package/src/public/avm/opcodes/environment_getters.ts +7 -7
  219. package/src/public/avm/opcodes/external_calls.ts +17 -15
  220. package/src/public/avm/opcodes/hashing.ts +13 -9
  221. package/src/public/avm/opcodes/instruction_impl.ts +2 -2
  222. package/src/public/avm/opcodes/memory.ts +19 -19
  223. package/src/public/avm/opcodes/misc.ts +2 -2
  224. package/src/public/avm/opcodes/storage.ts +30 -22
  225. package/src/public/avm/serialization/bytecode_serialization.ts +2 -2
  226. package/src/public/avm/serialization/instruction_serialization.ts +1 -1
  227. package/src/public/debug_fn_name.ts +17 -8
  228. package/src/public/executor_metrics.ts +9 -33
  229. package/src/public/fixtures/amm_test.ts +2 -2
  230. package/src/public/fixtures/bulk_test.ts +8 -8
  231. package/src/public/fixtures/custom_bytecode_tester.ts +53 -19
  232. package/src/public/fixtures/custom_bytecode_tests.ts +91 -22
  233. package/src/public/fixtures/index.ts +7 -1
  234. package/src/public/fixtures/minimal_public_tx.ts +5 -14
  235. package/src/public/fixtures/opcode_spammer.ts +1717 -0
  236. package/src/public/fixtures/public_tx_simulation_tester.ts +66 -10
  237. package/src/public/fixtures/utils.ts +7 -7
  238. package/src/public/fuzzing/avm_fuzzer_simulator.ts +68 -13
  239. package/src/public/fuzzing/avm_simulator_bin.ts +64 -10
  240. package/src/public/hinting_db_sources.ts +4 -0
  241. package/src/public/public_db_sources.ts +15 -5
  242. package/src/public/public_processor/guarded_merkle_tree.ts +4 -0
  243. package/src/public/public_processor/public_processor.ts +95 -55
  244. package/src/public/public_processor/public_processor_metrics.ts +16 -44
  245. package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +22 -14
  246. package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +64 -9
  247. package/src/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.ts +7 -5
  248. package/src/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.ts +9 -6
  249. package/src/public/public_tx_simulator/dumping_cpp_public_tx_simulator.ts +3 -1
  250. package/src/public/public_tx_simulator/factories.ts +6 -3
  251. package/src/public/public_tx_simulator/public_tx_context.ts +13 -6
  252. package/src/public/public_tx_simulator/public_tx_simulator.ts +21 -10
  253. package/src/public/public_tx_simulator/public_tx_simulator_interface.ts +23 -0
  254. package/src/public/side_effect_trace.ts +5 -2
  255. package/src/public/state_manager/state_manager.ts +29 -20
  256. package/src/public/test_executor_metrics.ts +3 -3
@@ -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'),
129
- private opts: Pick<SequencerConfig, 'fakeProcessingDelayPerTxMs'> = {},
141
+ private log: Logger,
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[]]> {
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;
@@ -160,7 +175,7 @@ export class PublicProcessor implements Traceable {
160
175
  let totalBlockGas = new Gas(0, 0);
161
176
  let totalBlobFields = 0;
162
177
 
163
- for await (const origTx of txs) {
178
+ for await (const tx of txs) {
164
179
  // Only process up to the max tx limit
165
180
  if (maxTransactions !== undefined && result.length >= maxTransactions) {
166
181
  this.log.debug(`Stopping tx processing due to reaching the max tx limit.`);
@@ -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
- const txHash = origTx.getTxHash().toString();
178
- const preTxSizeInBytes = origTx.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
- });
191
+ const txHash = tx.getTxHash().toString();
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
190
- const txGasLimit = origTx.data.constants.txContext.gasSettings.gasLimits;
191
- if (maxBlockGas !== undefined && totalBlockGas.add(txGasLimit).gtAny(maxBlockGas)) {
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.
206
+ const txGasLimit = tx.data.constants.txContext.gasSettings.gasLimits;
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,
@@ -198,9 +214,6 @@ export class PublicProcessor implements Traceable {
198
214
  continue;
199
215
  }
200
216
 
201
- // The processor modifies the tx objects in place, so we need to clone them.
202
- const tx = Tx.clone(origTx);
203
-
204
217
  // We validate the tx before processing it, to avoid unnecessary work.
205
218
  if (preprocessValidator) {
206
219
  const result = await preprocessValidator.validateTx(tx);
@@ -231,26 +244,19 @@ export class PublicProcessor implements Traceable {
231
244
  this.contractsDB.createCheckpoint();
232
245
 
233
246
  try {
234
- const [processedTx, returnValues] = await this.processTx(tx, deadline);
247
+ const [processedTx, returnValues, txDebugLogs] = await this.processTx(tx, deadline);
235
248
 
236
- const txBlobFields = processedTx.txEffect.getNumBlobFields();
249
+ // Inject a fake processing failure after N txs if requested
250
+ const fakeThrowAfter = this.opts.fakeThrowAfterProcessingTxCount;
251
+ if (fakeThrowAfter !== undefined && result.length + failed.length + 1 >= fakeThrowAfter) {
252
+ throw new Error(`Fake error after processing ${fakeThrowAfter} txs`);
253
+ }
237
254
 
238
- // If the actual size of this tx would exceed block size, skip it
255
+ const txBlobFields = processedTx.txEffect.getNumBlobFields();
239
256
  const txSize = txBlobFields * Fr.SIZE_IN_BYTES;
240
- if (maxBlockSize !== undefined && totalSizeInBytes + txSize > maxBlockSize) {
241
- this.log.debug(`Skipping processed tx ${txHash} sized ${txSize} due to max block size.`, {
242
- txHash,
243
- sizeInBytes: txSize,
244
- totalSizeInBytes,
245
- maxBlockSize,
246
- });
247
- // Need to revert the checkpoint here and don't go any further
248
- await checkpoint.revert();
249
- this.contractsDB.revertCheckpoint();
250
- continue;
251
- }
252
257
 
253
- // 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.
259
+ // Note: maxBlobFields already accounts for block end blob fields and previous blocks in checkpoint.
254
260
  if (maxBlobFields !== undefined && totalBlobFields + txBlobFields > maxBlobFields) {
255
261
  this.log.debug(
256
262
  `Skipping processed tx ${txHash} with ${txBlobFields} blob fields due to max blob fields limit.`,
@@ -267,12 +273,34 @@ export class PublicProcessor implements Traceable {
267
273
  continue;
268
274
  }
269
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
+
270
295
  // FIXME(fcarreiro): it's ugly to have to notify the validator of nullifiers.
271
296
  // I'd rather pass the validators the processedTx as well and let them deal with it.
272
297
  nullifierCache?.addNullifiers(processedTx.txEffect.nullifiers.map(n => n.toBuffer()));
273
298
  result.push(processedTx);
274
299
  usedTxs.push(tx);
275
300
  returns = returns.concat(returnValues);
301
+ debugLogs.push(...txDebugLogs);
302
+
303
+ this.debugLogStore.storeLogs(processedTx.hash.toString(), txDebugLogs);
276
304
 
277
305
  totalPublicGas = totalPublicGas.add(processedTx.gasUsed.publicGas);
278
306
  totalBlockGas = totalBlockGas.add(processedTx.gasUsed.totalGas);
@@ -282,7 +310,15 @@ export class PublicProcessor implements Traceable {
282
310
  if (err?.name === 'PublicProcessorTimeoutError') {
283
311
  this.log.warn(`Stopping tx processing due to timeout.`);
284
312
  // We hit the transaction execution deadline.
285
- // There may still be a transaction executing. We stop the guarded fork to prevent any further access to the world state.
313
+ // There may still be a transaction executing on a worker thread (C++ via NAPI).
314
+ // Signal cancellation AND WAIT for the simulation to actually stop.
315
+ // This is critical because C++ might be in the middle of a slow operation (e.g., pad_trees)
316
+ // and won't check the cancellation flag until that operation completes.
317
+ // Without waiting, we'd proceed to revert checkpoints while C++ is still writing to state.
318
+ // Wait for C++ to stop gracefully.
319
+ await this.publicTxSimulator.cancel?.();
320
+
321
+ // Now stop the guarded fork to prevent any further TS-side access to the world state.
286
322
  await this.guardedMerkleTree.stop();
287
323
 
288
324
  // We now know there can't be any further access to world state. The fork is in a state where there is:
@@ -338,7 +374,7 @@ export class PublicProcessor implements Traceable {
338
374
  totalSizeInBytes,
339
375
  });
340
376
 
341
- return [result, failed, usedTxs, returns];
377
+ return [result, failed, usedTxs, returns, debugLogs];
342
378
  }
343
379
 
344
380
  private async checkWorldStateUnchanged(
@@ -358,8 +394,13 @@ export class PublicProcessor implements Traceable {
358
394
  }
359
395
 
360
396
  @trackSpan('PublicProcessor.processTx', tx => ({ [Attributes.TX_HASH]: tx.getTxHash().toString() }))
361
- private async processTx(tx: Tx, deadline: Date | undefined): Promise<[ProcessedTx, NestedProcessReturnValues[]]> {
362
- const [time, [processedTx, returnValues]] = await elapsed(() => this.processTxWithinDeadline(tx, deadline));
397
+ private async processTx(
398
+ tx: Tx,
399
+ deadline: Date | undefined,
400
+ ): Promise<[ProcessedTx, NestedProcessReturnValues[], DebugLog[]]> {
401
+ const [time, [processedTx, returnValues, debugLogs]] = await elapsed(() =>
402
+ this.processTxWithinDeadline(tx, deadline),
403
+ );
363
404
 
364
405
  this.log.verbose(
365
406
  !tx.hasPublicCalls()
@@ -382,7 +423,7 @@ export class PublicProcessor implements Traceable {
382
423
  },
383
424
  );
384
425
 
385
- return [processedTx, returnValues ?? []];
426
+ return [processedTx, returnValues ?? [], debugLogs];
386
427
  }
387
428
 
388
429
  private async doTreeInsertionsForPrivateOnlyTx(processedTx: ProcessedTx): Promise<void> {
@@ -416,10 +457,9 @@ export class PublicProcessor implements Traceable {
416
457
  private async processTxWithinDeadline(
417
458
  tx: Tx,
418
459
  deadline: Date | undefined,
419
- ): Promise<[ProcessedTx, NestedProcessReturnValues[] | undefined]> {
420
- const innerProcessFn: () => Promise<[ProcessedTx, NestedProcessReturnValues[] | undefined]> = tx.hasPublicCalls()
421
- ? () => this.processTxWithPublicCalls(tx)
422
- : () => this.processPrivateOnlyTx(tx);
460
+ ): Promise<[ProcessedTx, NestedProcessReturnValues[] | undefined, DebugLog[]]> {
461
+ const innerProcessFn: () => Promise<[ProcessedTx, NestedProcessReturnValues[] | undefined, DebugLog[]]> =
462
+ tx.hasPublicCalls() ? () => this.processTxWithPublicCalls(tx) : () => this.processPrivateOnlyTx(tx);
423
463
 
424
464
  // Fake a delay per tx if instructed (used for tests)
425
465
  const fakeDelayPerTxMs = this.opts.fakeProcessingDelayPerTxMs;
@@ -487,7 +527,7 @@ export class PublicProcessor implements Traceable {
487
527
  @trackSpan('PublicProcessor.processPrivateOnlyTx', (tx: Tx) => ({
488
528
  [Attributes.TX_HASH]: tx.getTxHash().toString(),
489
529
  }))
490
- private async processPrivateOnlyTx(tx: Tx): Promise<[ProcessedTx, undefined]> {
530
+ private async processPrivateOnlyTx(tx: Tx): Promise<[ProcessedTx, undefined, DebugLog[]]> {
491
531
  const gasFees = this.globalVariables.gasFees;
492
532
  const transactionFee = computeTransactionFee(gasFees, tx.data.constants.txContext.gasSettings, tx.data.gasUsed);
493
533
 
@@ -512,13 +552,13 @@ export class PublicProcessor implements Traceable {
512
552
 
513
553
  await this.contractsDB.addNewContracts(tx);
514
554
 
515
- return [processedTx, undefined];
555
+ return [processedTx, undefined, []];
516
556
  }
517
557
 
518
558
  @trackSpan('PublicProcessor.processTxWithPublicCalls', tx => ({
519
559
  [Attributes.TX_HASH]: tx.getTxHash().toString(),
520
560
  }))
521
- private async processTxWithPublicCalls(tx: Tx): Promise<[ProcessedTx, NestedProcessReturnValues[]]> {
561
+ private async processTxWithPublicCalls(tx: Tx): Promise<[ProcessedTx, NestedProcessReturnValues[], DebugLog[]]> {
522
562
  const timer = new Timer();
523
563
 
524
564
  const result = await this.publicTxSimulator.simulate(tx);
@@ -556,7 +596,7 @@ export class PublicProcessor implements Traceable {
556
596
  revertReason,
557
597
  );
558
598
 
559
- return [processedTx, appLogicReturnValues];
599
+ return [processedTx, appLogicReturnValues, result.logs ?? []];
560
600
  }
561
601
 
562
602
  /**
@@ -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,7 +9,7 @@ import {
9
9
  type TelemetryClient,
10
10
  type Tracer,
11
11
  type UpDownCounter,
12
- ValueType,
12
+ createUpDownCounterWithDefault,
13
13
  } from '@aztec/telemetry-client';
14
14
 
15
15
  export class PublicProcessorMetrics {
@@ -34,60 +34,32 @@ export class PublicProcessorMetrics {
34
34
  this.tracer = client.getTracer(name);
35
35
  const meter = client.getMeter(name);
36
36
 
37
- this.txDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TX_DURATION, {
38
- description: 'How long it takes to process a transaction',
39
- unit: 'ms',
40
- valueType: ValueType.INT,
41
- });
37
+ this.txDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TX_DURATION);
42
38
 
43
- this.txCount = meter.createUpDownCounter(Metrics.PUBLIC_PROCESSOR_TX_COUNT, {
44
- description: 'Number of transactions processed',
39
+ this.txCount = createUpDownCounterWithDefault(meter, Metrics.PUBLIC_PROCESSOR_TX_COUNT, {
40
+ [Attributes.OK]: [true, false],
45
41
  });
46
42
 
47
- this.txPhaseCount = meter.createUpDownCounter(Metrics.PUBLIC_PROCESSOR_TX_PHASE_COUNT, {
48
- description: 'Number of phases processed',
49
- });
43
+ this.txPhaseCount = createUpDownCounterWithDefault(meter, Metrics.PUBLIC_PROCESSOR_TX_PHASE_COUNT);
50
44
 
51
- this.phaseDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_PHASE_DURATION, {
52
- description: 'How long it takes to process a phase',
53
- unit: 'ms',
54
- valueType: ValueType.INT,
55
- });
45
+ this.phaseDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_PHASE_DURATION);
56
46
 
57
- this.phaseCount = meter.createUpDownCounter(Metrics.PUBLIC_PROCESSOR_PHASE_COUNT, {
58
- description: 'Number of failed phases',
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],
59
50
  });
60
51
 
61
- this.bytecodeDeployed = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_DEPLOY_BYTECODE_SIZE, {
62
- description: 'Size of deployed bytecode',
63
- unit: 'By',
64
- });
52
+ this.bytecodeDeployed = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_DEPLOY_BYTECODE_SIZE);
65
53
 
66
- this.totalGas = meter.createGauge(Metrics.PUBLIC_PROCESSOR_TOTAL_GAS, {
67
- description: 'Total gas used in block',
68
- unit: 'gas',
69
- });
54
+ this.totalGas = meter.createGauge(Metrics.PUBLIC_PROCESSOR_TOTAL_GAS);
70
55
 
71
- this.totalGasHistogram = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TOTAL_GAS_HISTOGRAM, {
72
- description: 'Total gas used in block as histogram',
73
- unit: 'gas/block',
74
- });
56
+ this.totalGasHistogram = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TOTAL_GAS_HISTOGRAM);
75
57
 
76
- this.txGas = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TX_GAS, {
77
- description: 'Gas used in transaction',
78
- unit: 'gas/tx',
79
- });
58
+ this.txGas = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TX_GAS);
80
59
 
81
- this.gasRate = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_GAS_RATE, {
82
- description: 'L2 gas per second for complete block',
83
- unit: 'gas/s',
84
- });
60
+ this.gasRate = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_GAS_RATE);
85
61
 
86
- this.treeInsertionDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TREE_INSERTION, {
87
- description: 'How long it takes for tree insertion',
88
- unit: 'us',
89
- valueType: ValueType.INT,
90
- });
62
+ this.treeInsertionDuration = meter.createHistogram(Metrics.PUBLIC_PROCESSOR_TREE_INSERTION);
91
63
  }
92
64
 
93
65
  recordPhaseDuration(phaseName: TxExecutionPhase, durationMs: number) {
@@ -1,5 +1,5 @@
1
1
  import { Fr } from '@aztec/foundation/curves/bn254';
2
- import { type Logger, createLogger } from '@aztec/foundation/log';
2
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
3
3
  import type { ContractProvider } from '@aztec/native';
4
4
  import { FunctionSelector } from '@aztec/stdlib/abi';
5
5
  import { deserializeFromMessagePack, serializeWithMessagePack } from '@aztec/stdlib/avm';
@@ -10,15 +10,18 @@ import type { GlobalVariables } from '@aztec/stdlib/tx';
10
10
  import type { PublicContractsDB } from '../public_db_sources.js';
11
11
 
12
12
  export class ContractProviderForCpp implements ContractProvider {
13
- private log: Logger = createLogger('simulator:contract_provider_for_cpp');
13
+ private log: Logger;
14
14
 
15
15
  constructor(
16
16
  private contractsDB: PublicContractsDB,
17
17
  private globalVariables: GlobalVariables,
18
- ) {}
18
+ bindings?: LoggerBindings,
19
+ ) {
20
+ this.log = createLogger('simulator:contract_provider_for_cpp', bindings);
21
+ }
19
22
 
20
23
  public getContractInstance = async (address: string): Promise<Buffer | undefined> => {
21
- this.log.debug(`Contract provider callback: getContractInstance(${address})`);
24
+ this.log.trace(`Contract provider callback: getContractInstance(${address})`);
22
25
 
23
26
  const aztecAddr = AztecAddress.fromString(address);
24
27
 
@@ -33,7 +36,7 @@ export class ContractProviderForCpp implements ContractProvider {
33
36
  };
34
37
 
35
38
  public getContractClass = async (classId: string): Promise<Buffer | undefined> => {
36
- this.log.debug(`Contract provider callback: getContractClass(${classId})`);
39
+ this.log.trace(`Contract provider callback: getContractClass(${classId})`);
37
40
 
38
41
  // Parse classId string to Fr
39
42
  const classIdFr = Fr.fromString(classId);
@@ -50,7 +53,7 @@ export class ContractProviderForCpp implements ContractProvider {
50
53
  };
51
54
 
52
55
  public addContracts = async (contractDeploymentDataBuffer: Buffer): Promise<void> => {
53
- this.log.debug(`Contract provider callback: addContracts`);
56
+ this.log.trace(`Contract provider callback: addContracts`);
54
57
 
55
58
  const rawData: any = deserializeFromMessagePack(contractDeploymentDataBuffer);
56
59
 
@@ -58,12 +61,12 @@ export class ContractProviderForCpp implements ContractProvider {
58
61
  const contractDeploymentData = ContractDeploymentData.fromPlainObject(rawData);
59
62
 
60
63
  // Add contracts to the contracts DB
61
- this.log.debug(`Calling contractsDB.addContracts`);
64
+ this.log.trace(`Calling contractsDB.addContracts`);
62
65
  await this.contractsDB.addContracts(contractDeploymentData);
63
66
  };
64
67
 
65
68
  public getBytecodeCommitment = async (classId: string): Promise<Buffer | undefined> => {
66
- this.log.debug(`Contract provider callback: getBytecodeCommitment(${classId})`);
69
+ this.log.trace(`Contract provider callback: getBytecodeCommitment(${classId})`);
67
70
 
68
71
  // Parse classId string to Fr
69
72
  const classIdFr = Fr.fromString(classId);
@@ -81,18 +84,23 @@ export class ContractProviderForCpp implements ContractProvider {
81
84
  };
82
85
 
83
86
  public getDebugFunctionName = async (address: string, selector: string): Promise<string | undefined> => {
84
- this.log.debug(`Contract provider callback: getDebugFunctionName(${address}, ${selector})`);
87
+ this.log.trace(`Contract provider callback: getDebugFunctionName(${address}, ${selector})`);
85
88
 
86
89
  // Parse address and selector strings
87
90
  const aztecAddr = AztecAddress.fromString(address);
88
91
  const selectorFr = Fr.fromString(selector);
89
- const functionSelector = FunctionSelector.fromField(selectorFr);
92
+ const functionSelector = FunctionSelector.fromFieldOrUndefined(selectorFr);
93
+
94
+ if (!functionSelector) {
95
+ this.log.trace(`calldata[0] is not a function selector: ${selector}`);
96
+ return undefined;
97
+ }
90
98
 
91
99
  // Fetch debug function name from the contracts DB
92
100
  const name = await this.contractsDB.getDebugFunctionName(aztecAddr, functionSelector);
93
101
 
94
102
  if (!name) {
95
- this.log.debug(`Debug function name not found for ${address}:${selector}`);
103
+ this.log.trace(`Debug function name not found for ${address}:${selector}`);
96
104
  return undefined;
97
105
  }
98
106
 
@@ -100,17 +108,17 @@ export class ContractProviderForCpp implements ContractProvider {
100
108
  };
101
109
 
102
110
  public createCheckpoint = (): Promise<void> => {
103
- this.log.debug(`Contract provider callback: createCheckpoint`);
111
+ this.log.trace(`Contract provider callback: createCheckpoint`);
104
112
  return Promise.resolve(this.contractsDB.createCheckpoint());
105
113
  };
106
114
 
107
115
  public commitCheckpoint = (): Promise<void> => {
108
- this.log.debug(`Contract provider callback: commitCheckpoint`);
116
+ this.log.trace(`Contract provider callback: commitCheckpoint`);
109
117
  return Promise.resolve(this.contractsDB.commitCheckpoint());
110
118
  };
111
119
 
112
120
  public revertCheckpoint = (): Promise<void> => {
113
- this.log.debug(`Contract provider callback: revertCheckpoint`);
121
+ this.log.trace(`Contract provider callback: revertCheckpoint`);
114
122
  return Promise.resolve(this.contractsDB.revertCheckpoint());
115
123
  };
116
124
  }
@@ -1,5 +1,6 @@
1
- import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
2
- import { avmSimulate } from '@aztec/native';
1
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
2
+ import { sleep } from '@aztec/foundation/sleep';
3
+ import { type CancellationToken, avmSimulate, cancelSimulation, createCancellationToken } from '@aztec/native';
3
4
  import { ProtocolContractsList } from '@aztec/protocol-contracts';
4
5
  import {
5
6
  AvmFastSimulationInputs,
@@ -33,15 +34,20 @@ import type {
33
34
  */
34
35
  export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxSimulatorInterface {
35
36
  protected override log: Logger;
37
+ /** Current cancellation token for in-flight simulation. */
38
+ private cancellationToken?: CancellationToken;
39
+ /** Current simulation promise, used to wait for completion after cancellation. */
40
+ private simulationPromise?: Promise<Buffer>;
36
41
 
37
42
  constructor(
38
43
  merkleTree: MerkleTreeWriteOperations,
39
44
  contractsDB: PublicContractsDB,
40
45
  globalVariables: GlobalVariables,
41
46
  config?: Partial<PublicSimulatorConfig>,
47
+ bindings?: LoggerBindings,
42
48
  ) {
43
- super(merkleTree, contractsDB, globalVariables, config);
44
- this.log = createLogger(`simulator:cpp_public_tx_simulator`);
49
+ super(merkleTree, contractsDB, globalVariables, config, undefined, bindings);
50
+ this.log = createLogger(`simulator:cpp_public_tx_simulator`, bindings);
45
51
  }
46
52
 
47
53
  /**
@@ -79,18 +85,38 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
79
85
  );
80
86
 
81
87
  // Create contract provider for callbacks to TypeScript PublicContractsDB from C++
82
- const contractProvider = new ContractProviderForCpp(this.contractsDB, this.globalVariables);
88
+ const contractProvider = new ContractProviderForCpp(this.contractsDB, this.globalVariables, this.bindings);
83
89
 
84
90
  // Serialize to msgpack and call the C++ simulator
85
91
  this.log.trace(`Serializing fast simulation inputs to msgpack...`);
86
92
  const inputBuffer = fastSimInputs.serializeWithMessagePack();
87
93
 
94
+ // Create cancellation token for this simulation
95
+ this.cancellationToken = createCancellationToken();
96
+
97
+ // Store the promise so cancel() can wait for it
98
+ this.log.debug(`Calling C++ simulator for tx ${txHash}`);
99
+ this.simulationPromise = avmSimulate(
100
+ inputBuffer,
101
+ contractProvider,
102
+ wsCppHandle,
103
+ this.log.level,
104
+ undefined,
105
+ this.cancellationToken,
106
+ );
107
+
88
108
  let resultBuffer: Buffer;
89
109
  try {
90
- this.log.debug(`Calling C++ simulator for tx ${txHash}`);
91
- resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle, logLevel);
110
+ resultBuffer = await this.simulationPromise;
92
111
  } catch (error: any) {
112
+ // Check if this was a cancellation
113
+ if (error.message?.includes('Simulation cancelled')) {
114
+ throw new SimulationError(`C++ simulation cancelled`, []);
115
+ }
93
116
  throw new SimulationError(`C++ simulation failed: ${error.message}`, []);
117
+ } finally {
118
+ this.cancellationToken = undefined;
119
+ this.simulationPromise = undefined;
94
120
  }
95
121
 
96
122
  // If we've reached this point, C++ succeeded during simulation,
@@ -109,6 +135,33 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
109
135
 
110
136
  return cppResult;
111
137
  }
138
+
139
+ /**
140
+ * Cancel the current simulation if one is in progress.
141
+ * This signals the C++ simulator to stop at the next opcode or before the next WorldState write.
142
+ * Safe to call even if no simulation is in progress.
143
+ *
144
+ * @param waitTimeoutMs - If provided, wait up to this many ms for the simulation to actually stop.
145
+ * This is important because C++ might be in the middle of a slow operation
146
+ * (e.g., pad_trees) and won't check the cancellation flag until it completes.
147
+ * Default timeout of 100ms after cancellation.
148
+ */
149
+ public async cancel(waitTimeoutMs: number = 100): Promise<void> {
150
+ if (this.cancellationToken) {
151
+ this.log.debug('Cancelling C++ simulation');
152
+ cancelSimulation(this.cancellationToken);
153
+ }
154
+
155
+ // Wait for the simulation to actually complete if not already done
156
+ if (this.simulationPromise) {
157
+ this.log.debug(`Waiting up to ${waitTimeoutMs}ms for C++ simulation to stop`);
158
+ await Promise.race([
159
+ this.simulationPromise.catch(() => {}), // Ignore rejection, just wait for completion
160
+ sleep(waitTimeoutMs),
161
+ ]);
162
+ this.log.debug('C++ simulation stopped or wait timed out');
163
+ }
164
+ }
112
165
  }
113
166
 
114
167
  export class MeasuredCppPublicTxSimulator extends CppPublicTxSimulator implements MeasuredPublicTxSimulatorInterface {
@@ -118,8 +171,9 @@ export class MeasuredCppPublicTxSimulator extends CppPublicTxSimulator implement
118
171
  globalVariables: GlobalVariables,
119
172
  protected readonly metrics: ExecutorMetricsInterface,
120
173
  config?: Partial<PublicSimulatorConfig>,
174
+ bindings?: LoggerBindings,
121
175
  ) {
122
- super(merkleTree, contractsDB, globalVariables, config);
176
+ super(merkleTree, contractsDB, globalVariables, config, bindings);
123
177
  }
124
178
 
125
179
  public override async simulate(tx: Tx, txLabel: string = 'unlabeledTx'): Promise<PublicTxResult> {
@@ -147,9 +201,10 @@ export class TelemetryCppPublicTxSimulator extends MeasuredCppPublicTxSimulator
147
201
  globalVariables: GlobalVariables,
148
202
  telemetryClient: TelemetryClient = getTelemetryClient(),
149
203
  config?: Partial<PublicSimulatorConfig>,
204
+ bindings?: LoggerBindings,
150
205
  ) {
151
206
  const metrics = new ExecutorMetrics(telemetryClient, 'CppPublicTxSimulator');
152
- super(merkleTree, contractsDB, globalVariables, metrics, config);
207
+ super(merkleTree, contractsDB, globalVariables, metrics, config, bindings);
153
208
  this.tracer = metrics.tracer;
154
209
  }
155
210
  }