@aztec/pxe 0.0.1-commit.e6bd8901 → 0.0.1-commit.ec5f612

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 (205) 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/block_synchronizer/block_synchronizer.d.ts +5 -3
  5. package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
  6. package/dest/block_synchronizer/block_synchronizer.js +11 -5
  7. package/dest/config/package_info.js +1 -1
  8. package/dest/contract_function_simulator/contract_function_simulator.d.ts +54 -30
  9. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  10. package/dest/contract_function_simulator/contract_function_simulator.js +174 -70
  11. package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts +5 -5
  12. package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts.map +1 -1
  13. package/dest/contract_function_simulator/execution_tagging_index_cache.js +3 -3
  14. package/dest/contract_function_simulator/noir-structs/event_validation_request.js +1 -1
  15. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +2 -2
  16. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts.map +1 -1
  17. package/dest/contract_function_simulator/noir-structs/note_validation_request.js +1 -1
  18. package/dest/contract_function_simulator/oracle/interfaces.d.ts +10 -10
  19. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  20. package/dest/contract_function_simulator/oracle/oracle.d.ts +5 -5
  21. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  22. package/dest/contract_function_simulator/oracle/oracle.js +38 -26
  23. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +36 -36
  24. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
  25. package/dest/contract_function_simulator/oracle/private_execution_oracle.js +74 -31
  26. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +53 -29
  27. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  28. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +84 -63
  29. package/dest/contract_logging.d.ts +22 -0
  30. package/dest/contract_logging.d.ts.map +1 -0
  31. package/dest/contract_logging.js +23 -0
  32. package/dest/contract_sync/contract_sync_service.d.ts +43 -0
  33. package/dest/contract_sync/contract_sync_service.d.ts.map +1 -0
  34. package/dest/contract_sync/contract_sync_service.js +97 -0
  35. package/dest/contract_sync/helpers.d.ts +29 -0
  36. package/dest/contract_sync/helpers.d.ts.map +1 -0
  37. package/dest/contract_sync/{index.js → helpers.js} +14 -15
  38. package/dest/debug/pxe_debug_utils.d.ts +24 -10
  39. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  40. package/dest/debug/pxe_debug_utils.js +28 -18
  41. package/dest/entrypoints/client/bundle/index.d.ts +4 -1
  42. package/dest/entrypoints/client/bundle/index.d.ts.map +1 -1
  43. package/dest/entrypoints/client/bundle/index.js +3 -0
  44. package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
  45. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
  46. package/dest/entrypoints/client/bundle/utils.js +21 -7
  47. package/dest/entrypoints/client/lazy/index.d.ts +4 -1
  48. package/dest/entrypoints/client/lazy/index.d.ts.map +1 -1
  49. package/dest/entrypoints/client/lazy/index.js +3 -0
  50. package/dest/entrypoints/client/lazy/utils.d.ts +2 -2
  51. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  52. package/dest/entrypoints/client/lazy/utils.js +22 -8
  53. package/dest/entrypoints/pxe_creation_options.d.ts +3 -2
  54. package/dest/entrypoints/pxe_creation_options.d.ts.map +1 -1
  55. package/dest/entrypoints/server/index.d.ts +4 -2
  56. package/dest/entrypoints/server/index.d.ts.map +1 -1
  57. package/dest/entrypoints/server/index.js +3 -1
  58. package/dest/entrypoints/server/utils.d.ts +1 -1
  59. package/dest/entrypoints/server/utils.d.ts.map +1 -1
  60. package/dest/entrypoints/server/utils.js +28 -9
  61. package/dest/events/event_service.d.ts +4 -5
  62. package/dest/events/event_service.d.ts.map +1 -1
  63. package/dest/events/event_service.js +5 -6
  64. package/dest/job_coordinator/job_coordinator.d.ts +3 -2
  65. package/dest/job_coordinator/job_coordinator.d.ts.map +1 -1
  66. package/dest/job_coordinator/job_coordinator.js +3 -2
  67. package/dest/logs/log_service.d.ts +7 -5
  68. package/dest/logs/log_service.d.ts.map +1 -1
  69. package/dest/logs/log_service.js +19 -30
  70. package/dest/notes/note_service.d.ts +7 -7
  71. package/dest/notes/note_service.d.ts.map +1 -1
  72. package/dest/notes/note_service.js +9 -10
  73. package/dest/notes_filter.d.ts +25 -0
  74. package/dest/notes_filter.d.ts.map +1 -0
  75. package/dest/notes_filter.js +4 -0
  76. package/dest/oracle_version.d.ts +3 -3
  77. package/dest/oracle_version.d.ts.map +1 -1
  78. package/dest/oracle_version.js +2 -2
  79. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts +4 -0
  80. package/dest/private_kernel/hints/compute_tx_expiration_timestamp.d.ts.map +1 -0
  81. package/dest/private_kernel/hints/{compute_tx_include_by_timestamp.js → compute_tx_expiration_timestamp.js} +12 -12
  82. package/dest/private_kernel/hints/index.d.ts +1 -1
  83. package/dest/private_kernel/hints/index.js +1 -1
  84. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts +4 -3
  85. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts.map +1 -1
  86. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.js +129 -68
  87. package/dest/private_kernel/hints/test_utils.d.ts +122 -0
  88. package/dest/private_kernel/hints/test_utils.d.ts.map +1 -0
  89. package/dest/private_kernel/hints/test_utils.js +203 -0
  90. package/dest/private_kernel/private_kernel_execution_prover.d.ts +3 -2
  91. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  92. package/dest/private_kernel/private_kernel_execution_prover.js +21 -13
  93. package/dest/private_kernel/private_kernel_oracle.d.ts +8 -4
  94. package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
  95. package/dest/private_kernel/private_kernel_oracle.js +7 -3
  96. package/dest/pxe.d.ts +69 -23
  97. package/dest/pxe.d.ts.map +1 -1
  98. package/dest/pxe.js +99 -65
  99. package/dest/storage/address_store/address_store.d.ts +1 -1
  100. package/dest/storage/address_store/address_store.d.ts.map +1 -1
  101. package/dest/storage/address_store/address_store.js +12 -11
  102. package/dest/storage/anchor_block_store/anchor_block_store.d.ts +9 -1
  103. package/dest/storage/anchor_block_store/anchor_block_store.d.ts.map +1 -1
  104. package/dest/storage/anchor_block_store/anchor_block_store.js +8 -1
  105. package/dest/storage/capsule_store/capsule_store.js +6 -8
  106. package/dest/storage/contract_store/contract_store.d.ts +42 -15
  107. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  108. package/dest/storage/contract_store/contract_store.js +157 -72
  109. package/dest/storage/metadata.d.ts +1 -1
  110. package/dest/storage/metadata.js +1 -1
  111. package/dest/storage/note_store/note_store.d.ts +13 -3
  112. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  113. package/dest/storage/note_store/note_store.js +147 -107
  114. package/dest/storage/private_event_store/private_event_store.d.ts +1 -1
  115. package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
  116. package/dest/storage/private_event_store/private_event_store.js +84 -61
  117. package/dest/storage/private_event_store/stored_private_event.d.ts +4 -4
  118. package/dest/storage/private_event_store/stored_private_event.d.ts.map +1 -1
  119. package/dest/storage/private_event_store/stored_private_event.js +2 -2
  120. package/dest/storage/tagging_store/recipient_tagging_store.d.ts +6 -6
  121. package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
  122. package/dest/storage/tagging_store/recipient_tagging_store.js +31 -19
  123. package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
  124. package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
  125. package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
  126. package/dest/storage/tagging_store/sender_tagging_store.d.ts +5 -5
  127. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  128. package/dest/storage/tagging_store/sender_tagging_store.js +184 -114
  129. package/dest/tagging/get_all_logs_by_tags.d.ts +4 -4
  130. package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -1
  131. package/dest/tagging/get_all_logs_by_tags.js +17 -3
  132. package/dest/tagging/index.d.ts +2 -2
  133. package/dest/tagging/index.d.ts.map +1 -1
  134. package/dest/tagging/index.js +1 -1
  135. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +5 -6
  136. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
  137. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.js +7 -7
  138. package/dest/tagging/recipient_sync/utils/find_highest_indexes.js +2 -2
  139. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +7 -8
  140. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
  141. package/dest/tagging/recipient_sync/utils/load_logs_for_range.js +12 -11
  142. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +5 -9
  143. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
  144. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +3 -6
  145. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +5 -8
  146. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
  147. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.js +14 -15
  148. package/package.json +25 -16
  149. package/src/access_scopes.ts +9 -0
  150. package/src/block_synchronizer/block_synchronizer.ts +23 -19
  151. package/src/config/package_info.ts +1 -1
  152. package/src/contract_function_simulator/contract_function_simulator.ts +323 -128
  153. package/src/contract_function_simulator/execution_tagging_index_cache.ts +5 -5
  154. package/src/contract_function_simulator/noir-structs/event_validation_request.ts +1 -1
  155. package/src/contract_function_simulator/noir-structs/note_validation_request.ts +1 -1
  156. package/src/contract_function_simulator/oracle/interfaces.ts +12 -12
  157. package/src/contract_function_simulator/oracle/oracle.ts +41 -24
  158. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +101 -113
  159. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +139 -70
  160. package/src/contract_logging.ts +39 -0
  161. package/src/contract_sync/contract_sync_service.ts +152 -0
  162. package/src/contract_sync/{index.ts → helpers.ts} +22 -24
  163. package/src/debug/pxe_debug_utils.ts +63 -19
  164. package/src/entrypoints/client/bundle/index.ts +3 -0
  165. package/src/entrypoints/client/bundle/utils.ts +16 -15
  166. package/src/entrypoints/client/lazy/index.ts +3 -0
  167. package/src/entrypoints/client/lazy/utils.ts +17 -15
  168. package/src/entrypoints/pxe_creation_options.ts +2 -1
  169. package/src/entrypoints/server/index.ts +3 -1
  170. package/src/entrypoints/server/utils.ts +22 -26
  171. package/src/events/event_service.ts +4 -6
  172. package/src/job_coordinator/job_coordinator.ts +4 -3
  173. package/src/logs/log_service.ts +31 -39
  174. package/src/notes/note_service.ts +9 -11
  175. package/src/notes_filter.ts +26 -0
  176. package/src/oracle_version.ts +2 -2
  177. package/src/private_kernel/hints/{compute_tx_include_by_timestamp.ts → compute_tx_expiration_timestamp.ts} +13 -13
  178. package/src/private_kernel/hints/index.ts +1 -1
  179. package/src/private_kernel/hints/private_kernel_reset_private_inputs_builder.ts +164 -117
  180. package/src/private_kernel/hints/test_utils.ts +325 -0
  181. package/src/private_kernel/private_kernel_execution_prover.ts +25 -15
  182. package/src/private_kernel/private_kernel_oracle.ts +9 -9
  183. package/src/pxe.ts +192 -117
  184. package/src/storage/address_store/address_store.ts +15 -15
  185. package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
  186. package/src/storage/capsule_store/capsule_store.ts +8 -8
  187. package/src/storage/contract_store/contract_store.ts +186 -76
  188. package/src/storage/metadata.ts +1 -1
  189. package/src/storage/note_store/note_store.ts +169 -132
  190. package/src/storage/private_event_store/private_event_store.ts +102 -81
  191. package/src/storage/private_event_store/stored_private_event.ts +3 -3
  192. package/src/storage/tagging_store/recipient_tagging_store.ts +38 -24
  193. package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
  194. package/src/storage/tagging_store/sender_tagging_store.ts +214 -130
  195. package/src/tagging/get_all_logs_by_tags.ts +31 -7
  196. package/src/tagging/index.ts +1 -1
  197. package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +9 -12
  198. package/src/tagging/recipient_sync/utils/find_highest_indexes.ts +2 -2
  199. package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +12 -17
  200. package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +6 -11
  201. package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +14 -23
  202. package/dest/contract_sync/index.d.ts +0 -23
  203. package/dest/contract_sync/index.d.ts.map +0 -1
  204. package/dest/private_kernel/hints/compute_tx_include_by_timestamp.d.ts +0 -4
  205. package/dest/private_kernel/hints/compute_tx_include_by_timestamp.d.ts.map +0 -1
