@aztec/aztec-node 0.0.0-test.1 → 0.0.1-commit.017a351

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.
Files changed (57) hide show
  1. package/dest/aztec-node/block_response_helpers.d.ts +25 -0
  2. package/dest/aztec-node/block_response_helpers.d.ts.map +1 -0
  3. package/dest/aztec-node/block_response_helpers.js +112 -0
  4. package/dest/aztec-node/config.d.ts +25 -11
  5. package/dest/aztec-node/config.d.ts.map +1 -1
  6. package/dest/aztec-node/config.js +90 -15
  7. package/dest/aztec-node/node_metrics.d.ts +5 -1
  8. package/dest/aztec-node/node_metrics.d.ts.map +1 -1
  9. package/dest/aztec-node/node_metrics.js +20 -6
  10. package/dest/aztec-node/public_data_overrides.d.ts +13 -0
  11. package/dest/aztec-node/public_data_overrides.d.ts.map +1 -0
  12. package/dest/aztec-node/public_data_overrides.js +21 -0
  13. package/dest/aztec-node/server.d.ts +139 -189
  14. package/dest/aztec-node/server.d.ts.map +1 -1
  15. package/dest/aztec-node/server.js +1842 -438
  16. package/dest/bin/index.d.ts +1 -1
  17. package/dest/bin/index.js +4 -2
  18. package/dest/index.d.ts +1 -2
  19. package/dest/index.d.ts.map +1 -1
  20. package/dest/index.js +0 -1
  21. package/dest/sentinel/config.d.ts +9 -0
  22. package/dest/sentinel/config.d.ts.map +1 -0
  23. package/dest/sentinel/config.js +39 -0
  24. package/dest/sentinel/factory.d.ts +11 -0
  25. package/dest/sentinel/factory.d.ts.map +1 -0
  26. package/dest/sentinel/factory.js +17 -0
  27. package/dest/sentinel/index.d.ts +3 -0
  28. package/dest/sentinel/index.d.ts.map +1 -0
  29. package/dest/sentinel/index.js +1 -0
  30. package/dest/sentinel/sentinel.d.ts +217 -0
  31. package/dest/sentinel/sentinel.d.ts.map +1 -0
  32. package/dest/sentinel/sentinel.js +571 -0
  33. package/dest/sentinel/store.d.ts +35 -0
  34. package/dest/sentinel/store.d.ts.map +1 -0
  35. package/dest/sentinel/store.js +182 -0
  36. package/dest/test/index.d.ts +31 -0
  37. package/dest/test/index.d.ts.map +1 -0
  38. package/dest/test/index.js +1 -0
  39. package/package.json +48 -35
  40. package/src/aztec-node/block_response_helpers.ts +161 -0
  41. package/src/aztec-node/config.ts +153 -26
  42. package/src/aztec-node/node_metrics.ts +23 -6
  43. package/src/aztec-node/public_data_overrides.ts +35 -0
  44. package/src/aztec-node/server.ts +1724 -573
  45. package/src/bin/index.ts +4 -2
  46. package/src/index.ts +0 -1
  47. package/src/sentinel/README.md +103 -0
  48. package/src/sentinel/config.ts +49 -0
  49. package/src/sentinel/factory.ts +34 -0
  50. package/src/sentinel/index.ts +8 -0
  51. package/src/sentinel/sentinel.ts +728 -0
  52. package/src/sentinel/store.ts +193 -0
  53. package/src/test/index.ts +32 -0
  54. package/dest/aztec-node/http_rpc_server.d.ts +0 -8
  55. package/dest/aztec-node/http_rpc_server.d.ts.map +0 -1
  56. package/dest/aztec-node/http_rpc_server.js +0 -9
  57. package/src/aztec-node/http_rpc_server.ts +0 -11
@@ -1,39 +1,80 @@
1
- import { createArchiver } from '@aztec/archiver';
2
- import { BBCircuitVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
3
- import { type BlobSinkClientInterface, createBlobSinkClient } from '@aztec/blob-sink/client';
4
- import {
5
- type 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
- REGISTERER_CONTRACT_ADDRESS,
12
- } from '@aztec/constants';
13
- import { EpochCache } from '@aztec/epoch-cache';
14
- import { type L1ContractAddresses, createEthereumChain } from '@aztec/ethereum';
15
- import { compactArray } from '@aztec/foundation/collection';
1
+ import { Archiver, L1ToL2MessagesNotReadyError, createArchiver } from '@aztec/archiver';
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';
7
+ import { EpochCache, type EpochCacheInterface } from '@aztec/epoch-cache';
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, pickL1ContractAddresses } 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';
16
16
  import { EthAddress } from '@aztec/foundation/eth-address';
17
- import { Fr } from '@aztec/foundation/fields';
17
+ import { BadRequestError } from '@aztec/foundation/json-rpc';
18
18
  import { type Logger, createLogger } from '@aztec/foundation/log';
19
+ import { retryUntil } from '@aztec/foundation/retry';
20
+ import { count } from '@aztec/foundation/string';
19
21
  import { DateProvider, Timer } from '@aztec/foundation/timer';
