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