@aztec/pxe 0.0.1-commit.f504929 → 0.0.1-commit.f5d02921e

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 (123) hide show
  1. package/dest/block_synchronizer/block_synchronizer.d.ts +1 -1
  2. package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
  3. package/dest/block_synchronizer/block_synchronizer.js +6 -0
  4. package/dest/config/index.d.ts +2 -2
  5. package/dest/config/index.d.ts.map +1 -1
  6. package/dest/config/index.js +1 -1
  7. package/dest/contract_function_simulator/contract_function_simulator.d.ts +9 -3
  8. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  9. package/dest/contract_function_simulator/contract_function_simulator.js +29 -6
  10. package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts +5 -5
  11. package/dest/contract_function_simulator/execution_tagging_index_cache.d.ts.map +1 -1
  12. package/dest/contract_function_simulator/execution_tagging_index_cache.js +17 -9
  13. package/dest/contract_function_simulator/index.d.ts +2 -1
  14. package/dest/contract_function_simulator/index.d.ts.map +1 -1
  15. package/dest/contract_function_simulator/index.js +1 -0
  16. package/dest/contract_function_simulator/noir-structs/event_validation_request.d.ts +3 -5
  17. package/dest/contract_function_simulator/noir-structs/event_validation_request.d.ts.map +1 -1
  18. package/dest/contract_function_simulator/noir-structs/event_validation_request.js +7 -9
  19. package/dest/contract_function_simulator/noir-structs/log_retrieval_response.d.ts +1 -1
  20. package/dest/contract_function_simulator/noir-structs/log_retrieval_response.d.ts.map +1 -1
  21. package/dest/contract_function_simulator/noir-structs/log_retrieval_response.js +1 -3
  22. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts +3 -6
  23. package/dest/contract_function_simulator/noir-structs/note_validation_request.d.ts.map +1 -1
  24. package/dest/contract_function_simulator/noir-structs/note_validation_request.js +5 -10
  25. package/dest/contract_function_simulator/oracle/interfaces.d.ts +26 -21
  26. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  27. package/dest/contract_function_simulator/oracle/legacy_oracle_mappings.d.ts +9 -0
  28. package/dest/contract_function_simulator/oracle/legacy_oracle_mappings.d.ts.map +1 -0
  29. package/dest/contract_function_simulator/oracle/legacy_oracle_mappings.js +38 -0
  30. package/dest/contract_function_simulator/oracle/oracle.d.ts +39 -19
  31. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  32. package/dest/contract_function_simulator/oracle/oracle.js +91 -44
  33. package/dest/contract_function_simulator/oracle/private_execution.js +5 -3
  34. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +11 -36
  35. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts.map +1 -1
  36. package/dest/contract_function_simulator/oracle/private_execution_oracle.js +12 -44
  37. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +50 -28
  38. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  39. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +167 -64
  40. package/dest/contract_logging.d.ts +9 -4
  41. package/dest/contract_logging.d.ts.map +1 -1
  42. package/dest/contract_logging.js +21 -6
  43. package/dest/contract_sync/contract_sync_service.d.ts +7 -4
  44. package/dest/contract_sync/contract_sync_service.d.ts.map +1 -1
  45. package/dest/contract_sync/contract_sync_service.js +59 -32
  46. package/dest/contract_sync/helpers.d.ts +2 -2
  47. package/dest/contract_sync/helpers.d.ts.map +1 -1
  48. package/dest/contract_sync/helpers.js +7 -2
  49. package/dest/events/event_service.d.ts +3 -2
  50. package/dest/events/event_service.d.ts.map +1 -1
  51. package/dest/events/event_service.js +16 -4
  52. package/dest/logs/log_service.d.ts +6 -7
  53. package/dest/logs/log_service.d.ts.map +1 -1
  54. package/dest/logs/log_service.js +24 -28
  55. package/dest/messages/message_context_service.d.ts +17 -0
  56. package/dest/messages/message_context_service.d.ts.map +1 -0
  57. package/dest/messages/message_context_service.js +36 -0
  58. package/dest/notes/note_service.d.ts +2 -2
  59. package/dest/notes/note_service.d.ts.map +1 -1
  60. package/dest/notes/note_service.js +14 -5
  61. package/dest/oracle_version.d.ts +2 -2
  62. package/dest/oracle_version.js +2 -2
  63. package/dest/pxe.d.ts +6 -3
  64. package/dest/pxe.d.ts.map +1 -1
  65. package/dest/pxe.js +37 -21
  66. package/dest/storage/capsule_store/capsule_service.d.ts +22 -0
  67. package/dest/storage/capsule_store/capsule_service.d.ts.map +1 -0
  68. package/dest/storage/capsule_store/capsule_service.js +50 -0
  69. package/dest/storage/capsule_store/capsule_store.d.ts +9 -9
  70. package/dest/storage/capsule_store/capsule_store.d.ts.map +1 -1
  71. package/dest/storage/capsule_store/capsule_store.js +33 -28
  72. package/dest/storage/capsule_store/index.d.ts +2 -1
  73. package/dest/storage/capsule_store/index.d.ts.map +1 -1
  74. package/dest/storage/capsule_store/index.js +1 -0
  75. package/dest/storage/metadata.d.ts +1 -1
  76. package/dest/storage/metadata.js +1 -1
  77. package/dest/storage/tagging_store/sender_tagging_store.d.ts +26 -25
  78. package/dest/storage/tagging_store/sender_tagging_store.d.ts.map +1 -1
  79. package/dest/storage/tagging_store/sender_tagging_store.js +141 -115
  80. package/dest/tagging/index.d.ts +2 -2
  81. package/dest/tagging/index.d.ts.map +1 -1
  82. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts +1 -1
  83. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.d.ts.map +1 -1
  84. package/dest/tagging/sender_sync/sync_sender_tagging_indexes.js +10 -1
  85. package/dest/tagging/sender_sync/utils/get_status_change_of_pending.d.ts +4 -3
  86. package/dest/tagging/sender_sync/utils/get_status_change_of_pending.d.ts.map +1 -1
  87. package/dest/tagging/sender_sync/utils/get_status_change_of_pending.js +20 -10
  88. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts +2 -1
  89. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.d.ts.map +1 -1
  90. package/dest/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.js +24 -11
  91. package/package.json +16 -16
  92. package/src/block_synchronizer/block_synchronizer.ts +6 -0
  93. package/src/config/index.ts +1 -1
  94. package/src/contract_function_simulator/contract_function_simulator.ts +40 -7
  95. package/src/contract_function_simulator/execution_tagging_index_cache.ts +16 -11
  96. package/src/contract_function_simulator/index.ts +1 -0
  97. package/src/contract_function_simulator/noir-structs/event_validation_request.ts +8 -8
  98. package/src/contract_function_simulator/noir-structs/log_retrieval_response.ts +1 -4
  99. package/src/contract_function_simulator/noir-structs/note_validation_request.ts +3 -9
  100. package/src/contract_function_simulator/oracle/interfaces.ts +38 -29
  101. package/src/contract_function_simulator/oracle/legacy_oracle_mappings.ts +98 -0
  102. package/src/contract_function_simulator/oracle/oracle.ts +120 -64
  103. package/src/contract_function_simulator/oracle/private_execution.ts +4 -4
  104. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +14 -62
  105. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +290 -77
  106. package/src/contract_logging.ts +18 -5
  107. package/src/contract_sync/contract_sync_service.ts +89 -49
  108. package/src/contract_sync/helpers.ts +3 -2
  109. package/src/events/event_service.ts +17 -4
  110. package/src/logs/log_service.ts +51 -46
  111. package/src/messages/message_context_service.ts +44 -0
  112. package/src/notes/note_service.ts +16 -5
  113. package/src/oracle_version.ts +2 -2
  114. package/src/pxe.ts +55 -19
  115. package/src/storage/capsule_store/capsule_service.ts +91 -0
  116. package/src/storage/capsule_store/capsule_store.ts +34 -26
  117. package/src/storage/capsule_store/index.ts +1 -0
  118. package/src/storage/metadata.ts +1 -1
  119. package/src/storage/tagging_store/sender_tagging_store.ts +182 -135
  120. package/src/tagging/index.ts +1 -1
  121. package/src/tagging/sender_sync/sync_sender_tagging_indexes.ts +19 -1
  122. package/src/tagging/sender_sync/utils/get_status_change_of_pending.ts +26 -11
  123. package/src/tagging/sender_sync/utils/load_and_store_new_tagging_indexes.ts +19 -9
