@aztec/prover-client 0.0.1-commit.96dac018d → 0.0.1-commit.993d240

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 (78) 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 +2 -1
  5. package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -1
  6. package/dest/light/lightweight_checkpoint_builder.js +19 -4
  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 +13 -7
  10. package/dest/orchestrator/block-building-helpers.d.ts +1 -1
  11. package/dest/orchestrator/checkpoint-proving-state.d.ts +8 -1
  12. package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
  13. package/dest/orchestrator/checkpoint-proving-state.js +10 -1
  14. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts +107 -0
  15. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts.map +1 -0
  16. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.js +151 -0
  17. package/dest/orchestrator/epoch-proving-context.d.ts +51 -0
  18. package/dest/orchestrator/epoch-proving-context.d.ts.map +1 -0
  19. package/dest/orchestrator/epoch-proving-context.js +81 -0
  20. package/dest/orchestrator/epoch-proving-state.d.ts +1 -1
  21. package/dest/orchestrator/index.d.ts +4 -1
  22. package/dest/orchestrator/index.d.ts.map +1 -1
  23. package/dest/orchestrator/index.js +3 -0
  24. package/dest/orchestrator/orchestrator.d.ts +15 -25
  25. package/dest/orchestrator/orchestrator.d.ts.map +1 -1
  26. package/dest/orchestrator/orchestrator.js +33 -154
  27. package/dest/orchestrator/proving-scheduler.d.ts +72 -0
  28. package/dest/orchestrator/proving-scheduler.d.ts.map +1 -0
  29. package/dest/orchestrator/proving-scheduler.js +117 -0
  30. package/dest/orchestrator/top-tree-orchestrator.d.ts +83 -0
  31. package/dest/orchestrator/top-tree-orchestrator.d.ts.map +1 -0
  32. package/dest/orchestrator/top-tree-orchestrator.js +182 -0
  33. package/dest/orchestrator/top-tree-proving-scheduler.d.ts +62 -0
  34. package/dest/orchestrator/top-tree-proving-scheduler.d.ts.map +1 -0
  35. package/dest/orchestrator/top-tree-proving-scheduler.js +73 -0
  36. package/dest/orchestrator/top-tree-proving-state.d.ts +61 -0
  37. package/dest/orchestrator/top-tree-proving-state.d.ts.map +1 -0
  38. package/dest/orchestrator/top-tree-proving-state.js +185 -0
  39. package/dest/prover-client/prover-client.d.ts +62 -3
  40. package/dest/prover-client/prover-client.d.ts.map +1 -1
  41. package/dest/prover-client/prover-client.js +50 -2
  42. package/dest/proving_broker/broker_prover_facade.d.ts +1 -1
  43. package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
  44. package/dest/proving_broker/broker_prover_facade.js +13 -19
  45. package/dest/proving_broker/config.d.ts +9 -73
  46. package/dest/proving_broker/config.d.ts.map +1 -1
  47. package/dest/proving_broker/config.js +3 -3
  48. package/dest/proving_broker/index.d.ts +2 -1
  49. package/dest/proving_broker/index.d.ts.map +1 -1
  50. package/dest/proving_broker/index.js +1 -0
  51. package/dest/proving_broker/proving_broker.d.ts +2 -2
  52. package/dest/proving_broker/proving_broker.d.ts.map +1 -1
  53. package/dest/proving_broker/proving_broker.js +32 -7
  54. package/dest/proving_broker/proving_broker_database/persisted.js +2 -2
  55. package/dest/proving_broker/rpc.d.ts +3 -1
  56. package/dest/proving_broker/rpc.d.ts.map +1 -1
  57. package/dest/proving_broker/rpc.js +80 -24
  58. package/dest/test/mock_prover.d.ts +3 -3
  59. package/package.json +18 -19
  60. package/src/config.ts +18 -2
  61. package/src/light/lightweight_checkpoint_builder.ts +25 -6
  62. package/src/mocks/test_context.ts +11 -8
  63. package/src/orchestrator/checkpoint-proving-state.ts +14 -1
  64. package/src/orchestrator/checkpoint-sub-tree-orchestrator.ts +271 -0
  65. package/src/orchestrator/epoch-proving-context.ts +101 -0
  66. package/src/orchestrator/index.ts +8 -0
  67. package/src/orchestrator/orchestrator.ts +46 -210
  68. package/src/orchestrator/proving-scheduler.ts +156 -0
  69. package/src/orchestrator/top-tree-orchestrator.ts +314 -0
  70. package/src/orchestrator/top-tree-proving-scheduler.ts +154 -0
  71. package/src/orchestrator/top-tree-proving-state.ts +220 -0
  72. package/src/prover-client/prover-client.ts +127 -2
  73. package/src/proving_broker/broker_prover_facade.ts +17 -20
  74. package/src/proving_broker/config.ts +3 -2
  75. package/src/proving_broker/index.ts +1 -0
  76. package/src/proving_broker/proving_broker.ts +27 -5
  77. package/src/proving_broker/proving_broker_database/persisted.ts +2 -2
  78. package/src/proving_broker/rpc.ts +36 -24
