@aztec/aztec-node 0.0.0-test.1 → 0.0.1-commit.03f7ef2

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 (48) hide show
  1. package/dest/aztec-node/config.d.ts +18 -10
  2. package/dest/aztec-node/config.d.ts.map +1 -1
  3. package/dest/aztec-node/config.js +81 -14
  4. package/dest/aztec-node/node_metrics.d.ts +5 -1
  5. package/dest/aztec-node/node_metrics.d.ts.map +1 -1
  6. package/dest/aztec-node/node_metrics.js +21 -0
  7. package/dest/aztec-node/server.d.ts +110 -82
  8. package/dest/aztec-node/server.d.ts.map +1 -1
  9. package/dest/aztec-node/server.js +581 -236
  10. package/dest/bin/index.d.ts +1 -1
  11. package/dest/bin/index.js +4 -2
  12. package/dest/index.d.ts +1 -2
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +0 -1
  15. package/dest/sentinel/config.d.ts +8 -0
  16. package/dest/sentinel/config.d.ts.map +1 -0
  17. package/dest/sentinel/config.js +29 -0
  18. package/dest/sentinel/factory.d.ts +9 -0
  19. package/dest/sentinel/factory.d.ts.map +1 -0
  20. package/dest/sentinel/factory.js +17 -0
  21. package/dest/sentinel/index.d.ts +3 -0
  22. package/dest/sentinel/index.d.ts.map +1 -0
  23. package/dest/sentinel/index.js +1 -0
  24. package/dest/sentinel/sentinel.d.ts +92 -0
  25. package/dest/sentinel/sentinel.d.ts.map +1 -0
  26. package/dest/sentinel/sentinel.js +398 -0
  27. package/dest/sentinel/store.d.ts +35 -0
  28. package/dest/sentinel/store.d.ts.map +1 -0
  29. package/dest/sentinel/store.js +170 -0
  30. package/dest/test/index.d.ts +31 -0
  31. package/dest/test/index.d.ts.map +1 -0
  32. package/dest/test/index.js +1 -0
  33. package/package.json +45 -34
  34. package/src/aztec-node/config.ts +132 -25
  35. package/src/aztec-node/node_metrics.ts +28 -0
  36. package/src/aztec-node/server.ts +776 -319
  37. package/src/bin/index.ts +4 -2
  38. package/src/index.ts +0 -1
  39. package/src/sentinel/config.ts +37 -0
  40. package/src/sentinel/factory.ts +36 -0
  41. package/src/sentinel/index.ts +8 -0
  42. package/src/sentinel/sentinel.ts +501 -0
  43. package/src/sentinel/store.ts +185 -0
  44. package/src/test/index.ts +32 -0
  45. package/dest/aztec-node/http_rpc_server.d.ts +0 -8
  46. package/dest/aztec-node/http_rpc_server.d.ts.map +0 -1
  47. package/dest/aztec-node/http_rpc_server.js +0 -9
  48. package/src/aztec-node/http_rpc_server.ts +0 -11
@@ -5,33 +5,50 @@ function _ts_decorate(decorators, target, key, desc) {
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  }
7
7
  import { createArchiver } from '@aztec/archiver';
