@aztec/aztec-node 0.0.1-commit.7d4e6cd → 0.0.1-commit.858058eac

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,14 +1,7 @@
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 { 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';
@@ -31,7 +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 { BlockBuilder, GlobalVariableBuilder, SequencerClient, type SequencerPublisher } from '@aztec/sequencer-client';
27
+ import { GlobalVariableBuilder, SequencerClient, type SequencerPublisher } from '@aztec/sequencer-client';
35
28
  import { PublicProcessorFactory } from '@aztec/simulator/server';
36
29
  import {
37
30
  AttestationsBlockWatcher,
@@ -42,15 +35,7 @@ import {
42
35
  } from '@aztec/slasher';
43
36
  import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
44
37
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
45
- import {
46
- type BlockParameter,
47
- type DataInBlock,
48
- type L2Block,
49
- L2BlockHash,
50
- L2BlockNew,
51
- type L2BlockSource,
52
- type PublishedL2Block,
53
- } from '@aztec/stdlib/block';
38
+ import { BlockHash, type BlockParameter, type DataInBlock, L2Block, type L2BlockSource } from '@aztec/stdlib/block';
54
39
  import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
55
40
  import type {
56
41
  ContractClassPublic,
@@ -128,6 +113,7 @@ import { NodeMetrics } from './node_metrics.js';
128
113
  */
129
114
  export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
130
115
  private metrics: NodeMetrics;
116
+ private initialHeaderHashPromise: Promise<BlockHash> | undefined = undefined;
131
117
 
132
118
  // Prevent two snapshot operations to happen simultaneously
133
119
  private isUploadingSnapshot = false;
@@ -309,18 +295,10 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
309
295
  // We should really not be modifying the config object
310
296
  config.txPublicSetupAllowList = config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
311
297
 
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
298
  // Create FullNodeCheckpointsBuilder for validator and non-validator block proposal handling
322
299
  const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder(
323
300
  { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
301
+ worldStateSynchronizer,
324
302
  archiver,
325
303
  dateProvider,
326
304
  telemetry,
@@ -330,7 +308,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
330
308
  const watchers: Watcher[] = [];
331
309
 
332
310
  // Create validator client if required
333
- const validatorClient = createValidatorClient(config, {
311
+ const validatorClient = await createValidatorClient(config, {
334
312
  checkpointsBuilder: validatorCheckpointsBuilder,
335
313
  worldState: worldStateSynchronizer,
336
314
  p2pClient,
@@ -387,7 +365,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
387
365
  archiver,
388
366
  epochCache,
389
367
  p2pClient.getTxProvider(),
390
- blockBuilder,
368
+ validatorCheckpointsBuilder,
391
369
  config,
392
370
  );
393
371
  watchers.push(epochPruneWatcher);
@@ -452,6 +430,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
452
430
  // Create and start the sequencer client
453
431
  const checkpointsBuilder = new CheckpointsBuilder(
454
432
  { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
433
+ worldStateSynchronizer,
455
434
  archiver,
456
435
  dateProvider,
457
436
  telemetry,
@@ -574,19 +553,26 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
574
553
  enr,
575
554
  l1ContractAddresses: contractAddresses,
576
555
  protocolContractAddresses: protocolContractAddresses,
556
+ realProofs: !!this.config.realProofs,
577
557
  };
578
558
 
579
559
  return nodeInfo;
580
560
  }
581
561
 
582
562
  /**
583
- * Get a block specified by its number.
584
- * @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').
585
565
  * @returns The requested block.
586
566
  */
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);
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);
590
576
  }
591
577
 
592
578
  /**
@@ -594,9 +580,17 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
594
580
  * @param blockHash - The block hash being requested.
595
581
  * @returns The requested block.
596
582
  */
597
- public async getBlockByHash(blockHash: Fr): Promise<L2Block | undefined> {
598
- const publishedBlock = await this.blockSource.getPublishedBlockByHash(blockHash);
599
- 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);
600
594
  }
601
595
 
602
596
  /**
@@ -605,8 +599,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
605
599
  * @returns The requested block.
606
600
  */
607
601
  public async getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
608
- const publishedBlock = await this.blockSource.getPublishedBlockByArchive(archive);
609
- return publishedBlock?.block;
602
+ return await this.blockSource.getL2BlockByArchive(archive);
610
603
  }
611
604
 
612
605
  /**
@@ -616,23 +609,15 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
616
609
  * @returns The blocks requested.
617
610
  */
618
611
  public async getBlocks(from: BlockNumber, limit: number): Promise<L2Block[]> {
619
- return (await this.blockSource.getBlocks(from, limit)) ?? [];
620
- }
621
-
622
- public async getPublishedBlocks(from: BlockNumber, limit: number): Promise<PublishedL2Block[]> {
623
- return (await this.blockSource.getPublishedBlocks(from, limit)) ?? [];
612
+ return (await this.blockSource.getBlocks(from, BlockNumber(limit))) ?? [];
624
613
  }
625
614
 
626
- public async getPublishedCheckpoints(from: CheckpointNumber, limit: number): Promise<PublishedCheckpoint[]> {
627
- return (await this.blockSource.getPublishedCheckpoints(from, limit)) ?? [];
615
+ public async getCheckpoints(from: CheckpointNumber, limit: number): Promise<PublishedCheckpoint[]> {
616
+ return (await this.blockSource.getCheckpoints(from, limit)) ?? [];
628
617
  }
629
618
 
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)) ?? [];
619
+ public async getCheckpointedBlocks(from: BlockNumber, limit: number) {
620
+ return (await this.blockSource.getCheckpointedBlocks(from, limit)) ?? [];
636
621
  }
637
622
 
638
623
  /**
@@ -663,6 +648,10 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
663
648
  return await this.blockSource.getProvenBlockNumber();
664
649
  }
665
650
 
651
+ public async getCheckpointedBlockNumber(): Promise<BlockNumber> {
652
+ return await this.blockSource.getCheckpointedL2BlockNumber();
653
+ }
654
+
666
655
  /**
667
656
  * Method to fetch the version of the package.
668
657
  * @returns The node package version
@@ -695,12 +684,43 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
695
684
  return this.contractDataSource.getContract(address);
696
685
  }
697
686
 
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);
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);
704
724
  }
705
725
 
706
726
  /**
@@ -747,21 +767,26 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
747
767
  }
748
768
 
749
769
  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
- }
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';
758
774
 
775
+ // Then get the actual tx from the archiver, which tracks every tx in a mined block.
759
776
  const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
777
+
760
778
  if (settledTxReceipt) {
761
- 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');
762
789
  }
763
-
764
- return txReceipt;
765
790
  }
766
791
 
767
792
  public getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
@@ -826,20 +851,12 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
826
851
  return compactArray(await Promise.all(txHashes.map(txHash => this.getTxByHash(txHash))));
827
852
  }
828
853
 
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
854
  public async findLeavesIndexes(
838
- blockNumber: BlockParameter,
855
+ referenceBlock: BlockParameter,
839
856
  treeId: MerkleTreeId,
840
857
  leafValues: Fr[],
841
858
  ): Promise<(DataInBlock<bigint> | undefined)[]> {
842
- const committedDb = await this.#getWorldState(blockNumber);
859
+ const committedDb = await this.#getWorldState(referenceBlock);
843
860
  const maybeIndices = await committedDb.findLeafIndices(
844
861
  treeId,
845
862
  leafValues.map(x => x.toBuffer()),
@@ -891,56 +908,28 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
891
908
  }
892
909
  return {
893
910
  l2BlockNumber: BlockNumber(Number(blockNumber)),
894
- l2BlockHash: L2BlockHash.fromField(blockHash),
911
+ l2BlockHash: new BlockHash(blockHash),
895
912
  data: index,
896
913
  };
897
914
  });
898
915
  }
899
916
 
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,
917
+ public async getBlockHashMembershipWitness(
918
+ referenceBlock: BlockParameter,
919
+ blockHash: BlockHash,
931
920
  ): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
932
- const committedDb = await this.#getWorldState(blockNumber);
933
- 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]);
934
923
  return pathAndIndex === undefined
935
924
  ? undefined
936
925
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
937
926
  }
938
927
 
939
928
  public async getNoteHashMembershipWitness(
940
- blockNumber: BlockParameter,
929
+ referenceBlock: BlockParameter,
941
930
  noteHash: Fr,
942
931
  ): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
943
- const committedDb = await this.#getWorldState(blockNumber);
932
+ const committedDb = await this.#getWorldState(referenceBlock);
944
933
  const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.NOTE_HASH_TREE>(
945
934
  MerkleTreeId.NOTE_HASH_TREE,
946
935
  [noteHash],
@@ -950,17 +939,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
950
939
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
951
940
  }
952
941
 
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
942
  public async getL1ToL2MessageMembershipWitness(
960
- blockNumber: BlockParameter,
943
+ referenceBlock: BlockParameter,
961
944
  l1ToL2Message: Fr,
962
945
  ): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined> {
963
- const db = await this.#getWorldState(blockNumber);
946
+ const db = await this.#getWorldState(referenceBlock);
964
947
  const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [l1ToL2Message]);
965
948
  if (!witness) {
966
949
  return undefined;
@@ -993,12 +976,13 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
993
976
  * @returns The L2 to L1 messages (empty array if the epoch is not found).
994
977
  */
995
978
  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);
979
+ // Assumes `getCheckpointedBlocksForEpoch` returns blocks in ascending order of block number.
980
+ const checkpointedBlocks = await this.blockSource.getCheckpointedBlocksForEpoch(epoch);
998
981
  const blocksInCheckpoints: L2Block[][] = [];
999
982
  let previousSlotNumber = SlotNumber.ZERO;
1000
983
  let checkpointIndex = -1;
1001
- for (const block of blocks) {
984
+ for (const checkpointedBlock of checkpointedBlocks) {
985
+ const block = checkpointedBlock.block;
1002
986
  const slotNumber = block.header.globalVariables.slotNumber;
1003
987
  if (slotNumber !== previousSlotNumber) {
1004
988
  checkpointIndex++;
@@ -1012,45 +996,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1012
996
  );
1013
997
  }
1014
998
 
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
999
  public async getNullifierMembershipWitness(
1050
- blockNumber: BlockParameter,
1000
+ referenceBlock: BlockParameter,
1051
1001
  nullifier: Fr,
1052
1002
  ): Promise<NullifierMembershipWitness | undefined> {
1053
- const db = await this.#getWorldState(blockNumber);
1003
+ const db = await this.#getWorldState(referenceBlock);
1054
1004
  const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]);
1055
1005
  if (!witness) {
1056
1006
  return undefined;
@@ -1067,7 +1017,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1067
1017
 
1068
1018
  /**
1069
1019
  * 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.
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).
1071
1022
  * @param nullifier - Nullifier we try to find the low nullifier witness for.
1072
1023
  * @returns The low nullifier membership witness (if found).
1073
1024
  * @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
@@ -1080,10 +1031,10 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1080
1031
  * TODO: This is a confusing behavior and we should eventually address that.
1081
1032
  */
1082
1033
  public async getLowNullifierMembershipWitness(
1083
- blockNumber: BlockParameter,
1034
+ referenceBlock: BlockParameter,
1084
1035
  nullifier: Fr,
1085
1036
  ): Promise<NullifierMembershipWitness | undefined> {
1086
- const committedDb = await this.#getWorldState(blockNumber);
1037
+ const committedDb = await this.#getWorldState(referenceBlock);
1087
1038
  const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
1088
1039
  if (!findResult) {
1089
1040
  return undefined;
@@ -1098,8 +1049,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1098
1049
  return new NullifierMembershipWitness(BigInt(index), preimageData as NullifierLeafPreimage, siblingPath);
1099
1050
  }
1100
1051
 
1101
- async getPublicDataWitness(blockNumber: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1102
- const committedDb = await this.#getWorldState(blockNumber);
1052
+ async getPublicDataWitness(referenceBlock: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1053
+ const committedDb = await this.#getWorldState(referenceBlock);
1103
1054
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
1104
1055
  if (!lowLeafResult) {
1105
1056
  return undefined;
@@ -1113,19 +1064,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1113
1064
  }
1114
1065
  }
1115
1066
 
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);
1067
+ public async getPublicStorageAt(referenceBlock: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
1068
+ const committedDb = await this.#getWorldState(referenceBlock);
1129
1069
  const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
1130
1070
 
1131
1071
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
@@ -1139,24 +1079,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1139
1079
  return preimage.leaf.value;
1140
1080
  }
1141
1081
 
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);
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
+ }
1160
1098
  }
