@aztec/pxe 0.0.1-commit.2ed92850 → 0.0.1-commit.3d8f95d

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 (144) hide show
  1. package/dest/block_synchronizer/block_synchronizer.d.ts +5 -3
  2. package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
  3. package/dest/block_synchronizer/block_synchronizer.js +11 -5
  4. package/dest/contract_function_simulator/contract_function_simulator.d.ts +50 -28
  5. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  6. package/dest/contract_function_simulator/contract_function_simulator.js +70 -36
  7. package/dest/contract_function_simulator/oracle/interfaces.d.ts +10 -10
  8. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  9. package/dest/contract_function_simulator/oracle/oracle.d.ts +5 -5
  10. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  11. package/dest/contract_function_simulator/oracle/oracle.js +38 -26
  12. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +35 -36
  13. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
  14. package/dest/contract_function_simulator/oracle/private_execution_oracle.js +66 -21
  15. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +52 -29
  16. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  17. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +80 -61
  18. package/dest/contract_sync/contract_sync_service.d.ts +41 -0
  19. package/dest/contract_sync/contract_sync_service.d.ts.map +1 -0
  20. package/dest/contract_sync/contract_sync_service.js +82 -0
  21. package/dest/contract_sync/helpers.d.ts +28 -0
  22. package/dest/contract_sync/helpers.d.ts.map +1 -0
  23. package/dest/contract_sync/{index.js → helpers.js} +13 -12
  24. package/dest/debug/pxe_debug_utils.d.ts +22 -9
  25. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  26. package/dest/debug/pxe_debug_utils.js +28 -18
  27. package/dest/entrypoints/client/bundle/utils.d.ts +1 -1
  28. package/dest/entrypoints/client/bundle/utils.d.ts.map +1 -1
  29. package/dest/entrypoints/client/bundle/utils.js +21 -7
  30. package/dest/entrypoints/client/lazy/utils.d.ts +2 -2
  31. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  32. package/dest/entrypoints/client/lazy/utils.js +22 -8
  33. package/dest/entrypoints/pxe_creation_options.d.ts +3 -2
  34. package/dest/entrypoints/pxe_creation_options.d.ts.map +1 -1
  35. package/dest/entrypoints/server/index.d.ts +2 -2
  36. package/dest/entrypoints/server/index.d.ts.map +1 -1
  37. package/dest/entrypoints/server/index.js +1 -1
  38. package/dest/entrypoints/server/utils.d.ts +1 -1
  39. package/dest/entrypoints/server/utils.d.ts.map +1 -1
  40. package/dest/entrypoints/server/utils.js +28 -9
  41. package/dest/events/event_service.d.ts +4 -5
  42. package/dest/events/event_service.d.ts.map +1 -1
  43. package/dest/events/event_service.js +5 -6
  44. package/dest/job_coordinator/job_coordinator.d.ts +3 -2
  45. package/dest/job_coordinator/job_coordinator.d.ts.map +1 -1
  46. package/dest/job_coordinator/job_coordinator.js +3 -2
  47. package/dest/logs/log_service.d.ts +6 -5
  48. package/dest/logs/log_service.d.ts.map +1 -1
  49. package/dest/logs/log_service.js +10 -15
  50. package/dest/notes/note_service.d.ts +4 -5
  51. package/dest/notes/note_service.d.ts.map +1 -1
  52. package/dest/notes/note_service.js +6 -7
  53. package/dest/oracle_version.d.ts +3 -3
  54. package/dest/oracle_version.d.ts.map +1 -1
  55. package/dest/oracle_version.js +2 -2
  56. package/dest/private_kernel/private_kernel_execution_prover.d.ts +3 -2
  57. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  58. package/dest/private_kernel/private_kernel_execution_prover.js +2 -2
  59. package/dest/private_kernel/private_kernel_oracle.d.ts +3 -3
  60. package/dest/private_kernel/private_kernel_oracle.d.ts.map +1 -1
  61. package/dest/pxe.d.ts +64 -22
  62. package/dest/pxe.d.ts.map +1 -1
  63. package/dest/pxe.js +66 -39
  64. package/dest/storage/address_store/address_store.d.ts +1 -1
  65. package/dest/storage/address_store/address_store.d.ts.map +1 -1
  66. package/dest/storage/address_store/address_store.js +12 -11
  67. package/dest/storage/anchor_block_store/anchor_block_store.d.ts +9 -1
  68. package/dest/storage/anchor_block_store/anchor_block_store.d.ts.map +1 -1
  69. package/dest/storage/anchor_block_store/anchor_block_store.js +8 -1
  70. package/dest/storage/capsule_store/capsule_store.js +6 -8
  71. package/dest/storage/contract_store/contract_store.d.ts +1 -1
  72. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  73. package/dest/storage/contract_store/contract_store.js +27 -18
  74. package/dest/storage/metadata.d.ts +1 -1
  75. package/dest/storage/metadata.js +1 -1
  76. package/dest/storage/note_store/note_store.d.ts +11 -1
  77. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  78. package/dest/storage/note_store/note_store.js +146 -105
  79. package/dest/storage/private_event_store/private_event_store.d.ts +1 -1
  80. package/dest/storage/private_event_store/private_event_store.d.ts.map +1 -1
  81. package/dest/storage/private_event_store/private_event_store.js +84 -61
  82. package/dest/storage/private_event_store/stored_private_event.d.ts +4 -4
  83. package/dest/storage/private_event_store/stored_private_event.d.ts.map +1 -1
  84. package/dest/storage/private_event_store/stored_private_event.js +2 -2
  85. package/dest/storage/tagging_store/recipient_tagging_store.d.ts +1 -1
  86. package/dest/storage/tagging_store/recipient_tagging_store.d.ts.map +1 -1
  87. package/dest/storage/tagging_store/recipient_tagging_store.js +31 -19
  88. package/dest/storage/tagging_store/sender_address_book_store.d.ts +1 -1
  89. package/dest/storage/tagging_store/sender_address_book_store.d.ts.map +1 -1
  90. package/dest/storage/tagging_store/sender_address_book_store.js +20 -14
  91. package/dest/storage/tagging_store/sender_tagging_store.d.ts +1 -1
  92. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  93. package/dest/storage/tagging_store/sender_tagging_store.js +183 -113
  94. package/dest/tagging/get_all_logs_by_tags.d.ts +4 -4
  95. package/dest/tagging/get_all_logs_by_tags.d.ts.map +1 -1
  96. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts +3 -3
  97. package/dest/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.d.ts.map +1 -1
  98. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts +3 -3
  99. package/dest/tagging/recipient_sync/utils/load_logs_for_range.d.ts.map +1 -1
  100. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +3 -3
  101. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
  102. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +3 -3
  103. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
  104. package/package.json +25 -16
  105. package/src/block_synchronizer/block_synchronizer.ts +23 -19
  106. package/src/contract_function_simulator/contract_function_simulator.ts +115 -77
  107. package/src/contract_function_simulator/oracle/interfaces.ts +12 -12
  108. package/src/contract_function_simulator/oracle/oracle.ts +41 -24
  109. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +86 -101
  110. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +131 -69
  111. package/src/contract_sync/contract_sync_service.ts +129 -0
  112. package/src/contract_sync/{index.ts → helpers.ts} +15 -20
  113. package/src/debug/pxe_debug_utils.ts +60 -18
  114. package/src/entrypoints/client/bundle/utils.ts +16 -15
  115. package/src/entrypoints/client/lazy/utils.ts +17 -15
  116. package/src/entrypoints/pxe_creation_options.ts +2 -1
  117. package/src/entrypoints/server/index.ts +1 -1
  118. package/src/entrypoints/server/utils.ts +22 -26
  119. package/src/events/event_service.ts +4 -6
  120. package/src/job_coordinator/job_coordinator.ts +4 -3
  121. package/src/logs/log_service.ts +14 -14
  122. package/src/notes/note_service.ts +5 -7
  123. package/src/oracle_version.ts +2 -2
  124. package/src/private_kernel/private_kernel_execution_prover.ts +6 -3
  125. package/src/private_kernel/private_kernel_oracle.ts +2 -2
  126. package/src/pxe.ts +140 -74
  127. package/src/storage/address_store/address_store.ts +15 -15
  128. package/src/storage/anchor_block_store/anchor_block_store.ts +8 -0
  129. package/src/storage/capsule_store/capsule_store.ts +8 -8
  130. package/src/storage/contract_store/contract_store.ts +26 -15
  131. package/src/storage/metadata.ts +1 -1
  132. package/src/storage/note_store/note_store.ts +163 -129
  133. package/src/storage/private_event_store/private_event_store.ts +102 -81
  134. package/src/storage/private_event_store/stored_private_event.ts +3 -3
  135. package/src/storage/tagging_store/recipient_tagging_store.ts +31 -21
  136. package/src/storage/tagging_store/sender_address_book_store.ts +20 -14
  137. package/src/storage/tagging_store/sender_tagging_store.ts +210 -126
  138. package/src/tagging/get_all_logs_by_tags.ts +3 -3
  139. package/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts +2 -2
  140. package/src/tagging/recipient_sync/utils/load_logs_for_range.ts +2 -2
  141. package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +2 -2
  142. package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +3 -3
  143. package/dest/contract_sync/index.d.ts +0 -23
  144. package/dest/contract_sync/index.d.ts.map +0 -1
