@aztec/pxe 0.0.1-commit.fcb71a6 → 0.0.1-commit.ff7989d6c

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 (266) hide show
  1. package/dest/access_scopes.d.ts +9 -0
  2. package/dest/access_scopes.d.ts.map +1 -0
  3. package/dest/access_scopes.js +6 -0
  4. package/dest/bin/check_oracle_version.js +1 -1
  5. package/dest/block_synchronizer/block_synchronizer.d.ts +13 -5
  6. package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
  7. package/dest/block_synchronizer/block_synchronizer.js +74 -20
  8. package/dest/config/index.d.ts +3 -1
  9. package/dest/config/index.d.ts.map +1 -1
  10. package/dest/config/index.js +17 -0
  11. package/dest/config/package_info.js +1 -1
  12. package/dest/contract_function_simulator/benchmarked_node.d.ts +9 -0
  13. package/dest/contract_function_simulator/benchmarked_node.d.ts.map +1 -0
  14. package/dest/contract_function_simulator/benchmarked_node.js +77 -0
  15. package/dest/contract_function_simulator/contract_function_simulator.d.ts +57 -34
  16. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  17. package/dest/contract_function_simulator/contract_function_simulator.js +197 -82
  18. package/dest/contract_function_simulator/execution_note_cache.d.ts +18 -9
  19. package/dest/contract_function_simulator/execution_note_cache.d.ts.map +1 -1
  20. package/dest/contract_function_simulator/execution_note_cache.js +45 -28
  21. package/dest/contract_function_simulator/index.d.ts +2 -2
  22. package/dest/contract_function_simulator/index.d.ts.map +1 -1
  23. package/dest/contract_function_simulator/index.js +1 -1
  24. package/dest/contract_function_simulator/noir-structs/event_validation_request.d.ts +4 -3
  25. package/dest/contract_function_simulator/noir-structs/event_validation_request.d.ts.map +1 -1
  26. package/dest/contract_function_simulator/noir-structs/event_validation_request.js +7 -4
  27. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +3 -3
  28. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts.map +1 -1
  29. package/dest/contract_function_simulator/noir-structs/note_validation_request.js +2 -2
  30. package/dest/contract_function_simulator/noir-structs/utility_context.d.ts +4 -10
  31. package/dest/contract_function_simulator/noir-structs/utility_context.d.ts.map +1 -1
  32. package/dest/contract_function_simulator/noir-structs/utility_context.js +7 -18
  33. package/dest/contract_function_simulator/oracle/interfaces.d.ts +17 -13
  34. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  35. package/dest/contract_function_simulator/oracle/message_load_oracle_inputs.d.ts +3 -1
  36. package/dest/contract_function_simulator/oracle/message_load_oracle_inputs.d.ts.map +1 -1
  37. package/dest/contract_function_simulator/oracle/note_packing_utils.d.ts +6 -6
  38. package/dest/contract_function_simulator/oracle/note_packing_utils.d.ts.map +1 -1
  39. package/dest/contract_function_simulator/oracle/note_packing_utils.js +8 -8
  40. package/dest/contract_function_simulator/oracle/oracle.d.ts +12 -10
  41. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  42. package/dest/contract_function_simulator/oracle/oracle.js +67 -44
  43. package/dest/contract_function_simulator/oracle/private_execution.d.ts +2 -26
  44. package/dest/contract_function_simulator/oracle/private_execution.d.ts.map +1 -1
  45. package/dest/contract_function_simulator/oracle/private_execution.js +1 -37
  46. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +44 -50
  47. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
  48. package/dest/contract_function_simulator/oracle/private_execution_oracle.js +83 -54
  49. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +67 -33
  50. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  51. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +131 -110
  52. package/dest/contract_logging.d.ts +22 -0
  53. package/dest/contract_logging.d.ts.map +1 -0
  54. package/dest/contract_logging.js +23 -0
  55. package/dest/contract_sync/contract_sync_service.d.ts +43 -0
  56. package/dest/contract_sync/contract_sync_service.d.ts.map +1 -0
  57. package/dest/contract_sync/contract_sync_service.js +97 -0
  58. package/dest/contract_sync/helpers.d.ts +29 -0
  59. package/dest/contract_sync/helpers.d.ts.map +1 -0
  60. package/dest/contract_sync/helpers.js +55 -0
  61. package/dest/debug/pxe_debug_utils.d.ts +24 -10
  62. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  63. package/dest/debug/pxe_debug_utils.js +28 -17
  64. package/dest/entrypoints/client/bundle/index.d.ts +4 -1
  65. package/dest/entrypoints/client/bundle/index.d.ts.map +1 -1
  66. package/dest/entrypoints/client/bundle/index.js +3 -0
  67. package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
  68. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
  69. package/dest/entrypoints/client/bundle/utils.js +31 -8
  70. package/dest/entrypoints/client/lazy/index.d.ts +4 -1
  71. package/dest/entrypoints/client/lazy/index.d.ts.map +1 -1
  72. package/dest/entrypoints/client/lazy/index.js +3 -0
  73. package/dest/entrypoints/client/lazy/utils.d.ts +2 -2
  74. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  75. package/dest/entrypoints/client/lazy/utils.js +32 -9
  76. package/dest/entrypoints/pxe_creation_options.d.ts +5 -3
  77. package/dest/entrypoints/pxe_creation_options.d.ts.map +1 -1
  78. package/dest/entrypoints/server/index.d.ts +5 -1
  79. package/dest/entrypoints/server/index.d.ts.map +1 -1
  80. package/dest/entrypoints/server/index.js +4 -0
  81. package/dest/entrypoints/server/utils.d.ts +1 -1
  82. package/dest/entrypoints/server/utils.d.ts.map +1 -1
  83. package/dest/entrypoints/server/utils.js +39 -16
  84. package/dest/events/event_service.d.ts +6 -6
  85. package/dest/events/event_service.d.ts.map +1 -1
  86. package/dest/events/event_service.js +19 -22
  87. package/dest/events/private_event_filter_validator.d.ts +5 -5
  88. package/dest/events/private_event_filter_validator.d.ts.map +1 -1
  89. package/dest/events/private_event_filter_validator.js +5 -6
  90. package/dest/job_coordinator/job_coordinator.d.ts +75 -0
  91. package/dest/job_coordinator/job_coordinator.d.ts.map +1 -0
  92. package/dest/job_coordinator/job_coordinator.js +94 -0
  93. package/dest/logs/log_service.d.ts +8 -5
  94. package/dest/logs/log_service.d.ts.map +1 -1
  95. package/dest/logs/log_service.js +28 -30
  96. package/dest/notes/note_service.d.ts +10 -9
  97. package/dest/notes/note_service.d.ts.map +1 -1
  98. package/dest/notes/note_service.js +34 -38
  99. package/dest/notes_filter.d.ts +25 -0
  100. package/dest/notes_filter.d.ts.map +1 -0
  101. package/dest/notes_filter.js +4 -0
  102. package/dest/oracle_version.d.ts +3 -3
  103. package/dest/oracle_version.d.ts.map +1 -1
  104. package/dest/oracle_version.js +4 -3
  105. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts +4 -0
  106. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts.map +1 -0
  107. package/dest/private_kernel/hints/{compute_tx_include_by_timestamp.js → compute_tx_expiration_timestamp.js} +12 -12
  108. package/dest/private_kernel/hints/index.d.ts +3 -3
  109. package/dest/private_kernel/hints/index.d.ts.map +1 -1
  110. package/dest/private_kernel/hints/index.js +2 -2
  111. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts +28 -0
  112. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts.map +1 -0
  113. package/dest/private_kernel/hints/{build_private_kernel_reset_private_inputs.js → private_kernel_reset_private_inputs_builder.js} +16 -10
  114. package/dest/private_kernel/private_kernel_execution_prover.d.ts +3 -2
  115. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  116. package/dest/private_kernel/private_kernel_execution_prover.js +12 -13
  117. package/dest/private_kernel/private_kernel_oracle.d.ts +24 -24
  118. package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
  119. package/dest/private_kernel/private_kernel_oracle.js +94 -2
  120. package/dest/pxe.d.ts +76 -58
  121. package/dest/pxe.d.ts.map +1 -1
  122. package/dest/pxe.js +149 -138
  123. package/dest/storage/address_store/address_store.d.ts +1 -1
  124. package/dest/storage/address_store/address_store.d.ts.map +1 -1
  125. package/dest/storage/address_store/address_store.js +12 -11
  126. package/dest/storage/anchor_block_store/anchor_block_store.d.ts +9 -1
  127. package/dest/storage/anchor_block_store/anchor_block_store.d.ts.map +1 -1
  128. package/dest/storage/anchor_block_store/anchor_block_store.js +8 -1
  129. package/dest/storage/capsule_store/capsule_store.d.ts +24 -9
  130. package/dest/storage/capsule_store/capsule_store.d.ts.map +1 -1
  131. package/dest/storage/capsule_store/capsule_store.js +130 -23
  132. package/dest/storage/contract_store/contract_store.d.ts +42 -16
  133. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  134. package/dest/storage/contract_store/contract_store.js +157 -84
  135. package/dest/storage/metadata.d.ts +1 -1
  136. package/dest/storage/metadata.js +1 -1
  137. package/dest/storage/note_store/note_store.d.ts +50 -51
  138. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  139. package/dest/storage/note_store/note_store.js +284 -263
  140. package/dest/storage/note_store/stored_note.d.ts +16 -0
  141. package/dest/storage/note_store/stored_note.d.ts.map +1 -0
  142. package/dest/storage/note_store/stored_note.js +43 -0
  143. package/dest/storage/private_event_store/private_event_store.d.ts +47 -7
  144. package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
  145. package/dest/storage/private_event_store/private_event_store.js +231 -73
  146. package/dest/storage/private_event_store/stored_private_event.d.ts +23 -0
  147. package/dest/storage/private_event_store/stored_private_event.d.ts.map +1 -0
  148. package/dest/storage/private_event_store/stored_private_event.js +56 -0
  149. package/dest/storage/tagging_store/recipient_tagging_store.d.ts +15 -8
  150. package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
  151. package/dest/storage/tagging_store/recipient_tagging_store.js +88 -19
  152. package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
  153. package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
  154. package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
  155. package/dest/storage/tagging_store/sender_tagging_store.d.ts +19 -9
  156. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  157. package/dest/storage/tagging_store/sender_tagging_store.js +253 -101
  158. package/dest/tagging/constants.d.ts +2 -0
  159. package/dest/tagging/constants.d.ts.map +1 -0
  160. package/dest/tagging/constants.js +10 -0
  161. package/dest/tagging/get_all_logs_by_tags.d.ts +24 -0
  162. package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -0
  163. package/dest/tagging/get_all_logs_by_tags.js +60 -0
  164. package/dest/tagging/index.d.ts +3 -2
  165. package/dest/tagging/index.d.ts.map +1 -1
  166. package/dest/tagging/index.js +2 -10
  167. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +3 -2
  168. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
  169. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.js +12 -12
  170. package/dest/tagging/recipient_sync/utils/find_highest_indexes.js +2 -2
  171. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +3 -2
  172. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
  173. package/dest/tagging/recipient_sync/utils/load_logs_for_range.js +5 -2
  174. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +3 -2
  175. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
  176. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +8 -8
  177. package/dest/tagging/sender_sync/utils/get_status_change_of_pending.d.ts +1 -1
  178. package/dest/tagging/sender_sync/utils/get_status_change_of_pending.d.ts.map +1 -1
  179. package/dest/tagging/sender_sync/utils/get_status_change_of_pending.js +5 -8
  180. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +5 -2
  181. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
  182. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.js +10 -5
  183. package/package.json +27 -18
  184. package/src/access_scopes.ts +9 -0
  185. package/src/bin/check_oracle_version.ts +1 -0
  186. package/src/block_synchronizer/block_synchronizer.ts +93 -32
  187. package/src/config/index.ts +14 -0
  188. package/src/config/package_info.ts +1 -1
  189. package/src/contract_function_simulator/benchmarked_node.ts +103 -0
  190. package/src/contract_function_simulator/contract_function_simulator.ts +350 -143
  191. package/src/contract_function_simulator/execution_note_cache.ts +44 -25
  192. package/src/contract_function_simulator/index.ts +1 -1
  193. package/src/contract_function_simulator/noir-structs/event_validation_request.ts +6 -2
  194. package/src/contract_function_simulator/noir-structs/note_validation_request.ts +2 -2
  195. package/src/contract_function_simulator/noir-structs/utility_context.ts +6 -25
  196. package/src/contract_function_simulator/oracle/interfaces.ts +22 -12
  197. package/src/contract_function_simulator/oracle/note_packing_utils.ts +10 -10
  198. package/src/contract_function_simulator/oracle/oracle.ts +79 -47
  199. package/src/contract_function_simulator/oracle/private_execution.ts +1 -69
  200. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +119 -137
  201. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +198 -115
  202. package/src/contract_logging.ts +39 -0
  203. package/src/contract_sync/contract_sync_service.ts +152 -0
  204. package/src/contract_sync/helpers.ts +98 -0
  205. package/src/debug/pxe_debug_utils.ts +63 -18
  206. package/src/entrypoints/client/bundle/index.ts +3 -0
  207. package/src/entrypoints/client/bundle/utils.ts +23 -16
  208. package/src/entrypoints/client/lazy/index.ts +3 -0
  209. package/src/entrypoints/client/lazy/utils.ts +24 -17
  210. package/src/entrypoints/pxe_creation_options.ts +4 -2
  211. package/src/entrypoints/server/index.ts +4 -0
  212. package/src/entrypoints/server/utils.ts +33 -41
  213. package/src/events/event_service.ts +19 -25
  214. package/src/events/private_event_filter_validator.ts +3 -5
  215. package/src/job_coordinator/job_coordinator.ts +150 -0
  216. package/src/logs/log_service.ts +40 -34
  217. package/src/notes/note_service.ts +41 -45
  218. package/src/notes_filter.ts +26 -0
  219. package/src/oracle_version.ts +4 -3
  220. package/src/private_kernel/hints/{compute_tx_include_by_timestamp.ts → compute_tx_expiration_timestamp.ts} +13 -13
  221. package/src/private_kernel/hints/index.ts +2 -2
  222. package/src/private_kernel/hints/{build_private_kernel_reset_private_inputs.ts → private_kernel_reset_private_inputs_builder.ts} +39 -28
  223. package/src/private_kernel/private_kernel_execution_prover.ts +15 -14
  224. package/src/private_kernel/private_kernel_oracle.ts +116 -36
  225. package/src/pxe.ts +280 -209
  226. package/src/storage/address_store/address_store.ts +15 -15
  227. package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
  228. package/src/storage/capsule_store/capsule_store.ts +159 -23
  229. package/src/storage/contract_store/contract_store.ts +186 -96
  230. package/src/storage/metadata.ts +1 -1
  231. package/src/storage/note_store/note_store.ts +326 -320
  232. package/src/storage/note_store/stored_note.ts +48 -0
  233. package/src/storage/private_event_store/private_event_store.ts +301 -79
  234. package/src/storage/private_event_store/stored_private_event.ts +73 -0
  235. package/src/storage/tagging_store/recipient_tagging_store.ts +106 -20
  236. package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
  237. package/src/storage/tagging_store/sender_tagging_store.ts +294 -109
  238. package/src/tagging/constants.ts +10 -0
  239. package/src/tagging/get_all_logs_by_tags.ts +92 -0
  240. package/src/tagging/index.ts +2 -11
  241. package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +25 -11
  242. package/src/tagging/recipient_sync/utils/find_highest_indexes.ts +2 -2
  243. package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +7 -1
  244. package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +10 -7
  245. package/src/tagging/sender_sync/utils/get_status_change_of_pending.ts +5 -13
  246. package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +16 -4
  247. package/dest/contract_function_simulator/proxied_node.d.ts +0 -9
  248. package/dest/contract_function_simulator/proxied_node.d.ts.map +0 -1
  249. package/dest/contract_function_simulator/proxied_node.js +0 -27
  250. package/dest/private_kernel/hints/build_private_kernel_reset_private_inputs.d.ts +0 -28
  251. package/dest/private_kernel/hints/build_private_kernel_reset_private_inputs.d.ts.map +0 -1
  252. package/dest/private_kernel/hints/compute_tx_include_by_timestamp.d.ts +0 -4
  253. package/dest/private_kernel/hints/compute_tx_include_by_timestamp.d.ts.map +0 -1
  254. package/dest/private_kernel/private_kernel_oracle_impl.d.ts +0 -46
  255. package/dest/private_kernel/private_kernel_oracle_impl.d.ts.map +0 -1
  256. package/dest/private_kernel/private_kernel_oracle_impl.js +0 -86
  257. package/dest/public_storage/public_storage_service.d.ts +0 -24
  258. package/dest/public_storage/public_storage_service.d.ts.map +0 -1
  259. package/dest/public_storage/public_storage_service.js +0 -26
  260. package/dest/tree_membership/tree_membership_service.d.ts +0 -52
  261. package/dest/tree_membership/tree_membership_service.d.ts.map +0 -1
  262. package/dest/tree_membership/tree_membership_service.js +0 -84
  263. package/src/contract_function_simulator/proxied_node.ts +0 -33
  264. package/src/private_kernel/private_kernel_oracle_impl.ts +0 -133
  265. package/src/public_storage/public_storage_service.ts +0 -33
  266. package/src/tree_membership/tree_membership_service.ts +0 -112
