@aztec/aztec-node 0.0.1-commit.5daedc8 → 0.0.1-commit.6201a7b05

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,42 +1,43 @@
1
1
  import { Archiver, createArchiver } from '@aztec/archiver';
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';
2
+ import { BBCircuitVerifier, BatchChonkVerifier, QueuedIVCVerifier } from '@aztec/bb-prover';
3
+ import { TestCircuitVerifier } from '@aztec/bb-prover/test';
4
+ import { type BlobClientInterface, createBlobClientWithFileStores } from '@aztec/blob-client/client';
5
+ import { Blob } from '@aztec/blob-lib';
6
+ import { ARCHIVE_HEIGHT, type L1_TO_L2_MSG_TREE_HEIGHT, type NOTE_HASH_TREE_HEIGHT } from '@aztec/constants';
12
7
  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';
8
+ import { createEthereumChain } from '@aztec/ethereum/chain';
9
+ import { getPublicClient, makeL1HttpTransport } from '@aztec/ethereum/client';
10
+ import { RegistryContract, RollupContract } from '@aztec/ethereum/contracts';
11
+ import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
12
+ import type { L1TxUtils } from '@aztec/ethereum/l1-tx-utils';
13
+ import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
14
+ import { chunkBy, compactArray, pick, unique } from '@aztec/foundation/collection';
15
+ import { Fr } from '@aztec/foundation/curves/bn254';
22
16
  import { EthAddress } from '@aztec/foundation/eth-address';
23
- import { Fr } from '@aztec/foundation/fields';
24
17
  import { BadRequestError } from '@aztec/foundation/json-rpc';
25
18
  import { type Logger, createLogger } from '@aztec/foundation/log';
19
+ import { retryUntil } from '@aztec/foundation/retry';
26
20
  import { count } from '@aztec/foundation/string';
27
21
  import { DateProvider, Timer } from '@aztec/foundation/timer';
28
22
  import { MembershipWitness, SiblingPath } from '@aztec/foundation/trees';
