@aztec/prover-client 5.0.0-private.20260319 → 5.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/dest/config.d.ts +1 -1
  2. package/dest/config.d.ts.map +1 -1
  3. package/dest/config.js +16 -2
  4. package/dest/light/lightweight_checkpoint_builder.d.ts +1 -1
  5. package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -1
  6. package/dest/light/lightweight_checkpoint_builder.js +15 -5
  7. package/dest/mocks/test_context.d.ts +11 -11
  8. package/dest/mocks/test_context.d.ts.map +1 -1
  9. package/dest/mocks/test_context.js +25 -28
  10. package/dest/orchestrator/block-building-helpers.d.ts +1 -1
  11. package/dest/orchestrator/checkpoint-proving-state.d.ts +20 -36
  12. package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
  13. package/dest/orchestrator/checkpoint-proving-state.js +22 -120
  14. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts +161 -0
  15. package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts.map +1 -0
  16. package/dest/orchestrator/{orchestrator.js → checkpoint-sub-tree-orchestrator.js} +267 -451
  17. package/dest/orchestrator/chonk-cache.d.ts +39 -0
  18. package/dest/orchestrator/chonk-cache.d.ts.map +1 -0
  19. package/dest/orchestrator/chonk-cache.js +80 -0
  20. package/dest/orchestrator/index.d.ts +4 -2
  21. package/dest/orchestrator/index.d.ts.map +1 -1
  22. package/dest/orchestrator/index.js +3 -1
  23. package/dest/orchestrator/proving-scheduler.d.ts +81 -0
  24. package/dest/orchestrator/proving-scheduler.d.ts.map +1 -0
  25. package/dest/orchestrator/proving-scheduler.js +120 -0
  26. package/dest/orchestrator/top-tree-orchestrator.d.ts +88 -0
  27. package/dest/orchestrator/top-tree-orchestrator.d.ts.map +1 -0
  28. package/dest/orchestrator/top-tree-orchestrator.js +232 -0
  29. package/dest/orchestrator/top-tree-proving-state.d.ts +61 -0
  30. package/dest/orchestrator/top-tree-proving-state.d.ts.map +1 -0
  31. package/dest/orchestrator/top-tree-proving-state.js +185 -0
  32. package/dest/prover-client/prover-client.d.ts +61 -4
  33. package/dest/prover-client/prover-client.d.ts.map +1 -1
  34. package/dest/prover-client/prover-client.js +54 -7
  35. package/dest/proving_broker/broker_prover_facade.d.ts +1 -1
  36. package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
  37. package/dest/proving_broker/broker_prover_facade.js +10 -16
  38. package/dest/proving_broker/config.d.ts +8 -72
  39. package/dest/proving_broker/config.d.ts.map +1 -1
  40. package/dest/proving_broker/config.js +2 -2
  41. package/dest/proving_broker/index.d.ts +2 -1
  42. package/dest/proving_broker/index.d.ts.map +1 -1
  43. package/dest/proving_broker/index.js +1 -0
  44. package/dest/proving_broker/proving_broker.d.ts +2 -2
  45. package/dest/proving_broker/proving_broker.d.ts.map +1 -1
  46. package/dest/proving_broker/proving_broker.js +32 -7
  47. package/dest/proving_broker/proving_broker_database/persisted.js +2 -2
  48. package/dest/proving_broker/rpc.d.ts +3 -1
  49. package/dest/proving_broker/rpc.d.ts.map +1 -1
  50. package/dest/proving_broker/rpc.js +80 -24
  51. package/dest/test/epoch_settlement.d.ts +36 -0
  52. package/dest/test/epoch_settlement.d.ts.map +1 -0
  53. package/dest/test/epoch_settlement.js +52 -0
  54. package/dest/test/index.d.ts +2 -0
  55. package/dest/test/index.d.ts.map +1 -0
  56. package/dest/test/index.js +1 -0
  57. package/dest/test/mock_prover.d.ts +3 -3
  58. package/package.json +19 -18
  59. package/src/config.ts +18 -2
  60. package/src/light/lightweight_checkpoint_builder.ts +16 -6
  61. package/src/mocks/test_context.ts +24 -41
  62. package/src/orchestrator/checkpoint-proving-state.ts +21 -162
  63. package/src/orchestrator/{orchestrator.ts → checkpoint-sub-tree-orchestrator.ts} +415 -661
  64. package/src/orchestrator/chonk-cache.ts +99 -0
  65. package/src/orchestrator/index.ts +8 -1
  66. package/src/orchestrator/proving-scheduler.ts +160 -0
  67. package/src/orchestrator/top-tree-orchestrator.ts +384 -0
  68. package/src/orchestrator/top-tree-proving-state.ts +219 -0
  69. package/src/prover-client/prover-client.ts +128 -18
  70. package/src/proving_broker/broker_prover_facade.ts +8 -16
  71. package/src/proving_broker/config.ts +2 -1
  72. package/src/proving_broker/index.ts +1 -0
  73. package/src/proving_broker/proving_broker.ts +27 -5
  74. package/src/proving_broker/proving_broker_database/persisted.ts +2 -2
  75. package/src/proving_broker/rpc.ts +36 -24
  76. package/src/test/epoch_settlement.ts +82 -0
  77. package/src/test/index.ts +1 -0
  78. package/dest/orchestrator/epoch-proving-state.d.ts +0 -75
  79. package/dest/orchestrator/epoch-proving-state.d.ts.map +0 -1
  80. package/dest/orchestrator/epoch-proving-state.js +0 -269
  81. package/dest/orchestrator/orchestrator.d.ts +0 -130
  82. package/dest/orchestrator/orchestrator.d.ts.map +0 -1
  83. package/dest/prover-client/server-epoch-prover.d.ts +0 -32
  84. package/dest/prover-client/server-epoch-prover.d.ts.map +0 -1
  85. package/dest/prover-client/server-epoch-prover.js +0 -40
  86. package/src/orchestrator/epoch-proving-state.ts +0 -380
  87. package/src/prover-client/server-epoch-prover.ts +0 -69
@@ -1,45 +1,38 @@
1
- import { BatchedBlob, FinalBlobBatchingChallenges, SpongeBlob } from '@aztec/blob-lib/types';
1
+ import type { SpongeBlob } from '@aztec/blob-lib/types';
2
2
  import {
3
+ type ARCHIVE_HEIGHT,
3
4
  L1_TO_L2_MSG_SUBTREE_HEIGHT,
4
5
  L1_TO_L2_MSG_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
5
6
  NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
6
- NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
7
7
  NUM_BASE_PARITY_PER_ROOT_PARITY,
8
8
  } from '@aztec/constants';
9
- import { BlockNumber, EpochNumber } from '@aztec/foundation/branded-types';
10
- import { padArrayEnd } from '@aztec/foundation/collection';
9
+ import { BlockNumber, type EpochNumber } from '@aztec/foundation/branded-types';
11
10
  import { Fr } from '@aztec/foundation/curves/bn254';
12
11
  import { AbortError } from '@aztec/foundation/error';
13
- import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
14
- import { promiseWithResolvers } from '@aztec/foundation/promise';
15
- import { SerialQueue } from '@aztec/foundation/queue';
16
- import { assertLength } from '@aztec/foundation/serialize';
17
- import { sleep } from '@aztec/foundation/sleep';
18
- import { pushTestData } from '@aztec/foundation/testing';
19
- import { elapsed } from '@aztec/foundation/timer';
12
+ import type { LoggerBindings } from '@aztec/foundation/log';
13
+ import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
14
+ import type { SerialQueue } from '@aztec/foundation/queue';
15
+ import { type Tuple, assertLength } from '@aztec/foundation/serialize';
20
16
  import type { TreeNodeLocation } from '@aztec/foundation/trees';
21
17
  import { EthAddress } from '@aztec/stdlib/block';
22
18
  import type {
23
- EpochProver,
24
19
  ForkMerkleTreeOperations,
25
20
  MerkleTreeWriteOperations,
26
21
  PublicInputsAndRecursiveProof,
27
22
  ReadonlyWorldStateAccess,
28
23
  ServerCircuitProver,
29
24
  } from '@aztec/stdlib/interfaces/server';
30
- import type { Proof } from '@aztec/stdlib/proofs';
25
+ import { appendL1ToL2MessagesToTree } from '@aztec/stdlib/messaging';
31
26
  import {
32
27
  type BaseRollupHints,
28
+ type BlockRollupPublicInputs,
33
29
  BlockRootEmptyTxFirstRollupPrivateInputs,
34
30
  BlockRootFirstRollupPrivateInputs,
35
31
  BlockRootSingleTxFirstRollupPrivateInputs,
36
32
  BlockRootSingleTxRollupPrivateInputs,
37
33
  CheckpointConstantData,
38
- CheckpointRootSingleBlockRollupPrivateInputs,
39
34
  PrivateTxBaseRollupPrivateInputs,
40
- PublicChonkVerifierPrivateInputs,
41
- PublicChonkVerifierPublicInputs,
42
- RootRollupPublicInputs,
35
+ type PublicChonkVerifierPublicInputs,
43
36
  } from '@aztec/stdlib/rollup';
44
37
  import type { CircuitName } from '@aztec/stdlib/stats';
