@aztec/prover-node 0.0.1-commit.2f68f620 → 0.0.1-commit.321f6a9

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 (61) hide show
  1. package/README.md +572 -0
  2. package/dest/actions/rerun-epoch-proving-job.d.ts +12 -3
  3. package/dest/actions/rerun-epoch-proving-job.d.ts.map +1 -1
  4. package/dest/actions/rerun-epoch-proving-job.js +170 -28
  5. package/dest/checkpoint-store.d.ts +95 -0
  6. package/dest/checkpoint-store.d.ts.map +1 -0
  7. package/dest/checkpoint-store.js +178 -0
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +1 -1
  11. package/dest/factory.d.ts +4 -1
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +1 -6
  14. package/dest/index.d.ts +2 -1
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -0
  17. package/dest/job/checkpoint-prover.d.ts +154 -0
  18. package/dest/job/checkpoint-prover.d.ts.map +1 -0
  19. package/dest/job/checkpoint-prover.js +363 -0
  20. package/dest/job/epoch-session.d.ts +160 -0
  21. package/dest/job/epoch-session.d.ts.map +1 -0
  22. package/dest/job/epoch-session.js +744 -0
  23. package/dest/job/top-tree-job.d.ts +82 -0
  24. package/dest/job/top-tree-job.d.ts.map +1 -0
  25. package/dest/job/top-tree-job.js +152 -0
  26. package/dest/metrics.d.ts +25 -8
  27. package/dest/metrics.d.ts.map +1 -1
  28. package/dest/metrics.js +64 -14
  29. package/dest/proof-publishing-service.d.ts +161 -0
  30. package/dest/proof-publishing-service.d.ts.map +1 -0
  31. package/dest/proof-publishing-service.js +335 -0
  32. package/dest/prover-node-publisher.d.ts +7 -19
  33. package/dest/prover-node-publisher.d.ts.map +1 -1
  34. package/dest/prover-node-publisher.js +38 -96
  35. package/dest/prover-node.d.ts +133 -66
  36. package/dest/prover-node.d.ts.map +1 -1
  37. package/dest/prover-node.js +529 -255
  38. package/dest/session-manager.d.ts +158 -0
  39. package/dest/session-manager.d.ts.map +1 -0
  40. package/dest/session-manager.js +486 -0
  41. package/dest/test/index.d.ts +7 -6
  42. package/dest/test/index.d.ts.map +1 -1
  43. package/package.json +24 -23
  44. package/src/actions/rerun-epoch-proving-job.ts +178 -30
  45. package/src/checkpoint-store.ts +212 -0
  46. package/src/config.ts +2 -1
  47. package/src/factory.ts +4 -8
  48. package/src/index.ts +1 -0
  49. package/src/job/checkpoint-prover.ts +496 -0
  50. package/src/job/epoch-session.ts +462 -0
  51. package/src/job/top-tree-job.ts +227 -0
  52. package/src/metrics.ts +65 -23
  53. package/src/proof-publishing-service.ts +427 -0
  54. package/src/prover-node-publisher.ts +52 -121
  55. package/src/prover-node.ts +617 -277
  56. package/src/session-manager.ts +589 -0
  57. package/src/test/index.ts +6 -6
  58. package/dest/job/epoch-proving-job.d.ts +0 -67
  59. package/dest/job/epoch-proving-job.d.ts.map +0 -1
  60. package/dest/job/epoch-proving-job.js +0 -912
  61. package/src/job/epoch-proving-job.ts +0 -531
