@aztec/prover-node 0.0.1-commit.a89ec08 → 0.0.1-commit.aa0c64f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +572 -0
  2. package/dest/actions/download-epoch-proving-job.js +1 -1
  3. package/dest/actions/rerun-epoch-proving-job.d.ts +14 -4
  4. package/dest/actions/rerun-epoch-proving-job.d.ts.map +1 -1
  5. package/dest/actions/rerun-epoch-proving-job.js +245 -23
  6. package/dest/actions/upload-epoch-proof-failure.d.ts +2 -2
  7. package/dest/actions/upload-epoch-proof-failure.d.ts.map +1 -1
  8. package/dest/bin/run-failed-epoch.js +1 -3
  9. package/dest/checkpoint-store.d.ts +95 -0
  10. package/dest/checkpoint-store.d.ts.map +1 -0
  11. package/dest/checkpoint-store.js +178 -0
  12. package/dest/config.d.ts +4 -2
  13. package/dest/config.d.ts.map +1 -1
  14. package/dest/config.js +9 -2
  15. package/dest/factory.d.ts +4 -1
  16. package/dest/factory.d.ts.map +1 -1
  17. package/dest/factory.js +23 -8
  18. package/dest/index.d.ts +2 -1
  19. package/dest/index.d.ts.map +1 -1
  20. package/dest/index.js +1 -0
  21. package/dest/job/checkpoint-prover.d.ts +154 -0
  22. package/dest/job/checkpoint-prover.d.ts.map +1 -0
  23. package/dest/job/checkpoint-prover.js +363 -0
  24. package/dest/job/epoch-session.d.ts +160 -0
  25. package/dest/job/epoch-session.d.ts.map +1 -0
  26. package/dest/job/epoch-session.js +744 -0
  27. package/dest/job/top-tree-job.d.ts +82 -0
  28. package/dest/job/top-tree-job.d.ts.map +1 -0
  29. package/dest/job/top-tree-job.js +152 -0
  30. package/dest/metrics.d.ts +35 -8
  31. package/dest/metrics.d.ts.map +1 -1
  32. package/dest/metrics.js +86 -14
  33. package/dest/monitors/epoch-monitor.js +6 -2
  34. package/dest/proof-publishing-service.d.ts +161 -0
  35. package/dest/proof-publishing-service.d.ts.map +1 -0
  36. package/dest/proof-publishing-service.js +335 -0
  37. package/dest/prover-node-publisher.d.ts +25 -15
  38. package/dest/prover-node-publisher.d.ts.map +1 -1
  39. package/dest/prover-node-publisher.js +201 -62
  40. package/dest/prover-node.d.ts +134 -67
  41. package/dest/prover-node.d.ts.map +1 -1
  42. package/dest/prover-node.js +538 -219
  43. package/dest/prover-publisher-factory.d.ts +4 -2
  44. package/dest/prover-publisher-factory.d.ts.map +1 -1
  45. package/dest/prover-publisher-factory.js +4 -3
  46. package/dest/session-manager.d.ts +158 -0
  47. package/dest/session-manager.d.ts.map +1 -0
  48. package/dest/session-manager.js +486 -0
  49. package/dest/test/index.d.ts +7 -6
  50. package/dest/test/index.d.ts.map +1 -1
  51. package/package.json +24 -23
  52. package/src/actions/download-epoch-proving-job.ts +1 -1
  53. package/src/actions/rerun-epoch-proving-job.ts +190 -31
  54. package/src/actions/upload-epoch-proof-failure.ts +1 -1
  55. package/src/bin/run-failed-epoch.ts +1 -2
  56. package/src/checkpoint-store.ts +212 -0
  57. package/src/config.ts +13 -2
  58. package/src/factory.ts +23 -10
  59. package/src/index.ts +1 -0
  60. package/src/job/checkpoint-prover.ts +496 -0
  61. package/src/job/epoch-session.ts +462 -0
  62. package/src/job/top-tree-job.ts +227 -0
  63. package/src/metrics.ts +102 -23
  64. package/src/monitors/epoch-monitor.ts +2 -2
  65. package/src/proof-publishing-service.ts +427 -0
  66. package/src/prover-node-publisher.ts +236 -78
  67. package/src/prover-node.ts +621 -244
  68. package/src/prover-publisher-factory.ts +6 -3
  69. package/src/session-manager.ts +589 -0
  70. package/src/test/index.ts +6 -6
  71. package/dest/job/epoch-proving-job.d.ts +0 -63
  72. package/dest/job/epoch-proving-job.d.ts.map +0 -1
  73. package/dest/job/epoch-proving-job.js +0 -762
  74. package/src/job/epoch-proving-job.ts +0 -465
