@aztec/simulator 0.0.1-commit.d3ec352c → 0.0.1-commit.fcb71a6

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 (213) hide show
  1. package/dest/common/errors.d.ts +2 -2
  2. package/dest/common/errors.d.ts.map +1 -1
  3. package/dest/private/acvm/deserialize.d.ts +2 -2
  4. package/dest/private/acvm/deserialize.d.ts.map +1 -1
  5. package/dest/private/acvm/deserialize.js +1 -1
  6. package/dest/private/acvm/serialize.d.ts +2 -2
  7. package/dest/private/acvm/serialize.d.ts.map +1 -1
  8. package/dest/private/acvm/serialize.js +1 -1
  9. package/dest/private/circuit_recording/circuit_recorder.d.ts +1 -1
  10. package/dest/private/circuit_recording/circuit_recorder.d.ts.map +1 -1
  11. package/dest/private/circuit_recording/circuit_recorder.js +16 -15
  12. package/dest/public/avm/avm_context.d.ts +2 -2
  13. package/dest/public/avm/avm_context.d.ts.map +1 -1
  14. package/dest/public/avm/avm_contract_call_result.d.ts +2 -2
  15. package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -1
  16. package/dest/public/avm/avm_execution_environment.d.ts +2 -2
  17. package/dest/public/avm/avm_execution_environment.d.ts.map +1 -1
  18. package/dest/public/avm/avm_execution_environment.js +1 -1
  19. package/dest/public/avm/avm_gas.d.ts +1 -1
  20. package/dest/public/avm/avm_gas.d.ts.map +1 -1
  21. package/dest/public/avm/avm_machine_state.d.ts +2 -2
  22. package/dest/public/avm/avm_machine_state.d.ts.map +1 -1
  23. package/dest/public/avm/avm_memory_types.d.ts +2 -2
  24. package/dest/public/avm/avm_memory_types.d.ts.map +1 -1
  25. package/dest/public/avm/avm_memory_types.js +1 -1
  26. package/dest/public/avm/avm_simulator.d.ts +2 -2
  27. package/dest/public/avm/avm_simulator.d.ts.map +1 -1
  28. package/dest/public/avm/avm_simulator.js +1 -1
  29. package/dest/public/avm/errors.d.ts +2 -2
  30. package/dest/public/avm/errors.d.ts.map +1 -1
  31. package/dest/public/avm/fixtures/avm_simulation_tester.js +1 -1
  32. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +2 -2
  33. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -1
  34. package/dest/public/avm/fixtures/base_avm_simulation_tester.js +1 -1
  35. package/dest/public/avm/fixtures/initializers.d.ts +2 -2
  36. package/dest/public/avm/fixtures/initializers.d.ts.map +1 -1
  37. package/dest/public/avm/fixtures/initializers.js +1 -1
  38. package/dest/public/avm/fixtures/utils.d.ts +2 -2
  39. package/dest/public/avm/fixtures/utils.d.ts.map +1 -1
  40. package/dest/public/avm/fixtures/utils.js +1 -1
  41. package/dest/public/avm/opcodes/arithmetic.d.ts +3 -1
  42. package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -1
  43. package/dest/public/avm/opcodes/arithmetic.js +11 -1
  44. package/dest/public/avm/opcodes/ec_add.js +2 -2
  45. package/dest/public/avm/opcodes/hashing.d.ts +1 -1
  46. package/dest/public/avm/opcodes/hashing.d.ts.map +1 -1
  47. package/dest/public/avm/opcodes/hashing.js +9 -4
  48. package/dest/public/avm/opcodes/memory.js +1 -1
  49. package/dest/public/avm/revert_reason.d.ts +2 -2
  50. package/dest/public/avm/revert_reason.d.ts.map +1 -1
  51. package/dest/public/avm/serialization/instruction_serialization.js +1 -1
  52. package/dest/public/avm/test_utils.d.ts +2 -2
  53. package/dest/public/avm/test_utils.d.ts.map +1 -1
  54. package/dest/public/avm/test_utils.js +1 -1
  55. package/dest/public/contracts_db_checkpoint.d.ts +2 -2
  56. package/dest/public/contracts_db_checkpoint.d.ts.map +1 -1
  57. package/dest/public/db_interfaces.d.ts +2 -2
  58. package/dest/public/db_interfaces.d.ts.map +1 -1
  59. package/dest/public/debug_fn_name.d.ts +2 -2
  60. package/dest/public/debug_fn_name.d.ts.map +1 -1
  61. package/dest/public/debug_fn_name.js +10 -3
  62. package/dest/public/fixtures/amm_test.js +2 -2
  63. package/dest/public/fixtures/bulk_test.js +4 -52
  64. package/dest/public/fixtures/custom_bytecode_tester.d.ts +28 -6
  65. package/dest/public/fixtures/custom_bytecode_tester.d.ts.map +1 -1
  66. package/dest/public/fixtures/custom_bytecode_tester.js +36 -12
  67. package/dest/public/fixtures/custom_bytecode_tests.d.ts +4 -1
  68. package/dest/public/fixtures/custom_bytecode_tests.d.ts.map +1 -1
  69. package/dest/public/fixtures/custom_bytecode_tests.js +74 -9
  70. package/dest/public/fixtures/index.d.ts +4 -2
  71. package/dest/public/fixtures/index.d.ts.map +1 -1
  72. package/dest/public/fixtures/index.js +3 -1
  73. package/dest/public/fixtures/minimal_public_tx.d.ts +2 -7
  74. package/dest/public/fixtures/minimal_public_tx.d.ts.map +1 -1
  75. package/dest/public/fixtures/minimal_public_tx.js +2 -12
  76. package/dest/public/fixtures/opcode_spammer.d.ts +123 -0
  77. package/dest/public/fixtures/opcode_spammer.d.ts.map +1 -0
  78. package/dest/public/fixtures/opcode_spammer.js +1681 -0
  79. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +15 -2
  80. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  81. package/dest/public/fixtures/public_tx_simulation_tester.js +36 -8
  82. package/dest/public/fixtures/simple_contract_data_source.d.ts +2 -2
  83. package/dest/public/fixtures/simple_contract_data_source.d.ts.map +1 -1
  84. package/dest/public/fixtures/token_test.js +1 -1
  85. package/dest/public/fixtures/utils.d.ts +2 -2
  86. package/dest/public/fixtures/utils.d.ts.map +1 -1
  87. package/dest/public/fixtures/utils.js +4 -3
  88. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts +46 -0
  89. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts.map +1 -0
  90. package/dest/public/fuzzing/avm_fuzzer_simulator.js +139 -0
  91. package/dest/public/fuzzing/avm_simulator_bin.d.ts +2 -0
  92. package/dest/public/fuzzing/avm_simulator_bin.d.ts.map +1 -0
  93. package/dest/public/fuzzing/avm_simulator_bin.js +100 -0
  94. package/dest/public/hinting_db_sources.d.ts +3 -2
  95. package/dest/public/hinting_db_sources.d.ts.map +1 -1
  96. package/dest/public/hinting_db_sources.js +5 -2
  97. package/dest/public/index.d.ts +2 -2
  98. package/dest/public/index.d.ts.map +1 -1
  99. package/dest/public/index.js +1 -1
  100. package/dest/public/public_db_sources.d.ts +2 -2
  101. package/dest/public/public_db_sources.d.ts.map +1 -1
  102. package/dest/public/public_db_sources.js +1 -1
  103. package/dest/public/public_processor/guarded_merkle_tree.d.ts +2 -1
  104. package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -1
  105. package/dest/public/public_processor/guarded_merkle_tree.js +3 -0
  106. package/dest/public/public_processor/public_processor.d.ts +2 -2
  107. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  108. package/dest/public/public_processor/public_processor.js +23 -11
  109. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +1 -13
  110. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts.map +1 -1
  111. package/dest/public/public_tx_simulator/contract_provider_for_cpp.js +18 -53
  112. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +16 -1
  113. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts.map +1 -1
  114. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.js +41 -3
  115. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts +1 -1
  116. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.d.ts.map +1 -1
  117. package/dest/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.js +7 -6
  118. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts +22 -0
  119. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.d.ts.map +1 -0
  120. package/dest/public/public_tx_simulator/dumping_cpp_public_tx_simulator.js +52 -0
  121. package/dest/public/public_tx_simulator/factories.d.ts +13 -0
  122. package/dest/public/public_tx_simulator/factories.d.ts.map +1 -0
  123. package/dest/public/public_tx_simulator/factories.js +28 -0
  124. package/dest/public/public_tx_simulator/index.d.ts +3 -1
  125. package/dest/public/public_tx_simulator/index.d.ts.map +1 -1
  126. package/dest/public/public_tx_simulator/index.js +2 -0
  127. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +2 -2
  128. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts.map +1 -1
  129. package/dest/public/public_tx_simulator/public_tx_context.d.ts +2 -2
  130. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  131. package/dest/public/public_tx_simulator/public_tx_context.js +1 -1
  132. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +2 -2
  133. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  134. package/dest/public/public_tx_simulator/public_tx_simulator.js +5 -3
  135. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts +24 -1
  136. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts.map +1 -1
  137. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts +2 -2
  138. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts.map +1 -1
  139. package/dest/public/side_effect_trace.d.ts +2 -2
  140. package/dest/public/side_effect_trace.d.ts.map +1 -1
  141. package/dest/public/side_effect_trace.js +1 -1
  142. package/dest/public/side_effect_trace_interface.d.ts +2 -2
  143. package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
  144. package/dest/public/state_manager/nullifiers.d.ts +2 -2
  145. package/dest/public/state_manager/nullifiers.d.ts.map +1 -1
  146. package/dest/public/state_manager/public_storage.d.ts +2 -2
  147. package/dest/public/state_manager/public_storage.d.ts.map +1 -1
  148. package/dest/public/state_manager/public_storage.js +1 -1
  149. package/dest/public/state_manager/state_manager.d.ts +2 -2
  150. package/dest/public/state_manager/state_manager.d.ts.map +1 -1
  151. package/dest/public/state_manager/state_manager.js +1 -1
  152. package/package.json +17 -17
  153. package/src/common/errors.ts +1 -1
  154. package/src/private/acvm/deserialize.ts +1 -1
  155. package/src/private/acvm/serialize.ts +1 -1
  156. package/src/private/circuit_recording/circuit_recorder.ts +17 -16
  157. package/src/public/avm/avm_context.ts +1 -1
  158. package/src/public/avm/avm_contract_call_result.ts +1 -1
  159. package/src/public/avm/avm_execution_environment.ts +1 -1
  160. package/src/public/avm/avm_gas.ts +3 -3
  161. package/src/public/avm/avm_machine_state.ts +1 -1
  162. package/src/public/avm/avm_memory_types.ts +1 -1
  163. package/src/public/avm/avm_simulator.ts +1 -1
  164. package/src/public/avm/errors.ts +1 -1
  165. package/src/public/avm/fixtures/avm_simulation_tester.ts +1 -1
  166. package/src/public/avm/fixtures/base_avm_simulation_tester.ts +1 -1
  167. package/src/public/avm/fixtures/initializers.ts +1 -1
  168. package/src/public/avm/fixtures/utils.ts +1 -1
  169. package/src/public/avm/opcodes/arithmetic.ts +13 -1
  170. package/src/public/avm/opcodes/ec_add.ts +2 -2
  171. package/src/public/avm/opcodes/hashing.ts +10 -4
  172. package/src/public/avm/opcodes/memory.ts +1 -1
  173. package/src/public/avm/revert_reason.ts +1 -1
  174. package/src/public/avm/serialization/instruction_serialization.ts +1 -1
  175. package/src/public/avm/test_utils.ts +1 -1
  176. package/src/public/contracts_db_checkpoint.ts +1 -1
  177. package/src/public/db_interfaces.ts +1 -1
  178. package/src/public/debug_fn_name.ts +11 -4
  179. package/src/public/fixtures/amm_test.ts +2 -2
  180. package/src/public/fixtures/bulk_test.ts +7 -7
  181. package/src/public/fixtures/custom_bytecode_tester.ts +53 -19
  182. package/src/public/fixtures/custom_bytecode_tests.ts +93 -9
  183. package/src/public/fixtures/index.ts +7 -1
  184. package/src/public/fixtures/minimal_public_tx.ts +4 -13
  185. package/src/public/fixtures/opcode_spammer.ts +1638 -0
  186. package/src/public/fixtures/public_tx_simulation_tester.ts +40 -6
  187. package/src/public/fixtures/simple_contract_data_source.ts +1 -1
  188. package/src/public/fixtures/token_test.ts +1 -1
  189. package/src/public/fixtures/utils.ts +2 -3
  190. package/src/public/fuzzing/avm_fuzzer_simulator.ts +240 -0
  191. package/src/public/fuzzing/avm_simulator_bin.ts +140 -0
  192. package/src/public/hinting_db_sources.ts +6 -2
  193. package/src/public/index.ts +2 -0
  194. package/src/public/public_db_sources.ts +1 -1
  195. package/src/public/public_processor/guarded_merkle_tree.ts +4 -0
  196. package/src/public/public_processor/public_processor.ts +28 -12
  197. package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +19 -60
  198. package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +48 -3
  199. package/src/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.ts +1 -1
  200. package/src/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.ts +7 -10
  201. package/src/public/public_tx_simulator/dumping_cpp_public_tx_simulator.ts +81 -0
  202. package/src/public/public_tx_simulator/factories.ts +41 -0
  203. package/src/public/public_tx_simulator/index.ts +2 -0
  204. package/src/public/public_tx_simulator/measured_public_tx_simulator.ts +1 -1
  205. package/src/public/public_tx_simulator/public_tx_context.ts +1 -1
  206. package/src/public/public_tx_simulator/public_tx_simulator.ts +14 -3
  207. package/src/public/public_tx_simulator/public_tx_simulator_interface.ts +23 -0
  208. package/src/public/public_tx_simulator/telemetry_public_tx_simulator.ts +1 -1
  209. package/src/public/side_effect_trace.ts +1 -1
  210. package/src/public/side_effect_trace_interface.ts +1 -1
  211. package/src/public/state_manager/nullifiers.ts +1 -1
  212. package/src/public/state_manager/public_storage.ts +1 -1
  213. package/src/public/state_manager/state_manager.ts +2 -2
