@aztec/prover-client 0.0.1-commit.e558bd1c → 0.0.1-commit.e57c76e

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 (92) 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 +10 -5
  5. package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -1
  6. package/dest/light/lightweight_checkpoint_builder.js +53 -22
  7. package/dest/mocks/test_context.d.ts +3 -1
  8. package/dest/mocks/test_context.d.ts.map +1 -1
  9. package/dest/mocks/test_context.js +18 -9
  10. package/dest/orchestrator/block-building-helpers.d.ts +4 -4
  11. package/dest/orchestrator/block-building-helpers.d.ts.map +1 -1
  12. package/dest/orchestrator/block-building-helpers.js +2 -2
  13. package/dest/orchestrator/block-proving-state.d.ts +4 -1
  14. package/dest/orchestrator/block-proving-state.d.ts.map +1 -1
  15. package/dest/orchestrator/block-proving-state.js +7 -0
  16. package/dest/orchestrator/checkpoint-proving-state.d.ts +10 -3
  17. package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
  18. package/dest/orchestrator/checkpoint-proving-state.js +13 -4
  19. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts +107 -0
  20. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts.map +1 -0
  21. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.js +151 -0
  22. package/dest/orchestrator/epoch-proving-context.d.ts +51 -0
  23. package/dest/orchestrator/epoch-proving-context.d.ts.map +1 -0
  24. package/dest/orchestrator/epoch-proving-context.js +81 -0
  25. package/dest/orchestrator/epoch-proving-state.d.ts +3 -3
  26. package/dest/orchestrator/epoch-proving-state.d.ts.map +1 -1
  27. package/dest/orchestrator/epoch-proving-state.js +5 -3
  28. package/dest/orchestrator/index.d.ts +4 -1
  29. package/dest/orchestrator/index.d.ts.map +1 -1
  30. package/dest/orchestrator/index.js +3 -0
  31. package/dest/orchestrator/orchestrator.d.ts +16 -26
  32. package/dest/orchestrator/orchestrator.d.ts.map +1 -1
  33. package/dest/orchestrator/orchestrator.js +76 -206
  34. package/dest/orchestrator/proving-scheduler.d.ts +72 -0
  35. package/dest/orchestrator/proving-scheduler.d.ts.map +1 -0
  36. package/dest/orchestrator/proving-scheduler.js +117 -0
  37. package/dest/orchestrator/top-tree-orchestrator.d.ts +83 -0
  38. package/dest/orchestrator/top-tree-orchestrator.d.ts.map +1 -0
  39. package/dest/orchestrator/top-tree-orchestrator.js +182 -0
  40. package/dest/orchestrator/top-tree-proving-scheduler.d.ts +62 -0
  41. package/dest/orchestrator/top-tree-proving-scheduler.d.ts.map +1 -0
  42. package/dest/orchestrator/top-tree-proving-scheduler.js +73 -0
  43. package/dest/orchestrator/top-tree-proving-state.d.ts +61 -0
  44. package/dest/orchestrator/top-tree-proving-state.d.ts.map +1 -0
  45. package/dest/orchestrator/top-tree-proving-state.js +185 -0
  46. package/dest/prover-client/prover-client.d.ts +62 -3
  47. package/dest/prover-client/prover-client.d.ts.map +1 -1
  48. package/dest/prover-client/prover-client.js +50 -2
  49. package/dest/proving_broker/broker_prover_facade.d.ts +1 -1
  50. package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
  51. package/dest/proving_broker/broker_prover_facade.js +13 -19
  52. package/dest/proving_broker/config.d.ts +9 -73
  53. package/dest/proving_broker/config.d.ts.map +1 -1
  54. package/dest/proving_broker/config.js +3 -3
  55. package/dest/proving_broker/index.d.ts +2 -1
  56. package/dest/proving_broker/index.d.ts.map +1 -1
  57. package/dest/proving_broker/index.js +1 -0
  58. package/dest/proving_broker/proving_broker.d.ts +2 -2
  59. package/dest/proving_broker/proving_broker.d.ts.map +1 -1
  60. package/dest/proving_broker/proving_broker.js +39 -10
  61. package/dest/proving_broker/proving_broker_database/persisted.js +2 -2
  62. package/dest/proving_broker/proving_broker_instrumentation.d.ts +3 -1
  63. package/dest/proving_broker/proving_broker_instrumentation.d.ts.map +1 -1
  64. package/dest/proving_broker/proving_broker_instrumentation.js +7 -0
  65. package/dest/proving_broker/rpc.d.ts +3 -1
  66. package/dest/proving_broker/rpc.d.ts.map +1 -1
  67. package/dest/proving_broker/rpc.js +80 -24
  68. package/dest/test/mock_prover.d.ts +4 -4
  69. package/package.json +18 -19
  70. package/src/config.ts +18 -2
  71. package/src/light/lightweight_checkpoint_builder.ts +56 -25
  72. package/src/mocks/test_context.ts +13 -10
  73. package/src/orchestrator/block-building-helpers.ts +2 -2
  74. package/src/orchestrator/block-proving-state.ts +9 -0
  75. package/src/orchestrator/checkpoint-proving-state.ts +18 -5
  76. package/src/orchestrator/checkpoint-sub-tree-orchestrator.ts +271 -0
  77. package/src/orchestrator/epoch-proving-context.ts +101 -0
  78. package/src/orchestrator/epoch-proving-state.ts +6 -4
  79. package/src/orchestrator/index.ts +8 -0
  80. package/src/orchestrator/orchestrator.ts +98 -268
  81. package/src/orchestrator/proving-scheduler.ts +156 -0
  82. package/src/orchestrator/top-tree-orchestrator.ts +314 -0
  83. package/src/orchestrator/top-tree-proving-scheduler.ts +154 -0
  84. package/src/orchestrator/top-tree-proving-state.ts +220 -0
  85. package/src/prover-client/prover-client.ts +127 -2
  86. package/src/proving_broker/broker_prover_facade.ts +17 -20
  87. package/src/proving_broker/config.ts +3 -2
  88. package/src/proving_broker/index.ts +1 -0
  89. package/src/proving_broker/proving_broker.ts +34 -7
  90. package/src/proving_broker/proving_broker_database/persisted.ts +2 -2
  91. package/src/proving_broker/proving_broker_instrumentation.ts +9 -0
  92. package/src/proving_broker/rpc.ts +36 -24