@@ -1,7 +1,5 @@
1
1
  import { SpongeBlob, computeBlobsHashFromBlobs, encodeCheckpointEndMarker, getBlobsPerL1Block } from '@aztec/blob-lib';
2
- import { NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
3
2
  import { type CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
4
- import { padArrayEnd } from '@aztec/foundation/collection';
5
3
  import { Fr } from '@aztec/foundation/curves/bn254';
6
4
  import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
7
5
  import { elapsed } from '@aztec/foundation/timer';
@@ -10,6 +8,7 @@ import { Checkpoint } from '@aztec/stdlib/checkpoint';
10
8
  import type { MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
11
9
  import {
12
10
  accumulateCheckpointOutHashes,
11
+ appendL1ToL2MessagesToTree,
13
12
  computeCheckpointOutHash,
14
13
  computeInHashFromL1ToL2Messages,
15
14
  } from '@aztec/stdlib/messaging';
@@ -69,10 +68,7 @@ export class LightweightCheckpointBuilder {
69
68
  feeAssetPriceModifier: bigint = 0n,
70
69
  ): Promise<LightweightCheckpointBuilder> {
71
70
  // Insert l1-to-l2 messages into the tree.
72
- await db.appendLeaves(
73
- MerkleTreeId.L1_TO_L2_MESSAGE_TREE,
74
- padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP),
75
- );
71
+ await appendL1ToL2MessagesToTree(db, l1ToL2Messages);
76
72
 
77
73
  return new LightweightCheckpointBuilder(
78
74
  checkpointNumber,
@@ -117,6 +113,10 @@ export class LightweightCheckpointBuilder {
117
113
  blockNumbers: existingBlocks.map(b => b.header.getBlockNumber()),
118
114
  });
119
115
 
116
+ if (existingBlocks.length === 0) {
117
+ throw new Error(`Cannot resume checkpoint ${checkpointNumber} with no existing blocks`);
118
+ }
119
+
120
120
  // Validate block order and consistency
121
121
  for (let i = 1; i < existingBlocks.length; i++) {
122
122
  const prev = existingBlocks[i - 1];
@@ -154,6 +154,10 @@ export class LightweightCheckpointBuilder {
154
154
  return this.blocks.length;
155
155
  }
156
156
 
157
+ public getBlocks() {
158
+ return this.blocks;
159
+ }
160
+
157
161
  /**
158
162
  * Adds a new block to the checkpoint. The tx effects must have already been inserted into the db if
159
163
  * this is called after tx processing, if that's not the case, then set `insertTxsEffects` to true.
@@ -216,6 +220,13 @@ export class LightweightCheckpointBuilder {
216
220
  timings.updateArchive = msUpdateArchive;
217
221
  this.lastArchives.push(newArchive);
218
222
 
223
+ const expectedNextLeafIndex = Number(globalVariables.blockNumber) + 1;
224
+ if (newArchive.nextAvailableLeafIndex !== expectedNextLeafIndex) {
225
+ throw new Error(
226
+ `Archive tree next leaf index mismatch after building block ${globalVariables.blockNumber} (expected ${expectedNextLeafIndex} but got ${newArchive.nextAvailableLeafIndex})`,
227
+ );
228
+ }
229
+
219
230
  const indexWithinCheckpoint = IndexWithinCheckpoint(this.blocks.length);
220
231
  const block = new L2Block(newArchive, header, body, this.checkpointNumber, indexWithinCheckpoint);
221
232
  this.blocks.push(block);
@@ -279,6 +290,14 @@ export class LightweightCheckpointBuilder {
279
290
  totalManaUsed,
280
291
  });
281
292
 
293
+ this.logger.debug(`Completed checkpoint ${this.checkpointNumber}`, {
294
+ checkpointNumber: this.checkpointNumber,
295
+ headerHash: header.hash().toString(),
296
+ checkpointOutHash: checkpointOutHash.toString(),
297
+ numPreviousCheckpointOutHashes: this.previousCheckpointOutHashes.length,
298
+ ...header.toInspect(),
299
+ });
300
+
282
301
  return new Checkpoint(newArchive, header, blocks, this.checkpointNumber, this.feeAssetPriceModifier);
283
302
  }
284
303
 
@@ -24,6 +24,7 @@ import {
24
24
  StateReference,
25
25
  TreeSnapshots,
26
26
  } from '@aztec/stdlib/tx';
27
+ import type { GenesisData } from '@aztec/stdlib/world-state';
27
28
  import type { MerkleTreeAdminDatabase } from '@aztec/world-state';
28
29
  import { NativeWorldStateService } from '@aztec/world-state/native';
29
30
 
@@ -84,14 +85,13 @@ export class TestContext {
84
85
  const feePayer = AztecAddress.fromNumber(42222);
85
86
  const initialFeePayerBalance = new Fr(10n ** 20n);
86
87
  const feePayerSlot = await computeFeePayerBalanceLeafSlot(feePayer);
87
- const prefilledPublicData = [new PublicDataTreeLeaf(feePayerSlot, initialFeePayerBalance)];
88
+ const genesis: GenesisData = {
89
+ prefilledPublicData: [new PublicDataTreeLeaf(feePayerSlot, initialFeePayerBalance)],
90
+ genesisTimestamp: 0n,
91
+ };
88
92
 
89
93
  // Separated dbs for public processor and prover - see public_processor for context
90
- const ws = await NativeWorldStateService.tmp(
91
- /*rollupAddress=*/ undefined,
92
- /*cleanupTmpDir=*/ true,
93
- prefilledPublicData,
94
- );
94
+ const ws = await NativeWorldStateService.tmp(/*rollupAddress=*/ undefined, /*cleanupTmpDir=*/ true, genesis);
95
95
 
96
96
  let localProver: ServerCircuitProver;
97
97
  const config = await getEnvironmentConfig(logger);
@@ -104,8 +104,10 @@ export class TestContext {
104
104
  bbBinaryPath: config.expectedBBPath,
105
105
  bbWorkingDirectory: config.bbWorkingDirectory,
106
106
  bbSkipCleanup: config.bbSkipCleanup,
107
- numConcurrentIVCVerifiers: 2,
107
+ numConcurrentIVCVerifiers: 8,
108
108
  bbIVCConcurrency: 1,
109
+ bbChonkVerifyMaxBatch: 16,
110
+ bbChonkVerifyConcurrency: 6,
109
111
  };
110
112
  localProver = await createProver(bbConfig);
111
113
  }
@@ -116,7 +118,7 @@ export class TestContext {
116
118
 
117
119
  const broker = new TestBroker(proverCount, localProver);
118
120
  const facade = new BrokerCircuitProverFacade(broker);
119
- const orchestrator = new TestProvingOrchestrator(ws, facade, EthAddress.ZERO);
121
+ const orchestrator = new TestProvingOrchestrator(ws, facade, EthAddress.ZERO, false, 10);
120
122
 
121
123
  await broker.start();
122
124
  facade.start();
@@ -282,6 +284,7 @@ export class TestContext {
282
284
 
283
285
  return {
284
286
  constants,
287
+ checkpoint,
285
288
  header: checkpoint.header,
286
289
  blocks,
287
290
  l1ToL2Messages,
@@ -204,7 +204,7 @@ export class CheckpointProvingState {
204
204
  Fr.ZERO,
205
205
  NUM_MSGS_PER_BASE_PARITY,
206
206
  );
207
- return new ParityBasePrivateInputs(messages, this.constants.vkTreeRoot);
207
+ return new ParityBasePrivateInputs(messages, this.constants.vkTreeRoot, this.constants.proverId);
208
208
  }
209
209
 
210
210
  public setOutHashHint(hint: OutHashHint) {
@@ -346,4 +346,17 @@ export class CheckpointProvingState {
346
346
  ? [this.blockProofs.getNode(rootLocation)?.provingOutput] // If there's only 1 block, its proof will be stored at the root.
347
347
  : this.blockProofs.getChildren(rootLocation).map(c => c?.provingOutput);
348
348
  }
349
+
350
+ /**
351
+ * Returns the block-level proof outputs that feed into the checkpoint root rollup.
352
+ * Used by `CheckpointSubTreeOrchestrator` to surface its sub-tree result.
353
+ */
354
+ public getSubTreeOutputProofs() {
355
+ return this.#getChildProofsForRoot();
356
+ }
357
+
358
+ /** Sibling path of the archive tree captured before any block in this checkpoint landed. */
359
+ public getLastArchiveSiblingPath() {
360
+ return this.lastArchiveSiblingPath;
361
+ }
349
362
  }
@@ -0,0 +1,271 @@
1
+ import { FinalBlobBatchingChallenges } from '@aztec/blob-lib';
2
+ import type { ARCHIVE_HEIGHT, NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH } from '@aztec/constants';
3
+ import type { EpochNumber } from '@aztec/foundation/branded-types';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
+ import type { LoggerBindings } from '@aztec/foundation/log';
6
+ import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
7
+ import type { Tuple } from '@aztec/foundation/serialize';
8
+ import type { EthAddress } from '@aztec/stdlib/block';
9
+ import type {
10
+ ForkMerkleTreeOperations,
11
+ PublicInputsAndRecursiveProof,
12
+ ReadonlyWorldStateAccess,
13
+ ServerCircuitProver,
14
+ } from '@aztec/stdlib/interfaces/server';
15
+ import type {
16
+ BlockRollupPublicInputs,
17
+ CheckpointConstantData,
18
+ PublicChonkVerifierPublicInputs,
19
+ } from '@aztec/stdlib/rollup';
20
+ import type { BlockHeader, Tx } from '@aztec/stdlib/tx';
21
+ import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
22
+
23
+ import { getPublicChonkVerifierPrivateInputsFromTx } from './block-building-helpers.js';
24
+ import type { BlockProvingState } from './block-proving-state.js';
25
+ import type { CheckpointProvingState } from './checkpoint-proving-state.js';
26
+ import type { EpochProvingContext } from './epoch-proving-context.js';
27
+ import { ProvingOrchestrator } from './orchestrator.js';
28
+
29
+ /**
30
+ * Result of proving a single checkpoint's block-level sub-tree.
31
+ *
32
+ * Contains the final block-rollup proof outputs that feed the checkpoint root rollup,
33
+ * plus the archive sibling path captured before any block in the checkpoint landed
34
+ * (the top-tree needs this to assemble the checkpoint root rollup hints).
35
+ */
36
+ export type SubTreeResult = {
37
+ blockProofOutputs: PublicInputsAndRecursiveProof<
38
+ BlockRollupPublicInputs,
39
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
40
+ >[];
41
+ previousArchiveSiblingPath: Tuple<Fr, typeof ARCHIVE_HEIGHT>;
42
+ };
43
+
44
+ /**
45
+ * Orchestrates block-level proving for a single checkpoint, stopping at the boundary
46
+ * where checkpoint root rollup would otherwise begin.
47
+ *
48
+ * Reuses every circuit driver in `ProvingOrchestrator` (chonk verifier, base, merge,
49
+ * block-root, parity, block-merge) but overrides the gating method that escalates to
50
+ * checkpoint root rollup. Instead of escalating, the orchestrator resolves
51
+ * `getSubTreeResult()` once every block-level proof in the checkpoint's tree is ready.
52
+ *
53
+ * Wiring: a single-checkpoint epoch is created in the constructor (epoch number sourced
54
+ * from the supplied `EpochProvingContext`). The canonical way to obtain a fully-started
55
+ * sub-tree is the `start` static factory, which also drives the single internal
56
+ * `startNewCheckpoint(0, ...)` call. The sub-tree never calls `finalizeEpochStructure`;
57
+ * the override of `checkAndEnqueueCheckpointRootRollup` resolves `getSubTreeResult` once
58
+ * block-level proving completes.
59
+ */
60
+ export class CheckpointSubTreeOrchestrator extends ProvingOrchestrator {
61
+ private readonly subTreeResult: PromiseWithResolvers<SubTreeResult>;
62
+
63
+ constructor(
64
+ dbProvider: ReadonlyWorldStateAccess & ForkMerkleTreeOperations,
65
+ prover: ServerCircuitProver,
66
+ proverId: EthAddress,
67
+ /**
68
+ * Per-epoch shared chonk-verifier proof cache. Every chonk-verifier proof started
69
+ * by this sub-tree lives on the context and survives the sub-tree's cancellation,
70
+ * so a tx whose original checkpoint is reorged out and re-appears in a replacement
71
+ * checkpoint reuses the cached proof. The context's `epochNumber` is the epoch
72
+ * this sub-tree proves into.
73
+ */
74
+ private readonly epochContext: EpochProvingContext,
75
+ cancelJobsOnStop: boolean = false,
76
+ enqueueConcurrency: number,
77
+ telemetryClient: TelemetryClient = getTelemetryClient(),
78
+ bindings?: LoggerBindings,
79
+ ) {
80
+ super(dbProvider, prover, proverId, cancelJobsOnStop, enqueueConcurrency, telemetryClient, bindings);
81
+
82
+ // Single-checkpoint mini-epoch by construction. The total/challenges supplied to
83
+ // `super.startNewEpoch` are never read, because the sub-tree overrides
84
+ // `checkAndEnqueueCheckpointRootRollup` to short-circuit before the parent's
85
+ // checkpoint-root / finalize machinery would consume them.
86
+ super.startNewEpoch(epochContext.epochNumber, 1, FinalBlobBatchingChallenges.empty());
87
+
88
+ this.subTreeResult = promiseWithResolvers<SubTreeResult>();
89
+ // Mark the rejection branch as observed so a `cancel()` or proving failure does not
90
+ // surface an unhandled rejection when no consumer awaits getSubTreeResult().
91
+ this.subTreeResult.promise.catch(() => {});
92
+
93
+ // If the parent's proving state ever rejects, surface the failure on the sub-tree promise.
94
+ void this.provingPromise!.then(result => {
95
+ if (result.status === 'failure') {
96
+ this.subTreeResult.reject(new Error(result.reason));
97
+ }
98
+ });
99
+ }
100
+
101
+ /**
102
+ * Constructs and starts a sub-tree for a single checkpoint. The returned sub-tree
103
+ * has had its single internal `startNewCheckpoint(0, ...)` driven; callers proceed
104
+ * directly to per-block `startNewBlock` / `addTxs` / `setBlockCompleted`.
105
+ *
106
+ * If the internal `startNewCheckpoint` rejects, the partially-constructed sub-tree
107
+ * is stopped before the error propagates, so no broker resources leak.
108
+ */
109
+ public static async start(
110
+ dbProvider: ReadonlyWorldStateAccess & ForkMerkleTreeOperations,
111
+ prover: ServerCircuitProver,
112
+ proverId: EthAddress,
113
+ epochContext: EpochProvingContext,
114
+ cancelJobsOnStop: boolean,
115
+ enqueueConcurrency: number,
116
+ checkpointConstants: CheckpointConstantData,
117
+ l1ToL2Messages: Fr[],
118
+ totalNumBlocks: number,
119
+ headerOfLastBlockInPreviousCheckpoint: BlockHeader,
120
+ telemetryClient: TelemetryClient = getTelemetryClient(),
121
+ bindings?: LoggerBindings,
122
+ ): Promise<CheckpointSubTreeOrchestrator> {
123
+ const subTree = new CheckpointSubTreeOrchestrator(
124
+ dbProvider,
125
+ prover,
126
+ proverId,
127
+ epochContext,
128
+ cancelJobsOnStop,
129
+ enqueueConcurrency,
130
+ telemetryClient,
131
+ bindings,
132
+ );
133
+ try {
134
+ await ProvingOrchestrator.prototype.startNewCheckpoint.call(
135
+ subTree,
136
+ 0,
137
+ checkpointConstants,
138
+ l1ToL2Messages,
139
+ totalNumBlocks,
140
+ headerOfLastBlockInPreviousCheckpoint,
141
+ );
142
+ return subTree;
143
+ } catch (err) {
144
+ await subTree.stop().catch(() => {});
145
+ throw err;
146
+ }
147
+ }
148
+
149
+ /** Returns a promise that resolves when block-level proving completes for the checkpoint. */
150
+ public getSubTreeResult(): Promise<SubTreeResult> {
151
+ return this.subTreeResult.promise;
152
+ }
153
+
154
+ /**
155
+ * The epoch is started in the constructor.
156
+ */
157
+ public override startNewEpoch(
158
+ _epochNumber: EpochNumber,
159
+ _totalNumCheckpoints: number,
160
+ _finalBlobBatchingChallenges: FinalBlobBatchingChallenges,
161
+ ): void {
162
+ throw new Error('CheckpointSubTreeOrchestrator starts its epoch in the constructor; do not call startNewEpoch.');
163
+ }
164
+
165
+ /**
166
+ * The single internal checkpoint is started by the `start` factory
167
+ */
168
+ public override startNewCheckpoint(
169
+ _checkpointIndex: number,
170
+ _constants: CheckpointConstantData,
171
+ _l1ToL2Messages: Fr[],
172
+ _totalNumBlocks: number,
173
+ _headerOfLastBlockInPreviousCheckpoint: BlockHeader,
174
+ ): Promise<void> {
175
+ return Promise.reject(
176
+ new Error(
177
+ 'CheckpointSubTreeOrchestrator drives its single checkpoint in `start`; do not call startNewCheckpoint.',
178
+ ),
179
+ );
180
+ }
181
+
182
+ /**
183
+ * Returns the archive sibling path captured at the internal `startNewCheckpoint`.
184
+ * Available synchronously once `start` has resolved, before block-level proving
185
+ * completes. The top-tree consumer uses this to assemble checkpoint root rollup hints
186
+ * up-front so checkpoint root proofs can pipeline against in-flight sub-tree proving.
187
+ */
188
+ public getPreviousArchiveSiblingPath(): Tuple<Fr, typeof ARCHIVE_HEIGHT> {
189
+ const checkpoint = this.provingState!.getCheckpointProvingState(0);
190
+ if (!checkpoint) {
191
+ throw new Error('Checkpoint not started; call CheckpointSubTreeOrchestrator.start first.');
192
+ }
193
+ return checkpoint.getLastArchiveSiblingPath();
194
+ }
195
+
196
+ /**
197
+ * Override the checkpoint-root boundary: instead of escalating to checkpoint root,
198
+ * resolve the sub-tree promise with the block-level proof outputs once they're all ready.
199
+ */
200
+ // eslint-disable-next-line require-await
201
+ protected override async checkAndEnqueueCheckpointRootRollup(provingState: CheckpointProvingState): Promise<void> {
202
+ const proofs = provingState.getSubTreeOutputProofs();
203
+ const nonEmpty = proofs.filter((p): p is NonNullable<typeof p> => !!p);
204
+ if (proofs.length !== nonEmpty.length) {
205
+ // Block merge tree not fully resolved yet — will be retried as more block proofs land.
206
+ return;
207
+ }
208
+
209
+ this.subTreeResult.resolve({
210
+ blockProofOutputs: nonEmpty,
211
+ previousArchiveSiblingPath: provingState.getLastArchiveSiblingPath(),
212
+ });
213
+ }
214
+
215
+ /**
216
+ * Kickstart chonk-verifier circuits via the shared `EpochProvingContext`. The context
217
+ * owns the broker job lifecycle, so the proof survives this sub-tree's `cancel()` —
218
+ * a tx that ends up in a replacement checkpoint after a reorg can pick the cached
219
+ * promise up and skip re-proving.
220
+ */
221
+ public override startChonkVerifierCircuits(txs: Tx[]): Promise<void> {
222
+ if (!this.provingState?.verifyState()) {
223
+ return Promise.reject(new Error('Sub-tree proving state is not active.'));
224
+ }
225
+ const publicTxs = txs.filter(tx => tx.data.forPublic);
226
+ for (const tx of publicTxs) {
227
+ const txHash = tx.getTxHash().toString();
228
+ const inputs = getPublicChonkVerifierPrivateInputsFromTx(tx, this.getProverId().toField());
229
+ // Fire and forget — getOrEnqueueChonkVerifier later picks up the cached promise
230
+ // when the tx is processed inside its block.
231
+ void this.epochContext.enqueue(txHash, inputs);
232
+ }
233
+ return Promise.resolve();
234
+ }
235
+
236
+ /**
237
+ * Route the tx's chonk-verifier dependency through the per-epoch context: read the
238
+ * cached promise (or enqueue if missing), then `.then(handleResult)` to progress to
239
+ * the base rollup once the proof lands.
240
+ */
241
+ protected override getOrEnqueueChonkVerifier(provingState: BlockProvingState, txIndex: number) {
242
+ if (!provingState.verifyState()) {
243
+ return;
244
+ }
245
+
246
+ const txProvingState = provingState.getTxProvingState(txIndex);
247
+ const txHash = txProvingState.processedTx.hash.toString();
248
+
249
+ const handleResult = (
250
+ result: PublicInputsAndRecursiveProof<
251
+ PublicChonkVerifierPublicInputs,
252
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
253
+ >,
254
+ ) => {
255
+ if (!provingState.verifyState()) {
256
+ return;
257
+ }
258
+ txProvingState.setPublicChonkVerifierProof(result);
259
+ this.checkAndEnqueueBaseRollup(provingState, txIndex);
260
+ };
261
+
262
+ let promise = this.epochContext.getCached(txHash);
263
+ if (!promise) {
264
+ promise = this.epochContext.enqueue(txHash, txProvingState.getPublicChonkVerifierPrivateInputs());
265
+ }
266
+ void promise.then(handleResult).catch(() => {
267
+ // The context self-cleans on rejection; a future call (replacement sub-tree
268
+ // for this tx) will see the miss and re-enqueue. No action needed here.
269
+ });
270
+ }
271
+ }
@@ -0,0 +1,101 @@
1
+ import type { NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH } from '@aztec/constants';
2
+ import type { EpochNumber } from '@aztec/foundation/branded-types';
3
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
4
+ import type { PublicInputsAndRecursiveProof, ServerCircuitProver } from '@aztec/stdlib/interfaces/server';
5
+ import type { PublicChonkVerifierPrivateInputs, PublicChonkVerifierPublicInputs } from '@aztec/stdlib/rollup';
6
+
7
+ /**
8
+ * Result of a chonk-verifier proof, cached per tx hash on `EpochProvingContext`.
9
+ */
10
+ export type ChonkVerifierProofResult = PublicInputsAndRecursiveProof<
11
+ PublicChonkVerifierPublicInputs,
12
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
13
+ >;
14
+
15
+ /**
16
+ * Per-epoch state shared across every `CheckpointSubTreeOrchestrator` constructed for
17
+ * the same epoch. Owns the chonk-verifier proof cache so a tx whose checkpoint is
18
+ * reorged out and re-appears in a replacement checkpoint does not have to re-prove
19
+ * its chonk circuit.
20
+ *
21
+ * The context's chonk-verifier broker jobs are deliberately submitted **outside** the
22
+ * sub-tree's deferred-proving queue. The sub-tree's `cancel()` therefore does not abort
23
+ * them — by design, because their result is tx-scoped, not sub-tree-scoped, and a
24
+ * replacement sub-tree should be able to consume the cached proof.
25
+ *
26
+ * Callers (`EpochProvingJob`, or unit tests) construct one context per epoch and pass
27
+ * it into every sub-tree they create. `stop()` aborts every in-flight chonk job.
28
+ */
29
+ export class EpochProvingContext {
30
+ private readonly cache = new Map<string, Promise<ChonkVerifierProofResult>>();
31
+ /** Abort controllers for in-flight chonk jobs, keyed by tx hash. */
32
+ private readonly pending = new Map<string, AbortController>();
33
+ private readonly log: Logger;
34
+ private stopped = false;
35
+
36
+ constructor(
37
+ private readonly prover: ServerCircuitProver,
38
+ public readonly epochNumber: EpochNumber,
39
+ bindings?: LoggerBindings,
40
+ ) {
41
+ this.log = createLogger('prover-client:epoch-proving-context', bindings);
42
+ }
43
+
44
+ /**
45
+ * Returns the cached chonk-verifier proof promise for the given tx hash, or
46
+ * `undefined` if none has been enqueued yet. Non-mutating.
47
+ */
48
+ public getCached(txHash: string): Promise<ChonkVerifierProofResult> | undefined {
49
+ return this.cache.get(txHash);
50
+ }
51
+
52
+ /**
53
+ * Enqueues a chonk-verifier proof for the given tx hash, returning the promise (or
54
+ * the already-cached one if already enqueued). The promise resolves when the broker
55
+ * delivers the result; on rejection (including `stop()`), the cache entry is removed
56
+ * so a subsequent caller can re-enqueue.
57
+ */
58
+ public enqueue(txHash: string, inputs: PublicChonkVerifierPrivateInputs): Promise<ChonkVerifierProofResult> {
59
+ if (this.stopped) {
60
+ return Promise.reject(new Error('EpochProvingContext is stopped'));
61
+ }
62
+
63
+ const cached = this.cache.get(txHash);
64
+ if (cached) {
65
+ return cached;
66
+ }
67
+
68
+ const controller = new AbortController();
69
+ this.pending.set(txHash, controller);
70
+ this.log.debug(`Enqueueing chonk-verifier circuit`, { txHash, epochNumber: this.epochNumber });
71
+
72
+ const promise = this.prover
73
+ .getPublicChonkVerifierProof(inputs, controller.signal, this.epochNumber)
74
+ .finally(() => this.pending.delete(txHash));
75
+
76
+ // Self-clean on rejection so a future caller can re-enqueue. Mark the rejection
77
+ // path as observed to silence unhandled-rejection warnings when no consumer
78
+ // awaits the promise (e.g. when the only `.then` chain belonged to a cancelled
79
+ // sub-tree's tx-proving state).
80
+ promise.catch(err => {
81
+ this.cache.delete(txHash);
82
+ this.log.debug(`Chonk-verifier proof failed; evicted from cache`, { txHash, error: `${err}` });
83
+ });
84
+
85
+ this.cache.set(txHash, promise);
86
+ return promise;
87
+ }
88
+
89
+ /**
90
+ * Aborts every in-flight chonk-verifier broker job and clears the cache. Called by
91
+ * the owning `EpochProvingJob` when the job stops.
92
+ */
93
+ public stop() {
94
+ this.stopped = true;
95
+ for (const controller of this.pending.values()) {
96
+ controller.abort();
97
+ }
98
+ this.pending.clear();
99
+ this.cache.clear();
100
+ }
101
+ }
@@ -1 +1,9 @@
1
1
  export { ProvingOrchestrator } from './orchestrator.js';
2
+ export { CheckpointSubTreeOrchestrator, type SubTreeResult } from './checkpoint-sub-tree-orchestrator.js';
3
+ export { EpochProvingContext, type ChonkVerifierProofResult } from './epoch-proving-context.js';
4
+ export {
5
+ TopTreeOrchestrator,
6
+ TopTreeCancelledError,
7
+ type CheckpointTopTreeData,
8
+ type TopTreeResult,
9
+ } from './top-tree-orchestrator.js';