@@ -5,18 +5,22 @@ import type { DebugLog } from '@aztec/stdlib/logs';
5
5
 
6
6
  /** Resolves a contract address to a human-readable name, if available. */
7
7
  export type ContractNameResolver = (address: AztecAddress) => Promise<string | undefined>;
8
+ export type CONTRACT_LOG_KIND = 'aztecnr' | 'user';
8
9
 
9
10
  /**
10
- * Creates a logger whose output is prefixed with `contract_log::<name>(<addrAbbrev>)`.
11
+ * Creates a logger whose output is prefixed with `contract:<name>(<addrAbbrev>)`.
11
12
  */
12
13
  export async function createContractLogger(
13
14
  contractAddress: AztecAddress,
14
15
  getContractName: ContractNameResolver,
16
+ kind: CONTRACT_LOG_KIND,
15
17
  options?: { instanceId?: string },
16
18
  ): Promise<Logger> {
17
19
  const addrAbbrev = contractAddress.toString().slice(0, 10);
18
20
  const name = await getContractName(contractAddress);
19
- const module = name ? `contract_log::${name}(${addrAbbrev})` : `contract_log::Unknown(${addrAbbrev})`;
21
+
22
+ const prefix = kind == 'aztecnr' ? 'aztecnr' : 'contract';
23
+ const module = name ? `${prefix}:${name}(${addrAbbrev})` : `${prefix}:Unknown(${addrAbbrev})`;
20
24
  return createLogger(module, options);
21
25
  }
