@aztec/aztec-node 0.0.0-test.0 → 0.0.1-commit.023c3e5

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 +20 -6
  7. package/dest/aztec-node/server.d.ts +111 -141
  8. package/dest/aztec-node/server.d.ts.map +1 -1
  9. package/dest/aztec-node/server.js +1082 -347
  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 +93 -0
  25. package/dest/sentinel/sentinel.d.ts.map +1 -0
  26. package/dest/sentinel/sentinel.js +403 -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 +46 -35
  34. package/src/aztec-node/config.ts +132 -25
  35. package/src/aztec-node/node_metrics.ts +23 -6
  36. package/src/aztec-node/server.ts +899 -449
  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 +31 -0
  41. package/src/sentinel/index.ts +8 -0
  42. package/src/sentinel/sentinel.ts +510 -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
@@ -1,38 +1,422 @@
1
- function _ts_decorate(decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1
+ function applyDecs2203RFactory() {
2
+ function createAddInitializerMethod(initializers, decoratorFinishedRef) {
3
+ return function addInitializer(initializer) {
4
+ assertNotFinished(decoratorFinishedRef, "addInitializer");
5
+ assertCallable(initializer, "An initializer");
6
+ initializers.push(initializer);
7
+ };
8
+ }
9
+ function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
10
+ var kindStr;
11
+ switch(kind){
12
+ case 1:
13
+ kindStr = "accessor";
14
+ break;
15
+ case 2:
16
+ kindStr = "method";
17
+ break;
18
+ case 3:
19
+ kindStr = "getter";
20
+ break;
21
+ case 4:
22
+ kindStr = "setter";
23
+ break;
24
+ default:
25
+ kindStr = "field";
26
+ }
27
+ var ctx = {
28
+ kind: kindStr,
29
+ name: isPrivate ? "#" + name : name,
30
+ static: isStatic,
31
+ private: isPrivate,
32
+ metadata: metadata
33
+ };
34
+ var decoratorFinishedRef = {
35
+ v: false
36
+ };
37
+ ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
38
+ var get, set;
39
+ if (kind === 0) {
40
+ if (isPrivate) {
41
+ get = desc.get;
42
+ set = desc.set;
43
+ } else {
44
+ get = function() {
45
+ return this[name];
46
+ };
47
+ set = function(v) {
48
+ this[name] = v;
49
+ };
50
+ }
51
+ } else if (kind === 2) {
52
+ get = function() {
53
+ return desc.value;
54
+ };
55
+ } else {
56
+ if (kind === 1 || kind === 3) {
57
+ get = function() {
58
+ return desc.get.call(this);
59
+ };
60
+ }
61
+ if (kind === 1 || kind === 4) {
62
+ set = function(v) {
63
+ desc.set.call(this, v);
64
+ };
65
+ }
66
+ }
67
+ ctx.access = get && set ? {
68
+ get: get,
69
+ set: set
70
+ } : get ? {
71
+ get: get
72
+ } : {
73
+ set: set
74
+ };
75
+ try {
76
+ return dec(value, ctx);
77
+ } finally{
78
+ decoratorFinishedRef.v = true;
79
+ }
80
+ }
81
+ function assertNotFinished(decoratorFinishedRef, fnName) {
82
+ if (decoratorFinishedRef.v) {
83
+ throw new Error("attempted to call " + fnName + " after decoration was finished");
84
+ }
85
+ }
86
+ function assertCallable(fn, hint) {
87
+ if (typeof fn !== "function") {
88
+ throw new TypeError(hint + " must be a function");
89
+ }
90
+ }
91
+ function assertValidReturnValue(kind, value) {
92
+ var type = typeof value;
93
+ if (kind === 1) {
94
+ if (type !== "object" || value === null) {
95
+ throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
96
+ }
97
+ if (value.get !== undefined) {
98
+ assertCallable(value.get, "accessor.get");
99
+ }
100
+ if (value.set !== undefined) {
101
+ assertCallable(value.set, "accessor.set");
102
+ }
103
+ if (value.init !== undefined) {
104
+ assertCallable(value.init, "accessor.init");
105
+ }
106
+ } else if (type !== "function") {
107
+ var hint;
108
+ if (kind === 0) {
109
+ hint = "field";
110
+ } else if (kind === 10) {
111
+ hint = "class";
112
+ } else {
113
+ hint = "method";
114
+ }
115
+ throw new TypeError(hint + " decorators must return a function or void 0");
116
+ }
117
+ }
118
+ function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
119
+ var decs = decInfo[0];
120
+ var desc, init, value;
121
+ if (isPrivate) {
122
+ if (kind === 0 || kind === 1) {
123
+ desc = {
124
+ get: decInfo[3],
125
+ set: decInfo[4]
126
+ };
127
+ } else if (kind === 3) {
128
+ desc = {
129
+ get: decInfo[3]
130
+ };
131
+ } else if (kind === 4) {
132
+ desc = {
133
+ set: decInfo[3]
134
+ };
135
+ } else {
136
+ desc = {
137
+ value: decInfo[3]
138
+ };
139
+ }
140
+ } else if (kind !== 0) {
141
+ desc = Object.getOwnPropertyDescriptor(base, name);
142
+ }
143
+ if (kind === 1) {
144
+ value = {
145
+ get: desc.get,
146
+ set: desc.set
147
+ };
148
+ } else if (kind === 2) {
149
+ value = desc.value;
150
+ } else if (kind === 3) {
151
+ value = desc.get;
152
+ } else if (kind === 4) {
153
+ value = desc.set;
154
+ }
155
+ var newValue, get, set;
156
+ if (typeof decs === "function") {
157
+ newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
158
+ if (newValue !== void 0) {
159
+ assertValidReturnValue(kind, newValue);
160
+ if (kind === 0) {
161
+ init = newValue;
162
+ } else if (kind === 1) {
163
+ init = newValue.init;
164
+ get = newValue.get || value.get;
165
+ set = newValue.set || value.set;
166
+ value = {
167
+ get: get,
168
+ set: set
169
+ };
170
+ } else {
171
+ value = newValue;
172
+ }
173
+ }
174
+ } else {
175
+ for(var i = decs.length - 1; i >= 0; i--){
176
+ var dec = decs[i];
177
+ newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
178
+ if (newValue !== void 0) {
179
+ assertValidReturnValue(kind, newValue);
180
+ var newInit;
181
+ if (kind === 0) {
182
+ newInit = newValue;
183
+ } else if (kind === 1) {
184
+ newInit = newValue.init;
185
+ get = newValue.get || value.get;
186
+ set = newValue.set || value.set;
187
+ value = {
188
+ get: get,
189
+ set: set
190
+ };
191
+ } else {
192
+ value = newValue;
193
+ }
194
+ if (newInit !== void 0) {
195
+ if (init === void 0) {
196
+ init = newInit;
197
+ } else if (typeof init === "function") {
198
+ init = [
199
+ init,
200
+ newInit
201
+ ];
202
+ } else {
203
+ init.push(newInit);
204
+ }
205
+ }
206
+ }
207
+ }
208
+ }
209
+ if (kind === 0 || kind === 1) {
210
+ if (init === void 0) {
211
+ init = function(instance, init) {
212
+ return init;
213
+ };
214
+ } else if (typeof init !== "function") {
215
+ var ownInitializers = init;
216
+ init = function(instance, init) {
217
+ var value = init;
218
+ for(var i = 0; i < ownInitializers.length; i++){
219
+ value = ownInitializers[i].call(instance, value);
220
+ }
221
+ return value;
222
+ };
223
+ } else {
224
+ var originalInitializer = init;
225
+ init = function(instance, init) {
226
+ return originalInitializer.call(instance, init);
227
+ };
228
+ }
229
+ ret.push(init);
230
+ }
231
+ if (kind !== 0) {
232
+ if (kind === 1) {
233
+ desc.get = value.get;
234
+ desc.set = value.set;
235
+ } else if (kind === 2) {
236
+ desc.value = value;
237
+ } else if (kind === 3) {
238
+ desc.get = value;
239
+ } else if (kind === 4) {
240
+ desc.set = value;
241
+ }
242
+ if (isPrivate) {
243
+ if (kind === 1) {
244
+ ret.push(function(instance, args) {
245
+ return value.get.call(instance, args);
246
+ });
247
+ ret.push(function(instance, args) {
248
+ return value.set.call(instance, args);
249
+ });
250
+ } else if (kind === 2) {
251
+ ret.push(value);
252
+ } else {
253
+ ret.push(function(instance, args) {
254
+ return value.call(instance, args);
255
+ });
256
+ }
257
+ } else {
258
+ Object.defineProperty(base, name, desc);
259
+ }
260
+ }
261
+ }
262
+ function applyMemberDecs(Class, decInfos, metadata) {
263
+ var ret = [];
264
+ var protoInitializers;
265
+ var staticInitializers;
266
+ var existingProtoNonFields = new Map();
267
+ var existingStaticNonFields = new Map();
268
+ for(var i = 0; i < decInfos.length; i++){
269
+ var decInfo = decInfos[i];
270
+ if (!Array.isArray(decInfo)) continue;
271
+ var kind = decInfo[1];
272
+ var name = decInfo[2];
273
+ var isPrivate = decInfo.length > 3;
274
+ var isStatic = kind >= 5;
275
+ var base;
276
+ var initializers;
277
+ if (isStatic) {
278
+ base = Class;
279
+ kind = kind - 5;
280
+ staticInitializers = staticInitializers || [];
281
+ initializers = staticInitializers;
282
+ } else {
283
+ base = Class.prototype;
284
+ protoInitializers = protoInitializers || [];
285
+ initializers = protoInitializers;
286
+ }
287
+ if (kind !== 0 && !isPrivate) {
288
+ var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
289
+ var existingKind = existingNonFields.get(name) || 0;
290
+ if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
291
+ throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
292
+ } else if (!existingKind && kind > 2) {
293
+ existingNonFields.set(name, kind);
294
+ } else {
295
+ existingNonFields.set(name, true);
296
+ }
297
+ }
298
+ applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
299
+ }
300
+ pushInitializers(ret, protoInitializers);
301
+ pushInitializers(ret, staticInitializers);
302
+ return ret;
303
+ }
304
+ function pushInitializers(ret, initializers) {
305
+ if (initializers) {
306
+ ret.push(function(instance) {
307
+ for(var i = 0; i < initializers.length; i++){
308
+ initializers[i].call(instance);
309
+ }
310
+ return instance;
311
+ });
312
+ }
313
+ }
314
+ function applyClassDecs(targetClass, classDecs, metadata) {
315
+ if (classDecs.length > 0) {
316
+ var initializers = [];
317
+ var newClass = targetClass;
318
+ var name = targetClass.name;
319
+ for(var i = classDecs.length - 1; i >= 0; i--){
320
+ var decoratorFinishedRef = {
321
+ v: false
322
+ };
323
+ try {
324
+ var nextNewClass = classDecs[i](newClass, {
325
+ kind: "class",
326
+ name: name,
327
+ addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
328
+ metadata
329
+ });
330
+ } finally{
331
+ decoratorFinishedRef.v = true;
332
+ }
333
+ if (nextNewClass !== undefined) {
334
+ assertValidReturnValue(10, nextNewClass);
335
+ newClass = nextNewClass;
336
+ }
337
+ }
338
+ return [
339
+ defineMetadata(newClass, metadata),
340
+ function() {
341
+ for(var i = 0; i < initializers.length; i++){
342
+ initializers[i].call(newClass);
343
+ }
344
+ }
345
+ ];
346
+ }
347
+ }
348
+ function defineMetadata(Class, metadata) {
349
+ return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
350
+ configurable: true,
351
+ enumerable: true,
352
+ value: metadata
353
+ });
354
+ }
355
+ return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
356
+ if (parentClass !== void 0) {
357
+ var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
358
+ }
359
+ var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
360
+ var e = applyMemberDecs(targetClass, memberDecs, metadata);
361
+ if (!classDecs.length) defineMetadata(targetClass, metadata);
362
+ return {
363
+ e: e,
364
+ get c () {
365
+ return applyClassDecs(targetClass, classDecs, metadata);
366
+ }
367
+ };
368
+ };
6
369
  }
