@aztec/aztec-node 3.0.3 → 4.0.0-devnet.1-patch.1

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,20 +1,13 @@
1
1
  import { Archiver, createArchiver } from '@aztec/archiver';
2
2
  import { BBCircuitVerifier, QueuedIVCVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
3
- import { type BlobSinkClientInterface, createBlobSinkClient } from '@aztec/blob-sink/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';
3
+ import { type BlobClientInterface, createBlobClientWithFileStores } from '@aztec/blob-client/client';
4
+ import { ARCHIVE_HEIGHT, type L1_TO_L2_MSG_TREE_HEIGHT, type NOTE_HASH_TREE_HEIGHT } from '@aztec/constants';
12
5
  import { EpochCache, type EpochCacheInterface } from '@aztec/epoch-cache';
13
6
  import { createEthereumChain } from '@aztec/ethereum/chain';
14
7
  import { getPublicClient } from '@aztec/ethereum/client';
15
8
  import { RegistryContract, RollupContract } from '@aztec/ethereum/contracts';
16
9
  import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
17
- import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
10
+ import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
18
11
  import { compactArray, pick } from '@aztec/foundation/collection';
19
12
  import { Fr } from '@aztec/foundation/curves/bn254';
20
13
  import { EthAddress } from '@aztec/foundation/eth-address';
@@ -31,13 +24,7 @@ import {
31
24
  } from '@aztec/node-lib/factories';
32
25
  import { type P2P, type P2PClientDeps, createP2PClient, getDefaultAllowedSetupFunctions } from '@aztec/p2p';
33
26
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
34
- import {
35
- BlockBuilder,
36
- GlobalVariableBuilder,
37
- SequencerClient,
38
- type SequencerPublisher,
39
- createValidatorForAcceptingTxs,
40
- } from '@aztec/sequencer-client';
27
+ import { GlobalVariableBuilder, SequencerClient, type SequencerPublisher } from '@aztec/sequencer-client';
41
28
  import { PublicProcessorFactory } from '@aztec/simulator/server';
42
29
  import {
43
30
  AttestationsBlockWatcher,
@@ -48,14 +35,8 @@ import {
48
35
  } from '@aztec/slasher';
49
36
  import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
50
37
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
51
- import {
52
- type BlockParameter,
53
- type DataInBlock,
54
- type L2Block,
55
- L2BlockHash,
56
- type L2BlockSource,
57
- type PublishedL2Block,
58
- } from '@aztec/stdlib/block';
38
+ import { BlockHash, type BlockParameter, type DataInBlock, L2Block, type L2BlockSource } from '@aztec/stdlib/block';
39
+ import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
59
40
  import type {
60
41
  ContractClassPublic,
61
42
  ContractDataSource,
@@ -82,7 +63,7 @@ import {
82
63
  type WorldStateSynchronizer,
83
64
  tryStop,
84
65
  } from '@aztec/stdlib/interfaces/server';
85
- import type { LogFilter, TxScopedL2Log } from '@aztec/stdlib/logs';
66
+ import type { LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
86
67
  import { InboxLeaf, type L1ToL2MessageSource } from '@aztec/stdlib/messaging';
87
68
  import { P2PClientType } from '@aztec/stdlib/p2p';
88
69
  import type { Offense, SlashPayloadRound } from '@aztec/stdlib/slashing';
@@ -110,10 +91,13 @@ import {
110
91
  trackSpan,
111
92
  } from '@aztec/telemetry-client';
112
93
  import {
94
+ FullNodeCheckpointsBuilder as CheckpointsBuilder,
95
+ FullNodeCheckpointsBuilder,
113
96
  NodeKeystoreAdapter,
114
97
  ValidatorClient,
115
98
  createBlockProposalHandler,
116
99
  createValidatorClient,
100
+ createValidatorForAcceptingTxs,
117
101
  } from '@aztec/validator-client';
118
102
  import { createWorldStateSynchronizer } from '@aztec/world-state';
119
103
 
@@ -129,6 +113,7 @@ import { NodeMetrics } from './node_metrics.js';
129
113
  */
130
114
  export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
131
115
  private metrics: NodeMetrics;
116
+ private initialHeaderHashPromise: Promise<BlockHash> | undefined = undefined;
132
117
 
133
118
  // Prevent two snapshot operations to happen simultaneously
134
119
  private isUploadingSnapshot = false;
@@ -155,6 +140,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
155
140
  private proofVerifier: ClientProtocolCircuitVerifier,
156
141
  private telemetry: TelemetryClient = getTelemetryClient(),
157
142
  private log = createLogger('node'),
143
+ private blobClient?: BlobClientInterface,
158
144
  ) {
159
145
  this.metrics = new NodeMetrics(telemetry, 'AztecNodeService');
160
146
  this.tracer = telemetry.getTracer('AztecNodeService');
@@ -184,7 +170,6 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
184
170
  logger?: Logger;
185
171
  publisher?: SequencerPublisher;
186
172
  dateProvider?: DateProvider;
187
- blobSinkClient?: BlobSinkClientInterface;
188
173
  p2pClientDeps?: P2PClientDeps<P2PClientType.Full>;
189
174
  } = {},
190
175
  options: {
@@ -197,8 +182,6 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
197
182
  const packageVersion = getPackageVersion() ?? '';
198
183
  const telemetry = deps.telemetry ?? getTelemetryClient();
199
184
  const dateProvider = deps.dateProvider ?? new DateProvider();
200
- const blobSinkClient =
201
- deps.blobSinkClient ?? createBlobSinkClient(config, { logger: createLogger('node:blob-sink:client') });
202
185
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
203
186
 
204
187
  // Build a key store from file if given or from environment otherwise
@@ -238,7 +221,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
238
221
 
239
222
  const publicClient = createPublicClient({
240
223
  chain: ethereumChain.chainInfo,
241
- transport: fallback(config.l1RpcUrls.map((url: string) => http(url))),
224
+ transport: fallback(config.l1RpcUrls.map((url: string) => http(url, { batch: false }))),
242
225
  pollingInterval: config.viemPollingIntervalMS,
243
226
  });
244
227
 
@@ -266,6 +249,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
266
249
  );
267
250
  }
268
251
 
252
+ const blobClient = await createBlobClientWithFileStores(config, createLogger('node:blob-client:client'));
253
+
269
254
  // attempt snapshot sync if possible
270
255
  await trySnapshotSync(config, log);
271
256
 
@@ -273,7 +258,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
273
258
 
274
259
  const archiver = await createArchiver(
275
260
  config,
276
- { blobSinkClient, epochCache, telemetry, dateProvider },
261
+ { blobClient, epochCache, telemetry, dateProvider },
277
262
  { blockUntilSync: !config.skipArchiverInitialSync },
278
263
  );
279
264
 
@@ -310,7 +295,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
310
295
  // We should really not be modifying the config object
311
296
  config.txPublicSetupAllowList = config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
312
297
 
313
- const blockBuilder = new BlockBuilder(
298
+ // Create FullNodeCheckpointsBuilder for validator and non-validator block proposal handling
299
+ const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder(
314
300
  { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
315
301
  worldStateSynchronizer,
316
302
  archiver,
@@ -322,15 +308,17 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
322
308
  const watchers: Watcher[] = [];
323
309
 
324
310
  // Create validator client if required
325
- const validatorClient = createValidatorClient(config, {
311
+ const validatorClient = await createValidatorClient(config, {
312
+ checkpointsBuilder: validatorCheckpointsBuilder,
313
+ worldState: worldStateSynchronizer,
326
314
  p2pClient,
327
315
  telemetry,
328
316
  dateProvider,
329
317
  epochCache,
330
- blockBuilder,
331
318
  blockSource: archiver,
332
319
  l1ToL2MessageSource: archiver,
333
320
  keyStoreManager,
321
+ blobClient,
334
322
  });
335
323
 
336
324
  // If we have a validator client, register it as a source of offenses for the slasher,
@@ -348,7 +336,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
348
336
  if (!validatorClient && config.alwaysReexecuteBlockProposals) {
349
337
  log.info('Setting up block proposal reexecution for monitoring');
350
338
  createBlockProposalHandler(config, {
351
- blockBuilder,
339
+ checkpointsBuilder: validatorCheckpointsBuilder,
340
+ worldState: worldStateSynchronizer,
352
341
  epochCache,
353
342
  blockSource: archiver,
354
343
  l1ToL2MessageSource: archiver,
@@ -376,7 +365,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
376
365
  archiver,
377
366
  epochCache,
378
367
  p2pClient.getTxProvider(),
379
- blockBuilder,
368
+ validatorCheckpointsBuilder,
380
369
  config,
381
370
  );
382
371
  watchers.push(epochPruneWatcher);
@@ -404,7 +393,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
404
393
  // Validator enabled, create/start relevant service
405
394
  let sequencer: SequencerClient | undefined;
406
395
  let slasherClient: SlasherClientInterface | undefined;
407
- if (!config.disableValidator) {
396
+ if (!config.disableValidator && validatorClient) {
408
397
  // We create a slasher only if we have a sequencer, since all slashing actions go through the sequencer publisher
409
398
  // as they are executed when the node is selected as proposer.
410
399
  const validatorAddresses = keyStoreManager
@@ -439,6 +428,14 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
439
428
  );
440
429
 
441
430
  // Create and start the sequencer client
431
+ const checkpointsBuilder = new CheckpointsBuilder(
432
+ { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
433
+ worldStateSynchronizer,
434
+ archiver,
435
+ dateProvider,
436
+ telemetry,
437
+ );
438
+
442
439
  sequencer = await SequencerClient.new(config, {
443
440
  ...deps,
444
441
  epochCache,
@@ -447,12 +444,12 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
447
444
  p2pClient,
448
445
  worldStateSynchronizer,
449
446
  slasherClient,
450
- blockBuilder,
447
+ checkpointsBuilder,
451
448
  l2BlockSource: archiver,
452
449
  l1ToL2MessageSource: archiver,
453
450
  telemetry,
454
451
  dateProvider,
455
- blobSinkClient,
452
+ blobClient,
456
453
  nodeKeyStore: keyStoreManager!,
457
454
  });
458
455
  }
@@ -464,6 +461,13 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
464
461
  log.warn(`Sequencer created but not started`);
465
462
  }
466
463
 
464
+ const globalVariableBuilder = new GlobalVariableBuilder({
465
+ ...config,
466
+ rollupVersion: BigInt(config.rollupVersion),
467
+ l1GenesisTime,
468
+ slotDuration: Number(slotDuration),
469
+ });
470
+
467
471
  return new AztecNodeService(
468
472
  config,
469
473
  p2pClient,
@@ -478,12 +482,13 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
478
482
  epochPruneWatcher,
479
483
  ethereumChain.chainInfo.id,
480
484
  config.rollupVersion,
481
- new GlobalVariableBuilder(config),
485
+ globalVariableBuilder,
482
486
  epochCache,
483
487
  packageVersion,
484
488
  proofVerifier,
485
489
  telemetry,
486
490
  log,
491
+ blobClient,
487
492
  );
488
493
  }
489
494
 
@@ -548,19 +553,26 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
548
553
  enr,
549
554
  l1ContractAddresses: contractAddresses,
550
555
  protocolContractAddresses: protocolContractAddresses,
556
+ realProofs: !!this.config.realProofs,
551
557
  };
552
558
 
553
559
  return nodeInfo;
554
560
  }
555
561
 
556
562
  /**
557
- * Get a block specified by its number.
558
- * @param number - The block number being requested.
563
+ * Get a block specified by its block number, block hash, or 'latest'.
564
+ * @param block - The block parameter (block number, block hash, or 'latest').
559
565
  * @returns The requested block.
560
566
  */
561
- public async getBlock(number: BlockParameter): Promise<L2Block | undefined> {
562
- const blockNumber = number === 'latest' ? await this.getBlockNumber() : (number as BlockNumber);
563
- return await this.blockSource.getBlock(blockNumber);
567
+ public async getBlock(block: BlockParameter): Promise<L2Block | undefined> {
568
+ if (BlockHash.isBlockHash(block)) {
569
+ return this.getBlockByHash(block);
570
+ }
571
+ const blockNumber = block === 'latest' ? await this.getBlockNumber() : (block as BlockNumber);
572
+ if (blockNumber === BlockNumber.ZERO) {
573
+ return this.buildInitialBlock();
574
+ }
575
+ return await this.blockSource.getL2Block(blockNumber);
564
576
  }
565
577
 
566
578
  /**
@@ -568,9 +580,17 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
568
580
  * @param blockHash - The block hash being requested.
569
581
  * @returns The requested block.
570
582
  */
571
- public async getBlockByHash(blockHash: Fr): Promise<L2Block | undefined> {
572
- const publishedBlock = await this.blockSource.getPublishedBlockByHash(blockHash);
573
- return publishedBlock?.block;
583
+ public async getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
584
+ const initialBlockHash = await this.#getInitialHeaderHash();
585
+ if (blockHash.equals(initialBlockHash)) {
586
+ return this.buildInitialBlock();
587
+ }
588
+ return await this.blockSource.getL2BlockByHash(blockHash);
589
+ }
590
+
591
+ private buildInitialBlock(): L2Block {
592
+ const initialHeader = this.worldStateSynchronizer.getCommitted().getInitialHeader();
593
+ return L2Block.empty(initialHeader);
574
594
  }
575
595
 
576
596
  /**
@@ -579,8 +599,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
579
599
  * @returns The requested block.
580
600
  */
581
601
  public async getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
582
- const publishedBlock = await this.blockSource.getPublishedBlockByArchive(archive);
583
- return publishedBlock?.block;
602
+ return await this.blockSource.getL2BlockByArchive(archive);
584
603
  }
585
604
 
586
605
  /**
@@ -590,19 +609,23 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
590
609
  * @returns The blocks requested.
591
610
  */
592
611
  public async getBlocks(from: BlockNumber, limit: number): Promise<L2Block[]> {
593
- return (await this.blockSource.getBlocks(from, limit)) ?? [];
612
+ return (await this.blockSource.getBlocks(from, BlockNumber(limit))) ?? [];
594
613
  }
595
614
 
596
- public async getPublishedBlocks(from: BlockNumber, limit: number): Promise<PublishedL2Block[]> {
597
- return (await this.blockSource.getPublishedBlocks(from, limit)) ?? [];
615
+ public async getCheckpoints(from: CheckpointNumber, limit: number): Promise<PublishedCheckpoint[]> {
616
+ return (await this.blockSource.getCheckpoints(from, limit)) ?? [];
617
+ }
618
+
619
+ public async getCheckpointedBlocks(from: BlockNumber, limit: number) {
620
+ return (await this.blockSource.getCheckpointedBlocks(from, limit)) ?? [];
598
621
  }
599
622
 
600
623
  /**
601
- * Method to fetch the current base fees.
602
- * @returns The current base fees.
624
+ * Method to fetch the current min L2 fees.
625
+ * @returns The current min L2 fees.
603
626
  */
604
- public async getCurrentBaseFees(): Promise<GasFees> {
605
- return await this.globalVariableBuilder.getCurrentBaseFees();
627
+ public async getCurrentMinFees(): Promise<GasFees> {
628
+ return await this.globalVariableBuilder.getCurrentMinFees();
606
629
  }
607
630
 
608
631
  public async getMaxPriorityFees(): Promise<GasFees> {
@@ -625,6 +648,10 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
625
648
  return await this.blockSource.getProvenBlockNumber();
626
649
  }
627
650
 
651
+ public async getCheckpointedBlockNumber(): Promise<BlockNumber> {
652
+ return await this.blockSource.getCheckpointedL2BlockNumber();
653
+ }
654
+
628
655
  /**
629
656
  * Method to fetch the version of the package.
630
657
  * @returns The node package version
@@ -657,15 +684,43 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
657
684
  return this.contractDataSource.getContract(address);
658
685
  }
659
686
 
660
- /**
661
- * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
662
- * @param tags - The tags to filter the logs by.
663
- * @param logsPerTag - The maximum number of logs to return for each tag. By default no limit is set
664
- * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
665
- * that tag.
666
- */
667
- public getLogsByTags(tags: Fr[], logsPerTag?: number): Promise<TxScopedL2Log[][]> {
668
- return this.logsSource.getLogsByTags(tags, logsPerTag);
687
+ public async getPrivateLogsByTags(
688
+ tags: SiloedTag[],
689
+ page?: number,
690
+ referenceBlock?: BlockHash,
691
+ ): Promise<TxScopedL2Log[][]> {
692
+ if (referenceBlock) {
693
+ const initialBlockHash = await this.#getInitialHeaderHash();
694
+ if (!referenceBlock.equals(initialBlockHash)) {
695
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
696
+ if (!header) {
697
+ throw new Error(
698
+ `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
699
+ );
700
+ }
701
+ }
702
+ }
703
+ return this.logsSource.getPrivateLogsByTags(tags, page);
704
+ }
705
+
706
+ public async getPublicLogsByTagsFromContract(
707
+ contractAddress: AztecAddress,
708
+ tags: Tag[],
709
+ page?: number,
710
+ referenceBlock?: BlockHash,
711
+ ): Promise<TxScopedL2Log[][]> {
712
+ if (referenceBlock) {
713
+ const initialBlockHash = await this.#getInitialHeaderHash();
714
+ if (!referenceBlock.equals(initialBlockHash)) {
715
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
716
+ if (!header) {
717
+ throw new Error(
718
+ `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
719
+ );
720
+ }
721
+ }
722
+ }
723
+ return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags, page);
669
724
  }
670
725
 
671
726
  /**
@@ -712,21 +767,26 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
712
767
  }
713
768
 
714
769
  public async getTxReceipt(txHash: TxHash): Promise<TxReceipt> {
715
- let txReceipt = new TxReceipt(txHash, TxStatus.DROPPED, 'Tx dropped by P2P node.');
716
-
717
- // We first check if the tx is in pending (instead of first checking if it is mined) because if we first check
718
- // for mined and then for pending there could be a race condition where the tx is mined between the two checks
719
- // and we would incorrectly return a TxReceipt with status DROPPED
720
- if ((await this.p2pClient.getTxStatus(txHash)) === 'pending') {
721
- txReceipt = new TxReceipt(txHash, TxStatus.PENDING, '');
722
- }
770
+ // Check the tx pool status first. If the tx is known to the pool (pending or mined), we'll use that
771
+ // as a fallback if we don't find a settled receipt in the archiver.
772
+ const txPoolStatus = await this.p2pClient.getTxStatus(txHash);
773
+ const isKnownToPool = txPoolStatus === 'pending' || txPoolStatus === 'mined';
723
774
 
775
+ // Then get the actual tx from the archiver, which tracks every tx in a mined block.
724
776
  const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
777
+
725
778
  if (settledTxReceipt) {
726
- txReceipt = settledTxReceipt;
779
+ // If the archiver has the receipt then return it.
780
+ return settledTxReceipt;
781
+ } else if (isKnownToPool) {
782
+ // If the tx is in the pool but not in the archiver, it's pending.
783
+ // This handles race conditions between archiver and p2p, where the archiver
784
+ // has pruned the block in which a tx was mined, but p2p has not caught up yet.
785
+ return new TxReceipt(txHash, TxStatus.PENDING, undefined, undefined);
786
+ } else {
787
+ // Otherwise, if we don't know the tx, we consider it dropped.
788
+ return new TxReceipt(txHash, TxStatus.DROPPED, undefined, 'Tx dropped by P2P node');
727
789
  }
728
-
729
- return txReceipt;
730
790
  }
731
791
 
732
792
  public getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
@@ -746,10 +806,19 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
746
806
  await tryStop(this.p2pClient);
747
807
  await tryStop(this.worldStateSynchronizer);
748
808
  await tryStop(this.blockSource);
809
+ await tryStop(this.blobClient);
749
810
  await tryStop(this.telemetry);
750
811
  this.log.info(`Stopped Aztec Node`);
751
812
  }
752
813
 
814
+ /**
815
+ * Returns the blob client used by this node.
816
+ * @internal - Exposed for testing purposes only.
817
+ */
818
+ public getBlobClient(): BlobClientInterface | undefined {
819
+ return this.blobClient;
820
+ }
821
+
753
822
  /**
754
823
  * Method to retrieve pending txs.
755
824
  * @param limit - The number of items to returns
@@ -782,20 +851,12 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
782
851
  return compactArray(await Promise.all(txHashes.map(txHash => this.getTxByHash(txHash))));
783
852
  }
784
853
 
785
- /**
786
- * Find the indexes of the given leaves in the given tree along with a block metadata pointing to the block in which
787
- * the leaves were inserted.
788
- * @param blockNumber - The block number at which to get the data or 'latest' for latest data.
789
- * @param treeId - The tree to search in.
790
- * @param leafValues - The values to search for.
791
- * @returns The indices of leaves and the block metadata of a block in which the leaves were inserted.
792
- */
793
854
  public async findLeavesIndexes(
794
- blockNumber: BlockParameter,
855
+ referenceBlock: BlockParameter,
795
856
  treeId: MerkleTreeId,
796
857
  leafValues: Fr[],
797
858
  ): Promise<(DataInBlock<bigint> | undefined)[]> {
798
- const committedDb = await this.#getWorldState(blockNumber);
859
+ const committedDb = await this.#getWorldState(referenceBlock);
799
860
  const maybeIndices = await committedDb.findLeafIndices(
800
861
  treeId,
801
862
  leafValues.map(x => x.toBuffer()),
@@ -847,56 +908,28 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
847
908
  }
848
909
  return {
849
910
  l2BlockNumber: BlockNumber(Number(blockNumber)),
850
- l2BlockHash: L2BlockHash.fromField(blockHash),
911
+ l2BlockHash: new BlockHash(blockHash),
851
912
  data: index,
852
913
  };
853
914
  });
854
915
  }
855
916
 
856
- /**
857
- * Returns a sibling path for the given index in the nullifier tree.
858
- * @param blockNumber - The block number at which to get the data.
859
- * @param leafIndex - The index of the leaf for which the sibling path is required.
860
- * @returns The sibling path for the leaf index.
861
- */
862
- public async getNullifierSiblingPath(
863
- blockNumber: BlockParameter,
864
- leafIndex: bigint,
865
- ): Promise<SiblingPath<typeof NULLIFIER_TREE_HEIGHT>> {
866
- const committedDb = await this.#getWorldState(blockNumber);
867
- return committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, leafIndex);
868
- }
869
-
870
- /**
871
- * Returns a sibling path for the given index in the data tree.
872
- * @param blockNumber - The block number at which to get the data.
873
- * @param leafIndex - The index of the leaf for which the sibling path is required.
874
- * @returns The sibling path for the leaf index.
875
- */
876
- public async getNoteHashSiblingPath(
877
- blockNumber: BlockParameter,
878
- leafIndex: bigint,
879
- ): Promise<SiblingPath<typeof NOTE_HASH_TREE_HEIGHT>> {
880
- const committedDb = await this.#getWorldState(blockNumber);
881
- return committedDb.getSiblingPath(MerkleTreeId.NOTE_HASH_TREE, leafIndex);
882
- }
883
-
884
- public async getArchiveMembershipWitness(
885
- blockNumber: BlockParameter,
886
- archive: Fr,
917
+ public async getBlockHashMembershipWitness(
918
+ referenceBlock: BlockParameter,
919
+ blockHash: BlockHash,
887
920
  ): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
888
- const committedDb = await this.#getWorldState(blockNumber);
889
- const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [archive]);
921
+ const committedDb = await this.#getWorldState(referenceBlock);
922
+ const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [blockHash]);
890
923
  return pathAndIndex === undefined
891
924
  ? undefined
892
925
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
893
926
  }
894
927
 
895
928
  public async getNoteHashMembershipWitness(
896
- blockNumber: BlockParameter,
929
+ referenceBlock: BlockParameter,
897
930
  noteHash: Fr,
898
931
  ): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
899
- const committedDb = await this.#getWorldState(blockNumber);
932
+ const committedDb = await this.#getWorldState(referenceBlock);
900
933
  const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.NOTE_HASH_TREE>(
901
934
  MerkleTreeId.NOTE_HASH_TREE,
902
935
  [noteHash],
@@ -906,17 +939,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
906
939
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
907
940
  }
908
941
 
909
- /**
910
- * Returns the index and a sibling path for a leaf in the committed l1 to l2 data tree.
911
- * @param blockNumber - The block number at which to get the data.
912
- * @param l1ToL2Message - The l1ToL2Message to get the index / sibling path for.
913
- * @returns A tuple of the index and the sibling path of the L1ToL2Message (undefined if not found).
914
- */
915
942
  public async getL1ToL2MessageMembershipWitness(
916
- blockNumber: BlockParameter,
943
+ referenceBlock: BlockParameter,
917
944
  l1ToL2Message: Fr,
918
945
  ): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined> {
919
- const db = await this.#getWorldState(blockNumber);
946
+ const db = await this.#getWorldState(referenceBlock);
920
947
  const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [l1ToL2Message]);
921
948
  if (!witness) {
922
949
  return undefined;
@@ -944,56 +971,36 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
944
971
  }
945
972
 
946
973
  /**
947
- * Returns all the L2 to L1 messages in a block.
948
- * @param blockNumber - The block number at which to get the data.
949
- * @returns The L2 to L1 messages (undefined if the block number is not found).
974
+ * Returns all the L2 to L1 messages in an epoch.
975
+ * @param epoch - The epoch at which to get the data.
976
+ * @returns The L2 to L1 messages (empty array if the epoch is not found).
950
977
  */
951
- public async getL2ToL1Messages(blockNumber: BlockParameter): Promise<Fr[][] | undefined> {
952
- const block = await this.blockSource.getBlock(
953
- blockNumber === 'latest' ? await this.getBlockNumber() : (blockNumber as BlockNumber),
978
+ public async getL2ToL1Messages(epoch: EpochNumber): Promise<Fr[][][][]> {
979
+ // Assumes `getCheckpointedBlocksForEpoch` returns blocks in ascending order of block number.
980
+ const checkpointedBlocks = await this.blockSource.getCheckpointedBlocksForEpoch(epoch);
981
+ const blocksInCheckpoints: L2Block[][] = [];
982
+ let previousSlotNumber = SlotNumber.ZERO;
983
+ let checkpointIndex = -1;
984
+ for (const checkpointedBlock of checkpointedBlocks) {
985
+ const block = checkpointedBlock.block;
986
+ const slotNumber = block.header.globalVariables.slotNumber;
987
+ if (slotNumber !== previousSlotNumber) {
988
+ checkpointIndex++;
989
+ blocksInCheckpoints.push([]);
990
+ previousSlotNumber = slotNumber;
991
+ }
992
+ blocksInCheckpoints[checkpointIndex].push(block);
993
+ }
994
+ return blocksInCheckpoints.map(blocks =>
995
+ blocks.map(block => block.body.txEffects.map(txEffect => txEffect.l2ToL1Msgs)),
954
996
  );
955
- return block?.body.txEffects.map(txEffect => txEffect.l2ToL1Msgs);
956
- }
957
-
958
- /**
959
- * Returns a sibling path for a leaf in the committed blocks tree.
960
- * @param blockNumber - The block number at which to get the data.
961
- * @param leafIndex - Index of the leaf in the tree.
962
- * @returns The sibling path.
963
- */
964
- public async getArchiveSiblingPath(
965
- blockNumber: BlockParameter,
966
- leafIndex: bigint,
967
- ): Promise<SiblingPath<typeof ARCHIVE_HEIGHT>> {
968
- const committedDb = await this.#getWorldState(blockNumber);
969
- return committedDb.getSiblingPath(MerkleTreeId.ARCHIVE, leafIndex);
970
997
  }
971
998
 
972
- /**
973
- * Returns a sibling path for a leaf in the committed public data tree.
974
- * @param blockNumber - The block number at which to get the data.
975
- * @param leafIndex - Index of the leaf in the tree.
976
- * @returns The sibling path.
977
- */
978
- public async getPublicDataSiblingPath(
979
- blockNumber: BlockParameter,
980
- leafIndex: bigint,
981
- ): Promise<SiblingPath<typeof PUBLIC_DATA_TREE_HEIGHT>> {
982
- const committedDb = await this.#getWorldState(blockNumber);
983
- return committedDb.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex);
984
- }
985
-
986
- /**
987
- * Returns a nullifier membership witness for a given nullifier at a given block.
988
- * @param blockNumber - The block number at which to get the index.
989
- * @param nullifier - Nullifier we try to find witness for.
990
- * @returns The nullifier membership witness (if found).
991
- */
992
999
  public async getNullifierMembershipWitness(
993
- blockNumber: BlockParameter,
1000
+ referenceBlock: BlockParameter,
994
1001
  nullifier: Fr,
995
1002
  ): Promise<NullifierMembershipWitness | undefined> {
996
- const db = await this.#getWorldState(blockNumber);
1003
+ const db = await this.#getWorldState(referenceBlock);
997
1004
  const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]);
998
1005
  if (!witness) {
999
1006
  return undefined;
@@ -1010,7 +1017,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1010
1017
 
1011
1018
  /**
1012
1019
  * Returns a low nullifier membership witness for a given nullifier at a given block.
1013
- * @param blockNumber - The block number at which to get the index.
1020
+ * @param referenceBlock - The block parameter (block number, block hash, or 'latest') at which to get the data
1021
+ * (which contains the root of the nullifier tree in which we are searching for the nullifier).
1014
1022
  * @param nullifier - Nullifier we try to find the low nullifier witness for.
1015
1023
  * @returns The low nullifier membership witness (if found).
1016
1024
  * @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
@@ -1023,10 +1031,10 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1023
1031
  * TODO: This is a confusing behavior and we should eventually address that.
1024
1032
  */
1025
1033
  public async getLowNullifierMembershipWitness(
1026
- blockNumber: BlockParameter,
1034
+ referenceBlock: BlockParameter,
1027
1035
  nullifier: Fr,
1028
1036
  ): Promise<NullifierMembershipWitness | undefined> {
1029
- const committedDb = await this.#getWorldState(blockNumber);
1037
+ const committedDb = await this.#getWorldState(referenceBlock);
1030
1038
  const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
1031
1039
  if (!findResult) {
1032
1040
  return undefined;
@@ -1041,8 +1049,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1041
1049
  return new NullifierMembershipWitness(BigInt(index), preimageData as NullifierLeafPreimage, siblingPath);
1042
1050
  }
1043
1051
 
1044
- async getPublicDataWitness(blockNumber: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1045
- const committedDb = await this.#getWorldState(blockNumber);
1052
+ async getPublicDataWitness(referenceBlock: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1053
+ const committedDb = await this.#getWorldState(referenceBlock);
1046
1054
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
1047
1055
  if (!lowLeafResult) {
1048
1056
  return undefined;
@@ -1056,19 +1064,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1056
1064
  }
1057
1065
  }
1058
1066
 
1059
- /**
1060
- * Gets the storage value at the given contract storage slot.
1061
- *
1062
- * @remarks The storage slot here refers to the slot as it is defined in Noir not the index in the merkle tree.
1063
- * Aztec's version of `eth_getStorageAt`.
1064
- *
1065
- * @param contract - Address of the contract to query.
1066
- * @param slot - Slot to query.
1067
- * @param blockNumber - The block number at which to get the data or 'latest'.
1068
- * @returns Storage value at the given contract slot.
1069
- */
1070
- public async getPublicStorageAt(blockNumber: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
1071
- const committedDb = await this.#getWorldState(blockNumber);
1067
+ public async getPublicStorageAt(referenceBlock: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
1068
+ const committedDb = await this.#getWorldState(referenceBlock);
1072
1069
  const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
1073
1070
 
1074
1071
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
@@ -1082,24 +1079,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1082
1079
  return preimage.leaf.value;
1083
1080
  }
1084
1081
 
1085
- /**
1086
- * Returns the currently committed block header, or the initial header if no blocks have been produced.
1087
- * @returns The current committed block header.
1088
- */
1089
- public async getBlockHeader(blockNumber: BlockParameter = 'latest'): Promise<BlockHeader | undefined> {
1090
- return blockNumber === BlockNumber.ZERO ||
1091
- (blockNumber === 'latest' && (await this.blockSource.getBlockNumber()) === BlockNumber.ZERO)
1092
- ? this.worldStateSynchronizer.getCommitted().getInitialHeader()
1093
- : this.blockSource.getBlockHeader(blockNumber === 'latest' ? blockNumber : (blockNumber as BlockNumber));
1094
- }
1095
-
1096
- /**
1097
- * Get a block header specified by its hash.
1098
- * @param blockHash - The block hash being requested.
1099
- * @returns The requested block header.
1100
- */
1101
- public async getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
1102
- return await this.blockSource.getBlockHeaderByHash(blockHash);
1082
+ public async getBlockHeader(block: BlockParameter = 'latest'): Promise<BlockHeader | undefined> {
1083
+ if (BlockHash.isBlockHash(block)) {
1084
+ const initialBlockHash = await this.#getInitialHeaderHash();
1085
+ if (block.equals(initialBlockHash)) {
1086
+ // Block source doesn't handle initial header so we need to handle the case separately.
1087
+ return this.worldStateSynchronizer.getCommitted().getInitialHeader();
1088
+ }
1089
+ return this.blockSource.getBlockHeaderByHash(block);
1090
+ } else {
1091
+ // Block source doesn't handle initial header so we need to handle the case separately.
1092
+ const blockNumber = block === 'latest' ? await this.getBlockNumber() : (block as BlockNumber);
1093
+ if (blockNumber === BlockNumber.ZERO) {
1094
+ return this.worldStateSynchronizer.getCommitted().getInitialHeader();
1095
+ }
1096
+ return this.blockSource.getBlockHeader(block);
1097
+ }
1103
1098
  }
1104
1099
 
1105
1100
  /**
@@ -1149,6 +1144,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1149
1144
  this.contractDataSource,
1150
1145
  new DateProvider(),
1151
1146
  this.telemetry,
1147
+ this.log.getBindings(),
1152
1148
  );
1153
1149
 
1154
1150
  this.log.verbose(`Simulating public calls for tx ${txHash}`, {
@@ -1202,16 +1198,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1202
1198
  // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1203
1199
  const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1204
1200
  const blockNumber = BlockNumber((await this.blockSource.getBlockNumber()) + 1);
1205
- const validator = createValidatorForAcceptingTxs(db, this.contractDataSource, verifier, {
1206
- timestamp: nextSlotTimestamp,
1207
- blockNumber,
1208
- l1ChainId: this.l1ChainId,
1209
- rollupVersion: this.version,
1210
- setupAllowList: this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions()),
1211
- gasFees: await this.getCurrentBaseFees(),
1212
- skipFeeEnforcement,
1213
- txsPermitted: !this.config.disableTransactions,
1214
- });
1201
+ const validator = createValidatorForAcceptingTxs(
1202
+ db,
1203
+ this.contractDataSource,
1204
+ verifier,
1205
+ {
1206
+ timestamp: nextSlotTimestamp,
1207
+ blockNumber,
1208
+ l1ChainId: this.l1ChainId,
1209
+ rollupVersion: this.version,
1210
+ setupAllowList: this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions()),
1211
+ gasFees: await this.getCurrentMinFees(),
1212
+ skipFeeEnforcement,
1213
+ txsPermitted: !this.config.disableTransactions,
1214
+ },
1215
+ this.log.getBindings(),
1216
+ );
1215
1217
 
1216
1218
  return await validator.validateTx(tx);
1217
1219
  }
@@ -1280,7 +1282,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1280
1282
  }
1281
1283
 
1282
1284
  // And it has an L2 block hash
1283
- const l2BlockHash = await archiver.getL2Tips().then(tips => tips.latest.hash);
1285
+ const l2BlockHash = await archiver.getL2Tips().then(tips => tips.proposed.hash);
1284
1286
  if (!l2BlockHash) {
1285
1287
  this.metrics.recordSnapshotError();
1286
1288
  throw new Error(`Archiver has no latest L2 block hash downloaded. Cannot start snapshot.`);
@@ -1314,7 +1316,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1314
1316
  throw new Error('Archiver implementation does not support rollbacks.');
1315
1317
  }
1316
1318
 
1317
- const finalizedBlock = await archiver.getL2Tips().then(tips => tips.finalized.number);
1319
+ const finalizedBlock = await archiver.getL2Tips().then(tips => tips.finalized.block.number);
1318
1320
  if (targetBlock < finalizedBlock) {
1319
1321
  if (force) {
1320
1322
  this.log.warn(`Clearing world state database to allow rolling back behind finalized block ${finalizedBlock}`);
@@ -1375,16 +1377,19 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1375
1377
  }
1376
1378
  }
1377
1379
 
1380
+ #getInitialHeaderHash(): Promise<BlockHash> {
1381
+ if (!this.initialHeaderHashPromise) {
1382
+ this.initialHeaderHashPromise = this.worldStateSynchronizer.getCommitted().getInitialHeader().hash();
1383
+ }
1384
+ return this.initialHeaderHashPromise;
1385
+ }
1386
+
1378
1387
  /**
1379
1388
  * Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
1380
- * @param blockNumber - The block number at which to get the data.
1389
+ * @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
1381
1390
  * @returns An instance of a committed MerkleTreeOperations
1382
1391
  */
1383
- async #getWorldState(blockNumber: BlockParameter) {
1384
- if (typeof blockNumber === 'number' && blockNumber < INITIAL_L2_BLOCK_NUM - 1) {
1385
- throw new Error('Invalid block number to get world state for: ' + blockNumber);
1386
- }
1387
-
1392
+ async #getWorldState(block: BlockParameter) {
1388
1393
  let blockSyncedTo: BlockNumber = BlockNumber.ZERO;
1389
1394
  try {
1390
1395
  // Attempt to sync the world state if necessary
@@ -1393,15 +1398,39 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1393
1398
  this.log.error(`Error getting world state: ${err}`);
1394
1399
  }
1395
1400
 
1396
- // using a snapshot could be less efficient than using the committed db
1397
- if (blockNumber === 'latest' /*|| blockNumber === blockSyncedTo*/) {
1398
- this.log.debug(`Using committed db for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1401
+ if (block === 'latest') {
1402
+ this.log.debug(`Using committed db for block 'latest', world state synced upto ${blockSyncedTo}`);
1399
1403
  return this.worldStateSynchronizer.getCommitted();
1400
- } else if (blockNumber <= blockSyncedTo) {
1404
+ }
1405
+
1406
+ if (BlockHash.isBlockHash(block)) {
1407
+ const initialBlockHash = await this.#getInitialHeaderHash();
1408
+ if (block.equals(initialBlockHash)) {
1409
+ // Block source doesn't handle initial header so we need to handle the case separately.
1410
+ return this.worldStateSynchronizer.getSnapshot(BlockNumber.ZERO);
1411
+ }
1412
+
1413
+ const header = await this.blockSource.getBlockHeaderByHash(block);
1414
+ if (!header) {
1415
+ throw new Error(
1416
+ `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.`,
1417
+ );
1418
+ }
1419
+ const blockNumber = header.getBlockNumber();
1401
1420
  this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1402
- return this.worldStateSynchronizer.getSnapshot(blockNumber as BlockNumber);
1403
- } else {
1404
- throw new Error(`Block ${blockNumber} not yet synced`);
1421
+ return this.worldStateSynchronizer.getSnapshot(blockNumber);
1422
+ }
1423
+
1424
+ // Block number provided
1425
+ {
1426
+ const blockNumber = block as BlockNumber;
1427
+
1428
+ if (blockNumber > blockSyncedTo) {
1429
+ throw new Error(`Queried block ${block} not yet synced by the node (node is synced upto ${blockSyncedTo}).`);
1430
+ }
1431
+
1432
+ this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1433
+ return this.worldStateSynchronizer.getSnapshot(blockNumber);
1405
1434
  }
1406
1435
  }
1407
1436