@aztec/aztec-node 0.0.1-commit.5daedc8 → 0.0.1-commit.5de5ca79e

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,43 +1,36 @@
1
1
  import { Archiver, createArchiver } from '@aztec/archiver';
2
2
  import { BBCircuitVerifier, QueuedIVCVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
3
- import { type BlobSinkClientInterface, createBlobSinkClient } from '@aztec/blob-sink/client';
4
- import {
5
- ARCHIVE_HEIGHT,
6
- INITIAL_L2_BLOCK_NUM,
7
- type L1_TO_L2_MSG_TREE_HEIGHT,
8
- type NOTE_HASH_TREE_HEIGHT,
9
- type NULLIFIER_TREE_HEIGHT,
10
- type PUBLIC_DATA_TREE_HEIGHT,
11
- } from '@aztec/constants';
3
+ import { type BlobClientInterface, createBlobClientWithFileStores } from '@aztec/blob-client/client';
4
+ import { Blob } from '@aztec/blob-lib';
5
+ import { ARCHIVE_HEIGHT, type L1_TO_L2_MSG_TREE_HEIGHT, type NOTE_HASH_TREE_HEIGHT } from '@aztec/constants';
12
6
  import { EpochCache, type EpochCacheInterface } from '@aztec/epoch-cache';
13
- import {
14
- type L1ContractAddresses,
15
- RegistryContract,
16
- RollupContract,
17
- createEthereumChain,
18
- getPublicClient,
19
- } from '@aztec/ethereum';
20
- import { SlotNumber } from '@aztec/foundation/branded-types';
21
- import { compactArray, pick } from '@aztec/foundation/collection';
7
+ import { createEthereumChain } from '@aztec/ethereum/chain';
8
+ import { getPublicClient, makeL1HttpTransport } from '@aztec/ethereum/client';
9
+ import { RegistryContract, RollupContract } from '@aztec/ethereum/contracts';
10
+ import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
11
+ import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
12
+ import { chunkBy, compactArray, pick, unique } from '@aztec/foundation/collection';
13
+ import { Fr } from '@aztec/foundation/curves/bn254';
22
14
  import { EthAddress } from '@aztec/foundation/eth-address';
23
- import { Fr } from '@aztec/foundation/fields';
24
15
  import { BadRequestError } from '@aztec/foundation/json-rpc';
25
16
  import { type Logger, createLogger } from '@aztec/foundation/log';
26
17
  import { count } from '@aztec/foundation/string';
27
18
  import { DateProvider, Timer } from '@aztec/foundation/timer';
28
19
  import { MembershipWitness, SiblingPath } from '@aztec/foundation/trees';
29
- import { KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
20
+ import { type KeyStore, KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
30
21
  import { trySnapshotSync, uploadSnapshot } from '@aztec/node-lib/actions';
31
- import { createL1TxUtilsWithBlobsFromEthSigner } from '@aztec/node-lib/factories';
32
- import { type P2P, type P2PClientDeps, createP2PClient, getDefaultAllowedSetupFunctions } from '@aztec/p2p';
33
- import { ProtocolContractAddress } from '@aztec/protocol-contracts';
22
+ import { createForwarderL1TxUtilsFromSigners, createL1TxUtilsFromSigners } from '@aztec/node-lib/factories';
34
23
  import {
35
- BlockBuilder,
36
- GlobalVariableBuilder,
37
- SequencerClient,
38
- type SequencerPublisher,
39
- createValidatorForAcceptingTxs,
40
- } from '@aztec/sequencer-client';
24
+ type P2P,
25
+ type P2PClientDeps,
26
+ createP2PClient,
27
+ createTxValidatorForAcceptingTxsOverRPC,
28
+ getDefaultAllowedSetupFunctions,
29
+ } from '@aztec/p2p';
30
+ import { ProtocolContractAddress } from '@aztec/protocol-contracts';
31
+ import { type ProverNode, type ProverNodeDeps, createProverNode } from '@aztec/prover-node';
32
+ import { createKeyStoreForProver } from '@aztec/prover-node/config';
33
+ import { GlobalVariableBuilder, SequencerClient, type SequencerPublisher } from '@aztec/sequencer-client';
41
34
  import { PublicProcessorFactory } from '@aztec/simulator/server';
42
35
  import {
43
36
  AttestationsBlockWatcher,
@@ -46,16 +39,17 @@ import {
46
39
  type Watcher,
47
40
  createSlasher,
48
41
  } from '@aztec/slasher';
49
- import { PublicSimulatorConfig } from '@aztec/stdlib/avm';
42
+ import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
50
43
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
51
44
  import {
52
- type InBlock,
53
- type L2Block,
54
- L2BlockHash,
55
- type L2BlockNumber,
45
+ type BlockData,
46
+ BlockHash,
47
+ type BlockParameter,
48
+ type DataInBlock,
49
+ L2Block,
56
50
  type L2BlockSource,
57
- type PublishedL2Block,
58
51
  } from '@aztec/stdlib/block';
52
+ import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
59
53
  import type {
60
54
  ContractClassPublic,
61
55
  ContractDataSource,
@@ -82,9 +76,9 @@ import {
82
76
  type WorldStateSynchronizer,
83
77
  tryStop,
84
78
  } from '@aztec/stdlib/interfaces/server';
85
- import type { LogFilter, PrivateLog, TxScopedL2Log } from '@aztec/stdlib/logs';
79
+ import type { DebugLogStore, LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
80
+ import { InMemoryDebugLogStore, NullDebugLogStore } from '@aztec/stdlib/logs';
86
81
  import { InboxLeaf, type L1ToL2MessageSource } from '@aztec/stdlib/messaging';
87
- import { P2PClientType } from '@aztec/stdlib/p2p';
88
82
  import type { Offense, SlashPayloadRound } from '@aztec/stdlib/slashing';
89
83
  import type { NullifierLeafPreimage, PublicDataTreeLeaf, PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
90
84
  import { MerkleTreeId, NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
@@ -110,14 +104,17 @@ import {
110
104
  trackSpan,
111
105
  } from '@aztec/telemetry-client';
112
106
  import {
107
+ FullNodeCheckpointsBuilder as CheckpointsBuilder,
108
+ FullNodeCheckpointsBuilder,
113
109
  NodeKeystoreAdapter,
114
110
  ValidatorClient,
115
111
  createBlockProposalHandler,
116
112
  createValidatorClient,
117
113
  } from '@aztec/validator-client';
114
+ import type { SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
118
115
  import { createWorldStateSynchronizer } from '@aztec/world-state';
119
116
 
120
- import { createPublicClient, fallback, http } from 'viem';
117
+ import { createPublicClient } from 'viem';
121
118
 
122
119
  import { createSentinel } from '../sentinel/factory.js';
123
120
  import { Sentinel } from '../sentinel/sentinel.js';
@@ -129,6 +126,7 @@ import { NodeMetrics } from './node_metrics.js';
129
126
  */
130
127
  export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
131
128
  private metrics: NodeMetrics;
129
+ private initialHeaderHashPromise: Promise<BlockHash> | undefined = undefined;
132
130
 
133
131
  // Prevent two snapshot operations to happen simultaneously
134
132
  private isUploadingSnapshot = false;
@@ -144,6 +142,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
144
142
  protected readonly l1ToL2MessageSource: L1ToL2MessageSource,
145
143
  protected readonly worldStateSynchronizer: WorldStateSynchronizer,
146
144
  protected readonly sequencer: SequencerClient | undefined,
145
+ protected readonly proverNode: ProverNode | undefined,
147
146
  protected readonly slasherClient: SlasherClientInterface | undefined,
148
147
  protected readonly validatorsSentinel: Sentinel | undefined,
149
148
  protected readonly epochPruneWatcher: EpochPruneWatcher | undefined,
@@ -155,12 +154,23 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
155
154
  private proofVerifier: ClientProtocolCircuitVerifier,
156
155
  private telemetry: TelemetryClient = getTelemetryClient(),
157
156
  private log = createLogger('node'),
157
+ private blobClient?: BlobClientInterface,
158
+ private validatorClient?: ValidatorClient,
159
+ private keyStoreManager?: KeystoreManager,
160
+ private debugLogStore: DebugLogStore = new NullDebugLogStore(),
158
161
  ) {
159
162
  this.metrics = new NodeMetrics(telemetry, 'AztecNodeService');
160
163
  this.tracer = telemetry.getTracer('AztecNodeService');
161
164
 
162
165
  this.log.info(`Aztec Node version: ${this.packageVersion}`);
163
166
  this.log.info(`Aztec Node started on chain 0x${l1ChainId.toString(16)}`, config.l1Contracts);
167
+
168
+ // A defensive check that protects us against introducing a bug in the complex `createAndSync` function. We must
169
+ // never have debugLogStore enabled when not in test mode because then we would be accumulating debug logs in
170
+ // memory which could be a DoS vector on the sequencer (since no fees are paid for debug logs).
171
+ if (debugLogStore.isEnabled && config.realProofs) {
172
+ throw new Error('debugLogStore should never be enabled when realProofs are set');
173
+ }
164
174
  }
165
175
 
166
176
  public async getWorldStateSyncStatus(): Promise<WorldStateSyncStatus> {
@@ -184,12 +194,14 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
184
194
  logger?: Logger;
185
195
  publisher?: SequencerPublisher;
186
196
  dateProvider?: DateProvider;
187
- blobSinkClient?: BlobSinkClientInterface;
188
- p2pClientDeps?: P2PClientDeps<P2PClientType.Full>;
197
+ p2pClientDeps?: P2PClientDeps;
198
+ proverNodeDeps?: Partial<ProverNodeDeps>;
199
+ slashingProtectionDb?: SlashingProtectionDatabase;
189
200
  } = {},
190
201
  options: {
191
202
  prefilledPublicData?: PublicDataTreeLeaf[];
192
203
  dontStartSequencer?: boolean;
204
+ dontStartProverNode?: boolean;
193
205
  } = {},
194
206
  ): Promise<AztecNodeService> {
195
207
  const config = { ...inputConfig }; // Copy the config so we dont mutate the input object
@@ -197,20 +209,31 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
197
209
  const packageVersion = getPackageVersion() ?? '';
198
210
  const telemetry = deps.telemetry ?? getTelemetryClient();
199
211
  const dateProvider = deps.dateProvider ?? new DateProvider();
200
- const blobSinkClient =
201
- deps.blobSinkClient ?? createBlobSinkClient(config, { logger: createLogger('node:blob-sink:client') });
202
212
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
203
213
 
204
- // Build a key store from file if given or from environment otherwise
214
+ // Build a key store from file if given or from environment otherwise.
215
+ // We keep the raw KeyStore available so we can merge with prover keys if enableProverNode is set.
205
216
  let keyStoreManager: KeystoreManager | undefined;
206
217
  const keyStoreProvided = config.keyStoreDirectory !== undefined && config.keyStoreDirectory.length > 0;
207
218
  if (keyStoreProvided) {
208
219
  const keyStores = loadKeystores(config.keyStoreDirectory!);
209
220
  keyStoreManager = new KeystoreManager(mergeKeystores(keyStores));
210
221
  } else {
211
- const keyStore = createKeyStoreForValidator(config);
212
- if (keyStore) {
213
- keyStoreManager = new KeystoreManager(keyStore);
222
+ const rawKeyStores: KeyStore[] = [];
223
+ const validatorKeyStore = createKeyStoreForValidator(config);
224
+ if (validatorKeyStore) {
225
+ rawKeyStores.push(validatorKeyStore);
226
+ }
227
+ if (config.enableProverNode) {
228
+ const proverKeyStore = createKeyStoreForProver(config);
229
+ if (proverKeyStore) {
230
+ rawKeyStores.push(proverKeyStore);
231
+ }
232
+ }
233
+ if (rawKeyStores.length > 0) {
234
+ keyStoreManager = new KeystoreManager(
235
+ rawKeyStores.length === 1 ? rawKeyStores[0] : mergeKeystores(rawKeyStores),
236
+ );
214
237
  }
215
238
  }
216
239
 
@@ -221,10 +244,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
221
244
  if (keyStoreManager === undefined) {
222
245
  throw new Error('Failed to create key store, a requirement for running a validator');
223
246
  }
224
- if (!keyStoreProvided) {
225
- log.warn(
226
- 'KEY STORE CREATED FROM ENVIRONMENT, IT IS RECOMMENDED TO USE A FILE-BASED KEY STORE IN PRODUCTION ENVIRONMENTS',
227
- );
247
+ if (!keyStoreProvided && process.env.NODE_ENV !== 'test') {
248
+ log.warn("Keystore created from env: it's recommended to use a file-based key store for production");
228
249
  }
229
250
  ValidatorClient.validateKeyStoreConfiguration(keyStoreManager, log);
230
251
  }
@@ -238,7 +259,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
238
259
 
239
260
  const publicClient = createPublicClient({
240
261
  chain: ethereumChain.chainInfo,
241
- transport: fallback(config.l1RpcUrls.map((url: string) => http(url))),
262
+ transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
242
263
  pollingInterval: config.viemPollingIntervalMS,
243
264
  });
244
265
 
@@ -252,10 +273,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
252
273
  config.l1Contracts = { ...config.l1Contracts, ...l1ContractsAddresses };
253
274
 
254
275
  const rollupContract = new RollupContract(publicClient, config.l1Contracts.rollupAddress.toString());
255
- const [l1GenesisTime, slotDuration, rollupVersionFromRollup] = await Promise.all([
276
+ const [l1GenesisTime, slotDuration, rollupVersionFromRollup, rollupManaLimit] = await Promise.all([
256
277
  rollupContract.getL1GenesisTime(),
257
278
  rollupContract.getSlotDuration(),
258
279
  rollupContract.getVersion(),
280
+ rollupContract.getManaLimit().then(Number),
259
281
  ] as const);
260
282
 
261
283
  config.rollupVersion ??= Number(rollupVersionFromRollup);
@@ -266,6 +288,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
266
288
  );
267
289
  }
268
290
 
291
+ const blobClient = await createBlobClientWithFileStores(config, log.createChild('blob-client'));
292
+
269
293
  // attempt snapshot sync if possible
270
294
  await trySnapshotSync(config, log);
271
295
 
@@ -273,7 +297,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
273
297
 
274
298
  const archiver = await createArchiver(
275
299
  config,
276
- { blobSinkClient, epochCache, telemetry, dateProvider },
300
+ { blobClient, epochCache, telemetry, dateProvider },
277
301
  { blockUntilSync: !config.skipArchiverInitialSync },
278
302
  );
279
303
 
@@ -284,17 +308,32 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
284
308
  options.prefilledPublicData,
285
309
  telemetry,
286
310
  );
287
- const circuitVerifier = config.realProofs
288
- ? await BBCircuitVerifier.new(config)
289
- : new TestCircuitVerifier(config.proverTestVerificationDelayMs);
311
+ const circuitVerifier =
312
+ config.realProofs || config.debugForceTxProofVerification
313
+ ? await BBCircuitVerifier.new(config)
314
+ : new TestCircuitVerifier(config.proverTestVerificationDelayMs);
315
+
316
+ let debugLogStore: DebugLogStore;
290
317
  if (!config.realProofs) {
291
318
  log.warn(`Aztec node is accepting fake proofs`);
319
+
320
+ debugLogStore = new InMemoryDebugLogStore();
321
+ log.info(
322
+ 'Aztec node started in test mode (realProofs set to false) hence debug logs from public functions will be collected and served',
323
+ );
324
+ } else {
325
+ debugLogStore = new NullDebugLogStore();
292
326
  }
327
+
293
328
  const proofVerifier = new QueuedIVCVerifier(config, circuitVerifier);
294
329
 
330
+ const proverOnly = config.enableProverNode && config.disableValidator;
331
+ if (proverOnly) {
332
+ log.info('Starting in prover-only mode: skipping validator, sequencer, sentinel, and slasher subsystems');
333
+ }
334
+
295
335
  // create the tx pool and the p2p client, which will need the l2 block source
296
336
  const p2pClient = await createP2PClient(
297
- P2PClientType.Full,
298
337
  config,
299
338
  archiver,
300
339
  proofVerifier,
@@ -306,55 +345,70 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
306
345
  deps.p2pClientDeps,
307
346
  );
308
347
 
309
- // We should really not be modifying the config object
310
- config.txPublicSetupAllowList = config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
348
+ // We'll accumulate sentinel watchers here
349
+ const watchers: Watcher[] = [];
311
350
 
312
- const blockBuilder = new BlockBuilder(
313
- { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
351
+ // Create FullNodeCheckpointsBuilder for block proposal handling and tx validation.
352
+ // Override maxTxsPerCheckpoint with the validator-specific limit if set.
353
+ const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder(
354
+ {
355
+ ...config,
356
+ l1GenesisTime,
357
+ slotDuration: Number(slotDuration),
358
+ rollupManaLimit,
359
+ maxTxsPerCheckpoint: config.validateMaxTxsPerCheckpoint,
360
+ },
314
361
  worldStateSynchronizer,
315
362
  archiver,
316
363
  dateProvider,
317
364
  telemetry,
318
365
  );
319
366
 
320
- // We'll accumulate sentinel watchers here
321
- const watchers: Watcher[] = [];
367
+ let validatorClient: ValidatorClient | undefined;
322
368
 
323
- // Create validator client if required
324
- const validatorClient = createValidatorClient(config, {
325
- p2pClient,
326
- telemetry,
327
- dateProvider,
328
- epochCache,
329
- blockBuilder,
330
- blockSource: archiver,
331
- l1ToL2MessageSource: archiver,
332
- keyStoreManager,
333
- });
369
+ if (!proverOnly) {
370
+ // Create validator client if required
371
+ validatorClient = await createValidatorClient(config, {
372
+ checkpointsBuilder: validatorCheckpointsBuilder,
373
+ worldState: worldStateSynchronizer,
374
+ p2pClient,
375
+ telemetry,
376
+ dateProvider,
377
+ epochCache,
378
+ blockSource: archiver,
379
+ l1ToL2MessageSource: archiver,
380
+ keyStoreManager,
381
+ blobClient,
382
+ slashingProtectionDb: deps.slashingProtectionDb,
383
+ });
334
384
 
335
- // If we have a validator client, register it as a source of offenses for the slasher,
336
- // and have it register callbacks on the p2p client *before* we start it, otherwise messages
337
- // like attestations or auths will fail.
338
- if (validatorClient) {
339
- watchers.push(validatorClient);
340
- if (!options.dontStartSequencer) {
341
- await validatorClient.registerHandlers();
385
+ // If we have a validator client, register it as a source of offenses for the slasher,
386
+ // and have it register callbacks on the p2p client *before* we start it, otherwise messages
387
+ // like attestations or auths will fail.
388
+ if (validatorClient) {
389
+ watchers.push(validatorClient);
390
+ if (!options.dontStartSequencer) {
391
+ await validatorClient.registerHandlers();
392
+ }
342
393
  }
343
394
  }
344
395
 
345
- // If there's no validator client but alwaysReexecuteBlockProposals is enabled,
346
- // create a BlockProposalHandler to reexecute block proposals for monitoring
347
- if (!validatorClient && config.alwaysReexecuteBlockProposals) {
348
- log.info('Setting up block proposal reexecution for monitoring');
396
+ // If there's no validator client, create a BlockProposalHandler to handle block proposals
397
+ // for monitoring or reexecution. Reexecution (default) allows us to follow the pending chain,
398
+ // while non-reexecution is used for validating the proposals and collecting their txs.
399
+ if (!validatorClient) {
400
+ const reexecute = !!config.alwaysReexecuteBlockProposals;
401
+ log.info(`Setting up block proposal handler` + (reexecute ? ' with reexecution of proposals' : ''));
349
402
  createBlockProposalHandler(config, {
350
- blockBuilder,
403
+ checkpointsBuilder: validatorCheckpointsBuilder,
404
+ worldState: worldStateSynchronizer,
351
405
  epochCache,
352
406
  blockSource: archiver,
353
407
  l1ToL2MessageSource: archiver,
354
408
  p2pClient,
355
409
  dateProvider,
356
410
  telemetry,
357
- }).registerForReexecution(p2pClient);
411
+ }).register(p2pClient, reexecute);
358
412
  }
359
413
 
360
414
  // Start world state and wait for it to sync to the archiver.
@@ -363,29 +417,33 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
363
417
  // Start p2p. Note that it depends on world state to be running.
364
418
  await p2pClient.start();
365
419
 
366
- const validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
367
- if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
368
- watchers.push(validatorsSentinel);
369
- }
370
-
420
+ let validatorsSentinel: Awaited<ReturnType<typeof createSentinel>> | undefined;
371
421
  let epochPruneWatcher: EpochPruneWatcher | undefined;
372
- if (config.slashPrunePenalty > 0n || config.slashDataWithholdingPenalty > 0n) {
373
- epochPruneWatcher = new EpochPruneWatcher(
374
- archiver,
375
- archiver,
376
- epochCache,
377
- p2pClient.getTxProvider(),
378
- blockBuilder,
379
- config,
380
- );
381
- watchers.push(epochPruneWatcher);
382
- }
383
-
384
- // We assume we want to slash for invalid attestations unless all max penalties are set to 0
385
422
  let attestationsBlockWatcher: AttestationsBlockWatcher | undefined;
386
- if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
387
- attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
388
- watchers.push(attestationsBlockWatcher);
423
+
424
+ if (!proverOnly) {
425
+ validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
426
+ if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
427
+ watchers.push(validatorsSentinel);
428
+ }
429
+
430
+ if (config.slashPrunePenalty > 0n || config.slashDataWithholdingPenalty > 0n) {
431
+ epochPruneWatcher = new EpochPruneWatcher(
432
+ archiver,
433
+ archiver,
434
+ epochCache,
435
+ p2pClient.getTxProvider(),
436
+ validatorCheckpointsBuilder,
437
+ config,
438
+ );
439
+ watchers.push(epochPruneWatcher);
440
+ }
441
+
442
+ // We assume we want to slash for invalid attestations unless all max penalties are set to 0
443
+ if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
444
+ attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
445
+ watchers.push(attestationsBlockWatcher);
446
+ }
389
447
  }
390
448
 
391
449
  // Start p2p-related services once the archiver has completed sync
@@ -403,7 +461,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
403
461
  // Validator enabled, create/start relevant service
404
462
  let sequencer: SequencerClient | undefined;
405
463
  let slasherClient: SlasherClientInterface | undefined;
406
- if (!config.disableValidator) {
464
+ if (!config.disableValidator && validatorClient) {
407
465
  // We create a slasher only if we have a sequencer, since all slashing actions go through the sequencer publisher
408
466
  // as they are executed when the node is selected as proposer.
409
467
  const validatorAddresses = keyStoreManager
@@ -422,14 +480,31 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
422
480
  );
423
481
  await slasherClient.start();
424
482
 
425
- const l1TxUtils = await createL1TxUtilsWithBlobsFromEthSigner(
426
- publicClient,
427
- keyStoreManager!.createAllValidatorPublisherSigners(),
428
- { ...config, scope: 'sequencer' },
429
- { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider },
430
- );
483
+ const l1TxUtils = config.sequencerPublisherForwarderAddress
484
+ ? await createForwarderL1TxUtilsFromSigners(
485
+ publicClient,
486
+ keyStoreManager!.createAllValidatorPublisherSigners(),
487
+ config.sequencerPublisherForwarderAddress,
488
+ { ...config, scope: 'sequencer' },
489
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
490
+ )
491
+ : await createL1TxUtilsFromSigners(
492
+ publicClient,
493
+ keyStoreManager!.createAllValidatorPublisherSigners(),
494
+ { ...config, scope: 'sequencer' },
495
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
496
+ );
431
497
 
432
498
  // Create and start the sequencer client
499
+ const checkpointsBuilder = new CheckpointsBuilder(
500
+ { ...config, l1GenesisTime, slotDuration: Number(slotDuration), rollupManaLimit },
501
+ worldStateSynchronizer,
502
+ archiver,
503
+ dateProvider,
504
+ telemetry,
505
+ debugLogStore,
506
+ );
507
+
433
508
  sequencer = await SequencerClient.new(config, {
434
509
  ...deps,
435
510
  epochCache,
@@ -438,12 +513,12 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
438
513
  p2pClient,
439
514
  worldStateSynchronizer,
440
515
  slasherClient,
441
- blockBuilder,
516
+ checkpointsBuilder,
442
517
  l2BlockSource: archiver,
443
518
  l1ToL2MessageSource: archiver,
444
519
  telemetry,
445
520
  dateProvider,
446
- blobSinkClient,
521
+ blobClient,
447
522
  nodeKeyStore: keyStoreManager!,
448
523
  });
449
524
  }
@@ -455,7 +530,37 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
455
530
  log.warn(`Sequencer created but not started`);
456
531
  }
457
532
 
458
- return new AztecNodeService(
533
+ // Create prover node subsystem if enabled
534
+ let proverNode: ProverNode | undefined;
535
+ if (config.enableProverNode) {
536
+ proverNode = await createProverNode(config, {
537
+ ...deps.proverNodeDeps,
538
+ telemetry,
539
+ dateProvider,
540
+ archiver,
541
+ worldStateSynchronizer,
542
+ p2pClient,
543
+ epochCache,
544
+ blobClient,
545
+ keyStoreManager,
546
+ });
547
+
548
+ if (!options.dontStartProverNode) {
549
+ await proverNode.start();
550
+ log.info(`Prover node subsystem started`);
551
+ } else {
552
+ log.info(`Prover node subsystem created but not started`);
553
+ }
554
+ }
555
+
556
+ const globalVariableBuilder = new GlobalVariableBuilder({
557
+ ...config,
558
+ rollupVersion: BigInt(config.rollupVersion),
559
+ l1GenesisTime,
560
+ slotDuration: Number(slotDuration),
561
+ });
562
+
563
+ const node = new AztecNodeService(
459
564
  config,
460
565
  p2pClient,
461
566
  archiver,
@@ -464,18 +569,25 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
464
569
  archiver,
465
570
  worldStateSynchronizer,
466
571
  sequencer,
572
+ proverNode,
467
573
  slasherClient,
468
574
  validatorsSentinel,
469
575
  epochPruneWatcher,
470
576
  ethereumChain.chainInfo.id,
471
577
  config.rollupVersion,
472
- new GlobalVariableBuilder(config),
578
+ globalVariableBuilder,
473
579
  epochCache,
474
580
  packageVersion,
475
581
  proofVerifier,
476
582
  telemetry,
477
583
  log,
584
+ blobClient,
585
+ validatorClient,
586
+ keyStoreManager,
587
+ debugLogStore,
478
588
  );
589
+
590
+ return node;
479
591
  }
480
592
 
481
593
  /**
@@ -486,6 +598,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
486
598
  return this.sequencer;
487
599
  }
488
600
 
601
+ /** Returns the prover node subsystem, if enabled. */
602
+ public getProverNode(): ProverNode | undefined {
603
+ return this.proverNode;
604
+ }
605
+
489
606
  public getBlockSource(): L2BlockSource {
490
607
  return this.blockSource;
491
608
  }
@@ -511,7 +628,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
511
628
  }
512
629
 
513
630
  public async getAllowedPublicSetup(): Promise<AllowedElement[]> {
514
- return this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
631
+ return [...(await getDefaultAllowedSetupFunctions()), ...(this.config.txPublicSetupAllowListExtend ?? [])];
515
632
  }
516
633
 
517
634
  /**
@@ -539,19 +656,26 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
539
656
  enr,
540
657
  l1ContractAddresses: contractAddresses,
541
658
  protocolContractAddresses: protocolContractAddresses,
659
+ realProofs: !!this.config.realProofs,
542
660
  };
543
661
 
544
662
  return nodeInfo;
545
663
  }
546
664
 
547
665
  /**
548
- * Get a block specified by its number.
549
- * @param number - The block number being requested.
666
+ * Get a block specified by its block number, block hash, or 'latest'.
667
+ * @param block - The block parameter (block number, block hash, or 'latest').
550
668
  * @returns The requested block.
551
669
  */
552
- public async getBlock(number: L2BlockNumber): Promise<L2Block | undefined> {
553
- const blockNumber = number === 'latest' ? await this.getBlockNumber() : number;
554
- return await this.blockSource.getBlock(blockNumber);
670
+ public async getBlock(block: BlockParameter): Promise<L2Block | undefined> {
671
+ if (BlockHash.isBlockHash(block)) {
672
+ return this.getBlockByHash(block);
673
+ }
674
+ const blockNumber = block === 'latest' ? await this.getBlockNumber() : (block as BlockNumber);
675
+ if (blockNumber === BlockNumber.ZERO) {
676
+ return this.buildInitialBlock();
677
+ }
678
+ return await this.blockSource.getL2Block(blockNumber);
555
679
  }
556
680
 
557
681
  /**
@@ -559,9 +683,17 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
559
683
  * @param blockHash - The block hash being requested.
560
684
  * @returns The requested block.
561
685
  */
562
- public async getBlockByHash(blockHash: Fr): Promise<L2Block | undefined> {
563
- const publishedBlock = await this.blockSource.getPublishedBlockByHash(blockHash);
564
- return publishedBlock?.block;
686
+ public async getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
687
+ const initialBlockHash = await this.#getInitialHeaderHash();
688
+ if (blockHash.equals(initialBlockHash)) {
689
+ return this.buildInitialBlock();
690
+ }
691
+ return await this.blockSource.getL2BlockByHash(blockHash);
692
+ }
693
+
694
+ private buildInitialBlock(): L2Block {
695
+ const initialHeader = this.worldStateSynchronizer.getCommitted().getInitialHeader();
696
+ return L2Block.empty(initialHeader);
565
697
  }
566
698
 
567
699
  /**
@@ -570,8 +702,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
570
702
  * @returns The requested block.
571
703
  */
572
704
  public async getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
573
- const publishedBlock = await this.blockSource.getPublishedBlockByArchive(archive);
574
- return publishedBlock?.block;
705
+ return await this.blockSource.getL2BlockByArchive(archive);
575
706
  }
576
707
 
577
708
  /**
@@ -580,20 +711,28 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
580
711
  * @param limit - The maximum number of blocks to obtain.
581
712
  * @returns The blocks requested.
582
713
  */
583
- public async getBlocks(from: number, limit: number): Promise<L2Block[]> {
584
- return (await this.blockSource.getBlocks(from, limit)) ?? [];
714
+ public async getBlocks(from: BlockNumber, limit: number): Promise<L2Block[]> {
715
+ return (await this.blockSource.getBlocks(from, BlockNumber(limit))) ?? [];
585
716
  }
586
717
 
587
- public async getPublishedBlocks(from: number, limit: number): Promise<PublishedL2Block[]> {
588
- return (await this.blockSource.getPublishedBlocks(from, limit)) ?? [];
718
+ public async getCheckpoints(from: CheckpointNumber, limit: number): Promise<PublishedCheckpoint[]> {
719
+ return (await this.blockSource.getCheckpoints(from, limit)) ?? [];
720
+ }
721
+
722
+ public async getCheckpointedBlocks(from: BlockNumber, limit: number) {
723
+ return (await this.blockSource.getCheckpointedBlocks(from, limit)) ?? [];
724
+ }
725
+
726
+ public getCheckpointsDataForEpoch(epochNumber: EpochNumber) {
727
+ return this.blockSource.getCheckpointsDataForEpoch(epochNumber);
589
728
  }
590
729
 
591
730
  /**
592
- * Method to fetch the current base fees.
593
- * @returns The current base fees.
731
+ * Method to fetch the current min L2 fees.
732
+ * @returns The current min L2 fees.
594
733
  */
595
- public async getCurrentBaseFees(): Promise<GasFees> {
596
- return await this.globalVariableBuilder.getCurrentBaseFees();
734
+ public async getCurrentMinFees(): Promise<GasFees> {
735
+ return await this.globalVariableBuilder.getCurrentMinFees();
597
736
  }
598
737
 
599
738
  public async getMaxPriorityFees(): Promise<GasFees> {
@@ -608,14 +747,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
608
747
  * Method to fetch the latest block number synchronized by the node.
609
748
  * @returns The block number.
610
749
  */
611
- public async getBlockNumber(): Promise<number> {
750
+ public async getBlockNumber(): Promise<BlockNumber> {
612
751
  return await this.blockSource.getBlockNumber();
613
752
  }
614
753
 
615
- public async getProvenBlockNumber(): Promise<number> {
754
+ public async getProvenBlockNumber(): Promise<BlockNumber> {
616
755
  return await this.blockSource.getProvenBlockNumber();
617
756
  }
618
757
 
758
+ public async getCheckpointedBlockNumber(): Promise<BlockNumber> {
759
+ return await this.blockSource.getCheckpointedL2BlockNumber();
760
+ }
761
+
762
+ public getCheckpointNumber(): Promise<CheckpointNumber> {
763
+ return this.blockSource.getCheckpointNumber();
764
+ }
765
+
619
766
  /**
620
767
  * Method to fetch the version of the package.
621
768
  * @returns The node package version
@@ -648,25 +795,51 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
648
795
  return this.contractDataSource.getContract(address);
649
796
  }
650
797
 
651
- /**
652
- * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
653
- * @param from - The block number from which to begin retrieving logs.
654
- * @param limit - The maximum number of blocks to retrieve logs from.
655
- * @returns An array of private logs from the specified range of blocks.
656
- */
657
- public getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
658
- return this.logsSource.getPrivateLogs(from, limit);
659
- }
660
-
661
- /**
662
- * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
663
- * @param tags - The tags to filter the logs by.
664
- * @param logsPerTag - The maximum number of logs to return for each tag. By default no limit is set
665
- * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
666
- * that tag.
667
- */
668
- public getLogsByTags(tags: Fr[], logsPerTag?: number): Promise<TxScopedL2Log[][]> {
669
- return this.logsSource.getLogsByTags(tags, logsPerTag);
798
+ public async getPrivateLogsByTags(
799
+ tags: SiloedTag[],
800
+ page?: number,
801
+ referenceBlock?: BlockHash,
802
+ ): Promise<TxScopedL2Log[][]> {
803
+ let upToBlockNumber: BlockNumber | undefined;
804
+ if (referenceBlock) {
805
+ const initialBlockHash = await this.#getInitialHeaderHash();
806
+ if (referenceBlock.equals(initialBlockHash)) {
807
+ upToBlockNumber = BlockNumber(0);
808
+ } else {
809
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
810
+ if (!header) {
811
+ throw new Error(
812
+ `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
813
+ );
814
+ }
815
+ upToBlockNumber = header.globalVariables.blockNumber;
816
+ }
817
+ }
818
+ return this.logsSource.getPrivateLogsByTags(tags, page, upToBlockNumber);
819
+ }
820
+
821
+ public async getPublicLogsByTagsFromContract(
822
+ contractAddress: AztecAddress,
823
+ tags: Tag[],
824
+ page?: number,
825
+ referenceBlock?: BlockHash,
826
+ ): Promise<TxScopedL2Log[][]> {
827
+ let upToBlockNumber: BlockNumber | undefined;
828
+ if (referenceBlock) {
829
+ const initialBlockHash = await this.#getInitialHeaderHash();
830
+ if (referenceBlock.equals(initialBlockHash)) {
831
+ upToBlockNumber = BlockNumber(0);
832
+ } else {
833
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
834
+ if (!header) {
835
+ throw new Error(
836
+ `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
837
+ );
838
+ }
839
+ upToBlockNumber = header.globalVariables.blockNumber;
840
+ }
841
+ }
842
+ return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber);
670
843
  }
671
844
 
672
845
  /**
@@ -708,26 +881,36 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
708
881
  }
709
882
 
710
883
  await this.p2pClient!.sendTx(tx);
711
- this.metrics.receivedTx(timer.ms(), true);
712
- this.log.info(`Received tx ${txHash}`, { txHash });
884
+ const duration = timer.ms();
885
+ this.metrics.receivedTx(duration, true);
886
+ this.log.info(`Received tx ${txHash} in ${duration}ms`, { txHash });
713
887
  }
714
888
 
715
889
  public async getTxReceipt(txHash: TxHash): Promise<TxReceipt> {
716
- let txReceipt = new TxReceipt(txHash, TxStatus.DROPPED, 'Tx dropped by P2P node.');
717
-
718
- // We first check if the tx is in pending (instead of first checking if it is mined) because if we first check
719
- // for mined and then for pending there could be a race condition where the tx is mined between the two checks
720
- // and we would incorrectly return a TxReceipt with status DROPPED
721
- if ((await this.p2pClient.getTxStatus(txHash)) === 'pending') {
722
- txReceipt = new TxReceipt(txHash, TxStatus.PENDING, '');
723
- }
890
+ // Check the tx pool status first. If the tx is known to the pool (pending or mined), we'll use that
891
+ // as a fallback if we don't find a settled receipt in the archiver.
892
+ const txPoolStatus = await this.p2pClient.getTxStatus(txHash);
893
+ const isKnownToPool = txPoolStatus === 'pending' || txPoolStatus === 'mined';
724
894
 
895
+ // Then get the actual tx from the archiver, which tracks every tx in a mined block.
725
896
  const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
897
+
898
+ let receipt: TxReceipt;
726
899
  if (settledTxReceipt) {
727
- txReceipt = settledTxReceipt;
900
+ receipt = settledTxReceipt;
901
+ } else if (isKnownToPool) {
902
+ // If the tx is in the pool but not in the archiver, it's pending.
903
+ // This handles race conditions between archiver and p2p, where the archiver
904
+ // has pruned the block in which a tx was mined, but p2p has not caught up yet.
905
+ receipt = new TxReceipt(txHash, TxStatus.PENDING, undefined, undefined);
906
+ } else {
907
+ // Otherwise, if we don't know the tx, we consider it dropped.
908
+ receipt = new TxReceipt(txHash, TxStatus.DROPPED, undefined, 'Tx dropped by P2P node');
728
909
  }
729
910
 
730
- return txReceipt;
911
+ this.debugLogStore.decorateReceiptWithLogs(txHash.toString(), receipt);
912
+
913
+ return receipt;
731
914
  }
732
915
 
733
916
  public getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
@@ -744,13 +927,23 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
744
927
  await tryStop(this.slasherClient);
745
928
  await tryStop(this.proofVerifier);
746
929
  await tryStop(this.sequencer);
930
+ await tryStop(this.proverNode);
747
931
  await tryStop(this.p2pClient);
748
932
  await tryStop(this.worldStateSynchronizer);
749
933
  await tryStop(this.blockSource);
934
+ await tryStop(this.blobClient);
750
935
  await tryStop(this.telemetry);
751
936
  this.log.info(`Stopped Aztec Node`);
752
937
  }
753
938
 
939
+ /**
940
+ * Returns the blob client used by this node.
941
+ * @internal - Exposed for testing purposes only.
942
+ */
943
+ public getBlobClient(): BlobClientInterface | undefined {
944
+ return this.blobClient;
945
+ }
946
+
754
947
  /**
755
948
  * Method to retrieve pending txs.
756
949
  * @param limit - The number of items to returns
@@ -783,121 +976,95 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
783
976
  return compactArray(await Promise.all(txHashes.map(txHash => this.getTxByHash(txHash))));
784
977
  }
785
978
 
786
- /**
787
- * Find the indexes of the given leaves in the given tree along with a block metadata pointing to the block in which
788
- * the leaves were inserted.
789
- * @param blockNumber - The block number at which to get the data or 'latest' for latest data.
790
- * @param treeId - The tree to search in.
791
- * @param leafValues - The values to search for.
792
- * @returns The indices of leaves and the block metadata of a block in which the leaves were inserted.
793
- */
794
979
  public async findLeavesIndexes(
795
- blockNumber: L2BlockNumber,
980
+ referenceBlock: BlockParameter,
796
981
  treeId: MerkleTreeId,
797
982
  leafValues: Fr[],
798
- ): Promise<(InBlock<bigint> | undefined)[]> {
799
- const committedDb = await this.#getWorldState(blockNumber);
983
+ ): Promise<(DataInBlock<bigint> | undefined)[]> {
984
+ const committedDb = await this.getWorldState(referenceBlock);
800
985
  const maybeIndices = await committedDb.findLeafIndices(
801
986
  treeId,
802
987
  leafValues.map(x => x.toBuffer()),
803
988
  );
804
- // We filter out undefined values
805
- const indices = maybeIndices.filter(x => x !== undefined) as bigint[];
989
+ // Filter out undefined values to query block numbers only for found leaves
990
+ const definedIndices = maybeIndices.filter(x => x !== undefined);
806
991
 
807
- // Now we find the block numbers for the indices
808
- const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, indices);
992
+ // Now we find the block numbers for the defined indices
993
+ const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, definedIndices);
809
994
 
810
- // If any of the block numbers are undefined, we throw an error.
811
- for (let i = 0; i < indices.length; i++) {
812
- if (blockNumbers[i] === undefined) {
813
- throw new Error(`Block number is undefined for leaf index ${indices[i]} in tree ${MerkleTreeId[treeId]}`);
995
+ // Build a map from leaf index to block number
996
+ const indexToBlockNumber = new Map<bigint, BlockNumber>();
997
+ for (let i = 0; i < definedIndices.length; i++) {
998
+ const blockNumber = blockNumbers[i];
999
+ if (blockNumber === undefined) {
1000
+ throw new Error(
1001
+ `Block number is undefined for leaf index ${definedIndices[i]} in tree ${MerkleTreeId[treeId]}`,
1002
+ );
814
1003
  }
1004
+ indexToBlockNumber.set(definedIndices[i], blockNumber);
815
1005
  }
816
1006
 
817
1007
  // Get unique block numbers in order to optimize num calls to getLeafValue function.
818
- const uniqueBlockNumbers = [...new Set(blockNumbers.filter(x => x !== undefined))];
1008
+ const uniqueBlockNumbers = [...new Set(indexToBlockNumber.values())];
819
1009
 
820
- // Now we obtain the block hashes from the archive tree by calling await `committedDb.getLeafValue(treeId, index)`
821
- // (note that block number corresponds to the leaf index in the archive tree).
1010
+ // Now we obtain the block hashes from the archive tree (block number = leaf index in archive tree).
822
1011
  const blockHashes = await Promise.all(
823
1012
  uniqueBlockNumbers.map(blockNumber => {
824
- return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, blockNumber!);
1013
+ return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
825
1014
  }),
826
1015
  );
827
1016
 
828
- // If any of the block hashes are undefined, we throw an error.
1017
+ // Build a map from block number to block hash
1018
+ const blockNumberToHash = new Map<BlockNumber, Fr>();
829
1019
  for (let i = 0; i < uniqueBlockNumbers.length; i++) {
830
- if (blockHashes[i] === undefined) {
1020
+ const blockHash = blockHashes[i];
1021
+ if (blockHash === undefined) {
831
1022
  throw new Error(`Block hash is undefined for block number ${uniqueBlockNumbers[i]}`);
832
1023
  }
1024
+ blockNumberToHash.set(uniqueBlockNumbers[i], blockHash);
833
1025
  }
834
1026
 
835
- // Create InBlock objects by combining indices, blockNumbers and blockHashes and return them.
836
- return maybeIndices.map((index, i) => {
1027
+ // Create DataInBlock objects by combining indices, blockNumbers and blockHashes and return them.
1028
+ return maybeIndices.map(index => {
837
1029
  if (index === undefined) {
838
1030
  return undefined;
839
1031
  }
840
- const blockNumber = blockNumbers[i];
1032
+ const blockNumber = indexToBlockNumber.get(index);
841
1033
  if (blockNumber === undefined) {
842
- return undefined;
1034
+ throw new Error(`Block number not found for leaf index ${index} in tree ${MerkleTreeId[treeId]}`);
843
1035
  }
844
- const blockHashIndex = uniqueBlockNumbers.indexOf(blockNumber);
845
- const blockHash = blockHashes[blockHashIndex];
846
- if (!blockHash) {
847
- return undefined;
1036
+ const blockHash = blockNumberToHash.get(blockNumber);
1037
+ if (blockHash === undefined) {
1038
+ throw new Error(`Block hash not found for block number ${blockNumber}`);
848
1039
  }
849
1040
  return {
850
- l2BlockNumber: Number(blockNumber),
851
- l2BlockHash: L2BlockHash.fromField(blockHash),
1041
+ l2BlockNumber: blockNumber,
1042
+ l2BlockHash: new BlockHash(blockHash),
852
1043
  data: index,
853
1044
  };
854
1045
  });
855
1046
  }
856
1047
 
857
- /**
858
- * Returns a sibling path for the given index in the nullifier tree.
859
- * @param blockNumber - The block number at which to get the data.
860
- * @param leafIndex - The index of the leaf for which the sibling path is required.
861
- * @returns The sibling path for the leaf index.
862
- */
863
- public async getNullifierSiblingPath(
864
- blockNumber: L2BlockNumber,
865
- leafIndex: bigint,
866
- ): Promise<SiblingPath<typeof NULLIFIER_TREE_HEIGHT>> {
867
- const committedDb = await this.#getWorldState(blockNumber);
868
- return committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, leafIndex);
869
- }
870
-
871
- /**
872
- * Returns a sibling path for the given index in the data tree.
873
- * @param blockNumber - The block number at which to get the data.
874
- * @param leafIndex - The index of the leaf for which the sibling path is required.
875
- * @returns The sibling path for the leaf index.
876
- */
877
- public async getNoteHashSiblingPath(
878
- blockNumber: L2BlockNumber,
879
- leafIndex: bigint,
880
- ): Promise<SiblingPath<typeof NOTE_HASH_TREE_HEIGHT>> {
881
- const committedDb = await this.#getWorldState(blockNumber);
882
- return committedDb.getSiblingPath(MerkleTreeId.NOTE_HASH_TREE, leafIndex);
883
- }
884
-
885
- public async getArchiveMembershipWitness(
886
- blockNumber: L2BlockNumber,
887
- archive: Fr,
1048
+ public async getBlockHashMembershipWitness(
1049
+ referenceBlock: BlockParameter,
1050
+ blockHash: BlockHash,
888
1051
  ): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
889
- const committedDb = await this.#getWorldState(blockNumber);
890
- const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [archive]);
1052
+ // The Noir circuit checks the archive membership proof against `anchor_block_header.last_archive.root`,
1053
+ // which is the archive tree root BEFORE the anchor block was added (i.e. the state after block N-1).
1054
+ // So we need the world state at block N-1, not block N, to produce a sibling path matching that root.
1055
+ const referenceBlockNumber = await this.resolveBlockNumber(referenceBlock);
1056
+ const committedDb = await this.getWorldState(BlockNumber(referenceBlockNumber - 1));
1057
+ const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [blockHash]);
891
1058
  return pathAndIndex === undefined
892
1059
  ? undefined
893
1060
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
894
1061
  }
895
1062
 
896
1063
  public async getNoteHashMembershipWitness(
897
- blockNumber: L2BlockNumber,
1064
+ referenceBlock: BlockParameter,
898
1065
  noteHash: Fr,
899
1066
  ): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
900
- const committedDb = await this.#getWorldState(blockNumber);
1067
+ const committedDb = await this.getWorldState(referenceBlock);
901
1068
  const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.NOTE_HASH_TREE>(
902
1069
  MerkleTreeId.NOTE_HASH_TREE,
903
1070
  [noteHash],
@@ -907,17 +1074,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
907
1074
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
908
1075
  }
909
1076
 
910
- /**
911
- * Returns the index and a sibling path for a leaf in the committed l1 to l2 data tree.
912
- * @param blockNumber - The block number at which to get the data.
913
- * @param l1ToL2Message - The l1ToL2Message to get the index / sibling path for.
914
- * @returns A tuple of the index and the sibling path of the L1ToL2Message (undefined if not found).
915
- */
916
1077
  public async getL1ToL2MessageMembershipWitness(
917
- blockNumber: L2BlockNumber,
1078
+ referenceBlock: BlockParameter,
918
1079
  l1ToL2Message: Fr,
919
1080
  ): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined> {
920
- const db = await this.#getWorldState(blockNumber);
1081
+ const db = await this.getWorldState(referenceBlock);
921
1082
  const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [l1ToL2Message]);
922
1083
  if (!witness) {
923
1084
  return undefined;
@@ -927,9 +1088,9 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
927
1088
  return [witness.index, witness.path];
928
1089
  }
929
1090
 
930
- public async getL1ToL2MessageBlock(l1ToL2Message: Fr): Promise<number | undefined> {
1091
+ public async getL1ToL2MessageCheckpoint(l1ToL2Message: Fr): Promise<CheckpointNumber | undefined> {
931
1092
  const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
932
- return messageIndex ? InboxLeaf.l2BlockFromIndex(messageIndex) : undefined;
1093
+ return messageIndex ? InboxLeaf.checkpointNumberFromIndex(messageIndex) : undefined;
933
1094
  }
934
1095
 
935
1096
  /**
@@ -943,54 +1104,26 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
943
1104
  }
944
1105
 
945
1106
  /**
946
- * Returns all the L2 to L1 messages in a block.
947
- * @param blockNumber - The block number at which to get the data.
948
- * @returns The L2 to L1 messages (undefined if the block number is not found).
949
- */
950
- public async getL2ToL1Messages(blockNumber: L2BlockNumber): Promise<Fr[][] | undefined> {
951
- const block = await this.blockSource.getBlock(blockNumber === 'latest' ? await this.getBlockNumber() : blockNumber);
952
- return block?.body.txEffects.map(txEffect => txEffect.l2ToL1Msgs);
953
- }
954
-
955
- /**
956
- * Returns a sibling path for a leaf in the committed blocks tree.
957
- * @param blockNumber - The block number at which to get the data.
958
- * @param leafIndex - Index of the leaf in the tree.
959
- * @returns The sibling path.
960
- */
961
- public async getArchiveSiblingPath(
962
- blockNumber: L2BlockNumber,
963
- leafIndex: bigint,
964
- ): Promise<SiblingPath<typeof ARCHIVE_HEIGHT>> {
965
- const committedDb = await this.#getWorldState(blockNumber);
966
- return committedDb.getSiblingPath(MerkleTreeId.ARCHIVE, leafIndex);
967
- }
968
-
969
- /**
970
- * Returns a sibling path for a leaf in the committed public data tree.
971
- * @param blockNumber - The block number at which to get the data.
972
- * @param leafIndex - Index of the leaf in the tree.
973
- * @returns The sibling path.
1107
+ * Returns all the L2 to L1 messages in an epoch.
1108
+ * @param epoch - The epoch at which to get the data.
1109
+ * @returns The L2 to L1 messages (empty array if the epoch is not found).
974
1110
  */
975
- public async getPublicDataSiblingPath(
976
- blockNumber: L2BlockNumber,
977
- leafIndex: bigint,
978
- ): Promise<SiblingPath<typeof PUBLIC_DATA_TREE_HEIGHT>> {
979
- const committedDb = await this.#getWorldState(blockNumber);
980
- return committedDb.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex);
1111
+ public async getL2ToL1Messages(epoch: EpochNumber): Promise<Fr[][][][]> {
1112
+ // Assumes `getCheckpointedBlocksForEpoch` returns blocks in ascending order of block number.
1113
+ const checkpointedBlocks = await this.blockSource.getCheckpointedBlocksForEpoch(epoch);
1114
+ const blocksInCheckpoints = chunkBy(checkpointedBlocks, cb => cb.block.header.globalVariables.slotNumber).map(
1115
+ group => group.map(cb => cb.block),
1116
+ );
1117
+ return blocksInCheckpoints.map(blocks =>
1118
+ blocks.map(block => block.body.txEffects.map(txEffect => txEffect.l2ToL1Msgs)),
1119
+ );
981
1120
  }
982
1121
 
983
- /**
984
- * Returns a nullifier membership witness for a given nullifier at a given block.
985
- * @param blockNumber - The block number at which to get the index.
986
- * @param nullifier - Nullifier we try to find witness for.
987
- * @returns The nullifier membership witness (if found).
988
- */
989
1122
  public async getNullifierMembershipWitness(
990
- blockNumber: L2BlockNumber,
1123
+ referenceBlock: BlockParameter,
991
1124
  nullifier: Fr,
992
1125
  ): Promise<NullifierMembershipWitness | undefined> {
993
- const db = await this.#getWorldState(blockNumber);
1126
+ const db = await this.getWorldState(referenceBlock);
994
1127
  const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]);
995
1128
  if (!witness) {
996
1129
  return undefined;
@@ -1005,32 +1138,20 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1005
1138
  return new NullifierMembershipWitness(index, leafPreimage as NullifierLeafPreimage, path);
1006
1139
  }
1007
1140
 
1008
- /**
1009
- * Returns a low nullifier membership witness for a given nullifier at a given block.
1010
- * @param blockNumber - The block number at which to get the index.
1011
- * @param nullifier - Nullifier we try to find the low nullifier witness for.
1012
- * @returns The low nullifier membership witness (if found).
1013
- * @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
1014
- * list structure" of leaves and proving that a lower nullifier is pointing to a bigger next value than the nullifier
1015
- * we are trying to prove non-inclusion for.
1016
- *
1017
- * Note: This function returns the membership witness of the nullifier itself and not the low nullifier when
1018
- * the nullifier already exists in the tree. This is because the `getPreviousValueIndex` function returns the
1019
- * index of the nullifier itself when it already exists in the tree.
1020
- * TODO: This is a confusing behavior and we should eventually address that.
1021
- */
1022
1141
  public async getLowNullifierMembershipWitness(
1023
- blockNumber: L2BlockNumber,
1142
+ referenceBlock: BlockParameter,
1024
1143
  nullifier: Fr,
1025
1144
  ): Promise<NullifierMembershipWitness | undefined> {
1026
- const committedDb = await this.#getWorldState(blockNumber);
1145
+ const committedDb = await this.getWorldState(referenceBlock);
1027
1146
  const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
1028
1147
  if (!findResult) {
1029
1148
  return undefined;
1030
1149
  }
1031
1150
  const { index, alreadyPresent } = findResult;
1032
1151
  if (alreadyPresent) {
1033
- this.log.warn(`Nullifier ${nullifier.toBigInt()} already exists in the tree`);
1152
+ throw new Error(
1153
+ `Cannot prove nullifier non-inclusion: nullifier ${nullifier.toBigInt()} already exists in the tree`,
1154
+ );
1034
1155
  }
1035
1156
  const preimageData = (await committedDb.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index))!;
1036
1157
 
@@ -1038,8 +1159,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1038
1159
  return new NullifierMembershipWitness(BigInt(index), preimageData as NullifierLeafPreimage, siblingPath);
1039
1160
  }
1040
1161
 
1041
- async getPublicDataWitness(blockNumber: L2BlockNumber, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1042
- const committedDb = await this.#getWorldState(blockNumber);
1162
+ async getPublicDataWitness(referenceBlock: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1163
+ const committedDb = await this.getWorldState(referenceBlock);
1043
1164
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
1044
1165
  if (!lowLeafResult) {
1045
1166
  return undefined;
@@ -1053,19 +1174,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1053
1174
  }
1054
1175
  }
1055
1176
 
1056
- /**
1057
- * Gets the storage value at the given contract storage slot.
1058
- *
1059
- * @remarks The storage slot here refers to the slot as it is defined in Noir not the index in the merkle tree.
1060
- * Aztec's version of `eth_getStorageAt`.
1061
- *
1062
- * @param contract - Address of the contract to query.
1063
- * @param slot - Slot to query.
1064
- * @param blockNumber - The block number at which to get the data or 'latest'.
1065
- * @returns Storage value at the given contract slot.
1066
- */
1067
- public async getPublicStorageAt(blockNumber: L2BlockNumber, contract: AztecAddress, slot: Fr): Promise<Fr> {
1068
- const committedDb = await this.#getWorldState(blockNumber);
1177
+ public async getPublicStorageAt(referenceBlock: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
1178
+ const committedDb = await this.getWorldState(referenceBlock);
1069
1179
  const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
1070
1180
 
1071
1181
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
@@ -1079,23 +1189,22 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1079
1189
  return preimage.leaf.value;
1080
1190
  }
1081
1191
 
1082
- /**
1083
- * Returns the currently committed block header, or the initial header if no blocks have been produced.
1084
- * @returns The current committed block header.
1085
- */
1086
- public async getBlockHeader(blockNumber: L2BlockNumber = 'latest'): Promise<BlockHeader | undefined> {
1087
- return blockNumber === 0 || (blockNumber === 'latest' && (await this.blockSource.getBlockNumber()) === 0)
1088
- ? this.worldStateSynchronizer.getCommitted().getInitialHeader()
1089
- : this.blockSource.getBlockHeader(blockNumber);
1090
- }
1091
-
1092
- /**
1093
- * Get a block header specified by its hash.
1094
- * @param blockHash - The block hash being requested.
1095
- * @returns The requested block header.
1096
- */
1097
- public async getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
1098
- return await this.blockSource.getBlockHeaderByHash(blockHash);
1192
+ public async getBlockHeader(block: BlockParameter = 'latest'): Promise<BlockHeader | undefined> {
1193
+ if (BlockHash.isBlockHash(block)) {
1194
+ const initialBlockHash = await this.#getInitialHeaderHash();
1195
+ if (block.equals(initialBlockHash)) {
1196
+ // Block source doesn't handle initial header so we need to handle the case separately.
1197
+ return this.worldStateSynchronizer.getCommitted().getInitialHeader();
1198
+ }
1199
+ return this.blockSource.getBlockHeaderByHash(block);
1200
+ } else {
1201
+ // Block source doesn't handle initial header so we need to handle the case separately.
1202
+ const blockNumber = block === 'latest' ? await this.getBlockNumber() : (block as BlockNumber);
1203
+ if (blockNumber === BlockNumber.ZERO) {
1204
+ return this.worldStateSynchronizer.getCommitted().getInitialHeader();
1205
+ }
1206
+ return this.blockSource.getBlockHeader(block);
1207
+ }
1099
1208
  }
1100
1209
 
1101
1210
  /**
@@ -1107,6 +1216,14 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1107
1216
  return await this.blockSource.getBlockHeaderByArchive(archive);
1108
1217
  }
1109
1218
 
1219
+ public getBlockData(number: BlockNumber): Promise<BlockData | undefined> {
1220
+ return this.blockSource.getBlockData(number);
1221
+ }
1222
+
1223
+ public getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
1224
+ return this.blockSource.getBlockDataByArchive(archive);
1225
+ }
1226
+
1110
1227
  /**
1111
1228
  * Simulates the public part of a transaction with the current state.
1112
1229
  * @param tx - The transaction to simulate.
@@ -1130,7 +1247,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1130
1247
  }
1131
1248
 
1132
1249
  const txHash = tx.getTxHash();
1133
- const blockNumber = (await this.blockSource.getBlockNumber()) + 1;
1250
+ const latestBlockNumber = await this.blockSource.getBlockNumber();
1251
+ const blockNumber = BlockNumber.add(latestBlockNumber, 1);
1134
1252
 
1135
1253
  // If sequencer is not initialized, we just set these values to zero for simulation.
1136
1254
  const coinbase = EthAddress.ZERO;
@@ -1145,6 +1263,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1145
1263
  this.contractDataSource,
1146
1264
  new DateProvider(),
1147
1265
  this.telemetry,
1266
+ this.log.getBindings(),
1148
1267
  );
1149
1268
 
1150
1269
  this.log.verbose(`Simulating public calls for tx ${txHash}`, {
@@ -1153,6 +1272,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1153
1272
  blockNumber,
1154
1273
  });
1155
1274
 
1275
+ // Ensure world-state has caught up with the latest block we loaded from the archiver
1276
+ await this.worldStateSynchronizer.syncImmediate(latestBlockNumber);
1156
1277
  const merkleTreeFork = await this.worldStateSynchronizer.fork();
1157
1278
  try {
1158
1279
  const config = PublicSimulatorConfig.from({
@@ -1160,13 +1281,15 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1160
1281
  collectDebugLogs: true,
1161
1282
  collectHints: false,
1162
1283
  collectCallMetadata: true,
1163
- maxDebugLogMemoryReads: this.config.rpcSimulatePublicMaxDebugLogMemoryReads,
1164
1284
  collectStatistics: false,
1285
+ collectionLimits: CollectionLimitsConfig.from({
1286
+ maxDebugLogMemoryReads: this.config.rpcSimulatePublicMaxDebugLogMemoryReads,
1287
+ }),
1165
1288
  });
1166
1289
  const processor = publicProcessorFactory.create(merkleTreeFork, newGlobalVariables, config);
1167
1290
 
1168
1291
  // REFACTOR: Consider merging ProcessReturnValues into ProcessedTx
1169
- const [processedTxs, failedTxs, _usedTxs, returns] = await processor.process([tx]);
1292
+ const [processedTxs, failedTxs, _usedTxs, returns, debugLogs] = await processor.process([tx]);
1170
1293
  // REFACTOR: Consider returning the error rather than throwing
1171
1294
  if (failedTxs.length) {
1172
1295
  this.log.warn(`Simulated tx ${txHash} fails: ${failedTxs[0].error}`, { txHash });
@@ -1180,6 +1303,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1180
1303
  processedTx.txEffect,
1181
1304
  returns,
1182
1305
  processedTx.gasUsed,
1306
+ debugLogs,
1183
1307
  );
1184
1308
  } finally {
1185
1309
  await merkleTreeFork.close();
@@ -1193,19 +1317,32 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1193
1317
  const db = this.worldStateSynchronizer.getCommitted();
1194
1318
  const verifier = isSimulation ? undefined : this.proofVerifier;
1195
1319
 
1196
- // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1320
+ // We accept transactions if they are not expired by the next slot (checked based on the ExpirationTimestamp field)
1197
1321
  const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1198
- const blockNumber = (await this.blockSource.getBlockNumber()) + 1;
1199
- const validator = createValidatorForAcceptingTxs(db, this.contractDataSource, verifier, {
1200
- timestamp: nextSlotTimestamp,
1201
- blockNumber,
1202
- l1ChainId: this.l1ChainId,
1203
- rollupVersion: this.version,
1204
- setupAllowList: this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions()),
1205
- gasFees: await this.getCurrentBaseFees(),
1206
- skipFeeEnforcement,
1207
- txsPermitted: !this.config.disableTransactions,
1208
- });
1322
+ const blockNumber = BlockNumber((await this.blockSource.getBlockNumber()) + 1);
1323
+ const l1Constants = await this.blockSource.getL1Constants();
1324
+ const validator = createTxValidatorForAcceptingTxsOverRPC(
1325
+ db,
1326
+ this.contractDataSource,
1327
+ verifier,
1328
+ {
1329
+ timestamp: nextSlotTimestamp,
1330
+ blockNumber,
1331
+ l1ChainId: this.l1ChainId,
1332
+ rollupVersion: this.version,
1333
+ setupAllowList: [
1334
+ ...(await getDefaultAllowedSetupFunctions()),
1335
+ ...(this.config.txPublicSetupAllowListExtend ?? []),
1336
+ ],
1337
+ gasFees: await this.getCurrentMinFees(),
1338
+ skipFeeEnforcement,
1339
+ txsPermitted: !this.config.disableTransactions,
1340
+ rollupManaLimit: l1Constants.rollupManaLimit,
1341
+ maxBlockL2Gas: this.config.validateMaxL2BlockGas,
1342
+ maxBlockDAGas: this.config.validateMaxDABlockGas,
1343
+ },
1344
+ this.log.getBindings(),
1345
+ );
1209
1346
 
1210
1347
  return await validator.validateTx(tx);
1211
1348
  }
@@ -1274,7 +1411,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1274
1411
  }
1275
1412
 
1276
1413
  // And it has an L2 block hash
1277
- const l2BlockHash = await archiver.getL2Tips().then(tips => tips.latest.hash);
1414
+ const l2BlockHash = await archiver.getL2Tips().then(tips => tips.proposed.hash);
1278
1415
  if (!l2BlockHash) {
1279
1416
  this.metrics.recordSnapshotError();
1280
1417
  throw new Error(`Archiver has no latest L2 block hash downloaded. Cannot start snapshot.`);
@@ -1302,13 +1439,13 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1302
1439
  return Promise.resolve();
1303
1440
  }
1304
1441
 
1305
- public async rollbackTo(targetBlock: number, force?: boolean): Promise<void> {
1442
+ public async rollbackTo(targetBlock: BlockNumber, force?: boolean): Promise<void> {
1306
1443
  const archiver = this.blockSource as Archiver;
1307
1444
  if (!('rollbackTo' in archiver)) {
1308
1445
  throw new Error('Archiver implementation does not support rollbacks.');
1309
1446
  }
1310
1447
 
1311
- const finalizedBlock = await archiver.getL2Tips().then(tips => tips.finalized.number);
1448
+ const finalizedBlock = await archiver.getL2Tips().then(tips => tips.finalized.block.number);
1312
1449
  if (targetBlock < finalizedBlock) {
1313
1450
  if (force) {
1314
1451
  this.log.warn(`Clearing world state database to allow rolling back behind finalized block ${finalizedBlock}`);
@@ -1369,17 +1506,108 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1369
1506
  }
1370
1507
  }
1371
1508
 
1509
+ public async reloadKeystore(): Promise<void> {
1510
+ if (!this.config.keyStoreDirectory?.length) {
1511
+ throw new BadRequestError(
1512
+ 'Cannot reload keystore: node is not using a file-based keystore. ' +
1513
+ 'Set KEY_STORE_DIRECTORY to use file-based keystores.',
1514
+ );
1515
+ }
1516
+ if (!this.validatorClient) {
1517
+ throw new BadRequestError('Cannot reload keystore: validator is not enabled.');
1518
+ }
1519
+
1520
+ this.log.info('Reloading keystore from disk');
1521
+
1522
+ // Re-read and validate keystore files
1523
+ const keyStores = loadKeystores(this.config.keyStoreDirectory);
1524
+ const newManager = new KeystoreManager(mergeKeystores(keyStores));
1525
+ await newManager.validateSigners();
1526
+ ValidatorClient.validateKeyStoreConfiguration(newManager, this.log);
1527
+
1528
+ // Validate that every validator's publisher keys overlap with the L1 signers
1529
+ // that were initialized at startup. Publishers cannot be hot-reloaded, so a
1530
+ // validator with a publisher key that doesn't match any existing L1 signer
1531
+ // would silently fail on every proposer slot.
1532
+ if (this.keyStoreManager && this.sequencer) {
1533
+ const oldAdapter = NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager);
1534
+ const availablePublishers = new Set(
1535
+ oldAdapter
1536
+ .getAttesterAddresses()
1537
+ .flatMap(a => oldAdapter.getPublisherAddresses(a).map(p => p.toString().toLowerCase())),
1538
+ );
1539
+
1540
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1541
+ for (const attester of newAdapter.getAttesterAddresses()) {
1542
+ const pubs = newAdapter.getPublisherAddresses(attester);
1543
+ if (pubs.length > 0 && !pubs.some(p => availablePublishers.has(p.toString().toLowerCase()))) {
1544
+ throw new BadRequestError(
1545
+ `Cannot reload keystore: validator ${attester} has publisher keys ` +
1546
+ `[${pubs.map(p => p.toString()).join(', ')}] but none match the L1 signers initialized at startup ` +
1547
+ `[${[...availablePublishers].join(', ')}]. Publishers cannot be hot-reloaded — ` +
1548
+ `use an existing publisher key or restart the node.`,
1549
+ );
1550
+ }
1551
+ }
1552
+ }
1553
+
1554
+ // Build adapters for old and new keystores to compute diff
1555
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1556
+ const newAddresses = newAdapter.getAttesterAddresses();
1557
+ const oldAddresses = this.keyStoreManager
1558
+ ? NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager).getAttesterAddresses()
1559
+ : [];
1560
+
1561
+ const oldSet = new Set(oldAddresses.map(a => a.toString()));
1562
+ const newSet = new Set(newAddresses.map(a => a.toString()));
1563
+ const added = newAddresses.filter(a => !oldSet.has(a.toString()));
1564
+ const removed = oldAddresses.filter(a => !newSet.has(a.toString()));
1565
+
1566
+ if (added.length > 0) {
1567
+ this.log.info(`Keystore reload: adding attester keys: ${added.map(a => a.toString()).join(', ')}`);
1568
+ }
1569
+ if (removed.length > 0) {
1570
+ this.log.info(`Keystore reload: removing attester keys: ${removed.map(a => a.toString()).join(', ')}`);
1571
+ }
1572
+ if (added.length === 0 && removed.length === 0) {
1573
+ this.log.info('Keystore reload: attester keys unchanged');
1574
+ }
1575
+
1576
+ // Update the validator client (coinbase, feeRecipient, attester keys)
1577
+ this.validatorClient.reloadKeystore(newManager);
1578
+
1579
+ // Update the publisher factory's keystore so newly-added validators
1580
+ // can be matched to existing publisher keys when proposing blocks.
1581
+ if (this.sequencer) {
1582
+ this.sequencer.updatePublisherNodeKeyStore(newAdapter);
1583
+ }
1584
+
1585
+ // Update slasher's "don't-slash-self" list with new validator addresses
1586
+ if (this.slasherClient && !this.config.slashSelfAllowed) {
1587
+ const slashValidatorsNever = unique(
1588
+ [...(this.config.slashValidatorsNever ?? []), ...newAddresses].map(a => a.toString()),
1589
+ ).map(EthAddress.fromString);
1590
+ this.slasherClient.updateConfig({ slashValidatorsNever });
1591
+ }
1592
+
1593
+ this.keyStoreManager = newManager;
1594
+ this.log.info('Keystore reloaded: coinbase, feeRecipient, and attester keys updated');
1595
+ }
1596
+
1597
+ #getInitialHeaderHash(): Promise<BlockHash> {
1598
+ if (!this.initialHeaderHashPromise) {
1599
+ this.initialHeaderHashPromise = this.worldStateSynchronizer.getCommitted().getInitialHeader().hash();
1600
+ }
1601
+ return this.initialHeaderHashPromise;
1602
+ }
1603
+
1372
1604
  /**
1373
1605
  * Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
1374
- * @param blockNumber - The block number at which to get the data.
1606
+ * @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
1375
1607
  * @returns An instance of a committed MerkleTreeOperations
1376
1608
  */
1377
- async #getWorldState(blockNumber: L2BlockNumber) {
1378
- if (typeof blockNumber === 'number' && blockNumber < INITIAL_L2_BLOCK_NUM - 1) {
1379
- throw new Error('Invalid block number to get world state for: ' + blockNumber);
1380
- }
1381
-
1382
- let blockSyncedTo: number = 0;
1609
+ protected async getWorldState(block: BlockParameter) {
1610
+ let blockSyncedTo: BlockNumber = BlockNumber.ZERO;
1383
1611
  try {
1384
1612
  // Attempt to sync the world state if necessary
1385
1613
  blockSyncedTo = await this.#syncWorldState();
@@ -1387,24 +1615,78 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1387
1615
  this.log.error(`Error getting world state: ${err}`);
1388
1616
  }
1389
1617
 
1390
- // using a snapshot could be less efficient than using the committed db
1391
- if (blockNumber === 'latest' /*|| blockNumber === blockSyncedTo*/) {
1392
- this.log.debug(`Using committed db for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1618
+ if (block === 'latest') {
1619
+ this.log.debug(`Using committed db for block 'latest', world state synced upto ${blockSyncedTo}`);
1393
1620
  return this.worldStateSynchronizer.getCommitted();
1394
- } else if (blockNumber <= blockSyncedTo) {
1395
- this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1396
- return this.worldStateSynchronizer.getSnapshot(blockNumber);
1621
+ }
1622
+
1623
+ // Get the block number, either directly from the parameter or by quering the archiver with the block hash
1624
+ let blockNumber: BlockNumber;
1625
+ if (BlockHash.isBlockHash(block)) {
1626
+ const initialBlockHash = await this.#getInitialHeaderHash();
1627
+ if (block.equals(initialBlockHash)) {
1628
+ // Block source doesn't handle initial header so we need to handle the case separately.
1629
+ return this.worldStateSynchronizer.getSnapshot(BlockNumber.ZERO);
1630
+ }
1631
+
1632
+ const header = await this.blockSource.getBlockHeaderByHash(block);
1633
+ if (!header) {
1634
+ throw new Error(
1635
+ `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.`,
1636
+ );
1637
+ }
1638
+
1639
+ blockNumber = header.getBlockNumber();
1397
1640
  } else {
1398
- throw new Error(`Block ${blockNumber} not yet synced`);
1641
+ blockNumber = block as BlockNumber;
1642
+ }
1643
+
1644
+ // Check it's within world state sync range
1645
+ if (blockNumber > blockSyncedTo) {
1646
+ throw new Error(`Queried block ${block} not yet synced by the node (node is synced upto ${blockSyncedTo}).`);
1647
+ }
1648
+ this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1649
+
1650
+ const snapshot = this.worldStateSynchronizer.getSnapshot(blockNumber);
1651
+
1652
+ // Double-check world-state synced to the same block hash as was requested
1653
+ if (BlockHash.isBlockHash(block)) {
1654
+ const blockHash = await snapshot.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
1655
+ if (!blockHash || !new BlockHash(blockHash).equals(block)) {
1656
+ throw new Error(
1657
+ `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.`,
1658
+ );
1659
+ }
1660
+ }
1661
+
1662
+ return snapshot;
1663
+ }
1664
+
1665
+ /** Resolves a block parameter to a block number. */
1666
+ protected async resolveBlockNumber(block: BlockParameter): Promise<BlockNumber> {
1667
+ if (block === 'latest') {
1668
+ return BlockNumber(await this.blockSource.getBlockNumber());
1669
+ }
1670
+ if (BlockHash.isBlockHash(block)) {
1671
+ const initialBlockHash = await this.#getInitialHeaderHash();
1672
+ if (block.equals(initialBlockHash)) {
1673
+ return BlockNumber.ZERO;
1674
+ }
1675
+ const header = await this.blockSource.getBlockHeaderByHash(block);
1676
+ if (!header) {
1677
+ throw new Error(`Block hash ${block.toString()} not found.`);
1678
+ }
1679
+ return header.getBlockNumber();
1399
1680
  }
1681
+ return block as BlockNumber;
1400
1682
  }
1401
1683
 
1402
1684
  /**
1403
1685
  * Ensure we fully sync the world state
1404
1686
  * @returns A promise that fulfils once the world state is synced
1405
1687
  */
1406
- async #syncWorldState(): Promise<number> {
1688
+ async #syncWorldState(): Promise<BlockNumber> {
1407
1689
  const blockSourceHeight = await this.blockSource.getBlockNumber();
1408
- return this.worldStateSynchronizer.syncImmediate(blockSourceHeight);
1690
+ return await this.worldStateSynchronizer.syncImmediate(blockSourceHeight);
1409
1691
  }
1410
1692
  }