370
+ function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
371
+ return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
372
+ }
373
+ var _dec, _initProto;
7
374
  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';
375
+ import { BBCircuitVerifier, QueuedIVCVerifier, TestCircuitVerifier } from '@aztec/bb-prover';
376
+ import { createBlobClientWithFileStores } from '@aztec/blob-client/client';
11
377
  import { EpochCache } from '@aztec/epoch-cache';
12
- import { createEthereumChain } from '@aztec/ethereum';
13
- import { compactArray } from '@aztec/foundation/collection';
378
+ import { createEthereumChain } from '@aztec/ethereum/chain';
379
+ import { getPublicClient } from '@aztec/ethereum/client';
380
+ import { RegistryContract, RollupContract } from '@aztec/ethereum/contracts';
381
+ import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
382
+ import { compactArray, pick } from '@aztec/foundation/collection';
383
+ import { Fr } from '@aztec/foundation/curves/bn254';
14
384
  import { EthAddress } from '@aztec/foundation/eth-address';
15
- import { Fr } from '@aztec/foundation/fields';
385
+ import { BadRequestError } from '@aztec/foundation/json-rpc';
16
386
  import { createLogger } from '@aztec/foundation/log';
387
+ import { count } from '@aztec/foundation/string';
17
388
  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';
389
+ import { MembershipWitness } from '@aztec/foundation/trees';
390
+ import { KeystoreManager, loadKeystores, mergeKeystores } from '@aztec/node-keystore';
391
+ import { trySnapshotSync, uploadSnapshot } from '@aztec/node-lib/actions';
392
+ import { createForwarderL1TxUtilsFromEthSigner, createL1TxUtilsWithBlobsFromEthSigner } from '@aztec/node-lib/factories';
393
+ import { createP2PClient, getDefaultAllowedSetupFunctions } from '@aztec/p2p';
22
394
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
23
- import { GlobalVariableBuilder, SequencerClient, createSlasherClient, createValidatorForAcceptingTxs, getDefaultAllowedSetupFunctions } from '@aztec/sequencer-client';
395
+ import { GlobalVariableBuilder, SequencerClient } from '@aztec/sequencer-client';
24
396
  import { PublicProcessorFactory } from '@aztec/simulator/server';
397
+ import { AttestationsBlockWatcher, EpochPruneWatcher, createSlasher } from '@aztec/slasher';
398
+ import { CollectionLimitsConfig, PublicSimulatorConfig } from '@aztec/stdlib/avm';
25
399
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
26
- import { computePublicDataTreeLeafSlot, siloNullifier } from '@aztec/stdlib/hash';
400
+ import { BlockHash, L2Block } from '@aztec/stdlib/block';
401
+ import { GasFees } from '@aztec/stdlib/gas';
402
+ import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
403
+ import { AztecNodeAdminConfigSchema } from '@aztec/stdlib/interfaces/client';
27
404
  import { tryStop } from '@aztec/stdlib/interfaces/server';
405
+ import { InboxLeaf } from '@aztec/stdlib/messaging';
28
406
  import { P2PClientType } from '@aztec/stdlib/p2p';
