@aztec/prover-client 0.0.1-commit.b655e406 → 0.0.1-commit.b8a057fa

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 (178) hide show
  1. package/dest/config.d.ts +2 -2
  2. package/dest/config.d.ts.map +1 -1
  3. package/dest/config.js +17 -3
  4. package/dest/index.d.ts +1 -1
  5. package/dest/light/index.d.ts +2 -0
  6. package/dest/light/index.d.ts.map +1 -0
  7. package/dest/light/index.js +1 -0
  8. package/dest/light/lightweight_checkpoint_builder.d.ts +52 -0
  9. package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -0
  10. package/dest/light/lightweight_checkpoint_builder.js +233 -0
  11. package/dest/mocks/fixtures.d.ts +1 -4
  12. package/dest/mocks/fixtures.d.ts.map +1 -1
  13. package/dest/mocks/fixtures.js +9 -18
  14. package/dest/mocks/test_context.d.ts +38 -55
  15. package/dest/mocks/test_context.d.ts.map +1 -1
  16. package/dest/mocks/test_context.js +143 -142
  17. package/dest/orchestrator/block-building-helpers.d.ts +18 -20
  18. package/dest/orchestrator/block-building-helpers.d.ts.map +1 -1
  19. package/dest/orchestrator/block-building-helpers.js +90 -115
  20. package/dest/orchestrator/block-proving-state.d.ts +20 -11
  21. package/dest/orchestrator/block-proving-state.d.ts.map +1 -1
  22. package/dest/orchestrator/block-proving-state.js +87 -19
  23. package/dest/orchestrator/checkpoint-proving-state.d.ts +24 -27
  24. package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
  25. package/dest/orchestrator/checkpoint-proving-state.js +28 -94
  26. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts +161 -0
  27. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts.map +1 -0
  28. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.js +1066 -0
  29. package/dest/orchestrator/chonk-cache.d.ts +39 -0
  30. package/dest/orchestrator/chonk-cache.d.ts.map +1 -0
  31. package/dest/orchestrator/chonk-cache.js +80 -0
  32. package/dest/orchestrator/index.d.ts +4 -2
  33. package/dest/orchestrator/index.d.ts.map +1 -1
  34. package/dest/orchestrator/index.js +3 -1
  35. package/dest/orchestrator/orchestrator_metrics.d.ts +1 -3
  36. package/dest/orchestrator/orchestrator_metrics.d.ts.map +1 -1
  37. package/dest/orchestrator/orchestrator_metrics.js +2 -15
  38. package/dest/orchestrator/proving-scheduler.d.ts +81 -0
  39. package/dest/orchestrator/proving-scheduler.d.ts.map +1 -0
  40. package/dest/orchestrator/proving-scheduler.js +120 -0
  41. package/dest/orchestrator/top-tree-orchestrator.d.ts +88 -0
  42. package/dest/orchestrator/top-tree-orchestrator.d.ts.map +1 -0
  43. package/dest/orchestrator/top-tree-orchestrator.js +232 -0
  44. package/dest/orchestrator/top-tree-proving-state.d.ts +61 -0
  45. package/dest/orchestrator/top-tree-proving-state.d.ts.map +1 -0
  46. package/dest/orchestrator/top-tree-proving-state.js +185 -0
  47. package/dest/orchestrator/tx-proving-state.d.ts +7 -6
  48. package/dest/orchestrator/tx-proving-state.d.ts.map +1 -1
  49. package/dest/orchestrator/tx-proving-state.js +7 -16
  50. package/dest/prover-client/factory.d.ts +3 -3
  51. package/dest/prover-client/factory.d.ts.map +1 -1
  52. package/dest/prover-client/index.d.ts +1 -1
  53. package/dest/prover-client/prover-client.d.ts +65 -8
  54. package/dest/prover-client/prover-client.d.ts.map +1 -1
  55. package/dest/prover-client/prover-client.js +66 -14
  56. package/dest/proving_broker/broker_prover_facade.d.ts +26 -24
  57. package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
  58. package/dest/proving_broker/broker_prover_facade.js +21 -33
  59. package/dest/proving_broker/config.d.ts +14 -62
  60. package/dest/proving_broker/config.d.ts.map +1 -1
  61. package/dest/proving_broker/config.js +29 -6
  62. package/dest/proving_broker/factory.d.ts +1 -1
  63. package/dest/proving_broker/fixtures.d.ts +3 -2
  64. package/dest/proving_broker/fixtures.d.ts.map +1 -1
  65. package/dest/proving_broker/fixtures.js +3 -2
  66. package/dest/proving_broker/index.d.ts +2 -1
  67. package/dest/proving_broker/index.d.ts.map +1 -1
  68. package/dest/proving_broker/index.js +1 -0
  69. package/dest/proving_broker/proof_store/factory.d.ts +2 -5
  70. package/dest/proving_broker/proof_store/factory.d.ts.map +1 -1
  71. package/dest/proving_broker/proof_store/factory.js +7 -30
  72. package/dest/proving_broker/proof_store/file_store_proof_store.d.ts +18 -0
  73. package/dest/proving_broker/proof_store/file_store_proof_store.d.ts.map +1 -0
  74. package/dest/proving_broker/proof_store/file_store_proof_store.js +60 -0
  75. package/dest/proving_broker/proof_store/index.d.ts +2 -2
  76. package/dest/proving_broker/proof_store/index.d.ts.map +1 -1
  77. package/dest/proving_broker/proof_store/index.js +1 -1
  78. package/dest/proving_broker/proof_store/inline_proof_store.d.ts +1 -1
  79. package/dest/proving_broker/proof_store/inline_proof_store.d.ts.map +1 -1
  80. package/dest/proving_broker/proof_store/proof_store.d.ts +1 -1
  81. package/dest/proving_broker/proving_agent.d.ts +5 -9
  82. package/dest/proving_broker/proving_agent.d.ts.map +1 -1
  83. package/dest/proving_broker/proving_agent.js +4 -19
  84. package/dest/proving_broker/proving_broker.d.ts +8 -5
  85. package/dest/proving_broker/proving_broker.d.ts.map +1 -1
  86. package/dest/proving_broker/proving_broker.js +74 -22
  87. package/dest/proving_broker/proving_broker_database/memory.d.ts +3 -2
  88. package/dest/proving_broker/proving_broker_database/memory.d.ts.map +1 -1
  89. package/dest/proving_broker/proving_broker_database/persisted.d.ts +5 -3
  90. package/dest/proving_broker/proving_broker_database/persisted.d.ts.map +1 -1
  91. package/dest/proving_broker/proving_broker_database/persisted.js +394 -5
  92. package/dest/proving_broker/proving_broker_database.d.ts +3 -2
  93. package/dest/proving_broker/proving_broker_database.d.ts.map +1 -1
  94. package/dest/proving_broker/proving_broker_instrumentation.d.ts +3 -1
  95. package/dest/proving_broker/proving_broker_instrumentation.d.ts.map +1 -1
  96. package/dest/proving_broker/proving_broker_instrumentation.js +22 -35
  97. package/dest/proving_broker/proving_job_controller.d.ts +5 -3
  98. package/dest/proving_broker/proving_job_controller.d.ts.map +1 -1
  99. package/dest/proving_broker/proving_job_controller.js +8 -6
  100. package/dest/proving_broker/rpc.d.ts +9 -5
  101. package/dest/proving_broker/rpc.d.ts.map +1 -1
  102. package/dest/proving_broker/rpc.js +87 -23
  103. package/dest/test/epoch_settlement.d.ts +36 -0
  104. package/dest/test/epoch_settlement.d.ts.map +1 -0
  105. package/dest/test/epoch_settlement.js +52 -0
  106. package/dest/test/index.d.ts +2 -0
  107. package/dest/test/index.d.ts.map +1 -0
  108. package/dest/test/index.js +1 -0
  109. package/dest/test/mock_proof_store.d.ts +1 -1
  110. package/dest/test/mock_proof_store.d.ts.map +1 -1
  111. package/dest/test/mock_prover.d.ts +5 -6
  112. package/dest/test/mock_prover.d.ts.map +1 -1
  113. package/dest/test/mock_prover.js +4 -4
  114. package/package.json +25 -23
  115. package/src/config.ts +19 -3
  116. package/src/light/index.ts +1 -0
  117. package/src/light/lightweight_checkpoint_builder.ts +322 -0
  118. package/src/mocks/fixtures.ts +9 -31
  119. package/src/mocks/test_context.ts +182 -217
  120. package/src/orchestrator/block-building-helpers.ts +129 -209
  121. package/src/orchestrator/block-proving-state.ts +109 -22
  122. package/src/orchestrator/checkpoint-proving-state.ts +32 -123
  123. package/src/orchestrator/checkpoint-sub-tree-orchestrator.ts +1044 -0
  124. package/src/orchestrator/chonk-cache.ts +99 -0
  125. package/src/orchestrator/index.ts +8 -1
  126. package/src/orchestrator/orchestrator_metrics.ts +2 -25
  127. package/src/orchestrator/proving-scheduler.ts +160 -0
  128. package/src/orchestrator/top-tree-orchestrator.ts +384 -0
  129. package/src/orchestrator/top-tree-proving-state.ts +219 -0
  130. package/src/orchestrator/tx-proving-state.ts +10 -27
  131. package/src/prover-client/factory.ts +6 -2
  132. package/src/prover-client/prover-client.ts +148 -29
  133. package/src/proving_broker/broker_prover_facade.ts +47 -56
  134. package/src/proving_broker/config.ts +33 -3
  135. package/src/proving_broker/fixtures.ts +8 -3
  136. package/src/proving_broker/index.ts +1 -0
  137. package/src/proving_broker/proof_store/factory.ts +10 -32
  138. package/src/proving_broker/proof_store/file_store_proof_store.ts +78 -0
  139. package/src/proving_broker/proof_store/index.ts +1 -1
  140. package/src/proving_broker/proving_agent.ts +6 -19
  141. package/src/proving_broker/proving_broker.ts +70 -17
  142. package/src/proving_broker/proving_broker_database/memory.ts +2 -1
  143. package/src/proving_broker/proving_broker_database/persisted.ts +22 -7
  144. package/src/proving_broker/proving_broker_database.ts +2 -1
  145. package/src/proving_broker/proving_broker_instrumentation.ts +23 -35
  146. package/src/proving_broker/proving_job_controller.ts +13 -7
  147. package/src/proving_broker/rpc.ts +46 -20
  148. package/src/test/epoch_settlement.ts +82 -0
  149. package/src/test/index.ts +1 -0
  150. package/src/test/mock_prover.ts +3 -15
  151. package/dest/block-factory/index.d.ts +0 -2
  152. package/dest/block-factory/index.d.ts.map +0 -1
  153. package/dest/block-factory/index.js +0 -1
  154. package/dest/block-factory/light.d.ts +0 -38
  155. package/dest/block-factory/light.d.ts.map +0 -1
  156. package/dest/block-factory/light.js +0 -94
  157. package/dest/orchestrator/epoch-proving-state.d.ts +0 -73
  158. package/dest/orchestrator/epoch-proving-state.d.ts.map +0 -1
  159. package/dest/orchestrator/epoch-proving-state.js +0 -233
  160. package/dest/orchestrator/orchestrator.d.ts +0 -113
  161. package/dest/orchestrator/orchestrator.d.ts.map +0 -1
  162. package/dest/orchestrator/orchestrator.js +0 -848
  163. package/dest/prover-client/server-epoch-prover.d.ts +0 -31
  164. package/dest/prover-client/server-epoch-prover.d.ts.map +0 -1
  165. package/dest/prover-client/server-epoch-prover.js +0 -40
  166. package/dest/proving_broker/proof_store/gcs_proof_store.d.ts +0 -14
  167. package/dest/proving_broker/proof_store/gcs_proof_store.d.ts.map +0 -1
  168. package/dest/proving_broker/proof_store/gcs_proof_store.js +0 -52
  169. package/dest/proving_broker/proving_agent_instrumentation.d.ts +0 -8
  170. package/dest/proving_broker/proving_agent_instrumentation.d.ts.map +0 -1
  171. package/dest/proving_broker/proving_agent_instrumentation.js +0 -16
  172. package/src/block-factory/index.ts +0 -1
  173. package/src/block-factory/light.ts +0 -140
  174. package/src/orchestrator/epoch-proving-state.ts +0 -328
  175. package/src/orchestrator/orchestrator.ts +0 -1272
  176. package/src/prover-client/server-epoch-prover.ts +0 -70
  177. package/src/proving_broker/proof_store/gcs_proof_store.ts +0 -76
  178. package/src/proving_broker/proving_agent_instrumentation.ts +0 -21