@@ -1,9 +1,9 @@
1
1
  import type { FUNCTION_TREE_HEIGHT } from '@aztec/constants';
2
- import type { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { toArray } from '@aztec/foundation/iterable';
4
+ import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
4
5
  import type { MembershipWitness } from '@aztec/foundation/trees';
5
6
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
6
- import { isProtocolContract } from '@aztec/protocol-contracts';
7
7
  import {
8
8
  type ContractArtifact,
9
9
  type FunctionAbi,
@@ -20,7 +20,8 @@ import {
20
20
  } from '@aztec/stdlib/abi';
21
21
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
22
22
  import {
23
- type ContractClass,
23
+ type ContractClassIdPreimage,
24
+ type ContractClassWithId,
24
25
  type ContractInstanceWithAddress,
25
26
  SerializableContractInstance,
26
27
  getContractClassFromArtifact,
@@ -28,6 +29,68 @@ import {
28
29
 
29
30
  import { PrivateFunctionsTree } from './private_functions_tree.js';
30
31
 
32
+ const VERSION = 1 as const;
33
+
34
+ /**
35
+ * All contract class data except the large packedBytecode.
36
+ * The expensive data from the ContractClass is precomputed and stored in this format to avoid redundant hashing.
37
+ * Since we have to store the artifacts anyway, the final ContractClass is reconstructed by combining this data
38
+ * with the packedBytecode obtained from the former. That way we can have quick class lookups without wasted storage.
39
+ */
40
+ export class SerializableContractClassData {
41
+ public readonly version = VERSION;
42
+ public readonly id: Fr;
43
+ public readonly artifactHash: Fr;
44
+ public readonly privateFunctionsRoot: Fr;
45
+ public readonly publicBytecodeCommitment: Fr;
46
+ public readonly privateFunctions: { selector: FunctionSelector; vkHash: Fr }[];
47
+
48
+ constructor(
49
+ data: ContractClassIdPreimage & {
50
+ id: Fr;
51
+ privateFunctions: { selector: FunctionSelector; vkHash: Fr }[];
52
+ },
53
+ ) {
54
+ this.id = data.id;
55
+ this.artifactHash = data.artifactHash;
56
+ this.privateFunctionsRoot = data.privateFunctionsRoot;
57
+ this.publicBytecodeCommitment = data.publicBytecodeCommitment;
58
+ this.privateFunctions = data.privateFunctions;
59
+ }
60
+
61
+ toBuffer(): Buffer {
62
+ return serializeToBuffer(
63
+ numToUInt8(this.version),
64
+ this.id,
65
+ this.artifactHash,
66
+ this.privateFunctionsRoot,
67
+ this.publicBytecodeCommitment,
68
+ this.privateFunctions.length,
69
+ ...this.privateFunctions.map(fn => serializeToBuffer(fn.selector, fn.vkHash)),
70
+ );
71
+ }
72
+
73
+ static fromBuffer(bufferOrReader: Buffer | BufferReader): SerializableContractClassData {
74
+ const reader = BufferReader.asReader(bufferOrReader);
75
+ const version = reader.readUInt8();
76
+ if (version !== VERSION) {
77
+ throw new Error(`Unexpected contract class data version ${version}`);
78
+ }
79
+ return new SerializableContractClassData({
80
+ id: reader.readObject(Fr),
81
+ artifactHash: reader.readObject(Fr),
82
+ privateFunctionsRoot: reader.readObject(Fr),
83
+ publicBytecodeCommitment: reader.readObject(Fr),
84
+ privateFunctions: reader.readVector({
85
+ fromBuffer: (r: BufferReader) => ({
86
+ selector: r.readObject(FunctionSelector),
87
+ vkHash: r.readObject(Fr),
88
+ }),
89
+ }),
90
+ });
91
+ }
92
+ }
93
+
31
94
  /**
32
95
  * ContractStore serves as a data manager and retriever for Aztec.nr contracts.
33
96
  * It provides methods to obtain contract addresses, function ABI, bytecode, and membership witnesses
@@ -40,37 +103,68 @@ export class ContractStore {
40
103
  // TODO: Update it to be LRU cache so that it doesn't keep all the data all the time.
41
104
  #privateFunctionTrees: Map<string, PrivateFunctionsTree> = new Map();
42
105
 
43
- /** Map from contract address to contract class id */
106
+ /**
107
+ * In-memory cache of deserialized ContractArtifact objects, keyed by class id string.
108
+ * Avoids repeated LMDB reads + JSON.parse + Zod validation on every oracle call.
109
+ * Artifacts are large but immutable after registration — safe to cache for the lifetime of the store.
110
+ */
111
+ // TODO: Update it to be LRU cache so that it doesn't keep all the data all the time.
112
+ #contractArtifactCache: Map<string, ContractArtifact> = new Map();
113
+
114
+ /** Map from contract address to contract class id (avoids KV round-trip on hot path). */
44
115
  #contractClassIdMap: Map<string, Fr> = new Map();
45
116
 
117
+ #store: AztecAsyncKVStore;
46
118
  #contractArtifacts: AztecAsyncMap<string, Buffer>;
119
+ #contractClassData: AztecAsyncMap<string, Buffer>;
47
120
  #contractInstances: AztecAsyncMap<string, Buffer>;
48
121
 
49
122
  constructor(store: AztecAsyncKVStore) {
123
+ this.#store = store;
50
124
  this.#contractArtifacts = store.openMap('contract_artifacts');
125
+ this.#contractClassData = store.openMap('contract_classes');
51
126
  this.#contractInstances = store.openMap('contracts_instances');
52
127
  }
53
128
 
54
129
  // Setters
55
130
 
56
- public async addContractArtifact(id: Fr, contract: ContractArtifact): Promise<void> {
131
+ /**
132
+ * Registers a new contract artifact and its corresponding class data.
133
+ * IMPORTANT: This method does not verify that the provided artifact matches the class data or that the class id matches the artifact.
134
+ * It is the caller's responsibility to ensure the consistency and correctness of the provided data.
135
+ * This is done to avoid redundant, expensive contract class computations
136
+ */
137
+ public async addContractArtifact(
138
+ contract: ContractArtifact,
139
+ contractClassWithIdAndPreimage?: ContractClassWithId & ContractClassIdPreimage,
140
+ ): Promise<Fr> {
141
+ const contractClass = contractClassWithIdAndPreimage ?? (await getContractClassFromArtifact(contract));
142
+ const key = contractClass.id.toString();
143
+
144
+ if (this.#contractArtifactCache.has(key)) {
145
+ return contractClass.id;
146
+ }
147
+
57
148
  const privateFunctions = contract.functions.filter(
58
149
  functionArtifact => functionArtifact.functionType === FunctionType.PRIVATE,
59
150
  );
60
-
61
151
  const privateSelectors = await Promise.all(
62
- privateFunctions.map(async privateFunctionArtifact =>
63
- (
64
- await FunctionSelector.fromNameAndParameters(privateFunctionArtifact.name, privateFunctionArtifact.parameters)
65
- ).toString(),
152
+ privateFunctions.map(async fn =>
153
+ (await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters)).toString(),
66
154
  ),
67
155
  );
68
-
69
156
  if (privateSelectors.length !== new Set(privateSelectors).size) {
70
157
  throw new Error('Repeated function selectors of private functions');
71
158
  }
72
159
 
73
- await this.#contractArtifacts.set(id.toString(), contractArtifactToBuffer(contract));
160
+ this.#contractArtifactCache.set(key, contract);
161
+
162
+ await this.#store.transactionAsync(async () => {
163
+ await this.#contractArtifacts.set(key, contractArtifactToBuffer(contract));
164
+ await this.#contractClassData.set(key, new SerializableContractClassData(contractClass).toBuffer());
165
+ });
166
+
167
+ return contractClass.id;
74
168
  }
75
169
 
76
170
  async addContractInstance(contract: ContractInstanceWithAddress): Promise<void> {
@@ -85,26 +179,17 @@ export class ContractStore {
85
179
  // Private getters
86
180
 
87
181
  async #getContractClassId(contractAddress: AztecAddress): Promise<Fr | undefined> {
88
- if (!this.#contractClassIdMap.has(contractAddress.toString())) {
182
+ const key = contractAddress.toString();
183
+ if (!this.#contractClassIdMap.has(key)) {
89
184
  const instance = await this.getContractInstance(contractAddress);
90
185
  if (!instance) {
91
186
  return;
92
187
  }
93
- this.#contractClassIdMap.set(contractAddress.toString(), instance.currentContractClassId);
188
+ this.#contractClassIdMap.set(key, instance.currentContractClassId);
94
189
  }
95
- return this.#contractClassIdMap.get(contractAddress.toString());
190
+ return this.#contractClassIdMap.get(key);
96
191
  }
97
192
 
98
- /**
99
- * Retrieve or create a ContractTree instance based on the provided class id.
100
- * If an existing tree with the same class id is found in the cache, it will be returned.
101
- * Otherwise, a new ContractTree instance will be created using the contract data from the database
102
- * and added to the cache before returning.
103
- *
104
- * @param classId - The class id of the contract for which the ContractTree is required.
105
- * @returns A ContractTree instance associated with the specified contract address.
106
- * @throws An Error if the contract is not found in the ContractDatabase.
107
- */
108
193
  async #getPrivateFunctionTreeForClassId(classId: Fr): Promise<PrivateFunctionsTree | undefined> {
109
194
  if (!this.#privateFunctionTrees.has(classId.toString())) {
110
195
  const artifact = await this.getContractArtifact(classId);
@@ -117,34 +202,61 @@ export class ContractStore {
117
202
  return this.#privateFunctionTrees.get(classId.toString())!;
118
203
  }
119
204
 
120
- async #getContractArtifactByAddress(contractAddress: AztecAddress): Promise<ContractArtifact | undefined> {
121
- const contractClassId = await this.#getContractClassId(contractAddress);
122
- return contractClassId && this.getContractArtifact(contractClassId);
205
+ async #getArtifactByAddress(contractAddress: AztecAddress): Promise<ContractArtifact | undefined> {
206
+ const classId = await this.#getContractClassId(contractAddress);
207
+ return classId && this.getContractArtifact(classId);
123
208
  }
124
209
 
125
210
  // Public getters
126
211
 
127
- async getContractsAddresses(): Promise<AztecAddress[]> {
128
- const keys = await toArray(this.#contractInstances.keysAsync());
129
- return keys.map(AztecAddress.fromString);
212
+ getContractsAddresses(): Promise<AztecAddress[]> {
213
+ return this.#store.transactionAsync(async () => {
214
+ const keys = await toArray(this.#contractInstances.keysAsync());
215
+ return keys.map(AztecAddress.fromString);
216
+ });
130
217
  }
131
218
 
132
- /** Returns a contract instance for a given address. Throws if not found. */
133
- public async getContractInstance(contractAddress: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
134
- const contract = await this.#contractInstances.getAsync(contractAddress.toString());
135
- return contract && SerializableContractInstance.fromBuffer(contract).withAddress(contractAddress);
219
+ /** Returns a contract instance for a given address. */
220
+ public getContractInstance(contractAddress: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
221
+ return this.#store.transactionAsync(async () => {
222
+ const contract = await this.#contractInstances.getAsync(contractAddress.toString());
223
+ return contract && SerializableContractInstance.fromBuffer(contract).withAddress(contractAddress);
224
+ });
136
225
  }
137
226
 
227
+ /** Returns the raw contract artifact for a given class id. */
138
228
  public async getContractArtifact(contractClassId: Fr): Promise<ContractArtifact | undefined> {
139
- const contract = await this.#contractArtifacts.getAsync(contractClassId.toString());
140
- // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
141
- return contract && contractArtifactFromBuffer(Buffer.from(contract));
229
+ const key = contractClassId.toString();
230
+ const cached = this.#contractArtifactCache.get(key);
231
+ if (cached) {
232
+ return cached;
233
+ }
234
+ const artifact = await this.#store.transactionAsync(async () => {
235
+ const buf = await this.#contractArtifacts.getAsync(key);
236
+ return buf && contractArtifactFromBuffer(buf);
237
+ });
238
+ if (artifact) {
239
+ this.#contractArtifactCache.set(key, artifact);
240
+ }
241
+ return artifact;
142
242
  }
143
243
 
144
- /** Returns a contract class for a given class id. Throws if not found. */
145
- public async getContractClass(contractClassId: Fr): Promise<ContractClass | undefined> {
244
+ /** Returns a contract class for a given class id. */
245
+ public async getContractClassWithPreimage(
246
+ contractClassId: Fr,
247
+ ): Promise<(ContractClassWithId & ContractClassIdPreimage) | undefined> {
248
+ const key = contractClassId.toString();
249
+ const buf = await this.#contractClassData.getAsync(key);
250
+ if (!buf) {
251
+ return undefined;
252
+ }
253
+ const classData = SerializableContractClassData.fromBuffer(buf);
146
254
  const artifact = await this.getContractArtifact(contractClassId);
147
- return artifact && getContractClassFromArtifact(artifact);
255
+ if (!artifact) {
256
+ return undefined;
257
+ }
258
+ const packedBytecode = artifact.functions.find(f => f.name === 'public_dispatch')?.bytecode ?? Buffer.alloc(0);
259
+ return { ...classData, packedBytecode };
148
260
  }
149
261
 
150
262
  public async getContract(
@@ -163,8 +275,6 @@ export class ContractStore {
163
275
 
164
276
  /**
165
277
  * Retrieves the artifact of a specified function within a given contract.
166
- * The function is identified by its selector, which is a unique code generated from the function's signature.
167
- * Throws an error if the contract address or function selector are invalid or not found.
168
278
  *
169
279
  * @param contractAddress - The AztecAddress representing the contract containing the function.
170
280
  * @param selector - The function selector.
@@ -174,9 +284,12 @@ export class ContractStore {
174
284
  contractAddress: AztecAddress,
175
285
  selector: FunctionSelector,
176
286
  ): Promise<FunctionArtifactWithContractName | undefined> {
177
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
178
- const fnArtifact = artifact && (await this.#findFunctionArtifactBySelector(artifact, selector));
179
- return fnArtifact && { ...fnArtifact, contractName: artifact.name };
287
+ const artifact = await this.#getArtifactByAddress(contractAddress);
288
+ if (!artifact) {
289
+ return undefined;
290
+ }
291
+ const fn = await this.#findFunctionArtifactBySelector(artifact, selector);
292
+ return fn && { ...fn, contractName: artifact.name };
180
293
  }
181
294
 
182
295
  public async getFunctionArtifactWithDebugMetadata(
@@ -197,50 +310,48 @@ export class ContractStore {
197
310
  public async getPublicFunctionArtifact(
198
311
  contractAddress: AztecAddress,
199
312
  ): Promise<FunctionArtifactWithContractName | undefined> {
200
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
201
- const fnArtifact = artifact && artifact.functions.find(fn => fn.functionType === FunctionType.PUBLIC);
202
- return fnArtifact && { ...fnArtifact, contractName: artifact.name };
313
+ const artifact = await this.#getArtifactByAddress(contractAddress);
314
+ const fn = artifact && artifact.functions.find(f => f.functionType === FunctionType.PUBLIC);
315
+ return fn && { ...fn, contractName: artifact.name };
203
316
  }
204
317
 
205
318
  public async getFunctionAbi(
206
319
  contractAddress: AztecAddress,
207
320
  selector: FunctionSelector,
208
321
  ): Promise<FunctionAbi | undefined> {
209
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
322
+ const artifact = await this.#getArtifactByAddress(contractAddress);
210
323
  return artifact && (await this.#findFunctionAbiBySelector(artifact, selector));
211
324
  }
212
325
 
213
326
  /**
214
327
  * Retrieves the debug metadata of a specified function within a given contract.
215
- * The function is identified by its selector, which is a unique code generated from the function's signature.
216
- * Returns undefined if the debug metadata for the given function is not found.
217
- * Throws if the contract has not been added to the database.
218
328
  *
219
329
  * @param contractAddress - The AztecAddress representing the contract containing the function.
220
330
  * @param selector - The function selector.
221
- * @returns The corresponding function's artifact as an object.
331
+ * @returns The corresponding function's debug metadata, or undefined.
222
332
  */
223
333
  public async getFunctionDebugMetadata(
224
334
  contractAddress: AztecAddress,
225
335
  selector: FunctionSelector,
226
336
  ): Promise<FunctionDebugMetadata | undefined> {
227
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
228
- const fnArtifact = artifact && (await this.#findFunctionArtifactBySelector(artifact, selector));
229
- return fnArtifact && getFunctionDebugMetadata(artifact, fnArtifact);
337
+ const artifact = await this.#getArtifactByAddress(contractAddress);
338
+ if (!artifact) {
339
+ return undefined;
340
+ }
341
+ const fn = await this.#findFunctionArtifactBySelector(artifact, selector);
342
+ return fn && getFunctionDebugMetadata(artifact, fn);
230
343
  }
231
344
 
232
345
  public async getPublicFunctionDebugMetadata(
233
346
  contractAddress: AztecAddress,
234
347
  ): Promise<FunctionDebugMetadata | undefined> {
235
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
236
- const fnArtifact = artifact && artifact.functions.find(fn => fn.functionType === FunctionType.PUBLIC);
237
- return fnArtifact && getFunctionDebugMetadata(artifact, fnArtifact);
348
+ const artifact = await this.#getArtifactByAddress(contractAddress);
349
+ const fn = artifact && artifact.functions.find(f => f.functionType === FunctionType.PUBLIC);
350
+ return fn && getFunctionDebugMetadata(artifact, fn);
238
351
  }
239
352
 
240
353
  /**
241
354
  * Retrieve the function membership witness for the given contract class and function selector.
242
- * The function membership witness represents a proof that the function belongs to the specified contract.
243
- * Throws an error if the contract address or function selector is unknown.
244
355
  *
245
356
  * @param contractClassId - The id of the class.
246
357
  * @param selector - The function selector.
@@ -255,23 +366,21 @@ export class ContractStore {
255
366
  }
256
367
 
257
368
  public async getDebugContractName(contractAddress: AztecAddress) {
258
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
369
+ const artifact = await this.#getArtifactByAddress(contractAddress);
259
370
  return artifact?.name;
260
371
  }
261
372
 
262
373
  public async getDebugFunctionName(contractAddress: AztecAddress, selector: FunctionSelector) {
263
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
264
- const fnArtifact = artifact && (await this.#findFunctionAbiBySelector(artifact, selector));
265
- return `${artifact?.name ?? contractAddress}:${fnArtifact?.name ?? selector}`;
374
+ const artifact = await this.#getArtifactByAddress(contractAddress);
375
+ const fn = artifact && (await this.#findFunctionAbiBySelector(artifact, selector));
376
+ return `${artifact?.name ?? contractAddress}:${fn?.name ?? selector}`;
266
377
  }
267
378
 
268
379
  async #findFunctionArtifactBySelector(
269
380
  artifact: ContractArtifact,
270
381
  selector: FunctionSelector,
271
382
  ): Promise<FunctionArtifact | undefined> {
272
- const functions = artifact.functions;
273
- for (let i = 0; i < functions.length; i++) {
274
- const fn = functions[i];
383
+ for (const fn of artifact.functions) {
275
384
  const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
276
385
  if (fnSelector.equals(selector)) {
277
386
  return fn;
@@ -283,9 +392,7 @@ export class ContractStore {
283
392
  artifact: ContractArtifact,
284
393
  selector: FunctionSelector,
285
394
  ): Promise<FunctionAbi | undefined> {
286
- const functions = [...artifact.functions, ...(artifact.nonDispatchPublicFunctions ?? [])];
287
- for (let i = 0; i < functions.length; i++) {
288
- const fn = functions[i];
395
+ for (const fn of [...artifact.functions, ...(artifact.nonDispatchPublicFunctions ?? [])]) {
289
396
  const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
290
397
  if (fnSelector.equals(selector)) {
291
398
  return fn;
@@ -306,34 +413,17 @@ export class ContractStore {
306
413
  throw new Error(`Unknown function ${functionName} in contract ${contract.name}.`);
307
414
  }
308
415
 
309
- return {
416
+ const selector = await FunctionSelector.fromNameAndParameters(functionDao.name, functionDao.parameters);
417
+
418
+ return FunctionCall.from({
310
419
  name: functionDao.name,
311
- args: encodeArguments(functionDao, args),
312
- selector: await FunctionSelector.fromNameAndParameters(functionDao.name, functionDao.parameters),
313
- type: functionDao.functionType,
314
420
  to,
421
+ selector,
422
+ type: functionDao.functionType,
315
423
  hideMsgSender: false,
316
424
  isStatic: functionDao.isStatic,
425
+ args: encodeArguments(functionDao, args),
317
426
  returnTypes: functionDao.returnTypes,
318
- };
319
- }
320
-
321
- // Synchronize target contract data
322
- public async syncPrivateState(
323
- contractAddress: AztecAddress,
324
- functionToInvokeAfterSync: FunctionSelector | null,
325
- utilityExecutor: (privateSyncCall: FunctionCall) => Promise<any>,
326
- ) {
327
- // Protocol contracts don't have private state to sync
328
- if (!isProtocolContract(contractAddress)) {
329
- const syncPrivateStateFunctionCall = await this.getFunctionCall('sync_private_state', [], contractAddress);
330
- if (functionToInvokeAfterSync && functionToInvokeAfterSync.equals(syncPrivateStateFunctionCall.selector)) {
331
- throw new Error(
332
- 'Forbidden `sync_private_state` invocation. `sync_private_state` can only be invoked by PXE, manual execution can lead to inconsistencies.',
333
- );
334
- }
335
-
336
- return utilityExecutor(syncPrivateStateFunctionCall);
337
- }
427
+ });
338
428
  }
339
429
  }
@@ -1 +1 @@
1
- export const PXE_DATA_SCHEMA_VERSION = 2;
1
+ export const PXE_DATA_SCHEMA_VERSION = 3;