@aztec/simulator 0.0.0-test.0

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 (367) hide show
  1. package/README.md +49 -0
  2. package/dest/client.d.ts +5 -0
  3. package/dest/client.d.ts.map +1 -0
  4. package/dest/client.js +3 -0
  5. package/dest/common/db_interfaces.d.ts +80 -0
  6. package/dest/common/db_interfaces.d.ts.map +1 -0
  7. package/dest/common/db_interfaces.js +1 -0
  8. package/dest/common/debug_fn_name.d.ts +5 -0
  9. package/dest/common/debug_fn_name.d.ts.map +1 -0
  10. package/dest/common/debug_fn_name.js +6 -0
  11. package/dest/common/errors.d.ts +54 -0
  12. package/dest/common/errors.d.ts.map +1 -0
  13. package/dest/common/errors.js +135 -0
  14. package/dest/common/index.d.ts +4 -0
  15. package/dest/common/index.d.ts.map +1 -0
  16. package/dest/common/index.js +3 -0
  17. package/dest/common/message_load_oracle_inputs.d.ts +15 -0
  18. package/dest/common/message_load_oracle_inputs.d.ts.map +1 -0
  19. package/dest/common/message_load_oracle_inputs.js +15 -0
  20. package/dest/common/stats/index.d.ts +2 -0
  21. package/dest/common/stats/index.d.ts.map +1 -0
  22. package/dest/common/stats/index.js +1 -0
  23. package/dest/common/stats/stats.d.ts +4 -0
  24. package/dest/common/stats/stats.d.ts.map +1 -0
  25. package/dest/common/stats/stats.js +10 -0
  26. package/dest/private/acvm/acvm.d.ts +35 -0
  27. package/dest/private/acvm/acvm.d.ts.map +1 -0
  28. package/dest/private/acvm/acvm.js +70 -0
  29. package/dest/private/acvm/acvm_types.d.ts +10 -0
  30. package/dest/private/acvm/acvm_types.d.ts.map +1 -0
  31. package/dest/private/acvm/acvm_types.js +3 -0
  32. package/dest/private/acvm/deserialize.d.ts +36 -0
  33. package/dest/private/acvm/deserialize.d.ts.map +1 -0
  34. package/dest/private/acvm/deserialize.js +42 -0
  35. package/dest/private/acvm/index.d.ts +6 -0
  36. package/dest/private/acvm/index.d.ts.map +1 -0
  37. package/dest/private/acvm/index.js +5 -0
  38. package/dest/private/acvm/oracle/index.d.ts +14 -0
  39. package/dest/private/acvm/oracle/index.d.ts.map +1 -0
  40. package/dest/private/acvm/oracle/index.js +2 -0
  41. package/dest/private/acvm/oracle/oracle.d.ts +49 -0
  42. package/dest/private/acvm/oracle/oracle.d.ts.map +1 -0
  43. package/dest/private/acvm/oracle/oracle.js +263 -0
  44. package/dest/private/acvm/oracle/typed_oracle.d.ts +83 -0
  45. package/dest/private/acvm/oracle/typed_oracle.d.ts.map +1 -0
  46. package/dest/private/acvm/oracle/typed_oracle.js +132 -0
  47. package/dest/private/acvm/serialize.d.ts +24 -0
  48. package/dest/private/acvm/serialize.d.ts.map +1 -0
  49. package/dest/private/acvm/serialize.js +46 -0
  50. package/dest/private/execution_data_provider.d.ts +261 -0
  51. package/dest/private/execution_data_provider.d.ts.map +1 -0
  52. package/dest/private/execution_data_provider.js +14 -0
  53. package/dest/private/execution_note_cache.d.ts +93 -0
  54. package/dest/private/execution_note_cache.d.ts.map +1 -0
  55. package/dest/private/execution_note_cache.js +180 -0
  56. package/dest/private/hashed_values_cache.d.ts +28 -0
  57. package/dest/private/hashed_values_cache.d.ts.map +1 -0
  58. package/dest/private/hashed_values_cache.js +46 -0
  59. package/dest/private/index.d.ts +13 -0
  60. package/dest/private/index.d.ts.map +1 -0
  61. package/dest/private/index.js +12 -0
  62. package/dest/private/pick_notes.d.ts +85 -0
  63. package/dest/private/pick_notes.d.ts.map +1 -0
  64. package/dest/private/pick_notes.js +51 -0
  65. package/dest/private/private_execution.d.ts +25 -0
  66. package/dest/private/private_execution.d.ts.map +1 -0
  67. package/dest/private/private_execution.js +92 -0
  68. package/dest/private/private_execution_oracle.d.ts +215 -0
  69. package/dest/private/private_execution_oracle.d.ts.map +1 -0
  70. package/dest/private/private_execution_oracle.js +382 -0
  71. package/dest/private/providers/acvm_native.d.ts +40 -0
  72. package/dest/private/providers/acvm_native.d.ts.map +1 -0
  73. package/dest/private/providers/acvm_native.js +139 -0
  74. package/dest/private/providers/acvm_wasm.d.ts +15 -0
  75. package/dest/private/providers/acvm_wasm.d.ts.map +1 -0
  76. package/dest/private/providers/acvm_wasm.js +62 -0
  77. package/dest/private/providers/acvm_wasm_with_blobs.d.ts +19 -0
  78. package/dest/private/providers/acvm_wasm_with_blobs.d.ts.map +1 -0
  79. package/dest/private/providers/acvm_wasm_with_blobs.js +32 -0
  80. package/dest/private/providers/factory.d.ts +12 -0
  81. package/dest/private/providers/factory.d.ts.map +1 -0
  82. package/dest/private/providers/factory.js +27 -0
  83. package/dest/private/providers/simulation_provider.d.ts +19 -0
  84. package/dest/private/providers/simulation_provider.d.ts.map +1 -0
  85. package/dest/private/providers/simulation_provider.js +24 -0
  86. package/dest/private/simulator.d.ts +34 -0
  87. package/dest/private/simulator.d.ts.map +1 -0
  88. package/dest/private/simulator.js +76 -0
  89. package/dest/private/unconstrained_execution.d.ts +10 -0
  90. package/dest/private/unconstrained_execution.d.ts.map +1 -0
  91. package/dest/private/unconstrained_execution.js +27 -0
  92. package/dest/private/unconstrained_execution_oracle.d.ts +161 -0
  93. package/dest/private/unconstrained_execution_oracle.d.ts.map +1 -0
  94. package/dest/private/unconstrained_execution_oracle.js +258 -0
  95. package/dest/public/avm/avm_context.d.ts +41 -0
  96. package/dest/public/avm/avm_context.d.ts.map +1 -0
  97. package/dest/public/avm/avm_context.js +44 -0
  98. package/dest/public/avm/avm_contract_call_result.d.ts +30 -0
  99. package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -0
  100. package/dest/public/avm/avm_contract_call_result.js +50 -0
  101. package/dest/public/avm/avm_execution_environment.d.ts +21 -0
  102. package/dest/public/avm/avm_execution_environment.d.ts.map +1 -0
  103. package/dest/public/avm/avm_execution_environment.js +31 -0
  104. package/dest/public/avm/avm_gas.d.ts +60 -0
  105. package/dest/public/avm/avm_gas.d.ts.map +1 -0
  106. package/dest/public/avm/avm_gas.js +203 -0
  107. package/dest/public/avm/avm_machine_state.d.ts +95 -0
  108. package/dest/public/avm/avm_machine_state.d.ts.map +1 -0
  109. package/dest/public/avm/avm_machine_state.js +107 -0
  110. package/dest/public/avm/avm_memory_types.d.ts +264 -0
  111. package/dest/public/avm/avm_memory_types.d.ts.map +1 -0
  112. package/dest/public/avm/avm_memory_types.js +340 -0
  113. package/dest/public/avm/avm_simulator.d.ts +40 -0
  114. package/dest/public/avm/avm_simulator.d.ts.map +1 -0
  115. package/dest/public/avm/avm_simulator.js +202 -0
  116. package/dest/public/avm/bytecode_utils.d.ts +5 -0
  117. package/dest/public/avm/bytecode_utils.d.ts.map +1 -0
  118. package/dest/public/avm/bytecode_utils.js +17 -0
  119. package/dest/public/avm/errors.d.ts +122 -0
  120. package/dest/public/avm/errors.d.ts.map +1 -0
  121. package/dest/public/avm/errors.js +179 -0
  122. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts +21 -0
  123. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -0
  124. package/dest/public/avm/fixtures/avm_simulation_tester.js +76 -0
  125. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +36 -0
  126. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -0
  127. package/dest/public/avm/fixtures/base_avm_simulation_tester.js +83 -0
  128. package/dest/public/avm/fixtures/index.d.ts +84 -0
  129. package/dest/public/avm/fixtures/index.d.ts.map +1 -0
  130. package/dest/public/avm/fixtures/index.js +175 -0
  131. package/dest/public/avm/fixtures/simple_contract_data_source.d.ts +35 -0
  132. package/dest/public/avm/fixtures/simple_contract_data_source.d.ts.map +1 -0
  133. package/dest/public/avm/fixtures/simple_contract_data_source.js +74 -0
  134. package/dest/public/avm/index.d.ts +4 -0
  135. package/dest/public/avm/index.d.ts.map +1 -0
  136. package/dest/public/avm/index.js +3 -0
  137. package/dest/public/avm/journal/index.d.ts +2 -0
  138. package/dest/public/avm/journal/index.d.ts.map +1 -0
  139. package/dest/public/avm/journal/index.js +1 -0
  140. package/dest/public/avm/journal/journal.d.ts +209 -0
  141. package/dest/public/avm/journal/journal.d.ts.map +1 -0
  142. package/dest/public/avm/journal/journal.js +486 -0
  143. package/dest/public/avm/journal/nullifiers.d.ts +64 -0
  144. package/dest/public/avm/journal/nullifiers.d.ts.map +1 -0
  145. package/dest/public/avm/journal/nullifiers.js +97 -0
  146. package/dest/public/avm/journal/public_storage.d.ts +66 -0
  147. package/dest/public/avm/journal/public_storage.d.ts.map +1 -0
  148. package/dest/public/avm/journal/public_storage.js +145 -0
  149. package/dest/public/avm/opcodes/accrued_substate.d.ts +75 -0
  150. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -0
  151. package/dest/public/avm/opcodes/accrued_substate.js +252 -0
  152. package/dest/public/avm/opcodes/addressing_mode.d.ts +27 -0
  153. package/dest/public/avm/opcodes/addressing_mode.d.ts.map +1 -0
  154. package/dest/public/avm/opcodes/addressing_mode.js +74 -0
  155. package/dest/public/avm/opcodes/arithmetic.d.ts +37 -0
  156. package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -0
  157. package/dest/public/avm/opcodes/arithmetic.js +73 -0
  158. package/dest/public/avm/opcodes/bitwise.d.ts +50 -0
  159. package/dest/public/avm/opcodes/bitwise.d.ts.map +1 -0
  160. package/dest/public/avm/opcodes/bitwise.js +106 -0
  161. package/dest/public/avm/opcodes/comparators.d.ts +25 -0
  162. package/dest/public/avm/opcodes/comparators.d.ts.map +1 -0
  163. package/dest/public/avm/opcodes/comparators.js +43 -0
  164. package/dest/public/avm/opcodes/contract.d.ts +21 -0
  165. package/dest/public/avm/opcodes/contract.d.ts.map +1 -0
  166. package/dest/public/avm/opcodes/contract.js +66 -0
  167. package/dest/public/avm/opcodes/control_flow.d.ts +41 -0
  168. package/dest/public/avm/opcodes/control_flow.d.ts.map +1 -0
  169. package/dest/public/avm/opcodes/control_flow.js +105 -0
  170. package/dest/public/avm/opcodes/conversion.d.ts +17 -0
  171. package/dest/public/avm/opcodes/conversion.d.ts.map +1 -0
  172. package/dest/public/avm/opcodes/conversion.js +69 -0
  173. package/dest/public/avm/opcodes/ec_add.d.ts +19 -0
  174. package/dest/public/avm/opcodes/ec_add.d.ts.map +1 -0
  175. package/dest/public/avm/opcodes/ec_add.js +84 -0
  176. package/dest/public/avm/opcodes/environment_getters.d.ts +28 -0
  177. package/dest/public/avm/opcodes/environment_getters.d.ts.map +1 -0
  178. package/dest/public/avm/opcodes/environment_getters.js +79 -0
  179. package/dest/public/avm/opcodes/external_calls.d.ts +58 -0
  180. package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -0
  181. package/dest/public/avm/opcodes/external_calls.js +204 -0
  182. package/dest/public/avm/opcodes/hashing.d.ts +36 -0
  183. package/dest/public/avm/opcodes/hashing.d.ts.map +1 -0
  184. package/dest/public/avm/opcodes/hashing.js +114 -0
  185. package/dest/public/avm/opcodes/index.d.ts +16 -0
  186. package/dest/public/avm/opcodes/index.d.ts.map +1 -0
  187. package/dest/public/avm/opcodes/index.js +15 -0
  188. package/dest/public/avm/opcodes/instruction.d.ts +70 -0
  189. package/dest/public/avm/opcodes/instruction.d.ts.map +1 -0
  190. package/dest/public/avm/opcodes/instruction.js +91 -0
  191. package/dest/public/avm/opcodes/instruction_impl.d.ts +19 -0
  192. package/dest/public/avm/opcodes/instruction_impl.d.ts.map +1 -0
  193. package/dest/public/avm/opcodes/instruction_impl.js +30 -0
  194. package/dest/public/avm/opcodes/memory.d.ts +74 -0
  195. package/dest/public/avm/opcodes/memory.d.ts.map +1 -0
  196. package/dest/public/avm/opcodes/memory.js +253 -0
  197. package/dest/public/avm/opcodes/misc.d.ts +17 -0
  198. package/dest/public/avm/opcodes/misc.d.ts.map +1 -0
  199. package/dest/public/avm/opcodes/misc.js +48 -0
  200. package/dest/public/avm/opcodes/storage.d.ts +24 -0
  201. package/dest/public/avm/opcodes/storage.d.ts.map +1 -0
  202. package/dest/public/avm/opcodes/storage.js +66 -0
  203. package/dest/public/avm/serialization/buffer_cursor.d.ts +28 -0
  204. package/dest/public/avm/serialization/buffer_cursor.d.ts.map +1 -0
  205. package/dest/public/avm/serialization/buffer_cursor.js +99 -0
  206. package/dest/public/avm/serialization/bytecode_serialization.d.ts +21 -0
  207. package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +1 -0
  208. package/dest/public/avm/serialization/bytecode_serialization.js +332 -0
  209. package/dest/public/avm/serialization/instruction_serialization.d.ts +105 -0
  210. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -0
  211. package/dest/public/avm/serialization/instruction_serialization.js +226 -0
  212. package/dest/public/avm/test_utils.d.ts +18 -0
  213. package/dest/public/avm/test_utils.d.ts.map +1 -0
  214. package/dest/public/avm/test_utils.js +52 -0
  215. package/dest/public/bytecode_errors.d.ts +4 -0
  216. package/dest/public/bytecode_errors.d.ts.map +1 -0
  217. package/dest/public/bytecode_errors.js +6 -0
  218. package/dest/public/execution.d.ts +108 -0
  219. package/dest/public/execution.d.ts.map +1 -0
  220. package/dest/public/execution.js +9 -0
  221. package/dest/public/executor_metrics.d.ts +13 -0
  222. package/dest/public/executor_metrics.d.ts.map +1 -0
  223. package/dest/public/executor_metrics.js +54 -0
  224. package/dest/public/fixtures/index.d.ts +3 -0
  225. package/dest/public/fixtures/index.d.ts.map +1 -0
  226. package/dest/public/fixtures/index.js +2 -0
  227. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +32 -0
  228. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -0
  229. package/dest/public/fixtures/public_tx_simulation_tester.js +93 -0
  230. package/dest/public/fixtures/utils.d.ts +13 -0
  231. package/dest/public/fixtures/utils.d.ts.map +1 -0
  232. package/dest/public/fixtures/utils.js +126 -0
  233. package/dest/public/index.d.ts +10 -0
  234. package/dest/public/index.d.ts.map +1 -0
  235. package/dest/public/index.js +8 -0
  236. package/dest/public/public_db_sources.d.ts +129 -0
  237. package/dest/public/public_db_sources.d.ts.map +1 -0
  238. package/dest/public/public_db_sources.js +264 -0
  239. package/dest/public/public_processor/public_processor.d.ts +74 -0
  240. package/dest/public/public_processor/public_processor.d.ts.map +1 -0
  241. package/dest/public/public_processor/public_processor.js +379 -0
  242. package/dest/public/public_processor/public_processor_metrics.d.ts +27 -0
  243. package/dest/public/public_processor/public_processor_metrics.d.ts.map +1 -0
  244. package/dest/public/public_processor/public_processor_metrics.js +125 -0
  245. package/dest/public/public_tx_simulator/public_tx_context.d.ts +137 -0
  246. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -0
  247. package/dest/public/public_tx_simulator/public_tx_context.js +320 -0
  248. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +102 -0
  249. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -0
  250. package/dest/public/public_tx_simulator/public_tx_simulator.js +334 -0
  251. package/dest/public/side_effect_errors.d.ts +4 -0
  252. package/dest/public/side_effect_errors.d.ts.map +1 -0
  253. package/dest/public/side_effect_errors.js +6 -0
  254. package/dest/public/side_effect_trace.d.ts +132 -0
  255. package/dest/public/side_effect_trace.d.ts.map +1 -0
  256. package/dest/public/side_effect_trace.js +249 -0
  257. package/dest/public/side_effect_trace_interface.d.ts +34 -0
  258. package/dest/public/side_effect_trace_interface.d.ts.map +1 -0
  259. package/dest/public/side_effect_trace_interface.js +1 -0
  260. package/dest/public/tx_contract_cache.d.ts +41 -0
  261. package/dest/public/tx_contract_cache.d.ts.map +1 -0
  262. package/dest/public/tx_contract_cache.js +49 -0
  263. package/dest/public/unique_class_ids.d.ts +37 -0
  264. package/dest/public/unique_class_ids.d.ts.map +1 -0
  265. package/dest/public/unique_class_ids.js +61 -0
  266. package/dest/public/utils.d.ts +5 -0
  267. package/dest/public/utils.d.ts.map +1 -0
  268. package/dest/public/utils.js +35 -0
  269. package/dest/server.d.ts +6 -0
  270. package/dest/server.d.ts.map +1 -0
  271. package/dest/server.js +4 -0
  272. package/dest/test/utils.d.ts +13 -0
  273. package/dest/test/utils.d.ts.map +1 -0
  274. package/dest/test/utils.js +22 -0
  275. package/package.json +104 -0
  276. package/src/client.ts +4 -0
  277. package/src/common/db_interfaces.ts +94 -0
  278. package/src/common/debug_fn_name.ts +18 -0
  279. package/src/common/errors.ts +183 -0
  280. package/src/common/index.ts +3 -0
  281. package/src/common/message_load_oracle_inputs.ts +15 -0
  282. package/src/common/stats/index.ts +1 -0
  283. package/src/common/stats/stats.ts +20 -0
  284. package/src/private/acvm/acvm.ts +127 -0
  285. package/src/private/acvm/acvm_types.ts +11 -0
  286. package/src/private/acvm/deserialize.ts +52 -0
  287. package/src/private/acvm/index.ts +5 -0
  288. package/src/private/acvm/oracle/index.ts +16 -0
  289. package/src/private/acvm/oracle/oracle.ts +455 -0
  290. package/src/private/acvm/oracle/typed_oracle.ts +259 -0
  291. package/src/private/acvm/serialize.ts +60 -0
  292. package/src/private/execution_data_provider.ts +323 -0
  293. package/src/private/execution_note_cache.ts +217 -0
  294. package/src/private/hashed_values_cache.ts +55 -0
  295. package/src/private/index.ts +16 -0
  296. package/src/private/pick_notes.ts +141 -0
  297. package/src/private/private_execution.ts +151 -0
  298. package/src/private/private_execution_oracle.ts +614 -0
  299. package/src/private/providers/acvm_native.ts +171 -0
  300. package/src/private/providers/acvm_wasm.ts +63 -0
  301. package/src/private/providers/acvm_wasm_with_blobs.ts +50 -0
  302. package/src/private/providers/factory.ts +38 -0
  303. package/src/private/providers/simulation_provider.ts +45 -0
  304. package/src/private/simulator.ts +147 -0
  305. package/src/private/unconstrained_execution.ts +50 -0
  306. package/src/private/unconstrained_execution_oracle.ts +373 -0
  307. package/src/public/avm/avm_context.ts +61 -0
  308. package/src/public/avm/avm_contract_call_result.ts +55 -0
  309. package/src/public/avm/avm_execution_environment.ts +39 -0
  310. package/src/public/avm/avm_gas.ts +195 -0
  311. package/src/public/avm/avm_machine_state.ts +164 -0
  312. package/src/public/avm/avm_memory_types.ts +423 -0
  313. package/src/public/avm/avm_simulator.ts +284 -0
  314. package/src/public/avm/bytecode_utils.ts +17 -0
  315. package/src/public/avm/errors.ts +232 -0
  316. package/src/public/avm/fixtures/avm_simulation_tester.ts +105 -0
  317. package/src/public/avm/fixtures/base_avm_simulation_tester.ts +109 -0
  318. package/src/public/avm/fixtures/index.ts +296 -0
  319. package/src/public/avm/fixtures/simple_contract_data_source.ts +105 -0
  320. package/src/public/avm/index.ts +3 -0
  321. package/src/public/avm/journal/index.ts +1 -0
  322. package/src/public/avm/journal/journal.ts +742 -0
  323. package/src/public/avm/journal/nullifiers.ts +109 -0
  324. package/src/public/avm/journal/public_storage.ts +174 -0
  325. package/src/public/avm/opcodes/.eslintrc.cjs +8 -0
  326. package/src/public/avm/opcodes/accrued_substate.ts +253 -0
  327. package/src/public/avm/opcodes/addressing_mode.ts +87 -0
  328. package/src/public/avm/opcodes/arithmetic.ts +95 -0
  329. package/src/public/avm/opcodes/bitwise.ts +109 -0
  330. package/src/public/avm/opcodes/comparators.ts +52 -0
  331. package/src/public/avm/opcodes/contract.ts +72 -0
  332. package/src/public/avm/opcodes/control_flow.ts +114 -0
  333. package/src/public/avm/opcodes/conversion.ts +80 -0
  334. package/src/public/avm/opcodes/ec_add.ts +93 -0
  335. package/src/public/avm/opcodes/environment_getters.ts +82 -0
  336. package/src/public/avm/opcodes/external_calls.ts +228 -0
  337. package/src/public/avm/opcodes/hashing.ts +125 -0
  338. package/src/public/avm/opcodes/index.ts +15 -0
  339. package/src/public/avm/opcodes/instruction.ts +126 -0
  340. package/src/public/avm/opcodes/instruction_impl.ts +36 -0
  341. package/src/public/avm/opcodes/memory.ts +254 -0
  342. package/src/public/avm/opcodes/misc.ts +60 -0
  343. package/src/public/avm/opcodes/storage.ts +71 -0
  344. package/src/public/avm/serialization/buffer_cursor.ts +115 -0
  345. package/src/public/avm/serialization/bytecode_serialization.ts +206 -0
  346. package/src/public/avm/serialization/instruction_serialization.ts +208 -0
  347. package/src/public/avm/test_utils.ts +81 -0
  348. package/src/public/bytecode_errors.ts +6 -0
  349. package/src/public/execution.ts +140 -0
  350. package/src/public/executor_metrics.ts +71 -0
  351. package/src/public/fixtures/index.ts +2 -0
  352. package/src/public/fixtures/public_tx_simulation_tester.ts +179 -0
  353. package/src/public/fixtures/utils.ts +199 -0
  354. package/src/public/index.ts +9 -0
  355. package/src/public/public_db_sources.ts +369 -0
  356. package/src/public/public_processor/public_processor.ts +521 -0
  357. package/src/public/public_processor/public_processor_metrics.ts +154 -0
  358. package/src/public/public_tx_simulator/public_tx_context.ts +504 -0
  359. package/src/public/public_tx_simulator/public_tx_simulator.ts +474 -0
  360. package/src/public/side_effect_errors.ts +6 -0
  361. package/src/public/side_effect_trace.ts +516 -0
  362. package/src/public/side_effect_trace_interface.ts +76 -0
  363. package/src/public/tx_contract_cache.ts +69 -0
  364. package/src/public/unique_class_ids.ts +80 -0
  365. package/src/public/utils.ts +32 -0
  366. package/src/server.ts +5 -0
  367. package/src/test/utils.ts +36 -0