@@ -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
+ }
@@ -0,0 +1,219 @@
1
+ import type { BatchedBlob, BatchedBlobAccumulator, FinalBlobBatchingChallenges } from '@aztec/blob-lib';
2
+ import type { NESTED_RECURSIVE_PROOF_LENGTH, NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH } from '@aztec/constants';
3
+ import { EpochNumber } from '@aztec/foundation/branded-types';
4
+ import { type TreeNodeLocation, UnbalancedTreeStore } from '@aztec/foundation/trees';
5
+ import type { PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
6
+ import type { Proof } from '@aztec/stdlib/proofs';
7
+ import {
8
+ CheckpointMergeRollupPrivateInputs,
9
+ CheckpointPaddingRollupPrivateInputs,
10
+ CheckpointRollupPublicInputs,
11
+ RootRollupPrivateInputs,
12
+ type RootRollupPublicInputs,
13
+ } from '@aztec/stdlib/rollup';
14
+
15
+ import { toProofData } from './block-building-helpers.js';
16
+ import type { ProofState } from './block-proving-state.js';
17
+
18
+ enum TOP_TREE_LIFECYCLE {
19
+ CREATED,
20
+ RESOLVED,
21
+ REJECTED,
22
+ }
23
+
24
+ /**
25
+ * Lean top-tree-only state. Owns the merge tree of checkpoint root proofs, the
26
+ * single-checkpoint padding proof slot, the final root rollup proof, and the blob
27
+ * accumulator endpoints needed to finalise the epoch's batched blob proof.
28
+ *
29
+ * Constructed with `totalNumCheckpoints` and `finalBlobBatchingChallenges` upfront —
30
+ * by the time the top tree starts, all checkpoints are known and the challenges are
31
+ * derivable from their blob fields.
32
+ */
33
+ export class TopTreeProvingState {
34
+ private checkpointProofs: UnbalancedTreeStore<
35
+ ProofState<CheckpointRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
36
+ >;
37
+ private checkpointPaddingProof:
38
+ | ProofState<CheckpointRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
39
+ | undefined;
40
+ private rootRollupProof: ProofState<RootRollupPublicInputs, typeof NESTED_RECURSIVE_PROOF_LENGTH> | undefined;
41
+ private endBlobAccumulator: BatchedBlobAccumulator | undefined;
42
+ private finalBatchedBlob: BatchedBlob | undefined;
43
+ private lifecycle = TOP_TREE_LIFECYCLE.CREATED;
44
+ constructor(
45
+ public readonly epochNumber: EpochNumber,
46
+ public readonly totalNumCheckpoints: number,
47
+ public readonly finalBlobBatchingChallenges: FinalBlobBatchingChallenges,
48
+ public readonly startBlobAccumulator: BatchedBlobAccumulator,
49
+ private readonly completionCallback: () => void,
50
+ private readonly rejectionCallback: (reason: string) => void,
51
+ ) {
52
+ if (totalNumCheckpoints < 1) {
53
+ throw new Error(`TopTreeProvingState requires at least one checkpoint; got ${totalNumCheckpoints}.`);
54
+ }
55
+ this.checkpointProofs = new UnbalancedTreeStore(totalNumCheckpoints);
56
+ }
57
+
58
+ // --- checkpoint root rollup ---
59
+
60
+ public setCheckpointRootRollupProof(
61
+ checkpointIndex: number,
62
+ provingOutput: PublicInputsAndRecursiveProof<
63
+ CheckpointRollupPublicInputs,
64
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
65
+ >,
66
+ ): TreeNodeLocation {
67
+ return this.checkpointProofs.setLeaf(checkpointIndex, { provingOutput });
68
+ }
69
+
70
+ // --- checkpoint merge rollup ---
71
+
72
+ public tryStartProvingCheckpointMerge(location: TreeNodeLocation) {
73
+ if (this.checkpointProofs.getNode(location)?.isProving) {
74
+ return false;
75
+ }
76
+ this.checkpointProofs.setNode(location, { isProving: true });
77
+ return true;
78
+ }
79
+
80
+ public setCheckpointMergeRollupProof(
81
+ location: TreeNodeLocation,
82
+ provingOutput: PublicInputsAndRecursiveProof<
83
+ CheckpointRollupPublicInputs,
84
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
85
+ >,
86
+ ) {
87
+ this.checkpointProofs.setNode(location, { provingOutput });
88
+ }
89
+
90
+ public isReadyForCheckpointMerge(location: TreeNodeLocation) {
91
+ return !!this.checkpointProofs.getSibling(location)?.provingOutput;
92
+ }
93
+
94
+ public getParentLocation(location: TreeNodeLocation) {
95
+ return this.checkpointProofs.getParentLocation(location);
96
+ }
97
+
98
+ public getCheckpointMergeRollupInputs(mergeLocation: TreeNodeLocation) {
99
+ const [left, right] = this.checkpointProofs.getChildren(mergeLocation).map(c => c?.provingOutput);
100
+ if (!left || !right) {
101
+ throw new Error('At least one child is not ready for the checkpoint merge rollup.');
102
+ }
103
+ return new CheckpointMergeRollupPrivateInputs([toProofData(left), toProofData(right)]);
104
+ }
105
+
106
+ // --- padding (single-checkpoint case) ---
107
+
108
+ public tryStartProvingPaddingCheckpoint() {
109
+ if (this.checkpointPaddingProof?.isProving) {
110
+ return false;
111
+ }
112
+ this.checkpointPaddingProof = { isProving: true };
113
+ return true;
114
+ }
115
+
116
+ public setCheckpointPaddingProof(
117
+ provingOutput: PublicInputsAndRecursiveProof<
118
+ CheckpointRollupPublicInputs,
119
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
120
+ >,
121
+ ) {
122
+ this.checkpointPaddingProof = { provingOutput };
123
+ }
124
+
125
+ public getPaddingCheckpointInputs() {
126
+ return new CheckpointPaddingRollupPrivateInputs();
127
+ }
128
+
129
+ // --- root rollup ---
130
+
131
+ public tryStartProvingRootRollup() {
132
+ if (this.rootRollupProof?.isProving) {
133
+ return false;
134
+ }
135
+ this.rootRollupProof = { isProving: true };
136
+ return true;
137
+ }
138
+
139
+ public setRootRollupProof(provingOutput: PublicInputsAndRecursiveProof<RootRollupPublicInputs>) {
140
+ this.rootRollupProof = { provingOutput };
141
+ }
142
+
143
+ public isReadyForRootRollup() {
144
+ const childProofs = this.#getChildProofsForRoot();
145
+ return childProofs.every(p => !!p);
146
+ }
147
+
148
+ public getRootRollupInputs() {
149
+ const [left, right] = this.#getChildProofsForRoot();
150
+ if (!left || !right) {
151
+ throw new Error('At least one child is not ready for the root rollup.');
152
+ }
153
+ return RootRollupPrivateInputs.from({
154
+ previousRollups: [toProofData(left), toProofData(right)],
155
+ });
156
+ }
157
+
158
+ // --- blob accumulator finalisation ---
159
+
160
+ /**
161
+ * Sets the end-of-epoch blob accumulator, computed by the top-tree orchestrator
162
+ * from the surviving checkpoints' blob fields. Required before `finalizeBatchedBlob`.
163
+ */
164
+ public setEndBlobAccumulator(accumulator: BatchedBlobAccumulator) {
165
+ this.endBlobAccumulator = accumulator;
166
+ }
167
+
168
+ public async finalizeBatchedBlob() {
169
+ if (!this.endBlobAccumulator) {
170
+ throw new Error('End blob accumulator not set; call setEndBlobAccumulator before finalize.');
171
+ }
172
+ this.finalBatchedBlob = await this.endBlobAccumulator.finalize(true /* verifyProof */);
173
+ }
174
+
175
+ public getEpochProofResult(): { proof: Proof; publicInputs: RootRollupPublicInputs; batchedBlobInputs: BatchedBlob } {
176
+ const provingOutput = this.rootRollupProof?.provingOutput;
177
+ if (!provingOutput || !this.finalBatchedBlob) {
178
+ throw new Error('Top-tree proof not ready; root rollup or batched blob missing.');
179
+ }
180
+ return {
181
+ proof: provingOutput.proof.binaryProof,
182
+ publicInputs: provingOutput.inputs,
183
+ batchedBlobInputs: this.finalBatchedBlob,
184
+ };
185
+ }
186
+
187
+ // --- lifecycle ---
188
+
189
+ public verifyState() {
190
+ return this.lifecycle === TOP_TREE_LIFECYCLE.CREATED;
191
+ }
192
+
193
+ public resolve() {
194
+ if (!this.verifyState()) {
195
+ return;
196
+ }
197
+ this.lifecycle = TOP_TREE_LIFECYCLE.RESOLVED;
198
+ this.completionCallback();
199
+ }
200
+
201
+ public reject(reason: string) {
202
+ if (!this.verifyState()) {
203
+ return;
204
+ }
205
+ this.lifecycle = TOP_TREE_LIFECYCLE.REJECTED;
206
+ this.rejectionCallback(reason);
207
+ }
208
+
209
+ public cancel() {
210
+ this.reject('Top-tree proving cancelled');
211
+ }
212
+
213
+ #getChildProofsForRoot() {
214
+ const rootLocation = { level: 0, index: 0 };
215
+ return this.totalNumCheckpoints === 1
216
+ ? [this.checkpointProofs.getNode(rootLocation)?.provingOutput, this.checkpointPaddingProof?.provingOutput]
217
+ : this.checkpointProofs.getChildren(rootLocation).map(c => c?.provingOutput);
218
+ }
219
+ }
@@ -1,14 +1,9 @@
1
- import {
2
- AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED,
3
- AVM_VK_INDEX,
4
- NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
5
- } from '@aztec/constants';
6
- import type { Fr } from '@aztec/foundation/fields';
1
+ import { AVM_V2_PROOF_LENGTH_IN_FIELDS, NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH } from '@aztec/constants';
2
+ import type { Fr } from '@aztec/foundation/curves/bn254';
7
3
  import { getVkData } from '@aztec/noir-protocol-circuits-types/server/vks';