8
- import { BBCircuitVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
9
- import { createBlobSinkClient } from '@aztec/blob-sink/client';
10
- import { INITIAL_L2_BLOCK_NUM, REGISTERER_CONTRACT_ADDRESS } from '@aztec/constants';
8
+ import { BBCircuitVerifier, QueuedIVCVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
9
+ import { createBlobClient } from '@aztec/blob-client/client';
10
+ import { createReadOnlyFileStoreBlobClients, createWritableFileStoreBlobClient } from '@aztec/blob-client/filestore';
11
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
11
12
  import { EpochCache } from '@aztec/epoch-cache';
12
- import { createEthereumChain } from '@aztec/ethereum';
13
- import { compactArray } from '@aztec/foundation/collection';
13
+ import { createEthereumChain } from '@aztec/ethereum/chain';
14
+ import { getPublicClient } from '@aztec/ethereum/client';
15
+ import { RegistryContract, RollupContract } from '@aztec/ethereum/contracts';
16
+ import { BlockNumber } from '@aztec/foundation/branded-types';
17
+ import { compactArray, pick } from '@aztec/foundation/collection';
18
+ import { Fr } from '@aztec/foundation/curves/bn254';
14
19
  import { EthAddress } from '@aztec/foundation/eth-address';
15
- import { Fr } from '@aztec/foundation/fields';
20
+ import { BadRequestError } from '@aztec/foundation/json-rpc';
16
21
  import { createLogger } from '@aztec/foundation/log';
22
+ import { count } from '@aztec/foundation/string';
17
23
  import { DateProvider, Timer } from '@aztec/foundation/timer';
18
- import { SiblingPath } from '@aztec/foundation/trees';
19
- import { openTmpStore } from '@aztec/kv-store/lmdb';
20
- import { SHA256Trunc, StandardTree, UnbalancedTree } from '@aztec/merkle-tree';
21
- import { createP2PClient } from '@aztec/p2p';
24
+ import { MembershipWitness } from '@aztec/foundation/trees';
25
+ import { KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
26
+ import { trySnapshotSync, uploadSnapshot } from '@aztec/node-lib/actions';
27
+ import { createForwarderL1TxUtilsFromEthSigner, createL1TxUtilsWithBlobsFromEthSigner } from '@aztec/node-lib/factories';
28
+ import { createP2PClient, getDefaultAllowedSetupFunctions } from '@aztec/p2p';
22
29
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
23
- import { GlobalVariableBuilder, SequencerClient, createSlasherClient, createValidatorForAcceptingTxs, getDefaultAllowedSetupFunctions } from '@aztec/sequencer-client';
30
+ import { BlockBuilder, GlobalVariableBuilder, SequencerClient, createValidatorForAcceptingTxs } from '@aztec/sequencer-client';
31
+ import { CheckpointsBuilder } from '@aztec/sequencer-client';
24
32
  import { PublicProcessorFactory } from '@aztec/simulator/server';
33
+ import { AttestationsBlockWatcher, EpochPruneWatcher, createSlasher } from '@aztec/slasher';
34
+ import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
25
35
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
26
- import { computePublicDataTreeLeafSlot, siloNullifier } from '@aztec/stdlib/hash';
36
+ import { L2BlockHash } from '@aztec/stdlib/block';
37
+ import { GasFees } from '@aztec/stdlib/gas';
38
+ import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
39
+ import { AztecNodeAdminConfigSchema } from '@aztec/stdlib/interfaces/client';
27
40
  import { tryStop } from '@aztec/stdlib/interfaces/server';
41
+ import { InboxLeaf } from '@aztec/stdlib/messaging';
28
42
  import { P2PClientType } from '@aztec/stdlib/p2p';
29
43
  import { MerkleTreeId, NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
30
44
  import { PublicSimulationOutput, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
45
+ import { getPackageVersion } from '@aztec/stdlib/update-checker';
31
46
  import { Attributes, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
32
- import { createValidatorClient } from '@aztec/validator-client';
47
+ import { NodeKeystoreAdapter, ValidatorClient, createBlockProposalHandler, createValidatorClient } from '@aztec/validator-client';
33
48
  import { createWorldStateSynchronizer } from '@aztec/world-state';
34
- import { getPackageVersion } from './config.js';
49
+ import { createPublicClient, fallback, http } from 'viem';
50
+ import { createSentinel } from '../sentinel/factory.js';
51
+ import { createKeyStoreForValidator } from './config.js';
35
52
  import { NodeMetrics } from './node_metrics.js';
36
53
  /**
37
54
  * The aztec node.
@@ -42,35 +59,44 @@ import { NodeMetrics } from './node_metrics.js';
42
59
  logsSource;
43
60
  contractDataSource;
44
61
  l1ToL2MessageSource;
45
- nullifierSource;
46
62
  worldStateSynchronizer;
47
63
  sequencer;
64
+ slasherClient;
65
+ validatorsSentinel;
66
+ epochPruneWatcher;
48
67
  l1ChainId;
49
68
  version;
50
69
  globalVariableBuilder;
70
+ epochCache;
71
+ packageVersion;
51
72
  proofVerifier;
52
73
  telemetry;
53
74
  log;
54
- packageVersion;
55
75
  metrics;
76
+ // Prevent two snapshot operations to happen simultaneously
77
+ isUploadingSnapshot;
56
78
  tracer;
57
- constructor(config, p2pClient, blockSource, logsSource, contractDataSource, l1ToL2MessageSource, nullifierSource, worldStateSynchronizer, sequencer, l1ChainId, version, globalVariableBuilder, proofVerifier, telemetry = getTelemetryClient(), log = createLogger('node')){
79
+ constructor(config, p2pClient, blockSource, logsSource, contractDataSource, l1ToL2MessageSource, worldStateSynchronizer, sequencer, slasherClient, validatorsSentinel, epochPruneWatcher, l1ChainId, version, globalVariableBuilder, epochCache, packageVersion, proofVerifier, telemetry = getTelemetryClient(), log = createLogger('node')){
58
80
  this.config = config;
59
81
  this.p2pClient = p2pClient;
60
82
  this.blockSource = blockSource;
61
83
  this.logsSource = logsSource;
62
84
  this.contractDataSource = contractDataSource;
63
85
  this.l1ToL2MessageSource = l1ToL2MessageSource;
64
- this.nullifierSource = nullifierSource;
65
86
  this.worldStateSynchronizer = worldStateSynchronizer;
66
87
  this.sequencer = sequencer;
88
+ this.slasherClient = slasherClient;
89
+ this.validatorsSentinel = validatorsSentinel;
90
+ this.epochPruneWatcher = epochPruneWatcher;
67
91
  this.l1ChainId = l1ChainId;
68
92
  this.version = version;
69
93
  this.globalVariableBuilder = globalVariableBuilder;
94
+ this.epochCache = epochCache;
95
+ this.packageVersion = packageVersion;
70
96
  this.proofVerifier = proofVerifier;
71
97
  this.telemetry = telemetry;
72
98
  this.log = log;
73
- this.packageVersion = getPackageVersion();
99
+ this.isUploadingSnapshot = false;
74
100
  this.metrics = new NodeMetrics(telemetry, 'AztecNodeService');
75
101
  this.tracer = telemetry.getTracer('AztecNodeService');
76
102
  this.log.info(`Aztec Node version: ${this.packageVersion}`);
@@ -87,59 +113,232 @@ import { NodeMetrics } from './node_metrics.js';
87
113
  * initializes the Aztec Node, wait for component to sync.
88
114
  * @param config - The configuration to be used by the aztec node.
89
115
  * @returns - A fully synced Aztec Node for use in development/testing.
90
- */ static async createAndSync(config, deps = {}, options = {}) {
91
- const telemetry = deps.telemetry ?? getTelemetryClient();
116
+ */ static async createAndSync(inputConfig, deps = {}, options = {}) {
117
+ const config = {
118
+ ...inputConfig
119
+ }; // Copy the config so we dont mutate the input object
92
120
  const log = deps.logger ?? createLogger('node');
121
+ const packageVersion = getPackageVersion() ?? '';
122
+ const telemetry = deps.telemetry ?? getTelemetryClient();
93
123
  const dateProvider = deps.dateProvider ?? new DateProvider();
94
- const blobSinkClient = deps.blobSinkClient ?? createBlobSinkClient(config);
95
124
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
96
- //validate that the actual chain id matches that specified in configuration
125
+ // Build a key store from file if given or from environment otherwise
126
+ let keyStoreManager;
127
+ const keyStoreProvided = config.keyStoreDirectory !== undefined && config.keyStoreDirectory.length > 0;
128
+ if (keyStoreProvided) {
129
+ const keyStores = loadKeystores(config.keyStoreDirectory);
130
+ keyStoreManager = new KeystoreManager(mergeKeystores(keyStores));
131
+ } else {
132
+ const keyStore = createKeyStoreForValidator(config);
133
+ if (keyStore) {
134
+ keyStoreManager = new KeystoreManager(keyStore);
135
+ }
136
+ }
137
+ await keyStoreManager?.validateSigners();
138
+ // If we are a validator, verify our configuration before doing too much more.
139
+ if (!config.disableValidator) {
140
+ if (keyStoreManager === undefined) {
141
+ throw new Error('Failed to create key store, a requirement for running a validator');
142
+ }
143
+ if (!keyStoreProvided) {
144
+ log.warn('KEY STORE CREATED FROM ENVIRONMENT, IT IS RECOMMENDED TO USE A FILE-BASED KEY STORE IN PRODUCTION ENVIRONMENTS');
145
+ }
146
+ ValidatorClient.validateKeyStoreConfiguration(keyStoreManager, log);
147
+ }
148
+ // validate that the actual chain id matches that specified in configuration
97
149
  if (config.l1ChainId !== ethereumChain.chainInfo.id) {
98
150
  throw new Error(`RPC URL configured for chain id ${ethereumChain.chainInfo.id} but expected id ${config.l1ChainId}`);
99
151
  }
100
- const archiver = await createArchiver(config, blobSinkClient, {
101
- blockUntilSync: true
102
- }, telemetry);
152
+ const publicClient = createPublicClient({
153
+ chain: ethereumChain.chainInfo,
154
+ transport: fallback(config.l1RpcUrls.map((url)=>http(url, {
155
+ batch: false
156
+ }))),
157
+ pollingInterval: config.viemPollingIntervalMS
158
+ });
159
+ const l1ContractsAddresses = await RegistryContract.collectAddresses(publicClient, config.l1Contracts.registryAddress, config.rollupVersion ?? 'canonical');
160
+ // Overwrite the passed in vars.
161
+ config.l1Contracts = {
162
+ ...config.l1Contracts,
163
+ ...l1ContractsAddresses
164
+ };
165
+ const rollupContract = new RollupContract(publicClient, config.l1Contracts.rollupAddress.toString());
166
+ const [l1GenesisTime, slotDuration, rollupVersionFromRollup] = await Promise.all([
167
+ rollupContract.getL1GenesisTime(),
168
+ rollupContract.getSlotDuration(),
169
+ rollupContract.getVersion()
170
+ ]);
171
+ config.rollupVersion ??= Number(rollupVersionFromRollup);
172
+ if (config.rollupVersion !== Number(rollupVersionFromRollup)) {
173
+ log.warn(`Registry looked up and returned a rollup with version (${config.rollupVersion}), but this does not match with version detected from the rollup directly: (${rollupVersionFromRollup}).`);
174
+ }
175
+ const blobFileStoreMetadata = {
176
+ l1ChainId: config.l1ChainId,
177
+ rollupVersion: config.rollupVersion,
178
+ rollupAddress: config.l1Contracts.rollupAddress.toString()
179
+ };
180
+ const [fileStoreClients, fileStoreUploadClient] = await Promise.all([
181
+ createReadOnlyFileStoreBlobClients(config.blobFileStoreUrls, blobFileStoreMetadata, log),
182
+ createWritableFileStoreBlobClient(config.blobFileStoreUploadUrl, blobFileStoreMetadata, log)
183
+ ]);
184
+ const blobClient = deps.blobClient ?? createBlobClient(config, {
185
+ logger: createLogger('node:blob-client:client'),
186
+ fileStoreClients,
187
+ fileStoreUploadClient
188
+ });
189
+ // attempt snapshot sync if possible
190
+ await trySnapshotSync(config, log);
191
+ const epochCache = await EpochCache.create(config.l1Contracts.rollupAddress, config, {
192
+ dateProvider
193
+ });
194
+ const archiver = await createArchiver(config, {
195
+ blobClient,
196
+ epochCache,
197
+ telemetry,
198
+ dateProvider
199
+ }, {
200
+ blockUntilSync: !config.skipArchiverInitialSync
201
+ });
103
202
  // now create the merkle trees and the world state synchronizer
104
203
  const worldStateSynchronizer = await createWorldStateSynchronizer(config, archiver, options.prefilledPublicData, telemetry);
105
- const proofVerifier = config.realProofs ? await BBCircuitVerifier.new(config) : new TestCircuitVerifier();
204
+ const circuitVerifier = config.realProofs || config.debugForceTxProofVerification ? await BBCircuitVerifier.new(config) : new TestCircuitVerifier(config.proverTestVerificationDelayMs);
106
205
  if (!config.realProofs) {
107
206
  log.warn(`Aztec node is accepting fake proofs`);
108
207
  }
109
- const epochCache = await EpochCache.create(config.l1Contracts.rollupAddress, config, {
110
- dateProvider
111
- });
208
+ const proofVerifier = new QueuedIVCVerifier(config, circuitVerifier);
112
209
  // create the tx pool and the p2p client, which will need the l2 block source
113
- const p2pClient = await createP2PClient(P2PClientType.Full, config, archiver, proofVerifier, worldStateSynchronizer, epochCache, telemetry);
114
- const slasherClient = createSlasherClient(config, archiver, telemetry);
115
- // start both and wait for them to sync from the block source
116
- await Promise.all([
117
- p2pClient.start(),
118
- worldStateSynchronizer.start(),
119
- slasherClient.start()
120
- ]);
121
- log.verbose(`All Aztec Node subsystems synced`);
210
+ const p2pClient = await createP2PClient(P2PClientType.Full, config, archiver, proofVerifier, worldStateSynchronizer, epochCache, packageVersion, dateProvider, telemetry, deps.p2pClientDeps);
211
+ // We should really not be modifying the config object
212
+ config.txPublicSetupAllowList = config.txPublicSetupAllowList ?? await getDefaultAllowedSetupFunctions();
213
+ const blockBuilder = new BlockBuilder({
214
+ ...config,
215
+ l1GenesisTime,
216
+ slotDuration: Number(slotDuration)
217
+ }, worldStateSynchronizer, archiver, dateProvider, telemetry);
218
+ // We'll accumulate sentinel watchers here
219
+ const watchers = [];
220
+ // Create validator client if required
122
221
  const validatorClient = createValidatorClient(config, {
123
222
  p2pClient,
124
223
  telemetry,
125
224
  dateProvider,
126
- epochCache
127
- });
128
- // now create the sequencer
129
- const sequencer = config.disableValidator ? undefined : await SequencerClient.new(config, {
130
- ...deps,
131
- validatorClient,
132
- p2pClient,
133
- worldStateSynchronizer,
134
- slasherClient,
135
- contractDataSource: archiver,
136
- l2BlockSource: archiver,
225
+ epochCache,
226
+ blockBuilder,
227
+ blockSource: archiver,
137
228
  l1ToL2MessageSource: archiver,
138
- telemetry,
139
- dateProvider,
140
- blobSinkClient
229
+ keyStoreManager,
230
+ fileStoreBlobUploadClient: fileStoreUploadClient
231
+ });
232
+ // If we have a validator client, register it as a source of offenses for the slasher,
233
+ // and have it register callbacks on the p2p client *before* we start it, otherwise messages
234
+ // like attestations or auths will fail.
235
+ if (validatorClient) {
236
+ watchers.push(validatorClient);
237
+ if (!options.dontStartSequencer) {
238
+ await validatorClient.registerHandlers();
239
+ }
240
+ }
241
+ // If there's no validator client but alwaysReexecuteBlockProposals is enabled,
242
+ // create a BlockProposalHandler to reexecute block proposals for monitoring
243
+ if (!validatorClient && config.alwaysReexecuteBlockProposals) {
244
+ log.info('Setting up block proposal reexecution for monitoring');
245
+ createBlockProposalHandler(config, {
246
+ blockBuilder,
247
+ epochCache,
248
+ blockSource: archiver,
249
+ l1ToL2MessageSource: archiver,
250
+ p2pClient,
251
+ dateProvider,
252
+ telemetry
253
+ }).registerForReexecution(p2pClient);
254
+ }
255
+ // Start world state and wait for it to sync to the archiver.
256
+ await worldStateSynchronizer.start();
257
+ // Start p2p. Note that it depends on world state to be running.
258
+ await p2pClient.start();
259
+ const validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
260
+ if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
261
+ watchers.push(validatorsSentinel);
262
+ }
263
+ let epochPruneWatcher;
264
+ if (config.slashPrunePenalty > 0n || config.slashDataWithholdingPenalty > 0n) {
265
+ epochPruneWatcher = new EpochPruneWatcher(archiver, archiver, epochCache, p2pClient.getTxProvider(), blockBuilder, config);
266
+ watchers.push(epochPruneWatcher);
267
+ }
268
+ // We assume we want to slash for invalid attestations unless all max penalties are set to 0
269
+ let attestationsBlockWatcher;
270
+ if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
271
+ attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
272
+ watchers.push(attestationsBlockWatcher);
273
+ }
274
+ // Start p2p-related services once the archiver has completed sync
275
+ void archiver.waitForInitialSync().then(async ()=>{
276
+ await p2pClient.start();
277
+ await validatorsSentinel?.start();
278
+ await epochPruneWatcher?.start();
279
+ await attestationsBlockWatcher?.start();
280
+ log.info(`All p2p services started`);
281
+ }).catch((err)=>log.error('Failed to start p2p services after archiver sync', err));
282
+ // Validator enabled, create/start relevant service
283
+ let sequencer;
284
+ let slasherClient;
285
+ if (!config.disableValidator && validatorClient) {
286
+ // We create a slasher only if we have a sequencer, since all slashing actions go through the sequencer publisher
287
+ // as they are executed when the node is selected as proposer.
288
+ const validatorAddresses = keyStoreManager ? NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager).getAddresses() : [];
289
+ slasherClient = await createSlasher(config, config.l1Contracts, getPublicClient(config), watchers, dateProvider, epochCache, validatorAddresses, undefined);
290
+ await slasherClient.start();
291
+ const l1TxUtils = config.publisherForwarderAddress ? await createForwarderL1TxUtilsFromEthSigner(publicClient, keyStoreManager.createAllValidatorPublisherSigners(), config.publisherForwarderAddress, {
292
+ ...config,
293
+ scope: 'sequencer'
294
+ }, {
295
+ telemetry,
296
+ logger: log.createChild('l1-tx-utils'),
297
+ dateProvider
298
+ }) : await createL1TxUtilsWithBlobsFromEthSigner(publicClient, keyStoreManager.createAllValidatorPublisherSigners(), {
299
+ ...config,
300
+ scope: 'sequencer'
301
+ }, {
302
+ telemetry,
303
+ logger: log.createChild('l1-tx-utils'),
304
+ dateProvider
305
+ });
306
+ // Create and start the sequencer client
307
+ const checkpointsBuilder = new CheckpointsBuilder({
308
+ ...config,
309
+ l1GenesisTime,
310
+ slotDuration: Number(slotDuration)
311
+ }, archiver, dateProvider, telemetry);
312
+ sequencer = await SequencerClient.new(config, {
313
+ ...deps,
314
+ epochCache,
315
+ l1TxUtils,
316
+ validatorClient,
317
+ p2pClient,
318
+ worldStateSynchronizer,
319
+ slasherClient,
320
+ checkpointsBuilder,
321
+ l2BlockSource: archiver,
322
+ l1ToL2MessageSource: archiver,
323
+ telemetry,
324
+ dateProvider,
325
+ blobClient,
326
+ nodeKeyStore: keyStoreManager
327
+ });
328
+ }
329
+ if (!options.dontStartSequencer && sequencer) {
330
+ await sequencer.start();
331
+ log.verbose(`Sequencer started`);
332
+ } else if (sequencer) {
333
+ log.warn(`Sequencer created but not started`);
334
+ }
335
+ const globalVariableBuilder = new GlobalVariableBuilder({
336
+ ...config,
337
+ rollupVersion: BigInt(config.rollupVersion),
338
+ l1GenesisTime,
339
+ slotDuration: Number(slotDuration)
141
340
  });
142
- return new AztecNodeService(config, p2pClient, archiver, archiver, archiver, archiver, archiver, worldStateSynchronizer, sequencer, ethereumChain.chainInfo.id, config.version, new GlobalVariableBuilder(config), proofVerifier, telemetry, log);
341
+ return new AztecNodeService(config, p2pClient, archiver, archiver, archiver, archiver, worldStateSynchronizer, sequencer, slasherClient, validatorsSentinel, epochPruneWatcher, ethereumChain.chainInfo.id, config.rollupVersion, globalVariableBuilder, epochCache, packageVersion, proofVerifier, telemetry, log);
143
342
  }
144
343
  /**
145
344
  * Returns the sequencer client instance.
@@ -165,6 +364,9 @@ import { NodeMetrics } from './node_metrics.js';
165
364
  getEncodedEnr() {
166
365
  return Promise.resolve(this.p2pClient.getEnr()?.encodeTxt());
167
366
  }
367
+ async getAllowedPublicSetup() {
368
+ return this.config.txPublicSetupAllowList ?? await getDefaultAllowedSetupFunctions();
369
+ }
168
370
  /**
169
371
  * Method to determine if the node is ready to accept transactions.
170
372
  * @returns - Flag indicating the readiness for tx submission.
@@ -172,7 +374,7 @@ import { NodeMetrics } from './node_metrics.js';
172
374
  return Promise.resolve(this.p2pClient.isReady() ?? false);
173
375
  }
174
376
  async getNodeInfo() {
175
- const [nodeVersion, protocolVersion, chainId, enr, contractAddresses, protocolContractAddresses] = await Promise.all([
377
+ const [nodeVersion, rollupVersion, chainId, enr, contractAddresses, protocolContractAddresses] = await Promise.all([
176
378
  this.getNodeVersion(),
177
379
  this.getVersion(),
178
380
  this.getChainId(),
@@ -183,7 +385,7 @@ import { NodeMetrics } from './node_metrics.js';
183
385
  const nodeInfo = {
184
386
  nodeVersion,
185
387
  l1ChainId: chainId,
186
- protocolVersion,
388
+ rollupVersion,
187
389
  enr,
188
390
  l1ContractAddresses: contractAddresses,
189
391
  protocolContractAddresses: protocolContractAddresses
@@ -195,7 +397,24 @@ import { NodeMetrics } from './node_metrics.js';
195
397
  * @param number - The block number being requested.
196
398
  * @returns The requested block.
197
399
  */ async getBlock(number) {
198
- return await this.blockSource.getBlock(number);
400
+ const blockNumber = number === 'latest' ? await this.getBlockNumber() : number;
401
+ return await this.blockSource.getBlock(blockNumber);
402
+ }
403
+ /**
404
+ * Get a block specified by its hash.
405
+ * @param blockHash - The block hash being requested.
406
+ * @returns The requested block.
407
+ */ async getBlockByHash(blockHash) {
408
+ const publishedBlock = await this.blockSource.getPublishedBlockByHash(blockHash);
409
+ return publishedBlock?.block;
410
+ }
411
+ /**
412
+ * Get a block specified by its archive root.
413
+ * @param archive - The archive root being requested.
414
+ * @returns The requested block.
415
+ */ async getBlockByArchive(archive) {
416
+ const publishedBlock = await this.blockSource.getPublishedBlockByArchive(archive);
417
+ return publishedBlock?.block;
199
418
  }
200
419
  /**
201
420
  * Method to request blocks. Will attempt to return all requested blocks but will return only those available.
@@ -205,14 +424,26 @@ import { NodeMetrics } from './node_metrics.js';
205
424
  */ async getBlocks(from, limit) {
206
425
  return await this.blockSource.getBlocks(from, limit) ?? [];
207
426
  }
427
+ async getPublishedBlocks(from, limit) {
428
+ return await this.blockSource.getPublishedBlocks(from, limit) ?? [];
429
+ }
208
430
  /**
209
431
  * Method to fetch the current base fees.
210
432
  * @returns The current base fees.
211
433
  */ async getCurrentBaseFees() {
212
434
  return await this.globalVariableBuilder.getCurrentBaseFees();
213
435
  }
436
+ async getMaxPriorityFees() {
437
+ for await (const tx of this.p2pClient.iteratePendingTxs()){
438
+ return tx.getGasSettings().maxPriorityFeesPerGas;
439
+ }
440
+ return GasFees.from({
441
+ feePerDaGas: 0n,
442
+ feePerL2Gas: 0n
443
+ });
444
+ }
214
445
  /**
215
- * Method to fetch the current block number.
446
+ * Method to fetch the latest block number synchronized by the node.
216
447
  * @returns The block number.
217
448
  */ async getBlockNumber() {
218
449
  return await this.blockSource.getBlockNumber();
@@ -238,44 +469,17 @@ import { NodeMetrics } from './node_metrics.js';
238
469
  */ getChainId() {
239
470
  return Promise.resolve(this.l1ChainId);
240
471
  }
241
- async getContractClass(id) {
242
- const klazz = await this.contractDataSource.getContractClass(id);
243
- // TODO(#10007): Remove this check. This is needed only because we're manually registering
244
- // some contracts in the archiver so they are available to all nodes (see `registerCommonContracts`
245
- // in `archiver/src/factory.ts`), but we still want clients to send the registration tx in order
246
- // to emit the corresponding nullifier, which is now being checked. Note that this method
247
- // is only called by the PXE to check if a contract is publicly registered.
248
- if (klazz) {
249
- const classNullifier = await siloNullifier(AztecAddress.fromNumber(REGISTERER_CONTRACT_ADDRESS), id);
250
- const worldState = await this.#getWorldState('latest');
251
- const [index] = await worldState.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [
252
- classNullifier.toBuffer()
253
- ]);
254
- this.log.debug(`Registration nullifier ${classNullifier} for contract class ${id} found at index ${index}`);
255
- if (index === undefined) {
256
- return undefined;
257
- }
258
- }
259
- return klazz;
472
+ getContractClass(id) {
473
+ return this.contractDataSource.getContractClass(id);
260
474
  }
261
475
  getContract(address) {
262
476
  return this.contractDataSource.getContract(address);
263
477
  }
264
- /**
265
- * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
266
- * @param from - The block number from which to begin retrieving logs.
267
- * @param limit - The maximum number of blocks to retrieve logs from.
268
- * @returns An array of private logs from the specified range of blocks.
269
- */ getPrivateLogs(from, limit) {
270
- return this.logsSource.getPrivateLogs(from, limit);
478
+ getPrivateLogsByTags(tags) {
479
+ return this.logsSource.getPrivateLogsByTags(tags);
271
480
  }
272
- /**
273
- * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
274
- * @param tags - The tags to filter the logs by.
275
- * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
276
- * that tag.
277
- */ getLogsByTags(tags) {
278
- return this.logsSource.getLogsByTags(tags);
481
+ getPublicLogsByTagsFromContract(contractAddress, tags) {
482
+ return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags);
279
483
  }
280
484
  /**
281
485
  * Gets public logs based on the provided filter.
@@ -295,18 +499,19 @@ import { NodeMetrics } from './node_metrics.js';
295
499
  * Method to submit a transaction to the p2p pool.
296
500
  * @param tx - The transaction to be submitted.
297
501
  */ async sendTx(tx) {
502
+ await this.#sendTx(tx);
503
+ }
504
+ async #sendTx(tx) {
298
505
  const timer = new Timer();
299
- const txHash = (await tx.getTxHash()).toString();
506
+ const txHash = tx.getTxHash().toString();
300
507
  const valid = await this.isValidTx(tx);
301
508
  if (valid.result !== 'valid') {
302
509
  const reason = valid.reason.join(', ');
303
510
  this.metrics.receivedTx(timer.ms(), false);
304
- this.log.warn(`Invalid tx ${txHash}: ${reason}`, {
511
+ this.log.warn(`Received invalid tx ${txHash}: ${reason}`, {
305
512
  txHash
306
513
  });
307
- // TODO(#10967): Throw when receiving an invalid tx instead of just returning
308
- // throw new Error(`Invalid tx: ${reason}`);
309
- return;
514
+ throw new Error(`Invalid tx: ${reason}`);
310
515
  }
311
516
  await this.p2pClient.sendTx(tx);
312
517
  this.metrics.receivedTx(timer.ms(), true);
@@ -334,63 +539,98 @@ import { NodeMetrics } from './node_metrics.js';
334
539
  /**
335
540
  * Method to stop the aztec node.
336
541
  */ async stop() {
337
- this.log.info(`Stopping`);
338
- await this.sequencer?.stop();
339
- await this.p2pClient.stop();
340
- await this.worldStateSynchronizer.stop();
542
+ this.log.info(`Stopping Aztec Node`);
543
+ await tryStop(this.validatorsSentinel);
544
+ await tryStop(this.epochPruneWatcher);
545
+ await tryStop(this.slasherClient);
546
+ await tryStop(this.proofVerifier);
547
+ await tryStop(this.sequencer);
548
+ await tryStop(this.p2pClient);
549
+ await tryStop(this.worldStateSynchronizer);
341
550
  await tryStop(this.blockSource);
342
- await this.telemetry.stop();
343
- this.log.info(`Stopped`);
551
+ await tryStop(this.telemetry);
552
+ this.log.info(`Stopped Aztec Node`);
344
553
  }
345
554
  /**
346
555
  * Method to retrieve pending txs.
556
+ * @param limit - The number of items to returns
557
+ * @param after - The last known pending tx. Used for pagination
347
558
  * @returns - The pending txs.
348
- */ getPendingTxs() {
349
- return this.p2pClient.getPendingTxs();
559
+ */ getPendingTxs(limit, after) {
560
+ return this.p2pClient.getPendingTxs(limit, after);
350
561
  }
351
- async getPendingTxCount() {
352
- const pendingTxs = await this.getPendingTxs();
353
- return pendingTxs.length;
562
+ getPendingTxCount() {
563
+ return this.p2pClient.getPendingTxCount();
354
564
  }
355
565
  /**
356
- * Method to retrieve a single tx from the mempool or unfinalised chain.
566
+ * Method to retrieve a single tx from the mempool or unfinalized chain.
357
567
  * @param txHash - The transaction hash to return.
358
568
  * @returns - The tx if it exists.
359
569
  */ getTxByHash(txHash) {
360
570
  return Promise.resolve(this.p2pClient.getTxByHashFromPool(txHash));
361
571
  }
362
572
  /**
363
- * Method to retrieve txs from the mempool or unfinalised chain.
573
+ * Method to retrieve txs from the mempool or unfinalized chain.
364
574
  * @param txHash - The transaction hash to return.
365
575
  * @returns - The txs if it exists.
366
576
  */ async getTxsByHash(txHashes) {
367
577
  return compactArray(await Promise.all(txHashes.map((txHash)=>this.getTxByHash(txHash))));
368
578
  }
369
579
  /**
370
- * Find the indexes of the given leaves in the given tree.
371
- * @param blockNumber - The block number at which to get the data or 'latest' for latest data
580
+ * Find the indexes of the given leaves in the given tree along with a block metadata pointing to the block in which
581
+ * the leaves were inserted.
582
+ * @param blockNumber - The block number at which to get the data or 'latest' for latest data.
372
583
  * @param treeId - The tree to search in.
373
- * @param leafValue - The values to search for
374
- * @returns The indexes of the given leaves in the given tree or undefined if not found.
584
+ * @param leafValues - The values to search for.
585
+ * @returns The indices of leaves and the block metadata of a block in which the leaves were inserted.
375
586
  */ async findLeavesIndexes(blockNumber, treeId, leafValues) {
376
587
  const committedDb = await this.#getWorldState(blockNumber);
377
- return await committedDb.findLeafIndices(treeId, leafValues.map((x)=>x.toBuffer()));
378
- }
379
- /**
380
- * Find the block numbers of the given leaf indices in the given tree.
381
- * @param blockNumber - The block number at which to get the data or 'latest' for latest data
382
- * @param treeId - The tree to search in.
383
- * @param leafIndices - The values to search for
384
- * @returns The indexes of the given leaves in the given tree or undefined if not found.
385
- */ async findBlockNumbersForIndexes(blockNumber, treeId, leafIndices) {
386
- const committedDb = await this.#getWorldState(blockNumber);
387
- return await committedDb.getBlockNumbersForLeafIndices(treeId, leafIndices);
388
- }
389
- async findNullifiersIndexesWithBlock(blockNumber, nullifiers) {
390
- if (blockNumber === 'latest') {
391
- blockNumber = await this.getBlockNumber();
588
+ const maybeIndices = await committedDb.findLeafIndices(treeId, leafValues.map((x)=>x.toBuffer()));
589
+ // We filter out undefined values
590
+ const indices = maybeIndices.filter((x)=>x !== undefined);
591
+ // Now we find the block numbers for the indices
592
+ const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, indices);
593
+ // If any of the block numbers are undefined, we throw an error.
594
+ for(let i = 0; i < indices.length; i++){
595
+ if (blockNumbers[i] === undefined) {
596
+ throw new Error(`Block number is undefined for leaf index ${indices[i]} in tree ${MerkleTreeId[treeId]}`);
597
+ }
598
+ }
599
+ // Get unique block numbers in order to optimize num calls to getLeafValue function.
600
+ const uniqueBlockNumbers = [
601
+ ...new Set(blockNumbers.filter((x)=>x !== undefined))
602
+ ];
603
+ // Now we obtain the block hashes from the archive tree by calling await `committedDb.getLeafValue(treeId, index)`
604
+ // (note that block number corresponds to the leaf index in the archive tree).
605
+ const blockHashes = await Promise.all(uniqueBlockNumbers.map((blockNumber)=>{
606
+ return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
607
+ }));
608
+ // If any of the block hashes are undefined, we throw an error.
609
+ for(let i = 0; i < uniqueBlockNumbers.length; i++){
610
+ if (blockHashes[i] === undefined) {
611
+ throw new Error(`Block hash is undefined for block number ${uniqueBlockNumbers[i]}`);
612
+ }
392
613
  }
393
- return this.nullifierSource.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
614
+ // Create DataInBlock objects by combining indices, blockNumbers and blockHashes and return them.
615
+ return maybeIndices.map((index, i)=>{
616
+ if (index === undefined) {
617
+ return undefined;
618
+ }
619
+ const blockNumber = blockNumbers[i];
620
+ if (blockNumber === undefined) {
621
+ return undefined;
622
+ }
623
+ const blockHashIndex = uniqueBlockNumbers.indexOf(blockNumber);
624
+ const blockHash = blockHashes[blockHashIndex];
625
+ if (!blockHash) {
626
+ return undefined;
627
+ }
628
+ return {
629
+ l2BlockNumber: BlockNumber(Number(blockNumber)),
630
+ l2BlockHash: L2BlockHash.fromField(blockHash),
631
+ data: index
632
+ };
633
+ });
394
634
  }
395
635
  /**
396
636
  * Returns a sibling path for the given index in the nullifier tree.
@@ -410,90 +650,58 @@ import { NodeMetrics } from './node_metrics.js';
410
650
  const committedDb = await this.#getWorldState(blockNumber);
411
651
  return committedDb.getSiblingPath(MerkleTreeId.NOTE_HASH_TREE, leafIndex);
412
652
  }
653
+ async getArchiveMembershipWitness(blockNumber, archive) {
654
+ const committedDb = await this.#getWorldState(blockNumber);
655
+ const [pathAndIndex] = await committedDb.findSiblingPaths(MerkleTreeId.ARCHIVE, [
656
+ archive
657
+ ]);
658
+ return pathAndIndex === undefined ? undefined : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
659
+ }
660
+ async getNoteHashMembershipWitness(blockNumber, noteHash) {
661
+ const committedDb = await this.#getWorldState(blockNumber);
662
+ const [pathAndIndex] = await committedDb.findSiblingPaths(MerkleTreeId.NOTE_HASH_TREE, [
663
+ noteHash
664
+ ]);
665
+ return pathAndIndex === undefined ? undefined : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
666
+ }
413
667
  /**
414
668
  * Returns the index and a sibling path for a leaf in the committed l1 to l2 data tree.
415
669
  * @param blockNumber - The block number at which to get the data.
416
670
  * @param l1ToL2Message - The l1ToL2Message to get the index / sibling path for.
417
671
  * @returns A tuple of the index and the sibling path of the L1ToL2Message (undefined if not found).
418
672
  */ async getL1ToL2MessageMembershipWitness(blockNumber, l1ToL2Message) {
419
- const index = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
420
- if (index === undefined) {
673
+ const db = await this.#getWorldState(blockNumber);
674
+ const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [
675
+ l1ToL2Message
676
+ ]);
677
+ if (!witness) {
421
678
  return undefined;
422
679
  }
423
- const committedDb = await this.#getWorldState(blockNumber);
424
- const siblingPath = await committedDb.getSiblingPath(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, index);
680
+ // REFACTOR: Return a MembershipWitness object
425
681
  return [
426
- index,
427
- siblingPath
682
+ witness.index,
683
+ witness.path
428
684
  ];
429
685
  }
686
+ async getL1ToL2MessageBlock(l1ToL2Message) {
687
+ const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
688
+ return messageIndex ? BlockNumber.fromCheckpointNumber(InboxLeaf.checkpointNumberFromIndex(messageIndex)) : undefined;
689
+ }
430
690
  /**
431
691
  * Returns whether an L1 to L2 message is synced by archiver and if it's ready to be included in a block.
432
692
  * @param l1ToL2Message - The L1 to L2 message to check.
433
693
  * @returns Whether the message is synced and ready to be included in a block.
434
694
  */ async isL1ToL2MessageSynced(l1ToL2Message) {
435
- return await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message) !== undefined;
695
+ const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
696
+ return messageIndex !== undefined;
436
697
  }
437
698
  /**
438
- * Returns the index of a l2ToL1Message in a ephemeral l2 to l1 data tree as well as its sibling path.
439
- * @remarks This tree is considered ephemeral because it is created on-demand by: taking all the l2ToL1 messages
440
- * in a single block, and then using them to make a variable depth append-only tree with these messages as leaves.
441
- * The tree is discarded immediately after calculating what we need from it.
442
- * TODO: Handle the case where two messages in the same tx have the same hash.
699
+ * Returns all the L2 to L1 messages in a block.
443
700
  * @param blockNumber - The block number at which to get the data.
444
- * @param l2ToL1Message - The l2ToL1Message get the index / sibling path for.
445
- * @returns A tuple of the index and the sibling path of the L2ToL1Message.
446
- */ async getL2ToL1MessageMembershipWitness(blockNumber, l2ToL1Message) {
701
+ * @returns The L2 to L1 messages (undefined if the block number is not found).
702
+ */ async getL2ToL1Messages(blockNumber) {
447
703
  const block = await this.blockSource.getBlock(blockNumber === 'latest' ? await this.getBlockNumber() : blockNumber);
448
- if (block === undefined) {
449
- throw new Error('Block is not defined');
450
- }
451
- const l2ToL1Messages = block.body.txEffects.map((txEffect)=>txEffect.l2ToL1Msgs);
452
- // Find index of message
453
- let indexOfMsgInSubtree = -1;
454
- const indexOfMsgTx = l2ToL1Messages.findIndex((msgs)=>{
455
- const idx = msgs.findIndex((msg)=>msg.equals(l2ToL1Message));
456
- indexOfMsgInSubtree = Math.max(indexOfMsgInSubtree, idx);
457
- return idx !== -1;
458
- });
459
- if (indexOfMsgTx === -1) {
460
- throw new Error('The L2ToL1Message you are trying to prove inclusion of does not exist');
461
- }
462
- const tempStores = [];
463
- // Construct message subtrees
464
- const l2toL1Subtrees = await Promise.all(l2ToL1Messages.map(async (msgs, i)=>{
465
- const store = openTmpStore(true);
466
- tempStores.push(store);
467
- const treeHeight = msgs.length <= 1 ? 1 : Math.ceil(Math.log2(msgs.length));
468
- const tree = new StandardTree(store, new SHA256Trunc(), `temp_msgs_subtrees_${i}`, treeHeight, 0n, Fr);
469
- await tree.appendLeaves(msgs);
470
- return tree;
471
- }));
472
- // path of the input msg from leaf -> first out hash calculated in base rolllup
473
- const subtreePathOfL2ToL1Message = await l2toL1Subtrees[indexOfMsgTx].getSiblingPath(BigInt(indexOfMsgInSubtree), true);
474
- const numTxs = block.body.txEffects.length;
475
- if (numTxs === 1) {
476
- return [
477
- BigInt(indexOfMsgInSubtree),
478
- subtreePathOfL2ToL1Message
479
- ];
480
- }
481
- const l2toL1SubtreeRoots = l2toL1Subtrees.map((t)=>Fr.fromBuffer(t.getRoot(true)));
482
- const maxTreeHeight = Math.ceil(Math.log2(l2toL1SubtreeRoots.length));
483
- // The root of this tree is the out_hash calculated in Noir => we truncate to match Noir's SHA
484
- const outHashTree = new UnbalancedTree(new SHA256Trunc(), 'temp_outhash_sibling_path', maxTreeHeight, Fr);
485
- await outHashTree.appendLeaves(l2toL1SubtreeRoots);
486
- const pathOfTxInOutHashTree = await outHashTree.getSiblingPath(l2toL1SubtreeRoots[indexOfMsgTx].toBigInt());
487
- // Append subtree path to out hash tree path
488
- const mergedPath = subtreePathOfL2ToL1Message.toBufferArray().concat(pathOfTxInOutHashTree.toBufferArray());
489
- // Append binary index of subtree path to binary index of out hash tree path
490
- const mergedIndex = parseInt(indexOfMsgTx.toString(2).concat(indexOfMsgInSubtree.toString(2).padStart(l2toL1Subtrees[indexOfMsgTx].getDepth(), '0')), 2);
491
- // clear the tmp stores
492
- await Promise.all(tempStores.map((store)=>store.delete()));
493
- return [
494
- BigInt(mergedIndex),
495
- new SiblingPath(mergedPath.length, mergedPath)
496
- ];
704
+ return block?.body.txEffects.map((txEffect)=>txEffect.l2ToL1Msgs);
497
705
  }
498
706
  /**
499
707
  * Returns a sibling path for a leaf in the committed blocks tree.
@@ -520,22 +728,18 @@ import { NodeMetrics } from './node_metrics.js';
520
728
  * @returns The nullifier membership witness (if found).
521
729
  */ async getNullifierMembershipWitness(blockNumber, nullifier) {
522
730
  const db = await this.#getWorldState(blockNumber);
523
- const index = (await db.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [
731
+ const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [
524
732
  nullifier.toBuffer()
525
- ]))[0];
526
- if (!index) {
733
+ ]);
734
+ if (!witness) {
527
735
  return undefined;
528
736
  }
529
- const leafPreimagePromise = db.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
530
- const siblingPathPromise = db.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, BigInt(index));
531
- const [leafPreimage, siblingPath] = await Promise.all([
532
- leafPreimagePromise,
533
- siblingPathPromise
534
- ]);
737
+ const { index, path } = witness;
738
+ const leafPreimage = await db.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
535
739
  if (!leafPreimage) {
536
740
  return undefined;
537
741
  }
538
- return new NullifierMembershipWitness(BigInt(index), leafPreimage, siblingPath);
742
+ return new NullifierMembershipWitness(index, leafPreimage, path);
539
743
  }
540
744
  /**
541
745
  * Returns a low nullifier membership witness for a given nullifier at a given block.
@@ -564,7 +768,7 @@ import { NodeMetrics } from './node_metrics.js';
564
768
  const siblingPath = await committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, BigInt(index));
565
769
  return new NullifierMembershipWitness(BigInt(index), preimageData, siblingPath);
566
770
  }
567
- async getPublicDataTreeWitness(blockNumber, leafSlot) {
771
+ async getPublicDataWitness(blockNumber, leafSlot) {
568
772
  const committedDb = await this.#getWorldState(blockNumber);
569
773
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
570
774
  if (!lowLeafResult) {
@@ -593,35 +797,66 @@ import { NodeMetrics } from './node_metrics.js';
593
797
  return Fr.ZERO;
594
798
  }
595
799
  const preimage = await committedDb.getLeafPreimage(MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index);
596
- return preimage.value;
800
+ return preimage.leaf.value;
597
801
  }
598
802
  /**
599
803
  * Returns the currently committed block header, or the initial header if no blocks have been produced.
600
804
  * @returns The current committed block header.
601
805
  */ async getBlockHeader(blockNumber = 'latest') {
602
- return blockNumber === 0 || blockNumber === 'latest' && await this.blockSource.getBlockNumber() === 0 ? this.worldStateSynchronizer.getCommitted().getInitialHeader() : this.blockSource.getBlockHeader(blockNumber);
806
+ return blockNumber === BlockNumber.ZERO || blockNumber === 'latest' && await this.blockSource.getBlockNumber() === BlockNumber.ZERO ? this.worldStateSynchronizer.getCommitted().getInitialHeader() : this.blockSource.getBlockHeader(blockNumber === 'latest' ? blockNumber : blockNumber);
807
+ }
808
+ /**
809
+ * Get a block header specified by its hash.
810
+ * @param blockHash - The block hash being requested.
811
+ * @returns The requested block header.
812
+ */ async getBlockHeaderByHash(blockHash) {
813
+ return await this.blockSource.getBlockHeaderByHash(blockHash);
814
+ }
815
+ /**
816
+ * Get a block header specified by its archive root.
817
+ * @param archive - The archive root being requested.
818
+ * @returns The requested block header.
819
+ */ async getBlockHeaderByArchive(archive) {
820
+ return await this.blockSource.getBlockHeaderByArchive(archive);
603
821
  }
604
822
  /**
605
823
  * Simulates the public part of a transaction with the current state.
606
824
  * @param tx - The transaction to simulate.
607
825
  **/ async simulatePublicCalls(tx, skipFeeEnforcement = false) {
608
- const txHash = await tx.getTxHash();
609
- const blockNumber = await this.blockSource.getBlockNumber() + 1;
826
+ // Check total gas limit for simulation
827
+ const gasSettings = tx.data.constants.txContext.gasSettings;
828
+ const txGasLimit = gasSettings.gasLimits.l2Gas;
829
+ const teardownGasLimit = gasSettings.teardownGasLimits.l2Gas;
830
+ if (txGasLimit + teardownGasLimit > this.config.rpcSimulatePublicMaxGasLimit) {
831
+ throw new BadRequestError(`Transaction total gas limit ${txGasLimit + teardownGasLimit} (${txGasLimit} + ${teardownGasLimit}) exceeds maximum gas limit ${this.config.rpcSimulatePublicMaxGasLimit} for simulation`);
832
+ }
833
+ const txHash = tx.getTxHash();
834
+ const blockNumber = BlockNumber(await this.blockSource.getBlockNumber() + 1);
610
835
  // If sequencer is not initialized, we just set these values to zero for simulation.
611
- const coinbase = this.sequencer?.coinbase || EthAddress.ZERO;
612
- const feeRecipient = this.sequencer?.feeRecipient || AztecAddress.ZERO;
613
- const newGlobalVariables = await this.globalVariableBuilder.buildGlobalVariables(new Fr(blockNumber), coinbase, feeRecipient);
836
+ const coinbase = EthAddress.ZERO;
837
+ const feeRecipient = AztecAddress.ZERO;
838
+ const newGlobalVariables = await this.globalVariableBuilder.buildGlobalVariables(blockNumber, coinbase, feeRecipient);
614
839
  const publicProcessorFactory = new PublicProcessorFactory(this.contractDataSource, new DateProvider(), this.telemetry);
615
- const fork = await this.worldStateSynchronizer.fork();
616
840
  this.log.verbose(`Simulating public calls for tx ${txHash}`, {
617
841
  globalVariables: newGlobalVariables.toInspect(),
618
842
  txHash,
619
843
  blockNumber
620
844
  });
845
+ const merkleTreeFork = await this.worldStateSynchronizer.fork();
621
846
  try {
622
- const processor = publicProcessorFactory.create(fork, newGlobalVariables, skipFeeEnforcement);
847
+ const config = PublicSimulatorConfig.from({
848
+ skipFeeEnforcement,
849
+ collectDebugLogs: true,
850
+ collectHints: false,
851
+ collectCallMetadata: true,
852
+ collectStatistics: false,
853
+ collectionLimits: CollectionLimitsConfig.from({
854
+ maxDebugLogMemoryReads: this.config.rpcSimulatePublicMaxDebugLogMemoryReads
855
+ })
856
+ });
857
+ const processor = publicProcessorFactory.create(merkleTreeFork, newGlobalVariables, config);
623
858
  // REFACTOR: Consider merging ProcessReturnValues into ProcessedTx
624
- const [processedTxs, failedTxs, returns] = await processor.process([
859
+ const [processedTxs, failedTxs, _usedTxs, returns] = await processor.process([
625
860
  tx
626
861
  ]);
627
862
  // REFACTOR: Consider returning the error rather than throwing
@@ -632,30 +867,47 @@ import { NodeMetrics } from './node_metrics.js';
632
867
  throw failedTxs[0].error;
633
868
  }
634
869
  const [processedTx] = processedTxs;
635
- return new PublicSimulationOutput(processedTx.revertReason, processedTx.constants, processedTx.txEffect, returns, processedTx.gasUsed);
870
+ return new PublicSimulationOutput(processedTx.revertReason, processedTx.globalVariables, processedTx.txEffect, returns, processedTx.gasUsed);
636
871
  } finally{
637
- await fork.close();
872
+ await merkleTreeFork.close();
638
873
  }
639
874
  }
640
875
  async isValidTx(tx, { isSimulation, skipFeeEnforcement } = {}) {
641
- const blockNumber = await this.blockSource.getBlockNumber() + 1;
642
876
  const db = this.worldStateSynchronizer.getCommitted();
643
877
  const verifier = isSimulation ? undefined : this.proofVerifier;
878
+ // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
879
+ const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
880
+ const blockNumber = BlockNumber(await this.blockSource.getBlockNumber() + 1);
644
881
  const validator = createValidatorForAcceptingTxs(db, this.contractDataSource, verifier, {
882
+ timestamp: nextSlotTimestamp,
645
883
  blockNumber,
646
884
  l1ChainId: this.l1ChainId,
647
- setupAllowList: this.config.allowedInSetup ?? await getDefaultAllowedSetupFunctions(),
885
+ rollupVersion: this.version,
886
+ setupAllowList: this.config.txPublicSetupAllowList ?? await getDefaultAllowedSetupFunctions(),
648
887
  gasFees: await this.getCurrentBaseFees(),
649
- skipFeeEnforcement
888
+ skipFeeEnforcement,
889
+ txsPermitted: !this.config.disableTransactions
650
890
  });
651
891
  return await validator.validateTx(tx);
652
892
  }
893
+ getConfig() {
894
+ const schema = AztecNodeAdminConfigSchema;
895
+ const keys = schema.keyof().options;
896
+ return Promise.resolve(pick(this.config, ...keys));
897
+ }
653
898
  async setConfig(config) {
654
899
  const newConfig = {
655
900
  ...this.config,
656
901
  ...config
657
902
  };
658
- await this.sequencer?.updateSequencerConfig(config);
903
+ this.sequencer?.updateConfig(config);
904
+ this.slasherClient?.updateConfig(config);
905
+ this.validatorsSentinel?.updateConfig(config);
906
+ await this.p2pClient.updateP2PConfig(config);
907
+ const archiver = this.blockSource;
908
+ if ('updateConfig' in archiver) {
909
+ archiver.updateConfig(config);
910
+ }
659
911
  if (newConfig.realProofs !== this.config.realProofs) {
660
912
  this.proofVerifier = config.realProofs ? await BBCircuitVerifier.new(newConfig) : new TestCircuitVerifier();
661
913
  }
@@ -663,27 +915,120 @@ import { NodeMetrics } from './node_metrics.js';
663
915
  }
664
916
  getProtocolContractAddresses() {
665
917
  return Promise.resolve({
666
- classRegisterer: ProtocolContractAddress.ContractClassRegisterer,
918
+ classRegistry: ProtocolContractAddress.ContractClassRegistry,
667
919
  feeJuice: ProtocolContractAddress.FeeJuice,
668
- instanceDeployer: ProtocolContractAddress.ContractInstanceDeployer,
920
+ instanceRegistry: ProtocolContractAddress.ContractInstanceRegistry,
669
921
  multiCallEntrypoint: ProtocolContractAddress.MultiCallEntrypoint
670
922
  });
671
923
  }
672
- // TODO(#10007): Remove this method
673
- addContractClass(contractClass) {
674
- this.log.info(`Adding contract class via API ${contractClass.id}`);
675
- return this.contractDataSource.addContractClass(contractClass);
924
+ registerContractFunctionSignatures(signatures) {
925
+ return this.contractDataSource.registerContractFunctionSignatures(signatures);
926
+ }
927
+ getValidatorsStats() {
928
+ return this.validatorsSentinel?.computeStats() ?? Promise.resolve({
929
+ stats: {},
930
+ slotWindow: 0
931
+ });
676
932
  }
677
- registerContractFunctionSignatures(_address, signatures) {
678
- return this.contractDataSource.registerContractFunctionSignatures(_address, signatures);
933
+ getValidatorStats(validatorAddress, fromSlot, toSlot) {
934
+ return this.validatorsSentinel?.getValidatorStats(validatorAddress, fromSlot, toSlot) ?? Promise.resolve(undefined);
679
935
  }
680
- flushTxs() {
681
- if (!this.sequencer) {
682
- throw new Error(`Sequencer is not initialized`);
936
+ async startSnapshotUpload(location) {
937
+ // Note that we are forcefully casting the blocksource as an archiver
938
+ // We break support for archiver running remotely to the node
939
+ const archiver = this.blockSource;
940
+ if (!('backupTo' in archiver)) {
941
+ this.metrics.recordSnapshotError();
942
+ throw new Error('Archiver implementation does not support backups. Cannot generate snapshot.');
943
+ }
944
+ // Test that the archiver has done an initial sync.
945
+ if (!archiver.isInitialSyncComplete()) {
946
+ this.metrics.recordSnapshotError();
947
+ throw new Error(`Archiver initial sync not complete. Cannot start snapshot.`);
948
+ }
949
+ // And it has an L2 block hash
950
+ const l2BlockHash = await archiver.getL2Tips().then((tips)=>tips.latest.hash);
951
+ if (!l2BlockHash) {
952
+ this.metrics.recordSnapshotError();
953
+ throw new Error(`Archiver has no latest L2 block hash downloaded. Cannot start snapshot.`);
954
+ }
955
+ if (this.isUploadingSnapshot) {
956
+ this.metrics.recordSnapshotError();
957
+ throw new Error(`Snapshot upload already in progress. Cannot start another one until complete.`);
683
958
  }
684
- this.sequencer.flush();
959
+ // Do not wait for the upload to be complete to return to the caller, but flag that an operation is in progress
960
+ this.isUploadingSnapshot = true;
961
+ const timer = new Timer();
962
+ void uploadSnapshot(location, this.blockSource, this.worldStateSynchronizer, this.config, this.log).then(()=>{
963
+ this.isUploadingSnapshot = false;
964
+ this.metrics.recordSnapshot(timer.ms());
965
+ }).catch((err)=>{
966
+ this.isUploadingSnapshot = false;
967
+ this.metrics.recordSnapshotError();
968
+ this.log.error(`Error uploading snapshot: ${err}`);
969
+ });
685
970
  return Promise.resolve();
686
971
  }
972
+ async rollbackTo(targetBlock, force) {
973
+ const archiver = this.blockSource;
974
+ if (!('rollbackTo' in archiver)) {
975
+ throw new Error('Archiver implementation does not support rollbacks.');
976
+ }
977
+ const finalizedBlock = await archiver.getL2Tips().then((tips)=>tips.finalized.number);
978
+ if (targetBlock < finalizedBlock) {
979
+ if (force) {
980
+ this.log.warn(`Clearing world state database to allow rolling back behind finalized block ${finalizedBlock}`);
981
+ await this.worldStateSynchronizer.clear();
982
+ await this.p2pClient.clear();
983
+ } else {
984
+ throw new Error(`Cannot rollback to block ${targetBlock} as it is before finalized ${finalizedBlock}`);
985
+ }
986
+ }
987
+ try {
988
+ this.log.info(`Pausing archiver and world state sync to start rollback`);
989
+ await archiver.stop();
990
+ await this.worldStateSynchronizer.stopSync();
991
+ const currentBlock = await archiver.getBlockNumber();
992
+ const blocksToUnwind = currentBlock - targetBlock;
993
+ this.log.info(`Unwinding ${count(blocksToUnwind, 'block')} from L2 block ${currentBlock} to ${targetBlock}`);
994
+ await archiver.rollbackTo(targetBlock);
995
+ this.log.info(`Unwinding complete.`);
996
+ } catch (err) {
997
+ this.log.error(`Error during rollback`, err);
998
+ throw err;
999
+ } finally{
1000
+ this.log.info(`Resuming world state and archiver sync.`);
1001
+ this.worldStateSynchronizer.resumeSync();
1002
+ archiver.resume();
1003
+ }
1004
+ }
1005
+ async pauseSync() {
1006
+ this.log.info(`Pausing archiver and world state sync`);
1007
+ await this.blockSource.stop();
1008
+ await this.worldStateSynchronizer.stopSync();
1009
+ }
1010
+ resumeSync() {
1011
+ this.log.info(`Resuming world state and archiver sync.`);
1012
+ this.worldStateSynchronizer.resumeSync();
1013
+ this.blockSource.resume();
1014
+ return Promise.resolve();
1015
+ }
1016
+ getSlashPayloads() {
1017
+ if (!this.slasherClient) {
1018
+ throw new Error(`Slasher client not enabled`);
1019
+ }
1020
+ return this.slasherClient.getSlashPayloads();
1021
+ }
1022
+ getSlashOffenses(round) {
1023
+ if (!this.slasherClient) {
1024
+ throw new Error(`Slasher client not enabled`);
1025
+ }
1026
+ if (round === 'all') {
1027
+ return this.slasherClient.getPendingOffenses();
1028
+ } else {
1029
+ return this.slasherClient.gatherOffensesForRound(round === 'current' ? undefined : BigInt(round));
1030
+ }
1031
+ }
687
1032
  /**
688
1033
  * Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
689
1034
  * @param blockNumber - The block number at which to get the data.
@@ -692,7 +1037,7 @@ import { NodeMetrics } from './node_metrics.js';
692
1037
  if (typeof blockNumber === 'number' && blockNumber < INITIAL_L2_BLOCK_NUM - 1) {
693
1038
  throw new Error('Invalid block number to get world state for: ' + blockNumber);
694
1039
  }
695
- let blockSyncedTo = 0;
1040
+ let blockSyncedTo = BlockNumber.ZERO;
696
1041
  try {
697
1042
  // Attempt to sync the world state if necessary
698
1043
  blockSyncedTo = await this.#syncWorldState();
@@ -715,11 +1060,11 @@ import { NodeMetrics } from './node_metrics.js';
715
1060
  * @returns A promise that fulfils once the world state is synced
716
1061
  */ async #syncWorldState() {
717
1062
  const blockSourceHeight = await this.blockSource.getBlockNumber();
718
- return this.worldStateSynchronizer.syncImmediate(blockSourceHeight);
1063
+ return await this.worldStateSynchronizer.syncImmediate(blockSourceHeight);
719
1064
  }
720
1065
  }
721
1066
  _ts_decorate([
722
- trackSpan('AztecNodeService.simulatePublicCalls', async (tx)=>({
723
- [Attributes.TX_HASH]: (await tx.getTxHash()).toString()
1067
+ trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
1068
+ [Attributes.TX_HASH]: tx.getTxHash().toString()
724
1069
  }))
725
1070
  ], AztecNodeService.prototype, "simulatePublicCalls", null);