@@ -1,11 +1,10 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import { type Logger, 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';
6
6
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
7
7
  import { ContractDeploymentData } from '@aztec/stdlib/contract';
8
- import { ContractClassLog, ContractClassLogFields, PrivateLog } from '@aztec/stdlib/logs';
9
8
  import type { GlobalVariables } from '@aztec/stdlib/tx';
10
9
 
11
10
  import type { PublicContractsDB } from '../public_db_sources.js';
@@ -19,7 +18,7 @@ export class ContractProviderForCpp implements ContractProvider {
19
18
  ) {}
20
19
 
21
20
  public getContractInstance = async (address: string): Promise<Buffer | undefined> => {
22
- this.log.debug(`Contract provider callback: getContractInstance(${address})`);
21
+ this.log.trace(`Contract provider callback: getContractInstance(${address})`);
23
22
 
24
23
  const aztecAddr = AztecAddress.fromString(address);
25
24
 
@@ -34,7 +33,7 @@ export class ContractProviderForCpp implements ContractProvider {
34
33
  };
35
34
 
36
35
  public getContractClass = async (classId: string): Promise<Buffer | undefined> => {
37
- this.log.debug(`Contract provider callback: getContractClass(${classId})`);
36
+ this.log.trace(`Contract provider callback: getContractClass(${classId})`);
38
37
 
39
38
  // Parse classId string to Fr
40
39
  const classIdFr = Fr.fromString(classId);
@@ -51,20 +50,20 @@ export class ContractProviderForCpp implements ContractProvider {
51
50
  };
52
51
 
53
52
  public addContracts = async (contractDeploymentDataBuffer: Buffer): Promise<void> => {
54
- this.log.debug(`Contract provider callback: addContracts`);
53
+ this.log.trace(`Contract provider callback: addContracts`);
55
54
 
56
55
  const rawData: any = deserializeFromMessagePack(contractDeploymentDataBuffer);
57
56
 
58
- // Construct class instances using the from method
59
- const contractDeploymentData = this.reconstructContractDeploymentData(rawData);
57
+ // Construct ContractDeploymentData from plain object.
58
+ const contractDeploymentData = ContractDeploymentData.fromPlainObject(rawData);
60
59
 
61
60
  // Add contracts to the contracts DB
62
- this.log.debug(`Calling contractsDB.addContracts`);
61
+ this.log.trace(`Calling contractsDB.addContracts`);
63
62
  await this.contractsDB.addContracts(contractDeploymentData);
64
63
  };
65
64
 
66
65
  public getBytecodeCommitment = async (classId: string): Promise<Buffer | undefined> => {
67
- this.log.debug(`Contract provider callback: getBytecodeCommitment(${classId})`);
66
+ this.log.trace(`Contract provider callback: getBytecodeCommitment(${classId})`);
68
67
 
69
68
  // Parse classId string to Fr
70
69
  const classIdFr = Fr.fromString(classId);
@@ -82,18 +81,23 @@ export class ContractProviderForCpp implements ContractProvider {
82
81
  };
83
82
 
84
83
  public getDebugFunctionName = async (address: string, selector: string): Promise<string | undefined> => {
85
- this.log.debug(`Contract provider callback: getDebugFunctionName(${address}, ${selector})`);
84
+ this.log.trace(`Contract provider callback: getDebugFunctionName(${address}, ${selector})`);
86
85
 
87
86
  // Parse address and selector strings
88
87
  const aztecAddr = AztecAddress.fromString(address);
89
88
  const selectorFr = Fr.fromString(selector);
90
- const functionSelector = FunctionSelector.fromField(selectorFr);
89
+ const functionSelector = FunctionSelector.fromFieldOrUndefined(selectorFr);
90
+
91
+ if (!functionSelector) {
92
+ this.log.trace(`calldata[0] is not a function selector: ${selector}`);
93
+ return undefined;
94
+ }
91
95
 
92
96
  // Fetch debug function name from the contracts DB
93
97
  const name = await this.contractsDB.getDebugFunctionName(aztecAddr, functionSelector);
94
98
 
95
99
  if (!name) {
96
- this.log.debug(`Debug function name not found for ${address}:${selector}`);
100
+ this.log.trace(`Debug function name not found for ${address}:${selector}`);
97
101
  return undefined;
98
102
  }
99
103
 
@@ -101,62 +105,17 @@ export class ContractProviderForCpp implements ContractProvider {
101
105
  };
102
106
 
103
107
  public createCheckpoint = (): Promise<void> => {
104
- this.log.debug(`Contract provider callback: createCheckpoint`);
108
+ this.log.trace(`Contract provider callback: createCheckpoint`);
105
109
  return Promise.resolve(this.contractsDB.createCheckpoint());
106
110
  };
107
111
 
108
112
  public commitCheckpoint = (): Promise<void> => {
109
- this.log.debug(`Contract provider callback: commitCheckpoint`);
113
+ this.log.trace(`Contract provider callback: commitCheckpoint`);
110
114
  return Promise.resolve(this.contractsDB.commitCheckpoint());
111
115
  };
112
116
 
113
117
  public revertCheckpoint = (): Promise<void> => {
114
- this.log.debug(`Contract provider callback: revertCheckpoint`);
118
+ this.log.trace(`Contract provider callback: revertCheckpoint`);
115
119
  return Promise.resolve(this.contractsDB.revertCheckpoint());
116
120
  };
117
-
118
- /**
119
- * Reconstruct ContractDeploymentData from plain msgpack-deserialized objects.
120
- *
121
- * msgpackr does not automatically apply extensions to nested fields, so we need to
122
- * manually reconstruct ContractClassLog and PrivateLog instances with proper types.
123
- *
124
- * TODO(dbanks12): we really shouldn't have to do this.... We need to for now because
125
- * msgpack deserialization doesn't give us actual typed objects, but rather just JSON.
126
- * It would be easier if all types matched between languages (like AztecAddress which is just
127
- * FF in C++).
128
- */
129
- private reconstructContractDeploymentData(rawData: any): ContractDeploymentData {
130
- // Helper to ensure a value is an Fr instance
131
- const toFr = (value: any): Fr => {
132
- if (value instanceof Fr) {
133
- return value;
134
- }
135
- if (Buffer.isBuffer(value)) {
136
- return Fr.fromBuffer(value);
137
- }
138
- return new Fr(value);
139
- };
140
-
141
- // Reconstruct ContractClassLogs
142
- const contractClassLogs = (rawData.contractClassLogs || []).map((log: any) => {
143
- // Convert contractAddress to TS AztecAddress
144
- const addressFr = toFr(log.contractAddress);
145
- const address = AztecAddress.fromField(addressFr);
146
-
147
- // Ensure all fields are Fr instances
148
- const fields = (log.fields.fields || []).map((field: any) => toFr(field));
149
-
150
- // Create proper ContractClassLog instance
151
- return new ContractClassLog(address, new ContractClassLogFields(fields), log.emittedLength);
152
- });
153
-
154
- // Reconstruct PrivateLogs - ensure fields are Fr instances
155
- const privateLogs = (rawData.privateLogs || []).map((log: any) => {
156
- const fields = (log.fields || []).map((field: any) => toFr(field));
157
- return new PrivateLog(fields as any, log.emittedLength);
158
- });
159
-
160
- return new ContractDeploymentData(contractClassLogs, privateLogs);
161
- }
162
121
  }
@@ -1,5 +1,6 @@
1
1
  import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
2
- import { avmSimulate } from '@aztec/native';
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,6 +34,10 @@ 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,
@@ -85,12 +90,25 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
85
90
  this.log.trace(`Serializing fast simulation inputs to msgpack...`);
86
91
  const inputBuffer = fastSimInputs.serializeWithMessagePack();
87
92
 
93
+ // Create cancellation token for this simulation
94
+ this.cancellationToken = createCancellationToken();
95
+
96
+ // Store the promise so cancel() can wait for it
97
+ this.log.debug(`Calling C++ simulator for tx ${txHash}`);
98
+ this.simulationPromise = avmSimulate(inputBuffer, contractProvider, wsCppHandle, logLevel, this.cancellationToken);
99
+
88
100
  let resultBuffer: Buffer;
89
101
  try {
90
- this.log.debug(`Calling C++ simulator for tx ${txHash}`);
91
- resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle, logLevel);
102
+ resultBuffer = await this.simulationPromise;
92
103
  } catch (error: any) {
104
+ // Check if this was a cancellation
105
+ if (error.message?.includes('Simulation cancelled')) {
106
+ throw new SimulationError(`C++ simulation cancelled`, []);
107
+ }
93
108
  throw new SimulationError(`C++ simulation failed: ${error.message}`, []);
109
+ } finally {
110
+ this.cancellationToken = undefined;
111
+ this.simulationPromise = undefined;
94
112
  }
95
113
 
96
114
  // If we've reached this point, C++ succeeded during simulation,
@@ -109,6 +127,33 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
109
127
 
110
128
  return cppResult;
111
129
  }
130
+
131
+ /**
132
+ * Cancel the current simulation if one is in progress.
133
+ * This signals the C++ simulator to stop at the next opcode or before the next WorldState write.
134
+ * Safe to call even if no simulation is in progress.
135
+ *
136
+ * @param waitTimeoutMs - If provided, wait up to this many ms for the simulation to actually stop.
137
+ * This is important because C++ might be in the middle of a slow operation
138
+ * (e.g., pad_trees) and won't check the cancellation flag until it completes.
139
+ * Default timeout of 100ms after cancellation.
140
+ */
141
+ public async cancel(waitTimeoutMs: number = 100): Promise<void> {
142
+ if (this.cancellationToken) {
143
+ this.log.debug('Cancelling C++ simulation');
144
+ cancelSimulation(this.cancellationToken);
145
+ }
146
+
147
+ // Wait for the simulation to actually complete if not already done
148
+ if (this.simulationPromise) {
149
+ this.log.debug(`Waiting up to ${waitTimeoutMs}ms for C++ simulation to stop`);
150
+ await Promise.race([
151
+ this.simulationPromise.catch(() => {}), // Ignore rejection, just wait for completion
152
+ sleep(waitTimeoutMs),
153
+ ]);
154
+ this.log.debug('C++ simulation stopped or wait timed out');
155
+ }
156
+ }
112
157
  }
113
158
 
114
159
  export class MeasuredCppPublicTxSimulator extends CppPublicTxSimulator implements MeasuredPublicTxSimulatorInterface {
@@ -64,7 +64,7 @@ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements
64
64
  this.log.debug(`TS simulation succeeded for tx ${txHash}`);
65
65
 
66
66
  // Extract the full AvmCircuitInputs from the TS result
67
- const avmCircuitInputs = new AvmCircuitInputs(tsResult.hints!, tsResult.publicInputs);
67
+ const avmCircuitInputs = new AvmCircuitInputs(tsResult.hints!, tsResult.publicInputs!);
68
68
 
69
69
  // Second, run C++ simulation with hinted DBs
70
70
  this.log.debug(`Running C++ simulation with hinted DBs for tx ${txHash}`);
@@ -1,5 +1,4 @@
1
1
  import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
2
- import { writeTestData } from '@aztec/foundation/testing/files';
3
2
  import { avmSimulate } from '@aztec/native';
4
3
  import { ProtocolContractsList } from '@aztec/protocol-contracts';
5
4
  import {
@@ -40,7 +39,7 @@ export class CppVsTsPublicTxSimulator extends PublicTxSimulator implements Publi
40
39
  config?: Partial<PublicSimulatorConfig>,
41
40
  ) {
42
41
  super(merkleTree, contractsDB, globalVariables, config);
43
- this.log = createLogger(`simulator:cpp_public_tx_simulator`);
42
+ this.log = createLogger(`simulator:cpp_vs_public_tx_simulator`);
44
43
  }
45
44
 
46
45
  /**
@@ -126,12 +125,6 @@ export class CppVsTsPublicTxSimulator extends PublicTxSimulator implements Publi
126
125
  // Deserialize the msgpack result
127
126
  this.log.debug(`Deserializing C++ from buffer (size: ${resultBuffer.length})...`);
128
127
  const cppResultJSON: object = deserializeFromMessagePack(resultBuffer);
129
- // Write testdata if AZTEC_WRITE_TESTDATA=1.
130
- writeTestData(
131
- `barretenberg/cpp/src/barretenberg/vm2/testing/tx_result_${txHash}.testdata.bin`,
132
- resultBuffer,
133
- /*raw=*/ true,
134
- );
135
128
  this.log.debug(`Deserializing C++ result to PublicTxResult...`);
136
129
  const cppResult = PublicTxResult.fromPlainObject(cppResultJSON);
137
130
  this.log.debug(`Done.`);
@@ -140,7 +133,10 @@ export class CppVsTsPublicTxSimulator extends PublicTxSimulator implements Publi
140
133
  assert(cppResult.gasUsed.publicGas.equals(tsResult.gasUsed.publicGas));
141
134
  assert(cppResult.gasUsed.teardownGas.equals(tsResult.gasUsed.teardownGas));
142
135
  assert(cppResult.gasUsed.billedGas.equals(tsResult.gasUsed.billedGas));
143
- assert(cppResult.publicInputs.toBuffer().equals(tsResult.publicInputs.toBuffer()));
136
+ assert(cppResult.publicTxEffect.equals(tsResult.publicTxEffect));
137
+ if (cppResult.publicInputs !== undefined) {
138
+ assert(cppResult.publicInputs!.toBuffer().equals(tsResult.publicInputs!.toBuffer()));
139
+ }
144
140
 
145
141
  // TODO(fcarreiro): complete this.
146
142
  // Check that C++ hints are a strict subset of TS hints.
@@ -209,7 +205,8 @@ export class CppVsTsPublicTxSimulator extends PublicTxSimulator implements Publi
209
205
  cppGasUsed: cppResult.gasUsed.totalGas.l2Gas,
210
206
  });
