@aztec/simulator 0.0.1-commit.b655e406 → 0.0.1-commit.b9865e97

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 (380) hide show
  1. package/README.md +6 -4
  2. package/dest/client.d.ts +1 -1
  3. package/dest/common/errors.d.ts +2 -2
  4. package/dest/common/errors.d.ts.map +1 -1
  5. package/dest/common/index.d.ts +1 -1
  6. package/dest/common/stats/index.d.ts +1 -1
  7. package/dest/common/stats/stats.d.ts +1 -1
  8. package/dest/private/acvm/acvm.d.ts +4 -2
  9. package/dest/private/acvm/acvm.d.ts.map +1 -1
  10. package/dest/private/acvm/acvm.js +4 -3
  11. package/dest/private/acvm/acvm_types.d.ts +1 -1
  12. package/dest/private/acvm/deserialize.d.ts +2 -2
  13. package/dest/private/acvm/deserialize.d.ts.map +1 -1
  14. package/dest/private/acvm/deserialize.js +1 -1
  15. package/dest/private/acvm/index.d.ts +1 -1
  16. package/dest/private/acvm/serialize.d.ts +2 -2
  17. package/dest/private/acvm/serialize.d.ts.map +1 -1
  18. package/dest/private/acvm/serialize.js +1 -1
  19. package/dest/private/acvm_native.d.ts +5 -3
  20. package/dest/private/acvm_native.d.ts.map +1 -1
  21. package/dest/private/acvm_native.js +8 -6
  22. package/dest/private/acvm_wasm.d.ts +4 -3
  23. package/dest/private/acvm_wasm.d.ts.map +1 -1
  24. package/dest/private/acvm_wasm.js +7 -5
  25. package/dest/private/acvm_wasm_with_blobs.d.ts +1 -1
  26. package/dest/private/acvm_wasm_with_blobs.d.ts.map +1 -1
  27. package/dest/private/circuit_recording/circuit_recorder.d.ts +4 -3
  28. package/dest/private/circuit_recording/circuit_recorder.d.ts.map +1 -1
  29. package/dest/private/circuit_recording/circuit_recorder.js +23 -20
  30. package/dest/private/circuit_recording/file_circuit_recorder.d.ts +3 -2
  31. package/dest/private/circuit_recording/file_circuit_recorder.d.ts.map +1 -1
  32. package/dest/private/circuit_recording/file_circuit_recorder.js +2 -2
  33. package/dest/private/circuit_recording/memory_circuit_recorder.d.ts +7 -2
  34. package/dest/private/circuit_recording/memory_circuit_recorder.d.ts.map +1 -1
  35. package/dest/private/circuit_recording/memory_circuit_recorder.js +4 -4
  36. package/dest/private/circuit_recording/simulator_recorder_wrapper.d.ts +1 -1
  37. package/dest/private/circuit_recording/simulator_recorder_wrapper.d.ts.map +1 -1
  38. package/dest/private/circuit_simulator.d.ts +1 -1
  39. package/dest/private/factory.d.ts +3 -3
  40. package/dest/private/factory.d.ts.map +1 -1
  41. package/dest/private/factory.js +7 -4
  42. package/dest/public/avm/testing/account_proof_fetcher.d.ts +2 -0
  43. package/dest/public/avm/testing/account_proof_fetcher.d.ts.map +1 -0
  44. package/dest/public/avm/testing/account_proof_fetcher.js +152 -0
  45. package/dest/public/avm/{fixtures → testing}/base_avm_simulation_tester.d.ts +3 -2
  46. package/dest/public/avm/testing/base_avm_simulation_tester.d.ts.map +1 -0
  47. package/dest/public/avm/{fixtures → testing}/base_avm_simulation_tester.js +18 -3
  48. package/dest/public/avm/{fixtures → testing}/utils.d.ts +4 -10
  49. package/dest/public/avm/testing/utils.d.ts.map +1 -0
  50. package/dest/public/avm/{fixtures → testing}/utils.js +6 -35
  51. package/dest/public/contracts_db_checkpoint.d.ts +16 -0
  52. package/dest/public/contracts_db_checkpoint.d.ts.map +1 -0
  53. package/dest/public/contracts_db_checkpoint.js +30 -0
  54. package/dest/public/db_interfaces.d.ts +14 -2
  55. package/dest/public/db_interfaces.d.ts.map +1 -1
  56. package/dest/public/executor_metrics.d.ts +3 -2
  57. package/dest/public/executor_metrics.d.ts.map +1 -1
  58. package/dest/public/executor_metrics.js +13 -34
  59. package/dest/public/executor_metrics_interface.d.ts +3 -2
  60. package/dest/public/executor_metrics_interface.d.ts.map +1 -1
  61. package/dest/public/fixtures/amm_test.d.ts +1 -1
  62. package/dest/public/fixtures/amm_test.js +4 -4
  63. package/dest/public/fixtures/bulk_test.d.ts +3 -3
  64. package/dest/public/fixtures/bulk_test.d.ts.map +1 -1
  65. package/dest/public/fixtures/bulk_test.js +36 -73
  66. package/dest/public/fixtures/custom_bytecode_tester.d.ts +29 -7
  67. package/dest/public/fixtures/custom_bytecode_tester.d.ts.map +1 -1
  68. package/dest/public/fixtures/custom_bytecode_tester.js +36 -12
  69. package/dest/public/fixtures/index.d.ts +2 -3
  70. package/dest/public/fixtures/index.d.ts.map +1 -1
  71. package/dest/public/fixtures/index.js +1 -2
  72. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +30 -11
  73. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  74. package/dest/public/fixtures/public_tx_simulation_tester.js +98 -31
  75. package/dest/public/fixtures/simple_contract_data_source.d.ts +5 -4
  76. package/dest/public/fixtures/simple_contract_data_source.d.ts.map +1 -1
  77. package/dest/public/fixtures/simple_contract_data_source.js +5 -5
  78. package/dest/public/fixtures/token_test.d.ts +6 -2
  79. package/dest/public/fixtures/token_test.d.ts.map +1 -1
  80. package/dest/public/fixtures/token_test.js +13 -12
  81. package/dest/public/fixtures/utils.d.ts +3 -3
  82. package/dest/public/fixtures/utils.d.ts.map +1 -1
  83. package/dest/public/fixtures/utils.js +23 -23
  84. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts +62 -0
  85. package/dest/public/fuzzing/avm_fuzzer_simulator.d.ts.map +1 -0
  86. package/dest/public/fuzzing/avm_fuzzer_simulator.js +176 -0
  87. package/dest/public/fuzzing/avm_simulator_bin.d.ts +2 -0
  88. package/dest/public/fuzzing/avm_simulator_bin.d.ts.map +1 -0
  89. package/dest/public/fuzzing/avm_simulator_bin.js +118 -0
  90. package/dest/public/index.d.ts +3 -2
  91. package/dest/public/index.d.ts.map +1 -1
  92. package/dest/public/index.js +1 -1
  93. package/dest/public/public_db_sources.d.ts +29 -66
  94. package/dest/public/public_db_sources.d.ts.map +1 -1
  95. package/dest/public/public_db_sources.js +83 -134
  96. package/dest/public/public_errors.d.ts +1 -1
  97. package/dest/public/public_errors.d.ts.map +1 -1
  98. package/dest/public/public_processor/guarded_merkle_tree.d.ts +8 -5
  99. package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -1
  100. package/dest/public/public_processor/guarded_merkle_tree.js +12 -4
  101. package/dest/public/public_processor/public_processor.d.ts +19 -21
  102. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  103. package/dest/public/public_processor/public_processor.js +553 -123
  104. package/dest/public/public_processor/public_processor_metrics.d.ts +5 -2
  105. package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -1
  106. package/dest/public/public_processor/public_processor_metrics.js +36 -45
  107. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts +19 -0
  108. package/dest/public/public_tx_simulator/contract_provider_for_cpp.d.ts.map +1 -0
  109. package/dest/public/public_tx_simulator/contract_provider_for_cpp.js +99 -0
  110. package/dest/public/public_tx_simulator/dumping_public_tx_simulator.d.ts +23 -0
  111. package/dest/public/public_tx_simulator/dumping_public_tx_simulator.d.ts.map +1 -0
  112. package/dest/public/public_tx_simulator/dumping_public_tx_simulator.js +52 -0
  113. package/dest/public/public_tx_simulator/factories.d.ts +14 -0
  114. package/dest/public/public_tx_simulator/factories.d.ts.map +1 -0
  115. package/dest/public/public_tx_simulator/factories.js +28 -0
  116. package/dest/public/public_tx_simulator/index.d.ts +5 -3
  117. package/dest/public/public_tx_simulator/index.d.ts.map +1 -1
  118. package/dest/public/public_tx_simulator/index.js +3 -2
  119. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +42 -89
  120. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  121. package/dest/public/public_tx_simulator/public_tx_simulator.js +101 -334
  122. package/dest/public/public_tx_simulator/public_tx_simulator_base.d.ts +22 -0
  123. package/dest/public/public_tx_simulator/public_tx_simulator_base.d.ts.map +1 -0
  124. package/dest/public/public_tx_simulator/public_tx_simulator_base.js +28 -0
  125. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts +25 -2
  126. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts.map +1 -1
  127. package/dest/public/side_effect_errors.d.ts +1 -1
  128. package/dest/public/side_effect_errors.d.ts.map +1 -1
  129. package/dest/public/test_executor_metrics.d.ts +11 -4
  130. package/dest/public/test_executor_metrics.d.ts.map +1 -1
  131. package/dest/public/test_executor_metrics.js +29 -6
  132. package/dest/public/unique_class_ids.d.ts +1 -1
  133. package/dest/public/unique_class_ids.d.ts.map +1 -1
  134. package/dest/public/utils.d.ts +1 -1
  135. package/dest/server.d.ts +1 -1
  136. package/dest/testing.d.ts +1 -1
  137. package/package.json +22 -22
  138. package/src/common/errors.ts +1 -1
  139. package/src/private/acvm/acvm.ts +4 -3
  140. package/src/private/acvm/deserialize.ts +1 -1
  141. package/src/private/acvm/serialize.ts +1 -1
  142. package/src/private/acvm_native.ts +11 -5
  143. package/src/private/acvm_wasm.ts +11 -4
  144. package/src/private/circuit_recording/circuit_recorder.ts +24 -21
  145. package/src/private/circuit_recording/file_circuit_recorder.ts +7 -2
  146. package/src/private/circuit_recording/memory_circuit_recorder.ts +6 -4
  147. package/src/private/factory.ts +7 -4
  148. package/src/public/avm/testing/account_proof.json +553 -0
  149. package/src/public/avm/testing/account_proof_fetcher.ts +166 -0
  150. package/src/public/avm/{fixtures → testing}/base_avm_simulation_tester.ts +23 -3
  151. package/src/public/avm/{fixtures → testing}/utils.ts +6 -41
  152. package/src/public/contracts_db_checkpoint.ts +41 -0
  153. package/src/public/db_interfaces.ts +16 -1
  154. package/src/public/executor_metrics.ts +11 -34
  155. package/src/public/executor_metrics_interface.ts +2 -1
  156. package/src/public/fixtures/amm_test.ts +4 -4
  157. package/src/public/fixtures/bulk_test.ts +38 -13
  158. package/src/public/fixtures/custom_bytecode_tester.ts +54 -20
  159. package/src/public/fixtures/index.ts +5 -2
  160. package/src/public/fixtures/public_tx_simulation_tester.ts +129 -27
  161. package/src/public/fixtures/simple_contract_data_source.ts +9 -10
  162. package/src/public/fixtures/token_test.ts +18 -10
  163. package/src/public/fixtures/utils.ts +28 -25
  164. package/src/public/fuzzing/avm_fuzzer_simulator.ts +285 -0
  165. package/src/public/fuzzing/avm_simulator_bin.ts +173 -0
  166. package/src/public/index.ts +4 -2
  167. package/src/public/public_db_sources.ts +112 -174
  168. package/src/public/public_processor/guarded_merkle_tree.ts +14 -6
  169. package/src/public/public_processor/public_processor.ts +225 -136
  170. package/src/public/public_processor/public_processor_metrics.ts +28 -44
  171. package/src/public/public_tx_simulator/contract_provider_for_cpp.ts +125 -0
  172. package/src/public/public_tx_simulator/dumping_public_tx_simulator.ts +83 -0
  173. package/src/public/public_tx_simulator/factories.ts +44 -0
  174. package/src/public/public_tx_simulator/index.ts +4 -2
  175. package/src/public/public_tx_simulator/public_tx_simulator.ts +153 -500
  176. package/src/public/public_tx_simulator/public_tx_simulator_base.ts +35 -0
  177. package/src/public/public_tx_simulator/public_tx_simulator_interface.ts +24 -2
  178. package/src/public/test_executor_metrics.ts +34 -8
  179. package/dest/public/avm/avm_context.d.ts +0 -41
  180. package/dest/public/avm/avm_context.d.ts.map +0 -1
  181. package/dest/public/avm/avm_context.js +0 -44
  182. package/dest/public/avm/avm_contract_call_result.d.ts +0 -32
  183. package/dest/public/avm/avm_contract_call_result.d.ts.map +0 -1
  184. package/dest/public/avm/avm_contract_call_result.js +0 -54
  185. package/dest/public/avm/avm_execution_environment.d.ts +0 -23
  186. package/dest/public/avm/avm_execution_environment.d.ts.map +0 -1
  187. package/dest/public/avm/avm_execution_environment.js +0 -36
  188. package/dest/public/avm/avm_gas.d.ts +0 -44
  189. package/dest/public/avm/avm_gas.d.ts.map +0 -1
  190. package/dest/public/avm/avm_gas.js +0 -195
  191. package/dest/public/avm/avm_machine_state.d.ts +0 -97
  192. package/dest/public/avm/avm_machine_state.d.ts.map +0 -1
  193. package/dest/public/avm/avm_machine_state.js +0 -109
  194. package/dest/public/avm/avm_memory_types.d.ts +0 -275
  195. package/dest/public/avm/avm_memory_types.d.ts.map +0 -1
  196. package/dest/public/avm/avm_memory_types.js +0 -351
  197. package/dest/public/avm/avm_simulator.d.ts +0 -39
  198. package/dest/public/avm/avm_simulator.d.ts.map +0 -1
  199. package/dest/public/avm/avm_simulator.js +0 -190
  200. package/dest/public/avm/avm_simulator_interface.d.ts +0 -9
  201. package/dest/public/avm/avm_simulator_interface.d.ts.map +0 -1
  202. package/dest/public/avm/avm_simulator_interface.js +0 -3
  203. package/dest/public/avm/errors.d.ts +0 -98
  204. package/dest/public/avm/errors.d.ts.map +0 -1
  205. package/dest/public/avm/errors.js +0 -129
  206. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts +0 -22
  207. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +0 -1
  208. package/dest/public/avm/fixtures/avm_simulation_tester.js +0 -74
  209. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +0 -1
  210. package/dest/public/avm/fixtures/initializers.d.ts +0 -43
  211. package/dest/public/avm/fixtures/initializers.d.ts.map +0 -1
  212. package/dest/public/avm/fixtures/initializers.js +0 -42
  213. package/dest/public/avm/fixtures/utils.d.ts.map +0 -1
  214. package/dest/public/avm/index.d.ts +0 -2
  215. package/dest/public/avm/index.d.ts.map +0 -1
  216. package/dest/public/avm/index.js +0 -1
  217. package/dest/public/avm/opcodes/accrued_substate.d.ts +0 -75
  218. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +0 -1
  219. package/dest/public/avm/opcodes/accrued_substate.js +0 -255
  220. package/dest/public/avm/opcodes/addressing_mode.d.ts +0 -31
  221. package/dest/public/avm/opcodes/addressing_mode.d.ts.map +0 -1
  222. package/dest/public/avm/opcodes/addressing_mode.js +0 -94
  223. package/dest/public/avm/opcodes/arithmetic.d.ts +0 -47
  224. package/dest/public/avm/opcodes/arithmetic.d.ts.map +0 -1
  225. package/dest/public/avm/opcodes/arithmetic.js +0 -87
  226. package/dest/public/avm/opcodes/bitwise.d.ts +0 -42
  227. package/dest/public/avm/opcodes/bitwise.d.ts.map +0 -1
  228. package/dest/public/avm/opcodes/bitwise.js +0 -99
  229. package/dest/public/avm/opcodes/comparators.d.ts +0 -25
  230. package/dest/public/avm/opcodes/comparators.d.ts.map +0 -1
  231. package/dest/public/avm/opcodes/comparators.js +0 -43
  232. package/dest/public/avm/opcodes/contract.d.ts +0 -20
  233. package/dest/public/avm/opcodes/contract.d.ts.map +0 -1
  234. package/dest/public/avm/opcodes/contract.js +0 -65
  235. package/dest/public/avm/opcodes/control_flow.d.ts +0 -41
  236. package/dest/public/avm/opcodes/control_flow.d.ts.map +0 -1
  237. package/dest/public/avm/opcodes/control_flow.js +0 -107
  238. package/dest/public/avm/opcodes/conversion.d.ts +0 -18
  239. package/dest/public/avm/opcodes/conversion.d.ts.map +0 -1
  240. package/dest/public/avm/opcodes/conversion.js +0 -333
  241. package/dest/public/avm/opcodes/ec_add.d.ts +0 -19
  242. package/dest/public/avm/opcodes/ec_add.d.ts.map +0 -1
  243. package/dest/public/avm/opcodes/ec_add.js +0 -84
  244. package/dest/public/avm/opcodes/environment_getters.d.ts +0 -28
  245. package/dest/public/avm/opcodes/environment_getters.d.ts.map +0 -1
  246. package/dest/public/avm/opcodes/environment_getters.js +0 -77
  247. package/dest/public/avm/opcodes/external_calls.d.ts +0 -59
  248. package/dest/public/avm/opcodes/external_calls.d.ts.map +0 -1
  249. package/dest/public/avm/opcodes/external_calls.js +0 -209
  250. package/dest/public/avm/opcodes/hashing.d.ts +0 -36
  251. package/dest/public/avm/opcodes/hashing.d.ts.map +0 -1
  252. package/dest/public/avm/opcodes/hashing.js +0 -114
  253. package/dest/public/avm/opcodes/index.d.ts +0 -16
  254. package/dest/public/avm/opcodes/index.d.ts.map +0 -1
  255. package/dest/public/avm/opcodes/index.js +0 -15
  256. package/dest/public/avm/opcodes/instruction.d.ts +0 -74
  257. package/dest/public/avm/opcodes/instruction.d.ts.map +0 -1
  258. package/dest/public/avm/opcodes/instruction.js +0 -95
  259. package/dest/public/avm/opcodes/instruction_impl.d.ts +0 -19
  260. package/dest/public/avm/opcodes/instruction_impl.d.ts.map +0 -1
  261. package/dest/public/avm/opcodes/instruction_impl.js +0 -30
  262. package/dest/public/avm/opcodes/memory.d.ts +0 -74
  263. package/dest/public/avm/opcodes/memory.d.ts.map +0 -1
  264. package/dest/public/avm/opcodes/memory.js +0 -257
  265. package/dest/public/avm/opcodes/misc.d.ts +0 -18
  266. package/dest/public/avm/opcodes/misc.d.ts.map +0 -1
  267. package/dest/public/avm/opcodes/misc.js +0 -73
  268. package/dest/public/avm/opcodes/storage.d.ts +0 -24
  269. package/dest/public/avm/opcodes/storage.d.ts.map +0 -1
  270. package/dest/public/avm/opcodes/storage.js +0 -68
  271. package/dest/public/avm/revert_reason.d.ts +0 -18
  272. package/dest/public/avm/revert_reason.d.ts.map +0 -1
  273. package/dest/public/avm/revert_reason.js +0 -38
  274. package/dest/public/avm/serialization/buffer_cursor.d.ts +0 -26
  275. package/dest/public/avm/serialization/buffer_cursor.d.ts.map +0 -1
  276. package/dest/public/avm/serialization/buffer_cursor.js +0 -99
  277. package/dest/public/avm/serialization/bytecode_serialization.d.ts +0 -17
  278. package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +0 -1
  279. package/dest/public/avm/serialization/bytecode_serialization.js +0 -333
  280. package/dest/public/avm/serialization/instruction_serialization.d.ts +0 -114
  281. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +0 -1
  282. package/dest/public/avm/serialization/instruction_serialization.js +0 -261
  283. package/dest/public/avm/test_utils.d.ts +0 -15
  284. package/dest/public/avm/test_utils.d.ts.map +0 -1
  285. package/dest/public/avm/test_utils.js +0 -44
  286. package/dest/public/debug_fn_name.d.ts +0 -5
  287. package/dest/public/debug_fn_name.d.ts.map +0 -1
  288. package/dest/public/debug_fn_name.js +0 -9
  289. package/dest/public/fixtures/custom_bytecode_tests.d.ts +0 -9
  290. package/dest/public/fixtures/custom_bytecode_tests.d.ts.map +0 -1
  291. package/dest/public/fixtures/custom_bytecode_tests.js +0 -109
  292. package/dest/public/fixtures/minimal_public_tx.d.ts +0 -10
  293. package/dest/public/fixtures/minimal_public_tx.d.ts.map +0 -1
  294. package/dest/public/fixtures/minimal_public_tx.js +0 -29
  295. package/dest/public/hinting_db_sources.d.ts +0 -70
  296. package/dest/public/hinting_db_sources.d.ts.map +0 -1
  297. package/dest/public/hinting_db_sources.js +0 -297
  298. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +0 -41
  299. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts.map +0 -1
  300. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.js +0 -89
  301. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +0 -24
  302. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts.map +0 -1
  303. package/dest/public/public_tx_simulator/measured_public_tx_simulator.js +0 -51
  304. package/dest/public/public_tx_simulator/public_tx_context.d.ts +0 -127
  305. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +0 -1
  306. package/dest/public/public_tx_simulator/public_tx_context.js +0 -292
  307. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts +0 -21
  308. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts.map +0 -1
  309. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.js +0 -39
  310. package/dest/public/side_effect_trace.d.ts +0 -87
  311. package/dest/public/side_effect_trace.d.ts.map +0 -1
  312. package/dest/public/side_effect_trace.js +0 -199
  313. package/dest/public/side_effect_trace_interface.d.ts +0 -23
  314. package/dest/public/side_effect_trace_interface.d.ts.map +0 -1
  315. package/dest/public/side_effect_trace_interface.js +0 -1
  316. package/dest/public/state_manager/index.d.ts +0 -2
  317. package/dest/public/state_manager/index.d.ts.map +0 -1
  318. package/dest/public/state_manager/index.js +0 -1
  319. package/dest/public/state_manager/nullifiers.d.ts +0 -61
  320. package/dest/public/state_manager/nullifiers.d.ts.map +0 -1
  321. package/dest/public/state_manager/nullifiers.js +0 -92
  322. package/dest/public/state_manager/public_storage.d.ts +0 -66
  323. package/dest/public/state_manager/public_storage.d.ts.map +0 -1
  324. package/dest/public/state_manager/public_storage.js +0 -145
  325. package/dest/public/state_manager/state_manager.d.ts +0 -166
  326. package/dest/public/state_manager/state_manager.d.ts.map +0 -1
  327. package/dest/public/state_manager/state_manager.js +0 -391
  328. package/dest/public/tx_contract_cache.d.ts +0 -41
  329. package/dest/public/tx_contract_cache.d.ts.map +0 -1
  330. package/dest/public/tx_contract_cache.js +0 -49
  331. package/src/public/avm/avm_context.ts +0 -61
  332. package/src/public/avm/avm_contract_call_result.ts +0 -67
  333. package/src/public/avm/avm_execution_environment.ts +0 -48
  334. package/src/public/avm/avm_gas.ts +0 -183
  335. package/src/public/avm/avm_machine_state.ts +0 -169
  336. package/src/public/avm/avm_memory_types.ts +0 -436
  337. package/src/public/avm/avm_simulator.ts +0 -274
  338. package/src/public/avm/avm_simulator_interface.ts +0 -8
  339. package/src/public/avm/errors.ts +0 -163
  340. package/src/public/avm/fixtures/avm_simulation_tester.ts +0 -108
  341. package/src/public/avm/fixtures/initializers.ts +0 -104
  342. package/src/public/avm/index.ts +0 -1
  343. package/src/public/avm/opcodes/accrued_substate.ts +0 -288
  344. package/src/public/avm/opcodes/addressing_mode.ts +0 -111
  345. package/src/public/avm/opcodes/arithmetic.ts +0 -117
  346. package/src/public/avm/opcodes/bitwise.ts +0 -113
  347. package/src/public/avm/opcodes/comparators.ts +0 -55
  348. package/src/public/avm/opcodes/contract.ts +0 -75
  349. package/src/public/avm/opcodes/control_flow.ts +0 -123
  350. package/src/public/avm/opcodes/conversion.ts +0 -104
  351. package/src/public/avm/opcodes/ec_add.ts +0 -96
  352. package/src/public/avm/opcodes/environment_getters.ts +0 -87
  353. package/src/public/avm/opcodes/external_calls.ts +0 -260
  354. package/src/public/avm/opcodes/hashing.ts +0 -145
  355. package/src/public/avm/opcodes/index.ts +0 -15
  356. package/src/public/avm/opcodes/instruction.ts +0 -136
  357. package/src/public/avm/opcodes/instruction_impl.ts +0 -36
  358. package/src/public/avm/opcodes/memory.ts +0 -293
  359. package/src/public/avm/opcodes/misc.ts +0 -102
  360. package/src/public/avm/opcodes/storage.ts +0 -87
  361. package/src/public/avm/revert_reason.ts +0 -55
  362. package/src/public/avm/serialization/buffer_cursor.ts +0 -118
  363. package/src/public/avm/serialization/bytecode_serialization.ts +0 -204
  364. package/src/public/avm/serialization/instruction_serialization.ts +0 -249
  365. package/src/public/avm/test_utils.ts +0 -64
  366. package/src/public/debug_fn_name.ts +0 -18
  367. package/src/public/fixtures/custom_bytecode_tests.ts +0 -135
  368. package/src/public/fixtures/minimal_public_tx.ts +0 -36
  369. package/src/public/hinting_db_sources.ts +0 -505
  370. package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +0 -128
  371. package/src/public/public_tx_simulator/measured_public_tx_simulator.ts +0 -109
  372. package/src/public/public_tx_simulator/public_tx_context.ts +0 -471
  373. package/src/public/public_tx_simulator/telemetry_public_tx_simulator.ts +0 -63
  374. package/src/public/side_effect_trace.ts +0 -290
  375. package/src/public/side_effect_trace_interface.ts +0 -28
  376. package/src/public/state_manager/index.ts +0 -1
  377. package/src/public/state_manager/nullifiers.ts +0 -103
  378. package/src/public/state_manager/public_storage.ts +0 -174
  379. package/src/public/state_manager/state_manager.ts +0 -560
  380. package/src/public/tx_contract_cache.ts +0 -69
