@aztec/prover-node 0.0.1-commit.f2ce05ee → 0.0.1-commit.f5a9928
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 +572 -0
- package/dest/actions/download-epoch-proving-job.js +1 -1
- package/dest/actions/rerun-epoch-proving-job.d.ts +14 -4
- package/dest/actions/rerun-epoch-proving-job.d.ts.map +1 -1
- package/dest/actions/rerun-epoch-proving-job.js +146 -24
- package/dest/actions/upload-epoch-proof-failure.d.ts +2 -2
- package/dest/actions/upload-epoch-proof-failure.d.ts.map +1 -1
- package/dest/bin/run-failed-epoch.js +6 -5
- package/dest/checkpoint-store.d.ts +95 -0
- package/dest/checkpoint-store.d.ts.map +1 -0
- package/dest/checkpoint-store.js +178 -0
- package/dest/config.d.ts +7 -8
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +24 -20
- package/dest/factory.d.ts +19 -13
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +41 -65
- 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 +165 -0
- package/dest/job/checkpoint-prover.d.ts.map +1 -0
- package/dest/job/checkpoint-prover.js +405 -0
- package/dest/job/epoch-session.d.ts +160 -0
- package/dest/job/epoch-session.d.ts.map +1 -0
- package/dest/job/{epoch-proving-job.js → epoch-session.js} +301 -298
- 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 +40 -3
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +101 -4
- package/dest/monitors/epoch-monitor.d.ts +1 -1
- package/dest/monitors/epoch-monitor.d.ts.map +1 -1
- package/dest/monitors/epoch-monitor.js +11 -9
- 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 +25 -15
- package/dest/prover-node-publisher.d.ts.map +1 -1
- package/dest/prover-node-publisher.js +201 -62
- package/dest/prover-node.d.ts +144 -69
- package/dest/prover-node.d.ts.map +1 -1
- package/dest/prover-node.js +543 -221
- package/dest/prover-publisher-factory.d.ts +6 -4
- package/dest/prover-publisher-factory.d.ts.map +1 -1
- package/dest/prover-publisher-factory.js +4 -3
- package/dest/session-manager.d.ts +158 -0
- package/dest/session-manager.d.ts.map +1 -0
- package/dest/session-manager.js +492 -0
- package/dest/test/index.d.ts +7 -6
- package/dest/test/index.d.ts.map +1 -1
- package/package.json +23 -22
- package/src/actions/download-epoch-proving-job.ts +1 -1
- package/src/actions/rerun-epoch-proving-job.ts +178 -33
- package/src/actions/upload-epoch-proof-failure.ts +1 -1
- package/src/bin/run-failed-epoch.ts +5 -3
- package/src/checkpoint-store.ts +212 -0
- package/src/config.ts +35 -32
- package/src/factory.ts +69 -111
- package/src/index.ts +1 -0
- package/src/job/checkpoint-prover.ts +538 -0
- package/src/job/epoch-session.ts +462 -0
- package/src/job/top-tree-job.ts +227 -0
- package/src/metrics.ts +123 -10
- package/src/monitors/epoch-monitor.ts +5 -6
- package/src/proof-publishing-service.ts +427 -0
- package/src/prover-node-publisher.ts +236 -78
- package/src/prover-node.ts +628 -247
- package/src/prover-publisher-factory.ts +8 -5
- package/src/session-manager.ts +592 -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/src/job/epoch-proving-job.ts +0 -435
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 { EventDrivenL2BlockStream, 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,38 +412,45 @@ _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;
|
|
418
|
+
delayer;
|
|
419
|
+
dateProvider;
|
|
407
420
|
static{
|
|
408
421
|
({ e: [_initProto] } = _apply_decs_2203_r(this, [
|
|
409
|
-
[
|
|
410
|
-
_dec,
|
|
411
|
-
2,
|
|
412
|
-
"createProvingJob"
|
|
413
|
-
],
|
|
414
422
|
[
|
|
415
423
|
memoize,
|
|
416
424
|
2,
|
|
417
425
|
"getL1Constants"
|
|
418
|
-
],
|
|
419
|
-
[
|
|
420
|
-
_dec1,
|
|
421
|
-
2,
|
|
422
|
-
"gatherEpochData"
|
|
423
426
|
]
|
|
424
427
|
], []));
|
|
425
428
|
}
|
|
426
429
|
log;
|
|
427
|
-
|
|
428
|
-
|
|
430
|
+
checkpointStore;
|
|
431
|
+
chonkCache;
|
|
432
|
+
sessionManager;
|
|
429
433
|
config;
|
|
430
434
|
jobMetrics;
|
|
431
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;
|
|
432
451
|
tracer;
|
|
433
|
-
|
|
434
|
-
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()){
|
|
435
454
|
this.prover = prover;
|
|
436
455
|
this.publisherFactory = publisherFactory;
|
|
437
456
|
this.l2BlockSource = l2BlockSource;
|
|
@@ -439,17 +458,17 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
439
458
|
this.contractDataSource = contractDataSource;
|
|
440
459
|
this.worldState = worldState;
|
|
441
460
|
this.p2pClient = p2pClient;
|
|
442
|
-
this.epochsMonitor = epochsMonitor;
|
|
443
461
|
this.rollupContract = rollupContract;
|
|
444
462
|
this.l1Metrics = l1Metrics;
|
|
445
463
|
this.telemetryClient = telemetryClient;
|
|
464
|
+
this.delayer = delayer;
|
|
465
|
+
this.dateProvider = dateProvider;
|
|
446
466
|
this.log = (_initProto(this), createLogger('prover-node'));
|
|
447
|
-
this.
|
|
448
|
-
this.jobs = new Map();
|
|
467
|
+
this.lastProcessedCheckpoint = CheckpointNumber.ZERO;
|
|
449
468
|
this.config = {
|
|
450
469
|
proverNodePollingIntervalMs: 1_000,
|
|
451
470
|
proverNodeMaxPendingJobs: 100,
|
|
452
|
-
proverNodeMaxParallelBlocksPerEpoch:
|
|
471
|
+
proverNodeMaxParallelBlocksPerEpoch: 0,
|
|
453
472
|
txGatheringIntervalMs: 1_000,
|
|
454
473
|
txGatheringBatchSize: 10,
|
|
455
474
|
txGatheringMaxParallelRequestsPerNode: 100,
|
|
@@ -463,6 +482,23 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
463
482
|
this.tracer = telemetryClient.getTracer('ProverNode');
|
|
464
483
|
this.jobMetrics = new ProverNodeJobMetrics(meter, telemetryClient.getTracer('EpochProvingJob'));
|
|
465
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
|
+
// A checkpoint prover that fails (a sub-tree fault or a prune-induced fork fault) uploads a
|
|
499
|
+
// post-mortem for its own checkpoint, independently of any session. Fire-and-forget.
|
|
500
|
+
onFailed: (prover)=>void this.tryUploadCheckpointFailure(prover)
|
|
501
|
+
}, this.log.getBindings());
|
|
466
502
|
}
|
|
467
503
|
getProverId() {
|
|
468
504
|
return this.prover.getProverId();
|
|
@@ -470,230 +506,520 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
470
506
|
getP2P() {
|
|
471
507
|
return this.p2pClient;
|
|
472
508
|
}
|
|
509
|
+
/** Test-only: the shared L1 tx delayer, if enabled. */ getDelayer() {
|
|
510
|
+
return this.delayer;
|
|
511
|
+
}
|
|
512
|
+
/** Observability summary for the ProverNodeApi. */ getJobs() {
|
|
513
|
+
return Promise.resolve(this.sessionManager?.getJobs() ?? []);
|
|
514
|
+
}
|
|
515
|
+
/** Tests inspect this when validating reconcile behaviour. */ getCheckpointStore() {
|
|
516
|
+
return this.checkpointStore;
|
|
517
|
+
}
|
|
518
|
+
/** Tests inspect this to verify chonk-cache release semantics. */ getChonkCache() {
|
|
519
|
+
return this.chonkCache;
|
|
520
|
+
}
|
|
521
|
+
/** Tests inspect this when looking up live sessions. */ getSessionManager() {
|
|
522
|
+
if (!this.sessionManager) {
|
|
523
|
+
throw new Error('SessionManager not yet constructed — start() must be called first.');
|
|
524
|
+
}
|
|
525
|
+
return this.sessionManager;
|
|
526
|
+
}
|
|
527
|
+
/** Returns the underlying prover instance. */ getProver() {
|
|
528
|
+
return this.prover;
|
|
529
|
+
}
|
|
530
|
+
// ---------------- L2BlockStream handler ----------------
|
|
531
|
+
async handleBlockStreamEvent(event) {
|
|
532
|
+
switch(event.type){
|
|
533
|
+
case 'chain-checkpointed':
|
|
534
|
+
await this.processCheckpointJump(event.checkpoint.number);
|
|
535
|
+
break;
|
|
536
|
+
case 'chain-pruned':
|
|
537
|
+
await this.handlePruneEvent(event.block);
|
|
538
|
+
break;
|
|
539
|
+
case 'chain-proven':
|
|
540
|
+
this.publishingService?.onChainProven(BlockNumber(event.block.number));
|
|
541
|
+
break;
|
|
542
|
+
// The proposed tip drives only the tips store's walk-back history (recorded below); the prover-node
|
|
543
|
+
// tracks checkpoints, not proposed blocks. `blocks-added` is never emitted in tips-only mode, and
|
|
544
|
+
// `chain-finalized` carries nothing the prover-node acts on.
|
|
545
|
+
case 'chain-proposed':
|
|
546
|
+
case 'chain-finalized':
|
|
547
|
+
case 'blocks-added':
|
|
548
|
+
break;
|
|
549
|
+
default:
|
|
550
|
+
{
|
|
551
|
+
const _ = event;
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
// Advance the local tips store only after the proving-side handling (registration / prune) has
|
|
556
|
+
// succeeded. Any failure above propagates to the L2BlockStream (which logs and stops this poll
|
|
557
|
+
// pass) and skips this update, so the event is re-emitted on the next poll rather than skipped
|
|
558
|
+
await this.tipsStore.handleBlockStreamEvent(event);
|
|
559
|
+
}
|
|
473
560
|
/**
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
561
|
+
* Walks every checkpoint between the local cursor and the newly-reported checkpointed tip, registering
|
|
562
|
+
* each one that belongs to an epoch that can still be proven. The block stream now delivers a single thin
|
|
563
|
+
* `chain-checkpointed` tip event per pass rather than one fat event per checkpoint, so this drives the
|
|
564
|
+
* catch-up itself: light metadata first (`getCheckpointsData`) to decide relevance per epoch, then a heavy
|
|
565
|
+
* `getCheckpoints` fetch only for checkpoints in provable epochs.
|
|
566
|
+
*
|
|
567
|
+
* The cursor advances one checkpoint at a time and only after that checkpoint's proving-side handling has
|
|
568
|
+
* fully succeeded, preserving the A-1041 at-least-once semantics: a mid-jump failure leaves the cursor
|
|
569
|
+
* behind so the next pass retries from the first checkpoint that did not complete.
|
|
570
|
+
*/ async processCheckpointJump(targetCheckpoint) {
|
|
571
|
+
if (targetCheckpoint <= this.lastProcessedCheckpoint) {
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const l1Constants = await this.getL1Constants();
|
|
575
|
+
// Cap the catch-up at the `(proofSubmissionEpochs + 1) * epochDuration` most recent checkpoints.
|
|
576
|
+
// When the cursor is much further behind (e.g. resyncing after a long time offline), fetching the whole gap could
|
|
577
|
+
// load thousands of checkpoints we cannot act on: anything older than the last two epochs is already past
|
|
578
|
+
// its proof-submission window, so we skip it and jump the cursor forward to the start of the capped range.
|
|
579
|
+
const maxCheckpoints = (l1Constants.proofSubmissionEpochs + 1) * l1Constants.epochDuration;
|
|
580
|
+
let from = CheckpointNumber(this.lastProcessedCheckpoint + 1);
|
|
581
|
+
if (Number(targetCheckpoint - from) + 1 > maxCheckpoints) {
|
|
582
|
+
const cappedFrom = CheckpointNumber(targetCheckpoint - maxCheckpoints + 1);
|
|
583
|
+
this.log.warn(`Skipping unprovable checkpoints during catch-up; the prover node is far behind`, {
|
|
584
|
+
from,
|
|
585
|
+
cappedFrom,
|
|
586
|
+
targetCheckpoint,
|
|
587
|
+
maxCheckpoints
|
|
481
588
|
});
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
589
|
+
// Advance the cursor past the skipped checkpoints so they are never retried.
|
|
590
|
+
this.lastProcessedCheckpoint = CheckpointNumber(cappedFrom - 1);
|
|
591
|
+
from = cappedFrom;
|
|
592
|
+
}
|
|
593
|
+
const limit = Number(targetCheckpoint - from) + 1;
|
|
594
|
+
const metadatas = await this.l2BlockSource.getCheckpointsData({
|
|
595
|
+
from,
|
|
596
|
+
limit
|
|
597
|
+
});
|
|
598
|
+
// Per-epoch relevance is cached so a multi-checkpoint epoch resolves it once. Skipping is whole-epoch
|
|
599
|
+
// only: the SessionManager requires an epoch's checkpoints fully covered before it opens a session, so we
|
|
600
|
+
// never drop an individual checkpoint inside an epoch we will prove.
|
|
601
|
+
const epochSkippable = new Map();
|
|
602
|
+
for (const metadata of metadatas){
|
|
603
|
+
const epochNumber = getEpochAtSlot(metadata.header.slotNumber, l1Constants);
|
|
604
|
+
let skippable = epochSkippable.get(epochNumber);
|
|
605
|
+
if (skippable === undefined) {
|
|
606
|
+
skippable = await this.isEpochFullyProven(epochNumber, l1Constants) || await this.isEpochPastProofSubmissionWindow(epochNumber, l1Constants);
|
|
607
|
+
epochSkippable.set(epochNumber, skippable);
|
|
488
608
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
} catch (err) {
|
|
492
|
-
if (err instanceof EmptyEpochError) {
|
|
493
|
-
this.log.info(`Not starting proof for ${epochNumber} since no blocks were found`);
|
|
609
|
+
if (skippable) {
|
|
610
|
+
this.log.debug(`Skipping checkpoint ${metadata.checkpointNumber} for unprovable epoch ${epochNumber}`);
|
|
494
611
|
} else {
|
|
495
|
-
this.
|
|
612
|
+
await this.registerCheckpoint(metadata.checkpointNumber, epochNumber);
|
|
496
613
|
}
|
|
614
|
+
// Advance only after the checkpoint's handling succeeded (or it was legitimately skipped). registerCheckpoint
|
|
615
|
+
// throws on failure, which leaves the cursor here for the next pass to retry (A-1041).
|
|
616
|
+
this.lastProcessedCheckpoint = metadata.checkpointNumber;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
/** Heavy-fetch a single checkpoint, register it with the store, and notify the session manager. */ async registerCheckpoint(checkpointNumber, epochNumber) {
|
|
620
|
+
const published = await this.l2BlockSource.getCheckpoint({
|
|
621
|
+
number: checkpointNumber
|
|
622
|
+
});
|
|
623
|
+
if (!published) {
|
|
624
|
+
throw new Error(`Checkpoint ${checkpointNumber} not found in block source during catch-up`);
|
|
625
|
+
}
|
|
626
|
+
const checkpoint = published.checkpoint;
|
|
627
|
+
this.log.info(`New checkpoint ${checkpoint.number} for epoch ${epochNumber}`, {
|
|
628
|
+
checkpointNumber: checkpoint.number,
|
|
629
|
+
epochNumber,
|
|
630
|
+
slotNumber: checkpoint.header.slotNumber
|
|
631
|
+
});
|
|
632
|
+
const registerData = await this.collectRegisterData(checkpoint, published.attestations);
|
|
633
|
+
await this.checkpointStore.addOrUpdate(checkpoint, registerData);
|
|
634
|
+
await this.sessionManager?.onCheckpointAdded(epochNumber);
|
|
635
|
+
// Tips-only mode delivers no blocks, so record one witness per checkpointed block: a reorg into the checkpoint's
|
|
636
|
+
// range then prunes at the true divergence instead of the nearest sparse tip anchor.
|
|
637
|
+
await this.tipsStore.recordBlockHashes(await Promise.all(checkpoint.blocks.map(async (block)=>({
|
|
638
|
+
number: block.number,
|
|
639
|
+
hash: (await block.header.hash()).toString()
|
|
640
|
+
}))));
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Gathers register-time data for a checkpoint: previous block header, L1-to-L2 messages,
|
|
644
|
+
* and the archive sibling path.
|
|
645
|
+
*/ async collectRegisterData(checkpoint, attestations) {
|
|
646
|
+
const previousBlockNumber = BlockNumber(checkpoint.blocks[0].number - 1);
|
|
647
|
+
const previousBlockHeader = await this.gatherPreviousBlockHeader(previousBlockNumber);
|
|
648
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpoint.number);
|
|
649
|
+
const lastBlock = checkpoint.blocks.at(-1);
|
|
650
|
+
const lastBlockHash = await lastBlock.header.hash();
|
|
651
|
+
await this.worldState.syncImmediate(lastBlock.number, lastBlockHash);
|
|
652
|
+
const previousArchiveSiblingPath = await getLastSiblingPath(MerkleTreeId.ARCHIVE, this.worldState.getSnapshot(previousBlockNumber));
|
|
653
|
+
return {
|
|
654
|
+
attestations,
|
|
655
|
+
previousBlockHeader,
|
|
656
|
+
l1ToL2Messages,
|
|
657
|
+
previousArchiveSiblingPath
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Marks every prover orphaned by the prune as pruned, clamps the catch-up cursor below the prune target's
|
|
662
|
+
* checkpoint, and notifies the session manager. Keyed off the prune target block (the highest surviving block)
|
|
663
|
+
* rather than the source's checkpointed tip, which can sit above the target after a re-checkpoint and would leave
|
|
664
|
+
* orphaned provers canonical. Throws (rather than warning) if the cursor floor cannot be resolved, so the pass
|
|
665
|
+
* fails and the prune is retried next iteration.
|
|
666
|
+
*/ async handlePruneEvent(prunedToBlock) {
|
|
667
|
+
this.log.warn(`Chain pruned to block ${prunedToBlock.number}`, {
|
|
668
|
+
prunedToBlock
|
|
669
|
+
});
|
|
670
|
+
// Resolve the cursor floor BEFORE removing provers: cancelAndRemoveAboveBlock returns only the provers it removed,
|
|
671
|
+
// so a throw after removing would leave a retry pass with nothing to act on. Resolving first means a throw leaves
|
|
672
|
+
// everything untouched and the next pass retries the whole handler (the tips cursor only advances on success).
|
|
673
|
+
let cursorFloor;
|
|
674
|
+
if (prunedToBlock.number === 0) {
|
|
675
|
+
cursorFloor = CheckpointNumber.ZERO;
|
|
676
|
+
} else {
|
|
677
|
+
const targetData = await this.l2BlockSource.getBlockData({
|
|
678
|
+
number: prunedToBlock.number
|
|
679
|
+
});
|
|
680
|
+
if (targetData === undefined) {
|
|
681
|
+
throw new Error(`No block data found for prune target block ${prunedToBlock.number}; cannot clamp checkpoint cursor`);
|
|
682
|
+
}
|
|
683
|
+
// Clamp to `cpAtTarget - 1`: a mid-checkpoint target leaves that checkpoint partially orphaned and it must be
|
|
684
|
+
// reprocessed. Over-clamping merely re-registers a checkpoint (at-least-once by design — A-1041); under-clamping
|
|
685
|
+
// would permanently skip a rebuilt same-number checkpoint.
|
|
686
|
+
cursorFloor = CheckpointNumber(Math.max(0, Number(targetData.checkpointNumber) - 1));
|
|
687
|
+
}
|
|
688
|
+
const affected = this.checkpointStore.cancelAndRemoveAboveBlock(prunedToBlock.number);
|
|
689
|
+
if (this.lastProcessedCheckpoint > cursorFloor) {
|
|
690
|
+
this.lastProcessedCheckpoint = cursorFloor;
|
|
691
|
+
}
|
|
692
|
+
if (affected.length === 0) {
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
const l1Constants = await this.getL1Constants();
|
|
696
|
+
const affectedEpochs = Array.from(new Set(affected.map((p)=>Number(getEpochAtSlot(p.slotNumber, l1Constants))))).map((n)=>EpochNumber(n));
|
|
697
|
+
// The session manager cancels every affected session, which in turn calls
|
|
698
|
+
// publishingService.withdraw(uuid) for each candidate; no separate notification to the
|
|
699
|
+
// publishing service is needed.
|
|
700
|
+
await this.sessionManager?.onPrune(affectedEpochs);
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Returns true once the chain has advanced past the given epoch's proof-submission window.
|
|
704
|
+
* Used to ignore checkpoints whose epoch can no longer be proven in time — chiefly while the
|
|
705
|
+
* archiver replays old blocks after a restart. Compares the archiver's latest synced L2 slot
|
|
706
|
+
* against the epoch's submission-deadline epoch; conservatively returns false if the slot can't
|
|
707
|
+
* be read yet.
|
|
708
|
+
*/ async isEpochPastProofSubmissionWindow(epochNumber, l1Constants) {
|
|
709
|
+
const latestSlot = await this.l2BlockSource.getSyncedL2SlotNumber();
|
|
710
|
+
if (latestSlot === undefined) {
|
|
497
711
|
return false;
|
|
498
712
|
}
|
|
713
|
+
const latestEpoch = getEpochAtSlot(latestSlot, l1Constants);
|
|
714
|
+
return latestEpoch >= getProofSubmissionDeadlineEpoch(epochNumber, l1Constants);
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Compares the archiver's latest synced L2 slot against `lastExpiredEpoch` and, for each
|
|
718
|
+
* newly-expired epoch, releases the chonk-cache entries for its blocks and reaps any
|
|
719
|
+
* CheckpointProvers in the store. An epoch E is expired once the chain reaches the start
|
|
720
|
+
* of epoch `E + proofSubmissionEpochs + 1`. Silently no-ops if nothing has expired since
|
|
721
|
+
* the last check or the archiver's slot can't be read.
|
|
722
|
+
*/ async checkEpochExpiry() {
|
|
723
|
+
const latestSlot = await this.l2BlockSource.getSyncedL2SlotNumber();
|
|
724
|
+
if (latestSlot === undefined) {
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
const l1Constants = await this.getL1Constants();
|
|
728
|
+
const latestEpoch = getEpochAtSlot(latestSlot, l1Constants);
|
|
729
|
+
const offset = l1Constants.proofSubmissionEpochs + 1;
|
|
730
|
+
if (latestEpoch < offset) {
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
const newlyExpiredUpTo = EpochNumber(latestEpoch - offset);
|
|
734
|
+
const from = this.lastExpiredEpoch === undefined ? EpochNumber(0) : EpochNumber(this.lastExpiredEpoch + 1);
|
|
735
|
+
if (newlyExpiredUpTo < from) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
for(let e = from; e <= newlyExpiredUpTo; e = EpochNumber(e + 1)){
|
|
739
|
+
await this.expireEpoch(e);
|
|
740
|
+
}
|
|
741
|
+
this.lastExpiredEpoch = newlyExpiredUpTo;
|
|
499
742
|
}
|
|
500
743
|
/**
|
|
501
|
-
*
|
|
502
|
-
*
|
|
503
|
-
*/ async
|
|
504
|
-
|
|
744
|
+
* Releases chonk-cache entries for every block in the supplied epoch (best-effort) and reaps every
|
|
745
|
+
* CheckpointProver in the store whose epoch is at or below it.
|
|
746
|
+
*/ async expireEpoch(epoch) {
|
|
747
|
+
try {
|
|
748
|
+
const blocks = await this.l2BlockSource.getBlocks({
|
|
749
|
+
epoch,
|
|
750
|
+
onlyCheckpointed: true
|
|
751
|
+
});
|
|
752
|
+
if (blocks.length > 0) {
|
|
753
|
+
this.chonkCache.releaseForBlocks(blocks);
|
|
754
|
+
}
|
|
755
|
+
} catch (err) {
|
|
756
|
+
this.log.warn(`Could not release chonk-cache entries for expired epoch ${epoch}`, err);
|
|
757
|
+
}
|
|
758
|
+
this.checkpointStore.reapExpired(epoch);
|
|
759
|
+
}
|
|
760
|
+
// ---------------- public API ----------------
|
|
761
|
+
/**
|
|
762
|
+
* Schedules proving for the given epoch and returns the job id without waiting for completion.
|
|
763
|
+
*/ async startProof(epochNumber) {
|
|
764
|
+
if (!this.sessionManager) {
|
|
765
|
+
throw new Error('ProverNode not started');
|
|
766
|
+
}
|
|
767
|
+
return await this.sessionManager.startProof(epochNumber);
|
|
768
|
+
}
|
|
769
|
+
// ---------------- Service lifecycle ----------------
|
|
770
|
+
async start() {
|
|
771
|
+
await this.checkpointStore.start();
|
|
505
772
|
await this.publisherFactory.start();
|
|
506
|
-
this.
|
|
773
|
+
this.publishingService = new ProofPublishingService({
|
|
774
|
+
publisherFactory: this.publisherFactory,
|
|
775
|
+
l2BlockSource: this.l2BlockSource,
|
|
776
|
+
dateProvider: this.dateProvider,
|
|
777
|
+
config: {
|
|
778
|
+
skipSubmitProof: !!this.config.proverNodeDisableProofPublish
|
|
779
|
+
},
|
|
780
|
+
bindings: this.log.getBindings()
|
|
781
|
+
});
|
|
782
|
+
this.sessionManager = this.createSessionManager(this.publishingService);
|
|
783
|
+
// SessionManager owns its own periodic tick; start it here so it begins picking up
|
|
784
|
+
// epochs that become complete by time (no fresh checkpoint event) and advances once
|
|
785
|
+
// the previous epoch is proven on L1.
|
|
786
|
+
this.sessionManager.start();
|
|
787
|
+
// Now that the store + manager exist, arm the live-state observable gauges.
|
|
788
|
+
this.jobMetrics.observeState(this.checkpointStore, this.sessionManager);
|
|
789
|
+
const { lastFullyProvenEpoch } = await this.resolveLastFullyProvenEpoch();
|
|
790
|
+
this.lastExpiredEpoch = lastFullyProvenEpoch;
|
|
791
|
+
this.lastProcessedCheckpoint = await this.computeStartingCheckpoint(lastFullyProvenEpoch);
|
|
792
|
+
this.blockStream = new EventDrivenL2BlockStream(this.l2BlockSource, this.tipsStore, this, this.log, {
|
|
793
|
+
pollIntervalMS: this.config.proverNodePollingIntervalMs,
|
|
794
|
+
tipsOnly: true
|
|
795
|
+
});
|
|
796
|
+
this.blockStream.start();
|
|
797
|
+
// The periodic ticker is the sole driver of the expiry sweep: it fires every poll interval whether
|
|
798
|
+
// or not block-stream events arrive, and RunningPromise never overlaps its own runs, so the sweep's
|
|
799
|
+
// `lastExpiredEpoch` high-water mark advances — and each epoch's post-mortem uploads — exactly once.
|
|
800
|
+
this.expiryTicker = new RunningPromise(()=>this.checkEpochExpiry(), this.log, this.config.proverNodePollingIntervalMs);
|
|
801
|
+
this.expiryTicker.start();
|
|
507
802
|
await this.rewardsMetrics.start();
|
|
508
803
|
this.l1Metrics.start();
|
|
509
804
|
this.log.info(`Started Prover Node with prover id ${this.prover.getProverId().toString()}`, this.config);
|
|
510
805
|
}
|
|
511
|
-
|
|
512
|
-
* Stops the prover node and all its dependencies.
|
|
513
|
-
*/ async stop() {
|
|
806
|
+
async stop() {
|
|
514
807
|
this.log.info('Stopping ProverNode');
|
|
515
|
-
|
|
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();
|
|
516
823
|
await this.prover.stop();
|
|
517
|
-
await tryStop(this.p2pClient);
|
|
518
|
-
await tryStop(this.l2BlockSource);
|
|
519
824
|
await tryStop(this.publisherFactory);
|
|
520
|
-
this.publisher?.interrupt();
|
|
521
|
-
await Promise.all(Array.from(this.jobs.values()).map((job)=>job.stop()));
|
|
522
|
-
await this.worldState.stop();
|
|
523
825
|
this.rewardsMetrics.stop();
|
|
524
826
|
this.l1Metrics.stop();
|
|
525
827
|
await this.telemetryClient.stop();
|
|
526
828
|
this.log.info('Stopped ProverNode');
|
|
527
829
|
}
|
|
528
|
-
/**
|
|
529
|
-
|
|
530
|
-
|
|
830
|
+
/**
|
|
831
|
+
* Constructs the session manager. Extracted so subclasses (test harness) can swap the
|
|
832
|
+
* implementation. Wired to upload a post-mortem when a full session ends in its own genuine failure
|
|
833
|
+
* (`EpochSession.hasFailed()` — top-tree/submit failed with every prover healthy, so definitively not
|
|
834
|
+
* a prune). A `stopped` session (a prover under it failed) is not uploaded; it recovers on re-add.
|
|
835
|
+
*/ createSessionManager(publishingService) {
|
|
836
|
+
return new SessionManager({
|
|
837
|
+
checkpointStore: this.checkpointStore,
|
|
838
|
+
l2BlockSource: this.l2BlockSource,
|
|
839
|
+
proverFactory: this.prover,
|
|
840
|
+
proverId: this.prover.getProverId(),
|
|
841
|
+
publishingService,
|
|
842
|
+
metrics: this.jobMetrics,
|
|
843
|
+
dateProvider: this.dateProvider,
|
|
844
|
+
config: {
|
|
845
|
+
maxPendingJobs: this.config.proverNodeMaxPendingJobs,
|
|
846
|
+
tickIntervalMs: this.config.proverNodePollingIntervalMs,
|
|
847
|
+
finalizationDelayMs: this.config.proverNodeEpochProvingDelayMs
|
|
848
|
+
},
|
|
849
|
+
onSessionFailed: async (session)=>{
|
|
850
|
+
await this.tryUploadEpochFailure(session.getId(), session.getCheckpoints());
|
|
851
|
+
},
|
|
852
|
+
bindings: this.log.getBindings()
|
|
853
|
+
});
|
|
531
854
|
}
|
|
532
|
-
/**
|
|
533
|
-
|
|
855
|
+
/**
|
|
856
|
+
* Installs session hooks for the e2e harness to interpose around top-tree proving
|
|
857
|
+
* (gate, override, or observe it) without monkey-patching the orchestrator factory.
|
|
858
|
+
* Applies to every session constructed after this call.
|
|
859
|
+
*/ setSessionHooks(hooks) {
|
|
860
|
+
if (!this.sessionManager) {
|
|
861
|
+
throw new Error('ProverNode not started; call start() before setting session hooks.');
|
|
862
|
+
}
|
|
863
|
+
this.sessionManager.setSessionHooks(hooks);
|
|
534
864
|
}
|
|
535
865
|
/**
|
|
536
|
-
*
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
});
|
|
541
|
-
void this.runJob(job);
|
|
866
|
+
* Installs checkpoint-prover test hooks (e.g. forcing a sub-tree failure) applied to every
|
|
867
|
+
* CheckpointProver constructed after this call. For the e2e harness only.
|
|
868
|
+
*/ setCheckpointHooks(hooks) {
|
|
869
|
+
this.checkpointStore.setTestHooks(hooks);
|
|
542
870
|
}
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
871
|
+
/**
|
|
872
|
+
* Uploads a post-mortem snapshot for an epoch whose full session failed to prove, built from that
|
|
873
|
+
* session's checkpoint provers. Fired from the session manager's `onSessionFailed` callback (a
|
|
874
|
+
* genuine, race-free failure). Exposed as a method so tests can spy on it. No-ops if no failed-epoch
|
|
875
|
+
* store is configured or the checkpoint set is empty.
|
|
876
|
+
*/ async tryUploadEpochFailure(id, checkpoints) {
|
|
877
|
+
if (!this.config.proverNodeFailedEpochStore || checkpoints.length === 0) {
|
|
878
|
+
return undefined;
|
|
879
|
+
}
|
|
880
|
+
const data = await SessionManager.buildProvingData(checkpoints);
|
|
881
|
+
return await uploadEpochProofFailure(this.config.proverNodeFailedEpochStore, // The session's own id; `uploadEpochProofFailure` already prefixes the path with the epoch number.
|
|
882
|
+
id, data, this.l2BlockSource, this.worldState, assertRequired(pick(this.config, 'l1ChainId', 'rollupVersion', 'dataDirectory')), this.log);
|
|
883
|
+
}
|
|
884
|
+
/**
|
|
885
|
+
* Uploads a post-mortem for a single failed checkpoint prover, built from just that checkpoint's
|
|
886
|
+
* proving data. Fired (fire-and-forget) from the store's `onFailed` callback for any non-cancel
|
|
887
|
+
* block-proof failure — a genuine sub-tree fault or a prune-induced fork fault alike. No-ops if no
|
|
888
|
+
* failed-epoch store is configured, or if the checkpoint is no longer canonical (a prune left nothing
|
|
889
|
+
* to diagnose). Swallows its own errors so a fire-and-forget caller can't leak.
|
|
890
|
+
*/ async tryUploadCheckpointFailure(prover) {
|
|
891
|
+
if (!this.config.proverNodeFailedEpochStore) {
|
|
892
|
+
return undefined;
|
|
893
|
+
}
|
|
550
894
|
try {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
this.log.verbose(`Job for ${epochNumber} exited with state ${state}`, ctx);
|
|
895
|
+
// A prune-induced fork fault and a genuine sub-tree failure are indistinguishable at the moment the
|
|
896
|
+
// prover rejects (no control-plane cancel has landed yet). But the archiver is the authoritative
|
|
897
|
+
// committed chain: if this checkpoint was pruned out, its last block is no longer canonical there.
|
|
898
|
+
// Only upload for a checkpoint that still exists on-chain — a prune leaves nothing to diagnose, and
|
|
899
|
+
// the snapshot (full world-state + archiver) is expensive to produce and store.
|
|
900
|
+
if (!await this.isCheckpointCanonical(prover.checkpoint)) {
|
|
901
|
+
this.log.debug(`Skipping checkpoint-failure upload for ${prover.id}: no longer canonical (pruned)`, {
|
|
902
|
+
checkpointNumber: prover.checkpoint.number
|
|
903
|
+
});
|
|
904
|
+
return undefined;
|
|
562
905
|
}
|
|
906
|
+
const data = await SessionManager.buildProvingData([
|
|
907
|
+
prover
|
|
908
|
+
]);
|
|
909
|
+
return await uploadEpochProofFailure(this.config.proverNodeFailedEpochStore, // The prover's content-addressed id; the epoch number is already in the upload path.
|
|
910
|
+
prover.id, data, this.l2BlockSource, this.worldState, assertRequired(pick(this.config, 'l1ChainId', 'rollupVersion', 'dataDirectory')), this.log);
|
|
563
911
|
} catch (err) {
|
|
564
|
-
this.log.error(`Error
|
|
565
|
-
|
|
566
|
-
this.jobs.delete(job.getId());
|
|
912
|
+
this.log.error(`Error uploading checkpoint failure for ${prover.id}`, err);
|
|
913
|
+
return undefined;
|
|
567
914
|
}
|
|
568
915
|
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
916
|
+
// ---------------- helpers ----------------
|
|
917
|
+
/**
|
|
918
|
+
* True if the checkpoint still exists on the canonical chain: the archiver holds a block at its last
|
|
919
|
+
* block's height whose archive root matches. A prune (fork fault) leaves the block missing or replaced,
|
|
920
|
+
* so this returns false. Protected for direct unit-test access.
|
|
921
|
+
*/ async isCheckpointCanonical(checkpoint) {
|
|
922
|
+
const lastBlock = checkpoint.blocks.at(-1);
|
|
923
|
+
if (!lastBlock) {
|
|
924
|
+
return false;
|
|
572
925
|
}
|
|
926
|
+
const onChain = await this.l2BlockSource.getBlock({
|
|
927
|
+
number: lastBlock.number
|
|
928
|
+
});
|
|
929
|
+
return !!onChain && onChain.archive.root.equals(checkpoint.archive.root);
|
|
573
930
|
}
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
*/ getProver() {
|
|
577
|
-
return this.prover;
|
|
931
|
+
getL1Constants() {
|
|
932
|
+
return this.l2BlockSource.getL1Constants();
|
|
578
933
|
}
|
|
579
934
|
/**
|
|
580
|
-
* Returns
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
const
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
const fromCheckpoint = epochData.checkpoints[0].number;
|
|
604
|
-
const toCheckpoint = epochData.checkpoints.at(-1).number;
|
|
605
|
-
const fromBlock = epochData.checkpoints[0].blocks[0].number;
|
|
606
|
-
const toBlock = epochData.checkpoints.at(-1).blocks.at(-1).number;
|
|
607
|
-
this.log.verbose(`Creating proving job for epoch ${epochNumber} for checkpoint range ${fromCheckpoint} to ${toCheckpoint} and block range ${fromBlock} to ${toBlock}`);
|
|
608
|
-
// Fast forward world state to right before the target block and get a fork
|
|
609
|
-
await this.worldState.syncImmediate(toBlock);
|
|
610
|
-
// Create a processor factory
|
|
611
|
-
const publicProcessorFactory = new PublicProcessorFactory(this.contractDataSource, this.dateProvider, this.telemetryClient, this.log.getBindings());
|
|
612
|
-
// Set deadline for this job to run. It will abort if it takes too long.
|
|
613
|
-
const deadlineTs = getProofSubmissionDeadlineTimestamp(epochNumber, await this.getL1Constants());
|
|
614
|
-
const deadline = new Date(Number(deadlineTs) * 1000);
|
|
615
|
-
const job = this.doCreateEpochProvingJob(epochData, deadline, publicProcessorFactory, this.publisher, opts);
|
|
616
|
-
this.jobs.set(job.getId(), job);
|
|
617
|
-
return job;
|
|
935
|
+
* Returns true if every block in the given epoch is proven on L1. An epoch is only
|
|
936
|
+
* fully proven when its *last* block is proven. Protected for direct unit-test access.
|
|
937
|
+
*/ async isEpochFullyProven(epochNumber, l1Constants) {
|
|
938
|
+
const provenBlockNumber = await this.l2BlockSource.getBlockNumber({
|
|
939
|
+
tag: 'proven'
|
|
940
|
+
});
|
|
941
|
+
if (!provenBlockNumber || provenBlockNumber <= 0) {
|
|
942
|
+
return false;
|
|
943
|
+
}
|
|
944
|
+
const provenHeader = (await this.l2BlockSource.getBlockData({
|
|
945
|
+
number: BlockNumber(provenBlockNumber)
|
|
946
|
+
}))?.header;
|
|
947
|
+
if (!provenHeader) {
|
|
948
|
+
return false;
|
|
949
|
+
}
|
|
950
|
+
const provenEpoch = getEpochAtSlot(provenHeader.getSlot(), l1Constants);
|
|
951
|
+
if (epochNumber < provenEpoch) {
|
|
952
|
+
return true;
|
|
953
|
+
}
|
|
954
|
+
if (epochNumber > provenEpoch) {
|
|
955
|
+
return false;
|
|
956
|
+
}
|
|
957
|
+
return this.isProvenBlockLastOfItsEpoch(BlockNumber(provenBlockNumber), provenEpoch, l1Constants);
|
|
618
958
|
}
|
|
619
|
-
|
|
620
|
-
|
|
959
|
+
/** Protected for direct unit-test access. */ async isProvenBlockLastOfItsEpoch(provenBlockNumber, provenEpoch, l1Constants) {
|
|
960
|
+
const nextHeader = (await this.l2BlockSource.getBlockData({
|
|
961
|
+
number: BlockNumber(provenBlockNumber + 1)
|
|
962
|
+
}))?.header;
|
|
963
|
+
if (nextHeader) {
|
|
964
|
+
return getEpochAtSlot(nextHeader.getSlot(), l1Constants) > provenEpoch;
|
|
965
|
+
}
|
|
966
|
+
return this.l2BlockSource.isEpochComplete(provenEpoch);
|
|
621
967
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
968
|
+
/**
|
|
969
|
+
* Resolves the last fully-proven epoch from L1 proven state, used to seed the catch-up cursor (via
|
|
970
|
+
* `computeStartingCheckpoint`) and `lastExpiredEpoch`. The fully-proven epoch is `provenEpoch` when the
|
|
971
|
+
* proven tip is the last block of its epoch, otherwise `provenEpoch - 1`, or `undefined` if no block is
|
|
972
|
+
* proven yet (so a restart reprocesses the partially-proven epoch rather than trusting a stale tip).
|
|
973
|
+
*/ async resolveLastFullyProvenEpoch() {
|
|
974
|
+
const provenBlockNumber = await this.l2BlockSource.getBlockNumber({
|
|
975
|
+
tag: 'proven'
|
|
976
|
+
});
|
|
977
|
+
if (!provenBlockNumber || provenBlockNumber <= 0) {
|
|
978
|
+
return {
|
|
979
|
+
lastFullyProvenEpoch: undefined
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
const l1Constants = await this.getL1Constants();
|
|
983
|
+
const provenHeader = (await this.l2BlockSource.getBlockData({
|
|
984
|
+
number: BlockNumber(provenBlockNumber)
|
|
985
|
+
}))?.header;
|
|
986
|
+
if (!provenHeader) {
|
|
987
|
+
return {
|
|
988
|
+
lastFullyProvenEpoch: undefined
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
const provenEpoch = getEpochAtSlot(provenHeader.getSlot(), l1Constants);
|
|
992
|
+
if (await this.isProvenBlockLastOfItsEpoch(BlockNumber(provenBlockNumber), provenEpoch, l1Constants)) {
|
|
993
|
+
return {
|
|
994
|
+
lastFullyProvenEpoch: provenEpoch
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
const lastFullyProvenEpoch = provenEpoch > 0 ? EpochNumber(provenEpoch - 1) : undefined;
|
|
634
998
|
return {
|
|
635
|
-
|
|
636
|
-
txs,
|
|
637
|
-
l1ToL2Messages,
|
|
638
|
-
epochNumber,
|
|
639
|
-
previousBlockHeader,
|
|
640
|
-
attestations
|
|
999
|
+
lastFullyProvenEpoch
|
|
641
1000
|
};
|
|
642
1001
|
}
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
async gatherTxs(epochNumber, checkpoints) {
|
|
651
|
-
const deadline = new Date(this.dateProvider.now() + this.config.txGatheringTimeoutMs);
|
|
652
|
-
const txProvider = this.p2pClient.getTxProvider();
|
|
653
|
-
const blocks = checkpoints.flatMap((checkpoint)=>checkpoint.blocks);
|
|
654
|
-
const txsByBlock = await Promise.all(blocks.map((block)=>txProvider.getTxsForBlock(block, {
|
|
655
|
-
deadline
|
|
656
|
-
})));
|
|
657
|
-
const txs = txsByBlock.map(({ txs })=>txs).flat();
|
|
658
|
-
const missingTxs = txsByBlock.map(({ missingTxs })=>missingTxs).flat();
|
|
659
|
-
if (missingTxs.length === 0) {
|
|
660
|
-
this.log.verbose(`Gathered all ${txs.length} txs for epoch ${epochNumber}`, {
|
|
661
|
-
epochNumber
|
|
662
|
-
});
|
|
663
|
-
return txs;
|
|
1002
|
+
/**
|
|
1003
|
+
* Resolves the catch-up cursor seed: the last checkpoint of the last fully-proven epoch, or 0 if none. Seeding
|
|
1004
|
+
* from a checkpoint (rather than a checkpointed tip) guarantees a restart reprocesses every checkpoint of the
|
|
1005
|
+
* partially-proven epoch, since the checkpointed tip can sit ahead of the last fully-proven checkpoint.
|
|
1006
|
+
*/ async computeStartingCheckpoint(lastFullyProvenEpoch) {
|
|
1007
|
+
if (lastFullyProvenEpoch === undefined) {
|
|
1008
|
+
return CheckpointNumber.ZERO;
|
|
664
1009
|
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
async gatherMessages(epochNumber, checkpoints) {
|
|
668
|
-
const messages = await Promise.all(checkpoints.map((c)=>this.l1ToL2MessageSource.getL1ToL2Messages(c.number)));
|
|
669
|
-
const messageCount = sum(messages.map((m)=>m.length));
|
|
670
|
-
this.log.verbose(`Gathered all ${messageCount} messages for epoch ${epochNumber}`, {
|
|
671
|
-
epochNumber
|
|
1010
|
+
const checkpoints = await this.l2BlockSource.getCheckpointsData({
|
|
1011
|
+
epoch: lastFullyProvenEpoch
|
|
672
1012
|
});
|
|
673
|
-
|
|
674
|
-
for(let i = 0; i < checkpoints.length; i++){
|
|
675
|
-
messagesByCheckpoint[checkpoints[i].number] = messages[i];
|
|
676
|
-
}
|
|
677
|
-
return messagesByCheckpoint;
|
|
678
|
-
}
|
|
679
|
-
async gatherPreviousBlockHeader(epochNumber, previousBlockNumber) {
|
|
680
|
-
const header = await (previousBlockNumber === 0 ? this.worldState.getCommitted().getInitialHeader() : this.l2BlockSource.getBlockHeader(BlockNumber(previousBlockNumber)));
|
|
681
|
-
if (!header) {
|
|
682
|
-
throw new Error(`Previous block header ${previousBlockNumber} not found for proving epoch ${epochNumber}`);
|
|
683
|
-
}
|
|
684
|
-
this.log.verbose(`Gathered previous block header ${header.getBlockNumber()} for epoch ${epochNumber}`);
|
|
685
|
-
return header;
|
|
686
|
-
}
|
|
687
|
-
/** Extracted for testing purposes. */ doCreateEpochProvingJob(data, deadline, publicProcessorFactory, publisher, opts = {}) {
|
|
688
|
-
const { proverNodeMaxParallelBlocksPerEpoch: parallelBlockLimit, proverNodeDisableProofPublish } = this.config;
|
|
689
|
-
return new EpochProvingJob(data, this.worldState, this.prover.createEpochProver(), publicProcessorFactory, publisher, this.l2BlockSource, this.jobMetrics, deadline, {
|
|
690
|
-
parallelBlockLimit,
|
|
691
|
-
skipSubmitProof: proverNodeDisableProofPublish,
|
|
692
|
-
...opts
|
|
693
|
-
}, this.log.getBindings());
|
|
1013
|
+
return checkpoints.at(-1)?.checkpointNumber ?? CheckpointNumber.ZERO;
|
|
694
1014
|
}
|
|
695
|
-
|
|
696
|
-
await this.
|
|
1015
|
+
async gatherPreviousBlockHeader(previousBlockNumber) {
|
|
1016
|
+
const data = await this.l2BlockSource.getBlockData({
|
|
1017
|
+
number: BlockNumber(previousBlockNumber)
|
|
1018
|
+
});
|
|
1019
|
+
if (!data?.header) {
|
|
1020
|
+
throw new Error(`Previous block header ${previousBlockNumber} not found`);
|
|
1021
|
+
}
|
|
1022
|
+
return data.header;
|
|
697
1023
|
}
|
|
698
1024
|
validateConfig() {
|
|
699
1025
|
if (this.config.proverNodeFailedEpochStore && (!this.config.dataDirectory || !this.config.l1ChainId || this.config.rollupVersion === undefined)) {
|
|
@@ -702,9 +1028,5 @@ _dec = trackSpan('ProverNode.createProvingJob', (epochNumber)=>({
|
|
|
702
1028
|
}
|
|
703
1029
|
}
|
|
704
1030
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
super(`No blocks found for epoch ${epochNumber}`);
|
|
708
|
-
this.name = 'EmptyEpochError';
|
|
709
|
-
}
|
|
710
|
-
}
|
|
1031
|
+
// Re-export so handlers can compare states externally.
|
|
1032
|
+
export { EpochProvingJobTerminalState };
|