211
207
 
212
- return tsResult;
208
+ // Return cpp result as it has more detailed metadata / revert reasons
209
+ return cppResult;
213
210
  }
214
211
  }
215
212
 
@@ -0,0 +1,81 @@
1
+ import {
2
+ AvmCircuitInputs,
3
+ AvmCircuitPublicInputs,
4
+ AvmExecutionHints,
5
+ type PublicSimulatorConfig,
6
+ PublicTxResult,
7
+ serializeWithMessagePack,
8
+ } from '@aztec/stdlib/avm';
9
+ import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
10
+ import type { GlobalVariables, Tx, TxHash } from '@aztec/stdlib/tx';
11
+
12
+ import { strict as assert } from 'assert';
13
+ import { mkdirSync, writeFileSync } from 'fs';
14
+ import { join } from 'path';
15
+
16
+ import type { PublicContractsDB } from '../public_db_sources.js';
17
+ import { CppPublicTxSimulator } from './cpp_public_tx_simulator.js';
18
+
19
+ /**
20
+ * A C++ public tx simulator that dumps AVM circuit inputs to disk after simulation.
21
+ * Used during nightly CI runs to collect circuit inputs for benchmarking.
22
+ */
23
+ export class DumpingCppPublicTxSimulator extends CppPublicTxSimulator {
24
+ private readonly outputDir: string;
25
+
26
+ constructor(
27
+ merkleTree: MerkleTreeWriteOperations,
28
+ contractsDB: PublicContractsDB,
29
+ globalVariables: GlobalVariables,
30
+ config: Partial<PublicSimulatorConfig>,
31
+ outputDir: string,
32
+ ) {
33
+ super(merkleTree, contractsDB, globalVariables, config);
34
+ assert(config.collectHints === true, 'collectHints must be enabled to dump AVM circuit inputs');
35
+ assert(config.collectPublicInputs === true, 'collectPublicInputs must be enabled to dump AVM circuit inputs');
36
+ this.outputDir = outputDir;
37
+ }
38
+
39
+ public override async simulate(tx: Tx): Promise<PublicTxResult> {
40
+ const result = await super.simulate(tx);
41
+
42
+ // Dump the circuit inputs after successful simulation
43
+ const txHash = this.computeTxHash(tx);
44
+ this.dumpAvmCircuitInputs(result, txHash);
45
+
46
+ return result;
47
+ }
48
+
49
+ /**
50
+ * Dumps AVM circuit inputs to disk.
51
+ *
52
+ * @param result - The simulation result containing hints and public inputs
53
+ * @param txHash - The transaction hash to use in the filename
54
+ */
55
+ private dumpAvmCircuitInputs(result: PublicTxResult, txHash: TxHash): void {
56
+ try {
57
+ // Ensure the output directory exists
58
+ mkdirSync(this.outputDir, { recursive: true });
59
+
60
+ // Generate filename using transaction hash
61
+ const filename = `avm-circuit-inputs-tx-${txHash.toString()}.bin`;
62
+ const filepath = join(this.outputDir, filename);
63
+
64
+ // Create circuit inputs from the result
65
+ const hints = result.hints ?? AvmExecutionHints.empty();
66
+ const publicInputs = result.publicInputs ?? AvmCircuitPublicInputs.empty();
67
+ const avmCircuitInputs = new AvmCircuitInputs(hints, publicInputs);
68
+
69
+ // Serialize the circuit inputs using MessagePack
70
+ const serialized = serializeWithMessagePack(avmCircuitInputs);
71
+
72
+ // Write to disk
73
+ writeFileSync(filepath, serialized);
74
+
75
+ this.log.debug(`Dumped AVM circuit inputs to ${filepath}`);
76
+ } catch (error) {
77
+ // Non-blocking error handling - log but don't interrupt processing
78
+ this.log.warn(`Failed to dump AVM circuit inputs for tx ${txHash.toString()}: ${error}`);
79
+ }
80
+ }
81
+ }
@@ -0,0 +1,41 @@
1
+ import { PublicSimulatorConfig } from '@aztec/stdlib/avm';
2
+ import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
3
+ import type { GlobalVariables } from '@aztec/stdlib/tx';
4
+ import type { TelemetryClient } from '@aztec/telemetry-client';
5
+
6
+ import type { PublicContractsDB } from '../public_db_sources.js';
7
+ import { TelemetryCppPublicTxSimulator } from './cpp_public_tx_simulator.js';
8
+ import { DumpingCppPublicTxSimulator } from './dumping_cpp_public_tx_simulator.js';
9
+
10
+ /**
11
+ * Creates a public tx simulator for block building.
12
+ * Uses DumpingCppPublicTxSimulator if DUMP_AVM_INPUTS_TO_DIR env var is set (for CI/testing avm circuit),
13
+ * otherwise uses TelemetryCppPublicTxSimulator (for production).
14
+ */
15
+ export function createPublicTxSimulatorForBlockBuilding(
16
+ merkleTree: MerkleTreeWriteOperations,
17
+ contractsDB: PublicContractsDB,
18
+ globalVariables: GlobalVariables,
19
+ telemetryClient: TelemetryClient,
20
+ ) {
21
+ const config = PublicSimulatorConfig.from({
22
+ skipFeeEnforcement: false,
23
+ collectDebugLogs: false,
24
+ collectHints: false,
25
+ collectPublicInputs: false,
26
+ collectStatistics: false,
27
+ collectCallMetadata: false,
28
+ });
29
+
30
+ const dumpDir = process.env.DUMP_AVM_INPUTS_TO_DIR;
31
+ if (dumpDir) {
32
+ // must collect hints and PIs for dumping
33
+ const dumpingConfig = {
34
+ ...config,
35
+ collectHints: true,
36
+ collectPublicInputs: true,
37
+ };
38
+ return new DumpingCppPublicTxSimulator(merkleTree, contractsDB, globalVariables, dumpingConfig, dumpDir);
39
+ }
40
+ return new TelemetryCppPublicTxSimulator(merkleTree, contractsDB, globalVariables, telemetryClient, config);
41
+ }
@@ -1,5 +1,7 @@
1
1
  export * from './public_tx_simulator.js';