@@ -1,912 +0,0 @@
1
- function _ts_add_disposable_resource(env, value, async) {
2
- if (value !== null && value !== void 0) {
3
- if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
- var dispose, inner;
5
- if (async) {
6
- if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
- dispose = value[Symbol.asyncDispose];
8
- }
9
- if (dispose === void 0) {
10
- if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
- dispose = value[Symbol.dispose];
12
- if (async) inner = dispose;
13
- }
14
- if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
- if (inner) dispose = function() {
16
- try {
17
- inner.call(this);
18
- } catch (e) {
19
- return Promise.reject(e);
20
- }
21
- };
22
- env.stack.push({
23
- value: value,
24
- dispose: dispose,
25
- async: async
26
- });
27
- } else if (async) {
28
- env.stack.push({
29
- async: true
30
- });
31
- }
32
- return value;
33
- }
34
- function _ts_dispose_resources(env) {
35
- var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
36
- var e = new Error(message);
37
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
38
- };
39
- return (_ts_dispose_resources = function _ts_dispose_resources(env) {
40
- function fail(e) {
41
- env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
42
- env.hasError = true;
43
- }
44
- var r, s = 0;
45
- function next() {
46
- while(r = env.stack.pop()){
47
- try {
48
- if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
49
- if (r.dispose) {
50
- var result = r.dispose.call(r.value);
51
- if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
52
- fail(e);
53
- return next();
54
- });
55
- } else s |= 1;
56
- } catch (e) {
57
- fail(e);
58
- }
59
- }
60
- if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
61
- if (env.hasError) throw env.error;
62
- }
63
- return next();
64
- })(env);
65
- }
66
- function applyDecs2203RFactory() {
67
- function createAddInitializerMethod(initializers, decoratorFinishedRef) {
68
- return function addInitializer(initializer) {
69
- assertNotFinished(decoratorFinishedRef, "addInitializer");
70
- assertCallable(initializer, "An initializer");
71
- initializers.push(initializer);
72
- };
73
- }
74
- function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
75
- var kindStr;
76
- switch(kind){
77
- case 1:
78
- kindStr = "accessor";
79
- break;
80
- case 2:
81
- kindStr = "method";
82
- break;
83
- case 3:
84
- kindStr = "getter";
85
- break;
86
- case 4:
87
- kindStr = "setter";
88
- break;
89
- default:
90
- kindStr = "field";
91
- }
92
- var ctx = {
93
- kind: kindStr,
94
- name: isPrivate ? "#" + name : name,
95
- static: isStatic,
96
- private: isPrivate,
97
- metadata: metadata
98
- };
99
- var decoratorFinishedRef = {
100
- v: false
101
- };
102
- ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
103
- var get, set;
104
- if (kind === 0) {
105
- if (isPrivate) {
106
- get = desc.get;
107
- set = desc.set;
108
- } else {
109
- get = function() {
110
- return this[name];
111
- };
112
- set = function(v) {
113
- this[name] = v;
114
- };
115
- }
116
- } else if (kind === 2) {
117
- get = function() {
118
- return desc.value;
119
- };
120
- } else {
121
- if (kind === 1 || kind === 3) {
122
- get = function() {
123
- return desc.get.call(this);
124
- };
125
- }
126
- if (kind === 1 || kind === 4) {
127
- set = function(v) {
128
- desc.set.call(this, v);
129
- };
130
- }
131
- }
132
- ctx.access = get && set ? {
133
- get: get,
134
- set: set
135
- } : get ? {
136
- get: get
137
- } : {
138
- set: set
139
- };
140
- try {
141
- return dec(value, ctx);
142
- } finally{
143
- decoratorFinishedRef.v = true;
144
- }
145
- }
146
- function assertNotFinished(decoratorFinishedRef, fnName) {
147
- if (decoratorFinishedRef.v) {
148
- throw new Error("attempted to call " + fnName + " after decoration was finished");
149
- }
150
- }
151
- function assertCallable(fn, hint) {
152
- if (typeof fn !== "function") {
153
- throw new TypeError(hint + " must be a function");
154
- }
155
- }
156
- function assertValidReturnValue(kind, value) {
157
- var type = typeof value;
158
- if (kind === 1) {
159
- if (type !== "object" || value === null) {
160
- throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
161
- }
162
- if (value.get !== undefined) {
163
- assertCallable(value.get, "accessor.get");
164
- }
165
- if (value.set !== undefined) {
166
- assertCallable(value.set, "accessor.set");
167
- }
168
- if (value.init !== undefined) {
169
- assertCallable(value.init, "accessor.init");
170
- }
171
- } else if (type !== "function") {
172
- var hint;
173
- if (kind === 0) {
174
- hint = "field";
175
- } else if (kind === 10) {
176
- hint = "class";
177
- } else {
178
- hint = "method";
179
- }
180
- throw new TypeError(hint + " decorators must return a function or void 0");
181
- }
182
- }
183
- function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
184
- var decs = decInfo[0];
185
- var desc, init, value;
186
- if (isPrivate) {
187
- if (kind === 0 || kind === 1) {
188
- desc = {
189
- get: decInfo[3],
190
- set: decInfo[4]
191
- };
192
- } else if (kind === 3) {
193
- desc = {
194
- get: decInfo[3]
195
- };
196
- } else if (kind === 4) {
197
- desc = {
198
- set: decInfo[3]
199
- };
200
- } else {
201
- desc = {
202
- value: decInfo[3]
203
- };
204
- }
205
- } else if (kind !== 0) {
206
- desc = Object.getOwnPropertyDescriptor(base, name);
207
- }
208
- if (kind === 1) {
209
- value = {
210
- get: desc.get,
211
- set: desc.set
212
- };
213
- } else if (kind === 2) {
214
- value = desc.value;
215
- } else if (kind === 3) {
216
- value = desc.get;
217
- } else if (kind === 4) {
218
- value = desc.set;
219
- }
220
- var newValue, get, set;
221
- if (typeof decs === "function") {
222
- newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
223
- if (newValue !== void 0) {
224
- assertValidReturnValue(kind, newValue);
225
- if (kind === 0) {
226
- init = newValue;
227
- } else if (kind === 1) {
228
- init = newValue.init;
229
- get = newValue.get || value.get;
230
- set = newValue.set || value.set;
231
- value = {
232
- get: get,
233
- set: set
234
- };
235
- } else {
236
- value = newValue;
237
- }
238
- }
239
- } else {
240
- for(var i = decs.length - 1; i >= 0; i--){
241
- var dec = decs[i];
242
- newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
243
- if (newValue !== void 0) {
244
- assertValidReturnValue(kind, newValue);
245
- var newInit;
246
- if (kind === 0) {
247
- newInit = newValue;
248
- } else if (kind === 1) {
249
- newInit = newValue.init;
250
- get = newValue.get || value.get;
251
- set = newValue.set || value.set;
252
- value = {
253
- get: get,
254
- set: set
255
- };
256
- } else {
257
- value = newValue;
258
- }
259
- if (newInit !== void 0) {
260
- if (init === void 0) {
261
- init = newInit;
262
- } else if (typeof init === "function") {
263
- init = [
264
- init,
265
- newInit
266
- ];
267
- } else {
268
- init.push(newInit);
269
- }
270
- }
271
- }
272
- }
273
- }
274
- if (kind === 0 || kind === 1) {
275
- if (init === void 0) {
276
- init = function(instance, init) {
277
- return init;
278
- };
279
- } else if (typeof init !== "function") {
280
- var ownInitializers = init;
281
- init = function(instance, init) {
282
- var value = init;
283
- for(var i = 0; i < ownInitializers.length; i++){
284
- value = ownInitializers[i].call(instance, value);
285
- }
286
- return value;
287
- };
288
- } else {
289
- var originalInitializer = init;
290
- init = function(instance, init) {
291
- return originalInitializer.call(instance, init);
292
- };
293
- }
294
- ret.push(init);
295
- }
296
- if (kind !== 0) {
297
- if (kind === 1) {
298
- desc.get = value.get;
299
- desc.set = value.set;
300
- } else if (kind === 2) {
301
- desc.value = value;
302
- } else if (kind === 3) {
303
- desc.get = value;
304
- } else if (kind === 4) {
305
- desc.set = value;
306
- }
307
- if (isPrivate) {
308
- if (kind === 1) {
309
- ret.push(function(instance, args) {
310
- return value.get.call(instance, args);
311
- });
312
- ret.push(function(instance, args) {
313
- return value.set.call(instance, args);
314
- });
315
- } else if (kind === 2) {
316
- ret.push(value);
317
- } else {
318
- ret.push(function(instance, args) {
319
- return value.call(instance, args);
320
- });
321
- }
322
- } else {
323
- Object.defineProperty(base, name, desc);
324
- }
325
- }
326
- }
327
- function applyMemberDecs(Class, decInfos, metadata) {
328
- var ret = [];
329
- var protoInitializers;
330
- var staticInitializers;
331
- var existingProtoNonFields = new Map();
332
- var existingStaticNonFields = new Map();
333
- for(var i = 0; i < decInfos.length; i++){
334
- var decInfo = decInfos[i];
335
- if (!Array.isArray(decInfo)) continue;
336
- var kind = decInfo[1];
337
- var name = decInfo[2];
338
- var isPrivate = decInfo.length > 3;
339
- var isStatic = kind >= 5;
340
- var base;
341
- var initializers;
342
- if (isStatic) {
343
- base = Class;
344
- kind = kind - 5;
345
- staticInitializers = staticInitializers || [];
346
- initializers = staticInitializers;
347
- } else {
348
- base = Class.prototype;
349
- protoInitializers = protoInitializers || [];
350
- initializers = protoInitializers;
351
- }
352
- if (kind !== 0 && !isPrivate) {
353
- var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
354
- var existingKind = existingNonFields.get(name) || 0;
355
- if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
356
- 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);
357
- } else if (!existingKind && kind > 2) {
358
- existingNonFields.set(name, kind);
359
- } else {
360
- existingNonFields.set(name, true);
361
- }
362
- }
363
- applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
364
- }
365
- pushInitializers(ret, protoInitializers);
366
- pushInitializers(ret, staticInitializers);
367
- return ret;
368
- }
369
- function pushInitializers(ret, initializers) {
370
- if (initializers) {
371
- ret.push(function(instance) {
372
- for(var i = 0; i < initializers.length; i++){
373
- initializers[i].call(instance);
374
- }
375
- return instance;
376
- });
377
- }
378
- }
379
- function applyClassDecs(targetClass, classDecs, metadata) {
380
- if (classDecs.length > 0) {
381
- var initializers = [];
382
- var newClass = targetClass;
383
- var name = targetClass.name;
384
- for(var i = classDecs.length - 1; i >= 0; i--){
385
- var decoratorFinishedRef = {
386
- v: false
387
- };
388
- try {
389
- var nextNewClass = classDecs[i](newClass, {
390
- kind: "class",
391
- name: name,
392
- addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
393
- metadata
394
- });
395
- } finally{
396
- decoratorFinishedRef.v = true;
397
- }
398
- if (nextNewClass !== undefined) {
399
- assertValidReturnValue(10, nextNewClass);
400
- newClass = nextNewClass;
401
- }
402
- }
403
- return [
404
- defineMetadata(newClass, metadata),
405
- function() {
406
- for(var i = 0; i < initializers.length; i++){
407
- initializers[i].call(newClass);
408
- }
409
- }
410
- ];
411
- }
412
- }
413
- function defineMetadata(Class, metadata) {
414
- return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
415
- configurable: true,
416
- enumerable: true,
417
- value: metadata
418
- });
419
- }
420
- return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
421
- if (parentClass !== void 0) {
422
- var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
423
- }
424
- var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
425
- var e = applyMemberDecs(targetClass, memberDecs, metadata);
426
- if (!classDecs.length) defineMetadata(targetClass, metadata);
427
- return {
428
- e: e,
429
- get c () {
430
- return applyClassDecs(targetClass, classDecs, metadata);
431
- }
432
- };
433
- };
434
- }
435
- function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
436
- return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
437
- }
438
- var _dec, _initProto;
439
- import { asyncPool } from '@aztec/foundation/async-pool';
440
- import { BlockNumber } from '@aztec/foundation/branded-types';
441
- import { createLogger } from '@aztec/foundation/log';
442
- import { RunningPromise, promiseWithResolvers } from '@aztec/foundation/promise';
443
- import { Timer } from '@aztec/foundation/timer';
444
- import { AVM_MAX_CONCURRENT_SIMULATIONS } from '@aztec/native';
445
- import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
446
- import { protocolContractsHash } from '@aztec/protocol-contracts';
447
- import { buildFinalBlobChallenges } from '@aztec/prover-client/helpers';
448
- import { PublicSimulatorConfig } from '@aztec/stdlib/avm';
449
- import { EpochProvingJobTerminalState } from '@aztec/stdlib/interfaces/server';
450
- import { appendL1ToL2MessagesToTree } from '@aztec/stdlib/messaging';
451
- import { CheckpointConstantData } from '@aztec/stdlib/rollup';
452
- import { Attributes, trackSpan } from '@aztec/telemetry-client';
453
- import * as crypto from 'node:crypto';
454
- import { validateEpochProvingJobData } from './epoch-proving-job-data.js';
455
- _dec = trackSpan('EpochProvingJob.run', function() {
456
- return {
457
- [Attributes.EPOCH_NUMBER]: this.data.epochNumber
458
- };
459
- });
460
- /**
461
- * Job that grabs a range of blocks from the unfinalized chain from L1, gets their txs given their hashes,
462
- * re-executes their public calls, generates a rollup proof, and submits it to L1. This job will update the
463
- * world state as part of public call execution via the public processor.
464
- */ export class EpochProvingJob {
465
- data;
466
- dbProvider;
467
- prover;
468
- publicProcessorFactory;
469
- publisher;
470
- l2BlockSource;
471
- metrics;
472
- deadline;
473
- config;
474
- static{
475
- ({ e: [_initProto] } = _apply_decs_2203_r(this, [
476
- [
477
- _dec,
478
- 2,
479
- "run"
480
- ]
481
- ], []));
482
- }
483
- state;
484
- log;
485
- uuid;
486
- runPromise;
487
- abortController;
488
- epochCheckPromise;
489
- deadlineTimeoutHandler;
490
- tracer;
491
- constructor(data, dbProvider, prover, publicProcessorFactory, publisher, l2BlockSource, metrics, deadline, config, bindings){
492
- this.data = data;
493
- this.dbProvider = dbProvider;
494
- this.prover = prover;
495
- this.publicProcessorFactory = publicProcessorFactory;
496
- this.publisher = publisher;
497
- this.l2BlockSource = l2BlockSource;
498
- this.metrics = metrics;
499
- this.deadline = deadline;
500
- this.config = config;
501
- this.state = (_initProto(this), 'initialized');
502
- this.abortController = new AbortController();
503
- validateEpochProvingJobData(data);
504
- this.uuid = crypto.randomUUID();
505
- this.log = createLogger('prover-node:epoch-proving-job', {
506
- ...bindings,
507
- instanceId: `epoch-${data.epochNumber}`
508
- });
509
- this.tracer = metrics.tracer;
510
- }
511
- getId() {
512
- return this.uuid;
513
- }
514
- getState() {
515
- return this.state;
516
- }
517
- getEpochNumber() {
518
- return this.data.epochNumber;
519
- }
520
- getDeadline() {
521
- return this.deadline;
522
- }
523
- getProvingData() {
524
- return this.data;
525
- }
526
- get epochNumber() {
527
- return this.data.epochNumber;
528
- }
529
- get checkpoints() {
530
- return this.data.checkpoints;
531
- }
532
- get txs() {
533
- return this.data.txs;
534
- }
535
- get attestations() {
536
- return this.data.attestations;
537
- }
538
- /**
539
- * Proves the given epoch and submits the proof to L1.
540
- */ async run() {
541
- this.scheduleDeadlineStop();
542
- if (!this.config.skipEpochCheck) {
543
- await this.scheduleEpochCheck();
544
- }
545
- const attestations = this.attestations.map((attestation)=>attestation.toViem());
546
- const epochNumber = this.epochNumber;
547
- const epochSizeCheckpoints = this.checkpoints.length;
548
- const epochSizeBlocks = this.checkpoints.reduce((accum, checkpoint)=>accum + checkpoint.blocks.length, 0);
549
- const epochSizeTxs = this.checkpoints.reduce((accum, checkpoint)=>accum + checkpoint.blocks.reduce((accumC, block)=>accumC + block.body.txEffects.length, 0), 0);
550
- const fromCheckpoint = this.checkpoints[0].number;
551
- const toCheckpoint = this.checkpoints.at(-1).number;
552
- const fromBlock = this.checkpoints[0].blocks[0].number;
553
- const toBlock = this.checkpoints.at(-1).blocks.at(-1).number;
554
- this.log.info(`Starting epoch ${epochNumber} proving job with checkpoints ${fromCheckpoint} to ${toCheckpoint}`, {
555
- fromBlock,
556
- toBlock,
557
- epochSizeTxs,
558
- epochNumber,
559
- uuid: this.uuid
560
- });
561
- this.progressState('processing');
562
- const timer = new Timer();
563
- const { promise, resolve } = promiseWithResolvers();
564
- this.runPromise = promise;
565
- try {
566
- const blobTimer = new Timer();
567
- const blobFieldsPerCheckpoint = this.checkpoints.map((checkpoint)=>checkpoint.toBlobFields());
568
- const finalBlobBatchingChallenges = await buildFinalBlobChallenges(blobFieldsPerCheckpoint);
569
- this.metrics.recordBlobProcessing(blobTimer.ms());
570
- this.prover.startNewEpoch(epochNumber, epochSizeCheckpoints, finalBlobBatchingChallenges);
571
- const chonkTimer = new Timer();
572
- await this.prover.startChonkVerifierCircuits(Array.from(this.txs.values()));
573
- this.metrics.recordChonkVerifier(chonkTimer.ms());
574
- // Everything in the epoch should have the same chainId and version.
575
- const { chainId, version } = this.checkpoints[0].blocks[0].header.globalVariables;
576
- const previousBlockHeaders = this.gatherPreviousBlockHeaders();
577
- const allCheckpointsTimer = new Timer();
578
- const parallelism = this.config.parallelBlockLimit ? this.config.parallelBlockLimit : AVM_MAX_CONCURRENT_SIMULATIONS > 0 ? AVM_MAX_CONCURRENT_SIMULATIONS : this.checkpoints.length;
579
- await this.processCheckpoints(parallelism, async (checkpoint)=>{
580
- this.checkState();
581
- const checkpointTimer = new Timer();
582
- const checkpointIndex = checkpoint.number - fromCheckpoint;
583
- const checkpointConstants = CheckpointConstantData.from({
584
- chainId,
585
- version,
586
- vkTreeRoot: getVKTreeRoot(),
587
- protocolContractsHash: protocolContractsHash,
588
- proverId: this.prover.getProverId().toField(),
589
- slotNumber: checkpoint.header.slotNumber,
590
- coinbase: checkpoint.header.coinbase,
591
- feeRecipient: checkpoint.header.feeRecipient,
592
- gasFees: checkpoint.header.gasFees
593
- });
594
- const previousHeader = previousBlockHeaders[checkpointIndex];
595
- const l1ToL2Messages = this.getL1ToL2Messages(checkpoint);
596
- this.log.debug(`Starting processing checkpoint ${checkpoint.number}`, {
597
- number: checkpoint.number,
598
- checkpointHash: checkpoint.hash().toString(),
599
- headerHash: checkpoint.header.hash().toString(),
600
- numL1ToL2Messages: l1ToL2Messages.length,
601
- previousBlockNumber: previousHeader.globalVariables.blockNumber,
602
- uuid: this.uuid
603
- });
604
- await this.prover.startNewCheckpoint(checkpointIndex, checkpointConstants, l1ToL2Messages, checkpoint.blocks.length, previousHeader);
605
- for(let blockIndex = 0; blockIndex < checkpoint.blocks.length; blockIndex++){
606
- const blockTimer = new Timer();
607
- const block = checkpoint.blocks[blockIndex];
608
- const globalVariables = block.header.globalVariables;
609
- const txs = this.getTxs(block);
610
- this.log.verbose(`Starting processing block ${block.number}`, {
611
- number: block.number,
612
- blockHash: (await block.hash()).toString(),
613
- lastArchive: block.header.lastArchive.root,
614
- noteHashTreeRoot: block.header.state.partial.noteHashTree.root,
615
- nullifierTreeRoot: block.header.state.partial.nullifierTree.root,
616
- publicDataTreeRoot: block.header.state.partial.publicDataTree.root,
617
- ...globalVariables,
618
- numTxs: txs.length
619
- });
620
- // Start block proving
621
- await this.prover.startNewBlock(block.number, globalVariables.timestamp, txs.length);
622
- // Process public fns. L1 to L2 messages are only inserted for the first block of a checkpoint,
623
- // as the fork for subsequent blocks already includes them from the previous block's synced state.
624
- {
625
- const env = {
626
- stack: [],
627
- error: void 0,
628
- hasError: false
629
- };
630
- try {
631
- const db = _ts_add_disposable_resource(env, await this.createFork(BlockNumber(block.number - 1), blockIndex === 0 ? l1ToL2Messages : undefined), true);
632
- this.checkState();
633
- const config = PublicSimulatorConfig.from({
634
- proverId: this.prover.getProverId().toField(),
635
- skipFeeEnforcement: false,
636
- collectDebugLogs: false,
637
- collectHints: true,
638
- collectPublicInputs: true,
639
- collectStatistics: false
640
- });
641
- const publicProcessor = this.publicProcessorFactory.create(db, globalVariables, config);
642
- const processed = await this.processTxs(publicProcessor, txs);
643
- this.checkState();
644
- await this.prover.addTxs(processed);
645
- } catch (e) {
646
- env.error = e;
647
- env.hasError = true;
648
- } finally{
649
- const result = _ts_dispose_resources(env);
650
- if (result) await result;
651
- }
652
- }
653
- this.checkState();
654
- this.log.verbose(`Processed all ${txs.length} txs for block ${block.number}`, {
655
- blockNumber: block.number,
656
- blockHash: (await block.hash()).toString(),
657
- uuid: this.uuid
658
- });
659
- // Mark block as completed to pad it
660
- const expectedBlockHeader = block.header;
661
- await this.prover.setBlockCompleted(block.number, expectedBlockHeader);
662
- this.metrics.recordBlockProcessing(blockTimer.ms());
663
- }
664
- this.metrics.recordCheckpointProcessing(checkpointTimer.ms());
665
- });
666
- this.metrics.recordAllCheckpointsProcessing(allCheckpointsTimer.ms());
667
- const executionTime = timer.ms();
668
- this.progressState('awaiting-prover');
669
- const { publicInputs, proof, batchedBlobInputs } = await this.prover.finalizeEpoch();
670
- this.log.info(`Finalized proof for epoch ${epochNumber}`, {
671
- epochNumber,
672
- uuid: this.uuid,
673
- duration: timer.ms()
674
- });
675
- this.progressState('publishing-proof');
676
- if (this.config.skipSubmitProof) {
677
- this.log.info(`Proof publishing is disabled. Analyzing estimated L1 fees for epoch ${epochNumber} (checkpoints ${fromCheckpoint} to ${toCheckpoint})`);
678
- try {
679
- await this.publisher.analyzeEpochProofSubmission({
680
- fromCheckpoint,
681
- toCheckpoint,
682
- epochNumber,
683
- publicInputs,
684
- proof,
685
- batchedBlobInputs,
686
- attestations
687
- });
688
- } catch (err) {
689
- this.log.warn(`Failed to analyze estimated L1 fees for epoch ${epochNumber}`, err);
690
- }
691
- this.state = 'completed';
692
- this.metrics.recordProvingJob(executionTime, timer.ms(), epochSizeCheckpoints, epochSizeBlocks, epochSizeTxs);
693
- return;
694
- }
695
- const success = await this.publisher.submitEpochProof({
696
- fromCheckpoint,
697
- toCheckpoint,
698
- epochNumber,
699
- publicInputs,
700
- proof,
701
- batchedBlobInputs,
702
- attestations
703
- });
704
- if (!success) {
705
- throw new Error('Failed to submit epoch proof to L1');
706
- }
707
- this.log.info(`Submitted proof for epoch ${epochNumber} (checkpoints ${fromCheckpoint} to ${toCheckpoint})`, {
708
- epochNumber,
709
- uuid: this.uuid
710
- });
711
- this.state = 'completed';
712
- this.metrics.recordProvingJob(executionTime, timer.ms(), epochSizeCheckpoints, epochSizeBlocks, epochSizeTxs);
713
- } catch (err) {
714
- if (err && err.name === 'HaltExecutionError') {
715
- this.log.warn(`Halted execution of epoch ${epochNumber} prover job`, {
716
- uuid: this.uuid,
717
- epochNumber,
718
- details: err.message
719
- });
720
- return;
721
- }
722
- this.log.error(`Error running epoch ${epochNumber} prover job`, err, {
723
- uuid: this.uuid,
724
- epochNumber
725
- });
726
- if (this.state === 'processing' || this.state === 'awaiting-prover' || this.state === 'publishing-proof') {
727
- this.state = 'failed';
728
- }
729
- } finally{
730
- clearTimeout(this.deadlineTimeoutHandler);
731
- await this.epochCheckPromise?.stop();
732
- await this.prover.stop();
733
- resolve();
734
- }
735
- }
736
- /**
737
- * Create a new db fork for tx processing, optionally inserting L1 to L2 messages.
738
- * L1 to L2 messages should only be inserted for the first block in a checkpoint,
739
- * as subsequent blocks' synced state already includes them.
740
- * REFACTOR: The prover already spawns a db fork of its own for each block, so we may be able to do away with just one fork.
741
- */ async createFork(blockNumber, l1ToL2Messages) {
742
- const env = {
743
- stack: [],
744
- error: void 0,
745
- hasError: false
746
- };
747
- try {
748
- this.log.verbose(`Creating fork at ${blockNumber}`, {
749
- blockNumber
750
- });
751
- const cleanup = _ts_add_disposable_resource(env, new AsyncDisposableStack(), true);
752
- const db = cleanup.use(await this.dbProvider.fork(blockNumber));
753
- if (l1ToL2Messages !== undefined) {
754
- this.log.verbose(`Inserting ${l1ToL2Messages.length} L1 to L2 messages in fork`, {
755
- blockNumber,
756
- l1ToL2Messages: l1ToL2Messages.map((m)=>m.toString())
757
- });
758
- await appendL1ToL2MessagesToTree(db, l1ToL2Messages);
759
- }
760
- // everything run succesfully so we can release this stack and give control of the fork's lifetime to the caller
761
- cleanup.move();
762
- return db;
763
- } catch (e) {
764
- env.error = e;
765
- env.hasError = true;
766
- } finally{
767
- const result = _ts_dispose_resources(env);
768
- if (result) await result;
769
- }
770
- }
771
- async processCheckpoints(parallelism, processCheckpoint) {
772
- let hasError = false;
773
- let firstError;
774
- await asyncPool(Math.max(parallelism, 1), this.checkpoints, async (checkpoint)=>{
775
- if (hasError || this.abortController.signal.aborted) {
776
- return;
777
- }
778
- try {
779
- this.checkState();
780
- await processCheckpoint(checkpoint);
781
- } catch (err) {
782
- if (!hasError) {
783
- hasError = true;
784
- firstError = err;
785
- this.failProcessing();
786
- }
787
- }
788
- });
789
- if (hasError) {
790
- throw firstError;
791
- }
792
- if (this.abortController.signal.aborted) {
793
- this.checkState();
794
- }
795
- }
796
- progressState(state) {
797
- this.checkState();
798
- this.state = state;
799
- }
800
- checkState() {
801
- if (this.state === 'timed-out' || this.state === 'stopped' || this.state === 'failed' || this.state === 'reorg') {
802
- throw new HaltExecutionError(this.state);
803
- }
804
- }
805
- async stop(state = 'stopped') {
806
- this.state = state;
807
- this.interruptProcessing();
808
- if (this.runPromise) {
809
- await this.runPromise;
810
- }
811
- }
812
- failProcessing() {
813
- if (!EpochProvingJobTerminalState.includes(this.state)) {
814
- this.state = 'failed';
815
- }
816
- this.interruptProcessing();
817
- }
818
- interruptProcessing() {
819
- this.abortController.abort();
820
- this.prover.cancel();
821
- }
822
- scheduleDeadlineStop() {
823
- const deadline = this.deadline;
824
- if (deadline) {
825
- const timeout = deadline.getTime() - Date.now();
826
- if (timeout <= 0) {
827
- throw new Error('Cannot start job with deadline in the past');
828
- }
829
- this.deadlineTimeoutHandler = setTimeout(()=>{
830
- if (EpochProvingJobTerminalState.includes(this.state)) {
831
- return;
832
- }
833
- this.log.warn('Stopping job due to deadline hit', {
834
- uuid: this.uuid,
835
- epochNumber: this.epochNumber
836
- });
837
- this.stop('timed-out').catch((err)=>{
838
- this.log.error('Error stopping job', err, {
839
- uuid: this.uuid,
840
- epochNumber: this.epochNumber
841
- });
842
- });
843
- }, timeout);
844
- }
845
- }
846
- /**
847
- * Kicks off a running promise that queries the archiver for the set of L2 blocks of the current epoch.
848
- * If those change, stops the proving job with a `rerun` state, so the node re-enqueues it.
849
- */ async scheduleEpochCheck() {
850
- const l2BlockSource = this.l2BlockSource;
851
- if (!l2BlockSource) {
852
- this.log.warn(`No L2 block source available, skipping epoch check`);
853
- return;
854
- }
855
- const intervalMs = Math.ceil((await l2BlockSource.getL1Constants()).ethereumSlotDuration / 2) * 1000;
856
- this.epochCheckPromise = new RunningPromise(async ()=>{
857
- const blockHeaders = (await l2BlockSource.getBlocksData({
858
- epoch: this.epochNumber,
859
- onlyCheckpointed: true
860
- })).map((d)=>d.header);
861
- const blockHashes = await Promise.all(blockHeaders.map((header)=>header.hash()));
862
- const thisBlocks = this.checkpoints.flatMap((checkpoint)=>checkpoint.blocks);
863
- const thisBlockHashes = await Promise.all(thisBlocks.map((block)=>block.hash()));
864
- if (blockHeaders.length !== thisBlocks.length || !blockHashes.every((block, i)=>block.equals(thisBlockHashes[i]))) {
865
- this.log.warn('Epoch blocks changed underfoot', {
866
- uuid: this.uuid,
867
- epochNumber: this.epochNumber,
868
- oldBlockHashes: thisBlockHashes,
869
- newBlockHashes: blockHashes
870
- });
871
- void this.stop('reorg');
872
- }
873
- }, this.log, intervalMs).start();
874
- this.log.verbose(`Scheduled epoch check for epoch ${this.epochNumber} every ${intervalMs}ms`);
875
- }
876
- /* Returns the last block header in the previous checkpoint for all checkpoints in the epoch */ gatherPreviousBlockHeaders() {
877
- const lastBlocks = this.checkpoints.map((checkpoint)=>checkpoint.blocks.at(-1));
878
- return [
879
- this.data.previousBlockHeader,
880
- ...lastBlocks.map((block)=>block.header).slice(0, -1)
881
- ];
882
- }
883
- getTxs(block) {
884
- return block.body.txEffects.map((txEffect)=>this.txs.get(txEffect.txHash.toString()));
885
- }
886
- getL1ToL2Messages(checkpoint) {
887
- return this.data.l1ToL2Messages[checkpoint.number];
888
- }
889
- async processTxs(publicProcessor, txs) {
890
- const { deadline } = this;
891
- const [processedTxs, failedTxs] = await publicProcessor.process(txs, {
892
- deadline,
893
- signal: this.abortController.signal
894
- });
895
- this.checkState();
896
- if (failedTxs.length) {
897
- const failedTxHashes = await Promise.all(failedTxs.map(({ tx })=>tx.getTxHash()));
898
- throw new Error(`Txs failed processing: ${failedTxs.map(({ error }, index)=>`${failedTxHashes[index]} (${error})`).join(', ')}`);
899
- }
900
- if (processedTxs.length !== txs.length) {
901
- throw new Error(`Failed to process all txs: processed ${processedTxs.length} out of ${txs.length}`);
902
- }
903
- return processedTxs;
904
- }
905
- }
906
- class HaltExecutionError extends Error {
907
- state;
908
- constructor(state){
909
- super(`Halted execution due to state ${state}`), this.state = state;
910
- this.name = 'HaltExecutionError';
911
- }
912
- }