@aztec/aztec-node 0.0.1-commit.aada20e3 → 0.0.1-commit.b2a5d0dd1

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.
@@ -372,27 +372,32 @@ function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
372
372
  }
373
373
  var _dec, _initProto;
374
374
  import { createArchiver } from '@aztec/archiver';
375
- import { BBCircuitVerifier, QueuedIVCVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
375
+ import { BBCircuitVerifier, BatchChonkVerifier, QueuedIVCVerifier } from '@aztec/bb-prover';
376
+ import { TestCircuitVerifier } from '@aztec/bb-prover/test';
376
377
  import { createBlobClientWithFileStores } from '@aztec/blob-client/client';
378
+ import { Blob } from '@aztec/blob-lib';
377
379
  import { EpochCache } from '@aztec/epoch-cache';
378
380
  import { createEthereumChain } from '@aztec/ethereum/chain';
379
- import { getPublicClient } from '@aztec/ethereum/client';
381
+ import { getPublicClient, makeL1HttpTransport } from '@aztec/ethereum/client';
380
382
  import { RegistryContract, RollupContract } from '@aztec/ethereum/contracts';
381
- import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
382
- import { compactArray, pick } from '@aztec/foundation/collection';
383
+ import { BlockNumber } from '@aztec/foundation/branded-types';
384
+ import { chunkBy, compactArray, pick, unique } from '@aztec/foundation/collection';
383
385
  import { Fr } from '@aztec/foundation/curves/bn254';
384
386
  import { EthAddress } from '@aztec/foundation/eth-address';
385
387
  import { BadRequestError } from '@aztec/foundation/json-rpc';
386
388
  import { createLogger } from '@aztec/foundation/log';
389
+ import { retryUntil } from '@aztec/foundation/retry';
387
390
  import { count } from '@aztec/foundation/string';
388
391
  import { DateProvider, Timer } from '@aztec/foundation/timer';
389
392
  import { MembershipWitness } from '@aztec/foundation/trees';
390
393
  import { KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
391
394
  import { trySnapshotSync, uploadSnapshot } from '@aztec/node-lib/actions';
392
- import { createForwarderL1TxUtilsFromEthSigner, createL1TxUtilsWithBlobsFromEthSigner } from '@aztec/node-lib/factories';
393
- import { createP2PClient, getDefaultAllowedSetupFunctions } from '@aztec/p2p';
395
+ import { createForwarderL1TxUtilsFromSigners, createL1TxUtilsFromSigners } from '@aztec/node-lib/factories';
396
+ import { createP2PClient, createTxValidatorForAcceptingTxsOverRPC, getDefaultAllowedSetupFunctions } from '@aztec/p2p';
394
397
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
395
- import { GlobalVariableBuilder, SequencerClient } from '@aztec/sequencer-client';
398
+ import { createProverNode } from '@aztec/prover-node';
399
+ import { createKeyStoreForProver } from '@aztec/prover-node/config';
400
+ import { FeeProviderImpl, GlobalVariableBuilder, SequencerClient } from '@aztec/sequencer-client';
396
401
  import { PublicProcessorFactory } from '@aztec/simulator/server';
397
402
  import { AttestationsBlockWatcher, EpochPruneWatcher, createSlasher } from '@aztec/slasher';
398
403
  import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
@@ -402,15 +407,15 @@ import { GasFees } from '@aztec/stdlib/gas';
402
407
  import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
403
408
  import { AztecNodeAdminConfigSchema } from '@aztec/stdlib/interfaces/client';
404
409
  import { tryStop } from '@aztec/stdlib/interfaces/server';
410
+ import { InMemoryDebugLogStore, NullDebugLogStore } from '@aztec/stdlib/logs';
405
411
  import { InboxLeaf } from '@aztec/stdlib/messaging';
406
- import { P2PClientType } from '@aztec/stdlib/p2p';
407
412
  import { MerkleTreeId, NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
408
413
  import { PublicSimulationOutput, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
409
414
  import { getPackageVersion } from '@aztec/stdlib/update-checker';
410
415
  import { Attributes, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
411
- import { FullNodeCheckpointsBuilder as CheckpointsBuilder, FullNodeCheckpointsBuilder, NodeKeystoreAdapter, ValidatorClient, createBlockProposalHandler, createValidatorClient, createValidatorForAcceptingTxs } from '@aztec/validator-client';
416
+ import { FullNodeCheckpointsBuilder as CheckpointsBuilder, FullNodeCheckpointsBuilder, NodeKeystoreAdapter, ValidatorClient, createProposalHandler, createValidatorClient } from '@aztec/validator-client';
412
417
  import { createWorldStateSynchronizer } from '@aztec/world-state';
413
- import { createPublicClient, fallback, http } from 'viem';
418
+ import { createPublicClient } from 'viem';
414
419
  import { createSentinel } from '../sentinel/factory.js';
415
420
  import { createKeyStoreForValidator } from './config.js';
416
421
  import { NodeMetrics } from './node_metrics.js';
@@ -428,18 +433,24 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
428
433
  l1ToL2MessageSource;
429
434
  worldStateSynchronizer;
430
435
  sequencer;
436
+ proverNode;
431
437
  slasherClient;
432
438
  validatorsSentinel;
433
439
  epochPruneWatcher;
434
440
  l1ChainId;
435
441
  version;
436
442
  globalVariableBuilder;
443
+ feeProvider;
437
444
  epochCache;
438
445
  packageVersion;
439
- proofVerifier;
446
+ peerProofVerifier;
447
+ rpcProofVerifier;
440
448
  telemetry;
441
449
  log;
442
450
  blobClient;
451
+ validatorClient;
452
+ keyStoreManager;
453
+ debugLogStore;
443
454
  static{
444
455
  ({ e: [_initProto] } = _apply_decs_2203_r(this, [
445
456
  [
@@ -454,7 +465,7 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
454
465
  // Prevent two snapshot operations to happen simultaneously
455
466
  isUploadingSnapshot;
456
467
  tracer;
457
- constructor(config, p2pClient, blockSource, logsSource, contractDataSource, l1ToL2MessageSource, worldStateSynchronizer, sequencer, slasherClient, validatorsSentinel, epochPruneWatcher, l1ChainId, version, globalVariableBuilder, epochCache, packageVersion, proofVerifier, telemetry = getTelemetryClient(), log = createLogger('node'), blobClient){
468
+ constructor(config, p2pClient, blockSource, logsSource, contractDataSource, l1ToL2MessageSource, worldStateSynchronizer, sequencer, proverNode, slasherClient, validatorsSentinel, epochPruneWatcher, l1ChainId, version, globalVariableBuilder, feeProvider, epochCache, packageVersion, peerProofVerifier, rpcProofVerifier, telemetry = getTelemetryClient(), log = createLogger('node'), blobClient, validatorClient, keyStoreManager, debugLogStore = new NullDebugLogStore()){
458
469
  this.config = config;
459
470
  this.p2pClient = p2pClient;
460
471
  this.blockSource = blockSource;
@@ -463,24 +474,39 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
463
474
  this.l1ToL2MessageSource = l1ToL2MessageSource;
464
475
  this.worldStateSynchronizer = worldStateSynchronizer;
465
476
  this.sequencer = sequencer;
477
+ this.proverNode = proverNode;
466
478
  this.slasherClient = slasherClient;
467
479
  this.validatorsSentinel = validatorsSentinel;
468
480
  this.epochPruneWatcher = epochPruneWatcher;
469
481
  this.l1ChainId = l1ChainId;
470
482
  this.version = version;
471
483
  this.globalVariableBuilder = globalVariableBuilder;
484
+ this.feeProvider = feeProvider;
472
485
  this.epochCache = epochCache;
473
486
  this.packageVersion = packageVersion;
474
- this.proofVerifier = proofVerifier;
487
+ this.peerProofVerifier = peerProofVerifier;
488
+ this.rpcProofVerifier = rpcProofVerifier;
475
489
  this.telemetry = telemetry;
476
490
  this.log = log;
477
491
  this.blobClient = blobClient;
492
+ this.validatorClient = validatorClient;
493
+ this.keyStoreManager = keyStoreManager;
494
+ this.debugLogStore = debugLogStore;
478
495
  this.initialHeaderHashPromise = (_initProto(this), undefined);
479
496
  this.isUploadingSnapshot = false;
480
497
  this.metrics = new NodeMetrics(telemetry, 'AztecNodeService');
481
498
  this.tracer = telemetry.getTracer('AztecNodeService');
482
499
  this.log.info(`Aztec Node version: ${this.packageVersion}`);
483
500
  this.log.info(`Aztec Node started on chain 0x${l1ChainId.toString(16)}`, config.l1Contracts);
501
+ // A defensive check that protects us against introducing a bug in the complex `createAndSync` function. We must
502
+ // never have debugLogStore enabled when not in test mode because then we would be accumulating debug logs in
503
+ // memory which could be a DoS vector on the sequencer (since no fees are paid for debug logs).
504
+ if (debugLogStore.isEnabled && config.realProofs) {
505
+ throw new Error('debugLogStore should never be enabled when realProofs are set');
506
+ }
507
+ }
508
+ /** @internal Exposed for testing — returns the RPC proof verifier. */ getProofVerifier() {
509
+ return this.rpcProofVerifier;
484
510
  }
485
511
  async getWorldStateSyncStatus() {
486
512
  const status = await this.worldStateSynchronizer.status();
@@ -502,16 +528,27 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
502
528
  const telemetry = deps.telemetry ?? getTelemetryClient();
503
529
  const dateProvider = deps.dateProvider ?? new DateProvider();
504
530
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
505
- // Build a key store from file if given or from environment otherwise
531
+ // Build a key store from file if given or from environment otherwise.
532
+ // We keep the raw KeyStore available so we can merge with prover keys if enableProverNode is set.
506
533
  let keyStoreManager;
507
534
  const keyStoreProvided = config.keyStoreDirectory !== undefined && config.keyStoreDirectory.length > 0;
508
535
  if (keyStoreProvided) {
509
536
  const keyStores = loadKeystores(config.keyStoreDirectory);
510
537
  keyStoreManager = new KeystoreManager(mergeKeystores(keyStores));
511
538
  } else {
512
- const keyStore = createKeyStoreForValidator(config);
513
- if (keyStore) {
514
- keyStoreManager = new KeystoreManager(keyStore);
539
+ const rawKeyStores = [];
540
+ const validatorKeyStore = createKeyStoreForValidator(config);
541
+ if (validatorKeyStore) {
542
+ rawKeyStores.push(validatorKeyStore);
543
+ }
544
+ if (config.enableProverNode) {
545
+ const proverKeyStore = createKeyStoreForProver(config);
546
+ if (proverKeyStore) {
547
+ rawKeyStores.push(proverKeyStore);
548
+ }
549
+ }
550
+ if (rawKeyStores.length > 0) {
551
+ keyStoreManager = new KeystoreManager(rawKeyStores.length === 1 ? rawKeyStores[0] : mergeKeystores(rawKeyStores));
515
552
  }
516
553
  }
517
554
  await keyStoreManager?.validateSigners();
@@ -520,8 +557,8 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
520
557
  if (keyStoreManager === undefined) {
521
558
  throw new Error('Failed to create key store, a requirement for running a validator');
522
559
  }
523
- if (!keyStoreProvided) {
524
- log.warn('KEY STORE CREATED FROM ENVIRONMENT, IT IS RECOMMENDED TO USE A FILE-BASED KEY STORE IN PRODUCTION ENVIRONMENTS');
560
+ if (!keyStoreProvided && process.env.NODE_ENV !== 'test') {
561
+ log.warn("Keystore created from env: it's recommended to use a file-based key store for production");
525
562
  }
526
563
  ValidatorClient.validateKeyStoreConfiguration(keyStoreManager, log);
527
564
  }
@@ -531,9 +568,9 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
531
568
  }
532
569
  const publicClient = createPublicClient({
533
570
  chain: ethereumChain.chainInfo,
534
- transport: fallback(config.l1RpcUrls.map((url)=>http(url, {
535
- batch: false
536
- }))),
571
+ transport: makeL1HttpTransport(config.l1RpcUrls, {
572
+ timeout: config.l1HttpTimeoutMS
573
+ }),
537
574
  pollingInterval: config.viemPollingIntervalMS
538
575
  });
539
576
  const l1ContractsAddresses = await RegistryContract.collectAddresses(publicClient, config.l1Contracts.registryAddress, config.rollupVersion ?? 'canonical');
@@ -543,172 +580,268 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
543
580
  ...l1ContractsAddresses
544
581
  };
545
582
  const rollupContract = new RollupContract(publicClient, config.l1Contracts.rollupAddress.toString());
546
- const [l1GenesisTime, slotDuration, rollupVersionFromRollup] = await Promise.all([
583
+ const [l1GenesisTime, slotDuration, rollupVersionFromRollup, rollupManaLimit] = await Promise.all([
547
584
  rollupContract.getL1GenesisTime(),
548
585
  rollupContract.getSlotDuration(),
549
- rollupContract.getVersion()
586
+ rollupContract.getVersion(),
587
+ rollupContract.getManaLimit().then(Number)
550
588
  ]);
551
589
  config.rollupVersion ??= Number(rollupVersionFromRollup);
552
590
  if (config.rollupVersion !== Number(rollupVersionFromRollup)) {
553
591
  log.warn(`Registry looked up and returned a rollup with version (${config.rollupVersion}), but this does not match with version detected from the rollup directly: (${rollupVersionFromRollup}).`);
554
592
  }
555
- const blobClient = await createBlobClientWithFileStores(config, createLogger('node:blob-client:client'));
593
+ const blobClient = await createBlobClientWithFileStores(config, log.createChild('blob-client'));
556
594
  // attempt snapshot sync if possible
557
595
  await trySnapshotSync(config, log);
558
596
  const epochCache = await EpochCache.create(config.l1Contracts.rollupAddress, config, {
559
597
  dateProvider
560
598
  });
561
- const archiver = await createArchiver(config, {
562
- blobClient,
563
- epochCache,
564
- telemetry,
565
- dateProvider
566
- }, {
567
- blockUntilSync: !config.skipArchiverInitialSync
568
- });
569
- // now create the merkle trees and the world state synchronizer
570
- const worldStateSynchronizer = await createWorldStateSynchronizer(config, archiver, options.prefilledPublicData, telemetry);
571
- const circuitVerifier = config.realProofs || config.debugForceTxProofVerification ? await BBCircuitVerifier.new(config) : new TestCircuitVerifier(config.proverTestVerificationDelayMs);
572
- if (!config.realProofs) {
573
- log.warn(`Aztec node is accepting fake proofs`);
574
- }
575
- const proofVerifier = new QueuedIVCVerifier(config, circuitVerifier);
576
- // create the tx pool and the p2p client, which will need the l2 block source
577
- const p2pClient = await createP2PClient(P2PClientType.Full, config, archiver, proofVerifier, worldStateSynchronizer, epochCache, packageVersion, dateProvider, telemetry, deps.p2pClientDeps);
578
- // We should really not be modifying the config object
579
- config.txPublicSetupAllowList = config.txPublicSetupAllowList ?? await getDefaultAllowedSetupFunctions();
580
- // Create FullNodeCheckpointsBuilder for validator and non-validator block proposal handling
581
- const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder({
582
- ...config,
583
- l1GenesisTime,
584
- slotDuration: Number(slotDuration)
585
- }, worldStateSynchronizer, archiver, dateProvider, telemetry);
586
- // We'll accumulate sentinel watchers here
587
- const watchers = [];
588
- // Create validator client if required
589
- const validatorClient = await createValidatorClient(config, {
590
- checkpointsBuilder: validatorCheckpointsBuilder,
591
- worldState: worldStateSynchronizer,
592
- p2pClient,
593
- telemetry,
594
- dateProvider,
595
- epochCache,
596
- blockSource: archiver,
597
- l1ToL2MessageSource: archiver,
598
- keyStoreManager,
599
- blobClient
600
- });
601
- // If we have a validator client, register it as a source of offenses for the slasher,
602
- // and have it register callbacks on the p2p client *before* we start it, otherwise messages
603
- // like attestations or auths will fail.
604
- if (validatorClient) {
605
- watchers.push(validatorClient);
606
- if (!options.dontStartSequencer) {
607
- await validatorClient.registerHandlers();
608
- }
609
- }
610
- // If there's no validator client but alwaysReexecuteBlockProposals is enabled,
611
- // create a BlockProposalHandler to reexecute block proposals for monitoring
612
- if (!validatorClient && config.alwaysReexecuteBlockProposals) {
613
- log.info('Setting up block proposal reexecution for monitoring');
614
- createBlockProposalHandler(config, {
615
- checkpointsBuilder: validatorCheckpointsBuilder,
616
- worldState: worldStateSynchronizer,
599
+ // Track started resources so we can clean up on partial failure during node creation.
600
+ const started = [];
601
+ try {
602
+ const archiver = await createArchiver(config, {
603
+ blobClient,
617
604
  epochCache,
618
- blockSource: archiver,
619
- l1ToL2MessageSource: archiver,
620
- p2pClient,
621
- dateProvider,
622
- telemetry
623
- }).registerForReexecution(p2pClient);
624
- }
625
- // Start world state and wait for it to sync to the archiver.
626
- await worldStateSynchronizer.start();
627
- // Start p2p. Note that it depends on world state to be running.
628
- await p2pClient.start();
629
- const validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
630
- if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
631
- watchers.push(validatorsSentinel);
632
- }
633
- let epochPruneWatcher;
634
- if (config.slashPrunePenalty > 0n || config.slashDataWithholdingPenalty > 0n) {
635
- epochPruneWatcher = new EpochPruneWatcher(archiver, archiver, epochCache, p2pClient.getTxProvider(), validatorCheckpointsBuilder, config);
636
- watchers.push(epochPruneWatcher);
637
- }
638
- // We assume we want to slash for invalid attestations unless all max penalties are set to 0
639
- let attestationsBlockWatcher;
640
- if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
641
- attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
642
- watchers.push(attestationsBlockWatcher);
643
- }
644
- // Start p2p-related services once the archiver has completed sync
645
- void archiver.waitForInitialSync().then(async ()=>{
646
- await p2pClient.start();
647
- await validatorsSentinel?.start();
648
- await epochPruneWatcher?.start();
649
- await attestationsBlockWatcher?.start();
650
- log.info(`All p2p services started`);
651
- }).catch((err)=>log.error('Failed to start p2p services after archiver sync', err));
652
- // Validator enabled, create/start relevant service
653
- let sequencer;
654
- let slasherClient;
655
- if (!config.disableValidator && validatorClient) {
656
- // We create a slasher only if we have a sequencer, since all slashing actions go through the sequencer publisher
657
- // as they are executed when the node is selected as proposer.
658
- const validatorAddresses = keyStoreManager ? NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager).getAddresses() : [];
659
- slasherClient = await createSlasher(config, config.l1Contracts, getPublicClient(config), watchers, dateProvider, epochCache, validatorAddresses, undefined);
660
- await slasherClient.start();
661
- const l1TxUtils = config.publisherForwarderAddress ? await createForwarderL1TxUtilsFromEthSigner(publicClient, keyStoreManager.createAllValidatorPublisherSigners(), config.publisherForwarderAddress, {
662
- ...config,
663
- scope: 'sequencer'
664
- }, {
665
605
  telemetry,
666
- logger: log.createChild('l1-tx-utils'),
667
606
  dateProvider
668
- }) : await createL1TxUtilsWithBlobsFromEthSigner(publicClient, keyStoreManager.createAllValidatorPublisherSigners(), {
669
- ...config,
670
- scope: 'sequencer'
671
607
  }, {
672
- telemetry,
673
- logger: log.createChild('l1-tx-utils'),
674
- dateProvider
608
+ blockUntilSync: !config.skipArchiverInitialSync
675
609
  });
676
- // Create and start the sequencer client
677
- const checkpointsBuilder = new CheckpointsBuilder({
678
- ...config,
610
+ started.push(archiver);
611
+ // now create the merkle trees and the world state synchronizer
612
+ const worldStateSynchronizer = await createWorldStateSynchronizer(config, archiver, options.genesis, telemetry);
613
+ started.push(worldStateSynchronizer);
614
+ const useRealVerifiers = config.realProofs || config.debugForceTxProofVerification;
615
+ let peerProofVerifier;
616
+ let rpcProofVerifier;
617
+ if (useRealVerifiers) {
618
+ peerProofVerifier = await BatchChonkVerifier.new(config, config.bbChonkVerifyMaxBatch, 'peer');
619
+ const rpcVerifier = await BBCircuitVerifier.new(config);
620
+ rpcProofVerifier = new QueuedIVCVerifier(rpcVerifier, config.numConcurrentIVCVerifiers);
621
+ } else {
622
+ peerProofVerifier = new TestCircuitVerifier(config.proverTestVerificationDelayMs);
623
+ rpcProofVerifier = new TestCircuitVerifier(config.proverTestVerificationDelayMs);
624
+ }
625
+ started.push(peerProofVerifier, rpcProofVerifier);
626
+ let debugLogStore;
627
+ if (!config.realProofs) {
628
+ log.warn(`Aztec node is accepting fake proofs`);
629
+ debugLogStore = new InMemoryDebugLogStore();
630
+ log.info('Aztec node started in test mode (realProofs set to false) hence debug logs from public functions will be collected and served');
631
+ } else {
632
+ debugLogStore = new NullDebugLogStore();
633
+ }
634
+ const globalVariableBuilderConfig = {
635
+ l1Contracts: config.l1Contracts,
636
+ ethereumSlotDuration: config.ethereumSlotDuration,
637
+ rollupVersion: BigInt(config.rollupVersion),
679
638
  l1GenesisTime,
680
639
  slotDuration: Number(slotDuration)
640
+ };
641
+ const globalVariableBuilder = new GlobalVariableBuilder(dateProvider, publicClient, globalVariableBuilderConfig);
642
+ const feeProvider = new FeeProviderImpl(dateProvider, publicClient, globalVariableBuilderConfig);
643
+ const proverOnly = config.enableProverNode && config.disableValidator;
644
+ if (proverOnly) {
645
+ log.info('Starting in prover-only mode: skipping validator, sequencer, sentinel, and slasher subsystems');
646
+ }
647
+ // create the tx pool and the p2p client, which will need the l2 block source
648
+ const p2pClient = await createP2PClient(config, archiver, peerProofVerifier, worldStateSynchronizer, epochCache, feeProvider, packageVersion, dateProvider, telemetry, deps.p2pClientDeps);
649
+ started.push(p2pClient);
650
+ // We'll accumulate sentinel watchers here
651
+ const watchers = [];
652
+ // Create FullNodeCheckpointsBuilder for block proposal handling and tx validation.
653
+ // Override maxTxsPerCheckpoint with the validator-specific limit if set.
654
+ const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder({
655
+ ...config,
656
+ l1GenesisTime,
657
+ slotDuration: Number(slotDuration),
658
+ rollupManaLimit,
659
+ maxTxsPerCheckpoint: config.validateMaxTxsPerCheckpoint
681
660
  }, worldStateSynchronizer, archiver, dateProvider, telemetry);
682
- sequencer = await SequencerClient.new(config, {
683
- ...deps,
684
- epochCache,
685
- l1TxUtils,
686
- validatorClient,
687
- p2pClient,
688
- worldStateSynchronizer,
689
- slasherClient,
690
- checkpointsBuilder,
691
- l2BlockSource: archiver,
692
- l1ToL2MessageSource: archiver,
693
- telemetry,
694
- dateProvider,
695
- blobClient,
696
- nodeKeyStore: keyStoreManager
697
- });
661
+ let validatorClient;
662
+ if (!proverOnly) {
663
+ // Create validator client if required
664
+ validatorClient = await createValidatorClient(config, {
665
+ checkpointsBuilder: validatorCheckpointsBuilder,
666
+ worldState: worldStateSynchronizer,
667
+ p2pClient,
668
+ telemetry,
669
+ dateProvider,
670
+ epochCache,
671
+ blockSource: archiver,
672
+ l1ToL2MessageSource: archiver,
673
+ keyStoreManager,
674
+ blobClient,
675
+ slashingProtectionDb: deps.slashingProtectionDb
676
+ });
677
+ // If we have a validator client, register it as a source of offenses for the slasher,
678
+ // and have it register callbacks on the p2p client *before* we start it, otherwise messages
679
+ // like attestations or auths will fail.
680
+ if (validatorClient) {
681
+ watchers.push(validatorClient);
682
+ const vc = validatorClient;
683
+ const getValidatorAddresses = ()=>vc.getValidatorAddresses().map((a)=>a.toString());
684
+ validatorClient.getProposalHandler().register(p2pClient, true, archiver, getValidatorAddresses);
685
+ if (!options.dontStartSequencer) {
686
+ await validatorClient.registerHandlers();
687
+ }
688
+ }
689
+ }
690
+ // If there's no validator client, create a ProposalHandler to handle block and checkpoint proposals
691
+ // for monitoring or reexecution. Reexecution (default) allows us to follow the pending chain,
692
+ // while non-reexecution is used for validating the proposals and collecting their txs.
693
+ // Checkpoint proposals rebuild blobs if the blob client can upload blobs.
694
+ if (!validatorClient) {
695
+ const reexecute = !!config.alwaysReexecuteBlockProposals;
696
+ log.info(`Setting up proposal handler` + (reexecute ? ' with reexecution of proposals' : ''));
697
+ createProposalHandler(config, {
698
+ checkpointsBuilder: validatorCheckpointsBuilder,
699
+ worldState: worldStateSynchronizer,
700
+ epochCache,
701
+ blockSource: archiver,
702
+ l1ToL2MessageSource: archiver,
703
+ p2pClient,
704
+ blobClient,
705
+ dateProvider,
706
+ telemetry
707
+ }).register(p2pClient, reexecute, archiver);
708
+ }
709
+ // Start world state and wait for it to sync to the archiver.
710
+ await worldStateSynchronizer.start();
711
+ // Start p2p. Note that it depends on world state to be running.
712
+ await p2pClient.start();
713
+ let validatorsSentinel;
714
+ let epochPruneWatcher;
715
+ let attestationsBlockWatcher;
716
+ if (!proverOnly) {
717
+ validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
718
+ if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
719
+ watchers.push(validatorsSentinel);
720
+ }
721
+ if (config.slashPrunePenalty > 0n || config.slashDataWithholdingPenalty > 0n) {
722
+ epochPruneWatcher = new EpochPruneWatcher(archiver, archiver, epochCache, p2pClient.getTxProvider(), validatorCheckpointsBuilder, config);
723
+ watchers.push(epochPruneWatcher);
724
+ }
725
+ // We assume we want to slash for invalid attestations unless all max penalties are set to 0
726
+ if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
727
+ attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
728
+ watchers.push(attestationsBlockWatcher);
729
+ }
730
+ }
731
+ // Start p2p-related services once the archiver has completed sync
732
+ void archiver.waitForInitialSync().then(async ()=>{
733
+ await validatorsSentinel?.start();
734
+ await epochPruneWatcher?.start();
735
+ await attestationsBlockWatcher?.start();
736
+ log.info(`All p2p services started`);
737
+ }).catch((err)=>log.error('Failed to start p2p services after archiver sync', err));
738
+ // Validator enabled, create/start relevant service
739
+ let sequencer;
740
+ let slasherClient;
741
+ if (!config.disableValidator && validatorClient) {
742
+ // We create a slasher only if we have a sequencer, since all slashing actions go through the sequencer publisher
743
+ // as they are executed when the node is selected as proposer.
744
+ const validatorAddresses = keyStoreManager ? NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager).getAddresses() : [];
745
+ slasherClient = await createSlasher(config, config.l1Contracts, getPublicClient(config), watchers, dateProvider, epochCache, validatorAddresses, undefined);
746
+ await slasherClient.start();
747
+ started.push(slasherClient);
748
+ const l1TxUtils = config.sequencerPublisherForwarderAddress ? await createForwarderL1TxUtilsFromSigners(publicClient, keyStoreManager.createAllValidatorPublisherSigners(), config.sequencerPublisherForwarderAddress, {
749
+ ...config,
750
+ scope: 'sequencer'
751
+ }, {
752
+ telemetry,
753
+ logger: log.createChild('l1-tx-utils'),
754
+ dateProvider,
755
+ kzg: Blob.getViemKzgInstance()
756
+ }) : await createL1TxUtilsFromSigners(publicClient, keyStoreManager.createAllValidatorPublisherSigners(), {
757
+ ...config,
758
+ scope: 'sequencer'
759
+ }, {
760
+ telemetry,
761
+ logger: log.createChild('l1-tx-utils'),
762
+ dateProvider,
763
+ kzg: Blob.getViemKzgInstance()
764
+ });
765
+ // Create a funder L1TxUtils from the keystore funding account (if configured)
766
+ const fundingSigner = keyStoreManager?.createFundingSigner();
767
+ let funderL1TxUtils;
768
+ if (fundingSigner) {
769
+ const [funder] = await createL1TxUtilsFromSigners(publicClient, [
770
+ fundingSigner
771
+ ], {
772
+ ...config,
773
+ scope: 'sequencer'
774
+ }, {
775
+ telemetry,
776
+ logger: log.createChild('l1-tx-utils:funder'),
777
+ dateProvider
778
+ });
779
+ funderL1TxUtils = funder;
780
+ }
781
+ // Create and start the sequencer client
782
+ const checkpointsBuilder = new CheckpointsBuilder({
783
+ ...config,
784
+ l1GenesisTime,
785
+ slotDuration: Number(slotDuration),
786
+ rollupManaLimit
787
+ }, worldStateSynchronizer, archiver, dateProvider, telemetry, debugLogStore);
788
+ sequencer = await SequencerClient.new(config, {
789
+ ...deps,
790
+ epochCache,
791
+ l1TxUtils,
792
+ funderL1TxUtils,
793
+ validatorClient,
794
+ p2pClient,
795
+ worldStateSynchronizer,
796
+ slasherClient,
797
+ checkpointsBuilder,
798
+ l2BlockSource: archiver,
799
+ l1ToL2MessageSource: archiver,
800
+ telemetry,
801
+ dateProvider,
802
+ blobClient,
803
+ nodeKeyStore: keyStoreManager,
804
+ globalVariableBuilder
805
+ });
806
+ }
807
+ if (!options.dontStartSequencer && sequencer) {
808
+ await sequencer.start();
809
+ started.push(sequencer);
810
+ log.verbose(`Sequencer started`);
811
+ } else if (sequencer) {
812
+ log.warn(`Sequencer created but not started`);
813
+ }
814
+ // Create prover node subsystem if enabled
815
+ let proverNode;
816
+ if (config.enableProverNode) {
817
+ proverNode = await createProverNode(config, {
818
+ ...deps.proverNodeDeps,
819
+ telemetry,
820
+ dateProvider,
821
+ archiver,
822
+ worldStateSynchronizer,
823
+ p2pClient,
824
+ epochCache,
825
+ blobClient,
826
+ keyStoreManager
827
+ });
828
+ if (!options.dontStartProverNode) {
829
+ await proverNode.start();
830
+ started.push(proverNode);
831
+ log.info(`Prover node subsystem started`);
832
+ } else {
833
+ log.info(`Prover node subsystem created but not started`);
834
+ }
835
+ }
836
+ const node = new AztecNodeService(config, p2pClient, archiver, archiver, archiver, archiver, worldStateSynchronizer, sequencer, proverNode, slasherClient, validatorsSentinel, epochPruneWatcher, ethereumChain.chainInfo.id, config.rollupVersion, globalVariableBuilder, feeProvider, epochCache, packageVersion, peerProofVerifier, rpcProofVerifier, telemetry, log, blobClient, validatorClient, keyStoreManager, debugLogStore);
837
+ return node;
838
+ } catch (err) {
839
+ log.error('Failed during node creation, stopping started resources', err);
840
+ for (const resource of started.reverse()){
841
+ await tryStop(resource);
842
+ }
843
+ throw err;
698
844
  }
699
- if (!options.dontStartSequencer && sequencer) {
700
- await sequencer.start();
701
- log.verbose(`Sequencer started`);
702
- } else if (sequencer) {
703
- log.warn(`Sequencer created but not started`);
704
- }
705
- const globalVariableBuilder = new GlobalVariableBuilder({
706
- ...config,
707
- rollupVersion: BigInt(config.rollupVersion),
708
- l1GenesisTime,
709
- slotDuration: Number(slotDuration)
710
- });
711
- return new AztecNodeService(config, p2pClient, archiver, archiver, archiver, archiver, worldStateSynchronizer, sequencer, slasherClient, validatorsSentinel, epochPruneWatcher, ethereumChain.chainInfo.id, config.rollupVersion, globalVariableBuilder, epochCache, packageVersion, proofVerifier, telemetry, log, blobClient);
712
845
  }
713
846
  /**
714
847
  * Returns the sequencer client instance.
@@ -716,6 +849,9 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
716
849
  */ getSequencer() {
717
850
  return this.sequencer;
718
851
  }
852
+ /** Returns the prover node subsystem, if enabled. */ getProverNode() {
853
+ return this.proverNode;
854
+ }
719
855
  getBlockSource() {
720
856
  return this.blockSource;
721
857
  }
@@ -735,7 +871,10 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
735
871
  return Promise.resolve(this.p2pClient.getEnr()?.encodeTxt());
736
872
  }
737
873
  async getAllowedPublicSetup() {
738
- return this.config.txPublicSetupAllowList ?? await getDefaultAllowedSetupFunctions();
874
+ return [
875
+ ...await getDefaultAllowedSetupFunctions(),
876
+ ...this.config.txPublicSetupAllowListExtend ?? []
877
+ ];
739
878
  }
740
879
  /**
741
880
  * Method to determine if the node is ready to accept transactions.
@@ -758,7 +897,8 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
758
897
  rollupVersion,
759
898
  enr,
760
899
  l1ContractAddresses: contractAddresses,
761
- protocolContractAddresses: protocolContractAddresses
900
+ protocolContractAddresses: protocolContractAddresses,
901
+ realProofs: !!this.config.realProofs
762
902
  };
763
903
  return nodeInfo;
764
904
  }
@@ -812,11 +952,14 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
812
952
  async getCheckpointedBlocks(from, limit) {
813
953
  return await this.blockSource.getCheckpointedBlocks(from, limit) ?? [];
814
954
  }
815
- /**
816
- * Method to fetch the current min L2 fees.
817
- * @returns The current min L2 fees.
818
- */ async getCurrentMinFees() {
819
- return await this.globalVariableBuilder.getCurrentMinFees();
955
+ getCheckpointsDataForEpoch(epochNumber) {
956
+ return this.blockSource.getCheckpointsDataForEpoch(epochNumber);
957
+ }
958
+ async getCurrentMinFees() {
959
+ return await this.feeProvider.getCurrentMinFees();
960
+ }
961
+ /** Returns predicted min fees for the current slot and next N slots. */ async getPredictedMinFees(manaUsage) {
962
+ return await this.feeProvider.getPredictedMinFees(manaUsage);
820
963
  }
821
964
  async getMaxPriorityFees() {
822
965
  for await (const tx of this.p2pClient.iteratePendingTxs()){
@@ -839,6 +982,9 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
839
982
  async getCheckpointedBlockNumber() {
840
983
  return await this.blockSource.getCheckpointedL2BlockNumber();
841
984
  }
985
+ getCheckpointNumber() {
986
+ return this.blockSource.getCheckpointNumber();
987
+ }
842
988
  /**
843
989
  * Method to fetch the version of the package.
844
990
  * @returns The node package version
@@ -864,28 +1010,36 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
864
1010
  return this.contractDataSource.getContract(address);
865
1011
  }
866
1012
  async getPrivateLogsByTags(tags, page, referenceBlock) {
1013
+ let upToBlockNumber;
867
1014
  if (referenceBlock) {
868
1015
  const initialBlockHash = await this.#getInitialHeaderHash();
869
- if (!referenceBlock.equals(initialBlockHash)) {
1016
+ if (referenceBlock.equals(initialBlockHash)) {
1017
+ upToBlockNumber = BlockNumber(0);
1018
+ } else {
870
1019
  const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
871
1020
  if (!header) {
872
1021
  throw new Error(`Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`);
873
1022
  }
1023
+ upToBlockNumber = header.globalVariables.blockNumber;
874
1024
  }
875
1025
  }
876
- return this.logsSource.getPrivateLogsByTags(tags, page);
1026
+ return this.logsSource.getPrivateLogsByTags(tags, page, upToBlockNumber);
877
1027
  }
878
1028
  async getPublicLogsByTagsFromContract(contractAddress, tags, page, referenceBlock) {
1029
+ let upToBlockNumber;
879
1030
  if (referenceBlock) {
880
1031
  const initialBlockHash = await this.#getInitialHeaderHash();
881
- if (!referenceBlock.equals(initialBlockHash)) {
1032
+ if (referenceBlock.equals(initialBlockHash)) {
1033
+ upToBlockNumber = BlockNumber(0);
1034
+ } else {
882
1035
  const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
883
1036
  if (!header) {
884
1037
  throw new Error(`Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`);
885
1038
  }
1039
+ upToBlockNumber = header.globalVariables.blockNumber;
886
1040
  }
887
1041
  }
888
- return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags, page);
1042
+ return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber);
889
1043
  }
890
1044
  /**
891
1045
  * Gets public logs based on the provided filter.
@@ -920,8 +1074,9 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
920
1074
  throw new Error(`Invalid tx: ${reason}`);
921
1075
  }
922
1076
  await this.p2pClient.sendTx(tx);
923
- this.metrics.receivedTx(timer.ms(), true);
924
- this.log.info(`Received tx ${txHash}`, {
1077
+ const duration = timer.ms();
1078
+ this.metrics.receivedTx(duration, true);
1079
+ this.log.info(`Received tx ${txHash} in ${duration}ms`, {
925
1080
  txHash
926
1081
  });
927
1082
  }
@@ -932,18 +1087,20 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
932
1087
  const isKnownToPool = txPoolStatus === 'pending' || txPoolStatus === 'mined';
933
1088
  // Then get the actual tx from the archiver, which tracks every tx in a mined block.
934
1089
  const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
1090
+ let receipt;
935
1091
  if (settledTxReceipt) {
936
- // If the archiver has the receipt then return it.
937
- return settledTxReceipt;
1092
+ receipt = settledTxReceipt;
938
1093
  } else if (isKnownToPool) {
939
1094
  // If the tx is in the pool but not in the archiver, it's pending.
940
1095
  // This handles race conditions between archiver and p2p, where the archiver
941
1096
  // has pruned the block in which a tx was mined, but p2p has not caught up yet.
942
- return new TxReceipt(txHash, TxStatus.PENDING, undefined, undefined);
1097
+ receipt = new TxReceipt(txHash, TxStatus.PENDING, undefined, undefined);
943
1098
  } else {
944
1099
  // Otherwise, if we don't know the tx, we consider it dropped.
945
- return new TxReceipt(txHash, TxStatus.DROPPED, undefined, 'Tx dropped by P2P node');
1100
+ receipt = new TxReceipt(txHash, TxStatus.DROPPED, undefined, 'Tx dropped by P2P node');
946
1101
  }
1102
+ this.debugLogStore.decorateReceiptWithLogs(txHash.toString(), receipt);
1103
+ return receipt;
947
1104
  }
948
1105
  getTxEffect(txHash) {
949
1106
  return this.blockSource.getTxEffect(txHash);
@@ -955,8 +1112,12 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
955
1112
  await tryStop(this.validatorsSentinel);
956
1113
  await tryStop(this.epochPruneWatcher);
957
1114
  await tryStop(this.slasherClient);
958
- await tryStop(this.proofVerifier);
1115
+ await Promise.all([
1116
+ tryStop(this.peerProofVerifier),
1117
+ tryStop(this.rpcProofVerifier)
1118
+ ]);
959
1119
  await tryStop(this.sequencer);
1120
+ await tryStop(this.proverNode);
960
1121
  await tryStop(this.p2pClient);
961
1122
  await tryStop(this.worldStateSynchronizer);
962
1123
  await tryStop(this.blockSource);
@@ -996,70 +1157,82 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
996
1157
  return compactArray(await Promise.all(txHashes.map((txHash)=>this.getTxByHash(txHash))));
997
1158
  }
998
1159
  async findLeavesIndexes(referenceBlock, treeId, leafValues) {
999
- const committedDb = await this.#getWorldState(referenceBlock);
1160
+ const committedDb = await this.getWorldState(referenceBlock);
1000
1161
  const maybeIndices = await committedDb.findLeafIndices(treeId, leafValues.map((x)=>x.toBuffer()));
1001
- // We filter out undefined values
1002
- const indices = maybeIndices.filter((x)=>x !== undefined);
1003
- // Now we find the block numbers for the indices
1004
- const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, indices);
1005
- // If any of the block numbers are undefined, we throw an error.
1006
- for(let i = 0; i < indices.length; i++){
1007
- if (blockNumbers[i] === undefined) {
1008
- throw new Error(`Block number is undefined for leaf index ${indices[i]} in tree ${MerkleTreeId[treeId]}`);
1162
+ // Filter out undefined values to query block numbers only for found leaves
1163
+ const definedIndices = maybeIndices.filter((x)=>x !== undefined);
1164
+ // Now we find the block numbers for the defined indices
1165
+ const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, definedIndices);
1166
+ // Build a map from leaf index to block number
1167
+ const indexToBlockNumber = new Map();
1168
+ for(let i = 0; i < definedIndices.length; i++){
1169
+ const blockNumber = blockNumbers[i];
1170
+ if (blockNumber === undefined) {
1171
+ throw new Error(`Block number is undefined for leaf index ${definedIndices[i]} in tree ${MerkleTreeId[treeId]}`);
1009
1172
  }
1173
+ indexToBlockNumber.set(definedIndices[i], blockNumber);
1010
1174
  }
1011
1175
  // Get unique block numbers in order to optimize num calls to getLeafValue function.
1012
1176
  const uniqueBlockNumbers = [
1013
- ...new Set(blockNumbers.filter((x)=>x !== undefined))
1177
+ ...new Set(indexToBlockNumber.values())
1014
1178
  ];
1015
- // Now we obtain the block hashes from the archive tree by calling await `committedDb.getLeafValue(treeId, index)`
1016
- // (note that block number corresponds to the leaf index in the archive tree).
1179
+ // Now we obtain the block hashes from the archive tree (block number = leaf index in archive tree).
1017
1180
  const blockHashes = await Promise.all(uniqueBlockNumbers.map((blockNumber)=>{
1018
1181
  return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
1019
1182
  }));
1020
- // If any of the block hashes are undefined, we throw an error.
1183
+ // Build a map from block number to block hash
1184
+ const blockNumberToHash = new Map();
1021
1185
  for(let i = 0; i < uniqueBlockNumbers.length; i++){
1022
- if (blockHashes[i] === undefined) {
1186
+ const blockHash = blockHashes[i];
1187
+ if (blockHash === undefined) {
1023
1188
  throw new Error(`Block hash is undefined for block number ${uniqueBlockNumbers[i]}`);
1024
1189
  }
1190
+ blockNumberToHash.set(uniqueBlockNumbers[i], blockHash);
1025
1191
  }
1026
1192
  // Create DataInBlock objects by combining indices, blockNumbers and blockHashes and return them.
1027
- return maybeIndices.map((index, i)=>{
1193
+ return maybeIndices.map((index)=>{
1028
1194
  if (index === undefined) {
1029
1195
  return undefined;
1030
1196
  }
1031
- const blockNumber = blockNumbers[i];
1197
+ const blockNumber = indexToBlockNumber.get(index);
1032
1198
  if (blockNumber === undefined) {
1033
- return undefined;
1199
+ throw new Error(`Block number not found for leaf index ${index} in tree ${MerkleTreeId[treeId]}`);
1034
1200
  }
1035
- const blockHashIndex = uniqueBlockNumbers.indexOf(blockNumber);
1036
- const blockHash = blockHashes[blockHashIndex];
1037
- if (!blockHash) {
1038
- return undefined;
1201
+ const l2BlockHash = blockNumberToHash.get(blockNumber);
1202
+ if (l2BlockHash === undefined) {
1203
+ throw new Error(`Block hash not found for block number ${blockNumber}`);
1039
1204
  }
1040
1205
  return {
1041
- l2BlockNumber: BlockNumber(Number(blockNumber)),
1042
- l2BlockHash: new BlockHash(blockHash),
1206
+ l2BlockNumber: blockNumber,
1207
+ l2BlockHash,
1043
1208
  data: index
1044
1209
  };
1045
1210
  });
1046
1211
  }
1047
1212
  async getBlockHashMembershipWitness(referenceBlock, blockHash) {
1048
- const committedDb = await this.#getWorldState(referenceBlock);
1213
+ // The Noir circuit checks the archive membership proof against `anchor_block_header.last_archive.root`,
1214
+ // which is the archive tree root BEFORE the anchor block was added (i.e. the state after block N-1).
1215
+ // So we need the world state at block N-1, not block N, to produce a sibling path matching that root.
1216
+ const referenceBlockNumber = await this.resolveBlockNumber(referenceBlock);
1217
+ if (referenceBlockNumber === BlockNumber.ZERO) {
1218
+ // Block 0 (the initial block) has an empty archive, so no membership witness can exist.
1219
+ return undefined;
1220
+ }
1221
+ const committedDb = await this.getWorldState(BlockNumber(referenceBlockNumber - 1));
1049
1222
  const [pathAndIndex] = await committedDb.findSiblingPaths(MerkleTreeId.ARCHIVE, [
1050
1223
  blockHash
1051
1224
  ]);
1052
1225
  return pathAndIndex === undefined ? undefined : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
1053
1226
  }
1054
1227
  async getNoteHashMembershipWitness(referenceBlock, noteHash) {
1055
- const committedDb = await this.#getWorldState(referenceBlock);
1228
+ const committedDb = await this.getWorldState(referenceBlock);
1056
1229
  const [pathAndIndex] = await committedDb.findSiblingPaths(MerkleTreeId.NOTE_HASH_TREE, [
1057
1230
  noteHash
1058
1231
  ]);
1059
1232
  return pathAndIndex === undefined ? undefined : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
1060
1233
  }
1061
1234
  async getL1ToL2MessageMembershipWitness(referenceBlock, l1ToL2Message) {
1062
- const db = await this.#getWorldState(referenceBlock);
1235
+ const db = await this.getWorldState(referenceBlock);
1063
1236
  const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [
1064
1237
  l1ToL2Message
1065
1238
  ]);
@@ -1072,9 +1245,9 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1072
1245
  witness.path
1073
1246
  ];
1074
1247
  }
1075
- async getL1ToL2MessageBlock(l1ToL2Message) {
1248
+ async getL1ToL2MessageCheckpoint(l1ToL2Message) {
1076
1249
  const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
1077
- return messageIndex ? BlockNumber.fromCheckpointNumber(InboxLeaf.checkpointNumberFromIndex(messageIndex)) : undefined;
1250
+ return messageIndex ? InboxLeaf.checkpointNumberFromIndex(messageIndex) : undefined;
1078
1251
  }
1079
1252
  /**
1080
1253
  * Returns whether an L1 to L2 message is synced by archiver and if it's ready to be included in a block.
@@ -1091,23 +1264,11 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1091
1264
  */ async getL2ToL1Messages(epoch) {
1092
1265
  // Assumes `getCheckpointedBlocksForEpoch` returns blocks in ascending order of block number.
1093
1266
  const checkpointedBlocks = await this.blockSource.getCheckpointedBlocksForEpoch(epoch);
1094
- const blocksInCheckpoints = [];
1095
- let previousSlotNumber = SlotNumber.ZERO;
1096
- let checkpointIndex = -1;
1097
- for (const checkpointedBlock of checkpointedBlocks){
1098
- const block = checkpointedBlock.block;
1099
- const slotNumber = block.header.globalVariables.slotNumber;
1100
- if (slotNumber !== previousSlotNumber) {
1101
- checkpointIndex++;
1102
- blocksInCheckpoints.push([]);
1103
- previousSlotNumber = slotNumber;
1104
- }
1105
- blocksInCheckpoints[checkpointIndex].push(block);
1106
- }
1267
+ const blocksInCheckpoints = chunkBy(checkpointedBlocks, (cb)=>cb.block.header.globalVariables.slotNumber).map((group)=>group.map((cb)=>cb.block));
1107
1268
  return blocksInCheckpoints.map((blocks)=>blocks.map((block)=>block.body.txEffects.map((txEffect)=>txEffect.l2ToL1Msgs)));
1108
1269
  }
1109
1270
  async getNullifierMembershipWitness(referenceBlock, nullifier) {
1110
- const db = await this.#getWorldState(referenceBlock);
1271
+ const db = await this.getWorldState(referenceBlock);
1111
1272
  const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [
1112
1273
  nullifier.toBuffer()
1113
1274
  ]);
@@ -1121,36 +1282,22 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1121
1282
  }
1122
1283
  return new NullifierMembershipWitness(index, leafPreimage, path);
1123
1284
  }
1124
- /**
1125
- * Returns a low nullifier membership witness for a given nullifier at a given block.
1126
- * @param referenceBlock - The block parameter (block number, block hash, or 'latest') at which to get the data
1127
- * (which contains the root of the nullifier tree in which we are searching for the nullifier).
1128
- * @param nullifier - Nullifier we try to find the low nullifier witness for.
1129
- * @returns The low nullifier membership witness (if found).
1130
- * @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
1131
- * list structure" of leaves and proving that a lower nullifier is pointing to a bigger next value than the nullifier
1132
- * we are trying to prove non-inclusion for.
1133
- *
1134
- * Note: This function returns the membership witness of the nullifier itself and not the low nullifier when
1135
- * the nullifier already exists in the tree. This is because the `getPreviousValueIndex` function returns the
1136
- * index of the nullifier itself when it already exists in the tree.
1137
- * TODO: This is a confusing behavior and we should eventually address that.
1138
- */ async getLowNullifierMembershipWitness(referenceBlock, nullifier) {
1139
- const committedDb = await this.#getWorldState(referenceBlock);
1285
+ async getLowNullifierMembershipWitness(referenceBlock, nullifier) {
1286
+ const committedDb = await this.getWorldState(referenceBlock);
1140
1287
  const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
1141
1288
  if (!findResult) {
1142
1289
  return undefined;
1143
1290
  }
1144
1291
  const { index, alreadyPresent } = findResult;
1145
1292
  if (alreadyPresent) {
1146
- this.log.warn(`Nullifier ${nullifier.toBigInt()} already exists in the tree`);
1293
+ throw new Error(`Cannot prove nullifier non-inclusion: nullifier ${nullifier.toBigInt()} already exists in the tree`);
1147
1294
  }
1148
1295
  const preimageData = await committedDb.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
1149
1296
  const siblingPath = await committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, BigInt(index));
1150
1297
  return new NullifierMembershipWitness(BigInt(index), preimageData, siblingPath);
1151
1298
  }
1152
1299
  async getPublicDataWitness(referenceBlock, leafSlot) {
1153
- const committedDb = await this.#getWorldState(referenceBlock);
1300
+ const committedDb = await this.getWorldState(referenceBlock);
1154
1301
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
1155
1302
  if (!lowLeafResult) {
1156
1303
  return undefined;
@@ -1161,7 +1308,7 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1161
1308
  }
1162
1309
  }
1163
1310
  async getPublicStorageAt(referenceBlock, contract, slot) {
1164
- const committedDb = await this.#getWorldState(referenceBlock);
1311
+ const committedDb = await this.getWorldState(referenceBlock);
1165
1312
  const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
1166
1313
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
1167
1314
  if (!lowLeafResult || !lowLeafResult.alreadyPresent) {
@@ -1194,6 +1341,12 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1194
1341
  */ async getBlockHeaderByArchive(archive) {
1195
1342
  return await this.blockSource.getBlockHeaderByArchive(archive);
1196
1343
  }
1344
+ getBlockData(number) {
1345
+ return this.blockSource.getBlockData(number);
1346
+ }
1347
+ getBlockDataByArchive(archive) {
1348
+ return this.blockSource.getBlockDataByArchive(archive);
1349
+ }
1197
1350
  /**
1198
1351
  * Simulates the public part of a transaction with the current state.
1199
1352
  * @param tx - The transaction to simulate.
@@ -1206,7 +1359,8 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1206
1359
  throw new BadRequestError(`Transaction total gas limit ${txGasLimit + teardownGasLimit} (${txGasLimit} + ${teardownGasLimit}) exceeds maximum gas limit ${this.config.rpcSimulatePublicMaxGasLimit} for simulation`);
1207
1360
  }
1208
1361
  const txHash = tx.getTxHash();
1209
- const blockNumber = BlockNumber(await this.blockSource.getBlockNumber() + 1);
1362
+ const latestBlockNumber = await this.blockSource.getBlockNumber();
1363
+ const blockNumber = BlockNumber.add(latestBlockNumber, 1);
1210
1364
  // If sequencer is not initialized, we just set these values to zero for simulation.
1211
1365
  const coinbase = EthAddress.ZERO;
1212
1366
  const feeRecipient = AztecAddress.ZERO;
@@ -1217,6 +1371,8 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1217
1371
  txHash,
1218
1372
  blockNumber
1219
1373
  });
1374
+ // Ensure world-state has caught up with the latest block we loaded from the archiver
1375
+ await this.worldStateSynchronizer.syncImmediate(latestBlockNumber);
1220
1376
  const merkleTreeFork = await this.worldStateSynchronizer.fork();
1221
1377
  try {
1222
1378
  const config = PublicSimulatorConfig.from({
@@ -1231,7 +1387,7 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1231
1387
  });
1232
1388
  const processor = publicProcessorFactory.create(merkleTreeFork, newGlobalVariables, config);
1233
1389
  // REFACTOR: Consider merging ProcessReturnValues into ProcessedTx
1234
- const [processedTxs, failedTxs, _usedTxs, returns] = await processor.process([
1390
+ const [processedTxs, failedTxs, _usedTxs, returns, debugLogs] = await processor.process([
1235
1391
  tx
1236
1392
  ]);
1237
1393
  // REFACTOR: Consider returning the error rather than throwing
@@ -1242,26 +1398,33 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1242
1398
  throw failedTxs[0].error;
1243
1399
  }
1244
1400
  const [processedTx] = processedTxs;
1245
- return new PublicSimulationOutput(processedTx.revertReason, processedTx.globalVariables, processedTx.txEffect, returns, processedTx.gasUsed);
1401
+ return new PublicSimulationOutput(processedTx.revertReason, processedTx.globalVariables, processedTx.txEffect, returns, processedTx.gasUsed, debugLogs);
1246
1402
  } finally{
1247
1403
  await merkleTreeFork.close();
1248
1404
  }
1249
1405
  }
1250
1406
  async isValidTx(tx, { isSimulation, skipFeeEnforcement } = {}) {
1251
1407
  const db = this.worldStateSynchronizer.getCommitted();
1252
- const verifier = isSimulation ? undefined : this.proofVerifier;
1253
- // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1408
+ const verifier = isSimulation ? undefined : this.rpcProofVerifier;
1409
+ // We accept transactions if they are not expired by the next slot (checked based on the ExpirationTimestamp field)
1254
1410
  const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1255
1411
  const blockNumber = BlockNumber(await this.blockSource.getBlockNumber() + 1);
1256
- const validator = createValidatorForAcceptingTxs(db, this.contractDataSource, verifier, {
1412
+ const l1Constants = await this.blockSource.getL1Constants();
1413
+ const validator = createTxValidatorForAcceptingTxsOverRPC(db, this.contractDataSource, verifier, {
1257
1414
  timestamp: nextSlotTimestamp,
1258
1415
  blockNumber,
1259
1416
  l1ChainId: this.l1ChainId,
1260
1417
  rollupVersion: this.version,
1261
- setupAllowList: this.config.txPublicSetupAllowList ?? await getDefaultAllowedSetupFunctions(),
1418
+ setupAllowList: [
1419
+ ...await getDefaultAllowedSetupFunctions(),
1420
+ ...this.config.txPublicSetupAllowListExtend ?? []
1421
+ ],
1262
1422
  gasFees: await this.getCurrentMinFees(),
1263
1423
  skipFeeEnforcement,
1264
- txsPermitted: !this.config.disableTransactions
1424
+ txsPermitted: !this.config.disableTransactions,
1425
+ rollupManaLimit: l1Constants.rollupManaLimit,
1426
+ maxBlockL2Gas: this.config.validateMaxL2BlockGas,
1427
+ maxBlockDAGas: this.config.validateMaxDABlockGas
1265
1428
  }, this.log.getBindings());
1266
1429
  return await validator.validateTx(tx);
1267
1430
  }
@@ -1284,7 +1447,18 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1284
1447
  archiver.updateConfig(config);
1285
1448
  }
1286
1449
  if (newConfig.realProofs !== this.config.realProofs) {
1287
- this.proofVerifier = config.realProofs ? await BBCircuitVerifier.new(newConfig) : new TestCircuitVerifier();
1450
+ await Promise.all([
1451
+ tryStop(this.peerProofVerifier),
1452
+ tryStop(this.rpcProofVerifier)
1453
+ ]);
1454
+ if (newConfig.realProofs) {
1455
+ this.peerProofVerifier = await BatchChonkVerifier.new(newConfig, newConfig.bbChonkVerifyMaxBatch, 'peer');
1456
+ const rpcVerifier = await BBCircuitVerifier.new(newConfig);
1457
+ this.rpcProofVerifier = new QueuedIVCVerifier(rpcVerifier, newConfig.numConcurrentIVCVerifiers);
1458
+ } else {
1459
+ this.peerProofVerifier = new TestCircuitVerifier();
1460
+ this.rpcProofVerifier = new TestCircuitVerifier();
1461
+ }
1288
1462
  }
1289
1463
  this.config = newConfig;
1290
1464
  }
@@ -1344,7 +1518,7 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1344
1518
  });
1345
1519
  return Promise.resolve();
1346
1520
  }
1347
- async rollbackTo(targetBlock, force) {
1521
+ async rollbackTo(targetBlock, force, resumeSync = true) {
1348
1522
  const archiver = this.blockSource;
1349
1523
  if (!('rollbackTo' in archiver)) {
1350
1524
  throw new Error('Archiver implementation does not support rollbacks.');
@@ -1372,9 +1546,13 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1372
1546
  this.log.error(`Error during rollback`, err);
1373
1547
  throw err;
1374
1548
  } finally{
1375
- this.log.info(`Resuming world state and archiver sync.`);
1376
- this.worldStateSynchronizer.resumeSync();
1377
- archiver.resume();
1549
+ if (resumeSync) {
1550
+ this.log.info(`Resuming world state and archiver sync.`);
1551
+ this.worldStateSynchronizer.resumeSync();
1552
+ archiver.resume();
1553
+ } else {
1554
+ this.log.info(`Sync left paused after rollback (resumeSync=false).`);
1555
+ }
1378
1556
  }
1379
1557
  }
1380
1558
  async pauseSync() {
@@ -1388,22 +1566,110 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1388
1566
  this.blockSource.resume();
1389
1567
  return Promise.resolve();
1390
1568
  }
1391
- getSlashPayloads() {
1392
- if (!this.slasherClient) {
1393
- throw new Error(`Slasher client not enabled`);
1394
- }
1395
- return this.slasherClient.getSlashPayloads();
1396
- }
1397
1569
  getSlashOffenses(round) {
1398
1570
  if (!this.slasherClient) {
1399
1571
  throw new Error(`Slasher client not enabled`);
1400
1572
  }
1401
1573
  if (round === 'all') {
1402
- return this.slasherClient.getPendingOffenses();
1574
+ return this.slasherClient.getOffenses();
1403
1575
  } else {
1404
1576
  return this.slasherClient.gatherOffensesForRound(round === 'current' ? undefined : BigInt(round));
1405
1577
  }
1406
1578
  }
1579
+ async reloadKeystore() {
1580
+ if (!this.config.keyStoreDirectory?.length) {
1581
+ throw new BadRequestError('Cannot reload keystore: node is not using a file-based keystore. ' + 'Set KEY_STORE_DIRECTORY to use file-based keystores.');
1582
+ }
1583
+ if (!this.validatorClient) {
1584
+ throw new BadRequestError('Cannot reload keystore: validator is not enabled.');
1585
+ }
1586
+ this.log.info('Reloading keystore from disk');
1587
+ // Re-read and validate keystore files
1588
+ const keyStores = loadKeystores(this.config.keyStoreDirectory);
1589
+ const newManager = new KeystoreManager(mergeKeystores(keyStores));
1590
+ await newManager.validateSigners();
1591
+ ValidatorClient.validateKeyStoreConfiguration(newManager, this.log);
1592
+ // Validate that every validator's publisher keys overlap with the L1 signers
1593
+ // that were initialized at startup. Publishers cannot be hot-reloaded, so a
1594
+ // validator with a publisher key that doesn't match any existing L1 signer
1595
+ // would silently fail on every proposer slot.
1596
+ if (this.keyStoreManager && this.sequencer) {
1597
+ const oldAdapter = NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager);
1598
+ const availablePublishers = new Set(oldAdapter.getAttesterAddresses().flatMap((a)=>oldAdapter.getPublisherAddresses(a).map((p)=>p.toString().toLowerCase())));
1599
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1600
+ for (const attester of newAdapter.getAttesterAddresses()){
1601
+ const pubs = newAdapter.getPublisherAddresses(attester);
1602
+ if (pubs.length > 0 && !pubs.some((p)=>availablePublishers.has(p.toString().toLowerCase()))) {
1603
+ throw new BadRequestError(`Cannot reload keystore: validator ${attester} has publisher keys ` + `[${pubs.map((p)=>p.toString()).join(', ')}] but none match the L1 signers initialized at startup ` + `[${[
1604
+ ...availablePublishers
1605
+ ].join(', ')}]. Publishers cannot be hot-reloaded — ` + `use an existing publisher key or restart the node.`);
1606
+ }
1607
+ }
1608
+ }
1609
+ // Build adapters for old and new keystores to compute diff
1610
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1611
+ const newAddresses = newAdapter.getAttesterAddresses();
1612
+ const oldAddresses = this.keyStoreManager ? NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager).getAttesterAddresses() : [];
1613
+ const oldSet = new Set(oldAddresses.map((a)=>a.toString()));
1614
+ const newSet = new Set(newAddresses.map((a)=>a.toString()));
1615
+ const added = newAddresses.filter((a)=>!oldSet.has(a.toString()));
1616
+ const removed = oldAddresses.filter((a)=>!newSet.has(a.toString()));
1617
+ if (added.length > 0) {
1618
+ this.log.info(`Keystore reload: adding attester keys: ${added.map((a)=>a.toString()).join(', ')}`);
1619
+ }
1620
+ if (removed.length > 0) {
1621
+ this.log.info(`Keystore reload: removing attester keys: ${removed.map((a)=>a.toString()).join(', ')}`);
1622
+ }
1623
+ if (added.length === 0 && removed.length === 0) {
1624
+ this.log.info('Keystore reload: attester keys unchanged');
1625
+ }
1626
+ // Update the validator client (coinbase, feeRecipient, attester keys)
1627
+ this.validatorClient.reloadKeystore(newManager);
1628
+ // Update the publisher factory's keystore so newly-added validators
1629
+ // can be matched to existing publisher keys when proposing blocks.
1630
+ if (this.sequencer) {
1631
+ this.sequencer.updatePublisherNodeKeyStore(newAdapter);
1632
+ }
1633
+ // Update slasher's "don't-slash-self" list with new validator addresses
1634
+ if (this.slasherClient && !this.config.slashSelfAllowed) {
1635
+ const slashValidatorsNever = unique([
1636
+ ...this.config.slashValidatorsNever ?? [],
1637
+ ...newAddresses
1638
+ ].map((a)=>a.toString())).map(EthAddress.fromString);
1639
+ this.slasherClient.updateConfig({
1640
+ slashValidatorsNever
1641
+ });
1642
+ }
1643
+ this.keyStoreManager = newManager;
1644
+ this.log.info('Keystore reloaded: coinbase, feeRecipient, and attester keys updated');
1645
+ }
1646
+ async mineBlock() {
1647
+ if (!this.sequencer) {
1648
+ throw new BadRequestError('Cannot mine block: no sequencer is running');
1649
+ }
1650
+ const currentBlockNumber = await this.getBlockNumber();
1651
+ // Use slot duration + 50% buffer as the timeout so this works on running networks too
1652
+ const { slotDuration } = await this.blockSource.getL1Constants();
1653
+ const timeoutSeconds = Math.ceil(slotDuration * 1.5);
1654
+ // Temporarily set minTxsPerBlock to 0 so the sequencer produces a block even with no txs
1655
+ const originalMinTxsPerBlock = this.sequencer.getSequencer().getConfig().minTxsPerBlock;
1656
+ this.sequencer.updateConfig({
1657
+ minTxsPerBlock: 0
1658
+ });
1659
+ try {
1660
+ // Trigger the sequencer to produce a block immediately
1661
+ void this.sequencer.trigger();
1662
+ // Wait for the new L2 block to appear
1663
+ await retryUntil(async ()=>{
1664
+ const newBlockNumber = await this.getBlockNumber();
1665
+ return newBlockNumber > currentBlockNumber ? true : undefined;
1666
+ }, 'mineBlock', timeoutSeconds, 0.1);
1667
+ } finally{
1668
+ this.sequencer.updateConfig({
1669
+ minTxsPerBlock: originalMinTxsPerBlock
1670
+ });
1671
+ }
1672
+ }
1407
1673
  #getInitialHeaderHash() {
1408
1674
  if (!this.initialHeaderHashPromise) {
1409
1675
  this.initialHeaderHashPromise = this.worldStateSynchronizer.getCommitted().getInitialHeader().hash();
@@ -1414,7 +1680,7 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1414
1680
  * Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
1415
1681
  * @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
1416
1682
  * @returns An instance of a committed MerkleTreeOperations
1417
- */ async #getWorldState(block) {
1683
+ */ async getWorldState(block) {
1418
1684
  let blockSyncedTo = BlockNumber.ZERO;
1419
1685
  try {
1420
1686
  // Attempt to sync the world state if necessary
@@ -1426,29 +1692,57 @@ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1426
1692
  this.log.debug(`Using committed db for block 'latest', world state synced upto ${blockSyncedTo}`);
1427
1693
  return this.worldStateSynchronizer.getCommitted();
1428
1694
  }
1695
+ // Get the block number, either directly from the parameter or by quering the archiver with the block hash
1696
+ let blockNumber;
1429
1697
  if (BlockHash.isBlockHash(block)) {
1430
1698
  const initialBlockHash = await this.#getInitialHeaderHash();
1431
1699
  if (block.equals(initialBlockHash)) {
1432
- // Block source doesn't handle initial header so we need to handle the case separately.
1433
- return this.worldStateSynchronizer.getSnapshot(BlockNumber.ZERO);
1700
+ // Block 0 is a first-class historical block: its state lives in the trees' persisted
1701
+ // block-0 payload. Resolving the genesis hash to block number 0 lets the snapshot path
1702
+ // pin reads to genesis state even after the node has advanced past it.
1703
+ blockNumber = BlockNumber.ZERO;
1704
+ } else {
1705
+ const header = await this.blockSource.getBlockHeaderByHash(block);
1706
+ if (!header) {
1707
+ throw new Error(`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.`);
1708
+ }
1709
+ blockNumber = header.getBlockNumber();
1710
+ }
1711
+ } else {
1712
+ blockNumber = block;
1713
+ }
1714
+ // Check it's within world state sync range
1715
+ if (blockNumber > blockSyncedTo) {
1716
+ throw new Error(`Queried block ${block} not yet synced by the node (node is synced upto ${blockSyncedTo}).`);
1717
+ }
1718
+ this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1719
+ const snapshot = this.worldStateSynchronizer.getSnapshot(blockNumber);
1720
+ // Double-check world-state synced to the same block hash as was requested
1721
+ if (BlockHash.isBlockHash(block)) {
1722
+ const blockHash = await snapshot.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
1723
+ if (!blockHash || !block.equals(blockHash)) {
1724
+ const initialBlockHash = await this.#getInitialHeaderHash();
1725
+ throw new Error(`Block hash ${block.toString()} not found in world state at block number ${blockNumber} (world state has ${blockHash?.toString() ?? 'no hash'} at that index, genesis header hash is ${initialBlockHash.toString()}). If the node API has been queried with anchor block hash possibly a reorg has occurred.`);
1726
+ }
1727
+ }
1728
+ return snapshot;
1729
+ }
1730
+ /** Resolves a block parameter to a block number. */ async resolveBlockNumber(block) {
1731
+ if (block === 'latest') {
1732
+ return BlockNumber(await this.blockSource.getBlockNumber());
1733
+ }
1734
+ if (BlockHash.isBlockHash(block)) {
1735
+ const initialBlockHash = await this.#getInitialHeaderHash();
1736
+ if (block.equals(initialBlockHash)) {
1737
+ return BlockNumber.ZERO;
1434
1738
  }
1435
1739
  const header = await this.blockSource.getBlockHeaderByHash(block);
1436
1740
  if (!header) {
1437
- throw new Error(`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.`);
1438
- }
1439
- const blockNumber = header.getBlockNumber();
1440
- this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1441
- return this.worldStateSynchronizer.getSnapshot(blockNumber);
1442
- }
1443
- // Block number provided
1444
- {
1445
- const blockNumber = block;
1446
- if (blockNumber > blockSyncedTo) {
1447
- throw new Error(`Queried block ${block} not yet synced by the node (node is synced upto ${blockSyncedTo}).`);
1741
+ throw new Error(`Block hash ${block.toString()} not found.`);
1448
1742
  }
1449
- this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1450
- return this.worldStateSynchronizer.getSnapshot(blockNumber);
1743
+ return header.getBlockNumber();
1451
1744
  }
1745
+ return block;
1452
1746
  }
1453
1747
  /**
1454
1748
  * Ensure we fully sync the world state