@aztec/pxe 0.0.1-commit.96bb3f7 → 0.0.1-commit.a072138

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 (226) hide show
  1. package/dest/bin/check_oracle_version.js +1 -1
  2. package/dest/block_synchronizer/block_synchronizer.d.ts +9 -5
  3. package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
  4. package/dest/block_synchronizer/block_synchronizer.js +66 -21
  5. package/dest/config/index.d.ts +3 -1
  6. package/dest/config/index.d.ts.map +1 -1
  7. package/dest/config/index.js +17 -0
  8. package/dest/contract_function_simulator/benchmarked_node.d.ts +9 -0
  9. package/dest/contract_function_simulator/benchmarked_node.d.ts.map +1 -0
  10. package/dest/contract_function_simulator/benchmarked_node.js +77 -0
  11. package/dest/contract_function_simulator/contract_function_simulator.d.ts +6 -10
  12. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  13. package/dest/contract_function_simulator/contract_function_simulator.js +33 -25
  14. package/dest/contract_function_simulator/execution_note_cache.d.ts +18 -9
  15. package/dest/contract_function_simulator/execution_note_cache.d.ts.map +1 -1
  16. package/dest/contract_function_simulator/execution_note_cache.js +45 -28
  17. package/dest/contract_function_simulator/index.d.ts +2 -2
  18. package/dest/contract_function_simulator/index.d.ts.map +1 -1
  19. package/dest/contract_function_simulator/index.js +1 -1
  20. package/dest/contract_function_simulator/noir-structs/event_validation_request.d.ts +1 -1
  21. package/dest/contract_function_simulator/noir-structs/event_validation_request.js +1 -1
  22. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +1 -1
  23. package/dest/contract_function_simulator/noir-structs/note_validation_request.js +1 -1
  24. package/dest/contract_function_simulator/noir-structs/utility_context.d.ts +4 -10
  25. package/dest/contract_function_simulator/noir-structs/utility_context.d.ts.map +1 -1
  26. package/dest/contract_function_simulator/noir-structs/utility_context.js +7 -18
  27. package/dest/contract_function_simulator/oracle/interfaces.d.ts +15 -11
  28. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  29. package/dest/contract_function_simulator/oracle/message_load_oracle_inputs.d.ts +3 -1
  30. package/dest/contract_function_simulator/oracle/message_load_oracle_inputs.d.ts.map +1 -1
  31. package/dest/contract_function_simulator/oracle/note_packing_utils.d.ts +6 -6
  32. package/dest/contract_function_simulator/oracle/note_packing_utils.d.ts.map +1 -1
  33. package/dest/contract_function_simulator/oracle/note_packing_utils.js +8 -8
  34. package/dest/contract_function_simulator/oracle/oracle.d.ts +10 -8
  35. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  36. package/dest/contract_function_simulator/oracle/oracle.js +44 -33
  37. package/dest/contract_function_simulator/oracle/private_execution.d.ts +2 -26
  38. package/dest/contract_function_simulator/oracle/private_execution.d.ts.map +1 -1
  39. package/dest/contract_function_simulator/oracle/private_execution.js +0 -35
  40. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +11 -3
  41. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
  42. package/dest/contract_function_simulator/oracle/private_execution_oracle.js +22 -10
  43. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +33 -20
  44. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  45. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +78 -76
  46. package/dest/contract_sync/index.d.ts +23 -0
  47. package/dest/contract_sync/index.d.ts.map +1 -0
  48. package/dest/contract_sync/index.js +54 -0
  49. package/dest/debug/pxe_debug_utils.d.ts +16 -6
  50. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  51. package/dest/debug/pxe_debug_utils.js +20 -10
  52. package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
  53. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
  54. package/dest/entrypoints/client/bundle/utils.js +12 -6
  55. package/dest/entrypoints/client/lazy/utils.d.ts +2 -2
  56. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  57. package/dest/entrypoints/client/lazy/utils.js +13 -7
  58. package/dest/entrypoints/pxe_creation_options.d.ts +3 -2
  59. package/dest/entrypoints/pxe_creation_options.d.ts.map +1 -1
  60. package/dest/entrypoints/server/index.d.ts +3 -1
  61. package/dest/entrypoints/server/index.d.ts.map +1 -1
  62. package/dest/entrypoints/server/index.js +2 -0
  63. package/dest/entrypoints/server/utils.d.ts +1 -1
  64. package/dest/entrypoints/server/utils.d.ts.map +1 -1
  65. package/dest/entrypoints/server/utils.js +19 -8
  66. package/dest/events/event_service.d.ts +6 -6
  67. package/dest/events/event_service.d.ts.map +1 -1
  68. package/dest/events/event_service.js +19 -22
  69. package/dest/events/private_event_filter_validator.d.ts +5 -5
  70. package/dest/events/private_event_filter_validator.d.ts.map +1 -1
  71. package/dest/events/private_event_filter_validator.js +5 -6
  72. package/dest/job_coordinator/job_coordinator.d.ts +3 -2
  73. package/dest/job_coordinator/job_coordinator.d.ts.map +1 -1
  74. package/dest/job_coordinator/job_coordinator.js +3 -2
  75. package/dest/logs/log_service.d.ts +6 -4
  76. package/dest/logs/log_service.d.ts.map +1 -1
  77. package/dest/logs/log_service.js +21 -16
  78. package/dest/notes/note_service.d.ts +7 -7
  79. package/dest/notes/note_service.d.ts.map +1 -1
  80. package/dest/notes/note_service.js +31 -36
  81. package/dest/oracle_version.d.ts +3 -3
  82. package/dest/oracle_version.d.ts.map +1 -1
  83. package/dest/oracle_version.js +4 -3
  84. package/dest/private_kernel/hints/index.d.ts +2 -2
  85. package/dest/private_kernel/hints/index.d.ts.map +1 -1
  86. package/dest/private_kernel/hints/index.js +1 -1
  87. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts +28 -0
  88. package/dest/private_kernel/hints/private_kernel_reset_private_inputs_builder.d.ts.map +1 -0
  89. package/dest/private_kernel/hints/{build_private_kernel_reset_private_inputs.js → private_kernel_reset_private_inputs_builder.js} +12 -6
  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 +3 -3
  93. package/dest/private_kernel/private_kernel_oracle.d.ts +23 -28
  94. package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
  95. package/dest/private_kernel/private_kernel_oracle.js +90 -2
  96. package/dest/pxe.d.ts +7 -36
  97. package/dest/pxe.d.ts.map +1 -1
  98. package/dest/pxe.js +43 -83
  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.d.ts +24 -9
  106. package/dest/storage/capsule_store/capsule_store.d.ts.map +1 -1
  107. package/dest/storage/capsule_store/capsule_store.js +130 -23
  108. package/dest/storage/contract_store/contract_store.d.ts +1 -2
  109. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  110. package/dest/storage/contract_store/contract_store.js +22 -25
  111. package/dest/storage/metadata.d.ts +1 -1
  112. package/dest/storage/metadata.js +1 -1
  113. package/dest/storage/note_store/note_store.d.ts +51 -52
  114. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  115. package/dest/storage/note_store/note_store.js +282 -263
  116. package/dest/storage/note_store/stored_note.d.ts +16 -0
  117. package/dest/storage/note_store/stored_note.d.ts.map +1 -0
  118. package/dest/storage/note_store/stored_note.js +43 -0
  119. package/dest/storage/private_event_store/private_event_store.d.ts +43 -8
  120. package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
  121. package/dest/storage/private_event_store/private_event_store.js +226 -111
  122. package/dest/storage/private_event_store/stored_private_event.d.ts +23 -0
  123. package/dest/storage/private_event_store/stored_private_event.d.ts.map +1 -0
  124. package/dest/storage/private_event_store/stored_private_event.js +56 -0
  125. package/dest/storage/tagging_store/recipient_tagging_store.d.ts +15 -8
  126. package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
  127. package/dest/storage/tagging_store/recipient_tagging_store.js +88 -19
  128. package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
  129. package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
  130. package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
  131. package/dest/storage/tagging_store/sender_tagging_store.d.ts +19 -9
  132. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  133. package/dest/storage/tagging_store/sender_tagging_store.js +252 -100
  134. package/dest/tagging/get_all_logs_by_tags.d.ts +24 -0
  135. package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -0
  136. package/dest/tagging/get_all_logs_by_tags.js +46 -0
  137. package/dest/tagging/index.d.ts +2 -1
  138. package/dest/tagging/index.d.ts.map +1 -1
  139. package/dest/tagging/index.js +1 -0
  140. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +3 -2
  141. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
  142. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.js +6 -6
  143. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +3 -2
  144. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
  145. package/dest/tagging/recipient_sync/utils/load_logs_for_range.js +5 -2
  146. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +3 -2
  147. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
  148. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +7 -7
  149. package/dest/tagging/sender_sync/utils/get_status_change_of_pending.d.ts +1 -1
  150. package/dest/tagging/sender_sync/utils/get_status_change_of_pending.d.ts.map +1 -1
  151. package/dest/tagging/sender_sync/utils/get_status_change_of_pending.js +5 -8
  152. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +5 -2
  153. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
  154. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.js +10 -5
  155. package/package.json +18 -18
  156. package/src/bin/check_oracle_version.ts +1 -0
  157. package/src/block_synchronizer/block_synchronizer.ts +85 -33
  158. package/src/config/index.ts +14 -0
  159. package/src/contract_function_simulator/benchmarked_node.ts +103 -0
  160. package/src/contract_function_simulator/contract_function_simulator.ts +34 -28
  161. package/src/contract_function_simulator/execution_note_cache.ts +44 -25
  162. package/src/contract_function_simulator/index.ts +1 -1
  163. package/src/contract_function_simulator/noir-structs/event_validation_request.ts +1 -1
  164. package/src/contract_function_simulator/noir-structs/note_validation_request.ts +1 -1
  165. package/src/contract_function_simulator/noir-structs/utility_context.ts +6 -25
  166. package/src/contract_function_simulator/oracle/interfaces.ts +20 -10
  167. package/src/contract_function_simulator/oracle/note_packing_utils.ts +10 -10
  168. package/src/contract_function_simulator/oracle/oracle.ts +65 -40
  169. package/src/contract_function_simulator/oracle/private_execution.ts +1 -67
  170. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +35 -12
  171. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +100 -82
  172. package/src/contract_sync/index.ts +98 -0
  173. package/src/debug/pxe_debug_utils.ts +26 -11
  174. package/src/entrypoints/client/bundle/utils.ts +7 -14
  175. package/src/entrypoints/client/lazy/utils.ts +8 -14
  176. package/src/entrypoints/pxe_creation_options.ts +2 -1
  177. package/src/entrypoints/server/index.ts +2 -0
  178. package/src/entrypoints/server/utils.ts +15 -19
  179. package/src/events/event_service.ts +17 -25
  180. package/src/events/private_event_filter_validator.ts +3 -5
  181. package/src/job_coordinator/job_coordinator.ts +4 -3
  182. package/src/logs/log_service.ts +32 -14
  183. package/src/notes/note_service.ts +38 -43
  184. package/src/oracle_version.ts +4 -3
  185. package/src/private_kernel/hints/index.ts +1 -1
  186. package/src/private_kernel/hints/{build_private_kernel_reset_private_inputs.ts → private_kernel_reset_private_inputs_builder.ts} +32 -20
  187. package/src/private_kernel/private_kernel_execution_prover.ts +7 -4
  188. package/src/private_kernel/private_kernel_oracle.ts +116 -37
  189. package/src/pxe.ts +77 -118
  190. package/src/storage/address_store/address_store.ts +15 -15
  191. package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
  192. package/src/storage/capsule_store/capsule_store.ts +159 -23
  193. package/src/storage/contract_store/contract_store.ts +22 -31
  194. package/src/storage/metadata.ts +1 -1
  195. package/src/storage/note_store/note_store.ts +317 -318
  196. package/src/storage/note_store/stored_note.ts +48 -0
  197. package/src/storage/private_event_store/private_event_store.ts +292 -124
  198. package/src/storage/private_event_store/stored_private_event.ts +73 -0
  199. package/src/storage/tagging_store/recipient_tagging_store.ts +106 -20
  200. package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
  201. package/src/storage/tagging_store/sender_tagging_store.ts +293 -108
  202. package/src/tagging/get_all_logs_by_tags.ts +68 -0
  203. package/src/tagging/index.ts +1 -0
  204. package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +16 -5
  205. package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +7 -1
  206. package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +9 -6
  207. package/src/tagging/sender_sync/utils/get_status_change_of_pending.ts +5 -17
  208. package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +16 -4
  209. package/dest/contract_function_simulator/proxied_node.d.ts +0 -9
  210. package/dest/contract_function_simulator/proxied_node.d.ts.map +0 -1
  211. package/dest/contract_function_simulator/proxied_node.js +0 -27
  212. package/dest/private_kernel/hints/build_private_kernel_reset_private_inputs.d.ts +0 -28
  213. package/dest/private_kernel/hints/build_private_kernel_reset_private_inputs.d.ts.map +0 -1
  214. package/dest/private_kernel/private_kernel_oracle_impl.d.ts +0 -46
  215. package/dest/private_kernel/private_kernel_oracle_impl.d.ts.map +0 -1
  216. package/dest/private_kernel/private_kernel_oracle_impl.js +0 -85
  217. package/dest/public_storage/public_storage_service.d.ts +0 -24
  218. package/dest/public_storage/public_storage_service.d.ts.map +0 -1
  219. package/dest/public_storage/public_storage_service.js +0 -26
  220. package/dest/tree_membership/tree_membership_service.d.ts +0 -52
  221. package/dest/tree_membership/tree_membership_service.d.ts.map +0 -1
  222. package/dest/tree_membership/tree_membership_service.js +0 -84
  223. package/src/contract_function_simulator/proxied_node.ts +0 -33
  224. package/src/private_kernel/private_kernel_oracle_impl.ts +0 -127
  225. package/src/public_storage/public_storage_service.ts +0 -33
  226. package/src/tree_membership/tree_membership_service.ts +0 -112