@@ -0,0 +1,363 @@
1
+ import { NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import { padArrayEnd } from '@aztec/foundation/collection';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
+ import { promiseWithResolvers } from '@aztec/foundation/promise';
6
+ import { Timer } from '@aztec/foundation/timer';
7
+ import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
8
+ import { protocolContractsHash } from '@aztec/protocol-contracts';
9
+ import { PublicSimulatorConfig } from '@aztec/stdlib/avm';
10
+ import { CheckpointConstantData } from '@aztec/stdlib/rollup';
11
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
12
+ /**
13
+ * Self-contained per-checkpoint prover, content-addressed by
14
+ * `(checkpoint number, slot number, checkpoint archive root)`.
15
+ *
16
+ * The store creates a CheckpointProver once per content-key. Keying on the checkpoint's
17
+ * own archive root (its post-state) means two checkpoints are "the same" iff they
18
+ * produce the same archive — so a reorg branch, or a replacement built on the same
19
+ * predecessor but with different content, keys to a distinct prover.
20
+ *
21
+ * The prover eagerly starts its own tx gather and sub-tree work in the constructor, so
22
+ * callers only need to call `whenBlockProofsReady()` to obtain the resulting block-rollup
23
+ * proofs.
24
+ *
25
+ * A CheckpointProver does not survive a prune: its sub-tree work forks world-state per
26
+ * block, and an L1 prune of a base block faults those reads. The store therefore cancels and
27
+ * discards a prover when its checkpoint is pruned, and a re-add (even of identical content)
28
+ * constructs a fresh prover.
29
+ *
30
+ * `cancel()` is idempotent. It aborts the gather + sub-tree, rejects the block-proof
31
+ * promise, and exposes a `whenDone()` that resolves once teardown has unwound.
32
+ */ export class CheckpointProver {
33
+ deps;
34
+ id;
35
+ checkpoint;
36
+ epochNumber;
37
+ slotNumber;
38
+ attestations;
39
+ previousBlockHeader;
40
+ l1ToL2Messages;
41
+ previousArchiveSiblingPath;
42
+ /** Per-prover tx map — populated by the internal gather. Empty until then. */ txs;
43
+ /** Resolved by the sub-tree on success, rejected on cancel/failure. */ blockProofs;
44
+ // Three independent lifecycle facts — deliberately not collapsed into one status enum, because several
45
+ // combinations are legal and relied on: a prover can be `completed` and then `cancelled` (routine
46
+ // teardown of an already-proven checkpoint), or `completed` and then `failed` (block proving was
47
+ // enqueued, but the sub-tree subsequently faulted). Only `failed` + `cancelled` is excluded — a cancel
48
+ // is not a failure (enforced in `failBlockProofs`).
49
+ /** Block-level proving was fully *enqueued* (a progress marker; the sub-tree may still be proving). */ completed;
50
+ /** Block proofs rejected for a genuine (non-cancel) reason — a sub-tree or prune-induced fork fault. */ failed;
51
+ /** The prover was torn down (prune / reap / shutdown). */ cancelled;
52
+ subTree;
53
+ abortController;
54
+ /** Tracks the eager gather+execute task so `cancel()` and `whenDone()` can await its unwind. */ runPromise;
55
+ /** Tracks the cancel-driven teardown so `whenDone()` can await it. */ cancelPromise;
56
+ constructor(args, deps){
57
+ this.deps = deps;
58
+ this.txs = new Map();
59
+ this.blockProofs = promiseWithResolvers();
60
+ this.completed = false;
61
+ this.failed = false;
62
+ this.cancelled = false;
63
+ this.abortController = new AbortController();
64
+ this.checkpoint = args.checkpoint;
65
+ this.epochNumber = args.epochNumber;
66
+ this.slotNumber = args.checkpoint.header.slotNumber;
67
+ this.attestations = args.attestations;
68
+ this.previousBlockHeader = args.previousBlockHeader;
69
+ this.l1ToL2Messages = args.l1ToL2Messages;
70
+ this.previousArchiveSiblingPath = args.previousArchiveSiblingPath;
71
+ this.id = CheckpointProver.idFor(args.checkpoint);
72
+ // Mark blockProofs as observed so a cancel that lands before any consumer awaits
73
+ // does not surface as an unhandled rejection.
74
+ this.blockProofs.promise.catch(()=>{});
75
+ deps.log.info(`Created CheckpointProver ${this.id}`, {
76
+ checkpointNumber: this.checkpoint.number,
77
+ epochNumber: this.epochNumber,
78
+ slotNumber: this.slotNumber,
79
+ blockCount: this.checkpoint.blocks.length,
80
+ l1ToL2MessageCount: this.l1ToL2Messages.length,
81
+ archiveRoot: this.checkpoint.archive.root.toString()
82
+ });
83
+ // Kick off the eager gather + sub-tree pipeline.
84
+ this.runPromise = this.gatherAndExecute();
85
+ }
86
+ /**
87
+ * Stable content-addressed identifier: `${checkpoint number}:${slot}:${archive root}`.
88
+ * The archive root is the checkpoint's post-state, so it distinguishes any two
89
+ * checkpoints that differ in history or content while collapsing identical re-adds.
90
+ */ static idFor(checkpoint) {
91
+ return `${checkpoint.number}:${checkpoint.header.slotNumber}:${checkpoint.archive.root.toString()}`;
92
+ }
93
+ isCancelled() {
94
+ return this.cancelled;
95
+ }
96
+ /**
97
+ * True once this prover's block proofs have rejected for a genuine (non-cancel) reason — a sub-tree
98
+ * proving fault or a prune-induced world-state fork fault. A failed prover cannot produce its block
99
+ * proofs, so the reconciler must not build (or rebuild) an EpochSession over it; it is cleared only by
100
+ * a prune/re-add replacing it with a fresh prover, or by expiry reaping it.
101
+ */ isFailed() {
102
+ return this.failed;
103
+ }
104
+ /** AbortSignal that fires on cancel — for callers that want to wire their own tasks. */ getAbortSignal() {
105
+ return this.abortController.signal;
106
+ }
107
+ /** Promise that resolves with the block-rollup proofs for this checkpoint (or rejects on cancel/failure). */ whenBlockProofsReady() {
108
+ return this.blockProofs.promise;
109
+ }
110
+ /** Resolves when all in-flight work for this prover has fully unwound. */ async whenDone() {
111
+ await this.runPromise.catch(()=>{});
112
+ if (this.cancelPromise) {
113
+ await this.cancelPromise;
114
+ }
115
+ }
116
+ async gatherAndExecute() {
117
+ try {
118
+ const txs = await this.gatherTxs();
119
+ if (this.cancelled) {
120
+ return;
121
+ }
122
+ await this.executeCheckpoint(txs);
123
+ } catch (err) {
124
+ if (this.cancelled) {
125
+ this.deps.log.debug(`CheckpointProver ${this.id} cancelled during gather/execute`, {
126
+ checkpointNumber: this.checkpoint.number
127
+ });
128
+ return;
129
+ }
130
+ this.deps.log.error(`Error in CheckpointProver ${this.id}`, err, {
131
+ checkpointNumber: this.checkpoint.number
132
+ });
133
+ this.failBlockProofs(err instanceof Error ? err : new Error(String(err)));
134
+ }
135
+ }
136
+ /**
137
+ * Rejects the block-proof promise and, unless this is a cancellation, records the prover as failed so
138
+ * the reconciler won't build an EpochSession over it. First rejection wins, so a later duplicate reject
139
+ * (e.g. the executeCheckpoint `finally`) is a harmless no-op.
140
+ */ failBlockProofs(err) {
141
+ if (!this.cancelled && !this.failed) {
142
+ this.failed = true;
143
+ // Notify the owner so it can upload a post-mortem for this checkpoint. Fire-and-forget: the
144
+ // callback must not block the prover's teardown, and a throw in it must not mask the rejection.
145
+ try {
146
+ this.deps.onFailed?.(this);
147
+ } catch (err) {
148
+ this.deps.log.error(`Error in CheckpointProver onFailed callback for ${this.id}`, err);
149
+ }
150
+ }
151
+ this.blockProofs.reject(err);
152
+ }
153
+ async gatherTxs() {
154
+ const deadline = new Date(this.deps.dateProvider.now() + this.deps.txGatheringTimeoutMs);
155
+ const txsByBlock = await Promise.all(this.checkpoint.blocks.map((block)=>this.deps.txProvider.getTxsForBlock(block, {
156
+ deadline
157
+ })));
158
+ const txs = txsByBlock.map(({ txs })=>txs).flat();
159
+ const missingTxs = txsByBlock.map(({ missingTxs })=>missingTxs).flat();
160
+ if (missingTxs.length > 0) {
161
+ throw new Error(`Txs not found for checkpoint ${this.checkpoint.number}: ${missingTxs.map((hash)=>hash.toString()).join(', ')}`);
162
+ }
163
+ return new Map(txs.map((tx)=>[
164
+ tx.getTxHash().toString(),
165
+ tx
166
+ ]));
167
+ }
168
+ async executeCheckpoint(txs) {
169
+ const signal = this.abortController.signal;
170
+ const checkpointTimer = new Timer();
171
+ let subTreeStarted = false;
172
+ try {
173
+ // Test hook: force a sub-tree failure to exercise the checkpoint failure/upload path.
174
+ if (this.deps.checkpointProveOverride) {
175
+ await this.deps.checkpointProveOverride();
176
+ }
177
+ for (const [hash, tx] of txs){
178
+ this.txs.set(hash, tx);
179
+ }
180
+ const { chainId, version } = this.checkpoint.blocks[0].header.globalVariables;
181
+ const checkpointConstants = CheckpointConstantData.from({
182
+ chainId,
183
+ version,
184
+ vkTreeRoot: getVKTreeRoot(),
185
+ protocolContractsHash: protocolContractsHash,
186
+ proverId: this.deps.proverId.toField(),
187
+ slotNumber: this.checkpoint.header.slotNumber,
188
+ coinbase: this.checkpoint.header.coinbase,
189
+ feeRecipient: this.checkpoint.header.feeRecipient,
190
+ gasFees: this.checkpoint.header.gasFees
191
+ });
192
+ this.deps.log.info(`Starting processing checkpoint ${this.checkpoint.number}`, {
193
+ checkpointNumber: this.checkpoint.number,
194
+ checkpointHash: this.checkpoint.hash().toString(),
195
+ blockCount: this.checkpoint.blocks.length
196
+ });
197
+ this.subTree = await this.deps.proverFactory.createCheckpointSubTreeOrchestrator(this.deps.chonkCache, this.epochNumber, checkpointConstants, this.l1ToL2Messages, this.checkpoint.blocks.length, this.previousBlockHeader);
198
+ subTreeStarted = true;
199
+ // Bridge the sub-tree's result onto blockProofs.
200
+ void this.subTree.getSubTreeResult().then((result)=>{
201
+ this.deps.log.info(`Sub-tree block proofs ready for checkpoint ${this.checkpoint.number}`, {
202
+ checkpointNumber: this.checkpoint.number,
203
+ blockProofCount: result.blockProofOutputs.length
204
+ });
205
+ // Spans processing + proving (from executeCheckpoint start, after tx gathering) to proofs ready.
206
+ this.deps.metrics.recordCheckpointProving(checkpointTimer.ms());
207
+ this.blockProofs.resolve(result.blockProofOutputs);
208
+ }, (err)=>this.failBlockProofs(err instanceof Error ? err : new Error(String(err))));
209
+ if (signal.aborted) {
210
+ return;
211
+ }
212
+ const allTxs = this.checkpoint.blocks.flatMap((block)=>block.body.txEffects.map((txEffect)=>txs.get(txEffect.txHash.toString())));
213
+ const publicTxs = allTxs.filter((tx)=>tx?.data.forPublic);
214
+ if (publicTxs.length > 0) {
215
+ await this.subTree.startChonkVerifierCircuits(publicTxs);
216
+ if (signal.aborted) {
217
+ return;
218
+ }
219
+ }
220
+ for(let blockIndex = 0; blockIndex < this.checkpoint.blocks.length; blockIndex++){
221
+ const blockTimer = new Timer();
222
+ const block = this.checkpoint.blocks[blockIndex];
223
+ const globalVariables = block.header.globalVariables;
224
+ const blockTxs = this.getTxsForBlock(block, txs);
225
+ await this.subTree.startNewBlock(block.number, globalVariables.timestamp, blockTxs.length);
226
+ if (signal.aborted) {
227
+ return;
228
+ }
229
+ const db = await this.createFork(BlockNumber(block.number - 1), blockIndex === 0 ? this.l1ToL2Messages : undefined);
230
+ try {
231
+ if (signal.aborted) {
232
+ return;
233
+ }
234
+ const config = PublicSimulatorConfig.from({
235
+ proverId: this.deps.proverId.toField(),
236
+ skipFeeEnforcement: false,
237
+ collectDebugLogs: false,
238
+ collectHints: true,
239
+ collectPublicInputs: true,
240
+ collectStatistics: false
241
+ });
242
+ const publicProcessor = this.deps.publicProcessorFactory.create(db, globalVariables, config);
243
+ const processed = await this.processTxs(publicProcessor, blockTxs);
244
+ if (signal.aborted) {
245
+ return;
246
+ }
247
+ await this.subTree.addTxs(processed);
248
+ } finally{
249
+ await db.close();
250
+ }
251
+ if (signal.aborted) {
252
+ return;
253
+ }
254
+ await this.subTree.setBlockCompleted(block.number, block.header);
255
+ this.deps.metrics.recordBlockProcessing(blockTimer.ms());
256
+ if (signal.aborted) {
257
+ return;
258
+ }
259
+ }
260
+ this.completed = true;
261
+ const numTxs = this.checkpoint.blocks.reduce((acc, block)=>acc + block.body.txEffects.length, 0);
262
+ this.deps.metrics.recordCheckpointProcessing(checkpointTimer.ms(), this.checkpoint.blocks.length, numTxs);
263
+ this.deps.log.info(`Finished enqueueing block-level proving for checkpoint ${this.checkpoint.number} in ${checkpointTimer.ms()}ms`, {
264
+ checkpointNumber: this.checkpoint.number,
265
+ blockCount: this.checkpoint.blocks.length,
266
+ durationMs: checkpointTimer.ms()
267
+ });
268
+ } finally{
269
+ if (!this.completed) {
270
+ if (subTreeStarted) {
271
+ await this.teardownSubTree();
272
+ }
273
+ this.failBlockProofs(new Error(`Checkpoint ${this.id} did not complete block processing`));
274
+ }
275
+ }
276
+ }
277
+ /**
278
+ * Mark cancelled. Idempotent. Aborts in-flight work, rejects the block-proof promise,
279
+ * and kicks off a background teardown of the sub-tree. The teardown promise is exposed
280
+ * via `whenDone()`.
281
+ *
282
+ * `routine` distinguishes a post-finalize teardown (sub-tree already proven, fires
283
+ * once at prover exit) from a real abort (reorg, prune, deadline). Behaviour is
284
+ * identical either way; the flag only adjusts log verbosity.
285
+ */ cancel({ routine = false } = {}) {
286
+ if (this.cancelled) {
287
+ return;
288
+ }
289
+ this.cancelled = true;
290
+ // A teardown of a completed prover is routine regardless of the caller's flag —
291
+ // we logged the work as done already, so don't relabel it as a mid-flight cancel.
292
+ if (routine || this.completed) {
293
+ this.deps.log.verbose(`Tearing down CheckpointProver ${this.id}`, {
294
+ checkpointNumber: this.checkpoint.number,
295
+ wasCompleted: this.completed
296
+ });
297
+ } else {
298
+ this.deps.log.info(`Cancelling in-flight CheckpointProver ${this.id}`, {
299
+ checkpointNumber: this.checkpoint.number,
300
+ wasCompleted: this.completed
301
+ });
302
+ }
303
+ this.abortController.abort();
304
+ this.blockProofs.reject(new Error(`Checkpoint ${this.id} cancelled`));
305
+ this.cancelPromise = this.runCancel().catch(()=>{});
306
+ }
307
+ async runCancel() {
308
+ if (this.subTree) {
309
+ try {
310
+ this.subTree.cancel();
311
+ } catch (err) {
312
+ this.deps.log.error('Error cancelling sub-tree', err);
313
+ }
314
+ }
315
+ await this.runPromise.catch(()=>{});
316
+ if (this.subTree) {
317
+ await this.teardownSubTree();
318
+ }
319
+ }
320
+ async teardownSubTree() {
321
+ const { subTree } = this;
322
+ this.subTree = undefined;
323
+ if (subTree) {
324
+ this.deps.log.debug(`Tearing down sub-tree for checkpoint ${this.checkpoint.number}`, {
325
+ checkpointNumber: this.checkpoint.number
326
+ });
327
+ try {
328
+ await subTree.stop();
329
+ } catch (err) {
330
+ this.deps.log.error('Error stopping sub-tree', err);
331
+ }
332
+ }
333
+ }
334
+ getTxsForBlock(block, txs) {
335
+ return block.body.txEffects.map((txEffect)=>txs.get(txEffect.txHash.toString()));
336
+ }
337
+ async processTxs(publicProcessor, txs) {
338
+ // Pass the abort signal so a prune-driven cancel stops the current block's public execution
339
+ // immediately, rather than running it to completion before the next `signal.aborted` check.
340
+ // On abort `process` returns a partial result, the length check below throws, and
341
+ // `gatherAndExecute` swallows it via its `cancelled` guard.
342
+ const [processedTxs, failedTxs] = await publicProcessor.process(txs, {
343
+ deadline: this.deps.deadline,
344
+ signal: this.abortController.signal
345
+ });
346
+ if (failedTxs.length) {
347
+ const failedTxHashes = await Promise.all(failedTxs.map(({ tx })=>tx.getTxHash()));
348
+ throw new Error(`Txs failed processing: ${failedTxs.map(({ error }, index)=>`${failedTxHashes[index]} (${error})`).join(', ')}`);
349
+ }
350
+ if (processedTxs.length !== txs.length) {
351
+ throw new Error(`Failed to process all txs: processed ${processedTxs.length} out of ${txs.length}`);
352
+ }
353
+ return processedTxs;
354
+ }
355
+ async createFork(blockNumber, l1ToL2Messages) {
356
+ const db = await this.deps.dbProvider.fork(blockNumber);
357
+ if (l1ToL2Messages !== undefined) {
358
+ const l1ToL2MessagesPadded = padArrayEnd(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, 'Too many L1 to L2 messages');
359
+ await db.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
360
+ }
361
+ return db;
362
+ }
363
+ }
@@ -0,0 +1,160 @@
1
+ import { BlockNumber, type EpochNumber, type SlotNumber } from '@aztec/foundation/branded-types';
2
+ import type { EthAddress } from '@aztec/foundation/eth-address';
3
+ import { type LoggerBindings } from '@aztec/foundation/log';
4
+ import { type DateProvider } from '@aztec/foundation/timer';
5
+ import type { EpochProverFactory } from '@aztec/prover-client';
6
+ import { type EpochProvingJobState } from '@aztec/stdlib/interfaces/server';
7
+ import { type Traceable, type Tracer } from '@aztec/telemetry-client';
8
+ import type { ProverNodeJobMetrics } from '../metrics.js';
9
+ import type { ProofPublishingService } from '../proof-publishing-service.js';
10
+ import { CheckpointProver } from './checkpoint-prover.js';
11
+ import { type TopTreeProof } from './top-tree-job.js';
12
+ export type { EpochProvingJobState };
13
+ /** Full vs partial — the only behavioural difference is at the L1 submission step. */
14
+ export type SessionKind = 'full' | 'partial';
15
+ /**
16
+ * Identifies what a session proves: a contiguous slot range within an epoch. The
17
+ * concrete prover set the session holds is the *implementation* of the spec — frozen
18
+ * at construction time, derived from the canonical content for `[fromSlot, toSlot]`.
19
+ *
20
+ * Reconciliation in `ProverNode` is uniform across kinds: whenever the canonical
21
+ * content for the slot range changes, the session is cancelled and replaced with a
22
+ * fresh session that **preserves the slot range** but adopts the new checkpoints.
23
+ *
24
+ * Kind affects only the publishing decision (see `EpochSession`).
25
+ */
26
+ export interface SessionSpec {
27
+ kind: SessionKind;
28
+ epochNumber: EpochNumber;
29
+ fromSlot: SlotNumber;
30
+ toSlot: SlotNumber;
31
+ }
32
+ /** Stable string key for use in maps. */
33
+ export declare function specKey(spec: SessionSpec): string;
34
+ /** Hooks tests use to interpose around the top-tree prove without monkey-patching. */
35
+ export type EpochSessionHooks = {
36
+ beforeTopTreeProve?: () => Promise<void> | void;
37
+ afterTopTreeProve?: () => Promise<void> | void;
38
+ topTreeProveOverride?: (defaultProve: () => Promise<TopTreeProof>) => Promise<TopTreeProof>;
39
+ };
40
+ export type EpochSessionOptions = {
41
+ /**
42
+ * If set, the session sleeps this many ms after `start()` (before the TopTreeJob is
43
+ * constructed). Lets late-arriving events (e.g. a prune) be processed before
44
+ * top-tree proving begins.
45
+ */
46
+ finalizationDelayMs?: number;
47
+ };
48
+ /** Dependencies an `EpochSession` needs at construction. */
49
+ export type EpochSessionDeps = {
50
+ proverFactory: EpochProverFactory;
51
+ proverId: EthAddress;
52
+ publishingService: Pick<ProofPublishingService, 'submit' | 'withdraw'>;
53
+ metrics: ProverNodeJobMetrics;
54
+ dateProvider: DateProvider;
55
+ /** Optional proving deadline. The session enters `timed-out` if exceeded. */
56
+ deadline: Date | undefined;
57
+ config: EpochSessionOptions;
58
+ bindings?: LoggerBindings;
59
+ hooks?: EpochSessionHooks;
60
+ };
61
+ /**
62
+ * One attempt at proving and publishing a contiguous slot range. The `SessionSpec` and
63
+ * the prover set are both frozen at construction time; the session does not adapt to
64
+ * reorgs or extensions of canonical content. Instead, `SessionManager` owns the
65
+ * reconciliation loop and replaces invalidated sessions wholesale (cancel + construct
66
+ * a fresh session with the new prover set).
67
+ *
68
+ * Each session does three things in sequence:
69
+ *
70
+ * 1. Run a `TopTreeJob` over its frozen prover subset to produce the epoch proof.
71
+ * 2. Hand the proof to the shared `ProofPublishingService` as a `PublishCandidate`.
72
+ * 3. Translate the service's outcome into a terminal session state.
73
+ *
74
+ * Everything to do with submission — predecessor gating, same-epoch dedup, deadline
75
+ * enforcement, and the L1 transaction itself — is the publishing service's concern.
76
+ * The session is just the producer of one candidate and the observer of its outcome.
77
+ *
78
+ * Lifecycle (happy path):
79
+ *
80
+ * initialized → awaiting-checkpoints → awaiting-root → publishing-proof → completed
81
+ *
82
+ * Terminal states map the publishing outcome: `published` → `completed`, `superseded` →
83
+ * `superseded`, `expired` → `timed-out`, `withdrawn` → `cancelled`. A fault ends the attempt in one
84
+ * of two terminal states depending on its cause: `stopped` if a checkpoint prover under it failed
85
+ * (possibly a prune — the reconciler will rebuild over a fresh prover on re-add), or `failed` if the
86
+ * session's own top-tree/submit work failed while every prover was healthy (a genuine, non-prune
87
+ * failure the reconciler retains and uploads — see `hasFailed()`).
88
+ * Additionally, the session-level deadline fires `cancel('deadline')` and transitions
89
+ * to `timed-out` for the pre-submit window (top-tree proving) — the publishing service
90
+ * handles the post-submit window via the candidate's `deadline`.
91
+ *
92
+ * `cancel()` is idempotent. It marks the session terminal, calls
93
+ * `publishingService.withdraw(uuid)` to drop any queued candidate (an in-flight publish
94
+ * runs to natural completion; the session has already settled), and tears down the
95
+ * top-tree job if proving is still in progress.
96
+ */
97
+ export declare class EpochSession implements Traceable {
98
+ private readonly spec;
99
+ private readonly deps;
100
+ readonly tracer: Tracer;
101
+ private readonly uuid;
102
+ private readonly log;
103
+ private state;
104
+ private deadlineTimeoutHandler;
105
+ private topTreeJob;
106
+ /** Cancelled top-tree jobs whose teardown is still in flight. Awaited at session stop. */
107
+ private readonly pendingTopTreeCleanups;
108
+ private readonly completionPromise;
109
+ private resolveCompletion;
110
+ /** Stable reference; never mutated after construction. */
111
+ private readonly checkpoints;
112
+ constructor(spec: SessionSpec, checkpoints: readonly CheckpointProver[], deps: EpochSessionDeps);
113
+ getId(): string;
114
+ getSpec(): SessionSpec;
115
+ getState(): EpochProvingJobState;
116
+ getEpochNumber(): EpochNumber;
117
+ getKind(): SessionKind;
118
+ getDeadline(): Date | undefined;
119
+ getCheckpoints(): readonly CheckpointProver[];
120
+ /** Resolves when the session reaches a terminal state. */
121
+ whenDone(): Promise<EpochProvingJobState>;
122
+ /** True if the session is in a terminal state. */
123
+ isTerminal(): boolean;
124
+ /**
125
+ * True if the session ended in its own genuine failure — top-tree proving or L1 submission failed
126
+ * while every checkpoint prover succeeded. Because healthy provers rule out a prune-induced fault,
127
+ * this is a race-free "the epoch could not be proven" signal: the reconciler retains such a (full)
128
+ * session rather than re-proving it, and uploads a post-mortem. A `stopped` session (a checkpoint
129
+ * prover failed under it) is NOT a session failure in this sense.
130
+ */
131
+ hasFailed(): boolean;
132
+ /** First block this session proves. */
133
+ getStartBlockNumber(): BlockNumber;
134
+ /** Last block this session proves. */
135
+ getEndBlockNumber(): BlockNumber;
136
+ /**
137
+ * Kicks off proving + submission. Fires and forgets — callers should await `whenDone()`.
138
+ * Returns a promise that resolves to the final state for callers that want to wait inline.
139
+ */
140
+ start(): Promise<EpochProvingJobState>;
141
+ /**
142
+ * Cancels the session. Idempotent. Withdraws any submitted candidate from the
143
+ * publishing service so the in-flight publisher (if any) is interrupted.
144
+ */
145
+ cancel(reason?: string, { abortJobs }?: {
146
+ abortJobs?: boolean;
147
+ }): Promise<void>;
148
+ private run;
149
+ private submitProof;
150
+ private teardownTopTreeIfNeeded;
151
+ private scheduleDeadlineStop;
152
+ /**
153
+ * Returns a promise that resolves once cancellation has propagated and the state has
154
+ * been flipped from 'cancelled' to 'timed-out'. Protected so unit tests can drive the
155
+ * deadline path without waiting on the real `setTimeout` to fire.
156
+ */
157
+ protected handleDeadline(): Promise<void>;
158
+ private toTopTreeHooks;
159
+ }
160
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXBvY2gtc2Vzc2lvbi5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2pvYi9lcG9jaC1zZXNzaW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxXQUFXLEVBQXlCLEtBQUssV0FBVyxFQUFFLEtBQUssVUFBVSxFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFDeEgsT0FBTyxLQUFLLEVBQUUsVUFBVSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFDaEUsT0FBTyxFQUFlLEtBQUssY0FBYyxFQUFnQixNQUFNLHVCQUF1QixDQUFDO0FBRXZGLE9BQU8sRUFBRSxLQUFLLFlBQVksRUFBUyxNQUFNLHlCQUF5QixDQUFDO0FBQ25FLE9BQU8sS0FBSyxFQUFFLGtCQUFrQixFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFFL0QsT0FBTyxFQUFFLEtBQUssb0JBQW9CLEVBQWdDLE1BQU0saUNBQWlDLENBQUM7QUFDMUcsT0FBTyxFQUFjLEtBQUssU0FBUyxFQUFFLEtBQUssTUFBTSxFQUFhLE1BQU0seUJBQXlCLENBQUM7QUFJN0YsT0FBTyxLQUFLLEVBQUUsb0JBQW9CLEVBQUUsTUFBTSxlQUFlLENBQUM7QUFDMUQsT0FBTyxLQUFLLEVBQUUsc0JBQXNCLEVBQUUsTUFBTSxnQ0FBZ0MsQ0FBQztBQUM3RSxPQUFPLEVBQUUsZ0JBQWdCLEVBQUUsTUFBTSx3QkFBd0IsQ0FBQztBQUMxRCxPQUFPLEVBQW9DLEtBQUssWUFBWSxFQUFFLE1BQU0sbUJBQW1CLENBQUM7QUFFeEYsWUFBWSxFQUFFLG9CQUFvQixFQUFFLENBQU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