@@ -3,12 +3,12 @@ import type { BlockNumber } from '@aztec/foundation/branded-types';
3
3
  import { Aes128 } from '@aztec/foundation/crypto/aes128';
4
4
  import { Fr } from '@aztec/foundation/curves/bn254';
5
5
  import { Point } from '@aztec/foundation/curves/grumpkin';
6
- import { LogLevels, applyStringFormatting, createLogger } from '@aztec/foundation/log';
6
+ import { LogLevels, type Logger, applyStringFormatting, createLogger } from '@aztec/foundation/log';
7
7
  import type { MembershipWitness } from '@aztec/foundation/trees';
8
8
  import type { KeyStore } from '@aztec/key-store';
9
9
  import type { AuthWitness } from '@aztec/stdlib/auth-witness';
10
10
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
11
- import { L2BlockHash } from '@aztec/stdlib/block';
11
+ import { BlockHash } from '@aztec/stdlib/block';
12
12
  import type { CompleteAddress, ContractInstance } from '@aztec/stdlib/contract';
13
13
  import { siloNullifier } from '@aztec/stdlib/hash';
14
14
  import type { AztecNode } from '@aztec/stdlib/interfaces/server';
@@ -25,7 +25,6 @@ import { LogService } from '../../logs/log_service.js';
25
25
  import { NoteService } from '../../notes/note_service.js';
26
26
  import { ORACLE_VERSION } from '../../oracle_version.js';
27
27
  import type { AddressStore } from '../../storage/address_store/address_store.js';