22
26
 
@@ -29,11 +33,20 @@ export function logContractMessage(logger: Logger, level: LogLevel, message: str
29
33
 
30
34
  /**
31
35
  * Displays debug logs collected during public function simulation,
32
- * using the `contract_log::` prefixed logger format.
36
+ * using the `contract:` prefixed logger format.
33
37
  */
34
38
  export async function displayDebugLogs(debugLogs: DebugLog[], getContractName: ContractNameResolver): Promise<void> {
35
39
  for (const log of debugLogs) {
36
- const logger = await createContractLogger(log.contractAddress, getContractName);
37
- logContractMessage(logger, log.level, log.message, log.fields);
40
+ const { kind, message } = stripAztecnrLogPrefix(log.message);
41
+ const logger = await createContractLogger(log.contractAddress, getContractName, kind);
42
+ logContractMessage(logger, log.level, message, log.fields);
43
+ }
44
+ }
45
+
46
+ export function stripAztecnrLogPrefix(message: string): { kind: CONTRACT_LOG_KIND; message: string } {
47
+ if (message.startsWith('[aztec-nr] ')) {
48
+ return { kind: 'aztecnr', message: message.slice('[aztec-nr] '.length) };
49
+ } else {
50
+ return { kind: 'user', message };
38
51
  }
39
52
  }
@@ -20,23 +20,24 @@ export class ContractSyncService implements StagedStore {
20
20
  readonly storeName = 'contract_sync';
21
21
 
22
22
  // Tracks contracts synced since last wipe. The cache is keyed per individual scope address
23
- // (`contractAddress:scopeAddress`), or `contractAddress:*` for undefined scopes (all accounts).
23
+ // (`contractAddress:scopeAddress`), or `contractAddress:*` for all scopes (all accounts).
24
24
  // The value is a promise that resolves when the contract is synced.
25
25
  private syncedContracts: Map<string, Promise<void>> = new Map();
26
26
 
27
- // Per-job overridden contract addresses - these contracts should not be synced.
28
- private overriddenContracts: Map<string, Set<string>> = new Map();
27
+ // Per-job excluded contract addresses - these contracts should not be synced.
28
+ private excludedFromSync: Map<string, Set<string>> = new Map();
29
29
 
30
30
  constructor(
31
31
  private aztecNode: AztecNode,
32
32
  private contractStore: ContractStore,
33
33
  private noteStore: NoteStore,
34
+ private getRegisteredAccounts: () => Promise<AztecAddress[]>,
34
35
  private log: Logger,
35
36
  ) {}
36
37
 
37
38
  /** Sets contracts that should be skipped during sync for a specific job. */
38
- setOverriddenContracts(jobId: string, addresses: Set<string>): void {
39
- this.overriddenContracts.set(jobId, addresses);
39
+ setExcludedFromSync(jobId: string, addresses: Set<string>): void {
40
+ this.excludedFromSync.set(jobId, addresses);
40
41
  }
41
42
 
42
43
  /**
@@ -56,47 +57,34 @@ export class ContractSyncService implements StagedStore {
56
57
  jobId: string,
57
58
  scopes: AccessScopes,
58
59
  ): Promise<void> {
59
- // Skip sync if this contract has an override for this job (overrides are keyed by contract address only)
60
- const overrides = this.overriddenContracts.get(jobId);
61
- if (overrides?.has(contractAddress.toString())) {
60
+ if (this.#shouldSkipSync(jobId, contractAddress)) {
62
61
  return;
63
62
  }
64
63
 
65
- // Skip sync if we already synced for "all scopes", or if we have an empty list of scopes
66
- const allScopesKey = toKey(contractAddress, 'ALL_SCOPES');
67
- const allScopesExisting = this.syncedContracts.get(allScopesKey);
68
- if (allScopesExisting || (scopes !== 'ALL_SCOPES' && scopes.length == 0)) {
69
- return;
70
- }
71
-
72
- const unsyncedScopes =
73
- scopes === 'ALL_SCOPES'
74
- ? scopes
75
- : scopes.filter(scope => !this.syncedContracts.has(toKey(contractAddress, scope)));
76
- const unsyncedScopesKeys = toKeys(contractAddress, unsyncedScopes);
77
-
78
- if (unsyncedScopesKeys.length > 0) {
79
- // Start sync and store the promise for all unsynced scopes
80
- const promise = this.#doSync(
64
+ this.#startSyncIfNeeded(contractAddress, scopes, scopesToSync =>
65
+ this.#syncContract(
81
66
  contractAddress,
82
67
  functionToInvokeAfterSync,
83
68
  utilityExecutor,
84
69
  anchorBlockHeader,
85
70
  jobId,
86
- unsyncedScopes,
87
- ).catch(err => {
88
- // There was an error syncing the contract, so we remove it from the cache so that it can be retried.
89
- unsyncedScopesKeys.forEach(key => this.syncedContracts.delete(key));
90
- throw err;
91
- });
92
- unsyncedScopesKeys.forEach(key => this.syncedContracts.set(key, promise));
93
- }
71
+ scopesToSync,
72
+ ),
73
+ );
94
74
 
95
- const promises = toKeys(contractAddress, scopes).map(key => this.syncedContracts.get(key)!);
96
- await Promise.all(promises);
75
+ await this.#awaitSync(contractAddress, scopes);
97
76
  }
98
77
 
99
- async #doSync(
78
+ /** Clears sync cache entries for the given scopes of a contract. Also clears the ALL_SCOPES entry. */
79
+ invalidateContractForScopes(contractAddress: AztecAddress, scopes: AztecAddress[]): void {
80
+ if (scopes.length === 0) {
81
+ return;
82
+ }
83
+ scopes.forEach(scope => this.syncedContracts.delete(toKey(contractAddress, scope)));
84
+ this.syncedContracts.delete(toKey(contractAddress, 'ALL_SCOPES'));
85
+ }
86
+
87
+ async #syncContract(
100
88
  contractAddress: AztecAddress,
101
89
  functionToInvokeAfterSync: FunctionSelector | null,
102
90
  utilityExecutor: (call: FunctionCall, scopes: AccessScopes) => Promise<any>,
@@ -105,18 +93,28 @@ export class ContractSyncService implements StagedStore {
105
93
  scopes: AccessScopes,
106
94
  ): Promise<void> {
107
95
  this.log.debug(`Syncing contract ${contractAddress}`);
96
+
97
+ // Resolve ALL_SCOPES to actual registered accounts, since sync_state must be called once per account.
98
+ const scopeAddresses = scopes === 'ALL_SCOPES' ? await this.getRegisteredAccounts() : scopes;
99
+
108
100
  await Promise.all([
109
- syncState(
110
- contractAddress,
111
- this.contractStore,
112
- functionToInvokeAfterSync,
113
- utilityExecutor,
114
- this.noteStore,
115
- this.aztecNode,
116
- anchorBlockHeader,
117
- jobId,
118
- scopes,
119
- ),
101
+ // Call sync_state sequentially for each scope address — each invocation synchronizes one account's private
102
+ // state using scoped capsule arrays.
103
+ (async () => {
104
+ for (const scope of scopeAddresses) {
105
+ await syncState(
106
+ contractAddress,
107
+ this.contractStore,
108
+ functionToInvokeAfterSync,
109
+ utilityExecutor,
110
+ this.noteStore,
111
+ this.aztecNode,
112
+ anchorBlockHeader,
113
+ jobId,
114
+ scope,
115
+ );
116
+ }
117
+ })(),
120
118
  verifyCurrentClassId(contractAddress, this.aztecNode, this.contractStore, anchorBlockHeader),
121
119
  ]);
122
120
  this.log.debug(`Contract ${contractAddress} synced`);
@@ -129,8 +127,8 @@ export class ContractSyncService implements StagedStore {
129
127
  }
130
128
 
131
129
  commit(jobId: string): Promise<void> {
132
- // Clear overridden contracts for this job
133
- this.overriddenContracts.delete(jobId);
130
+ // Clear excluded contracts for this job
131
+ this.excludedFromSync.delete(jobId);
134
132
  return Promise.resolve();
135
133
  }
136
134
 
@@ -138,9 +136,51 @@ export class ContractSyncService implements StagedStore {
138
136
  // We clear the synced contracts cache here because, when the job is discarded, any associated database writes from
139
137
  // the sync are also undone.
140
138
  this.syncedContracts.clear();
141
- this.overriddenContracts.delete(jobId);
139
+ this.excludedFromSync.delete(jobId);
142
140
  return Promise.resolve();
143
141
  }
142
+ /** Returns true if sync should be skipped for this contract */
143
+ #shouldSkipSync(jobId: string, contractAddress: AztecAddress): boolean {
144
+ return !!this.excludedFromSync.get(jobId)?.has(contractAddress.toString());
145
+ }
146
+
147
+ /** If there are unsynced scopes, starts sync and stores the promise in cache with error cleanup. */
148
+ #startSyncIfNeeded(
149
+ contractAddress: AztecAddress,
150
+ scopes: AccessScopes,
151
+ syncFn: (scopesToSync: AccessScopes) => Promise<void>,
152
+ ): void {
153
+ const scopesToSync = this.#getScopesToSync(contractAddress, scopes);
154
+ const keys = toKeys(contractAddress, scopesToSync);
155
+ if (keys.length === 0) {
156
+ return;
157
+ }
158
+ const promise = syncFn(scopesToSync).catch(err => {
159
+ keys.forEach(key => this.syncedContracts.delete(key));
160
+ throw err;
161
+ });
162
+ keys.forEach(key => this.syncedContracts.set(key, promise));
163
+ }
164
+
165
+ /** Filters out scopes that are already cached, returning only those that still need syncing. */
166
+ #getScopesToSync(contractAddress: AztecAddress, scopes: AccessScopes): AccessScopes {
167
+ if (this.syncedContracts.has(toKey(contractAddress, 'ALL_SCOPES'))) {
168
+ // If we are already syncing all scopes, then return an empty list
169
+ return [];
170
+ }
171
+ if (scopes === 'ALL_SCOPES') {
172
+ return 'ALL_SCOPES';
173
+ }
174
+ return scopes.filter(scope => !this.syncedContracts.has(toKey(contractAddress, scope)));
175
+ }
176
+
177
+ /** Collects all relevant scope promises (including in-flight ones from concurrent calls) and awaits them. */
178
+ async #awaitSync(contractAddress: AztecAddress, scopes: AccessScopes): Promise<void> {
179
+ const promises = toKeys(contractAddress, scopes)
180
+ .map(key => this.syncedContracts.get(key))
181
+ .filter(p => p !== undefined);
182
+ await Promise.all(promises);
183
+ }
144
184
  }