@@ -0,0 +1,156 @@
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 `EpochProvingState` /
8
+ * `CheckpointProvingState` / `BlockProvingState` (used by `ProvingOrchestrator`) and
9
+ * `TopTreeProvingState` (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
+ * - One `SerialQueue` (`deferredJobQueue`) acting as the enqueue-throttle.
23
+ * - A list of `AbortController`s (`pendingProvingJobs`) so a `cancel()` can abort
24
+ * in-flight broker jobs when needed.
25
+ * - A `deferredProving<T>(state, request, callback, isCancelled?)` method that wraps
26
+ * a broker request in the standard "submit, drop result if state invalidated, push
27
+ * errors to state.reject" envelope.
28
+ *
29
+ * Subclasses own their own concrete proving state and define `cancelInternal()` for
30
+ * the rest of the cleanup work (closing world-state forks, marking sub-trees
31
+ * cancelled, etc.). `stop()` lives on the base class and follows the standard pattern
32
+ * of grabbing the old queue, calling `cancelInternal()` (which recreates the queue),
33
+ * and awaiting the old queue's drain.
34
+ */
35
+ export abstract class ProvingScheduler {
36
+ protected pendingProvingJobs: AbortController[] = [];
37
+ protected logger: Logger;
38
+ private deferredJobQueue: SerialQueue;
39
+
40
+ constructor(
41
+ private readonly enqueueConcurrency: number,
42
+ loggerName = 'prover-client:proving-scheduler',
43
+ bindings?: LoggerBindings,
44
+ ) {
45
+ this.logger = createLogger(loggerName, bindings);
46
+ this.deferredJobQueue = new SerialQueue();
47
+ this.deferredJobQueue.start(this.enqueueConcurrency);
48
+ }
49
+
50
+ /** Number of broker jobs currently in flight. */
51
+ public getNumPendingProvingJobs(): number {
52
+ return this.pendingProvingJobs.length;
53
+ }
54
+
55
+ /**
56
+ * Drains the deferred-job queue, recreates it (so the subclass can be reused), and
57
+ * optionally aborts every in-flight broker job. Aborting is the right choice on
58
+ * reorg-driven cancel (where the in-flight inputs are no longer valid) and the
59
+ * wrong choice on shutdown (where leaving jobs in the broker queue lets a restart
60
+ * pick them up).
61
+ */
62
+ protected resetSchedulerState(abortJobs: boolean): void {
63
+ void this.deferredJobQueue.cancel();
64
+ this.deferredJobQueue = new SerialQueue();
65
+ this.deferredJobQueue.start(this.enqueueConcurrency);
66
+ if (abortJobs) {
67
+ for (const controller of this.pendingProvingJobs) {
68
+ controller.abort();
69
+ }
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Subclass-defined cancellation. Implementations call `resetSchedulerState(...)`
75
+ * and then do their own cleanup (close world-state forks, propagate cancel into
76
+ * the proving state, etc.).
77
+ */
78
+ protected abstract cancelInternal(): void;
79
+
80
+ /**
81
+ * Standard stop: grab the old queue, cancel (which recreates the queue), then
82
+ * await the old queue's drain so any final job tear-down has unwound before we
83
+ * return.
84
+ */
85
+ public async stop(): Promise<void> {
86
+ const oldQueue = this.deferredJobQueue;
87
+ this.cancelInternal();
88
+ await oldQueue.cancel();
89
+ }
90
+
91
+ /**
92
+ * Submits a broker request. The returned-via-callback result is dropped if the
93
+ * state has become invalid (re-org, cancellation) by the time it lands. Errors
94
+ * are routed to `state.reject` unless they are abort-driven or the state is
95
+ * already invalid (in which case they're a stale echo of the cancel).
96
+ *
97
+ * @param state - Object exposing `verifyState()` and `reject()`.
98
+ * @param request - The broker call. Receives the controller's signal.
99
+ * @param callback - Runs on success, after `verifyState()` is checked.
100
+ * @param isCancelled - Optional extra cancellation predicate (e.g. a `cancelled`
101
+ * flag the subclass maintains independently of the state). Defaults to never.
102
+ */
103
+ protected deferredProving<S extends ProvingStateLike, T>(
104
+ state: S,
105
+ request: (signal: AbortSignal) => Promise<T>,
106
+ callback: (result: T) => void | Promise<void>,
107
+ isCancelled: () => boolean = () => false,
108
+ ): void {
109
+ if (!state.verifyState()) {
110
+ this.logger.debug(`Not enqueuing job, state no longer valid`);
111
+ return;
112
+ }
113
+
114
+ const controller = new AbortController();
115
+ this.pendingProvingJobs.push(controller);
116
+
117
+ // We use a 'safeJob'. We don't want promise rejections in the proving pool — we
118
+ // want to capture the error here and reject the proving state while keeping the
119
+ // event loop free of rejections.
120
+ const safeJob = async () => {
121
+ try {
122
+ if (controller.signal.aborted) {
123
+ return;
124
+ }
125
+ const result = await request(controller.signal);
126
+ if (controller.signal.aborted || !state.verifyState() || isCancelled()) {
127
+ this.logger.debug(`State no longer valid, discarding result`);
128
+ return;
129
+ }
130
+ await callback(result);
131
+ } catch (err) {
132
+ if (err instanceof AbortError || isCancelled()) {
133
+ return;
134
+ }
135
+ if (!state.verifyState()) {
136
+ this.logger.debug(`State no longer valid, discarding error from proving job`, err);
137
+ return;
138
+ }
139
+ this.logger.error(`Error thrown when proving job`, err);
140
+ state.reject(`${err}`);
141
+ } finally {
142
+ const idx = this.pendingProvingJobs.indexOf(controller);
143
+ if (idx > -1) {
144
+ this.pendingProvingJobs.splice(idx, 1);
145
+ }
146
+ }
147
+ };
148
+
149
+ void this.deferredJobQueue.put(async () => {
150
+ void safeJob();
151
+ // Yield to the macrotask queue so Node has a chance to interleave other work
152
+ // between enqueues.
153
+ await sleep(0);
154
+ });
155
+ }
156
+ }
@@ -0,0 +1,314 @@
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 { Tuple } from '@aztec/foundation/serialize';
17
+ import { MerkleTreeCalculator, shaMerkleHash } from '@aztec/foundation/trees';
18
+ import type { EthAddress } from '@aztec/stdlib/block';
19
+ import type { PublicInputsAndRecursiveProof, ServerCircuitProver } from '@aztec/stdlib/interfaces/server';
20
+ import { computeCheckpointOutHash } from '@aztec/stdlib/messaging';
21
+ import type { Proof } from '@aztec/stdlib/proofs';
22
+ import {
23
+ type BlockRollupPublicInputs,
24
+ CheckpointRootRollupHints,
25
+ CheckpointRootRollupPrivateInputs,
26
+ CheckpointRootSingleBlockRollupPrivateInputs,
27
+ type RootRollupPublicInputs,
28
+ } from '@aztec/stdlib/rollup';
29
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
30
+ import type { BlockHeader } from '@aztec/stdlib/tx';
31
+ import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
32
+
33
+ import { buildBlobHints, toProofData } from './block-building-helpers.js';
34
+ import { TopTreeProvingScheduler } from './top-tree-proving-scheduler.js';
35
+ import { TopTreeProvingState } from './top-tree-proving-state.js';
36
+
37
+ /** Per-checkpoint data fed into the top tree. */
38
+ export type CheckpointTopTreeData = {
39
+ /**
40
+ * Block-rollup proof outputs from the checkpoint's sub-tree. Passed as a Promise so the
41
+ * top tree can start (compute hints, pipeline merges) while sub-trees are still proving.
42
+ * The promise resolves to 1 entry for a single-block checkpoint, 2 for multi-block.
43
+ */
44
+ blockProofs: Promise<
45
+ PublicInputsAndRecursiveProof<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>[]
46
+ >;
47
+ /** L2-to-L1 messages per block in the checkpoint, used to compute the out hash. */
48
+ l2ToL1MsgsPerBlock: Fr[][][];
49
+ /** Blob fields encoding the checkpoint's tx effects, used to compute the blob accumulator. */
50
+ blobFields: Fr[];
51
+ /** Header of the last block in the previous checkpoint (or the epoch's predecessor for index 0). */
52
+ previousBlockHeader: BlockHeader;
53
+ /** Sibling path of the archive tree before any block in this checkpoint landed. */
54
+ previousArchiveSiblingPath: Tuple<Fr, typeof ARCHIVE_HEIGHT>;
55
+ };
56
+
57
+ /** Result of proving the top tree. */
58
+ export type TopTreeResult = {
59
+ publicInputs: RootRollupPublicInputs;
60
+ proof: Proof;
61
+ batchedBlobInputs: BatchedBlob;
62
+ };
63
+
64
+ /**
65
+ * Sentinel thrown by `cancel` so callers can distinguish reorg-driven cancellation from
66
+ * a genuine proving failure (and choose to rebuild + retry instead of failing the epoch).
67
+ */
68
+ export class TopTreeCancelledError extends Error {
69
+ constructor(reason = 'Top-tree proving cancelled') {
70
+ super(reason);
71
+ this.name = 'TopTreeCancelledError';
72
+ }
73
+ }
74
+
75
+ type OutHashHint = {
76
+ treeSnapshot: AppendOnlyTreeSnapshot;
77
+ siblingPath: Tuple<Fr, typeof OUT_HASH_TREE_HEIGHT>;
78
+ };
79
+
80
+ /**
81
+ * Drives proving from checkpoint root rollups through the epoch root rollup. Owns no
82
+ * world-state forks or tx processing — every input is supplied by the caller.
83
+ *
84
+ * Pipelined start: `prove()` does not wait for block-level proving. It pre-computes the
85
+ * out-hash and blob-accumulator hint chains immediately from archiver-derivable data,
86
+ * and each checkpoint's root rollup fires the moment its sub-tree's `blockProofs`
87
+ * promise resolves. Later checkpoints can still be block-level proving in parallel.
88
+ */
89
+ export class TopTreeOrchestrator extends TopTreeProvingScheduler {
90
+ private state: TopTreeProvingState | undefined;
91
+ private cancelled = false;
92
+
93
+ constructor(
94
+ prover: ServerCircuitProver,
95
+ private readonly proverId: EthAddress,
96
+ enqueueConcurrency: number,
97
+ _telemetryClient: TelemetryClient = getTelemetryClient(),
98
+ bindings?: LoggerBindings,
99
+ ) {
100
+ super(prover, enqueueConcurrency, 'prover-client:top-tree-orchestrator', bindings);
101
+ }
102
+
103
+ public getProverId(): EthAddress {
104
+ return this.proverId;
105
+ }
106
+
107
+ /**
108
+ * Proves the top tree from per-checkpoint data and pending block-proofs promises.
109
+ * Resolves with the final epoch proof, or rejects with `TopTreeCancelledError` if
110
+ * `cancel()` is invoked, or any other error if a circuit fails.
111
+ */
112
+ public async prove(
113
+ epochNumber: EpochNumber,
114
+ totalNumCheckpoints: number,
115
+ finalBlobBatchingChallenges: FinalBlobBatchingChallenges,
116
+ checkpointData: CheckpointTopTreeData[],
117
+ ): Promise<TopTreeResult> {
118
+ if (checkpointData.length !== totalNumCheckpoints) {
119
+ throw new Error(
120
+ `checkpointData length (${checkpointData.length}) does not match totalNumCheckpoints (${totalNumCheckpoints}).`,
121
+ );
122
+ }
123
+ if (this.state) {
124
+ throw new Error('TopTreeOrchestrator.prove called twice; construct a new orchestrator per epoch.');
125
+ }
126
+ // If cancel() was already called before prove() ran (e.g. a removeCheckpoint that
127
+ // landed while the caller was still preparing inputs), short-circuit the whole
128
+ // proving path. Without this, prove() would build its state, the per-checkpoint
129
+ // .then handlers would all bail on `this.cancelled`, and the completion promise
130
+ // would never resolve — prove() would hang forever.
131
+ if (this.cancelled) {
132
+ throw new TopTreeCancelledError();
133
+ }
134
+
135
+ const { promise: completionPromise, resolve, reject } = promiseWithResolvers<void>();
136
+ // The completion promise is awaited inside the try/catch below. Attach a no-op catch
137
+ // here as well so any spurious unhandled-rejection detection during cancellation
138
+ // (where reject() can fire synchronously before the await microtask installs a handler)
139
+ // 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
+ try {
193
+ await completionPromise;
194
+ await this.state.finalizeBatchedBlob();
195
+ return this.state.getEpochProofResult();
196
+ } catch (err: any) {
197
+ if (this.cancelled) {
198
+ throw new TopTreeCancelledError();
199
+ }
200
+ throw err;
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Cancels in-flight proving. If `abortJobs` is true, each pending broker job is aborted
206
+ * (used on reorg, when the surviving checkpoint set differs and the in-flight jobs'
207
+ * inputs are no longer valid). On shutdown the caller passes `false` so jobs remain in
208
+ * the broker queue for reuse on restart.
209
+ */
210
+ public cancel({ abortJobs }: { abortJobs: boolean }) {
211
+ this.cancelled = true;
212
+ this.resetSchedulerState(abortJobs);
213
+ this.state?.cancel();
214
+ }
215
+
216
+ /** Standard shutdown — preserve the broker queue (`abortJobs: false`). */
217
+ protected override cancelInternal(): void {
218
+ this.cancel({ abortJobs: false });
219
+ }
220
+
221
+ // --- internal: per-checkpoint enqueue path ---
222
+
223
+ protected override onRootRollupComplete(state: TopTreeProvingState) {
224
+ state.resolve();
225
+ }
226
+
227
+ private enqueueCheckpointRoot(
228
+ state: TopTreeProvingState,
229
+ checkpointIndex: number,
230
+ blockProofs: PublicInputsAndRecursiveProof<
231
+ BlockRollupPublicInputs,
232
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
233
+ >[],
234
+ cd: CheckpointTopTreeData,
235
+ outHashHint: OutHashHint,
236
+ startBlobAccumulator: BatchedBlobAccumulator,
237
+ ) {
238
+ void this.buildCheckpointRootInputs(blockProofs, cd, outHashHint, startBlobAccumulator).then(inputs => {
239
+ this.deferredProving(
240
+ state,
241
+ signal => {
242
+ if (inputs instanceof CheckpointRootSingleBlockRollupPrivateInputs) {
243
+ return this.prover.getCheckpointRootSingleBlockRollupProof(inputs, signal, state.epochNumber);
244
+ }
245
+ return this.prover.getCheckpointRootRollupProof(inputs, signal, state.epochNumber);
246
+ },
247
+ result => {
248
+ this.logger.debug(`Completed checkpoint root proof for checkpoint ${checkpointIndex}`);
249
+ const leafLocation = state.setCheckpointRootRollupProof(checkpointIndex, result);
250
+ if (state.totalNumCheckpoints === 1) {
251
+ this.enqueueEpochPadding(state);
252
+ } else {
253
+ this.checkAndEnqueueNextCheckpointMergeRollup(state, leafLocation);
254
+ }
255
+ },
256
+ );
257
+ });
258
+ }
259
+
260
+ private async buildCheckpointRootInputs(
261
+ blockProofs: PublicInputsAndRecursiveProof<
262
+ BlockRollupPublicInputs,
263
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
264
+ >[],
265
+ cd: CheckpointTopTreeData,
266
+ outHashHint: OutHashHint,
267
+ startBlobAccumulator: BatchedBlobAccumulator,
268
+ ) {
269
+ const { blobCommitments, blobsHash } = await buildBlobHints(cd.blobFields);
270
+
271
+ const hints = CheckpointRootRollupHints.from({
272
+ previousBlockHeader: cd.previousBlockHeader,
273
+ previousArchiveSiblingPath: cd.previousArchiveSiblingPath,
274
+ previousOutHash: outHashHint.treeSnapshot,
275
+ newOutHashSiblingPath: outHashHint.siblingPath,
276
+ startBlobAccumulator: startBlobAccumulator.toBlobAccumulator(),
277
+ finalBlobChallenges: this.state!.finalBlobBatchingChallenges,
278
+ blobFields: padArrayEnd(cd.blobFields, Fr.ZERO, FIELDS_PER_BLOB * BLOBS_PER_CHECKPOINT),
279
+ blobCommitments: padArrayEnd(blobCommitments, BLS12Point.ZERO, BLOBS_PER_CHECKPOINT),
280
+ blobsHash,
281
+ });
282
+
283
+ const proofDatas = blockProofs.map(p => toProofData(p));
284
+ return proofDatas.length === 1
285
+ ? new CheckpointRootSingleBlockRollupPrivateInputs(proofDatas[0], hints)
286
+ : new CheckpointRootRollupPrivateInputs([proofDatas[0], proofDatas[1]], hints);
287
+ }
288
+
289
+ private async computeOutHashHints(checkpointData: CheckpointTopTreeData[]): Promise<OutHashHint[]> {
290
+ const treeCalculator = await MerkleTreeCalculator.create(OUT_HASH_TREE_HEIGHT, undefined, (left, right) =>
291
+ Promise.resolve(shaMerkleHash(left, right)),
292
+ );
293
+
294
+ const computeHint = async (leaves: Fr[]): Promise<OutHashHint> => {
295
+ const tree = await treeCalculator.computeTree(leaves.map(l => l.toBuffer()));
296
+ const nextAvailableLeafIndex = leaves.length;
297
+ return {
298
+ treeSnapshot: new AppendOnlyTreeSnapshot(Fr.fromBuffer(tree.root), nextAvailableLeafIndex),
299
+ siblingPath: tree.getSiblingPath(nextAvailableLeafIndex).map(Fr.fromBuffer) as Tuple<
300
+ Fr,
301
+ typeof OUT_HASH_TREE_HEIGHT
302
+ >,
303
+ };
304
+ };
305
+
306
+ const hints: OutHashHint[] = [];
307
+ const outHashes: Fr[] = [];
308
+ for (const cd of checkpointData) {
309
+ hints.push(await computeHint(outHashes));
310
+ outHashes.push(computeCheckpointOutHash(cd.l2ToL1MsgsPerBlock));
311
+ }
312
+ return hints;
313
+ }
314
+ }
@@ -0,0 +1,154 @@
1
+ import type { NESTED_RECURSIVE_PROOF_LENGTH, NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH } from '@aztec/constants';
2
+ import type { EpochNumber } from '@aztec/foundation/branded-types';
3
+ import type { LoggerBindings } from '@aztec/foundation/log';
4
+ import type { TreeNodeLocation } from '@aztec/foundation/trees';
5
+ import type { PublicInputsAndRecursiveProof, ServerCircuitProver } from '@aztec/stdlib/interfaces/server';
6
+ import type {
7
+ CheckpointMergeRollupPrivateInputs,
8
+ CheckpointPaddingRollupPrivateInputs,
9
+ CheckpointRollupPublicInputs,
10
+ RootRollupPrivateInputs,
11
+ RootRollupPublicInputs,
12
+ } from '@aztec/stdlib/rollup';
13
+
14
+ import { ProvingScheduler, type ProvingStateLike } from './proving-scheduler.js';
15
+
16
+ type CheckpointRollupProof = PublicInputsAndRecursiveProof<
17
+ CheckpointRollupPublicInputs,
18
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
19
+ >;
20
+
21
+ type RootRollupProof = PublicInputsAndRecursiveProof<RootRollupPublicInputs, typeof NESTED_RECURSIVE_PROOF_LENGTH>;
22
+
23
+ /**
24
+ * State interface required by the top-tree proving drivers (checkpoint-merge → padding →
25
+ * root-rollup). Both `EpochProvingState` and `TopTreeProvingState` satisfy it structurally;
26
+ * the per-checkpoint state in `EpochProvingState` (block/tx proving, world-state forks)
27
+ * is owned outside this surface.
28
+ */
29
+ export interface TopTreeStateLike extends ProvingStateLike {
30
+ readonly epochNumber: EpochNumber;
31
+ readonly totalNumCheckpoints: number;
32
+
33
+ tryStartProvingCheckpointMerge(location: TreeNodeLocation): boolean;
34
+ setCheckpointMergeRollupProof(location: TreeNodeLocation, provingOutput: CheckpointRollupProof): void;
35
+ isReadyForCheckpointMerge(location: TreeNodeLocation): boolean;
36
+ getParentLocation(location: TreeNodeLocation): TreeNodeLocation;
37
+ getCheckpointMergeRollupInputs(location: TreeNodeLocation): CheckpointMergeRollupPrivateInputs;
38
+
39
+ tryStartProvingPaddingCheckpoint(): boolean;
40
+ setCheckpointPaddingProof(provingOutput: CheckpointRollupProof): void;
41
+ getPaddingCheckpointInputs(): CheckpointPaddingRollupPrivateInputs;
42
+
43
+ tryStartProvingRootRollup(): boolean;
44
+ setRootRollupProof(provingOutput: RootRollupProof): void;
45
+ isReadyForRootRollup(): boolean;
46
+ getRootRollupInputs(): RootRollupPrivateInputs;
47
+ }
48
+
49
+ /**
50
+ * Shared scheduling for the top-tree section of epoch proving — checkpoint-merge,
51
+ * padding (single-checkpoint case), and root rollup. Both `ProvingOrchestrator` and
52
+ * `TopTreeOrchestrator` extend this; their per-checkpoint-root drivers diverge (one
53
+ * drains state-derived inputs once block-merge is done, the other builds inputs from
54
+ * caller-supplied checkpoint data), but the rest of the tree is identical.
55
+ *
56
+ * Subclasses provide a `wrapCircuitCall` hook for telemetry (the orchestrator wraps
57
+ * each call in a span; the top-tree leaves it as identity), and an
58
+ * `onRootRollupComplete` hook to invoke the right shape of `state.resolve()` —
59
+ * `EpochProvingState.resolve` takes a `ProvingResult`, `TopTreeProvingState.resolve`
60
+ * is no-arg.
61
+ */
62
+ export abstract class TopTreeProvingScheduler extends ProvingScheduler {
63
+ constructor(
64
+ protected readonly prover: ServerCircuitProver,
65
+ enqueueConcurrency: number,
66
+ loggerName?: string,
67
+ bindings?: LoggerBindings,
68
+ ) {
69
+ super(enqueueConcurrency, loggerName, bindings);
70
+ }
71
+
72
+ /**
73
+ * Wraps a circuit call for telemetry. Default is identity; the orchestrator overrides
74
+ * to wrap with `wrapCallbackInSpan`.
75
+ */
76
+ protected wrapCircuitCall<T>(
77
+ _circuitName: string,
78
+ fn: (signal: AbortSignal) => Promise<T>,
79
+ ): (signal: AbortSignal) => Promise<T> {
80
+ return fn;
81
+ }
82
+
83
+ /** Called once the root rollup proof has been set; subclasses call `state.resolve(...)` with the right shape. */
84
+ protected abstract onRootRollupComplete(state: TopTreeStateLike): void;
85
+
86
+ protected enqueueCheckpointMergeRollup(state: TopTreeStateLike, location: TreeNodeLocation) {
87
+ if (!state.verifyState() || !state.tryStartProvingCheckpointMerge(location)) {
88
+ return;
89
+ }
90
+ const inputs = state.getCheckpointMergeRollupInputs(location);
91
+ this.deferredProving(
92
+ state,
93
+ this.wrapCircuitCall('rollup-checkpoint-merge', signal =>
94
+ this.prover.getCheckpointMergeRollupProof(inputs, signal, state.epochNumber),
95
+ ),
96
+ result => {
97
+ state.setCheckpointMergeRollupProof(location, result);
98
+ this.checkAndEnqueueNextCheckpointMergeRollup(state, location);
99
+ },
100
+ );
101
+ }
102
+
103
+ protected enqueueEpochPadding(state: TopTreeStateLike) {
104
+ if (!state.verifyState() || !state.tryStartProvingPaddingCheckpoint()) {
105
+ return;
106
+ }
107
+ const inputs = state.getPaddingCheckpointInputs();
108
+ this.deferredProving(
109
+ state,
110
+ this.wrapCircuitCall('rollup-checkpoint-padding', signal =>
111
+ this.prover.getCheckpointPaddingRollupProof(inputs, signal, state.epochNumber),
112
+ ),
113
+ result => {
114
+ state.setCheckpointPaddingProof(result);
115
+ this.checkAndEnqueueRootRollup(state);
116
+ },
117
+ );
118
+ }
119
+
120
+ protected enqueueRootRollup(state: TopTreeStateLike) {
121
+ if (!state.verifyState() || !state.tryStartProvingRootRollup()) {
122
+ return;
123
+ }
124
+ const inputs = state.getRootRollupInputs();
125
+ this.deferredProving(
126
+ state,
127
+ this.wrapCircuitCall('rollup-root', signal => this.prover.getRootRollupProof(inputs, signal, state.epochNumber)),
128
+ result => {
129
+ this.logger.verbose(`Completed root rollup for epoch ${state.epochNumber}`);
130
+ state.setRootRollupProof(result);
131
+ this.onRootRollupComplete(state);
132
+ },
133
+ );
134
+ }
135
+
136
+ protected checkAndEnqueueNextCheckpointMergeRollup(state: TopTreeStateLike, currentLocation: TreeNodeLocation) {
137
+ if (!state.isReadyForCheckpointMerge(currentLocation)) {
138
+ return;
139
+ }
140
+ const parentLocation = state.getParentLocation(currentLocation);
141
+ if (parentLocation.level === 0) {
142
+ this.checkAndEnqueueRootRollup(state);
143
+ } else {
144
+ this.enqueueCheckpointMergeRollup(state, parentLocation);
145
+ }
146
+ }
147
+
148
+ protected checkAndEnqueueRootRollup(state: TopTreeStateLike) {
149
+ if (!state.isReadyForRootRollup()) {
150
+ return;
151
+ }
152
+ this.enqueueRootRollup(state);
153
+ }
154
+ }