@@ -10,6 +10,14 @@ export class AnchorBlockStore {
10
10
  this.#synchronizedHeader = this.#store.openSingleton('header');
11
11
  }
12
12
 
13
+ /**
14
+ * Sets the currently synchronized block
15
+ *
16
+ * Important: this method is only called from BlockSynchronizer, and since we need it to run atomically with other
17
+ * stores in the case of a reorg, it MUST NOT be wrapped in a `transactionAsync` call. Doing so would result in a
18
+ * deadlock when the backend is IndexedDB, because `transactionAsync` is not designed to support reentrancy.
19
+ *
20
+ */
13
21
  async setHeader(header: BlockHeader): Promise<void> {
14
22
  await this.#synchronizedHeader.set(header.toBuffer());
15
23
  }
@@ -57,14 +57,14 @@ export class CapsuleStore implements StagedStore {
57
57
  */
58
58
  async #getFromStage(jobId: string, dbSlotKey: string): Promise<Buffer | null | undefined> {
59
59
  const jobStagedCapsules = this.#getJobStagedCapsules(jobId);
60
- let staged: Buffer | null | undefined = jobStagedCapsules.get(dbSlotKey);
61
- // Note that if staged === null, we marked it for deletion, so we don't want to
62
- // re-read it from DB
63
- if (staged === undefined) {
64
- // If we don't have a staged version of this dbSlotKey, first we check if there's one in DB
65
- staged = await this.#loadCapsuleFromDb(dbSlotKey);
66
- }
67
- return staged;
60
+ const staged: Buffer | null | undefined = jobStagedCapsules.get(dbSlotKey);
61
+
62
+ // Always issue DB read to keep IndexedDB transaction alive, even if the value is in the job staged data. This
63
+ // keeps IndexedDB transactions alive (they auto-commit when a new micro-task starts and there are no pending read
64
+ // requests). The staged value still takes precedence if it exists (including null for deletions).
65
+ const dbValue = await this.#loadCapsuleFromDb(dbSlotKey);
66
+
67
+ return staged !== undefined ? staged : dbValue;
68
68
  }