@@ -43,23 +43,23 @@ export class AddressStore {
43
43
  });
44
44
  }
45
45
 
46
- async #getCompleteAddress(address: AztecAddress): Promise<CompleteAddress | undefined> {
47
- const index = await this.#completeAddressIndex.getAsync(address.toString());
48
- if (index === undefined) {
49
- return undefined;
50
- }
51
-
52
- const value = await this.#completeAddresses.atAsync(index);
53
- return value ? await CompleteAddress.fromBuffer(value) : undefined;
54
- }
55
-
56
46
  getCompleteAddress(account: AztecAddress): Promise<CompleteAddress | undefined> {
57
- return this.#getCompleteAddress(account);
47
+ return this.#store.transactionAsync(async () => {
48
+ const index = await this.#completeAddressIndex.getAsync(account.toString());
49
+ if (index === undefined) {
50
+ return undefined;
51
+ }
52
+
53
+ const value = await this.#completeAddresses.atAsync(index);
54
+ return value ? await CompleteAddress.fromBuffer(value) : undefined;
55
+ });
58
56
  }
59
57
 
60
- async getCompleteAddresses(): Promise<CompleteAddress[]> {
61
- return await Promise.all(
62
- (await toArray(this.#completeAddresses.valuesAsync())).map(v => CompleteAddress.fromBuffer(v)),
63
- );
58
+ getCompleteAddresses(): Promise<CompleteAddress[]> {
59
+ return this.#store.transactionAsync(async () => {
60
+ return await Promise.all(
61
+ (await toArray(this.#completeAddresses.valuesAsync())).map(v => CompleteAddress.fromBuffer(v)),
62
+ );
63
+ });
64
64
  }
65
65
  }
@@ -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
  }
@@ -3,12 +3,21 @@ import { type Logger, createLogger } from '@aztec/foundation/log';
3
3
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
4
4
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
5
 
6
- export class CapsuleStore {
6
+ import type { StagedStore } from '../../job_coordinator/job_coordinator.js';
7
+
8
+ export class CapsuleStore implements StagedStore {
9
+ readonly storeName = 'capsule';
10
+
7
11
  #store: AztecAsyncKVStore;
8
12
 
9
13
  // Arbitrary data stored by contracts. Key is computed as `${contractAddress}:${key}`
10
14
  #capsules: AztecAsyncMap<string, Buffer>;
11
15
 
16
+ // jobId => `${contractAddress}:${key}` => capsule data
17
+ // when `#stagedCapsules.get('some-job-id').get('${some-contract-address:some-key') === null`,
18
+ // it signals that the capsule was deleted during the job, so it needs to be deleted on commit
19
+ #stagedCapsules: Map<string, Map<string, Buffer | null>>;
20
+
12
21
  logger: Logger;
13
22
 
14
23
  constructor(store: AztecAsyncKVStore) {
@@ -16,21 +25,120 @@ export class CapsuleStore {
16
25
 
17
26
  this.#capsules = this.#store.openMap('capsules');
18
27
 
28
+ this.#stagedCapsules = new Map();
29
+
19
30
  this.logger = createLogger('pxe:capsule-data-provider');
20
31
  }
21
32
 
33
+ /**
34
+ * Given a job denoted by `jobId`, it returns the
35
+ * capsules that said job has interacted with.
36
+ *
37
+ * Capsules that haven't been committed to persistence KV storage
38
+ * are kept in-memory in `#stagedCapsules`, this method provides a convenient
39
+ * way to access that in-memory collection of data.
40
+ *
41
+ * @param jobId
42
+ * @returns
43
+ */
44
+ #getJobStagedCapsules(jobId: string): Map<string, Buffer | null> {
45
+ let jobStagedCapsules = this.#stagedCapsules.get(jobId);
46
+ if (!jobStagedCapsules) {
47
+ jobStagedCapsules = new Map();
48
+ this.#stagedCapsules.set(jobId, jobStagedCapsules);
49
+ }
50
+ return jobStagedCapsules;
51
+ }
52
+
53
+ /**
54
+ * Reads a capsule's slot from the staged version of the data associated to the given jobId.
55
+ *
56
+ * If it is not there, it reads it from the KV store.
57
+ */
58
+ async #getFromStage(jobId: string, dbSlotKey: string): Promise<Buffer | null | undefined> {
59
+ const jobStagedCapsules = this.#getJobStagedCapsules(jobId);
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
+ }
69
+
70
+ /**
71
+ * Writes a capsule to the stage of a job.
72
+ */
73
+ #setOnStage(jobId: string, dbSlotKey: string, capsuleData: Buffer) {
74
+ this.#getJobStagedCapsules(jobId).set(dbSlotKey, capsuleData);
75
+ }
76
+
77
+ /**
78
+ * Deletes a capsule on the stage of a job. Note the capsule will still
79
+ * exist in storage until the job is committed.
80
+ */
81
+ #deleteOnStage(jobId: string, dbSlotKey: string) {
82
+ this.#getJobStagedCapsules(jobId).set(dbSlotKey, null);
83
+ }
84
+
85
+ async #loadCapsuleFromDb(dbSlotKey: string): Promise<Buffer | null> {
86
+ const dataBuffer = await this.#capsules.getAsync(dbSlotKey);
87
+ if (!dataBuffer) {
88
+ return null;
89
+ }
90
+
91
+ return dataBuffer;
92
+ }
93
+
94
+ /**
95
+ * Commits staged data to main storage.
96
+ * Called by JobCoordinator when a job completes successfully.
97
+ * Note: JobCoordinator wraps all commits in a single transaction, so we don't
98
+ * need our own transactionAsync here (and using one would deadlock on IndexedDB).
99
+ * @param jobId - The jobId identifying which staged data to commit
100
+ */
101
+ async commit(jobId: string): Promise<void> {
102
+ const jobStagedCapsules = this.#getJobStagedCapsules(jobId);
103
+
104
+ for (const [key, value] of jobStagedCapsules) {
105
+ // In the write stage, we represent deleted capsules with null
106
+ // (as opposed to undefined, which denotes there was never a capsule there to begin with).
107
+ // So we delete from actual KV store here.
108
+ if (value === null) {
109
+ await this.#capsules.delete(key);
110
+ } else {
111
+ await this.#capsules.set(key, value);
112
+ }
113
+ }
114
+
115
+ this.#stagedCapsules.delete(jobId);
116
+ }
117
+
118
+ /**
119
+ * Discards staged data without committing.
120
+ */
121
+ discardStaged(jobId: string): Promise<void> {
122
+ this.#stagedCapsules.delete(jobId);
123
+ return Promise.resolve();
124
+ }
125
+
22
126
  /**
23
127
  * Stores arbitrary information in a per-contract non-volatile database, which can later be retrieved with `loadCapsule`.
24
128
  * * If data was already stored at this slot, it is overwritten.
25
129
  * @param contractAddress - The contract address to scope the data under.
26
130
  * @param slot - The slot in the database in which to store the value. Slots need not be contiguous.
27
131
  * @param capsule - An array of field elements representing the capsule.
132
+ * @param jobId - The context in which this store will be visible until PXE decides to persist it to underlying KV store
28
133
  * @remarks A capsule is a "blob" of data that is passed to the contract through an oracle. It works similarly
29
134
  * to public contract storage in that it's indexed by the contract address and storage slot but instead of the global
30
135
  * network state it's backed by local PXE db.
31
136
  */
