@aztec/pxe 0.0.1-commit.6230a0c → 0.0.1-commit.64b6bbb

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 (65) hide show
  1. package/dest/block_synchronizer/block_synchronizer.d.ts +4 -2
  2. package/dest/block_synchronizer/block_synchronizer.d.ts.map +1 -1
  3. package/dest/block_synchronizer/block_synchronizer.js +7 -1
  4. package/dest/contract_function_simulator/contract_function_simulator.d.ts +50 -26
  5. package/dest/contract_function_simulator/contract_function_simulator.d.ts.map +1 -1
  6. package/dest/contract_function_simulator/contract_function_simulator.js +66 -30
  7. package/dest/contract_function_simulator/oracle/interfaces.d.ts +2 -2
  8. package/dest/contract_function_simulator/oracle/interfaces.d.ts.map +1 -1
  9. package/dest/contract_function_simulator/oracle/oracle.d.ts +2 -2
  10. package/dest/contract_function_simulator/oracle/oracle.d.ts.map +1 -1
  11. package/dest/contract_function_simulator/oracle/oracle.js +3 -3
  12. package/dest/contract_function_simulator/oracle/private_execution_oracle.d.ts +35 -35
  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 +64 -19
  15. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +31 -11
  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 +53 -29
  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} +6 -12
  24. package/dest/debug/pxe_debug_utils.d.ts +12 -9
  25. package/dest/debug/pxe_debug_utils.d.ts.map +1 -1
  26. package/dest/debug/pxe_debug_utils.js +16 -15
  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 +9 -1
  30. package/dest/entrypoints/client/lazy/utils.d.ts +1 -1
  31. package/dest/entrypoints/client/lazy/utils.d.ts.map +1 -1
  32. package/dest/entrypoints/client/lazy/utils.js +9 -1
  33. package/dest/entrypoints/server/index.d.ts +2 -2
  34. package/dest/entrypoints/server/index.d.ts.map +1 -1
  35. package/dest/entrypoints/server/index.js +1 -1
  36. package/dest/entrypoints/server/utils.js +9 -1
  37. package/dest/oracle_version.d.ts +2 -2
  38. package/dest/oracle_version.js +2 -2
  39. package/dest/pxe.d.ts +64 -22
  40. package/dest/pxe.d.ts.map +1 -1
  41. package/dest/pxe.js +62 -37
  42. package/dest/storage/contract_store/contract_store.js +5 -5
  43. package/dest/storage/note_store/note_store.d.ts +1 -1
  44. package/dest/storage/note_store/note_store.d.ts.map +1 -1
  45. package/dest/storage/note_store/note_store.js +3 -0
  46. package/package.json +25 -16
  47. package/src/block_synchronizer/block_synchronizer.ts +6 -0
  48. package/src/contract_function_simulator/contract_function_simulator.ts +109 -71
  49. package/src/contract_function_simulator/oracle/interfaces.ts +1 -1
  50. package/src/contract_function_simulator/oracle/oracle.ts +3 -3
  51. package/src/contract_function_simulator/oracle/private_execution_oracle.ts +83 -96
  52. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +94 -28
  53. package/src/contract_sync/contract_sync_service.ts +129 -0
  54. package/src/contract_sync/{index.ts → helpers.ts} +6 -32
  55. package/src/debug/pxe_debug_utils.ts +45 -17
  56. package/src/entrypoints/client/bundle/utils.ts +9 -1
  57. package/src/entrypoints/client/lazy/utils.ts +9 -1
  58. package/src/entrypoints/server/index.ts +1 -1
  59. package/src/entrypoints/server/utils.ts +7 -7
  60. package/src/oracle_version.ts +2 -2
  61. package/src/pxe.ts +124 -72
  62. package/src/storage/contract_store/contract_store.ts +4 -4
  63. package/src/storage/note_store/note_store.ts +4 -0
  64. package/dest/contract_sync/index.d.ts +0 -24
  65. package/dest/contract_sync/index.d.ts.map +0 -1
@@ -417,7 +417,7 @@ export class Oracle {
417
417
  return Promise.resolve([]);
418
418
  }
419
419
 