69
69
 
70
70
  /**
@@ -1,6 +1,7 @@
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
7
  import {
@@ -19,7 +20,8 @@ import {
19
20
  } from '@aztec/stdlib/abi';
20
21
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
21
22
  import {
22
- type ContractClass,
23
+ type ContractClassIdPreimage,
24
+ type ContractClassWithId,
23
25
  type ContractInstanceWithAddress,
24
26
  SerializableContractInstance,
25
27
  getContractClassFromArtifact,
@@ -27,6 +29,68 @@ import {
27
29
 
28
30
  import { PrivateFunctionsTree } from './private_functions_tree.js';
29
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
+
30
94
  /**
31
95
  * ContractStore serves as a data manager and retriever for Aztec.nr contracts.
32
96
  * It provides methods to obtain contract addresses, function ABI, bytecode, and membership witnesses
@@ -39,37 +103,68 @@ export class ContractStore {
39
103
  // TODO: Update it to be LRU cache so that it doesn't keep all the data all the time.
40
104
  #privateFunctionTrees: Map<string, PrivateFunctionsTree> = new Map();
41
105
 
42
- /** 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). */
43
115
  #contractClassIdMap: Map<string, Fr> = new Map();
44
116
 
117
+ #store: AztecAsyncKVStore;
45
118
  #contractArtifacts: AztecAsyncMap<string, Buffer>;