2
2
  export { CppPublicTxSimulator, TelemetryCppPublicTxSimulator } from './cpp_public_tx_simulator.js';
3
+ export { DumpingCppPublicTxSimulator } from './dumping_cpp_public_tx_simulator.js';
4
+ export { createPublicTxSimulatorForBlockBuilding } from './factories.js';
3
5
  export type { PublicTxSimulatorInterface } from './public_tx_simulator_interface.js';
4
6
  export { TelemetryPublicTxSimulator } from './telemetry_public_tx_simulator.js';
5
7
  export type { PublicTxResult, PublicSimulatorConfig as PublicTxSimulatorConfig } from '@aztec/stdlib/avm';
@@ -1,4 +1,4 @@
1
- import type { Fr } from '@aztec/foundation/fields';
1
+ import type { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import { Timer } from '@aztec/foundation/timer';
3
3
  import type { PublicSimulatorConfig, PublicTxResult } from '@aztec/stdlib/avm';
4
4
  import type { Gas } from '@aztec/stdlib/gas';
@@ -6,7 +6,7 @@ import {
6
6
  MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
7
7
  } from '@aztec/constants';
8
8
  import { padArrayEnd } from '@aztec/foundation/collection';
9
- import { Fr } from '@aztec/foundation/fields';
9
+ import { Fr } from '@aztec/foundation/curves/bn254';
10
10
  import { type Logger, createLogger } from '@aztec/foundation/log';
11
11
  import {
12
12
  AvmAccumulatedData,
@@ -1,9 +1,9 @@
1
1
  import { AVM_MAX_PROCESSABLE_L2_GAS } from '@aztec/constants';
2
- import { Fr } from '@aztec/foundation/fields';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { type Logger, createLogger } from '@aztec/foundation/log';
4
4
  import { ProtocolContractAddress, ProtocolContractsList } from '@aztec/protocol-contracts';
5
5
  import { computeFeePayerBalanceStorageSlot } from '@aztec/protocol-contracts/fee-juice';
6
- import { AvmExecutionHints, AvmTxHint, PublicSimulatorConfig, PublicTxResult } from '@aztec/stdlib/avm';
6
+ import { AvmExecutionHints, AvmTxHint, PublicSimulatorConfig, PublicTxEffect, PublicTxResult } from '@aztec/stdlib/avm';
7
7
  import { SimulationError } from '@aztec/stdlib/errors';
8
8
  import { Gas } from '@aztec/stdlib/gas';
9
9
  import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
@@ -209,6 +209,16 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
209
209
  (appLogicReturnValues as any).revertReason = context.revertReason;
210
210
  }
211
211
 
212
+ // Create PublicTxEffect from PublicInputs.
213
+ const publicTxEffect = new PublicTxEffect(
214
+ publicInputs.transactionFee,
215
+ publicInputs.accumulatedData.noteHashes.filter(h => !h.isEmpty()),
216
+ publicInputs.accumulatedData.nullifiers.filter(n => !n.isEmpty()),
217
+ publicInputs.accumulatedData.l2ToL1Msgs.filter(m => !m.isEmpty()),
218
+ publicInputs.accumulatedData.publicLogs.toLogs(),
219
+ publicInputs.accumulatedData.publicDataWrites.filter(w => !w.isEmpty()),
220
+ );
221
+
212
222
  return new PublicTxResult(
213
223
  /*gasUsed=*/ {
214
224
  totalGas: context.getActualGasUsed(),
@@ -217,6 +227,7 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
217
227
  billedGas: context.getTotalGasUsed(),
218
228
  },
219
229
  /*revertCode=*/ revertCode,
230
+ /*publicTxEffect=*/ publicTxEffect,
220
231
  /*callStackMetadata=*/ appLogicReturnValues,
221
232
  /*logs=*/ context.state.getActiveStateManager().getLogs(),
222
233
  /*hints=*/ hints,
@@ -303,7 +314,7 @@ export class PublicTxSimulator implements PublicTxSimulatorInterface {
303
314
  );
304
315
 
305
316
  if (result.reverted) {
306
- const culprit = `${contractAddress}:${callRequest.functionSelector}`;
317
+ const culprit = `${contractAddress}:${fnName}`;
307
318
  context.revert(phase, result.revertReason, culprit);
308
319
  }
309
320
 
@@ -3,8 +3,31 @@ import type { Tx } from '@aztec/stdlib/tx';
3
3
 
4
4
  export interface PublicTxSimulatorInterface {
5
5
  simulate(tx: Tx): Promise<PublicTxResult>;
6
+ /**
7
+ * Cancel the current simulation if one is in progress.
8
+ * This signals the underlying simulator (e.g., C++) to stop at the next safe point.
9
+ * Safe to call even if no simulation is in progress.
10
+ * Optional - not all implementations support cancellation.
11
+ *
12
+ * @param waitTimeoutMs - If provided, wait up to this many ms for the simulation to actually stop.
13
+ * This is important because signaling cancellation doesn't immediately stop C++ -
14
+ * it only sets a flag that C++ checks at certain points. If C++ is in the middle
15
+ * of a slow operation (e.g., pad_trees), it won't stop until that completes.
16
+ * @returns Promise that resolves when cancellation is signaled (and optionally when simulation stops)
17
+ */
18
+ cancel?(waitTimeoutMs?: number): Promise<void>;
6
19
  }
7
20
 
8
21
  export interface MeasuredPublicTxSimulatorInterface {
9
22
  simulate(tx: Tx, txLabel: string): Promise<PublicTxResult>;
23
+ /**
24
+ * Cancel the current simulation if one is in progress.
25
+ * This signals the underlying simulator (e.g., C++) to stop at the next safe point.
26
+ * Safe to call even if no simulation is in progress.
27
+ * Optional - not all implementations support cancellation.
28
+ *
29
+ * @param waitTimeoutMs - If provided, wait up to this many ms for the simulation to actually stop.
30
+ * @returns Promise that resolves when cancellation is signaled (and optionally when simulation stops)
31
+ */
32
+ cancel?(waitTimeoutMs?: number): Promise<void>;
10
33
  }
@@ -1,4 +1,4 @@
1
- import type { Fr } from '@aztec/foundation/fields';
1
+ import type { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import type { PublicSimulatorConfig } from '@aztec/stdlib/avm';
3
3
  import type { Gas } from '@aztec/stdlib/gas';
4
4
  import type { MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
@@ -7,8 +7,8 @@ import {
7
7
  MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
8
8
  PROTOCOL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
9
9
  } from '@aztec/constants';
10
+ import { Fr } from '@aztec/foundation/curves/bn254';
10
11
  import { EthAddress } from '@aztec/foundation/eth-address';
11
- import { Fr } from '@aztec/foundation/fields';
12
12
  import { type LogLevel, createLogger } from '@aztec/foundation/log';
13
13
  import { PublicDataUpdateRequest } from '@aztec/stdlib/avm';
14
14
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
@@ -1,4 +1,4 @@
1
- import type { Fr } from '@aztec/foundation/fields';
1
+ import type { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import type { LogLevel } from '@aztec/foundation/log';
3
3
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
4
  import type { DebugLog } from '@aztec/stdlib/logs';
@@ -1,4 +1,4 @@
1
- import type { Fr } from '@aztec/foundation/fields';
1
+ import type { Fr } from '@aztec/foundation/curves/bn254';
2
2
 
3
3
  import type { PublicTreesDB } from '../public_db_sources.js';
4
4
  import { NullifierCollisionError } from '../side_effect_errors.js';
@@ -1,4 +1,4 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
3
3
 
4
4
  import type { PublicStateDBInterface } from '../db_interfaces.js';
@@ -6,8 +6,8 @@ import {
6
6
  MULTI_CALL_ENTRYPOINT_ADDRESS,
7
7
  ROUTER_ADDRESS,
8
8
  } from '@aztec/constants';
9
- import { poseidon2Hash } from '@aztec/foundation/crypto';
10
- import { Fr } from '@aztec/foundation/fields';
9
+ import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
10
+ import { Fr } from '@aztec/foundation/curves/bn254';
11
11
  import { jsonStringify } from '@aztec/foundation/json-rpc';
12
12
  import { type LogLevel, createLogger } from '@aztec/foundation/log';
13
13
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';