@@ -0,0 +1,208 @@
1
+ import { strict as assert } from 'assert';
2
+
3
+ import { BufferCursor } from './buffer_cursor.js';
4
+
5
+ /**
6
+ * All AVM opcodes. (Keep in sync with cpp counterpart code avm_opcode.hpp and rs in opcodes.rs).
7
+ * Source: https://yp-aztec.netlify.app/docs/public-vm/instruction-set
8
+ */
9
+ export enum Opcode {
10
+ // Compute
11
+ ADD_8,
12
+ ADD_16,
13
+ SUB_8,
14
+ SUB_16,
15
+ MUL_8,
16
+ MUL_16,
17
+ DIV_8,
18
+ DIV_16,
19
+ FDIV_8,
20
+ FDIV_16,
21
+ EQ_8,
22
+ EQ_16,
23
+ LT_8,
24
+ LT_16,
25
+ LTE_8,
26
+ LTE_16,
27
+ AND_8,
28
+ AND_16,
29
+ OR_8,
30
+ OR_16,
31
+ XOR_8,
32
+ XOR_16,
33
+ NOT_8,
34
+ NOT_16,
35
+ SHL_8,
36
+ SHL_16,
37
+ SHR_8,
38
+ SHR_16,
39
+ CAST_8,
40
+ CAST_16,
41
+ // Execution environment
42
+ GETENVVAR_16,
43
+ CALLDATACOPY,
44
+ SUCCESSCOPY,
45
+ RETURNDATASIZE,
46
+ RETURNDATACOPY,
47
+ // Control flow
48
+ JUMP_32,
49
+ JUMPI_32,
50
+ INTERNALCALL,
51
+ INTERNALRETURN,
52
+ // Memory
53
+ SET_8,
54
+ SET_16,
55
+ SET_32,
56
+ SET_64,
57
+ SET_128,
58
+ SET_FF,
59
+ MOV_8,
60
+ MOV_16,
61
+ // World state
62
+ SLOAD,
63
+ SSTORE,
64
+ NOTEHASHEXISTS,
65
+ EMITNOTEHASH,
66
+ NULLIFIEREXISTS,
67
+ EMITNULLIFIER,
68
+ L1TOL2MSGEXISTS,
69
+ GETCONTRACTINSTANCE,
70
+ EMITUNENCRYPTEDLOG,
71
+ SENDL2TOL1MSG,
72
+ // External calls
73
+ CALL,
74
+ STATICCALL,
75
+ RETURN,
76
+ REVERT_8,
77
+ REVERT_16,
78
+ // Misc
79
+ DEBUGLOG,
80
+ // Gadgets
81
+ POSEIDON2,
82
+ SHA256COMPRESSION,
83
+ KECCAKF1600,
84
+ ECADD,
85
+ // Conversion
86
+ TORADIXBE,
87
+ }
88
+
89
+ export const MAX_OPCODE_VALUE = Math.max(
90
+ ...Object.values(Opcode)
91
+ .map(k => +k)
92
+ .filter(k => !isNaN(k)),
93
+ );
94
+
95
+ // Possible types for an instruction's operand in its wire format. (Keep in sync with CPP code.
96
+ // See vm/avm_trace/deserialization.cpp)
97
+ // Note that cpp code introduced an additional enum value TAG to express the instruction tag. In TS,
98
+ // this one is parsed as UINT8.
99
+ export enum OperandType {
100
+ UINT8,
101
+ UINT16,
102
+ UINT32,
103
+ UINT64,
104
+ UINT128,
105
+ FF,
106
+ }
107
+
108
+ type OperandNativeType = number | bigint;
109
+ type OperandWriter = (value: any) => void;
110
+
111
+ // Specifies how to read and write each operand type.
112
+ const OPERAND_SPEC = new Map<OperandType, [number, (offset: number) => OperandNativeType, OperandWriter]>([
113
+ [OperandType.UINT8, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
114
+ [OperandType.UINT16, [2, Buffer.prototype.readUint16BE, Buffer.prototype.writeUint16BE]],
115
+ [OperandType.UINT32, [4, Buffer.prototype.readUint32BE, Buffer.prototype.writeUint32BE]],
116
+ [OperandType.UINT64, [8, Buffer.prototype.readBigInt64BE, Buffer.prototype.writeBigInt64BE]],
117
+ [OperandType.UINT128, [16, readBigInt128BE, writeBigInt128BE]],
118
+ [OperandType.FF, [32, readBigInt254BE, writeBigInt254BE]],
119
+ ]);
120
+
121
+ function readBigInt254BE(this: Buffer, offset: number): bigint {
122
+ const totalBytes = 32;
123
+ let ret: bigint = 0n;
124
+ for (let i = 0; i < totalBytes; ++i) {
125
+ ret <<= 8n;
126
+ ret |= BigInt(this.readUint8(i + offset));
127
+ }
128
+ return ret;
129
+ }
130
+
131
+ function writeBigInt254BE(this: Buffer, value: bigint): void {
132
+ const totalBytes = 32;
133
+ for (let offset = totalBytes - 1; offset >= 0; --offset) {
134
+ this.writeUint8(Number(value & 0xffn), offset);
135
+ value >>= 8n;
136
+ }
137
+ }
138
+
139
+ function readBigInt128BE(this: Buffer, offset: number): bigint {
140
+ const totalBytes = 16;
141
+ let ret: bigint = 0n;
142
+ for (let i = 0; i < totalBytes; ++i) {
143
+ ret <<= 8n;
144
+ ret |= BigInt(this.readUint8(i + offset));
145
+ }
146
+ return ret;
147
+ }
148
+
149
+ function writeBigInt128BE(this: Buffer, value: bigint): void {
150
+ const totalBytes = 16;
151
+ for (let offset = totalBytes - 1; offset >= 0; --offset) {
152
+ this.writeUint8(Number(value & 0xffn), offset);
153
+ value >>= 8n;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Reads an array of operands from a buffer.
159
+ * @param cursor Buffer to read from. Might be longer than needed.
160
+ * @param operands Specification of the operand types.
161
+ * @returns An array as big as {@code operands}, with the converted TS values.
162
+ */
163
+ export function deserialize(cursor: BufferCursor | Buffer, operands: OperandType[]): (number | bigint)[] {
164
+ const argValues = [];
165
+ if (Buffer.isBuffer(cursor)) {
166
+ cursor = new BufferCursor(cursor);
167
+ }
168
+
169
+ for (const op of operands) {
170
+ const opType = op;
171
+ const [sizeBytes, reader, _writer] = OPERAND_SPEC.get(opType)!;
172
+ argValues.push(reader.call(cursor.buffer(), cursor.position()));
173
+ cursor.advance(sizeBytes);
174
+ }
175
+
176
+ return argValues;
177
+ }
178
+
179
+ /**
180
+ * Serializes a class using the specified operand types.
181
+ * More specifically, this serializes {@code [cls.constructor.opcode, ...Object.values(cls)]}.
182
+ * Observe in particular that:
183
+ * (1) the first operand type specified must correspond to the opcode;
184
+ * (2) the rest of the operand types must be specified in the order returned by {@code Object.values()}.
185
+ * @param operands Type specification for the values to be serialized.
186
+ * @param cls The class to be serialized.
187
+ * @returns
188
+ */
189
+ export function serializeAs(operands: OperandType[], opcode: Opcode, cls: any): Buffer {
190
+ const chunks: Buffer[] = [];
191
+
192
+ const rawClassValues: any[] = [opcode, ...Object.values(cls)];
193
+ assert(
194
+ rawClassValues.length === operands.length,
195
+ `Got ${rawClassValues.length} values but only ${operands.length} serialization operands are specified!`,
196
+ );
197
+ const classValues = rawClassValues as OperandNativeType[];
198
+
199
+ for (let i = 0; i < operands.length; i++) {
200
+ const opType = operands[i];
201
+ const [sizeBytes, _reader, writer] = OPERAND_SPEC.get(opType)!;
202
+ const buf = Buffer.alloc(sizeBytes);
203
+ writer.call(buf, classValues[i]);
204
+ chunks.push(buf);
205
+ }
206
+
207
+ return Buffer.concat(chunks);
208
+ }
@@ -0,0 +1,81 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+ import {
3
+ type ContractClassPublic,
4
+ type ContractInstanceWithAddress,
5
+ computePublicBytecodeCommitment,
6
+ } from '@aztec/stdlib/contract';
7
+
8
+ import type { jest } from '@jest/globals';
9
+ import { mock } from 'jest-mock-extended';
10
+
11
+ import type { WorldStateDB } from '../../public/public_db_sources.js';
12
+ import type { PublicSideEffectTraceInterface } from '../side_effect_trace_interface.js';
13
+
14
+ export async function mockGetBytecode(worldStateDB: WorldStateDB, bytecode: Buffer) {
15
+ const commitment = await computePublicBytecodeCommitment(bytecode);
16
+ (worldStateDB as jest.Mocked<WorldStateDB>).getBytecodeCommitment.mockResolvedValue(commitment);
17
+ }
18
+
19
+ export function mockTraceFork(trace: PublicSideEffectTraceInterface, nestedTrace?: PublicSideEffectTraceInterface) {
20
+ (trace as jest.Mocked<PublicSideEffectTraceInterface>).fork.mockReturnValue(
21
+ nestedTrace ?? mock<PublicSideEffectTraceInterface>(),
22
+ );
23
+ }
24
+
25
+ export function mockStorageRead(worldStateDB: WorldStateDB, value: Fr) {
26
+ (worldStateDB as jest.Mocked<WorldStateDB>).storageRead.mockResolvedValue(value);
27
+ }
28
+
29
+ export function mockNoteHashCount(mockedTrace: PublicSideEffectTraceInterface, count: number) {
30
+ (mockedTrace as jest.Mocked<PublicSideEffectTraceInterface>).getNoteHashCount.mockReturnValue(count);
31
+ }
32
+
33
+ export function mockStorageReadWithMap(worldStateDB: WorldStateDB, mockedStorage: Map<bigint, Fr>) {
34
+ (worldStateDB as jest.Mocked<WorldStateDB>).storageRead.mockImplementation((_address, slot) =>
35
+ Promise.resolve(mockedStorage.get(slot.toBigInt()) ?? Fr.ZERO),
36
+ );
37
+ }
38
+
39
+ export function mockGetBytecodeCommitment(worldStateDB: WorldStateDB, commitment: Fr) {
40
+ (worldStateDB as jest.Mocked<WorldStateDB>).getBytecodeCommitment.mockResolvedValue(commitment);
41
+ }
42
+
43
+ export function mockNoteHashExists(worldStateDB: WorldStateDB, _leafIndex: Fr, value?: Fr) {
44
+ (worldStateDB as jest.Mocked<WorldStateDB>).getCommitmentValue.mockImplementation((index: bigint) => {
45
+ if (index == _leafIndex.toBigInt()) {
46
+ return Promise.resolve(value);
47
+ } else {
48
+ // This is ok for now since the traceing functions handle it
49
+ return Promise.resolve(undefined);
50
+ }
51
+ });
52
+ }
53
+
54
+ export function mockGetNullifierIndex(worldStateDB: WorldStateDB, leafIndex: Fr, _ignoredValue?: Fr) {
55
+ (worldStateDB as jest.Mocked<WorldStateDB>).getNullifierIndex.mockResolvedValue(leafIndex.toBigInt());
56
+ }
57
+
58
+ export function mockL1ToL2MessageExists(
59
+ worldStateDB: WorldStateDB,
60
+ leafIndex: Fr,
61
+ value: Fr,
62
+ valueAtOtherIndices?: Fr,
63
+ ) {
64
+ (worldStateDB as jest.Mocked<WorldStateDB>).getL1ToL2LeafValue.mockImplementation((index: bigint) => {
65
+ if (index == leafIndex.toBigInt()) {
66
+ return Promise.resolve(value);
67
+ } else {
68
+ // any indices other than mockAtLeafIndex will return a different value
69
+ // (or undefined if no value is specified for other indices)
70
+ return Promise.resolve(valueAtOtherIndices!);
71
+ }
72
+ });
73
+ }
74
+
75
+ export function mockGetContractInstance(worldStateDB: WorldStateDB, contractInstance: ContractInstanceWithAddress) {
76
+ (worldStateDB as jest.Mocked<WorldStateDB>).getContractInstance.mockResolvedValue(contractInstance);
77
+ }
78
+
79
+ export function mockGetContractClass(worldStateDB: WorldStateDB, contractClass: ContractClassPublic) {
80
+ (worldStateDB as jest.Mocked<WorldStateDB>).getContractClass.mockResolvedValue(contractClass);
81
+ }
@@ -0,0 +1,6 @@
1
+ export class ContractClassBytecodeError extends Error {
2
+ constructor(contractAddress: string) {
3
+ super(`Failed to get bytecode for contract at address ${contractAddress}`);
4
+ this.name = 'ContractClassBytecodeError';
5
+ }
6
+ }
@@ -0,0 +1,140 @@
1
+ import type { Fr } from '@aztec/foundation/fields';
2
+ import {
3
+ type AvmExecutionHints,
4
+ type ContractStorageRead,
5
+ type ContractStorageUpdateRequest,
6
+ PublicCallStackItemCompressed,
7
+ type PublicDataUpdateRequest,
8
+ PublicInnerCallRequest,
9
+ RevertCode,
10
+ } from '@aztec/stdlib/avm';
11
+ import type { SimulationError } from '@aztec/stdlib/errors';
12
+ import { Gas } from '@aztec/stdlib/gas';
13
+ import { computeVarArgsHash } from '@aztec/stdlib/hash';
14
+ import type { NoteHash, Nullifier, ReadRequest, TreeLeafReadRequest } from '@aztec/stdlib/kernel';
15
+ import type { PublicLog } from '@aztec/stdlib/logs';
16
+ import type { L2ToL1Message, ScopedL2ToL1Message } from '@aztec/stdlib/messaging';
17
+ import type { PublicExecutionRequest } from '@aztec/stdlib/tx';
18
+
19
+ export interface PublicSideEffects {
20
+ /** The contract storage update requests performed. */
21
+ publicDataWrites: PublicDataUpdateRequest[];
22
+ /** The new note hashes to be inserted into the note hashes tree. */
23
+ noteHashes: NoteHash[];
24
+ /** The new nullifiers to be inserted into the nullifier tree. */
25
+ nullifiers: Nullifier[];
26
+ /** The new l2 to l1 messages generated to be inserted into the messages tree. */
27
+ l2ToL1Messages: ScopedL2ToL1Message[];
28
+ /** Public logs emitted during execution. */
29
+ publicLogs: PublicLog[];
30
+ }
31
+
32
+ export interface EnqueuedPublicCallExecutionResult {
33
+ /** How much gas was left after this public execution. */
34
+ endGasLeft: Gas;
35
+ /** The side effect counter after execution */
36
+ endSideEffectCounter: Fr;
37
+
38
+ /** The return values of the function. */
39
+ returnValues: Fr[];
40
+ /** Whether the execution reverted. */
41
+ reverted: boolean;
42
+ /** The revert reason if the execution reverted. */
43
+ revertReason?: SimulationError;
44
+ }
45
+
46
+ export interface EnqueuedPublicCallExecutionResultWithSideEffects {
47
+ /** How much gas was left after this public execution. */
48
+ endGasLeft: Gas;
49
+ /** The side effect counter after execution */
50
+ endSideEffectCounter: Fr;
51
+
52
+ /** The return values of the function. */
53
+ returnValues: Fr[];
54
+ /** Whether the execution reverted. */
55
+ reverted: boolean;
56
+ /** The revert reason if the execution reverted. */
57
+ revertReason?: SimulationError;
58
+
59
+ /** The public side effects of the function. */
60
+ sideEffects: PublicSideEffects;
61
+ }
62
+
63
+ /**
64
+ * The public function execution result.
65
+ */
66
+ export interface PublicFunctionCallResult {
67
+ /** The execution request that triggered this result. */
68
+ executionRequest: PublicExecutionRequest;
69
+
70
+ /** The side effect counter at the start of the function call. */
71
+ startSideEffectCounter: Fr;
72
+ /** The side effect counter after executing this function call */
73
+ endSideEffectCounter: Fr;
74
+ /** How much gas was available for this public execution. */
75
+ startGasLeft: Gas;
76
+ /** How much gas was left after this public execution. */
77
+ endGasLeft: Gas;
78
+ /** Transaction fee set for this tx. */
79
+ transactionFee: Fr;
80
+
81
+ /** Bytecode used for this execution. */
82
+ bytecode?: Buffer;
83
+ /** Calldata used for this execution. */
84
+ calldata: Fr[];
85
+ /** The return values of the function. */
86
+ returnValues: Fr[];
87
+ /** Whether the execution reverted. */
88
+ reverted: boolean;
89
+ /** The revert reason if the execution reverted. */
90
+ revertReason?: SimulationError;
91
+
92
+ /** The contract storage reads performed by the function. */
93
+ contractStorageReads: ContractStorageRead[];
94
+ /** The contract storage update requests performed by the function. */
95
+ contractStorageUpdateRequests: ContractStorageUpdateRequest[];
96
+ /** The new note hashes to be inserted into the note hashes tree. */
97
+ noteHashes: NoteHash[];
98
+ /** The new l2 to l1 messages generated in this call. */
99
+ l2ToL1Messages: L2ToL1Message[];
100
+ /** The new nullifiers to be inserted into the nullifier tree. */
101
+ nullifiers: Nullifier[];
102
+ /** The note hash read requests emitted in this call. */
103
+ noteHashReadRequests: TreeLeafReadRequest[];
104
+ /** The nullifier read requests emitted in this call. */
105
+ nullifierReadRequests: ReadRequest[];
106
+ /** The nullifier non existent read requests emitted in this call. */
107
+ nullifierNonExistentReadRequests: ReadRequest[];
108
+ /** L1 to L2 message read requests emitted in this call. */
109
+ l1ToL2MsgReadRequests: TreeLeafReadRequest[];
110
+ /**
111
+ * The public logs emitted in this call.
112
+ * Note: PublicLog has no counter - unsure if this is needed bc this struct is unused
113
+ */
114
+ publicLogs: PublicLog[];
115
+ /** The requests to call public functions made by this call. */
116
+ publicCallRequests: PublicInnerCallRequest[];
117
+ /** The results of nested calls. */
118
+ nestedExecutions: this[];
119
+
120
+ /** Hints for proving AVM execution. */
121
+ avmCircuitHints: AvmExecutionHints;
122
+
123
+ /** The name of the function that was executed. Only used for logging. */
124
+ functionName: string;
125
+ }
126
+
127
+ export async function resultToPublicCallRequest(result: PublicFunctionCallResult) {
128
+ const request = result.executionRequest;
129
+ const item = new PublicCallStackItemCompressed(
130
+ request.callContext.contractAddress,
131
+ request.callContext,
132
+ await computeVarArgsHash(request.args),
133
+ await computeVarArgsHash(result.returnValues),
134
+ // TODO(@just-mitch): need better mapping from simulator to revert code.
135
+ result.reverted ? RevertCode.APP_LOGIC_REVERTED : RevertCode.OK,
136
+ Gas.from(result.startGasLeft),
137
+ Gas.from(result.endGasLeft),
138
+ );
139
+ return new PublicInnerCallRequest(item, result.startSideEffectCounter.toNumber());
140
+ }
@@ -0,0 +1,71 @@
1
+ import {
2
+ Attributes,
3
+ type Histogram,
4
+ Metrics,
5
+ type TelemetryClient,
6
+ type Tracer,
7
+ type UpDownCounter,
8
+ ValueType,
9
+ } from '@aztec/telemetry-client';
10
+
11
+ export class ExecutorMetrics {
12
+ public readonly tracer: Tracer;
13
+ private fnCount: UpDownCounter;
14
+ private fnDuration: Histogram;
15
+ private manaPerSecond: Histogram;
16
+ private privateEffectsInsertions: Histogram;
17
+
18
+ constructor(client: TelemetryClient, name = 'PublicExecutor') {
19
+ this.tracer = client.getTracer(name);
20
+ const meter = client.getMeter(name);
21
+
22
+ this.fnCount = meter.createUpDownCounter(Metrics.PUBLIC_EXECUTOR_SIMULATION_COUNT, {
23
+ description: 'Number of functions executed',
24
+ });
25
+
26
+ this.fnDuration = meter.createHistogram(Metrics.PUBLIC_EXECUTOR_SIMULATION_DURATION, {
27
+ description: 'How long it takes to execute a function',
28
+ unit: 'ms',
29
+ valueType: ValueType.INT,
30
+ });
31
+
32
+ this.manaPerSecond = meter.createHistogram(Metrics.PUBLIC_EXECUTOR_SIMULATION_MANA_PER_SECOND, {
33
+ description: 'Mana used per second',
34
+ unit: 'mana/s',
35
+ valueType: ValueType.INT,
36
+ });
37
+
38
+ this.privateEffectsInsertions = meter.createHistogram(Metrics.PUBLIC_EXECUTION_PRIVATE_EFFECTS_INSERTION, {
39
+ description: 'Private effects insertion time',
40
+ unit: 'us',
41
+ valueType: ValueType.INT,
42
+ });
43
+ }
44
+
45
+ recordFunctionSimulation(durationMs: number, manaUsed: number, fnName: string) {
46
+ this.fnCount.add(1, {
47
+ [Attributes.OK]: true,
48
+ [Attributes.APP_CIRCUIT_NAME]: fnName,
49
+ [Attributes.MANA_USED]: manaUsed,
50
+ });
51
+ this.fnDuration.record(Math.ceil(durationMs));
52
+ if (durationMs > 0 && manaUsed > 0) {
53
+ const manaPerSecond = Math.round((manaUsed * 1000) / durationMs);
54
+ this.manaPerSecond.record(manaPerSecond, {
55
+ [Attributes.APP_CIRCUIT_NAME]: fnName,
56
+ });
57
+ }
58
+ }
59
+
60
+ recordFunctionSimulationFailure() {
61
+ this.fnCount.add(1, {
62
+ [Attributes.OK]: false,
63
+ });
64
+ }
65
+
66
+ recordPrivateEffectsInsertion(durationUs: number, type: 'revertible' | 'non-revertible') {
67
+ this.privateEffectsInsertions.record(Math.ceil(durationUs), {
68
+ [Attributes.REVERTIBILITY]: type,
69
+ });
70
+ }
71
+ }
@@ -0,0 +1,2 @@
1
+ export * from './public_tx_simulation_tester.js';
2
+ export * from './utils.js';
@@ -0,0 +1,179 @@
1
+ import { PUBLIC_DISPATCH_SELECTOR } from '@aztec/constants';
2
+ import { Fr } from '@aztec/foundation/fields';
3
+ import { type ContractArtifact, FunctionSelector, encodeArguments } from '@aztec/stdlib/abi';
4
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
+ import { GasFees } from '@aztec/stdlib/gas';
6
+ import type { MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
7
+ import { PublicExecutionRequest, type Tx } from '@aztec/stdlib/tx';
8
+ import { CallContext, GlobalVariables } from '@aztec/stdlib/tx';
9
+ import { NativeWorldStateService } from '@aztec/world-state';
10
+
11
+ import { BaseAvmSimulationTester } from '../avm/fixtures/base_avm_simulation_tester.js';
12
+ import { getContractFunctionArtifact, getFunctionSelector } from '../avm/fixtures/index.js';
13
+ import { SimpleContractDataSource } from '../avm/fixtures/simple_contract_data_source.js';
14
+ import { WorldStateDB } from '../public_db_sources.js';
15
+ import { type PublicTxResult, PublicTxSimulator } from '../public_tx_simulator/public_tx_simulator.js';
16
+ import { createTxForPublicCalls } from './index.js';
17
+
18
+ const TIMESTAMP = new Fr(99833);
19
+ const DEFAULT_GAS_FEES = new GasFees(2, 3);
20
+ export const DEFAULT_BLOCK_NUMBER = 42;
21
+
22
+ export type TestEnqueuedCall = {
23
+ address: AztecAddress;
24
+ fnName: string;
25
+ args: any[];
26
+ isStaticCall?: boolean;
27
+ contractArtifact?: ContractArtifact;
28
+ };
29
+
30
+ /**
31
+ * A test class that extends the BaseAvmSimulationTester to enable real-app testing of the PublicTxSimulator.
32
+ * It provides an interface for simulating one transaction at a time and maintains state between subsequent
33
+ * transactions.
34
+ */
35
+ export class PublicTxSimulationTester extends BaseAvmSimulationTester {
36
+ private txCount = 0;
37
+
38
+ constructor(
39
+ private worldStateDB: WorldStateDB,
40
+ contractDataSource: SimpleContractDataSource,
41
+ merkleTrees: MerkleTreeWriteOperations,
42
+ ) {
43
+ super(contractDataSource, merkleTrees);
44
+ }
45
+
46
+ public static async create(): Promise<PublicTxSimulationTester> {
47
+ const contractDataSource = new SimpleContractDataSource();
48
+ const merkleTrees = await (await NativeWorldStateService.tmp()).fork();
49
+ const worldStateDB = new WorldStateDB(merkleTrees, contractDataSource);
50
+ return new PublicTxSimulationTester(worldStateDB, contractDataSource, merkleTrees);
51
+ }
52
+
53
+ public async createTx(
54
+ sender: AztecAddress,
55
+ setupCalls: TestEnqueuedCall[] = [],
56
+ appCalls: TestEnqueuedCall[] = [],
57
+ teardownCall?: TestEnqueuedCall,
58
+ feePayer: AztecAddress = sender,
59
+ /* need some unique first nullifier for note-nonce computations */
60
+ firstNullifier = new Fr(420000 + this.txCount++),
61
+ ): Promise<Tx> {
62
+ const setupExecutionRequests: PublicExecutionRequest[] = [];
63
+ for (let i = 0; i < setupCalls.length; i++) {
64
+ const address = setupCalls[i].address;
65
+ const contractArtifact =
66
+ setupCalls[i].contractArtifact || (await this.contractDataSource.getContractArtifact(address));
67
+ if (!contractArtifact) {
68
+ throw new Error(`Contract artifact not found for address: ${address}`);
69
+ }
70
+ const req = await executionRequestForCall(
71
+ contractArtifact,
72
+ sender,
73
+ address,
74
+ setupCalls[i].fnName,
75
+ setupCalls[i].args,
76
+ setupCalls[i].isStaticCall,
77
+ );
78
+ setupExecutionRequests.push(req);
79
+ }
80
+ const appExecutionRequests: PublicExecutionRequest[] = [];
81
+ for (let i = 0; i < appCalls.length; i++) {
82
+ const address = appCalls[i].address;
83
+ const contractArtifact =
84
+ appCalls[i].contractArtifact || (await this.contractDataSource.getContractArtifact(address));
85
+ if (!contractArtifact) {
86
+ throw new Error(`Contract artifact not found for address: ${address}`);
87
+ }
88
+ const req = await executionRequestForCall(
89
+ contractArtifact,
90
+ sender,
91
+ address,
92
+ appCalls[i].fnName,
93
+ appCalls[i].args,
94
+ appCalls[i].isStaticCall,
95
+ );
96
+ appExecutionRequests.push(req);
97
+ }
98
+
99
+ let teardownExecutionRequest: PublicExecutionRequest | undefined = undefined;
100
+ if (teardownCall) {
101
+ const address = teardownCall.address;
102
+ const contractArtifact =
103
+ teardownCall.contractArtifact || (await this.contractDataSource.getContractArtifact(address));
104
+ if (!contractArtifact) {
105
+ throw new Error(`Contract artifact not found for address: ${address}`);
106
+ }
107
+ teardownExecutionRequest = await executionRequestForCall(
108
+ contractArtifact,
109
+ sender,
110
+ address,
111
+ teardownCall.fnName,
112
+ teardownCall.args,
113
+ teardownCall.isStaticCall,
114
+ );
115
+ }
116
+
117
+ return await createTxForPublicCalls(
118
+ firstNullifier,
119
+ setupExecutionRequests,
120
+ appExecutionRequests,
121
+ teardownExecutionRequest,
122
+ feePayer,
123
+ );
124
+ }
125
+
126
+ public async simulateTx(
127
+ sender: AztecAddress,
128
+ setupCalls: TestEnqueuedCall[] = [],
129
+ appCalls: TestEnqueuedCall[] = [],
130
+ teardownCall?: TestEnqueuedCall,
131
+ feePayer: AztecAddress = sender,
132
+ /* need some unique first nullifier for note-nonce computations */
133
+ firstNullifier = new Fr(420000 + this.txCount++),
134
+ globals = defaultGlobals(),
135
+ ): Promise<PublicTxResult> {
136
+ const tx = await this.createTx(sender, setupCalls, appCalls, teardownCall, feePayer, firstNullifier);
137
+
138
+ await this.setFeePayerBalance(feePayer);
139
+
140
+ const simulator = new PublicTxSimulator(this.merkleTrees, this.worldStateDB, globals, /*doMerkleOperations=*/ true);
141
+
142
+ const startTime = performance.now();
143
+ const avmResult = await simulator.simulate(tx);
144
+ const endTime = performance.now();
145
+ this.logger.debug(`Public transaction simulation took ${endTime - startTime}ms`);
146
+
147
+ return avmResult;
148
+ }
149
+ }
150
+
151
+ async function executionRequestForCall(
152
+ contractArtifact: ContractArtifact,
153
+ sender: AztecAddress,
154
+ address: AztecAddress,
155
+ fnName: string,
156
+ args: Fr[] = [],
157
+ isStaticCall: boolean = false,
158
+ ): Promise<PublicExecutionRequest> {
159
+ const fnSelector = await getFunctionSelector(fnName, contractArtifact);
160
+ const fnAbi = getContractFunctionArtifact(fnName, contractArtifact);
161
+ const encodedArgs = encodeArguments(fnAbi!, args);
162
+ const calldata = [fnSelector.toField(), ...encodedArgs];
163
+
164
+ const callContext = new CallContext(
165
+ sender,
166
+ address,
167
+ /*selector=*/ new FunctionSelector(PUBLIC_DISPATCH_SELECTOR),
168
+ isStaticCall,
169
+ );
170
+ return new PublicExecutionRequest(callContext, calldata);
171
+ }
172
+
173
+ function defaultGlobals() {
174
+ const globals = GlobalVariables.empty();
175
+ globals.timestamp = TIMESTAMP;
176
+ globals.gasFees = DEFAULT_GAS_FEES; // apply some nonzero default gas fees
177
+ globals.blockNumber = new Fr(DEFAULT_BLOCK_NUMBER);
178
+ return globals;
179
+ }