119
+ #contractClassData: AztecAsyncMap<string, Buffer>;
46
120
  #contractInstances: AztecAsyncMap<string, Buffer>;
47
121
 
48
122
  constructor(store: AztecAsyncKVStore) {
123
+ this.#store = store;
49
124
  this.#contractArtifacts = store.openMap('contract_artifacts');
125
+ this.#contractClassData = store.openMap('contract_classes');
50
126
  this.#contractInstances = store.openMap('contracts_instances');
51
127
  }
52
128
 
53
129
  // Setters
54
130
 
55
- 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
+
56
148
  const privateFunctions = contract.functions.filter(
57
149
  functionArtifact => functionArtifact.functionType === FunctionType.PRIVATE,
58
150
  );
59
-
60
151
  const privateSelectors = await Promise.all(
61
- privateFunctions.map(async privateFunctionArtifact =>
62
- (
63
- await FunctionSelector.fromNameAndParameters(privateFunctionArtifact.name, privateFunctionArtifact.parameters)
64
- ).toString(),
152
+ privateFunctions.map(async fn =>
153
+ (await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters)).toString(),
65
154
  ),
66
155
  );
67
-
68
156
  if (privateSelectors.length !== new Set(privateSelectors).size) {
69
157
  throw new Error('Repeated function selectors of private functions');
70
158
  }
71
159
 
72
- 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;
73
168
  }
74
169
 
