@aztec/prover-node 5.0.0-private.20260319 → 5.0.0-rc.2
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.
- package/README.md +518 -0
- package/dest/actions/download-epoch-proving-job.js +1 -1
- package/dest/actions/rerun-epoch-proving-job.d.ts +4 -3
- package/dest/actions/rerun-epoch-proving-job.d.ts.map +1 -1
- package/dest/actions/rerun-epoch-proving-job.js +103 -21
- package/dest/bin/run-failed-epoch.js +1 -3
- package/dest/checkpoint-store.d.ts +87 -0
- package/dest/checkpoint-store.d.ts.map +1 -0
- package/dest/checkpoint-store.js +186 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +1 -1
- package/dest/factory.d.ts +1 -1
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +22 -8
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/job/checkpoint-prover.d.ts +134 -0
- package/dest/job/checkpoint-prover.d.ts.map +1 -0
- package/dest/job/checkpoint-prover.js +353 -0
- package/dest/job/epoch-session.d.ts +146 -0
- package/dest/job/epoch-session.d.ts.map +1 -0
- package/dest/job/epoch-session.js +720 -0
- package/dest/job/top-tree-job.d.ts +82 -0
- package/dest/job/top-tree-job.d.ts.map +1 -0
- package/dest/job/top-tree-job.js +152 -0
- package/dest/metrics.d.ts +35 -8
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +86 -14
- package/dest/monitors/epoch-monitor.js +6 -2
- package/dest/proof-publishing-service.d.ts +161 -0
- package/dest/proof-publishing-service.d.ts.map +1 -0
- package/dest/proof-publishing-service.js +335 -0
- package/dest/prover-node-publisher.d.ts +22 -15
- package/dest/prover-node-publisher.d.ts.map +1 -1
- package/dest/prover-node-publisher.js +197 -60
- package/dest/prover-node.d.ts +105 -67
- package/dest/prover-node.d.ts.map +1 -1
- package/dest/prover-node.js +482 -224
- package/dest/prover-publisher-factory.d.ts +2 -2
- package/dest/prover-publisher-factory.d.ts.map +1 -1
- package/dest/prover-publisher-factory.js +3 -3
- package/dest/session-manager.d.ts +158 -0
- package/dest/session-manager.d.ts.map +1 -0
- package/dest/session-manager.js +452 -0
- package/dest/test/index.d.ts +7 -6
- package/dest/test/index.d.ts.map +1 -1
- package/package.json +23 -23
- package/src/actions/download-epoch-proving-job.ts +1 -1
- package/src/actions/rerun-epoch-proving-job.ts +114 -28
- package/src/bin/run-failed-epoch.ts +1 -2
- package/src/checkpoint-store.ts +218 -0
- package/src/config.ts +2 -1
- package/src/factory.ts +18 -10
- package/src/index.ts +1 -0
- package/src/job/checkpoint-prover.ts +468 -0
- package/src/job/epoch-session.ts +436 -0
- package/src/job/top-tree-job.ts +227 -0
- package/src/metrics.ts +102 -23
- package/src/monitors/epoch-monitor.ts +2 -2
- package/src/proof-publishing-service.ts +427 -0
- package/src/prover-node-publisher.ts +231 -77
- package/src/prover-node.ts +552 -251
- package/src/prover-publisher-factory.ts +3 -3
- package/src/session-manager.ts +552 -0
- package/src/test/index.ts +6 -6
- package/dest/job/epoch-proving-job.d.ts +0 -63
- package/dest/job/epoch-proving-job.d.ts.map +0 -1
- package/dest/job/epoch-proving-job.js +0 -762
- package/src/job/epoch-proving-job.ts +0 -465
package/dest/prover-node.js
CHANGED
|
@@ -370,28 +370,40 @@ function applyDecs2203RFactory() {
|
|
|
370
370
|
function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
|
|
371
371
|
return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
|
|
372
372
|
}
|
|
373
|
-
var
|
|
374
|
-
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
375
|
-
import { assertRequired, compact, pick
|
|
373
|
+
var _initProto;
|
|
374
|
+
import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/branded-types';
|
|
375
|
+
import { assertRequired, compact, pick } from '@aztec/foundation/collection';
|
|
376
376
|
import { memoize } from '@aztec/foundation/decorators';
|
|
377
377
|
import { createLogger } from '@aztec/foundation/log';
|
|
378
|
-
import {
|
|
378
|
+
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
379
|
+
import { DateProvider, executeTimeout } from '@aztec/foundation/timer';
|
|
380
|
+
import { getLastSiblingPath } from '@aztec/prover-client/helpers';
|
|
381
|
+
import { ChonkCache } from '@aztec/prover-client/orchestrator';
|
|
379
382
|
import { PublicProcessorFactory } from '@aztec/simulator/server';
|
|
380
|
-
import {
|
|
383
|
+
import { L2BlockStream, L2TipsMemoryStore } from '@aztec/stdlib/block';
|
|
384
|
+
import { getEpochAtSlot, getProofSubmissionDeadlineEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
381
385
|
import { EpochProvingJobTerminalState, tryStop } from '@aztec/stdlib/interfaces/server';
|
|
382
|
-
import {
|
|
386
|
+
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
387
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
383
388
|
import { uploadEpochProofFailure } from './actions/upload-epoch-proof-failure.js';
|
|
384
|
-
import {
|
|
389
|
+
import { CheckpointStore } from './checkpoint-store.js';
|
|
385
390
|
import { ProverNodeJobMetrics, ProverNodeRewardsMetrics } from './metrics.js';
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
})), _dec1 = trackSpan('ProverNode.gatherEpochData', (epochNumber)=>({
|
|
389
|
-
[Attributes.EPOCH_NUMBER]: epochNumber
|
|
390
|
-
}));
|
|
391
|
+
import { ProofPublishingService } from './proof-publishing-service.js';
|
|
392
|
+
import { SessionManager } from './session-manager.js';
|
|
391
393
|
/**
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
*
|
|
394
|
+
* Grace period for the proof-publishing service to settle during shutdown. The service waits for
|
|
395
|
+
* any in-flight L1 proof-submission tx to finish; that tx can take a long time to mine, so we cap
|
|
396
|
+
* the wait rather than letting `stop()` hang indefinitely.
|
|
397
|
+
*/ const PUBLISHING_SERVICE_STOP_TIMEOUT_MS = 30_000;
|
|
398
|
+
/**
|
|
399
|
+
* An Aztec Prover Node is a standalone process that monitors the chain for new checkpoints,
|
|
400
|
+
* starts proving them optimistically as they arrive, and submits epoch proofs to L1 once
|
|
401
|
+
* complete.
|
|
402
|
+
*
|
|
403
|
+
* The class is intentionally thin: it owns the long-lived collections (`CheckpointStore`,
|
|
404
|
+
* `ChonkCache`, `SessionManager`), the L2BlockStream, and a periodic ticker that nudges the
|
|
405
|
+
* manager to pick up newly-complete epochs. Every session lifecycle decision is delegated to
|
|
406
|
+
* the `SessionManager`. Each chain event is translated here into a single method call on it.
|
|
395
407
|
*/ export class ProverNode {
|
|
396
408
|
prover;
|
|
397
409
|
publisherFactory;
|
|
@@ -400,7 +412,6 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
400
412
|
contractDataSource;
|
|
401
413
|
worldState;
|
|
402
414
|
p2pClient;
|
|
403
|
-
epochsMonitor;
|
|
404
415
|
rollupContract;
|
|
405
416
|
l1Metrics;
|
|
406
417
|
telemetryClient;
|
|
@@ -408,31 +419,38 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
408
419
|
dateProvider;
|
|
409
420
|
static{
|
|
410
421
|
({ e: [_initProto] } = _apply_decs_2203_r(this, [
|
|
411
|
-
[
|
|
412
|
-
_dec,
|
|
413
|
-
2,
|
|
414
|
-
"createProvingJob"
|
|
415
|
-
],
|
|
416
422
|
[
|
|
417
423
|
memoize,
|
|
418
424
|
2,
|
|
419
425
|
"getL1Constants"
|
|
420
|
-
],
|
|
421
|
-
[
|
|
422
|
-
_dec1,
|
|
423
|
-
2,
|
|
424
|
-
"gatherEpochData"
|
|
425
426
|
]
|
|
426
427
|
], []));
|
|
427
428
|
}
|
|
428
429
|
log;
|
|
429
|
-
|
|
430
|
+
checkpointStore;
|
|
431
|
+
chonkCache;
|
|
432
|
+
sessionManager;
|
|
430
433
|
config;
|
|
431
434
|
jobMetrics;
|
|
432
435
|
rewardsMetrics;
|
|
436
|
+
/** In-memory store for the L2BlockStream's local data provider. */ tipsStore;
|
|
437
|
+
/** Block stream for checkpoint and reorg detection. */ blockStream;
|
|
438
|
+
/**
|
|
439
|
+
* Highest epoch whose proof-submission window has passed. Monotonic high-water mark.
|
|
440
|
+
* Seeded from the last fully-proven epoch at start(); advanced on every block-stream
|
|
441
|
+
* event by comparing the archiver's latest synced L2 slot against each epoch's
|
|
442
|
+
* submission deadline. Protected so tests can verify the start() seeding.
|
|
443
|
+
*/ lastExpiredEpoch;
|
|
444
|
+
/**
|
|
445
|
+
* Highest checkpoint number whose proving-side handling has completed (or that was legitimately skipped).
|
|
446
|
+
* The catch-up loop walks from here to each `chain-checkpointed` tip event. Seeded at start() from the last
|
|
447
|
+
* checkpoint of the last fully-proven epoch (or 0), so a restart reprocesses the partially-proven epoch rather
|
|
448
|
+
* than trusting a checkpointed tip that may sit ahead of unproven checkpoints. Clamped down on a prune.
|
|
449
|
+
*/ lastProcessedCheckpoint;
|
|
450
|
+
/** Periodic tick that runs the epoch-expiry sweep during idle periods when no block-stream events arrive. */ expiryTicker;
|
|
433
451
|
tracer;
|
|
434
|
-
|
|
435
|
-
constructor(prover, publisherFactory, l2BlockSource, l1ToL2MessageSource, contractDataSource, worldState, p2pClient,
|
|
452
|
+
publishingService;
|
|
453
|
+
constructor(prover, publisherFactory, l2BlockSource, l1ToL2MessageSource, contractDataSource, worldState, p2pClient, rollupContract, l1Metrics, config = {}, telemetryClient = getTelemetryClient(), delayer, dateProvider = new DateProvider()){
|
|
436
454
|
this.prover = prover;
|
|
437
455
|
this.publisherFactory = publisherFactory;
|
|
438
456
|
this.l2BlockSource = l2BlockSource;
|
|
@@ -440,14 +458,13 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
440
458
|
this.contractDataSource = contractDataSource;
|
|
441
459
|
this.worldState = worldState;
|
|
442
460
|
this.p2pClient = p2pClient;
|
|
443
|
-
this.epochsMonitor = epochsMonitor;
|
|
444
461
|
this.rollupContract = rollupContract;
|
|
445
462
|
this.l1Metrics = l1Metrics;
|
|
446
463
|
this.telemetryClient = telemetryClient;
|
|
447
464
|
this.delayer = delayer;
|
|
448
465
|
this.dateProvider = dateProvider;
|
|
449
466
|
this.log = (_initProto(this), createLogger('prover-node'));
|
|
450
|
-
this.
|
|
467
|
+
this.lastProcessedCheckpoint = CheckpointNumber.ZERO;
|
|
451
468
|
this.config = {
|
|
452
469
|
proverNodePollingIntervalMs: 1_000,
|
|
453
470
|
proverNodeMaxPendingJobs: 100,
|
|
@@ -465,6 +482,22 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
465
482
|
this.tracer = telemetryClient.getTracer('ProverNode');
|
|
466
483
|
this.jobMetrics = new ProverNodeJobMetrics(meter, telemetryClient.getTracer('EpochProvingJob'));
|
|
467
484
|
this.rewardsMetrics = new ProverNodeRewardsMetrics(meter, this.prover.getProverId(), rollupContract);
|
|
485
|
+
this.tipsStore = new L2TipsMemoryStore(this.l2BlockSource.getGenesisBlockHash());
|
|
486
|
+
this.chonkCache = new ChonkCache(this.log.getBindings());
|
|
487
|
+
this.checkpointStore = new CheckpointStore(this.l2BlockSource, {
|
|
488
|
+
proverFactory: this.prover,
|
|
489
|
+
chonkCache: this.chonkCache,
|
|
490
|
+
publicProcessorFactory: new PublicProcessorFactory(this.contractDataSource, this.dateProvider, this.telemetryClient, this.log.getBindings()),
|
|
491
|
+
dbProvider: this.worldState,
|
|
492
|
+
txProvider: this.p2pClient.getTxProvider(),
|
|
493
|
+
dateProvider: this.dateProvider,
|
|
494
|
+
proverId: this.prover.getProverId(),
|
|
495
|
+
metrics: this.jobMetrics,
|
|
496
|
+
txGatheringTimeoutMs: this.config.txGatheringTimeoutMs,
|
|
497
|
+
deadline: undefined
|
|
498
|
+
}, {
|
|
499
|
+
slotWatcherPollIntervalMs: this.config.proverNodePollingIntervalMs
|
|
500
|
+
}, this.log.getBindings());
|
|
468
501
|
}
|
|
469
502
|
getProverId() {
|
|
470
503
|
return this.prover.getProverId();
|
|
@@ -472,233 +505,462 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
472
505
|
getP2P() {
|
|
473
506
|
return this.p2pClient;
|
|
474
507
|
}
|
|
475
|
-
/**
|
|
508
|
+
/** Test-only: the shared L1 tx delayer, if enabled. */ getDelayer() {
|
|
476
509
|
return this.delayer;
|
|
477
510
|
}
|
|
511
|
+
/** Observability summary for the ProverNodeApi. */ getJobs() {
|
|
512
|
+
return Promise.resolve(this.sessionManager?.getJobs() ?? []);
|
|
513
|
+
}
|
|
514
|
+
/** Tests inspect this when validating reconcile behaviour. */ getCheckpointStore() {
|
|
515
|
+
return this.checkpointStore;
|
|
516
|
+
}
|
|
517
|
+
/** Tests inspect this to verify chonk-cache release semantics. */ getChonkCache() {
|
|
518
|
+
return this.chonkCache;
|
|
519
|
+
}
|
|
520
|
+
/** Tests inspect this when looking up live sessions. */ getSessionManager() {
|
|
521
|
+
if (!this.sessionManager) {
|
|
522
|
+
throw new Error('SessionManager not yet constructed — start() must be called first.');
|
|
523
|
+
}
|
|
524
|
+
return this.sessionManager;
|
|
525
|
+
}
|
|
526
|
+
/** Returns the underlying prover instance. */ getProver() {
|
|
527
|
+
return this.prover;
|
|
528
|
+
}
|
|
529
|
+
// ---------------- L2BlockStream handler ----------------
|
|
530
|
+
async handleBlockStreamEvent(event) {
|
|
531
|
+
switch(event.type){
|
|
532
|
+
case 'chain-checkpointed':
|
|
533
|
+
await this.processCheckpointJump(event.checkpoint.number);
|
|
534
|
+
break;
|
|
535
|
+
case 'chain-pruned':
|
|
536
|
+
await this.handlePruneEvent(event.block);
|
|
537
|
+
break;
|
|
538
|
+
case 'chain-proven':
|
|
539
|
+
this.publishingService?.onChainProven(BlockNumber(event.block.number));
|
|
540
|
+
break;
|
|
541
|
+
// The proposed tip drives only the tips store's walk-back history (recorded below); the prover-node
|
|
542
|
+
// tracks checkpoints, not proposed blocks. `blocks-added` is never emitted in tips-only mode, and
|
|
543
|
+
// `chain-finalized` carries nothing the prover-node acts on.
|
|
544
|
+
case 'chain-proposed':
|
|
545
|
+
case 'chain-finalized':
|
|
546
|
+
case 'blocks-added':
|
|
547
|
+
break;
|
|
548
|
+
default:
|
|
549
|
+
{
|
|
550
|
+
const _ = event;
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
// Expiry is driven by the archiver's latest synced L2 slot
|
|
555
|
+
await this.checkEpochExpiry();
|
|
556
|
+
// Advance the local tips store only after the proving-side handling has succeeded. Any
|
|
557
|
+
// failure above propagates to the L2BlockStream (which logs and stops this poll pass) and
|
|
558
|
+
// skips this update, so the event is re-emitted on the next poll rather than skipped (A-1041).
|
|
559
|
+
await this.tipsStore.handleBlockStreamEvent(event);
|
|
560
|
+
}
|
|
478
561
|
/**
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
562
|
+
* Walks every checkpoint between the local cursor and the newly-reported checkpointed tip, registering
|
|
563
|
+
* each one that belongs to an epoch that can still be proven. The block stream now delivers a single thin
|
|
564
|
+
* `chain-checkpointed` tip event per pass rather than one fat event per checkpoint, so this drives the
|
|
565
|
+
* catch-up itself: light metadata first (`getCheckpointsData`) to decide relevance per epoch, then a heavy
|
|
566
|
+
* `getCheckpoints` fetch only for checkpoints in provable epochs.
|
|
567
|
+
*
|
|
568
|
+
* The cursor advances one checkpoint at a time and only after that checkpoint's proving-side handling has
|
|
569
|
+
* fully succeeded, preserving the A-1041 at-least-once semantics: a mid-jump failure leaves the cursor
|
|
570
|
+
* behind so the next pass retries from the first checkpoint that did not complete.
|
|
571
|
+
*/ async processCheckpointJump(targetCheckpoint) {
|
|
572
|
+
if (targetCheckpoint <= this.lastProcessedCheckpoint) {
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
const l1Constants = await this.getL1Constants();
|
|
576
|
+
// Cap the catch-up at the `(proofSubmissionEpochs + 1) * epochDuration` most recent checkpoints.
|
|
577
|
+
// When the cursor is much further behind (e.g. resyncing after a long time offline), fetching the whole gap could
|
|
578
|
+
// load thousands of checkpoints we cannot act on: anything older than the last two epochs is already past
|
|
579
|
+
// its proof-submission window, so we skip it and jump the cursor forward to the start of the capped range.
|
|
580
|
+
const maxCheckpoints = (l1Constants.proofSubmissionEpochs + 1) * l1Constants.epochDuration;
|
|
581
|
+
let from = CheckpointNumber(this.lastProcessedCheckpoint + 1);
|
|
582
|
+
if (Number(targetCheckpoint - from) + 1 > maxCheckpoints) {
|
|
583
|
+
const cappedFrom = CheckpointNumber(targetCheckpoint - maxCheckpoints + 1);
|
|
584
|
+
this.log.warn(`Skipping unprovable checkpoints during catch-up; the prover node is far behind`, {
|
|
585
|
+
from,
|
|
586
|
+
cappedFrom,
|
|
587
|
+
targetCheckpoint,
|
|
588
|
+
maxCheckpoints
|
|
486
589
|
});
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
590
|
+
// Advance the cursor past the skipped checkpoints so they are never retried.
|
|
591
|
+
this.lastProcessedCheckpoint = CheckpointNumber(cappedFrom - 1);
|
|
592
|
+
from = cappedFrom;
|
|
593
|
+
}
|
|
594
|
+
const limit = Number(targetCheckpoint - from) + 1;
|
|
595
|
+
const metadatas = await this.l2BlockSource.getCheckpointsData({
|
|
596
|
+
from,
|
|
597
|
+
limit
|
|
598
|
+
});
|
|
599
|
+
// Per-epoch relevance is cached so a multi-checkpoint epoch resolves it once. Skipping is whole-epoch
|
|
600
|
+
// only: the SessionManager requires an epoch's checkpoints fully covered before it opens a session, so we
|
|
601
|
+
// never drop an individual checkpoint inside an epoch we will prove.
|
|
602
|
+
const epochSkippable = new Map();
|
|
603
|
+
for (const metadata of metadatas){
|
|
604
|
+
const epochNumber = getEpochAtSlot(metadata.header.slotNumber, l1Constants);
|
|
605
|
+
let skippable = epochSkippable.get(epochNumber);
|
|
606
|
+
if (skippable === undefined) {
|
|
607
|
+
skippable = await this.isEpochFullyProven(epochNumber, l1Constants) || await this.isEpochPastProofSubmissionWindow(epochNumber, l1Constants);
|
|
608
|
+
epochSkippable.set(epochNumber, skippable);
|
|
493
609
|
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
} catch (err) {
|
|
497
|
-
if (err instanceof EmptyEpochError) {
|
|
498
|
-
this.log.info(`Not starting proof for ${epochNumber} since no blocks were found`);
|
|
610
|
+
if (skippable) {
|
|
611
|
+
this.log.debug(`Skipping checkpoint ${metadata.checkpointNumber} for unprovable epoch ${epochNumber}`);
|
|
499
612
|
} else {
|
|
500
|
-
this.
|
|
613
|
+
await this.registerCheckpoint(metadata.checkpointNumber, epochNumber);
|
|
501
614
|
}
|
|
615
|
+
// Advance only after the checkpoint's handling succeeded (or it was legitimately skipped). registerCheckpoint
|
|
616
|
+
// throws on failure, which leaves the cursor here for the next pass to retry (A-1041).
|
|
617
|
+
this.lastProcessedCheckpoint = metadata.checkpointNumber;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
/** Heavy-fetch a single checkpoint, register it with the store, and notify the session manager. */ async registerCheckpoint(checkpointNumber, epochNumber) {
|
|
621
|
+
const published = await this.l2BlockSource.getCheckpoint({
|
|
622
|
+
number: checkpointNumber
|
|
623
|
+
});
|
|
624
|
+
if (!published) {
|
|
625
|
+
throw new Error(`Checkpoint ${checkpointNumber} not found in block source during catch-up`);
|
|
626
|
+
}
|
|
627
|
+
const checkpoint = published.checkpoint;
|
|
628
|
+
this.log.info(`New checkpoint ${checkpoint.number} for epoch ${epochNumber}`, {
|
|
629
|
+
checkpointNumber: checkpoint.number,
|
|
630
|
+
epochNumber,
|
|
631
|
+
slotNumber: checkpoint.header.slotNumber
|
|
632
|
+
});
|
|
633
|
+
const registerData = await this.collectRegisterData(checkpoint, published.attestations);
|
|
634
|
+
await this.checkpointStore.addOrUpdate(checkpoint, registerData);
|
|
635
|
+
await this.sessionManager?.onCheckpointAdded(epochNumber);
|
|
636
|
+
// Tips-only mode delivers no blocks, so record one witness per checkpointed block: a reorg into the checkpoint's
|
|
637
|
+
// range then prunes at the true divergence instead of the nearest sparse tip anchor.
|
|
638
|
+
await this.tipsStore.recordBlockHashes(await Promise.all(checkpoint.blocks.map(async (block)=>({
|
|
639
|
+
number: block.number,
|
|
640
|
+
hash: (await block.header.hash()).toString()
|
|
641
|
+
}))));
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Gathers register-time data for a checkpoint: previous block header, L1-to-L2 messages,
|
|
645
|
+
* and the archive sibling path.
|
|
646
|
+
*/ async collectRegisterData(checkpoint, attestations) {
|
|
647
|
+
const previousBlockNumber = BlockNumber(checkpoint.blocks[0].number - 1);
|
|
648
|
+
const previousBlockHeader = await this.gatherPreviousBlockHeader(previousBlockNumber);
|
|
649
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpoint.number);
|
|
650
|
+
const lastBlock = checkpoint.blocks.at(-1);
|
|
651
|
+
const lastBlockHash = await lastBlock.header.hash();
|
|
652
|
+
await this.worldState.syncImmediate(lastBlock.number, lastBlockHash);
|
|
653
|
+
const previousArchiveSiblingPath = await getLastSiblingPath(MerkleTreeId.ARCHIVE, this.worldState.getSnapshot(previousBlockNumber));
|
|
654
|
+
return {
|
|
655
|
+
attestations,
|
|
656
|
+
previousBlockHeader,
|
|
657
|
+
l1ToL2Messages,
|
|
658
|
+
previousArchiveSiblingPath
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Marks every prover orphaned by the prune as pruned, clamps the catch-up cursor below the prune target's
|
|
663
|
+
* checkpoint, and notifies the session manager. Keyed off the prune target block (the highest surviving block)
|
|
664
|
+
* rather than the source's checkpointed tip, which can sit above the target after a re-checkpoint and would leave
|
|
665
|
+
* orphaned provers canonical. Throws (rather than warning) if the cursor floor cannot be resolved, so the pass
|
|
666
|
+
* fails and the prune is retried next iteration.
|
|
667
|
+
*/ async handlePruneEvent(prunedToBlock) {
|
|
668
|
+
this.log.warn(`Chain pruned to block ${prunedToBlock.number}`, {
|
|
669
|
+
prunedToBlock
|
|
670
|
+
});
|
|
671
|
+
// Resolve the cursor floor BEFORE marking provers: markPrunedAboveBlock returns only newly-marked provers, so a
|
|
672
|
+
// throw after marking would leave a retry pass with nothing to act on. Resolving first means a throw leaves
|
|
673
|
+
// everything untouched and the next pass retries the whole handler (the tips cursor only advances on success).
|
|
674
|
+
let cursorFloor;
|
|
675
|
+
if (prunedToBlock.number === 0) {
|
|
676
|
+
cursorFloor = CheckpointNumber.ZERO;
|
|
677
|
+
} else {
|
|
678
|
+
const targetData = await this.l2BlockSource.getBlockData({
|
|
679
|
+
number: prunedToBlock.number
|
|
680
|
+
});
|
|
681
|
+
if (targetData === undefined) {
|
|
682
|
+
throw new Error(`No block data found for prune target block ${prunedToBlock.number}; cannot clamp checkpoint cursor`);
|
|
683
|
+
}
|
|
684
|
+
// Clamp to `cpAtTarget - 1`: a mid-checkpoint target leaves that checkpoint partially orphaned and it must be
|
|
685
|
+
// reprocessed. Over-clamping merely re-registers a checkpoint (at-least-once by design — A-1041); under-clamping
|
|
686
|
+
// would permanently skip a rebuilt same-number checkpoint.
|
|
687
|
+
cursorFloor = CheckpointNumber(Math.max(0, Number(targetData.checkpointNumber) - 1));
|
|
688
|
+
}
|
|
689
|
+
const affected = this.checkpointStore.markPrunedAboveBlock(prunedToBlock.number);
|
|
690
|
+
if (this.lastProcessedCheckpoint > cursorFloor) {
|
|
691
|
+
this.lastProcessedCheckpoint = cursorFloor;
|
|
692
|
+
}
|
|
693
|
+
if (affected.length === 0) {
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
const l1Constants = await this.getL1Constants();
|
|
697
|
+
const affectedEpochs = Array.from(new Set(affected.map((p)=>Number(getEpochAtSlot(p.slotNumber, l1Constants))))).map((n)=>EpochNumber(n));
|
|
698
|
+
// The session manager cancels every affected session, which in turn calls
|
|
699
|
+
// publishingService.withdraw(uuid) for each candidate; no separate notification to the
|
|
700
|
+
// publishing service is needed.
|
|
701
|
+
await this.sessionManager?.onPrune(affectedEpochs);
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* Returns true once the chain has advanced past the given epoch's proof-submission window.
|
|
705
|
+
* Used to ignore checkpoints whose epoch can no longer be proven in time — chiefly while the
|
|
706
|
+
* archiver replays old blocks after a restart. Compares the archiver's latest synced L2 slot
|
|
707
|
+
* against the epoch's submission-deadline epoch; conservatively returns false if the slot can't
|
|
708
|
+
* be read yet.
|
|
709
|
+
*/ async isEpochPastProofSubmissionWindow(epochNumber, l1Constants) {
|
|
710
|
+
const latestSlot = await this.l2BlockSource.getSyncedL2SlotNumber();
|
|
711
|
+
if (latestSlot === undefined) {
|
|
502
712
|
return false;
|
|
503
713
|
}
|
|
714
|
+
const latestEpoch = getEpochAtSlot(latestSlot, l1Constants);
|
|
715
|
+
return latestEpoch >= getProofSubmissionDeadlineEpoch(epochNumber, l1Constants);
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Compares the archiver's latest synced L2 slot against `lastExpiredEpoch` and, for each
|
|
719
|
+
* newly-expired epoch, releases the chonk-cache entries for its blocks and reaps any
|
|
720
|
+
* CheckpointProvers in the store. An epoch E is expired once the chain reaches the start
|
|
721
|
+
* of epoch `E + proofSubmissionEpochs + 1`. Silently no-ops if nothing has expired since
|
|
722
|
+
* the last check or the archiver's slot can't be read.
|
|
723
|
+
*/ async checkEpochExpiry() {
|
|
724
|
+
const latestSlot = await this.l2BlockSource.getSyncedL2SlotNumber();
|
|
725
|
+
if (latestSlot === undefined) {
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
const l1Constants = await this.getL1Constants();
|
|
729
|
+
const latestEpoch = getEpochAtSlot(latestSlot, l1Constants);
|
|
730
|
+
const offset = l1Constants.proofSubmissionEpochs + 1;
|
|
731
|
+
if (latestEpoch < offset) {
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
const newlyExpiredUpTo = EpochNumber(latestEpoch - offset);
|
|
735
|
+
const from = this.lastExpiredEpoch === undefined ? EpochNumber(0) : EpochNumber(this.lastExpiredEpoch + 1);
|
|
736
|
+
if (newlyExpiredUpTo < from) {
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
for(let e = from; e <= newlyExpiredUpTo; e = EpochNumber(e + 1)){
|
|
740
|
+
await this.expireEpoch(e);
|
|
741
|
+
}
|
|
742
|
+
this.lastExpiredEpoch = newlyExpiredUpTo;
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Releases chonk-cache entries for every block in the supplied epoch (best-effort) and
|
|
746
|
+
* reaps every CheckpointProver in the store whose epoch number matches.
|
|
747
|
+
*/ async expireEpoch(epoch) {
|
|
748
|
+
try {
|
|
749
|
+
const blocks = await this.l2BlockSource.getBlocks({
|
|
750
|
+
epoch,
|
|
751
|
+
onlyCheckpointed: true
|
|
752
|
+
});
|
|
753
|
+
if (blocks.length > 0) {
|
|
754
|
+
this.chonkCache.releaseForBlocks(blocks);
|
|
755
|
+
}
|
|
756
|
+
} catch (err) {
|
|
757
|
+
this.log.warn(`Could not release chonk-cache entries for expired epoch ${epoch}`, err);
|
|
758
|
+
}
|
|
759
|
+
this.checkpointStore.reapExpired(epoch);
|
|
504
760
|
}
|
|
761
|
+
// ---------------- public API ----------------
|
|
505
762
|
/**
|
|
506
|
-
*
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
763
|
+
* Schedules proving for the given epoch and returns the job id without waiting for completion.
|
|
764
|
+
*/ async startProof(epochNumber) {
|
|
765
|
+
if (!this.sessionManager) {
|
|
766
|
+
throw new Error('ProverNode not started');
|
|
767
|
+
}
|
|
768
|
+
return await this.sessionManager.startProof(epochNumber);
|
|
769
|
+
}
|
|
770
|
+
// ---------------- Service lifecycle ----------------
|
|
771
|
+
async start() {
|
|
772
|
+
await this.checkpointStore.start();
|
|
510
773
|
await this.publisherFactory.start();
|
|
511
|
-
this.
|
|
774
|
+
this.publishingService = new ProofPublishingService({
|
|
775
|
+
publisherFactory: this.publisherFactory,
|
|
776
|
+
l2BlockSource: this.l2BlockSource,
|
|
777
|
+
dateProvider: this.dateProvider,
|
|
778
|
+
config: {
|
|
779
|
+
skipSubmitProof: !!this.config.proverNodeDisableProofPublish
|
|
780
|
+
},
|
|
781
|
+
bindings: this.log.getBindings()
|
|
782
|
+
});
|
|
783
|
+
this.sessionManager = this.createSessionManager(this.publishingService);
|
|
784
|
+
// SessionManager owns its own periodic tick; start it here so it begins picking up
|
|
785
|
+
// epochs that become complete by time (no fresh checkpoint event) and advances once
|
|
786
|
+
// the previous epoch is proven on L1.
|
|
787
|
+
this.sessionManager.start();
|
|
788
|
+
// Now that the store + manager exist, arm the live-state observable gauges.
|
|
789
|
+
this.jobMetrics.observeState(this.checkpointStore, this.sessionManager);
|
|
790
|
+
const { lastFullyProvenEpoch } = await this.resolveLastFullyProvenEpoch();
|
|
791
|
+
this.lastExpiredEpoch = lastFullyProvenEpoch;
|
|
792
|
+
this.lastProcessedCheckpoint = await this.computeStartingCheckpoint(lastFullyProvenEpoch);
|
|
793
|
+
this.blockStream = new L2BlockStream(this.l2BlockSource, this.tipsStore, this, this.log, {
|
|
794
|
+
pollIntervalMS: this.config.proverNodePollingIntervalMs,
|
|
795
|
+
tipsOnly: true
|
|
796
|
+
});
|
|
797
|
+
this.blockStream.start();
|
|
798
|
+
// With thin once-per-pass tip events, the expiry sweep no longer fires once per checkpoint; drive it
|
|
799
|
+
// from a periodic tick so epochs still expire during idle/no-event periods.
|
|
800
|
+
this.expiryTicker = new RunningPromise(()=>this.checkEpochExpiry(), this.log, this.config.proverNodePollingIntervalMs);
|
|
801
|
+
this.expiryTicker.start();
|
|
512
802
|
await this.rewardsMetrics.start();
|
|
513
803
|
this.l1Metrics.start();
|
|
514
804
|
this.log.info(`Started Prover Node with prover id ${this.prover.getProverId().toString()}`, this.config);
|
|
515
805
|
}
|
|
516
|
-
|
|
517
|
-
* Stops the prover node and all its dependencies.
|
|
518
|
-
* Resources not owned by this node (shared with the parent aztec-node) are skipped.
|
|
519
|
-
*/ async stop() {
|
|
806
|
+
async stop() {
|
|
520
807
|
this.log.info('Stopping ProverNode');
|
|
521
|
-
|
|
808
|
+
this.jobMetrics.stopObservingState();
|
|
809
|
+
await this.blockStream?.stop();
|
|
810
|
+
await this.expiryTicker?.stop();
|
|
811
|
+
if (this.sessionManager) {
|
|
812
|
+
await this.sessionManager.stop();
|
|
813
|
+
}
|
|
814
|
+
if (this.publishingService) {
|
|
815
|
+
// Bound the wait: the publishing service blocks until any in-flight L1 proof-submission tx
|
|
816
|
+
// settles, which can outlast a reasonable shutdown window. On timeout we log and move on —
|
|
817
|
+
// the tx may still mine, but shutdown must not hang on it.
|
|
818
|
+
const publishingService = this.publishingService;
|
|
819
|
+
await executeTimeout(()=>publishingService.stop(), PUBLISHING_SERVICE_STOP_TIMEOUT_MS, 'prover-node publishing-service stop').catch((err)=>this.log.warn(`Timed out stopping proof publishing service`, err));
|
|
820
|
+
}
|
|
821
|
+
await this.checkpointStore.stop();
|
|
822
|
+
this.chonkCache.stop();
|
|
522
823
|
await this.prover.stop();
|
|
523
824
|
await tryStop(this.publisherFactory);
|
|
524
|
-
this.publisher?.interrupt();
|
|
525
|
-
await Promise.all(Array.from(this.jobs.values()).map((job)=>job.stop()));
|
|
526
825
|
this.rewardsMetrics.stop();
|
|
527
826
|
this.l1Metrics.stop();
|
|
528
827
|
await this.telemetryClient.stop();
|
|
529
828
|
this.log.info('Stopped ProverNode');
|
|
530
829
|
}
|
|
531
|
-
/** Returns world state status. */ async getWorldStateSyncStatus() {
|
|
532
|
-
const { syncSummary } = await this.worldState.status();
|
|
533
|
-
return syncSummary;
|
|
534
|
-
}
|
|
535
|
-
/** Returns archiver status. */ getL2Tips() {
|
|
536
|
-
return this.l2BlockSource.getL2Tips();
|
|
537
|
-
}
|
|
538
830
|
/**
|
|
539
|
-
*
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
831
|
+
* Constructs the session manager. Extracted so subclasses (test harness) can swap
|
|
832
|
+
* the implementation. Wired to `tryUploadSessionFailure` so failed sessions get
|
|
833
|
+
* their proving data uploaded.
|
|
834
|
+
*/ createSessionManager(publishingService) {
|
|
835
|
+
return new SessionManager({
|
|
836
|
+
checkpointStore: this.checkpointStore,
|
|
837
|
+
l2BlockSource: this.l2BlockSource,
|
|
838
|
+
proverFactory: this.prover,
|
|
839
|
+
proverId: this.prover.getProverId(),
|
|
840
|
+
publishingService,
|
|
841
|
+
metrics: this.jobMetrics,
|
|
842
|
+
dateProvider: this.dateProvider,
|
|
843
|
+
config: {
|
|
844
|
+
maxPendingJobs: this.config.proverNodeMaxPendingJobs,
|
|
845
|
+
tickIntervalMs: this.config.proverNodePollingIntervalMs,
|
|
846
|
+
finalizationDelayMs: this.config.proverNodeEpochProvingDelayMs
|
|
847
|
+
},
|
|
848
|
+
onSessionFailed: async (session)=>{
|
|
849
|
+
await this.tryUploadSessionFailure(session);
|
|
850
|
+
},
|
|
851
|
+
bindings: this.log.getBindings()
|
|
543
852
|
});
|
|
544
|
-
void this.runJob(job);
|
|
545
853
|
}
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
try {
|
|
554
|
-
await job.run();
|
|
555
|
-
const state = job.getState();
|
|
556
|
-
ctx.state = state;
|
|
557
|
-
if (state === 'reorg') {
|
|
558
|
-
this.log.warn(`Running new job for epoch ${epochNumber} due to reorg`, ctx);
|
|
559
|
-
await this.createProvingJob(epochNumber);
|
|
560
|
-
} else if (state === 'failed') {
|
|
561
|
-
this.log.error(`Job for ${epochNumber} exited with state ${state}`, ctx);
|
|
562
|
-
await this.tryUploadEpochFailure(job);
|
|
563
|
-
} else {
|
|
564
|
-
this.log.verbose(`Job for ${epochNumber} exited with state ${state}`, ctx);
|
|
565
|
-
}
|
|
566
|
-
} catch (err) {
|
|
567
|
-
this.log.error(`Error proving epoch ${epochNumber}`, err, ctx);
|
|
568
|
-
} finally{
|
|
569
|
-
this.jobs.delete(job.getId());
|
|
854
|
+
/**
|
|
855
|
+
* Installs session hooks for the e2e harness to interpose around top-tree proving
|
|
856
|
+
* (gate, override, or observe it) without monkey-patching the orchestrator factory.
|
|
857
|
+
* Applies to every session constructed after this call.
|
|
858
|
+
*/ setSessionHooks(hooks) {
|
|
859
|
+
if (!this.sessionManager) {
|
|
860
|
+
throw new Error('ProverNode not started; call start() before setting session hooks.');
|
|
570
861
|
}
|
|
862
|
+
this.sessionManager.setSessionHooks(hooks);
|
|
571
863
|
}
|
|
572
|
-
async
|
|
573
|
-
if (this.config.proverNodeFailedEpochStore) {
|
|
574
|
-
return
|
|
864
|
+
/** Uploads failure snapshots when sessions exit with `failed`. Exposed as a method so tests can spy on it. */ async tryUploadSessionFailure(session) {
|
|
865
|
+
if (!this.config.proverNodeFailedEpochStore) {
|
|
866
|
+
return undefined;
|
|
575
867
|
}
|
|
868
|
+
const data = SessionManager.buildSessionProvingData(session);
|
|
869
|
+
return await uploadEpochProofFailure(this.config.proverNodeFailedEpochStore, session.getId(), data, this.l2BlockSource, this.worldState, assertRequired(pick(this.config, 'l1ChainId', 'rollupVersion', 'dataDirectory')), this.log);
|
|
576
870
|
}
|
|
577
|
-
|
|
578
|
-
* Returns the prover instance.
|
|
579
|
-
*/ getProver() {
|
|
580
|
-
return this.prover;
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* Returns an array of jobs being processed.
|
|
584
|
-
*/ getJobs() {
|
|
585
|
-
return Promise.resolve(Array.from(this.jobs.entries()).map(([uuid, job])=>({
|
|
586
|
-
uuid,
|
|
587
|
-
status: job.getState(),
|
|
588
|
-
epochNumber: job.getEpochNumber()
|
|
589
|
-
})));
|
|
590
|
-
}
|
|
591
|
-
async getActiveJobsForEpoch(epochNumber) {
|
|
592
|
-
const jobs = await this.getJobs();
|
|
593
|
-
return jobs.filter((job)=>job.epochNumber === epochNumber && !EpochProvingJobTerminalState.includes(job.status));
|
|
594
|
-
}
|
|
595
|
-
checkMaximumPendingJobs() {
|
|
596
|
-
const { proverNodeMaxPendingJobs: maxPendingJobs } = this.config;
|
|
597
|
-
if (maxPendingJobs > 0 && this.jobs.size >= maxPendingJobs) {
|
|
598
|
-
throw new Error(`Maximum pending proving jobs ${maxPendingJobs} reached. Cannot create new job.`);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
async createProvingJob(epochNumber, opts = {}) {
|
|
602
|
-
this.checkMaximumPendingJobs();
|
|
603
|
-
this.publisher = await this.publisherFactory.create();
|
|
604
|
-
// Gather all data for this epoch
|
|
605
|
-
const epochData = await this.gatherEpochData(epochNumber);
|
|
606
|
-
const fromCheckpoint = epochData.checkpoints[0].number;
|
|
607
|
-
const toCheckpoint = epochData.checkpoints.at(-1).number;
|
|
608
|
-
const fromBlock = epochData.checkpoints[0].blocks[0].number;
|
|
609
|
-
const lastBlock = epochData.checkpoints.at(-1).blocks.at(-1);
|
|
610
|
-
const toBlock = lastBlock.number;
|
|
611
|
-
this.log.verbose(`Creating proving job for epoch ${epochNumber} for checkpoint range ${fromCheckpoint} to ${toCheckpoint} and block range ${fromBlock} to ${toBlock}`);
|
|
612
|
-
// Fast forward world state to right before the target block and get a fork
|
|
613
|
-
const lastBlockHash = await lastBlock.header.hash();
|
|
614
|
-
await this.worldState.syncImmediate(toBlock, lastBlockHash);
|
|
615
|
-
// Create a processor factory
|
|
616
|
-
const publicProcessorFactory = new PublicProcessorFactory(this.contractDataSource, this.dateProvider, this.telemetryClient, this.log.getBindings());
|
|
617
|
-
// Set deadline for this job to run. It will abort if it takes too long.
|
|
618
|
-
const deadlineTs = getProofSubmissionDeadlineTimestamp(epochNumber, await this.getL1Constants());
|
|
619
|
-
const deadline = new Date(Number(deadlineTs) * 1000);
|
|
620
|
-
const job = this.doCreateEpochProvingJob(epochData, deadline, publicProcessorFactory, this.publisher, opts);
|
|
621
|
-
this.jobs.set(job.getId(), job);
|
|
622
|
-
return job;
|
|
623
|
-
}
|
|
871
|
+
// ---------------- helpers ----------------
|
|
624
872
|
getL1Constants() {
|
|
625
873
|
return this.l2BlockSource.getL1Constants();
|
|
626
874
|
}
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
const
|
|
638
|
-
|
|
875
|
+
/**
|
|
876
|
+
* Returns true if every block in the given epoch is proven on L1. An epoch is only
|
|
877
|
+
* fully proven when its *last* block is proven. Protected for direct unit-test access.
|
|
878
|
+
*/ async isEpochFullyProven(epochNumber, l1Constants) {
|
|
879
|
+
const provenBlockNumber = await this.l2BlockSource.getBlockNumber({
|
|
880
|
+
tag: 'proven'
|
|
881
|
+
});
|
|
882
|
+
if (!provenBlockNumber || provenBlockNumber <= 0) {
|
|
883
|
+
return false;
|
|
884
|
+
}
|
|
885
|
+
const provenHeader = (await this.l2BlockSource.getBlockData({
|
|
886
|
+
number: BlockNumber(provenBlockNumber)
|
|
887
|
+
}))?.header;
|
|
888
|
+
if (!provenHeader) {
|
|
889
|
+
return false;
|
|
890
|
+
}
|
|
891
|
+
const provenEpoch = getEpochAtSlot(provenHeader.getSlot(), l1Constants);
|
|
892
|
+
if (epochNumber < provenEpoch) {
|
|
893
|
+
return true;
|
|
894
|
+
}
|
|
895
|
+
if (epochNumber > provenEpoch) {
|
|
896
|
+
return false;
|
|
897
|
+
}
|
|
898
|
+
return this.isProvenBlockLastOfItsEpoch(BlockNumber(provenBlockNumber), provenEpoch, l1Constants);
|
|
899
|
+
}
|
|
900
|
+
/** Protected for direct unit-test access. */ async isProvenBlockLastOfItsEpoch(provenBlockNumber, provenEpoch, l1Constants) {
|
|
901
|
+
const nextHeader = (await this.l2BlockSource.getBlockData({
|
|
902
|
+
number: BlockNumber(provenBlockNumber + 1)
|
|
903
|
+
}))?.header;
|
|
904
|
+
if (nextHeader) {
|
|
905
|
+
return getEpochAtSlot(nextHeader.getSlot(), l1Constants) > provenEpoch;
|
|
906
|
+
}
|
|
907
|
+
return this.l2BlockSource.isEpochComplete(provenEpoch);
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Resolves the last fully-proven epoch from L1 proven state, used to seed the catch-up cursor (via
|
|
911
|
+
* `computeStartingCheckpoint`) and `lastExpiredEpoch`. The fully-proven epoch is `provenEpoch` when the
|
|
912
|
+
* proven tip is the last block of its epoch, otherwise `provenEpoch - 1`, or `undefined` if no block is
|
|
913
|
+
* proven yet (so a restart reprocesses the partially-proven epoch rather than trusting a stale tip).
|
|
914
|
+
*/ async resolveLastFullyProvenEpoch() {
|
|
915
|
+
const provenBlockNumber = await this.l2BlockSource.getBlockNumber({
|
|
916
|
+
tag: 'proven'
|
|
917
|
+
});
|
|
918
|
+
if (!provenBlockNumber || provenBlockNumber <= 0) {
|
|
919
|
+
return {
|
|
920
|
+
lastFullyProvenEpoch: undefined
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
const l1Constants = await this.getL1Constants();
|
|
924
|
+
const provenHeader = (await this.l2BlockSource.getBlockData({
|
|
925
|
+
number: BlockNumber(provenBlockNumber)
|
|
926
|
+
}))?.header;
|
|
927
|
+
if (!provenHeader) {
|
|
928
|
+
return {
|
|
929
|
+
lastFullyProvenEpoch: undefined
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
const provenEpoch = getEpochAtSlot(provenHeader.getSlot(), l1Constants);
|
|
933
|
+
if (await this.isProvenBlockLastOfItsEpoch(BlockNumber(provenBlockNumber), provenEpoch, l1Constants)) {
|
|
934
|
+
return {
|
|
935
|
+
lastFullyProvenEpoch: provenEpoch
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
const lastFullyProvenEpoch = provenEpoch > 0 ? EpochNumber(provenEpoch - 1) : undefined;
|
|
639
939
|
return {
|
|
640
|
-
|
|
641
|
-
txs,
|
|
642
|
-
l1ToL2Messages,
|
|
643
|
-
epochNumber,
|
|
644
|
-
previousBlockHeader,
|
|
645
|
-
attestations
|
|
940
|
+
lastFullyProvenEpoch
|
|
646
941
|
};
|
|
647
942
|
}
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
async gatherTxs(epochNumber, checkpoints) {
|
|
656
|
-
const deadline = new Date(this.dateProvider.now() + this.config.txGatheringTimeoutMs);
|
|
657
|
-
const txProvider = this.p2pClient.getTxProvider();
|
|
658
|
-
const blocks = checkpoints.flatMap((checkpoint)=>checkpoint.blocks);
|
|
659
|
-
const txsByBlock = await Promise.all(blocks.map((block)=>txProvider.getTxsForBlock(block, {
|
|
660
|
-
deadline
|
|
661
|
-
})));
|
|
662
|
-
const txs = txsByBlock.map(({ txs })=>txs).flat();
|
|
663
|
-
const missingTxs = txsByBlock.map(({ missingTxs })=>missingTxs).flat();
|
|
664
|
-
if (missingTxs.length === 0) {
|
|
665
|
-
this.log.verbose(`Gathered all ${txs.length} txs for epoch ${epochNumber}`, {
|
|
666
|
-
epochNumber
|
|
667
|
-
});
|
|
668
|
-
return txs;
|
|
943
|
+
/**
|
|
944
|
+
* Resolves the catch-up cursor seed: the last checkpoint of the last fully-proven epoch, or 0 if none. Seeding
|
|
945
|
+
* from a checkpoint (rather than a checkpointed tip) guarantees a restart reprocesses every checkpoint of the
|
|
946
|
+
* partially-proven epoch, since the checkpointed tip can sit ahead of the last fully-proven checkpoint.
|
|
947
|
+
*/ async computeStartingCheckpoint(lastFullyProvenEpoch) {
|
|
948
|
+
if (lastFullyProvenEpoch === undefined) {
|
|
949
|
+
return CheckpointNumber.ZERO;
|
|
669
950
|
}
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
async gatherMessages(epochNumber, checkpoints) {
|
|
673
|
-
const messages = await Promise.all(checkpoints.map((c)=>this.l1ToL2MessageSource.getL1ToL2Messages(c.number)));
|
|
674
|
-
const messageCount = sum(messages.map((m)=>m.length));
|
|
675
|
-
this.log.verbose(`Gathered all ${messageCount} messages for epoch ${epochNumber}`, {
|
|
676
|
-
epochNumber
|
|
951
|
+
const checkpoints = await this.l2BlockSource.getCheckpointsData({
|
|
952
|
+
epoch: lastFullyProvenEpoch
|
|
677
953
|
});
|
|
678
|
-
|
|
679
|
-
for(let i = 0; i < checkpoints.length; i++){
|
|
680
|
-
messagesByCheckpoint[checkpoints[i].number] = messages[i];
|
|
681
|
-
}
|
|
682
|
-
return messagesByCheckpoint;
|
|
683
|
-
}
|
|
684
|
-
async gatherPreviousBlockHeader(epochNumber, previousBlockNumber) {
|
|
685
|
-
const header = await (previousBlockNumber === 0 ? this.worldState.getCommitted().getInitialHeader() : this.l2BlockSource.getBlockHeader(BlockNumber(previousBlockNumber)));
|
|
686
|
-
if (!header) {
|
|
687
|
-
throw new Error(`Previous block header ${previousBlockNumber} not found for proving epoch ${epochNumber}`);
|
|
688
|
-
}
|
|
689
|
-
this.log.verbose(`Gathered previous block header ${header.getBlockNumber()} for epoch ${epochNumber}`);
|
|
690
|
-
return header;
|
|
691
|
-
}
|
|
692
|
-
/** Extracted for testing purposes. */ doCreateEpochProvingJob(data, deadline, publicProcessorFactory, publisher, opts = {}) {
|
|
693
|
-
const { proverNodeMaxParallelBlocksPerEpoch: parallelBlockLimit, proverNodeDisableProofPublish } = this.config;
|
|
694
|
-
return new EpochProvingJob(data, this.worldState, this.prover.createEpochProver(), publicProcessorFactory, publisher, this.l2BlockSource, this.jobMetrics, deadline, {
|
|
695
|
-
parallelBlockLimit,
|
|
696
|
-
skipSubmitProof: proverNodeDisableProofPublish,
|
|
697
|
-
...opts
|
|
698
|
-
}, this.log.getBindings());
|
|
954
|
+
return checkpoints.at(-1)?.checkpointNumber ?? CheckpointNumber.ZERO;
|
|
699
955
|
}
|
|
700
|
-
|
|
701
|
-
await this.
|
|
956
|
+
async gatherPreviousBlockHeader(previousBlockNumber) {
|
|
957
|
+
const data = await this.l2BlockSource.getBlockData({
|
|
958
|
+
number: BlockNumber(previousBlockNumber)
|
|
959
|
+
});
|
|
960
|
+
if (!data?.header) {
|
|
961
|
+
throw new Error(`Previous block header ${previousBlockNumber} not found`);
|
|
962
|
+
}
|
|
963
|
+
return data.header;
|
|
702
964
|
}
|
|
703
965
|
validateConfig() {
|
|
704
966
|
if (this.config.proverNodeFailedEpochStore && (!this.config.dataDirectory || !this.config.l1ChainId || this.config.rollupVersion === undefined)) {
|
|
@@ -707,9 +969,5 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
707
969
|
}
|
|
708
970
|
}
|
|
709
971
|
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
super(`No blocks found for epoch ${epochNumber}`);
|
|
713
|
-
this.name = 'EmptyEpochError';
|
|
714
|
-
}
|
|
715
|
-
}
|
|
972
|
+
// Re-export so handlers can compare states externally.
|
|
973
|
+
export { EpochProvingJobTerminalState };
|