32
- async storeCapsule(contractAddress: AztecAddress, slot: Fr, capsule: Fr[]): Promise<void> {
33
- await this.#capsules.set(dbSlotToKey(contractAddress, slot), Buffer.concat(capsule.map(value => value.toBuffer())));
137
+ storeCapsule(contractAddress: AztecAddress, slot: Fr, capsule: Fr[], jobId: string) {
138
+ const dbSlotKey = dbSlotToKey(contractAddress, slot);
139
+
140
+ // A store overrides any pre-existing data on the slot
141
+ this.#setOnStage(jobId, dbSlotKey, Buffer.concat(capsule.map(value => value.toBuffer())));
34
142
  }
35
143
 
36
144
  /**
@@ -39,8 +147,8 @@ export class CapsuleStore {
39
147
  * @param slot - The slot in the database to read.
40
148
  * @returns The stored data or `null` if no data is stored under the slot.
41
149
  */
42
- async loadCapsule(contractAddress: AztecAddress, slot: Fr): Promise<Fr[] | null> {
43
- const dataBuffer = await this.#capsules.getAsync(dbSlotToKey(contractAddress, slot));
150
+ async loadCapsule(contractAddress: AztecAddress, slot: Fr, jobId: string): Promise<Fr[] | null> {
151
+ const dataBuffer = await this.#getFromStage(jobId, dbSlotToKey(contractAddress, slot));
44
152
  if (!dataBuffer) {
45
153
  this.logger.trace(`Data not found for contract ${contractAddress.toString()} and slot ${slot.toString()}`);
46
154
  return null;
@@ -57,8 +165,9 @@ export class CapsuleStore {
57
165
  * @param contractAddress - The contract address under which the data is scoped.
58
166
  * @param slot - The slot in the database to delete.
59
167
  */
60
- async deleteCapsule(contractAddress: AztecAddress, slot: Fr): Promise<void> {
61
- await this.#capsules.delete(dbSlotToKey(contractAddress, slot));
168
+ deleteCapsule(contractAddress: AztecAddress, slot: Fr, jobId: string) {
169
+ // When we commit this, we will interpret null as a deletion, so we'll propagate the delete to the KV store
170
+ this.#deleteOnStage(jobId, dbSlotToKey(contractAddress, slot));
62
171
  }
63
172
 
64
173
  /**
@@ -72,13 +181,22 @@ export class CapsuleStore {
72
181
  * @param dstSlot - The first slot to copy to.
73
182
  * @param numEntries - The number of entries to copy.
74
183
  */
75
- copyCapsule(contractAddress: AztecAddress, srcSlot: Fr, dstSlot: Fr, numEntries: number): Promise<void> {
184
+ copyCapsule(
185
+ contractAddress: AztecAddress,
186
+ srcSlot: Fr,
187
+ dstSlot: Fr,
188
+ numEntries: number,
189
+ jobId: string,
190
+ ): Promise<void> {
191
+ // This transactional context gives us "copy atomicity":
192
+ // there shouldn't be concurrent writes to what's being copied here.
193
+ // Equally important: this in practice is expected to perform thousands of DB operations
194
+ // and not using a transaction here would heavily impact performance.
76
195
  return this.#store.transactionAsync(async () => {
77
196
  // In order to support overlapping source and destination regions, we need to check the relative positions of source
78
197
  // and destination. If destination is ahead of source, then by the time we overwrite source elements using forward
79
198
  // indexes we'll have already read those. On the contrary, if source is ahead of destination we need to use backward
80
199
  // indexes to avoid reading elements that've been overwritten.
81
-
82
200
  const indexes = Array.from(Array(numEntries).keys());
83
201
  if (srcSlot.lt(dstSlot)) {
84
202
  indexes.reverse();
@@ -88,12 +206,12 @@ export class CapsuleStore {
88
206
  const currentSrcSlot = dbSlotToKey(contractAddress, srcSlot.add(new Fr(i)));
89
207
  const currentDstSlot = dbSlotToKey(contractAddress, dstSlot.add(new Fr(i)));
90
208
 
91
- const toCopy = await this.#capsules.getAsync(currentSrcSlot);
209
+ const toCopy = await this.#getFromStage(jobId, currentSrcSlot);
92
210
  if (!toCopy) {
93
211
  throw new Error(`Attempted to copy empty slot ${currentSrcSlot} for contract ${contractAddress.toString()}`);
94
212
  }
95
213
 
96
- await this.#capsules.set(currentDstSlot, toCopy);
214
+ this.#setOnStage(jobId, currentDstSlot, toCopy);
97
215
  }
98
216
  });
99
217
  }
@@ -106,35 +224,45 @@ export class CapsuleStore {
106
224
  * @param baseSlot - The slot where the array length is stored
107
225
  * @param content - Array of capsule data to append
108
226
  */
109
- appendToCapsuleArray(contractAddress: AztecAddress, baseSlot: Fr, content: Fr[][]): Promise<void> {
227
+ appendToCapsuleArray(contractAddress: AztecAddress, baseSlot: Fr, content: Fr[][], jobId: string): Promise<void> {
228
+ // We wrap this in a transaction to serialize concurrent calls from Promise.all.
229
+ // Without this, concurrent appends to the same array could race: both read length=0,
230
+ // both write at the same slots, one overwrites the other.
231
+ // Equally important: this in practice is expected to perform thousands of DB operations
232
+ // and not using a transaction here would heavily impact performance.
110
233
  return this.#store.transactionAsync(async () => {
111
234
  // Load current length, defaulting to 0 if not found
112
- const lengthData = await this.loadCapsule(contractAddress, baseSlot);
235
+ const lengthData = await this.loadCapsule(contractAddress, baseSlot, jobId);
113
236
  const currentLength = lengthData ? lengthData[0].toNumber() : 0;
114
237
 
115
238
  // Store each capsule at consecutive slots after baseSlot + 1 + currentLength
116
239
  for (let i = 0; i < content.length; i++) {
117
240
  const nextSlot = arraySlot(baseSlot, currentLength + i);
118
- await this.storeCapsule(contractAddress, nextSlot, content[i]);
241
+ this.storeCapsule(contractAddress, nextSlot, content[i], jobId);
119
242
  }
120
243
 
121
244
  // Update length to include all new capsules
122
245
  const newLength = currentLength + content.length;
123
- await this.storeCapsule(contractAddress, baseSlot, [new Fr(newLength)]);
246
+ this.storeCapsule(contractAddress, baseSlot, [new Fr(newLength)], jobId);
124
247
  });
125
248
  }
126
249
 
127
- readCapsuleArray(contractAddress: AztecAddress, baseSlot: Fr): Promise<Fr[][]> {
250
+ readCapsuleArray(contractAddress: AztecAddress, baseSlot: Fr, jobId: string): Promise<Fr[][]> {
251
+ // I'm leaving this transactional context here though because I'm assuming this
252
+ // gives us "read array atomicity": there shouldn't be concurrent writes to what's being copied
253
+ // here.
254
+ // This is one point we should revisit in the future if we want to relax the concurrency
255
+ // of jobs: different calls running concurrently on the same contract may cause trouble.
128
256
  return this.#store.transactionAsync(async () => {
129
257
  // Load length, defaulting to 0 if not found
130
- const maybeLength = await this.loadCapsule(contractAddress, baseSlot);
258
+ const maybeLength = await this.loadCapsule(contractAddress, baseSlot, jobId);
131
259
  const length = maybeLength ? maybeLength[0].toBigInt() : 0n;
132
260
 
133
261
  const values: Fr[][] = [];
134
262
 
135
263
  // Read each capsule at consecutive slots after baseSlot
136
264
  for (let i = 0; i < length; i++) {
137
- const currentValue = await this.loadCapsule(contractAddress, arraySlot(baseSlot, i));
265
+ const currentValue = await this.loadCapsule(contractAddress, arraySlot(baseSlot, i), jobId);
138
266
  if (currentValue == undefined) {
139
267
  throw new Error(
140
268
  `Expected non-empty value at capsule array in base slot ${baseSlot} at index ${i} for contract ${contractAddress}`,
@@ -148,23 +276,31 @@ export class CapsuleStore {
148
276
  });
149
277
  }
150
278
 
151
- setCapsuleArray(contractAddress: AztecAddress, baseSlot: Fr, content: Fr[][]) {
279
+ setCapsuleArray(contractAddress: AztecAddress, baseSlot: Fr, content: Fr[][], jobId: string) {
280
+ // This transactional context in theory isn't so critical now because we aren't
281
+ // writing to DB so if there's exceptions midway and it blows up, no visible impact
282
+ // to persistent storage will happen.
283
+ // I'm leaving this transactional context here though because I'm assuming this
284
+ // gives us "write array atomicity": there shouldn't be concurrent writes to what's being copied
285
+ // here.
286
+ // This is one point we should revisit in the future if we want to relax the concurrency
287
+ // of jobs: different calls running concurrently on the same contract may cause trouble.
152
288
  return this.#store.transactionAsync(async () => {
153
289
  // Load current length, defaulting to 0 if not found
154
- const maybeLength = await this.loadCapsule(contractAddress, baseSlot);
290
+ const maybeLength = await this.loadCapsule(contractAddress, baseSlot, jobId);
155
291
  const originalLength = maybeLength ? maybeLength[0].toNumber() : 0;
156
292
 
157
293
  // Set the new length
158
- await this.storeCapsule(contractAddress, baseSlot, [new Fr(content.length)]);
294
+ this.storeCapsule(contractAddress, baseSlot, [new Fr(content.length)], jobId);
159
295
 
160
296
  // Store the new content, possibly overwriting existing values
161
297
  for (let i = 0; i < content.length; i++) {
162
- await this.storeCapsule(contractAddress, arraySlot(baseSlot, i), content[i]);
298
+ this.storeCapsule(contractAddress, arraySlot(baseSlot, i), content[i], jobId);
163
299
  }
164
300
 
165
301
  // Clear any stragglers
166
302
  for (let i = content.length; i < originalLength; i++) {
167
- await this.deleteCapsule(contractAddress, arraySlot(baseSlot, i));
303
+ this.deleteCapsule(contractAddress, arraySlot(baseSlot, i), jobId);
168
304
  }
169
305
  });
170
306
  }
@@ -3,7 +3,6 @@ import type { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { toArray } from '@aztec/foundation/iterable';
4
4
  import type { MembershipWitness } from '@aztec/foundation/trees';
5
5
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
6
- import { isProtocolContract } from '@aztec/protocol-contracts';
7
6
  import {
8
7
  type ContractArtifact,
9
8
  type FunctionAbi,
@@ -43,10 +42,12 @@ export class ContractStore {
43
42
  /** Map from contract address to contract class id */
44
43
  #contractClassIdMap: Map<string, Fr> = new Map();
45
44
 
45
+ #store: AztecAsyncKVStore;
46
46
  #contractArtifacts: AztecAsyncMap<string, Buffer>;
47
47
  #contractInstances: AztecAsyncMap<string, Buffer>;
48
48
 
49
49
  constructor(store: AztecAsyncKVStore) {
50
+ this.#store = store;
50
51
  this.#contractArtifacts = store.openMap('contract_artifacts');
51
52
  this.#contractInstances = store.openMap('contracts_instances');
52
53
  }
@@ -54,6 +55,7 @@ export class ContractStore {
54
55
  // Setters
55
56
 
56
57
  public async addContractArtifact(id: Fr, contract: ContractArtifact): Promise<void> {
58
+ // Validation outside transactionAsync - these are not DB operations
57
59
  const privateFunctions = contract.functions.filter(
58
60
  functionArtifact => functionArtifact.functionType === FunctionType.PRIVATE,
59
61
  );
@@ -70,7 +72,9 @@ export class ContractStore {
70
72
  throw new Error('Repeated function selectors of private functions');
71
73
  }
72
74
 
73
- await this.#contractArtifacts.set(id.toString(), contractArtifactToBuffer(contract));
75
+ await this.#store.transactionAsync(() =>
76
+ this.#contractArtifacts.set(id.toString(), contractArtifactToBuffer(contract)),
77
+ );
74
78
  }
75
79
 
76
80
  async addContractInstance(contract: ContractInstanceWithAddress): Promise<void> {
@@ -124,21 +128,27 @@ export class ContractStore {
124
128
 
125
129
  // Public getters
126
130
 
127
- async getContractsAddresses(): Promise<AztecAddress[]> {
128
- const keys = await toArray(this.#contractInstances.keysAsync());
129
- return keys.map(AztecAddress.fromString);
131
+ getContractsAddresses(): Promise<AztecAddress[]> {
132
+ return this.#store.transactionAsync(async () => {
133
+ const keys = await toArray(this.#contractInstances.keysAsync());
134
+ return keys.map(AztecAddress.fromString);
135
+ });
130
136
  }
131
137
 
132
138
  /** 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);
139
+ public getContractInstance(contractAddress: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
140
+ return this.#store.transactionAsync(async () => {
141
+ const contract = await this.#contractInstances.getAsync(contractAddress.toString());
142
+ return contract && SerializableContractInstance.fromBuffer(contract).withAddress(contractAddress);
143
+ });
136
144
  }
137
145
 
138
- 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));
146
+ public getContractArtifact(contractClassId: Fr): Promise<ContractArtifact | undefined> {
147
+ return this.#store.transactionAsync(async () => {
148
+ const contract = await this.#contractArtifacts.getAsync(contractClassId.toString());
149
+ // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
150
+ return contract && contractArtifactFromBuffer(Buffer.from(contract));
151
+ });
142
152
  }
143
153
 
144
154
  /** Returns a contract class for a given class id. Throws if not found. */
@@ -317,23 +327,4 @@ export class ContractStore {
317
327
  returnTypes: functionDao.returnTypes,
318
328
  };
319
329
  }
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
- }
338
- }
339
330
  }
@@ -1 +1 @@
1
- export const PXE_DATA_SCHEMA_VERSION = 2;
1
+ export const PXE_DATA_SCHEMA_VERSION = 3;