75
170
  async addContractInstance(contract: ContractInstanceWithAddress): Promise<void> {
@@ -84,26 +179,17 @@ export class ContractStore {
84
179
  // Private getters
85
180
 
86
181
  async #getContractClassId(contractAddress: AztecAddress): Promise<Fr | undefined> {
87
- if (!this.#contractClassIdMap.has(contractAddress.toString())) {
182
+ const key = contractAddress.toString();
183
+ if (!this.#contractClassIdMap.has(key)) {
88
184
  const instance = await this.getContractInstance(contractAddress);
89
185
  if (!instance) {
90
186
  return;
91
187
  }
92
- this.#contractClassIdMap.set(contractAddress.toString(), instance.currentContractClassId);
188
+ this.#contractClassIdMap.set(key, instance.currentContractClassId);
93
189
  }
94
- return this.#contractClassIdMap.get(contractAddress.toString());
190
+ return this.#contractClassIdMap.get(key);
95
191
  }
96
192
 
97
- /**
98
- * Retrieve or create a ContractTree instance based on the provided class id.
99
- * If an existing tree with the same class id is found in the cache, it will be returned.
100
- * Otherwise, a new ContractTree instance will be created using the contract data from the database
101
- * and added to the cache before returning.
102
- *
103
- * @param classId - The class id of the contract for which the ContractTree is required.
104
- * @returns A ContractTree instance associated with the specified contract address.
105
- * @throws An Error if the contract is not found in the ContractDatabase.
106
- */
107
193
  async #getPrivateFunctionTreeForClassId(classId: Fr): Promise<PrivateFunctionsTree | undefined> {
108
194
  if (!this.#privateFunctionTrees.has(classId.toString())) {
109
195
  const artifact = await this.getContractArtifact(classId);
@@ -116,34 +202,61 @@ export class ContractStore {
116
202
  return this.#privateFunctionTrees.get(classId.toString())!;
117
203
  }
118
204
 
119
- async #getContractArtifactByAddress(contractAddress: AztecAddress): Promise<ContractArtifact | undefined> {
120
- const contractClassId = await this.#getContractClassId(contractAddress);
121
- 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);
122
208
  }
123
209
 
124
210
  // Public getters
125
211
 
126
- async getContractsAddresses(): Promise<AztecAddress[]> {
127
- const keys = await toArray(this.#contractInstances.keysAsync());
128
- 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
+ });
129
217
  }
130
218
 
131
- /** Returns a contract instance for a given address. Throws if not found. */
132
- public async getContractInstance(contractAddress: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
133
- const contract = await this.#contractInstances.getAsync(contractAddress.toString());
134
- 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
+ });
135
225
  }
136
226
 
227
+ /** Returns the raw contract artifact for a given class id. */
137
228
  public async getContractArtifact(contractClassId: Fr): Promise<ContractArtifact | undefined> {
138
- const contract = await this.#contractArtifacts.getAsync(contractClassId.toString());
139
- // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
140
- 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;
141
242
  }
142
243
 
143
- /** Returns a contract class for a given class id. Throws if not found. */
144
- 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);
145
254
  const artifact = await this.getContractArtifact(contractClassId);
146
- 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 };
147
260
  }
148
261
 