@@ -1,436 +0,0 @@
1
- import {
2
- MEM_TAG_FF,
3
- MEM_TAG_U1,
4
- MEM_TAG_U8,
5
- MEM_TAG_U16,
6
- MEM_TAG_U32,
7
- MEM_TAG_U64,
8
- MEM_TAG_U128,
9
- } from '@aztec/constants';
10
- import { toBufferBE } from '@aztec/foundation/bigint-buffer';
11
- import { Fr } from '@aztec/foundation/fields';
12
- import { type Logger, createLogger } from '@aztec/foundation/log';
13
- import type { FunctionsOf } from '@aztec/foundation/types';
14
- import { AztecAddress } from '@aztec/stdlib/aztec-address';
15
-
16
- import { strict as assert } from 'assert';
17
-
18
- import { InvalidTagValueError, MemorySliceOutOfRangeError, TagCheckError } from './errors.js';
19
-
20
- /** MemoryValue gathers the common operations for all memory types. */
21
- export abstract class MemoryValue {
22
- public abstract add(rhs: MemoryValue): MemoryValue;
23
- public abstract sub(rhs: MemoryValue): MemoryValue;
24
- public abstract mul(rhs: MemoryValue): MemoryValue;
25
- public abstract div(rhs: MemoryValue): MemoryValue;
26
-
27
- public abstract equals(rhs: MemoryValue): boolean;
28
- public abstract lt(rhs: MemoryValue): boolean;
29
-
30
- // We need this to be able to build an instance of the subclasses.
31
- public abstract build(n: bigint): MemoryValue;
32
-
33
- // Use sparingly.
34
- public abstract toBigInt(): bigint;
35
- public getTag(): TypeTag {
36
- return TaggedMemory.getTag(this);
37
- }
38
-
39
- // To Buffer
40
- public abstract toBuffer(): Buffer;
41
-
42
- // To field
43
- public toFr(): Fr {
44
- return new Fr(this.toBigInt());
45
- }
46
-
47
- public toAztecAddress(): AztecAddress {
48
- return new AztecAddress(this.toFr());
49
- }
50
-
51
- // To number. Throws if exceeds max safe int.
52
- public toNumber(): number {
53
- return this.toFr().toNumber();
54
- }
55
-
56
- public toString(): string {
57
- return `${this.constructor.name}(0x${this.toBigInt().toString(16)})`;
58
- }
59
- }
60
-
61
- /** IntegralValue gathers the common operations for all integral memory types. */
62
- export abstract class IntegralValue extends MemoryValue {
63
- public abstract shl(rhs: IntegralValue): IntegralValue;
64
- public abstract shr(rhs: IntegralValue): IntegralValue;
65
- public abstract and(rhs: IntegralValue): IntegralValue;
66
- public abstract or(rhs: IntegralValue): IntegralValue;
67
- public abstract xor(rhs: IntegralValue): IntegralValue;
68
- public abstract not(): IntegralValue;
69
- }
70
-
71
- /**
72
- * This function creates a class for unsigned integers of a given number of bits.
73
- * In TypeScript terms, it's a class mixin.
74
- **/
75
- function UnsignedIntegerClassFactory(bits: number) {
76
- return class NewUintClass extends IntegralValue {
77
- static readonly bits: number = bits;
78
- static readonly mod: bigint = 1n << BigInt(bits);
79
- static readonly bitmask: bigint = this.mod - 1n;
80
- public readonly n: bigint; // Cannot be private due to TS limitations.
81
-
82
- public constructor(n: bigint | number) {
83
- super();
84
- this.n = BigInt(n);
85
- assert(n >= 0n, `${this.constructor.name} cannot handle negative values: ${n}.`);
86
- assert(n < NewUintClass.mod, `Value ${n} is too large for ${this.constructor.name}.`);
87
- }
88
-
89
- public build(n: bigint): NewUintClass {
90
- return new this.constructor.prototype.constructor(n);
91
- }
92
-
93
- public add(rhs: NewUintClass): NewUintClass {
94
- return this.build((this.n + rhs.n) & NewUintClass.bitmask);
95
- }
96
-
97
- public sub(rhs: NewUintClass): NewUintClass {
98
- const res: bigint = this.n - rhs.n;
99
- return this.build(res >= 0 ? res : res + NewUintClass.mod);
100
- }
101
-
102
- public mul(rhs: NewUintClass): NewUintClass {
103
- return this.build((this.n * rhs.n) & NewUintClass.bitmask);
104
- }
105
-
106
- public div(rhs: NewUintClass): NewUintClass {
107
- return this.build(this.n / rhs.n);
108
- }
109
-
110
- // No sign extension.
111
- public shr(rhs: NewUintClass): NewUintClass {
112
- // Note that this.n is >= 0 by class invariant.
113
- return this.build(this.n >> rhs.n);
114
- }
115
-
116
- public shl(rhs: NewUintClass): NewUintClass {
117
- const shiftAmount = rhs.n;
118
- const bitSize = BigInt(NewUintClass.bits);
119
-
120
- // Shifting by more than the bit size always results in 0
121
- if (shiftAmount >= bitSize) {
122
- return this.build(0n);
123
- }
124
-
125
- return this.build((this.n << shiftAmount) & NewUintClass.bitmask);
126
- }
127
-
128
- public and(rhs: NewUintClass): NewUintClass {
129
- return this.build(this.n & rhs.n);
130
- }
131
-
132
- public or(rhs: NewUintClass): NewUintClass {
133
- return this.build(this.n | rhs.n);
134
- }
135
-
136
- public xor(rhs: NewUintClass): NewUintClass {
137
- return this.build(this.n ^ rhs.n);
138
- }
139
-
140
- public not(): NewUintClass {
141
- return this.build(~this.n & NewUintClass.bitmask);
142
- }
143
-
144
- public equals(rhs: NewUintClass): boolean {
145
- return this.n === rhs.n;
146
- }
147
-
148
- public lt(rhs: NewUintClass): boolean {
149
- return this.n < rhs.n;
150
- }
151
-
152
- public toBigInt(): bigint {
153
- return this.n;
154
- }
155
-
156
- public toBuffer(): Buffer {
157
- if (bits < 8) {
158
- return toBufferBE(this.n, 1);
159
- }
160
- return toBufferBE(this.n, bits / 8);
161
- }
162
- };
163
- }
164
-
165
- // Now we can create the classes for each unsigned integer type.
166
- // We extend instead of just assigning so that the class has the right name.
167
- // Otherwise they are all called "NewUintClass".
168
- export class Uint1 extends UnsignedIntegerClassFactory(1) {}
169
- export class Uint8 extends UnsignedIntegerClassFactory(8) {}
170
- export class Uint16 extends UnsignedIntegerClassFactory(16) {}
171
- export class Uint32 extends UnsignedIntegerClassFactory(32) {}
172
- export class Uint64 extends UnsignedIntegerClassFactory(64) {}
173
- export class Uint128 extends UnsignedIntegerClassFactory(128) {}
174
-
175
- export class Field extends MemoryValue {
176
- public static readonly MODULUS: bigint = Fr.MODULUS;
177
- private readonly rep: Fr;
178
-
179
- constructor(v: number | bigint | Fr | Buffer) {
180
- super();
181
- this.rep = new Fr(v);
182
- }
183
-
184
- public build(n: bigint): Field {
185
- return new Field(n);
186
- }
187
-
188
- public add(rhs: Field): Field {
189
- return new Field(this.rep.add(rhs.rep));
190
- }
191
-
192
- public sub(rhs: Field): Field {
193
- return new Field(this.rep.sub(rhs.rep));
194
- }
195
-
196
- public mul(rhs: Field): Field {
197
- return new Field(this.rep.mul(rhs.rep));
198
- }
199
-
200
- // Euclidean division.
201
- public div(rhs: Field): Field {
202
- return new Field(this.rep.ediv(rhs.rep));
203
- }
204
-
205
- // Field division.
206
- public fdiv(rhs: Field): Field {
207
- return new Field(this.rep.div(rhs.rep));
208
- }
209
-
210
- public equals(rhs: Field): boolean {
211
- return this.rep.equals(rhs.rep);
212
- }
213
-
214
- public lt(rhs: Field): boolean {
215
- return this.rep.lt(rhs.rep);
216
- }
217
-
218
- public toBigInt(): bigint {
219
- return this.rep.toBigInt();
220
- }
221
-
222
- public toBuffer(): Buffer {
223
- return this.rep.toBuffer();
224
- }
225
- }
226
-
227
- export enum TypeTag {
228
- FIELD = MEM_TAG_FF,
229
- UINT1 = MEM_TAG_U1,
230
- UINT8 = MEM_TAG_U8,
231
- UINT16 = MEM_TAG_U16,
232
- UINT32 = MEM_TAG_U32,
233
- UINT64 = MEM_TAG_U64,
234
- UINT128 = MEM_TAG_U128,
235
- INVALID = MEM_TAG_U128 + 1,
236
- }
237
-
238
- // Lazy interface definition for tagged memory
239
- export type TaggedMemoryInterface = FunctionsOf<TaggedMemory>;
240
-
241
- export class TaggedMemory implements TaggedMemoryInterface {
242
- static readonly log: Logger = createLogger('simulator:avm:memory');
243
-
244
- // Whether to track and validate memory accesses for each instruction.
245
- static readonly TRACK_MEMORY_ACCESSES = process.env.NODE_ENV === 'test';
246
-
247
- // Memory is modelled by a map with key type being number.
248
- // We however restrict the keys to be non-negative integers smaller than
249
- // MAX_MEMORY_SIZE.
250
- static readonly MAX_MEMORY_SIZE = Number(1n << 32n);
251
- private _mem: Map<number, MemoryValue>;
252
-
253
- constructor() {
254
- this._mem = new Map<number, MemoryValue>();
255
- }
256
-
257
- public getMaxMemorySize(): number {
258
- return TaggedMemory.MAX_MEMORY_SIZE;
259
- }
260
-
261
- public get(offset: number): MemoryValue {
262
- return this.getAs<MemoryValue>(offset);
263
- }
264
-
265
- public getAs<T>(offset: number): T {
266
- assert(Number.isInteger(offset) && offset < TaggedMemory.MAX_MEMORY_SIZE);
267
- const word = this._mem.get(offset);
268
- //TaggedMemory.log.trace(`Memory read: ${offset} -> ${word}`);
269
- if (word === undefined) {
270
- TaggedMemory.log.debug(`WARNING: Memory at offset ${offset} is undefined!`);
271
- return new Field(0) as T;
272
- }
273
- return word as T;
274
- }
275
-
276
- public getSlice(offset: number, size: number): MemoryValue[] {
277
- assert(Number.isInteger(offset) && Number.isInteger(size));
278
-
279
- if (offset + size > TaggedMemory.MAX_MEMORY_SIZE) {
280
- throw new MemorySliceOutOfRangeError(offset, size);
281
- }
282
-
283
- const slice = new Array<MemoryValue>(size);
284
-
285
- for (let i = 0; i < size; i++) {
286
- slice[i] = this._mem.get(offset + i) ?? new Field(0);
287
- }
288
-
289
- TaggedMemory.log.trace(`getSlice(${offset}, ${size}) = ${slice}`);
290
- return slice;
291
- }
292
-
293
- public getSliceAs<T>(offset: number, size: number): T[] {
294
- return this.getSlice(offset, size) as T[];
295
- }
296
-
297
- public getSliceTags(offset: number, size: number): TypeTag[] {
298
- return this.getSlice(offset, size).map(TaggedMemory.getTag);
299
- }
300
-
301
- public set(offset: number, v: MemoryValue) {
302
- assert(Number.isInteger(offset) && offset < TaggedMemory.MAX_MEMORY_SIZE);
303
- this._mem.set(offset, v);
304
- //TaggedMemory.log.trace(`Memory write: ${offset} <- ${v}`);
305
- }
306
-
307
- public setSlice(offset: number, slice: MemoryValue[]) {
308
- assert(Number.isInteger(offset));
309
-
310
- if (offset + slice.length > TaggedMemory.MAX_MEMORY_SIZE) {
311
- throw new MemorySliceOutOfRangeError(offset, slice.length);
312
- }
313
-
314
- slice.forEach((element, idx) => {
315
- this._mem.set(offset + idx, element);
316
- });
317
- TaggedMemory.log.trace(`setSlice(${offset}, ${slice})`);
318
- }
319
-
320
- public getTag(offset: number): TypeTag {
321
- assert(Number.isInteger(offset) && offset < TaggedMemory.MAX_MEMORY_SIZE);
322
- return TaggedMemory.getTag(this._mem.get(offset));
323
- }
324
-
325
- /**
326
- * Check that the memory at the given offset matches the specified tag.
327
- */
328
- public checkTag(tag: TypeTag, offset: number) {
329
- const gotTag = this.getTag(offset);
330
- if (gotTag !== tag) {
331
- throw TagCheckError.forOffset(offset, TypeTag[gotTag], TypeTag[tag]);
332
- }
333
- }
334
-
335
- public static isValidMemoryAddressTag(tag: TypeTag) {
336
- return tag === TypeTag.UINT32;
337
- }
338
-
339
- public static checkIsIntegralTag(tag: TypeTag) {
340
- if (!INTEGRAL_TAGS.has(tag)) {
341
- throw TagCheckError.forTag(TypeTag[tag], 'integral');
342
- }
343
- }
344
-
345
- public static checkIsValidTag(tagNumber: number) {
346
- if (!VALID_TAGS.has(tagNumber)) {
347
- throw new InvalidTagValueError(tagNumber);
348
- }
349
- }
350
-
351
- /**
352
- * Check tags for memory at all of the specified offsets.
353
- */
354
- public checkTags(tag: TypeTag, ...offsets: number[]) {
355
- for (const offset of offsets) {
356
- this.checkTag(tag, offset);
357
- }
358
- }
359
-
360
- /**
361
- * Check that all tags at the given offsets are the same.
362
- */
363
- public checkTagsAreSame(offset0: number, offset1: number) {
364
- const tag0 = this.getTag(offset0);
365
- this.checkTag(tag0, offset1);
366
- }
367
-
368
- /**
369
- * Check tags for all memory in the specified range.
370
- */
371
- public checkTagsRange(tag: TypeTag, startOffset: number, size: number) {
372
- for (let offset = startOffset; offset < startOffset + size; offset++) {
373
- this.checkTag(tag, offset);
374
- }
375
- }
376
-
377
- public static getTag(v: MemoryValue | undefined): TypeTag {
378
- if (v === undefined) {
379
- return TypeTag.FIELD; // uninitialized memory is Field(0)
380
- } else {
381
- return TAG_FOR_MEM_VAL.get(v.constructor.name) ?? TypeTag.INVALID;
382
- }
383
- }
384
-
385
- // Truncates the value to fit the type.
386
- public static buildFromTagTruncating(v: bigint | number, tag: TypeTag): MemoryValue {
387
- v = BigInt(v);
388
- switch (tag) {
389
- case TypeTag.FIELD:
390
- return new Field(v);
391
- case TypeTag.UINT1:
392
- return new Uint1(v & 1n);
393
- case TypeTag.UINT8:
394
- return new Uint8(v & ((1n << 8n) - 1n));
395
- case TypeTag.UINT16:
396
- return new Uint16(v & ((1n << 16n) - 1n));
397
- case TypeTag.UINT32:
398
- return new Uint32(v & ((1n << 32n) - 1n));
399
- case TypeTag.UINT64:
400
- return new Uint64(v & ((1n << 64n) - 1n));
401
- case TypeTag.UINT128:
402
- return new Uint128(v & ((1n << 128n) - 1n));
403
- default:
404
- throw new InvalidTagValueError(tag);
405
- }
406
- }
407
- }
408
-
409
- const TAG_FOR_MEM_VAL = new Map<string, TypeTag>([
410
- ['Field', TypeTag.FIELD],
411
- ['Uint1', TypeTag.UINT1],
412
- ['Uint8', TypeTag.UINT8],
413
- ['Uint16', TypeTag.UINT16],
414
- ['Uint32', TypeTag.UINT32],
415
- ['Uint64', TypeTag.UINT64],
416
- ['Uint128', TypeTag.UINT128],
417
- ]);
418
-
419
- const VALID_TAGS = new Set([
420
- TypeTag.FIELD,
421
- TypeTag.UINT1,
422
- TypeTag.UINT8,
423
- TypeTag.UINT16,
424
- TypeTag.UINT32,
425
- TypeTag.UINT64,
426
- TypeTag.UINT128,
427
- ]);
428
-
429
- const INTEGRAL_TAGS = new Set([
430
- TypeTag.UINT1,
431
- TypeTag.UINT8,
432
- TypeTag.UINT16,
433
- TypeTag.UINT32,
434
- TypeTag.UINT64,
435
- TypeTag.UINT128,
436
- ]);
@@ -1,274 +0,0 @@
1
- import { Fr } from '@aztec/foundation/fields';
2
- import { type Logger, createLogger } from '@aztec/foundation/log';
3
- import { Timer } from '@aztec/foundation/timer';
4
- import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
- import type { GlobalVariables } from '@aztec/stdlib/tx';
6
-
7
- import { strict as assert } from 'assert';
8
-
9
- import { CheckedPublicExecutionError } from '../public_errors.js';
10
- import type { PublicPersistableStateManager } from '../state_manager/state_manager.js';
11
- import { AvmContext } from './avm_context.js';
12
- import { AvmContractCallResult } from './avm_contract_call_result.js';
13
- import { AvmExecutionEnvironment } from './avm_execution_environment.js';
14
- import type { Gas } from './avm_gas.js';
15
- import { AvmMachineState } from './avm_machine_state.js';
16
- import type { AvmSimulatorInterface } from './avm_simulator_interface.js';
17
- import { AvmRevertReason, InvalidProgramCounterError } from './errors.js';
18
- import type { Instruction } from './opcodes/instruction.js';
19
- import { revertReasonFromExceptionalHalt, revertReasonFromExplicitRevert } from './revert_reason.js';
20
- import {
21
- INSTRUCTION_SET,
22
- type InstructionSet,
23
- decodeInstructionFromBytecode,
24
- } from './serialization/bytecode_serialization.js';
25
-
26
- type OpcodeTally = {
27
- count: number;
28
- gas: Gas;
29
- };
30
-
31
- export class AvmSimulator implements AvmSimulatorInterface {
32
- private log: Logger;
33
- private bytecode: Buffer | undefined;
34
- private opcodeTallies: Map<string, OpcodeTally> = new Map();
35
- // maps pc to [instr, bytesRead]
36
- private deserializedInstructionsCache: Map<number, [Instruction, number]> = new Map();
37
-
38
- private tallyPrintFunction = () => {};
39
- private tallyInstructionFunction = (_b: string, _c: Gas) => {};
40
-
41
- // Test Purposes only: Logger will not have the proper function name. Use this constructor for testing purposes
42
- // only. Otherwise, use build() below.
43
- constructor(
44
- private context: AvmContext,
45
- private instructionSet: InstructionSet = INSTRUCTION_SET,
46
- enableTallying = false,
47
- ) {
48
- // This will be used by the CALL opcode to create a new simulator. It is required to
49
- // avoid a dependency cycle.
50
- context.provideSimulator = AvmSimulator.build;
51
- this.log = createLogger(`simulator:avm(calldata[0]: ${context.environment.calldata[0]})`);
52
- // Turn on tallying if explicitly enabled or if trace logging
53
- if (enableTallying || this.log.isLevelEnabled('trace')) {
54
- this.tallyPrintFunction = this.printOpcodeTallies;
55
- this.tallyInstructionFunction = this.tallyInstruction;
56
- }
57
- }
58
-
59
- // Factory to have a proper function name in the logger. Retrieving the name is asynchronous and
60
- // cannot be done as part of the constructor.
61
- public static async build(context: AvmContext): Promise<AvmSimulator> {
62
- const simulator = new AvmSimulator(context);
63
- const fnName = await context.persistableState.getPublicFunctionDebugName(context.environment);
64
- simulator.log = createLogger(`simulator:avm(f:${fnName})`);
65
-
66
- return simulator;
67
- }
68
-
69
- public static async create(
70
- stateManager: PublicPersistableStateManager,
71
- address: AztecAddress,
72
- sender: AztecAddress,
73
- transactionFee: Fr,
74
- globals: GlobalVariables,
75
- isStaticCall: boolean,
76
- calldata: Fr[],
77
- allocatedGas: Gas,
78
- clientInitiatedSimulation: boolean = false,
79
- maxDebugLogMemoryReads?: number,
80
- ) {
81
- const avmExecutionEnv = new AvmExecutionEnvironment(
82
- address,
83
- sender,
84
- /*contractCallDepth=*/ Fr.zero(),
85
- transactionFee,
86
- globals,
87
- isStaticCall,
88
- calldata,
89
- clientInitiatedSimulation,
90
- maxDebugLogMemoryReads,
91
- );
92
-
93
- const avmMachineState = new AvmMachineState(allocatedGas);
94
- const avmContext = new AvmContext(stateManager, avmExecutionEnv, avmMachineState);
95
- return await AvmSimulator.build(avmContext);
96
- }
97
-
98
- /**
99
- * Fetch the bytecode and execute it in the current context.
100
- */
101
- public async execute(): Promise<AvmContractCallResult> {
102
- const bytecode = await this.context.persistableState.getBytecode(this.context.environment.address);
103
- // getBytecode returns undefined if bytecode is not found or if the limit of contract calls to unique class IDs is reached.
104
- // If it throws an error that reaches this point, it is a bug.
105
-
106
- if (!bytecode) {
107
- return await this.handleFailureToRetrieveBytecode(
108
- `No bytecode found, or limit encountered for max calls to unique contract class IDs. Contract address: ${this.context.environment.address}. Reverting...`,
109
- );
110
- }
111
-
112
- return await this.executeBytecode(bytecode);
113
- }
114
-
115
- /**
116
- * Return the bytecode used for execution, if any.
117
- */
118
- public getBytecode(): Buffer | undefined {
119
- return this.bytecode;
120
- }
121
-
122
- /**
123
- * Executes the provided bytecode in the current context.
124
- * This method is useful for testing and debugging.
125
- */
126
- public async executeBytecode(bytecode: Buffer): Promise<AvmContractCallResult> {
127
- const timer = new Timer();
128
- assert(bytecode.length > 0, "AVM simulator can't execute empty bytecode");
129
-
130
- this.bytecode = bytecode;
131
- let instructionName = 'NONE'; // This is used for logging purposes
132
-
133
- const { machineState } = this.context;
134
- const callStartGas = machineState.gasLeft; // Save gas before executing instruction (for profiling)
135
- try {
136
- // Execute instruction pointed to by the current program counter
137
- // continuing until the machine state signifies a halt
138
- while (!machineState.getHalted()) {
139
- // Get the instruction from cache, or deserialize for the first time
140
- let cachedInstruction = this.deserializedInstructionsCache.get(machineState.pc);
141
-
142
- if (cachedInstruction === undefined) {
143
- cachedInstruction = decodeInstructionFromBytecode(bytecode, machineState.pc, this.instructionSet);
144
- this.deserializedInstructionsCache.set(machineState.pc, cachedInstruction);
145
- }
146
- const [instruction, bytesRead] = cachedInstruction;
147
-
148
- const instrStartGas = machineState.gasLeft; // Save gas before executing instruction (for profiling)
149
-
150
- if (this.log.isLevelEnabled('trace')) {
151
- // Skip this entirely to avoid toStringing etc if trace is not enabled
152
- this.log.trace(
153
- `[PC:${machineState.pc}] [IC:${machineState.instrCounter}] ${instruction.toString()} (gasLeft l2=${
154
- machineState.l2GasLeft
155
- } da=${machineState.daGasLeft})`,
156
- );
157
- }
158
- machineState.nextPc = machineState.pc + bytesRead;
159
-
160
- instructionName = instruction.constructor.name;
161
- // Execute the instruction.
162
- // Normal returns and reverts will return normally here.
163
- // "Exceptional halts" will throw.
164
- await instruction.execute(this.context);
165
- if (!instruction.handlesPC()) {
166
- // Increment PC if the instruction doesn't handle it itself
167
- machineState.pc += bytesRead;
168
- }
169
-
170
- machineState.instrCounter++;
171
-
172
- // gas used by this instruction - used for profiling/tallying
173
- const gasUsed: Gas = {
174
- l2Gas: instrStartGas.l2Gas - machineState.l2GasLeft,
175
- daGas: instrStartGas.daGas - machineState.daGasLeft,
176
- };
177
- this.tallyInstructionFunction(instruction.constructor.name, gasUsed);
178
-
179
- if (machineState.pc >= bytecode.length) {
180
- this.log.warn('Passed end of program');
181
- throw new InvalidProgramCounterError(machineState.pc, /*max=*/ bytecode.length);
182
- }
183
- }
184
-
185
- const output = machineState.getOutput();
186
- const reverted = machineState.getReverted();
187
- const revertReason = reverted ? await revertReasonFromExplicitRevert(output, this.context) : undefined;
188
- const results = new AvmContractCallResult(
189
- reverted,
190
- output,
191
- machineState.gasLeft,
192
- revertReason,
193
- machineState.instrCounter,
194
- );
195
- this.log.debug(`Context execution results: ${results.toString()}`);
196
- const totalGasUsed: Gas = {
197
- l2Gas: callStartGas.l2Gas - machineState.l2GasLeft,
198
- daGas: callStartGas.daGas - machineState.daGasLeft,
199
- };
200
- this.log.debug(`Executed ${machineState.instrCounter} instructions and consumed ${totalGasUsed.l2Gas} L2 Gas`);
201
-
202
- this.tallyPrintFunction();
203
-
204
- this.log.debug(`Core AVM simulation took ${timer.ms()}ms`);
205
-
206
- // Return results for processing by calling context
207
- return results;
208
- } catch (err: any) {
209
- this.log.info(
210
- `Exceptional halt (revert by something other than REVERT opcode) for instruction
211
- ${instructionName} at pc ${machineState.pc} and instruction counter ${machineState.instrCounter}`,
212
- );
213
- if (!(err instanceof CheckedPublicExecutionError)) {
214
- this.log.error(`Unchecked/unknown error thrown by AVM. This is a bug. Error: ${err}`);
215
- throw err;
216
- }
217
-
218
- const revertReason = await revertReasonFromExceptionalHalt(err, this.context);
219
- // Exceptional halts consume all allocated gas
220
- const noGasLeft = { l2Gas: 0, daGas: 0 };
221
- // Note: "exceptional halts" cannot return data, hence [].
222
- const results = new AvmContractCallResult(
223
- /*reverted=*/ true,
224
- /*output=*/ [],
225
- noGasLeft,
226
- revertReason,
227
- machineState.instrCounter,
228
- );
229
- this.log.debug(`Context execution results: ${results.toString()}`);
230
-
231
- this.tallyPrintFunction();
232
- // Return results for processing by calling context
233
- return results;
234
- }
235
- }
236
-
237
- private async handleFailureToRetrieveBytecode(message: string): Promise<AvmContractCallResult> {
238
- // revert, consuming all gas
239
- const fnName = await this.context.persistableState.getPublicFunctionDebugName(this.context.environment);
240
- const revertReason = new AvmRevertReason(
241
- message,
242
- /*failingFunction=*/ {
243
- contractAddress: this.context.environment.address,
244
- functionName: fnName,
245
- },
246
- /*noirCallStack=*/ [],
247
- );
248
- this.log.warn(message);
249
- return new AvmContractCallResult(
250
- /*reverted=*/ true,
251
- /*output=*/ [],
252
- /*gasLeft=*/ { l2Gas: 0, daGas: 0 }, // consumes all allocated gas
253
- revertReason,
254
- );
255
- }
256
-
257
- private tallyInstruction(opcode: string, gasUsed: Gas) {
258
- const opcodeTally = this.opcodeTallies.get(opcode) || ({ count: 0, gas: { l2Gas: 0, daGas: 0 } } as OpcodeTally);
259
- opcodeTally.count++;
260
- opcodeTally.gas.l2Gas += gasUsed.l2Gas;
261
- opcodeTally.gas.daGas += gasUsed.daGas;
262
- this.opcodeTallies.set(opcode, opcodeTally);
263
- }
264
-
265
- private printOpcodeTallies() {
266
- this.log.debug(`Printing tallies per opcode sorted by gas...`);
267
- // sort descending by L2 gas consumed
268
- const sortedOpcodes = Array.from(this.opcodeTallies.entries()).sort((a, b) => b[1].gas.l2Gas - a[1].gas.l2Gas);
269
- for (const [opcode, tally] of sortedOpcodes) {
270
- // NOTE: don't care to clutter the logs with DA gas for now
271
- this.log.debug(`${opcode} executed ${tally.count} times consuming a total of ${tally.gas.l2Gas} L2 gas`);
272
- }
273
- }
274
- }