@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,199 @@
1
+ import {
2
+ CONTRACT_CLASS_LOG_DATA_SIZE_IN_FIELDS,
3
+ DEFAULT_GAS_LIMIT,
4
+ DEPLOYER_CONTRACT_ADDRESS,
5
+ MAX_L2_GAS_PER_TX_PUBLIC_PORTION,
6
+ PRIVATE_LOG_SIZE_IN_FIELDS,
7
+ REGISTERER_CONTRACT_ADDRESS,
8
+ REGISTERER_CONTRACT_CLASS_REGISTERED_MAGIC_VALUE,
9
+ } from '@aztec/constants';
10
+ import { Fr } from '@aztec/foundation/fields';
11
+ import { assertLength } from '@aztec/foundation/serialize';
12
+ import { DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_TAG } from '@aztec/protocol-contracts';
13
+ import { bufferAsFields } from '@aztec/stdlib/abi';
14
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
15
+ import type { ContractClassPublic, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
16
+ import { Gas, GasFees, GasSettings } from '@aztec/stdlib/gas';
17
+ import { siloNullifier } from '@aztec/stdlib/hash';
18
+ import {
19
+ PartialPrivateTailPublicInputsForPublic,
20
+ PartialPrivateTailPublicInputsForRollup,
21
+ PrivateKernelTailCircuitPublicInputs,
22
+ RollupValidationRequests,
23
+ ScopedLogHash,
24
+ countAccumulatedItems,
25
+ } from '@aztec/stdlib/kernel';
26
+ import { ContractClassLog, PrivateLog } from '@aztec/stdlib/logs';
27
+ import { type PublicExecutionRequest, Tx } from '@aztec/stdlib/tx';
28
+ import { BlockHeader, TxConstantData, TxContext } from '@aztec/stdlib/tx';
29
+
30
+ import { strict as assert } from 'assert';
31
+
32
+ /**
33
+ * Craft a carrier transaction for some public calls for simulation by PublicTxSimulator.
34
+ */
35
+ export async function createTxForPublicCalls(
36
+ firstNullifier: Fr,
37
+ setupExecutionRequests: PublicExecutionRequest[],
38
+ appExecutionRequests: PublicExecutionRequest[],
39
+ teardownExecutionRequest?: PublicExecutionRequest,
40
+ feePayer = AztecAddress.zero(),
41
+ gasUsedByPrivate: Gas = Gas.empty(),
42
+ ): Promise<Tx> {
43
+ assert(
44
+ setupExecutionRequests.length > 0 || appExecutionRequests.length > 0 || teardownExecutionRequest !== undefined,
45
+ "Can't create public tx with no enqueued calls",
46
+ );
47
+ const setupCallRequests = await Promise.all(setupExecutionRequests.map(er => er.toCallRequest()));
48
+ const appCallRequests = await Promise.all(appExecutionRequests.map(er => er.toCallRequest()));
49
+ // use max limits
50
+ const gasLimits = new Gas(DEFAULT_GAS_LIMIT, MAX_L2_GAS_PER_TX_PUBLIC_PORTION);
51
+
52
+ const forPublic = PartialPrivateTailPublicInputsForPublic.empty();
53
+ // TODO(#9269): Remove this fake nullifier method as we move away from 1st nullifier as hash.
54
+ forPublic.nonRevertibleAccumulatedData.nullifiers[0] = firstNullifier;
55
+
56
+ // We reverse order because the simulator expects it to be like a "stack" of calls to pop from
57
+ for (let i = setupCallRequests.length - 1; i >= 0; i--) {
58
+ forPublic.nonRevertibleAccumulatedData.publicCallRequests[i] = setupCallRequests[i];
59
+ }
60
+ for (let i = appCallRequests.length - 1; i >= 0; i--) {
61
+ forPublic.revertibleAccumulatedData.publicCallRequests[i] = appCallRequests[i];
62
+ }
63
+ if (teardownExecutionRequest) {
64
+ forPublic.publicTeardownCallRequest = await teardownExecutionRequest.toCallRequest();
65
+ }
66
+
67
+ const maxFeesPerGas = feePayer.isZero() ? GasFees.empty() : new GasFees(10, 10);
68
+ const teardownGasLimits = teardownExecutionRequest ? gasLimits : Gas.empty();
69
+ const gasSettings = new GasSettings(gasLimits, teardownGasLimits, maxFeesPerGas, GasFees.empty());
70
+ const txContext = new TxContext(Fr.zero(), Fr.zero(), gasSettings);
71
+ const constantData = new TxConstantData(BlockHeader.empty(), txContext, Fr.zero(), Fr.zero());
72
+
73
+ const txData = new PrivateKernelTailCircuitPublicInputs(
74
+ constantData,
75
+ RollupValidationRequests.empty(),
76
+ /*gasUsed=*/ gasUsedByPrivate,
77
+ feePayer,
78
+ forPublic,
79
+ );
80
+ const tx = Tx.newWithTxData(txData, teardownExecutionRequest);
81
+
82
+ // Reverse order because the simulator expects it to be like a "stack" of calls to pop from.
83
+ // Also push app calls before setup calls for this reason.
84
+ for (let i = appExecutionRequests.length - 1; i >= 0; i--) {
85
+ tx.enqueuedPublicFunctionCalls.push(appExecutionRequests[i]);
86
+ }
87
+ for (let i = setupExecutionRequests.length - 1; i >= 0; i--) {
88
+ tx.enqueuedPublicFunctionCalls.push(setupExecutionRequests[i]);
89
+ }
90
+
91
+ return tx;
92
+ }
93
+
94
+ export function createTxForPrivateOnly(feePayer = AztecAddress.zero(), gasUsedByPrivate: Gas = new Gas(10, 10)): Tx {
95
+ // use max limits
96
+ const gasLimits = new Gas(DEFAULT_GAS_LIMIT, MAX_L2_GAS_PER_TX_PUBLIC_PORTION);
97
+
98
+ const forRollup = PartialPrivateTailPublicInputsForRollup.empty();
99
+
100
+ const maxFeesPerGas = feePayer.isZero() ? GasFees.empty() : new GasFees(10, 10);
101
+ const gasSettings = new GasSettings(gasLimits, Gas.empty(), maxFeesPerGas, GasFees.empty());
102
+ const txContext = new TxContext(Fr.zero(), Fr.zero(), gasSettings);
103
+ const constantData = new TxConstantData(BlockHeader.empty(), txContext, Fr.zero(), Fr.zero());
104
+
105
+ const txData = new PrivateKernelTailCircuitPublicInputs(
106
+ constantData,
107
+ RollupValidationRequests.empty(),
108
+ /*gasUsed=*/ gasUsedByPrivate,
109
+ feePayer,
110
+ /*forPublic=*/ undefined,
111
+ forRollup,
112
+ );
113
+ return Tx.newWithTxData(txData);
114
+ }
115
+
116
+ export async function addNewContractClassToTx(
117
+ tx: Tx,
118
+ contractClass: ContractClassPublic,
119
+ skipNullifierInsertion = false,
120
+ ) {
121
+ const contractClassLogFields = [
122
+ new Fr(REGISTERER_CONTRACT_CLASS_REGISTERED_MAGIC_VALUE),
123
+ contractClass.id,
124
+ new Fr(contractClass.version),
125
+ new Fr(contractClass.artifactHash),
126
+ new Fr(contractClass.privateFunctionsRoot),
127
+ ...bufferAsFields(contractClass.packedBytecode, Math.ceil(contractClass.packedBytecode.length / 31) + 1),
128
+ ];
129
+ const contractClassLog = ContractClassLog.fromFields([
130
+ new Fr(REGISTERER_CONTRACT_ADDRESS),
131
+ ...contractClassLogFields.concat(
132
+ new Array(CONTRACT_CLASS_LOG_DATA_SIZE_IN_FIELDS - contractClassLogFields.length).fill(Fr.ZERO),
133
+ ),
134
+ ]);
135
+ const contractClassLogHash = ScopedLogHash.fromFields([
136
+ await contractClassLog.hash(),
137
+ new Fr(7),
138
+ new Fr(contractClassLog.getEmittedLength()),
139
+ new Fr(REGISTERER_CONTRACT_ADDRESS),
140
+ ]);
141
+
142
+ const accumulatedData = tx.data.forPublic ? tx.data.forPublic!.revertibleAccumulatedData : tx.data.forRollup!.end;
143
+ if (!skipNullifierInsertion) {
144
+ const nextNullifierIndex = countAccumulatedItems(accumulatedData.nullifiers);
145
+ accumulatedData.nullifiers[nextNullifierIndex] = contractClass.id;
146
+ }
147
+
148
+ const nextLogIndex = countAccumulatedItems(accumulatedData.contractClassLogsHashes);
149
+ accumulatedData.contractClassLogsHashes[nextLogIndex] = contractClassLogHash;
150
+
151
+ tx.contractClassLogs.push(contractClassLog);
152
+ }
153
+
154
+ export async function addNewContractInstanceToTx(
155
+ tx: Tx,
156
+ contractInstance: ContractInstanceWithAddress,
157
+ skipNullifierInsertion = false,
158
+ ) {
159
+ // can't use publicKeys.toFields() because it includes isInfinite which
160
+ // is not broadcast in such private logs
161
+ const publicKeysAsFields = [
162
+ contractInstance.publicKeys.masterNullifierPublicKey.x,
163
+ contractInstance.publicKeys.masterNullifierPublicKey.y,
164
+ contractInstance.publicKeys.masterIncomingViewingPublicKey.x,
165
+ contractInstance.publicKeys.masterIncomingViewingPublicKey.y,
166
+ contractInstance.publicKeys.masterOutgoingViewingPublicKey.x,
167
+ contractInstance.publicKeys.masterOutgoingViewingPublicKey.y,
168
+ contractInstance.publicKeys.masterTaggingPublicKey.x,
169
+ contractInstance.publicKeys.masterTaggingPublicKey.y,
170
+ ];
171
+ const fields = [
172
+ DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_TAG,
173
+ contractInstance.address.toField(),
174
+ new Fr(contractInstance.version),
175
+ new Fr(contractInstance.salt),
176
+ contractInstance.currentContractClassId,
177
+ contractInstance.initializationHash,
178
+ ...publicKeysAsFields,
179
+ contractInstance.deployer.toField(),
180
+ new Fr(0),
181
+ new Fr(0),
182
+ new Fr(0),
183
+ ];
184
+ const contractInstanceLog = new PrivateLog(assertLength(fields, PRIVATE_LOG_SIZE_IN_FIELDS));
185
+
186
+ const contractAddressNullifier = await siloNullifier(
187
+ AztecAddress.fromNumber(DEPLOYER_CONTRACT_ADDRESS),
188
+ contractInstance.address.toField(),
189
+ );
190
+
191
+ const accumulatedData = tx.data.forPublic ? tx.data.forPublic!.revertibleAccumulatedData : tx.data.forRollup!.end;
192
+ if (!skipNullifierInsertion) {
193
+ const nextNullifierIndex = countAccumulatedItems(accumulatedData.nullifiers);
194
+ accumulatedData.nullifiers[nextNullifierIndex] = contractAddressNullifier;
195
+ }
196
+
197
+ const nextLogIndex = countAccumulatedItems(accumulatedData.privateLogs);
198
+ accumulatedData.privateLogs[nextLogIndex] = contractInstanceLog;
199
+ }
@@ -0,0 +1,9 @@
1
+ export * from '../common/db_interfaces.js';
2
+ export * from './public_tx_simulator/public_tx_simulator.js';
3
+ export { type EnqueuedPublicCallExecutionResult, type PublicFunctionCallResult } from './execution.js';
4
+ export * from './public_db_sources.js';
5
+ export { PublicProcessor, PublicProcessorFactory } from './public_processor/public_processor.js';
6
+ export { SideEffectTrace } from './side_effect_trace.js';
7
+ export { getExecutionRequestsByPhase } from './utils.js';
8
+ export { PublicTxSimulationTester } from './fixtures/index.js';
9
+ export * from './avm/index.js';
@@ -0,0 +1,369 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import { Timer } from '@aztec/foundation/timer';
4
+ import { ContractClassRegisteredEvent } from '@aztec/protocol-contracts/class-registerer';
5
+ import { ContractInstanceDeployedEvent } from '@aztec/protocol-contracts/instance-deployer';
6
+ import type { FunctionSelector } from '@aztec/stdlib/abi';
7
+ import { PublicDataWrite } from '@aztec/stdlib/avm';
8
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
9
+ import {
10
+ type ContractClassPublic,
11
+ type ContractDataSource,
12
+ type ContractInstanceWithAddress,
13
+ computePublicBytecodeCommitment,
14
+ } from '@aztec/stdlib/contract';
15
+ import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
16
+ import type {
17
+ MerkleTreeCheckpointOperations,
18
+ MerkleTreeReadOperations,
19
+ MerkleTreeWriteOperations,
20
+ } from '@aztec/stdlib/interfaces/server';
21
+ import { ContractClassLog, PrivateLog } from '@aztec/stdlib/logs';
22
+ import type { PublicDBAccessStats } from '@aztec/stdlib/stats';
23
+ import { MerkleTreeId, type PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
24
+ import type { Tx } from '@aztec/stdlib/tx';
25
+
26
+ import type { PublicContractsDB, PublicStateDB } from '../common/db_interfaces.js';
27
+ import { TxContractCache } from './tx_contract_cache.js';
28
+
29
+ /**
30
+ * Implements the PublicContractsDB using a ContractDataSource.
31
+ * Progressively records contracts in transaction as they are processed in a block.
32
+ * Separates block-level contract information (from processed/included txs) from the
33
+ * current tx's contract information (which may be cleared on tx revert/death).
34
+ */
35
+ export class ContractsDataSourcePublicDB implements PublicContractsDB {
36
+ // Two caching layers for contract classes and instances.
37
+ // Tx-level cache:
38
+ // - The current tx's new contract information is cached
39
+ // in currentTxNonRevertibleCache and currentTxRevertibleCache.
40
+ // Block-level cache:
41
+ // - Contract information from earlier in the block, usable by later txs.
42
+ // When a tx succeeds, that tx's caches are merged into the block cache and cleared.
43
+ private currentTxNonRevertibleCache = new TxContractCache();
44
+ private currentTxRevertibleCache = new TxContractCache();
45
+ private blockCache = new TxContractCache();
46
+ // Separate flat cache for bytecode commitments.
47
+ private bytecodeCommitmentCache = new Map<string, Fr>();
48
+
49
+ private log = createLogger('simulator:contracts-data-source');
50
+
51
+ constructor(private dataSource: ContractDataSource) {}
52
+
53
+ /**
54
+ * Add new contracts from a transaction
55
+ * @param tx - The transaction to add contracts from.
56
+ */
57
+ public async addNewContracts(tx: Tx): Promise<void> {
58
+ await this.addNonRevertibleContractClasses(tx);
59
+ await this.addRevertibleContractClasses(tx);
60
+ this.addNonRevertibleContractInstances(tx);
61
+ this.addRevertibleContractInstances(tx);
62
+ }
63
+
64
+ /**
65
+ * Add non revertible contracts from a transaction
66
+ * @param tx - The transaction to add non revertible contracts from.
67
+ */
68
+ public async addNewNonRevertibleContracts(tx: Tx) {
69
+ await this.addNonRevertibleContractClasses(tx);
70
+ this.addNonRevertibleContractInstances(tx);
71
+ }
72
+
73
+ /**
74
+ * Add revertible contracts from a transaction
75
+ * @param tx - The transaction to add revertible contracts from.
76
+ */
77
+ public async addNewRevertibleContracts(tx: Tx) {
78
+ await this.addRevertibleContractClasses(tx);
79
+ this.addRevertibleContractInstances(tx);
80
+ }
81
+
82
+ /**
83
+ * Add non-revertible contract classes from a transaction
84
+ * For private-only txs, this will be all contract classes (found in tx.data.forPublic)
85
+ * @param tx - The transaction to add non-revertible contract classes from.
86
+ */
87
+ private async addNonRevertibleContractClasses(tx: Tx) {
88
+ const siloedContractClassLogs = tx.data.forPublic
89
+ ? await tx.filterContractClassLogs(
90
+ tx.data.forPublic!.nonRevertibleAccumulatedData.contractClassLogsHashes,
91
+ /*siloed=*/ true,
92
+ )
93
+ : await tx.filterContractClassLogs(tx.data.forRollup!.end.contractClassLogsHashes, /*siloed=*/ true);
94
+
95
+ await this.addContractClassesFromLogs(siloedContractClassLogs, this.currentTxNonRevertibleCache, 'non-revertible');
96
+ }
97
+
98
+ /**
99
+ * Add revertible contract classes from a transaction
100
+ * None for private-only txs.
101
+ * @param tx - The transaction to add revertible contract classes from.
102
+ */
103
+ private async addRevertibleContractClasses(tx: Tx) {
104
+ const siloedContractClassLogs = tx.data.forPublic
105
+ ? await tx.filterContractClassLogs(
106
+ tx.data.forPublic!.revertibleAccumulatedData.contractClassLogsHashes,
107
+ /*siloed=*/ true,
108
+ )
109
+ : [];
110
+
111
+ await this.addContractClassesFromLogs(siloedContractClassLogs, this.currentTxRevertibleCache, 'revertible');
112
+ }
113
+
114
+ /**
115
+ * Add non-revertible contract instances from a transaction
116
+ * For private-only txs, this will be all contract instances (found in tx.data.forRollup)
117
+ * @param tx - The transaction to add non-revertible contract instances from.
118
+ */
119
+ private addNonRevertibleContractInstances(tx: Tx) {
120
+ const contractInstanceLogs = tx.data.forPublic
121
+ ? tx.data.forPublic!.nonRevertibleAccumulatedData.privateLogs.filter(l => !l.isEmpty())
122
+ : tx.data.forRollup!.end.privateLogs.filter(l => !l.isEmpty());
123
+
124
+ this.addContractInstancesFromLogs(contractInstanceLogs, this.currentTxNonRevertibleCache, 'non-revertible');
125
+ }
126
+
127
+ /**
128
+ * Add revertible contract instances from a transaction
129
+ * None for private-only txs.
130
+ * @param tx - The transaction to add revertible contract instances from.
131
+ */
132
+ private addRevertibleContractInstances(tx: Tx) {
133
+ const contractInstanceLogs = tx.data.forPublic
134
+ ? tx.data.forPublic!.revertibleAccumulatedData.privateLogs.filter(l => !l.isEmpty())
135
+ : [];
136
+
137
+ this.addContractInstancesFromLogs(contractInstanceLogs, this.currentTxRevertibleCache, 'revertible');
138
+ }
139
+
140
+ /**
141
+ * Given a tx's siloed contract class logs, add the contract classes to the cache
142
+ * @param siloedContractClassLogs - Contract class logs to process
143
+ * @param cache - The cache to store the contract classes in
144
+ * @param cacheType - Type of cache (for logging)
145
+ */
146
+ private async addContractClassesFromLogs(
147
+ siloedContractClassLogs: ContractClassLog[],
148
+ cache: TxContractCache,
149
+ cacheType: string,
150
+ ) {
151
+ const contractClassEvents = siloedContractClassLogs
152
+ .filter((log: ContractClassLog) => ContractClassRegisteredEvent.isContractClassRegisteredEvent(log))
153
+ .map((log: ContractClassLog) => ContractClassRegisteredEvent.fromLog(log));
154
+
155
+ // Cache contract classes
156
+ await Promise.all(
157
+ contractClassEvents.map(async (event: ContractClassRegisteredEvent) => {
158
+ this.log.debug(`Adding class ${event.contractClassId.toString()} to contract's ${cacheType} tx cache`);
159
+ const contractClass = await event.toContractClassPublic();
160
+
161
+ cache.addClass(event.contractClassId, contractClass);
162
+ }),
163
+ );
164
+ }
165
+
166
+ /**
167
+ * Given a tx's contract instance logs, add the contract instances to the cache
168
+ * @param contractInstanceLogs - Contract instance logs to process
169
+ * @param cache - The cache to store the contract instances in
170
+ * @param cacheType - Type of cache (for logging)
171
+ */
172
+ private addContractInstancesFromLogs(contractInstanceLogs: PrivateLog[], cache: TxContractCache, cacheType: string) {
173
+ const contractInstanceEvents = contractInstanceLogs
174
+ .filter(log => ContractInstanceDeployedEvent.isContractInstanceDeployedEvent(log))
175
+ .map(log => ContractInstanceDeployedEvent.fromLog(log));
176
+
177
+ // Cache contract instances
178
+ contractInstanceEvents.forEach(e => {
179
+ this.log.debug(
180
+ `Adding instance ${e.address.toString()} with class ${e.contractClassId.toString()} to ${cacheType} tx contract cache`,
181
+ );
182
+ cache.addInstance(e.address, e.toContractInstance());
183
+ });
184
+ }
185
+
186
+ /**
187
+ * Clear new contracts from the current tx's cache
188
+ */
189
+ public clearContractsForTx() {
190
+ this.currentTxRevertibleCache.clear();
191
+ this.currentTxRevertibleCache.clear();
192
+ this.currentTxNonRevertibleCache.clear();
193
+ }
194
+
195
+ /**
196
+ * Commits the current transaction's cached contracts to the block-level cache.
197
+ * Then, clears the tx cache.
198
+ */
199
+ public commitContractsForTx(onlyNonRevertibles: boolean = false) {
200
+ // Merge non-revertible tx cache into block cache
201
+ this.blockCache.mergeFrom(this.currentTxNonRevertibleCache);
202
+
203
+ if (!onlyNonRevertibles) {
204
+ // Merge revertible tx cache into block cache
205
+ this.blockCache.mergeFrom(this.currentTxRevertibleCache);
206
+ }
207
+
208
+ // Clear the tx's caches
209
+ this.currentTxNonRevertibleCache.clear();
210
+ this.currentTxRevertibleCache.clear();
211
+ }
212
+
213
+ public async getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
214
+ // Check caches in order: tx revertible -> tx non-revertible -> block -> data source
215
+ return (
216
+ this.currentTxRevertibleCache.getInstance(address) ??
217
+ this.currentTxNonRevertibleCache.getInstance(address) ??
218
+ this.blockCache.getInstance(address) ??
219
+ (await this.dataSource.getContract(address))
220
+ );
221
+ }
222
+
223
+ public async getContractClass(contractClassId: Fr): Promise<ContractClassPublic | undefined> {
224
+ // Check caches in order: tx revertible -> tx non-revertible -> block -> data source
225
+ return (
226
+ this.currentTxRevertibleCache.getClass(contractClassId) ??
227
+ this.currentTxNonRevertibleCache.getClass(contractClassId) ??
228
+ this.blockCache.getClass(contractClassId) ??
229
+ (await this.dataSource.getContractClass(contractClassId))
230
+ );
231
+ }
232
+
233
+ public async getBytecodeCommitment(contractClassId: Fr): Promise<Fr | undefined> {
234
+ // Try and retrieve from cache
235
+ const key = contractClassId.toString();
236
+ const result = this.bytecodeCommitmentCache.get(key);
237
+ if (result !== undefined) {
238
+ return result;
239
+ }
240
+ // Now try from the store
241
+ const fromStore = await this.dataSource.getBytecodeCommitment(contractClassId);
242
+ if (fromStore !== undefined) {
243
+ this.bytecodeCommitmentCache.set(key, fromStore);
244
+ return fromStore;
245
+ }
246
+
247
+ // Not in either the store or the cache, build it here and cache
248
+ const contractClass = await this.getContractClass(contractClassId);
249
+ if (contractClass === undefined) {
250
+ return undefined;
251
+ }
252
+
253
+ const value = await computePublicBytecodeCommitment(contractClass.packedBytecode);
254
+ this.bytecodeCommitmentCache.set(key, value);
255
+ return value;
256
+ }
257
+
258
+ public async getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
259
+ return await this.dataSource.getContractFunctionName(address, selector);
260
+ }
261
+ }
262
+
263
+ /**
264
+ * A public state DB that reads and writes to the world state.
265
+ */
266
+ export class WorldStateDB extends ContractsDataSourcePublicDB implements PublicStateDB, MerkleTreeCheckpointOperations {
267
+ private logger = createLogger('simulator:world-state-db');
268
+
269
+ constructor(public db: MerkleTreeWriteOperations, dataSource: ContractDataSource) {
270
+ super(dataSource);
271
+ }
272
+
273
+ /**
274
+ * Checkpoints the current fork state
275
+ */
276
+ public async createCheckpoint() {
277
+ await this.db.createCheckpoint();
278
+ }
279
+
280
+ /**
281
+ * Commits the current checkpoint
282
+ */
283
+ public async commitCheckpoint() {
284
+ await this.db.commitCheckpoint();
285
+ }
286
+
287
+ /**
288
+ * Reverts the current checkpoint
289
+ */
290
+ public async revertCheckpoint() {
291
+ await this.db.revertCheckpoint();
292
+ }
293
+
294
+ public getMerkleInterface(): MerkleTreeWriteOperations {
295
+ return this.db;
296
+ }
297
+
298
+ /**
299
+ * Reads a value from public storage, returning zero if none.
300
+ * @param contract - Owner of the storage.
301
+ * @param slot - Slot to read in the contract storage.
302
+ * @returns The current value in the storage slot.
303
+ */
304
+ public async storageRead(contract: AztecAddress, slot: Fr): Promise<Fr> {
305
+ return await readPublicState(this.db, contract, slot);
306
+ }
307
+
308
+ /**
309
+ * Records a write to public storage.
310
+ * @param contract - Owner of the storage.
311
+ * @param slot - Slot to read in the contract storage.
312
+ * @param newValue - The new value to store.
313
+ * @returns The slot of the written leaf in the public data tree.
314
+ */
315
+ public async storageWrite(contract: AztecAddress, slot: Fr, newValue: Fr): Promise<void> {
316
+ const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
317
+ const publicDataWrite = new PublicDataWrite(leafSlot, newValue);
318
+ await this.db.sequentialInsert(MerkleTreeId.PUBLIC_DATA_TREE, [publicDataWrite.toBuffer()]);
319
+ }
320
+
321
+ public async getL1ToL2LeafValue(leafIndex: bigint): Promise<Fr | undefined> {
322
+ const timer = new Timer();
323
+ const leafValue = await this.db.getLeafValue(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, leafIndex);
324
+ this.logger.debug(`[DB] Fetched L1 to L2 message leaf value`, {
325
+ eventName: 'public-db-access',
326
+ duration: timer.ms(),
327
+ operation: 'get-l1-to-l2-message-leaf-value',
328
+ } satisfies PublicDBAccessStats);
329
+ return leafValue;
330
+ }
331
+
332
+ public async getCommitmentValue(leafIndex: bigint): Promise<Fr | undefined> {
333
+ const timer = new Timer();
334
+ const leafValue = await this.db.getLeafValue(MerkleTreeId.NOTE_HASH_TREE, leafIndex);
335
+ this.logger.debug(`[DB] Fetched commitment leaf value`, {
336
+ eventName: 'public-db-access',
337
+ duration: timer.ms(),
338
+ operation: 'get-commitment-leaf-value',
339
+ } satisfies PublicDBAccessStats);
340
+ return leafValue;
341
+ }
342
+
343
+ public async getNullifierIndex(nullifier: Fr): Promise<bigint | undefined> {
344
+ const timer = new Timer();
345
+ const index = (await this.db.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]))[0];
346
+ this.logger.debug(`[DB] Fetched nullifier index`, {
347
+ eventName: 'public-db-access',
348
+ duration: timer.ms(),
349
+ operation: 'get-nullifier-index',
350
+ } satisfies PublicDBAccessStats);
351
+ return index;
352
+ }
353
+ }
354
+
355
+ export async function readPublicState(db: MerkleTreeReadOperations, contract: AztecAddress, slot: Fr): Promise<Fr> {
356
+ const leafSlot = (await computePublicDataTreeLeafSlot(contract, slot)).toBigInt();
357
+
358
+ const lowLeafResult = await db.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot);
359
+ if (!lowLeafResult || !lowLeafResult.alreadyPresent) {
360
+ return Fr.ZERO;
361
+ }
362
+
363
+ const preimage = (await db.getLeafPreimage(
364
+ MerkleTreeId.PUBLIC_DATA_TREE,
365
+ lowLeafResult.index,
366
+ )) as PublicDataTreeLeafPreimage;
367
+
368
+ return preimage.value;
369
+ }