@aztec/simulator 0.76.4 → 0.77.0-testnet-ignition.17

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 (505) hide show
  1. package/README.md +3 -3
  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/{public → common}/db_interfaces.d.ts +8 -33
  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 +3 -2
  9. package/dest/common/debug_fn_name.d.ts.map +1 -1
  10. package/dest/common/debug_fn_name.js +2 -5
  11. package/dest/common/errors.d.ts +4 -4
  12. package/dest/common/errors.d.ts.map +1 -1
  13. package/dest/common/errors.js +30 -39
  14. package/dest/common/index.d.ts +2 -1
  15. package/dest/common/index.d.ts.map +1 -1
  16. package/dest/common/index.js +2 -2
  17. package/dest/common/message_load_oracle_inputs.d.ts +2 -2
  18. package/dest/common/message_load_oracle_inputs.d.ts.map +1 -1
  19. package/dest/common/message_load_oracle_inputs.js +8 -8
  20. package/dest/common/stats/index.d.ts.map +1 -0
  21. package/dest/{stats → common/stats}/stats.d.ts +2 -2
  22. package/dest/common/stats/stats.d.ts.map +1 -0
  23. package/dest/common/stats/stats.js +10 -0
  24. package/dest/{acvm → private/acvm}/acvm.d.ts +4 -4
  25. package/dest/private/acvm/acvm.d.ts.map +1 -0
  26. package/dest/private/acvm/acvm.js +70 -0
  27. package/dest/{acvm → private/acvm}/acvm_types.d.ts +1 -1
  28. package/dest/private/acvm/acvm_types.d.ts.map +1 -0
  29. package/dest/private/acvm/acvm_types.js +3 -0
  30. package/dest/{acvm → private/acvm}/deserialize.d.ts +2 -2
  31. package/dest/private/acvm/deserialize.d.ts.map +1 -0
  32. package/dest/private/acvm/deserialize.js +42 -0
  33. package/dest/private/acvm/index.d.ts.map +1 -0
  34. package/dest/private/acvm/index.js +5 -0
  35. package/dest/{acvm → private/acvm}/oracle/index.d.ts +1 -1
  36. package/dest/private/acvm/oracle/index.d.ts.map +1 -0
  37. package/dest/private/acvm/oracle/index.js +2 -0
  38. package/dest/{acvm → private/acvm}/oracle/oracle.d.ts +4 -3
  39. package/dest/private/acvm/oracle/oracle.d.ts.map +1 -0
  40. package/dest/private/acvm/oracle/oracle.js +263 -0
  41. package/dest/{acvm → private/acvm}/oracle/typed_oracle.d.ts +12 -6
  42. package/dest/private/acvm/oracle/typed_oracle.d.ts.map +1 -0
  43. package/dest/private/acvm/oracle/typed_oracle.js +132 -0
  44. package/dest/{acvm → private/acvm}/serialize.d.ts +7 -3
  45. package/dest/private/acvm/serialize.d.ts.map +1 -0
  46. package/dest/private/acvm/serialize.js +46 -0
  47. package/dest/{client → private}/client_execution_context.d.ts +20 -17
  48. package/dest/private/client_execution_context.d.ts.map +1 -0
  49. package/dest/private/client_execution_context.js +378 -0
  50. package/dest/{client → private}/db_oracle.d.ts +19 -7
  51. package/dest/private/db_oracle.d.ts.map +1 -0
  52. package/dest/private/db_oracle.js +14 -0
  53. package/dest/{client → private}/execution_note_cache.d.ts +2 -2
  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/{common → private}/hashed_values_cache.d.ts +2 -2
  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/{client → private}/pick_notes.d.ts +1 -1
  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 +24 -0
  66. package/dest/private/private_execution.d.ts.map +1 -0
  67. package/dest/private/private_execution.js +90 -0
  68. package/dest/{providers → private/providers}/acvm_native.d.ts +5 -5
  69. package/dest/private/providers/acvm_native.d.ts.map +1 -0
  70. package/dest/private/providers/acvm_native.js +139 -0
  71. package/dest/{providers → private/providers}/acvm_wasm.d.ts +4 -4
  72. package/dest/private/providers/acvm_wasm.d.ts.map +1 -0
  73. package/dest/private/providers/acvm_wasm.js +62 -0
  74. package/dest/{providers → private/providers}/acvm_wasm_with_blobs.d.ts +5 -5
  75. package/dest/private/providers/acvm_wasm_with_blobs.d.ts.map +1 -0
  76. package/dest/{providers → private/providers}/acvm_wasm_with_blobs.js +3 -6
  77. package/dest/{providers → private/providers}/factory.d.ts +1 -1
  78. package/dest/private/providers/factory.d.ts.map +1 -0
  79. package/dest/private/providers/factory.js +27 -0
  80. package/dest/{common → private/providers}/simulation_provider.d.ts +5 -5
  81. package/dest/private/providers/simulation_provider.d.ts.map +1 -0
  82. package/dest/private/providers/simulation_provider.js +24 -0
  83. package/dest/private/simulator.d.ts +36 -0
  84. package/dest/private/simulator.d.ts.map +1 -0
  85. package/dest/private/simulator.js +78 -0
  86. package/dest/{client → private}/unconstrained_execution.d.ts +5 -5
  87. package/dest/private/unconstrained_execution.d.ts.map +1 -0
  88. package/dest/private/unconstrained_execution.js +27 -0
  89. package/dest/{client → private}/view_data_oracle.d.ts +15 -7
  90. package/dest/private/view_data_oracle.d.ts.map +1 -0
  91. package/dest/private/view_data_oracle.js +260 -0
  92. package/dest/{avm → public/avm}/avm_context.d.ts +7 -5
  93. package/dest/public/avm/avm_context.d.ts.map +1 -0
  94. package/dest/public/avm/avm_context.js +44 -0
  95. package/dest/{avm → public/avm}/avm_contract_call_result.d.ts +5 -5
  96. package/dest/public/avm/avm_contract_call_result.d.ts.map +1 -0
  97. package/dest/public/avm/avm_contract_call_result.js +50 -0
  98. package/dest/{avm → public/avm}/avm_execution_environment.d.ts +2 -2
  99. package/dest/public/avm/avm_execution_environment.d.ts.map +1 -0
  100. package/dest/public/avm/avm_execution_environment.js +31 -0
  101. package/dest/public/avm/avm_gas.d.ts.map +1 -0
  102. package/dest/public/avm/avm_gas.js +202 -0
  103. package/dest/{avm → public/avm}/avm_machine_state.d.ts +2 -2
  104. package/dest/public/avm/avm_machine_state.d.ts.map +1 -0
  105. package/dest/public/avm/avm_machine_state.js +106 -0
  106. package/dest/{avm → public/avm}/avm_memory_types.d.ts +2 -2
  107. package/dest/public/avm/avm_memory_types.d.ts.map +1 -0
  108. package/dest/public/avm/avm_memory_types.js +340 -0
  109. package/dest/{avm → public/avm}/avm_simulator.d.ts +5 -3
  110. package/dest/public/avm/avm_simulator.d.ts.map +1 -0
  111. package/dest/public/avm/avm_simulator.js +202 -0
  112. package/dest/public/avm/bytecode_utils.d.ts.map +1 -0
  113. package/dest/public/avm/bytecode_utils.js +17 -0
  114. package/dest/{avm → public/avm}/errors.d.ts +5 -4
  115. package/dest/public/avm/errors.d.ts.map +1 -0
  116. package/dest/public/avm/errors.js +179 -0
  117. package/dest/{avm → public/avm}/fixtures/avm_simulation_tester.d.ts +6 -6
  118. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -0
  119. package/dest/public/avm/fixtures/avm_simulation_tester.js +76 -0
  120. package/dest/{avm → public/avm}/fixtures/base_avm_simulation_tester.d.ts +13 -11
  121. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -0
  122. package/dest/public/avm/fixtures/base_avm_simulation_tester.js +77 -0
  123. package/dest/{avm → public/avm}/fixtures/index.d.ts +7 -7
  124. package/dest/public/avm/fixtures/index.d.ts.map +1 -0
  125. package/dest/public/avm/fixtures/index.js +140 -0
  126. package/dest/{avm → public/avm}/fixtures/simple_contract_data_source.d.ts +10 -4
  127. package/dest/public/avm/fixtures/simple_contract_data_source.d.ts.map +1 -0
  128. package/dest/public/avm/fixtures/simple_contract_data_source.js +112 -0
  129. package/dest/public/avm/index.d.ts +4 -0
  130. package/dest/public/avm/index.d.ts.map +1 -0
  131. package/dest/{avm/index.d.ts → public/avm/index.js} +1 -2
  132. package/dest/public/avm/journal/index.d.ts.map +1 -0
  133. package/dest/{avm → public/avm}/journal/journal.d.ts +51 -18
  134. package/dest/public/avm/journal/journal.d.ts.map +1 -0
  135. package/dest/public/avm/journal/journal.js +488 -0
  136. package/dest/{avm → public/avm}/journal/nullifiers.d.ts +8 -6
  137. package/dest/public/avm/journal/nullifiers.d.ts.map +1 -0
  138. package/dest/public/avm/journal/nullifiers.js +97 -0
  139. package/dest/{avm → public/avm}/journal/public_storage.d.ts +2 -2
  140. package/dest/public/avm/journal/public_storage.d.ts.map +1 -0
  141. package/dest/public/avm/journal/public_storage.js +145 -0
  142. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -0
  143. package/dest/public/avm/opcodes/accrued_substate.js +252 -0
  144. package/dest/public/avm/opcodes/addressing_mode.d.ts.map +1 -0
  145. package/dest/public/avm/opcodes/addressing_mode.js +74 -0
  146. package/dest/public/avm/opcodes/arithmetic.d.ts.map +1 -0
  147. package/dest/public/avm/opcodes/arithmetic.js +73 -0
  148. package/dest/public/avm/opcodes/bitwise.d.ts.map +1 -0
  149. package/dest/public/avm/opcodes/bitwise.js +106 -0
  150. package/dest/public/avm/opcodes/comparators.d.ts.map +1 -0
  151. package/dest/public/avm/opcodes/comparators.js +43 -0
  152. package/dest/public/avm/opcodes/contract.d.ts.map +1 -0
  153. package/dest/public/avm/opcodes/contract.js +66 -0
  154. package/dest/public/avm/opcodes/control_flow.d.ts.map +1 -0
  155. package/dest/public/avm/opcodes/control_flow.js +105 -0
  156. package/dest/{avm → public/avm}/opcodes/conversion.d.ts +1 -1
  157. package/dest/public/avm/opcodes/conversion.d.ts.map +1 -0
  158. package/dest/public/avm/opcodes/conversion.js +69 -0
  159. package/dest/{avm → public/avm}/opcodes/ec_add.d.ts +1 -1
  160. package/dest/public/avm/opcodes/ec_add.d.ts.map +1 -0
  161. package/dest/public/avm/opcodes/ec_add.js +84 -0
  162. package/dest/public/avm/opcodes/environment_getters.d.ts.map +1 -0
  163. package/dest/public/avm/opcodes/environment_getters.js +79 -0
  164. package/dest/public/avm/opcodes/external_calls.d.ts.map +1 -0
  165. package/dest/public/avm/opcodes/external_calls.js +180 -0
  166. package/dest/{avm → public/avm}/opcodes/hashing.d.ts +1 -1
  167. package/dest/public/avm/opcodes/hashing.d.ts.map +1 -0
  168. package/dest/public/avm/opcodes/hashing.js +114 -0
  169. package/dest/public/avm/opcodes/index.d.ts.map +1 -0
  170. package/dest/{avm → public/avm}/opcodes/instruction.d.ts +2 -2
  171. package/dest/public/avm/opcodes/instruction.d.ts.map +1 -0
  172. package/dest/public/avm/opcodes/instruction.js +91 -0
  173. package/dest/public/avm/opcodes/instruction_impl.d.ts.map +1 -0
  174. package/dest/public/avm/opcodes/instruction_impl.js +30 -0
  175. package/dest/public/avm/opcodes/memory.d.ts.map +1 -0
  176. package/dest/public/avm/opcodes/memory.js +253 -0
  177. package/dest/{avm → public/avm}/opcodes/misc.d.ts +1 -1
  178. package/dest/public/avm/opcodes/misc.d.ts.map +1 -0
  179. package/dest/public/avm/opcodes/misc.js +48 -0
  180. package/dest/public/avm/opcodes/storage.d.ts.map +1 -0
  181. package/dest/public/avm/opcodes/storage.js +66 -0
  182. package/dest/public/avm/serialization/buffer_cursor.d.ts.map +1 -0
  183. package/dest/public/avm/serialization/buffer_cursor.js +99 -0
  184. package/dest/{avm → public/avm}/serialization/bytecode_serialization.d.ts +1 -1
  185. package/dest/public/avm/serialization/bytecode_serialization.d.ts.map +1 -0
  186. package/dest/public/avm/serialization/bytecode_serialization.js +328 -0
  187. package/dest/{avm → public/avm}/serialization/instruction_serialization.d.ts +1 -2
  188. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -0
  189. package/dest/public/avm/serialization/instruction_serialization.js +225 -0
  190. package/dest/{avm → public/avm}/test_utils.d.ts +6 -4
  191. package/dest/public/avm/test_utils.d.ts.map +1 -0
  192. package/dest/public/avm/test_utils.js +52 -0
  193. package/dest/public/bytecode_errors.js +1 -2
  194. package/dest/public/execution.d.ts +8 -2
  195. package/dest/public/execution.d.ts.map +1 -1
  196. package/dest/public/execution.js +4 -5
  197. package/dest/public/executor_metrics.js +16 -12
  198. package/dest/public/fixtures/index.js +0 -1
  199. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +12 -8
  200. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  201. package/dest/public/fixtures/public_tx_simulation_tester.js +37 -44
  202. package/dest/public/fixtures/utils.d.ts +3 -11
  203. package/dest/public/fixtures/utils.d.ts.map +1 -1
  204. package/dest/public/fixtures/utils.js +15 -35
  205. package/dest/public/index.d.ts +3 -2
  206. package/dest/public/index.d.ts.map +1 -1
  207. package/dest/public/index.js +3 -3
  208. package/dest/public/public_db_sources.d.ts +22 -28
  209. package/dest/public/public_db_sources.d.ts.map +1 -1
  210. package/dest/public/public_db_sources.js +75 -181
  211. package/dest/public/public_processor.d.ts +8 -6
  212. package/dest/public/public_processor.d.ts.map +1 -1
  213. package/dest/public/public_processor.js +329 -308
  214. package/dest/public/public_processor_metrics.d.ts +3 -3
  215. package/dest/public/public_processor_metrics.d.ts.map +1 -1
  216. package/dest/public/public_processor_metrics.js +46 -27
  217. package/dest/public/public_tx_context.d.ts +14 -8
  218. package/dest/public/public_tx_context.d.ts.map +1 -1
  219. package/dest/public/public_tx_context.js +122 -143
  220. package/dest/public/public_tx_simulator.d.ts +8 -5
  221. package/dest/public/public_tx_simulator.d.ts.map +1 -1
  222. package/dest/public/public_tx_simulator.js +306 -317
  223. package/dest/public/side_effect_errors.js +1 -2
  224. package/dest/public/side_effect_trace.d.ts +16 -10
  225. package/dest/public/side_effect_trace.d.ts.map +1 -1
  226. package/dest/public/side_effect_trace.js +65 -132
  227. package/dest/public/side_effect_trace_interface.d.ts +9 -7
  228. package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
  229. package/dest/public/side_effect_trace_interface.js +1 -2
  230. package/dest/public/unique_class_ids.js +23 -28
  231. package/dest/public/utils.d.ts +2 -2
  232. package/dest/public/utils.d.ts.map +1 -1
  233. package/dest/public/utils.js +17 -12
  234. package/dest/server.d.ts +4 -4
  235. package/dest/server.d.ts.map +1 -1
  236. package/dest/server.js +3 -5
  237. package/dest/test/utils.d.ts +3 -2
  238. package/dest/test/utils.d.ts.map +1 -1
  239. package/dest/test/utils.js +9 -7
  240. package/package.json +16 -18
  241. package/src/client.ts +4 -0
  242. package/src/{public → common}/db_interfaces.ts +8 -40
  243. package/src/common/debug_fn_name.ts +4 -2
  244. package/src/common/errors.ts +5 -5
  245. package/src/common/index.ts +2 -1
  246. package/src/common/message_load_oracle_inputs.ts +2 -2
  247. package/src/common/stats/index.ts +1 -0
  248. package/src/{stats → common/stats}/stats.ts +2 -2
  249. package/src/{acvm → private/acvm}/acvm.ts +5 -5
  250. package/src/{acvm → private/acvm}/acvm_types.ts +1 -1
  251. package/src/{acvm → private/acvm}/deserialize.ts +2 -2
  252. package/src/{acvm → private/acvm}/oracle/index.ts +1 -1
  253. package/src/{acvm → private/acvm}/oracle/oracle.ts +22 -12
  254. package/src/{acvm → private/acvm}/oracle/typed_oracle.ts +50 -55
  255. package/src/{acvm → private/acvm}/serialize.ts +10 -3
  256. package/src/{client → private}/client_execution_context.ts +54 -37
  257. package/src/{client → private}/db_oracle.ts +20 -20
  258. package/src/{client → private}/execution_note_cache.ts +3 -3
  259. package/src/{common → private}/hashed_values_cache.ts +2 -2
  260. package/src/private/index.ts +12 -0
  261. package/src/{client → private}/pick_notes.ts +1 -1
  262. package/src/{client → private}/private_execution.ts +46 -14
  263. package/src/{providers → private/providers}/acvm_native.ts +6 -6
  264. package/src/{providers → private/providers}/acvm_wasm.ts +4 -4
  265. package/src/{providers → private/providers}/acvm_wasm_with_blobs.ts +5 -5
  266. package/src/{providers → private/providers}/factory.ts +1 -1
  267. package/src/{common → private/providers}/simulation_provider.ts +5 -5
  268. package/src/{client → private}/simulator.ts +31 -123
  269. package/src/{client → private}/unconstrained_execution.ts +7 -7
  270. package/src/{client → private}/view_data_oracle.ts +25 -28
  271. package/src/{avm → public/avm}/avm_context.ts +12 -7
  272. package/src/{avm → public/avm}/avm_contract_call_result.ts +6 -6
  273. package/src/{avm → public/avm}/avm_execution_environment.ts +2 -2
  274. package/src/{avm → public/avm}/avm_gas.ts +1 -3
  275. package/src/{avm → public/avm}/avm_machine_state.ts +2 -2
  276. package/src/{avm → public/avm}/avm_memory_types.ts +4 -4
  277. package/src/{avm → public/avm}/avm_simulator.ts +12 -6
  278. package/src/{avm → public/avm}/errors.ts +5 -4
  279. package/src/{avm → public/avm}/fixtures/avm_simulation_tester.ts +16 -15
  280. package/src/{avm → public/avm}/fixtures/base_avm_simulation_tester.ts +38 -40
  281. package/src/{avm → public/avm}/fixtures/index.ts +17 -12
  282. package/src/{avm → public/avm}/fixtures/simple_contract_data_source.ts +64 -9
  283. package/src/{avm → public/avm}/index.ts +1 -1
  284. package/src/public/avm/journal/index.ts +1 -0
  285. package/src/{avm → public/avm}/journal/journal.ts +332 -305
  286. package/src/{avm → public/avm}/journal/nullifiers.ts +20 -19
  287. package/src/{avm → public/avm}/journal/public_storage.ts +2 -2
  288. package/src/{avm → public/avm}/opcodes/contract.ts +1 -1
  289. package/src/{avm → public/avm}/opcodes/control_flow.ts +1 -1
  290. package/src/{avm → public/avm}/opcodes/conversion.ts +1 -1
  291. package/src/{avm → public/avm}/opcodes/ec_add.ts +2 -2
  292. package/src/{avm → public/avm}/opcodes/external_calls.ts +5 -6
  293. package/src/{avm → public/avm}/opcodes/hashing.ts +1 -1
  294. package/src/public/avm/opcodes/index.ts +15 -0
  295. package/src/{avm → public/avm}/opcodes/instruction.ts +2 -2
  296. package/src/{avm → public/avm}/opcodes/misc.ts +1 -1
  297. package/src/public/avm/serialization/bytecode_serialization.ts +204 -0
  298. package/src/{avm → public/avm}/serialization/instruction_serialization.ts +0 -1
  299. package/src/{avm → public/avm}/test_utils.ts +10 -7
  300. package/src/public/execution.ts +9 -12
  301. package/src/public/fixtures/public_tx_simulation_tester.ts +45 -63
  302. package/src/public/fixtures/utils.ts +8 -43
  303. package/src/public/index.ts +3 -2
  304. package/src/public/public_db_sources.ts +55 -188
  305. package/src/public/public_processor.ts +66 -47
  306. package/src/public/public_processor_metrics.ts +3 -3
  307. package/src/public/public_tx_context.ts +69 -74
  308. package/src/public/public_tx_simulator.ts +35 -27
  309. package/src/public/side_effect_trace.ts +67 -140
  310. package/src/public/side_effect_trace_interface.ts +10 -23
  311. package/src/public/unique_class_ids.ts +1 -1
  312. package/src/public/utils.ts +2 -2
  313. package/src/server.ts +4 -4
  314. package/src/test/utils.ts +5 -3
  315. package/dest/acvm/acvm.d.ts.map +0 -1
  316. package/dest/acvm/acvm.js +0 -73
  317. package/dest/acvm/acvm_types.d.ts.map +0 -1
  318. package/dest/acvm/acvm_types.js +0 -2
  319. package/dest/acvm/deserialize.d.ts.map +0 -1
  320. package/dest/acvm/deserialize.js +0 -46
  321. package/dest/acvm/index.d.ts.map +0 -1
  322. package/dest/acvm/index.js +0 -6
  323. package/dest/acvm/oracle/index.d.ts.map +0 -1
  324. package/dest/acvm/oracle/index.js +0 -3
  325. package/dest/acvm/oracle/oracle.d.ts.map +0 -1
  326. package/dest/acvm/oracle/oracle.js +0 -230
  327. package/dest/acvm/oracle/typed_oracle.d.ts.map +0 -1
  328. package/dest/acvm/oracle/typed_oracle.js +0 -131
  329. package/dest/acvm/serialize.d.ts.map +0 -1
  330. package/dest/acvm/serialize.js +0 -48
  331. package/dest/avm/avm_context.d.ts.map +0 -1
  332. package/dest/avm/avm_context.js +0 -44
  333. package/dest/avm/avm_contract_call_result.d.ts.map +0 -1
  334. package/dest/avm/avm_contract_call_result.js +0 -45
  335. package/dest/avm/avm_execution_environment.d.ts.map +0 -1
  336. package/dest/avm/avm_execution_environment.js +0 -28
  337. package/dest/avm/avm_gas.d.ts.map +0 -1
  338. package/dest/avm/avm_gas.js +0 -170
  339. package/dest/avm/avm_machine_state.d.ts.map +0 -1
  340. package/dest/avm/avm_machine_state.js +0 -112
  341. package/dest/avm/avm_memory_types.d.ts.map +0 -1
  342. package/dest/avm/avm_memory_types.js +0 -327
  343. package/dest/avm/avm_simulator.d.ts.map +0 -1
  344. package/dest/avm/avm_simulator.js +0 -193
  345. package/dest/avm/avm_tree.d.ts +0 -282
  346. package/dest/avm/avm_tree.d.ts.map +0 -1
  347. package/dest/avm/avm_tree.js +0 -684
  348. package/dest/avm/bytecode_utils.d.ts.map +0 -1
  349. package/dest/avm/bytecode_utils.js +0 -15
  350. package/dest/avm/errors.d.ts.map +0 -1
  351. package/dest/avm/errors.js +0 -196
  352. package/dest/avm/fixtures/avm_simulation_tester.d.ts.map +0 -1
  353. package/dest/avm/fixtures/avm_simulation_tester.js +0 -73
  354. package/dest/avm/fixtures/base_avm_simulation_tester.d.ts.map +0 -1
  355. package/dest/avm/fixtures/base_avm_simulation_tester.js +0 -76
  356. package/dest/avm/fixtures/index.d.ts.map +0 -1
  357. package/dest/avm/fixtures/index.js +0 -134
  358. package/dest/avm/fixtures/simple_contract_data_source.d.ts.map +0 -1
  359. package/dest/avm/fixtures/simple_contract_data_source.js +0 -75
  360. package/dest/avm/index.d.ts.map +0 -1
  361. package/dest/avm/index.js +0 -4
  362. package/dest/avm/journal/index.d.ts.map +0 -1
  363. package/dest/avm/journal/index.js +0 -2
  364. package/dest/avm/journal/journal.d.ts.map +0 -1
  365. package/dest/avm/journal/journal.js +0 -499
  366. package/dest/avm/journal/nullifiers.d.ts.map +0 -1
  367. package/dest/avm/journal/nullifiers.js +0 -99
  368. package/dest/avm/journal/public_storage.d.ts.map +0 -1
  369. package/dest/avm/journal/public_storage.js +0 -159
  370. package/dest/avm/opcodes/accrued_substate.d.ts.map +0 -1
  371. package/dest/avm/opcodes/accrued_substate.js +0 -215
  372. package/dest/avm/opcodes/addressing_mode.d.ts.map +0 -1
  373. package/dest/avm/opcodes/addressing_mode.js +0 -81
  374. package/dest/avm/opcodes/arithmetic.d.ts.map +0 -1
  375. package/dest/avm/opcodes/arithmetic.js +0 -70
  376. package/dest/avm/opcodes/bitwise.d.ts.map +0 -1
  377. package/dest/avm/opcodes/bitwise.js +0 -90
  378. package/dest/avm/opcodes/comparators.d.ts.map +0 -1
  379. package/dest/avm/opcodes/comparators.js +0 -40
  380. package/dest/avm/opcodes/contract.d.ts.map +0 -1
  381. package/dest/avm/opcodes/contract.js +0 -63
  382. package/dest/avm/opcodes/control_flow.d.ts.map +0 -1
  383. package/dest/avm/opcodes/control_flow.js +0 -97
  384. package/dest/avm/opcodes/conversion.d.ts.map +0 -1
  385. package/dest/avm/opcodes/conversion.js +0 -64
  386. package/dest/avm/opcodes/ec_add.d.ts.map +0 -1
  387. package/dest/avm/opcodes/ec_add.js +0 -82
  388. package/dest/avm/opcodes/environment_getters.d.ts.map +0 -1
  389. package/dest/avm/opcodes/environment_getters.js +0 -78
  390. package/dest/avm/opcodes/external_calls.d.ts.map +0 -1
  391. package/dest/avm/opcodes/external_calls.js +0 -169
  392. package/dest/avm/opcodes/hashing.d.ts.map +0 -1
  393. package/dest/avm/opcodes/hashing.js +0 -103
  394. package/dest/avm/opcodes/index.d.ts.map +0 -1
  395. package/dest/avm/opcodes/index.js +0 -16
  396. package/dest/avm/opcodes/instruction.d.ts.map +0 -1
  397. package/dest/avm/opcodes/instruction.js +0 -100
  398. package/dest/avm/opcodes/instruction_impl.d.ts.map +0 -1
  399. package/dest/avm/opcodes/instruction_impl.js +0 -33
  400. package/dest/avm/opcodes/memory.d.ts.map +0 -1
  401. package/dest/avm/opcodes/memory.js +0 -226
  402. package/dest/avm/opcodes/misc.d.ts.map +0 -1
  403. package/dest/avm/opcodes/misc.js +0 -45
  404. package/dest/avm/opcodes/multi_scalar_mul.d.ts +0 -16
  405. package/dest/avm/opcodes/multi_scalar_mul.d.ts.map +0 -1
  406. package/dest/avm/opcodes/multi_scalar_mul.js +0 -112
  407. package/dest/avm/opcodes/storage.d.ts.map +0 -1
  408. package/dest/avm/opcodes/storage.js +0 -61
  409. package/dest/avm/serialization/buffer_cursor.d.ts.map +0 -1
  410. package/dest/avm/serialization/buffer_cursor.js +0 -99
  411. package/dest/avm/serialization/bytecode_serialization.d.ts.map +0 -1
  412. package/dest/avm/serialization/bytecode_serialization.js +0 -132
  413. package/dest/avm/serialization/instruction_serialization.d.ts.map +0 -1
  414. package/dest/avm/serialization/instruction_serialization.js +0 -187
  415. package/dest/avm/test_utils.d.ts.map +0 -1
  416. package/dest/avm/test_utils.js +0 -52
  417. package/dest/client/client_execution_context.d.ts.map +0 -1
  418. package/dest/client/client_execution_context.js +0 -392
  419. package/dest/client/db_oracle.d.ts.map +0 -1
  420. package/dest/client/db_oracle.js +0 -17
  421. package/dest/client/execution_note_cache.d.ts.map +0 -1
  422. package/dest/client/execution_note_cache.js +0 -181
  423. package/dest/client/index.d.ts +0 -16
  424. package/dest/client/index.d.ts.map +0 -1
  425. package/dest/client/index.js +0 -15
  426. package/dest/client/pick_notes.d.ts.map +0 -1
  427. package/dest/client/pick_notes.js +0 -54
  428. package/dest/client/private_execution.d.ts +0 -19
  429. package/dest/client/private_execution.d.ts.map +0 -1
  430. package/dest/client/private_execution.js +0 -72
  431. package/dest/client/simulator.d.ts +0 -60
  432. package/dest/client/simulator.d.ts.map +0 -1
  433. package/dest/client/simulator.js +0 -136
  434. package/dest/client/unconstrained_execution.d.ts.map +0 -1
  435. package/dest/client/unconstrained_execution.js +0 -30
  436. package/dest/client/view_data_oracle.d.ts.map +0 -1
  437. package/dest/client/view_data_oracle.js +0 -268
  438. package/dest/common/hashed_values_cache.d.ts.map +0 -1
  439. package/dest/common/hashed_values_cache.js +0 -50
  440. package/dest/common/simulation_provider.d.ts.map +0 -1
  441. package/dest/common/simulation_provider.js +0 -27
  442. package/dest/common.d.ts +0 -2
  443. package/dest/common.d.ts.map +0 -1
  444. package/dest/common.js +0 -2
  445. package/dest/providers/acvm_native.d.ts.map +0 -1
  446. package/dest/providers/acvm_native.js +0 -125
  447. package/dest/providers/acvm_wasm.d.ts.map +0 -1
  448. package/dest/providers/acvm_wasm.js +0 -54
  449. package/dest/providers/acvm_wasm_with_blobs.d.ts.map +0 -1
  450. package/dest/providers/factory.d.ts.map +0 -1
  451. package/dest/providers/factory.js +0 -27
  452. package/dest/providers/index.d.ts +0 -5
  453. package/dest/providers/index.d.ts.map +0 -1
  454. package/dest/providers/index.js +0 -5
  455. package/dest/public/db_interfaces.d.ts.map +0 -1
  456. package/dest/public/db_interfaces.js +0 -2
  457. package/dest/public/fee_payment.d.ts +0 -11
  458. package/dest/public/fee_payment.d.ts.map +0 -1
  459. package/dest/public/fee_payment.js +0 -21
  460. package/dest/stats/index.d.ts.map +0 -1
  461. package/dest/stats/index.js +0 -2
  462. package/dest/stats/stats.d.ts.map +0 -1
  463. package/dest/stats/stats.js +0 -11
  464. package/src/avm/avm_tree.ts +0 -887
  465. package/src/avm/opcodes/multi_scalar_mul.ts +0 -121
  466. package/src/avm/serialization/bytecode_serialization.ts +0 -212
  467. package/src/client/index.ts +0 -15
  468. package/src/common.ts +0 -1
  469. package/src/providers/index.ts +0 -4
  470. package/src/public/fee_payment.ts +0 -23
  471. /package/dest/{stats → common/stats}/index.d.ts +0 -0
  472. /package/{src/stats/index.ts → dest/common/stats/index.js} +0 -0
  473. /package/dest/{acvm → private/acvm}/index.d.ts +0 -0
  474. /package/dest/{avm → public/avm}/avm_gas.d.ts +0 -0
  475. /package/dest/{avm → public/avm}/bytecode_utils.d.ts +0 -0
  476. /package/dest/{avm → public/avm}/journal/index.d.ts +0 -0
  477. /package/{src/avm/journal/index.ts → dest/public/avm/journal/index.js} +0 -0
  478. /package/dest/{avm → public/avm}/opcodes/accrued_substate.d.ts +0 -0
  479. /package/dest/{avm → public/avm}/opcodes/addressing_mode.d.ts +0 -0
  480. /package/dest/{avm → public/avm}/opcodes/arithmetic.d.ts +0 -0
  481. /package/dest/{avm → public/avm}/opcodes/bitwise.d.ts +0 -0
  482. /package/dest/{avm → public/avm}/opcodes/comparators.d.ts +0 -0
  483. /package/dest/{avm → public/avm}/opcodes/contract.d.ts +0 -0
  484. /package/dest/{avm → public/avm}/opcodes/control_flow.d.ts +0 -0
  485. /package/dest/{avm → public/avm}/opcodes/environment_getters.d.ts +0 -0
  486. /package/dest/{avm → public/avm}/opcodes/external_calls.d.ts +0 -0
  487. /package/dest/{avm → public/avm}/opcodes/index.d.ts +0 -0
  488. /package/{src/avm/opcodes/index.ts → dest/public/avm/opcodes/index.js} +0 -0
  489. /package/dest/{avm → public/avm}/opcodes/instruction_impl.d.ts +0 -0
  490. /package/dest/{avm → public/avm}/opcodes/memory.d.ts +0 -0
  491. /package/dest/{avm → public/avm}/opcodes/storage.d.ts +0 -0
  492. /package/dest/{avm → public/avm}/serialization/buffer_cursor.d.ts +0 -0
  493. /package/src/{acvm → private/acvm}/index.ts +0 -0
  494. /package/src/{avm → public/avm}/bytecode_utils.ts +0 -0
  495. /package/src/{avm → public/avm}/opcodes/.eslintrc.cjs +0 -0
  496. /package/src/{avm → public/avm}/opcodes/accrued_substate.ts +0 -0
  497. /package/src/{avm → public/avm}/opcodes/addressing_mode.ts +0 -0
  498. /package/src/{avm → public/avm}/opcodes/arithmetic.ts +0 -0
  499. /package/src/{avm → public/avm}/opcodes/bitwise.ts +0 -0
  500. /package/src/{avm → public/avm}/opcodes/comparators.ts +0 -0
  501. /package/src/{avm → public/avm}/opcodes/environment_getters.ts +0 -0
  502. /package/src/{avm → public/avm}/opcodes/instruction_impl.ts +0 -0
  503. /package/src/{avm → public/avm}/opcodes/memory.ts +0 -0
  504. /package/src/{avm → public/avm}/opcodes/storage.ts +0 -0
  505. /package/src/{avm → public/avm}/serialization/buffer_cursor.ts +0 -0
