@aztec/prover-client 0.0.1-commit.f2ce05ee → 0.0.1-commit.f5a9928

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