149
262
  public async getContract(
@@ -162,8 +275,6 @@ export class ContractStore {
162
275
 
163
276
  /**
164
277
  * Retrieves the artifact of a specified function within a given contract.
165
- * The function is identified by its selector, which is a unique code generated from the function's signature.
166
- * Throws an error if the contract address or function selector are invalid or not found.
167
278
  *
168
279
  * @param contractAddress - The AztecAddress representing the contract containing the function.
169
280
  * @param selector - The function selector.
@@ -173,9 +284,12 @@ export class ContractStore {
173
284
  contractAddress: AztecAddress,
174
285
  selector: FunctionSelector,
175
286
  ): Promise<FunctionArtifactWithContractName | undefined> {
176
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
177
- const fnArtifact = artifact && (await this.#findFunctionArtifactBySelector(artifact, selector));
178
- 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 };
179
293
  }
180
294
 
181
295
  public async getFunctionArtifactWithDebugMetadata(
@@ -196,50 +310,48 @@ export class ContractStore {
196
310
  public async getPublicFunctionArtifact(
197
311
  contractAddress: AztecAddress,
198
312
  ): Promise<FunctionArtifactWithContractName | undefined> {
199
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
200
- const fnArtifact = artifact && artifact.functions.find(fn => fn.functionType === FunctionType.PUBLIC);
201
- 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 };
202
316
  }
203
317
 
204
318
  public async getFunctionAbi(
205
319
  contractAddress: AztecAddress,
206
320
  selector: FunctionSelector,
207
321
  ): Promise<FunctionAbi | undefined> {
208
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
322
+ const artifact = await this.#getArtifactByAddress(contractAddress);
209
323
  return artifact && (await this.#findFunctionAbiBySelector(artifact, selector));
210
324
  }
211
325
 
212
326
  /**
213
327
  * Retrieves the debug metadata of a specified function within a given contract.
214
- * The function is identified by its selector, which is a unique code generated from the function's signature.
215
- * Returns undefined if the debug metadata for the given function is not found.
216
- * Throws if the contract has not been added to the database.
217
328
  *
218
329
  * @param contractAddress - The AztecAddress representing the contract containing the function.
219
330
  * @param selector - The function selector.
220
- * @returns The corresponding function's artifact as an object.
331
+ * @returns The corresponding function's debug metadata, or undefined.
221
332
  */
222
333
  public async getFunctionDebugMetadata(
223
334
  contractAddress: AztecAddress,
224
335
  selector: FunctionSelector,
225
336
  ): Promise<FunctionDebugMetadata | undefined> {
226
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
227
- const fnArtifact = artifact && (await this.#findFunctionArtifactBySelector(artifact, selector));
228
- 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);
229
343
  }
230
344
 
231
345
  public async getPublicFunctionDebugMetadata(
232
346
  contractAddress: AztecAddress,
233
347
  ): Promise<FunctionDebugMetadata | undefined> {
234
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
235
- const fnArtifact = artifact && artifact.functions.find(fn => fn.functionType === FunctionType.PUBLIC);
236
- 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);
237
351
  }
238
352
 
239
353
  /**
240
354
  * Retrieve the function membership witness for the given contract class and function selector.
241
- * The function membership witness represents a proof that the function belongs to the specified contract.
242
- * Throws an error if the contract address or function selector is unknown.
243
355
  *
244
356
  * @param contractClassId - The id of the class.
245
357
  * @param selector - The function selector.
@@ -254,23 +366,21 @@ export class ContractStore {
254
366
  }
255
367
 
256
368
  public async getDebugContractName(contractAddress: AztecAddress) {
257
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
369
+ const artifact = await this.#getArtifactByAddress(contractAddress);
258
370
  return artifact?.name;
259
371
  }
260
372
 
261
373
  public async getDebugFunctionName(contractAddress: AztecAddress, selector: FunctionSelector) {
262
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
263
- const fnArtifact = artifact && (await this.#findFunctionAbiBySelector(artifact, selector));
264
- 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}`;
265
377
  }
266
378
 
267
379
  async #findFunctionArtifactBySelector(
268
380
  artifact: ContractArtifact,
269
381
  selector: FunctionSelector,
270
382
  ): Promise<FunctionArtifact | undefined> {
271
- const functions = artifact.functions;
272
- for (let i = 0; i < functions.length; i++) {
273
- const fn = functions[i];
383
+ for (const fn of artifact.functions) {
274
384
  const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
275
385
  if (fnSelector.equals(selector)) {
276
386
  return fn;
@@ -282,9 +392,7 @@ export class ContractStore {
282
392
  artifact: ContractArtifact,
283
393
  selector: FunctionSelector,
284
394
  ): Promise<FunctionAbi | undefined> {
285
- const functions = [...artifact.functions, ...(artifact.nonDispatchPublicFunctions ?? [])];
286
- for (let i = 0; i < functions.length; i++) {
287
- const fn = functions[i];
395
+ for (const fn of [...artifact.functions, ...(artifact.nonDispatchPublicFunctions ?? [])]) {
288
396
  const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
289
397
  if (fnSelector.equals(selector)) {
290
398
  return fn;
@@ -305,15 +413,17 @@ export class ContractStore {
305
413
  throw new Error(`Unknown function ${functionName} in contract ${contract.name}.`);
306
414
  }
307
415
 
308
- return {
416
+ const selector = await FunctionSelector.fromNameAndParameters(functionDao.name, functionDao.parameters);
417
+
418
+ return FunctionCall.from({
309
419
  name: functionDao.name,
310
- args: encodeArguments(functionDao, args),
311
- selector: await FunctionSelector.fromNameAndParameters(functionDao.name, functionDao.parameters),
312
- type: functionDao.functionType,
313
420
  to,
421
+ selector,
422
+ type: functionDao.functionType,
314
423
  hideMsgSender: false,
315
424
  isStatic: functionDao.isStatic,
425
+ args: encodeArguments(functionDao, args),
316
426
  returnTypes: functionDao.returnTypes,
317
- };
427
+ });
318
428
  }
319
429
  }
@@ -1 +1 @@
1
- export const PXE_DATA_SCHEMA_VERSION = 2;
1
+ export const PXE_DATA_SCHEMA_VERSION = 3;