20
- import { SiblingPath } from '@aztec/foundation/trees';
21
- import type { AztecKVStore } from '@aztec/kv-store';
22
- import { openTmpStore } from '@aztec/kv-store/lmdb';
23
- import { SHA256Trunc, StandardTree, UnbalancedTree } from '@aztec/merkle-tree';
24
- import { type P2P, createP2PClient } from '@aztec/p2p';
22
+ import { MembershipWitness, SiblingPath } from '@aztec/foundation/trees';
23
+ import { isErrorClass } from '@aztec/foundation/types';
24
+ import { type KeyStore, KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
25
+ import { trySnapshotSync, uploadSnapshot } from '@aztec/node-lib/actions';
26
+ import { createForwarderL1TxUtilsFromSigners, createL1TxUtilsFromSigners } from '@aztec/node-lib/factories';
27
+ import {
28
+ type P2P,
29
+ type P2PClientDeps,
30
+ createP2PClient,
31
+ createTxValidatorForAcceptingTxsOverRPC,
32
+ getDefaultAllowedSetupFunctions,
33
+ } from '@aztec/p2p';
25
34
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
35
+ import { type ProverNode, type ProverNodeDeps, createProverNode } from '@aztec/prover-node';
36
+ import { createKeyStoreForProver } from '@aztec/prover-node/config';
26
37
  import {
38
+ AutomineSequencer,
39
+ FeeProviderImpl,
27
40
  GlobalVariableBuilder,
28
41
  SequencerClient,
29
42
  type SequencerPublisher,
30
- createSlasherClient,
31
- createValidatorForAcceptingTxs,
32
- getDefaultAllowedSetupFunctions,
43
+ createAutomineSequencer,
33
44
  } from '@aztec/sequencer-client';
34
- import { PublicProcessorFactory } from '@aztec/simulator/server';
45
+ import { PublicContractsDB, PublicProcessorFactory } from '@aztec/simulator/server';
46
+ import {
47
+ AttestationsBlockWatcher,
48
+ AttestedInvalidProposalWatcher,
49
+ BroadcastedInvalidCheckpointProposalWatcher,
50
+ CheckpointEquivocationWatcher,
51
+ DataWithholdingWatcher,
52
+ type SlasherClientInterface,
53
+ type Watcher,
54
+ createSlasher,
55
+ } from '@aztec/slasher';
56
+ import { STANDARD_MULTI_CALL_ENTRYPOINT_ADDRESS } from '@aztec/standard-contracts/multi-call-entrypoint';
57
+ import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
35
58
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
36
- import type { InBlock, L2Block, L2BlockNumber, L2BlockSource, NullifierWithBlockSource } from '@aztec/stdlib/block';
59
+ import {
60
+ type BlockData,
61
+ BlockHash,
62
+ type BlockParameter,
63
+ BlockTag,
64
+ type CheckpointsQuery,
65
+ type CommitteeAttestation,
66
+ type DataInBlock,
67
+ type L2BlockSource,
68
+ type L2Tips,
69
+ type NormalizedBlockParameter,
70
+ inspectBlockParameter,
71
+ } from '@aztec/stdlib/block';
72
+ import {
73
+ type CheckpointData,
74
+ CheckpointReexecutionTracker,
75
+ L1PublishedData,
76
+ type PublishedCheckpoint,
77
+ } from '@aztec/stdlib/checkpoint';
37
78
  import type {
38
79
  ContractClassPublic,
39
80
  ContractDataSource,
@@ -41,34 +82,65 @@ import type {
41
82
  NodeInfo,
42
83
  ProtocolContractAddresses,
43
84
  } from '@aztec/stdlib/contract';
44
- import type { GasFees } from '@aztec/stdlib/gas';
45
- import { computePublicDataTreeLeafSlot, siloNullifier } from '@aztec/stdlib/hash';
46
- import type { AztecNode, GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
85
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
86
+ import { GasFees, type ManaUsageEstimate } from '@aztec/stdlib/gas';
87
+ import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
88
+ import type {
89
+ AztecNode,
90
+ AztecNodeAdmin,
91
+ AztecNodeAdminConfig,
92
+ AztecNodeDebug,
93
+ BlockIncludeOptions,
94
+ BlockResponse,
95
+ BlocksIncludeOptions,
96
+ ChainTip,
97
+ ChainTips,
98
+ CheckpointIncludeOptions,
99
+ CheckpointParameter,
100
+ CheckpointResponse,
101
+ } from '@aztec/stdlib/interfaces/client';
102
+ import { AztecNodeAdminConfigSchema } from '@aztec/stdlib/interfaces/client';
47
103
  import {
104
+ type AllowedElement,
48
105
  type ClientProtocolCircuitVerifier,
49
106
  type L2LogsSource,
50
- type ProverConfig,
51
- type SequencerConfig,
52
107
  type Service,
53
108
  type WorldStateSyncStatus,
54
109
  type WorldStateSynchronizer,
55
110
  tryStop,
56
111
  } from '@aztec/stdlib/interfaces/server';
57
- import type { LogFilter, PrivateLog, TxScopedL2Log } from '@aztec/stdlib/logs';
58
- import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
59
- import { P2PClientType } from '@aztec/stdlib/p2p';
112
+ import type { DebugLogStore, LogResult, PrivateLogsQuery, PublicLogsQuery } from '@aztec/stdlib/logs';
113
+ import { InMemoryDebugLogStore, NullDebugLogStore } from '@aztec/stdlib/logs';
114
+ import {
115
+ InboxLeaf,
116
+ type L1ToL2MessageSource,
117
+ type L2ToL1MembershipWitness,
118
+ appendL1ToL2MessagesToTree,
119
+ } from '@aztec/stdlib/messaging';
120
+ import type { Offense } from '@aztec/stdlib/slashing';
121
+ import { MIN_EXECUTION_TIME } from '@aztec/stdlib/timetable';
122
+ import type { NullifierLeafPreimage, PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
60
123
  import { MerkleTreeId, NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
61
- import type { NullifierLeafPreimage, PublicDataTreeLeaf, PublicDataTreeLeafPreimage } from '@aztec/stdlib/trees';
62
124
  import {
63
- type BlockHeader,
125
+ DroppedTxReceipt,
126
+ type FeeProvider,
127
+ type GetTxReceiptOptions,
128
+ type GlobalVariableBuilder as GlobalVariableBuilderInterface,
129
+ type IndexedTxEffect,
130
+ MinedTxReceipt,
131
+ type MinedTxStatus,
132
+ PendingTxReceipt,
64
133
  PublicSimulationOutput,
134
+ type SimulationOverrides,
65
135
  Tx,
66
- TxEffect,
67
136
  type TxHash,
68
- TxReceipt,
137
+ type TxReceipt,
69
138
  TxStatus,
70
139
  type TxValidationResult,
71
140
  } from '@aztec/stdlib/tx';
141
+ import { getPackageVersion } from '@aztec/stdlib/update-checker';
142
+ import type { SingleValidatorStats, ValidatorsStats } from '@aztec/stdlib/validators';
143
+ import type { GenesisData } from '@aztec/stdlib/world-state';
72
144
  import {
73
145
  Attributes,
74
146
  type TelemetryClient,
@@ -77,18 +149,41 @@ import {
77
149
  getTelemetryClient,
78
150
  trackSpan,
79
151
  } from '@aztec/telemetry-client';
80
- import { createValidatorClient } from '@aztec/validator-client';
81
- import { createWorldStateSynchronizer } from '@aztec/world-state';
82
-
83
- import { type AztecNodeConfig, getPackageVersion } from './config.js';
152
+ import {
153
+ FullNodeCheckpointsBuilder as CheckpointsBuilder,
154
+ FullNodeCheckpointsBuilder,
155
+ NodeKeystoreAdapter,
156
+ ValidatorClient,
157
+ createProposalHandler,
158
+ createValidatorClient,
159
+ } from '@aztec/validator-client';
160
+ import type { SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
161
+ import { createWorldState, createWorldStateSynchronizer } from '@aztec/world-state';
162
+
163
+ import { createPublicClient } from 'viem';
164
+
165
+ import { createSentinel } from '../sentinel/factory.js';
166
+ import { Sentinel } from '../sentinel/sentinel.js';
167
+ import {
168
+ blockResponseFromBlockData,
169
+ blockResponseFromL2Block,
170
+ checkpointResponseFromCheckpointData,
171
+ checkpointResponseFromPublishedCheckpoint,
172
+ projectProposedToCheckpointResponse,
173
+ } from './block_response_helpers.js';
174
+ import { type AztecNodeConfig, createKeyStoreForValidator } from './config.js';
84
175
  import { NodeMetrics } from './node_metrics.js';
176
+ import { applyPublicDataOverrides } from './public_data_overrides.js';
85
177
 
86
178
  /**
87
179
  * The aztec node.
88
180
  */
89
- export class AztecNodeService implements AztecNode, Traceable {
90
- private packageVersion: string;
181
+ export class AztecNodeService implements AztecNode, AztecNodeAdmin, AztecNodeDebug, Traceable {
91
182
  private metrics: NodeMetrics;
183
+ // Prevent two snapshot operations to happen simultaneously
184
+ private isUploadingSnapshot = false;
185
+ // Saved minTxsPerBlock used by `pauseSequencer` to restore production-sequencer config on resume.
186
+ private sequencerPausedMinTxsPerBlock: number | undefined;
92
187
 
93
188
  public readonly tracer: Tracer;
94
189
 
@@ -99,22 +194,45 @@ export class AztecNodeService implements AztecNode, Traceable {
99
194
  protected readonly logsSource: L2LogsSource,
100
195
  protected readonly contractDataSource: ContractDataSource,
101
196
  protected readonly l1ToL2MessageSource: L1ToL2MessageSource,
102
- protected readonly nullifierSource: NullifierWithBlockSource,
103
197
  protected readonly worldStateSynchronizer: WorldStateSynchronizer,
104
198
  protected readonly sequencer: SequencerClient | undefined,
199
+ protected readonly proverNode: ProverNode | undefined,
200
+ protected readonly slasherClient: SlasherClientInterface | undefined,
201
+ protected readonly validatorsSentinel: Sentinel | undefined,
202
+ private readonly stopStartedWatchers: () => Promise<void>,
105
203
  protected readonly l1ChainId: number,
106
204
  protected readonly version: number,
107
- protected readonly globalVariableBuilder: GlobalVariableBuilder,
108
- private proofVerifier: ClientProtocolCircuitVerifier,
205
+ protected readonly globalVariableBuilder: GlobalVariableBuilderInterface,
206
+ protected readonly feeProvider: FeeProvider,
207
+ protected readonly epochCache: EpochCacheInterface,
208
+ protected readonly packageVersion: string,
209
+ private peerProofVerifier: ClientProtocolCircuitVerifier,
210
+ private rpcProofVerifier: ClientProtocolCircuitVerifier,
109
211
  private telemetry: TelemetryClient = getTelemetryClient(),
110
212
  private log = createLogger('node'),
213
+ private blobClient?: BlobClientInterface,
214
+ private validatorClient?: ValidatorClient,
215
+ private keyStoreManager?: KeystoreManager,
216
+ private debugLogStore: DebugLogStore = new NullDebugLogStore(),
217
+ private readonly automineSequencer?: AutomineSequencer,
111
218
  ) {
112
- this.packageVersion = getPackageVersion();
113
219
  this.metrics = new NodeMetrics(telemetry, 'AztecNodeService');
114
220
  this.tracer = telemetry.getTracer('AztecNodeService');
115
221
 
116
222
  this.log.info(`Aztec Node version: ${this.packageVersion}`);
117
- this.log.info(`Aztec Node started on chain 0x${l1ChainId.toString(16)}`, config.l1Contracts);
223
+ this.log.info(`Aztec Node started on chain 0x${l1ChainId.toString(16)}`, pickL1ContractAddresses(config));
224
+
225
+ // A defensive check that protects us against introducing a bug in the complex `createAndSync` function. We must
226
+ // never have debugLogStore enabled when not in test mode because then we would be accumulating debug logs in
227
+ // memory which could be a DoS vector on the sequencer (since no fees are paid for debug logs).
228
+ if (debugLogStore.isEnabled && config.realProofs) {
229
+ throw new Error('debugLogStore should never be enabled when realProofs are set');
230
+ }
231
+ }
232
+
233
+ /** @internal Exposed for testing — returns the RPC proof verifier. */
234
+ public getProofVerifier(): ClientProtocolCircuitVerifier {
235
+ return this.rpcProofVerifier;
118
236
  }
119
237
 
120
238
  public async getWorldStateSyncStatus(): Promise<WorldStateSyncStatus> {
@@ -122,8 +240,238 @@ export class AztecNodeService implements AztecNode, Traceable {
122
240
  return status.syncSummary;
123
241
  }
124
242
 
125
- public getL2Tips() {
126
- return this.blockSource.getL2Tips();
243
+ public async getChainTips(): Promise<ChainTips> {
244
+ const { proposed, checkpointed, proven, finalized } = await this.blockSource.getL2Tips();
245
+ return { proposed, checkpointed, proven, finalized };
246
+ }
247
+
248
+ public getCheckpointsData(query: CheckpointsQuery) {
249
+ return this.blockSource.getCheckpointsData(query);
250
+ }
251
+
252
+ public async getBlockNumber(tip?: ChainTip): Promise<BlockNumber> {
253
+ if (tip === undefined || tip === 'proposed') {
254
+ return this.blockSource.getBlockNumber();
255
+ }
256
+ return (await this.blockSource.getBlockNumber({ tag: tip })) ?? BlockNumber.ZERO;
257
+ }
258
+
259
+ public async getCheckpointNumber(tip?: ChainTip): Promise<CheckpointNumber> {
260
+ const tips = await this.blockSource.getL2Tips();
261
+ switch (tip) {
262
+ case undefined:
263
+ case 'checkpointed':
264
+ return tips.checkpointed.checkpoint.number;
265
+ case 'proposed':
266
+ return tips.proposedCheckpoint.checkpoint.number;
267
+ case 'proven':
268
+ return tips.proven.checkpoint.number;
269
+ case 'finalized':
270
+ return tips.finalized.checkpoint.number;
271
+ }
272
+ }
273
+
274
+ private isChainTip(value: unknown): value is ChainTip {
275
+ return value === 'proposed' || value === 'checkpointed' || value === 'proven' || value === 'finalized';
276
+ }
277
+
278
+ /**
279
+ * Normalizes a {@link BlockParameter} (which may be a bare value) into a
280
+ * {@link NormalizedBlockParameter} object form. Performs no chain-tip resolution — tag
281
+ * lookups are deferred to the underlying block source.
282
+ */
283
+ private normalizeBlockParameter(param: BlockParameter): NormalizedBlockParameter {
284
+ if (BlockHash.isBlockHash(param)) {
285
+ return { hash: param };
286
+ }
287
+ if (typeof param === 'number') {
288
+ return { number: param as BlockNumber };
289
+ }
290
+ if (typeof param === 'string') {
291
+ if (this.isBlockTag(param)) {
292
+ return { tag: param === 'latest' ? 'proposed' : param };
293
+ }
294
+ throw new BadRequestError(`Invalid BlockParameter tag: ${param}`);
295
+ }
296
+ if (typeof param === 'object' && param !== null) {
297
+ if ('number' in param) {
298
+ return { number: param.number };
299
+ }
300
+ if ('hash' in param) {
301
+ return { hash: param.hash };
302
+ }
303
+ if ('archive' in param) {
304
+ return { archive: param.archive };
305
+ }
306
+ if ('tag' in param) {
307
+ if (this.isBlockTag(param.tag)) {
308
+ return { tag: param.tag };
309
+ }
310
+ throw new BadRequestError(`Invalid BlockParameter tag: ${param.tag}`);
311
+ }
312
+ }
313
+ throw new BadRequestError(`Invalid BlockParameter: ${JSON.stringify(param)}`);
314
+ }
315
+
316
+ private isBlockTag(value: string): value is BlockTag {
317
+ return BlockTag.includes(value as BlockTag);
318
+ }
319
+
320
+ /**
321
+ * Resolves a {@link CheckpointParameter} into a concrete `{ number }` or `{ slot }` query.
322
+ *
323
+ * Tag-based parameters (`'proposed'`, `'checkpointed'`, `'proven'`, `'finalized'`) are
324
+ * translated up-front to the corresponding tip's checkpoint number via {@link L2BlockSource.getL2Tips}.
325
+ * After resolution the unified {@link getCheckpoint} flow can perform a single
326
+ * confirmed→proposed lookup against either store.
327
+ */
328
+ private async resolveCheckpointParameter(
329
+ param: CheckpointParameter,
330
+ ): Promise<{ number: CheckpointNumber } | { slot: SlotNumber }> {
331
+ if (typeof param === 'number') {
332
+ return { number: param as CheckpointNumber };
333
+ }
334
+ if (this.isChainTip(param)) {
335
+ const tips = await this.blockSource.getL2Tips();
336
+ switch (param) {
337
+ case 'proposed':
338
+ return { number: tips.proposedCheckpoint.checkpoint.number };
339
+ case 'checkpointed':
340
+ return { number: tips.checkpointed.checkpoint.number };
341
+ case 'proven':
342
+ return { number: tips.proven.checkpoint.number };
343
+ case 'finalized':
344
+ return { number: tips.finalized.checkpoint.number };
345
+ }
346
+ }
347
+ if (typeof param === 'object' && param !== null) {
348
+ if ('number' in param) {
349
+ return { number: param.number };
350
+ }
351
+ if ('slot' in param) {
352
+ return { slot: param.slot };
353
+ }
354
+ }
355
+ throw new BadRequestError(`Invalid CheckpointParameter: ${JSON.stringify(param)}`);
356
+ }
357
+
358
+ /** Fetches checkpoint-level L1 and attestation data for use as block response context. */
359
+ async #getCheckpointContext(
360
+ checkpointNumber: CheckpointNumber,
361
+ ): Promise<{ l1?: L1PublishedData; attestations?: CommitteeAttestation[] } | undefined> {
362
+ const checkpoint = await this.blockSource.getCheckpointData({ number: checkpointNumber });
363
+ if (!checkpoint) {
364
+ return undefined;
365
+ }
366
+ return { l1: checkpoint.l1, attestations: checkpoint.attestations };
367
+ }
368
+
369
+ public async getBlock<Opts extends BlockIncludeOptions = {}>(
370
+ param: BlockParameter,
371
+ options: Opts = {} as Opts,
372
+ ): Promise<BlockResponse<Opts> | undefined> {
373
+ const query = this.normalizeBlockParameter(param);
374
+ const wantTxs = !!options.includeTransactions;
375
+ const wantContext = !!options.includeL1PublishInfo || !!options.includeAttestations;
376
+
377
+ if (wantTxs) {
378
+ const block = await this.blockSource.getBlock(query);
379
+ if (!block) {
380
+ return undefined;
381
+ }
382
+ const ctx = wantContext ? await this.#getCheckpointContext(block.checkpointNumber) : undefined;
383
+ return (await blockResponseFromL2Block(block, options, ctx)) as BlockResponse<Opts>;
384
+ }
385
+ const data = await this.blockSource.getBlockData(query);
386
+ if (!data) {
387
+ return undefined;
388
+ }
389
+ const ctx = wantContext ? await this.#getCheckpointContext(data.checkpointNumber) : undefined;
390
+ return blockResponseFromBlockData(data, options, ctx) as BlockResponse<Opts>;
391
+ }
392
+
393
+ public getBlockData(param: BlockParameter): Promise<BlockData | undefined> {
394
+ const query = this.normalizeBlockParameter(param);
395
+ return this.blockSource.getBlockData(query);
396
+ }
397
+
398
+ public async getBlocks<Opts extends BlocksIncludeOptions = {}>(
399
+ from: BlockNumber,
400
+ limit: number,
401
+ options: Opts = {} as Opts,
402
+ ): Promise<BlockResponse<Opts>[]> {
403
+ const wantTxs = !!options.includeTransactions;
404
+ const wantContext = !!options.includeL1PublishInfo || !!options.includeAttestations;
405
+ const onlyCheckpointed = !!options.onlyCheckpointed;
406
+ if (wantTxs) {
407
+ const blocks = await this.blockSource.getBlocks({ from, limit, onlyCheckpointed });
408
+ const ctxByCheckpoint = await this.#getCheckpointContextsForBlocks(wantContext ? blocks : []);
409
+ return (await Promise.all(
410
+ blocks.map(block => blockResponseFromL2Block(block, options, ctxByCheckpoint.get(block.checkpointNumber))),
411
+ )) as BlockResponse<Opts>[];
412
+ }
413
+ const dataItems = await this.blockSource.getBlocksData({ from, limit, onlyCheckpointed });
414
+ const ctxByCheckpoint = await this.#getCheckpointContextsForBlocks(wantContext ? dataItems : []);
415
+ return (await Promise.all(
416
+ dataItems.map(data => blockResponseFromBlockData(data, options, ctxByCheckpoint.get(data.checkpointNumber))),
417
+ )) as BlockResponse<Opts>[];
418
+ }
419
+
420
+ /** Fetches checkpoint context for a set of blocks, deduplicating shared checkpoints. */
421
+ async #getCheckpointContextsForBlocks(
422
+ blocks: { checkpointNumber: CheckpointNumber }[],
423
+ ): Promise<Map<CheckpointNumber, { l1?: L1PublishedData; attestations?: CommitteeAttestation[] } | undefined>> {
424
+ const unique = Array.from(new Set(blocks.map(b => b.checkpointNumber)));
425
+ const entries = await Promise.all(unique.map(async n => [n, await this.#getCheckpointContext(n)] as const));
426
+ return new Map(entries);
427
+ }
428
+
429
+ public async getCheckpoint<Opts extends CheckpointIncludeOptions = {}>(
430
+ param: CheckpointParameter,
431
+ options: Opts = {} as Opts,
432
+ ): Promise<CheckpointResponse<Opts> | undefined> {
433
+ const query = await this.resolveCheckpointParameter(param);
434
+
435
+ // Try the confirmed store first.
436
+ const confirmed = options.includeBlocks
437
+ ? await this.blockSource.getCheckpoint(query)
438
+ : await this.blockSource.getCheckpointData(query);
439
+ if (confirmed) {
440
+ return (await (options.includeBlocks
441
+ ? checkpointResponseFromPublishedCheckpoint(confirmed as PublishedCheckpoint, options)
442
+ : checkpointResponseFromCheckpointData(confirmed as CheckpointData, options))) as CheckpointResponse<Opts>;
443
+ }
444
+
445
+ // Fall back to the proposed store.
446
+ const proposed = await this.blockSource.getProposedCheckpointData(query);
447
+ if (proposed) {
448
+ if (options.includeAttestations || options.includeL1PublishInfo) {
449
+ throw new BadRequestError(
450
+ `Options includeL1PublishInfo or includeAttestations cannot be satisfied for a proposed checkpoint`,
451
+ );
452
+ }
453
+ const blocks = options.includeBlocks
454
+ ? await this.blockSource.getBlocks({ from: proposed.startBlock, limit: proposed.blockCount })
455
+ : undefined;
456
+ return (await projectProposedToCheckpointResponse(proposed, options, blocks)) as CheckpointResponse<Opts>;
457
+ }
458
+
459
+ return undefined;
460
+ }
461
+
462
+ public async getCheckpoints<Opts extends CheckpointIncludeOptions = {}>(
463
+ from: CheckpointNumber,
464
+ limit: number,
465
+ options: Opts = {} as Opts,
466
+ ): Promise<CheckpointResponse<Opts>[]> {
467
+ if (options.includeBlocks) {
468
+ const checkpoints = await this.blockSource.getCheckpoints({ from, limit });
469
+ return (await Promise.all(
470
+ checkpoints.map(cp => checkpointResponseFromPublishedCheckpoint(cp, options)),
471
+ )) as CheckpointResponse<Opts>[];
472
+ }
473
+ const datas = await this.blockSource.getCheckpointsData({ from, limit });
474
+ return datas.map(d => checkpointResponseFromCheckpointData(d, options)) as CheckpointResponse<Opts>[];
127
475
  }
128
476
 
129
477
  /**
@@ -132,99 +480,551 @@ export class AztecNodeService implements AztecNode, Traceable {
132
480
  * @returns - A fully synced Aztec Node for use in development/testing.
133
481
  */
134
482
  public static async createAndSync(
135
- config: AztecNodeConfig,
483
+ inputConfig: AztecNodeConfig,
136
484
  deps: {
137
485
  telemetry?: TelemetryClient;
138
486
  logger?: Logger;
139
487
  publisher?: SequencerPublisher;
140
488
  dateProvider?: DateProvider;
141
- blobSinkClient?: BlobSinkClientInterface;
489
+ p2pClientDeps?: P2PClientDeps;
490
+ proverNodeDeps?: Partial<ProverNodeDeps>;
491
+ slashingProtectionDb?: SlashingProtectionDatabase;
142
492
  } = {},
143
493
  options: {
144
- prefilledPublicData?: PublicDataTreeLeaf[];
494
+ genesis?: GenesisData;
495
+ dontStartSequencer?: boolean;
496
+ dontStartProverNode?: boolean;
145
497
  } = {},
146
498
  ): Promise<AztecNodeService> {
147
- const telemetry = deps.telemetry ?? getTelemetryClient();
499
+ const config = { ...inputConfig }; // Copy the config so we dont mutate the input object
148
500
  const log = deps.logger ?? createLogger('node');
501
+ const packageVersion = getPackageVersion();
502
+ const telemetry = deps.telemetry ?? getTelemetryClient();
149
503
  const dateProvider = deps.dateProvider ?? new DateProvider();
150
- const blobSinkClient = deps.blobSinkClient ?? createBlobSinkClient(config);
151
504
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
152
- //validate that the actual chain id matches that specified in configuration
505
+
506
+ // Build a key store from file if given or from environment otherwise.
507
+ // We keep the raw KeyStore available so we can merge with prover keys if enableProverNode is set.
508
+ let keyStoreManager: KeystoreManager | undefined;
509
+ const keyStoreProvided = config.keyStoreDirectory !== undefined && config.keyStoreDirectory.length > 0;
510
+ if (keyStoreProvided) {
511
+ const keyStores = loadKeystores(config.keyStoreDirectory!);
512
+ keyStoreManager = new KeystoreManager(mergeKeystores(keyStores));
513
+ } else {
514
+ const rawKeyStores: KeyStore[] = [];
515
+ const validatorKeyStore = createKeyStoreForValidator(config);
516
+ if (validatorKeyStore) {
517
+ rawKeyStores.push(validatorKeyStore);
518
+ }
519
+ if (config.enableProverNode) {
520
+ const proverKeyStore = createKeyStoreForProver(config);
521
+ if (proverKeyStore) {
522
+ rawKeyStores.push(proverKeyStore);
523
+ }
524
+ }
525
+ if (rawKeyStores.length > 0) {
526
+ keyStoreManager = new KeystoreManager(
527
+ rawKeyStores.length === 1 ? rawKeyStores[0] : mergeKeystores(rawKeyStores),
528
+ );
529
+ }
530
+ }
531
+
532
+ await keyStoreManager?.validateSigners();
533
+
534
+ // If we are a validator, verify our configuration before doing too much more.
535
+ if (!config.disableValidator) {
536
+ if (keyStoreManager === undefined) {
537
+ throw new Error('Failed to create key store, a requirement for running a validator');
538
+ }
539
+ if (!keyStoreProvided && process.env.NODE_ENV !== 'test') {
540
+ log.warn("Keystore created from env: it's recommended to use a file-based key store for production");
541
+ }
542
+ ValidatorClient.validateKeyStoreConfiguration(keyStoreManager, log);
543
+ }
544
+
545
+ // validate that the actual chain id matches that specified in configuration
153
546
  if (config.l1ChainId !== ethereumChain.chainInfo.id) {
154
547
  throw new Error(
155
548
  `RPC URL configured for chain id ${ethereumChain.chainInfo.id} but expected id ${config.l1ChainId}`,
156
549
  );
157
550
  }
158
551
 
159
- const archiver = await createArchiver(config, blobSinkClient, { blockUntilSync: true }, telemetry);
552
+ const publicClient = createPublicClient({
553
+ chain: ethereumChain.chainInfo,
554
+ transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: config.l1HttpTimeoutMS }),
555
+ pollingInterval: config.viemPollingIntervalMS,
556
+ });
160
557
 
161
- // now create the merkle trees and the world state synchronizer
162
- const worldStateSynchronizer = await createWorldStateSynchronizer(
163
- config,
164
- archiver,
165
- options.prefilledPublicData,
166
- telemetry,
167
- );
168
- const proofVerifier = config.realProofs ? await BBCircuitVerifier.new(config) : new TestCircuitVerifier();
169
- if (!config.realProofs) {
170
- log.warn(`Aztec node is accepting fake proofs`);
171
- }
172
-
173
- const epochCache = await EpochCache.create(config.l1Contracts.rollupAddress, config, { dateProvider });
174
-
175
- // create the tx pool and the p2p client, which will need the l2 block source
176
- const p2pClient = await createP2PClient(
177
- P2PClientType.Full,
178
- config,
179
- archiver,
180
- proofVerifier,
181
- worldStateSynchronizer,
182
- epochCache,
183
- telemetry,
558
+ const l1ContractsAddresses = await RegistryContract.collectAddresses(
559
+ publicClient,
560
+ config.registryAddress,
561
+ config.rollupVersion ?? 'canonical',
184
562
  );
185
563
 
186
- const slasherClient = createSlasherClient(config, archiver, telemetry);
564
+ Object.assign(config, l1ContractsAddresses);
187
565
 
188
- // start both and wait for them to sync from the block source
189
- await Promise.all([p2pClient.start(), worldStateSynchronizer.start(), slasherClient.start()]);
190
- log.verbose(`All Aztec Node subsystems synced`);
566
+ const rollupContract = new RollupContract(publicClient, config.rollupAddress.toString());
567
+ const [l1GenesisTime, slotDuration, rollupVersionFromRollup, rollupManaLimit] = await Promise.all([
568
+ rollupContract.getL1GenesisTime(),
569
+ rollupContract.getSlotDuration(),
570
+ rollupContract.getVersion(),
571
+ rollupContract.getManaLimit().then(Number),
572
+ ] as const);
191
573
 
192
- const validatorClient = createValidatorClient(config, { p2pClient, telemetry, dateProvider, epochCache });
574
+ config.rollupVersion ??= Number(rollupVersionFromRollup);
193
575
 
194
- // now create the sequencer
195
- const sequencer = config.disableValidator
196
- ? undefined
197
- : await SequencerClient.new(config, {
198
- ...deps,
199
- validatorClient,
576
+ if (config.rollupVersion !== Number(rollupVersionFromRollup)) {
577
+ log.warn(
578
+ `Registry looked up and returned a rollup with version (${config.rollupVersion}), but this does not match with version detected from the rollup directly: (${rollupVersionFromRollup}).`,
579
+ );
580
+ }
581
+
582
+ const blobClient = await createBlobClientWithFileStores(config, log.createChild('blob-client'));
583
+
584
+ // attempt snapshot sync if possible
585
+ await trySnapshotSync(config, log);
586
+
587
+ const epochCache = await EpochCache.create(config.rollupAddress, config, { dateProvider });
588
+
589
+ // Track started resources so we can clean up on partial failure during node creation.
590
+ const started: { stop?(): Promise<void> | void }[] = [];
591
+ try {
592
+ // Default the orphan-prune grace window from the block build duration when unset, so the archiver
593
+ // waits roughly one build slot for a proposed checkpoint to arrive before pruning a block-only tip.
594
+ config.orphanProposedBlockPruneGraceSeconds ??=
595
+ config.blockDurationMs !== undefined ? Math.ceil(config.blockDurationMs / 1000) : MIN_EXECUTION_TIME;
596
+
597
+ // Create world-state first so we can retrieve the initial header before constructing the archiver.
598
+ const nativeWs = await createWorldState(config, options.genesis);
599
+ const initialHeader = nativeWs.getInitialHeader();
600
+ const initialBlockHash = await initialHeader.hash();
601
+ const archiver = await createArchiver(
602
+ config,
603
+ { blobClient, epochCache, telemetry, dateProvider },
604
+ {
605
+ blockUntilSync: !config.skipArchiverInitialSync,
606
+ // The non-pipelined automine sequencer publishes each checkpoint in-slot, so it never
607
+ // leaves orphan proposed blocks; pruning would race its local push. See pruneOrphanProposedBlocks.
608
+ enableOrphanProposedBlockPruning: !config.useAutomineSequencer,
609
+ },
610
+ initialHeader,
611
+ initialBlockHash,
612
+ );
613
+ started.push(archiver);
614
+
615
+ // The synchronizer takes ownership of the native world-state from here
616
+ const worldStateSynchronizer = await createWorldStateSynchronizer(config, archiver, nativeWs, telemetry);
617
+ started.push(worldStateSynchronizer);
618
+ const useRealVerifiers = config.realProofs || config.debugForceTxProofVerification;
619
+ let peerProofVerifier: ClientProtocolCircuitVerifier;
620
+ let rpcProofVerifier: ClientProtocolCircuitVerifier;
621
+ if (useRealVerifiers) {
622
+ peerProofVerifier = await BatchChonkVerifier.new(config, config.bbChonkVerifyMaxBatch, 'peer');
623
+ const rpcVerifier = await BBCircuitVerifier.new(config);
624
+ rpcProofVerifier = new QueuedIVCVerifier(rpcVerifier, config.numConcurrentIVCVerifiers);
625
+ } else {
626
+ peerProofVerifier = new TestCircuitVerifier(config.proverTestVerificationDelayMs);
627
+ rpcProofVerifier = new TestCircuitVerifier(config.proverTestVerificationDelayMs);
628
+ }
629
+ started.push(peerProofVerifier, rpcProofVerifier);
630
+
631
+ let debugLogStore: DebugLogStore;
632
+ if (!config.realProofs) {
633
+ log.warn(`Aztec node is accepting fake proofs`);
634
+
635
+ debugLogStore = new InMemoryDebugLogStore();
636
+ log.info(
637
+ 'Aztec node started in test mode (realProofs set to false) hence debug logs from public functions will be collected and served',
638
+ );
639
+ } else {
640
+ debugLogStore = new NullDebugLogStore();
641
+ }
642
+
643
+ const globalVariableBuilderConfig = {
644
+ rollupAddress: config.rollupAddress,
645
+ ethereumSlotDuration: config.ethereumSlotDuration,
646
+ rollupVersion: BigInt(config.rollupVersion),
647
+ l1GenesisTime,
648
+ slotDuration: Number(slotDuration),
649
+ };
650
+
651
+ const globalVariableBuilder = new GlobalVariableBuilder(dateProvider, publicClient, globalVariableBuilderConfig);
652
+ const feeProvider = new FeeProviderImpl(dateProvider, publicClient, globalVariableBuilderConfig);
653
+
654
+ const proverOnly = config.enableProverNode && config.disableValidator;
655
+ if (proverOnly) {
656
+ log.info('Starting in prover-only mode: skipping validator, sequencer, sentinel, and slasher subsystems');
657
+ }
658
+
659
+ // create the tx pool and the p2p client, which will need the l2 block source
660
+ const p2pClient = await createP2PClient(
661
+ config,
662
+ archiver,
663
+ peerProofVerifier,
664
+ worldStateSynchronizer,
665
+ epochCache,
666
+ feeProvider,
667
+ packageVersion,
668
+ dateProvider,
669
+ telemetry,
670
+ deps.p2pClientDeps,
671
+ initialBlockHash,
672
+ );
673
+ started.push(p2pClient);
674
+
675
+ // We'll accumulate sentinel watchers here
676
+ const watchers: Watcher[] = [];
677
+
678
+ // Create FullNodeCheckpointsBuilder for block proposal handling and tx validation.
679
+ // Override maxTxsPerCheckpoint with the validator-specific limit if set.
680
+ const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder(
681
+ {
682
+ ...config,
683
+ l1GenesisTime,
684
+ slotDuration: Number(slotDuration),
685
+ rollupManaLimit,
686
+ maxTxsPerCheckpoint: config.validateMaxTxsPerCheckpoint,
687
+ },
688
+ worldStateSynchronizer,
689
+ archiver,
690
+ dateProvider,
691
+ telemetry,
692
+ );
693
+
694
+ let validatorClient: ValidatorClient | undefined;
695
+
696
+ // Tracks successful checkpoint re-execution by a checkpoint proposal handler.
697
+ const reexecutionTracker = new CheckpointReexecutionTracker();
698
+
699
+ if (!config.disableValidator) {
700
+ // Create validator client if required
701
+ validatorClient = await createValidatorClient(config, {
702
+ checkpointsBuilder: validatorCheckpointsBuilder,
703
+ worldState: worldStateSynchronizer,
200
704
  p2pClient,
201
- worldStateSynchronizer,
202
- slasherClient,
203
- contractDataSource: archiver,
204
- l2BlockSource: archiver,
705
+ telemetry,
706
+ dateProvider,
707
+ epochCache,
708
+ blockSource: archiver,
205
709
  l1ToL2MessageSource: archiver,
710
+ keyStoreManager,
711
+ blobClient,
712
+ reexecutionTracker,
713
+ slashingProtectionDb: deps.slashingProtectionDb,
714
+ });
715
+
716
+ // If we have a validator client, register it as a source of offenses for the slasher,
717
+ // and have it register callbacks on the p2p client *before* we start it, otherwise messages
718
+ // like attestations or auths will fail.
719
+ if (validatorClient) {
720
+ watchers.push(validatorClient);
721
+
722
+ const vc = validatorClient;
723
+ const getValidatorAddresses = () => vc.getValidatorAddresses().map(a => a.toString());
724
+ validatorClient.getProposalHandler().register(p2pClient, true, archiver, getValidatorAddresses);
725
+
726
+ if (!options.dontStartSequencer) {
727
+ await validatorClient.registerHandlers();
728
+ }
729
+ }
730
+ }
731
+
732
+ // If there's no validator client, create a ProposalHandler to handle block and checkpoint proposals
733
+ // for monitoring or reexecution. Reexecution (default) allows us to follow the pending chain,
734
+ // while non-reexecution is used for validating the proposals and collecting their txs.
735
+ // Checkpoint proposals rebuild blobs if the blob client can upload blobs.
736
+ if (!validatorClient) {
737
+ const reexecute = !!config.alwaysReexecuteBlockProposals;
738
+ log.info(`Setting up proposal handler` + (reexecute ? ' with reexecution of proposals' : ''));
739
+ createProposalHandler(config, {
740
+ checkpointsBuilder: validatorCheckpointsBuilder,
741
+ worldState: worldStateSynchronizer,
742
+ epochCache,
743
+ blockSource: archiver,
744
+ l1ToL2MessageSource: archiver,
745
+ p2pClient,
746
+ blobClient,
747
+ dateProvider,
748
+ telemetry,
749
+ reexecutionTracker,
750
+ }).register(p2pClient, reexecute, archiver);
751
+ }
752
+
753
+ // Start world state and wait for it to sync to the archiver.
754
+ await worldStateSynchronizer.start();
755
+
756
+ // Start p2p. Note that it depends on world state to be running.
757
+ await p2pClient.start();
758
+
759
+ let validatorsSentinel: Awaited<ReturnType<typeof createSentinel>> | undefined;
760
+ let dataWithholdingWatcher: DataWithholdingWatcher | undefined;
761
+ let attestationsBlockWatcher: AttestationsBlockWatcher | undefined;
762
+ let attestedInvalidProposalWatcher: AttestedInvalidProposalWatcher | undefined;
763
+ let broadcastedInvalidCheckpointProposalWatcher: BroadcastedInvalidCheckpointProposalWatcher | undefined;
764
+ let checkpointEquivocationWatcher: CheckpointEquivocationWatcher | undefined;
765
+
766
+ if (!proverOnly) {
767
+ validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, reexecutionTracker, config);
768
+ if (validatorsSentinel) {
769
+ watchers.push(validatorsSentinel);
770
+ }
771
+
772
+ dataWithholdingWatcher = new DataWithholdingWatcher(
773
+ epochCache,
774
+ archiver,
775
+ p2pClient.getTxProvider(),
776
+ p2pClient,
777
+ reexecutionTracker,
778
+ { chainId: config.l1ChainId, rollupAddress: config.rollupAddress },
779
+ config,
780
+ );
781
+ watchers.push(dataWithholdingWatcher);
782
+
783
+ broadcastedInvalidCheckpointProposalWatcher = new BroadcastedInvalidCheckpointProposalWatcher(
784
+ p2pClient,
785
+ archiver,
786
+ epochCache,
787
+ config,
788
+ );
789
+ watchers.push(broadcastedInvalidCheckpointProposalWatcher);
790
+
791
+ if (validatorClient) {
792
+ attestedInvalidProposalWatcher = new AttestedInvalidProposalWatcher(
793
+ p2pClient,
794
+ validatorClient,
795
+ archiver,
796
+ epochCache,
797
+ config,
798
+ { log: log.createChild('attested-invalid-proposal-watcher') },
799
+ );
800
+ watchers.push(attestedInvalidProposalWatcher);
801
+ }
802
+
803
+ checkpointEquivocationWatcher = new CheckpointEquivocationWatcher(archiver, epochCache, config);
804
+ watchers.push(checkpointEquivocationWatcher);
805
+
806
+ attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config, log.getBindings());
807
+ watchers.push(attestationsBlockWatcher);
808
+ }
809
+
810
+ const watchersToStart = compactArray([
811
+ validatorsSentinel,
812
+ dataWithholdingWatcher,
813
+ attestationsBlockWatcher,
814
+ broadcastedInvalidCheckpointProposalWatcher,
815
+ attestedInvalidProposalWatcher,
816
+ checkpointEquivocationWatcher,
817
+ ]);
818
+ const startedWatchers: Watcher[] = [];
819
+ const stopStartedWatchers = async () => {
820
+ for (const watcher of startedWatchers) {
821
+ await tryStop(watcher);
822
+ }
823
+ };
824
+
825
+ // Start p2p-related services once the archiver has completed sync
826
+ void archiver
827
+ .waitForInitialSync()
828
+ .then(async () => {
829
+ for (const watcher of watchersToStart) {
830
+ await watcher.start();
831
+ startedWatchers.push(watcher);
832
+ }
833
+ log.info(`All p2p services started`);
834
+ })
835
+ .catch(err => log.error('Failed to start p2p services after archiver sync', err));
836
+ started.push({ stop: stopStartedWatchers });
837
+
838
+ // Validator enabled, create/start relevant service
839
+ let sequencer: SequencerClient | undefined;
840
+ let automineSequencer: AutomineSequencer | undefined;
841
+ let slasherClient: SlasherClientInterface | undefined;
842
+ if (!config.disableValidator && validatorClient) {
843
+ // We create a slasher only if we have a sequencer, since all slashing actions go through the sequencer publisher
844
+ // as they are executed when the node is selected as proposer.
845
+ const validatorAddresses = keyStoreManager
846
+ ? NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager).getAddresses()
847
+ : [];
848
+
849
+ slasherClient = await createSlasher(
850
+ config,
851
+ pickL1ContractAddresses(config),
852
+ getPublicClient(config),
853
+ watchers,
854
+ dateProvider,
855
+ epochCache,
856
+ validatorAddresses,
857
+ undefined, // logger
858
+ );
859
+ await slasherClient.start();
860
+ started.push(slasherClient);
861
+
862
+ const l1TxUtils = config.sequencerPublisherForwarderAddress
863
+ ? await createForwarderL1TxUtilsFromSigners(
864
+ publicClient,
865
+ keyStoreManager!.createAllValidatorPublisherSigners(),
866
+ config.sequencerPublisherForwarderAddress,
867
+ { ...config, scope: 'sequencer' },
868
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
869
+ )
870
+ : await createL1TxUtilsFromSigners(
871
+ publicClient,
872
+ keyStoreManager!.createAllValidatorPublisherSigners(),
873
+ { ...config, scope: 'sequencer' },
874
+ { telemetry, logger: log.createChild('l1-tx-utils'), dateProvider, kzg: Blob.getViemKzgInstance() },
875
+ );
876
+
877
+ // Create a funder L1TxUtils from the keystore funding account (if configured)
878
+ const fundingSigner = keyStoreManager?.createFundingSigner();
879
+ let funderL1TxUtils: L1TxUtils | undefined;
880
+ if (fundingSigner) {
881
+ const [funder] = await createL1TxUtilsFromSigners(
882
+ publicClient,
883
+ [fundingSigner],
884
+ { ...config, scope: 'sequencer' },
885
+ { telemetry, logger: log.createChild('l1-tx-utils:funder'), dateProvider },
886
+ );
887
+ funderL1TxUtils = funder;
888
+ }
889
+
890
+ // Create and start the sequencer client
891
+ const checkpointsBuilder = new CheckpointsBuilder(
892
+ { ...config, l1GenesisTime, slotDuration: Number(slotDuration), rollupManaLimit },
893
+ worldStateSynchronizer,
894
+ archiver,
895
+ dateProvider,
896
+ telemetry,
897
+ debugLogStore,
898
+ );
899
+
900
+ if (config.useAutomineSequencer) {
901
+ // Test-only path: deterministic, queue-driven sequencer for non-block-building e2e tests.
902
+ // See `AUTOMINE_E2E_OPTS` in `end-to-end/src/fixtures/fixtures.ts`.
903
+ automineSequencer = await createAutomineSequencer({
904
+ config,
905
+ l1TxUtils,
906
+ funderL1TxUtils,
907
+ publicClient,
908
+ rollupContract,
909
+ epochCache,
910
+ blobClient,
911
+ telemetry,
912
+ dateProvider,
913
+ keyStoreManager: keyStoreManager!,
914
+ validatorClient,
915
+ checkpointsBuilder,
916
+ globalVariableBuilder,
917
+ worldStateSynchronizer,
918
+ archiver,
919
+ p2pClient,
920
+ l1Constants: {
921
+ l1GenesisTime,
922
+ slotDuration: Number(slotDuration),
923
+ ethereumSlotDuration: config.ethereumSlotDuration,
924
+ rollupManaLimit,
925
+ },
926
+ log,
927
+ });
928
+ } else {
929
+ sequencer = await SequencerClient.new(config, {
930
+ ...deps,
931
+ epochCache,
932
+ l1TxUtils,
933
+ funderL1TxUtils,
934
+ validatorClient,
935
+ p2pClient,
936
+ worldStateSynchronizer,
937
+ slasherClient,
938
+ checkpointsBuilder,
939
+ l2BlockSource: archiver,
940
+ l1ToL2MessageSource: archiver,
941
+ telemetry,
942
+ dateProvider,
943
+ blobClient,
944
+ nodeKeyStore: keyStoreManager!,
945
+ globalVariableBuilder,
946
+ });
947
+ }
948
+ }
949
+
950
+ if (!options.dontStartSequencer && sequencer) {
951
+ await sequencer.start();
952
+ started.push(sequencer);
953
+ log.verbose(`Sequencer started`);
954
+ } else if (sequencer) {
955
+ log.warn(`Sequencer created but not started`);
956
+ }
957
+
958
+ if (!options.dontStartSequencer && automineSequencer) {
959
+ await automineSequencer.start();
960
+ started.push({ stop: () => automineSequencer!.stop() });
961
+ log.verbose(`AutomineSequencer started`);
962
+ } else if (automineSequencer) {
963
+ log.warn(`AutomineSequencer created but not started`);
964
+ }
965
+
966
+ // Create prover node subsystem if enabled
967
+ let proverNode: ProverNode | undefined;
968
+ if (config.enableProverNode) {
969
+ proverNode = await createProverNode(config, {
970
+ ...deps.proverNodeDeps,
206
971
  telemetry,
207
972
  dateProvider,
208
- blobSinkClient,
973
+ archiver,
974
+ worldStateSynchronizer,
975
+ p2pClient,
976
+ epochCache,
977
+ blobClient,
978
+ keyStoreManager,
209
979
  });
210
980
 
211
- return new AztecNodeService(
212
- config,
213
- p2pClient,
214
- archiver,
215
- archiver,
216
- archiver,
217
- archiver,
218
- archiver,
219
- worldStateSynchronizer,
220
- sequencer,
221
- ethereumChain.chainInfo.id,
222
- config.version,
223
- new GlobalVariableBuilder(config),
224
- proofVerifier,
225
- telemetry,
226
- log,
227
- );
981
+ if (!options.dontStartProverNode) {
982
+ await proverNode.start();
983
+ started.push(proverNode);
984
+ log.info(`Prover node subsystem started`);
985
+ } else {
986
+ log.info(`Prover node subsystem created but not started`);
987
+ }
988
+ }
989
+
990
+ const node = new AztecNodeService(
991
+ config,
992
+ p2pClient,
993
+ archiver,
994
+ archiver,
995
+ archiver,
996
+ archiver,
997
+ worldStateSynchronizer,
998
+ sequencer,
999
+ proverNode,
1000
+ slasherClient,
1001
+ validatorsSentinel,
1002
+ stopStartedWatchers,
1003
+ ethereumChain.chainInfo.id,
1004
+ config.rollupVersion,
1005
+ globalVariableBuilder,
1006
+ feeProvider,
1007
+ epochCache,
1008
+ packageVersion,
1009
+ peerProofVerifier,
1010
+ rpcProofVerifier,
1011
+ telemetry,
1012
+ log,
1013
+ blobClient,
1014
+ validatorClient,
1015
+ keyStoreManager,
1016
+ debugLogStore,
1017
+ automineSequencer,
1018
+ );
1019
+
1020
+ return node;
1021
+ } catch (err) {
1022
+ log.error('Failed during node creation, stopping started resources', err);
1023
+ for (const resource of started.reverse()) {
1024
+ await tryStop(resource);
1025
+ }
1026
+ throw err;
1027
+ }
228
1028
  }
229
1029
 
230
1030
  /**
@@ -235,6 +1035,16 @@ export class AztecNodeService implements AztecNode, Traceable {
235
1035
  return this.sequencer;
236
1036
  }
237
1037
 
1038
+ /** Test-only: returns the AutomineSequencer when wired via `useAutomineSequencer`. */
1039
+ public getAutomineSequencer(): AutomineSequencer | undefined {
1040
+ return this.automineSequencer;
1041
+ }
1042
+
1043
+ /** Returns the prover node subsystem, if enabled. */
1044
+ public getProverNode(): ProverNode | undefined {
1045
+ return this.proverNode;
1046
+ }
1047
+
238
1048
  public getBlockSource(): L2BlockSource {
239
1049
  return this.blockSource;
240
1050
  }
@@ -252,13 +1062,17 @@ export class AztecNodeService implements AztecNode, Traceable {
252
1062
  * @returns - The currently deployed L1 contract addresses.
253
1063
  */
254
1064
  public getL1ContractAddresses(): Promise<L1ContractAddresses> {
255
- return Promise.resolve(this.config.l1Contracts);
1065
+ return Promise.resolve(pickL1ContractAddresses(this.config));
256
1066
  }
257
1067
 
258
1068
  public getEncodedEnr(): Promise<string | undefined> {
259
1069
  return Promise.resolve(this.p2pClient.getEnr()?.encodeTxt());
260
1070
  }
261
1071
 
1072
+ public async getAllowedPublicSetup(): Promise<AllowedElement[]> {
1073
+ return [...(await getDefaultAllowedSetupFunctions()), ...(this.config.txPublicSetupAllowListExtend ?? [])];
1074
+ }
1075
+
262
1076
  /**
263
1077
  * Method to determine if the node is ready to accept transactions.
264
1078
  * @returns - Flag indicating the readiness for tx submission.
@@ -268,65 +1082,43 @@ export class AztecNodeService implements AztecNode, Traceable {
268
1082
  }
269
1083
 
270
1084
  public async getNodeInfo(): Promise<NodeInfo> {
271
- const [nodeVersion, protocolVersion, chainId, enr, contractAddresses, protocolContractAddresses] =
272
- await Promise.all([
273
- this.getNodeVersion(),
274
- this.getVersion(),
275
- this.getChainId(),
276
- this.getEncodedEnr(),
277
- this.getL1ContractAddresses(),
278
- this.getProtocolContractAddresses(),
279
- ]);
1085
+ const [nodeVersion, rollupVersion, chainId, enr, contractAddresses, protocolContractAddresses] = await Promise.all([
1086
+ this.getNodeVersion(),
1087
+ this.getVersion(),
1088
+ this.getChainId(),
1089
+ this.getEncodedEnr(),
1090
+ this.getL1ContractAddresses(),
1091
+ this.getProtocolContractAddresses(),
1092
+ ]);
280
1093
 
281
1094
  const nodeInfo: NodeInfo = {
282
1095
  nodeVersion,
283
1096
  l1ChainId: chainId,
284
- protocolVersion,
1097
+ rollupVersion,
285
1098
  enr,
286
1099
  l1ContractAddresses: contractAddresses,
287
1100
  protocolContractAddresses: protocolContractAddresses,
1101
+ realProofs: !!this.config.realProofs,
288
1102
  };
289
1103
 
290
1104
  return nodeInfo;
291
1105
  }
292
1106
 
293
- /**
294
- * Get a block specified by its number.
295
- * @param number - The block number being requested.
296
- * @returns The requested block.
297
- */
298
- public async getBlock(number: number): Promise<L2Block | undefined> {
299
- return await this.blockSource.getBlock(number);
300
- }
301
-
302
- /**
303
- * Method to request blocks. Will attempt to return all requested blocks but will return only those available.
304
- * @param from - The start of the range of blocks to return.
305
- * @param limit - The maximum number of blocks to obtain.
306
- * @returns The blocks requested.
307
- */
308
- public async getBlocks(from: number, limit: number): Promise<L2Block[]> {
309
- return (await this.blockSource.getBlocks(from, limit)) ?? [];
1107
+ public async getCurrentMinFees(): Promise<GasFees> {
1108
+ return await this.feeProvider.getCurrentMinFees();
310
1109
  }
311
1110
 
312
- /**
313
- * Method to fetch the current base fees.
314
- * @returns The current base fees.
315
- */
316
- public async getCurrentBaseFees(): Promise<GasFees> {
317
- return await this.globalVariableBuilder.getCurrentBaseFees();
1111
+ /** Returns predicted min fees for the current slot and next N slots. */
1112
+ public async getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]> {
1113
+ return await this.feeProvider.getPredictedMinFees(manaUsage);
318
1114
  }
319
1115
 
320
- /**
321
- * Method to fetch the current block number.
322
- * @returns The block number.
323
- */
324
- public async getBlockNumber(): Promise<number> {
325
- return await this.blockSource.getBlockNumber();
326
- }
1116
+ public async getMaxPriorityFees(): Promise<GasFees> {
1117
+ for await (const tx of this.p2pClient.iteratePendingTxs()) {
1118
+ return tx.getGasSettings().maxPriorityFeesPerGas;
1119
+ }
327
1120
 
328
- public async getProvenBlockNumber(): Promise<number> {
329
- return await this.blockSource.getProvenBlockNumber();
1121
+ return GasFees.from({ feePerDaGas: 0n, feePerL2Gas: 0n });
330
1122
  }
331
1123
 
332
1124
  /**
@@ -353,67 +1145,20 @@ export class AztecNodeService implements AztecNode, Traceable {
353
1145
  return Promise.resolve(this.l1ChainId);
354
1146
  }
355
1147
 
356
- public async getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
357
- const klazz = await this.contractDataSource.getContractClass(id);
358
-
359
- // TODO(#10007): Remove this check. This is needed only because we're manually registering
360
- // some contracts in the archiver so they are available to all nodes (see `registerCommonContracts`
361
- // in `archiver/src/factory.ts`), but we still want clients to send the registration tx in order
362
- // to emit the corresponding nullifier, which is now being checked. Note that this method
363
- // is only called by the PXE to check if a contract is publicly registered.
364
- if (klazz) {
365
- const classNullifier = await siloNullifier(AztecAddress.fromNumber(REGISTERER_CONTRACT_ADDRESS), id);
366
- const worldState = await this.#getWorldState('latest');
367
- const [index] = await worldState.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [classNullifier.toBuffer()]);
368
- this.log.debug(`Registration nullifier ${classNullifier} for contract class ${id} found at index ${index}`);
369
- if (index === undefined) {
370
- return undefined;
371
- }
372
- }
373
-
374
- return klazz;
1148
+ public getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
1149
+ return this.contractDataSource.getContractClass(id);
375
1150
  }
376
1151
 
377
1152
  public getContract(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
378
1153
  return this.contractDataSource.getContract(address);
379
1154
  }
380
1155
 
381
- /**
382
- * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
383
- * @param from - The block number from which to begin retrieving logs.
384
- * @param limit - The maximum number of blocks to retrieve logs from.
385
- * @returns An array of private logs from the specified range of blocks.
386
- */
387
- public getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
388
- return this.logsSource.getPrivateLogs(from, limit);
1156
+ public getPrivateLogsByTags(query: PrivateLogsQuery): Promise<LogResult[][]> {
1157
+ return this.logsSource.getPrivateLogsByTags(query);
389
1158
  }
390
1159
 
391
- /**
392
- * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
393
- * @param tags - The tags to filter the logs by.
394
- * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
395
- * that tag.
396
- */
397
- public getLogsByTags(tags: Fr[]): Promise<TxScopedL2Log[][]> {
398
- return this.logsSource.getLogsByTags(tags);
399
- }
400
-
401
- /**
402
- * Gets public logs based on the provided filter.
403
- * @param filter - The filter to apply to the logs.
404
- * @returns The requested logs.
405
- */
406
- getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
407
- return this.logsSource.getPublicLogs(filter);
408
- }
409
-
410
- /**
411
- * Gets contract class logs based on the provided filter.
412
- * @param filter - The filter to apply to the logs.
413
- * @returns The requested logs.
414
- */
415
- getContractClassLogs(filter: LogFilter): Promise<GetContractClassLogsResponse> {
416
- return this.logsSource.getContractClassLogs(filter);
1160
+ public getPublicLogsByTags(query: PublicLogsQuery): Promise<LogResult[][]> {
1161
+ return this.logsSource.getPublicLogsByTags(query);
417
1162
  }
418
1163
 
419
1164
  /**
@@ -421,43 +1166,109 @@ export class AztecNodeService implements AztecNode, Traceable {
421
1166
  * @param tx - The transaction to be submitted.
422
1167
  */
423
1168
  public async sendTx(tx: Tx) {
1169
+ await this.#sendTx(tx);
1170
+ }
1171
+
1172
+ async #sendTx(tx: Tx) {
424
1173
  const timer = new Timer();
425
- const txHash = (await tx.getTxHash()).toString();
1174
+ const txHash = tx.getTxHash().toString();
426
1175
 
427
1176
  const valid = await this.isValidTx(tx);
428
1177
  if (valid.result !== 'valid') {
429
1178
  const reason = valid.reason.join(', ');
430
1179
  this.metrics.receivedTx(timer.ms(), false);
431
- this.log.warn(`Invalid tx ${txHash}: ${reason}`, { txHash });
432
- // TODO(#10967): Throw when receiving an invalid tx instead of just returning
433
- // throw new Error(`Invalid tx: ${reason}`);
434
- return;
1180
+ this.log.warn(`Received invalid tx ${txHash}: ${reason}`, { txHash });
1181
+ throw new Error(`Invalid tx: ${reason}`);
435
1182
  }
436
1183
 
437
- await this.p2pClient!.sendTx(tx);
438
- this.metrics.receivedTx(timer.ms(), true);
439
- this.log.info(`Received tx ${txHash}`, { txHash });
1184
+ try {
1185
+ await this.p2pClient!.sendTx(tx);
1186
+ } catch (err) {
1187
+ this.metrics.receivedTx(timer.ms(), false);
1188
+ this.log.warn(`Mempool rejected tx ${txHash}: ${(err as Error).message}`, { txHash });
1189
+ throw err;
1190
+ }
1191
+ const duration = timer.ms();
1192
+ this.metrics.receivedTx(duration, true);
1193
+ this.log.info(`Received tx ${txHash} in ${duration}ms`, { txHash });
440
1194
  }
441
1195
 
442
- public async getTxReceipt(txHash: TxHash): Promise<TxReceipt> {
443
- let txReceipt = new TxReceipt(txHash, TxStatus.DROPPED, 'Tx dropped by P2P node.');
444
-
445
- // We first check if the tx is in pending (instead of first checking if it is mined) because if we first check
446
- // for mined and then for pending there could be a race condition where the tx is mined between the two checks
447
- // and we would incorrectly return a TxReceipt with status DROPPED
448
- if ((await this.p2pClient.getTxStatus(txHash)) === 'pending') {
449
- txReceipt = new TxReceipt(txHash, TxStatus.PENDING, '');
1196
+ public async getTxReceipt<TGetTxReceiptOptions extends GetTxReceiptOptions = {}>(
1197
+ txHash: TxHash,
1198
+ options?: TGetTxReceiptOptions,
1199
+ ): Promise<TxReceipt<TGetTxReceiptOptions>> {
1200
+ // Check the tx pool status first. If the tx is known to the pool (pending or mined), we'll use that
1201
+ // as a fallback if we don't find a mined tx effect in the archiver.
1202
+ const txPoolStatus = await this.p2pClient.getTxStatus(txHash);
1203
+ const isKnownToPool = txPoolStatus === 'pending' || txPoolStatus === 'mined';
1204
+
1205
+ // Then get the raw tx effect from the archiver, which tracks every tx in a mined block.
1206
+ const indexed = await this.blockSource.getTxEffect(txHash);
1207
+
1208
+ let receipt: TxReceipt;
1209
+ if (indexed) {
1210
+ receipt = await this.#assembleMinedReceipt(indexed, options);
1211
+ } else if (isKnownToPool) {
1212
+ // If the tx is in the pool but not in the archiver, it's pending.
1213
+ // This handles race conditions between archiver and p2p, where the archiver
1214
+ // has pruned the block in which a tx was mined, but p2p has not caught up yet.
1215
+ let tx: Tx | undefined;
1216
+ if (options?.includePendingTx) {
1217
+ // The tx may have left the pool since we checked its status (mined or dropped); in that case we
1218
+ // leave `tx` unset and still return a pending receipt.
1219
+ const pendingTx = await this.p2pClient.getTxByHashFromPool(txHash);
1220
+ tx = pendingTx && !options.includeProof ? pendingTx.withoutProof() : pendingTx;
1221
+ }
1222
+ receipt = new PendingTxReceipt(txHash, tx);
1223
+ } else {
1224
+ // Otherwise, if we don't know the tx, we consider it dropped.
1225
+ receipt = new DroppedTxReceipt(txHash, 'Tx dropped by P2P node');
450
1226
  }
451
1227
 
452
- const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
453
- if (settledTxReceipt) {
454
- txReceipt = settledTxReceipt;
455
- }
1228
+ this.debugLogStore.decorateReceiptWithLogs(txHash.toString(), receipt);
1229
+
1230
+ return receipt;
1231
+ }
1232
+
1233
+ /**
1234
+ * Assembles a {@link MinedTxReceipt} from a raw {@link IndexedTxEffect}, deriving the finalization status from the
1235
+ * cached L2 tips and the epoch from the block's slot number.
1236
+ */
1237
+ async #assembleMinedReceipt(indexed: IndexedTxEffect, options?: GetTxReceiptOptions): Promise<MinedTxReceipt> {
1238
+ const blockNumber = indexed.l2BlockNumber;
1239
+ const [tips, l1Constants] = await Promise.all([this.blockSource.getL2Tips(), this.blockSource.getL1Constants()]);
1240
+
1241
+ const status = this.#deriveMinedStatus(blockNumber, tips);
1242
+ const epochNumber = getEpochAtSlot(indexed.slotNumber, l1Constants);
1243
+
1244
+ return new MinedTxReceipt(
1245
+ indexed.data.txHash,
1246
+ status,
1247
+ MinedTxReceipt.executionResultFromRevertCode(indexed.data.revertCode),
1248
+ indexed.data.transactionFee.toBigInt(),
1249
+ indexed.l2BlockHash,
1250
+ blockNumber,
1251
+ indexed.slotNumber,
1252
+ indexed.txIndexInBlock,
1253
+ epochNumber,
1254
+ options?.includeTxEffect ? indexed.data : undefined,
1255
+ /*debugLogs=*/ undefined,
1256
+ );
1257
+ }
456
1258
 
457
- return txReceipt;
1259
+ #deriveMinedStatus(blockNumber: BlockNumber, tips: L2Tips): MinedTxStatus {
1260
+ if (blockNumber <= tips.finalized.block.number) {
1261
+ return TxStatus.FINALIZED;
1262
+ } else if (blockNumber <= tips.proven.block.number) {
1263
+ return TxStatus.PROVEN;
1264
+ } else if (blockNumber <= tips.checkpointed.block.number) {
1265
+ return TxStatus.CHECKPOINTED;
1266
+ } else {
1267
+ return TxStatus.PROPOSED;
1268
+ }
458
1269
  }
459
1270
 
460
- public getTxEffect(txHash: TxHash): Promise<InBlock<TxEffect> | undefined> {
1271
+ public getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
461
1272
  return this.blockSource.getTxEffect(txHash);
462
1273
  }
463
1274
 
@@ -465,329 +1276,252 @@ export class AztecNodeService implements AztecNode, Traceable {
465
1276
  * Method to stop the aztec node.
466
1277
  */
467
1278
  public async stop() {
468
- this.log.info(`Stopping`);
469
- await this.sequencer?.stop();
470
- await this.p2pClient.stop();
471
- await this.worldStateSynchronizer.stop();
1279
+ this.log.info(`Stopping Aztec Node`);
1280
+ await this.stopStartedWatchers();
1281
+ await tryStop(this.slasherClient);
1282
+ await Promise.all([tryStop(this.peerProofVerifier), tryStop(this.rpcProofVerifier)]);
1283
+ await tryStop(this.sequencer);
1284
+ await tryStop(this.automineSequencer);
1285
+ await tryStop(this.proverNode);
1286
+ await tryStop(this.p2pClient);
1287
+ await tryStop(this.worldStateSynchronizer);
472
1288
  await tryStop(this.blockSource);
473
- await this.telemetry.stop();
474
- this.log.info(`Stopped`);
1289
+ await tryStop(this.blobClient);
1290
+ await tryStop(this.telemetry);
1291
+ this.log.info(`Stopped Aztec Node`);
1292
+ }
1293
+
1294
+ /**
1295
+ * Returns the blob client used by this node.
1296
+ * @internal - Exposed for testing purposes only.
1297
+ */
1298
+ public getBlobClient(): BlobClientInterface | undefined {
1299
+ return this.blobClient;
475
1300
  }
476
1301
 
477
1302
  /**
478
1303
  * Method to retrieve pending txs.
1304
+ * @param limit - The number of items to returns
1305
+ * @param after - The last known pending tx. Used for pagination
479
1306
  * @returns - The pending txs.
480
1307
  */
481
- public getPendingTxs() {
482
- return this.p2pClient!.getPendingTxs();
1308
+ public getPendingTxs(limit?: number, after?: TxHash): Promise<Tx[]> {
1309
+ return this.p2pClient!.getPendingTxs(limit, after);
483
1310
  }
484
1311
 
485
- public async getPendingTxCount() {
486
- const pendingTxs = await this.getPendingTxs();
487
- return pendingTxs.length;
1312
+ public getPendingTxCount(): Promise<number> {
1313
+ return this.p2pClient!.getPendingTxCount();
488
1314
  }
489
1315
 
490
1316
  /**
491
- * Method to retrieve a single tx from the mempool or unfinalised chain.
1317
+ * Method to retrieve a single tx from the mempool or unfinalized chain.
492
1318
  * @param txHash - The transaction hash to return.
493
1319
  * @returns - The tx if it exists.
494
1320
  */
495
- public getTxByHash(txHash: TxHash) {
1321
+ public getTxByHash(txHash: TxHash): Promise<Tx | undefined> {
496
1322
  return Promise.resolve(this.p2pClient!.getTxByHashFromPool(txHash));
497
1323
  }
498
1324
 
499
1325
  /**
500
- * Method to retrieve txs from the mempool or unfinalised chain.
1326
+ * Method to retrieve txs from the mempool or unfinalized chain.
501
1327
  * @param txHash - The transaction hash to return.
502
1328
  * @returns - The txs if it exists.
503
1329
  */
504
- public async getTxsByHash(txHashes: TxHash[]) {
1330
+ public async getTxsByHash(txHashes: TxHash[]): Promise<Tx[]> {
505
1331
  return compactArray(await Promise.all(txHashes.map(txHash => this.getTxByHash(txHash))));
506
1332
  }
507
1333
 
508
- /**
509
- * Find the indexes of the given leaves in the given tree.
510
- * @param blockNumber - The block number at which to get the data or 'latest' for latest data
511
- * @param treeId - The tree to search in.
512
- * @param leafValue - The values to search for
513
- * @returns The indexes of the given leaves in the given tree or undefined if not found.
514
- */
515
1334
  public async findLeavesIndexes(
516
- blockNumber: L2BlockNumber,
1335
+ referenceBlock: BlockParameter,
517
1336
  treeId: MerkleTreeId,
518
1337
  leafValues: Fr[],
519
- ): Promise<(bigint | undefined)[]> {
520
- const committedDb = await this.#getWorldState(blockNumber);
521
- return await committedDb.findLeafIndices(
1338
+ ): Promise<(DataInBlock<bigint> | undefined)[]> {
1339
+ const committedDb = await this.getWorldState(referenceBlock);
1340
+ const maybeIndices = await committedDb.findLeafIndices(
522
1341
  treeId,
523
1342
  leafValues.map(x => x.toBuffer()),
524
1343
  );
525
- }
1344
+ // Filter out undefined values to query block numbers only for found leaves
1345
+ const definedIndices = maybeIndices.filter(x => x !== undefined);
1346
+
1347
+ // Now we find the block numbers for the defined indices
1348
+ const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, definedIndices);
1349
+
1350
+ // Build a map from leaf index to block number
1351
+ const indexToBlockNumber = new Map<bigint, BlockNumber>();
1352
+ for (let i = 0; i < definedIndices.length; i++) {
1353
+ const blockNumber = blockNumbers[i];
1354
+ if (blockNumber === undefined) {
1355
+ throw new Error(
1356
+ `Block number is undefined for leaf index ${definedIndices[i]} in tree ${MerkleTreeId[treeId]}`,
1357
+ );
1358
+ }
1359
+ indexToBlockNumber.set(definedIndices[i], blockNumber);
1360
+ }
526
1361
 
527
- /**
528
- * Find the block numbers of the given leaf indices in the given tree.
529
- * @param blockNumber - The block number at which to get the data or 'latest' for latest data
530
- * @param treeId - The tree to search in.
531
- * @param leafIndices - The values to search for
532
- * @returns The indexes of the given leaves in the given tree or undefined if not found.
533
- */
534
- public async findBlockNumbersForIndexes(
535
- blockNumber: L2BlockNumber,
536
- treeId: MerkleTreeId,
537
- leafIndices: bigint[],
538
- ): Promise<(bigint | undefined)[]> {
539
- const committedDb = await this.#getWorldState(blockNumber);
540
- return await committedDb.getBlockNumbersForLeafIndices(treeId, leafIndices);
541
- }
1362
+ // Get unique block numbers in order to optimize num calls to getLeafValue function.
1363
+ const uniqueBlockNumbers = [...new Set(indexToBlockNumber.values())];
542
1364
 
543
- public async findNullifiersIndexesWithBlock(
544
- blockNumber: L2BlockNumber,
545
- nullifiers: Fr[],
546
- ): Promise<(InBlock<bigint> | undefined)[]> {
547
- if (blockNumber === 'latest') {
548
- blockNumber = await this.getBlockNumber();
1365
+ // Now we obtain the block hashes from the archive tree (block number = leaf index in archive tree).
1366
+ const blockHashes = await Promise.all(
1367
+ uniqueBlockNumbers.map(blockNumber => {
1368
+ return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
1369
+ }),
1370
+ );
1371
+
1372
+ // Build a map from block number to block hash
1373
+ const blockNumberToHash = new Map<BlockNumber, BlockHash>();
1374
+ for (let i = 0; i < uniqueBlockNumbers.length; i++) {
1375
+ const blockHash = blockHashes[i];
1376
+ if (blockHash === undefined) {
1377
+ throw new Error(`Block hash is undefined for block number ${uniqueBlockNumbers[i]}`);
1378
+ }
1379
+ blockNumberToHash.set(uniqueBlockNumbers[i], blockHash);
549
1380
  }
550
- return this.nullifierSource.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
1381
+
1382
+ // Create DataInBlock objects by combining indices, blockNumbers and blockHashes and return them.
1383
+ return maybeIndices.map(index => {
1384
+ if (index === undefined) {
1385
+ return undefined;
1386
+ }
1387
+ const blockNumber = indexToBlockNumber.get(index);
1388
+ if (blockNumber === undefined) {
1389
+ throw new Error(`Block number not found for leaf index ${index} in tree ${MerkleTreeId[treeId]}`);
1390
+ }
1391
+ const l2BlockHash = blockNumberToHash.get(blockNumber);
1392
+ if (l2BlockHash === undefined) {
1393
+ throw new Error(`Block hash not found for block number ${blockNumber}`);
1394
+ }
1395
+ return {
1396
+ l2BlockNumber: blockNumber,
1397
+ l2BlockHash,
1398
+ data: index,
1399
+ };
1400
+ });
551
1401
  }
552
1402
 
553
- /**
554
- * Returns a sibling path for the given index in the nullifier tree.
555
- * @param blockNumber - The block number at which to get the data.
556
- * @param leafIndex - The index of the leaf for which the sibling path is required.
557
- * @returns The sibling path for the leaf index.
558
- */
559
- public async getNullifierSiblingPath(
560
- blockNumber: L2BlockNumber,
561
- leafIndex: bigint,
562
- ): Promise<SiblingPath<typeof NULLIFIER_TREE_HEIGHT>> {
563
- const committedDb = await this.#getWorldState(blockNumber);
564
- return committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, leafIndex);
1403
+ public async getBlockHashMembershipWitness(
1404
+ referenceBlock: BlockParameter,
1405
+ blockHash: BlockHash,
1406
+ ): Promise<MembershipWitness<typeof ARCHIVE_HEIGHT> | undefined> {
1407
+ // The Noir circuit checks the archive membership proof against `anchor_block_header.last_archive.root`,
1408
+ // which is the archive tree root BEFORE the anchor block was added (i.e. the state after block N-1).
1409
+ // So we need the world state at block N-1, not block N, to produce a sibling path matching that root.
1410
+ const referenceBlockNumber = await this.resolveBlockNumber(referenceBlock);
1411
+ if (referenceBlockNumber === BlockNumber.ZERO) {
1412
+ // Block 0 (the initial block) has an empty archive, so no membership witness can exist.
1413
+ return undefined;
1414
+ }
1415
+ const committedDb = await this.getWorldState(BlockNumber(referenceBlockNumber - 1));
1416
+ const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.ARCHIVE>(MerkleTreeId.ARCHIVE, [blockHash]);
1417
+ return pathAndIndex === undefined
1418
+ ? undefined
1419
+ : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
565
1420
  }
566
1421
 
567
- /**
568
- * Returns a sibling path for the given index in the data tree.
569
- * @param blockNumber - The block number at which to get the data.
570
- * @param leafIndex - The index of the leaf for which the sibling path is required.
571
- * @returns The sibling path for the leaf index.
572
- */
573
- public async getNoteHashSiblingPath(
574
- blockNumber: L2BlockNumber,
575
- leafIndex: bigint,
576
- ): Promise<SiblingPath<typeof NOTE_HASH_TREE_HEIGHT>> {
577
- const committedDb = await this.#getWorldState(blockNumber);
578
- return committedDb.getSiblingPath(MerkleTreeId.NOTE_HASH_TREE, leafIndex);
1422
+ public async getNoteHashMembershipWitness(
1423
+ referenceBlock: BlockParameter,
1424
+ noteHash: Fr,
1425
+ ): Promise<MembershipWitness<typeof NOTE_HASH_TREE_HEIGHT> | undefined> {
1426
+ const committedDb = await this.getWorldState(referenceBlock);
1427
+ const [pathAndIndex] = await committedDb.findSiblingPaths<MerkleTreeId.NOTE_HASH_TREE>(
1428
+ MerkleTreeId.NOTE_HASH_TREE,
1429
+ [noteHash],
1430
+ );
1431
+ return pathAndIndex === undefined
1432
+ ? undefined
1433
+ : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
579
1434
  }
580
1435
 
581
- /**
582
- * Returns the index and a sibling path for a leaf in the committed l1 to l2 data tree.
583
- * @param blockNumber - The block number at which to get the data.
584
- * @param l1ToL2Message - The l1ToL2Message to get the index / sibling path for.
585
- * @returns A tuple of the index and the sibling path of the L1ToL2Message (undefined if not found).
586
- */
587
1436
  public async getL1ToL2MessageMembershipWitness(
588
- blockNumber: L2BlockNumber,
1437
+ referenceBlock: BlockParameter,
589
1438
  l1ToL2Message: Fr,
590
1439
  ): Promise<[bigint, SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>] | undefined> {
591
- const index = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
592
- if (index === undefined) {
1440
+ const db = await this.getWorldState(referenceBlock);
1441
+ const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [l1ToL2Message]);
1442
+ if (!witness) {
593
1443
  return undefined;
594
1444
  }
595
- const committedDb = await this.#getWorldState(blockNumber);
596
- const siblingPath = await committedDb.getSiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>(
597
- MerkleTreeId.L1_TO_L2_MESSAGE_TREE,
598
- index,
599
- );
600
- return [index, siblingPath];
601
- }
602
1445
 
603
- /**
604
- * Returns whether an L1 to L2 message is synced by archiver and if it's ready to be included in a block.
605
- * @param l1ToL2Message - The L1 to L2 message to check.
606
- * @returns Whether the message is synced and ready to be included in a block.
607
- */
608
- public async isL1ToL2MessageSynced(l1ToL2Message: Fr): Promise<boolean> {
609
- return (await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message)) !== undefined;
1446
+ // REFACTOR: Return a MembershipWitness object
1447
+ return [witness.index, witness.path];
610
1448
  }
611
1449
 
612
- /**
613
- * Returns the index of a l2ToL1Message in a ephemeral l2 to l1 data tree as well as its sibling path.
614
- * @remarks This tree is considered ephemeral because it is created on-demand by: taking all the l2ToL1 messages
615
- * in a single block, and then using them to make a variable depth append-only tree with these messages as leaves.
616
- * The tree is discarded immediately after calculating what we need from it.
617
- * TODO: Handle the case where two messages in the same tx have the same hash.
618
- * @param blockNumber - The block number at which to get the data.
619
- * @param l2ToL1Message - The l2ToL1Message get the index / sibling path for.
620
- * @returns A tuple of the index and the sibling path of the L2ToL1Message.
621
- */
622
- public async getL2ToL1MessageMembershipWitness(
623
- blockNumber: L2BlockNumber,
624
- l2ToL1Message: Fr,
625
- ): Promise<[bigint, SiblingPath<number>]> {
626
- const block = await this.blockSource.getBlock(blockNumber === 'latest' ? await this.getBlockNumber() : blockNumber);
627
-
628
- if (block === undefined) {
629
- throw new Error('Block is not defined');
630
- }
631
-
632
- const l2ToL1Messages = block.body.txEffects.map(txEffect => txEffect.l2ToL1Msgs);
633
-
634
- // Find index of message
635
- let indexOfMsgInSubtree = -1;
636
- const indexOfMsgTx = l2ToL1Messages.findIndex(msgs => {
637
- const idx = msgs.findIndex(msg => msg.equals(l2ToL1Message));
638
- indexOfMsgInSubtree = Math.max(indexOfMsgInSubtree, idx);
639
- return idx !== -1;
640
- });
641
-
642
- if (indexOfMsgTx === -1) {
643
- throw new Error('The L2ToL1Message you are trying to prove inclusion of does not exist');
644
- }
645
-
646
- const tempStores: AztecKVStore[] = [];
647
-
648
- // Construct message subtrees
649
- const l2toL1Subtrees = await Promise.all(
650
- l2ToL1Messages.map(async (msgs, i) => {
651
- const store = openTmpStore(true);
652
- tempStores.push(store);
653
- const treeHeight = msgs.length <= 1 ? 1 : Math.ceil(Math.log2(msgs.length));
654
- const tree = new StandardTree(store, new SHA256Trunc(), `temp_msgs_subtrees_${i}`, treeHeight, 0n, Fr);
655
- await tree.appendLeaves(msgs);
656
- return tree;
657
- }),
658
- );
659
-
660
- // path of the input msg from leaf -> first out hash calculated in base rolllup
661
- const subtreePathOfL2ToL1Message = await l2toL1Subtrees[indexOfMsgTx].getSiblingPath(
662
- BigInt(indexOfMsgInSubtree),
663
- true,
664
- );
665
-
666
- const numTxs = block.body.txEffects.length;
667
- if (numTxs === 1) {
668
- return [BigInt(indexOfMsgInSubtree), subtreePathOfL2ToL1Message];
669
- }
670
-
671
- const l2toL1SubtreeRoots = l2toL1Subtrees.map(t => Fr.fromBuffer(t.getRoot(true)));
672
- const maxTreeHeight = Math.ceil(Math.log2(l2toL1SubtreeRoots.length));
673
- // The root of this tree is the out_hash calculated in Noir => we truncate to match Noir's SHA
674
- const outHashTree = new UnbalancedTree(new SHA256Trunc(), 'temp_outhash_sibling_path', maxTreeHeight, Fr);
675
- await outHashTree.appendLeaves(l2toL1SubtreeRoots);
676
-
677
- const pathOfTxInOutHashTree = await outHashTree.getSiblingPath(l2toL1SubtreeRoots[indexOfMsgTx].toBigInt());
678
- // Append subtree path to out hash tree path
679
- const mergedPath = subtreePathOfL2ToL1Message.toBufferArray().concat(pathOfTxInOutHashTree.toBufferArray());
680
- // Append binary index of subtree path to binary index of out hash tree path
681
- const mergedIndex = parseInt(
682
- indexOfMsgTx
683
- .toString(2)
684
- .concat(indexOfMsgInSubtree.toString(2).padStart(l2toL1Subtrees[indexOfMsgTx].getDepth(), '0')),
685
- 2,
686
- );
687
-
688
- // clear the tmp stores
689
- await Promise.all(tempStores.map(store => store.delete()));
690
-
691
- return [BigInt(mergedIndex), new SiblingPath(mergedPath.length, mergedPath)];
1450
+ public async getL1ToL2MessageCheckpoint(l1ToL2Message: Fr): Promise<CheckpointNumber | undefined> {
1451
+ const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
1452
+ return messageIndex !== undefined ? InboxLeaf.checkpointNumberFromIndex(messageIndex) : undefined;
692
1453
  }
693
1454
 
694
1455
  /**
695
- * Returns a sibling path for a leaf in the committed blocks tree.
696
- * @param blockNumber - The block number at which to get the data.
697
- * @param leafIndex - Index of the leaf in the tree.
698
- * @returns The sibling path.
1456
+ * Returns all the L2 to L1 messages in an epoch.
1457
+ *
1458
+ * @deprecated Use {@link getL2ToL1MembershipWitness} to get an L2-to-L1 message witness directly.
1459
+ *
1460
+ * @param epoch - The epoch at which to get the data.
1461
+ * @returns The L2 to L1 messages (empty array if the epoch is not found).
699
1462
  */
700
- public async getArchiveSiblingPath(
701
- blockNumber: L2BlockNumber,
702
- leafIndex: bigint,
703
- ): Promise<SiblingPath<typeof ARCHIVE_HEIGHT>> {
704
- const committedDb = await this.#getWorldState(blockNumber);
705
- return committedDb.getSiblingPath(MerkleTreeId.ARCHIVE, leafIndex);
1463
+ public async getL2ToL1Messages(epoch: EpochNumber): Promise<Fr[][][][]> {
1464
+ const blocks = await this.blockSource.getBlocks({ epoch, onlyCheckpointed: true });
1465
+ const blocksInCheckpoints = chunkBy(blocks, block => block.header.globalVariables.slotNumber);
1466
+ return blocksInCheckpoints.map(slotBlocks =>
1467
+ slotBlocks.map(block => block.body.txEffects.map(txEffect => txEffect.l2ToL1Msgs)),
1468
+ );
706
1469
  }
707
1470
 
708
1471
  /**
709
- * Returns a sibling path for a leaf in the committed public data tree.
710
- * @param blockNumber - The block number at which to get the data.
711
- * @param leafIndex - Index of the leaf in the tree.
712
- * @returns The sibling path.
1472
+ * Returns the L2-to-L1 membership witness for a message in `txHash`. Passthrough to the
1473
+ * archiver's locally-cached resolver — see {@link Archiver.getL2ToL1MembershipWitness}.
713
1474
  */
714
- public async getPublicDataSiblingPath(
715
- blockNumber: L2BlockNumber,
716
- leafIndex: bigint,
717
- ): Promise<SiblingPath<typeof PUBLIC_DATA_TREE_HEIGHT>> {
718
- const committedDb = await this.#getWorldState(blockNumber);
719
- return committedDb.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex);
1475
+ public getL2ToL1MembershipWitness(
1476
+ txHash: TxHash,
1477
+ message: Fr,
1478
+ messageIndexInTx?: number,
1479
+ ): Promise<L2ToL1MembershipWitness | undefined> {
1480
+ return this.blockSource.getL2ToL1MembershipWitness(txHash, message, messageIndexInTx);
720
1481
  }
721
1482
 
722
- /**
723
- * Returns a nullifier membership witness for a given nullifier at a given block.
724
- * @param blockNumber - The block number at which to get the index.
725
- * @param nullifier - Nullifier we try to find witness for.
726
- * @returns The nullifier membership witness (if found).
727
- */
728
1483
  public async getNullifierMembershipWitness(
729
- blockNumber: L2BlockNumber,
1484
+ referenceBlock: BlockParameter,
730
1485
  nullifier: Fr,
731
1486
  ): Promise<NullifierMembershipWitness | undefined> {
732
- const db = await this.#getWorldState(blockNumber);
733
- const index = (await db.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]))[0];
734
- if (!index) {
1487
+ const db = await this.getWorldState(referenceBlock);
1488
+ const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]);
1489
+ if (!witness) {
735
1490
  return undefined;
736
1491
  }
737
1492
 
738
- const leafPreimagePromise = db.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
739
- const siblingPathPromise = db.getSiblingPath<typeof NULLIFIER_TREE_HEIGHT>(
740
- MerkleTreeId.NULLIFIER_TREE,
741
- BigInt(index),
742
- );
743
-
744
- const [leafPreimage, siblingPath] = await Promise.all([leafPreimagePromise, siblingPathPromise]);
745
-
1493
+ const { index, path } = witness;
1494
+ const leafPreimage = await db.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
746
1495
  if (!leafPreimage) {
747
1496
  return undefined;
748
1497
  }
749
1498
 
750
- return new NullifierMembershipWitness(BigInt(index), leafPreimage as NullifierLeafPreimage, siblingPath);
1499
+ return new NullifierMembershipWitness(index, leafPreimage as NullifierLeafPreimage, path);
751
1500
  }
752
1501
 
753
- /**
754
- * Returns a low nullifier membership witness for a given nullifier at a given block.
755
- * @param blockNumber - The block number at which to get the index.
756
- * @param nullifier - Nullifier we try to find the low nullifier witness for.
757
- * @returns The low nullifier membership witness (if found).
758
- * @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
759
- * list structure" of leaves and proving that a lower nullifier is pointing to a bigger next value than the nullifier
760
- * we are trying to prove non-inclusion for.
761
- *
762
- * Note: This function returns the membership witness of the nullifier itself and not the low nullifier when
763
- * the nullifier already exists in the tree. This is because the `getPreviousValueIndex` function returns the
764
- * index of the nullifier itself when it already exists in the tree.
765
- * TODO: This is a confusing behavior and we should eventually address that.
766
- */
767
1502
  public async getLowNullifierMembershipWitness(
768
- blockNumber: L2BlockNumber,
1503
+ referenceBlock: BlockParameter,
769
1504
  nullifier: Fr,
770
1505
  ): Promise<NullifierMembershipWitness | undefined> {
771
- const committedDb = await this.#getWorldState(blockNumber);
1506
+ const committedDb = await this.getWorldState(referenceBlock);
772
1507
  const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
773
1508
  if (!findResult) {
774
1509
  return undefined;
775
1510
  }
776
1511
  const { index, alreadyPresent } = findResult;
777
1512
  if (alreadyPresent) {
778
- this.log.warn(`Nullifier ${nullifier.toBigInt()} already exists in the tree`);
1513
+ throw new Error(
1514
+ `Cannot prove nullifier non-inclusion: nullifier ${nullifier.toBigInt()} already exists in the tree`,
1515
+ );
779
1516
  }
780
1517
  const preimageData = (await committedDb.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index))!;
781
1518
 
782
- const siblingPath = await committedDb.getSiblingPath<typeof NULLIFIER_TREE_HEIGHT>(
783
- MerkleTreeId.NULLIFIER_TREE,
784
- BigInt(index),
785
- );
1519
+ const siblingPath = await committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, BigInt(index));
786
1520
  return new NullifierMembershipWitness(BigInt(index), preimageData as NullifierLeafPreimage, siblingPath);
787
1521
  }
788
1522
 
789
- async getPublicDataTreeWitness(blockNumber: L2BlockNumber, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
790
- const committedDb = await this.#getWorldState(blockNumber);
1523
+ async getPublicDataWitness(referenceBlock: BlockParameter, leafSlot: Fr): Promise<PublicDataWitness | undefined> {
1524
+ const committedDb = await this.getWorldState(referenceBlock);
791
1525
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
792
1526
  if (!lowLeafResult) {
793
1527
  return undefined;
@@ -796,27 +1530,13 @@ export class AztecNodeService implements AztecNode, Traceable {
796
1530
  MerkleTreeId.PUBLIC_DATA_TREE,
797
1531
  lowLeafResult.index,
798
1532
  )) as PublicDataTreeLeafPreimage;
799
- const path = await committedDb.getSiblingPath<typeof PUBLIC_DATA_TREE_HEIGHT>(
800
- MerkleTreeId.PUBLIC_DATA_TREE,
801
- lowLeafResult.index,
802
- );
1533
+ const path = await committedDb.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index);
803
1534
  return new PublicDataWitness(lowLeafResult.index, preimage, path);
804
1535
  }
805
1536
  }
806
1537
 
807
- /**
808
- * Gets the storage value at the given contract storage slot.
809
- *
810
- * @remarks The storage slot here refers to the slot as it is defined in Noir not the index in the merkle tree.
811
- * Aztec's version of `eth_getStorageAt`.
812
- *
813
- * @param contract - Address of the contract to query.
814
- * @param slot - Slot to query.
815
- * @param blockNumber - The block number at which to get the data or 'latest'.
816
- * @returns Storage value at the given contract slot.
817
- */
818
- public async getPublicStorageAt(blockNumber: L2BlockNumber, contract: AztecAddress, slot: Fr): Promise<Fr> {
819
- const committedDb = await this.#getWorldState(blockNumber);
1538
+ public async getPublicStorageAt(referenceBlock: BlockParameter, contract: AztecAddress, slot: Fr): Promise<Fr> {
1539
+ const committedDb = await this.getWorldState(referenceBlock);
820
1540
  const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
821
1541
 
822
1542
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
@@ -827,45 +1547,58 @@ export class AztecNodeService implements AztecNode, Traceable {
827
1547
  MerkleTreeId.PUBLIC_DATA_TREE,
828
1548
  lowLeafResult.index,
829
1549
  )) as PublicDataTreeLeafPreimage;
830
- return preimage.value;
831
- }
832
-
833
- /**
834
- * Returns the currently committed block header, or the initial header if no blocks have been produced.
835
- * @returns The current committed block header.
836
- */
837
- public async getBlockHeader(blockNumber: L2BlockNumber = 'latest'): Promise<BlockHeader | undefined> {
838
- return blockNumber === 0 || (blockNumber === 'latest' && (await this.blockSource.getBlockNumber()) === 0)
839
- ? this.worldStateSynchronizer.getCommitted().getInitialHeader()
840
- : this.blockSource.getBlockHeader(blockNumber);
1550
+ return preimage.leaf.value;
841
1551
  }
842
1552
 
843
1553
  /**
844
1554
  * Simulates the public part of a transaction with the current state.
845
1555
  * @param tx - The transaction to simulate.
1556
+ * @param skipFeeEnforcement - If true, fee enforcement is skipped.
1557
+ * @param overrides - Optional pre-simulation overrides applied to the ephemeral fork and contract DB.
846
1558
  **/
847
- @trackSpan('AztecNodeService.simulatePublicCalls', async (tx: Tx) => ({
848
- [Attributes.TX_HASH]: (await tx.getTxHash()).toString(),
1559
+ @trackSpan('AztecNodeService.simulatePublicCalls', (tx: Tx) => ({
1560
+ [Attributes.TX_HASH]: tx.getTxHash().toString(),
849
1561
  }))
850
- public async simulatePublicCalls(tx: Tx, skipFeeEnforcement = false): Promise<PublicSimulationOutput> {
851
- const txHash = await tx.getTxHash();
852
- const blockNumber = (await this.blockSource.getBlockNumber()) + 1;
1562
+ public async simulatePublicCalls(
1563
+ tx: Tx,
1564
+ skipFeeEnforcement = false,
1565
+ overrides?: SimulationOverrides,
1566
+ ): Promise<PublicSimulationOutput> {
1567
+ // Check total gas limit for simulation
1568
+ const gasSettings = tx.data.constants.txContext.gasSettings;
1569
+ const txGasLimit = gasSettings.gasLimits.l2Gas;
1570
+ const teardownGasLimit = gasSettings.teardownGasLimits.l2Gas;
1571
+ if (txGasLimit + teardownGasLimit > this.config.rpcSimulatePublicMaxGasLimit) {
1572
+ throw new BadRequestError(
1573
+ `Transaction total gas limit ${
1574
+ txGasLimit + teardownGasLimit
1575
+ } (${txGasLimit} + ${teardownGasLimit}) exceeds maximum gas limit ${
1576
+ this.config.rpcSimulatePublicMaxGasLimit
1577
+ } for simulation`,
1578
+ );
1579
+ }
1580
+
1581
+ const txHash = tx.getTxHash();
1582
+ const l2Tips = await this.blockSource.getL2Tips();
1583
+ const latestBlockNumber = l2Tips.proposed.number;
1584
+ const blockNumber = BlockNumber.add(latestBlockNumber, 1);
853
1585
 
854
1586
  // If sequencer is not initialized, we just set these values to zero for simulation.
855
- const coinbase = this.sequencer?.coinbase || EthAddress.ZERO;
856
- const feeRecipient = this.sequencer?.feeRecipient || AztecAddress.ZERO;
1587
+ const coinbase = EthAddress.ZERO;
1588
+ const feeRecipient = AztecAddress.ZERO;
857
1589
 
858
1590
  const newGlobalVariables = await this.globalVariableBuilder.buildGlobalVariables(
859
- new Fr(blockNumber),
1591
+ blockNumber,
860
1592
  coinbase,
861
1593
  feeRecipient,
862
1594
  );
1595
+
863
1596
  const publicProcessorFactory = new PublicProcessorFactory(
864
1597
  this.contractDataSource,
865
1598
  new DateProvider(),
866
1599
  this.telemetry,
1600
+ this.log.getBindings(),
867
1601
  );
868
- const fork = await this.worldStateSynchronizer.fork();
869
1602
 
870
1603
  this.log.verbose(`Simulating public calls for tx ${txHash}`, {
871
1604
  globalVariables: newGlobalVariables.toInspect(),
@@ -873,54 +1606,155 @@ export class AztecNodeService implements AztecNode, Traceable {
873
1606
  blockNumber,
874
1607
  });
875
1608
 
876
- try {
877
- const processor = publicProcessorFactory.create(fork, newGlobalVariables, skipFeeEnforcement);
878
-
879
- // REFACTOR: Consider merging ProcessReturnValues into ProcessedTx
880
- const [processedTxs, failedTxs, returns] = await processor.process([tx]);
881
- // REFACTOR: Consider returning the error rather than throwing
882
- if (failedTxs.length) {
883
- this.log.warn(`Simulated tx ${txHash} fails: ${failedTxs[0].error}`, { txHash });
884
- throw failedTxs[0].error;
885
- }
886
-
887
- const [processedTx] = processedTxs;
888
- return new PublicSimulationOutput(
889
- processedTx.revertReason,
890
- processedTx.constants,
891
- processedTx.txEffect,
892
- returns,
893
- processedTx.gasUsed,
1609
+ // Ensure world-state has caught up with the latest block we loaded from the archiver
1610
+ await this.worldStateSynchronizer.syncImmediate(latestBlockNumber);
1611
+
1612
+ // If we detect the next block would start a new checkpoint, then insert L1-to-L2 messages into
1613
+ // the world state tree so simulation can take them into account. We detect if the next block would
1614
+ // start a new checkpoint by checking if the proposed checkpoint's block number matches the latest block number,
1615
+ // which means the next block would be the first block of the next checkpoint.
1616
+ const targetCheckpoint = CheckpointNumber(
1617
+ (l2Tips.proposedCheckpoint.checkpoint.number ?? CheckpointNumber.ZERO) + 1,
1618
+ );
1619
+ const nextCheckpointMessages: Fr[] | undefined =
1620
+ l2Tips.proposedCheckpoint.block.number === l2Tips.proposed.number
1621
+ ? await this.l1ToL2MessageSource.getL1ToL2Messages(targetCheckpoint).catch(err => {
1622
+ if (isErrorClass(err, L1ToL2MessagesNotReadyError)) {
1623
+ this.log.warn(
1624
+ `L1-to-L2 messages for checkpoint ${targetCheckpoint} are not ready yet (simulating without them)`,
1625
+ );
1626
+ } else {
1627
+ this.log.error(
1628
+ `Failed to get L1-to-L2 messages for checkpoint ${targetCheckpoint} (simulating without them)`,
1629
+ err,
1630
+ );
1631
+ }
1632
+ return undefined;
1633
+ })
1634
+ : undefined;
1635
+
1636
+ // Request a new fork of the world state at the latest block number, and apply any overrides and next checkpoint messages to it before simulation
1637
+ await using merkleTreeFork = await this.worldStateSynchronizer.fork(latestBlockNumber);
1638
+
1639
+ if (nextCheckpointMessages !== undefined) {
1640
+ this.log.debug(
1641
+ `Appending ${nextCheckpointMessages.length} L1-to-L2 messages to the world state tree for the next checkpoint`,
1642
+ { checkpointNumber: l2Tips.proposedCheckpoint.checkpoint.number + 1 },
894
1643
  );
895
- } finally {
896
- await fork.close();
1644
+ await appendL1ToL2MessagesToTree(merkleTreeFork, nextCheckpointMessages);
897
1645
  }
1646
+ await applyPublicDataOverrides(merkleTreeFork, overrides?.publicStorage);
1647
+
1648
+ const config = PublicSimulatorConfig.from({
1649
+ skipFeeEnforcement,
1650
+ collectDebugLogs: true,
1651
+ collectHints: false,
1652
+ collectCallMetadata: true,
1653
+ collectStatistics: false,
1654
+ collectionLimits: CollectionLimitsConfig.from({
1655
+ maxDebugLogMemoryReads: this.config.rpcSimulatePublicMaxDebugLogMemoryReads,
1656
+ }),
1657
+ });
1658
+
1659
+ const contractsDB = new PublicContractsDB(this.contractDataSource, this.log.getBindings());
1660
+ if (overrides?.contracts) {
1661
+ contractsDB.addContracts(Object.values(overrides.contracts).map(({ instance }) => instance));
1662
+ }
1663
+ const processor = publicProcessorFactory.create(merkleTreeFork, newGlobalVariables, config, contractsDB);
1664
+
1665
+ // REFACTOR: Consider merging ProcessReturnValues into ProcessedTx
1666
+ const [processedTxs, failedTxs, _usedTxs, returns, debugLogs] = await processor.process([tx]);
1667
+ // REFACTOR: Consider returning the error rather than throwing
1668
+ if (failedTxs.length) {
1669
+ this.log.warn(`Simulated tx ${txHash} fails: ${failedTxs[0].error}`, { txHash });
1670
+ throw failedTxs[0].error;
1671
+ }
1672
+
1673
+ const [processedTx] = processedTxs;
1674
+ return new PublicSimulationOutput(
1675
+ processedTx.revertReason,
1676
+ processedTx.globalVariables,
1677
+ processedTx.txEffect,
1678
+ returns,
1679
+ processedTx.gasUsed,
1680
+ debugLogs,
1681
+ );
898
1682
  }
899
1683
 
900
1684
  public async isValidTx(
901
1685
  tx: Tx,
902
1686
  { isSimulation, skipFeeEnforcement }: { isSimulation?: boolean; skipFeeEnforcement?: boolean } = {},
903
1687
  ): Promise<TxValidationResult> {
904
- const blockNumber = (await this.blockSource.getBlockNumber()) + 1;
905
1688
  const db = this.worldStateSynchronizer.getCommitted();
906
- const verifier = isSimulation ? undefined : this.proofVerifier;
907
- const validator = createValidatorForAcceptingTxs(db, this.contractDataSource, verifier, {
908
- blockNumber,
909
- l1ChainId: this.l1ChainId,
910
- setupAllowList: this.config.allowedInSetup ?? (await getDefaultAllowedSetupFunctions()),
911
- gasFees: await this.getCurrentBaseFees(),
912
- skipFeeEnforcement,
913
- });
1689
+ const verifier = isSimulation ? undefined : this.rpcProofVerifier;
1690
+
1691
+ // We accept transactions if they are not expired by the next slot (checked based on the ExpirationTimestamp field)
1692
+ const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1693
+ const blockNumber = BlockNumber((await this.blockSource.getBlockNumber()) + 1);
1694
+ const l1Constants = await this.blockSource.getL1Constants();
1695
+ const validator = createTxValidatorForAcceptingTxsOverRPC(
1696
+ db,
1697
+ this.contractDataSource,
1698
+ verifier,
1699
+ {
1700
+ timestamp: nextSlotTimestamp,
1701
+ blockNumber,
1702
+ l1ChainId: this.l1ChainId,
1703
+ rollupVersion: this.version,
1704
+ setupAllowList: [
1705
+ ...(await getDefaultAllowedSetupFunctions()),
1706
+ ...(this.config.txPublicSetupAllowListExtend ?? []),
1707
+ ],
1708
+ gasFees: await this.getCurrentMinFees(),
1709
+ skipFeeEnforcement,
1710
+ txsPermitted: !this.config.disableTransactions,
1711
+ rollupManaLimit: l1Constants.rollupManaLimit,
1712
+ maxBlockL2Gas: this.config.validateMaxL2BlockGas,
1713
+ maxBlockDAGas: this.config.validateMaxDABlockGas,
1714
+ },
1715
+ this.log.getBindings(),
1716
+ );
914
1717
 
915
1718
  return await validator.validateTx(tx);
916
1719
  }
917
1720
 
918
- public async setConfig(config: Partial<SequencerConfig & ProverConfig>): Promise<void> {
919
- const newConfig = { ...this.config, ...config };
920
- await this.sequencer?.updateSequencerConfig(config);
1721
+ public getConfig(): Promise<AztecNodeAdminConfig> {
1722
+ const schema = AztecNodeAdminConfigSchema;
1723
+ const keys = schema.keyof().options;
1724
+ return Promise.resolve(pick(this.config, ...keys));
1725
+ }
921
1726
 
1727
+ public async setConfig(config: Partial<AztecNodeAdminConfig>): Promise<void> {
1728
+ const newConfig = { ...this.config, ...config };
1729
+ // If the sequencer is currently paused via pauseSequencer(), record the caller's desired
1730
+ // minTxsPerBlock as the restore value (so resumeSequencer applies it) and keep the freeze
1731
+ // (MAX_SAFE_INTEGER) applied to the underlying sequencer. Without this guard, forwarding
1732
+ // the new minTxsPerBlock to the sequencer would silently unpause block production while
1733
+ // pauseSequencer() still considers it paused.
1734
+ const sequencerUpdate = { ...config };
1735
+ if (this.sequencerPausedMinTxsPerBlock !== undefined && sequencerUpdate.minTxsPerBlock !== undefined) {
1736
+ this.sequencerPausedMinTxsPerBlock = sequencerUpdate.minTxsPerBlock;
1737
+ delete sequencerUpdate.minTxsPerBlock;
1738
+ }
1739
+ this.sequencer?.updateConfig(sequencerUpdate);
1740
+ this.automineSequencer?.updateConfig(sequencerUpdate);
1741
+ this.slasherClient?.updateConfig(config);
1742
+ this.validatorsSentinel?.updateConfig(config);
1743
+ await this.p2pClient.updateP2PConfig(config);
1744
+ const archiver = this.blockSource as Archiver;
1745
+ if ('updateConfig' in archiver) {
1746
+ archiver.updateConfig(config);
1747
+ }
922
1748
  if (newConfig.realProofs !== this.config.realProofs) {
923
- this.proofVerifier = config.realProofs ? await BBCircuitVerifier.new(newConfig) : new TestCircuitVerifier();
1749
+ await Promise.all([tryStop(this.peerProofVerifier), tryStop(this.rpcProofVerifier)]);
1750
+ if (newConfig.realProofs) {
1751
+ this.peerProofVerifier = await BatchChonkVerifier.new(newConfig, newConfig.bbChonkVerifyMaxBatch, 'peer');
1752
+ const rpcVerifier = await BBCircuitVerifier.new(newConfig);
1753
+ this.rpcProofVerifier = new QueuedIVCVerifier(rpcVerifier, newConfig.numConcurrentIVCVerifiers);
1754
+ } else {
1755
+ this.peerProofVerifier = new TestCircuitVerifier();
1756
+ this.rpcProofVerifier = new TestCircuitVerifier();
1757
+ }
924
1758
  }
925
1759
 
926
1760
  this.config = newConfig;
@@ -928,67 +1762,384 @@ export class AztecNodeService implements AztecNode, Traceable {
928
1762
 
929
1763
  public getProtocolContractAddresses(): Promise<ProtocolContractAddresses> {
930
1764
  return Promise.resolve({
931
- classRegisterer: ProtocolContractAddress.ContractClassRegisterer,
1765
+ classRegistry: ProtocolContractAddress.ContractClassRegistry,
932
1766
  feeJuice: ProtocolContractAddress.FeeJuice,
933
- instanceDeployer: ProtocolContractAddress.ContractInstanceDeployer,
934
- multiCallEntrypoint: ProtocolContractAddress.MultiCallEntrypoint,
1767
+ instanceRegistry: ProtocolContractAddress.ContractInstanceRegistry,
1768
+ multiCallEntrypoint: STANDARD_MULTI_CALL_ENTRYPOINT_ADDRESS,
935
1769
  });
936
1770
  }
937
1771
 
938
- // TODO(#10007): Remove this method
939
- public addContractClass(contractClass: ContractClassPublic): Promise<void> {
940
- this.log.info(`Adding contract class via API ${contractClass.id}`);
941
- return this.contractDataSource.addContractClass(contractClass);
1772
+ public registerContractFunctionSignatures(signatures: string[]): Promise<void> {
1773
+ return this.contractDataSource.registerContractFunctionSignatures(signatures);
942
1774
  }
943
1775
 
944
- public registerContractFunctionSignatures(_address: AztecAddress, signatures: string[]): Promise<void> {
945
- return this.contractDataSource.registerContractFunctionSignatures(_address, signatures);
1776
+ public getValidatorsStats(): Promise<ValidatorsStats> {
1777
+ return this.validatorsSentinel?.computeStats() ?? Promise.resolve({ stats: {}, slotWindow: 0 });
946
1778
  }
947
1779
 
948
- public flushTxs(): Promise<void> {
949
- if (!this.sequencer) {
950
- throw new Error(`Sequencer is not initialized`);
1780
+ public getValidatorStats(
1781
+ validatorAddress: EthAddress,
1782
+ fromSlot?: SlotNumber,
1783
+ toSlot?: SlotNumber,
1784
+ ): Promise<SingleValidatorStats | undefined> {
1785
+ return this.validatorsSentinel?.getValidatorStats(validatorAddress, fromSlot, toSlot) ?? Promise.resolve(undefined);
1786
+ }
1787
+
1788
+ public async startSnapshotUpload(location: string): Promise<void> {
1789
+ // Note that we are forcefully casting the blocksource as an archiver
1790
+ // We break support for archiver running remotely to the node
1791
+ const archiver = this.blockSource as Archiver;
1792
+ if (!('backupTo' in archiver)) {
1793
+ this.metrics.recordSnapshotError();
1794
+ throw new Error('Archiver implementation does not support backups. Cannot generate snapshot.');
1795
+ }
1796
+
1797
+ // Test that the archiver has done an initial sync.
1798
+ if (!archiver.isInitialSyncComplete()) {
1799
+ this.metrics.recordSnapshotError();
1800
+ throw new Error(`Archiver initial sync not complete. Cannot start snapshot.`);
951
1801
  }
952
- this.sequencer.flush();
1802
+
1803
+ // And it has an L2 block hash
1804
+ const l2BlockHash = await archiver.getL2Tips().then(tips => tips.proposed.hash);
1805
+ if (!l2BlockHash) {
1806
+ this.metrics.recordSnapshotError();
1807
+ throw new Error(`Archiver has no latest L2 block hash downloaded. Cannot start snapshot.`);
1808
+ }
1809
+
1810
+ if (this.isUploadingSnapshot) {
1811
+ this.metrics.recordSnapshotError();
1812
+ throw new Error(`Snapshot upload already in progress. Cannot start another one until complete.`);
1813
+ }
1814
+
1815
+ // Do not wait for the upload to be complete to return to the caller, but flag that an operation is in progress
1816
+ this.isUploadingSnapshot = true;
1817
+ const timer = new Timer();
1818
+ void uploadSnapshot(location, this.blockSource as Archiver, this.worldStateSynchronizer, this.config, this.log)
1819
+ .then(() => {
1820
+ this.isUploadingSnapshot = false;
1821
+ this.metrics.recordSnapshot(timer.ms());
1822
+ })
1823
+ .catch(err => {
1824
+ this.isUploadingSnapshot = false;
1825
+ this.metrics.recordSnapshotError();
1826
+ this.log.error(`Error uploading snapshot: ${err}`);
1827
+ });
1828
+
953
1829
  return Promise.resolve();
954
1830
  }
955
1831
 
1832
+ public async rollbackTo(targetBlock: BlockNumber, force?: boolean, resumeSync = true): Promise<void> {
1833
+ const archiver = this.blockSource as Archiver;
1834
+ if (!('rollbackTo' in archiver)) {
1835
+ throw new Error('Archiver implementation does not support rollbacks.');
1836
+ }
1837
+
1838
+ const finalizedBlock = await archiver.getL2Tips().then(tips => tips.finalized.block.number);
1839
+ if (targetBlock < finalizedBlock) {
1840
+ if (force) {
1841
+ this.log.warn(`Clearing world state database to allow rolling back behind finalized block ${finalizedBlock}`);
1842
+ await this.worldStateSynchronizer.clear();
1843
+ await this.p2pClient.clear();
1844
+ } else {
1845
+ throw new Error(`Cannot rollback to block ${targetBlock} as it is before finalized ${finalizedBlock}`);
1846
+ }
1847
+ }
1848
+
1849
+ try {
1850
+ this.log.info(`Pausing archiver and world state sync to start rollback`);
1851
+ await archiver.stop();
1852
+ await this.worldStateSynchronizer.stopSync();
1853
+ const currentBlock = await archiver.getBlockNumber();
1854
+ const blocksToUnwind = currentBlock - targetBlock;
1855
+ this.log.info(`Unwinding ${count(blocksToUnwind, 'block')} from L2 block ${currentBlock} to ${targetBlock}`);
1856
+ await archiver.rollbackTo(targetBlock);
1857
+ this.log.info(`Unwinding complete.`);
1858
+ } catch (err) {
1859
+ this.log.error(`Error during rollback`, err);
1860
+ throw err;
1861
+ } finally {
1862
+ if (resumeSync) {
1863
+ this.log.info(`Resuming world state and archiver sync.`);
1864
+ this.worldStateSynchronizer.resumeSync();
1865
+ archiver.resume();
1866
+ } else {
1867
+ this.log.info(`Sync left paused after rollback (resumeSync=false).`);
1868
+ }
1869
+ }
1870
+ }
1871
+
1872
+ public async pauseSync(): Promise<void> {
1873
+ this.log.info(`Pausing archiver and world state sync`);
1874
+ await (this.blockSource as Archiver).stop();
1875
+ await this.worldStateSynchronizer.stopSync();
1876
+ }
1877
+
1878
+ public resumeSync(): Promise<void> {
1879
+ this.log.info(`Resuming world state and archiver sync.`);
1880
+ this.worldStateSynchronizer.resumeSync();
1881
+ (this.blockSource as Archiver).resume();
1882
+ return Promise.resolve();
1883
+ }
1884
+
1885
+ public pauseSequencer(): Promise<void> {
1886
+ if (this.automineSequencer) {
1887
+ this.automineSequencer.pause();
1888
+ return Promise.resolve();
1889
+ }
1890
+ if (this.sequencer) {
1891
+ if (this.sequencerPausedMinTxsPerBlock === undefined) {
1892
+ this.sequencerPausedMinTxsPerBlock = this.sequencer.getSequencer().getConfig().minTxsPerBlock ?? 0;
1893
+ this.sequencer.updateConfig({ minTxsPerBlock: Number.MAX_SAFE_INTEGER });
1894
+ this.log.info(`Sequencer paused (minTxsPerBlock set to MAX_SAFE_INTEGER)`, {
1895
+ previousMinTxsPerBlock: this.sequencerPausedMinTxsPerBlock,
1896
+ });
1897
+ }
1898
+ return Promise.resolve();
1899
+ }
1900
+ throw new BadRequestError('Cannot pause sequencer: no sequencer is running');
1901
+ }
1902
+
1903
+ public resumeSequencer(): Promise<void> {
1904
+ if (this.automineSequencer) {
1905
+ this.automineSequencer.resume();
1906
+ return Promise.resolve();
1907
+ }
1908
+ if (this.sequencer) {
1909
+ if (this.sequencerPausedMinTxsPerBlock !== undefined) {
1910
+ const restored = this.sequencerPausedMinTxsPerBlock;
1911
+ this.sequencerPausedMinTxsPerBlock = undefined;
1912
+ this.sequencer.updateConfig({ minTxsPerBlock: restored });
1913
+ this.log.info(`Sequencer resumed (minTxsPerBlock restored)`, { minTxsPerBlock: restored });
1914
+ }
1915
+ return Promise.resolve();
1916
+ }
1917
+ throw new BadRequestError('Cannot resume sequencer: no sequencer is running');
1918
+ }
1919
+
1920
+ public getSlashOffenses(round: bigint | 'all' | 'current'): Promise<Offense[]> {
1921
+ if (!this.slasherClient) {
1922
+ throw new Error(`Slasher client not enabled`);
1923
+ }
1924
+ if (round === 'all') {
1925
+ return this.slasherClient.getOffenses();
1926
+ } else {
1927
+ return this.slasherClient.gatherOffensesForRound(round === 'current' ? undefined : BigInt(round));
1928
+ }
1929
+ }
1930
+
1931
+ public async reloadKeystore(): Promise<void> {
1932
+ if (!this.config.keyStoreDirectory?.length) {
1933
+ throw new BadRequestError(
1934
+ 'Cannot reload keystore: node is not using a file-based keystore. ' +
1935
+ 'Set KEY_STORE_DIRECTORY to use file-based keystores.',
1936
+ );
1937
+ }
1938
+ if (!this.validatorClient) {
1939
+ throw new BadRequestError('Cannot reload keystore: validator is not enabled.');
1940
+ }
1941
+
1942
+ this.log.info('Reloading keystore from disk');
1943
+
1944
+ // Re-read and validate keystore files
1945
+ const keyStores = loadKeystores(this.config.keyStoreDirectory);
1946
+ const newManager = new KeystoreManager(mergeKeystores(keyStores));
1947
+ await newManager.validateSigners();
1948
+ ValidatorClient.validateKeyStoreConfiguration(newManager, this.log);
1949
+
1950
+ // Validate that every validator's publisher keys overlap with the L1 signers
1951
+ // that were initialized at startup. Publishers cannot be hot-reloaded, so a
1952
+ // validator with a publisher key that doesn't match any existing L1 signer
1953
+ // would silently fail on every proposer slot.
1954
+ if (this.keyStoreManager && this.sequencer) {
1955
+ const oldAdapter = NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager);
1956
+ const availablePublishers = new Set(
1957
+ oldAdapter
1958
+ .getAttesterAddresses()
1959
+ .flatMap(a => oldAdapter.getPublisherAddresses(a).map(p => p.toString().toLowerCase())),
1960
+ );
1961
+
1962
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1963
+ for (const attester of newAdapter.getAttesterAddresses()) {
1964
+ const pubs = newAdapter.getPublisherAddresses(attester);
1965
+ if (pubs.length > 0 && !pubs.some(p => availablePublishers.has(p.toString().toLowerCase()))) {
1966
+ throw new BadRequestError(
1967
+ `Cannot reload keystore: validator ${attester} has publisher keys ` +
1968
+ `[${pubs.map(p => p.toString()).join(', ')}] but none match the L1 signers initialized at startup ` +
1969
+ `[${[...availablePublishers].join(', ')}]. Publishers cannot be hot-reloaded — ` +
1970
+ `use an existing publisher key or restart the node.`,
1971
+ );
1972
+ }
1973
+ }
1974
+ }
1975
+
1976
+ // Build adapters for old and new keystores to compute diff
1977
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
1978
+ const newAddresses = newAdapter.getAttesterAddresses();
1979
+ const oldAddresses = this.keyStoreManager
1980
+ ? NodeKeystoreAdapter.fromKeyStoreManager(this.keyStoreManager).getAttesterAddresses()
1981
+ : [];
1982
+
1983
+ const oldSet = new Set(oldAddresses.map(a => a.toString()));
1984
+ const newSet = new Set(newAddresses.map(a => a.toString()));
1985
+ const added = newAddresses.filter(a => !oldSet.has(a.toString()));
1986
+ const removed = oldAddresses.filter(a => !newSet.has(a.toString()));
1987
+
1988
+ if (added.length > 0) {
1989
+ this.log.info(`Keystore reload: adding attester keys: ${added.map(a => a.toString()).join(', ')}`);
1990
+ }
1991
+ if (removed.length > 0) {
1992
+ this.log.info(`Keystore reload: removing attester keys: ${removed.map(a => a.toString()).join(', ')}`);
1993
+ }
1994
+ if (added.length === 0 && removed.length === 0) {
1995
+ this.log.info('Keystore reload: attester keys unchanged');
1996
+ }
1997
+
1998
+ // Update the validator client (coinbase, feeRecipient, attester keys)
1999
+ this.validatorClient.reloadKeystore(newManager);
2000
+
2001
+ // Update the publisher factory's keystore so newly-added validators
2002
+ // can be matched to existing publisher keys when proposing blocks.
2003
+ if (this.sequencer) {
2004
+ this.sequencer.updatePublisherNodeKeyStore(newAdapter);
2005
+ }
2006
+
2007
+ // Update slasher's "don't-slash-self" list with new validator addresses
2008
+ if (this.slasherClient && !this.config.slashSelfAllowed) {
2009
+ const slashValidatorsNever = unique(
2010
+ [...(this.config.slashValidatorsNever ?? []), ...newAddresses].map(a => a.toString()),
2011
+ ).map(EthAddress.fromString);
2012
+ this.slasherClient.updateConfig({ slashValidatorsNever });
2013
+ }
2014
+
2015
+ this.keyStoreManager = newManager;
2016
+ this.log.info('Keystore reloaded: coinbase, feeRecipient, and attester keys updated');
2017
+ }
2018
+
2019
+ public async mineBlock(): Promise<void> {
2020
+ if (this.automineSequencer) {
2021
+ await this.automineSequencer.buildEmptyBlock();
2022
+ return;
2023
+ }
2024
+ if (!this.sequencer) {
2025
+ throw new BadRequestError('Cannot mine block: no sequencer is running');
2026
+ }
2027
+
2028
+ const currentBlockNumber = await this.getBlockNumber();
2029
+
2030
+ // Use slot duration + 50% buffer as the timeout so this works on running networks too
2031
+ const { slotDuration } = await this.blockSource.getL1Constants();
2032
+ const timeoutSeconds = Math.ceil(slotDuration * 1.5);
2033
+
2034
+ // Temporarily set minTxsPerBlock to 0 so the sequencer produces a block even with no txs
2035
+ const originalMinTxsPerBlock = this.sequencer.getSequencer().getConfig().minTxsPerBlock;
2036
+ this.sequencer.updateConfig({ minTxsPerBlock: 0 });
2037
+
2038
+ try {
2039
+ // Trigger the sequencer to produce a block immediately
2040
+ void this.sequencer.trigger();
2041
+
2042
+ // Wait for the new L2 block to appear
2043
+ await retryUntil(
2044
+ async () => {
2045
+ const newBlockNumber = await this.getBlockNumber();
2046
+ return newBlockNumber > currentBlockNumber ? true : undefined;
2047
+ },
2048
+ 'mineBlock',
2049
+ timeoutSeconds,
2050
+ 0.1,
2051
+ );
2052
+ } finally {
2053
+ this.sequencer.updateConfig({ minTxsPerBlock: originalMinTxsPerBlock });
2054
+ }
2055
+ }
2056
+
956
2057
  /**
957
2058
  * Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
958
- * @param blockNumber - The block number at which to get the data.
2059
+ * @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
959
2060
  * @returns An instance of a committed MerkleTreeOperations
960
2061
  */
961
- async #getWorldState(blockNumber: L2BlockNumber) {
962
- if (typeof blockNumber === 'number' && blockNumber < INITIAL_L2_BLOCK_NUM - 1) {
963
- throw new Error('Invalid block number to get world state for: ' + blockNumber);
964
- }
965
-
966
- let blockSyncedTo: number = 0;
2062
+ protected async getWorldState(block: BlockParameter) {
2063
+ const query = this.normalizeBlockParameter(block);
2064
+
2065
+ // When the request anchors on a specific block hash, resolve it against the archiver up front and
2066
+ // drive the world-state sync to that exact block number and hash. Resolving against the archiver
2067
+ // first fails fast with a clear reorg error if the hash is unknown, and passing the hash to the
2068
+ // synchronizer makes the sync reorg-aware: it barriers until the archive-tree commit for that block
2069
+ // has landed and verifies it matches the requested fork, instead of syncing to bare latest height
2070
+ // and then racing the snapshot read below against an in-flight archive-tree write.
2071
+ const requestedHash = 'hash' in query ? query.hash : undefined;
2072
+ const anchorBlockNumber = requestedHash !== undefined ? await this.resolveBlockNumber(query) : undefined;
2073
+
2074
+ let blockSyncedTo: BlockNumber = BlockNumber.ZERO;
967
2075
  try {
968
2076
  // Attempt to sync the world state if necessary
969
- blockSyncedTo = await this.#syncWorldState();
2077
+ blockSyncedTo = await this.#syncWorldState(anchorBlockNumber, requestedHash);
970
2078
  } catch (err) {
971
2079
  this.log.error(`Error getting world state: ${err}`);
972
2080
  }
973
2081
 
974
- // using a snapshot could be less efficient than using the committed db
975
- if (blockNumber === 'latest' /*|| blockNumber === blockSyncedTo*/) {
976
- this.log.debug(`Using committed db for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
2082
+ if ('tag' in query && query.tag === 'proposed') {
2083
+ this.log.debug(`Using committed db for latest block, world state synced upto ${blockSyncedTo}`);
977
2084
  return this.worldStateSynchronizer.getCommitted();
978
- } else if (blockNumber <= blockSyncedTo) {
979
- this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
980
- return this.worldStateSynchronizer.getSnapshot(blockNumber);
981
- } else {
982
- throw new Error(`Block ${blockNumber} not yet synced`);
983
2085
  }
2086
+
2087
+ const blockNumber = anchorBlockNumber ?? (await this.resolveBlockNumber(query));
2088
+
2089
+ // Check it's within world state sync range
2090
+ if (blockNumber > blockSyncedTo) {
2091
+ throw new Error(
2092
+ `Queried block ${inspectBlockParameter(block)} not yet synced by the node (node is synced upto ${blockSyncedTo}).`,
2093
+ );
2094
+ }
2095
+ this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
2096
+
2097
+ const snapshot = this.worldStateSynchronizer.getSnapshot(blockNumber);
2098
+
2099
+ // Double-check world-state synced to the same block hash as was requested.
2100
+ // Block 0 is skipped: the snapshot returned by `getSnapshot(0)` is the *pre*-genesis archive
2101
+ // (size 0), so leaf 0 is not yet inserted from that snapshot's view even though block 0's hash
2102
+ // does live at archive index 0 in the committed tree. The genesis hash is already validated by
2103
+ // the archiver when it resolves the hash query to block number 0.
2104
+ if (requestedHash !== undefined && blockNumber !== BlockNumber.ZERO) {
2105
+ const blockHash = await snapshot.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
2106
+ if (!blockHash || !requestedHash.equals(blockHash)) {
2107
+ throw new Error(
2108
+ `Block hash ${requestedHash.toString()} not found in world state at block number ${blockNumber} (world state has ${blockHash?.toString() ?? 'no hash'} at that index, genesis header hash is ${this.blockSource.getGenesisBlockHash().toString()}). If the node API has been queried with anchor block hash possibly a reorg has occurred.`,
2109
+ );
2110
+ }
2111
+ }
2112
+
2113
+ return snapshot;
2114
+ }
2115
+
2116
+ /** Resolves any {@link BlockParameter} variant to a concrete block number. */
2117
+ protected async resolveBlockNumber(block: BlockParameter): Promise<BlockNumber> {
2118
+ const query = this.normalizeBlockParameter(block);
2119
+ const blockNumber = await this.blockSource.getBlockNumber(query);
2120
+ if (blockNumber === undefined) {
2121
+ if ('hash' in query) {
2122
+ throw new Error(
2123
+ `Block hash ${query.hash.toString()} not found when querying world state. If the node API has been queried with anchor block hash possibly a reorg has occurred.`,
2124
+ );
2125
+ }
2126
+ if ('archive' in query) {
2127
+ throw new Error(`Block with archive ${query.archive.toString()} not found.`);
2128
+ }
2129
+ throw new Error(`Block not found for ${inspectBlockParameter(block)}.`);
2130
+ }
2131
+ return blockNumber;
984
2132
  }
985
2133
 
986
2134
  /**
987
- * Ensure we fully sync the world state
2135
+ * Ensure the world state is synced.
2136
+ * @param targetBlockNumber - Block to sync up to. Defaults to the latest block known to the archiver.
2137
+ * @param blockHash - If provided, the synchronizer verifies the block at `targetBlockNumber` matches this
2138
+ * hash, resyncing (and so detecting reorgs) if it does not yet match or has been reorged away.
988
2139
  * @returns A promise that fulfils once the world state is synced
989
2140
  */
990
- async #syncWorldState(): Promise<number> {
991
- const blockSourceHeight = await this.blockSource.getBlockNumber();
992
- return this.worldStateSynchronizer.syncImmediate(blockSourceHeight);
2141
+ async #syncWorldState(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
2142
+ const target = targetBlockNumber ?? (await this.blockSource.getBlockNumber());
2143
+ return await this.worldStateSynchronizer.syncImmediate(target, blockHash);
993
2144
  }
994
2145
  }