@aztec/prover-client 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.
Files changed (87) hide show
  1. package/dest/config.d.ts +1 -1
  2. package/dest/config.d.ts.map +1 -1
  3. package/dest/config.js +16 -2
  4. package/dest/light/lightweight_checkpoint_builder.d.ts +1 -1
  5. package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -1
  6. package/dest/light/lightweight_checkpoint_builder.js +15 -5
  7. package/dest/mocks/test_context.d.ts +11 -11
  8. package/dest/mocks/test_context.d.ts.map +1 -1
  9. package/dest/mocks/test_context.js +25 -28
  10. package/dest/orchestrator/block-building-helpers.d.ts +1 -1
  11. package/dest/orchestrator/checkpoint-proving-state.d.ts +20 -36
  12. package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
  13. package/dest/orchestrator/checkpoint-proving-state.js +22 -120
  14. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts +161 -0
  15. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts.map +1 -0
  16. package/dest/orchestrator/{orchestrator.js → checkpoint-sub-tree-orchestrator.js} +267 -451
  17. package/dest/orchestrator/chonk-cache.d.ts +39 -0
  18. package/dest/orchestrator/chonk-cache.d.ts.map +1 -0
  19. package/dest/orchestrator/chonk-cache.js +80 -0
  20. package/dest/orchestrator/index.d.ts +4 -2
  21. package/dest/orchestrator/index.d.ts.map +1 -1
  22. package/dest/orchestrator/index.js +3 -1
  23. package/dest/orchestrator/proving-scheduler.d.ts +81 -0
  24. package/dest/orchestrator/proving-scheduler.d.ts.map +1 -0
  25. package/dest/orchestrator/proving-scheduler.js +120 -0
  26. package/dest/orchestrator/top-tree-orchestrator.d.ts +88 -0
  27. package/dest/orchestrator/top-tree-orchestrator.d.ts.map +1 -0
  28. package/dest/orchestrator/top-tree-orchestrator.js +232 -0
  29. package/dest/orchestrator/top-tree-proving-state.d.ts +61 -0
  30. package/dest/orchestrator/top-tree-proving-state.d.ts.map +1 -0
  31. package/dest/orchestrator/top-tree-proving-state.js +185 -0
  32. package/dest/prover-client/prover-client.d.ts +61 -4
  33. package/dest/prover-client/prover-client.d.ts.map +1 -1
  34. package/dest/prover-client/prover-client.js +54 -7
  35. package/dest/proving_broker/broker_prover_facade.d.ts +1 -1
  36. package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
  37. package/dest/proving_broker/broker_prover_facade.js +10 -16
  38. package/dest/proving_broker/config.d.ts +8 -72
  39. package/dest/proving_broker/config.d.ts.map +1 -1
  40. package/dest/proving_broker/config.js +2 -2
  41. package/dest/proving_broker/index.d.ts +2 -1
  42. package/dest/proving_broker/index.d.ts.map +1 -1
  43. package/dest/proving_broker/index.js +1 -0
  44. package/dest/proving_broker/proving_broker.d.ts +2 -2
  45. package/dest/proving_broker/proving_broker.d.ts.map +1 -1
  46. package/dest/proving_broker/proving_broker.js +32 -7
  47. package/dest/proving_broker/proving_broker_database/persisted.js +2 -2
  48. package/dest/proving_broker/rpc.d.ts +3 -1
  49. package/dest/proving_broker/rpc.d.ts.map +1 -1
  50. package/dest/proving_broker/rpc.js +80 -24
  51. package/dest/test/epoch_settlement.d.ts +36 -0
  52. package/dest/test/epoch_settlement.d.ts.map +1 -0
  53. package/dest/test/epoch_settlement.js +52 -0
  54. package/dest/test/index.d.ts +2 -0
  55. package/dest/test/index.d.ts.map +1 -0
  56. package/dest/test/index.js +1 -0
  57. package/dest/test/mock_prover.d.ts +3 -3
  58. package/package.json +19 -18
  59. package/src/config.ts +18 -2
  60. package/src/light/lightweight_checkpoint_builder.ts +16 -6
  61. package/src/mocks/test_context.ts +24 -41
  62. package/src/orchestrator/checkpoint-proving-state.ts +21 -162
  63. package/src/orchestrator/{orchestrator.ts → checkpoint-sub-tree-orchestrator.ts} +415 -661
  64. package/src/orchestrator/chonk-cache.ts +99 -0
  65. package/src/orchestrator/index.ts +8 -1
  66. package/src/orchestrator/proving-scheduler.ts +160 -0
  67. package/src/orchestrator/top-tree-orchestrator.ts +384 -0
  68. package/src/orchestrator/top-tree-proving-state.ts +219 -0
  69. package/src/prover-client/prover-client.ts +128 -18
  70. package/src/proving_broker/broker_prover_facade.ts +8 -16
  71. package/src/proving_broker/config.ts +2 -1
  72. package/src/proving_broker/index.ts +1 -0
  73. package/src/proving_broker/proving_broker.ts +27 -5
  74. package/src/proving_broker/proving_broker_database/persisted.ts +2 -2
  75. package/src/proving_broker/rpc.ts +36 -24
  76. package/src/test/epoch_settlement.ts +82 -0
  77. package/src/test/index.ts +1 -0
  78. package/dest/orchestrator/epoch-proving-state.d.ts +0 -75
  79. package/dest/orchestrator/epoch-proving-state.d.ts.map +0 -1
  80. package/dest/orchestrator/epoch-proving-state.js +0 -269
  81. package/dest/orchestrator/orchestrator.d.ts +0 -130
  82. package/dest/orchestrator/orchestrator.d.ts.map +0 -1
  83. package/dest/prover-client/server-epoch-prover.d.ts +0 -32
  84. package/dest/prover-client/server-epoch-prover.d.ts.map +0 -1
  85. package/dest/prover-client/server-epoch-prover.js +0 -40
  86. package/src/orchestrator/epoch-proving-state.ts +0 -380
  87. package/src/prover-client/server-epoch-prover.ts +0 -69