29
- import { KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
23
+ import { type KeyStore, KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
30
24
  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';
25
+ import { createForwarderL1TxUtilsFromSigners, createL1TxUtilsFromSigners } from '@aztec/node-lib/factories';
26
+ import {
27
+ type P2P,
28
+ type P2PClientDeps,
29
+ createP2PClient,
30
+ createTxValidatorForAcceptingTxsOverRPC,
31
+ getDefaultAllowedSetupFunctions,
32
+ } from '@aztec/p2p';
33
33
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
34
+ import { type ProverNode, type ProverNodeDeps, createProverNode } from '@aztec/prover-node';
35
+ import { createKeyStoreForProver } from '@aztec/prover-node/config';
34
36
  import {
35
- BlockBuilder,
37
+ FeeProviderImpl,
36
38
  GlobalVariableBuilder,
37
39
  SequencerClient,
38
40
  type SequencerPublisher,
39
- createValidatorForAcceptingTxs,
40
41
  } from '@aztec/sequencer-client';
41
42
  import { PublicProcessorFactory } from '@aztec/simulator/server';
42
43
  import {
@@ -46,15 +47,15 @@ import {
46
47
  type Watcher,
47
48
  createSlasher,
48
49
  } from '@aztec/slasher';
49
- import { PublicSimulatorConfig } from '@aztec/stdlib/avm';
50
+ import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
50
51
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
51
52
  import {
52
- type InBlock,
53
- type L2Block,
54
- L2BlockHash,
55
- type L2BlockNumber,
53
+ BlockHash,
54
+ type BlockParameter,
55
+ type DataInBlock,
56
+ L2Block,
56
57
  type L2BlockSource,
57
- type PublishedL2Block,
58
+ inspectBlockParameter,
58
59
  } from '@aztec/stdlib/block';
59
60
  import type {
60
61
  ContractClassPublic,
@@ -63,16 +64,24 @@ import type {
63
64
  NodeInfo,
64
65
  ProtocolContractAddresses,
65
66
  } from '@aztec/stdlib/contract';
66
- import { GasFees } from '@aztec/stdlib/gas';
67
+ import { GasFees, type ManaUsageEstimate } from '@aztec/stdlib/gas';
67
68
  import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
68
- import {
69
- type AztecNode,
70
- type AztecNodeAdmin,
71
- type AztecNodeAdminConfig,
72
- AztecNodeAdminConfigSchema,
73
- type GetContractClassLogsResponse,
74
- type GetPublicLogsResponse,
69
+ import type {
70
+ AztecNode,
71
+ AztecNodeAdmin,
72
+ AztecNodeAdminConfig,
73
+ AztecNodeDebug,
74
+ BlockIncludeOptions,
75
+ BlockResponse,
76
+ ChainTip,
77
+ ChainTips,
78
+ CheckpointIncludeOptions,
79
+ CheckpointParameter,
80
+ CheckpointResponse,
81
+ GetContractClassLogsResponse,
82
+ GetPublicLogsResponse,
75
83
  } from '@aztec/stdlib/interfaces/client';
84
+ import { AztecNodeAdminConfigSchema } from '@aztec/stdlib/interfaces/client';
76
85
  import {
77
86
  type AllowedElement,
78
87
  type ClientProtocolCircuitVerifier,
@@ -82,14 +91,15 @@ import {
82
91
  type WorldStateSynchronizer,
83
92
  tryStop,
84
93
  } from '@aztec/stdlib/interfaces/server';
85
- import type { LogFilter, PrivateLog, TxScopedL2Log } from '@aztec/stdlib/logs';
94
+ import type { DebugLogStore, LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
95
+ import { InMemoryDebugLogStore, NullDebugLogStore } from '@aztec/stdlib/logs';
86
96
  import { InboxLeaf, type L1ToL2MessageSource } from '@aztec/stdlib/messaging';
87
- import { P2PClientType } from '@aztec/stdlib/p2p';
88
- import type { Offense, SlashPayloadRound } from '@aztec/stdlib/slashing';
89
- import type { NullifierLeafPreimage, PublicDataTreeLeaf, PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
97
+ import type { Offense } from '@aztec/stdlib/slashing';
98
+ import type { NullifierLeafPreimage, PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
90
99
  import { MerkleTreeId, NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
91
100
  import {
92
101
  type BlockHeader,
102
+ type FeeProvider,
93
103
  type GlobalVariableBuilder as GlobalVariableBuilderInterface,
94
104
  type IndexedTxEffect,
95
105
  PublicSimulationOutput,
@@ -101,6 +111,7 @@ import {
101
111
  } from '@aztec/stdlib/tx';
102
112
  import { getPackageVersion } from '@aztec/stdlib/update-checker';
103
113
  import type { SingleValidatorStats, ValidatorsStats } from '@aztec/stdlib/validators';
114
+ import type { GenesisData } from '@aztec/stdlib/world-state';
104
115
  import {
105
116
  Attributes,
106
117
  type TelemetryClient,
@@ -110,25 +121,35 @@ import {
110
121
  trackSpan,
111
122
  } from '@aztec/telemetry-client';
112
123
  import {
124
+ FullNodeCheckpointsBuilder as CheckpointsBuilder,
125
+ FullNodeCheckpointsBuilder,
113
126
  NodeKeystoreAdapter,
114
127
  ValidatorClient,
115
- createBlockProposalHandler,
128
+ createProposalHandler,
116
129
  createValidatorClient,
117
130
  } from '@aztec/validator-client';
131
+ import type { SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
118
132
  import { createWorldStateSynchronizer } from '@aztec/world-state';
119
133
 
120
- import { createPublicClient, fallback, http } from 'viem';
134
+ import { createPublicClient } from 'viem';
121
135
 
122
136
  import { createSentinel } from '../sentinel/factory.js';
123
137
  import { Sentinel } from '../sentinel/sentinel.js';
138
+ import {
139
+ blockResponseFromBlockData,
140
+ blockResponseFromL2Block,
141
+ checkpointResponseFromCheckpointData,
142
+ checkpointResponseFromPublishedCheckpoint,
143
+ } from './block_response_helpers.js';
124
144
  import { type AztecNodeConfig, createKeyStoreForValidator } from './config.js';
125
145
  import { NodeMetrics } from './node_metrics.js';
126
146
 
127
147
  /**
128
148
  * The aztec node.
129
149
  */
130
- export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
150
+ export class AztecNodeService implements AztecNode, AztecNodeAdmin, AztecNodeDebug, Traceable {
131
151
  private metrics: NodeMetrics;
152
+ private initialHeaderHashPromise: Promise<BlockHash> | undefined = undefined;
132
153
 
133
154
  // Prevent two snapshot operations to happen simultaneously
134
155
  private isUploadingSnapshot = false;
@@ -144,23 +165,43 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
144
165
  protected readonly l1ToL2MessageSource: L1ToL2MessageSource,
145
166
  protected readonly worldStateSynchronizer: WorldStateSynchronizer,
146
167
  protected readonly sequencer: SequencerClient | undefined,
168
+ protected readonly proverNode: ProverNode | undefined,
147
169
  protected readonly slasherClient: SlasherClientInterface | undefined,
148
170
  protected readonly validatorsSentinel: Sentinel | undefined,
149
171
  protected readonly epochPruneWatcher: EpochPruneWatcher | undefined,
172
+ protected readonly attestationsBlockWatcher: AttestationsBlockWatcher | undefined,
150
173
  protected readonly l1ChainId: number,
151
174
  protected readonly version: number,
152
175
  protected readonly globalVariableBuilder: GlobalVariableBuilderInterface,
176
+ protected readonly feeProvider: FeeProvider,
153
177
  protected readonly epochCache: EpochCacheInterface,
154
178
  protected readonly packageVersion: string,
155
- private proofVerifier: ClientProtocolCircuitVerifier,
179
+ private peerProofVerifier: ClientProtocolCircuitVerifier,
180
+ private rpcProofVerifier: ClientProtocolCircuitVerifier,
156
181
  private telemetry: TelemetryClient = getTelemetryClient(),
157
182
  private log = createLogger('node'),
183
+ private blobClient?: BlobClientInterface,
184
+ private validatorClient?: ValidatorClient,
185
+ private keyStoreManager?: KeystoreManager,
186
+ private debugLogStore: DebugLogStore = new NullDebugLogStore(),
158
187
  ) {
159
188
  this.metrics = new NodeMetrics(telemetry, 'AztecNodeService');
160
189
  this.tracer = telemetry.getTracer('AztecNodeService');
161
190
 
162
191
  this.log.info(`Aztec Node version: ${this.packageVersion}`);
163
192
  this.log.info(`Aztec Node started on chain 0x${l1ChainId.toString(16)}`, config.l1Contracts);
193
+
194
+ // A defensive check that protects us against introducing a bug in the complex `createAndSync` function. We must
195
+ // never have debugLogStore enabled when not in test mode because then we would be accumulating debug logs in
196
+ // memory which could be a DoS vector on the sequencer (since no fees are paid for debug logs).
197
+ if (debugLogStore.isEnabled && config.realProofs) {
198
+ throw new Error('debugLogStore should never be enabled when realProofs are set');
199
+ }
200
+ }
201
+
202
+ /** @internal Exposed for testing — returns the RPC proof verifier. */
203
+ public getProofVerifier(): ClientProtocolCircuitVerifier {
204
+ return this.rpcProofVerifier;
164
205
  }
165
206
 
166
207
  public async getWorldStateSyncStatus(): Promise<WorldStateSyncStatus> {
@@ -168,10 +209,281 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
168
209
  return status.syncSummary;
169
210
  }
170
211
 
212
+ public async getChainTips(): Promise<ChainTips> {
213
+ const { proposed, checkpointed, proven, finalized } = await this.blockSource.getL2Tips();
214
+ return { proposed, checkpointed, proven, finalized };
215
+ }
216
+
171
217
  public getL2Tips() {
172
218
  return this.blockSource.getL2Tips();
173
219
  }
174
220
 
221
+ public async getBlockHeader(number: BlockNumber | 'latest'): Promise<BlockHeader | undefined> {
222
+ const resolvedNumber = number === 'latest' ? await this.blockSource.getBlockNumber() : number;
223
+ if (resolvedNumber === BlockNumber.ZERO) {
224
+ return this.worldStateSynchronizer.getCommitted().getInitialHeader();
225
+ }
226
+ return this.blockSource.getBlockHeader(resolvedNumber);
227
+ }
228
+
229
+ public async getCheckpointedBlocks(from: BlockNumber, limit: number) {
230
+ return (await this.blockSource.getCheckpointedBlocks(from, limit)) ?? [];
231
+ }
232
+
233
+ public getCheckpointsDataForEpoch(epoch: EpochNumber) {
234
+ return this.blockSource.getCheckpointsDataForEpoch(epoch);
235
+ }
236
+
237
+ public getBlockNumber(tip?: ChainTip): Promise<BlockNumber> {
238
+ switch (tip) {
239
+ case undefined:
240
+ case 'proposed':
241
+ return this.blockSource.getBlockNumber();
242
+ case 'checkpointed':
243
+ return this.blockSource.getCheckpointedL2BlockNumber();
244
+ case 'proven':
245
+ return this.blockSource.getProvenBlockNumber();
246
+ case 'finalized':
247
+ return this.blockSource.getFinalizedL2BlockNumber();
248
+ }
249
+ }
250
+
251
+ public async getCheckpointNumber(tip?: ChainTip): Promise<CheckpointNumber> {
252
+ switch (tip) {
253
+ case undefined:
254
+ case 'proposed':
255
+ case 'checkpointed':
256
+ return await this.blockSource.getCheckpointNumber();
257
+ case 'proven':
258
+ case 'finalized': {
259
+ const tips = await this.blockSource.getL2Tips();
260
+ return tip === 'proven' ? tips.proven.checkpoint.number : tips.finalized.checkpoint.number;
261
+ }
262
+ }
263
+ }
264
+
265
+ private isChainTip(value: unknown): value is ChainTip {
266
+ return value === 'proposed' || value === 'checkpointed' || value === 'proven' || value === 'finalized';
267
+ }
268
+
269
+ private async resolveBlockParameter(
270
+ param: BlockParameter,
271
+ ): Promise<{ number?: BlockNumber; hash?: BlockHash; archive?: Fr }> {
272
+ if (BlockHash.isBlockHash(param)) {
273
+ return { hash: param };
274
+ }
275
+ if (typeof param === 'number') {
276
+ return { number: param as BlockNumber };
277
+ }
278
+ if (param === 'latest') {
279
+ return { number: await this.blockSource.getBlockNumber() };
280
+ }
281
+ if (this.isChainTip(param)) {
282
+ return { number: await this.getBlockNumber(param) };
283
+ }
284
+ if (typeof param === 'object' && param !== null) {
285
+ if ('number' in param) {
286
+ return { number: param.number };
287
+ }
288
+ if ('hash' in param) {
289
+ return { hash: param.hash };
290
+ }
291
+ if ('archive' in param) {
292
+ return { archive: param.archive };
293
+ }
294
+ }
295
+ throw new BadRequestError(`Invalid BlockParameter: ${JSON.stringify(param)}`);
296
+ }
297
+
298
+ private async resolveCheckpointParameter(
299
+ param: CheckpointParameter,
300
+ ): Promise<{ number?: CheckpointNumber; slot?: SlotNumber }> {
301
+ if (typeof param === 'number') {
302
+ return { number: param as CheckpointNumber };
303
+ }
304
+ if (param === 'latest') {
305
+ return { number: await this.blockSource.getCheckpointNumber() };
306
+ }
307
+ if (this.isChainTip(param)) {
308
+ return { number: await this.getCheckpointNumber(param) };
309
+ }
310
+ if (typeof param === 'object' && param !== null) {
311
+ if ('number' in param) {
312
+ return { number: param.number };
313
+ }
314
+ if ('slot' in param) {
315
+ return { slot: param.slot };
316
+ }
317
+ }
318
+ throw new BadRequestError(`Invalid CheckpointParameter: ${JSON.stringify(param)}`);
319
+ }
320
+
321
+ public async getBlock<Opts extends BlockIncludeOptions = {}>(
322
+ param: BlockParameter,
323
+ options: Opts = {} as Opts,
324
+ ): Promise<BlockResponse<Opts> | undefined> {
325
+ const resolved = await this.resolveBlockParameter(param);
326
+ const wantTxs = !!options.includeTransactions;
327
+ const wantContext = !!options.includeL1PublishInfo || !!options.includeAttestations;
328
+
329
+ if (resolved.hash !== undefined) {
330
+ const initial = await this.#getInitialHeaderHash();
331
+ if (resolved.hash.equals(initial)) {
332
+ return (await this.buildGenesisBlockResponse(options)) as BlockResponse<Opts>;
333
+ }
334
+ if (wantTxs) {
335
+ const block = await this.blockSource.getL2BlockByHash(resolved.hash);
336
+ if (!block) {
337
+ return undefined;
338
+ }
339
+ const ctx = wantContext ? await this.blockSource.getBlockDataWithCheckpointContext(block.number) : undefined;
340
+ return (await blockResponseFromL2Block(block, options, ctx)) as BlockResponse<Opts>;
341
+ }
342
+ const data = await this.blockSource.getBlockHeaderByHash(resolved.hash);
343
+ if (!data) {
344
+ return undefined;
345
+ }
346
+ const blockNumber = data.globalVariables.blockNumber;
347
+ const ctx = wantContext ? await this.blockSource.getBlockDataWithCheckpointContext(blockNumber) : undefined;
348
+ if (ctx) {
349
+ return blockResponseFromBlockData(ctx.data, blockNumber, options, ctx) as BlockResponse<Opts>;
350
+ }
351
+ const blockData = await this.blockSource.getBlockData(blockNumber);
352
+ if (!blockData) {
353
+ return undefined;
354
+ }
355
+ return blockResponseFromBlockData(blockData, blockNumber, options) as BlockResponse<Opts>;
356
+ }
357
+
358
+ if (resolved.archive !== undefined) {
359
+ if (wantTxs) {
360
+ const block = await this.blockSource.getL2BlockByArchive(resolved.archive);
361
+ if (!block) {
362
+ return undefined;
363
+ }
364
+ const ctx = wantContext ? await this.blockSource.getBlockDataWithCheckpointContext(block.number) : undefined;
365
+ return (await blockResponseFromL2Block(block, options, ctx)) as BlockResponse<Opts>;
366
+ }
367
+ const data = await this.blockSource.getBlockDataByArchive(resolved.archive);
368
+ if (!data) {
369
+ return undefined;
370
+ }
371
+ const blockNumber = data.header.globalVariables.blockNumber;
372
+ const ctx = wantContext ? await this.blockSource.getBlockDataWithCheckpointContext(blockNumber) : undefined;
373
+ return blockResponseFromBlockData(data, blockNumber, options, ctx) as BlockResponse<Opts>;
374
+ }
375
+
376
+ const blockNumber = resolved.number!;
377
+ if (blockNumber === BlockNumber.ZERO) {
378
+ return (await this.buildGenesisBlockResponse(options)) as BlockResponse<Opts>;
379
+ }
380
+ if (wantTxs) {
381
+ const block = await this.blockSource.getL2Block(blockNumber);
382
+ if (!block) {
383
+ return undefined;
384
+ }
385
+ const ctx = wantContext ? await this.blockSource.getBlockDataWithCheckpointContext(blockNumber) : undefined;
386
+ return (await blockResponseFromL2Block(block, options, ctx)) as BlockResponse<Opts>;
387
+ }
388
+ const ctx = await this.blockSource.getBlockDataWithCheckpointContext(blockNumber);
389
+ if (!ctx) {
390
+ return undefined;
391
+ }
392
+ return blockResponseFromBlockData(ctx.data, blockNumber, options, ctx) as BlockResponse<Opts>;
393
+ }
394
+
395
+ public async getBlocks<Opts extends BlockIncludeOptions = {}>(
396
+ from: BlockNumber,
397
+ limit: number,
398
+ options: Opts = {} as Opts,
399
+ ): Promise<BlockResponse<Opts>[]> {
400
+ const wantTxs = !!options.includeTransactions;
401
+ const wantContext = !!options.includeL1PublishInfo || !!options.includeAttestations;
402
+ if (wantTxs) {
403
+ const blocks = await this.blockSource.getBlocks(from, limit);
404
+ return (await Promise.all(
405
+ blocks.map(async block => {
406
+ const ctx = wantContext ? await this.blockSource.getBlockDataWithCheckpointContext(block.number) : undefined;
407
+ return blockResponseFromL2Block(block, options, ctx);
408
+ }),
409
+ )) as BlockResponse<Opts>[];
410
+ }
411
+ const results: BlockResponse<Opts>[] = [];
412
+ for (let i = 0; i < limit; i++) {
413
+ const blockNumber = BlockNumber(from + i);
414
+ const ctx = await this.blockSource.getBlockDataWithCheckpointContext(blockNumber);
415
+ if (!ctx) {
416
+ break;
417
+ }
418
+ results.push(blockResponseFromBlockData(ctx.data, blockNumber, options, ctx) as BlockResponse<Opts>);
419
+ }
420
+ return results;
421
+ }
422
+
423
+ public async getCheckpoint<Opts extends CheckpointIncludeOptions = {}>(
424
+ param: CheckpointParameter,
425
+ options: Opts = {} as Opts,
426
+ ): Promise<CheckpointResponse<Opts> | undefined> {
427
+ const resolved = await this.resolveCheckpointParameter(param);
428
+ let checkpointNumber = resolved.number;
429
+ if (checkpointNumber === undefined && resolved.slot !== undefined) {
430
+ checkpointNumber = await this.blockSource.getCheckpointNumberBySlot(resolved.slot);
431
+ }
432
+ if (checkpointNumber === undefined) {
433
+ return undefined;
434
+ }
435
+ if (options.includeBlocks) {
436
+ const [checkpoint] = await this.blockSource.getCheckpoints(checkpointNumber, 1);
437
+ if (!checkpoint) {
438
+ return undefined;
439
+ }
440
+ return (await checkpointResponseFromPublishedCheckpoint(checkpoint, options)) as CheckpointResponse<Opts>;
441
+ }
442
+ const data = await this.blockSource.getCheckpointData(checkpointNumber);
443
+ if (!data) {
444
+ return undefined;
445
+ }
446
+ return checkpointResponseFromCheckpointData(data, options) as CheckpointResponse<Opts>;
447
+ }
448
+
449
+ public async getCheckpoints<Opts extends CheckpointIncludeOptions = {}>(
450
+ from: CheckpointNumber,
451
+ limit: number,
452
+ options: Opts = {} as Opts,
453
+ ): Promise<CheckpointResponse<Opts>[]> {
454
+ if (options.includeBlocks) {
455
+ const checkpoints = await this.blockSource.getCheckpoints(from, limit);
456
+ return (await Promise.all(
457
+ checkpoints.map(cp => checkpointResponseFromPublishedCheckpoint(cp, options)),
458
+ )) as CheckpointResponse<Opts>[];
459
+ }
460
+ const datas = await this.blockSource.getCheckpointDataRange(from, limit);
461
+ return datas.map(d => checkpointResponseFromCheckpointData(d, options)) as CheckpointResponse<Opts>[];
462
+ }
463
+
464
+ private async buildGenesisBlockResponse(options: BlockIncludeOptions): Promise<BlockResponse> {
465
+ const initial = this.worldStateSynchronizer.getCommitted().getInitialHeader();
466
+ const empty = L2Block.empty(initial);
467
+ const response: BlockResponse = {
468
+ header: empty.header,
469
+ archive: empty.archive,
470
+ hash: await this.#getInitialHeaderHash(),
471
+ checkpointNumber: empty.checkpointNumber,
472
+ indexWithinCheckpoint: empty.indexWithinCheckpoint,
473
+ number: empty.number,
474
+ };
475
+ if (options.includeTransactions) {
476
+ (response as BlockResponse).body = empty.body;
477
+ }
478
+ if (options.includeL1PublishInfo) {
479
+ (response as BlockResponse).l1 = { published: false };
480
+ }
481
+ if (options.includeAttestations) {
482
+ (response as BlockResponse).attestations = [];
483
+ }
484
+ return response;
485
+ }
486
+
175
487
  /**
176
488
  * initializes the Aztec Node, wait for component to sync.
177
489
  * @param config - The configuration to be used by the aztec node.
@@ -184,12 +496,14 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
184
496
  logger?: Logger;
185
497
  publisher?: SequencerPublisher;
186
498
  dateProvider?: DateProvider;
187
- blobSinkClient?: BlobSinkClientInterface;
188
- p2pClientDeps?: P2PClientDeps<P2PClientType.Full>;
499
+ p2pClientDeps?: P2PClientDeps;
500
+ proverNodeDeps?: Partial<ProverNodeDeps>;
501
+ slashingProtectionDb?: SlashingProtectionDatabase;
189
502
  } = {},
190
503
  options: {
191
- prefilledPublicData?: PublicDataTreeLeaf[];
504
+ genesis?: GenesisData;
192
505
  dontStartSequencer?: boolean;
506
+ dontStartProverNode?: boolean;
193
507
  } = {},
194
508
  ): Promise<AztecNodeService> {
195
509
  const config = { ...inputConfig }; // Copy the config so we dont mutate the input object
@@ -197,20 +511,31 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
197
511
  const packageVersion = getPackageVersion() ?? '';
198
512
  const telemetry = deps.telemetry ?? getTelemetryClient();
199
513
  const dateProvider = deps.dateProvider ?? new DateProvider();
200
- const blobSinkClient =
201
- deps.blobSinkClient ?? createBlobSinkClient(config, { logger: createLogger('node:blob-sink:client') });
202
514
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
203
515
 
204
- // Build a key store from file if given or from environment otherwise
516
+ // Build a key store from file if given or from environment otherwise.
517
+ // We keep the raw KeyStore available so we can merge with prover keys if enableProverNode is set.
205
518
  let keyStoreManager: KeystoreManager | undefined;
206
519
  const keyStoreProvided = config.keyStoreDirectory !== undefined && config.keyStoreDirectory.length > 0;
207
520
  if (keyStoreProvided) {
208
521
  const keyStores = loadKeystores(config.keyStoreDirectory!);
209
522
  keyStoreManager = new KeystoreManager(mergeKeystores(keyStores));
210
523
  } else {
211
- const keyStore = createKeyStoreForValidator(config);
212
- if (keyStore) {
213
- keyStoreManager = new KeystoreManager(keyStore);
524
+ const rawKeyStores: KeyStore[] = [];
525
+ const validatorKeyStore = createKeyStoreForValidator(config);
526
+ if (validatorKeyStore) {
527
+ rawKeyStores.push(validatorKeyStore);
528
+ }
529
+ if (config.enableProverNode) {
530
+ const proverKeyStore = createKeyStoreForProver(config);
531
+ if (proverKeyStore) {
532
+ rawKeyStores.push(proverKeyStore);
533
+ }
534
+ }
535
+ if (rawKeyStores.length > 0) {
536
+ keyStoreManager = new KeystoreManager(
537
+ rawKeyStores.length === 1 ? rawKeyStores[0] : mergeKeystores(rawKeyStores),
538
+ );
214
539
  }
215
540
  }
216
541
 
@@ -221,10 +546,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
221
546
  if (keyStoreManager === undefined) {
222
547
  throw new Error('Failed to create key store, a requirement for running a validator');
223
548
  }
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
- );
549
+ if (!keyStoreProvided && process.env.NODE_ENV !== 'test') {
550
+ log.warn("Keystore created from env: it's recommended to use a file-based key store for production");
228
551
  }
229
552
  ValidatorClient.validateKeyStoreConfiguration(keyStoreManager, log);
230
553
  }
@@ -238,7 +561,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
238
561
 
239
562
  const publicClient = createPublicClient({
240
563
  chain: ethereumChain.chainInfo,
241
- transport: fallback(config.l1RpcUrls.map((url: string) => http(url))),
564
+ transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
242
565
  pollingInterval: config.viemPollingIntervalMS,
243
566
  });
244
567
 
@@ -252,10 +575,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
252
575
  config.l1Contracts = { ...config.l1Contracts, ...l1ContractsAddresses };
253
576
 
254
577
  const rollupContract = new RollupContract(publicClient, config.l1Contracts.rollupAddress.toString());
255
- const [l1GenesisTime, slotDuration, rollupVersionFromRollup] = await Promise.all([
578
+ const [l1GenesisTime, slotDuration, rollupVersionFromRollup, rollupManaLimit] = await Promise.all([
256
579
  rollupContract.getL1GenesisTime(),
257
580
  rollupContract.getSlotDuration(),
258
581
  rollupContract.getVersion(),
582
+ rollupContract.getManaLimit().then(Number),
259
583
  ] as const);
260
584
 
261
585
  config.rollupVersion ??= Number(rollupVersionFromRollup);
@@ -266,216 +590,361 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
266
590
  );
267
591
  }
268
592
 
593
+ const blobClient = await createBlobClientWithFileStores(config, log.createChild('blob-client'));
594
+
269
595
  // attempt snapshot sync if possible
270
596
  await trySnapshotSync(config, log);
271
597
 
272
598
  const epochCache = await EpochCache.create(config.l1Contracts.rollupAddress, config, { dateProvider });
273
599
 
274
- const archiver = await createArchiver(
275
- config,
276
- { blobSinkClient, epochCache, telemetry, dateProvider },
277
- { blockUntilSync: !config.skipArchiverInitialSync },
278
- );
600
+ // Track started resources so we can clean up on partial failure during node creation.
601
+ const started: { stop?(): Promise<void> | void }[] = [];
602
+ try {
603
+ const archiver = await createArchiver(
604
+ config,
605
+ { blobClient, epochCache, telemetry, dateProvider },
606
+ { blockUntilSync: !config.skipArchiverInitialSync },
607
+ );
608
+ started.push(archiver);
609
+
610
+ // now create the merkle trees and the world state synchronizer
611
+ const worldStateSynchronizer = await createWorldStateSynchronizer(config, archiver, options.genesis, telemetry);
612
+ started.push(worldStateSynchronizer);
613
+ const useRealVerifiers = config.realProofs || config.debugForceTxProofVerification;
614
+ let peerProofVerifier: ClientProtocolCircuitVerifier;
615
+ let rpcProofVerifier: ClientProtocolCircuitVerifier;
616
+ if (useRealVerifiers) {
617
+ peerProofVerifier = await BatchChonkVerifier.new(config, config.bbChonkVerifyMaxBatch, 'peer');
618
+ const rpcVerifier = await BBCircuitVerifier.new(config);
619
+ rpcProofVerifier = new QueuedIVCVerifier(rpcVerifier, config.numConcurrentIVCVerifiers);
620
+ } else {
621
+ peerProofVerifier = new TestCircuitVerifier(config.proverTestVerificationDelayMs);
622
+ rpcProofVerifier = new TestCircuitVerifier(config.proverTestVerificationDelayMs);
623
+ }
624
+ started.push(peerProofVerifier, rpcProofVerifier);
279
625
 
280
- // now create the merkle trees and the world state synchronizer
281
- const worldStateSynchronizer = await createWorldStateSynchronizer(
282
- config,
283
- archiver,
284
- options.prefilledPublicData,
285
- telemetry,
286
- );
287
- const circuitVerifier = config.realProofs
288
- ? await BBCircuitVerifier.new(config)
289
- : new TestCircuitVerifier(config.proverTestVerificationDelayMs);
290
- if (!config.realProofs) {
291
- log.warn(`Aztec node is accepting fake proofs`);
292
- }
293
- const proofVerifier = new QueuedIVCVerifier(config, circuitVerifier);
294
-
295
- // create the tx pool and the p2p client, which will need the l2 block source
296
- const p2pClient = await createP2PClient(
297
- P2PClientType.Full,
298
- config,
299
- archiver,
300
- proofVerifier,
301
- worldStateSynchronizer,
302
- epochCache,
303
- packageVersion,
304
- dateProvider,
305
- telemetry,
306
- deps.p2pClientDeps,
307
- );
626
+ let debugLogStore: DebugLogStore;
627
+ if (!config.realProofs) {
628
+ log.warn(`Aztec node is accepting fake proofs`);
308
629
 
309
- // We should really not be modifying the config object
310
- config.txPublicSetupAllowList = config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
630
+ debugLogStore = new InMemoryDebugLogStore();
631
+ log.info(
632
+ 'Aztec node started in test mode (realProofs set to false) hence debug logs from public functions will be collected and served',
633
+ );
634
+ } else {
635
+ debugLogStore = new NullDebugLogStore();
636
+ }
311
637
 
312
- const blockBuilder = new BlockBuilder(
313
- { ...config, l1GenesisTime, slotDuration: Number(slotDuration) },
314
- worldStateSynchronizer,
315
- archiver,
316
- dateProvider,
317
- telemetry,
318
- );
638
+ const globalVariableBuilderConfig = {
639
+ l1Contracts: config.l1Contracts,
640
+ ethereumSlotDuration: config.ethereumSlotDuration,
641
+ rollupVersion: BigInt(config.rollupVersion),
642
+ l1GenesisTime,
643
+ slotDuration: Number(slotDuration),
644
+ };
319
645
 
320
- // We'll accumulate sentinel watchers here
321
- const watchers: Watcher[] = [];
322
-
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
- });
646
+ const globalVariableBuilder = new GlobalVariableBuilder(dateProvider, publicClient, globalVariableBuilderConfig);
647
+ const feeProvider = new FeeProviderImpl(dateProvider, publicClient, globalVariableBuilderConfig);
334
648
 
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();
649
+ const proverOnly = config.enableProverNode && config.disableValidator;
650
+ if (proverOnly) {
651
+ log.info('Starting in prover-only mode: skipping validator, sequencer, sentinel, and slasher subsystems');
342
652
  }
343
- }
344
653
 
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');
349
- createBlockProposalHandler(config, {
350
- blockBuilder,
654
+ // create the tx pool and the p2p client, which will need the l2 block source
655
+ const p2pClient = await createP2PClient(
656
+ config,
657
+ archiver,
658
+ peerProofVerifier,
659
+ worldStateSynchronizer,
351
660
  epochCache,
352
- blockSource: archiver,
353
- l1ToL2MessageSource: archiver,
354
- p2pClient,
661
+ feeProvider,
662
+ packageVersion,
355
663
  dateProvider,
356
664
  telemetry,
357
- }).registerForReexecution(p2pClient);
358
- }
665
+ deps.p2pClientDeps,
666
+ );
667
+ started.push(p2pClient);
668
+
669
+ // We'll accumulate sentinel watchers here
670
+ const watchers: Watcher[] = [];
671
+
672
+ // Create FullNodeCheckpointsBuilder for block proposal handling and tx validation.
673
+ // Override maxTxsPerCheckpoint with the validator-specific limit if set.
674
+ const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder(
675
+ {
676
+ ...config,
677
+ l1GenesisTime,
678
+ slotDuration: Number(slotDuration),
679
+ rollupManaLimit,
680
+ maxTxsPerCheckpoint: config.validateMaxTxsPerCheckpoint,
681
+ },
682
+ worldStateSynchronizer,
683
+ archiver,
684
+ dateProvider,
685
+ telemetry,
686
+ );
359
687
 
360
- // Start world state and wait for it to sync to the archiver.
361
- await worldStateSynchronizer.start();
688
+ let validatorClient: ValidatorClient | undefined;
689
+
690
+ if (!config.disableValidator) {
691
+ // Create validator client if required
692
+ validatorClient = await createValidatorClient(config, {
693
+ checkpointsBuilder: validatorCheckpointsBuilder,
694
+ worldState: worldStateSynchronizer,
695
+ p2pClient,
696
+ telemetry,
697
+ dateProvider,
698
+ epochCache,
699
+ blockSource: archiver,
700
+ l1ToL2MessageSource: archiver,
701
+ keyStoreManager,
702
+ blobClient,
703
+ slashingProtectionDb: deps.slashingProtectionDb,
704
+ });
705
+
706
+ // If we have a validator client, register it as a source of offenses for the slasher,
707
+ // and have it register callbacks on the p2p client *before* we start it, otherwise messages
708
+ // like attestations or auths will fail.
709
+ if (validatorClient) {
710
+ watchers.push(validatorClient);
711
+
712
+ const vc = validatorClient;
713
+ const getValidatorAddresses = () => vc.getValidatorAddresses().map(a => a.toString());
714
+ validatorClient.getProposalHandler().register(p2pClient, true, archiver, getValidatorAddresses);
715
+
716
+ if (!options.dontStartSequencer) {
717
+ await validatorClient.registerHandlers();
718
+ }
719
+ }
720
+ }
362
721
 
363
- // Start p2p. Note that it depends on world state to be running.
364
- await p2pClient.start();
722
+ // If there's no validator client, create a ProposalHandler to handle block and checkpoint proposals
723
+ // for monitoring or reexecution. Reexecution (default) allows us to follow the pending chain,
724
+ // while non-reexecution is used for validating the proposals and collecting their txs.
725
+ // Checkpoint proposals rebuild blobs if the blob client can upload blobs.
726
+ if (!validatorClient) {
727
+ const reexecute = !!config.alwaysReexecuteBlockProposals;
728
+ log.info(`Setting up proposal handler` + (reexecute ? ' with reexecution of proposals' : ''));
729
+ createProposalHandler(config, {
730
+ checkpointsBuilder: validatorCheckpointsBuilder,
731
+ worldState: worldStateSynchronizer,
732
+ epochCache,
733
+ blockSource: archiver,
734
+ l1ToL2MessageSource: archiver,
735
+ p2pClient,
736
+ blobClient,
737
+ dateProvider,
738
+ telemetry,
739
+ }).register(p2pClient, reexecute, archiver);
740
+ }
365
741
 
366
- const validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
367
- if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
368
- watchers.push(validatorsSentinel);
369
- }
742
+ // Start world state and wait for it to sync to the archiver.
743
+ await worldStateSynchronizer.start();
744
+
745
+ // Start p2p. Note that it depends on world state to be running.
746
+ await p2pClient.start();
747
+
748
+ let validatorsSentinel: Awaited<ReturnType<typeof createSentinel>> | undefined;
749
+ let epochPruneWatcher: EpochPruneWatcher | undefined;
750
+ let attestationsBlockWatcher: AttestationsBlockWatcher | undefined;
751
+
752
+ if (!proverOnly) {
753
+ validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
754
+ if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
755
+ watchers.push(validatorsSentinel);
756
+ }
757
+
758
+ if (config.slashPrunePenalty > 0n || config.slashDataWithholdingPenalty > 0n) {
759
+ epochPruneWatcher = new EpochPruneWatcher(
760
+ archiver,
761
+ archiver,
762
+ epochCache,
763
+ p2pClient.getTxProvider(),
764
+ validatorCheckpointsBuilder,
765
+ config,
766
+ );
767
+ watchers.push(epochPruneWatcher);
768
+ }
769
+
770
+ // We assume we want to slash for invalid attestations unless all max penalties are set to 0
771
+ if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
772
+ attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
773
+ watchers.push(attestationsBlockWatcher);
774
+ }
775
+ }
370
776
 
371
- 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
- }
777
+ // Start p2p-related services once the archiver has completed sync
778
+ void archiver
779
+ .waitForInitialSync()
780
+ .then(async () => {
781
+ if (validatorsSentinel) {
782
+ await validatorsSentinel.start();
783
+ started.push(validatorsSentinel);
784
+ }
785
+ if (epochPruneWatcher) {
786
+ await epochPruneWatcher.start();
787
+ started.push(epochPruneWatcher);
788
+ }
789
+ if (attestationsBlockWatcher) {
790
+ await attestationsBlockWatcher.start();
791
+ started.push(attestationsBlockWatcher);
792
+ }
793
+ log.info(`All p2p services started`);
794
+ })
795
+ .catch(err => log.error('Failed to start p2p services after archiver sync', err));
796
+
797
+ // Validator enabled, create/start relevant service
798
+ let sequencer: SequencerClient | undefined;
799
+ let slasherClient: SlasherClientInterface | undefined;
800
+ if (!config.disableValidator && validatorClient) {
801
+ // We create a slasher only if we have a sequencer, since all slashing actions go through the sequencer publisher
802
+ // as they are executed when the node is selected as proposer.
803
+ const validatorAddresses = keyStoreManager
804
+ ? NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager).getAddresses()
805
+ : [];
806
+
807
+ slasherClient = await createSlasher(
808
+ config,
809
+ config.l1Contracts,
810
+ getPublicClient(config),
811
+ watchers,
812
+ dateProvider,
813
+ epochCache,
814
+ validatorAddresses,
815
+ undefined, // logger
816
+ );
817
+ await slasherClient.start();
818
+ started.push(slasherClient);
819
+
820
+ const l1TxUtils = config.sequencerPublisherForwarderAddress
821
+ ? await createForwarderL1TxUtilsFromSigners(
822
+ publicClient,
823
+ keyStoreManager!.createAllValidatorPublisherSigners(),
824
+ config.sequencerPublisherForwarderAddress,
825
+ { ...config, scope: 'sequencer' },
826
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
827
+ )
828
+ : await createL1TxUtilsFromSigners(
829
+ publicClient,
830
+ keyStoreManager!.createAllValidatorPublisherSigners(),
831
+ { ...config, scope: 'sequencer' },
832
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
833
+ );
834
+
835
+ // Create a funder L1TxUtils from the keystore funding account (if configured)
836
+ const fundingSigner = keyStoreManager?.createFundingSigner();
837
+ let funderL1TxUtils: L1TxUtils | undefined;
838
+ if (fundingSigner) {
839
+ const [funder] = await createL1TxUtilsFromSigners(
840
+ publicClient,
841
+ [fundingSigner],
842
+ { ...config, scope: 'sequencer' },
843
+ { telemetry, logger: log.createChild('l1-tx-utils:funder'), dateProvider },
844
+ );
845
+ funderL1TxUtils = funder;
846
+ }
847
+
848
+ // Create and start the sequencer client
849
+ const checkpointsBuilder = new CheckpointsBuilder(
850
+ { ...config, l1GenesisTime, slotDuration: Number(slotDuration), rollupManaLimit },
851
+ worldStateSynchronizer,
852
+ archiver,
853
+ dateProvider,
854
+ telemetry,
855
+ debugLogStore,
856
+ );
383
857
 
384
- // We assume we want to slash for invalid attestations unless all max penalties are set to 0
385
- let attestationsBlockWatcher: AttestationsBlockWatcher | undefined;
386
- if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
387
- attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
388
- watchers.push(attestationsBlockWatcher);
389
- }
858
+ sequencer = await SequencerClient.new(config, {
859
+ ...deps,
860
+ epochCache,
861
+ l1TxUtils,
862
+ funderL1TxUtils,
863
+ validatorClient,
864
+ p2pClient,
865
+ worldStateSynchronizer,
866
+ slasherClient,
867
+ checkpointsBuilder,
868
+ l2BlockSource: archiver,
869
+ l1ToL2MessageSource: archiver,
870
+ telemetry,
871
+ dateProvider,
872
+ blobClient,
873
+ nodeKeyStore: keyStoreManager!,
874
+ globalVariableBuilder,
875
+ });
876
+ }
390
877
 
391
- // Start p2p-related services once the archiver has completed sync
392
- void archiver
393
- .waitForInitialSync()
394
- .then(async () => {
395
- await p2pClient.start();
396
- await validatorsSentinel?.start();
397
- await epochPruneWatcher?.start();
398
- await attestationsBlockWatcher?.start();
399
- log.info(`All p2p services started`);
400
- })
401
- .catch(err => log.error('Failed to start p2p services after archiver sync', err));
878
+ if (!options.dontStartSequencer && sequencer) {
879
+ await sequencer.start();
880
+ started.push(sequencer);
881
+ log.verbose(`Sequencer started`);
882
+ } else if (sequencer) {
883
+ log.warn(`Sequencer created but not started`);
884
+ }
402
885
 
403
- // Validator enabled, create/start relevant service
404
- let sequencer: SequencerClient | undefined;
405
- let slasherClient: SlasherClientInterface | undefined;
406
- if (!config.disableValidator) {
407
- // We create a slasher only if we have a sequencer, since all slashing actions go through the sequencer publisher
408
- // as they are executed when the node is selected as proposer.
409
- const validatorAddresses = keyStoreManager
410
- ? NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager).getAddresses()
411
- : [];
886
+ // Create prover node subsystem if enabled
887
+ let proverNode: ProverNode | undefined;
888
+ if (config.enableProverNode) {
889
+ proverNode = await createProverNode(config, {
890
+ ...deps.proverNodeDeps,
891
+ telemetry,
892
+ dateProvider,
893
+ archiver,
894
+ worldStateSynchronizer,
895
+ p2pClient,
896
+ epochCache,
897
+ blobClient,
898
+ keyStoreManager,
899
+ });
900
+
901
+ if (!options.dontStartProverNode) {
902
+ await proverNode.start();
903
+ started.push(proverNode);
904
+ log.info(`Prover node subsystem started`);
905
+ } else {
906
+ log.info(`Prover node subsystem created but not started`);
907
+ }
908
+ }
412
909
 
413
- slasherClient = await createSlasher(
910
+ const node = new AztecNodeService(
414
911
  config,
415
- config.l1Contracts,
416
- getPublicClient(config),
417
- watchers,
418
- dateProvider,
419
- epochCache,
420
- validatorAddresses,
421
- undefined, // logger
422
- );
423
- await slasherClient.start();
424
-
425
- const l1TxUtils = await createL1TxUtilsWithBlobsFromEthSigner(
426
- publicClient,
427
- keyStoreManager!.createAllValidatorPublisherSigners(),
428
- { ...config, scope: 'sequencer' },
429
- { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider },
430
- );
431
-
432
- // Create and start the sequencer client
433
- sequencer = await SequencerClient.new(config, {
434
- ...deps,
435
- epochCache,
436
- l1TxUtils,
437
- validatorClient,
438
912
  p2pClient,
913
+ archiver,
914
+ archiver,
915
+ archiver,
916
+ archiver,
439
917
  worldStateSynchronizer,
918
+ sequencer,
919
+ proverNode,
440
920
  slasherClient,
441
- blockBuilder,
442
- l2BlockSource: archiver,
443
- l1ToL2MessageSource: archiver,
921
+ validatorsSentinel,
922
+ epochPruneWatcher,
923
+ attestationsBlockWatcher,
924
+ ethereumChain.chainInfo.id,
925
+ config.rollupVersion,
926
+ globalVariableBuilder,
927
+ feeProvider,
928
+ epochCache,
929
+ packageVersion,
930
+ peerProofVerifier,
931
+ rpcProofVerifier,
444
932
  telemetry,
445
- dateProvider,
446
- blobSinkClient,
447
- nodeKeyStore: keyStoreManager!,
448
- });
449
- }
933
+ log,
934
+ blobClient,
935
+ validatorClient,
936
+ keyStoreManager,
937
+ debugLogStore,
938
+ );
450
939
 
451
- if (!options.dontStartSequencer && sequencer) {
452
- await sequencer.start();
453
- log.verbose(`Sequencer started`);
454
- } else if (sequencer) {
455
- log.warn(`Sequencer created but not started`);
456
- }
457
-
458
- return new AztecNodeService(
459
- config,
460
- p2pClient,
461
- archiver,
462
- archiver,
463
- archiver,
464
- archiver,
465
- worldStateSynchronizer,
466
- sequencer,
467
- slasherClient,
468
- validatorsSentinel,
469
- epochPruneWatcher,
470
- ethereumChain.chainInfo.id,
471
- config.rollupVersion,
472
- new GlobalVariableBuilder(config),
473
- epochCache,
474
- packageVersion,
475
- proofVerifier,
476
- telemetry,
477
- log,
478
- );
940
+ return node;
941
+ } catch (err) {
942
+ log.error('Failed during node creation, stopping started resources', err);
943
+ for (const resource of started.reverse()) {
944
+ await tryStop(resource);
945
+ }
946
+ throw err;
947
+ }
479
948
  }
480
949
 
481
950
  /**
@@ -486,6 +955,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
486
955
  return this.sequencer;
487
956
  }
488
957
 
958
+ /** Returns the prover node subsystem, if enabled. */
959
+ public getProverNode(): ProverNode | undefined {
960
+ return this.proverNode;
961
+ }
962
+
489
963
  public getBlockSource(): L2BlockSource {
490
964
  return this.blockSource;
491
965
  }
@@ -511,7 +985,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
511
985
  }
512
986
 
513
987
  public async getAllowedPublicSetup(): Promise<AllowedElement[]> {
514
- return this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
988
+ return [...(await getDefaultAllowedSetupFunctions()), ...(this.config.txPublicSetupAllowListExtend ?? [])];
515
989
  }
516
990
 
517
991
  /**
@@ -539,61 +1013,19 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
539
1013
  enr,
540
1014
  l1ContractAddresses: contractAddresses,
541
1015
  protocolContractAddresses: protocolContractAddresses,
1016
+ realProofs: !!this.config.realProofs,
542
1017
  };
543
1018
 
544
1019
  return nodeInfo;
545
1020
  }
546
1021
 
547
- /**
548
- * Get a block specified by its number.
549
- * @param number - The block number being requested.
550
- * @returns The requested block.
551
- */
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);
555
- }
556
-
557
- /**
558
- * Get a block specified by its hash.
559
- * @param blockHash - The block hash being requested.
560
- * @returns The requested block.
561
- */
562
- public async getBlockByHash(blockHash: Fr): Promise<L2Block | undefined> {
563
- const publishedBlock = await this.blockSource.getPublishedBlockByHash(blockHash);
564
- return publishedBlock?.block;
1022
+ public async getCurrentMinFees(): Promise<GasFees> {
1023
+ return await this.feeProvider.getCurrentMinFees();
565
1024
  }
566
1025
 
567
- /**
568
- * Get a block specified by its archive root.
569
- * @param archive - The archive root being requested.
570
- * @returns The requested block.
571
- */
572
- public async getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
573
- const publishedBlock = await this.blockSource.getPublishedBlockByArchive(archive);
574
- return publishedBlock?.block;
575
- }
576
-
577
- /**
578
- * Method to request blocks. Will attempt to return all requested blocks but will return only those available.
579
- * @param from - The start of the range of blocks to return.
580
- * @param limit - The maximum number of blocks to obtain.
581
- * @returns The blocks requested.
582
- */
583
- public async getBlocks(from: number, limit: number): Promise<L2Block[]> {
584
- return (await this.blockSource.getBlocks(from, limit)) ?? [];
585
- }
586
-
587
- public async getPublishedBlocks(from: number, limit: number): Promise<PublishedL2Block[]> {
588
- return (await this.blockSource.getPublishedBlocks(from, limit)) ?? [];
589
- }
590
-
591
- /**
592
- * Method to fetch the current base fees.
593
- * @returns The current base fees.
594
- */
595
- public async getCurrentBaseFees(): Promise<GasFees> {
596
- return await this.globalVariableBuilder.getCurrentBaseFees();
1026
+ /** Returns predicted min fees for the current slot and next N slots. */
1027
+ public async getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]> {
1028
+ return await this.feeProvider.getPredictedMinFees(manaUsage);
597
1029
  }
598
1030
 
599
1031
  public async getMaxPriorityFees(): Promise<GasFees> {
@@ -604,18 +1036,6 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
604
1036
  return GasFees.from({ feePerDaGas: 0n, feePerL2Gas: 0n });
605
1037
  }
606
1038
 
607
- /**
608
- * Method to fetch the latest block number synchronized by the node.
609
- * @returns The block number.
610
- */
611
- public async getBlockNumber(): Promise<number> {
612
- return await this.blockSource.getBlockNumber();
613
- }
614
-
615
- public async getProvenBlockNumber(): Promise<number> {
616
- return await this.blockSource.getProvenBlockNumber();
617
- }
618
-
619
1039
  /**
620
1040
  * Method to fetch the version of the package.
621
1041
  * @returns The node package version
@@ -648,25 +1068,51 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
648
1068
  return this.contractDataSource.getContract(address);
649
1069
  }
650
1070
 
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);
1071
+ public async getPrivateLogsByTags(
1072
+ tags: SiloedTag[],
1073
+ page?: number,
1074
+ referenceBlock?: BlockHash,
1075
+ ): Promise<TxScopedL2Log[][]> {
1076
+ let upToBlockNumber: BlockNumber | undefined;
1077
+ if (referenceBlock) {
1078
+ const initialBlockHash = await this.#getInitialHeaderHash();
1079
+ if (referenceBlock.equals(initialBlockHash)) {
1080
+ upToBlockNumber = BlockNumber(0);
1081
+ } else {
1082
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
1083
+ if (!header) {
1084
+ throw new Error(
1085
+ `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
1086
+ );
1087
+ }
1088
+ upToBlockNumber = header.globalVariables.blockNumber;
1089
+ }
1090
+ }
1091
+ return this.logsSource.getPrivateLogsByTags(tags, page, upToBlockNumber);
1092
+ }
1093
+
1094
+ public async getPublicLogsByTagsFromContract(
1095
+ contractAddress: AztecAddress,
1096
+ tags: Tag[],
1097
+ page?: number,
1098
+ referenceBlock?: BlockHash,
1099
+ ): Promise<TxScopedL2Log[][]> {
1100
+ let upToBlockNumber: BlockNumber | undefined;
1101
+ if (referenceBlock) {
1102
+ const initialBlockHash = await this.#getInitialHeaderHash();
1103
+ if (referenceBlock.equals(initialBlockHash)) {
1104
+ upToBlockNumber = BlockNumber(0);
1105
+ } else {
1106
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
1107
+ if (!header) {
1108
+ throw new Error(
1109
+ `Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`,
1110
+ );
1111
+ }
1112
+ upToBlockNumber = header.globalVariables.blockNumber;
1113
+ }
1114
+ }
1115
+ return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber);
670
1116
  }
671
1117
 
672
1118
  /**
@@ -707,27 +1153,48 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
707
1153
  throw new Error(`Invalid tx: ${reason}`);
708
1154
  }
709
1155
 
710
- await this.p2pClient!.sendTx(tx);
711
- this.metrics.receivedTx(timer.ms(), true);
712
- this.log.info(`Received tx ${txHash}`, { txHash });
1156
+ try {
1157
+ await this.p2pClient!.sendTx(tx);
1158
+ } catch (err) {
1159
+ this.metrics.receivedTx(timer.ms(), false);
1160
+ this.log.warn(`Mempool rejected tx ${txHash}: ${(err as Error).message}`, { txHash });
1161
+ throw err;
1162
+ }
1163
+ const duration = timer.ms();
1164
+ this.metrics.receivedTx(duration, true);
1165
+ this.log.info(`Received tx ${txHash} in ${duration}ms`, { txHash });
713
1166
  }
714
1167
 
715
1168
  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
- }
1169
+ // Check the tx pool status first. If the tx is known to the pool (pending or mined), we'll use that
1170
+ // as a fallback if we don't find a settled receipt in the archiver.
1171
+ const txPoolStatus = await this.p2pClient.getTxStatus(txHash);
1172
+ const isKnownToPool = txPoolStatus === 'pending' || txPoolStatus === 'mined';
724
1173
 
1174
+ // Then get the actual tx from the archiver, which tracks every tx in a mined block.
725
1175
  const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
1176
+
1177
+ let receipt: TxReceipt;
726
1178
  if (settledTxReceipt) {
727
- txReceipt = settledTxReceipt;
1179
+ receipt = settledTxReceipt;
1180
+ } else if (isKnownToPool) {
1181
+ // If the tx is in the pool but not in the archiver, it's pending.
1182
+ // This handles race conditions between archiver and p2p, where the archiver
1183
+ // has pruned the block in which a tx was mined, but p2p has not caught up yet.
1184
+ receipt = new TxReceipt(txHash, TxStatus.PENDING, /*executionResult=*/ undefined, /*error=*/ undefined);
1185
+ } else {
1186
+ // Otherwise, if we don't know the tx, we consider it dropped.
1187
+ receipt = new TxReceipt(
1188
+ txHash,
1189
+ TxStatus.DROPPED,
1190
+ /*executionResult=*/ undefined,
1191
+ /*error=*/ 'Tx dropped by P2P node',
1192
+ );
728
1193
  }
729
1194
 
730
- return txReceipt;
1195
+ this.debugLogStore.decorateReceiptWithLogs(txHash.toString(), receipt);
1196
+
1197
+ return receipt;
731
1198
  }
732
1199
 
733
1200
  public getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
@@ -739,18 +1206,29 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
739
1206
  */
740
1207
  public async stop() {
741
1208
  this.log.info(`Stopping Aztec Node`);
1209
+ await tryStop(this.attestationsBlockWatcher);
742
1210
  await tryStop(this.validatorsSentinel);
743
1211
  await tryStop(this.epochPruneWatcher);
744
1212
  await tryStop(this.slasherClient);
745
- await tryStop(this.proofVerifier);
1213
+ await Promise.all([tryStop(this.peerProofVerifier), tryStop(this.rpcProofVerifier)]);
746
1214
  await tryStop(this.sequencer);
1215
+ await tryStop(this.proverNode);
747
1216
  await tryStop(this.p2pClient);
748
1217
  await tryStop(this.worldStateSynchronizer);
749
1218
  await tryStop(this.blockSource);
1219
+ await tryStop(this.blobClient);
750
1220
  await tryStop(this.telemetry);
751
1221
  this.log.info(`Stopped Aztec Node`);
752
1222
  }
753
1223
 
1224
+ /**
1225
+ * Returns the blob client used by this node.
1226
+ * @internal - Exposed for testing purposes only.
1227
+ */
1228
+ public getBlobClient(): BlobClientInterface | undefined {
1229
+ return this.blobClient;
1230
+ }
1231
+
754
1232
  /**
755
1233
  * Method to retrieve pending txs.
756
1234
  * @param limit - The number of items to returns
@@ -783,121 +1261,99 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
783
1261
  return compactArray(await Promise.all(txHashes.map(txHash => this.getTxByHash(txHash))));
784
1262
  }
785
1263
 
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
1264
  public async findLeavesIndexes(
795
- blockNumber: L2BlockNumber,
1265
+ referenceBlock: BlockParameter,
796
1266
  treeId: MerkleTreeId,
797
1267
  leafValues: Fr[],
798
- ): Promise<(InBlock<bigint> | undefined)[]> {
799
- const committedDb = await this.#getWorldState(blockNumber);
1268
+ ): Promise<(DataInBlock<bigint> | undefined)[]> {
1269
+ const committedDb = await this.getWorldState(referenceBlock);
800
1270
  const maybeIndices = await committedDb.findLeafIndices(
801
1271
  treeId,
802
1272
  leafValues.map(x => x.toBuffer()),
803
1273
  );
804
- // We filter out undefined values
805
- const indices = maybeIndices.filter(x => x !== undefined) as bigint[];
1274
+ // Filter out undefined values to query block numbers only for found leaves
1275
+ const definedIndices = maybeIndices.filter(x => x !== undefined);
806
1276
 
807
- // Now we find the block numbers for the indices
808
- const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, indices);
1277
+ // Now we find the block numbers for the defined indices
1278
+ const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, definedIndices);
809
1279
 
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]}`);
1280
+ // Build a map from leaf index to block number
1281
+ const indexToBlockNumber = new Map<bigint, BlockNumber>();
1282
+ for (let i = 0; i < definedIndices.length; i++) {
1283
+ const blockNumber = blockNumbers[i];
1284
+ if (blockNumber === undefined) {
1285
+ throw new Error(
1286
+ `Block number is undefined for leaf index ${definedIndices[i]} in tree ${MerkleTreeId[treeId]}`,
1287
+ );
814
1288
  }
1289
+ indexToBlockNumber.set(definedIndices[i], blockNumber);
815
1290
  }
816
1291
 
817
1292
  // Get unique block numbers in order to optimize num calls to getLeafValue function.
818
- const uniqueBlockNumbers = [...new Set(blockNumbers.filter(x => x !== undefined))];
1293
+ const uniqueBlockNumbers = [...new Set(indexToBlockNumber.values())];
819
1294
 
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).
1295
+ // Now we obtain the block hashes from the archive tree (block number = leaf index in archive tree).
822
1296
  const blockHashes = await Promise.all(
823
1297
  uniqueBlockNumbers.map(blockNumber => {
824
- return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, blockNumber!);
1298
+ return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
825
1299
  }),
826
1300
  );
827
1301
 
828
- // If any of the block hashes are undefined, we throw an error.
1302
+ // Build a map from block number to block hash
1303
+ const blockNumberToHash = new Map<BlockNumber, BlockHash>();
829
1304
  for (let i = 0; i < uniqueBlockNumbers.length; i++) {
830
- if (blockHashes[i] === undefined) {
1305
+ const blockHash = blockHashes[i];
1306
+ if (blockHash === undefined) {
831
1307
  throw new Error(`Block hash is undefined for block number ${uniqueBlockNumbers[i]}`);
832
1308
  }
1309
+ blockNumberToHash.set(uniqueBlockNumbers[i], blockHash);
833
1310
  }
834
1311
 
835
- // Create InBlock objects by combining indices, blockNumbers and blockHashes and return them.
836
- return maybeIndices.map((index, i) => {
1312
+ // Create DataInBlock objects by combining indices, blockNumbers and blockHashes and return them.
1313
+ return maybeIndices.map(index => {
837
1314
  if (index === undefined) {
838
1315
  return undefined;
839
1316
  }
840
- const blockNumber = blockNumbers[i];
1317
+ const blockNumber = indexToBlockNumber.get(index);
841
1318
  if (blockNumber === undefined) {
842
- return undefined;
1319
+ throw new Error(`Block number not found for leaf index ${index} in tree ${MerkleTreeId[treeId]}`);
843
1320
  }
844
- const blockHashIndex = uniqueBlockNumbers.indexOf(blockNumber);
845
- const blockHash = blockHashes[blockHashIndex];
846
- if (!blockHash) {
847
- return undefined;
1321
+ const l2BlockHash = blockNumberToHash.get(blockNumber);
1322
+ if (l2BlockHash === undefined) {
1323
+ throw new Error(`Block hash not found for block number ${blockNumber}`);
848
1324
  }
849
1325
  return {
850
- l2BlockNumber: Number(blockNumber),
851
- l2BlockHash: L2BlockHash.fromField(blockHash),
1326
+ l2BlockNumber: blockNumber,
1327
+ l2BlockHash,
852
1328
  data: index,
853
1329
  };
854
1330
  });
855
1331
  }
856
1332
 
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,
1333
+ public async getBlockHashMembershipWitness(
1334
+ referenceBlock: BlockParameter,
1335
+ blockHash: BlockHash,
888
1336
  ): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
889
- const committedDb = await this.#getWorldState(blockNumber);
890
- const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [archive]);
1337
+ // The Noir circuit checks the archive membership proof against `anchor_block_header.last_archive.root`,
1338
+ // which is the archive tree root BEFORE the anchor block was added (i.e. the state after block N-1).
1339
+ // So we need the world state at block N-1, not block N, to produce a sibling path matching that root.
1340
+ const referenceBlockNumber = await this.resolveBlockNumber(referenceBlock);
1341
+ if (referenceBlockNumber === BlockNumber.ZERO) {
1342
+ // Block 0 (the initial block) has an empty archive, so no membership witness can exist.
1343
+ return undefined;
1344
+ }
1345
+ const committedDb = await this.getWorldState(BlockNumber(referenceBlockNumber - 1));
1346
+ const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [blockHash]);
891
1347
  return pathAndIndex === undefined
892
1348
  ? undefined
893
1349
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
894
1350
  }
895
1351
 
896
1352
  public async getNoteHashMembershipWitness(
897
- blockNumber: L2BlockNumber,
1353
+ referenceBlock: BlockParameter,
898
1354
  noteHash: Fr,
899
1355
  ): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
900
- const committedDb = await this.#getWorldState(blockNumber);
1356
+ const committedDb = await this.getWorldState(referenceBlock);
901
1357
  const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.NOTE_HASH_TREE>(
902
1358
  MerkleTreeId.NOTE_HASH_TREE,
903
1359
  [noteHash],
@@ -907,17 +1363,11 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
907
1363
  : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
908
1364
  }
909
1365
 
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
1366
  public async getL1ToL2MessageMembershipWitness(
917
- blockNumber: L2BlockNumber,
1367
+ referenceBlock: BlockParameter,
918
1368
  l1ToL2Message: Fr,
919
1369
  ): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined> {
920
- const db = await this.#getWorldState(blockNumber);
1370
+ const db = await this.getWorldState(referenceBlock);
921
1371
  const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [l1ToL2Message]);
922
1372
  if (!witness) {
923
1373
  return undefined;
@@ -927,9 +1377,9 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
927
1377
  return [witness.index, witness.path];
928
1378
  }
929
1379
 
930
- public async getL1ToL2MessageBlock(l1ToL2Message: Fr): Promise<number | undefined> {
1380
+ public async getL1ToL2MessageCheckpoint(l1ToL2Message: Fr): Promise<CheckpointNumber | undefined> {
931
1381
  const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
932
- return messageIndex ? InboxLeaf.l2BlockFromIndex(messageIndex) : undefined;
1382
+ return messageIndex ? InboxLeaf.checkpointNumberFromIndex(messageIndex) : undefined;
933
1383
  }
934
1384
 
935
1385
  /**
@@ -943,54 +1393,26 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
943
1393
  }
944
1394
 
945
1395
  /**
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).
1396
+ * Returns all the L2 to L1 messages in an epoch.
1397
+ * @param epoch - The epoch at which to get the data.
1398
+ * @returns The L2 to L1 messages (empty array if the epoch is not found).
949
1399
  */
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.
974
- */
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);
1400
+ public async getL2ToL1Messages(epoch: EpochNumber): Promise<Fr[][][][]> {
1401
+ // Assumes `getCheckpointedBlocksForEpoch` returns blocks in ascending order of block number.
1402
+ const checkpointedBlocks = await this.blockSource.getCheckpointedBlocksForEpoch(epoch);
1403
+ const blocksInCheckpoints = chunkBy(checkpointedBlocks, cb => cb.block.header.globalVariables.slotNumber).map(
1404
+ group => group.map(cb => cb.block),
1405
+ );
1406
+ return blocksInCheckpoints.map(blocks =>
1407
+ blocks.map(block => block.body.txEffects.map(txEffect => txEffect.l2ToL1Msgs)),
1408
+ );
981
1409
  }
982
1410
 
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
1411
  public async getNullifierMembershipWitness(
990
- blockNumber: L2BlockNumber,
1412
+ referenceBlock: BlockParameter,
991
1413
  nullifier: Fr,
992
1414
  ): Promise<NullifierMembershipWitness | undefined> {
993
- const db = await this.#getWorldState(blockNumber);
1415
+ const db = await this.getWorldState(referenceBlock);
994
1416
  const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]);
995
1417
  if (!witness) {
996
1418
  return undefined;
@@ -1005,32 +1427,20 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1005
1427
  return new NullifierMembershipWitness(index, leafPreimage as NullifierLeafPreimage, path);
1006
1428
  }
1007
1429
 
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
1430
  public async getLowNullifierMembershipWitness(
1023
- blockNumber: L2BlockNumber,
1431
+ referenceBlock: BlockParameter,
1024
1432
  nullifier: Fr,
1025
1433
  ): Promise<NullifierMembershipWitness | undefined> {
1026
- const committedDb = await this.#getWorldState(blockNumber);
1434
+ const committedDb = await this.getWorldState(referenceBlock);
1027
1435
  const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
1028
1436
  if (!findResult) {
1029
1437
  return undefined;
1030
1438
  }
1031
1439
  const { index, alreadyPresent } = findResult;
1032
1440
  if (alreadyPresent) {
1033
- this.log.warn(`Nullifier ${nullifier.toBigInt()} already exists in the tree`);
1441
+ throw new Error(
1442
+ `Cannot prove nullifier non-inclusion: nullifier ${nullifier.toBigInt()} already exists in the tree`,
1443
+ );
1034
1444
  }
1035
1445
  const preimageData = (await committedDb.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index))!;
1036
1446
 
@@ -1038,8 +1448,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1038
1448
  return new NullifierMembershipWitness(BigInt(index), preimageData as NullifierLeafPreimage, siblingPath);
1039
1449
  }
1040
1450
 
1041
- async getPublicDataWitness(blockNumber: L2BlockNumber, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1042
- const committedDb = await this.#getWorldState(blockNumber);
1451
+ async getPublicDataWitness(referenceBlock: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1452
+ const committedDb = await this.getWorldState(referenceBlock);
1043
1453
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
1044
1454
  if (!lowLeafResult) {
1045
1455
  return undefined;
@@ -1053,19 +1463,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1053
1463
  }
1054
1464
  }
1055
1465
 
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);
1466
+ public async getPublicStorageAt(referenceBlock: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
1467
+ const committedDb = await this.getWorldState(referenceBlock);
1069
1468
  const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
1070
1469
 
1071
1470
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
@@ -1079,34 +1478,6 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1079
1478
  return preimage.leaf.value;
1080
1479
  }
1081
1480
 
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);
1099
- }
1100
-
1101
- /**
1102
- * Get a block header specified by its archive root.
1103
- * @param archive - The archive root being requested.
1104
- * @returns The requested block header.
1105
- */
1106
- public async getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
1107
- return await this.blockSource.getBlockHeaderByArchive(archive);
1108
- }
1109
-
1110
1481
  /**
1111
1482
  * Simulates the public part of a transaction with the current state.
1112
1483
  * @param tx - The transaction to simulate.
@@ -1130,7 +1501,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1130
1501
  }
1131
1502
 
1132
1503
  const txHash = tx.getTxHash();
1133
- const blockNumber = (await this.blockSource.getBlockNumber()) + 1;
1504
+ const latestBlockNumber = await this.blockSource.getBlockNumber();
1505
+ const blockNumber = BlockNumber.add(latestBlockNumber, 1);
1134
1506
 
1135
1507
  // If sequencer is not initialized, we just set these values to zero for simulation.
1136
1508
  const coinbase = EthAddress.ZERO;
@@ -1145,6 +1517,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1145
1517
  this.contractDataSource,
1146
1518
  new DateProvider(),
1147
1519
  this.telemetry,
1520
+ this.log.getBindings(),
1148
1521
  );
1149
1522
 
1150
1523
  this.log.verbose(`Simulating public calls for tx ${txHash}`, {
@@ -1153,6 +1526,8 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1153
1526
  blockNumber,
1154
1527
  });
1155
1528
 
1529
+ // Ensure world-state has caught up with the latest block we loaded from the archiver
1530
+ await this.worldStateSynchronizer.syncImmediate(latestBlockNumber);
1156
1531
  const merkleTreeFork = await this.worldStateSynchronizer.fork();
1157
1532
  try {
1158
1533
  const config = PublicSimulatorConfig.from({
@@ -1160,13 +1535,15 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1160
1535
  collectDebugLogs: true,
1161
1536
  collectHints: false,
1162
1537
  collectCallMetadata: true,
1163
- maxDebugLogMemoryReads: this.config.rpcSimulatePublicMaxDebugLogMemoryReads,
1164
1538
  collectStatistics: false,
1539
+ collectionLimits: CollectionLimitsConfig.from({
1540
+ maxDebugLogMemoryReads: this.config.rpcSimulatePublicMaxDebugLogMemoryReads,
1541
+ }),
1165
1542
  });
1166
1543
  const processor = publicProcessorFactory.create(merkleTreeFork, newGlobalVariables, config);
1167
1544
 
1168
1545
  // REFACTOR: Consider merging ProcessReturnValues into ProcessedTx
1169
- const [processedTxs, failedTxs, _usedTxs, returns] = await processor.process([tx]);
1546
+ const [processedTxs, failedTxs, _usedTxs, returns, debugLogs] = await processor.process([tx]);
1170
1547
  // REFACTOR: Consider returning the error rather than throwing
1171
1548
  if (failedTxs.length) {
1172
1549
  this.log.warn(`Simulated tx ${txHash} fails: ${failedTxs[0].error}`, { txHash });
@@ -1180,6 +1557,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1180
1557
  processedTx.txEffect,
1181
1558
  returns,
1182
1559
  processedTx.gasUsed,
1560
+ debugLogs,
1183
1561
  );
1184
1562
  } finally {
1185
1563
  await merkleTreeFork.close();
@@ -1191,21 +1569,34 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1191
1569
  { isSimulation, skipFeeEnforcement }: { isSimulation?: boolean; skipFeeEnforcement?: boolean } = {},
1192
1570
  ): Promise<TxValidationResult> {
1193
1571
  const db = this.worldStateSynchronizer.getCommitted();
1194
- const verifier = isSimulation ? undefined : this.proofVerifier;
1572
+ const verifier = isSimulation ? undefined : this.rpcProofVerifier;
1195
1573
 
1196
- // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1574
+ // We accept transactions if they are not expired by the next slot (checked based on the ExpirationTimestamp field)
1197
1575
  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
- });
1576
+ const blockNumber = BlockNumber((await this.blockSource.getBlockNumber()) + 1);
1577
+ const l1Constants = await this.blockSource.getL1Constants();
1578
+ const validator = createTxValidatorForAcceptingTxsOverRPC(
1579
+ db,
1580
+ this.contractDataSource,
1581
+ verifier,
1582
+ {
1583
+ timestamp: nextSlotTimestamp,
1584
+ blockNumber,
1585
+ l1ChainId: this.l1ChainId,
1586
+ rollupVersion: this.version,
1587
+ setupAllowList: [
1588
+ ...(await getDefaultAllowedSetupFunctions()),
1589
+ ...(this.config.txPublicSetupAllowListExtend ?? []),
1590
+ ],
1591
+ gasFees: await this.getCurrentMinFees(),
1592
+ skipFeeEnforcement,
1593
+ txsPermitted: !this.config.disableTransactions,
1594
+ rollupManaLimit: l1Constants.rollupManaLimit,
1595
+ maxBlockL2Gas: this.config.validateMaxL2BlockGas,
1596
+ maxBlockDAGas: this.config.validateMaxDABlockGas,
1597
+ },
1598
+ this.log.getBindings(),
1599
+ );
1209
1600
 
1210
1601
  return await validator.validateTx(tx);
1211
1602
  }
@@ -1227,7 +1618,15 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1227
1618
  archiver.updateConfig(config);
1228
1619
  }
1229
1620
  if (newConfig.realProofs !== this.config.realProofs) {
1230
- this.proofVerifier = config.realProofs ? await BBCircuitVerifier.new(newConfig) : new TestCircuitVerifier();
1621
+ await Promise.all([tryStop(this.peerProofVerifier), tryStop(this.rpcProofVerifier)]);
1622
+ if (newConfig.realProofs) {
1623
+ this.peerProofVerifier = await BatchChonkVerifier.new(newConfig, newConfig.bbChonkVerifyMaxBatch, 'peer');
1624
+ const rpcVerifier = await BBCircuitVerifier.new(newConfig);
1625
+ this.rpcProofVerifier = new QueuedIVCVerifier(rpcVerifier, newConfig.numConcurrentIVCVerifiers);
1626
+ } else {
1627
+ this.peerProofVerifier = new TestCircuitVerifier();
1628
+ this.rpcProofVerifier = new TestCircuitVerifier();
1629
+ }
1231
1630
  }
1232
1631
 
1233
1632
  this.config = newConfig;
@@ -1274,7 +1673,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1274
1673
  }
1275
1674
 
1276
1675
  // And it has an L2 block hash
1277
- const l2BlockHash = await archiver.getL2Tips().then(tips => tips.latest.hash);
1676
+ const l2BlockHash = await archiver.getL2Tips().then(tips => tips.proposed.hash);
1278
1677
  if (!l2BlockHash) {
1279
1678
  this.metrics.recordSnapshotError();
1280
1679
  throw new Error(`Archiver has no latest L2 block hash downloaded. Cannot start snapshot.`);
@@ -1302,13 +1701,13 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1302
1701
  return Promise.resolve();
1303
1702
  }
1304
1703
 
1305
- public async rollbackTo(targetBlock: number, force?: boolean): Promise<void> {
1704
+ public async rollbackTo(targetBlock: BlockNumber, force?: boolean, resumeSync = true): Promise<void> {
1306
1705
  const archiver = this.blockSource as Archiver;
1307
1706
  if (!('rollbackTo' in archiver)) {
1308
1707
  throw new Error('Archiver implementation does not support rollbacks.');
1309
1708
  }
1310
1709
 
1311
- const finalizedBlock = await archiver.getL2Tips().then(tips => tips.finalized.number);
1710
+ const finalizedBlock = await archiver.getL2Tips().then(tips => tips.finalized.block.number);
1312
1711
  if (targetBlock < finalizedBlock) {
1313
1712
  if (force) {
1314
1713
  this.log.warn(`Clearing world state database to allow rolling back behind finalized block ${finalizedBlock}`);
@@ -1332,9 +1731,13 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1332
1731
  this.log.error(`Error during rollback`, err);
1333
1732
  throw err;
1334
1733
  } finally {
1335
- this.log.info(`Resuming world state and archiver sync.`);
1336
- this.worldStateSynchronizer.resumeSync();
1337
- archiver.resume();
1734
+ if (resumeSync) {
1735
+ this.log.info(`Resuming world state and archiver sync.`);
1736
+ this.worldStateSynchronizer.resumeSync();
1737
+ archiver.resume();
1738
+ } else {
1739
+ this.log.info(`Sync left paused after rollback (resumeSync=false).`);
1740
+ }
1338
1741
  }
1339
1742
  }
1340
1743
 
@@ -1351,35 +1754,153 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1351
1754
  return Promise.resolve();
1352
1755
  }
1353
1756
 
1354
- public getSlashPayloads(): Promise<SlashPayloadRound[]> {
1355
- if (!this.slasherClient) {
1356
- throw new Error(`Slasher client not enabled`);
1357
- }
1358
- return this.slasherClient.getSlashPayloads();
1359
- }
1360
-
1361
1757
  public getSlashOffenses(round: bigint | 'all' | 'current'): Promise<Offense[]> {
1362
1758
  if (!this.slasherClient) {
1363
1759
  throw new Error(`Slasher client not enabled`);
1364
1760
  }
1365
1761
  if (round === 'all') {
1366
- return this.slasherClient.getPendingOffenses();
1762
+ return this.slasherClient.getOffenses();
1367
1763
  } else {
1368
1764
  return this.slasherClient.gatherOffensesForRound(round === 'current' ? undefined : BigInt(round));
1369
1765
  }
1370
1766
  }
1371
1767
 
1768
+ public async reloadKeystore(): Promise<void> {
1769
+ if (!this.config.keyStoreDirectory?.length) {
1770
+ throw new BadRequestError(
1771
+ 'Cannot reload keystore: node is not using a file-based keystore. ' +
1772
+ 'Set KEY_STORE_DIRECTORY to use file-based keystores.',
1773
+ );
1774
+ }
1775
+ if (!this.validatorClient) {
1776
+ throw new BadRequestError('Cannot reload keystore: validator is not enabled.');
1777
+ }
1778
+
1779
+ this.log.info('Reloading keystore from disk');
1780
+
1781
+ // Re-read and validate keystore files
1782
+ const keyStores = loadKeystores(this.config.keyStoreDirectory);
1783
+ const newManager = new KeystoreManager(mergeKeystores(keyStores));
1784
+ await newManager.validateSigners();
1785
+ ValidatorClient.validateKeyStoreConfiguration(newManager, this.log);
1786
+
1787
+ // Validate that every validator's publisher keys overlap with the L1 signers
1788
+ // that were initialized at startup. Publishers cannot be hot-reloaded, so a
1789
+ // validator with a publisher key that doesn't match any existing L1 signer
1790
+ // would silently fail on every proposer slot.
1791
+ if (this.keyStoreManager && this.sequencer) {
1792
+ const oldAdapter = NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager);
1793
+ const availablePublishers = new Set(
1794
+ oldAdapter
1795
+ .getAttesterAddresses()
1796
+ .flatMap(a => oldAdapter.getPublisherAddresses(a).map(p => p.toString().toLowerCase())),
1797
+ );
1798
+
1799
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1800
+ for (const attester of newAdapter.getAttesterAddresses()) {
1801
+ const pubs = newAdapter.getPublisherAddresses(attester);
1802
+ if (pubs.length > 0 && !pubs.some(p => availablePublishers.has(p.toString().toLowerCase()))) {
1803
+ throw new BadRequestError(
1804
+ `Cannot reload keystore: validator ${attester} has publisher keys ` +
1805
+ `[${pubs.map(p => p.toString()).join(', ')}] but none match the L1 signers initialized at startup ` +
1806
+ `[${[...availablePublishers].join(', ')}]. Publishers cannot be hot-reloaded — ` +
1807
+ `use an existing publisher key or restart the node.`,
1808
+ );
1809
+ }
1810
+ }
1811
+ }
1812
+
1813
+ // Build adapters for old and new keystores to compute diff
1814
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1815
+ const newAddresses = newAdapter.getAttesterAddresses();
1816
+ const oldAddresses = this.keyStoreManager
1817
+ ? NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager).getAttesterAddresses()
1818
+ : [];
1819
+
1820
+ const oldSet = new Set(oldAddresses.map(a => a.toString()));
1821
+ const newSet = new Set(newAddresses.map(a => a.toString()));
1822
+ const added = newAddresses.filter(a => !oldSet.has(a.toString()));
1823
+ const removed = oldAddresses.filter(a => !newSet.has(a.toString()));
1824
+
1825
+ if (added.length > 0) {
1826
+ this.log.info(`Keystore reload: adding attester keys: ${added.map(a => a.toString()).join(', ')}`);
1827
+ }
1828
+ if (removed.length > 0) {
1829
+ this.log.info(`Keystore reload: removing attester keys: ${removed.map(a => a.toString()).join(', ')}`);
1830
+ }
1831
+ if (added.length === 0 && removed.length === 0) {
1832
+ this.log.info('Keystore reload: attester keys unchanged');
1833
+ }
1834
+
1835
+ // Update the validator client (coinbase, feeRecipient, attester keys)
1836
+ this.validatorClient.reloadKeystore(newManager);
1837
+
1838
+ // Update the publisher factory's keystore so newly-added validators
1839
+ // can be matched to existing publisher keys when proposing blocks.
1840
+ if (this.sequencer) {
1841
+ this.sequencer.updatePublisherNodeKeyStore(newAdapter);
1842
+ }
1843
+
1844
+ // Update slasher's "don't-slash-self" list with new validator addresses
1845
+ if (this.slasherClient && !this.config.slashSelfAllowed) {
1846
+ const slashValidatorsNever = unique(
1847
+ [...(this.config.slashValidatorsNever ?? []), ...newAddresses].map(a => a.toString()),
1848
+ ).map(EthAddress.fromString);
1849
+ this.slasherClient.updateConfig({ slashValidatorsNever });
1850
+ }
1851
+
1852
+ this.keyStoreManager = newManager;
1853
+ this.log.info('Keystore reloaded: coinbase, feeRecipient, and attester keys updated');
1854
+ }
1855
+
1856
+ public async mineBlock(): Promise<void> {
1857
+ if (!this.sequencer) {
1858
+ throw new BadRequestError('Cannot mine block: no sequencer is running');
1859
+ }
1860
+
1861
+ const currentBlockNumber = await this.getBlockNumber();
1862
+
1863
+ // Use slot duration + 50% buffer as the timeout so this works on running networks too
1864
+ const { slotDuration } = await this.blockSource.getL1Constants();
1865
+ const timeoutSeconds = Math.ceil(slotDuration * 1.5);
1866
+
1867
+ // Temporarily set minTxsPerBlock to 0 so the sequencer produces a block even with no txs
1868
+ const originalMinTxsPerBlock = this.sequencer.getSequencer().getConfig().minTxsPerBlock;
1869
+ this.sequencer.updateConfig({ minTxsPerBlock: 0 });
1870
+
1871
+ try {
1872
+ // Trigger the sequencer to produce a block immediately
1873
+ void this.sequencer.trigger();
1874
+
1875
+ // Wait for the new L2 block to appear
1876
+ await retryUntil(
1877
+ async () => {
1878
+ const newBlockNumber = await this.getBlockNumber();
1879
+ return newBlockNumber > currentBlockNumber ? true : undefined;
1880
+ },
1881
+ 'mineBlock',
1882
+ timeoutSeconds,
1883
+ 0.1,
1884
+ );
1885
+ } finally {
1886
+ this.sequencer.updateConfig({ minTxsPerBlock: originalMinTxsPerBlock });
1887
+ }
1888
+ }
1889
+
1890
+ #getInitialHeaderHash(): Promise<BlockHash> {
1891
+ if (!this.initialHeaderHashPromise) {
1892
+ this.initialHeaderHashPromise = this.worldStateSynchronizer.getCommitted().getInitialHeader().hash();
1893
+ }
1894
+ return this.initialHeaderHashPromise;
1895
+ }
1896
+
1372
1897
  /**
1373
1898
  * 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.
1899
+ * @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
1375
1900
  * @returns An instance of a committed MerkleTreeOperations
1376
1901
  */
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;
1902
+ protected async getWorldState(block: BlockParameter) {
1903
+ let blockSyncedTo: BlockNumber = BlockNumber.ZERO;
1383
1904
  try {
1384
1905
  // Attempt to sync the world state if necessary
1385
1906
  blockSyncedTo = await this.#syncWorldState();
@@ -1387,24 +1908,90 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, Traceable {
1387
1908
  this.log.error(`Error getting world state: ${err}`);
1388
1909
  }
1389
1910
 
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}`);
1911
+ if (block === 'latest') {
1912
+ this.log.debug(`Using committed db for block 'latest', world state synced upto ${blockSyncedTo}`);
1393
1913
  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);
1914
+ }
1915
+
1916
+ // Get the block number, either directly from the parameter or by quering the archiver with the block hash
1917
+ let blockNumber: BlockNumber;
1918
+ if (BlockHash.isBlockHash(block)) {
1919
+ const initialBlockHash = await this.#getInitialHeaderHash();
1920
+ if (block.equals(initialBlockHash)) {
1921
+ // Block 0 is a first-class historical block: its state lives in the trees' persisted
1922
+ // block-0 payload. Resolving the genesis hash to block number 0 lets the snapshot path
1923
+ // pin reads to genesis state even after the node has advanced past it.
1924
+ blockNumber = BlockNumber.ZERO;
1925
+ } else {
1926
+ const header = await this.blockSource.getBlockHeaderByHash(block);
1927
+ if (!header) {
1928
+ throw new Error(
1929
+ `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.`,
1930
+ );
1931
+ }
1932
+ blockNumber = header.getBlockNumber();
1933
+ }
1397
1934
  } else {
1398
- throw new Error(`Block ${blockNumber} not yet synced`);
1935
+ blockNumber = block as BlockNumber;
1936
+ }
1937
+
1938
+ // Check it's within world state sync range
1939
+ if (blockNumber > blockSyncedTo) {
1940
+ throw new Error(
1941
+ `Queried block ${inspectBlockParameter(block)} not yet synced by the node (node is synced upto ${blockSyncedTo}).`,
1942
+ );
1943
+ }
1944
+ this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1945
+
1946
+ const snapshot = this.worldStateSynchronizer.getSnapshot(blockNumber);
1947
+
1948
+ // Double-check world-state synced to the same block hash as was requested
1949
+ if (BlockHash.isBlockHash(block)) {
1950
+ const blockHash = await snapshot.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
1951
+ if (!blockHash || !block.equals(blockHash)) {
1952
+ const initialBlockHash = await this.#getInitialHeaderHash();
1953
+ throw new Error(
1954
+ `Block hash ${block.toString()} not found in world state at block number ${blockNumber} (world state has ${blockHash?.toString() ?? 'no hash'} at that index, genesis header hash is ${initialBlockHash.toString()}). If the node API has been queried with anchor block hash possibly a reorg has occurred.`,
1955
+ );
1956
+ }
1957
+ }
1958
+
1959
+ return snapshot;
1960
+ }
1961
+
1962
+ /** Resolves any {@link BlockParameter} variant to a concrete block number. */
1963
+ protected async resolveBlockNumber(block: BlockParameter): Promise<BlockNumber> {
1964
+ const resolved = await this.resolveBlockParameter(block);
1965
+ if (resolved.number !== undefined) {
1966
+ return resolved.number;
1967
+ }
1968
+ if (resolved.hash !== undefined) {
1969
+ const initialBlockHash = await this.#getInitialHeaderHash();
1970
+ if (resolved.hash.equals(initialBlockHash)) {
1971
+ return BlockNumber.ZERO;
1972
+ }
1973
+ const header = await this.blockSource.getBlockHeaderByHash(resolved.hash);
1974
+ if (!header) {
1975
+ throw new Error(`Block hash ${resolved.hash.toString()} not found.`);
1976
+ }
1977
+ return header.getBlockNumber();
1978
+ }
1979
+ if (resolved.archive !== undefined) {
1980
+ const header = await this.blockSource.getBlockHeaderByArchive(resolved.archive);
1981
+ if (!header) {
1982
+ throw new Error(`Block with archive ${resolved.archive.toString()} not found.`);
1983
+ }
1984
+ return header.getBlockNumber();
1399
1985
  }
1986
+ throw new BadRequestError(`Invalid BlockParameter: ${JSON.stringify(block)}`);
1400
1987
  }
1401
1988
 
1402
1989
  /**
1403
1990
  * Ensure we fully sync the world state
1404
1991
  * @returns A promise that fulfils once the world state is synced
1405
1992
  */
1406
- async #syncWorldState(): Promise<number> {
1993
+ async #syncWorldState(): Promise<BlockNumber> {
1407
1994
  const blockSourceHeight = await this.blockSource.getBlockNumber();
1408
- return this.worldStateSynchronizer.syncImmediate(blockSourceHeight);
1995
+ return await this.worldStateSynchronizer.syncImmediate(blockSourceHeight);
1409
1996
  }
1410
1997
  }