@aztec/prover-client 0.0.1-commit.d3ec352c → 0.0.1-commit.d58ff9d0

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