1161
1099
 
1162
1100
  /**
@@ -1206,6 +1144,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1206
1144
  this.contractDataSource,
1207
1145
  new DateProvider(),
1208
1146
  this.telemetry,
1147
+ this.log.getBindings(),
1209
1148
  );
1210
1149
 
1211
1150
  this.log.verbose(`Simulating public calls for tx ${txHash}`, {
@@ -1259,16 +1198,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1259
1198
  // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1260
1199
  const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1261
1200
  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
- });
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
+ );
1272
1217
 
1273
1218
  return await validator.validateTx(tx);
1274
1219
  }
@@ -1432,16 +1377,19 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1432
1377
  }
1433
1378
  }
1434
1379
 
1380
+ #getInitialHeaderHash(): Promise<BlockHash> {
1381
+ if (!this.initialHeaderHashPromise) {
1382
+ this.initialHeaderHashPromise = this.worldStateSynchronizer.getCommitted().getInitialHeader().hash();
1383
+ }
1384
+ return this.initialHeaderHashPromise;
1385
+ }
1386
+
1435
1387
  /**
1436
1388
  * 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.
1389
+ * @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
1438
1390
  * @returns An instance of a committed MerkleTreeOperations
1439
1391
  */
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
-
1392
+ async #getWorldState(block: BlockParameter) {
1445
1393
  let blockSyncedTo: BlockNumber = BlockNumber.ZERO;
1446
1394
  try {
1447
1395
  // Attempt to sync the world state if necessary
@@ -1450,15 +1398,39 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1450
1398
  this.log.error(`Error getting world state: ${err}`);
1451
1399
  }
1452
1400
 
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}`);
1401
+ if (block === 'latest') {
1402
+ this.log.debug(`Using committed db for block 'latest', world state synced upto ${blockSyncedTo}`);
1456
1403
  return this.worldStateSynchronizer.getCommitted();
1457
- } 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();
1458
1420
  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`);
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);
1462
1434
  }
1463
1435
  }
1464
1436