@aztec/aztec-node 0.0.1-commit.7d4e6cd → 0.0.1-commit.808bf7f90

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.
@@ -1,21 +1,15 @@
1
1
  import { Archiver, createArchiver } from '@aztec/archiver';
2
2
  import { BBCircuitVerifier, QueuedIVCVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
3
3
  import { type BlobClientInterface, createBlobClientWithFileStores } from '@aztec/blob-client/client';
4
- import {
5
- ARCHIVE_HEIGHT,
6
- INITIAL_L2_BLOCK_NUM,
7
- type L1_TO_L2_MSG_TREE_HEIGHT,
8
- type NOTE_HASH_TREE_HEIGHT,
9
- type NULLIFIER_TREE_HEIGHT,
10
- type PUBLIC_DATA_TREE_HEIGHT,
11
- } from '@aztec/constants';
4
+ import { Blob } from '@aztec/blob-lib';
5
+ import { ARCHIVE_HEIGHT, type L1_TO_L2_MSG_TREE_HEIGHT, type NOTE_HASH_TREE_HEIGHT } from '@aztec/constants';
12
6
  import { EpochCache, type EpochCacheInterface } from '@aztec/epoch-cache';
13
7
  import { createEthereumChain } from '@aztec/ethereum/chain';
14
8
  import { getPublicClient } from '@aztec/ethereum/client';
15
9
  import { RegistryContract, RollupContract } from '@aztec/ethereum/contracts';
16
10
  import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
17
11
  import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
18
- import { compactArray, pick } from '@aztec/foundation/collection';
12
+ import { compactArray, pick, unique } from '@aztec/foundation/collection';
19
13
  import { Fr } from '@aztec/foundation/curves/bn254';
20
14
  import { EthAddress } from '@aztec/foundation/eth-address';
21
15
  import { BadRequestError } from '@aztec/foundation/json-rpc';
@@ -23,15 +17,14 @@ import { type Logger, createLogger } from '@aztec/foundation/log';
23
17
  import { count } from '@aztec/foundation/string';
24
18
  import { DateProvider, Timer } from '@aztec/foundation/timer';
25
19
  import { MembershipWitness, SiblingPath } from '@aztec/foundation/trees';
26
- import { KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
20
+ import { type KeyStore, KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
27
21
  import { trySnapshotSync, uploadSnapshot } from '@aztec/node-lib/actions';
28
- import {
29
- createForwarderL1TxUtilsFromEthSigner,
30
- createL1TxUtilsWithBlobsFromEthSigner,
31
- } from '@aztec/node-lib/factories';
22
+ import { createForwarderL1TxUtilsFromSigners, createL1TxUtilsFromSigners } from '@aztec/node-lib/factories';
32
23
  import { type P2P, type P2PClientDeps, createP2PClient, getDefaultAllowedSetupFunctions } from '@aztec/p2p';
33
24
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
34
- import { BlockBuilder, GlobalVariableBuilder, SequencerClient, type SequencerPublisher } from '@aztec/sequencer-client';
25
+ import { type ProverNode, type ProverNodeDeps, createProverNode } from '@aztec/prover-node';
26
+ import { createKeyStoreForProver } from '@aztec/prover-node/config';
27
+ import { GlobalVariableBuilder, SequencerClient, type SequencerPublisher } from '@aztec/sequencer-client';
35
28
  import { PublicProcessorFactory } from '@aztec/simulator/server';
36
29
  import {
37
30
  AttestationsBlockWatcher,
@@ -43,13 +36,12 @@ import {
43
36
  import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
44
37
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
45
38
  import {
39
+ type BlockData,
40
+ BlockHash,
46
41
  type BlockParameter,
47
42
  type DataInBlock,
48
- type L2Block,
49
- L2BlockHash,
50
- L2BlockNew,
43
+ L2Block,
51
44
  type L2BlockSource,
52
- type PublishedL2Block,
53
45
  } from '@aztec/stdlib/block';
54
46
  import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
55
47
  import type {
@@ -78,7 +70,8 @@ import {
78
70
  type WorldStateSynchronizer,
79
71
  tryStop,
80
72
  } from '@aztec/stdlib/interfaces/server';
81
- import type { LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
73
+ import type { DebugLogStore, LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
74
+ import { InMemoryDebugLogStore, NullDebugLogStore } from '@aztec/stdlib/logs';
82
75
  import { InboxLeaf, type L1ToL2MessageSource } from '@aztec/stdlib/messaging';
83
76
  import { P2PClientType } from '@aztec/stdlib/p2p';
84
77
  import type { Offense, SlashPayloadRound } from '@aztec/stdlib/slashing';
@@ -128,6 +121,7 @@ import { NodeMetrics } from './node_metrics.js';
128
121
  */
129
122
  export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
130
123
  private metrics: NodeMetrics;
124
+ private initialHeaderHashPromise: Promise<BlockHash> | undefined = undefined;
131
125
 
132
126
  // Prevent two snapshot operations to happen simultaneously
133
127
  private isUploadingSnapshot = false;
@@ -143,6 +137,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
143
137
  protected readonly l1ToL2MessageSource: L1ToL2MessageSource,
144
138
  protected readonly worldStateSynchronizer: WorldStateSynchronizer,
145
139
  protected readonly sequencer: SequencerClient | undefined,
140
+ protected readonly proverNode: ProverNode | undefined,
146
141
  protected readonly slasherClient: SlasherClientInterface | undefined,
147
142
  protected readonly validatorsSentinel: Sentinel | undefined,
148
143
  protected readonly epochPruneWatcher: EpochPruneWatcher | undefined,
@@ -155,12 +150,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
155
150
  private telemetry: TelemetryClient = getTelemetryClient(),
156
151
  private log = createLogger('node'),
157
152
  private blobClient?: BlobClientInterface,
153
+ private validatorClient?: ValidatorClient,
154
+ private keyStoreManager?: KeystoreManager,
155
+ private debugLogStore: DebugLogStore = new NullDebugLogStore(),
158
156
  ) {
159
157
  this.metrics = new NodeMetrics(telemetry, 'AztecNodeService');
160
158
  this.tracer = telemetry.getTracer('AztecNodeService');
161
159
 
162
160
  this.log.info(`Aztec Node version: ${this.packageVersion}`);
163
161
  this.log.info(`Aztec Node started on chain 0x${l1ChainId.toString(16)}`, config.l1Contracts);
162
+
163
+ // A defensive check that protects us against introducing a bug in the complex `createAndSync` function. We must
164
+ // never have debugLogStore enabled when not in test mode because then we would be accumulating debug logs in
165
+ // memory which could be a DoS vector on the sequencer (since no fees are paid for debug logs).
166
+ if (debugLogStore.isEnabled && config.realProofs) {
167
+ throw new Error('debugLogStore should never be enabled when realProofs are set');
168
+ }
164
169
  }
165
170
 
166
171
  public async getWorldStateSyncStatus(): Promise<WorldStateSyncStatus> {
@@ -185,10 +190,12 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
185
190
  publisher?: SequencerPublisher;
186
191
  dateProvider?: DateProvider;
187
192
  p2pClientDeps?: P2PClientDeps<P2PClientType.Full>;
193
+ proverNodeDeps?: Partial<ProverNodeDeps>;
188
194
  } = {},
189
195
  options: {
190
196
  prefilledPublicData?: PublicDataTreeLeaf[];
191
197
  dontStartSequencer?: boolean;
198
+ dontStartProverNode?: boolean;
192
199
  } = {},
193
200
  ): Promise<AztecNodeService> {
194
201
  const config = { ...inputConfig }; // Copy the config so we dont mutate the input object
@@ -198,16 +205,29 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
198
205
  const dateProvider = deps.dateProvider ?? new DateProvider();
199
206
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
200
207
 
201
- // Build a key store from file if given or from environment otherwise
208
+ // Build a key store from file if given or from environment otherwise.
209
+ // We keep the raw KeyStore available so we can merge with prover keys if enableProverNode is set.
202
210
  let keyStoreManager: KeystoreManager | undefined;
203
211
  const keyStoreProvided = config.keyStoreDirectory !== undefined && config.keyStoreDirectory.length > 0;
204
212
  if (keyStoreProvided) {
205
213
  const keyStores = loadKeystores(config.keyStoreDirectory!);
206
214
  keyStoreManager = new KeystoreManager(mergeKeystores(keyStores));
207
215
  } else {
208
- const keyStore = createKeyStoreForValidator(config);
209
- if (keyStore) {
210
- keyStoreManager = new KeystoreManager(keyStore);
216
+ const rawKeyStores: KeyStore[] = [];
217
+ const validatorKeyStore = createKeyStoreForValidator(config);
218
+ if (validatorKeyStore) {
219
+ rawKeyStores.push(validatorKeyStore);
220
+ }
221
+ if (config.enableProverNode) {
222
+ const proverKeyStore = createKeyStoreForProver(config);
223
+ if (proverKeyStore) {
224
+ rawKeyStores.push(proverKeyStore);
225
+ }
226
+ }
227
+ if (rawKeyStores.length > 0) {
228
+ keyStoreManager = new KeystoreManager(
229
+ rawKeyStores.length === 1 ? rawKeyStores[0] : mergeKeystores(rawKeyStores),
230
+ );
211
231
  }
212
232
  }
213
233
 
@@ -218,10 +238,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
218
238
  if (keyStoreManager === undefined) {
219
239
  throw new Error('Failed to create key store, a requirement for running a validator');
220
240
  }
221
- if (!keyStoreProvided) {
222
- log.warn(
223
- 'KEY STORE CREATED FROM ENVIRONMENT, IT IS RECOMMENDED TO USE A FILE-BASED KEY STORE IN PRODUCTION ENVIRONMENTS',
224
- );
241
+ if (!keyStoreProvided && process.env.NODE_ENV !== 'test') {
242
+ log.warn("Keystore created from env: it's recommended to use a file-based key store for production");
225
243
  }
226
244
  ValidatorClient.validateKeyStoreConfiguration(keyStoreManager, log);
227
245
  }
@@ -263,7 +281,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
263
281
  );
264
282
  }
265
283
 
266
- const blobClient = await createBlobClientWithFileStores(config, createLogger('node:blob-client:client'));
284
+ const blobClient = await createBlobClientWithFileStores(config, log.createChild('blob-client'));
267
285
 
268
286
  // attempt snapshot sync if possible
269
287
  await trySnapshotSync(config, log);
@@ -287,9 +305,19 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
287
305
  config.realProofs || config.debugForceTxProofVerification
288
306
  ? await BBCircuitVerifier.new(config)
289
307
  : new TestCircuitVerifier(config.proverTestVerificationDelayMs);
308
+
309
+ let debugLogStore: DebugLogStore;
290
310
  if (!config.realProofs) {
291
311
  log.warn(`Aztec node is accepting fake proofs`);
312
+
313
+ debugLogStore = new InMemoryDebugLogStore();
314
+ log.info(
315
+ 'Aztec node started in test mode (realProofs set to false) hence debug logs from public functions will be collected and served',
316
+ );
317
+ } else {
318
+ debugLogStore = new NullDebugLogStore();
292
319
  }
320
+
293
321
  const proofVerifier = new QueuedIVCVerifier(config, circuitVerifier);
294
322
 
295
323
  // create the tx pool and the p2p client, which will need the l2 block source
@@ -309,18 +337,10 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
309
337
  // We should really not be modifying the config object
310
338
  config.txPublicSetupAllowList = config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
311
339
 
312
- // Create BlockBuilder for EpochPruneWatcher (slasher functionality)
313
- const blockBuilder = new BlockBuilder(
314
- { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
315
- worldStateSynchronizer,
316
- archiver,
317
- dateProvider,
318
- telemetry,
319
- );
320
-
321
340
  // Create FullNodeCheckpointsBuilder for validator and non-validator block proposal handling
322
341
  const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder(
323
342
  { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
343
+ worldStateSynchronizer,
324
344
  archiver,
325
345
  dateProvider,
326
346
  telemetry,
@@ -330,7 +350,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
330
350
  const watchers: Watcher[] = [];
331
351
 
332
352
  // Create validator client if required
333
- const validatorClient = createValidatorClient(config, {
353
+ const validatorClient = await createValidatorClient(config, {
334
354
  checkpointsBuilder: validatorCheckpointsBuilder,
335
355
  worldState: worldStateSynchronizer,
336
356
  p2pClient,
@@ -387,7 +407,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
387
407
  archiver,
388
408
  epochCache,
389
409
  p2pClient.getTxProvider(),
390
- blockBuilder,
410
+ validatorCheckpointsBuilder,
391
411
  config,
392
412
  );
393
413
  watchers.push(epochPruneWatcher);
@@ -434,27 +454,29 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
434
454
  );
435
455
  await slasherClient.start();
436
456
 
437
- const l1TxUtils = config.publisherForwarderAddress
438
- ? await createForwarderL1TxUtilsFromEthSigner(
457
+ const l1TxUtils = config.sequencerPublisherForwarderAddress
458
+ ? await createForwarderL1TxUtilsFromSigners(
439
459
  publicClient,
440
460
  keyStoreManager!.createAllValidatorPublisherSigners(),
441
- config.publisherForwarderAddress,
461
+ config.sequencerPublisherForwarderAddress,
442
462
  { ...config, scope: 'sequencer' },
443
- { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider },
463
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
444
464
  )
445
- : await createL1TxUtilsWithBlobsFromEthSigner(
465
+ : await createL1TxUtilsFromSigners(
446
466
  publicClient,
447
467
  keyStoreManager!.createAllValidatorPublisherSigners(),
448
468
  { ...config, scope: 'sequencer' },
449
- { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider },
469
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
450
470
  );
451
471
 
452
472
  // Create and start the sequencer client
453
473
  const checkpointsBuilder = new CheckpointsBuilder(
454
474
  { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
475
+ worldStateSynchronizer,
455
476
  archiver,
456
477
  dateProvider,
457
478
  telemetry,
479
+ debugLogStore,
458
480
  );
459
481
 
460
482
  sequencer = await SequencerClient.new(config, {
@@ -482,6 +504,29 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
482
504
  log.warn(`Sequencer created but not started`);
483
505
  }
484
506
 
507
+ // Create prover node subsystem if enabled
508
+ let proverNode: ProverNode | undefined;
509
+ if (config.enableProverNode) {
510
+ proverNode = await createProverNode(config, {
511
+ ...deps.proverNodeDeps,
512
+ telemetry,
513
+ dateProvider,
514
+ archiver,
515
+ worldStateSynchronizer,
516
+ p2pClient,
517
+ epochCache,
518
+ blobClient,
519
+ keyStoreManager,
520
+ });
521
+
522
+ if (!options.dontStartProverNode) {
523
+ await proverNode.start();
524
+ log.info(`Prover node subsystem started`);
525
+ } else {
526
+ log.info(`Prover node subsystem created but not started`);
527
+ }
528
+ }
529
+
485
530
  const globalVariableBuilder = new GlobalVariableBuilder({
486
531
  ...config,
487
532
  rollupVersion: BigInt(config.rollupVersion),
@@ -489,7 +534,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
489
534
  slotDuration: Number(slotDuration),
490
535
  });
491
536
 
492
- return new AztecNodeService(
537
+ const node = new AztecNodeService(
493
538
  config,
494
539
  p2pClient,
495
540
  archiver,
@@ -498,6 +543,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
498
543
  archiver,
499
544
  worldStateSynchronizer,
500
545
  sequencer,
546
+ proverNode,
501
547
  slasherClient,
502
548
  validatorsSentinel,
503
549
  epochPruneWatcher,
@@ -510,7 +556,12 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
510
556
  telemetry,
511
557
  log,
512
558
  blobClient,
559
+ validatorClient,
560
+ keyStoreManager,
561
+ debugLogStore,
513
562
  );
563
+
564
+ return node;
514
565
  }
515
566
 
516
567
  /**
@@ -521,6 +572,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
521
572
  return this.sequencer;
522
573
  }
523
574
 
575
+ /** Returns the prover node subsystem, if enabled. */
576
+ public getProverNode(): ProverNode | undefined {
577
+ return this.proverNode;
578
+ }
579
+
524
580
  public getBlockSource(): L2BlockSource {
525
581
  return this.blockSource;
526
582
  }
@@ -574,19 +630,26 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
574
630
  enr,
575
631
  l1ContractAddresses: contractAddresses,
576
632
  protocolContractAddresses: protocolContractAddresses,
633
+ realProofs: !!this.config.realProofs,
577
634
  };
578
635
 
579
636
  return nodeInfo;
580
637
  }
581
638
 
582
639
  /**
583
- * Get a block specified by its number.
584
- * @param number - The block number being requested.
640
+ * Get a block specified by its block number, block hash, or 'latest'.
641
+ * @param block - The block parameter (block number, block hash, or 'latest').
585
642
  * @returns The requested block.
586
643
  */
587
- public async getBlock(number: BlockParameter): Promise<L2Block | undefined> {
588
- const blockNumber = number === 'latest' ? await this.getBlockNumber() : (number as BlockNumber);
589
- return await this.blockSource.getBlock(blockNumber);
644
+ public async getBlock(block: BlockParameter): Promise<L2Block | undefined> {
645
+ if (BlockHash.isBlockHash(block)) {
646
+ return this.getBlockByHash(block);
647
+ }
648
+ const blockNumber = block === 'latest' ? await this.getBlockNumber() : (block as BlockNumber);
649
+ if (blockNumber === BlockNumber.ZERO) {
650
+ return this.buildInitialBlock();
651
+ }
652
+ return await this.blockSource.getL2Block(blockNumber);
590
653
  }
591
654
 
592
655
  /**
@@ -594,9 +657,17 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
594
657
  * @param blockHash - The block hash being requested.
595
658
  * @returns The requested block.
596
659
  */
597
- public async getBlockByHash(blockHash: Fr): Promise<L2Block | undefined> {
598
- const publishedBlock = await this.blockSource.getPublishedBlockByHash(blockHash);
599
- return publishedBlock?.block;
660
+ public async getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
661
+ const initialBlockHash = await this.#getInitialHeaderHash();
662
+ if (blockHash.equals(initialBlockHash)) {
663
+ return this.buildInitialBlock();
664
+ }
665
+ return await this.blockSource.getL2BlockByHash(blockHash);
666
+ }
667
+
668
+ private buildInitialBlock(): L2Block {
669
+ const initialHeader = this.worldStateSynchronizer.getCommitted().getInitialHeader();
670
+ return L2Block.empty(initialHeader);
600
671
  }
601
672
 
602
673
  /**
@@ -605,8 +676,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
605
676
  * @returns The requested block.
606
677
  */
607
678
  public async getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
608
- const publishedBlock = await this.blockSource.getPublishedBlockByArchive(archive);
609
- return publishedBlock?.block;
679
+ return await this.blockSource.getL2BlockByArchive(archive);
610
680
  }
611
681
 
612
682
  /**
@@ -616,23 +686,15 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
616
686
  * @returns The blocks requested.
617
687
  */
618
688
  public async getBlocks(from: BlockNumber, limit: number): Promise<L2Block[]> {
619
- return (await this.blockSource.getBlocks(from, limit)) ?? [];
689
+ return (await this.blockSource.getBlocks(from, BlockNumber(limit))) ?? [];
620
690
  }
621
691
 
622
- public async getPublishedBlocks(from: BlockNumber, limit: number): Promise<PublishedL2Block[]> {
623
- return (await this.blockSource.getPublishedBlocks(from, limit)) ?? [];
692
+ public async getCheckpoints(from: CheckpointNumber, limit: number): Promise<PublishedCheckpoint[]> {
693
+ return (await this.blockSource.getCheckpoints(from, limit)) ?? [];
624
694
  }
625
695
 
626
- public async getPublishedCheckpoints(from: CheckpointNumber, limit: number): Promise<PublishedCheckpoint[]> {
627
- return (await this.blockSource.getPublishedCheckpoints(from, limit)) ?? [];
628
- }
629
-
630
- public async getL2BlocksNew(from: BlockNumber, limit: number): Promise<L2BlockNew[]> {
631
- return (await this.blockSource.getL2BlocksNew(from, limit)) ?? [];
632
- }
633
-
634
- public async getCheckpointedBlocks(from: BlockNumber, limit: number, proven?: boolean) {
635
- return (await this.blockSource.getCheckpointedBlocks(from, limit, proven)) ?? [];
696
+ public async getCheckpointedBlocks(from: BlockNumber, limit: number) {
697
+ return (await this.blockSource.getCheckpointedBlocks(from, limit)) ?? [];
636
698
  }
637
699
 
638
700
  /**
@@ -663,6 +725,10 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
663
725
  return await this.blockSource.getProvenBlockNumber();
664
726
  }
665
727
 
728
+ public async getCheckpointedBlockNumber(): Promise<BlockNumber> {
729
+ return await this.blockSource.getCheckpointedL2BlockNumber();
730
+ }
731
+
666
732
  /**
667
733
  * Method to fetch the version of the package.
668
734
  * @returns The node package version
@@ -695,12 +761,43 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
695
761
  return this.contractDataSource.getContract(address);
696
762
  }
697
763
 
698
- public getPrivateLogsByTags(tags: SiloedTag[]): Promise<TxScopedL2Log[][]> {
699
- return this.logsSource.getPrivateLogsByTags(tags);
700
- }
701
-
702
- public getPublicLogsByTagsFromContract(contractAddress: AztecAddress, tags: Tag[]): Promise<TxScopedL2Log[][]> {
703
- return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags);
764
+ public async getPrivateLogsByTags(
765
+ tags: SiloedTag[],
766
+ page?: number,
767
+ referenceBlock?: BlockHash,
768
+ ): Promise<TxScopedL2Log[][]> {
769
+ if (referenceBlock) {
770
+ const initialBlockHash = await this.#getInitialHeaderHash();
771
+ if (!referenceBlock.equals(initialBlockHash)) {
772
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
773
+ if (!header) {
774
+ throw new Error(
775
+ `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
776
+ );
777
+ }
778
+ }
779
+ }
780
+ return this.logsSource.getPrivateLogsByTags(tags, page);
781
+ }
782
+
783
+ public async getPublicLogsByTagsFromContract(
784
+ contractAddress: AztecAddress,
785
+ tags: Tag[],
786
+ page?: number,
787
+ referenceBlock?: BlockHash,
788
+ ): Promise<TxScopedL2Log[][]> {
789
+ if (referenceBlock) {
790
+ const initialBlockHash = await this.#getInitialHeaderHash();
791
+ if (!referenceBlock.equals(initialBlockHash)) {
792
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
793
+ if (!header) {
794
+ throw new Error(
795
+ `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
796
+ );
797
+ }
798
+ }
799
+ }
800
+ return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags, page);
704
801
  }
705
802
 
706
803
  /**
@@ -747,21 +844,30 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
747
844
  }
748
845
 
749
846
  public async getTxReceipt(txHash: TxHash): Promise<TxReceipt> {
750
- let txReceipt = new TxReceipt(txHash, TxStatus.DROPPED, 'Tx dropped by P2P node.');
751
-
752
- // We first check if the tx is in pending (instead of first checking if it is mined) because if we first check
753
- // for mined and then for pending there could be a race condition where the tx is mined between the two checks
754
- // and we would incorrectly return a TxReceipt with status DROPPED
755
- if ((await this.p2pClient.getTxStatus(txHash)) === 'pending') {
756
- txReceipt = new TxReceipt(txHash, TxStatus.PENDING, '');
757
- }
847
+ // Check the tx pool status first. If the tx is known to the pool (pending or mined), we'll use that
848
+ // as a fallback if we don't find a settled receipt in the archiver.
849
+ const txPoolStatus = await this.p2pClient.getTxStatus(txHash);
850
+ const isKnownToPool = txPoolStatus === 'pending' || txPoolStatus === 'mined';
758
851
 
852
+ // Then get the actual tx from the archiver, which tracks every tx in a mined block.
759
853
  const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
854
+
855
+ let receipt: TxReceipt;
760
856
  if (settledTxReceipt) {
761
- txReceipt = settledTxReceipt;
857
+ receipt = settledTxReceipt;
858
+ } else if (isKnownToPool) {
859
+ // If the tx is in the pool but not in the archiver, it's pending.
860
+ // This handles race conditions between archiver and p2p, where the archiver
861
+ // has pruned the block in which a tx was mined, but p2p has not caught up yet.
862
+ receipt = new TxReceipt(txHash, TxStatus.PENDING, undefined, undefined);
863
+ } else {
864
+ // Otherwise, if we don't know the tx, we consider it dropped.
865
+ receipt = new TxReceipt(txHash, TxStatus.DROPPED, undefined, 'Tx dropped by P2P node');
762
866
  }
763
867
 
764
- return txReceipt;
868
+ this.debugLogStore.decorateReceiptWithLogs(txHash.toString(), receipt);
869
+
870
+ return receipt;
765
871
  }
766
872
 
767
873
  public getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
@@ -778,6 +884,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
778
884
  await tryStop(this.slasherClient);
779
885
  await tryStop(this.proofVerifier);
780
886
  await tryStop(this.sequencer);
887
+ await tryStop(this.proverNode);
781
888
  await tryStop(this.p2pClient);
782
889
  await tryStop(this.worldStateSynchronizer);
783
890
  await tryStop(this.blockSource);
@@ -826,20 +933,12 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
826
933
  return compactArray(await Promise.all(txHashes.map(txHash => this.getTxByHash(txHash))));
827
934
  }
828
935
 
829
- /**
830
- * Find the indexes of the given leaves in the given tree along with a block metadata pointing to the block in which
831
- * the leaves were inserted.
832
- * @param blockNumber - The block number at which to get the data or 'latest' for latest data.
833
- * @param treeId - The tree to search in.
834
- * @param leafValues - The values to search for.
835
- * @returns The indices of leaves and the block metadata of a block in which the leaves were inserted.
836
- */
837
936
  public async findLeavesIndexes(
838
- blockNumber: BlockParameter,
937
+ referenceBlock: BlockParameter,
839
938
  treeId: MerkleTreeId,
840
939
  leafValues: Fr[],
841
940
  ): Promise<(DataInBlock<bigint> | undefined)[]> {
842
- const committedDb = await this.#getWorldState(blockNumber);
941
+ const committedDb = await this.#getWorldState(referenceBlock);
843
942
  const maybeIndices = await committedDb.findLeafIndices(
844
943
  treeId,
845
944
  leafValues.map(x => x.toBuffer()),
@@ -891,56 +990,28 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
891
990
  }
892
991
  return {
893
992
  l2BlockNumber: BlockNumber(Number(blockNumber)),
894
- l2BlockHash: L2BlockHash.fromField(blockHash),
993
+ l2BlockHash: new BlockHash(blockHash),
895
994
  data: index,
896
995
  };
897
996
  });
898
997
  }
899
998
 
900
- /**
901
- * Returns a sibling path for the given index in the nullifier tree.
902
- * @param blockNumber - The block number at which to get the data.
903
- * @param leafIndex - The index of the leaf for which the sibling path is required.
904
- * @returns The sibling path for the leaf index.
905
- */
906
- public async getNullifierSiblingPath(
907
- blockNumber: BlockParameter,
908
- leafIndex: bigint,
909
- ): Promise<SiblingPath<typeof NULLIFIER_TREE_HEIGHT>> {
910
- const committedDb = await this.#getWorldState(blockNumber);
911
- return committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, leafIndex);
912
- }
913
-
914
- /**
915
- * Returns a sibling path for the given index in the data tree.
916
- * @param blockNumber - The block number at which to get the data.
917
- * @param leafIndex - The index of the leaf for which the sibling path is required.
918
- * @returns The sibling path for the leaf index.
919
- */
920
- public async getNoteHashSiblingPath(
921
- blockNumber: BlockParameter,
922
- leafIndex: bigint,
923
- ): Promise<SiblingPath<typeof NOTE_HASH_TREE_HEIGHT>> {
924
- const committedDb = await this.#getWorldState(blockNumber);
925
- return committedDb.getSiblingPath(MerkleTreeId.NOTE_HASH_TREE, leafIndex);
926
- }
927
-
928
- public async getArchiveMembershipWitness(
929
- blockNumber: BlockParameter,
930
- archive: Fr,
999
+ public async getBlockHashMembershipWitness(
1000
+ referenceBlock: BlockParameter,
1001
+ blockHash: BlockHash,
931
1002
  ): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
932
- const committedDb = await this.#getWorldState(blockNumber);
933
- const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [archive]);
1003
+ const committedDb = await this.#getWorldState(referenceBlock);
1004
+ const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [blockHash]);
934
1005
  return pathAndIndex === undefined
935
1006
  ? undefined
936
1007
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
937
1008
  }
938
1009
 
939
1010
  public async getNoteHashMembershipWitness(
940
- blockNumber: BlockParameter,
1011
+ referenceBlock: BlockParameter,
941
1012
  noteHash: Fr,
942
1013
  ): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
943
- const committedDb = await this.#getWorldState(blockNumber);
1014
+ const committedDb = await this.#getWorldState(referenceBlock);
944
1015
  const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.NOTE_HASH_TREE>(
945
1016
  MerkleTreeId.NOTE_HASH_TREE,
946
1017
  [noteHash],
@@ -950,17 +1021,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
950
1021
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
951
1022
  }
952
1023
 
953
- /**
954
- * Returns the index and a sibling path for a leaf in the committed l1 to l2 data tree.
955
- * @param blockNumber - The block number at which to get the data.
956
- * @param l1ToL2Message - The l1ToL2Message to get the index / sibling path for.
957
- * @returns A tuple of the index and the sibling path of the L1ToL2Message (undefined if not found).
958
- */
959
1024
  public async getL1ToL2MessageMembershipWitness(
960
- blockNumber: BlockParameter,
1025
+ referenceBlock: BlockParameter,
961
1026
  l1ToL2Message: Fr,
962
1027
  ): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined> {
963
- const db = await this.#getWorldState(blockNumber);
1028
+ const db = await this.#getWorldState(referenceBlock);
964
1029
  const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [l1ToL2Message]);
965
1030
  if (!witness) {
966
1031
  return undefined;
@@ -993,12 +1058,13 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
993
1058
  * @returns The L2 to L1 messages (empty array if the epoch is not found).
994
1059
  */
995
1060
  public async getL2ToL1Messages(epoch: EpochNumber): Promise<Fr[][][][]> {
996
- // Assumes `getBlocksForEpoch` returns blocks in ascending order of block number.
997
- const blocks = await this.blockSource.getBlocksForEpoch(epoch);
1061
+ // Assumes `getCheckpointedBlocksForEpoch` returns blocks in ascending order of block number.
1062
+ const checkpointedBlocks = await this.blockSource.getCheckpointedBlocksForEpoch(epoch);
998
1063
  const blocksInCheckpoints: L2Block[][] = [];
999
1064
  let previousSlotNumber = SlotNumber.ZERO;
1000
1065
  let checkpointIndex = -1;
1001
- for (const block of blocks) {
1066
+ for (const checkpointedBlock of checkpointedBlocks) {
1067
+ const block = checkpointedBlock.block;
1002
1068
  const slotNumber = block.header.globalVariables.slotNumber;
1003
1069
  if (slotNumber !== previousSlotNumber) {
1004
1070
  checkpointIndex++;
@@ -1012,45 +1078,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1012
1078
  );
1013
1079
  }
1014
1080
 
1015
- /**
1016
- * Returns a sibling path for a leaf in the committed blocks tree.
1017
- * @param blockNumber - The block number at which to get the data.
1018
- * @param leafIndex - Index of the leaf in the tree.
1019
- * @returns The sibling path.
1020
- */
1021
- public async getArchiveSiblingPath(
1022
- blockNumber: BlockParameter,
1023
- leafIndex: bigint,
1024
- ): Promise<SiblingPath<typeof ARCHIVE_HEIGHT>> {
1025
- const committedDb = await this.#getWorldState(blockNumber);
1026
- return committedDb.getSiblingPath(MerkleTreeId.ARCHIVE, leafIndex);
1027
- }
1028
-
1029
- /**
1030
- * Returns a sibling path for a leaf in the committed public data tree.
1031
- * @param blockNumber - The block number at which to get the data.
1032
- * @param leafIndex - Index of the leaf in the tree.
1033
- * @returns The sibling path.
1034
- */
1035
- public async getPublicDataSiblingPath(
1036
- blockNumber: BlockParameter,
1037
- leafIndex: bigint,
1038
- ): Promise<SiblingPath<typeof PUBLIC_DATA_TREE_HEIGHT>> {
1039
- const committedDb = await this.#getWorldState(blockNumber);
1040
- return committedDb.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex);
1041
- }
1042
-
1043
- /**
1044
- * Returns a nullifier membership witness for a given nullifier at a given block.
1045
- * @param blockNumber - The block number at which to get the index.
1046
- * @param nullifier - Nullifier we try to find witness for.
1047
- * @returns The nullifier membership witness (if found).
1048
- */
1049
1081
  public async getNullifierMembershipWitness(
1050
- blockNumber: BlockParameter,
1082
+ referenceBlock: BlockParameter,
1051
1083
  nullifier: Fr,
1052
1084
  ): Promise<NullifierMembershipWitness | undefined> {
1053
- const db = await this.#getWorldState(blockNumber);
1085
+ const db = await this.#getWorldState(referenceBlock);
1054
1086
  const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]);
1055
1087
  if (!witness) {
1056
1088
  return undefined;
@@ -1067,7 +1099,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1067
1099
 
1068
1100
  /**
1069
1101
  * Returns a low nullifier membership witness for a given nullifier at a given block.
1070
- * @param blockNumber - The block number at which to get the index.
1102
+ * @param referenceBlock - The block parameter (block number, block hash, or 'latest') at which to get the data
1103
+ * (which contains the root of the nullifier tree in which we are searching for the nullifier).
1071
1104
  * @param nullifier - Nullifier we try to find the low nullifier witness for.
1072
1105
  * @returns The low nullifier membership witness (if found).
1073
1106
  * @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
@@ -1080,10 +1113,10 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1080
1113
  * TODO: This is a confusing behavior and we should eventually address that.
1081
1114
  */
1082
1115
  public async getLowNullifierMembershipWitness(
1083
- blockNumber: BlockParameter,
1116
+ referenceBlock: BlockParameter,
1084
1117
  nullifier: Fr,
1085
1118
  ): Promise<NullifierMembershipWitness | undefined> {
1086
- const committedDb = await this.#getWorldState(blockNumber);
1119
+ const committedDb = await this.#getWorldState(referenceBlock);
1087
1120
  const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
1088
1121
  if (!findResult) {
1089
1122
  return undefined;
@@ -1098,8 +1131,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1098
1131
  return new NullifierMembershipWitness(BigInt(index), preimageData as NullifierLeafPreimage, siblingPath);
1099
1132
  }
1100
1133
 
1101
- async getPublicDataWitness(blockNumber: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1102
- const committedDb = await this.#getWorldState(blockNumber);
1134
+ async getPublicDataWitness(referenceBlock: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1135
+ const committedDb = await this.#getWorldState(referenceBlock);
1103
1136
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
1104
1137
  if (!lowLeafResult) {
1105
1138
  return undefined;
@@ -1113,19 +1146,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1113
1146
  }
1114
1147
  }
1115
1148
 
1116
- /**
1117
- * Gets the storage value at the given contract storage slot.
1118
- *
1119
- * @remarks The storage slot here refers to the slot as it is defined in Noir not the index in the merkle tree.
1120
- * Aztec's version of `eth_getStorageAt`.
1121
- *
1122
- * @param contract - Address of the contract to query.
1123
- * @param slot - Slot to query.
1124
- * @param blockNumber - The block number at which to get the data or 'latest'.
1125
- * @returns Storage value at the given contract slot.
1126
- */
1127
- public async getPublicStorageAt(blockNumber: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
1128
- const committedDb = await this.#getWorldState(blockNumber);
1149
+ public async getPublicStorageAt(referenceBlock: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
1150
+ const committedDb = await this.#getWorldState(referenceBlock);
1129
1151
  const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
1130
1152
 
1131
1153
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
@@ -1139,24 +1161,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1139
1161
  return preimage.leaf.value;
1140
1162
  }
1141
1163
 
1142
- /**
1143
- * Returns the currently committed block header, or the initial header if no blocks have been produced.
1144
- * @returns The current committed block header.
1145
- */
1146
- public async getBlockHeader(blockNumber: BlockParameter = 'latest'): Promise<BlockHeader | undefined> {
1147
- return blockNumber === BlockNumber.ZERO ||
1148
- (blockNumber === 'latest' && (await this.blockSource.getBlockNumber()) === BlockNumber.ZERO)
1149
- ? this.worldStateSynchronizer.getCommitted().getInitialHeader()
1150
- : this.blockSource.getBlockHeader(blockNumber === 'latest' ? blockNumber : (blockNumber as BlockNumber));
1151
- }
1152
-
1153
- /**
1154
- * Get a block header specified by its hash.
1155
- * @param blockHash - The block hash being requested.
1156
- * @returns The requested block header.
1157
- */
1158
- public async getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
1159
- return await this.blockSource.getBlockHeaderByHash(blockHash);
1164
+ public async getBlockHeader(block: BlockParameter = 'latest'): Promise<BlockHeader | undefined> {
1165
+ if (BlockHash.isBlockHash(block)) {
1166
+ const initialBlockHash = await this.#getInitialHeaderHash();
1167
+ if (block.equals(initialBlockHash)) {
1168
+ // Block source doesn't handle initial header so we need to handle the case separately.
1169
+ return this.worldStateSynchronizer.getCommitted().getInitialHeader();
1170
+ }
1171
+ return this.blockSource.getBlockHeaderByHash(block);
1172
+ } else {
1173
+ // Block source doesn't handle initial header so we need to handle the case separately.
1174
+ const blockNumber = block === 'latest' ? await this.getBlockNumber() : (block as BlockNumber);
1175
+ if (blockNumber === BlockNumber.ZERO) {
1176
+ return this.worldStateSynchronizer.getCommitted().getInitialHeader();
1177
+ }
1178
+ return this.blockSource.getBlockHeader(block);
1179
+ }
1160
1180
  }
1161
1181
 
1162
1182
  /**
@@ -1168,6 +1188,14 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1168
1188
  return await this.blockSource.getBlockHeaderByArchive(archive);
1169
1189
  }
1170
1190
 
1191
+ public getBlockData(number: BlockNumber): Promise<BlockData | undefined> {
1192
+ return this.blockSource.getBlockData(number);
1193
+ }
1194
+
1195
+ public getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
1196
+ return this.blockSource.getBlockDataByArchive(archive);
1197
+ }
1198
+
1171
1199
  /**
1172
1200
  * Simulates the public part of a transaction with the current state.
1173
1201
  * @param tx - The transaction to simulate.
@@ -1191,7 +1219,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1191
1219
  }
1192
1220
 
1193
1221
  const txHash = tx.getTxHash();
1194
- const blockNumber = BlockNumber((await this.blockSource.getBlockNumber()) + 1);
1222
+ const latestBlockNumber = await this.blockSource.getBlockNumber();
1223
+ const blockNumber = BlockNumber.add(latestBlockNumber, 1);
1195
1224
 
1196
1225
  // If sequencer is not initialized, we just set these values to zero for simulation.
1197
1226
  const coinbase = EthAddress.ZERO;
@@ -1206,6 +1235,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1206
1235
  this.contractDataSource,
1207
1236
  new DateProvider(),
1208
1237
  this.telemetry,
1238
+ this.log.getBindings(),
1209
1239
  );
1210
1240
 
1211
1241
  this.log.verbose(`Simulating public calls for tx ${txHash}`, {
@@ -1214,6 +1244,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1214
1244
  blockNumber,
1215
1245
  });
1216
1246
 
1247
+ // Ensure world-state has caught up with the latest block we loaded from the archiver
1248
+ await this.worldStateSynchronizer.syncImmediate(latestBlockNumber);
1217
1249
  const merkleTreeFork = await this.worldStateSynchronizer.fork();
1218
1250
  try {
1219
1251
  const config = PublicSimulatorConfig.from({
@@ -1229,7 +1261,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1229
1261
  const processor = publicProcessorFactory.create(merkleTreeFork, newGlobalVariables, config);
1230
1262
 
1231
1263
  // REFACTOR: Consider merging ProcessReturnValues into ProcessedTx
1232
- const [processedTxs, failedTxs, _usedTxs, returns] = await processor.process([tx]);
1264
+ const [processedTxs, failedTxs, _usedTxs, returns, _blobFields, debugLogs] = await processor.process([tx]);
1233
1265
  // REFACTOR: Consider returning the error rather than throwing
1234
1266
  if (failedTxs.length) {
1235
1267
  this.log.warn(`Simulated tx ${txHash} fails: ${failedTxs[0].error}`, { txHash });
@@ -1243,6 +1275,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1243
1275
  processedTx.txEffect,
1244
1276
  returns,
1245
1277
  processedTx.gasUsed,
1278
+ debugLogs,
1246
1279
  );
1247
1280
  } finally {
1248
1281
  await merkleTreeFork.close();
@@ -1256,19 +1289,25 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1256
1289
  const db = this.worldStateSynchronizer.getCommitted();
1257
1290
  const verifier = isSimulation ? undefined : this.proofVerifier;
1258
1291
 
1259
- // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1292
+ // We accept transactions if they are not expired by the next slot (checked based on the ExpirationTimestamp field)
1260
1293
  const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1261
1294
  const blockNumber = BlockNumber((await this.blockSource.getBlockNumber()) + 1);
1262
- const validator = createValidatorForAcceptingTxs(db, this.contractDataSource, verifier, {
1263
- timestamp: nextSlotTimestamp,
1264
- blockNumber,
1265
- l1ChainId: this.l1ChainId,
1266
- rollupVersion: this.version,
1267
- setupAllowList: this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions()),
1268
- gasFees: await this.getCurrentMinFees(),
1269
- skipFeeEnforcement,
1270
- txsPermitted: !this.config.disableTransactions,
1271
- });
1295
+ const validator = createValidatorForAcceptingTxs(
1296
+ db,
1297
+ this.contractDataSource,
1298
+ verifier,
1299
+ {
1300
+ timestamp: nextSlotTimestamp,
1301
+ blockNumber,
1302
+ l1ChainId: this.l1ChainId,
1303
+ rollupVersion: this.version,
1304
+ setupAllowList: this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions()),
1305
+ gasFees: await this.getCurrentMinFees(),
1306
+ skipFeeEnforcement,
1307
+ txsPermitted: !this.config.disableTransactions,
1308
+ },
1309
+ this.log.getBindings(),
1310
+ );
1272
1311
 
1273
1312
  return await validator.validateTx(tx);
1274
1313
  }
@@ -1432,16 +1471,107 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1432
1471
  }
1433
1472
  }
1434
1473
 
1474
+ public async reloadKeystore(): Promise<void> {
1475
+ if (!this.config.keyStoreDirectory?.length) {
1476
+ throw new BadRequestError(
1477
+ 'Cannot reload keystore: node is not using a file-based keystore. ' +
1478
+ 'Set KEY_STORE_DIRECTORY to use file-based keystores.',
1479
+ );
1480
+ }
1481
+ if (!this.validatorClient) {
1482
+ throw new BadRequestError('Cannot reload keystore: validator is not enabled.');
1483
+ }
1484
+
1485
+ this.log.info('Reloading keystore from disk');
1486
+
1487
+ // Re-read and validate keystore files
1488
+ const keyStores = loadKeystores(this.config.keyStoreDirectory);
1489
+ const newManager = new KeystoreManager(mergeKeystores(keyStores));
1490
+ await newManager.validateSigners();
1491
+ ValidatorClient.validateKeyStoreConfiguration(newManager, this.log);
1492
+
1493
+ // Validate that every validator's publisher keys overlap with the L1 signers
1494
+ // that were initialized at startup. Publishers cannot be hot-reloaded, so a
1495
+ // validator with a publisher key that doesn't match any existing L1 signer
1496
+ // would silently fail on every proposer slot.
1497
+ if (this.keyStoreManager && this.sequencer) {
1498
+ const oldAdapter = NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager);
1499
+ const availablePublishers = new Set(
1500
+ oldAdapter
1501
+ .getAttesterAddresses()
1502
+ .flatMap(a => oldAdapter.getPublisherAddresses(a).map(p => p.toString().toLowerCase())),
1503
+ );
1504
+
1505
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1506
+ for (const attester of newAdapter.getAttesterAddresses()) {
1507
+ const pubs = newAdapter.getPublisherAddresses(attester);
1508
+ if (pubs.length > 0 && !pubs.some(p => availablePublishers.has(p.toString().toLowerCase()))) {
1509
+ throw new BadRequestError(
1510
+ `Cannot reload keystore: validator ${attester} has publisher keys ` +
1511
+ `[${pubs.map(p => p.toString()).join(', ')}] but none match the L1 signers initialized at startup ` +
1512
+ `[${[...availablePublishers].join(', ')}]. Publishers cannot be hot-reloaded — ` +
1513
+ `use an existing publisher key or restart the node.`,
1514
+ );
1515
+ }
1516
+ }
1517
+ }
1518
+
1519
+ // Build adapters for old and new keystores to compute diff
1520
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1521
+ const newAddresses = newAdapter.getAttesterAddresses();
1522
+ const oldAddresses = this.keyStoreManager
1523
+ ? NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager).getAttesterAddresses()
1524
+ : [];
1525
+
1526
+ const oldSet = new Set(oldAddresses.map(a => a.toString()));
1527
+ const newSet = new Set(newAddresses.map(a => a.toString()));
1528
+ const added = newAddresses.filter(a => !oldSet.has(a.toString()));
1529
+ const removed = oldAddresses.filter(a => !newSet.has(a.toString()));
1530
+
1531
+ if (added.length > 0) {
1532
+ this.log.info(`Keystore reload: adding attester keys: ${added.map(a => a.toString()).join(', ')}`);
1533
+ }
1534
+ if (removed.length > 0) {
1535
+ this.log.info(`Keystore reload: removing attester keys: ${removed.map(a => a.toString()).join(', ')}`);
1536
+ }
1537
+ if (added.length === 0 && removed.length === 0) {
1538
+ this.log.info('Keystore reload: attester keys unchanged');
1539
+ }
1540
+
1541
+ // Update the validator client (coinbase, feeRecipient, attester keys)
1542
+ this.validatorClient.reloadKeystore(newManager);
1543
+
1544
+ // Update the publisher factory's keystore so newly-added validators
1545
+ // can be matched to existing publisher keys when proposing blocks.
1546
+ if (this.sequencer) {
1547
+ this.sequencer.updatePublisherNodeKeyStore(newAdapter);
1548
+ }
1549
+
1550
+ // Update slasher's "don't-slash-self" list with new validator addresses
1551
+ if (this.slasherClient && !this.config.slashSelfAllowed) {
1552
+ const slashValidatorsNever = unique(
1553
+ [...(this.config.slashValidatorsNever ?? []), ...newAddresses].map(a => a.toString()),
1554
+ ).map(EthAddress.fromString);
1555
+ this.slasherClient.updateConfig({ slashValidatorsNever });
1556
+ }
1557
+
1558
+ this.keyStoreManager = newManager;
1559
+ this.log.info('Keystore reloaded: coinbase, feeRecipient, and attester keys updated');
1560
+ }
1561
+
1562
+ #getInitialHeaderHash(): Promise<BlockHash> {
1563
+ if (!this.initialHeaderHashPromise) {
1564
+ this.initialHeaderHashPromise = this.worldStateSynchronizer.getCommitted().getInitialHeader().hash();
1565
+ }
1566
+ return this.initialHeaderHashPromise;
1567
+ }
1568
+
1435
1569
  /**
1436
1570
  * Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
1437
- * @param blockNumber - The block number at which to get the data.
1571
+ * @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
1438
1572
  * @returns An instance of a committed MerkleTreeOperations
1439
1573
  */
1440
- async #getWorldState(blockNumber: BlockParameter) {
1441
- if (typeof blockNumber === 'number' && blockNumber < INITIAL_L2_BLOCK_NUM - 1) {
1442
- throw new Error('Invalid block number to get world state for: ' + blockNumber);
1443
- }
1444
-
1574
+ async #getWorldState(block: BlockParameter) {
1445
1575
  let blockSyncedTo: BlockNumber = BlockNumber.ZERO;
1446
1576
  try {
1447
1577
  // Attempt to sync the world state if necessary
@@ -1450,15 +1580,39 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1450
1580
  this.log.error(`Error getting world state: ${err}`);
1451
1581
  }
1452
1582
 
1453
- // using a snapshot could be less efficient than using the committed db
1454
- if (blockNumber === 'latest' /*|| blockNumber === blockSyncedTo*/) {
1455
- this.log.debug(`Using committed db for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1583
+ if (block === 'latest') {
1584
+ this.log.debug(`Using committed db for block 'latest', world state synced upto ${blockSyncedTo}`);
1456
1585
  return this.worldStateSynchronizer.getCommitted();
1457
- } else if (blockNumber <= blockSyncedTo) {
1586
+ }
1587
+
1588
+ if (BlockHash.isBlockHash(block)) {
1589
+ const initialBlockHash = await this.#getInitialHeaderHash();
1590
+ if (block.equals(initialBlockHash)) {
1591
+ // Block source doesn't handle initial header so we need to handle the case separately.
1592
+ return this.worldStateSynchronizer.getSnapshot(BlockNumber.ZERO);
1593
+ }
1594
+
1595
+ const header = await this.blockSource.getBlockHeaderByHash(block);
1596
+ if (!header) {
1597
+ throw new Error(
1598
+ `Block hash ${block.toString()} not found when querying world state. If the node API has been queried with anchor block hash possibly a reorg has occurred.`,
1599
+ );
1600
+ }
1601
+ const blockNumber = header.getBlockNumber();
1458
1602
  this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1459
- return this.worldStateSynchronizer.getSnapshot(blockNumber as BlockNumber);
1460
- } else {
1461
- throw new Error(`Block ${blockNumber} not yet synced`);
1603
+ return this.worldStateSynchronizer.getSnapshot(blockNumber);
1604
+ }
1605
+
1606
+ // Block number provided
1607
+ {
1608
+ const blockNumber = block as BlockNumber;
1609
+
1610
+ if (blockNumber > blockSyncedTo) {
1611
+ throw new Error(`Queried block ${block} not yet synced by the node (node is synced upto ${blockSyncedTo}).`);
1612
+ }
1613
+
1614
+ this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1615
+ return this.worldStateSynchronizer.getSnapshot(blockNumber);
1462
1616
  }
1463
1617
  }
1464
1618