@aztec/aztec-node 0.0.1-commit.8afd444 → 0.0.1-commit.8cb2d04d8

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,30 +1,44 @@
1
1
  import { Archiver, createArchiver } from '@aztec/archiver';
2
- import { BBCircuitVerifier, QueuedIVCVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
2
+ import { BBCircuitVerifier, BatchChonkVerifier, QueuedIVCVerifier } from '@aztec/bb-prover';
3
+ import { TestCircuitVerifier } from '@aztec/bb-prover/test';
3
4
  import { type BlobClientInterface, createBlobClientWithFileStores } from '@aztec/blob-client/client';
5
+ import { Blob } from '@aztec/blob-lib';
4
6
  import { ARCHIVE_HEIGHT, type L1_TO_L2_MSG_TREE_HEIGHT, type NOTE_HASH_TREE_HEIGHT } from '@aztec/constants';
5
7
  import { EpochCache, type EpochCacheInterface } from '@aztec/epoch-cache';
6
8
  import { createEthereumChain } from '@aztec/ethereum/chain';
7
- import { getPublicClient } from '@aztec/ethereum/client';
9
+ import { getPublicClient, makeL1HttpTransport } from '@aztec/ethereum/client';
8
10
  import { RegistryContract, RollupContract } from '@aztec/ethereum/contracts';
9
11
  import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
12
+ import type { L1TxUtils } from '@aztec/ethereum/l1-tx-utils';
10
13
  import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
11
- import { compactArray, pick } from '@aztec/foundation/collection';
14
+ import { chunkBy, compactArray, pick, unique } from '@aztec/foundation/collection';
12
15
  import { Fr } from '@aztec/foundation/curves/bn254';
13
16
  import { EthAddress } from '@aztec/foundation/eth-address';
14
17
  import { BadRequestError } from '@aztec/foundation/json-rpc';
15
18
  import { type Logger, createLogger } from '@aztec/foundation/log';
19
+ import { retryUntil } from '@aztec/foundation/retry';
16
20
  import { count } from '@aztec/foundation/string';
17
21
  import { DateProvider, Timer } from '@aztec/foundation/timer';
18
22
  import { MembershipWitness, SiblingPath } from '@aztec/foundation/trees';
19
- import { KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
23
+ import { type KeyStore, KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
20
24
  import { trySnapshotSync, uploadSnapshot } from '@aztec/node-lib/actions';
25
+ import { createForwarderL1TxUtilsFromSigners, createL1TxUtilsFromSigners } from '@aztec/node-lib/factories';
21
26
  import {
22
- createForwarderL1TxUtilsFromEthSigner,
23
- createL1TxUtilsWithBlobsFromEthSigner,
24
- } from '@aztec/node-lib/factories';
25
- import { type P2P, type P2PClientDeps, createP2PClient, getDefaultAllowedSetupFunctions } from '@aztec/p2p';
27
+ type P2P,
28
+ type P2PClientDeps,
29
+ createP2PClient,
30
+ createTxValidatorForAcceptingTxsOverRPC,
31
+ getDefaultAllowedSetupFunctions,
32
+ } from '@aztec/p2p';
26
33
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
27
- import { GlobalVariableBuilder, SequencerClient, type SequencerPublisher } from '@aztec/sequencer-client';
34
+ import { type ProverNode, type ProverNodeDeps, createProverNode } from '@aztec/prover-node';
35
+ import { createKeyStoreForProver } from '@aztec/prover-node/config';
36
+ import {
37
+ FeeProviderImpl,
38
+ GlobalVariableBuilder,
39
+ SequencerClient,
40
+ type SequencerPublisher,
41
+ } from '@aztec/sequencer-client';
28
42
  import { PublicProcessorFactory } from '@aztec/simulator/server';
29
43
  import {
30
44
  AttestationsBlockWatcher,
@@ -35,7 +49,14 @@ import {
35
49
  } from '@aztec/slasher';
36
50
  import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
37
51
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
38
- import { BlockHash, type BlockParameter, type DataInBlock, L2Block, type L2BlockSource } from '@aztec/stdlib/block';
52
+ import {
53
+ type BlockData,
54
+ BlockHash,
55
+ type BlockParameter,
56
+ type DataInBlock,
57
+ L2Block,
58
+ type L2BlockSource,
59
+ } from '@aztec/stdlib/block';
39
60
  import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
40
61
  import type {
41
62
  ContractClassPublic,
@@ -44,13 +65,14 @@ import type {
44
65
  NodeInfo,
45
66
  ProtocolContractAddresses,
46
67
  } from '@aztec/stdlib/contract';
47
- import { GasFees } from '@aztec/stdlib/gas';
68
+ import { GasFees, type ManaUsageEstimate } from '@aztec/stdlib/gas';
48
69
  import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
49
70
  import {
50
71
  type AztecNode,
51
72
  type AztecNodeAdmin,
52
73
  type AztecNodeAdminConfig,
53
74
  AztecNodeAdminConfigSchema,
75
+ type AztecNodeDebug,
54
76
  type GetContractClassLogsResponse,
55
77
  type GetPublicLogsResponse,
56
78
  } from '@aztec/stdlib/interfaces/client';
@@ -63,14 +85,15 @@ import {
63
85
  type WorldStateSynchronizer,
64
86
  tryStop,
65
87
  } from '@aztec/stdlib/interfaces/server';
66
- import type { LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
88
+ import type { DebugLogStore, LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
89
+ import { InMemoryDebugLogStore, NullDebugLogStore } from '@aztec/stdlib/logs';
67
90
  import { InboxLeaf, type L1ToL2MessageSource } from '@aztec/stdlib/messaging';
68
- import { P2PClientType } from '@aztec/stdlib/p2p';
69
- import type { Offense, SlashPayloadRound } from '@aztec/stdlib/slashing';
70
- import type { NullifierLeafPreimage, PublicDataTreeLeaf, PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
91
+ import type { Offense } from '@aztec/stdlib/slashing';
92
+ import type { NullifierLeafPreimage, PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
71
93
  import { MerkleTreeId, NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
72
94
  import {
73
95
  type BlockHeader,
96
+ type FeeProvider,
74
97
  type GlobalVariableBuilder as GlobalVariableBuilderInterface,
75
98
  type IndexedTxEffect,
76
99
  PublicSimulationOutput,
@@ -82,6 +105,7 @@ import {
82
105
  } from '@aztec/stdlib/tx';
83
106
  import { getPackageVersion } from '@aztec/stdlib/update-checker';
84
107
  import type { SingleValidatorStats, ValidatorsStats } from '@aztec/stdlib/validators';
108
+ import type { GenesisData } from '@aztec/stdlib/world-state';
85
109
  import {
86
110
  Attributes,
87
111
  type TelemetryClient,
@@ -95,13 +119,13 @@ import {
95
119
  FullNodeCheckpointsBuilder,
96
120
  NodeKeystoreAdapter,
97
121
  ValidatorClient,
98
- createBlockProposalHandler,
122
+ createProposalHandler,
99
123
  createValidatorClient,
100
- createValidatorForAcceptingTxs,
101
124
  } from '@aztec/validator-client';
125
+ import type { SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
102
126
  import { createWorldStateSynchronizer } from '@aztec/world-state';
103
127
 
104
- import { createPublicClient, fallback, http } from 'viem';
128
+ import { createPublicClient } from 'viem';
105
129
 
106
130
  import { createSentinel } from '../sentinel/factory.js';
107
131
  import { Sentinel } from '../sentinel/sentinel.js';
@@ -111,7 +135,7 @@ import { NodeMetrics } from './node_metrics.js';
111
135
  /**
112
136
  * The aztec node.
113
137
  */
114
- export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
138
+ export class AztecNodeService implements AztecNode, AztecNodeAdmin, AztecNodeDebug, Traceable {
115
139
  private metrics: NodeMetrics;
116
140
  private initialHeaderHashPromise: Promise<BlockHash> | undefined = undefined;
117
141
 
@@ -129,24 +153,42 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
129
153
  protected readonly l1ToL2MessageSource: L1ToL2MessageSource,
130
154
  protected readonly worldStateSynchronizer: WorldStateSynchronizer,
131
155
  protected readonly sequencer: SequencerClient | undefined,
156
+ protected readonly proverNode: ProverNode | undefined,
132
157
  protected readonly slasherClient: SlasherClientInterface | undefined,
133
158
  protected readonly validatorsSentinel: Sentinel | undefined,
134
159
  protected readonly epochPruneWatcher: EpochPruneWatcher | undefined,
135
160
  protected readonly l1ChainId: number,
136
161
  protected readonly version: number,
137
162
  protected readonly globalVariableBuilder: GlobalVariableBuilderInterface,
163
+ protected readonly feeProvider: FeeProvider,
138
164
  protected readonly epochCache: EpochCacheInterface,
139
165
  protected readonly packageVersion: string,
140
- private proofVerifier: ClientProtocolCircuitVerifier,
166
+ private peerProofVerifier: ClientProtocolCircuitVerifier,
167
+ private rpcProofVerifier: ClientProtocolCircuitVerifier,
141
168
  private telemetry: TelemetryClient = getTelemetryClient(),
142
169
  private log = createLogger('node'),
143
170
  private blobClient?: BlobClientInterface,
171
+ private validatorClient?: ValidatorClient,
172
+ private keyStoreManager?: KeystoreManager,
173
+ private debugLogStore: DebugLogStore = new NullDebugLogStore(),
144
174
  ) {
145
175
  this.metrics = new NodeMetrics(telemetry, 'AztecNodeService');
146
176
  this.tracer = telemetry.getTracer('AztecNodeService');
147
177
 
148
178
  this.log.info(`Aztec Node version: ${this.packageVersion}`);
149
179
  this.log.info(`Aztec Node started on chain 0x${l1ChainId.toString(16)}`, config.l1Contracts);
180
+
181
+ // A defensive check that protects us against introducing a bug in the complex `createAndSync` function. We must
182
+ // never have debugLogStore enabled when not in test mode because then we would be accumulating debug logs in
183
+ // memory which could be a DoS vector on the sequencer (since no fees are paid for debug logs).
184
+ if (debugLogStore.isEnabled && config.realProofs) {
185
+ throw new Error('debugLogStore should never be enabled when realProofs are set');
186
+ }
187
+ }
188
+
189
+ /** @internal Exposed for testing — returns the RPC proof verifier. */
190
+ public getProofVerifier(): ClientProtocolCircuitVerifier {
191
+ return this.rpcProofVerifier;
150
192
  }
151
193
 
152
194
  public async getWorldStateSyncStatus(): Promise<WorldStateSyncStatus> {
@@ -170,11 +212,14 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
170
212
  logger?: Logger;
171
213
  publisher?: SequencerPublisher;
172
214
  dateProvider?: DateProvider;
173
- p2pClientDeps?: P2PClientDeps<P2PClientType.Full>;
215
+ p2pClientDeps?: P2PClientDeps;
216
+ proverNodeDeps?: Partial<ProverNodeDeps>;
217
+ slashingProtectionDb?: SlashingProtectionDatabase;
174
218
  } = {},
175
219
  options: {
176
- prefilledPublicData?: PublicDataTreeLeaf[];
220
+ genesis?: GenesisData;
177
221
  dontStartSequencer?: boolean;
222
+ dontStartProverNode?: boolean;
178
223
  } = {},
179
224
  ): Promise<AztecNodeService> {
180
225
  const config = { ...inputConfig }; // Copy the config so we dont mutate the input object
@@ -184,16 +229,29 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
184
229
  const dateProvider = deps.dateProvider ?? new DateProvider();
185
230
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
186
231
 
187
- // Build a key store from file if given or from environment otherwise
232
+ // Build a key store from file if given or from environment otherwise.
233
+ // We keep the raw KeyStore available so we can merge with prover keys if enableProverNode is set.
188
234
  let keyStoreManager: KeystoreManager | undefined;
189
235
  const keyStoreProvided = config.keyStoreDirectory !== undefined && config.keyStoreDirectory.length > 0;
190
236
  if (keyStoreProvided) {
191
237
  const keyStores = loadKeystores(config.keyStoreDirectory!);
192
238
  keyStoreManager = new KeystoreManager(mergeKeystores(keyStores));
193
239
  } else {
194
- const keyStore = createKeyStoreForValidator(config);
195
- if (keyStore) {
196
- keyStoreManager = new KeystoreManager(keyStore);
240
+ const rawKeyStores: KeyStore[] = [];
241
+ const validatorKeyStore = createKeyStoreForValidator(config);
242
+ if (validatorKeyStore) {
243
+ rawKeyStores.push(validatorKeyStore);
244
+ }
245
+ if (config.enableProverNode) {
246
+ const proverKeyStore = createKeyStoreForProver(config);
247
+ if (proverKeyStore) {
248
+ rawKeyStores.push(proverKeyStore);
249
+ }
250
+ }
251
+ if (rawKeyStores.length > 0) {
252
+ keyStoreManager = new KeystoreManager(
253
+ rawKeyStores.length === 1 ? rawKeyStores[0] : mergeKeystores(rawKeyStores),
254
+ );
197
255
  }
198
256
  }
199
257
 
@@ -204,10 +262,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
204
262
  if (keyStoreManager === undefined) {
205
263
  throw new Error('Failed to create key store, a requirement for running a validator');
206
264
  }
207
- if (!keyStoreProvided) {
208
- log.warn(
209
- 'KEY STORE CREATED FROM ENVIRONMENT, IT IS RECOMMENDED TO USE A FILE-BASED KEY STORE IN PRODUCTION ENVIRONMENTS',
210
- );
265
+ if (!keyStoreProvided && process.env.NODE_ENV !== 'test') {
266
+ log.warn("Keystore created from env: it's recommended to use a file-based key store for production");
211
267
  }
212
268
  ValidatorClient.validateKeyStoreConfiguration(keyStoreManager, log);
213
269
  }
@@ -221,7 +277,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
221
277
 
222
278
  const publicClient = createPublicClient({
223
279
  chain: ethereumChain.chainInfo,
224
- transport: fallback(config.l1RpcUrls.map((url: string) => http(url, { batch: false }))),
280
+ transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
225
281
  pollingInterval: config.viemPollingIntervalMS,
226
282
  });
227
283
 
@@ -235,10 +291,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
235
291
  config.l1Contracts = { ...config.l1Contracts, ...l1ContractsAddresses };
236
292
 
237
293
  const rollupContract = new RollupContract(publicClient, config.l1Contracts.rollupAddress.toString());
238
- const [l1GenesisTime, slotDuration, rollupVersionFromRollup] = await Promise.all([
294
+ const [l1GenesisTime, slotDuration, rollupVersionFromRollup, rollupManaLimit] = await Promise.all([
239
295
  rollupContract.getL1GenesisTime(),
240
296
  rollupContract.getSlotDuration(),
241
297
  rollupContract.getVersion(),
298
+ rollupContract.getManaLimit().then(Number),
242
299
  ] as const);
243
300
 
244
301
  config.rollupVersion ??= Number(rollupVersionFromRollup);
@@ -249,7 +306,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
249
306
  );
250
307
  }
251
308
 
252
- const blobClient = await createBlobClientWithFileStores(config, createLogger('node:blob-client:client'));
309
+ const blobClient = await createBlobClientWithFileStores(config, log.createChild('blob-client'));
253
310
 
254
311
  // attempt snapshot sync if possible
255
312
  await trySnapshotSync(config, log);
@@ -263,27 +320,41 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
263
320
  );
264
321
 
265
322
  // now create the merkle trees and the world state synchronizer
266
- const worldStateSynchronizer = await createWorldStateSynchronizer(
267
- config,
268
- archiver,
269
- options.prefilledPublicData,
270
- telemetry,
271
- );
272
- const circuitVerifier =
273
- config.realProofs || config.debugForceTxProofVerification
274
- ? await BBCircuitVerifier.new(config)
275
- : new TestCircuitVerifier(config.proverTestVerificationDelayMs);
323
+ const worldStateSynchronizer = await createWorldStateSynchronizer(config, archiver, options.genesis, telemetry);
324
+ const useRealVerifiers = config.realProofs || config.debugForceTxProofVerification;
325
+ let peerProofVerifier: ClientProtocolCircuitVerifier;
326
+ let rpcProofVerifier: ClientProtocolCircuitVerifier;
327
+ if (useRealVerifiers) {
328
+ peerProofVerifier = await BatchChonkVerifier.new(config, config.bbChonkVerifyMaxBatch, 'peer');
329
+ const rpcVerifier = await BBCircuitVerifier.new(config);
330
+ rpcProofVerifier = new QueuedIVCVerifier(rpcVerifier, config.numConcurrentIVCVerifiers);
331
+ } else {
332
+ peerProofVerifier = new TestCircuitVerifier(config.proverTestVerificationDelayMs);
333
+ rpcProofVerifier = new TestCircuitVerifier(config.proverTestVerificationDelayMs);
334
+ }
335
+
336
+ let debugLogStore: DebugLogStore;
276
337
  if (!config.realProofs) {
277
338
  log.warn(`Aztec node is accepting fake proofs`);
339
+
340
+ debugLogStore = new InMemoryDebugLogStore();
341
+ log.info(
342
+ 'Aztec node started in test mode (realProofs set to false) hence debug logs from public functions will be collected and served',
343
+ );
344
+ } else {
345
+ debugLogStore = new NullDebugLogStore();
346
+ }
347
+
348
+ const proverOnly = config.enableProverNode && config.disableValidator;
349
+ if (proverOnly) {
350
+ log.info('Starting in prover-only mode: skipping validator, sequencer, sentinel, and slasher subsystems');
278
351
  }
279
- const proofVerifier = new QueuedIVCVerifier(config, circuitVerifier);
280
352
 
281
353
  // create the tx pool and the p2p client, which will need the l2 block source
282
354
  const p2pClient = await createP2PClient(
283
- P2PClientType.Full,
284
355
  config,
285
356
  archiver,
286
- proofVerifier,
357
+ peerProofVerifier,
287
358
  worldStateSynchronizer,
288
359
  epochCache,
289
360
  packageVersion,
@@ -292,59 +363,77 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
292
363
  deps.p2pClientDeps,
293
364
  );
294
365
 
295
- // We should really not be modifying the config object
296
- config.txPublicSetupAllowList = config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
366
+ // We'll accumulate sentinel watchers here
367
+ const watchers: Watcher[] = [];
297
368
 
298
- // Create FullNodeCheckpointsBuilder for validator and non-validator block proposal handling
369
+ // Create FullNodeCheckpointsBuilder for block proposal handling and tx validation.
370
+ // Override maxTxsPerCheckpoint with the validator-specific limit if set.
299
371
  const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder(
300
- { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
372
+ {
373
+ ...config,
374
+ l1GenesisTime,
375
+ slotDuration: Number(slotDuration),
376
+ rollupManaLimit,
377
+ maxTxsPerCheckpoint: config.validateMaxTxsPerCheckpoint,
378
+ },
301
379
  worldStateSynchronizer,
302
380
  archiver,
303
381
  dateProvider,
304
382
  telemetry,
305
383
  );
306
384
 
307
- // We'll accumulate sentinel watchers here
308
- const watchers: Watcher[] = [];
385
+ let validatorClient: ValidatorClient | undefined;
309
386
 
310
- // Create validator client if required
311
- const validatorClient = await createValidatorClient(config, {
312
- checkpointsBuilder: validatorCheckpointsBuilder,
313
- worldState: worldStateSynchronizer,
314
- p2pClient,
315
- telemetry,
316
- dateProvider,
317
- epochCache,
318
- blockSource: archiver,
319
- l1ToL2MessageSource: archiver,
320
- keyStoreManager,
321
- blobClient,
322
- });
387
+ if (!proverOnly) {
388
+ // Create validator client if required
389
+ validatorClient = await createValidatorClient(config, {
390
+ checkpointsBuilder: validatorCheckpointsBuilder,
391
+ worldState: worldStateSynchronizer,
392
+ p2pClient,
393
+ telemetry,
394
+ dateProvider,
395
+ epochCache,
396
+ blockSource: archiver,
397
+ l1ToL2MessageSource: archiver,
398
+ keyStoreManager,
399
+ blobClient,
400
+ slashingProtectionDb: deps.slashingProtectionDb,
401
+ });
402
+
403
+ // If we have a validator client, register it as a source of offenses for the slasher,
404
+ // and have it register callbacks on the p2p client *before* we start it, otherwise messages
405
+ // like attestations or auths will fail.
406
+ if (validatorClient) {
407
+ watchers.push(validatorClient);
408
+
409
+ const vc = validatorClient;
410
+ const getValidatorAddresses = () => vc.getValidatorAddresses().map(a => a.toString());
411
+ validatorClient.getProposalHandler().register(p2pClient, true, archiver, getValidatorAddresses);
323
412
 
324
- // If we have a validator client, register it as a source of offenses for the slasher,
325
- // and have it register callbacks on the p2p client *before* we start it, otherwise messages
326
- // like attestations or auths will fail.
327
- if (validatorClient) {
328
- watchers.push(validatorClient);
329
- if (!options.dontStartSequencer) {
330
- await validatorClient.registerHandlers();
413
+ if (!options.dontStartSequencer) {
414
+ await validatorClient.registerHandlers();
415
+ }
331
416
  }
332
417
  }
333
418
 
334
- // If there's no validator client but alwaysReexecuteBlockProposals is enabled,
335
- // create a BlockProposalHandler to reexecute block proposals for monitoring
336
- if (!validatorClient && config.alwaysReexecuteBlockProposals) {
337
- log.info('Setting up block proposal reexecution for monitoring');
338
- createBlockProposalHandler(config, {
419
+ // If there's no validator client, create a ProposalHandler to handle block and checkpoint proposals
420
+ // for monitoring or reexecution. Reexecution (default) allows us to follow the pending chain,
421
+ // while non-reexecution is used for validating the proposals and collecting their txs.
422
+ // Checkpoint proposals rebuild blobs if the blob client can upload blobs.
423
+ if (!validatorClient) {
424
+ const reexecute = !!config.alwaysReexecuteBlockProposals;
425
+ log.info(`Setting up proposal handler` + (reexecute ? ' with reexecution of proposals' : ''));
426
+ createProposalHandler(config, {
339
427
  checkpointsBuilder: validatorCheckpointsBuilder,
340
428
  worldState: worldStateSynchronizer,
341
429
  epochCache,
342
430
  blockSource: archiver,
343
431
  l1ToL2MessageSource: archiver,
344
432
  p2pClient,
433
+ blobClient,
345
434
  dateProvider,
346
435
  telemetry,
347
- }).registerForReexecution(p2pClient);
436
+ }).register(p2pClient, reexecute, archiver);
348
437
  }
349
438
 
350
439
  // Start world state and wait for it to sync to the archiver.
@@ -353,36 +442,39 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
353
442
  // Start p2p. Note that it depends on world state to be running.
354
443
  await p2pClient.start();
355
444
 
356
- const validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
357
- if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
358
- watchers.push(validatorsSentinel);
359
- }
360
-
445
+ let validatorsSentinel: Awaited<ReturnType<typeof createSentinel>> | undefined;
361
446
  let epochPruneWatcher: EpochPruneWatcher | undefined;
362
- if (config.slashPrunePenalty > 0n || config.slashDataWithholdingPenalty > 0n) {
363
- epochPruneWatcher = new EpochPruneWatcher(
364
- archiver,
365
- archiver,
366
- epochCache,
367
- p2pClient.getTxProvider(),
368
- validatorCheckpointsBuilder,
369
- config,
370
- );
371
- watchers.push(epochPruneWatcher);
372
- }
373
-
374
- // We assume we want to slash for invalid attestations unless all max penalties are set to 0
375
447
  let attestationsBlockWatcher: AttestationsBlockWatcher | undefined;
376
- if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
377
- attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
378
- watchers.push(attestationsBlockWatcher);
448
+
449
+ if (!proverOnly) {
450
+ validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
451
+ if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
452
+ watchers.push(validatorsSentinel);
453
+ }
454
+
455
+ if (config.slashPrunePenalty > 0n || config.slashDataWithholdingPenalty > 0n) {
456
+ epochPruneWatcher = new EpochPruneWatcher(
457
+ archiver,
458
+ archiver,
459
+ epochCache,
460
+ p2pClient.getTxProvider(),
461
+ validatorCheckpointsBuilder,
462
+ config,
463
+ );
464
+ watchers.push(epochPruneWatcher);
465
+ }
466
+
467
+ // We assume we want to slash for invalid attestations unless all max penalties are set to 0
468
+ if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
469
+ attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
470
+ watchers.push(attestationsBlockWatcher);
471
+ }
379
472
  }
380
473
 
381
474
  // Start p2p-related services once the archiver has completed sync
382
475
  void archiver
383
476
  .waitForInitialSync()
384
477
  .then(async () => {
385
- await p2pClient.start();
386
478
  await validatorsSentinel?.start();
387
479
  await epochPruneWatcher?.start();
388
480
  await attestationsBlockWatcher?.start();
@@ -390,6 +482,17 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
390
482
  })
391
483
  .catch(err => log.error('Failed to start p2p services after archiver sync', err));
392
484
 
485
+ const globalVariableBuilderConfig = {
486
+ l1Contracts: config.l1Contracts,
487
+ ethereumSlotDuration: config.ethereumSlotDuration,
488
+ rollupVersion: BigInt(config.rollupVersion),
489
+ l1GenesisTime,
490
+ slotDuration: Number(slotDuration),
491
+ };
492
+
493
+ const globalVariableBuilder = new GlobalVariableBuilder(dateProvider, publicClient, globalVariableBuilderConfig);
494
+ const feeProvider = new FeeProviderImpl(dateProvider, publicClient, globalVariableBuilderConfig);
495
+
393
496
  // Validator enabled, create/start relevant service
394
497
  let sequencer: SequencerClient | undefined;
395
498
  let slasherClient: SlasherClientInterface | undefined;
@@ -412,34 +515,49 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
412
515
  );
413
516
  await slasherClient.start();
414
517
 
415
- const l1TxUtils = config.publisherForwarderAddress
416
- ? await createForwarderL1TxUtilsFromEthSigner(
518
+ const l1TxUtils = config.sequencerPublisherForwarderAddress
519
+ ? await createForwarderL1TxUtilsFromSigners(
417
520
  publicClient,
418
521
  keyStoreManager!.createAllValidatorPublisherSigners(),
419
- config.publisherForwarderAddress,
522
+ config.sequencerPublisherForwarderAddress,
420
523
  { ...config, scope: 'sequencer' },
421
- { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider },
524
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
422
525
  )
423
- : await createL1TxUtilsWithBlobsFromEthSigner(
526
+ : await createL1TxUtilsFromSigners(
424
527
  publicClient,
425
528
  keyStoreManager!.createAllValidatorPublisherSigners(),
426
529
  { ...config, scope: 'sequencer' },
427
- { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider },
530
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
428
531
  );
429
532
 
533
+ // Create a funder L1TxUtils from the keystore funding account (if configured)
534
+ const fundingSigner = keyStoreManager?.createFundingSigner();
535
+ let funderL1TxUtils: L1TxUtils | undefined;
536
+ if (fundingSigner) {
537
+ const [funder] = await createL1TxUtilsFromSigners(
538
+ publicClient,
539
+ [fundingSigner],
540
+ { ...config, scope: 'sequencer' },
541
+ { telemetry, logger: log.createChild('l1-tx-utils:funder'), dateProvider },
542
+ );
543
+ funderL1TxUtils = funder;
544
+ }
545
+
430
546
  // Create and start the sequencer client
431
547
  const checkpointsBuilder = new CheckpointsBuilder(
432
- { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
548
+ { ...config, l1GenesisTime, slotDuration: Number(slotDuration), rollupManaLimit },
433
549
  worldStateSynchronizer,
434
550
  archiver,
435
551
  dateProvider,
436
552
  telemetry,
553
+ debugLogStore,
437
554
  );
438
555
 
439
556
  sequencer = await SequencerClient.new(config, {
440
557
  ...deps,
441
558
  epochCache,
442
559
  l1TxUtils,
560
+ funderL1TxUtils,
443
561
  validatorClient,
444
562
  p2pClient,
445
563
  worldStateSynchronizer,
@@ -451,6 +569,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
451
569
  dateProvider,
452
570
  blobClient,
453
571
  nodeKeyStore: keyStoreManager!,
572
+ globalVariableBuilder,
454
573
  });
455
574
  }
456
575
 
@@ -461,14 +580,30 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
461
580
  log.warn(`Sequencer created but not started`);
462
581
  }
463
582
 
464
- const globalVariableBuilder = new GlobalVariableBuilder({
465
- ...config,
466
- rollupVersion: BigInt(config.rollupVersion),
467
- l1GenesisTime,
468
- slotDuration: Number(slotDuration),
469
- });
583
+ // Create prover node subsystem if enabled
584
+ let proverNode: ProverNode | undefined;
585
+ if (config.enableProverNode) {
586
+ proverNode = await createProverNode(config, {
587
+ ...deps.proverNodeDeps,
588
+ telemetry,
589
+ dateProvider,
590
+ archiver,
591
+ worldStateSynchronizer,
592
+ p2pClient,
593
+ epochCache,
594
+ blobClient,
595
+ keyStoreManager,
596
+ });
597
+
598
+ if (!options.dontStartProverNode) {
599
+ await proverNode.start();
600
+ log.info(`Prover node subsystem started`);
601
+ } else {
602
+ log.info(`Prover node subsystem created but not started`);
603
+ }
604
+ }
470
605
 
471
- return new AztecNodeService(
606
+ const node = new AztecNodeService(
472
607
  config,
473
608
  p2pClient,
474
609
  archiver,
@@ -477,19 +612,27 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
477
612
  archiver,
478
613
  worldStateSynchronizer,
479
614
  sequencer,
615
+ proverNode,
480
616
  slasherClient,
481
617
  validatorsSentinel,
482
618
  epochPruneWatcher,
483
619
  ethereumChain.chainInfo.id,
484
620
  config.rollupVersion,
485
621
  globalVariableBuilder,
622
+ feeProvider,
486
623
  epochCache,
487
624
  packageVersion,
488
- proofVerifier,
625
+ peerProofVerifier,
626
+ rpcProofVerifier,
489
627
  telemetry,
490
628
  log,
491
629
  blobClient,
630
+ validatorClient,
631
+ keyStoreManager,
632
+ debugLogStore,
492
633
  );
634
+
635
+ return node;
493
636
  }
494
637
 
495
638
  /**
@@ -500,6 +643,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
500
643
  return this.sequencer;
501
644
  }
502
645
 
646
+ /** Returns the prover node subsystem, if enabled. */
647
+ public getProverNode(): ProverNode | undefined {
648
+ return this.proverNode;
649
+ }
650
+
503
651
  public getBlockSource(): L2BlockSource {
504
652
  return this.blockSource;
505
653
  }
@@ -525,7 +673,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
525
673
  }
526
674
 
527
675
  public async getAllowedPublicSetup(): Promise<AllowedElement[]> {
528
- return this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
676
+ return [...(await getDefaultAllowedSetupFunctions()), ...(this.config.txPublicSetupAllowListExtend ?? [])];
529
677
  }
530
678
 
531
679
  /**
@@ -553,6 +701,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
553
701
  enr,
554
702
  l1ContractAddresses: contractAddresses,
555
703
  protocolContractAddresses: protocolContractAddresses,
704
+ realProofs: !!this.config.realProofs,
556
705
  };
557
706
 
558
707
  return nodeInfo;
@@ -619,12 +768,17 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
619
768
  return (await this.blockSource.getCheckpointedBlocks(from, limit)) ?? [];
620
769
  }
621
770
 
622
- /**
623
- * Method to fetch the current min L2 fees.
624
- * @returns The current min L2 fees.
625
- */
771
+ public getCheckpointsDataForEpoch(epochNumber: EpochNumber) {
772
+ return this.blockSource.getCheckpointsDataForEpoch(epochNumber);
773
+ }
774
+
626
775
  public async getCurrentMinFees(): Promise<GasFees> {
627
- return await this.globalVariableBuilder.getCurrentMinFees();
776
+ return await this.feeProvider.getCurrentMinFees();
777
+ }
778
+
779
+ /** Returns predicted min fees for the current slot and next N slots. */
780
+ public async getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]> {
781
+ return await this.feeProvider.getPredictedMinFees(manaUsage);
628
782
  }
629
783
 
630
784
  public async getMaxPriorityFees(): Promise<GasFees> {
@@ -651,6 +805,10 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
651
805
  return await this.blockSource.getCheckpointedL2BlockNumber();
652
806
  }
653
807
 
808
+ public getCheckpointNumber(): Promise<CheckpointNumber> {
809
+ return this.blockSource.getCheckpointNumber();
810
+ }
811
+
654
812
  /**
655
813
  * Method to fetch the version of the package.
656
814
  * @returns The node package version
@@ -688,18 +846,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
688
846
  page?: number,
689
847
  referenceBlock?: BlockHash,
690
848
  ): Promise<TxScopedL2Log[][]> {
849
+ let upToBlockNumber: BlockNumber | undefined;
691
850
  if (referenceBlock) {
692
851
  const initialBlockHash = await this.#getInitialHeaderHash();
693
- if (!referenceBlock.equals(initialBlockHash)) {
852
+ if (referenceBlock.equals(initialBlockHash)) {
853
+ upToBlockNumber = BlockNumber(0);
854
+ } else {
694
855
  const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
695
856
  if (!header) {
696
857
  throw new Error(
697
858
  `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
698
859
  );
699
860
  }
861
+ upToBlockNumber = header.globalVariables.blockNumber;
700
862
  }
701
863
  }
702
- return this.logsSource.getPrivateLogsByTags(tags, page);
864
+ return this.logsSource.getPrivateLogsByTags(tags, page, upToBlockNumber);
703
865
  }
704
866
 
705
867
  public async getPublicLogsByTagsFromContract(
@@ -708,18 +870,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
708
870
  page?: number,
709
871
  referenceBlock?: BlockHash,
710
872
  ): Promise<TxScopedL2Log[][]> {
873
+ let upToBlockNumber: BlockNumber | undefined;
711
874
  if (referenceBlock) {
712
875
  const initialBlockHash = await this.#getInitialHeaderHash();
713
- if (!referenceBlock.equals(initialBlockHash)) {
876
+ if (referenceBlock.equals(initialBlockHash)) {
877
+ upToBlockNumber = BlockNumber(0);
878
+ } else {
714
879
  const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
715
880
  if (!header) {
716
881
  throw new Error(
717
882
  `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
718
883
  );
719
884
  }
885
+ upToBlockNumber = header.globalVariables.blockNumber;
720
886
  }
721
887
  }
722
- return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags, page);
888
+ return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber);
723
889
  }
724
890
 
725
891
  /**
@@ -761,8 +927,9 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
761
927
  }
762
928
 
763
929
  await this.p2pClient!.sendTx(tx);
764
- this.metrics.receivedTx(timer.ms(), true);
765
- this.log.info(`Received tx ${txHash}`, { txHash });
930
+ const duration = timer.ms();
931
+ this.metrics.receivedTx(duration, true);
932
+ this.log.info(`Received tx ${txHash} in ${duration}ms`, { txHash });
766
933
  }
767
934
 
768
935
  public async getTxReceipt(txHash: TxHash): Promise<TxReceipt> {
@@ -774,18 +941,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
774
941
  // Then get the actual tx from the archiver, which tracks every tx in a mined block.
775
942
  const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
776
943
 
944
+ let receipt: TxReceipt;
777
945
  if (settledTxReceipt) {
778
- // If the archiver has the receipt then return it.
779
- return settledTxReceipt;
946
+ receipt = settledTxReceipt;
780
947
  } else if (isKnownToPool) {
781
948
  // If the tx is in the pool but not in the archiver, it's pending.
782
949
  // This handles race conditions between archiver and p2p, where the archiver
783
950
  // has pruned the block in which a tx was mined, but p2p has not caught up yet.
784
- return new TxReceipt(txHash, TxStatus.PENDING, undefined, undefined);
951
+ receipt = new TxReceipt(txHash, TxStatus.PENDING, undefined, undefined);
785
952
  } else {
786
953
  // Otherwise, if we don't know the tx, we consider it dropped.
787
- return new TxReceipt(txHash, TxStatus.DROPPED, undefined, 'Tx dropped by P2P node');
954
+ receipt = new TxReceipt(txHash, TxStatus.DROPPED, undefined, 'Tx dropped by P2P node');
788
955
  }
956
+
957
+ this.debugLogStore.decorateReceiptWithLogs(txHash.toString(), receipt);
958
+
959
+ return receipt;
789
960
  }
790
961
 
791
962
  public getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
@@ -800,8 +971,9 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
800
971
  await tryStop(this.validatorsSentinel);
801
972
  await tryStop(this.epochPruneWatcher);
802
973
  await tryStop(this.slasherClient);
803
- await tryStop(this.proofVerifier);
974
+ await Promise.all([tryStop(this.peerProofVerifier), tryStop(this.rpcProofVerifier)]);
804
975
  await tryStop(this.sequencer);
976
+ await tryStop(this.proverNode);
805
977
  await tryStop(this.p2pClient);
806
978
  await tryStop(this.worldStateSynchronizer);
807
979
  await tryStop(this.blockSource);
@@ -855,59 +1027,65 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
855
1027
  treeId: MerkleTreeId,
856
1028
  leafValues: Fr[],
857
1029
  ): Promise<(DataInBlock<bigint> | undefined)[]> {
858
- const committedDb = await this.#getWorldState(referenceBlock);
1030
+ const committedDb = await this.getWorldState(referenceBlock);
859
1031
  const maybeIndices = await committedDb.findLeafIndices(
860
1032
  treeId,
861
1033
  leafValues.map(x => x.toBuffer()),
862
1034
  );
863
- // We filter out undefined values
864
- const indices = maybeIndices.filter(x => x !== undefined) as bigint[];
1035
+ // Filter out undefined values to query block numbers only for found leaves
1036
+ const definedIndices = maybeIndices.filter(x => x !== undefined);
865
1037
 
866
- // Now we find the block numbers for the indices
867
- const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, indices);
1038
+ // Now we find the block numbers for the defined indices
1039
+ const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, definedIndices);
868
1040
 
869
- // If any of the block numbers are undefined, we throw an error.
870
- for (let i = 0; i < indices.length; i++) {
871
- if (blockNumbers[i] === undefined) {
872
- throw new Error(`Block number is undefined for leaf index ${indices[i]} in tree ${MerkleTreeId[treeId]}`);
1041
+ // Build a map from leaf index to block number
1042
+ const indexToBlockNumber = new Map<bigint, BlockNumber>();
1043
+ for (let i = 0; i < definedIndices.length; i++) {
1044
+ const blockNumber = blockNumbers[i];
1045
+ if (blockNumber === undefined) {
1046
+ throw new Error(
1047
+ `Block number is undefined for leaf index ${definedIndices[i]} in tree ${MerkleTreeId[treeId]}`,
1048
+ );
873
1049
  }
1050
+ indexToBlockNumber.set(definedIndices[i], blockNumber);
874
1051
  }
875
1052
 
876
1053
  // Get unique block numbers in order to optimize num calls to getLeafValue function.
877
- const uniqueBlockNumbers = [...new Set(blockNumbers.filter(x => x !== undefined))];
1054
+ const uniqueBlockNumbers = [...new Set(indexToBlockNumber.values())];
878
1055
 
879
- // Now we obtain the block hashes from the archive tree by calling await `committedDb.getLeafValue(treeId, index)`
880
- // (note that block number corresponds to the leaf index in the archive tree).
1056
+ // Now we obtain the block hashes from the archive tree (block number = leaf index in archive tree).
881
1057
  const blockHashes = await Promise.all(
882
1058
  uniqueBlockNumbers.map(blockNumber => {
883
1059
  return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
884
1060
  }),
885
1061
  );
886
1062
 
887
- // If any of the block hashes are undefined, we throw an error.
1063
+ // Build a map from block number to block hash
1064
+ const blockNumberToHash = new Map<BlockNumber, BlockHash>();
888
1065
  for (let i = 0; i < uniqueBlockNumbers.length; i++) {
889
- if (blockHashes[i] === undefined) {
1066
+ const blockHash = blockHashes[i];
1067
+ if (blockHash === undefined) {
890
1068
  throw new Error(`Block hash is undefined for block number ${uniqueBlockNumbers[i]}`);
891
1069
  }
1070
+ blockNumberToHash.set(uniqueBlockNumbers[i], blockHash);
892
1071
  }
893
1072
 
894
1073
  // Create DataInBlock objects by combining indices, blockNumbers and blockHashes and return them.
895
- return maybeIndices.map((index, i) => {
1074
+ return maybeIndices.map(index => {
896
1075
  if (index === undefined) {
897
1076
  return undefined;
898
1077
  }
899
- const blockNumber = blockNumbers[i];
1078
+ const blockNumber = indexToBlockNumber.get(index);
900
1079
  if (blockNumber === undefined) {
901
- return undefined;
1080
+ throw new Error(`Block number not found for leaf index ${index} in tree ${MerkleTreeId[treeId]}`);
902
1081
  }
903
- const blockHashIndex = uniqueBlockNumbers.indexOf(blockNumber);
904
- const blockHash = blockHashes[blockHashIndex];
905
- if (!blockHash) {
906
- return undefined;
1082
+ const l2BlockHash = blockNumberToHash.get(blockNumber);
1083
+ if (l2BlockHash === undefined) {
1084
+ throw new Error(`Block hash not found for block number ${blockNumber}`);
907
1085
  }
908
1086
  return {
909
- l2BlockNumber: BlockNumber(Number(blockNumber)),
910
- l2BlockHash: new BlockHash(blockHash),
1087
+ l2BlockNumber: blockNumber,
1088
+ l2BlockHash,
911
1089
  data: index,
912
1090
  };
913
1091
  });
@@ -917,7 +1095,15 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
917
1095
  referenceBlock: BlockParameter,
918
1096
  blockHash: BlockHash,
919
1097
  ): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
920
- const committedDb = await this.#getWorldState(referenceBlock);
1098
+ // The Noir circuit checks the archive membership proof against `anchor_block_header.last_archive.root`,
1099
+ // which is the archive tree root BEFORE the anchor block was added (i.e. the state after block N-1).
1100
+ // So we need the world state at block N-1, not block N, to produce a sibling path matching that root.
1101
+ const referenceBlockNumber = await this.resolveBlockNumber(referenceBlock);
1102
+ if (referenceBlockNumber === BlockNumber.ZERO) {
1103
+ // Block 0 (the initial block) has an empty archive, so no membership witness can exist.
1104
+ return undefined;
1105
+ }
1106
+ const committedDb = await this.getWorldState(BlockNumber(referenceBlockNumber - 1));
921
1107
  const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [blockHash]);
922
1108
  return pathAndIndex === undefined
923
1109
  ? undefined
@@ -928,7 +1114,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
928
1114
  referenceBlock: BlockParameter,
929
1115
  noteHash: Fr,
930
1116
  ): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
931
- const committedDb = await this.#getWorldState(referenceBlock);
1117
+ const committedDb = await this.getWorldState(referenceBlock);
932
1118
  const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.NOTE_HASH_TREE>(
933
1119
  MerkleTreeId.NOTE_HASH_TREE,
934
1120
  [noteHash],
@@ -942,7 +1128,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
942
1128
  referenceBlock: BlockParameter,
943
1129
  l1ToL2Message: Fr,
944
1130
  ): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined> {
945
- const db = await this.#getWorldState(referenceBlock);
1131
+ const db = await this.getWorldState(referenceBlock);
946
1132
  const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [l1ToL2Message]);
947
1133
  if (!witness) {
948
1134
  return undefined;
@@ -952,11 +1138,9 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
952
1138
  return [witness.index, witness.path];
953
1139
  }
954
1140
 
955
- public async getL1ToL2MessageBlock(l1ToL2Message: Fr): Promise<BlockNumber | undefined> {
1141
+ public async getL1ToL2MessageCheckpoint(l1ToL2Message: Fr): Promise<CheckpointNumber | undefined> {
956
1142
  const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
957
- return messageIndex
958
- ? BlockNumber.fromCheckpointNumber(InboxLeaf.checkpointNumberFromIndex(messageIndex))
959
- : undefined;
1143
+ return messageIndex ? InboxLeaf.checkpointNumberFromIndex(messageIndex) : undefined;
960
1144
  }
961
1145
 
962
1146
  /**
@@ -977,19 +1161,9 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
977
1161
  public async getL2ToL1Messages(epoch: EpochNumber): Promise<Fr[][][][]> {
978
1162
  // Assumes `getCheckpointedBlocksForEpoch` returns blocks in ascending order of block number.
979
1163
  const checkpointedBlocks = await this.blockSource.getCheckpointedBlocksForEpoch(epoch);
980
- const blocksInCheckpoints: L2Block[][] = [];
981
- let previousSlotNumber = SlotNumber.ZERO;
982
- let checkpointIndex = -1;
983
- for (const checkpointedBlock of checkpointedBlocks) {
984
- const block = checkpointedBlock.block;
985
- const slotNumber = block.header.globalVariables.slotNumber;
986
- if (slotNumber !== previousSlotNumber) {
987
- checkpointIndex++;
988
- blocksInCheckpoints.push([]);
989
- previousSlotNumber = slotNumber;
990
- }
991
- blocksInCheckpoints[checkpointIndex].push(block);
992
- }
1164
+ const blocksInCheckpoints = chunkBy(checkpointedBlocks, cb => cb.block.header.globalVariables.slotNumber).map(
1165
+ group => group.map(cb => cb.block),
1166
+ );
993
1167
  return blocksInCheckpoints.map(blocks =>
994
1168
  blocks.map(block => block.body.txEffects.map(txEffect => txEffect.l2ToL1Msgs)),
995
1169
  );
@@ -999,7 +1173,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
999
1173
  referenceBlock: BlockParameter,
1000
1174
  nullifier: Fr,
1001
1175
  ): Promise<NullifierMembershipWitness | undefined> {
1002
- const db = await this.#getWorldState(referenceBlock);
1176
+ const db = await this.getWorldState(referenceBlock);
1003
1177
  const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]);
1004
1178
  if (!witness) {
1005
1179
  return undefined;
@@ -1014,33 +1188,20 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1014
1188
  return new NullifierMembershipWitness(index, leafPreimage as NullifierLeafPreimage, path);
1015
1189
  }
1016
1190
 
1017
- /**
1018
- * Returns a low nullifier membership witness for a given nullifier at a given block.
1019
- * @param referenceBlock - The block parameter (block number, block hash, or 'latest') at which to get the data
1020
- * (which contains the root of the nullifier tree in which we are searching for the nullifier).
1021
- * @param nullifier - Nullifier we try to find the low nullifier witness for.
1022
- * @returns The low nullifier membership witness (if found).
1023
- * @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
1024
- * list structure" of leaves and proving that a lower nullifier is pointing to a bigger next value than the nullifier
1025
- * we are trying to prove non-inclusion for.
1026
- *
1027
- * Note: This function returns the membership witness of the nullifier itself and not the low nullifier when
1028
- * the nullifier already exists in the tree. This is because the `getPreviousValueIndex` function returns the
1029
- * index of the nullifier itself when it already exists in the tree.
1030
- * TODO: This is a confusing behavior and we should eventually address that.
1031
- */
1032
1191
  public async getLowNullifierMembershipWitness(
1033
1192
  referenceBlock: BlockParameter,
1034
1193
  nullifier: Fr,
1035
1194
  ): Promise<NullifierMembershipWitness | undefined> {
1036
- const committedDb = await this.#getWorldState(referenceBlock);
1195
+ const committedDb = await this.getWorldState(referenceBlock);
1037
1196
  const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
1038
1197
  if (!findResult) {
1039
1198
  return undefined;
1040
1199
  }
1041
1200
  const { index, alreadyPresent } = findResult;
1042
1201
  if (alreadyPresent) {
1043
- this.log.warn(`Nullifier ${nullifier.toBigInt()} already exists in the tree`);
1202
+ throw new Error(
1203
+ `Cannot prove nullifier non-inclusion: nullifier ${nullifier.toBigInt()} already exists in the tree`,
1204
+ );
1044
1205
  }
1045
1206
  const preimageData = (await committedDb.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index))!;
1046
1207
 
@@ -1049,7 +1210,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1049
1210
  }
1050
1211
 
1051
1212
  async getPublicDataWitness(referenceBlock: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1052
- const committedDb = await this.#getWorldState(referenceBlock);
1213
+ const committedDb = await this.getWorldState(referenceBlock);
1053
1214
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
1054
1215
  if (!lowLeafResult) {
1055
1216
  return undefined;
@@ -1064,7 +1225,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1064
1225
  }
1065
1226
 
1066
1227
  public async getPublicStorageAt(referenceBlock: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
1067
- const committedDb = await this.#getWorldState(referenceBlock);
1228
+ const committedDb = await this.getWorldState(referenceBlock);
1068
1229
  const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
1069
1230
 
1070
1231
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
@@ -1105,6 +1266,14 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1105
1266
  return await this.blockSource.getBlockHeaderByArchive(archive);
1106
1267
  }
1107
1268
 
1269
+ public getBlockData(number: BlockNumber): Promise<BlockData | undefined> {
1270
+ return this.blockSource.getBlockData(number);
1271
+ }
1272
+
1273
+ public getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
1274
+ return this.blockSource.getBlockDataByArchive(archive);
1275
+ }
1276
+
1108
1277
  /**
1109
1278
  * Simulates the public part of a transaction with the current state.
1110
1279
  * @param tx - The transaction to simulate.
@@ -1128,7 +1297,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1128
1297
  }
1129
1298
 
1130
1299
  const txHash = tx.getTxHash();
1131
- const blockNumber = BlockNumber((await this.blockSource.getBlockNumber()) + 1);
1300
+ const latestBlockNumber = await this.blockSource.getBlockNumber();
1301
+ const blockNumber = BlockNumber.add(latestBlockNumber, 1);
1132
1302
 
1133
1303
  // If sequencer is not initialized, we just set these values to zero for simulation.
1134
1304
  const coinbase = EthAddress.ZERO;
@@ -1152,6 +1322,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1152
1322
  blockNumber,
1153
1323
  });
1154
1324
 
1325
+ // Ensure world-state has caught up with the latest block we loaded from the archiver
1326
+ await this.worldStateSynchronizer.syncImmediate(latestBlockNumber);
1155
1327
  const merkleTreeFork = await this.worldStateSynchronizer.fork();
1156
1328
  try {
1157
1329
  const config = PublicSimulatorConfig.from({
@@ -1167,7 +1339,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1167
1339
  const processor = publicProcessorFactory.create(merkleTreeFork, newGlobalVariables, config);
1168
1340
 
1169
1341
  // REFACTOR: Consider merging ProcessReturnValues into ProcessedTx
1170
- const [processedTxs, failedTxs, _usedTxs, returns] = await processor.process([tx]);
1342
+ const [processedTxs, failedTxs, _usedTxs, returns, debugLogs] = await processor.process([tx]);
1171
1343
  // REFACTOR: Consider returning the error rather than throwing
1172
1344
  if (failedTxs.length) {
1173
1345
  this.log.warn(`Simulated tx ${txHash} fails: ${failedTxs[0].error}`, { txHash });
@@ -1181,6 +1353,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1181
1353
  processedTx.txEffect,
1182
1354
  returns,
1183
1355
  processedTx.gasUsed,
1356
+ debugLogs,
1184
1357
  );
1185
1358
  } finally {
1186
1359
  await merkleTreeFork.close();
@@ -1192,12 +1365,13 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1192
1365
  { isSimulation, skipFeeEnforcement }: { isSimulation?: boolean; skipFeeEnforcement?: boolean } = {},
1193
1366
  ): Promise<TxValidationResult> {
1194
1367
  const db = this.worldStateSynchronizer.getCommitted();
1195
- const verifier = isSimulation ? undefined : this.proofVerifier;
1368
+ const verifier = isSimulation ? undefined : this.rpcProofVerifier;
1196
1369
 
1197
- // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1370
+ // We accept transactions if they are not expired by the next slot (checked based on the ExpirationTimestamp field)
1198
1371
  const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1199
1372
  const blockNumber = BlockNumber((await this.blockSource.getBlockNumber()) + 1);
1200
- const validator = createValidatorForAcceptingTxs(
1373
+ const l1Constants = await this.blockSource.getL1Constants();
1374
+ const validator = createTxValidatorForAcceptingTxsOverRPC(
1201
1375
  db,
1202
1376
  this.contractDataSource,
1203
1377
  verifier,
@@ -1206,10 +1380,16 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1206
1380
  blockNumber,
1207
1381
  l1ChainId: this.l1ChainId,
1208
1382
  rollupVersion: this.version,
1209
- setupAllowList: this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions()),
1383
+ setupAllowList: [
1384
+ ...(await getDefaultAllowedSetupFunctions()),
1385
+ ...(this.config.txPublicSetupAllowListExtend ?? []),
1386
+ ],
1210
1387
  gasFees: await this.getCurrentMinFees(),
1211
1388
  skipFeeEnforcement,
1212
1389
  txsPermitted: !this.config.disableTransactions,
1390
+ rollupManaLimit: l1Constants.rollupManaLimit,
1391
+ maxBlockL2Gas: this.config.validateMaxL2BlockGas,
1392
+ maxBlockDAGas: this.config.validateMaxDABlockGas,
1213
1393
  },
1214
1394
  this.log.getBindings(),
1215
1395
  );
@@ -1234,7 +1414,15 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1234
1414
  archiver.updateConfig(config);
1235
1415
  }
1236
1416
  if (newConfig.realProofs !== this.config.realProofs) {
1237
- this.proofVerifier = config.realProofs ? await BBCircuitVerifier.new(newConfig) : new TestCircuitVerifier();
1417
+ await Promise.all([tryStop(this.peerProofVerifier), tryStop(this.rpcProofVerifier)]);
1418
+ if (newConfig.realProofs) {
1419
+ this.peerProofVerifier = await BatchChonkVerifier.new(newConfig, newConfig.bbChonkVerifyMaxBatch, 'peer');
1420
+ const rpcVerifier = await BBCircuitVerifier.new(newConfig);
1421
+ this.rpcProofVerifier = new QueuedIVCVerifier(rpcVerifier, newConfig.numConcurrentIVCVerifiers);
1422
+ } else {
1423
+ this.peerProofVerifier = new TestCircuitVerifier();
1424
+ this.rpcProofVerifier = new TestCircuitVerifier();
1425
+ }
1238
1426
  }
1239
1427
 
1240
1428
  this.config = newConfig;
@@ -1309,7 +1497,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1309
1497
  return Promise.resolve();
1310
1498
  }
1311
1499
 
1312
- public async rollbackTo(targetBlock: BlockNumber, force?: boolean): Promise<void> {
1500
+ public async rollbackTo(targetBlock: BlockNumber, force?: boolean, resumeSync = true): Promise<void> {
1313
1501
  const archiver = this.blockSource as Archiver;
1314
1502
  if (!('rollbackTo' in archiver)) {
1315
1503
  throw new Error('Archiver implementation does not support rollbacks.');
@@ -1339,9 +1527,13 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1339
1527
  this.log.error(`Error during rollback`, err);
1340
1528
  throw err;
1341
1529
  } finally {
1342
- this.log.info(`Resuming world state and archiver sync.`);
1343
- this.worldStateSynchronizer.resumeSync();
1344
- archiver.resume();
1530
+ if (resumeSync) {
1531
+ this.log.info(`Resuming world state and archiver sync.`);
1532
+ this.worldStateSynchronizer.resumeSync();
1533
+ archiver.resume();
1534
+ } else {
1535
+ this.log.info(`Sync left paused after rollback (resumeSync=false).`);
1536
+ }
1345
1537
  }
1346
1538
  }
1347
1539
 
@@ -1358,24 +1550,139 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1358
1550
  return Promise.resolve();
1359
1551
  }
1360
1552
 
1361
- public getSlashPayloads(): Promise<SlashPayloadRound[]> {
1362
- if (!this.slasherClient) {
1363
- throw new Error(`Slasher client not enabled`);
1364
- }
1365
- return this.slasherClient.getSlashPayloads();
1366
- }
1367
-
1368
1553
  public getSlashOffenses(round: bigint | 'all' | 'current'): Promise<Offense[]> {
1369
1554
  if (!this.slasherClient) {
1370
1555
  throw new Error(`Slasher client not enabled`);
1371
1556
  }
1372
1557
  if (round === 'all') {
1373
- return this.slasherClient.getPendingOffenses();
1558
+ return this.slasherClient.getOffenses();
1374
1559
  } else {
1375
1560
  return this.slasherClient.gatherOffensesForRound(round === 'current' ? undefined : BigInt(round));
1376
1561
  }
1377
1562
  }
1378
1563
 
1564
+ public async reloadKeystore(): Promise<void> {
1565
+ if (!this.config.keyStoreDirectory?.length) {
1566
+ throw new BadRequestError(
1567
+ 'Cannot reload keystore: node is not using a file-based keystore. ' +
1568
+ 'Set KEY_STORE_DIRECTORY to use file-based keystores.',
1569
+ );
1570
+ }
1571
+ if (!this.validatorClient) {
1572
+ throw new BadRequestError('Cannot reload keystore: validator is not enabled.');
1573
+ }
1574
+
1575
+ this.log.info('Reloading keystore from disk');
1576
+
1577
+ // Re-read and validate keystore files
1578
+ const keyStores = loadKeystores(this.config.keyStoreDirectory);
1579
+ const newManager = new KeystoreManager(mergeKeystores(keyStores));
1580
+ await newManager.validateSigners();
1581
+ ValidatorClient.validateKeyStoreConfiguration(newManager, this.log);
1582
+
1583
+ // Validate that every validator's publisher keys overlap with the L1 signers
1584
+ // that were initialized at startup. Publishers cannot be hot-reloaded, so a
1585
+ // validator with a publisher key that doesn't match any existing L1 signer
1586
+ // would silently fail on every proposer slot.
1587
+ if (this.keyStoreManager && this.sequencer) {
1588
+ const oldAdapter = NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager);
1589
+ const availablePublishers = new Set(
1590
+ oldAdapter
1591
+ .getAttesterAddresses()
1592
+ .flatMap(a => oldAdapter.getPublisherAddresses(a).map(p => p.toString().toLowerCase())),
1593
+ );
1594
+
1595
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1596
+ for (const attester of newAdapter.getAttesterAddresses()) {
1597
+ const pubs = newAdapter.getPublisherAddresses(attester);
1598
+ if (pubs.length > 0 && !pubs.some(p => availablePublishers.has(p.toString().toLowerCase()))) {
1599
+ throw new BadRequestError(
1600
+ `Cannot reload keystore: validator ${attester} has publisher keys ` +
1601
+ `[${pubs.map(p => p.toString()).join(', ')}] but none match the L1 signers initialized at startup ` +
1602
+ `[${[...availablePublishers].join(', ')}]. Publishers cannot be hot-reloaded — ` +
1603
+ `use an existing publisher key or restart the node.`,
1604
+ );
1605
+ }
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
1613
+ ? NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager).getAttesterAddresses()
1614
+ : [];
1615
+
1616
+ const oldSet = new Set(oldAddresses.map(a => a.toString()));
1617
+ const newSet = new Set(newAddresses.map(a => a.toString()));
1618
+ const added = newAddresses.filter(a => !oldSet.has(a.toString()));
1619
+ const removed = oldAddresses.filter(a => !newSet.has(a.toString()));
1620
+
1621
+ if (added.length > 0) {
1622
+ this.log.info(`Keystore reload: adding attester keys: ${added.map(a => a.toString()).join(', ')}`);
1623
+ }
1624
+ if (removed.length > 0) {
1625
+ this.log.info(`Keystore reload: removing attester keys: ${removed.map(a => a.toString()).join(', ')}`);
1626
+ }
1627
+ if (added.length === 0 && removed.length === 0) {
1628
+ this.log.info('Keystore reload: attester keys unchanged');
1629
+ }
1630
+
1631
+ // Update the validator client (coinbase, feeRecipient, attester keys)
1632
+ this.validatorClient.reloadKeystore(newManager);
1633
+
1634
+ // Update the publisher factory's keystore so newly-added validators
1635
+ // can be matched to existing publisher keys when proposing blocks.
1636
+ if (this.sequencer) {
1637
+ this.sequencer.updatePublisherNodeKeyStore(newAdapter);
1638
+ }
1639
+
1640
+ // Update slasher's "don't-slash-self" list with new validator addresses
1641
+ if (this.slasherClient && !this.config.slashSelfAllowed) {
1642
+ const slashValidatorsNever = unique(
1643
+ [...(this.config.slashValidatorsNever ?? []), ...newAddresses].map(a => a.toString()),
1644
+ ).map(EthAddress.fromString);
1645
+ this.slasherClient.updateConfig({ slashValidatorsNever });
1646
+ }
1647
+
1648
+ this.keyStoreManager = newManager;
1649
+ this.log.info('Keystore reloaded: coinbase, feeRecipient, and attester keys updated');
1650
+ }
1651
+
1652
+ public async mineBlock(): Promise<void> {
1653
+ if (!this.sequencer) {
1654
+ throw new BadRequestError('Cannot mine block: no sequencer is running');
1655
+ }
1656
+
1657
+ const currentBlockNumber = await this.getBlockNumber();
1658
+
1659
+ // Use slot duration + 50% buffer as the timeout so this works on running networks too
1660
+ const { slotDuration } = await this.blockSource.getL1Constants();
1661
+ const timeoutSeconds = Math.ceil(slotDuration * 1.5);
1662
+
1663
+ // Temporarily set minTxsPerBlock to 0 so the sequencer produces a block even with no txs
1664
+ const originalMinTxsPerBlock = this.sequencer.getSequencer().getConfig().minTxsPerBlock;
1665
+ this.sequencer.updateConfig({ minTxsPerBlock: 0 });
1666
+
1667
+ try {
1668
+ // Trigger the sequencer to produce a block immediately
1669
+ void this.sequencer.trigger();
1670
+
1671
+ // Wait for the new L2 block to appear
1672
+ await retryUntil(
1673
+ async () => {
1674
+ const newBlockNumber = await this.getBlockNumber();
1675
+ return newBlockNumber > currentBlockNumber ? true : undefined;
1676
+ },
1677
+ 'mineBlock',
1678
+ timeoutSeconds,
1679
+ 0.1,
1680
+ );
1681
+ } finally {
1682
+ this.sequencer.updateConfig({ minTxsPerBlock: originalMinTxsPerBlock });
1683
+ }
1684
+ }
1685
+
1379
1686
  #getInitialHeaderHash(): Promise<BlockHash> {
1380
1687
  if (!this.initialHeaderHashPromise) {
1381
1688
  this.initialHeaderHashPromise = this.worldStateSynchronizer.getCommitted().getInitialHeader().hash();
@@ -1388,7 +1695,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1388
1695
  * @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
1389
1696
  * @returns An instance of a committed MerkleTreeOperations
1390
1697
  */
1391
- async #getWorldState(block: BlockParameter) {
1698
+ protected async getWorldState(block: BlockParameter) {
1392
1699
  let blockSyncedTo: BlockNumber = BlockNumber.ZERO;
1393
1700
  try {
1394
1701
  // Attempt to sync the world state if necessary
@@ -1402,6 +1709,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1402
1709
  return this.worldStateSynchronizer.getCommitted();
1403
1710
  }
1404
1711
 
1712
+ // Get the block number, either directly from the parameter or by quering the archiver with the block hash
1713
+ let blockNumber: BlockNumber;
1405
1714
  if (BlockHash.isBlockHash(block)) {
1406
1715
  const initialBlockHash = await this.#getInitialHeaderHash();
1407
1716
  if (block.equals(initialBlockHash)) {
@@ -1415,22 +1724,50 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1415
1724
  `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.`,
1416
1725
  );
1417
1726
  }
1418
- const blockNumber = header.getBlockNumber();
1419
- this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1420
- return this.worldStateSynchronizer.getSnapshot(blockNumber);
1727
+
1728
+ blockNumber = header.getBlockNumber();
1729
+ } else {
1730
+ blockNumber = block as BlockNumber;
1731
+ }
1732
+
1733
+ // Check it's within world state sync range
1734
+ if (blockNumber > blockSyncedTo) {
1735
+ throw new Error(`Queried block ${block} not yet synced by the node (node is synced upto ${blockSyncedTo}).`);
1421
1736
  }
1737
+ this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1422
1738
 
1423
- // Block number provided
1424
- {
1425
- const blockNumber = block as BlockNumber;
1739
+ const snapshot = this.worldStateSynchronizer.getSnapshot(blockNumber);
1426
1740
 
1427
- if (blockNumber > blockSyncedTo) {
1428
- throw new Error(`Queried block ${block} not yet synced by the node (node is synced upto ${blockSyncedTo}).`);
1741
+ // Double-check world-state synced to the same block hash as was requested
1742
+ if (BlockHash.isBlockHash(block)) {
1743
+ const blockHash = await snapshot.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
1744
+ if (!blockHash || !block.equals(blockHash)) {
1745
+ throw new Error(
1746
+ `Block hash ${block.toString()} not found in world state at block number ${blockNumber}. If the node API has been queried with anchor block hash possibly a reorg has occurred.`,
1747
+ );
1429
1748
  }
1749
+ }
1750
+
1751
+ return snapshot;
1752
+ }
1430
1753
 
1431
- this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1432
- return this.worldStateSynchronizer.getSnapshot(blockNumber);
1754
+ /** Resolves a block parameter to a block number. */
1755
+ protected async resolveBlockNumber(block: BlockParameter): Promise<BlockNumber> {
1756
+ if (block === 'latest') {
1757
+ return BlockNumber(await this.blockSource.getBlockNumber());
1758
+ }
1759
+ if (BlockHash.isBlockHash(block)) {
1760
+ const initialBlockHash = await this.#getInitialHeaderHash();
1761
+ if (block.equals(initialBlockHash)) {
1762
+ return BlockNumber.ZERO;
1763
+ }
1764
+ const header = await this.blockSource.getBlockHeaderByHash(block);
1765
+ if (!header) {
1766
+ throw new Error(`Block hash ${block.toString()} not found.`);
1767
+ }
1768
+ return header.getBlockNumber();
1433
1769
  }
1770
+ return block as BlockNumber;
1434
1771
  }
1435
1772
 
1436
1773
  /**