420
- utilityDebugLog(
420
+ async utilityLog(
421
421
  level: ACVMField[],
422
422
  message: ACVMField[],
423
423
  _ignoredFieldsSize: ACVMField[],
@@ -426,8 +426,8 @@ export class Oracle {
426
426
  const levelFr = Fr.fromString(level[0]);
427
427
  const messageStr = message.map(acvmField => String.fromCharCode(Fr.fromString(acvmField).toNumber())).join('');
428
428
  const fieldsFr = fields.map(Fr.fromString);
429
- this.handlerAsMisc().utilityDebugLog(levelFr.toNumber(), messageStr, fieldsFr);
430
- return Promise.resolve([]);
429
+ await this.handlerAsMisc().utilityLog(levelFr.toNumber(), messageStr, fieldsFr);
430
+ return [];
431
431
  }
432
432
 
433
433
  // This function's name is directly hardcoded in `circuit_recorder.ts`. Don't forget to update it there if you
@@ -2,7 +2,6 @@ import { MAX_FR_CALLDATA_TO_ALL_ENQUEUED_CALLS, PRIVATE_CONTEXT_INPUTS_LENGTH }
2
2
  import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { createLogger } from '@aztec/foundation/log';
4
4
  import { Timer } from '@aztec/foundation/timer';
5
- import type { KeyStore } from '@aztec/key-store';
6
5
  import { type CircuitSimulator, toACVMWitness } from '@aztec/simulator/client';
7
6
  import {
8
7
  type FunctionAbi,
@@ -12,33 +11,22 @@ import {
12
11
  type NoteSelector,
13
12
  countArgumentsSize,
14
13
  } from '@aztec/stdlib/abi';
15
- import type { AuthWitness } from '@aztec/stdlib/auth-witness';
16
14
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
17
15
  import { siloNullifier } from '@aztec/stdlib/hash';
18
- import type { AztecNode } from '@aztec/stdlib/interfaces/client';
19
16
  import { PrivateContextInputs } from '@aztec/stdlib/kernel';
20
17
  import { type ContractClassLog, DirectionalAppTaggingSecret, type PreTag } from '@aztec/stdlib/logs';
21
18
  import { Tag } from '@aztec/stdlib/logs';
22
19
  import { Note, type NoteStatus } from '@aztec/stdlib/note';
23
20
  import {
24
- type BlockHeader,
25
21
  CallContext,
26
- Capsule,
27
22
  CountedContractClassLog,
28
23
  NoteAndSlot,
29
24
  PrivateCallExecutionResult,
30
25
  type TxContext,
31
26
  } from '@aztec/stdlib/tx';
32
27
 
33
- import { ensureContractSynced } from '../../contract_sync/index.js';
28
+ import type { ContractSyncService } from '../../contract_sync/contract_sync_service.js';
34
29
  import { NoteService } from '../../notes/note_service.js';
35
- import type { AddressStore } from '../../storage/address_store/address_store.js';
36
- import type { CapsuleStore } from '../../storage/capsule_store/capsule_store.js';
37
- import type { ContractStore } from '../../storage/contract_store/contract_store.js';
38
- import type { NoteStore } from '../../storage/note_store/note_store.js';
39
- import type { PrivateEventStore } from '../../storage/private_event_store/private_event_store.js';
40
- import type { RecipientTaggingStore } from '../../storage/tagging_store/recipient_tagging_store.js';
41
- import type { SenderAddressBookStore } from '../../storage/tagging_store/sender_address_book_store.js';
42
30
  import type { SenderTaggingStore } from '../../storage/tagging_store/sender_tagging_store.js';
43
31
  import { syncSenderTaggingIndexes } from '../../tagging/index.js';
44
32
  import type { ExecutionNoteCache } from '../execution_note_cache.js';
@@ -47,7 +35,25 @@ import type { HashedValuesCache } from '../hashed_values_cache.js';
47
35
  import { pickNotes } from '../pick_notes.js';
48
36
  import type { IPrivateExecutionOracle, NoteData } from './interfaces.js';
49
37
  import { executePrivateFunction } from './private_execution.js';
50
- import { UtilityExecutionOracle } from './utility_execution_oracle.js';
38
+ import { UtilityExecutionOracle, type UtilityExecutionOracleArgs } from './utility_execution_oracle.js';
39
+
40
+ /** Args for PrivateExecutionOracle constructor. */
41
+ export type PrivateExecutionOracleArgs = Omit<UtilityExecutionOracleArgs, 'contractAddress'> & {
42
+ argsHash: Fr;
43
+ txContext: TxContext;
44
+ callContext: CallContext;
45
+ /** Needed to trigger contract synchronization before nested calls */
46
+ utilityExecutor: (call: FunctionCall) => Promise<void>;
47
+ executionCache: HashedValuesCache;
48
+ noteCache: ExecutionNoteCache;
49
+ taggingIndexCache: ExecutionTaggingIndexCache;
50
+ senderTaggingStore: SenderTaggingStore;
51
+ contractSyncService: ContractSyncService;
52
+ totalPublicCalldataCount?: number;
53
+ sideEffectCounter?: number;
54
+ senderForTags?: AztecAddress;
55
+ simulator?: CircuitSimulator;
56
+ };
51
57
 
52
58
  /**
53
59
  * The execution oracle for the private part of a transaction.
@@ -69,56 +75,39 @@ export class PrivateExecutionOracle extends UtilityExecutionOracle implements IP
69
75
  private offchainEffects: { data: Fr[] }[] = [];
70
76
  private nestedExecutionResults: PrivateCallExecutionResult[] = [];
71
77
 
72
- constructor(
73
- private readonly argsHash: Fr,
74
- private readonly txContext: TxContext,
75
- private readonly callContext: CallContext,
76
- /** Header of a block whose state is used during private execution (not the block the transaction is included in). */
77
- protected override readonly anchorBlockHeader: BlockHeader,
78
- /** Needed to trigger contract synchronization before nested calls */
79
- private readonly utilityExecutor: (call: FunctionCall) => Promise<void>,
80
- /** List of transient auth witnesses to be used during this simulation */
81
- authWitnesses: AuthWitness[],
82
- capsules: Capsule[],
83
- private readonly executionCache: HashedValuesCache,
84
- private readonly noteCache: ExecutionNoteCache,
85
- private readonly taggingIndexCache: ExecutionTaggingIndexCache,
86
- contractStore: ContractStore,
87
- noteStore: NoteStore,
88
- keyStore: KeyStore,
89
- addressStore: AddressStore,
90
- aztecNode: AztecNode,
91
- private readonly senderTaggingStore: SenderTaggingStore,
92
- recipientTaggingStore: RecipientTaggingStore,
93
- senderAddressBookStore: SenderAddressBookStore,
94
- capsuleStore: CapsuleStore,
95
- privateEventStore: PrivateEventStore,
96
- jobId: string,
97
- private totalPublicCalldataCount: number = 0,
98
- protected sideEffectCounter: number = 0,
99
- log = createLogger('simulator:client_execution_context'),
100
- scopes?: AztecAddress[],
101
- private senderForTags?: AztecAddress,
102
- private simulator?: CircuitSimulator,
103
- ) {
104
- super(
105
- callContext.contractAddress,
106
- authWitnesses,
107
- capsules,
108
- anchorBlockHeader,
109
- contractStore,
110
- noteStore,
111
- keyStore,
112
- addressStore,
113
- aztecNode,
114
- recipientTaggingStore,
115
- senderAddressBookStore,
116
- capsuleStore,
117
- privateEventStore,
118
- jobId,
119
- log,
120
- scopes,
121
- );
78
+ private readonly argsHash: Fr;
79
+ private readonly txContext: TxContext;
80
+ private readonly callContext: CallContext;
81
+ private readonly utilityExecutor: (call: FunctionCall) => Promise<void>;
82
+ private readonly executionCache: HashedValuesCache;
83
+ private readonly noteCache: ExecutionNoteCache;
84
+ private readonly taggingIndexCache: ExecutionTaggingIndexCache;
85
+ private readonly senderTaggingStore: SenderTaggingStore;
86
+ private readonly contractSyncService: ContractSyncService;
87
+ private totalPublicCalldataCount: number;
88
+ protected sideEffectCounter: number;
89
+ private senderForTags?: AztecAddress;
90
+ private readonly simulator?: CircuitSimulator;
91
+
92
+ constructor(args: PrivateExecutionOracleArgs) {
93
+ super({
94
+ ...args,
95
+ contractAddress: args.callContext.contractAddress,
96
+ log: args.log ?? createLogger('simulator:client_execution_context'),
97
+ });
98
+ this.argsHash = args.argsHash;
99
+ this.txContext = args.txContext;
100
+ this.callContext = args.callContext;
101
+ this.utilityExecutor = args.utilityExecutor;
102
+ this.executionCache = args.executionCache;
103
+ this.noteCache = args.noteCache;
104
+ this.taggingIndexCache = args.taggingIndexCache;
105
+ this.senderTaggingStore = args.senderTaggingStore;
106
+ this.contractSyncService = args.contractSyncService;
107
+ this.totalPublicCalldataCount = args.totalPublicCalldataCount ?? 0;
108
+ this.sideEffectCounter = args.sideEffectCounter ?? 0;
109
+ this.senderForTags = args.senderForTags;
110
+ this.simulator = args.simulator;
122
111
  }
123
112
 
124
113
  public getPrivateContextInputs(): PrivateContextInputs {
@@ -537,13 +526,10 @@ export class PrivateExecutionOracle extends UtilityExecutionOracle implements IP
537
526
 
538
527
  isStaticCall = isStaticCall || this.callContext.isStaticCall;
539
528
 
540
- await ensureContractSynced(
529
+ await this.contractSyncService.ensureContractSynced(
541
530
  targetContractAddress,
542
531
  functionSelector,
543
532
  this.utilityExecutor,
544
- this.aztecNode,
545
- this.contractStore,
546
- this.noteStore,
547
533
  this.anchorBlockHeader,
548
534
  this.jobId,
549
535
  );
@@ -557,40 +543,41 @@ export class PrivateExecutionOracle extends UtilityExecutionOracle implements IP
557
543
 
558
544
  const derivedCallContext = await this.deriveCallContext(targetContractAddress, targetArtifact, isStaticCall);
559
545
 
560
- const privateExecutionOracle = new PrivateExecutionOracle(
546
+ const privateExecutionOracle = new PrivateExecutionOracle({
561
547
  argsHash,
562
- derivedTxContext,
563
- derivedCallContext,
564
- this.anchorBlockHeader,
565
- this.utilityExecutor,
566
- this.authWitnesses,
567
- this.capsules,
568
- this.executionCache,
569
- this.noteCache,
570
- this.taggingIndexCache,
571
- this.contractStore,
572
- this.noteStore,
573
- this.keyStore,
574
- this.addressStore,
575
- this.aztecNode,
576
- this.senderTaggingStore,
577
- this.recipientTaggingStore,
578
- this.senderAddressBookStore,
579
- this.capsuleStore,
580
- this.privateEventStore,
581
- this.jobId,
582
- this.totalPublicCalldataCount,
548
+ txContext: derivedTxContext,
549
+ callContext: derivedCallContext,
550
+ anchorBlockHeader: this.anchorBlockHeader,
551
+ utilityExecutor: this.utilityExecutor,
552
+ authWitnesses: this.authWitnesses,
553
+ capsules: this.capsules,
554
+ executionCache: this.executionCache,
555
+ noteCache: this.noteCache,
556
+ taggingIndexCache: this.taggingIndexCache,
557
+ contractStore: this.contractStore,
558
+ noteStore: this.noteStore,
559
+ keyStore: this.keyStore,
560
+ addressStore: this.addressStore,
561
+ aztecNode: this.aztecNode,
562
+ senderTaggingStore: this.senderTaggingStore,
563
+ recipientTaggingStore: this.recipientTaggingStore,
564
+ senderAddressBookStore: this.senderAddressBookStore,
565
+ capsuleStore: this.capsuleStore,
566
+ privateEventStore: this.privateEventStore,
567
+ contractSyncService: this.contractSyncService,
568
+ jobId: this.jobId,
569
+ totalPublicCalldataCount: this.totalPublicCalldataCount,
583
570
  sideEffectCounter,
584
- this.log,
585
- this.scopes,
586
- this.senderForTags,
587
- this.simulator,
588
- );
571
+ log: this.log,
572
+ scopes: this.scopes,
573
+ senderForTags: this.senderForTags,
574
+ simulator: this.simulator!,
575
+ });
589
576
 
590
577
  const setupTime = simulatorSetupTimer.ms();
591
578
 
592
579
  const childExecutionResult = await executePrivateFunction(
593
- this.simulator,
580
+ this.simulator!,
594
581
  privateExecutionOracle,
595
582
  targetArtifact,
596
583
  targetContractAddress,
@@ -3,7 +3,7 @@ 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';
@@ -40,6 +40,27 @@ import { pickNotes } from '../pick_notes.js';
40
40
  import type { IMiscOracle, IUtilityExecutionOracle, NoteData } from './interfaces.js';
41
41
  import { MessageLoadOracleInputs } from './message_load_oracle_inputs.js';
42
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
+
43
64
  /**
44
65
  * The oracle for an execution of utility contract functions.
45
66
  */
@@ -47,27 +68,43 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
47
68
  isMisc = true as const;
48
69
  isUtility = true as const;
49
70
 
50
- private aztecNrDebugLog = createLogger('aztec-nr:debug_log');
51
-
52
- constructor(
53
- protected readonly contractAddress: AztecAddress,
54
- /** List of transient auth witnesses to be used during this simulation */
55
- protected readonly authWitnesses: AuthWitness[],
56
- protected readonly capsules: Capsule[], // TODO(#12425): Rename to transientCapsules
57
- protected readonly anchorBlockHeader: BlockHeader,
58
- protected readonly contractStore: ContractStore,
59
- protected readonly noteStore: NoteStore,
60
- protected readonly keyStore: KeyStore,
61
- protected readonly addressStore: AddressStore,
62
- protected readonly aztecNode: AztecNode,
63
- protected readonly recipientTaggingStore: RecipientTaggingStore,
64
- protected readonly senderAddressBookStore: SenderAddressBookStore,
65
- protected readonly capsuleStore: CapsuleStore,
66
- protected readonly privateEventStore: PrivateEventStore,
67
- protected readonly jobId: string,
68
- protected log = createLogger('simulator:client_view_context'),
69
- protected readonly scopes?: AztecAddress[],
70
- ) {}
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
+ }
71
108
 
72
109
  public utilityAssertCompatibleOracleVersion(version: number): void {
73
110
  if (version !== ORACLE_VERSION) {
@@ -88,8 +125,16 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
88
125
  * @param pkMHash - The master public key hash.
89
126
  * @returns A Promise that resolves to nullifier keys.
90
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.
91
129
  */
92
- 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
+ }
93
138
  return this.keyStore.getKeyValidationRequest(pkMHash, this.contractAddress);
94
139
  }
95
140
 
@@ -289,8 +334,13 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
289
334
  * @returns A boolean indicating whether the nullifier exists in the tree or not.
290
335
  */
291
336
  public async utilityCheckNullifierExists(innerNullifier: Fr) {
292
- const nullifier = await siloNullifier(this.contractAddress, innerNullifier!);
293
- 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
+ ]);
294
344
  return leafIndex?.data !== undefined;
295
345
  }
296
346
 
@@ -341,12 +391,28 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
341
391
  return values;
342
392
  }
343
393
 
344
- 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> {
345
410
  if (!LogLevels[level]) {
346
- throw new Error(`Invalid debug log level: ${level}`);
411
+ throw new Error(`Invalid log level: ${level}`);
347
412
  }
348
413
  const levelName = LogLevels[level];
349
- this.aztecNrDebugLog[levelName](`${applyStringFormatting(message, fields)}`);
414
+ const logger = await this.#getContractLogger();
415
+ logger[levelName](`${applyStringFormatting(message, fields)}`);
350
416
  }
351
417
 
352
418
  public async utilityFetchTaggedLogs(pendingTaggedLogArrayBaseSlot: Fr) {
@@ -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
+ }
@@ -45,7 +45,7 @@ export async function syncState(
45
45
  utilityExecutor: (privateSyncCall: FunctionCall) => Promise<any>,
46
46
  noteStore: NoteStore,
47
47
  aztecNode: AztecNode,
48
- header: BlockHeader,
48
+ anchorBlockHeader: BlockHeader,
49
49
  jobId: string,
50
50
  ) {
51
51
  // Protocol contracts don't have private state to sync
@@ -57,7 +57,7 @@ export async function syncState(
57
57
  );
58
58
  }
59
59
 
60
- const noteService = new NoteService(noteStore, aztecNode, header, jobId);
60
+ const noteService = new NoteService(noteStore, aztecNode, anchorBlockHeader, jobId);
61
61
 
62
62
  // Both sync_state and syncNoteNullifiers interact with the note store, but running them in parallel is safe
63
63
  // because note store is designed to handle concurrent operations.
@@ -68,9 +68,12 @@ export async function syncState(
68
68
  /**
69
69
  * Verify that the current class id of a contract obtained from AztecNode is the same as the one in contract data
70
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.
71
74
  * @param header - The header of the block at which to verify the current class id.
72
75
  */
73
- async function verifyCurrentClassId(
76
+ export async function verifyCurrentClassId(
74
77
  contractAddress: AztecAddress,
75
78
  aztecNode: AztecNode,
76
79
  contractStore: ContractStore,
@@ -88,32 +91,3 @@ async function verifyCurrentClassId(
88
91
  );
89
92
  }
90
93
  }
91
-
92
- /**
93
- * Ensures the contract's private state is synchronized and that the PXE holds the current class artifact for
94
- * the contract.
95
- */
96
- export async function ensureContractSynced(
97
- contractAddress: AztecAddress,
98
- functionToInvokeAfterSync: FunctionSelector | null,
99
- utilityExecutor: (call: FunctionCall) => Promise<any>,
100
- aztecNode: AztecNode,
101
- contractStore: ContractStore,
102
- noteStore: NoteStore,
103
- header: BlockHeader,
104
- jobId: string,
105
- ): Promise<void> {
106
- await Promise.all([
107
- syncState(
108
- contractAddress,
109
- contractStore,
110
- functionToInvokeAfterSync,
111
- utilityExecutor,
112
- noteStore,
113
- aztecNode,
114
- header,
115
- jobId,
116
- ),
117
- verifyCurrentClassId(contractAddress, aztecNode, contractStore, header),
118
- ]);
119
- }