28
- import type { AnchorBlockStore } from '../../storage/anchor_block_store/anchor_block_store.js';
29
28
  import type { CapsuleStore } from '../../storage/capsule_store/capsule_store.js';
30
29
  import type { ContractStore } from '../../storage/contract_store/contract_store.js';
31
30
  import type { NoteStore } from '../../storage/note_store/note_store.js';
@@ -41,6 +40,27 @@ import { pickNotes } from '../pick_notes.js';
41
40
  import type { IMiscOracle, IUtilityExecutionOracle, NoteData } from './interfaces.js';
42
41
  import { MessageLoadOracleInputs } from './message_load_oracle_inputs.js';
43
42
 
43
+ /** Args for UtilityExecutionOracle constructor. */
44
+ export type UtilityExecutionOracleArgs = {
45
+ contractAddress: AztecAddress;
46
+ /** List of transient auth witnesses to be used during this simulation */
47
+ authWitnesses: AuthWitness[];
48
+ capsules: Capsule[]; // TODO(#12425): Rename to transientCapsules
49
+ anchorBlockHeader: BlockHeader;
50
+ contractStore: ContractStore;
51
+ noteStore: NoteStore;
52
+ keyStore: KeyStore;
53
+ addressStore: AddressStore;
54
+ aztecNode: AztecNode;
55
+ recipientTaggingStore: RecipientTaggingStore;
56
+ senderAddressBookStore: SenderAddressBookStore;
57
+ capsuleStore: CapsuleStore;
58
+ privateEventStore: PrivateEventStore;
59
+ jobId: string;
60
+ log?: ReturnType<typeof createLogger>;
61
+ scopes?: AztecAddress[];
62
+ };
63
+
44
64
  /**
45
65
  * The oracle for an execution of utility contract functions.
46
66
  */
@@ -48,28 +68,43 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
48
68
  isMisc = true as const;
49
69
  isUtility = true as const;
50
70
 
51
- private aztecNrDebugLog = createLogger('aztec-nr:debug_log');
52
-
53
- constructor(
54
- protected readonly contractAddress: AztecAddress,
55
- /** List of transient auth witnesses to be used during this simulation */
56
- protected readonly authWitnesses: AuthWitness[],
57
- protected readonly capsules: Capsule[], // TODO(#12425): Rename to transientCapsules
58
- protected readonly anchorBlockHeader: BlockHeader,
59
- protected readonly contractStore: ContractStore,
60
- protected readonly noteStore: NoteStore,
61
- protected readonly keyStore: KeyStore,
62
- protected readonly addressStore: AddressStore,
63
- protected readonly aztecNode: AztecNode,
64
- protected readonly anchorBlockStore: AnchorBlockStore,
65
- protected readonly recipientTaggingStore: RecipientTaggingStore,
66
- protected readonly senderAddressBookStore: SenderAddressBookStore,
67
- protected readonly capsuleStore: CapsuleStore,
68
- protected readonly privateEventStore: PrivateEventStore,
69
- protected readonly jobId: string,
70
- protected log = createLogger('simulator:client_view_context'),
71
- protected readonly scopes?: AztecAddress[],
72
- ) {}
71
+ private contractLogger: Logger | undefined;
72
+
73
+ protected readonly contractAddress: AztecAddress;
74
+ protected readonly authWitnesses: AuthWitness[];
75
+ protected readonly capsules: Capsule[];
76
+ protected readonly anchorBlockHeader: BlockHeader;
77
+ protected readonly contractStore: ContractStore;
78
+ protected readonly noteStore: NoteStore;
79
+ protected readonly keyStore: KeyStore;
80
+ protected readonly addressStore: AddressStore;
81
+ protected readonly aztecNode: AztecNode;
82
+ protected readonly recipientTaggingStore: RecipientTaggingStore;
83
+ protected readonly senderAddressBookStore: SenderAddressBookStore;
84
+ protected readonly capsuleStore: CapsuleStore;
85
+ protected readonly privateEventStore: PrivateEventStore;
86
+ protected readonly jobId: string;
87
+ protected log: ReturnType<typeof createLogger>;
88
+ protected readonly scopes?: AztecAddress[];
89
+
90
+ constructor(args: UtilityExecutionOracleArgs) {
91
+ this.contractAddress = args.contractAddress;
92
+ this.authWitnesses = args.authWitnesses;
93
+ this.capsules = args.capsules;
94
+ this.anchorBlockHeader = args.anchorBlockHeader;
95
+ this.contractStore = args.contractStore;
96
+ this.noteStore = args.noteStore;
97
+ this.keyStore = args.keyStore;
98
+ this.addressStore = args.addressStore;
99
+ this.aztecNode = args.aztecNode;
100
+ this.recipientTaggingStore = args.recipientTaggingStore;
101
+ this.senderAddressBookStore = args.senderAddressBookStore;
102
+ this.capsuleStore = args.capsuleStore;
103
+ this.privateEventStore = args.privateEventStore;
104
+ this.jobId = args.jobId;
105
+ this.log = args.log ?? createLogger('simulator:client_view_context');
106
+ this.scopes = args.scopes;
107
+ }
73
108
 