145
185
 
146
186
  function toKeys(contract: AztecAddress, scopes: AccessScopes) {
@@ -48,11 +48,11 @@ export async function syncState(
48
48
  aztecNode: AztecNode,
49
49
  anchorBlockHeader: BlockHeader,
50
50
  jobId: string,
51
- scopes: AccessScopes,
51
+ scope: AztecAddress,
52
52
  ) {
53
53
  // Protocol contracts don't have private state to sync
54
54
  if (!isProtocolContract(contractAddress)) {
55
- const syncStateFunctionCall = await contractStore.getFunctionCall('sync_state', [], contractAddress);
55
+ const syncStateFunctionCall = await contractStore.getFunctionCall('sync_state', [scope], contractAddress);
56
56
  if (functionToInvokeAfterSync && functionToInvokeAfterSync.equals(syncStateFunctionCall.selector)) {
57
57
  throw new Error(
58
58
  'Forbidden `sync_state` invocation. `sync_state` can only be invoked by PXE, manual execution can lead to inconsistencies.',
@@ -60,6 +60,7 @@ export async function syncState(
60
60
  }
61
61
 
62
62
  const noteService = new NoteService(noteStore, aztecNode, anchorBlockHeader, jobId);
63
+ const scopes: AccessScopes = [scope];
63
64
 
64
65
  // Both sync_state and syncNoteNullifiers interact with the note store, but running them in parallel is safe
65
66
  // because note store is designed to handle concurrent operations.
@@ -1,4 +1,5 @@
1
1
  import type { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { createLogger } from '@aztec/foundation/log';
2
3
  import type { EventSelector } from '@aztec/stdlib/abi';
3
4
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
5
  import { siloNullifier } from '@aztec/stdlib/hash';
@@ -13,6 +14,7 @@ export class EventService {
13
14
  private readonly aztecNode: AztecNode,
14
15
  private readonly privateEventStore: PrivateEventStore,
15
16
  private readonly jobId: string,
17
+ private readonly log = createLogger('pxe:event_service'),
16
18
  ) {}
17
19
 
18
20
  public async validateAndStoreEvent(
@@ -36,19 +38,30 @@ export class EventService {
36
38
  const anchorBlockNumber = this.anchorBlockHeader.getBlockNumber();
37
39
 
38
40
  if (!txEffect) {
39
- throw new Error(`Could not find tx effect for tx hash ${txHash}`);
41
+ // We error out instead of just logging a warning and skipping the event because this would indicate a bug. This
42
+ // is because the node has already served info about this tx either when obtaining the log (TxScopedL2Log contain
43
+ // tx info) or when getting metadata for the offchain message (before the message got passed to `process_log`).
44
+ throw new Error(`Could not find tx effect for tx hash ${txHash} when processing an event.`);
40
45
  }
41
46
 
42
47
  if (txEffect.l2BlockNumber > anchorBlockNumber) {
43
- throw new Error(`Could not find tx effect for tx hash ${txHash} as of block number ${anchorBlockNumber}`);
48
+ // We should never process a message from a tx past the anchor block. If we got here, a preprocessing step made
49
+ // a mistake.
50
+ throw new Error(
51
+ `Obtained a newer tx effect for ${txHash} for an event validation request than the anchor block ${anchorBlockNumber}. This is a bug as smart contracts should not issue event validation requests for events from blocks newer than the anchor block.`,
52
+ );
44
53
  }
45
54
 
46
55
  // Find the index of the event commitment in the nullifiers array to determine event ordering within the tx
47
56
  const eventIndexInTx = txEffect.data.nullifiers.findIndex(n => n.equals(siloedEventCommitment));
48
57
  if (eventIndexInTx === -1) {
49
- throw new Error(
50
- `Event commitment ${eventCommitment} (siloed as ${siloedEventCommitment}) is not present in tx ${txHash}`,
58
+ // Unlike in NoteService, this might not be a bug since the commitment hasn't been verified yet in the message
59
+ // processing pipeline. A malformed or malicious message could trigger this condition. Because of this we don't
60
+ // error out and we just show a warning.
61
+ this.log.warn(
62
+ `Skipping event whose commitment is not present in its tx. siloedEventCommitment=${siloedEventCommitment}, contract=${contractAddress}, selector=${selector}, eventCommitment=${eventCommitment}, txHash=${txHash}`,
51
63
  );
64
+ return;
52
65
  }
53
66
 
54
67
  return this.privateEventStore.storePrivateEventLog(
@@ -12,11 +12,10 @@ import {
12
12
  } from '@aztec/stdlib/logs';
13
13
  import type { BlockHeader } from '@aztec/stdlib/tx';
14
14
 
15
- import type { AccessScopes } from '../access_scopes.js';
16
15
  import type { LogRetrievalRequest } from '../contract_function_simulator/noir-structs/log_retrieval_request.js';
17
16
  import { LogRetrievalResponse } from '../contract_function_simulator/noir-structs/log_retrieval_response.js';
18
17
  import { AddressStore } from '../storage/address_store/address_store.js';
19
- import { CapsuleStore } from '../storage/capsule_store/capsule_store.js';
18
+ import type { CapsuleService } from '../storage/capsule_store/capsule_service.js';
20
19
  import type { RecipientTaggingStore } from '../storage/tagging_store/recipient_tagging_store.js';
21
20
  import type { SenderAddressBookStore } from '../storage/tagging_store/sender_address_book_store.js';
22
21
  import {
@@ -32,7 +31,7 @@ export class LogService {
32
31
  private readonly aztecNode: AztecNode,
33
32
  private readonly anchorBlockHeader: BlockHeader,
34
33
  private readonly keyStore: KeyStore,
35
- private readonly capsuleStore: CapsuleStore,
34
+ private readonly capsuleService: CapsuleService,
36
35
  private readonly recipientTaggingStore: RecipientTaggingStore,
37
36
  private readonly senderAddressBookStore: SenderAddressBookStore,
38
37
  private readonly addressStore: AddressStore,
@@ -42,7 +41,16 @@ export class LogService {
42
41
  this.log = createLogger('pxe:log_service', bindings);
43
42
  }
44
43
 
45
- public async bulkRetrieveLogs(logRetrievalRequests: LogRetrievalRequest[]): Promise<(LogRetrievalResponse | null)[]> {
44
+ public async fetchLogsByTag(
45
+ contractAddress: AztecAddress,
46
+ logRetrievalRequests: LogRetrievalRequest[],
47
+ ): Promise<(LogRetrievalResponse | null)[]> {
48
+ for (const request of logRetrievalRequests) {
49
+ if (!contractAddress.equals(request.contractAddress)) {
50
+ throw new Error(`Got a log retrieval request from ${request.contractAddress}, expected ${contractAddress}`);
51
+ }
52
+ }
53
+
46
54
  return await Promise.all(
47
55
  logRetrievalRequests.map(async request => {
48
56
  const [publicLog, privateLog] = await Promise.all([
@@ -51,8 +59,8 @@ export class LogService {
51
59
  ]);
52
60
 
53
61
  if (publicLog !== null && privateLog !== null) {
54
- throw new Error(
55
- `Found both a public and private log when searching for tag ${request.tag} from contract ${request.contractAddress}`,
62
+ this.log.warn(
63
+ `Found both a public and private log for tag ${request.tag} from contract ${request.contractAddress}. This may indicate a contract bug. Returning the public log.`,
56
64
  );
57
65
  }
58
66
 
@@ -74,9 +82,8 @@ export class LogService {
74
82
  if (logsForTag.length === 0) {
75
83
  return null;
76
84
  } else if (logsForTag.length > 1) {
77
- // TODO(#11627): handle this case
78
- throw new Error(
79
- `Got ${logsForTag.length} logs for tag ${tag} and contract ${contractAddress.toString()}. getPublicLogByTag currently only supports a single log per tag`,
85
+ this.log.warn(
86
+ `Expected at most 1 public log for tag ${tag} and contract ${contractAddress.toString()}, got ${logsForTag.length}. This may indicate a contract bug. Returning the first log.`,
80
87
  );
81
88
  }
82
89
 
@@ -98,9 +105,8 @@ export class LogService {
98
105
  if (logsForTag.length === 0) {
99
106
  return null;
100
107
  } else if (logsForTag.length > 1) {
101
- // TODO(#11627): handle this case
102
- throw new Error(
103
- `Got ${logsForTag.length} logs for tag ${siloedTag}. getPrivateLogByTag currently only supports a single log per tag`,
108
+ this.log.warn(
109
+ `Expected at most 1 private log for tag ${siloedTag}, got ${logsForTag.length}. This may indicate a contract bug. Returning the first log.`,
104
110
  );
105
111
  }
106
112
 
@@ -114,46 +120,40 @@ export class LogService {
114
120
  );
115
121
  }
116
122
 
117
- public async fetchTaggedLogs(contractAddress: AztecAddress, pendingTaggedLogArrayBaseSlot: Fr, scopes: AccessScopes) {
123
+ public async fetchTaggedLogs(
124
+ contractAddress: AztecAddress,
125
+ pendingTaggedLogArrayBaseSlot: Fr,
126
+ recipient: AztecAddress,
127
+ ) {
118
128
  this.log.verbose(`Fetching tagged logs for ${contractAddress.toString()}`);
119
129
 
120
130
  // We only load logs from block up to and including the anchor block number
121
131
  const anchorBlockNumber = this.anchorBlockHeader.getBlockNumber();
122
132
  const anchorBlockHash = await this.anchorBlockHeader.hash();
123
133
 
124
- // Determine recipients: use scopes if provided, otherwise get all accounts
125
- const recipients = scopes !== 'ALL_SCOPES' && scopes.length > 0 ? scopes : await this.keyStore.getAccounts();
126
-
127
- // For each recipient, fetch secrets, load logs, and store them.
128
- // We run these per-recipient tasks in parallel so that logs are loaded for all recipients concurrently.
129
- await Promise.all(
130
- recipients.map(async recipient => {
131
- // Get all secrets for this recipient (one per sender)
132
- const secrets = await this.#getSecretsForSenders(contractAddress, recipient);
133
-
134
- // Load logs for all sender-recipient pairs in parallel
135
- const logArrays = await Promise.all(
136
- secrets.map(secret =>
137
- loadPrivateLogsForSenderRecipientPair(
138
- secret,
139
- this.aztecNode,
140
- this.recipientTaggingStore,
141
- anchorBlockNumber,
142
- anchorBlockHash,
143
- this.jobId,
144
- ),
145
- ),
146
- );
134
+ // Get all secrets for this recipient (one per sender)
135
+ const secrets = await this.#getSecretsForSenders(contractAddress, recipient);
136
+
137
+ // Load logs for all sender-recipient pairs in parallel
138
+ const logArrays = await Promise.all(
139
+ secrets.map(secret =>
140
+ loadPrivateLogsForSenderRecipientPair(
141
+ secret,
142
+ this.aztecNode,
143
+ this.recipientTaggingStore,
144
+ anchorBlockNumber,
145
+ anchorBlockHash,
146
+ this.jobId,
147
+ ),
148
+ ),
149
+ );
147
150
 
148
- // Flatten all logs from all secrets
149
- const allLogs = logArrays.flat();
151
+ // Flatten all logs from all secrets
152
+ const allLogs = logArrays.flat();
150
153
 
151
- // Store the logs for this recipient
152
- if (allLogs.length > 0) {
153
- await this.#storePendingTaggedLogs(contractAddress, pendingTaggedLogArrayBaseSlot, recipient, allLogs);
154
- }
155
- }),
156
- );
154
+ if (allLogs.length > 0) {
155
+ await this.#storePendingTaggedLogs(contractAddress, pendingTaggedLogArrayBaseSlot, recipient, allLogs);
156
+ }
157
157
  }
158
158
 
159
159
  async #getSecretsForSenders(
@@ -201,13 +201,18 @@ export class LogService {
201
201
  scopedLog.txHash,
202
202
  scopedLog.noteHashes,
203
203
  scopedLog.firstNullifier,
204
- recipient,
205
204
  );
206
205
 
207
206
  return pendingTaggedLog.toFields();
208
207
  });
209
208
 
210
209
  // TODO: This looks like it could belong more at the oracle interface level
211
- return this.capsuleStore.appendToCapsuleArray(contractAddress, capsuleArrayBaseSlot, pendingTaggedLogs, this.jobId);
210
+ return this.capsuleService.appendToCapsuleArray(
211
+ contractAddress,
212
+ capsuleArrayBaseSlot,
213
+ pendingTaggedLogs,
214
+ this.jobId,
215
+ recipient,
216
+ );
212
217
  }
213
218
  }
@@ -0,0 +1,44 @@
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import type { AztecNode } from '@aztec/stdlib/interfaces/server';
3
+ import { MessageContext } from '@aztec/stdlib/logs';
4
+ import { TxHash } from '@aztec/stdlib/tx';
5
+
6
+ /** Resolves transaction hashes into the context needed to process messages. */
7
+ export class MessageContextService {
8
+ constructor(private readonly aztecNode: AztecNode) {}
9
+
10
+ /**
11
+ * Resolves a list of tx hashes into their message contexts.
12
+ *
13
+ * For each tx hash, looks up the corresponding tx effect and extracts the note hashes and first nullifier needed to
14
+ * process messages that originated from that transaction. Returns `null` for tx hashes that are zero, not yet
15
+ * available, or in blocks beyond the anchor block.
16
+ */
17
+ getMessageContextsByTxHash(txHashes: Fr[], anchorBlockNumber: number): Promise<(MessageContext | null)[]> {
18
+ // TODO: optimize, we might be hitting the node to get the same txHash repeatedly
19
+ return Promise.all(
20
+ txHashes.map(async txHashField => {
21
+ // A zero tx hash indicates a tx-less offchain message (e.g. one not tied to any onchain transaction).
22
+ // These messages don't have a transaction context to resolve, so we return null.
23
+ if (txHashField.isZero()) {
24
+ return null;
25
+ }
26
+
27
+ const txHash = TxHash.fromField(txHashField);
28
+ const txEffect = await this.aztecNode.getTxEffect(txHash);
29
+ if (!txEffect || txEffect.l2BlockNumber > anchorBlockNumber) {
30
+ return null;
31
+ }
32
+
33
+ // Every tx has at least one nullifier (the first nullifier derived from the tx hash). Hitting this condition
34
+ // would mean a buggy node, but since we need to access data.nullifiers[0], the defensive check does no harm.
35
+ const data = txEffect.data;
36
+ if (data.nullifiers.length === 0) {
37
+ throw new Error(`Tx effect for ${txHash} has no nullifiers`);
38
+ }
39
+
40
+ return new MessageContext(data.txHash, data.noteHashes, data.nullifiers[0]);
41
+ }),
42
+ );
43
+ }
44
+ }
@@ -121,7 +121,7 @@ export class NoteService {
121
121
  noteHash: Fr,
122
122
  nullifier: Fr,
123
123
  txHash: TxHash,
124
- recipient: AztecAddress,
124
+ scope: AztecAddress,
125
125
  ): Promise<void> {
126
126
  // We are going to store the new note in the NoteStore, which will let us later return it via `getNotes`.
127
127
  // There's two things we need to check before we do this however:
@@ -155,16 +155,28 @@ export class NoteService {
155
155
  this.aztecNode.findLeavesIndexes(anchorBlockHash, MerkleTreeId.NULLIFIER_TREE, [siloedNullifier]),
156
156
  ]);
157
157
  if (!txEffect) {
158
- throw new Error(`Could not find tx effect for tx hash ${txHash}`);
158
+ // We error out instead of just logging a warning and skipping the note because this would indicate a bug. This
159
+ // is because the node has already served info about this tx either when obtaining the log (TxScopedL2Log contain
160
+ // tx info) or when getting metadata for the offchain message (before the message got passed to `process_log`).
161
+ throw new Error(`Could not find tx effect for tx hash ${txHash} when processing a note.`);
159
162
  }
160
163
 
161
164
  if (txEffect.l2BlockNumber > anchorBlockNumber) {
162
- throw new Error(`Could not find tx effect for tx hash ${txHash} as of block number ${anchorBlockNumber}`);
165
+ // If the message was delivered onchain, this would indicate a bug: log sync should never load logs from blocks
166
+ // newer than the anchor block. If the note came via an offchain message, it would likely also be a bug, since we
167
+ // sync a new anchor block before calling `process_message`. For this not to be a bug, the message would need to
168
+ // come from a newer block than the anchor served by the node, implying the node isn't properly synced.
169
+ // We therefore error out here rather than assuming the offchain message was constructed by a malicious
170
+ // sender with the intention of bricking recipient's PXE (if we assumed that we would just ignore the message).
171
+ throw new Error(
172
+ `Obtained a newer tx effect for ${txHash} for a note validation request than the anchor block ${anchorBlockNumber}. This is a bug as we should not ever be processing a note from a newer block than the anchor block.`,
173
+ );
163
174
  }
164
175
 
165
176
  // Find the index of the note hash in the noteHashes array to determine note ordering within the tx
166
177
  const noteIndexInTx = txEffect.data.noteHashes.findIndex(nh => nh.equals(uniqueNoteHash));
167
178
  if (noteIndexInTx === -1) {
179
+ // Similar to the comment above - we error out as this would indicate a bug in nonce discovery.
168
180
  throw new Error(`Note hash ${noteHash} (uniqued as ${uniqueNoteHash}) is not present in tx ${txHash}`);
169
181
  }
170
182
 
@@ -184,8 +196,7 @@ export class NoteService {
184
196
  noteIndexInTx,
185
197
  );
186
198
 
187
- // The note was found by `recipient`, so we use that as the scope when storing the note.
188
- await this.noteStore.addNotes([noteDao], recipient, this.jobId);
199
+ await this.noteStore.addNotes([noteDao], scope, this.jobId);
189
200
 
190
201
  if (nullifierIndex !== undefined) {
191
202
  // We found nullifier index which implies that the note has already been nullified.
@@ -4,9 +4,9 @@
4
4
  ///
5
5
  /// @dev Whenever a contract function or Noir test is run, the `aztec_utl_assertCompatibleOracleVersion` oracle is called
6
6
  /// and if the oracle version is incompatible an error is thrown.
7
- export const ORACLE_VERSION = 14;
7
+ export const ORACLE_VERSION = 21;
8
8
 
9
9
  /// This hash is computed as by hashing the Oracle interface and it is used to detect when the Oracle interface changes,
10
10
  /// which in turn implies that you need to update the ORACLE_VERSION constant in this file and in
11
11
  /// `noir-projects/aztec-nr/aztec/src/oracle/version.nr`.
12
- export const ORACLE_INTERFACE_HASH = '9fb918682455c164ce8dd3acb71c751e2b9b2fc48913604069c9ea885fa378ca';
12
+ export const ORACLE_INTERFACE_HASH = '83f1de1a9741a34916fd58cf12b857d0bac90f74bf00751b20304301a3f5c8eb';