@aztec/prover-client 0.0.1-commit.b33fc05d0 → 0.0.1-commit.b3d3157a
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/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +16 -2
- 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 +16 -4
- package/dest/mocks/test_context.d.ts +3 -1
- package/dest/mocks/test_context.d.ts.map +1 -1
- package/dest/mocks/test_context.js +13 -6
- package/dest/orchestrator/block-building-helpers.d.ts +1 -1
- package/dest/orchestrator/checkpoint-proving-state.d.ts +8 -1
- package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
- package/dest/orchestrator/checkpoint-proving-state.js +10 -1
- package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts +107 -0
- package/dest/orchestrator/checkpoint-sub-tree-orchestrator.d.ts.map +1 -0
- package/dest/orchestrator/checkpoint-sub-tree-orchestrator.js +151 -0
- package/dest/orchestrator/epoch-proving-context.d.ts +51 -0
- package/dest/orchestrator/epoch-proving-context.d.ts.map +1 -0
- package/dest/orchestrator/epoch-proving-context.js +81 -0
- package/dest/orchestrator/epoch-proving-state.d.ts +1 -1
- package/dest/orchestrator/index.d.ts +4 -1
- package/dest/orchestrator/index.d.ts.map +1 -1
- package/dest/orchestrator/index.js +3 -0
- package/dest/orchestrator/orchestrator.d.ts +14 -26
- package/dest/orchestrator/orchestrator.d.ts.map +1 -1
- package/dest/orchestrator/orchestrator.js +32 -166
- package/dest/orchestrator/proving-scheduler.d.ts +72 -0
- package/dest/orchestrator/proving-scheduler.d.ts.map +1 -0
- package/dest/orchestrator/proving-scheduler.js +117 -0
- package/dest/orchestrator/top-tree-orchestrator.d.ts +83 -0
- package/dest/orchestrator/top-tree-orchestrator.d.ts.map +1 -0
- package/dest/orchestrator/top-tree-orchestrator.js +182 -0
- package/dest/orchestrator/top-tree-proving-scheduler.d.ts +62 -0
- package/dest/orchestrator/top-tree-proving-scheduler.d.ts.map +1 -0
- package/dest/orchestrator/top-tree-proving-scheduler.js +73 -0
- package/dest/orchestrator/top-tree-proving-state.d.ts +61 -0
- package/dest/orchestrator/top-tree-proving-state.d.ts.map +1 -0
- package/dest/orchestrator/top-tree-proving-state.js +185 -0
- package/dest/prover-client/prover-client.d.ts +61 -2
- package/dest/prover-client/prover-client.d.ts.map +1 -1
- package/dest/prover-client/prover-client.js +49 -1
- package/dest/proving_broker/broker_prover_facade.d.ts +1 -1
- package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
- package/dest/proving_broker/broker_prover_facade.js +10 -16
- package/dest/proving_broker/config.d.ts +8 -72
- package/dest/proving_broker/config.d.ts.map +1 -1
- package/dest/proving_broker/config.js +2 -2
- package/dest/proving_broker/index.d.ts +2 -1
- package/dest/proving_broker/index.d.ts.map +1 -1
- package/dest/proving_broker/index.js +1 -0
- package/dest/proving_broker/proving_broker.d.ts +2 -2
- package/dest/proving_broker/proving_broker.d.ts.map +1 -1
- package/dest/proving_broker/proving_broker.js +32 -7
- package/dest/proving_broker/proving_broker_database/persisted.js +2 -2
- package/dest/proving_broker/rpc.d.ts +3 -1
- package/dest/proving_broker/rpc.d.ts.map +1 -1
- package/dest/proving_broker/rpc.js +80 -24
- package/dest/test/mock_prover.d.ts +3 -3
- package/package.json +17 -17
- package/src/config.ts +18 -2
- package/src/light/lightweight_checkpoint_builder.ts +21 -6
- package/src/mocks/test_context.ts +11 -7
- package/src/orchestrator/checkpoint-proving-state.ts +14 -1
- package/src/orchestrator/checkpoint-sub-tree-orchestrator.ts +271 -0
- package/src/orchestrator/epoch-proving-context.ts +101 -0
- package/src/orchestrator/index.ts +8 -0
- package/src/orchestrator/orchestrator.ts +46 -222
- package/src/orchestrator/proving-scheduler.ts +156 -0
- package/src/orchestrator/top-tree-orchestrator.ts +314 -0
- package/src/orchestrator/top-tree-proving-scheduler.ts +154 -0
- package/src/orchestrator/top-tree-proving-state.ts +220 -0
- package/src/prover-client/prover-client.ts +125 -1
- package/src/proving_broker/broker_prover_facade.ts +8 -16
- package/src/proving_broker/config.ts +2 -1
- package/src/proving_broker/index.ts +1 -0
- package/src/proving_broker/proving_broker.ts +27 -5
- package/src/proving_broker/proving_broker_database/persisted.ts +2 -2
- package/src/proving_broker/rpc.ts +36 -24
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { BatchedBlobAccumulator, type FinalBlobBatchingChallenges } from '@aztec/blob-lib';
|
|
2
|
+
import type { BatchedBlob } from '@aztec/blob-lib/types';
|
|
3
|
+
import {
|
|
4
|
+
type ARCHIVE_HEIGHT,
|
|
5
|
+
BLOBS_PER_CHECKPOINT,
|
|
6
|
+
FIELDS_PER_BLOB,
|
|
7
|
+
type NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
|
|
8
|
+
OUT_HASH_TREE_HEIGHT,
|
|
9
|
+
} from '@aztec/constants';
|
|
10
|
+
import type { EpochNumber } from '@aztec/foundation/branded-types';
|
|
11
|
+
import { padArrayEnd } from '@aztec/foundation/collection';
|
|
12
|
+
import { BLS12Point } from '@aztec/foundation/curves/bls12';
|
|
13
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
14
|
+
import type { LoggerBindings } from '@aztec/foundation/log';
|
|
15
|
+
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
16
|
+
import type { Tuple } from '@aztec/foundation/serialize';
|
|
17
|
+
import { MerkleTreeCalculator, shaMerkleHash } from '@aztec/foundation/trees';
|
|
18
|
+
import type { EthAddress } from '@aztec/stdlib/block';
|
|
19
|
+
import type { PublicInputsAndRecursiveProof, ServerCircuitProver } from '@aztec/stdlib/interfaces/server';
|
|
20
|
+
import { computeCheckpointOutHash } from '@aztec/stdlib/messaging';
|
|
21
|
+
import type { Proof } from '@aztec/stdlib/proofs';
|
|
22
|
+
import {
|
|
23
|
+
type BlockRollupPublicInputs,
|
|
24
|
+
CheckpointRootRollupHints,
|
|
25
|
+
CheckpointRootRollupPrivateInputs,
|
|
26
|
+
CheckpointRootSingleBlockRollupPrivateInputs,
|
|
27
|
+
type RootRollupPublicInputs,
|
|
28
|
+
} from '@aztec/stdlib/rollup';
|
|
29
|
+
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
30
|
+
import type { BlockHeader } from '@aztec/stdlib/tx';
|
|
31
|
+
import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
|
|
32
|
+
|
|
33
|
+
import { buildBlobHints, toProofData } from './block-building-helpers.js';
|
|
34
|
+
import { TopTreeProvingScheduler } from './top-tree-proving-scheduler.js';
|
|
35
|
+
import { TopTreeProvingState } from './top-tree-proving-state.js';
|
|
36
|
+
|
|
37
|
+
/** Per-checkpoint data fed into the top tree. */
|
|
38
|
+
export type CheckpointTopTreeData = {
|
|
39
|
+
/**
|
|
40
|
+
* Block-rollup proof outputs from the checkpoint's sub-tree. Passed as a Promise so the
|
|
41
|
+
* top tree can start (compute hints, pipeline merges) while sub-trees are still proving.
|
|
42
|
+
* The promise resolves to 1 entry for a single-block checkpoint, 2 for multi-block.
|
|
43
|
+
*/
|
|
44
|
+
blockProofs: Promise<
|
|
45
|
+
PublicInputsAndRecursiveProof<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>[]
|
|
46
|
+
>;
|
|
47
|
+
/** L2-to-L1 messages per block in the checkpoint, used to compute the out hash. */
|
|
48
|
+
l2ToL1MsgsPerBlock: Fr[][][];
|
|
49
|
+
/** Blob fields encoding the checkpoint's tx effects, used to compute the blob accumulator. */
|
|
50
|
+
blobFields: Fr[];
|
|
51
|
+
/** Header of the last block in the previous checkpoint (or the epoch's predecessor for index 0). */
|
|
52
|
+
previousBlockHeader: BlockHeader;
|
|
53
|
+
/** Sibling path of the archive tree before any block in this checkpoint landed. */
|
|
54
|
+
previousArchiveSiblingPath: Tuple<Fr, typeof ARCHIVE_HEIGHT>;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** Result of proving the top tree. */
|
|
58
|
+
export type TopTreeResult = {
|
|
59
|
+
publicInputs: RootRollupPublicInputs;
|
|
60
|
+
proof: Proof;
|
|
61
|
+
batchedBlobInputs: BatchedBlob;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Sentinel thrown by `cancel` so callers can distinguish reorg-driven cancellation from
|
|
66
|
+
* a genuine proving failure (and choose to rebuild + retry instead of failing the epoch).
|
|
67
|
+
*/
|
|
68
|
+
export class TopTreeCancelledError extends Error {
|
|
69
|
+
constructor(reason = 'Top-tree proving cancelled') {
|
|
70
|
+
super(reason);
|
|
71
|
+
this.name = 'TopTreeCancelledError';
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type OutHashHint = {
|
|
76
|
+
treeSnapshot: AppendOnlyTreeSnapshot;
|
|
77
|
+
siblingPath: Tuple<Fr, typeof OUT_HASH_TREE_HEIGHT>;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Drives proving from checkpoint root rollups through the epoch root rollup. Owns no
|
|
82
|
+
* world-state forks or tx processing — every input is supplied by the caller.
|
|
83
|
+
*
|
|
84
|
+
* Pipelined start: `prove()` does not wait for block-level proving. It pre-computes the
|
|
85
|
+
* out-hash and blob-accumulator hint chains immediately from archiver-derivable data,
|
|
86
|
+
* and each checkpoint's root rollup fires the moment its sub-tree's `blockProofs`
|
|
87
|
+
* promise resolves. Later checkpoints can still be block-level proving in parallel.
|
|
88
|
+
*/
|
|
89
|
+
export class TopTreeOrchestrator extends TopTreeProvingScheduler {
|
|
90
|
+
private state: TopTreeProvingState | undefined;
|
|
91
|
+
private cancelled = false;
|
|
92
|
+
|
|
93
|
+
constructor(
|
|
94
|
+
prover: ServerCircuitProver,
|
|
95
|
+
private readonly proverId: EthAddress,
|
|
96
|
+
enqueueConcurrency: number,
|
|
97
|
+
_telemetryClient: TelemetryClient = getTelemetryClient(),
|
|
98
|
+
bindings?: LoggerBindings,
|
|
99
|
+
) {
|
|
100
|
+
super(prover, enqueueConcurrency, 'prover-client:top-tree-orchestrator', bindings);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public getProverId(): EthAddress {
|
|
104
|
+
return this.proverId;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Proves the top tree from per-checkpoint data and pending block-proofs promises.
|
|
109
|
+
* Resolves with the final epoch proof, or rejects with `TopTreeCancelledError` if
|
|
110
|
+
* `cancel()` is invoked, or any other error if a circuit fails.
|
|
111
|
+
*/
|
|
112
|
+
public async prove(
|
|
113
|
+
epochNumber: EpochNumber,
|
|
114
|
+
totalNumCheckpoints: number,
|
|
115
|
+
finalBlobBatchingChallenges: FinalBlobBatchingChallenges,
|
|
116
|
+
checkpointData: CheckpointTopTreeData[],
|
|
117
|
+
): Promise<TopTreeResult> {
|
|
118
|
+
if (checkpointData.length !== totalNumCheckpoints) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`checkpointData length (${checkpointData.length}) does not match totalNumCheckpoints (${totalNumCheckpoints}).`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
if (this.state) {
|
|
124
|
+
throw new Error('TopTreeOrchestrator.prove called twice; construct a new orchestrator per epoch.');
|
|
125
|
+
}
|
|
126
|
+
// If cancel() was already called before prove() ran (e.g. a removeCheckpoint that
|
|
127
|
+
// landed while the caller was still preparing inputs), short-circuit the whole
|
|
128
|
+
// proving path. Without this, prove() would build its state, the per-checkpoint
|
|
129
|
+
// .then handlers would all bail on `this.cancelled`, and the completion promise
|
|
130
|
+
// would never resolve — prove() would hang forever.
|
|
131
|
+
if (this.cancelled) {
|
|
132
|
+
throw new TopTreeCancelledError();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const { promise: completionPromise, resolve, reject } = promiseWithResolvers<void>();
|
|
136
|
+
// The completion promise is awaited inside the try/catch below. Attach a no-op catch
|
|
137
|
+
// here as well so any spurious unhandled-rejection detection during cancellation
|
|
138
|
+
// (where reject() can fire synchronously before the await microtask installs a handler)
|
|
139
|
+
// is silenced.
|
|
140
|
+
completionPromise.catch(() => {});
|
|
141
|
+
const startBlobAccumulator = BatchedBlobAccumulator.newWithChallenges(finalBlobBatchingChallenges);
|
|
142
|
+
|
|
143
|
+
this.state = new TopTreeProvingState(
|
|
144
|
+
epochNumber,
|
|
145
|
+
totalNumCheckpoints,
|
|
146
|
+
finalBlobBatchingChallenges,
|
|
147
|
+
startBlobAccumulator,
|
|
148
|
+
resolve,
|
|
149
|
+
reason => reject(this.cancelled ? new TopTreeCancelledError(reason) : new Error(reason)),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
// Compute the full out-hash hint chain and per-checkpoint start blob accumulators
|
|
153
|
+
// synchronously from archiver data. No proving required.
|
|
154
|
+
const outHashHints = await this.computeOutHashHints(checkpointData);
|
|
155
|
+
const checkpointStartBlobs: BatchedBlobAccumulator[] = [];
|
|
156
|
+
let runningBlobAccumulator = startBlobAccumulator;
|
|
157
|
+
for (const cd of checkpointData) {
|
|
158
|
+
checkpointStartBlobs.push(runningBlobAccumulator);
|
|
159
|
+
runningBlobAccumulator = await runningBlobAccumulator.accumulateFields(cd.blobFields);
|
|
160
|
+
}
|
|
161
|
+
this.state.setEndBlobAccumulator(runningBlobAccumulator);
|
|
162
|
+
|
|
163
|
+
// For each checkpoint, await its block proofs promise then enqueue the checkpoint root.
|
|
164
|
+
// Each await runs independently — checkpoints whose sub-trees finish first start their
|
|
165
|
+
// root proofs first, in parallel with later checkpoints' block-level proving.
|
|
166
|
+
for (let i = 0; i < checkpointData.length; i++) {
|
|
167
|
+
const cd = checkpointData[i];
|
|
168
|
+
const checkpointIndex = i;
|
|
169
|
+
void cd.blockProofs.then(
|
|
170
|
+
blockProofs => {
|
|
171
|
+
if (this.cancelled || !this.state?.verifyState()) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
this.enqueueCheckpointRoot(
|
|
175
|
+
this.state,
|
|
176
|
+
checkpointIndex,
|
|
177
|
+
blockProofs,
|
|
178
|
+
cd,
|
|
179
|
+
outHashHints[i],
|
|
180
|
+
checkpointStartBlobs[i],
|
|
181
|
+
);
|
|
182
|
+
},
|
|
183
|
+
err => {
|
|
184
|
+
if (this.cancelled) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this.state?.reject(`Sub-tree for checkpoint ${i} failed: ${err}`);
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
await completionPromise;
|
|
194
|
+
await this.state.finalizeBatchedBlob();
|
|
195
|
+
return this.state.getEpochProofResult();
|
|
196
|
+
} catch (err: any) {
|
|
197
|
+
if (this.cancelled) {
|
|
198
|
+
throw new TopTreeCancelledError();
|
|
199
|
+
}
|
|
200
|
+
throw err;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Cancels in-flight proving. If `abortJobs` is true, each pending broker job is aborted
|
|
206
|
+
* (used on reorg, when the surviving checkpoint set differs and the in-flight jobs'
|
|
207
|
+
* inputs are no longer valid). On shutdown the caller passes `false` so jobs remain in
|
|
208
|
+
* the broker queue for reuse on restart.
|
|
209
|
+
*/
|
|
210
|
+
public cancel({ abortJobs }: { abortJobs: boolean }) {
|
|
211
|
+
this.cancelled = true;
|
|
212
|
+
this.resetSchedulerState(abortJobs);
|
|
213
|
+
this.state?.cancel();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Standard shutdown — preserve the broker queue (`abortJobs: false`). */
|
|
217
|
+
protected override cancelInternal(): void {
|
|
218
|
+
this.cancel({ abortJobs: false });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// --- internal: per-checkpoint enqueue path ---
|
|
222
|
+
|
|
223
|
+
protected override onRootRollupComplete(state: TopTreeProvingState) {
|
|
224
|
+
state.resolve();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private enqueueCheckpointRoot(
|
|
228
|
+
state: TopTreeProvingState,
|
|
229
|
+
checkpointIndex: number,
|
|
230
|
+
blockProofs: PublicInputsAndRecursiveProof<
|
|
231
|
+
BlockRollupPublicInputs,
|
|
232
|
+
typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
|
|
233
|
+
>[],
|
|
234
|
+
cd: CheckpointTopTreeData,
|
|
235
|
+
outHashHint: OutHashHint,
|
|
236
|
+
startBlobAccumulator: BatchedBlobAccumulator,
|
|
237
|
+
) {
|
|
238
|
+
void this.buildCheckpointRootInputs(blockProofs, cd, outHashHint, startBlobAccumulator).then(inputs => {
|
|
239
|
+
this.deferredProving(
|
|
240
|
+
state,
|
|
241
|
+
signal => {
|
|
242
|
+
if (inputs instanceof CheckpointRootSingleBlockRollupPrivateInputs) {
|
|
243
|
+
return this.prover.getCheckpointRootSingleBlockRollupProof(inputs, signal, state.epochNumber);
|
|
244
|
+
}
|
|
245
|
+
return this.prover.getCheckpointRootRollupProof(inputs, signal, state.epochNumber);
|
|
246
|
+
},
|
|
247
|
+
result => {
|
|
248
|
+
this.logger.debug(`Completed checkpoint root proof for checkpoint ${checkpointIndex}`);
|
|
249
|
+
const leafLocation = state.setCheckpointRootRollupProof(checkpointIndex, result);
|
|
250
|
+
if (state.totalNumCheckpoints === 1) {
|
|
251
|
+
this.enqueueEpochPadding(state);
|
|
252
|
+
} else {
|
|
253
|
+
this.checkAndEnqueueNextCheckpointMergeRollup(state, leafLocation);
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private async buildCheckpointRootInputs(
|
|
261
|
+
blockProofs: PublicInputsAndRecursiveProof<
|
|
262
|
+
BlockRollupPublicInputs,
|
|
263
|
+
typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
|
|
264
|
+
>[],
|
|
265
|
+
cd: CheckpointTopTreeData,
|
|
266
|
+
outHashHint: OutHashHint,
|
|
267
|
+
startBlobAccumulator: BatchedBlobAccumulator,
|
|
268
|
+
) {
|
|
269
|
+
const { blobCommitments, blobsHash } = await buildBlobHints(cd.blobFields);
|
|
270
|
+
|
|
271
|
+
const hints = CheckpointRootRollupHints.from({
|
|
272
|
+
previousBlockHeader: cd.previousBlockHeader,
|
|
273
|
+
previousArchiveSiblingPath: cd.previousArchiveSiblingPath,
|
|
274
|
+
previousOutHash: outHashHint.treeSnapshot,
|
|
275
|
+
newOutHashSiblingPath: outHashHint.siblingPath,
|
|
276
|
+
startBlobAccumulator: startBlobAccumulator.toBlobAccumulator(),
|
|
277
|
+
finalBlobChallenges: this.state!.finalBlobBatchingChallenges,
|
|
278
|
+
blobFields: padArrayEnd(cd.blobFields, Fr.ZERO, FIELDS_PER_BLOB * BLOBS_PER_CHECKPOINT),
|
|
279
|
+
blobCommitments: padArrayEnd(blobCommitments, BLS12Point.ZERO, BLOBS_PER_CHECKPOINT),
|
|
280
|
+
blobsHash,
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const proofDatas = blockProofs.map(p => toProofData(p));
|
|
284
|
+
return proofDatas.length === 1
|
|
285
|
+
? new CheckpointRootSingleBlockRollupPrivateInputs(proofDatas[0], hints)
|
|
286
|
+
: new CheckpointRootRollupPrivateInputs([proofDatas[0], proofDatas[1]], hints);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private async computeOutHashHints(checkpointData: CheckpointTopTreeData[]): Promise<OutHashHint[]> {
|
|
290
|
+
const treeCalculator = await MerkleTreeCalculator.create(OUT_HASH_TREE_HEIGHT, undefined, (left, right) =>
|
|
291
|
+
Promise.resolve(shaMerkleHash(left, right)),
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
const computeHint = async (leaves: Fr[]): Promise<OutHashHint> => {
|
|
295
|
+
const tree = await treeCalculator.computeTree(leaves.map(l => l.toBuffer()));
|
|
296
|
+
const nextAvailableLeafIndex = leaves.length;
|
|
297
|
+
return {
|
|
298
|
+
treeSnapshot: new AppendOnlyTreeSnapshot(Fr.fromBuffer(tree.root), nextAvailableLeafIndex),
|
|
299
|
+
siblingPath: tree.getSiblingPath(nextAvailableLeafIndex).map(Fr.fromBuffer) as Tuple<
|
|
300
|
+
Fr,
|
|
301
|
+
typeof OUT_HASH_TREE_HEIGHT
|
|
302
|
+
>,
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const hints: OutHashHint[] = [];
|
|
307
|
+
const outHashes: Fr[] = [];
|
|
308
|
+
for (const cd of checkpointData) {
|
|
309
|
+
hints.push(await computeHint(outHashes));
|
|
310
|
+
outHashes.push(computeCheckpointOutHash(cd.l2ToL1MsgsPerBlock));
|
|
311
|
+
}
|
|
312
|
+
return hints;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import type { NESTED_RECURSIVE_PROOF_LENGTH, NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH } from '@aztec/constants';
|
|
2
|
+
import type { EpochNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import type { LoggerBindings } from '@aztec/foundation/log';
|
|
4
|
+
import type { TreeNodeLocation } from '@aztec/foundation/trees';
|
|
5
|
+
import type { PublicInputsAndRecursiveProof, ServerCircuitProver } from '@aztec/stdlib/interfaces/server';
|
|
6
|
+
import type {
|
|
7
|
+
CheckpointMergeRollupPrivateInputs,
|
|
8
|
+
CheckpointPaddingRollupPrivateInputs,
|
|
9
|
+
CheckpointRollupPublicInputs,
|
|
10
|
+
RootRollupPrivateInputs,
|
|
11
|
+
RootRollupPublicInputs,
|
|
12
|
+
} from '@aztec/stdlib/rollup';
|
|
13
|
+
|
|
14
|
+
import { ProvingScheduler, type ProvingStateLike } from './proving-scheduler.js';
|
|
15
|
+
|
|
16
|
+
type CheckpointRollupProof = PublicInputsAndRecursiveProof<
|
|
17
|
+
CheckpointRollupPublicInputs,
|
|
18
|
+
typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
type RootRollupProof = PublicInputsAndRecursiveProof<RootRollupPublicInputs, typeof NESTED_RECURSIVE_PROOF_LENGTH>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* State interface required by the top-tree proving drivers (checkpoint-merge → padding →
|
|
25
|
+
* root-rollup). Both `EpochProvingState` and `TopTreeProvingState` satisfy it structurally;
|
|
26
|
+
* the per-checkpoint state in `EpochProvingState` (block/tx proving, world-state forks)
|
|
27
|
+
* is owned outside this surface.
|
|
28
|
+
*/
|
|
29
|
+
export interface TopTreeStateLike extends ProvingStateLike {
|
|
30
|
+
readonly epochNumber: EpochNumber;
|
|
31
|
+
readonly totalNumCheckpoints: number;
|
|
32
|
+
|
|
33
|
+
tryStartProvingCheckpointMerge(location: TreeNodeLocation): boolean;
|
|
34
|
+
setCheckpointMergeRollupProof(location: TreeNodeLocation, provingOutput: CheckpointRollupProof): void;
|
|
35
|
+
isReadyForCheckpointMerge(location: TreeNodeLocation): boolean;
|
|
36
|
+
getParentLocation(location: TreeNodeLocation): TreeNodeLocation;
|
|
37
|
+
getCheckpointMergeRollupInputs(location: TreeNodeLocation): CheckpointMergeRollupPrivateInputs;
|
|
38
|
+
|
|
39
|
+
tryStartProvingPaddingCheckpoint(): boolean;
|
|
40
|
+
setCheckpointPaddingProof(provingOutput: CheckpointRollupProof): void;
|
|
41
|
+
getPaddingCheckpointInputs(): CheckpointPaddingRollupPrivateInputs;
|
|
42
|
+
|
|
43
|
+
tryStartProvingRootRollup(): boolean;
|
|
44
|
+
setRootRollupProof(provingOutput: RootRollupProof): void;
|
|
45
|
+
isReadyForRootRollup(): boolean;
|
|
46
|
+
getRootRollupInputs(): RootRollupPrivateInputs;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Shared scheduling for the top-tree section of epoch proving — checkpoint-merge,
|
|
51
|
+
* padding (single-checkpoint case), and root rollup. Both `ProvingOrchestrator` and
|
|
52
|
+
* `TopTreeOrchestrator` extend this; their per-checkpoint-root drivers diverge (one
|
|
53
|
+
* drains state-derived inputs once block-merge is done, the other builds inputs from
|
|
54
|
+
* caller-supplied checkpoint data), but the rest of the tree is identical.
|
|
55
|
+
*
|
|
56
|
+
* Subclasses provide a `wrapCircuitCall` hook for telemetry (the orchestrator wraps
|
|
57
|
+
* each call in a span; the top-tree leaves it as identity), and an
|
|
58
|
+
* `onRootRollupComplete` hook to invoke the right shape of `state.resolve()` —
|
|
59
|
+
* `EpochProvingState.resolve` takes a `ProvingResult`, `TopTreeProvingState.resolve`
|
|
60
|
+
* is no-arg.
|
|
61
|
+
*/
|
|
62
|
+
export abstract class TopTreeProvingScheduler extends ProvingScheduler {
|
|
63
|
+
constructor(
|
|
64
|
+
protected readonly prover: ServerCircuitProver,
|
|
65
|
+
enqueueConcurrency: number,
|
|
66
|
+
loggerName?: string,
|
|
67
|
+
bindings?: LoggerBindings,
|
|
68
|
+
) {
|
|
69
|
+
super(enqueueConcurrency, loggerName, bindings);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Wraps a circuit call for telemetry. Default is identity; the orchestrator overrides
|
|
74
|
+
* to wrap with `wrapCallbackInSpan`.
|
|
75
|
+
*/
|
|
76
|
+
protected wrapCircuitCall<T>(
|
|
77
|
+
_circuitName: string,
|
|
78
|
+
fn: (signal: AbortSignal) => Promise<T>,
|
|
79
|
+
): (signal: AbortSignal) => Promise<T> {
|
|
80
|
+
return fn;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Called once the root rollup proof has been set; subclasses call `state.resolve(...)` with the right shape. */
|
|
84
|
+
protected abstract onRootRollupComplete(state: TopTreeStateLike): void;
|
|
85
|
+
|
|
86
|
+
protected enqueueCheckpointMergeRollup(state: TopTreeStateLike, location: TreeNodeLocation) {
|
|
87
|
+
if (!state.verifyState() || !state.tryStartProvingCheckpointMerge(location)) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const inputs = state.getCheckpointMergeRollupInputs(location);
|
|
91
|
+
this.deferredProving(
|
|
92
|
+
state,
|
|
93
|
+
this.wrapCircuitCall('rollup-checkpoint-merge', signal =>
|
|
94
|
+
this.prover.getCheckpointMergeRollupProof(inputs, signal, state.epochNumber),
|
|
95
|
+
),
|
|
96
|
+
result => {
|
|
97
|
+
state.setCheckpointMergeRollupProof(location, result);
|
|
98
|
+
this.checkAndEnqueueNextCheckpointMergeRollup(state, location);
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
protected enqueueEpochPadding(state: TopTreeStateLike) {
|
|
104
|
+
if (!state.verifyState() || !state.tryStartProvingPaddingCheckpoint()) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const inputs = state.getPaddingCheckpointInputs();
|
|
108
|
+
this.deferredProving(
|
|
109
|
+
state,
|
|
110
|
+
this.wrapCircuitCall('rollup-checkpoint-padding', signal =>
|
|
111
|
+
this.prover.getCheckpointPaddingRollupProof(inputs, signal, state.epochNumber),
|
|
112
|
+
),
|
|
113
|
+
result => {
|
|
114
|
+
state.setCheckpointPaddingProof(result);
|
|
115
|
+
this.checkAndEnqueueRootRollup(state);
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
protected enqueueRootRollup(state: TopTreeStateLike) {
|
|
121
|
+
if (!state.verifyState() || !state.tryStartProvingRootRollup()) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const inputs = state.getRootRollupInputs();
|
|
125
|
+
this.deferredProving(
|
|
126
|
+
state,
|
|
127
|
+
this.wrapCircuitCall('rollup-root', signal => this.prover.getRootRollupProof(inputs, signal, state.epochNumber)),
|
|
128
|
+
result => {
|
|
129
|
+
this.logger.verbose(`Completed root rollup for epoch ${state.epochNumber}`);
|
|
130
|
+
state.setRootRollupProof(result);
|
|
131
|
+
this.onRootRollupComplete(state);
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
protected checkAndEnqueueNextCheckpointMergeRollup(state: TopTreeStateLike, currentLocation: TreeNodeLocation) {
|
|
137
|
+
if (!state.isReadyForCheckpointMerge(currentLocation)) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const parentLocation = state.getParentLocation(currentLocation);
|
|
141
|
+
if (parentLocation.level === 0) {
|
|
142
|
+
this.checkAndEnqueueRootRollup(state);
|
|
143
|
+
} else {
|
|
144
|
+
this.enqueueCheckpointMergeRollup(state, parentLocation);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
protected checkAndEnqueueRootRollup(state: TopTreeStateLike) {
|
|
149
|
+
if (!state.isReadyForRootRollup()) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
this.enqueueRootRollup(state);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import type { BatchedBlob, BatchedBlobAccumulator, FinalBlobBatchingChallenges } from '@aztec/blob-lib';
|
|
2
|
+
import type { NESTED_RECURSIVE_PROOF_LENGTH, NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH } from '@aztec/constants';
|
|
3
|
+
import { EpochNumber } from '@aztec/foundation/branded-types';
|
|
4
|
+
import { type TreeNodeLocation, UnbalancedTreeStore } from '@aztec/foundation/trees';
|
|
5
|
+
import type { PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
|
|
6
|
+
import type { Proof } from '@aztec/stdlib/proofs';
|
|
7
|
+
import {
|
|
8
|
+
CheckpointMergeRollupPrivateInputs,
|
|
9
|
+
CheckpointPaddingRollupPrivateInputs,
|
|
10
|
+
CheckpointRollupPublicInputs,
|
|
11
|
+
RootRollupPrivateInputs,
|
|
12
|
+
type RootRollupPublicInputs,
|
|
13
|
+
} from '@aztec/stdlib/rollup';
|
|
14
|
+
|
|
15
|
+
import { toProofData } from './block-building-helpers.js';
|
|
16
|
+
import type { ProofState } from './block-proving-state.js';
|
|
17
|
+
|
|
18
|
+
enum TOP_TREE_LIFECYCLE {
|
|
19
|
+
CREATED,
|
|
20
|
+
RESOLVED,
|
|
21
|
+
REJECTED,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Lean top-tree-only state. Owns the merge tree of checkpoint root proofs, the
|
|
26
|
+
* single-checkpoint padding proof slot, the final root rollup proof, and the blob
|
|
27
|
+
* accumulator endpoints needed to finalise the epoch's batched blob proof.
|
|
28
|
+
*
|
|
29
|
+
* Constructed with `totalNumCheckpoints` and `finalBlobBatchingChallenges` upfront —
|
|
30
|
+
* by the time the top tree starts, all checkpoints are known and the challenges are
|
|
31
|
+
* derivable from their blob fields.
|
|
32
|
+
*/
|
|
33
|
+
export class TopTreeProvingState {
|
|
34
|
+
private checkpointProofs: UnbalancedTreeStore<
|
|
35
|
+
ProofState<CheckpointRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
|
|
36
|
+
>;
|
|
37
|
+
private checkpointPaddingProof:
|
|
38
|
+
| ProofState<CheckpointRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
|
|
39
|
+
| undefined;
|
|
40
|
+
private rootRollupProof: ProofState<RootRollupPublicInputs, typeof NESTED_RECURSIVE_PROOF_LENGTH> | undefined;
|
|
41
|
+
private endBlobAccumulator: BatchedBlobAccumulator | undefined;
|
|
42
|
+
private finalBatchedBlob: BatchedBlob | undefined;
|
|
43
|
+
private lifecycle = TOP_TREE_LIFECYCLE.CREATED;
|
|
44
|
+
|
|
45
|
+
constructor(
|
|
46
|
+
public readonly epochNumber: EpochNumber,
|
|
47
|
+
public readonly totalNumCheckpoints: number,
|
|
48
|
+
public readonly finalBlobBatchingChallenges: FinalBlobBatchingChallenges,
|
|
49
|
+
public readonly startBlobAccumulator: BatchedBlobAccumulator,
|
|
50
|
+
private readonly completionCallback: () => void,
|
|
51
|
+
private readonly rejectionCallback: (reason: string) => void,
|
|
52
|
+
) {
|
|
53
|
+
if (totalNumCheckpoints < 1) {
|
|
54
|
+
throw new Error(`TopTreeProvingState requires at least one checkpoint; got ${totalNumCheckpoints}.`);
|
|
55
|
+
}
|
|
56
|
+
this.checkpointProofs = new UnbalancedTreeStore(totalNumCheckpoints);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// --- checkpoint root rollup ---
|
|
60
|
+
|
|
61
|
+
public setCheckpointRootRollupProof(
|
|
62
|
+
checkpointIndex: number,
|
|
63
|
+
provingOutput: PublicInputsAndRecursiveProof<
|
|
64
|
+
CheckpointRollupPublicInputs,
|
|
65
|
+
typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
|
|
66
|
+
>,
|
|
67
|
+
): TreeNodeLocation {
|
|
68
|
+
return this.checkpointProofs.setLeaf(checkpointIndex, { provingOutput });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// --- checkpoint merge rollup ---
|
|
72
|
+
|
|
73
|
+
public tryStartProvingCheckpointMerge(location: TreeNodeLocation) {
|
|
74
|
+
if (this.checkpointProofs.getNode(location)?.isProving) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
this.checkpointProofs.setNode(location, { isProving: true });
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public setCheckpointMergeRollupProof(
|
|
82
|
+
location: TreeNodeLocation,
|
|
83
|
+
provingOutput: PublicInputsAndRecursiveProof<
|
|
84
|
+
CheckpointRollupPublicInputs,
|
|
85
|
+
typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
|
|
86
|
+
>,
|
|
87
|
+
) {
|
|
88
|
+
this.checkpointProofs.setNode(location, { provingOutput });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public isReadyForCheckpointMerge(location: TreeNodeLocation) {
|
|
92
|
+
return !!this.checkpointProofs.getSibling(location)?.provingOutput;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public getParentLocation(location: TreeNodeLocation) {
|
|
96
|
+
return this.checkpointProofs.getParentLocation(location);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public getCheckpointMergeRollupInputs(mergeLocation: TreeNodeLocation) {
|
|
100
|
+
const [left, right] = this.checkpointProofs.getChildren(mergeLocation).map(c => c?.provingOutput);
|
|
101
|
+
if (!left || !right) {
|
|
102
|
+
throw new Error('At least one child is not ready for the checkpoint merge rollup.');
|
|
103
|
+
}
|
|
104
|
+
return new CheckpointMergeRollupPrivateInputs([toProofData(left), toProofData(right)]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// --- padding (single-checkpoint case) ---
|
|
108
|
+
|
|
109
|
+
public tryStartProvingPaddingCheckpoint() {
|
|
110
|
+
if (this.checkpointPaddingProof?.isProving) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
this.checkpointPaddingProof = { isProving: true };
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public setCheckpointPaddingProof(
|
|
118
|
+
provingOutput: PublicInputsAndRecursiveProof<
|
|
119
|
+
CheckpointRollupPublicInputs,
|
|
120
|
+
typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
|
|
121
|
+
>,
|
|
122
|
+
) {
|
|
123
|
+
this.checkpointPaddingProof = { provingOutput };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public getPaddingCheckpointInputs() {
|
|
127
|
+
return new CheckpointPaddingRollupPrivateInputs();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// --- root rollup ---
|
|
131
|
+
|
|
132
|
+
public tryStartProvingRootRollup() {
|
|
133
|
+
if (this.rootRollupProof?.isProving) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
this.rootRollupProof = { isProving: true };
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public setRootRollupProof(provingOutput: PublicInputsAndRecursiveProof<RootRollupPublicInputs>) {
|
|
141
|
+
this.rootRollupProof = { provingOutput };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public isReadyForRootRollup() {
|
|
145
|
+
const childProofs = this.#getChildProofsForRoot();
|
|
146
|
+
return childProofs.every(p => !!p);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public getRootRollupInputs() {
|
|
150
|
+
const [left, right] = this.#getChildProofsForRoot();
|
|
151
|
+
if (!left || !right) {
|
|
152
|
+
throw new Error('At least one child is not ready for the root rollup.');
|
|
153
|
+
}
|
|
154
|
+
return RootRollupPrivateInputs.from({
|
|
155
|
+
previousRollups: [toProofData(left), toProofData(right)],
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// --- blob accumulator finalisation ---
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Sets the end-of-epoch blob accumulator, computed by the top-tree orchestrator
|
|
163
|
+
* from the surviving checkpoints' blob fields. Required before `finalizeBatchedBlob`.
|
|
164
|
+
*/
|
|
165
|
+
public setEndBlobAccumulator(accumulator: BatchedBlobAccumulator) {
|
|
166
|
+
this.endBlobAccumulator = accumulator;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
public async finalizeBatchedBlob() {
|
|
170
|
+
if (!this.endBlobAccumulator) {
|
|
171
|
+
throw new Error('End blob accumulator not set; call setEndBlobAccumulator before finalize.');
|
|
172
|
+
}
|
|
173
|
+
this.finalBatchedBlob = await this.endBlobAccumulator.finalize(true /* verifyProof */);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public getEpochProofResult(): { proof: Proof; publicInputs: RootRollupPublicInputs; batchedBlobInputs: BatchedBlob } {
|
|
177
|
+
const provingOutput = this.rootRollupProof?.provingOutput;
|
|
178
|
+
if (!provingOutput || !this.finalBatchedBlob) {
|
|
179
|
+
throw new Error('Top-tree proof not ready; root rollup or batched blob missing.');
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
proof: provingOutput.proof.binaryProof,
|
|
183
|
+
publicInputs: provingOutput.inputs,
|
|
184
|
+
batchedBlobInputs: this.finalBatchedBlob,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// --- lifecycle ---
|
|
189
|
+
|
|
190
|
+
public verifyState() {
|
|
191
|
+
return this.lifecycle === TOP_TREE_LIFECYCLE.CREATED;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public resolve() {
|
|
195
|
+
if (!this.verifyState()) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
this.lifecycle = TOP_TREE_LIFECYCLE.RESOLVED;
|
|
199
|
+
this.completionCallback();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public reject(reason: string) {
|
|
203
|
+
if (!this.verifyState()) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
this.lifecycle = TOP_TREE_LIFECYCLE.REJECTED;
|
|
207
|
+
this.rejectionCallback(reason);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
public cancel() {
|
|
211
|
+
this.reject('Top-tree proving cancelled');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
#getChildProofsForRoot() {
|
|
215
|
+
const rootLocation = { level: 0, index: 0 };
|
|
216
|
+
return this.totalNumCheckpoints === 1
|
|
217
|
+
? [this.checkpointProofs.getNode(rootLocation)?.provingOutput, this.checkpointPaddingProof?.provingOutput]
|
|
218
|
+
: this.checkpointProofs.getChildren(rootLocation).map(c => c?.provingOutput);
|
|
219
|
+
}
|
|
220
|
+
}
|