8
- import { getVKSiblingPath } from '@aztec/noir-protocol-circuits-types/vk-tree';
9
4
  import type { AvmCircuitInputs } from '@aztec/stdlib/avm';
10
- import type { ProofAndVerificationKey, PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
11
- import { ProofData } from '@aztec/stdlib/proofs';
5
+ import type { PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
6
+ import { ProofData, ProofDataForFixedVk, RecursiveProof } from '@aztec/stdlib/proofs';
12
7
  import {
13
8
  type BaseRollupHints,
14
9
  PrivateBaseRollupHints,
@@ -20,7 +15,6 @@ import {
20
15
  import type { CircuitName } from '@aztec/stdlib/stats';
21
16
  import type { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
22
17
  import type { ProcessedTx } from '@aztec/stdlib/tx';
23
- import { VerificationKeyData, VkData } from '@aztec/stdlib/vks';
24
18
 
25
19
  import {
26
20
  getChonkProofFromTx,
@@ -38,7 +32,7 @@ export class TxProvingState {
38
32
  PublicChonkVerifierPublicInputs,
39
33
  typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
40
34
  >;
41
- private avm?: ProofAndVerificationKey<typeof AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED>;
35
+ private avmProof?: RecursiveProof<typeof AVM_V2_PROOF_LENGTH_IN_FIELDS>;
42
36
 
43
37
  constructor(
44
38
  public readonly processedTx: ProcessedTx,
@@ -52,7 +46,7 @@ export class TxProvingState {
52
46
  }
53
47
 
54
48
  public ready() {
55
- return !this.requireAvmProof || (!!this.avm && !!this.publicChonkVerifier);
49
+ return !this.requireAvmProof || (!!this.avmProof && !!this.publicChonkVerifier);
56
50
  }
57
51
 
58
52
  public getAvmInputs(): AvmCircuitInputs {
@@ -86,8 +80,8 @@ export class TxProvingState {
86
80
  this.publicChonkVerifier = publicChonkVerifierProofAndVk;
87
81
  }
88
82
 
89
- public setAvmProof(avmProofAndVk: ProofAndVerificationKey<typeof AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED>) {
90
- this.avm = avmProofAndVk;
83
+ public setAvmProof(avmProof: RecursiveProof<typeof AVM_V2_PROOF_LENGTH_IN_FIELDS>) {
84
+ this.avmProof = avmProof;
91
85
  }
92
86
 
93
87
  #getPrivateBaseInputs() {
@@ -111,7 +105,7 @@ export class TxProvingState {
111
105
  if (!this.publicChonkVerifier) {
112
106
  throw new Error('Tx not ready for proving base rollup: public chonk verifier proof undefined');
113
107
  }
114
- if (!this.avm) {
108
+ if (!this.avmProof) {
115
109
  throw new Error('Tx not ready for proving base rollup: avm proof undefined');
116
110
  }
117
111
  if (!(this.baseRollupHints instanceof PublicBaseRollupHints)) {
@@ -120,19 +114,8 @@ export class TxProvingState {
120
114
 
121
115
  const publicChonkVerifierProofData = toProofData(this.publicChonkVerifier);
122
116
 
123
- const avmProofData = new ProofData(
124
- this.processedTx.avmProvingRequest.inputs.publicInputs,
125
- this.avm.proof,
126
- this.#getVkData(this.avm!.verificationKey, AVM_VK_INDEX),
127
- );
117
+ const avmProofData = new ProofDataForFixedVk(this.processedTx.avmProvingRequest.inputs.publicInputs, this.avmProof);
128
118
 
129
119
  return new PublicTxBaseRollupPrivateInputs(publicChonkVerifierProofData, avmProofData, this.baseRollupHints);
130
120
  }
131
-
132
- #getVkData(verificationKey: VerificationKeyData, vkIndex: number) {
133
- // TODO(#17162): Add avm vk hash to the tree and call `getVkData('AVM')` instead.
134
- // Below will return a path to an empty leaf.
135
- const vkPath = getVKSiblingPath(vkIndex);
136
- return new VkData(verificationKey, vkIndex, vkPath);
137
- }
138
121
  }