45
38
  import { type AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
@@ -68,51 +61,90 @@ import {
68
61
  validateTx,
69
62
  } from './block-building-helpers.js';
70
63
  import type { BlockProvingState } from './block-proving-state.js';
71
- import type { CheckpointProvingState } from './checkpoint-proving-state.js';
72
- import { EpochProvingState, type ProvingResult, type TreeSnapshots } from './epoch-proving-state.js';
64
+ import { CheckpointProvingState } from './checkpoint-proving-state.js';
65
+ import type { ChonkCache } from './chonk-cache.js';
73
66
  import { ProvingOrchestratorMetrics } from './orchestrator_metrics.js';
67
+ import { ProvingScheduler } from './proving-scheduler.js';
74
68
  import { TxProvingState } from './tx-proving-state.js';
75
69
 
76
70
  /**
77
- * Implements an event driven proving scheduler to build the recursive proof tree. The idea being:
78
- * 1. Transactions are provided to the scheduler post simulation.
79
- * 2. Tree insertions are performed as required to generate transaction specific proofs
80
- * 3. Those transaction specific proofs are generated in the necessary order accounting for dependencies
81
- * 4. Once a transaction is proven, it will be incorporated into a merge proof
82
- * 5. Merge proofs are produced at each level of the tree until the root proof is produced
71
+ * Result of proving a single checkpoint's block-level sub-tree.
83
72
  *
84
- * The proving implementation is determined by the provided prover. This could be for example a local prover or a remote prover pool.
73
+ * Contains the final block-rollup proof outputs that feed the checkpoint root rollup,
74
+ * plus the archive sibling path captured before any block in the checkpoint landed
75
+ * (the top-tree needs this to assemble the checkpoint root rollup hints).
85
76
  */
77
+ export type SubTreeResult = {
78
+ blockProofOutputs: PublicInputsAndRecursiveProof<
79
+ BlockRollupPublicInputs,
80
+ typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
81
+ >[];
82
+ previousArchiveSiblingPath: Tuple<Fr, typeof ARCHIVE_HEIGHT>;
83
+ };
84
+
85
+ type TreeSnapshots = Map<MerkleTreeId, AppendOnlyTreeSnapshot>;
86
86
 
87
87
  /**
88
- * The orchestrator, managing the flow of recursive proving operations required to build the rollup proof tree.
88
+ * Base rollup hints as produced before proving: `PrivateBaseRollupHints` / `PublicBaseRollupHints`
89
+ * deliberately carry no recursive proof or verification key. The proof + VK are supplied later, when
90
+ * `TxProvingState.getBaseRollupTypeAndInputs` wraps these hints into the "with proof + VK" types —
91
+ * `PrivateTxBaseRollupPrivateInputs` (a `ChonkProofData`) or `PublicTxBaseRollupPrivateInputs` (a
92
+ * chonk-verifier proof + AVM proof). Those proofs are *required constructor arguments* of the wrapper
93
+ * types, so the only way to obtain a provable input is to populate them — they cannot be silently
94
+ * omitted. Naming the proof-less hints type here makes that boundary explicit at `prepareBaseRollupInputs`.
89
95
  */
90
- export class ProvingOrchestrator implements EpochProver {
91
- private provingState: EpochProvingState | undefined = undefined;
92
- private pendingProvingJobs: AbortController[] = [];
96
+ type BaseRollupHintsWithoutProofAndVK = BaseRollupHints;
93
97
 
94
- private provingPromise: Promise<ProvingResult> | undefined = undefined;
95
- private metrics: ProvingOrchestratorMetrics;
96
- // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
98
+ /**
99
+ * Orchestrates block-level proving for a single checkpoint, stopping at the boundary
100
+ * where checkpoint root rollup would otherwise begin. Used by the per-checkpoint
101
+ * `CheckpointProver` in production; the top-tree orchestrator then composes the
102
+ * sub-tree's block proofs into the epoch proof.
103
+ *
104
+ * Wiring: a single-checkpoint mini-proving session is owned by the constructor. The
105
+ * canonical way to obtain a fully-started sub-tree is the `start` static factory,
106
+ * which also drives the single internal `startCheckpoint` call. The sub-tree never
107
+ * escalates past the checkpoint root boundary; `getSubTreeResult()` resolves once
108
+ * every block-level proof in the checkpoint's tree is ready.
109
+ */
110
+ export class CheckpointSubTreeOrchestrator extends ProvingScheduler {
111
+ /** The single checkpoint proving state this sub-tree owns. Allocated in the `start` factory. */
112
+ protected provingState: CheckpointProvingState | undefined = undefined;
113
+ private readonly subTreeResult: PromiseWithResolvers<SubTreeResult>;
114
+ private readonly metrics: ProvingOrchestratorMetrics;
97
115
  private dbs: Map<BlockNumber, MerkleTreeWriteOperations> = new Map();
98
- private logger: Logger;
99
- private deferredJobQueue = new SerialQueue();
100
116
 
101
117
  constructor(
102
- private dbProvider: ReadonlyWorldStateAccess & ForkMerkleTreeOperations,
103
- private prover: ServerCircuitProver,
118
+ private readonly dbProvider: ReadonlyWorldStateAccess & ForkMerkleTreeOperations,
119
+ protected readonly prover: ServerCircuitProver,
104
120
  private readonly proverId: EthAddress,
121
+ /**
122
+ * Shared chonk-verifier proof cache. Every chonk-verifier proof started by this
123
+ * sub-tree lives on the cache and survives the sub-tree's cancellation, so a tx
124
+ * whose original checkpoint is reorged out and re-appears in a replacement
125
+ * checkpoint reuses the cached proof.
126
+ */
127
+ private readonly chonkCache: ChonkCache,
128
+ /** The epoch this sub-tree proves into. */
129
+ private readonly epochNumber: EpochNumber,
105
130
  private readonly cancelJobsOnStop: boolean = false,
106
- private readonly enqueueConcurrency: number,
131
+ deferredJobQueue: SerialQueue,
107
132
  telemetryClient: TelemetryClient = getTelemetryClient(),
108
133
  bindings?: LoggerBindings,
109
134
  ) {
110
- this.logger = createLogger('prover-client:orchestrator', bindings);
111
- this.metrics = new ProvingOrchestratorMetrics(telemetryClient, 'ProvingOrchestrator');
112
- this.deferredJobQueue.start(this.enqueueConcurrency);
135
+ super(deferredJobQueue, 'prover-client:checkpoint-sub-tree-orchestrator', bindings);
136
+ this.metrics = new ProvingOrchestratorMetrics(telemetryClient, 'CheckpointSubTreeOrchestrator');
137
+
138
+ this.subTreeResult = promiseWithResolvers<SubTreeResult>();
139
+ // Mark the rejection branch as observed so a `cancel()` or proving failure does not
140
+ // surface an unhandled rejection when no consumer awaits getSubTreeResult().
141
+ this.subTreeResult.promise.catch(() => {});
113
142
  }
114
143
 
115
- get tracer(): Tracer {
144
+ /** Tracks whether `cancel()` has been called; flows into the checkpoint state's isAlive hook. */
145
+ private cancelled = false;
146
+
147
+ public get tracer(): Tracer {
116
148
  return this.metrics.tracer;
117
149
  }
118
150
 
@@ -120,127 +152,125 @@ export class ProvingOrchestrator implements EpochProver {
120
152
  return this.proverId;
121
153
  }
122
154
 
123
- public getNumActiveForks() {
155
+ public getNumActiveForks(): number {
124
156
  return this.dbs.size;
125
157
  }
126
158
 
127
- public async stop(): Promise<void> {
128
- // Grab the old queue before cancel() replaces it, so we can await its draining.
129
- const oldQueue = this.deferredJobQueue;
130
- this.cancel();
131
- await oldQueue.cancel();
159
+ /** Returns a promise that resolves when block-level proving completes for the checkpoint. */
160
+ public getSubTreeResult(): Promise<SubTreeResult> {
161
+ return this.subTreeResult.promise;
132
162
  }
133
163
 
134
- public startNewEpoch(
135
- epochNumber: EpochNumber,
136
- totalNumCheckpoints: number,
137
- finalBlobBatchingChallenges: FinalBlobBatchingChallenges,
138
- ) {
139
- if (this.provingState?.verifyState()) {
140
- throw new Error(
141
- `Cannot start epoch ${epochNumber} when epoch ${this.provingState.epochNumber} is still being processed.`,
142
- );
164
+ /**
165
+ * Returns the archive sibling path captured at the internal checkpoint start.
166
+ * Available synchronously once `start` has resolved, before block-level proving
167
+ * completes. The top-tree consumer uses this to assemble checkpoint root rollup hints
168
+ * up-front so checkpoint root proofs can pipeline against in-flight sub-tree proving.
169
+ */
170
+ public getPreviousArchiveSiblingPath(): Tuple<Fr, typeof ARCHIVE_HEIGHT> {
171
+ if (!this.provingState) {
172
+ throw new Error('Checkpoint not started; call CheckpointSubTreeOrchestrator.start first.');
143
173
  }
144
-
145
- const { promise: _promise, resolve, reject } = promiseWithResolvers<ProvingResult>();
146
- const promise = _promise.catch((reason): ProvingResult => ({ status: 'failure', reason }));
147
- this.logger.info(`Starting epoch ${epochNumber} with ${totalNumCheckpoints} checkpoints.`);
148
- this.provingState = new EpochProvingState(
149
- epochNumber,
150
- totalNumCheckpoints,
151
- finalBlobBatchingChallenges,
152
- provingState => this.checkAndEnqueueCheckpointRootRollup(provingState),
153
- resolve,
154
- reject,
155
- );
156
- this.provingPromise = promise;
174
+ return this.provingState.getLastArchiveSiblingPath();
157
175
  }
158
176
 
159
177
  /**
160
- * Starts a new checkpoint.
161
- * @param checkpointIndex - The index of the checkpoint in the epoch.
162
- * @param constants - The constants for this checkpoint.
163
- * @param l1ToL2Messages - The set of L1 to L2 messages to be inserted at the beginning of this checkpoint.
164
- * @param totalNumBlocks - The total number of blocks expected in the checkpoint (must be at least one).
165
- * @param headerOfLastBlockInPreviousCheckpoint - The header of the last block in the previous checkpoint.
178
+ * Constructs and starts a sub-tree for a single checkpoint. The returned sub-tree
179
+ * has had its single internal checkpoint state allocated; callers proceed directly
180
+ * to per-block `startNewBlock` / `addTxs` / `setBlockCompleted`.
181
+ *
182
+ * If the internal start rejects, the partially-constructed sub-tree is stopped
183
+ * before the error propagates, so no broker resources leak.
166
184
  */
167
- public async startNewCheckpoint(
168
- checkpointIndex: number,
169
- constants: CheckpointConstantData,
185
+ public static async start(
186
+ dbProvider: ReadonlyWorldStateAccess & ForkMerkleTreeOperations,
187
+ prover: ServerCircuitProver,
188
+ proverId: EthAddress,
189
+ chonkCache: ChonkCache,
190
+ epochNumber: EpochNumber,
191
+ cancelJobsOnStop: boolean,
192
+ deferredJobQueue: SerialQueue,
193
+ checkpointConstants: CheckpointConstantData,
170
194
  l1ToL2Messages: Fr[],
171
195
  totalNumBlocks: number,
172
196
  headerOfLastBlockInPreviousCheckpoint: BlockHeader,
173
- ) {
174
- if (!this.provingState) {
175
- throw new Error('Empty epoch proving state. Call startNewEpoch before starting a checkpoint.');
197
+ telemetryClient: TelemetryClient = getTelemetryClient(),
198
+ bindings?: LoggerBindings,
199
+ ): Promise<CheckpointSubTreeOrchestrator> {
200
+ const subTree = new CheckpointSubTreeOrchestrator(
201
+ dbProvider,
202
+ prover,
203
+ proverId,
204
+ chonkCache,
205
+ epochNumber,
206
+ cancelJobsOnStop,
207
+ deferredJobQueue,
208
+ telemetryClient,
209
+ bindings,
210
+ );
211
+ try {
212
+ await subTree.startCheckpoint(
213
+ checkpointConstants,
214
+ l1ToL2Messages,
215
+ totalNumBlocks,
216
+ headerOfLastBlockInPreviousCheckpoint,
217
+ );
218
+ return subTree;
219
+ } catch (err) {
220
+ await subTree.stop().catch(() => {});
221
+ throw err;
176
222
  }
223
+ }
177
224
 
178
- if (!this.provingState.isAcceptingCheckpoints()) {
179
- throw new Error(`Epoch not accepting further checkpoints.`);
225
+ /**
226
+ * Kickstart chonk-verifier circuits via the shared `ChonkCache`. The cache owns the
227
+ * broker job lifecycle, so the proof survives this sub-tree's `cancel()` — a tx that
228
+ * ends up in a replacement checkpoint after a reorg can pick the cached promise up
229
+ * and skip re-proving.
230
+ */
231
+ public startChonkVerifierCircuits(txs: Tx[]): Promise<void> {
232
+ if (!this.provingState?.verifyState()) {
233
+ return Promise.reject(new Error('Sub-tree proving state is not active.'));
180
234
  }
181
-
182
- // Fork world state at the end of the immediately previous block.
183
- const lastBlockNumber = headerOfLastBlockInPreviousCheckpoint.globalVariables.blockNumber;
184
- const db = await this.dbProvider.fork(lastBlockNumber);
185
-
186
- const firstBlockNumber = BlockNumber(lastBlockNumber + 1);
187
- this.dbs.set(firstBlockNumber, db);
188
-
189
- // Get archive sibling path before any block in this checkpoint lands.
190
- const lastArchiveSiblingPath = await getLastSiblingPath(MerkleTreeId.ARCHIVE, db);
191
-
192
- // Insert all the l1 to l2 messages into the db. And get the states before and after the insertion.
193
- const {
194
- lastL1ToL2MessageTreeSnapshot,
195
- lastL1ToL2MessageSubtreeRootSiblingPath,
196
- newL1ToL2MessageTreeSnapshot,
197
- newL1ToL2MessageSubtreeRootSiblingPath,
198
- } = await this.updateL1ToL2MessageTree(l1ToL2Messages, db);
199
-
200
- this.provingState.startNewCheckpoint(
201
- checkpointIndex,
202
- constants,
203
- totalNumBlocks,
204
- headerOfLastBlockInPreviousCheckpoint,
205
- lastArchiveSiblingPath,
206
- l1ToL2Messages,
207
- lastL1ToL2MessageTreeSnapshot,
208
- lastL1ToL2MessageSubtreeRootSiblingPath,
209
- newL1ToL2MessageTreeSnapshot,
210
- newL1ToL2MessageSubtreeRootSiblingPath,
211
- );
235
+ const publicTxs = txs.filter(tx => tx.data.forPublic);
236
+ for (const tx of publicTxs) {
237
+ const txHash = tx.getTxHash().toString();
238
+ const inputs = getPublicChonkVerifierPrivateInputsFromTx(tx, this.getProverId().toField());
239
+ // Fire and forget — getOrEnqueueChonkVerifier later picks up the cached promise
240
+ // when the tx is processed inside its block.
241
+ void this.chonkCache.getOrCompute(txHash, signal =>
242
+ this.prover.getPublicChonkVerifierProof(inputs, signal, this.epochNumber),
243
+ );
244
+ }
245
+ return Promise.resolve();
212
246
  }
213
247
 
248
+ // ---------------- per-block driving (called by the per-checkpoint CheckpointProver) ----------------
249
+
214
250
  /**
215
- * Starts off a new block
251
+ * Starts off a new block.
216
252
  * @param blockNumber - The block number
217
- * @param timestamp - The timestamp of the block. This is only required for constructing the private inputs for the
218
- * block that doesn't have any txs.
219
- * @param totalNumTxs - The total number of txs in the block
253
+ * @param timestamp - The timestamp of the block. Required for empty blocks to construct private inputs.
254
+ * @param totalNumTxs - The total number of txs in the block.
220
255
  */
221
- @trackSpan('ProvingOrchestrator.startNewBlock', blockNumber => ({
256
+ @trackSpan('CheckpointSubTreeOrchestrator.startNewBlock', blockNumber => ({
222
257
  [Attributes.BLOCK_NUMBER]: blockNumber,
223
258
  }))
224
259
  public async startNewBlock(blockNumber: BlockNumber, timestamp: UInt64, totalNumTxs: number) {
225
260
  if (!this.provingState) {
226
- throw new Error('Empty epoch proving state. Call startNewEpoch before starting a block.');
227
- }
228
-
229
- const checkpointProvingState = this.provingState.getCheckpointProvingStateByBlockNumber(blockNumber);
230
- if (!checkpointProvingState) {
231
- throw new Error(`Checkpoint not started. Call startNewCheckpoint first.`);
261
+ throw new Error('Empty proving state. The checkpoint sub-tree has not been started.');
232
262
  }
233
263
 
234
- if (!checkpointProvingState.isAcceptingBlocks()) {
264
+ if (!this.provingState.isAcceptingBlocks()) {
235
265
  throw new Error(`Checkpoint not accepting further blocks.`);
236
266
  }
237
267
 
238
- const constants = checkpointProvingState.constants;
268
+ const constants = this.provingState.constants;
239
269
  this.logger.info(`Starting block ${blockNumber} for slot ${constants.slotNumber}.`);
240
270
 
241
- // Fork the db only when it's not already set. The db for the first block is set in `startNewCheckpoint`.
271
+ // Fork the db only when it's not already set. The db for the first block is set in startCheckpoint.
242
272
  if (!this.dbs.has(blockNumber)) {
243
- // Fork world state at the end of the immediately previous block
273
+ // Fork world state at the end of the immediately previous block.
244
274
  const db = await this.dbProvider.fork(BlockNumber(blockNumber - 1));
245
275
  this.dbs.set(blockNumber, db);
246
276
  }
@@ -250,7 +280,7 @@ export class ProvingOrchestrator implements EpochProver {
250
280
  const lastArchiveTreeSnapshot = await getTreeSnapshot(MerkleTreeId.ARCHIVE, db);
251
281
  const lastArchiveSiblingPath = await getRootTreeSiblingPath(MerkleTreeId.ARCHIVE, db);
252
282
 
253
- const blockProvingState = checkpointProvingState.startNewBlock(
283
+ const blockProvingState = this.provingState.startNewBlock(
254
284
  blockNumber,
255
285
  timestamp,
256
286
  totalNumTxs,
@@ -261,12 +291,12 @@ export class ProvingOrchestrator implements EpochProver {
261
291
  // Enqueue base parity circuits for the first block in the checkpoint.
262
292
  if (blockProvingState.index === 0) {
263
293
  for (let i = 0; i < NUM_BASE_PARITY_PER_ROOT_PARITY; i++) {
264
- this.enqueueBaseParityCircuit(checkpointProvingState, blockProvingState, i);
294
+ this.enqueueBaseParityCircuit(this.provingState, blockProvingState, i);
265
295
  }
266
296
  }
267
297
 
268
- // Because `addTxs` won't be called for a block without txs, and that's where the sponge blob state is computed.
269
- // We need to set its end sponge blob here, which will become the start sponge blob for the next block.
298
+ // Because `addTxs` won't be called for a block without txs, and that's where the sponge blob state is computed,
299
+ // set its end sponge blob here. This becomes the start sponge blob for the next block.
270
300
  if (totalNumTxs === 0) {
271
301
  const endState = await db.getStateReference();
272
302
  blockProvingState.setEndState(endState);
@@ -275,29 +305,25 @@ export class ProvingOrchestrator implements EpochProver {
275
305
  const blockEndBlobFields = blockProvingState.getBlockEndBlobFields();
276
306
  await endSpongeBlob.absorb(blockEndBlobFields);
277
307
  blockProvingState.setEndSpongeBlob(endSpongeBlob);
278
-
279
- // Try to accumulate the out hashes and blobs as far as we can:
280
- await this.provingState.accumulateCheckpointOutHashes();
281
- await this.provingState.setBlobAccumulators();
282
308
  }
283
309
  }
284
310
 
285
311
  /**
286
- * The interface to add simulated transactions to the scheduler. This can only be called once per block.
312
+ * The interface to add simulated transactions to the scheduler. Called at most once per block.
287
313
  * @param txs - The transactions to be proven
288
314
  */
289
- @trackSpan('ProvingOrchestrator.addTxs', txs => ({
315
+ @trackSpan('CheckpointSubTreeOrchestrator.addTxs', txs => ({
290
316
  [Attributes.BLOCK_TXS_COUNT]: txs.length,
291
317
  }))
292
318
  public async addTxs(txs: ProcessedTx[]): Promise<void> {
293
319
  if (!this.provingState) {
294
- throw new Error(`Empty epoch proving state. Call startNewEpoch before adding txs.`);
320
+ throw new Error(`Empty proving state. The checkpoint sub-tree has not been started.`);
295
321
  }
296
322
 
297
323
  if (!txs.length) {
298
- // To avoid an ugly throw below. If we require an empty block, we can just call setBlockCompleted
299
- // on a block with no txs. We cannot do that here because we cannot find the blockNumber without any txs.
300
- this.logger.warn(`Provided no txs to orchestrator addTxs.`);
324
+ // Empty block: setBlockCompleted handles this without addTxs being called. Bail to
325
+ // avoid the throw below (we cannot find the blockNumber without any txs).
326
+ this.logger.warn(`Provided no txs to addTxs.`);
301
327
  return;
302
328
  }
303
329
 
@@ -373,46 +399,13 @@ export class ProvingOrchestrator implements EpochProver {
373
399
  await spongeBlobState.absorb(blockEndBlobFields);
374
400
 
375
401
  provingState.setEndSpongeBlob(spongeBlobState);
376
-
377
- // Txs have been added to the block. Now try to accumulate the out hashes and blobs as far as we can:
378
- await this.provingState.accumulateCheckpointOutHashes();
379
- await this.provingState.setBlobAccumulators();
380
- }
381
-
382
- /**
383
- * Kickstarts chonk verifier circuits for the specified txs. These will be used during epoch proving.
384
- * Note that if the chonk verifier circuits are not started this way, they will be started nontheless after processing.
385
- */
386
- @trackSpan('ProvingOrchestrator.startChonkVerifierCircuits')
387
- public startChonkVerifierCircuits(txs: Tx[]) {
388
- if (!this.provingState?.verifyState()) {
389
- throw new Error(`Empty epoch proving state. call startNewEpoch before starting chonk verifier circuits.`);
390
- }
391
- const publicTxs = txs.filter(tx => tx.data.forPublic);
392
- for (const tx of publicTxs) {
393
- const txHash = tx.getTxHash().toString();
394
- const privateInputs = getPublicChonkVerifierPrivateInputsFromTx(tx, this.proverId.toField());
395
- const tubeProof =
396
- promiseWithResolvers<
397
- PublicInputsAndRecursiveProof<
398
- PublicChonkVerifierPublicInputs,
399
- typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
400
- >
401
- >();
402
- this.logger.debug(`Starting chonk verifier circuit for tx ${txHash}`);
403
- this.doEnqueueChonkVerifier(txHash, privateInputs, proof => {
404
- tubeProof.resolve(proof);
405
- });
406
- this.provingState.cachedChonkVerifierProofs.set(txHash, tubeProof.promise);
407
- }
408
- return Promise.resolve();
409
402
  }
410
403
 
411
404
  /**
412
405
  * Marks the block as completed.
413
406
  * Computes the block header and updates the archive tree.
414
407
  */
415
- @trackSpan('ProvingOrchestrator.setBlockCompleted', (blockNumber: BlockNumber) => ({
408
+ @trackSpan('CheckpointSubTreeOrchestrator.setBlockCompleted', (blockNumber: BlockNumber) => ({
416
409
  [Attributes.BLOCK_NUMBER]: blockNumber,
417
410
  }))
418
411
  public async setBlockCompleted(blockNumber: BlockNumber, expectedHeader?: BlockHeader): Promise<BlockHeader> {
@@ -467,71 +460,17 @@ export class ProvingOrchestrator implements EpochProver {
467
460
  return header;
468
461
  }
469
462
 
470
- // Flagged as protected to disable in certain unit tests
471
- protected async verifyBuiltBlockAgainstSyncedState(provingState: BlockProvingState) {
472
- const builtBlockHeader = provingState.getBuiltBlockHeader();
473
- if (!builtBlockHeader) {
474
- this.logger.debug('Block header not built yet, skipping header check.');
475
- return;
476
- }
477
-
478
- const output = provingState.getBlockRootRollupOutput();
479
- if (!output) {
480
- this.logger.debug('Block root rollup proof not built yet, skipping header check.');
481
- return;
482
- }
483
-
484
- const newArchive = provingState.getBuiltArchive();
485
- if (!newArchive) {
486
- this.logger.debug('Archive snapshot not yet captured, skipping header check.');
487
- return;
488
- }
489
-
490
- const header = await buildHeaderFromCircuitOutputs(output);
491
-
492
- if (!(await header.hash()).equals(await builtBlockHeader.hash())) {
493
- this.logger.error(`Block header mismatch.\nCircuit: ${inspect(header)}\nComputed: ${inspect(builtBlockHeader)}`);
494
- provingState.reject(`Block header hash mismatch.`);
495
- return;
496
- }
497
-
498
- const blockNumber = provingState.blockNumber;
499
- const syncedArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, this.dbProvider.getSnapshot(blockNumber));
500
- if (!syncedArchive.equals(newArchive)) {
501
- this.logger.error(
502
- `Archive tree mismatch for block ${blockNumber}: world state synced to ${inspect(
503
- syncedArchive,
504
- )} but built ${inspect(newArchive)}`,
505
- );
506
- provingState.reject(`Archive tree mismatch.`);
507
- return;
508
- }
509
-
510
- const circuitArchive = output.newArchive;
511
- if (!newArchive.equals(circuitArchive)) {
512
- this.logger.error(`New archive mismatch.\nCircuit: ${output.newArchive}\nComputed: ${newArchive}`);
513
- provingState.reject(`New archive mismatch.`);
514
- return;
515
- }
516
- }
463
+ // ---------------- lifecycle ----------------
517
464
 
518
465
  /**
519
- * Cancel any further proving.
520
- * If cancelJobsOnStop is true, aborts all pending jobs with the broker (which marks them as 'Aborted').
521
- * If cancelJobsOnStop is false (default), jobs remain in the broker queue and can be reused on restart/reorg.
466
+ * Cancels any further proving. If `cancelJobsOnStop` was set, aborts all pending broker jobs
467
+ * (used on reorg). Otherwise jobs remain in the broker queue and can be reused on restart.
522
468
  */
523
469
  public cancel() {
524
- void this.deferredJobQueue.cancel();
525
- // Recreate the queue so it can accept jobs for subsequent epochs.
526
- this.deferredJobQueue = new SerialQueue();
527
- this.deferredJobQueue.start(this.enqueueConcurrency);
528
-
529
- if (this.cancelJobsOnStop) {
530
- for (const controller of this.pendingProvingJobs) {
531
- controller.abort();
532
- }
533
- }
534
-
470
+ this.cancelled = true;
471
+ this.resetSchedulerState(this.cancelJobsOnStop);
472
+ // Reject the proving state (and hence subTreeResult) so anyone awaiting the sub-tree result
473
+ // is released rather than hanging — matching TopTreeOrchestrator.cancel().
535
474
  this.provingState?.cancel();
536
475
 
537
476
  for (const [blockNumber, db] of this.dbs.entries()) {
@@ -540,124 +479,73 @@ export class ProvingOrchestrator implements EpochProver {
540
479
  this.dbs.clear();
541
480
  }
542
481
 
543
- private getDbForBlock(blockNumber: BlockNumber): MerkleTreeWriteOperations {
544
- const db = this.dbs.get(blockNumber);
545
- if (!db) {
546
- throw new Error(`World state fork for block ${blockNumber} not found.`);
547
- }
548
- return db;
482
+ protected override cancelInternal(): void {
483
+ this.cancel();
549
484
  }
550
485
 
551
- /**
552
- * Returns the proof for the current epoch.
553
- */
554
- public async finalizeEpoch(): Promise<{
555
- publicInputs: RootRollupPublicInputs;
556
- proof: Proof;
557
- batchedBlobInputs: BatchedBlob;
558
- }> {
559
- if (!this.provingState || !this.provingPromise) {
560
- throw new Error(`Invalid proving state, an epoch must be proven before it can be finalized`);
561
- }
562
-
563
- const result = await this.provingPromise!;
564
- if (result.status === 'failure') {
565
- throw new Error(`Epoch proving failed: ${result.reason}`);
566
- }
567
-
568
- await this.provingState.finalizeBatchedBlob();
569
-
570
- const epochProofResult = this.provingState.getEpochProofResult();
571
-
572
- pushTestData('epochProofResult', {
573
- proof: epochProofResult.proof.toString(),
574
- publicInputs: epochProofResult.publicInputs.toString(),
575
- });
576
-
577
- return epochProofResult;
578
- }
486
+ // ---------------- private: per-checkpoint init ----------------
579
487
 
580
488
  /**
581
- * Enqueue a job to be scheduled
582
- * @param provingState - The proving state object being operated on
583
- * @param jobType - The type of job to be queued
584
- * @param job - The actual job, returns a promise notifying of the job's completion
489
+ * Internal driver for the single-checkpoint init. Allocates the world-state fork,
490
+ * inserts L1-to-L2 messages, and creates the per-checkpoint proving state with this
491
+ * sub-tree as its parent. Only called once, from the `start` factory.
585
492
  */
586
- private deferredProving<T>(
587
- provingState: EpochProvingState | CheckpointProvingState | BlockProvingState,
588
- request: (signal: AbortSignal) => Promise<T>,
589
- callback: (result: T) => void | Promise<void>,
590
- ) {
591
- if (!provingState.verifyState()) {
592
- this.logger.debug(`Not enqueuing job, state no longer valid`);
593
- return;
493
+ private async startCheckpoint(
494
+ constants: CheckpointConstantData,
495
+ l1ToL2Messages: Fr[],
496
+ totalNumBlocks: number,
497
+ headerOfLastBlockInPreviousCheckpoint: BlockHeader,
498
+ ): Promise<void> {
499
+ if (this.provingState) {
500
+ throw new Error('Checkpoint sub-tree already started.');
594
501
  }
595
502
 
596
- const controller = new AbortController();
597
- this.pendingProvingJobs.push(controller);
598
-
599
- // We use a 'safeJob'. We don't want promise rejections in the proving pool, we want to capture the error here
600
- // and reject the proving job whilst keeping the event loop free of rejections
601
- const safeJob = async () => {
602
- try {
603
- // there's a delay between enqueueing this job and it actually running
604
- if (controller.signal.aborted) {
605
- return;
606
- }
607
-
608
- const result = await request(controller.signal);
609
- if (!provingState.verifyState()) {
610
- this.logger.debug(`State no longer valid, discarding result`);
611
- return;
612
- }
613
-
614
- // we could have been cancelled whilst waiting for the result
615
- // and the prover ignored the signal. Drop the result in that case
616
- if (controller.signal.aborted) {
617
- return;
618
- }
503
+ // Fork world state at the end of the immediately previous block.
504
+ const lastBlockNumber = headerOfLastBlockInPreviousCheckpoint.globalVariables.blockNumber;
505
+ const db = await this.dbProvider.fork(lastBlockNumber);
619
506
 
620
- await callback(result);
621
- } catch (err) {
622
- if (err instanceof AbortError) {
623
- // operation was cancelled, probably because the block was cancelled
624
- // drop this result
625
- return;
626
- }
507
+ const firstBlockNumber = BlockNumber(lastBlockNumber + 1);
508
+ this.dbs.set(firstBlockNumber, db);
627
509
 
628
- this.logger.error(`Error thrown when proving job`, err);
629
- provingState!.reject(`${err}`);
630
- } finally {
631
- const index = this.pendingProvingJobs.indexOf(controller);
632
- if (index > -1) {
633
- this.pendingProvingJobs.splice(index, 1);
634
- }
635
- }
636
- };
510
+ // Get archive sibling path before any block in this checkpoint lands.
511
+ const lastArchiveSiblingPath = await getLastSiblingPath(MerkleTreeId.ARCHIVE, db);
637
512
 
638
- void this.deferredJobQueue.put(async () => {
639
- void safeJob();
640
- // we yield here to the macro task queue such to give Nodejs a chance to run other operatoins in between enqueues
641
- await sleep(0);
642
- });
643
- }
513
+ // Insert all the l1 to l2 messages into the db. Get the states before and after the insertion.
514
+ const {
515
+ lastL1ToL2MessageTreeSnapshot,
516
+ lastL1ToL2MessageSubtreeRootSiblingPath,
517
+ newL1ToL2MessageTreeSnapshot,
518
+ newL1ToL2MessageSubtreeRootSiblingPath,
519
+ } = await this.updateL1ToL2MessageTree(l1ToL2Messages, db);
644
520
 
645
- private async updateL1ToL2MessageTree(l1ToL2Messages: Fr[], db: MerkleTreeWriteOperations) {
646
- const l1ToL2MessagesPadded = padArrayEnd<Fr, number>(
521
+ this.provingState = new CheckpointProvingState(
522
+ /* index */ 0,
523
+ constants,
524
+ totalNumBlocks,
525
+ headerOfLastBlockInPreviousCheckpoint,
526
+ lastArchiveSiblingPath,
647
527
  l1ToL2Messages,
648
- Fr.ZERO,
649
- NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
650
- 'Too many L1 to L2 messages',
528
+ lastL1ToL2MessageTreeSnapshot,
529
+ lastL1ToL2MessageSubtreeRootSiblingPath,
530
+ newL1ToL2MessageTreeSnapshot,
531
+ newL1ToL2MessageSubtreeRootSiblingPath,
532
+ Number(this.epochNumber),
533
+ /* isAlive */ () => !this.cancelled,
534
+ /* onReject */ reason => this.subTreeResult.reject(new Error(reason)),
651
535
  );
536
+ }
537
+
538
+ // ---------------- private: per-block proof orchestration ----------------
652
539
 
540
+ private async updateL1ToL2MessageTree(l1ToL2Messages: Fr[], db: MerkleTreeWriteOperations) {
653
541
  const lastL1ToL2MessageTreeSnapshot = await getTreeSnapshot(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, db);
654
542
  const lastL1ToL2MessageSubtreeRootSiblingPath = assertLength(
655
543
  await getSubtreeSiblingPath(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, L1_TO_L2_MSG_SUBTREE_HEIGHT, db),
656
544
  L1_TO_L2_MSG_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
657
545
  );
658
546
 
659
- // Update the local trees to include the new l1 to l2 messages
660
- await db.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
547
+ // Update the local trees to include the new l1 to l2 messages.
548
+ await appendL1ToL2MessagesToTree(db, l1ToL2Messages);
661
549
 
662
550
  const newL1ToL2MessageTreeSnapshot = await getTreeSnapshot(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, db);
663
551
  const newL1ToL2MessageSubtreeRootSiblingPath = assertLength(
@@ -673,8 +561,8 @@ export class ProvingOrchestrator implements EpochProver {
673
561
  };
674
562
  }
675
563
 
676
- // Updates the merkle trees for a transaction. The first enqueued job for a transaction
677
- @trackSpan('ProvingOrchestrator.prepareBaseRollupInputs', tx => ({
564
+ // Updates the merkle trees for a transaction. The first enqueued job for a transaction.
565
+ @trackSpan('CheckpointSubTreeOrchestrator.prepareBaseRollupInputs', tx => ({
678
566
  [Attributes.TX_HASH]: tx.hash.toString(),
679
567
  }))
680
568
  private async prepareBaseRollupInputs(
@@ -683,21 +571,21 @@ export class ProvingOrchestrator implements EpochProver {
683
571
  newL1ToL2MessageTreeSnapshot: AppendOnlyTreeSnapshot,
684
572
  startSpongeBlob: SpongeBlob,
685
573
  db: MerkleTreeWriteOperations,
686
- ): Promise<[BaseRollupHints, TreeSnapshots]> {
687
- // We build the base rollup inputs using a mock proof and verification key.
688
- // These will be overwritten later once we have proven the chonk verifier circuit and any public kernels
689
- const [ms, hints] = await elapsed(
690
- insertSideEffectsAndBuildBaseRollupHints(
691
- tx,
692
- lastArchive,
693
- newL1ToL2MessageTreeSnapshot,
694
- startSpongeBlob,
695
- this.proverId.toField(),
696
- db,
697
- ),
574
+ ): Promise<[BaseRollupHintsWithoutProofAndVK, TreeSnapshots]> {
575
+ // These hints deliberately carry no recursive proof or verification key — see
576
+ // BaseRollupHintsWithoutProofAndVK. The tx's proof + VK are attached later in
577
+ // TxProvingState.getBaseRollupTypeAndInputs from the proven chonk-verifier / kernel / AVM
578
+ // proofs, which are required there and so cannot be silently omitted.
579
+ const start = performance.now();
580
+ const hints = await insertSideEffectsAndBuildBaseRollupHints(
581
+ tx,
582
+ lastArchive,
583
+ newL1ToL2MessageTreeSnapshot,
584
+ startSpongeBlob,
585
+ this.proverId.toField(),
586
+ db,
698
587
  );
699
-
700
- this.metrics.recordBaseRollupInputs(ms);
588
+ this.metrics.recordBaseRollupInputs(performance.now() - start);
701
589
 
702
590
  const promises = [MerkleTreeId.NOTE_HASH_TREE, MerkleTreeId.NULLIFIER_TREE, MerkleTreeId.PUBLIC_DATA_TREE].map(
703
591
  async (id: MerkleTreeId) => {
@@ -709,8 +597,8 @@ export class ProvingOrchestrator implements EpochProver {
709
597
  return [hints, treeSnapshots];
710
598
  }
711
599
 
712
- // Executes the base rollup circuit and stored the output as intermediate state for the parent merge/root circuit
713
- // Executes the next level of merge if all inputs are available
600
+ // Executes the base rollup circuit and stores the output as intermediate state for the parent merge/root circuit.
601
+ // Executes the next level of merge if all inputs are available.
714
602
  private enqueueBaseRollup(provingState: BlockProvingState, txIndex: number) {
715
603
  if (!provingState.verifyState()) {
716
604
  this.logger.debug('Not running base rollup, state invalid');
@@ -730,17 +618,10 @@ export class ProvingOrchestrator implements EpochProver {
730
618
 
731
619
  this.deferredProving(
732
620
  provingState,
733
- wrapCallbackInSpan(
734
- this.tracer,
735
- `ProvingOrchestrator.prover.${
736
- inputs instanceof PrivateTxBaseRollupPrivateInputs
737
- ? 'getPrivateTxBaseRollupProof'
738
- : 'getPublicTxBaseRollupProof'
739
- }`,
740
- {
741
- [Attributes.TX_HASH]: processedTx.hash.toString(),
742
- [Attributes.PROTOCOL_CIRCUIT_NAME]: rollupType,
743
- },
621
+ this.wrapCircuitCall(
622
+ inputs instanceof PrivateTxBaseRollupPrivateInputs
623
+ ? 'getPrivateTxBaseRollupProof'
624
+ : 'getPublicTxBaseRollupProof',
744
625
  signal => {
745
626
  if (inputs instanceof PrivateTxBaseRollupPrivateInputs) {
746
627
  return this.prover.getPrivateTxBaseRollupProof(inputs, signal, provingState.epochNumber);
@@ -748,6 +629,7 @@ export class ProvingOrchestrator implements EpochProver {
748
629
  return this.prover.getPublicTxBaseRollupProof(inputs, signal, provingState.epochNumber);
749
630
  }
750
631
  },
632
+ { [Attributes.TX_HASH]: processedTx.hash.toString(), [Attributes.PROTOCOL_CIRCUIT_NAME]: rollupType },
751
633
  ),
752
634
  result => {
753
635
  this.logger.debug(`Completed proof for ${rollupType} for tx ${processedTx.hash.toString()}`);
@@ -762,72 +644,53 @@ export class ProvingOrchestrator implements EpochProver {
762
644
  );
763
645
  }
764
646
 
765
- // Enqueues the public chonk verifier circuit for a given transaction index, or reuses the one already enqueued.
766
- // Once completed, will enqueue the the public tx base rollup.
647
+ /**
648
+ * Route the tx's chonk-verifier dependency through the per-epoch context: read the
649
+ * cached promise (or enqueue if missing), then `.then(handleResult)` to progress to
650
+ * the base rollup once the proof lands.
651
+ */
767
652
  private getOrEnqueueChonkVerifier(provingState: BlockProvingState, txIndex: number) {
768
653
  if (!provingState.verifyState()) {
769
- this.logger.debug('Not running chonk verifier circuit, state invalid');
770
654
  return;
771
655
  }
772
656
 
773
657
  const txProvingState = provingState.getTxProvingState(txIndex);
774
658
  const txHash = txProvingState.processedTx.hash.toString();
775
- NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH;
659
+
776
660
  const handleResult = (
777
661
  result: PublicInputsAndRecursiveProof<
778
662
  PublicChonkVerifierPublicInputs,
779
663
  typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
780
664
  >,
781
665
  ) => {
782
- this.logger.debug(`Got chonk verifier proof for tx index: ${txIndex}`, { txHash });
666
+ if (!provingState.verifyState()) {
667
+ return;
668
+ }
783
669
  txProvingState.setPublicChonkVerifierProof(result);
784
- this.provingState?.cachedChonkVerifierProofs.delete(txHash);
785
670
  this.checkAndEnqueueBaseRollup(provingState, txIndex);
786
671
  };
787
672
 
788
- if (this.provingState?.cachedChonkVerifierProofs.has(txHash)) {
789
- this.logger.debug(`Chonk verifier proof already enqueued for tx index: ${txIndex}`, { txHash });
790
- void this.provingState!.cachedChonkVerifierProofs.get(txHash)!.then(handleResult);
791
- return;
792
- }
793
-
794
- this.logger.debug(`Enqueuing chonk verifier circuit for tx index: ${txIndex}`);
795
- this.doEnqueueChonkVerifier(txHash, txProvingState.getPublicChonkVerifierPrivateInputs(), handleResult);
796
- }
797
-
798
- private doEnqueueChonkVerifier(
799
- txHash: string,
800
- inputs: PublicChonkVerifierPrivateInputs,
801
- handler: (
802
- result: PublicInputsAndRecursiveProof<
803
- PublicChonkVerifierPublicInputs,
804
- typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
805
- >,
806
- ) => void,
807
- provingState: EpochProvingState | BlockProvingState = this.provingState!,
808
- ) {
809
- if (!provingState.verifyState()) {
810
- this.logger.debug('Not running chonk verifier circuit, state invalid');
811
- return;
812
- }
813
-
814
- this.deferredProving(
815
- provingState,
816
- wrapCallbackInSpan(
817
- this.tracer,
818
- 'ProvingOrchestrator.prover.getPublicChonkVerifierProof',
819
- {
820
- [Attributes.TX_HASH]: txHash,
821
- [Attributes.PROTOCOL_CIRCUIT_NAME]: 'chonk-verifier-public' satisfies CircuitName,
822
- },
823
- signal => this.prover.getPublicChonkVerifierProof(inputs, signal, provingState.epochNumber),
673
+ const promise = this.chonkCache.getOrCompute(txHash, signal =>
674
+ this.prover.getPublicChonkVerifierProof(
675
+ txProvingState.getPublicChonkVerifierPrivateInputs(),
676
+ signal,
677
+ this.epochNumber,
824
678
  ),
825
- handler,
826
679
  );
680
+ void promise.then(handleResult).catch(err => {
681
+ // The cache self-cleans on rejection, so a replacement sub-tree for this tx will see the
682
+ // miss and re-enqueue. But if this proving state is still active, the failure must abort
683
+ // it: otherwise the base rollup for this tx is never enqueued and the checkpoint (and
684
+ // epoch) orchestrators hang forever waiting for a proof that will never arrive.
685
+ if (err instanceof AbortError || !provingState.verifyState()) {
686
+ return;
687
+ }
688
+ this.logger.error(`Chonk verifier proof failed for tx ${txHash}`, err);
689
+ provingState.reject(`Chonk verifier proof failed for tx ${txHash}: ${err}`);
690
+ });
827
691
  }
828
692
 
829
- // Executes the merge rollup circuit and stored the output as intermediate state for the parent merge/block root circuit
830
- // Enqueues the next level of merge if all inputs are available
693
+ // Executes the merge rollup circuit. Enqueues the next level of merge if all inputs are available.
831
694
  private enqueueMergeRollup(provingState: BlockProvingState, location: TreeNodeLocation) {
832
695
  if (!provingState.verifyState()) {
833
696
  this.logger.debug('Not running merge rollup. State no longer valid.');
@@ -843,13 +706,10 @@ export class ProvingOrchestrator implements EpochProver {
843
706
 
844
707
  this.deferredProving(
845
708
  provingState,
846
- wrapCallbackInSpan(
847
- this.tracer,
848
- 'ProvingOrchestrator.prover.getTxMergeRollupProof',
849
- {
850
- [Attributes.PROTOCOL_CIRCUIT_NAME]: 'rollup-tx-merge' satisfies CircuitName,
851
- },
709
+ this.wrapCircuitCall(
710
+ 'getTxMergeRollupProof',
852
711
  signal => this.prover.getTxMergeRollupProof(inputs, signal, provingState.epochNumber),
712
+ { [Attributes.PROTOCOL_CIRCUIT_NAME]: 'rollup-tx-merge' satisfies CircuitName },
853
713
  ),
854
714
  result => {
855
715
  provingState.setMergeRollupProof(location, result);
@@ -858,7 +718,7 @@ export class ProvingOrchestrator implements EpochProver {
858
718
  );
859
719
  }
860
720
 
861
- // Executes the block root rollup circuit
721
+ // Executes the block root rollup circuit.
862
722
  private enqueueBlockRootRollup(provingState: BlockProvingState) {
863
723
  if (!provingState.verifyState()) {
864
724
  this.logger.debug('Not running block root rollup, state no longer valid');
@@ -876,12 +736,8 @@ export class ProvingOrchestrator implements EpochProver {
876
736
 
877
737
  this.deferredProving(
878
738
  provingState,
879
- wrapCallbackInSpan(
880
- this.tracer,
881
- 'ProvingOrchestrator.prover.getBlockRootRollupProof',
882
- {
883
- [Attributes.PROTOCOL_CIRCUIT_NAME]: rollupType,
884
- },
739
+ this.wrapCircuitCall(
740
+ 'getBlockRootRollupProof',
885
741
  signal => {
886
742
  if (inputs instanceof BlockRootFirstRollupPrivateInputs) {
887
743
  return this.prover.getBlockRootFirstRollupProof(inputs, signal, provingState.epochNumber);
@@ -895,9 +751,14 @@ export class ProvingOrchestrator implements EpochProver {
895
751
  return this.prover.getBlockRootRollupProof(inputs, signal, provingState.epochNumber);
896
752
  }
897
753
  },
754
+ { [Attributes.PROTOCOL_CIRCUIT_NAME]: rollupType },
898
755
  ),
899
756
  async result => {
900
- this.logger.debug(`Completed ${rollupType} proof for block ${provingState.blockNumber}`);
757
+ this.logger.debug(`Completed ${rollupType} proof for block ${provingState.blockNumber}`, {
758
+ blockNumber: provingState.blockNumber,
759
+ checkpointIndex: provingState.parentCheckpoint.index,
760
+ ...result.inputs.toInspect(),
761
+ });
901
762
 
902
763
  const leafLocation = provingState.setBlockRootRollupProof(result);
903
764
  const checkpointProvingState = provingState.parentCheckpoint;
@@ -907,16 +768,15 @@ export class ProvingOrchestrator implements EpochProver {
907
768
  await this.verifyBuiltBlockAgainstSyncedState(provingState);
908
769
 
909
770
  if (checkpointProvingState.totalNumBlocks === 1) {
910
- await this.checkAndEnqueueCheckpointRootRollup(checkpointProvingState);
771
+ this.checkAndEnqueueSubTreeResolution(checkpointProvingState);
911
772
  } else {
912
- await this.checkAndEnqueueNextBlockMergeRollup(checkpointProvingState, leafLocation);
773
+ this.checkAndEnqueueNextBlockMergeRollup(checkpointProvingState, leafLocation);
913
774
  }
914
775
  },
915
776
  );
916
777
  }
917
778
 
918
- // Executes the base parity circuit and stores the intermediate state for the root parity circuit
919
- // Enqueues the root parity circuit if all inputs are available
779
+ // Executes the base parity circuit. Enqueues the root parity circuit if all inputs are available.
920
780
  private enqueueBaseParityCircuit(
921
781
  checkpointProvingState: CheckpointProvingState,
922
782
  provingState: BlockProvingState,
@@ -936,13 +796,10 @@ export class ProvingOrchestrator implements EpochProver {
936
796
 
937
797
  this.deferredProving(
938
798
  provingState,
939
- wrapCallbackInSpan(
940
- this.tracer,
941
- 'ProvingOrchestrator.prover.getBaseParityProof',
942
- {
943
- [Attributes.PROTOCOL_CIRCUIT_NAME]: 'parity-base' satisfies CircuitName,
944
- },
799
+ this.wrapCircuitCall(
800
+ 'getBaseParityProof',
945
801
  signal => this.prover.getBaseParityProof(inputs, signal, provingState.epochNumber),
802
+ { [Attributes.PROTOCOL_CIRCUIT_NAME]: 'parity-base' satisfies CircuitName },
946
803
  ),
947
804
  provingOutput => {
948
805
  provingState.setBaseParityProof(baseParityIndex, provingOutput);
@@ -955,12 +812,11 @@ export class ProvingOrchestrator implements EpochProver {
955
812
  if (!provingState.isReadyForRootParity()) {
956
813
  return;
957
814
  }
958
-
959
815
  this.enqueueRootParityCircuit(provingState);
960
816
  }
961
817
 
962
- // Runs the root parity circuit ans stored the outputs
963
- // Enqueues the root rollup proof if all inputs are available
818
+ // Runs the root parity circuit and stores the outputs.
819
+ // Enqueues the block root rollup if all inputs are available.
964
820
  private enqueueRootParityCircuit(provingState: BlockProvingState) {
965
821
  if (!provingState.verifyState()) {
966
822
  this.logger.debug('Not running root parity. State no longer valid.');
@@ -976,13 +832,10 @@ export class ProvingOrchestrator implements EpochProver {
976
832
 
977
833
  this.deferredProving(
978
834
  provingState,
979
- wrapCallbackInSpan(
980
- this.tracer,
981
- 'ProvingOrchestrator.prover.getRootParityProof',
982
- {
983
- [Attributes.PROTOCOL_CIRCUIT_NAME]: 'parity-root' satisfies CircuitName,
984
- },
835
+ this.wrapCircuitCall(
836
+ 'getRootParityProof',
985
837
  signal => this.prover.getRootParityProof(inputs, signal, provingState.epochNumber),
838
+ { [Attributes.PROTOCOL_CIRCUIT_NAME]: 'parity-root' satisfies CircuitName },
986
839
  ),
987
840
  result => {
988
841
  provingState.setRootParityProof(result);
@@ -991,8 +844,7 @@ export class ProvingOrchestrator implements EpochProver {
991
844
  );
992
845
  }
993
846
 
994
- // Executes the block merge rollup circuit and stored the output as intermediate state for the parent merge/block root circuit
995
- // Enqueues the next level of merge if all inputs are available
847
+ // Executes the block merge rollup circuit.
996
848
  private enqueueBlockMergeRollup(provingState: CheckpointProvingState, location: TreeNodeLocation) {
997
849
  if (!provingState.verifyState()) {
998
850
  this.logger.debug('Not running block merge rollup. State no longer valid.');
@@ -1007,170 +859,19 @@ export class ProvingOrchestrator implements EpochProver {
1007
859
  const inputs = provingState.getBlockMergeRollupInputs(location);
1008
860
  this.deferredProving(
1009
861
  provingState,
1010
- wrapCallbackInSpan(
1011
- this.tracer,
1012
- 'ProvingOrchestrator.prover.getBlockMergeRollupProof',
1013
- {
1014
- [Attributes.PROTOCOL_CIRCUIT_NAME]: 'rollup-block-merge' satisfies CircuitName,
1015
- },
862
+ this.wrapCircuitCall(
863
+ 'getBlockMergeRollupProof',
1016
864
  signal => this.prover.getBlockMergeRollupProof(inputs, signal, provingState.epochNumber),
1017
- ),
1018
- async result => {
1019
- provingState.setBlockMergeRollupProof(location, result);
1020
- await this.checkAndEnqueueNextBlockMergeRollup(provingState, location);
1021
- },
1022
- );
1023
- }
1024
-
1025
- private async enqueueCheckpointRootRollup(provingState: CheckpointProvingState) {
1026
- if (!provingState.verifyState()) {
1027
- this.logger.debug('Not running checkpoint root rollup. State no longer valid.');
1028
- return;
1029
- }
1030
-
1031
- if (!provingState.tryStartProvingCheckpointRoot()) {
1032
- this.logger.debug('Checkpoint root rollup already started.');
1033
- return;
1034
- }
1035
-
1036
- const rollupType = provingState.getCheckpointRootRollupType();
1037
-
1038
- this.logger.debug(`Enqueuing ${rollupType} for checkpoint ${provingState.index}.`);
1039
-
1040
- const inputs = await provingState.getCheckpointRootRollupInputs();
1041
-
1042
- this.deferredProving(
1043
- provingState,
1044
- wrapCallbackInSpan(
1045
- this.tracer,
1046
- 'ProvingOrchestrator.prover.getCheckpointRootRollupProof',
1047
- {
1048
- [Attributes.PROTOCOL_CIRCUIT_NAME]: rollupType,
1049
- },
1050
- signal => {
1051
- if (inputs instanceof CheckpointRootSingleBlockRollupPrivateInputs) {
1052
- return this.prover.getCheckpointRootSingleBlockRollupProof(inputs, signal, provingState.epochNumber);
1053
- } else {
1054
- return this.prover.getCheckpointRootRollupProof(inputs, signal, provingState.epochNumber);
1055
- }
1056
- },
1057
- ),
1058
- result => {
1059
- const computedEndBlobAccumulatorState = provingState.getEndBlobAccumulator()!.toBlobAccumulator();
1060
- const circuitEndBlobAccumulatorState = result.inputs.endBlobAccumulator;
1061
- if (!circuitEndBlobAccumulatorState.equals(computedEndBlobAccumulatorState)) {
1062
- this.logger.error(
1063
- `Blob accumulator state mismatch.\nCircuit: ${inspect(circuitEndBlobAccumulatorState)}\nComputed: ${inspect(
1064
- computedEndBlobAccumulatorState,
1065
- )}`,
1066
- );
1067
- provingState.reject(`Blob accumulator state mismatch.`);
1068
- return;
1069
- }
1070
-
1071
- this.logger.debug(`Completed ${rollupType} proof for checkpoint ${provingState.index}.`);
1072
-
1073
- const leafLocation = provingState.setCheckpointRootRollupProof(result);
1074
- const epochProvingState = provingState.parentEpoch;
1075
-
1076
- if (epochProvingState.totalNumCheckpoints === 1) {
1077
- this.enqueueEpochPadding(epochProvingState);
1078
- } else {
1079
- this.checkAndEnqueueNextCheckpointMergeRollup(epochProvingState, leafLocation);
1080
- }
1081
- },
1082
- );
1083
- }
1084
-
1085
- private enqueueCheckpointMergeRollup(provingState: EpochProvingState, location: TreeNodeLocation) {
1086
- if (!provingState.verifyState()) {
1087
- this.logger.debug('Not running checkpoint merge rollup. State no longer valid.');
1088
- return;
1089
- }
1090
-
1091
- if (!provingState.tryStartProvingCheckpointMerge(location)) {
1092
- this.logger.debug('Checkpoint merge rollup already started.');
1093
- return;
1094
- }
1095
-
1096
- const inputs = provingState.getCheckpointMergeRollupInputs(location);
1097
-
1098
- this.deferredProving(
1099
- provingState,
1100
- wrapCallbackInSpan(
1101
- this.tracer,
1102
- 'ProvingOrchestrator.prover.getCheckpointMergeRollupProof',
1103
- {
1104
- [Attributes.PROTOCOL_CIRCUIT_NAME]: 'rollup-checkpoint-merge' satisfies CircuitName,
1105
- },
1106
- signal => this.prover.getCheckpointMergeRollupProof(inputs, signal, provingState.epochNumber),
1107
- ),
1108
- result => {
1109
- this.logger.debug('Completed proof for checkpoint merge rollup.');
1110
- provingState.setCheckpointMergeRollupProof(location, result);
1111
- this.checkAndEnqueueNextCheckpointMergeRollup(provingState, location);
1112
- },
1113
- );
1114
- }
1115
-
1116
- private enqueueEpochPadding(provingState: EpochProvingState) {
1117
- if (!provingState.verifyState()) {
1118
- this.logger.debug('Not running epoch padding. State no longer valid.');
1119
- return;
1120
- }
1121
-
1122
- if (!provingState.tryStartProvingPaddingCheckpoint()) {
1123
- this.logger.debug('Padding checkpoint already started.');
1124
- return;
1125
- }
1126
-
1127
- this.logger.debug('Padding epoch proof with a padding block root proof.');
1128
-
1129
- const inputs = provingState.getPaddingCheckpointInputs();
1130
-
1131
- this.deferredProving(
1132
- provingState,
1133
- wrapCallbackInSpan(
1134
- this.tracer,
1135
- 'ProvingOrchestrator.prover.getCheckpointPaddingRollupProof',
1136
- {
1137
- [Attributes.PROTOCOL_CIRCUIT_NAME]: 'rollup-checkpoint-padding' satisfies CircuitName,
1138
- },
1139
- signal => this.prover.getCheckpointPaddingRollupProof(inputs, signal, provingState.epochNumber),
1140
- ),
1141
- result => {
1142
- this.logger.debug('Completed proof for padding checkpoint.');
1143
- provingState.setCheckpointPaddingProof(result);
1144
- this.checkAndEnqueueRootRollup(provingState);
1145
- },
1146
- );
1147
- }
1148
-
1149
- // Executes the root rollup circuit
1150
- private enqueueRootRollup(provingState: EpochProvingState) {
1151
- if (!provingState.verifyState()) {
1152
- this.logger.debug('Not running root rollup, state no longer valid');
1153
- return;
1154
- }
1155
-
1156
- this.logger.debug(`Preparing root rollup`);
1157
-
1158
- const inputs = provingState.getRootRollupInputs();
1159
-
1160
- this.deferredProving(
1161
- provingState,
1162
- wrapCallbackInSpan(
1163
- this.tracer,
1164
- 'ProvingOrchestrator.prover.getRootRollupProof',
1165
- {
1166
- [Attributes.PROTOCOL_CIRCUIT_NAME]: 'rollup-root' satisfies CircuitName,
1167
- },
1168
- signal => this.prover.getRootRollupProof(inputs, signal, provingState.epochNumber),
865
+ { [Attributes.PROTOCOL_CIRCUIT_NAME]: 'rollup-block-merge' satisfies CircuitName },
1169
866
  ),
1170
867
  result => {
1171
- this.logger.verbose(`Orchestrator completed root rollup for epoch ${provingState.epochNumber}`);
1172
- provingState.setRootRollupProof(result);
1173
- provingState.resolve({ status: 'success' });
868
+ this.logger.debug(`Completed block merge rollup proof for checkpoint ${provingState.index}`, {
869
+ checkpointIndex: provingState.index,
870
+ mergeLocation: location,
871
+ ...result.inputs.toInspect(),
872
+ });
873
+ provingState.setBlockMergeRollupProof(location, result);
874
+ this.checkAndEnqueueNextBlockMergeRollup(provingState, location);
1174
875
  },
1175
876
  );
1176
877
  }
@@ -1179,7 +880,6 @@ export class ProvingOrchestrator implements EpochProver {
1179
880
  if (!provingState.isReadyForMergeRollup(currentLocation)) {
1180
881
  return;
1181
882
  }
1182
-
1183
883
  const parentLocation = provingState.getParentLocation(currentLocation);
1184
884
  if (parentLocation.level === 0) {
1185
885
  this.checkAndEnqueueBlockRootRollup(provingState);
@@ -1193,61 +893,45 @@ export class ProvingOrchestrator implements EpochProver {
1193
893
  this.logger.debug('Not ready for block root rollup');
1194
894
  return;
1195
895
  }
1196
-
1197
896
  this.enqueueBlockRootRollup(provingState);
1198
897
  }
1199
898
 
1200
- private async checkAndEnqueueNextBlockMergeRollup(
899
+ private checkAndEnqueueNextBlockMergeRollup(
1201
900
  provingState: CheckpointProvingState,
1202
901
  currentLocation: TreeNodeLocation,
1203
- ) {
902
+ ): void {
1204
903
  if (!provingState.isReadyForBlockMerge(currentLocation)) {
1205
904
  return;
1206
905
  }
1207
-
1208
906
  const parentLocation = provingState.getParentLocation(currentLocation);
1209
907
  if (parentLocation.level === 0) {
1210
- await this.checkAndEnqueueCheckpointRootRollup(provingState);
908
+ this.checkAndEnqueueSubTreeResolution(provingState);
1211
909
  } else {
1212
910
  this.enqueueBlockMergeRollup(provingState, parentLocation);
1213
911
  }
1214
912
  }
1215
913
 
1216
- private async checkAndEnqueueCheckpointRootRollup(provingState: CheckpointProvingState) {
1217
- if (!provingState.isReadyForCheckpointRoot()) {
1218
- return;
1219
- }
1220
-
1221
- await this.enqueueCheckpointRootRollup(provingState);
1222
- }
1223
-
1224
- private checkAndEnqueueNextCheckpointMergeRollup(provingState: EpochProvingState, currentLocation: TreeNodeLocation) {
1225
- if (!provingState.isReadyForCheckpointMerge(currentLocation)) {
1226
- return;
1227
- }
1228
-
1229
- const parentLocation = provingState.getParentLocation(currentLocation);
1230
- if (parentLocation.level === 0) {
1231
- this.checkAndEnqueueRootRollup(provingState);
1232
- } else {
1233
- this.enqueueCheckpointMergeRollup(provingState, parentLocation);
1234
- }
1235
- }
1236
-
1237
- private checkAndEnqueueRootRollup(provingState: EpochProvingState) {
1238
- if (!provingState.isReadyForRootRollup()) {
1239
- this.logger.debug('Not ready for root rollup');
914
+ /**
915
+ * Sub-tree analogue of the orchestrator's `checkAndEnqueueCheckpointRootRollup`:
916
+ * resolves the sub-tree promise with the block-level proof outputs once they're all ready,
917
+ * instead of escalating to the checkpoint root rollup.
918
+ */
919
+ private checkAndEnqueueSubTreeResolution(provingState: CheckpointProvingState): void {
920
+ const proofs = provingState.getSubTreeOutputProofs();
921
+ const nonEmpty = proofs.filter((p): p is NonNullable<typeof p> => !!p);
922
+ if (proofs.length !== nonEmpty.length) {
923
+ // Block merge tree not fully resolved yet — retried as more block proofs land.
1240
924
  return;
1241
925
  }
1242
-
1243
- this.enqueueRootRollup(provingState);
926
+ this.subTreeResult.resolve({
927
+ blockProofOutputs: nonEmpty,
928
+ previousArchiveSiblingPath: provingState.getLastArchiveSiblingPath(),
929
+ });
1244
930
  }
1245
931
 
1246
932
  /**
1247
- * Executes the VM circuit for a public function, will enqueue the corresponding kernel if the
1248
- * previous kernel is ready
1249
- * @param provingState - The proving state being operated on
1250
- * @param txIndex - The index of the transaction being proven
933
+ * Executes the VM circuit for a public function. Enqueues the base rollup once the
934
+ * tx's chonk-verifier + VM proofs are both ready.
1251
935
  */
1252
936
  private enqueueVM(provingState: BlockProvingState, txIndex: number) {
1253
937
  if (!provingState.verifyState()) {
@@ -1257,23 +941,22 @@ export class ProvingOrchestrator implements EpochProver {
1257
941
 
1258
942
  const txProvingState = provingState.getTxProvingState(txIndex);
1259
943
 
1260
- const doAvmProving = wrapCallbackInSpan(
1261
- this.tracer,
1262
- 'ProvingOrchestrator.prover.getAvmProof',
1263
- {
1264
- [Attributes.TX_HASH]: txProvingState.processedTx.hash.toString(),
1265
- },
1266
- async (signal: AbortSignal) => {
1267
- const inputs = txProvingState.getAvmInputs();
1268
- return await this.prover.getAvmProof(inputs, signal, provingState.epochNumber);
944
+ this.deferredProving(
945
+ provingState,
946
+ this.wrapCircuitCall(
947
+ 'getAvmProof',
948
+ async (signal: AbortSignal) => {
949
+ const inputs = txProvingState.getAvmInputs();
950
+ return await this.prover.getAvmProof(inputs, signal, provingState.epochNumber);
951
+ },
952
+ { [Attributes.TX_HASH]: txProvingState.processedTx.hash.toString() },
953
+ ),
954
+ proof => {
955
+ this.logger.debug(`Proven VM for tx index: ${txIndex}`);
956
+ txProvingState.setAvmProof(proof);
957
+ this.checkAndEnqueueBaseRollup(provingState, txIndex);
1269
958
  },
1270
959
  );
1271
-
1272
- this.deferredProving(provingState, doAvmProving, proof => {
1273
- this.logger.debug(`Proven VM for tx index: ${txIndex}`);
1274
- txProvingState.setAvmProof(proof);
1275
- this.checkAndEnqueueBaseRollup(provingState, txIndex);
1276
- });
1277
960
  }
1278
961
 
1279
962
  private checkAndEnqueueBaseRollup(provingState: BlockProvingState, txIndex: number) {
@@ -1281,10 +964,81 @@ export class ProvingOrchestrator implements EpochProver {
1281
964
  if (!txProvingState.ready()) {
1282
965
  return;
1283
966
  }
1284
-
1285
- // We must have completed all proving (chonk verifier proof and (if required) vm proof are generated), we now move to the base rollup.
967
+ // All upstream proofs (chonk verifier and, if required, vm) are ready — proceed to the base rollup.
1286
968
  this.logger.debug(`Public functions completed for tx ${txIndex} enqueueing base rollup`);
1287
-
1288
969
  this.enqueueBaseRollup(provingState, txIndex);
1289
970
  }
971
+
972
+ // Flagged as protected so unit tests can override.
973
+ protected async verifyBuiltBlockAgainstSyncedState(provingState: BlockProvingState) {
974
+ const builtBlockHeader = provingState.getBuiltBlockHeader();
975
+ if (!builtBlockHeader) {
976
+ this.logger.debug('Block header not built yet, skipping header check.');
977
+ return;
978
+ }
979
+
980
+ const output = provingState.getBlockRootRollupOutput();
981
+ if (!output) {
982
+ this.logger.debug('Block root rollup proof not built yet, skipping header check.');
983
+ return;
984
+ }
985
+
986
+ const newArchive = provingState.getBuiltArchive();
987
+ if (!newArchive) {
988
+ this.logger.debug('Archive snapshot not yet captured, skipping header check.');
989
+ return;
990
+ }
991
+
992
+ const header = await buildHeaderFromCircuitOutputs(output);
993
+
994
+ if (!(await header.hash()).equals(await builtBlockHeader.hash())) {
995
+ this.logger.error(`Block header mismatch.\nCircuit: ${inspect(header)}\nComputed: ${inspect(builtBlockHeader)}`);
996
+ provingState.reject(`Block header hash mismatch.`);
997
+ return;
998
+ }
999
+
1000
+ const blockNumber = provingState.blockNumber;
1001
+ const syncedArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, this.dbProvider.getSnapshot(blockNumber));
1002
+ if (!syncedArchive.equals(newArchive)) {
1003
+ this.logger.error(
1004
+ `Archive tree mismatch for block ${blockNumber}: world state synced to ${inspect(
1005
+ syncedArchive,
1006
+ )} but built ${inspect(newArchive)}`,
1007
+ );
1008
+ provingState.reject(`Archive tree mismatch.`);
1009
+ return;
1010
+ }
1011
+
1012
+ const circuitArchive = output.newArchive;
1013
+ if (!newArchive.equals(circuitArchive)) {
1014
+ this.logger.error(`New archive mismatch.\nCircuit: ${output.newArchive}\nComputed: ${newArchive}`);
1015
+ provingState.reject(`New archive mismatch.`);
1016
+ return;
1017
+ }
1018
+ }
1019
+
1020
+ private getDbForBlock(blockNumber: BlockNumber): MerkleTreeWriteOperations {
1021
+ const db = this.dbs.get(blockNumber);
1022
+ if (!db) {
1023
+ throw new Error(`World state fork for block ${blockNumber} not found.`);
1024
+ }
1025
+ return db;
1026
+ }
1027
+
1028
+ /**
1029
+ * Wraps a circuit call with a tracer span and circuit attributes. Replaces the
1030
+ * `ProvingScheduler.wrapCircuitCall` indirection that used to live on the abstract base.
1031
+ */
1032
+ private wrapCircuitCall<T>(
1033
+ circuitName: string,
1034
+ fn: (signal: AbortSignal) => Promise<T>,
1035
+ attributes: Record<string, unknown> = {},
1036
+ ): (signal: AbortSignal) => Promise<T> {
1037
+ return wrapCallbackInSpan(
1038
+ this.tracer,
1039
+ `CheckpointSubTreeOrchestrator.prover.${circuitName}`,
1040
+ { [Attributes.PROTOCOL_CIRCUIT_NAME]: circuitName as CircuitName, ...attributes },
1041
+ fn,
1042
+ );
1043
+ }
1290
1044
  }