74
109
  public utilityAssertCompatibleOracleVersion(version: number): void {
75
110
  if (version !== ORACLE_VERSION) {
@@ -90,35 +125,49 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
90
125
  * @param pkMHash - The master public key hash.
91
126
  * @returns A Promise that resolves to nullifier keys.
92
127
  * @throws If the keys are not registered in the key store.
128
+ * @throws If scopes are defined and the account is not in the scopes.
93
129
  */
94
- public utilityGetKeyValidationRequest(pkMHash: Fr): Promise<KeyValidationRequest> {
130
+ public async utilityGetKeyValidationRequest(pkMHash: Fr): Promise<KeyValidationRequest> {
131
+ // If scopes are defined, check that the key belongs to an account in the scopes
132
+ if (this.scopes && this.scopes.length > 0) {
133
+ const [, account] = await this.keyStore.getKeyPrefixAndAccount(pkMHash);
134
+ if (!this.scopes.some(scope => scope.equals(account))) {
135
+ throw new Error(`Key validation request denied: account ${account.toString()} is not in the allowed scopes.`);
136
+ }
137
+ }
95
138
  return this.keyStore.getKeyValidationRequest(pkMHash, this.contractAddress);
96
139
  }
97
140
 
98
141
  /**
99
142
  * Fetches the index and sibling path of a leaf at a given block from the note hash tree.
100
- * @param blockHash - The block hash at which to get the membership witness.
101
- * @param leafValue - The leaf value
143
+ * @param anchorBlockHash - The hash of a block that contains the note hash tree root in which to find the membership
144
+ * witness.
145
+ * @param noteHash - The note hash to find in the note hash tree.
102
146
  * @returns The membership witness containing the leaf index and sibling path
103
147
  */
104
148
  public utilityGetNoteHashMembershipWitness(
105
- blockHash: L2BlockHash,
106
- leafValue: Fr,
149
+ anchorBlockHash: BlockHash,
150
+ noteHash: Fr,
107
151
  ): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
108
- return this.aztecNode.getNoteHashMembershipWitness(blockHash, leafValue);
152
+ return this.aztecNode.getNoteHashMembershipWitness(anchorBlockHash, noteHash);
109
153
  }
110
154
 
111
155
  /**
112
- * Fetches the index and sibling path of a leaf at a given block from the archive tree.
113
- * @param blockHash - The block hash at which to get the membership witness.
114
- * @param leafValue - The leaf value
156
+ * Fetches the index and sibling path of a block hash in the archive tree.
157
+ *
158
+ * Block hashes are the leaves of the archive tree. Each time a new block is added to the chain,
159
+ * its block hash is appended as a new leaf to the archive tree.
160
+ *
161
+ * @param anchorBlockHash - The hash of a block that contains the archive tree root in which to find the membership
162
+ * witness.
163
+ * @param blockHash - The block hash to find in the archive tree.
115
164
  * @returns The membership witness containing the leaf index and sibling path
116
165
  */
117
- public utilityGetArchiveMembershipWitness(
118
- blockHash: L2BlockHash,
119
- leafValue: Fr,
166
+ public utilityGetBlockHashMembershipWitness(
167
+ anchorBlockHash: BlockHash,
168
+ blockHash: BlockHash,
120
169
  ): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
121
- return this.aztecNode.getArchiveMembershipWitness(blockHash, leafValue);
170
+ return this.aztecNode.getBlockHashMembershipWitness(anchorBlockHash, blockHash);
122
171
  }
123
172
 
124
173
  /**
@@ -128,7 +177,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
128
177
  * @returns The nullifier membership witness (if found).
129
178
  */
130
179
  public utilityGetNullifierMembershipWitness(
131
- blockHash: L2BlockHash,
180
+ blockHash: BlockHash,
132
181
  nullifier: Fr,
133
182
  ): Promise<NullifierMembershipWitness | undefined> {
134
183
  return this.aztecNode.getNullifierMembershipWitness(blockHash, nullifier);
@@ -144,7 +193,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
144
193
  * we are trying to prove non-inclusion for.
145
194
  */
146
195
  public utilityGetLowNullifierMembershipWitness(
147
- blockHash: L2BlockHash,
196
+ blockHash: BlockHash,
148
197
  nullifier: Fr,
149
198
  ): Promise<NullifierMembershipWitness | undefined> {
150
199
  return this.aztecNode.getLowNullifierMembershipWitness(blockHash, nullifier);
@@ -156,7 +205,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
156
205
  * @param leafSlot - The slot of the public data tree to get the witness for.
157
206
  * @returns - The witness
158
207
  */
159
- public utilityGetPublicDataWitness(blockHash: L2BlockHash, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
208
+ public utilityGetPublicDataWitness(blockHash: BlockHash, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
160
209
  return this.aztecNode.getPublicDataWitness(blockHash, leafSlot);
161
210
  }
162
211
 
@@ -166,7 +215,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
166
215
  * @returns Block extracted from a block with block number `blockNumber`.
167
216
  */
168
217
  public async utilityGetBlockHeader(blockNumber: BlockNumber): Promise<BlockHeader | undefined> {
169
- const anchorBlockNumber = (await this.anchorBlockStore.getBlockHeader()).getBlockNumber();
218
+ const anchorBlockNumber = this.anchorBlockHeader.getBlockNumber();
170
219
  if (blockNumber > anchorBlockNumber) {
171
220
  throw new Error(`Block number ${blockNumber} is higher than current block ${anchorBlockNumber}`);
172
221
  }
@@ -178,14 +227,13 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
178
227
  /**
179
228
  * Retrieve the complete address associated to a given address.
180
229
  * @param account - The account address.
181
- * @returns A complete address associated with the input address.
182
- * @throws An error if the account is not registered in the database.
230
+ * @returns A complete address associated with the input address, or `undefined` if not registered.
183
231
  */
184
- public utilityGetPublicKeysAndPartialAddress(account: AztecAddress): Promise<CompleteAddress> {
185
- return this.getCompleteAddress(account);
232
+ public utilityTryGetPublicKeysAndPartialAddress(account: AztecAddress): Promise<CompleteAddress | undefined> {
233
+ return this.addressStore.getCompleteAddress(account);
186
234
  }
187
235
 
188
- protected async getCompleteAddress(account: AztecAddress): Promise<CompleteAddress> {
236
+ protected async getCompleteAddressOrFail(account: AztecAddress): Promise<CompleteAddress> {
189
237
  const completeAddress = await this.addressStore.getCompleteAddress(account);
190
238
  if (!completeAddress) {
191
239
  throw new Error(
@@ -262,7 +310,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
262
310
  offset: number,
263
311
  status: NoteStatus,
264
312
  ): Promise<NoteData[]> {
265
- const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockStore, this.jobId);
313
+ const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockHeader, this.jobId);
266
314
 
267
315
  const dbNotes = await noteService.getNotes(this.contractAddress, owner, storageSlot, status, this.scopes);
268
316
  return pickNotes<NoteData>(dbNotes, {
@@ -286,8 +334,13 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
286
334
  * @returns A boolean indicating whether the nullifier exists in the tree or not.
287
335
  */
288
336
  public async utilityCheckNullifierExists(innerNullifier: Fr) {
289
- const nullifier = await siloNullifier(this.contractAddress, innerNullifier!);
290
- const [leafIndex] = await this.aztecNode.findLeavesIndexes('latest', MerkleTreeId.NULLIFIER_TREE, [nullifier]);
337
+ const [nullifier, anchorBlockHash] = await Promise.all([
338
+ siloNullifier(this.contractAddress, innerNullifier!),
339
+ this.anchorBlockHeader.hash(),
340
+ ]);
341
+ const [leafIndex] = await this.aztecNode.findLeavesIndexes(anchorBlockHash, MerkleTreeId.NULLIFIER_TREE, [
342
+ nullifier,
343
+ ]);
291
344
  return leafIndex?.data !== undefined;
292
345
  }
293
346
 
@@ -318,7 +371,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
318
371
  * @param numberOfElements - Number of elements to read from the starting storage slot.
319
372
  */
320
373
  public async utilityStorageRead(
321
- blockHash: L2BlockHash,
374
+ blockHash: BlockHash,
322
375
  contractAddress: AztecAddress,
323
376
  startStorageSlot: Fr,
324
377
  numberOfElements: number,
@@ -338,36 +391,44 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
338
391
  return values;
339
392
  }
340
393
 
341
- public utilityDebugLog(level: number, message: string, fields: Fr[]): void {
394
+ /**
395
+ * Returns a per-contract logger whose output is prefixed with `contract_log::<name>(<addrAbbrev>)`.
396
+ */
397
+ async #getContractLogger(): Promise<Logger> {
398
+ if (!this.contractLogger) {
399
+ const addrAbbrev = this.contractAddress.toString().slice(0, 10);
400
+ const name = await this.contractStore.getDebugContractName(this.contractAddress);
401
+ const module = name ? `contract_log::${name}(${addrAbbrev})` : `contract_log::${addrAbbrev}`;
402
+ // Purpose of instanceId is to distinguish logs from different instances of the same component. It makes sense
403
+ // to re-use jobId as instanceId here as executions of different PXE jobs are isolated.
404
+ this.contractLogger = createLogger(module, { instanceId: this.jobId });
405
+ }
406
+ return this.contractLogger;
407
+ }
408
+
409
+ public async utilityLog(level: number, message: string, fields: Fr[]): Promise<void> {
342
410
  if (!LogLevels[level]) {
343
- throw new Error(`Invalid debug log level: ${level}`);
411
+ throw new Error(`Invalid log level: ${level}`);
344
412
  }
345
413
  const levelName = LogLevels[level];
346
- this.aztecNrDebugLog[levelName](`${applyStringFormatting(message, fields)}`);
414
+ const logger = await this.#getContractLogger();
415
+ logger[levelName](`${applyStringFormatting(message, fields)}`);
347
416
  }
348
417
 
349
418
  public async utilityFetchTaggedLogs(pendingTaggedLogArrayBaseSlot: Fr) {
350
419
  const logService = new LogService(
351
420
  this.aztecNode,
352
- this.anchorBlockStore,
421
+ this.anchorBlockHeader,
353
422
  this.keyStore,
354
423
  this.capsuleStore,
355
424
  this.recipientTaggingStore,
356
425
  this.senderAddressBookStore,
357
426
  this.addressStore,
358
427
  this.jobId,
428
+ this.log.getBindings(),
359
429
  );
360
430
 
361
- const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockStore, this.jobId);
362
-
363
- // It is acceptable to run the following operations in parallel for several reasons:
364
- // 1. syncTaggedLogs does not write to the note store — it only stores the pending tagged logs in a capsule array,
365
- // which is then processed in Noir after this handler returns.
366
- // 2. Even if syncTaggedLogs did write to the note store, it would not cause inconsistent state.
367
- await Promise.all([
368
- logService.syncTaggedLogs(this.contractAddress, pendingTaggedLogArrayBaseSlot, this.scopes),
369
- noteService.syncNoteNullifiers(this.contractAddress),
370
- ]);
431
+ await logService.fetchTaggedLogs(this.contractAddress, pendingTaggedLogArrayBaseSlot, this.scopes);
371
432
  }
372
433
 
373
434
  /**
@@ -400,7 +461,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
400
461
  await this.capsuleStore.readCapsuleArray(contractAddress, eventValidationRequestsArrayBaseSlot, this.jobId)
401
462
  ).map(EventValidationRequest.fromFields);
402
463
 
403
- const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockStore, this.jobId);
464
+ const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockHeader, this.jobId);
404
465
  const noteStorePromises = noteValidationRequests.map(request =>
405
466
  noteService.validateAndStoreNote(
406
467
  request.contractAddress,
@@ -416,7 +477,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
416
477
  ),
417
478
  );
418
479
 
419
- const eventService = new EventService(this.anchorBlockStore, this.aztecNode, this.privateEventStore, this.jobId);
480
+ const eventService = new EventService(this.anchorBlockHeader, this.aztecNode, this.privateEventStore, this.jobId);
420
481
  const eventStorePromises = eventValidationRequests.map(request =>
421
482
  eventService.validateAndStoreEvent(
422
483
  request.contractAddress,
@@ -454,13 +515,14 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
454
515
 
455
516
  const logService = new LogService(
456
517
  this.aztecNode,
457
- this.anchorBlockStore,
518
+ this.anchorBlockHeader,
458
519
  this.keyStore,
459
520
  this.capsuleStore,
460
521
  this.recipientTaggingStore,
461
522
  this.senderAddressBookStore,
462
523
  this.addressStore,
463
524
  this.jobId,
525
+ this.log.getBindings(),
464
526
  );
465
527
 
466
528
  const maybeLogRetrievalResponses = await logService.bulkRetrieveLogs(logRetrievalRequests);
@@ -538,7 +600,7 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
538
600
 
539
601
  protected async getSharedSecret(address: AztecAddress, ephPk: Point): Promise<Point> {
540
602
  // TODO(#12656): return an app-siloed secret
541
- const recipientCompleteAddress = await this.getCompleteAddress(address);
603
+ const recipientCompleteAddress = await this.getCompleteAddressOrFail(address);
542
604
  const ivskM = await this.keyStore.getMasterSecretKey(
543
605
  recipientCompleteAddress.publicKeys.masterIncomingViewingPublicKey,
544
606
  );
@@ -0,0 +1,129 @@
1
+ import type { Logger } from '@aztec/foundation/log';
2
+ import type { FunctionCall, FunctionSelector } from '@aztec/stdlib/abi';
3
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
+ import type { AztecNode } from '@aztec/stdlib/interfaces/client';
5
+ import type { BlockHeader } from '@aztec/stdlib/tx';
6
+
7
+ import type { StagedStore } from '../job_coordinator/job_coordinator.js';
8
+ import type { ContractStore } from '../storage/contract_store/contract_store.js';
9
+ import type { NoteStore } from '../storage/note_store/note_store.js';
10
+ import { syncState, verifyCurrentClassId } from './helpers.js';
11
+
12
+ /**
13
+ * Service for syncing the private state of contracts and verifying that the PXE holds the current class artifact.
14
+ * It uses a cache to avoid redundant sync operations - the cache is wiped when the anchor block changes.
15
+ *
16
+ * TODO: The StagedStore naming is broken here. Figure out a better name.
17
+ */
18
+ export class ContractSyncService implements StagedStore {
19
+ readonly storeName = 'contract_sync';
20
+
21
+ // Tracks contracts synced since last wipe. Key is contract address string, value is a promise that resolves when
22
+ // the contract is synced.
23
+ private syncedContracts: Map<string, Promise<void>> = new Map();
24
+
25
+ // Per-job overridden contract addresses - these contracts should not be synced.
26
+ private overriddenContracts: Map<string, Set<string>> = new Map();
27
+
28
+ constructor(
29
+ private aztecNode: AztecNode,
30
+ private contractStore: ContractStore,
31
+ private noteStore: NoteStore,
32
+ private log: Logger,
33
+ ) {}
34
+
35
+ /** Sets contracts that should be skipped during sync for a specific job. */
36
+ setOverriddenContracts(jobId: string, addresses: Set<string>): void {
37
+ this.overriddenContracts.set(jobId, addresses);
38
+ }
39
+
40
+ /**
41
+ * Ensures a contract's private state is synchronized and that the PXE holds the current class artifact.
42
+ * Uses a cache to avoid redundant sync operations - the cache is wiped when the anchor block changes.
43
+ * @param contractAddress - The address of the contract to sync.
44
+ * @param functionToInvokeAfterSync - The function selector that will be called after sync (used to validate it's
45
+ * not sync_state itself).
46
+ * @param utilityExecutor - Executor function for running the sync_state utility function.
47
+ */
48
+ async ensureContractSynced(
49
+ contractAddress: AztecAddress,
50
+ functionToInvokeAfterSync: FunctionSelector | null,
51
+ utilityExecutor: (call: FunctionCall) => Promise<any>,
52
+ anchorBlockHeader: BlockHeader,
53
+ jobId: string,
54
+ ): Promise<void> {
55
+ const key = contractAddress.toString();
56
+
57
+ // Skip sync if this contract has an override for this job
58
+ const overrides = this.overriddenContracts.get(jobId);
59
+ if (overrides?.has(key)) {
60
+ return;
61
+ }
62
+
63
+ const existing = this.syncedContracts.get(key);
64
+ if (existing) {
65
+ return existing;
66
+ }
67
+
68
+ const syncPromise = this.#doSync(
69
+ contractAddress,
70
+ functionToInvokeAfterSync,
71
+ utilityExecutor,
72
+ anchorBlockHeader,
73
+ jobId,
74
+ );
75
+ this.syncedContracts.set(key, syncPromise);
76
+
77
+ try {
78
+ await syncPromise;
79
+ } catch (err) {
80
+ // There was an error syncing the contract, so we remove it from the cache so that it can be retried.
81
+ this.syncedContracts.delete(key);
82
+ throw err;
83
+ }
84
+ }
85
+
86
+ async #doSync(
87
+ contractAddress: AztecAddress,
88
+ functionToInvokeAfterSync: FunctionSelector | null,
89
+ utilityExecutor: (call: FunctionCall) => Promise<any>,
90
+ anchorBlockHeader: BlockHeader,
91
+ jobId: string,
92
+ ): Promise<void> {
93
+ this.log.debug(`Syncing contract ${contractAddress}`);
94
+ await Promise.all([
95
+ syncState(
96
+ contractAddress,
97
+ this.contractStore,
98
+ functionToInvokeAfterSync,
99
+ utilityExecutor,
100
+ this.noteStore,
101
+ this.aztecNode,
102
+ anchorBlockHeader,
103
+ jobId,
104
+ ),
105
+ verifyCurrentClassId(contractAddress, this.aztecNode, this.contractStore, anchorBlockHeader),
106
+ ]);
107
+ this.log.debug(`Contract ${contractAddress} synced`);
108
+ }
109
+
110
+ /** Clears sync cache. Called by BlockSynchronizer when anchor block changes. */
111
+ wipe(): void {
112
+ this.log.debug(`Wiping contract sync cache (${this.syncedContracts.size} entries)`);
113
+ this.syncedContracts.clear();
114
+ }
115
+
116
+ commit(jobId: string): Promise<void> {
117
+ // Clear overridden contracts for this job
118
+ this.overriddenContracts.delete(jobId);
119
+ return Promise.resolve();
120
+ }
121
+
122
+ discardStaged(jobId: string): Promise<void> {
123
+ // We clear the synced contracts cache here because, when the job is discarded, any associated database writes from
124
+ // the sync are also undone.
125
+ this.syncedContracts.clear();
126
+ this.overriddenContracts.delete(jobId);
127
+ return Promise.resolve();
128
+ }
129
+ }
@@ -6,7 +6,9 @@ import { DelayedPublicMutableValues, DelayedPublicMutableValuesWithHash } from '
6
6
  import type { AztecNode } from '@aztec/stdlib/interfaces/client';
7
7
  import type { BlockHeader } from '@aztec/stdlib/tx';
8
8
 
9
+ import { NoteService } from '../notes/note_service.js';
9
10
  import type { ContractStore } from '../storage/contract_store/contract_store.js';
11
+ import type { NoteStore } from '../storage/note_store/note_store.js';
10
12
 
11
13
  /**
12
14
  * Read the current class id of a contract from the execution data provider or AztecNode. If not found, class id
@@ -41,6 +43,10 @@ export async function syncState(
41
43
  contractStore: ContractStore,
42
44
  functionToInvokeAfterSync: FunctionSelector | null,
43
45
  utilityExecutor: (privateSyncCall: FunctionCall) => Promise<any>,
46
+ noteStore: NoteStore,
47
+ aztecNode: AztecNode,
48
+ anchorBlockHeader: BlockHeader,
49
+ jobId: string,
44
50
  ) {
45
51
  // Protocol contracts don't have private state to sync
46
52
  if (!isProtocolContract(contractAddress)) {
@@ -51,16 +57,23 @@ export async function syncState(
51
57
  );
52
58
  }
53
59
 
54
- return utilityExecutor(syncStateFunctionCall);
60
+ const noteService = new NoteService(noteStore, aztecNode, anchorBlockHeader, jobId);
61
+
62
+ // Both sync_state and syncNoteNullifiers interact with the note store, but running them in parallel is safe
63
+ // because note store is designed to handle concurrent operations.
64
+ await Promise.all([utilityExecutor(syncStateFunctionCall), noteService.syncNoteNullifiers(contractAddress)]);
55
65
  }
56
66
  }
57
67
 
58
68
  /**
59
69
  * Verify that the current class id of a contract obtained from AztecNode is the same as the one in contract data
60
70
  * provider (i.e. PXE's own storage).
71
+ * @param contractAddress - The address of the contract to verify.
72
+ * @param aztecNode - The Aztec node to query for storage.
73
+ * @param contractStore - The contract store to fetch the local instance from.
61
74
  * @param header - The header of the block at which to verify the current class id.
62
75
  */
63
- async function verifyCurrentClassId(
76
+ export async function verifyCurrentClassId(
64
77
  contractAddress: AztecAddress,
65
78
  aztecNode: AztecNode,
66
79
  contractStore: ContractStore,
@@ -78,21 +91,3 @@ async function verifyCurrentClassId(
78
91
  );
79
92
  }
80
93
  }
81
-
82
- /**
83
- * Ensures the contract's private state is synchronized and that the PXE holds the current class artifact for
84
- * the contract.
85
- */
86
- export async function ensureContractSynced(
87
- contractAddress: AztecAddress,
88
- functionToInvokeAfterSync: FunctionSelector | null,
89
- utilityExecutor: (call: FunctionCall) => Promise<any>,
90
- aztecNode: AztecNode,
91
- contractStore: ContractStore,
92
- header: BlockHeader,
93
- ): Promise<void> {
94
- await Promise.all([
95
- syncState(contractAddress, contractStore, functionToInvokeAfterSync, utilityExecutor),
96
- verifyCurrentClassId(contractAddress, aztecNode, contractStore, header),
97
- ]);
98
- }
@@ -1,8 +1,13 @@
1
- import { randomBytes } from '@aztec/foundation/crypto/random';
1
+ import type { FunctionCall } from '@aztec/stdlib/abi';
2
+ import type { AuthWitness } from '@aztec/stdlib/auth-witness';
3
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
2
4
  import type { NoteDao, NotesFilter } from '@aztec/stdlib/note';
5
+ import type { ContractOverrides } from '@aztec/stdlib/tx';
3
6
 
4
- import type { PXE } from '../pxe.js';
5
- import type { ContractStore } from '../storage/contract_store/contract_store.js';
7
+ import type { BlockSynchronizer } from '../block_synchronizer/block_synchronizer.js';
8
+ import type { ContractFunctionSimulator } from '../contract_function_simulator/contract_function_simulator.js';
9
+ import type { ContractSyncService } from '../contract_sync/contract_sync_service.js';
10
+ import type { AnchorBlockStore } from '../storage/index.js';
6
11
  import type { NoteStore } from '../storage/note_store/note_store.js';
7
12
 
8
13
  /**
@@ -10,18 +15,38 @@ import type { NoteStore } from '../storage/note_store/note_store.js';
10
15
  * No backwards compatibility or API stability should be expected. Use at your own risk.
11
16
  */
12
17
  export class PXEDebugUtils {
13
- #pxe: PXE | undefined = undefined;
18
+ #putJobInQueue!: <T>(job: (jobId: string) => Promise<T>) => Promise<T>;
19
+ #getSimulatorForTx!: (overrides?: { contracts?: ContractOverrides }) => ContractFunctionSimulator;
20
+ #simulateUtility!: (
21
+ contractFunctionSimulator: ContractFunctionSimulator,
22
+ call: FunctionCall,
23
+ authWitnesses: AuthWitness[] | undefined,
24
+ scopes: AztecAddress[] | undefined,
25
+ jobId: string,
26
+ ) => Promise<any>;
14
27
 
15
28
  constructor(
16
- private contractStore: ContractStore,
29
+ private contractSyncService: ContractSyncService,
17
30
  private noteStore: NoteStore,
31
+ private blockStateSynchronizer: BlockSynchronizer,
32
+ private anchorBlockStore: AnchorBlockStore,
18
33
  ) {}
19
34
 
20
- /**
21
- * Not injected through constructor since they're are co-dependant.
22
- */
23
- public setPXE(pxe: PXE) {
24
- this.#pxe = pxe;
35
+ /** Not injected through constructor since they're are co-dependant */
36
+ public setPXEHelpers(
37
+ putJobInQueue: <T>(job: (jobId: string) => Promise<T>) => Promise<T>,
38
+ getSimulatorForTx: (overrides?: { contracts?: ContractOverrides }) => ContractFunctionSimulator,
39
+ simulateUtility: (
40
+ contractFunctionSimulator: ContractFunctionSimulator,
41
+ call: FunctionCall,
42
+ authWitnesses: AuthWitness[] | undefined,
43
+ scopes: AztecAddress[] | undefined,
44
+ jobId: string,
45
+ ) => Promise<any>,
46
+ ) {
47
+ this.#putJobInQueue = putJobInQueue;
48
+ this.#getSimulatorForTx = getSimulatorForTx;
49
+ this.#simulateUtility = simulateUtility;
25
50
  }
26
51
 
27
52
  /**
@@ -35,15 +60,32 @@ export class PXEDebugUtils {
35
60
  * @param filter - The filter to apply to the notes.
36
61
  * @returns The requested notes.
37
62
  */
38
- public async getNotes(filter: NotesFilter): Promise<NoteDao[]> {
39
- if (!this.#pxe) {
40
- throw new Error('Cannot getNotes because no PXE is set');
41
- }
63
+ public getNotes(filter: NotesFilter): Promise<NoteDao[]> {
64
+ return this.#putJobInQueue(async (jobId: string) => {
65
+ await this.blockStateSynchronizer.sync();
66
+
67
+ const anchorBlockHeader = await this.anchorBlockStore.getBlockHeader();
68
+
69
+ const contractFunctionSimulator = this.#getSimulatorForTx();
42
70
 
43
- // We need to manually trigger private state sync to have a guarantee that all the notes are available.
44
- const call = await this.contractStore.getFunctionCall('sync_state', [], filter.contractAddress);
45
- await this.#pxe.simulateUtility(call);
71
+ await this.contractSyncService.ensureContractSynced(
72
+ filter.contractAddress,
73
+ null,
74
+ async privateSyncCall =>
75
+ await this.#simulateUtility(contractFunctionSimulator, privateSyncCall, [], undefined, jobId),
76
+ anchorBlockHeader,
77
+ jobId,
78
+ );
46
79
 
47
- return this.noteStore.getNotes(filter, randomBytes(8).toString('hex'));
80
+ return this.noteStore.getNotes(filter, jobId);
81
+ });
82
+ }
83
+
84
+ /**
85
+ * Triggers a sync of the PXE with the node.
86
+ * Blocks until the sync is complete.
87
+ */
88
+ public sync(): Promise<void> {
89
+ return this.#putJobInQueue(() => this.blockStateSynchronizer.sync());
48
90
  }
49
91
  }