@@ -0,0 +1,99 @@
1
+ import type { NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH } from '@aztec/constants';
2
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
3
+ import type { L2Block } from '@aztec/stdlib/block';
4
+ import type { PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
5
+ import type { PublicChonkVerifierPublicInputs } from '@aztec/stdlib/rollup';
6
+
7
+ /** Result of a chonk-verifier proof, cached by tx hash. */
8
+ export type ChonkVerifierProofResult = PublicInputsAndRecursiveProof<
9
+ PublicChonkVerifierPublicInputs,
10
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
11
+ >;
12
+
13
+ /**
14
+ * Shared cache of `getPublicChonkVerifierProof` results, keyed by tx hash.
15
+ *
16
+ * Owned by the prover-node and shared across every epoch / session: a tx remined into
17
+ * a different block (e.g. after a brief L1 reorg) reuses the already-computed chonk
18
+ * proof rather than redoing it. Entries are released via `releaseForBlocks` once the
19
+ * containing block is no longer interesting to the caller (e.g. its epoch's proof
20
+ * submission window has expired).
21
+ */
22
+ export class ChonkCache {
23
+ private readonly cache = new Map<string, Promise<ChonkVerifierProofResult>>();
24
+ private readonly pending = new Map<string, AbortController>();
25
+ private readonly log: Logger;
26
+ private stopped = false;
27
+
28
+ constructor(bindings?: LoggerBindings) {
29
+ this.log = createLogger('prover-client:chonk-cache', bindings);
30
+ }
31
+
32
+ /** Returns the cached promise for `txHash`, or `undefined` if none is registered. */
33
+ public get(txHash: string): Promise<ChonkVerifierProofResult> | undefined {
34
+ return this.cache.get(txHash);
35
+ }
36
+
37
+ /**
38
+ * Atomic get-or-compute: returns the cached promise for `txHash`, or runs `factory`
39
+ * (passing an AbortSignal the cache controls) and caches its result. Concurrent
40
+ * callers for the same `txHash` share the same promise.
41
+ *
42
+ * Rejected promises are evicted so a future caller can retry. The factory's
43
+ * AbortSignal fires when the cache is stopped.
44
+ */
45
+ public getOrCompute(
46
+ txHash: string,
47
+ factory: (signal: AbortSignal) => Promise<ChonkVerifierProofResult>,
48
+ ): Promise<ChonkVerifierProofResult> {
49
+ if (this.stopped) {
50
+ return Promise.reject(new Error('ChonkCache is stopped'));
51
+ }
52
+ const existing = this.cache.get(txHash);
53
+ if (existing) {
54
+ return existing;
55
+ }
56
+ const controller = new AbortController();
57
+ this.pending.set(txHash, controller);
58
+ this.log.debug(`Enqueueing chonk-verifier circuit`, { txHash });
59
+ const promise = factory(controller.signal).finally(() => this.pending.delete(txHash));
60
+ // Silently observe the rejection branch and evict so a retry is possible.
61
+ promise.catch(err => {
62
+ this.cache.delete(txHash);
63
+ this.log.debug(`Chonk-verifier proof failed; evicted from cache`, { txHash, error: `${err}` });
64
+ });
65
+ this.cache.set(txHash, promise);
66
+ return promise;
67
+ }
68
+
69
+ /** Drops cache entries for every tx in the supplied blocks. */
70
+ public releaseForBlocks(blocks: L2Block[]): void {
71
+ let released = 0;
72
+ for (const block of blocks) {
73
+ for (const txEffect of block.body.txEffects) {
74
+ if (this.cache.delete(txEffect.txHash.toString())) {
75
+ released++;
76
+ }
77
+ }
78
+ }
79
+ if (released > 0) {
80
+ this.log.debug(`Released ${released} chonk-verifier cache entries`, {
81
+ blockCount: blocks.length,
82
+ releasedCount: released,
83
+ });
84
+ }
85
+ }
86
+
87
+ /** Aborts every in-flight chonk-verifier job and clears the cache. */
88
+ public stop(): void {
89
+ if (this.stopped) {
90
+ return;
91
+ }
92
+ this.stopped = true;
93
+ for (const controller of this.pending.values()) {
94
+ controller.abort();
95
+ }
96
+ this.pending.clear();
97
+ this.cache.clear();
98
+ }
99
+ }
@@ -1 +1,8 @@
1
- export { ProvingOrchestrator } from './orchestrator.js';
1
+ export { CheckpointSubTreeOrchestrator, type SubTreeResult } from './checkpoint-sub-tree-orchestrator.js';
2
+ export { ChonkCache, type ChonkVerifierProofResult } from './chonk-cache.js';
3
+ export {
4
+ TopTreeOrchestrator,
5
+ TopTreeCancelledError,
6
+ type CheckpointTopTreeData,
7
+ type TopTreeResult,
8
+ } from './top-tree-orchestrator.js';
@@ -0,0 +1,160 @@
1
+ import { AbortError } from '@aztec/foundation/error';
2
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
3
+ import { SerialQueue } from '@aztec/foundation/queue';
4
+ import { sleep } from '@aztec/foundation/sleep';
5
+
6
+ /**
7
+ * Minimal surface a deferred-proving state must expose. Both `CheckpointProvingState` /
8
+ * `BlockProvingState` (used by `CheckpointSubTreeOrchestrator`) and `TopTreeProvingState`
9
+ * (used by `TopTreeOrchestrator`) satisfy it.
10
+ */
11
+ export interface ProvingStateLike {
12
+ /** Returns false once the state has been cancelled or otherwise invalidated. */
13
+ verifyState(): boolean;
14
+ /** Surfaces a proving error to the state's owner. */
15
+ reject(reason: string): void;
16
+ }
17
+
18
+ /**
19
+ * Common scheduling infrastructure shared by every orchestrator that drives broker
20
+ * proving jobs:
21
+ *
22
+ * - A shared `SerialQueue` (`deferredJobQueue`) that serialises the *act of handing a
23
+ * job to the broker*, one initiation per event-loop tick. Each queue task kicks off
24
+ * `safeJob` (which submits to the broker) without awaiting it, then yields with
25
+ * `sleep(0)`; the next task therefore runs on the following macrotask. This does NOT
26
+ * cap how many broker jobs are concurrently in flight — the broker's own queue absorbs
27
+ * that. What it bounds is the burst rate: a sub-tree that synchronously discovers
28
+ * thousands of ready jobs can't flood the broker (and monopolise the event loop) in a
29
+ * single tick. The queue is owned by the `ProverClient` and shared across every
30
+ * orchestrator (every sub-tree and top-tree across every concurrent epoch session), so
31
+ * this pacing is applied once globally rather than once-per-orchestrator.
32
+ * - A list of `AbortController`s (`pendingProvingJobs`) so a `cancel()` can abort
33
+ * in-flight broker jobs when needed.
34
+ * - A `deferredProving<T>(state, request, callback, isCancelled?)` method that wraps
35
+ * a broker request in the standard "submit, drop result if state invalidated, push
36
+ * errors to state.reject" envelope.
37
+ *
38
+ * Subclasses own their own concrete proving state and define `cancelInternal()` for
39
+ * the rest of the cleanup work (closing world-state forks, marking sub-trees
40
+ * cancelled, etc.). Because the queue is shared, neither `cancel()` nor `stop()` touch
41
+ * it — they only abort this orchestrator's in-flight broker jobs. Queued-but-unrun jobs
42
+ * for a cancelled orchestrator no-op via the guards in `deferredProving`.
43
+ */
44
+ export abstract class ProvingScheduler {
45
+ protected pendingProvingJobs: AbortController[] = [];
46
+ protected logger: Logger;
47
+
48
+ constructor(
49
+ private readonly deferredJobQueue: SerialQueue,
50
+ loggerName = 'prover-client:proving-scheduler',
51
+ bindings?: LoggerBindings,
52
+ ) {
53
+ this.logger = createLogger(loggerName, bindings);
54
+ }
55
+
56
+ /** Number of broker jobs currently in flight. */
57
+ public getNumPendingProvingJobs(): number {
58
+ return this.pendingProvingJobs.length;
59
+ }
60
+
61
+ /**
62
+ * Optionally aborts every in-flight broker job. Aborting is the right choice on
63
+ * reorg-driven cancel (where the in-flight inputs are no longer valid) and the
64
+ * wrong choice on shutdown (where leaving jobs in the broker queue lets a restart
65
+ * pick them up). The shared queue is not touched — queued-but-unrun jobs belonging
66
+ * to this orchestrator no-op once their controller is aborted (or once their state
67
+ * is marked invalid by the subclass).
68
+ */
69
+ protected resetSchedulerState(abortJobs: boolean): void {
70
+ if (abortJobs) {
71
+ for (const controller of this.pendingProvingJobs) {
72
+ controller.abort();
73
+ }
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Subclass-defined cancellation. Implementations call `resetSchedulerState(...)`
79
+ * and then do their own cleanup (close world-state forks, propagate cancel into
80
+ * the proving state, etc.).
81
+ */
82
+ protected abstract cancelInternal(): void;
83
+
84
+ /**
85
+ * Standard stop: cancel this orchestrator's work. The shared queue is owned by the
86
+ * `ProverClient` and outlives every orchestrator, so it is not drained here.
87
+ */
88
+ public stop(): Promise<void> {
89
+ this.cancelInternal();
90
+ return Promise.resolve();
91
+ }
92
+
93
+ /**
94
+ * Submits a broker request. The returned-via-callback result is dropped if the
95
+ * state has become invalid (re-org, cancellation) by the time it lands. Errors
96
+ * are routed to `state.reject` unless they are abort-driven or the state is
97
+ * already invalid (in which case they're a stale echo of the cancel).
98
+ *
99
+ * @param state - Object exposing `verifyState()` and `reject()`.
100
+ * @param request - The broker call. Receives the controller's signal.
101
+ * @param callback - Runs on success, after `verifyState()` is checked.
102
+ * @param isCancelled - Optional extra cancellation predicate (e.g. a `cancelled`
103
+ * flag the subclass maintains independently of the state). Defaults to never.
104
+ */
105
+ protected deferredProving<S extends ProvingStateLike, T>(
106
+ state: S,
107
+ request: (signal: AbortSignal) => Promise<T>,
108
+ callback: (result: T) => void | Promise<void>,
109
+ isCancelled: () => boolean = () => false,
110
+ ): void {
111
+ if (!state.verifyState()) {
112
+ this.logger.debug(`Not enqueuing job, state no longer valid`);
113
+ return;
114
+ }
115
+
116
+ const controller = new AbortController();
117
+ this.pendingProvingJobs.push(controller);
118
+
119
+ // We use a 'safeJob'. We don't want promise rejections in the proving pool — we
120
+ // want to capture the error here and reject the proving state while keeping the
121
+ // event loop free of rejections.
122
+ const safeJob = async () => {
123
+ try {
124
+ if (controller.signal.aborted) {
125
+ return;
126
+ }
127
+ const result = await request(controller.signal);
128
+ if (controller.signal.aborted || !state.verifyState() || isCancelled()) {
129
+ this.logger.debug(`State no longer valid, discarding result`);
130
+ return;
131
+ }
132
+ await callback(result);
133
+ } catch (err) {
134
+ if (err instanceof AbortError || isCancelled()) {
135
+ return;
136
+ }
137
+ if (!state.verifyState()) {
138
+ this.logger.debug(`State no longer valid, discarding error from proving job`, err);
139
+ return;
140
+ }
141
+ this.logger.error(`Error thrown when proving job`, err);
142
+ state.reject(`${err}`);
143
+ } finally {
144
+ const idx = this.pendingProvingJobs.indexOf(controller);
145
+ if (idx > -1) {
146
+ this.pendingProvingJobs.splice(idx, 1);
147
+ }
148
+ }
149
+ };
150
+
151
+ void this.deferredJobQueue.put(async () => {
152
+ // Kick off the broker submission without awaiting it — awaiting here would serialise all
153
+ // proving (one job at a time) and kill parallelism. The `sleep(0)` yields the event loop
154
+ // so the next queued job initiates on the following macrotask, pacing bursts rather than
155
+ // bounding in-flight broker concurrency (the broker's own queue handles that).
156
+ void safeJob();
157
+ await sleep(0);
158
+ });
159
+ }
160
+ }
@@ -0,0 +1,384 @@
1
+ import { BatchedBlobAccumulator, type FinalBlobBatchingChallenges } from '@aztec/blob-lib';
2
+ import type { BatchedBlob } from '@aztec/blob-lib/types';
3
+ import {
4
+ type ARCHIVE_HEIGHT,
5
+ BLOBS_PER_CHECKPOINT,
6
+ FIELDS_PER_BLOB,
7
+ type NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
8
+ OUT_HASH_TREE_HEIGHT,
9
+ } from '@aztec/constants';
10
+ import type { EpochNumber } from '@aztec/foundation/branded-types';
11
+ import { padArrayEnd } from '@aztec/foundation/collection';
12
+ import { BLS12Point } from '@aztec/foundation/curves/bls12';
13
+ import { Fr } from '@aztec/foundation/curves/bn254';
14
+ import type { LoggerBindings } from '@aztec/foundation/log';
15
+ import { promiseWithResolvers } from '@aztec/foundation/promise';
16
+ import type { SerialQueue } from '@aztec/foundation/queue';
17
+ import type { Tuple } from '@aztec/foundation/serialize';
18
+ import { MerkleTreeCalculator, type TreeNodeLocation, shaMerkleHash } from '@aztec/foundation/trees';
19
+ import type { EthAddress } from '@aztec/stdlib/block';
20
+ import type { PublicInputsAndRecursiveProof, ServerCircuitProver } from '@aztec/stdlib/interfaces/server';
21
+ import { computeCheckpointOutHash } from '@aztec/stdlib/messaging';
22
+ import type { Proof } from '@aztec/stdlib/proofs';
23
+ import {
24
+ type BlockRollupPublicInputs,
25
+ CheckpointRootRollupHints,
26
+ CheckpointRootRollupPrivateInputs,
27
+ CheckpointRootSingleBlockRollupPrivateInputs,
28
+ type RootRollupPublicInputs,
29
+ } from '@aztec/stdlib/rollup';
30
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
31
+ import type { BlockHeader } from '@aztec/stdlib/tx';
32
+ import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
33
+
34
+ import { buildBlobHints, toProofData } from './block-building-helpers.js';
35
+ import { ProvingScheduler } from './proving-scheduler.js';
36
+ import { TopTreeProvingState } from './top-tree-proving-state.js';
37
+
38
+ /** Per-checkpoint data fed into the top tree. */
39
+ export type CheckpointTopTreeData = {
40
+ /**
41
+ * Block-rollup proof outputs from the checkpoint's sub-tree. Passed as a Promise so the
42
+ * top tree can start (compute hints, pipeline merges) while sub-trees are still proving.
43
+ * The promise resolves to 1 entry for a single-block checkpoint, 2 for multi-block.
44
+ */
45
+ blockProofs: Promise<
46
+ PublicInputsAndRecursiveProof<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>[]
47
+ >;
48
+ /** L2-to-L1 messages per block in the checkpoint, used to compute the out hash. */
49
+ l2ToL1MsgsPerBlock: Fr[][][];
50
+ /** Blob fields encoding the checkpoint's tx effects, used to compute the blob accumulator. */
51
+ blobFields: Fr[];
52
+ /** Header of the last block in the previous checkpoint (or the epoch's predecessor for index 0). */
53
+ previousBlockHeader: BlockHeader;
54
+ /** Sibling path of the archive tree before any block in this checkpoint landed. */
55
+ previousArchiveSiblingPath: Tuple<Fr, typeof ARCHIVE_HEIGHT>;
56
+ };
57
+
58
+ /** Result of proving the top tree. */
59
+ export type TopTreeResult = {
60
+ publicInputs: RootRollupPublicInputs;
61
+ proof: Proof;
62
+ batchedBlobInputs: BatchedBlob;
63
+ };
64
+
65
+ /**
66
+ * Sentinel thrown by `cancel` so callers can distinguish reorg-driven cancellation from
67
+ * a genuine proving failure (and choose to rebuild + retry instead of failing the epoch).
68
+ */
69
+ export class TopTreeCancelledError extends Error {
70
+ constructor(reason = 'Top-tree proving cancelled') {
71
+ super(reason);
72
+ this.name = 'TopTreeCancelledError';
73
+ }
74
+ }
75
+
76
+ type OutHashHint = {
77
+ treeSnapshot: AppendOnlyTreeSnapshot;
78
+ siblingPath: Tuple<Fr, typeof OUT_HASH_TREE_HEIGHT>;
79
+ };
80
+
81
+ /**
82
+ * Drives proving from checkpoint root rollups through the epoch root rollup. Owns no
83
+ * world-state forks or tx processing — every input is supplied by the caller.
84
+ *
85
+ * Pipelined start: `prove()` does not wait for block-level proving. It pre-computes the
86
+ * out-hash and blob-accumulator hint chains immediately from archiver-derivable data,
87
+ * and each checkpoint's root rollup fires the moment its sub-tree's `blockProofs`
88
+ * promise resolves. Later checkpoints can still be block-level proving in parallel.
89
+ */
90
+ export class TopTreeOrchestrator extends ProvingScheduler {
91
+ private state: TopTreeProvingState | undefined;
92
+ private cancelled = false;
93
+
94
+ constructor(
95
+ protected readonly prover: ServerCircuitProver,
96
+ private readonly proverId: EthAddress,
97
+ deferredJobQueue: SerialQueue,
98
+ _telemetryClient: TelemetryClient = getTelemetryClient(),
99
+ bindings?: LoggerBindings,
100
+ ) {
101
+ super(deferredJobQueue, 'prover-client:top-tree-orchestrator', bindings);
102
+ }
103
+
104
+ public getProverId(): EthAddress {
105
+ return this.proverId;
106
+ }
107
+
108
+ /**
109
+ * Proves the top tree from per-checkpoint data and pending block-proofs promises.
110
+ * Resolves with the final epoch proof, or rejects with `TopTreeCancelledError` if
111
+ * `cancel()` is invoked, or any other error if a circuit fails.
112
+ */
113
+ public async prove(
114
+ epochNumber: EpochNumber,
115
+ totalNumCheckpoints: number,
116
+ finalBlobBatchingChallenges: FinalBlobBatchingChallenges,
117
+ checkpointData: CheckpointTopTreeData[],
118
+ ): Promise<TopTreeResult> {
119
+ if (checkpointData.length !== totalNumCheckpoints) {
120
+ throw new Error(
121
+ `checkpointData length (${checkpointData.length}) does not match totalNumCheckpoints (${totalNumCheckpoints}).`,
122
+ );
123
+ }
124
+ if (this.state) {
125
+ throw new Error('TopTreeOrchestrator.prove called twice; construct a new orchestrator per epoch.');
126
+ }
127
+ // If cancel() was already called before prove() ran (e.g. a removeCheckpoint that
128
+ // landed while the caller was still preparing inputs), short-circuit the whole
129
+ // proving path. Without this, prove() would build its state, the per-checkpoint
130
+ // .then handlers would all bail on `this.cancelled`, and the completion promise
131
+ // would never resolve — prove() would hang forever.
132
+ if (this.cancelled) {
133
+ throw new TopTreeCancelledError();
134
+ }
135
+
136
+ const { promise: completionPromise, resolve, reject } = promiseWithResolvers<void>();
137
+ // The completion promise is awaited below. Attach a no-op catch here as well so any
138
+ // spurious unhandled-rejection detection during cancellation (where reject() can fire
139
+ // synchronously before the await microtask installs a handler) is silenced.
140
+ completionPromise.catch(() => {});
141
+ const startBlobAccumulator = BatchedBlobAccumulator.newWithChallenges(finalBlobBatchingChallenges);
142
+
143
+ this.state = new TopTreeProvingState(
144
+ epochNumber,
145
+ totalNumCheckpoints,
146
+ finalBlobBatchingChallenges,
147
+ startBlobAccumulator,
148
+ resolve,
149
+ reason => reject(this.cancelled ? new TopTreeCancelledError(reason) : new Error(reason)),
150
+ );
151
+
152
+ // Compute the full out-hash hint chain and per-checkpoint start blob accumulators
153
+ // synchronously from archiver data. No proving required.
154
+ const outHashHints = await this.computeOutHashHints(checkpointData);
155
+ const checkpointStartBlobs: BatchedBlobAccumulator[] = [];
156
+ let runningBlobAccumulator = startBlobAccumulator;
157
+ for (const cd of checkpointData) {
158
+ checkpointStartBlobs.push(runningBlobAccumulator);
159
+ runningBlobAccumulator = await runningBlobAccumulator.accumulateFields(cd.blobFields);
160
+ }
161
+ this.state.setEndBlobAccumulator(runningBlobAccumulator);
162
+
163
+ // For each checkpoint, await its block proofs promise then enqueue the checkpoint root.
164
+ // Each await runs independently — checkpoints whose sub-trees finish first start their
165
+ // root proofs first, in parallel with later checkpoints' block-level proving.
166
+ for (let i = 0; i < checkpointData.length; i++) {
167
+ const cd = checkpointData[i];
168
+ const checkpointIndex = i;
169
+ void cd.blockProofs.then(
170
+ blockProofs => {
171
+ if (this.cancelled || !this.state?.verifyState()) {
172
+ return;
173
+ }
174
+ this.enqueueCheckpointRoot(
175
+ this.state,
176
+ checkpointIndex,
177
+ blockProofs,
178
+ cd,
179
+ outHashHints[i],
180
+ checkpointStartBlobs[i],
181
+ );
182
+ },
183
+ err => {
184
+ if (this.cancelled) {
185
+ return;
186
+ }
187
+ this.state?.reject(`Sub-tree for checkpoint ${i} failed: ${err}`);
188
+ },
189
+ );
190
+ }
191
+
192
+ // The error type is stamped atomically at rejection time by the rejectionCallback above
193
+ // (TopTreeCancelledError iff cancel() drove the rejection). Re-deriving it here from the
194
+ // live `cancelled` flag would mask a genuine failure that lost a race with a late cancel
195
+ // (A-1035), so let the original error propagate untouched.
196
+ await completionPromise;
197
+ await this.state.finalizeBatchedBlob();
198
+ return this.state.getEpochProofResult();
199
+ }
200
+
201
+ /**
202
+ * Cancels in-flight proving. If `abortJobs` is true, each pending broker job is aborted
203
+ * (used on reorg, when the surviving checkpoint set differs and the in-flight jobs'
204
+ * inputs are no longer valid). On shutdown the caller passes `false` so jobs remain in
205
+ * the broker queue for reuse on restart.
206
+ */
207
+ public cancel({ abortJobs }: { abortJobs: boolean }) {
208
+ this.cancelled = true;
209
+ this.resetSchedulerState(abortJobs);
210
+ this.state?.cancel();
211
+ }
212
+
213
+ /** Standard shutdown — preserve the broker queue (`abortJobs: false`). */
214
+ protected override cancelInternal(): void {
215
+ this.cancel({ abortJobs: false });
216
+ }
217
+
218
+ // --- internal: per-checkpoint enqueue path ---
219
+
220
+ private enqueueCheckpointRoot(
221
+ state: TopTreeProvingState,
222
+ checkpointIndex: number,
223
+ blockProofs: PublicInputsAndRecursiveProof<
224
+ BlockRollupPublicInputs,
225
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
226
+ >[],
227
+ cd: CheckpointTopTreeData,
228
+ outHashHint: OutHashHint,
229
+ startBlobAccumulator: BatchedBlobAccumulator,
230
+ ) {
231
+ void this.buildCheckpointRootInputs(blockProofs, cd, outHashHint, startBlobAccumulator).then(
232
+ inputs => {
233
+ this.deferredProving(
234
+ state,
235
+ signal => {
236
+ if (inputs instanceof CheckpointRootSingleBlockRollupPrivateInputs) {
237
+ return this.prover.getCheckpointRootSingleBlockRollupProof(inputs, signal, state.epochNumber);
238
+ }
239
+ return this.prover.getCheckpointRootRollupProof(inputs, signal, state.epochNumber);
240
+ },
241
+ result => {
242
+ this.logger.debug(`Completed checkpoint root proof for checkpoint ${checkpointIndex}`);
243
+ const leafLocation = state.setCheckpointRootRollupProof(checkpointIndex, result);
244
+ if (state.totalNumCheckpoints === 1) {
245
+ this.enqueueEpochPadding(state);
246
+ } else {
247
+ this.checkAndEnqueueNextCheckpointMergeRollup(state, leafLocation);
248
+ }
249
+ },
250
+ );
251
+ },
252
+ // Without this, an input-building failure rejects a discarded promise, state.reject() is
253
+ // never called, and prove() hangs forever on its completion promise (A-1036).
254
+ err => {
255
+ if (this.cancelled) {
256
+ return;
257
+ }
258
+ state.reject(`Building checkpoint root inputs for checkpoint ${checkpointIndex} failed: ${err}`);
259
+ },
260
+ );
261
+ }
262
+
263
+ private async buildCheckpointRootInputs(
264
+ blockProofs: PublicInputsAndRecursiveProof<
265
+ BlockRollupPublicInputs,
266
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
267
+ >[],
268
+ cd: CheckpointTopTreeData,
269
+ outHashHint: OutHashHint,
270
+ startBlobAccumulator: BatchedBlobAccumulator,
271
+ ) {
272
+ const { blobCommitments, blobsHash } = await buildBlobHints(cd.blobFields);
273
+
274
+ const hints = CheckpointRootRollupHints.from({
275
+ previousBlockHeader: cd.previousBlockHeader,
276
+ previousArchiveSiblingPath: cd.previousArchiveSiblingPath,
277
+ previousOutHash: outHashHint.treeSnapshot,
278
+ newOutHashSiblingPath: outHashHint.siblingPath,
279
+ startBlobAccumulator: startBlobAccumulator.toBlobAccumulator(),
280
+ finalBlobChallenges: this.state!.finalBlobBatchingChallenges,
281
+ blobFields: padArrayEnd(cd.blobFields, Fr.ZERO, FIELDS_PER_BLOB * BLOBS_PER_CHECKPOINT),
282
+ blobCommitments: padArrayEnd(blobCommitments, BLS12Point.ZERO, BLOBS_PER_CHECKPOINT),
283
+ blobsHash,
284
+ });
285
+
286
+ const proofDatas = blockProofs.map(p => toProofData(p));
287
+ return proofDatas.length === 1
288
+ ? new CheckpointRootSingleBlockRollupPrivateInputs(proofDatas[0], hints)
289
+ : new CheckpointRootRollupPrivateInputs([proofDatas[0], proofDatas[1]], hints);
290
+ }
291
+
292
+ // --- internal: top-tree proof orchestration (formerly TopTreeProvingScheduler) ---
293
+
294
+ private enqueueCheckpointMergeRollup(state: TopTreeProvingState, location: TreeNodeLocation) {
295
+ if (!state.verifyState() || !state.tryStartProvingCheckpointMerge(location)) {
296
+ return;
297
+ }
298
+ const inputs = state.getCheckpointMergeRollupInputs(location);
299
+ this.deferredProving(
300
+ state,
301
+ signal => this.prover.getCheckpointMergeRollupProof(inputs, signal, state.epochNumber),
302
+ result => {
303
+ state.setCheckpointMergeRollupProof(location, result);
304
+ this.checkAndEnqueueNextCheckpointMergeRollup(state, location);
305
+ },
306
+ );
307
+ }
308
+
309
+ private enqueueEpochPadding(state: TopTreeProvingState) {
310
+ if (!state.verifyState() || !state.tryStartProvingPaddingCheckpoint()) {
311
+ return;
312
+ }
313
+ const inputs = state.getPaddingCheckpointInputs();
314
+ this.deferredProving(
315
+ state,
316
+ signal => this.prover.getCheckpointPaddingRollupProof(inputs, signal, state.epochNumber),
317
+ result => {
318
+ state.setCheckpointPaddingProof(result);
319
+ this.checkAndEnqueueRootRollup(state);
320
+ },
321
+ );
322
+ }
323
+
324
+ private enqueueRootRollup(state: TopTreeProvingState) {
325
+ if (!state.verifyState() || !state.tryStartProvingRootRollup()) {
326
+ return;
327
+ }
328
+ const inputs = state.getRootRollupInputs();
329
+ this.deferredProving(
330
+ state,
331
+ signal => this.prover.getRootRollupProof(inputs, signal, state.epochNumber),
332
+ result => {
333
+ this.logger.verbose(`Completed root rollup for epoch ${state.epochNumber}`);
334
+ state.setRootRollupProof(result);
335
+ state.resolve();
336
+ },
337
+ );
338
+ }
339
+
340
+ private checkAndEnqueueNextCheckpointMergeRollup(state: TopTreeProvingState, currentLocation: TreeNodeLocation) {
341
+ if (!state.isReadyForCheckpointMerge(currentLocation)) {
342
+ return;
343
+ }
344
+ const parentLocation = state.getParentLocation(currentLocation);
345
+ if (parentLocation.level === 0) {
346
+ this.checkAndEnqueueRootRollup(state);
347
+ } else {
348
+ this.enqueueCheckpointMergeRollup(state, parentLocation);
349
+ }
350
+ }
351
+
352
+ private checkAndEnqueueRootRollup(state: TopTreeProvingState) {
353
+ if (!state.isReadyForRootRollup()) {
354
+ return;
355
+ }
356
+ this.enqueueRootRollup(state);
357
+ }
358
+
359
+ private async computeOutHashHints(checkpointData: CheckpointTopTreeData[]): Promise<OutHashHint[]> {
360
+ const treeCalculator = await MerkleTreeCalculator.create(OUT_HASH_TREE_HEIGHT, undefined, (left, right) =>
361
+ Promise.resolve(shaMerkleHash(left, right)),
362
+ );
363
+
364
+ const computeHint = async (leaves: Fr[]): Promise<OutHashHint> => {
365
+ const tree = await treeCalculator.computeTree(leaves.map(l => l.toBuffer()));
366
+ const nextAvailableLeafIndex = leaves.length;
367
+ return {
368
+ treeSnapshot: new AppendOnlyTreeSnapshot(Fr.fromBuffer(tree.root), nextAvailableLeafIndex),
369
+ siblingPath: tree.getSiblingPath(nextAvailableLeafIndex).map(Fr.fromBuffer) as Tuple<
370
+ Fr,
371
+ typeof OUT_HASH_TREE_HEIGHT
372
+ >,
373
+ };
374
+ };
375
+
376
+ const hints: OutHashHint[] = [];
377
+ const outHashes: Fr[] = [];
378
+ for (const cd of checkpointData) {
379
+ hints.push(await computeHint(outHashes));
380
+ outHashes.push(computeCheckpointOutHash(cd.l2ToL1MsgsPerBlock));
381
+ }
382
+ return hints;
383
+ }
384
+ }