@aztec/prover-client 0.0.1-commit.343b43af6 → 0.0.1-commit.350e0a4d

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