@aztec/aztec-node 0.0.0-test.1 → 0.0.1-commit.21caa21

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