29
407
  import { MerkleTreeId, NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
30
408
  import { PublicSimulationOutput, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
409
+ import { getPackageVersion } from '@aztec/stdlib/update-checker';
31
410
  import { Attributes, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
32
- import { createValidatorClient } from '@aztec/validator-client';
411
+ import { FullNodeCheckpointsBuilder as CheckpointsBuilder, FullNodeCheckpointsBuilder, NodeKeystoreAdapter, ValidatorClient, createBlockProposalHandler, createValidatorClient, createValidatorForAcceptingTxs } from '@aztec/validator-client';
33
412
  import { createWorldStateSynchronizer } from '@aztec/world-state';
34
- import { getPackageVersion } from './config.js';
413
+ import { createPublicClient, fallback, http } from 'viem';
414
+ import { createSentinel } from '../sentinel/factory.js';
415
+ import { createKeyStoreForValidator } from './config.js';
35
416
  import { NodeMetrics } from './node_metrics.js';
417
+ _dec = trackSpan('AztecNodeService.simulatePublicCalls', (tx)=>({
418
+ [Attributes.TX_HASH]: tx.getTxHash().toString()
419
+ }));
36
420
  /**
37
421
  * The aztec node.
38
422
  */ export class AztecNodeService {
@@ -42,35 +426,57 @@ import { NodeMetrics } from './node_metrics.js';
42
426
  logsSource;
43
427
  contractDataSource;
44
428
  l1ToL2MessageSource;
45
- nullifierSource;
46
429
  worldStateSynchronizer;
47
430
  sequencer;
431
+ slasherClient;
432
+ validatorsSentinel;
433
+ epochPruneWatcher;
48
434
  l1ChainId;
49
435
  version;
50
436
  globalVariableBuilder;
437
+ epochCache;
438
+ packageVersion;
51
439
  proofVerifier;
52
440
  telemetry;
53
441
  log;
54
- packageVersion;
442
+ blobClient;
443
+ static{
444
+ ({ e: [_initProto] } = _apply_decs_2203_r(this, [
445
+ [
446
+ _dec,
447
+ 2,
448
+ "simulatePublicCalls"
449
+ ]
450
+ ], []));
451
+ }
55
452
  metrics;
453
+ initialHeaderHashPromise;
454
+ // Prevent two snapshot operations to happen simultaneously
455
+ isUploadingSnapshot;
56
456
  tracer;
57
- constructor(config, p2pClient, blockSource, logsSource, contractDataSource, l1ToL2MessageSource, nullifierSource, worldStateSynchronizer, sequencer, l1ChainId, version, globalVariableBuilder, proofVerifier, telemetry = getTelemetryClient(), log = createLogger('node')){
457
+ constructor(config, p2pClient, blockSource, logsSource, contractDataSource, l1ToL2MessageSource, worldStateSynchronizer, sequencer, slasherClient, validatorsSentinel, epochPruneWatcher, l1ChainId, version, globalVariableBuilder, epochCache, packageVersion, proofVerifier, telemetry = getTelemetryClient(), log = createLogger('node'), blobClient){
58
458
  this.config = config;
59
459
  this.p2pClient = p2pClient;
60
460
  this.blockSource = blockSource;
61
461
  this.logsSource = logsSource;
62
462
  this.contractDataSource = contractDataSource;
63
463
  this.l1ToL2MessageSource = l1ToL2MessageSource;
64
- this.nullifierSource = nullifierSource;
65
464
  this.worldStateSynchronizer = worldStateSynchronizer;
66
465
  this.sequencer = sequencer;
466
+ this.slasherClient = slasherClient;
467
+ this.validatorsSentinel = validatorsSentinel;
468
+ this.epochPruneWatcher = epochPruneWatcher;
67
469
  this.l1ChainId = l1ChainId;
68
470
  this.version = version;
69
471
  this.globalVariableBuilder = globalVariableBuilder;
472
+ this.epochCache = epochCache;
473
+ this.packageVersion = packageVersion;
70
474
  this.proofVerifier = proofVerifier;
71
475
  this.telemetry = telemetry;
72
476
  this.log = log;
73
- this.packageVersion = getPackageVersion();
477
+ this.blobClient = blobClient;
478
+ this.initialHeaderHashPromise = (_initProto(this), undefined);
479
+ this.isUploadingSnapshot = false;
74
480
  this.metrics = new NodeMetrics(telemetry, 'AztecNodeService');
75
481
  this.tracer = telemetry.getTracer('AztecNodeService');
76
482
  this.log.info(`Aztec Node version: ${this.packageVersion}`);
@@ -87,59 +493,222 @@ import { NodeMetrics } from './node_metrics.js';
87
493
  * initializes the Aztec Node, wait for component to sync.
88
494
  * @param config - The configuration to be used by the aztec node.
89
495
  * @returns - A fully synced Aztec Node for use in development/testing.
90
- */ static async createAndSync(config, deps = {}, options = {}) {
91
- const telemetry = deps.telemetry ?? getTelemetryClient();
496
+ */ static async createAndSync(inputConfig, deps = {}, options = {}) {
497
+ const config = {
498
+ ...inputConfig
499
+ }; // Copy the config so we dont mutate the input object
92
500
  const log = deps.logger ?? createLogger('node');
501
+ const packageVersion = getPackageVersion() ?? '';
502
+ const telemetry = deps.telemetry ?? getTelemetryClient();
93
503
  const dateProvider = deps.dateProvider ?? new DateProvider();
94
- const blobSinkClient = deps.blobSinkClient ?? createBlobSinkClient(config);
95
504
  const ethereumChain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
96
- //validate that the actual chain id matches that specified in configuration
505
+ // Build a key store from file if given or from environment otherwise
506
+ let keyStoreManager;
507
+ const keyStoreProvided = config.keyStoreDirectory !== undefined && config.keyStoreDirectory.length > 0;
508
+ if (keyStoreProvided) {
509
+ const keyStores = loadKeystores(config.keyStoreDirectory);
510
+ keyStoreManager = new KeystoreManager(mergeKeystores(keyStores));
511
+ } else {
512
+ const keyStore = createKeyStoreForValidator(config);
513
+ if (keyStore) {
514
+ keyStoreManager = new KeystoreManager(keyStore);
515
+ }
516
+ }
517
+ await keyStoreManager?.validateSigners();
518
+ // If we are a validator, verify our configuration before doing too much more.
519
+ if (!config.disableValidator) {
520
+ if (keyStoreManager === undefined) {
521
+ throw new Error('Failed to create key store, a requirement for running a validator');
522
+ }
523
+ if (!keyStoreProvided) {
524
+ log.warn('KEY STORE CREATED FROM ENVIRONMENT, IT IS RECOMMENDED TO USE A FILE-BASED KEY STORE IN PRODUCTION ENVIRONMENTS');
525
+ }
526
+ ValidatorClient.validateKeyStoreConfiguration(keyStoreManager, log);
527
+ }
528
+ // validate that the actual chain id matches that specified in configuration
97
529
  if (config.l1ChainId !== ethereumChain.chainInfo.id) {
98
530
  throw new Error(`RPC URL configured for chain id ${ethereumChain.chainInfo.id} but expected id ${config.l1ChainId}`);
99
531
  }
100
- const archiver = await createArchiver(config, blobSinkClient, {
101
- blockUntilSync: true
102
- }, telemetry);
532
+ const publicClient = createPublicClient({
533
+ chain: ethereumChain.chainInfo,
534
+ transport: fallback(config.l1RpcUrls.map((url)=>http(url, {
535
+ batch: false
536
+ }))),
537
+ pollingInterval: config.viemPollingIntervalMS
538
+ });
539
+ const l1ContractsAddresses = await RegistryContract.collectAddresses(publicClient, config.l1Contracts.registryAddress, config.rollupVersion ?? 'canonical');
540
+ // Overwrite the passed in vars.
541
+ config.l1Contracts = {
542
+ ...config.l1Contracts,
543
+ ...l1ContractsAddresses
544
+ };
545
+ const rollupContract = new RollupContract(publicClient, config.l1Contracts.rollupAddress.toString());
546
+ const [l1GenesisTime, slotDuration, rollupVersionFromRollup] = await Promise.all([
547
+ rollupContract.getL1GenesisTime(),
548
+ rollupContract.getSlotDuration(),
549
+ rollupContract.getVersion()
550
+ ]);
551
+ config.rollupVersion ??= Number(rollupVersionFromRollup);
552
+ if (config.rollupVersion !== Number(rollupVersionFromRollup)) {
553
+ 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}).`);
554
+ }
555
+ const blobClient = await createBlobClientWithFileStores(config, createLogger('node:blob-client:client'));
556
+ // attempt snapshot sync if possible
557
+ await trySnapshotSync(config, log);
558
+ const epochCache = await EpochCache.create(config.l1Contracts.rollupAddress, config, {
559
+ dateProvider
560
+ });
561
+ const archiver = await createArchiver(config, {
562
+ blobClient,
563
+ epochCache,
564
+ telemetry,
565
+ dateProvider
566
+ }, {
567
+ blockUntilSync: !config.skipArchiverInitialSync
568
+ });
103
569
  // now create the merkle trees and the world state synchronizer
104
570
  const worldStateSynchronizer = await createWorldStateSynchronizer(config, archiver, options.prefilledPublicData, telemetry);
105
- const proofVerifier = config.realProofs ? await BBCircuitVerifier.new(config) : new TestCircuitVerifier();
571
+ const circuitVerifier = config.realProofs || config.debugForceTxProofVerification ? await BBCircuitVerifier.new(config) : new TestCircuitVerifier(config.proverTestVerificationDelayMs);
106
572
  if (!config.realProofs) {
107
573
  log.warn(`Aztec node is accepting fake proofs`);
108
574
  }
109
- const epochCache = await EpochCache.create(config.l1Contracts.rollupAddress, config, {
110
- dateProvider
111
- });
575
+ const proofVerifier = new QueuedIVCVerifier(config, circuitVerifier);
112
576
  // 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`);
122
- const validatorClient = createValidatorClient(config, {
577
+ const p2pClient = await createP2PClient(P2PClientType.Full, config, archiver, proofVerifier, worldStateSynchronizer, epochCache, packageVersion, dateProvider, telemetry, deps.p2pClientDeps);
578
+ // We should really not be modifying the config object
579
+ config.txPublicSetupAllowList = config.txPublicSetupAllowList ?? await getDefaultAllowedSetupFunctions();
580
+ // Create FullNodeCheckpointsBuilder for validator and non-validator block proposal handling
581
+ const validatorCheckpointsBuilder = new FullNodeCheckpointsBuilder({
582
+ ...config,
583
+ l1GenesisTime,
584
+ slotDuration: Number(slotDuration)
585
+ }, worldStateSynchronizer, archiver, dateProvider, telemetry);
586
+ // We'll accumulate sentinel watchers here
587
+ const watchers = [];
588
+ // Create validator client if required
589
+ const validatorClient = await createValidatorClient(config, {
590
+ checkpointsBuilder: validatorCheckpointsBuilder,
591
+ worldState: worldStateSynchronizer,
123
592
  p2pClient,
124
593
  telemetry,
125
594
  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,
595
+ epochCache,
596
+ blockSource: archiver,
137
597
  l1ToL2MessageSource: archiver,
138
- telemetry,
139
- dateProvider,
140
- blobSinkClient
598
+ keyStoreManager,
599
+ blobClient
141
600
  });
142
- return new AztecNodeService(config, p2pClient, archiver, archiver, archiver, archiver, archiver, worldStateSynchronizer, sequencer, ethereumChain.chainInfo.id, config.version, new GlobalVariableBuilder(config), proofVerifier, telemetry, log);
601
+ // If we have a validator client, register it as a source of offenses for the slasher,
602
+ // and have it register callbacks on the p2p client *before* we start it, otherwise messages
603
+ // like attestations or auths will fail.
604
+ if (validatorClient) {
605
+ watchers.push(validatorClient);
606
+ if (!options.dontStartSequencer) {
607
+ await validatorClient.registerHandlers();
608
+ }
609
+ }
610
+ // If there's no validator client but alwaysReexecuteBlockProposals is enabled,
611
+ // create a BlockProposalHandler to reexecute block proposals for monitoring
612
+ if (!validatorClient && config.alwaysReexecuteBlockProposals) {
613
+ log.info('Setting up block proposal reexecution for monitoring');
614
+ createBlockProposalHandler(config, {
615
+ checkpointsBuilder: validatorCheckpointsBuilder,
616
+ worldState: worldStateSynchronizer,
617
+ epochCache,
618
+ blockSource: archiver,
619
+ l1ToL2MessageSource: archiver,
620
+ p2pClient,
621
+ dateProvider,
622
+ telemetry
623
+ }).registerForReexecution(p2pClient);
624
+ }
625
+ // Start world state and wait for it to sync to the archiver.
626
+ await worldStateSynchronizer.start();
627
+ // Start p2p. Note that it depends on world state to be running.
628
+ await p2pClient.start();
629
+ const validatorsSentinel = await createSentinel(epochCache, archiver, p2pClient, config);
630
+ if (validatorsSentinel && config.slashInactivityPenalty > 0n) {
631
+ watchers.push(validatorsSentinel);
632
+ }
633
+ let epochPruneWatcher;
634
+ if (config.slashPrunePenalty > 0n || config.slashDataWithholdingPenalty > 0n) {
635
+ epochPruneWatcher = new EpochPruneWatcher(archiver, archiver, epochCache, p2pClient.getTxProvider(), validatorCheckpointsBuilder, config);
636
+ watchers.push(epochPruneWatcher);
637
+ }
638
+ // We assume we want to slash for invalid attestations unless all max penalties are set to 0
639
+ let attestationsBlockWatcher;
640
+ if (config.slashProposeInvalidAttestationsPenalty > 0n || config.slashAttestDescendantOfInvalidPenalty > 0n) {
641
+ attestationsBlockWatcher = new AttestationsBlockWatcher(archiver, epochCache, config);
642
+ watchers.push(attestationsBlockWatcher);
643
+ }
644
+ // Start p2p-related services once the archiver has completed sync
645
+ void archiver.waitForInitialSync().then(async ()=>{
646
+ await p2pClient.start();
647
+ await validatorsSentinel?.start();
648
+ await epochPruneWatcher?.start();
649
+ await attestationsBlockWatcher?.start();
650
+ log.info(`All p2p services started`);
651
+ }).catch((err)=>log.error('Failed to start p2p services after archiver sync', err));
652
+ // Validator enabled, create/start relevant service
653
+ let sequencer;
654
+ let slasherClient;
655
+ if (!config.disableValidator && validatorClient) {
656
+ // We create a slasher only if we have a sequencer, since all slashing actions go through the sequencer publisher
657
+ // as they are executed when the node is selected as proposer.
658
+ const validatorAddresses = keyStoreManager ? NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager).getAddresses() : [];
659
+ slasherClient = await createSlasher(config, config.l1Contracts, getPublicClient(config), watchers, dateProvider, epochCache, validatorAddresses, undefined);
660
+ await slasherClient.start();
661
+ const l1TxUtils = config.publisherForwarderAddress ? await createForwarderL1TxUtilsFromEthSigner(publicClient, keyStoreManager.createAllValidatorPublisherSigners(), config.publisherForwarderAddress, {
662
+ ...config,
663
+ scope: 'sequencer'
664
+ }, {
665
+ telemetry,
666
+ logger: log.createChild('l1-tx-utils'),
667
+ dateProvider
668
+ }) : await createL1TxUtilsWithBlobsFromEthSigner(publicClient, keyStoreManager.createAllValidatorPublisherSigners(), {
669
+ ...config,
670
+ scope: 'sequencer'
671
+ }, {
672
+ telemetry,
673
+ logger: log.createChild('l1-tx-utils'),
674
+ dateProvider
675
+ });
676
+ // Create and start the sequencer client
677
+ const checkpointsBuilder = new CheckpointsBuilder({
678
+ ...config,
679
+ l1GenesisTime,
680
+ slotDuration: Number(slotDuration)
681
+ }, worldStateSynchronizer, archiver, dateProvider, telemetry);
682
+ sequencer = await SequencerClient.new(config, {
683
+ ...deps,
684
+ epochCache,
685
+ l1TxUtils,
686
+ validatorClient,
687
+ p2pClient,
688
+ worldStateSynchronizer,
689
+ slasherClient,
690
+ checkpointsBuilder,
691
+ l2BlockSource: archiver,
692
+ l1ToL2MessageSource: archiver,
693
+ telemetry,
694
+ dateProvider,
695
+ blobClient,
696
+ nodeKeyStore: keyStoreManager
697
+ });
698
+ }
699
+ if (!options.dontStartSequencer && sequencer) {
700
+ await sequencer.start();
701
+ log.verbose(`Sequencer started`);
702
+ } else if (sequencer) {
703
+ log.warn(`Sequencer created but not started`);
704
+ }
705
+ const globalVariableBuilder = new GlobalVariableBuilder({
706
+ ...config,
707
+ rollupVersion: BigInt(config.rollupVersion),
708
+ l1GenesisTime,
709
+ slotDuration: Number(slotDuration)
710
+ });
711
+ 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, blobClient);
143
712
  }
144
713
  /**
145
714
  * Returns the sequencer client instance.
@@ -165,6 +734,9 @@ import { NodeMetrics } from './node_metrics.js';
165
734
  getEncodedEnr() {
166
735
  return Promise.resolve(this.p2pClient.getEnr()?.encodeTxt());
167
736
  }
737
+ async getAllowedPublicSetup() {
738
+ return this.config.txPublicSetupAllowList ?? await getDefaultAllowedSetupFunctions();
739
+ }
168
740
  /**
169
741
  * Method to determine if the node is ready to accept transactions.
170
742
  * @returns - Flag indicating the readiness for tx submission.
@@ -172,7 +744,7 @@ import { NodeMetrics } from './node_metrics.js';
172
744
  return Promise.resolve(this.p2pClient.isReady() ?? false);
173
745
  }
174
746
  async getNodeInfo() {
175
- const [nodeVersion, protocolVersion, chainId, enr, contractAddresses, protocolContractAddresses] = await Promise.all([
747
+ const [nodeVersion, rollupVersion, chainId, enr, contractAddresses, protocolContractAddresses] = await Promise.all([
176
748
  this.getNodeVersion(),
177
749
  this.getVersion(),
178
750
  this.getChainId(),
@@ -183,7 +755,7 @@ import { NodeMetrics } from './node_metrics.js';
183
755
  const nodeInfo = {
184
756
  nodeVersion,
185
757
  l1ChainId: chainId,
186
- protocolVersion,
758
+ rollupVersion,
187
759
  enr,
188
760
  l1ContractAddresses: contractAddresses,
189
761
  protocolContractAddresses: protocolContractAddresses
@@ -191,11 +763,40 @@ import { NodeMetrics } from './node_metrics.js';
191
763
  return nodeInfo;
192
764
  }
193
765
  /**
194
- * Get a block specified by its number.
195
- * @param number - The block number being requested.
766
+ * Get a block specified by its block number, block hash, or 'latest'.
767
+ * @param block - The block parameter (block number, block hash, or 'latest').
768
+ * @returns The requested block.
769
+ */ async getBlock(block) {
770
+ if (BlockHash.isBlockHash(block)) {
771
+ return this.getBlockByHash(block);
772
+ }
773
+ const blockNumber = block === 'latest' ? await this.getBlockNumber() : block;
774
+ if (blockNumber === BlockNumber.ZERO) {
775
+ return this.buildInitialBlock();
776
+ }
777
+ return await this.blockSource.getL2Block(blockNumber);
778
+ }
779
+ /**
780
+ * Get a block specified by its hash.
781
+ * @param blockHash - The block hash being requested.
782
+ * @returns The requested block.
783
+ */ async getBlockByHash(blockHash) {
784
+ const initialBlockHash = await this.#getInitialHeaderHash();
785
+ if (blockHash.equals(initialBlockHash)) {
786
+ return this.buildInitialBlock();
787
+ }
788
+ return await this.blockSource.getL2BlockByHash(blockHash);
789
+ }
790
+ buildInitialBlock() {
791
+ const initialHeader = this.worldStateSynchronizer.getCommitted().getInitialHeader();
792
+ return L2Block.empty(initialHeader);
793
+ }
794
+ /**
795
+ * Get a block specified by its archive root.
796
+ * @param archive - The archive root being requested.
196
797
  * @returns The requested block.
197
- */ async getBlock(number) {
198
- return await this.blockSource.getBlock(number);
798
+ */ async getBlockByArchive(archive) {
799
+ return await this.blockSource.getL2BlockByArchive(archive);
199
800
  }
200
801
  /**
201
802
  * Method to request blocks. Will attempt to return all requested blocks but will return only those available.
@@ -203,16 +804,31 @@ import { NodeMetrics } from './node_metrics.js';
203
804
  * @param limit - The maximum number of blocks to obtain.
204
805
  * @returns The blocks requested.
205
806
  */ async getBlocks(from, limit) {
206
- return await this.blockSource.getBlocks(from, limit) ?? [];
807
+ return await this.blockSource.getBlocks(from, BlockNumber(limit)) ?? [];
808
+ }
809
+ async getCheckpoints(from, limit) {
810
+ return await this.blockSource.getCheckpoints(from, limit) ?? [];
811
+ }
812
+ async getCheckpointedBlocks(from, limit) {
813
+ return await this.blockSource.getCheckpointedBlocks(from, limit) ?? [];
207
814
  }
208
815
  /**
209
- * Method to fetch the current base fees.
210
- * @returns The current base fees.
211
- */ async getCurrentBaseFees() {
212
- return await this.globalVariableBuilder.getCurrentBaseFees();
816
+ * Method to fetch the current min L2 fees.
817
+ * @returns The current min L2 fees.
818
+ */ async getCurrentMinFees() {
819
+ return await this.globalVariableBuilder.getCurrentMinFees();
820
+ }
821
+ async getMaxPriorityFees() {
822
+ for await (const tx of this.p2pClient.iteratePendingTxs()){
823
+ return tx.getGasSettings().maxPriorityFeesPerGas;
824
+ }
825
+ return GasFees.from({
826
+ feePerDaGas: 0n,
827
+ feePerL2Gas: 0n
828
+ });
213
829
  }
214
830
  /**
215
- * Method to fetch the current block number.
831
+ * Method to fetch the latest block number synchronized by the node.
216
832
  * @returns The block number.
217
833
  */ async getBlockNumber() {
218
834
  return await this.blockSource.getBlockNumber();
@@ -220,6 +836,9 @@ import { NodeMetrics } from './node_metrics.js';
220
836
  async getProvenBlockNumber() {
221
837
  return await this.blockSource.getProvenBlockNumber();
222
838
  }
839
+ async getCheckpointedBlockNumber() {
840
+ return await this.blockSource.getCheckpointedL2BlockNumber();
841
+ }
223
842
  /**
224
843
  * Method to fetch the version of the package.
225
844
  * @returns The node package version
@@ -238,44 +857,35 @@ import { NodeMetrics } from './node_metrics.js';
238
857
  */ getChainId() {
239
858
  return Promise.resolve(this.l1ChainId);
240
859
  }
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;
860
+ getContractClass(id) {
861
+ return this.contractDataSource.getContractClass(id);
260
862
  }
261
863
  getContract(address) {
262
864
  return this.contractDataSource.getContract(address);
263
865
  }
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);
866
+ async getPrivateLogsByTags(tags, page, referenceBlock) {
867
+ if (referenceBlock) {
868
+ const initialBlockHash = await this.#getInitialHeaderHash();
869
+ if (!referenceBlock.equals(initialBlockHash)) {
870
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
871
+ if (!header) {
872
+ throw new Error(`Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`);
873
+ }
874
+ }
875
+ }
876
+ return this.logsSource.getPrivateLogsByTags(tags, page);
271
877
  }
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);
878
+ async getPublicLogsByTagsFromContract(contractAddress, tags, page, referenceBlock) {
879
+ if (referenceBlock) {
880
+ const initialBlockHash = await this.#getInitialHeaderHash();
881
+ if (!referenceBlock.equals(initialBlockHash)) {
882
+ const header = await this.blockSource.getBlockHeaderByHash(referenceBlock);
883
+ if (!header) {
884
+ throw new Error(`Block ${referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`);
885
+ }
886
+ }
887
+ }
888
+ return this.logsSource.getPublicLogsByTagsFromContract(contractAddress, tags, page);
279
889
  }
280
890
  /**
281
891
  * Gets public logs based on the provided filter.
@@ -295,18 +905,19 @@ import { NodeMetrics } from './node_metrics.js';
295
905
  * Method to submit a transaction to the p2p pool.
296
906
  * @param tx - The transaction to be submitted.
297
907
  */ async sendTx(tx) {
908
+ await this.#sendTx(tx);
909
+ }
910
+ async #sendTx(tx) {
298
911
  const timer = new Timer();
299
- const txHash = (await tx.getTxHash()).toString();
912
+ const txHash = tx.getTxHash().toString();
300
913
  const valid = await this.isValidTx(tx);
301
914
  if (valid.result !== 'valid') {
302
915
  const reason = valid.reason.join(', ');
303
916
  this.metrics.receivedTx(timer.ms(), false);
304
- this.log.warn(`Invalid tx ${txHash}: ${reason}`, {
917
+ this.log.warn(`Received invalid tx ${txHash}: ${reason}`, {
305
918
  txHash
306
919
  });
307
- // TODO(#10967): Throw when receiving an invalid tx instead of just returning
308
- // throw new Error(`Invalid tx: ${reason}`);
309
- return;
920
+ throw new Error(`Invalid tx: ${reason}`);
310
921
  }
311
922
  await this.p2pClient.sendTx(tx);
312
923
  this.metrics.receivedTx(timer.ms(), true);
@@ -315,18 +926,24 @@ import { NodeMetrics } from './node_metrics.js';
315
926
  });
316
927
  }
317
928
  async getTxReceipt(txHash) {
318
- let txReceipt = new TxReceipt(txHash, TxStatus.DROPPED, 'Tx dropped by P2P node.');
319
- // We first check if the tx is in pending (instead of first checking if it is mined) because if we first check
320
- // for mined and then for pending there could be a race condition where the tx is mined between the two checks
321
- // and we would incorrectly return a TxReceipt with status DROPPED
322
- if (await this.p2pClient.getTxStatus(txHash) === 'pending') {
323
- txReceipt = new TxReceipt(txHash, TxStatus.PENDING, '');
324
- }
929
+ // Check the tx pool status first. If the tx is known to the pool (pending or mined), we'll use that
930
+ // as a fallback if we don't find a settled receipt in the archiver.
931
+ const txPoolStatus = await this.p2pClient.getTxStatus(txHash);
932
+ const isKnownToPool = txPoolStatus === 'pending' || txPoolStatus === 'mined';
933
+ // Then get the actual tx from the archiver, which tracks every tx in a mined block.
325
934
  const settledTxReceipt = await this.blockSource.getSettledTxReceipt(txHash);
326
935
  if (settledTxReceipt) {
327
- txReceipt = settledTxReceipt;
936
+ // If the archiver has the receipt then return it.
937
+ return settledTxReceipt;
938
+ } else if (isKnownToPool) {
939
+ // If the tx is in the pool but not in the archiver, it's pending.
940
+ // This handles race conditions between archiver and p2p, where the archiver
941
+ // has pruned the block in which a tx was mined, but p2p has not caught up yet.
942
+ return new TxReceipt(txHash, TxStatus.PENDING, undefined, undefined);
943
+ } else {
944
+ // Otherwise, if we don't know the tx, we consider it dropped.
945
+ return new TxReceipt(txHash, TxStatus.DROPPED, undefined, 'Tx dropped by P2P node');
328
946
  }
329
- return txReceipt;
330
947
  }
331
948
  getTxEffect(txHash) {
332
949
  return this.blockSource.getTxEffect(txHash);
@@ -334,212 +951,180 @@ import { NodeMetrics } from './node_metrics.js';
334
951
  /**
335
952
  * Method to stop the aztec node.
336
953
  */ async stop() {
337
- this.log.info(`Stopping`);
338
- await this.sequencer?.stop();
339
- await this.p2pClient.stop();
340
- await this.worldStateSynchronizer.stop();
954
+ this.log.info(`Stopping Aztec Node`);
955
+ await tryStop(this.validatorsSentinel);
956
+ await tryStop(this.epochPruneWatcher);
957
+ await tryStop(this.slasherClient);
958
+ await tryStop(this.proofVerifier);
959
+ await tryStop(this.sequencer);
960
+ await tryStop(this.p2pClient);
961
+ await tryStop(this.worldStateSynchronizer);
341
962
  await tryStop(this.blockSource);
342
- await this.telemetry.stop();
343
- this.log.info(`Stopped`);
963
+ await tryStop(this.blobClient);
964
+ await tryStop(this.telemetry);
965
+ this.log.info(`Stopped Aztec Node`);
966
+ }
967
+ /**
968
+ * Returns the blob client used by this node.
969
+ * @internal - Exposed for testing purposes only.
970
+ */ getBlobClient() {
971
+ return this.blobClient;
344
972
  }
345
973
  /**
346
974
  * Method to retrieve pending txs.
975
+ * @param limit - The number of items to returns
976
+ * @param after - The last known pending tx. Used for pagination
347
977
  * @returns - The pending txs.
348
- */ getPendingTxs() {
349
- return this.p2pClient.getPendingTxs();
978
+ */ getPendingTxs(limit, after) {
979
+ return this.p2pClient.getPendingTxs(limit, after);
350
980
  }
351
- async getPendingTxCount() {
352
- const pendingTxs = await this.getPendingTxs();
353
- return pendingTxs.length;
981
+ getPendingTxCount() {
982
+ return this.p2pClient.getPendingTxCount();
354
983
  }
355
984
  /**
356
- * Method to retrieve a single tx from the mempool or unfinalised chain.
985
+ * Method to retrieve a single tx from the mempool or unfinalized chain.
357
986
  * @param txHash - The transaction hash to return.
358
987
  * @returns - The tx if it exists.
359
988
  */ getTxByHash(txHash) {
360
989
  return Promise.resolve(this.p2pClient.getTxByHashFromPool(txHash));
361
990
  }
362
991
  /**
363
- * Method to retrieve txs from the mempool or unfinalised chain.
992
+ * Method to retrieve txs from the mempool or unfinalized chain.
364
993
  * @param txHash - The transaction hash to return.
365
994
  * @returns - The txs if it exists.
366
995
  */ async getTxsByHash(txHashes) {
367
996
  return compactArray(await Promise.all(txHashes.map((txHash)=>this.getTxByHash(txHash))));
368
997
  }
369
- /**
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
372
- * @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.
375
- */ async findLeavesIndexes(blockNumber, treeId, leafValues) {
376
- 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();
998
+ async findLeavesIndexes(referenceBlock, treeId, leafValues) {
999
+ const committedDb = await this.#getWorldState(referenceBlock);
1000
+ const maybeIndices = await committedDb.findLeafIndices(treeId, leafValues.map((x)=>x.toBuffer()));
1001
+ // We filter out undefined values
1002
+ const indices = maybeIndices.filter((x)=>x !== undefined);
1003
+ // Now we find the block numbers for the indices
1004
+ const blockNumbers = await committedDb.getBlockNumbersForLeafIndices(treeId, indices);
1005
+ // If any of the block numbers are undefined, we throw an error.
1006
+ for(let i = 0; i < indices.length; i++){
1007
+ if (blockNumbers[i] === undefined) {
1008
+ throw new Error(`Block number is undefined for leaf index ${indices[i]} in tree ${MerkleTreeId[treeId]}`);
1009
+ }
392
1010
  }
393
- return this.nullifierSource.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
1011
+ // Get unique block numbers in order to optimize num calls to getLeafValue function.
1012
+ const uniqueBlockNumbers = [
1013
+ ...new Set(blockNumbers.filter((x)=>x !== undefined))
1014
+ ];
1015
+ // Now we obtain the block hashes from the archive tree by calling await `committedDb.getLeafValue(treeId, index)`
1016
+ // (note that block number corresponds to the leaf index in the archive tree).
1017
+ const blockHashes = await Promise.all(uniqueBlockNumbers.map((blockNumber)=>{
1018
+ return committedDb.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber));
1019
+ }));
1020
+ // If any of the block hashes are undefined, we throw an error.
1021
+ for(let i = 0; i < uniqueBlockNumbers.length; i++){
1022
+ if (blockHashes[i] === undefined) {
1023
+ throw new Error(`Block hash is undefined for block number ${uniqueBlockNumbers[i]}`);
1024
+ }
1025
+ }
1026
+ // Create DataInBlock objects by combining indices, blockNumbers and blockHashes and return them.
1027
+ return maybeIndices.map((index, i)=>{
1028
+ if (index === undefined) {
1029
+ return undefined;
1030
+ }
1031
+ const blockNumber = blockNumbers[i];
1032
+ if (blockNumber === undefined) {
1033
+ return undefined;
1034
+ }
1035
+ const blockHashIndex = uniqueBlockNumbers.indexOf(blockNumber);
1036
+ const blockHash = blockHashes[blockHashIndex];
1037
+ if (!blockHash) {
1038
+ return undefined;
1039
+ }
1040
+ return {
1041
+ l2BlockNumber: BlockNumber(Number(blockNumber)),
1042
+ l2BlockHash: new BlockHash(blockHash),
1043
+ data: index
1044
+ };
1045
+ });
394
1046
  }
395
- /**
396
- * Returns a sibling path for the given index in the nullifier tree.
397
- * @param blockNumber - The block number at which to get the data.
398
- * @param leafIndex - The index of the leaf for which the sibling path is required.
399
- * @returns The sibling path for the leaf index.
400
- */ async getNullifierSiblingPath(blockNumber, leafIndex) {
401
- const committedDb = await this.#getWorldState(blockNumber);
402
- return committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, leafIndex);
1047
+ async getBlockHashMembershipWitness(referenceBlock, blockHash) {
1048
+ const committedDb = await this.#getWorldState(referenceBlock);
1049
+ const [pathAndIndex] = await committedDb.findSiblingPaths(MerkleTreeId.ARCHIVE, [
1050
+ blockHash
1051
+ ]);
1052
+ return pathAndIndex === undefined ? undefined : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
403
1053
  }
404
- /**
405
- * Returns a sibling path for the given index in the data tree.
406
- * @param blockNumber - The block number at which to get the data.
407
- * @param leafIndex - The index of the leaf for which the sibling path is required.
408
- * @returns The sibling path for the leaf index.
409
- */ async getNoteHashSiblingPath(blockNumber, leafIndex) {
410
- const committedDb = await this.#getWorldState(blockNumber);
411
- return committedDb.getSiblingPath(MerkleTreeId.NOTE_HASH_TREE, leafIndex);
1054
+ async getNoteHashMembershipWitness(referenceBlock, noteHash) {
1055
+ const committedDb = await this.#getWorldState(referenceBlock);
1056
+ const [pathAndIndex] = await committedDb.findSiblingPaths(MerkleTreeId.NOTE_HASH_TREE, [
1057
+ noteHash
1058
+ ]);
1059
+ return pathAndIndex === undefined ? undefined : MembershipWitness.fromSiblingPath(pathAndIndex.index, pathAndIndex.path);
412
1060
  }
413
- /**
414
- * Returns the index and a sibling path for a leaf in the committed l1 to l2 data tree.
415
- * @param blockNumber - The block number at which to get the data.
416
- * @param l1ToL2Message - The l1ToL2Message to get the index / sibling path for.
417
- * @returns A tuple of the index and the sibling path of the L1ToL2Message (undefined if not found).
418
- */ async getL1ToL2MessageMembershipWitness(blockNumber, l1ToL2Message) {
419
- const index = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
420
- if (index === undefined) {
1061
+ async getL1ToL2MessageMembershipWitness(referenceBlock, l1ToL2Message) {
1062
+ const db = await this.#getWorldState(referenceBlock);
1063
+ const [witness] = await db.findSiblingPaths(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, [
1064
+ l1ToL2Message
1065
+ ]);
1066
+ if (!witness) {
421
1067
  return undefined;
422
1068
  }
423
- const committedDb = await this.#getWorldState(blockNumber);
424
- const siblingPath = await committedDb.getSiblingPath(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, index);
1069
+ // REFACTOR: Return a MembershipWitness object
425
1070
  return [
426
- index,
427
- siblingPath
1071
+ witness.index,
1072
+ witness.path
428
1073
  ];
429
1074
  }
1075
+ async getL1ToL2MessageBlock(l1ToL2Message) {
1076
+ const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
1077
+ return messageIndex ? BlockNumber.fromCheckpointNumber(InboxLeaf.checkpointNumberFromIndex(messageIndex)) : undefined;
1078
+ }
430
1079
  /**
431
1080
  * Returns whether an L1 to L2 message is synced by archiver and if it's ready to be included in a block.
432
1081
  * @param l1ToL2Message - The L1 to L2 message to check.
433
1082
  * @returns Whether the message is synced and ready to be included in a block.
434
1083
  */ async isL1ToL2MessageSynced(l1ToL2Message) {
435
- return await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message) !== undefined;
1084
+ const messageIndex = await this.l1ToL2MessageSource.getL1ToL2MessageIndex(l1ToL2Message);
1085
+ return messageIndex !== undefined;
436
1086
  }
437
1087
  /**
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.
443
- * @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) {
447
- 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
- ];
1088
+ * Returns all the L2 to L1 messages in an epoch.
1089
+ * @param epoch - The epoch at which to get the data.
1090
+ * @returns The L2 to L1 messages (empty array if the epoch is not found).
1091
+ */ async getL2ToL1Messages(epoch) {
1092
+ // Assumes `getCheckpointedBlocksForEpoch` returns blocks in ascending order of block number.
1093
+ const checkpointedBlocks = await this.blockSource.getCheckpointedBlocksForEpoch(epoch);
1094
+ const blocksInCheckpoints = [];
1095
+ let previousSlotNumber = SlotNumber.ZERO;
1096
+ let checkpointIndex = -1;
1097
+ for (const checkpointedBlock of checkpointedBlocks){
1098
+ const block = checkpointedBlock.block;
1099
+ const slotNumber = block.header.globalVariables.slotNumber;
1100
+ if (slotNumber !== previousSlotNumber) {
1101
+ checkpointIndex++;
1102
+ blocksInCheckpoints.push([]);
1103
+ previousSlotNumber = slotNumber;
1104
+ }
1105
+ blocksInCheckpoints[checkpointIndex].push(block);
480
1106
  }
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
- ];
497
- }
498
- /**
499
- * Returns a sibling path for a leaf in the committed blocks tree.
500
- * @param blockNumber - The block number at which to get the data.
501
- * @param leafIndex - Index of the leaf in the tree.
502
- * @returns The sibling path.
503
- */ async getArchiveSiblingPath(blockNumber, leafIndex) {
504
- const committedDb = await this.#getWorldState(blockNumber);
505
- return committedDb.getSiblingPath(MerkleTreeId.ARCHIVE, leafIndex);
506
- }
507
- /**
508
- * Returns a sibling path for a leaf in the committed public data tree.
509
- * @param blockNumber - The block number at which to get the data.
510
- * @param leafIndex - Index of the leaf in the tree.
511
- * @returns The sibling path.
512
- */ async getPublicDataSiblingPath(blockNumber, leafIndex) {
513
- const committedDb = await this.#getWorldState(blockNumber);
514
- return committedDb.getSiblingPath(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex);
1107
+ return blocksInCheckpoints.map((blocks)=>blocks.map((block)=>block.body.txEffects.map((txEffect)=>txEffect.l2ToL1Msgs)));
515
1108
  }
516
- /**
517
- * Returns a nullifier membership witness for a given nullifier at a given block.
518
- * @param blockNumber - The block number at which to get the index.
519
- * @param nullifier - Nullifier we try to find witness for.
520
- * @returns The nullifier membership witness (if found).
521
- */ async getNullifierMembershipWitness(blockNumber, nullifier) {
522
- const db = await this.#getWorldState(blockNumber);
523
- const index = (await db.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [
1109
+ async getNullifierMembershipWitness(referenceBlock, nullifier) {
1110
+ const db = await this.#getWorldState(referenceBlock);
1111
+ const [witness] = await db.findSiblingPaths(MerkleTreeId.NULLIFIER_TREE, [
524
1112
  nullifier.toBuffer()
525
- ]))[0];
526
- if (!index) {
1113
+ ]);
1114
+ if (!witness) {
527
1115
  return undefined;
528
1116
  }
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
- ]);
1117
+ const { index, path } = witness;
1118
+ const leafPreimage = await db.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
535
1119
  if (!leafPreimage) {
536
1120
  return undefined;
537
1121
  }
538
- return new NullifierMembershipWitness(BigInt(index), leafPreimage, siblingPath);
1122
+ return new NullifierMembershipWitness(index, leafPreimage, path);
539
1123
  }
540
1124
  /**
541
1125
  * Returns a low nullifier membership witness for a given nullifier at a given block.
542
- * @param blockNumber - The block number at which to get the index.
1126
+ * @param referenceBlock - The block parameter (block number, block hash, or 'latest') at which to get the data
1127
+ * (which contains the root of the nullifier tree in which we are searching for the nullifier).
543
1128
  * @param nullifier - Nullifier we try to find the low nullifier witness for.
544
1129
  * @returns The low nullifier membership witness (if found).
545
1130
  * @remarks Low nullifier witness can be used to perform a nullifier non-inclusion proof by leveraging the "linked
@@ -550,8 +1135,8 @@ import { NodeMetrics } from './node_metrics.js';
550
1135
  * the nullifier already exists in the tree. This is because the `getPreviousValueIndex` function returns the
551
1136
  * index of the nullifier itself when it already exists in the tree.
552
1137
  * TODO: This is a confusing behavior and we should eventually address that.
553
- */ async getLowNullifierMembershipWitness(blockNumber, nullifier) {
554
- const committedDb = await this.#getWorldState(blockNumber);
1138
+ */ async getLowNullifierMembershipWitness(referenceBlock, nullifier) {
1139
+ const committedDb = await this.#getWorldState(referenceBlock);
555
1140
  const findResult = await committedDb.getPreviousValueIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBigInt());
556
1141
  if (!findResult) {
557
1142
  return undefined;
@@ -564,8 +1149,8 @@ import { NodeMetrics } from './node_metrics.js';
564
1149
  const siblingPath = await committedDb.getSiblingPath(MerkleTreeId.NULLIFIER_TREE, BigInt(index));
565
1150
  return new NullifierMembershipWitness(BigInt(index), preimageData, siblingPath);
566
1151
  }
567
- async getPublicDataTreeWitness(blockNumber, leafSlot) {
568
- const committedDb = await this.#getWorldState(blockNumber);
1152
+ async getPublicDataWitness(referenceBlock, leafSlot) {
1153
+ const committedDb = await this.#getWorldState(referenceBlock);
569
1154
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
570
1155
  if (!lowLeafResult) {
571
1156
  return undefined;
@@ -575,53 +1160,78 @@ import { NodeMetrics } from './node_metrics.js';
575
1160
  return new PublicDataWitness(lowLeafResult.index, preimage, path);
576
1161
  }
577
1162
  }
578
- /**
579
- * Gets the storage value at the given contract storage slot.
580
- *
581
- * @remarks The storage slot here refers to the slot as it is defined in Noir not the index in the merkle tree.
582
- * Aztec's version of `eth_getStorageAt`.
583
- *
584
- * @param contract - Address of the contract to query.
585
- * @param slot - Slot to query.
586
- * @param blockNumber - The block number at which to get the data or 'latest'.
587
- * @returns Storage value at the given contract slot.
588
- */ async getPublicStorageAt(blockNumber, contract, slot) {
589
- const committedDb = await this.#getWorldState(blockNumber);
1163
+ async getPublicStorageAt(referenceBlock, contract, slot) {
1164
+ const committedDb = await this.#getWorldState(referenceBlock);
590
1165
  const leafSlot = await computePublicDataTreeLeafSlot(contract, slot);
591
1166
  const lowLeafResult = await committedDb.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot.toBigInt());
592
1167
  if (!lowLeafResult || !lowLeafResult.alreadyPresent) {
593
1168
  return Fr.ZERO;
594
1169
  }
595
1170
  const preimage = await committedDb.getLeafPreimage(MerkleTreeId.PUBLIC_DATA_TREE, lowLeafResult.index);
596
- return preimage.value;
1171
+ return preimage.leaf.value;
1172
+ }
1173
+ async getBlockHeader(block = 'latest') {
1174
+ if (BlockHash.isBlockHash(block)) {
1175
+ const initialBlockHash = await this.#getInitialHeaderHash();
1176
+ if (block.equals(initialBlockHash)) {
1177
+ // Block source doesn't handle initial header so we need to handle the case separately.
1178
+ return this.worldStateSynchronizer.getCommitted().getInitialHeader();
1179
+ }
1180
+ return this.blockSource.getBlockHeaderByHash(block);
1181
+ } else {
1182
+ // Block source doesn't handle initial header so we need to handle the case separately.
1183
+ const blockNumber = block === 'latest' ? await this.getBlockNumber() : block;
1184
+ if (blockNumber === BlockNumber.ZERO) {
1185
+ return this.worldStateSynchronizer.getCommitted().getInitialHeader();
1186
+ }
1187
+ return this.blockSource.getBlockHeader(block);
1188
+ }
597
1189
  }
598
1190
  /**
599
- * Returns the currently committed block header, or the initial header if no blocks have been produced.
600
- * @returns The current committed block header.
601
- */ async getBlockHeader(blockNumber = 'latest') {
602
- return blockNumber === 0 || blockNumber === 'latest' && await this.blockSource.getBlockNumber() === 0 ? this.worldStateSynchronizer.getCommitted().getInitialHeader() : this.blockSource.getBlockHeader(blockNumber);
1191
+ * Get a block header specified by its archive root.
1192
+ * @param archive - The archive root being requested.
1193
+ * @returns The requested block header.
1194
+ */ async getBlockHeaderByArchive(archive) {
1195
+ return await this.blockSource.getBlockHeaderByArchive(archive);
603
1196
  }
604
1197
  /**
605
1198
  * Simulates the public part of a transaction with the current state.
606
1199
  * @param tx - The transaction to simulate.
607
1200
  **/ async simulatePublicCalls(tx, skipFeeEnforcement = false) {
608
- const txHash = await tx.getTxHash();
609
- const blockNumber = await this.blockSource.getBlockNumber() + 1;
1201
+ // Check total gas limit for simulation
1202
+ const gasSettings = tx.data.constants.txContext.gasSettings;
1203
+ const txGasLimit = gasSettings.gasLimits.l2Gas;
1204
+ const teardownGasLimit = gasSettings.teardownGasLimits.l2Gas;
1205
+ if (txGasLimit + teardownGasLimit > this.config.rpcSimulatePublicMaxGasLimit) {
1206
+ throw new BadRequestError(`Transaction total gas limit ${txGasLimit + teardownGasLimit} (${txGasLimit} + ${teardownGasLimit}) exceeds maximum gas limit ${this.config.rpcSimulatePublicMaxGasLimit} for simulation`);
1207
+ }
1208
+ const txHash = tx.getTxHash();
1209
+ const blockNumber = BlockNumber(await this.blockSource.getBlockNumber() + 1);
610
1210
  // 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);
614
- const publicProcessorFactory = new PublicProcessorFactory(this.contractDataSource, new DateProvider(), this.telemetry);
615
- const fork = await this.worldStateSynchronizer.fork();
1211
+ const coinbase = EthAddress.ZERO;
1212
+ const feeRecipient = AztecAddress.ZERO;
1213
+ const newGlobalVariables = await this.globalVariableBuilder.buildGlobalVariables(blockNumber, coinbase, feeRecipient);
1214
+ const publicProcessorFactory = new PublicProcessorFactory(this.contractDataSource, new DateProvider(), this.telemetry, this.log.getBindings());
616
1215
  this.log.verbose(`Simulating public calls for tx ${txHash}`, {
617
1216
  globalVariables: newGlobalVariables.toInspect(),
618
1217
  txHash,
619
1218
  blockNumber
620
1219
  });
1220
+ const merkleTreeFork = await this.worldStateSynchronizer.fork();
621
1221
  try {
622
- const processor = publicProcessorFactory.create(fork, newGlobalVariables, skipFeeEnforcement);
1222
+ const config = PublicSimulatorConfig.from({
1223
+ skipFeeEnforcement,
1224
+ collectDebugLogs: true,
1225
+ collectHints: false,
1226
+ collectCallMetadata: true,
1227
+ collectStatistics: false,
1228
+ collectionLimits: CollectionLimitsConfig.from({
1229
+ maxDebugLogMemoryReads: this.config.rpcSimulatePublicMaxDebugLogMemoryReads
1230
+ })
1231
+ });
1232
+ const processor = publicProcessorFactory.create(merkleTreeFork, newGlobalVariables, config);
623
1233
  // REFACTOR: Consider merging ProcessReturnValues into ProcessedTx
624
- const [processedTxs, failedTxs, returns] = await processor.process([
1234
+ const [processedTxs, failedTxs, _usedTxs, returns] = await processor.process([
625
1235
  tx
626
1236
  ]);
627
1237
  // REFACTOR: Consider returning the error rather than throwing
@@ -632,30 +1242,47 @@ import { NodeMetrics } from './node_metrics.js';
632
1242
  throw failedTxs[0].error;
633
1243
  }
634
1244
  const [processedTx] = processedTxs;
635
- return new PublicSimulationOutput(processedTx.revertReason, processedTx.constants, processedTx.txEffect, returns, processedTx.gasUsed);
1245
+ return new PublicSimulationOutput(processedTx.revertReason, processedTx.globalVariables, processedTx.txEffect, returns, processedTx.gasUsed);
636
1246
  } finally{
637
- await fork.close();
1247
+ await merkleTreeFork.close();
638
1248
  }
639
1249
  }
640
1250
  async isValidTx(tx, { isSimulation, skipFeeEnforcement } = {}) {
641
- const blockNumber = await this.blockSource.getBlockNumber() + 1;
642
1251
  const db = this.worldStateSynchronizer.getCommitted();
643
1252
  const verifier = isSimulation ? undefined : this.proofVerifier;
1253
+ // We accept transactions if they are not expired by the next slot (checked based on the IncludeByTimestamp field)
1254
+ const { ts: nextSlotTimestamp } = this.epochCache.getEpochAndSlotInNextL1Slot();
1255
+ const blockNumber = BlockNumber(await this.blockSource.getBlockNumber() + 1);
644
1256
  const validator = createValidatorForAcceptingTxs(db, this.contractDataSource, verifier, {
1257
+ timestamp: nextSlotTimestamp,
645
1258
  blockNumber,
646
1259
  l1ChainId: this.l1ChainId,
647
- setupAllowList: this.config.allowedInSetup ?? await getDefaultAllowedSetupFunctions(),
648
- gasFees: await this.getCurrentBaseFees(),
649
- skipFeeEnforcement
650
- });
1260
+ rollupVersion: this.version,
1261
+ setupAllowList: this.config.txPublicSetupAllowList ?? await getDefaultAllowedSetupFunctions(),
1262
+ gasFees: await this.getCurrentMinFees(),
1263
+ skipFeeEnforcement,
1264
+ txsPermitted: !this.config.disableTransactions
1265
+ }, this.log.getBindings());
651
1266
  return await validator.validateTx(tx);
652
1267
  }
1268
+ getConfig() {
1269
+ const schema = AztecNodeAdminConfigSchema;
1270
+ const keys = schema.keyof().options;
1271
+ return Promise.resolve(pick(this.config, ...keys));
1272
+ }
653
1273
  async setConfig(config) {
654
1274
  const newConfig = {
655
1275
  ...this.config,
656
1276
  ...config
657
1277
  };
658
- await this.sequencer?.updateSequencerConfig(config);
1278
+ this.sequencer?.updateConfig(config);
1279
+ this.slasherClient?.updateConfig(config);
1280
+ this.validatorsSentinel?.updateConfig(config);
1281
+ await this.p2pClient.updateP2PConfig(config);
1282
+ const archiver = this.blockSource;
1283
+ if ('updateConfig' in archiver) {
1284
+ archiver.updateConfig(config);
1285
+ }
659
1286
  if (newConfig.realProofs !== this.config.realProofs) {
660
1287
  this.proofVerifier = config.realProofs ? await BBCircuitVerifier.new(newConfig) : new TestCircuitVerifier();
661
1288
  }
@@ -663,51 +1290,164 @@ import { NodeMetrics } from './node_metrics.js';
663
1290
  }
664
1291
  getProtocolContractAddresses() {
665
1292
  return Promise.resolve({
666
- classRegisterer: ProtocolContractAddress.ContractClassRegisterer,
1293
+ classRegistry: ProtocolContractAddress.ContractClassRegistry,
667
1294
  feeJuice: ProtocolContractAddress.FeeJuice,
668
- instanceDeployer: ProtocolContractAddress.ContractInstanceDeployer,
1295
+ instanceRegistry: ProtocolContractAddress.ContractInstanceRegistry,
669
1296
  multiCallEntrypoint: ProtocolContractAddress.MultiCallEntrypoint
670
1297
  });
671
1298
  }
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);
1299
+ registerContractFunctionSignatures(signatures) {
1300
+ return this.contractDataSource.registerContractFunctionSignatures(signatures);
1301
+ }
1302
+ getValidatorsStats() {
1303
+ return this.validatorsSentinel?.computeStats() ?? Promise.resolve({
1304
+ stats: {},
1305
+ slotWindow: 0
1306
+ });
676
1307
  }
677
- registerContractFunctionSignatures(_address, signatures) {
678
- return this.contractDataSource.registerContractFunctionSignatures(_address, signatures);
1308
+ getValidatorStats(validatorAddress, fromSlot, toSlot) {
1309
+ return this.validatorsSentinel?.getValidatorStats(validatorAddress, fromSlot, toSlot) ?? Promise.resolve(undefined);
1310
+ }
1311
+ async startSnapshotUpload(location) {
1312
+ // Note that we are forcefully casting the blocksource as an archiver
1313
+ // We break support for archiver running remotely to the node
1314
+ const archiver = this.blockSource;
1315
+ if (!('backupTo' in archiver)) {
1316
+ this.metrics.recordSnapshotError();
1317
+ throw new Error('Archiver implementation does not support backups. Cannot generate snapshot.');
1318
+ }
1319
+ // Test that the archiver has done an initial sync.
1320
+ if (!archiver.isInitialSyncComplete()) {
1321
+ this.metrics.recordSnapshotError();
1322
+ throw new Error(`Archiver initial sync not complete. Cannot start snapshot.`);
1323
+ }
1324
+ // And it has an L2 block hash
1325
+ const l2BlockHash = await archiver.getL2Tips().then((tips)=>tips.proposed.hash);
1326
+ if (!l2BlockHash) {
1327
+ this.metrics.recordSnapshotError();
1328
+ throw new Error(`Archiver has no latest L2 block hash downloaded. Cannot start snapshot.`);
1329
+ }
1330
+ if (this.isUploadingSnapshot) {
1331
+ this.metrics.recordSnapshotError();
1332
+ throw new Error(`Snapshot upload already in progress. Cannot start another one until complete.`);
1333
+ }
1334
+ // Do not wait for the upload to be complete to return to the caller, but flag that an operation is in progress
1335
+ this.isUploadingSnapshot = true;
1336
+ const timer = new Timer();
1337
+ void uploadSnapshot(location, this.blockSource, this.worldStateSynchronizer, this.config, this.log).then(()=>{
1338
+ this.isUploadingSnapshot = false;
1339
+ this.metrics.recordSnapshot(timer.ms());
1340
+ }).catch((err)=>{
1341
+ this.isUploadingSnapshot = false;
1342
+ this.metrics.recordSnapshotError();
1343
+ this.log.error(`Error uploading snapshot: ${err}`);
1344
+ });
1345
+ return Promise.resolve();
679
1346
  }
680
- flushTxs() {
681
- if (!this.sequencer) {
682
- throw new Error(`Sequencer is not initialized`);
1347
+ async rollbackTo(targetBlock, force) {
1348
+ const archiver = this.blockSource;
1349
+ if (!('rollbackTo' in archiver)) {
1350
+ throw new Error('Archiver implementation does not support rollbacks.');
1351
+ }
1352
+ const finalizedBlock = await archiver.getL2Tips().then((tips)=>tips.finalized.block.number);
1353
+ if (targetBlock < finalizedBlock) {
1354
+ if (force) {
1355
+ this.log.warn(`Clearing world state database to allow rolling back behind finalized block ${finalizedBlock}`);
1356
+ await this.worldStateSynchronizer.clear();
1357
+ await this.p2pClient.clear();
1358
+ } else {
1359
+ throw new Error(`Cannot rollback to block ${targetBlock} as it is before finalized ${finalizedBlock}`);
1360
+ }
1361
+ }
1362
+ try {
1363
+ this.log.info(`Pausing archiver and world state sync to start rollback`);
1364
+ await archiver.stop();
1365
+ await this.worldStateSynchronizer.stopSync();
1366
+ const currentBlock = await archiver.getBlockNumber();
1367
+ const blocksToUnwind = currentBlock - targetBlock;
1368
+ this.log.info(`Unwinding ${count(blocksToUnwind, 'block')} from L2 block ${currentBlock} to ${targetBlock}`);
1369
+ await archiver.rollbackTo(targetBlock);
1370
+ this.log.info(`Unwinding complete.`);
1371
+ } catch (err) {
1372
+ this.log.error(`Error during rollback`, err);
1373
+ throw err;
1374
+ } finally{
1375
+ this.log.info(`Resuming world state and archiver sync.`);
1376
+ this.worldStateSynchronizer.resumeSync();
1377
+ archiver.resume();
683
1378
  }
684
- this.sequencer.flush();
1379
+ }
1380
+ async pauseSync() {
1381
+ this.log.info(`Pausing archiver and world state sync`);
1382
+ await this.blockSource.stop();
1383
+ await this.worldStateSynchronizer.stopSync();
1384
+ }
1385
+ resumeSync() {
1386
+ this.log.info(`Resuming world state and archiver sync.`);
1387
+ this.worldStateSynchronizer.resumeSync();
1388
+ this.blockSource.resume();
685
1389
  return Promise.resolve();
686
1390
  }
1391
+ getSlashPayloads() {
1392
+ if (!this.slasherClient) {
1393
+ throw new Error(`Slasher client not enabled`);
1394
+ }
1395
+ return this.slasherClient.getSlashPayloads();
1396
+ }
1397
+ getSlashOffenses(round) {
1398
+ if (!this.slasherClient) {
1399
+ throw new Error(`Slasher client not enabled`);
1400
+ }
1401
+ if (round === 'all') {
1402
+ return this.slasherClient.getPendingOffenses();
1403
+ } else {
1404
+ return this.slasherClient.gatherOffensesForRound(round === 'current' ? undefined : BigInt(round));
1405
+ }
1406
+ }
1407
+ #getInitialHeaderHash() {
1408
+ if (!this.initialHeaderHashPromise) {
1409
+ this.initialHeaderHashPromise = this.worldStateSynchronizer.getCommitted().getInitialHeader().hash();
1410
+ }
1411
+ return this.initialHeaderHashPromise;
1412
+ }
687
1413
  /**
688
1414
  * Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
689
- * @param blockNumber - The block number at which to get the data.
1415
+ * @param block - The block parameter (block number, block hash, or 'latest') at which to get the data.
690
1416
  * @returns An instance of a committed MerkleTreeOperations
691
- */ async #getWorldState(blockNumber) {
692
- if (typeof blockNumber === 'number' && blockNumber < INITIAL_L2_BLOCK_NUM - 1) {
693
- throw new Error('Invalid block number to get world state for: ' + blockNumber);
694
- }
695
- let blockSyncedTo = 0;
1417
+ */ async #getWorldState(block) {
1418
+ let blockSyncedTo = BlockNumber.ZERO;
696
1419
  try {
697
1420
  // Attempt to sync the world state if necessary
698
1421
  blockSyncedTo = await this.#syncWorldState();
699
1422
  } catch (err) {
700
1423
  this.log.error(`Error getting world state: ${err}`);
701
1424
  }
702
- // using a snapshot could be less efficient than using the committed db
703
- if (blockNumber === 'latest' /*|| blockNumber === blockSyncedTo*/ ) {
704
- this.log.debug(`Using committed db for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1425
+ if (block === 'latest') {
1426
+ this.log.debug(`Using committed db for block 'latest', world state synced upto ${blockSyncedTo}`);
705
1427
  return this.worldStateSynchronizer.getCommitted();
706
- } else if (blockNumber <= blockSyncedTo) {
1428
+ }
1429
+ if (BlockHash.isBlockHash(block)) {
1430
+ const initialBlockHash = await this.#getInitialHeaderHash();
1431
+ if (block.equals(initialBlockHash)) {
1432
+ // Block source doesn't handle initial header so we need to handle the case separately.
1433
+ return this.worldStateSynchronizer.getSnapshot(BlockNumber.ZERO);
1434
+ }
1435
+ const header = await this.blockSource.getBlockHeaderByHash(block);
1436
+ if (!header) {
1437
+ throw new Error(`Block hash ${block.toString()} not found when querying world state. If the node API has been queried with anchor block hash possibly a reorg has occurred.`);
1438
+ }
1439
+ const blockNumber = header.getBlockNumber();
1440
+ this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
1441
+ return this.worldStateSynchronizer.getSnapshot(blockNumber);
1442
+ }
1443
+ // Block number provided
1444
+ {
1445
+ const blockNumber = block;
1446
+ if (blockNumber > blockSyncedTo) {
1447
+ throw new Error(`Queried block ${block} not yet synced by the node (node is synced upto ${blockSyncedTo}).`);
1448
+ }
707
1449
  this.log.debug(`Using snapshot for block ${blockNumber}, world state synced upto ${blockSyncedTo}`);
708
1450
  return this.worldStateSynchronizer.getSnapshot(blockNumber);
709
- } else {
710
- throw new Error(`Block ${blockNumber} not yet synced`);
711
1451
  }
712
1452
  }
713
1453
  /**
@@ -715,11 +1455,6 @@ import { NodeMetrics } from './node_metrics.js';
715
1455
  * @returns A promise that fulfils once the world state is synced
716
1456
  */ async #syncWorldState() {
717
1457
  const blockSourceHeight = await this.blockSource.getBlockNumber();
718
- return this.worldStateSynchronizer.syncImmediate(blockSourceHeight);
1458
+ return await this.worldStateSynchronizer.syncImmediate(blockSourceHeight);
719
1459
  }
720
1460
  }
721
- _ts_decorate([
722
- trackSpan('AztecNodeService.simulatePublicCalls', async (tx)=>({
723
- [Attributes.TX_HASH]: (await tx.getTxHash()).toString()
724
- }))
725
- ], AztecNodeService.prototype, "simulatePublicCalls", null);