@@ -1,38 +1,60 @@
1
- import { MerkleTreeId } from '@aztec/circuit-types';
2
1
  import {
3
- AztecAddress,
4
2
  CANONICAL_AUTH_REGISTRY_ADDRESS,
5
3
  DEPLOYER_CONTRACT_ADDRESS,
6
4
  FEE_JUICE_ADDRESS,
7
5
  MULTI_CALL_ENTRYPOINT_ADDRESS,
8
- NullifierLeafPreimage,
9
- type PublicCallRequest,
10
- type PublicDataTreeLeafPreimage,
11
6
  REGISTERER_CONTRACT_ADDRESS,
12
7
  ROUTER_ADDRESS,
13
- SerializableContractInstance,
14
- } from '@aztec/circuits.js';
8
+ } from '@aztec/constants';
9
+ import { poseidon2Hash } from '@aztec/foundation/crypto';
10
+ import { Fr } from '@aztec/foundation/fields';
11
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
12
+ import { createLogger } from '@aztec/foundation/log';
13
+ import type { IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
14
+ import { ProtocolContractAddress } from '@aztec/protocol-contracts';
15
+ import { AvmPublicDataReadTreeHint, PublicDataWrite } from '@aztec/stdlib/avm';
16
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
17
+ import { SerializableContractInstance } from '@aztec/stdlib/contract';
18
+ import type { ContractClassWithCommitment } from '@aztec/stdlib/contract';
15
19
  import {
16
20
  computeNoteHashNonce,
17
21
  computePublicDataTreeLeafSlot,
18
22
  computeUniqueNoteHash,
19
23
  siloNoteHash,
20
24
  siloNullifier,
21
- } from '@aztec/circuits.js/hash';
22
- import { Fr } from '@aztec/foundation/fields';
23
- import { jsonStringify } from '@aztec/foundation/json-rpc';
24
- import { createLogger } from '@aztec/foundation/log';
25
+ } from '@aztec/stdlib/hash';
26
+ import type { IndexedTreeId, MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
27
+ import type { PublicCallRequest } from '@aztec/stdlib/kernel';
28
+ import { SharedMutableValues, SharedMutableValuesWithHash } from '@aztec/stdlib/shared-mutable';
29
+ import { MerkleTreeId, NullifierLeafPreimage, PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
25
30
 
26
31
  import { strict as assert } from 'assert';
32
+ import cloneDeep from 'lodash.clonedeep';
27
33
 
28
- import { getPublicFunctionDebugName } from '../../common/debug_fn_name.js';
29
- import { type WorldStateDB } from '../../public/public_db_sources.js';
30
- import { type PublicSideEffectTraceInterface } from '../../public/side_effect_trace_interface.js';
31
- import { type AvmExecutionEnvironment } from '../avm_execution_environment.js';
32
- import { AvmEphemeralForest } from '../avm_tree.js';
34
+ import { getPublicFunctionDebugName } from '../../../common/debug_fn_name.js';
35
+ import type { WorldStateDB } from '../../../public/public_db_sources.js';
36
+ import type { PublicSideEffectTraceInterface } from '../../side_effect_trace_interface.js';
37
+ import type { AvmExecutionEnvironment } from '../avm_execution_environment.js';
33
38
  import { NullifierCollisionError, NullifierManager } from './nullifiers.js';
34
39
  import { PublicStorage } from './public_storage.js';
35
40
 
41
+ /**
42
+ * The result of fetching a leaf from an indexed tree. Contains the preimage and wether the leaf was already present
43
+ * or it's a low leaf.
44
+ */
45
+ type GetLeafResult<T extends IndexedTreeLeafPreimage> = {
46
+ preimage: T;
47
+ leafOrLowLeafIndex: bigint;
48
+ alreadyPresent: boolean;
49
+ };
50
+
51
+ type NullifierMembershipResult = {
52
+ exists: boolean;
53
+ leafOrLowLeafPreimage: NullifierLeafPreimage;
54
+ leafOrLowLeafIndex: bigint;
55
+ leafOrLowLeafPath: Fr[];
56
+ };
57
+
36
58
  /**
37
59
  * A class to manage persistable AVM state for contract calls.
38
60
  * Maintains a cache of the current world state,
@@ -59,28 +81,30 @@ export class AvmPersistableStateManager {
59
81
  /** Nullifier set, including cached/recently-emitted nullifiers */
60
82
  private readonly nullifiers: NullifierManager = new NullifierManager(worldStateDB),
61
83
  private readonly doMerkleOperations: boolean = false,
62
- /** Ephmeral forest for merkle tree operations */
63
- public merkleTrees: AvmEphemeralForest,
84
+ /** DB interface for merkle tree operations */
85
+ public db: MerkleTreeWriteOperations,
64
86
  public readonly firstNullifier: Fr,
65
87
  ) {}
66
88
 
67
89
  /**
68
90
  * Create a new state manager
69
91
  */
70
- public static async create(
92
+ public static create(
71
93
  worldStateDB: WorldStateDB,
72
94
  trace: PublicSideEffectTraceInterface,
73
95
  doMerkleOperations: boolean = false,
74
96
  firstNullifier: Fr,
75
- ): Promise<AvmPersistableStateManager> {
76
- const ephemeralForest = await AvmEphemeralForest.create(worldStateDB.getMerkleInterface());
97
+ ): AvmPersistableStateManager {
98
+ // TODO(dbanks12): temporary until we establish a better world state interface
99
+ const db = worldStateDB.getMerkleInterface();
100
+
77
101
  return new AvmPersistableStateManager(
78
102
  worldStateDB,
79
103
  trace,
80
104
  /*publicStorage=*/ new PublicStorage(worldStateDB),
81
105
  /*nullifiers=*/ new NullifierManager(worldStateDB),
82
106
  /*doMerkleOperations=*/ doMerkleOperations,
83
- ephemeralForest,
107
+ db,
84
108
  firstNullifier,
85
109
  );
86
110
  }
@@ -88,14 +112,15 @@ export class AvmPersistableStateManager {
88
112
  /**
89
113
  * Create a new state manager forked from this one
90
114
  */
91
- public fork() {
115
+ public async fork() {
116
+ await this.worldStateDB.createCheckpoint();
92
117
  return new AvmPersistableStateManager(
93
118
  this.worldStateDB,
94
119
  this.trace.fork(),
95
120
  this.publicStorage.fork(),
96
121
  this.nullifiers.fork(),
97
122
  this.doMerkleOperations,
98
- this.merkleTrees.fork(),
123
+ this.db,
99
124
  this.firstNullifier,
100
125
  );
101
126
  }
@@ -103,18 +128,18 @@ export class AvmPersistableStateManager {
103
128
  /**
104
129
  * Accept forked world state modifications & traced side effects / hints
105
130
  */
106
- public merge(forkedState: AvmPersistableStateManager) {
107
- this._merge(forkedState, /*reverted=*/ false);
131
+ public async merge(forkedState: AvmPersistableStateManager) {
132
+ await this._merge(forkedState, /*reverted=*/ false);
108
133
  }
109
134
 
110
135
  /**
111
136
  * Reject forked world state modifications & traced side effects, keep traced hints
112
137
  */
113
- public reject(forkedState: AvmPersistableStateManager) {
114
- this._merge(forkedState, /*reverted=*/ true);
138
+ public async reject(forkedState: AvmPersistableStateManager) {
139
+ await this._merge(forkedState, /*reverted=*/ true);
115
140
  }
116
141
 
117
- private _merge(forkedState: AvmPersistableStateManager, reverted: boolean) {
142
+ private async _merge(forkedState: AvmPersistableStateManager, reverted: boolean) {
118
143
  // sanity check to avoid merging the same forked trace twice
119
144
  assert(
120
145
  !forkedState.alreadyMergedIntoParent,
@@ -125,14 +150,15 @@ export class AvmPersistableStateManager {
125
150
  this.nullifiers.acceptAndMerge(forkedState.nullifiers);
126
151
  this.trace.merge(forkedState.trace, reverted);
127
152
  if (reverted) {
153
+ await this.worldStateDB.revertCheckpoint();
128
154
  if (this.doMerkleOperations) {
129
155
  this.log.trace(
130
- `Rolled back nullifier tree to root ${this.merkleTrees.treeMap.get(MerkleTreeId.NULLIFIER_TREE)!.getRoot()}`,
156
+ `Rolled back nullifier tree to root ${new Fr((await this.db.getTreeInfo(MerkleTreeId.NULLIFIER_TREE)).root)}`,
131
157
  );
132
158
  }
133
159
  } else {
134
160
  this.log.trace('Merging forked state into parent...');
135
- this.merkleTrees = forkedState.merkleTrees;
161
+ await this.worldStateDB.commitCheckpoint();
136
162
  }
137
163
  }
138
164
 
@@ -144,30 +170,42 @@ export class AvmPersistableStateManager {
144
170
  * @param value - the value being written to the slot
145
171
  */
146
172
  public async writeStorage(contractAddress: AztecAddress, slot: Fr, value: Fr, protocolWrite = false): Promise<void> {
147
- this.log.trace(`Storage write (address=${contractAddress}, slot=${slot}): value=${value}`);
148
173
  const leafSlot = await computePublicDataTreeLeafSlot(contractAddress, slot);
149
- this.log.trace(`leafSlot=${leafSlot}`);
150
- // Cache storage writes for later reference/reads
151
- this.publicStorage.write(contractAddress, slot, value);
174
+ this.log.trace(`Storage write (address=${contractAddress}, slot=${slot}): value=${value}, leafSlot=${leafSlot}`);
152
175
 
153
176
  if (this.doMerkleOperations) {
154
- const result = await this.merkleTrees.writePublicStorage(leafSlot, value);
177
+ // write to native merkle trees
178
+ const publicDataWrite = new PublicDataWrite(leafSlot, value);
179
+ const result = await this.db.sequentialInsert(MerkleTreeId.PUBLIC_DATA_TREE, [publicDataWrite.toBuffer()]);
155
180
  assert(result !== undefined, 'Public data tree insertion error. You might want to disable doMerkleOperations.');
156
181
  this.log.trace(`Inserted public data tree leaf at leafSlot ${leafSlot}, value: ${value}`);
157
182
 
158
- const lowLeafInfo = result.lowWitness;
159
- const lowLeafPreimage = result.lowWitness.preimage as PublicDataTreeLeafPreimage;
160
- const lowLeafIndex = lowLeafInfo.index;
161
- const lowLeafPath = lowLeafInfo.siblingPath;
162
-
163
- const newLeafPreimage = result.element as PublicDataTreeLeafPreimage;
183
+ // low leaf hint
184
+ const lowLeafPreimage = result.lowLeavesWitnessData[0].leafPreimage as PublicDataTreeLeafPreimage;
185
+ const lowLeafIndex = result.lowLeavesWitnessData[0].index;
186
+ const lowLeafPath = result.lowLeavesWitnessData[0].siblingPath.toFields();
187
+ // new leaf insertion
188
+ const newLeafPreimage: PublicDataTreeLeafPreimage = cloneDeep(lowLeafPreimage);
164
189
  let insertionPath: Fr[] | undefined;
165
- if (!result.update) {
166
- insertionPath = result.insertionPath;
190
+
191
+ if (result.insertionWitnessData.length === 0) {
167
192
  assert(
168
193
  newLeafPreimage.value.equals(value),
169
- `Value mismatch when performing public data write (got value: ${value}, value in ephemeral tree: ${newLeafPreimage.value})`,
194
+ `Value mismatch when performing public data write (got value: ${value}, value in tree: ${newLeafPreimage.value})`,
195
+ );
196
+ } else {
197
+ this.log.debug(`insertion witness data length: ${result.insertionWitnessData.length}`);
198
+ // The new leaf preimage should have the new value and slot
199
+ newLeafPreimage.slot = leafSlot;
200
+ newLeafPreimage.value = value;
201
+ // TODO: is this necessary?! Why doesn't sequentialInsert return the newLeafPreimage via
202
+ // result.insertionWitnessData[0].leafPreimage?
203
+
204
+ this.log.debug(
205
+ `newLeafPreimage.slot: ${newLeafPreimage.slot}, newLeafPreimage.value: ${newLeafPreimage.value}`,
170
206
  );
207
+ this.log.debug(`insertion index: ${result.insertionWitnessData[0].index}`);
208
+ insertionPath = result.insertionWitnessData[0].siblingPath.toFields();
171
209
  }
172
210
 
173
211
  await this.trace.tracePublicStorageWrite(
@@ -182,6 +220,8 @@ export class AvmPersistableStateManager {
182
220
  insertionPath,
183
221
  );
184
222
  } else {
223
+ // Cache storage writes for later reference/reads
224
+ this.publicStorage.write(contractAddress, slot, value);
185
225
  await this.trace.tracePublicStorageWrite(contractAddress, slot, value, protocolWrite);
186
226
  }
187
227
  }
@@ -194,51 +234,64 @@ export class AvmPersistableStateManager {
194
234
  * @returns the latest value written to slot, or 0 if never written to before
195
235
  */
196
236
  public async readStorage(contractAddress: AztecAddress, slot: Fr): Promise<Fr> {
197
- const { value, cached } = await this.publicStorage.read(contractAddress, slot);
198
- this.log.trace(`Storage read (address=${contractAddress}, slot=${slot}): value=${value}, cached=${cached}`);
199
- const leafSlot = await computePublicDataTreeLeafSlot(contractAddress, slot);
200
- this.log.trace(`leafSlot=${leafSlot}`);
201
-
202
237
  if (this.doMerkleOperations) {
203
- // Get leaf if present, low leaf if absent
204
- // If leaf is present, hint/trace it. Otherwise, hint/trace the low leaf.
205
- const {
206
- preimage,
207
- index: leafIndex,
208
- alreadyPresent,
209
- } = await this.merkleTrees.getLeafOrLowLeafInfo(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot);
210
- // The index and preimage here is either the low leaf or the leaf itself (depending on the value of update flag)
211
- // In either case, we just want the sibling path to this leaf - it's up to the avm to distinguish if it's a low leaf or not
212
- const leafPath = await this.merkleTrees.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex);
213
- const leafPreimage = preimage as PublicDataTreeLeafPreimage;
214
-
215
- this.log.trace(`leafPreimage.slot: ${leafPreimage.slot}, leafPreimage.value: ${leafPreimage.value}`);
238
+ const { value, leafPreimage, leafIndex, leafPath } = await this.getPublicDataMembership(contractAddress, slot);
239
+ this.trace.tracePublicStorageRead(contractAddress, slot, value, leafPreimage, leafIndex, leafPath);
240
+ return value;
241
+ } else {
242
+ const read = await this.publicStorage.read(contractAddress, slot);
216
243
  this.log.trace(
217
- `leafPreimage.nextSlot: ${leafPreimage.nextSlot}, leafPreimage.nextIndex: ${Number(leafPreimage.nextIndex)}`,
244
+ `Storage read results (address=${contractAddress}, slot=${slot}): value=${read.value}, cached=${read.cached}`,
218
245
  );
246
+ this.trace.tracePublicStorageRead(contractAddress, slot, read.value);
247
+ return read.value;
248
+ }
249
+ }
219
250
 
220
- if (alreadyPresent) {
221
- assert(
222
- leafPreimage.value.equals(value),
223
- `Value mismatch when performing public data read (got value: ${value}, value in ephemeral tree: ${leafPreimage.value})`,
224
- );
225
- } else {
226
- this.log.trace(`Slot has never been written before!`);
227
- // Sanity check that the leaf slot is skipped by low leaf when it doesn't exist
228
- assert(
229
- leafPreimage.slot.toBigInt() < leafSlot.toBigInt() &&
230
- (leafPreimage.nextIndex === 0n || leafPreimage.nextSlot.toBigInt() > leafSlot.toBigInt()),
231
- 'Public data tree low leaf should skip the target leaf slot when the target leaf does not exist or is the max value.',
232
- );
233
- }
234
- // On non-existence, AVM circuit will need to recognize that leafPreimage.slot != leafSlot,
235
- // prove that this is a low leaf that skips leafSlot, and then prove membership of the leaf.
236
- this.trace.tracePublicStorageRead(contractAddress, slot, value, leafPreimage, new Fr(leafIndex), leafPath);
237
- } else {
238
- this.trace.tracePublicStorageRead(contractAddress, slot, value);
251
+ async getPublicDataMembership(
252
+ contractAddress: AztecAddress,
253
+ slot: Fr,
254
+ ): Promise<{
255
+ value: Fr;
256
+ leafPreimage: PublicDataTreeLeafPreimage;
257
+ leafIndex: Fr;
258
+ leafPath: Fr[];
259
+ }> {
260
+ const leafSlot = await computePublicDataTreeLeafSlot(contractAddress, slot);
261
+ const treeId = MerkleTreeId.PUBLIC_DATA_TREE;
262
+
263
+ // Get leaf if present, low leaf if absent
264
+ // If leaf is present, hint/trace it. Otherwise, hint/trace the low leaf.
265
+ const { preimage, leafOrLowLeafIndex, alreadyPresent } = await this.getLeafOrLowLeafInfo<
266
+ typeof treeId,
267
+ PublicDataTreeLeafPreimage
268
+ >(treeId, leafSlot.toBigInt());
269
+ // The index and preimage here is either the low leaf or the leaf itself (depending on the value of update flag)
270
+ // In either case, we just want the sibling path to this leaf - it's up to the avm to distinguish if it's a low leaf or not
271
+ const leafPath = await this.db.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, leafOrLowLeafIndex);
272
+ const leafPreimage = preimage as PublicDataTreeLeafPreimage;
273
+
274
+ const value = alreadyPresent ? leafPreimage.value : Fr.zero(); // default value of 0
275
+ if (!alreadyPresent) {
276
+ this.log.trace(`Slot has never been written before!`);
277
+ // Sanity check that the leaf slot is skipped by low leaf when it doesn't exist
278
+ assert(
279
+ leafPreimage.slot.toBigInt() < leafSlot.toBigInt() &&
280
+ (leafPreimage.nextIndex === 0n || leafPreimage.nextSlot.toBigInt() > leafSlot.toBigInt()),
281
+ 'Public data tree low leaf should skip the target leaf slot when the target leaf does not exist or is the max value.',
282
+ );
239
283
  }
240
284
 
241
- return Promise.resolve(value);
285
+ this.log.trace(
286
+ `Storage read results (address=${contractAddress}, slot=${slot}, leafSlot=${leafSlot}): value=${value}, previouslyWritten=${alreadyPresent}`,
287
+ );
288
+
289
+ return {
290
+ value,
291
+ leafPreimage,
292
+ leafIndex: new Fr(leafOrLowLeafIndex),
293
+ leafPath: leafPath.toFields(),
294
+ };
242
295
  }
243
296
 
244
297
  /**
@@ -270,10 +323,8 @@ export class AvmPersistableStateManager {
270
323
  `noteHashes(${contractAddress})@${noteHash} ?? leafIndex: ${leafIndex} | gotLeafValue: ${gotLeafValue}, exists: ${exists}.`,
271
324
  );
272
325
  if (this.doMerkleOperations) {
273
- // TODO(8287): We still return exists here, but we need to transmit both the requested noteHash and the gotLeafValue
274
- // such that the VM can constrain the equality and decide on exists based on that.
275
- const path = await this.merkleTrees.getSiblingPath(MerkleTreeId.NOTE_HASH_TREE, leafIndex.toBigInt());
276
- this.trace.traceNoteHashCheck(contractAddress, gotLeafValue, leafIndex, exists, path);
326
+ const path = await this.db.getSiblingPath(MerkleTreeId.NOTE_HASH_TREE, leafIndex.toBigInt());
327
+ this.trace.traceNoteHashCheck(contractAddress, gotLeafValue, leafIndex, exists, path.toFields());
277
328
  } else {
278
329
  this.trace.traceNoteHashCheck(contractAddress, gotLeafValue, leafIndex, exists);
279
330
  }
@@ -285,6 +336,7 @@ export class AvmPersistableStateManager {
285
336
  * @param noteHash - the unsiloed note hash to write
286
337
  */
287
338
  public async writeNoteHash(contractAddress: AztecAddress, noteHash: Fr): Promise<void> {
339
+ this.log.trace(`noteHashes(${contractAddress}) += ${noteHash}.`);
288
340
  const siloedNoteHash = await siloNoteHash(contractAddress, noteHash);
289
341
 
290
342
  await this.writeSiloedNoteHash(siloedNoteHash);
@@ -292,29 +344,31 @@ export class AvmPersistableStateManager {
292
344
 
293
345
  /**
294
346
  * Write a note hash, make it unique, trace the write.
295
- * @param noteHash - the non unique note hash to write
347
+ * @param siloedNoteHash - the non unique note hash to write
296
348
  */
297
- public async writeSiloedNoteHash(noteHash: Fr): Promise<void> {
349
+ public async writeSiloedNoteHash(siloedNoteHash: Fr): Promise<void> {
298
350
  const nonce = await computeNoteHashNonce(this.firstNullifier, this.trace.getNoteHashCount());
299
- const uniqueNoteHash = await computeUniqueNoteHash(nonce, noteHash);
351
+ const uniqueNoteHash = await computeUniqueNoteHash(nonce, siloedNoteHash);
300
352
 
301
353
  await this.writeUniqueNoteHash(uniqueNoteHash);
302
354
  }
303
355
 
304
356
  /**
305
357
  * Write a note hash, trace the write.
306
- * @param noteHash - the siloed unique hash to write
358
+ * @param uniqueNoteHash - the siloed unique hash to write
307
359
  */
308
- public async writeUniqueNoteHash(noteHash: Fr): Promise<void> {
309
- this.log.trace(`noteHashes += @${noteHash}.`);
360
+ public async writeUniqueNoteHash(uniqueNoteHash: Fr): Promise<void> {
361
+ this.log.trace(`noteHashes += @${uniqueNoteHash}.`);
310
362
 
311
363
  if (this.doMerkleOperations) {
312
364
  // Should write a helper for this
313
- const leafIndex = new Fr(this.merkleTrees.treeMap.get(MerkleTreeId.NOTE_HASH_TREE)!.leafCount);
314
- const insertionPath = await this.merkleTrees.appendNoteHash(noteHash);
315
- this.trace.traceNewNoteHash(noteHash, leafIndex, insertionPath);
365
+ const treeInfo = await this.db.getTreeInfo(MerkleTreeId.NOTE_HASH_TREE);
366
+ const leafIndex = new Fr(treeInfo.size);
367
+ await this.db.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, [uniqueNoteHash]);
368
+ const insertionPath = await this.db.getSiblingPath(MerkleTreeId.NOTE_HASH_TREE, leafIndex.toBigInt());
369
+ this.trace.traceNewNoteHash(uniqueNoteHash, leafIndex, insertionPath.toFields());
316
370
  } else {
317
- this.trace.traceNewNoteHash(noteHash);
371
+ this.trace.traceNewNoteHash(uniqueNoteHash);
318
372
  }
319
373
  }
320
374
 
@@ -327,22 +381,24 @@ export class AvmPersistableStateManager {
327
381
  public async checkNullifierExists(contractAddress: AztecAddress, nullifier: Fr): Promise<boolean> {
328
382
  this.log.trace(`Checking existence of nullifier (address=${contractAddress}, nullifier=${nullifier})`);
329
383
  const siloedNullifier = await siloNullifier(contractAddress, nullifier);
330
- const [exists, leafOrLowLeafPreimage, leafOrLowLeafIndex, leafOrLowLeafPath] = await this.getNullifierMembership(
331
- siloedNullifier,
332
- );
333
384
 
334
385
  if (this.doMerkleOperations) {
386
+ const { exists, leafOrLowLeafPreimage, leafOrLowLeafIndex, leafOrLowLeafPath } =
387
+ await this.getNullifierMembership(siloedNullifier);
335
388
  this.trace.traceNullifierCheck(
336
389
  siloedNullifier,
337
390
  exists,
338
391
  leafOrLowLeafPreimage,
339
- leafOrLowLeafIndex,
392
+ new Fr(leafOrLowLeafIndex!),
340
393
  leafOrLowLeafPath,
341
394
  );
395
+ return Promise.resolve(exists);
342
396
  } else {
397
+ const { exists, cacheHit } = await this.nullifiers.checkExists(siloedNullifier);
398
+ this.log.trace(`Checked siloed nullifier ${siloedNullifier} (exists=${exists}), cacheHit=${cacheHit}`);
343
399
  this.trace.traceNullifierCheck(siloedNullifier, exists);
400
+ return Promise.resolve(exists);
344
401
  }
345
- return Promise.resolve(exists);
346
402
  }
347
403
 
348
404
  /**
@@ -356,49 +412,35 @@ export class AvmPersistableStateManager {
356
412
  * - leafOrLowLeafIndex - the leaf index of the nullifier leaf or its low-leaf if it doesn't exist
357
413
  * - leafOrLowLeafPath - the sibling path of the nullifier leaf or its low-leaf if it doesn't exist
358
414
  */
359
- private async getNullifierMembership(
360
- siloedNullifier: Fr,
361
- ): Promise<
362
- [
363
- /*exists=*/ boolean,
364
- /*leafOrLowLeafPreimage=*/ NullifierLeafPreimage,
365
- /*leafOrLowLeafIndex=*/ Fr,
366
- /*leafOrLowLeafIndexPath=*/ Fr[],
367
- ]
368
- > {
369
- const [exists, isPending, _] = await this.nullifiers.checkExists(siloedNullifier);
370
- this.log.trace(`Checked siloed nullifier ${siloedNullifier} (exists=${exists}), pending=${isPending}`);
371
-
372
- if (this.doMerkleOperations) {
373
- // Get leaf if present, low leaf if absent
374
- // If leaf is present, hint/trace it. Otherwise, hint/trace the low leaf.
375
- const {
376
- preimage,
377
- index: leafIndex,
378
- alreadyPresent,
379
- } = await this.merkleTrees.getLeafOrLowLeafInfo(MerkleTreeId.NULLIFIER_TREE, siloedNullifier);
380
- const leafPreimage = preimage as NullifierLeafPreimage;
381
- const leafPath = await this.merkleTrees.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, leafIndex);
382
-
415
+ private async getNullifierMembership(siloedNullifier: Fr): Promise<NullifierMembershipResult> {
416
+ // Get leaf if present, low leaf if absent
417
+ // If leaf is present, hint/trace it. Otherwise, hint/trace the low leaf.
418
+ const treeId = MerkleTreeId.NULLIFIER_TREE;
419
+ const {
420
+ preimage: leafPreimage,
421
+ leafOrLowLeafIndex,
422
+ alreadyPresent,
423
+ } = await this.getLeafOrLowLeafInfo<typeof treeId, NullifierLeafPreimage>(treeId, siloedNullifier.toBigInt());
424
+ this.log.trace(`Checked siloed nullifier ${siloedNullifier} (exists=${alreadyPresent})`);
425
+
426
+ const leafPath = await this.db.getSiblingPath(treeId, leafOrLowLeafIndex!);
427
+
428
+ if (alreadyPresent) {
429
+ this.log.trace(`Siloed nullifier ${siloedNullifier} exists at leafIndex=${leafOrLowLeafIndex}`);
430
+ } else {
431
+ // Sanity check that the leaf value is skipped by low leaf when it doesn't exist
383
432
  assert(
384
- alreadyPresent == exists,
385
- 'WorldStateDB contains nullifier leaf, but merkle tree does not (or vice versa).... This is a bug!',
433
+ leafPreimage.nullifier.toBigInt() < siloedNullifier.toBigInt() &&
434
+ (leafPreimage.nextIndex === 0n || leafPreimage.nextNullifier.toBigInt() > siloedNullifier.toBigInt()),
435
+ 'Nullifier tree low leaf should skip the target leaf nullifier when the target leaf does not exist.',
386
436
  );
387
-
388
- if (exists) {
389
- this.log.trace(`Siloed nullifier ${siloedNullifier} exists at leafIndex=${leafIndex}`);
390
- } else {
391
- // Sanity check that the leaf value is skipped by low leaf when it doesn't exist
392
- assert(
393
- leafPreimage.nullifier.toBigInt() < siloedNullifier.toBigInt() &&
394
- (leafPreimage.nextIndex === 0n || leafPreimage.nextNullifier.toBigInt() > siloedNullifier.toBigInt()),
395
- 'Nullifier tree low leaf should skip the target leaf nullifier when the target leaf does not exist.',
396
- );
397
- }
398
- return [exists, leafPreimage, new Fr(leafIndex), leafPath];
399
- } else {
400
- return [exists, NullifierLeafPreimage.empty(), Fr.ZERO, []];
401
437
  }
438
+ return {
439
+ exists: alreadyPresent,
440
+ leafOrLowLeafPreimage: leafPreimage,
441
+ leafOrLowLeafIndex,
442
+ leafOrLowLeafPath: leafPath.toFields(),
443
+ };
402
444
  }
403
445
 
404
446
  /**
@@ -420,48 +462,37 @@ export class AvmPersistableStateManager {
420
462
  this.log.trace(`Inserting siloed nullifier=${siloedNullifier}`);
421
463
 
422
464
  if (this.doMerkleOperations) {
423
- // Maybe overkill, but we should check if the nullifier is already present in the tree before attempting to insert
424
- // It might be better to catch the error from the insert operation
425
- // Trace all nullifier creations, even duplicate insertions that fail
426
- const { preimage, index, alreadyPresent } = await this.merkleTrees.getLeafOrLowLeafInfo(
427
- MerkleTreeId.NULLIFIER_TREE,
428
- siloedNullifier,
429
- );
465
+ const treeId = MerkleTreeId.NULLIFIER_TREE;
466
+ const {
467
+ preimage: leafPreimage,
468
+ leafOrLowLeafIndex,
469
+ alreadyPresent,
470
+ } = await this.getLeafOrLowLeafInfo<typeof treeId, NullifierLeafPreimage>(treeId, siloedNullifier.toBigInt());
471
+
430
472
  if (alreadyPresent) {
431
- this.log.verbose(`Siloed nullifier ${siloedNullifier} already present in tree at index ${index}!`);
473
+ this.log.verbose(`Siloed nullifier ${siloedNullifier} already present in tree at index ${leafOrLowLeafIndex}!`);
432
474
  // If the nullifier is already present, we should not insert it again
433
475
  // instead we provide the direct membership path
434
- const path = await this.merkleTrees.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, index);
476
+ const membershipPath = await this.db.getSiblingPath(treeId, leafOrLowLeafIndex);
435
477
  // This just becomes a nullifier read hint
436
478
  this.trace.traceNullifierCheck(
437
479
  siloedNullifier,
438
480
  /*exists=*/ alreadyPresent,
439
- preimage as NullifierLeafPreimage,
440
- new Fr(index),
441
- path,
481
+ leafPreimage,
482
+ new Fr(leafOrLowLeafIndex),
483
+ membershipPath.toFields(),
442
484
  );
443
485
  throw new NullifierCollisionError(
444
486
  `Siloed nullifier ${siloedNullifier} already exists in parent cache or host.`,
445
487
  );
446
488
  } else {
447
- // Cache pending nullifiers for later access
448
- await this.nullifiers.append(siloedNullifier);
449
- // We append the new nullifier
450
- this.log.trace(
451
- `Nullifier tree root before insertion ${await this.merkleTrees.treeMap
452
- .get(MerkleTreeId.NULLIFIER_TREE)!
453
- .getRoot()}`,
454
- );
455
- const appendResult = await this.merkleTrees.appendNullifier(siloedNullifier);
456
- this.log.trace(
457
- `Nullifier tree root after insertion ${await this.merkleTrees.treeMap
458
- .get(MerkleTreeId.NULLIFIER_TREE)!
459
- .getRoot()}`,
460
- );
461
- const lowLeafPreimage = appendResult.lowWitness.preimage as NullifierLeafPreimage;
462
- const lowLeafIndex = appendResult.lowWitness.index;
463
- const lowLeafPath = appendResult.lowWitness.siblingPath;
464
- const insertionPath = appendResult.insertionPath;
489
+ const appendResult = await this.db.sequentialInsert(treeId, [siloedNullifier.toBuffer()]);
490
+ const lowLeafWitnessData = appendResult.lowLeavesWitnessData![0];
491
+ const lowLeafPreimage = lowLeafWitnessData.leafPreimage as NullifierLeafPreimage;
492
+ const lowLeafIndex = lowLeafWitnessData.index;
493
+ const lowLeafPath = lowLeafWitnessData.siblingPath.toFields();
494
+ const insertionPath = appendResult.insertionWitnessData[0].siblingPath.toFields();
495
+
465
496
  this.trace.traceNewNullifier(
466
497
  siloedNullifier,
467
498
  lowLeafPreimage,
@@ -501,13 +532,7 @@ export class AvmPersistableStateManager {
501
532
  );
502
533
 
503
534
  if (this.doMerkleOperations) {
504
- // TODO(8287): We still return exists here, but we need to transmit both the requested msgHash and the value
505
- // such that the VM can constrain the equality and decide on exists based on that.
506
- // We should defintely add a helper here
507
- const path = await this.merkleTrees.treeDb.getSiblingPath(
508
- MerkleTreeId.L1_TO_L2_MESSAGE_TREE,
509
- msgLeafIndex.toBigInt(),
510
- );
535
+ const path = await this.db.getSiblingPath(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, msgLeafIndex.toBigInt());
511
536
  this.trace.traceL1ToL2MessageCheck(contractAddress, valueAtIndex, msgLeafIndex, exists, path.toFields());
512
537
  } else {
513
538
  this.trace.traceL1ToL2MessageCheck(contractAddress, valueAtIndex, msgLeafIndex, exists);
@@ -546,154 +571,135 @@ export class AvmPersistableStateManager {
546
571
  const instanceWithAddress = await this.worldStateDB.getContractInstance(contractAddress);
547
572
  const exists = instanceWithAddress !== undefined;
548
573
 
549
- let [existsInTree, leafOrLowLeafPreimage, leafOrLowLeafIndex, leafOrLowLeafPath] = [
550
- exists,
551
- NullifierLeafPreimage.empty(),
552
- Fr.ZERO,
553
- new Array<Fr>(),
554
- ];
555
- if (!contractAddressIsCanonical(contractAddress)) {
556
- const contractAddressNullifier = await siloNullifier(
557
- AztecAddress.fromNumber(DEPLOYER_CONTRACT_ADDRESS),
558
- contractAddress.toField(),
559
- );
560
- [existsInTree, leafOrLowLeafPreimage, leafOrLowLeafIndex, leafOrLowLeafPath] = await this.getNullifierMembership(
561
- /*siloedNullifier=*/ contractAddressNullifier,
562
- );
563
- assert(
564
- exists == existsInTree,
565
- 'WorldStateDB contains contract instance, but nullifier tree does not contain contract address (or vice versa).... This is a bug!',
566
- );
574
+ const instance = exists ? new SerializableContractInstance(instanceWithAddress) : undefined;
575
+ if (!exists) {
576
+ this.log.debug(`Contract instance NOT FOUND (address=${contractAddress})`);
577
+ this.trace.traceGetContractInstance(contractAddress, exists);
578
+ return undefined;
567
579
  }
568
580
 
581
+ this.log.trace(`Got contract instance (address=${contractAddress}): instance=${jsonStringify(instance!)}`);
582
+ // Canonical addresses do not trigger nullifier & public storage checks
583
+ if (contractAddressIsCanonical(contractAddress)) {
584
+ this.trace.traceGetContractInstance(contractAddress, exists, instance);
585
+ return instance;
586
+ }
587
+
588
+ // This will decide internally whether to check the nullifier tree or not depending on doMerkleOperations.
589
+ const nullifierExistsInTree = await this.checkNullifierExists(
590
+ AztecAddress.fromNumber(DEPLOYER_CONTRACT_ADDRESS),
591
+ contractAddress.toField(),
592
+ );
593
+ assert(
594
+ exists == nullifierExistsInTree,
595
+ 'WorldStateDB contains contract instance, but nullifier tree does not contain contract address (or vice versa).... This is a bug!',
596
+ );
597
+
598
+ if (this.doMerkleOperations) {
599
+ // TODO(fcarreiro): this should be done via a public read.
600
+ const { updateMembership, updatePreimage } = await this.getContractUpdateHints(contractAddress);
601
+
602
+ this.trace.traceGetContractInstance(contractAddress, exists, instance, updateMembership, updatePreimage);
603
+ } else {
604
+ this.trace.traceGetContractInstance(contractAddress, exists, instance);
605
+ }
606
+
607
+ return instance;
608
+ }
609
+
610
+ /**
611
+ * Get a contract class.
612
+ * @param classId - class id to retrieve.
613
+ * @returns the contract class or undefined if it does not exist.
614
+ */
615
+ public async getContractClass(classId: Fr): Promise<ContractClassWithCommitment | undefined> {
616
+ this.log.trace(`Getting contract class for id ${classId}`);
617
+ const klass = await this.worldStateDB.getContractClass(classId);
618
+ const exists = klass !== undefined;
619
+ let extendedClass: ContractClassWithCommitment | undefined = undefined;
620
+
621
+ // Note: We currently do not generate info to check the nullifier tree, because
622
+ // this is not needed for our use cases.
569
623
  if (exists) {
570
- const instance = new SerializableContractInstance(instanceWithAddress);
571
- this.log.trace(
572
- `Got contract instance (address=${contractAddress}): exists=${exists}, instance=${jsonStringify(instance)}`,
624
+ this.log.trace(`Got contract class (id=${classId})`);
625
+ // Extend class information with public bytecode commitment.
626
+ const bytecodeCommitment = await this.worldStateDB.getBytecodeCommitment(classId);
627
+ assert(
628
+ bytecodeCommitment,
629
+ `Bytecode commitment was not found in DB for contract class (${classId}). This should not happen!`,
573
630
  );
574
- if (this.doMerkleOperations) {
575
- this.trace.traceGetContractInstance(
576
- contractAddress,
577
- exists,
578
- instance,
579
- leafOrLowLeafPreimage,
580
- leafOrLowLeafIndex,
581
- leafOrLowLeafPath,
582
- );
583
- } else {
584
- this.trace.traceGetContractInstance(contractAddress, exists, instance);
585
- }
586
-
587
- return Promise.resolve(instance);
631
+ extendedClass = {
632
+ ...klass,
633
+ publicBytecodeCommitment: bytecodeCommitment,
634
+ };
588
635
  } else {
589
- this.log.debug(`Contract instance NOT FOUND (address=${contractAddress})`);
590
- if (this.doMerkleOperations) {
591
- this.trace.traceGetContractInstance(
592
- contractAddress,
593
- exists,
594
- /*instance=*/ undefined,
595
- leafOrLowLeafPreimage,
596
- leafOrLowLeafIndex,
597
- leafOrLowLeafPath,
598
- );
599
- } else {
600
- this.trace.traceGetContractInstance(contractAddress, exists);
601
- }
602
- return Promise.resolve(undefined);
636
+ this.log.debug(`Contract instance NOT FOUND (id=${classId})`);
603
637
  }
638
+
639
+ this.trace.traceGetContractClass(classId, exists, extendedClass);
640
+ return extendedClass;
604
641
  }
605
642
 
606
643
  /**
607
- * Get a contract's bytecode from the contracts DB, also trace the contract class and instance
644
+ * Get a contract's bytecode from the contracts DB, also trace the contract class and instance indirectly.
608
645
  */
609
646
  public async getBytecode(contractAddress: AztecAddress): Promise<Buffer | undefined> {
610
647
  this.log.debug(`Getting bytecode for contract address ${contractAddress}`);
611
- const instanceWithAddress = await this.worldStateDB.getContractInstance(contractAddress);
612
- const exists = instanceWithAddress !== undefined;
648
+ const contractInstance = await this.getContractInstance(contractAddress);
613
649
 
614
- let [existsInTree, leafOrLowLeafPreimage, leafOrLowLeafIndex, leafOrLowLeafPath] = [
615
- exists,
616
- NullifierLeafPreimage.empty(),
617
- Fr.ZERO,
618
- new Array<Fr>(),
619
- ];
620
- if (!contractAddressIsCanonical(contractAddress)) {
621
- const contractAddressNullifier = await siloNullifier(
622
- AztecAddress.fromNumber(DEPLOYER_CONTRACT_ADDRESS),
623
- contractAddress.toField(),
624
- );
625
- [existsInTree, leafOrLowLeafPreimage, leafOrLowLeafIndex, leafOrLowLeafPath] = await this.getNullifierMembership(
626
- /*siloedNullifier=*/ contractAddressNullifier,
627
- );
628
- assert(
629
- exists == existsInTree,
630
- 'WorldStateDB contains contract instance, but nullifier tree does not contain contract address (or vice versa).... This is a bug!',
631
- );
650
+ if (!contractInstance) {
651
+ return undefined;
632
652
  }
633
653
 
634
- if (exists) {
635
- const instance = new SerializableContractInstance(instanceWithAddress);
636
- const contractClass = await this.worldStateDB.getContractClass(instance.contractClassId);
637
- const bytecodeCommitment = await this.worldStateDB.getBytecodeCommitment(instance.contractClassId);
654
+ const contractClass = await this.getContractClass(contractInstance.currentContractClassId);
655
+ assert(
656
+ contractClass,
657
+ `Contract class not found in DB, but a contract instance was found with this class ID (${contractInstance.currentContractClassId}). This should not happen!`,
658
+ );
638
659
 
639
- assert(
640
- contractClass,
641
- `Contract class not found in DB, but a contract instance was found with this class ID (${instance.contractClassId}). This should not happen!`,
642
- );
660
+ // NOTE: If the contract instance is not found, we assume it has not been deployed.
661
+ // It doesnt matter what the values of the contract instance are in this case, as long as we tag it with exists=false.
662
+ // This will hint to the avm circuit to just perform the non-membership check on the address and disregard the bytecode hash
663
+ return contractClass.packedBytecode;
664
+ }
643
665
 
644
- assert(
645
- bytecodeCommitment,
646
- `Bytecode commitment was not found in DB for contract class (${instance.contractClassId}). This should not happen!`,
647
- );
666
+ async getContractUpdateHints(contractAddress: AztecAddress) {
667
+ const { sharedMutableSlot, sharedMutableHashSlot } = await SharedMutableValuesWithHash.getContractUpdateSlots(
668
+ contractAddress,
669
+ );
648
670
 
649
- const contractClassPreimage = {
650
- artifactHash: contractClass.artifactHash,
651
- privateFunctionsRoot: contractClass.privateFunctionsRoot,
652
- publicBytecodeCommitment: bytecodeCommitment,
653
- };
671
+ const {
672
+ value: hash,
673
+ leafPreimage,
674
+ leafIndex,
675
+ leafPath,
676
+ } = await this.getPublicDataMembership(ProtocolContractAddress.ContractInstanceDeployer, sharedMutableHashSlot);
677
+ const updateMembership = new AvmPublicDataReadTreeHint(leafPreimage, leafIndex, leafPath);
654
678
 
655
- if (this.doMerkleOperations) {
656
- this.trace.traceGetBytecode(
657
- contractAddress,
658
- exists,
659
- contractClass.packedBytecode,
660
- instance,
661
- contractClassPreimage,
662
- leafOrLowLeafPreimage,
663
- leafOrLowLeafIndex,
664
- leafOrLowLeafPath,
665
- );
666
- } else {
667
- this.trace.traceGetBytecode(
668
- contractAddress,
669
- exists,
670
- contractClass.packedBytecode,
671
- instance,
672
- contractClassPreimage,
673
- );
674
- }
679
+ const readStorage = async (storageSlot: Fr) =>
680
+ (await this.publicStorage.read(ProtocolContractAddress.ContractInstanceDeployer, storageSlot)).value;
681
+
682
+ const sharedMutableValues = await SharedMutableValues.readFromTree(sharedMutableSlot, readStorage);
683
+
684
+ const updatePreimage = sharedMutableValues.toFields();
675
685
 
676
- return contractClass.packedBytecode;
686
+ if (!hash.isZero()) {
687
+ const hashed = await poseidon2Hash(updatePreimage);
688
+ if (!hashed.equals(hash)) {
689
+ throw new Error(`Update hint hash mismatch: ${hash} != ${hashed}`);
690
+ }
691
+ this.log.trace(`Non empty update hint found for contract ${contractAddress}`);
677
692
  } else {
678
- // If the contract instance is not found, we assume it has not been deployed.
679
- // It doesnt matter what the values of the contract instance are in this case, as long as we tag it with exists=false.
680
- // This will hint to the avm circuit to just perform the non-membership check on the address and disregard the bytecode hash
681
- if (this.doMerkleOperations) {
682
- this.trace.traceGetBytecode(
683
- contractAddress,
684
- exists,
685
- /*instance=*/ undefined,
686
- /*contractClass=*/ undefined,
687
- /*bytecode=*/ undefined,
688
- leafOrLowLeafPreimage,
689
- leafOrLowLeafIndex,
690
- leafOrLowLeafPath,
691
- );
692
- } else {
693
- this.trace.traceGetBytecode(contractAddress, exists); // bytecode, instance, class undefined
693
+ if (updatePreimage.some(f => !f.isZero())) {
694
+ throw new Error(`Update hint hash is zero, but update preimage is not: ${updatePreimage}`);
694
695
  }
695
- return undefined;
696
+ this.log.trace(`No update hint found for contract ${contractAddress}`);
696
697
  }
698
+
699
+ return {
700
+ updateMembership,
701
+ updatePreimage,
702
+ };
697
703
  }
698
704
 
699
705
  public traceEnqueuedCall(publicCallRequest: PublicCallRequest, calldata: Fr[], reverted: boolean) {
@@ -703,6 +709,27 @@ export class AvmPersistableStateManager {
703
709
  public async getPublicFunctionDebugName(avmEnvironment: AvmExecutionEnvironment): Promise<string> {
704
710
  return await getPublicFunctionDebugName(this.worldStateDB, avmEnvironment.address, avmEnvironment.calldata);
705
711
  }
712
+
713
+ public async getLeafOrLowLeafInfo<ID extends IndexedTreeId, T extends IndexedTreeLeafPreimage>(
714
+ treeId: ID,
715
+ key: bigint,
716
+ ): Promise<GetLeafResult<T>> {
717
+ // "key" is siloed slot (leafSlot) or siloed nullifier
718
+ const leafOrLowLeafInfo = await this.db.getPreviousValueIndex(treeId, key);
719
+ assert(
720
+ leafOrLowLeafInfo !== undefined,
721
+ `${MerkleTreeId[treeId]} low leaf index should always be found (even if target leaf does not exist)`,
722
+ );
723
+ const { index: leafOrLowLeafIndex, alreadyPresent } = leafOrLowLeafInfo;
724
+
725
+ const leafPreimage = await this.db.getLeafPreimage(treeId, leafOrLowLeafIndex);
726
+ assert(
727
+ leafPreimage !== undefined,
728
+ `${MerkleTreeId[treeId]} low leaf preimage should never be undefined (even if target leaf does not exist)`,
729
+ );
730
+
731
+ return { preimage: leafPreimage as T, leafOrLowLeafIndex, alreadyPresent };
732
+ }
706
733
  }
707
734
 
708
735
  function contractAddressIsCanonical(contractAddress: AztecAddress): boolean {