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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dest/light/lightweight_checkpoint_builder.d.ts +9 -5
  2. package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -1
  3. package/dest/light/lightweight_checkpoint_builder.js +34 -18
  4. package/dest/mocks/test_context.js +5 -2
  5. package/dest/orchestrator/block-building-helpers.d.ts +4 -4
  6. package/dest/orchestrator/block-building-helpers.d.ts.map +1 -1
  7. package/dest/orchestrator/block-building-helpers.js +2 -2
  8. package/dest/orchestrator/block-proving-state.d.ts +4 -1
  9. package/dest/orchestrator/block-proving-state.d.ts.map +1 -1
  10. package/dest/orchestrator/block-proving-state.js +7 -0
  11. package/dest/orchestrator/checkpoint-proving-state.d.ts +3 -3
  12. package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
  13. package/dest/orchestrator/checkpoint-proving-state.js +3 -3
  14. package/dest/orchestrator/epoch-proving-state.d.ts +2 -2
  15. package/dest/orchestrator/epoch-proving-state.d.ts.map +1 -1
  16. package/dest/orchestrator/epoch-proving-state.js +5 -3
  17. package/dest/orchestrator/orchestrator.d.ts +2 -2
  18. package/dest/orchestrator/orchestrator.d.ts.map +1 -1
  19. package/dest/orchestrator/orchestrator.js +43 -52
  20. package/dest/proving_broker/proving_broker.d.ts +1 -1
  21. package/dest/proving_broker/proving_broker.d.ts.map +1 -1
  22. package/dest/proving_broker/proving_broker.js +7 -3
  23. package/dest/proving_broker/proving_broker_instrumentation.d.ts +3 -1
  24. package/dest/proving_broker/proving_broker_instrumentation.d.ts.map +1 -1
  25. package/dest/proving_broker/proving_broker_instrumentation.js +7 -0
  26. package/dest/test/mock_prover.d.ts +1 -1
  27. package/package.json +17 -18
  28. package/src/light/lightweight_checkpoint_builder.ts +31 -19
  29. package/src/mocks/test_context.ts +2 -2
  30. package/src/orchestrator/block-building-helpers.ts +2 -2
  31. package/src/orchestrator/block-proving-state.ts +9 -0
  32. package/src/orchestrator/checkpoint-proving-state.ts +4 -4
  33. package/src/orchestrator/epoch-proving-state.ts +6 -4
  34. package/src/orchestrator/orchestrator.ts +53 -59
  35. package/src/proving_broker/proving_broker.ts +7 -2
  36. package/src/proving_broker/proving_broker_instrumentation.ts +9 -0
@@ -85,7 +85,7 @@ export class CheckpointProvingState {
85
85
  typeof L1_TO_L2_MSG_SUBTREE_ROOT_SIBLING_PATH_LENGTH
86
86
  >,
87
87
  public parentEpoch: EpochProvingState,
88
- private onBlobAccumulatorSet: (checkpoint: CheckpointProvingState) => void,
88
+ private onBlobAccumulatorSet: (checkpoint: CheckpointProvingState) => Promise<void>,
89
89
  ) {
90
90
  this.blockProofs = new UnbalancedTreeStore(totalNumBlocks);
91
91
  this.firstBlockNumber = BlockNumber(headerOfLastBlockInPreviousCheckpoint.globalVariables.blockNumber + 1);
@@ -245,7 +245,7 @@ export class CheckpointProvingState {
245
245
  this.endBlobAccumulator = await accumulateBlobs(this.blobFields!, startBlobAccumulator);
246
246
  this.startBlobAccumulator = startBlobAccumulator;
247
247
 
248
- this.onBlobAccumulatorSet(this);
248
+ await this.onBlobAccumulatorSet(this);
249
249
 
250
250
  return this.endBlobAccumulator;
251
251
  }
@@ -271,7 +271,7 @@ export class CheckpointProvingState {
271
271
  return this.totalNumBlocks === 1 ? 'rollup-checkpoint-root-single-block' : 'rollup-checkpoint-root';
272
272
  }
273
273
 
274
- public getCheckpointRootRollupInputs() {
274
+ public async getCheckpointRootRollupInputs() {
275
275
  const proofs = this.#getChildProofsForRoot();
276
276
  const nonEmptyProofs = proofs.filter(p => !!p);
277
277
  if (proofs.length !== nonEmptyProofs.length) {
@@ -287,7 +287,7 @@ export class CheckpointProvingState {
287
287
  // `blobFields` must've been set if `startBlobAccumulator` is set (in `accumulateBlobs`).
288
288
  const blobFields = this.blobFields!;
289
289
 
290
- const { blobCommitments, blobsHash } = buildBlobHints(blobFields);
290
+ const { blobCommitments, blobsHash } = await buildBlobHints(blobFields);
291
291
 
292
292
  const hints = CheckpointRootRollupHints.from({
293
293
  previousBlockHeader: this.headerOfLastBlockInPreviousCheckpoint,
@@ -76,7 +76,7 @@ export class EpochProvingState {
76
76
  public readonly epochNumber: EpochNumber,
77
77
  public readonly totalNumCheckpoints: number,
78
78
  private readonly finalBlobBatchingChallenges: FinalBlobBatchingChallenges,
79
- private onCheckpointBlobAccumulatorSet: (checkpoint: CheckpointProvingState) => void,
79
+ private onCheckpointBlobAccumulatorSet: (checkpoint: CheckpointProvingState) => Promise<void>,
80
80
  private completionCallback: (result: ProvingResult) => void,
81
81
  private rejectionCallback: (reason: string) => void,
82
82
  ) {
@@ -254,9 +254,11 @@ export class EpochProvingState {
254
254
  }
255
255
  outHashes.push(outHash);
256
256
 
257
- // Get or create hints for the next checkpoint.
258
- hint = checkpoint.getOutHashHintForNextCheckpoint() ?? (await computeOutHashHint(outHashes));
259
- checkpoint.setOutHashHintForNextCheckpoint(hint);
257
+ // If this is NOT the last checkpoint, get or create the hint for the next checkpoint.
258
+ if (i !== this.totalNumCheckpoints - 1) {
259
+ hint = checkpoint.getOutHashHintForNextCheckpoint() ?? (await computeOutHashHint(outHashes));
260
+ checkpoint.setOutHashHintForNextCheckpoint(hint);
261
+ }
260
262
  }
261
263
  }
262
264
 
@@ -71,11 +71,6 @@ import { EpochProvingState, type ProvingResult, type TreeSnapshots } from './epo
71
71
  import { ProvingOrchestratorMetrics } from './orchestrator_metrics.js';
72
72
  import { TxProvingState } from './tx-proving-state.js';
73
73
 
74
- type WorldStateFork = {
75
- fork: MerkleTreeWriteOperations;
76
- cleanupPromise: Promise<void> | undefined;
77
- };
78
-
79
74
  /**
80
75
  * Implements an event driven proving scheduler to build the recursive proof tree. The idea being:
81
76
  * 1. Transactions are provided to the scheduler post simulation.
@@ -97,7 +92,7 @@ export class ProvingOrchestrator implements EpochProver {
97
92
  private provingPromise: Promise<ProvingResult> | undefined = undefined;
98
93
  private metrics: ProvingOrchestratorMetrics;
99
94
  // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
100
- private dbs: Map<BlockNumber, WorldStateFork> = new Map();
95
+ private dbs: Map<BlockNumber, MerkleTreeWriteOperations> = new Map();
101
96
  private logger: Logger;
102
97
 
103
98
  constructor(
@@ -182,7 +177,7 @@ export class ProvingOrchestrator implements EpochProver {
182
177
  const db = await this.dbProvider.fork(lastBlockNumber);
183
178
 
184
179
  const firstBlockNumber = BlockNumber(lastBlockNumber + 1);
185
- this.dbs.set(firstBlockNumber, { fork: db, cleanupPromise: undefined });
180
+ this.dbs.set(firstBlockNumber, db);
186
181
 
187
182
  // Get archive sibling path before any block in this checkpoint lands.
188
183
  const lastArchiveSiblingPath = await getLastSiblingPath(MerkleTreeId.ARCHIVE, db);
@@ -240,9 +235,9 @@ export class ProvingOrchestrator implements EpochProver {
240
235
  if (!this.dbs.has(blockNumber)) {
241
236
  // Fork world state at the end of the immediately previous block
242
237
  const db = await this.dbProvider.fork(BlockNumber(blockNumber - 1));
243
- this.dbs.set(blockNumber, { fork: db, cleanupPromise: undefined });
238
+ this.dbs.set(blockNumber, db);
244
239
  }
245
- const db = this.dbs.get(blockNumber)!.fork;
240
+ const db = this.getDbForBlock(blockNumber);
246
241
 
247
242
  // Get archive snapshot and sibling path before any txs in this block lands.
248
243
  const lastArchiveTreeSnapshot = await getTreeSnapshot(MerkleTreeId.ARCHIVE, db);
@@ -317,7 +312,7 @@ export class ProvingOrchestrator implements EpochProver {
317
312
 
318
313
  this.logger.info(`Adding ${txs.length} transactions to block ${blockNumber}`);
319
314
 
320
- const db = this.dbs.get(blockNumber)!.fork;
315
+ const db = this.getDbForBlock(blockNumber);
321
316
  const lastArchive = provingState.lastArchiveTreeSnapshot;
322
317
  const newL1ToL2MessageTreeSnapshot = provingState.newL1ToL2MessageTreeSnapshot;
323
318
  const spongeBlobState = provingState.getStartSpongeBlob().clone();
@@ -445,14 +440,20 @@ export class ProvingOrchestrator implements EpochProver {
445
440
  throw new Error('Block header mismatch');
446
441
  }
447
442
 
448
- // Get db for this block
449
- const db = this.dbs.get(provingState.blockNumber)!.fork;
443
+ // Get db for this block and remove from map — no other code should use it after this point.
444
+ const db = this.getDbForBlock(provingState.blockNumber);
445
+ this.dbs.delete(provingState.blockNumber);
450
446
 
451
- // Update the archive tree, so we're ready to start processing the next block:
452
- this.logger.verbose(
453
- `Updating archive tree with block ${provingState.blockNumber} header ${(await header.hash()).toString()}`,
454
- );
455
- await db.updateArchive(header);
447
+ // Update the archive tree, capture the snapshot, and close the fork deterministically.
448
+ try {
449
+ this.logger.verbose(
450
+ `Updating archive tree with block ${provingState.blockNumber} header ${(await header.hash()).toString()}`,
451
+ );
452
+ await db.updateArchive(header);
453
+ provingState.setBuiltArchive(await getTreeSnapshot(MerkleTreeId.ARCHIVE, db));
454
+ } finally {
455
+ await db.close();
456
+ }
456
457
 
457
458
  await this.verifyBuiltBlockAgainstSyncedState(provingState);
458
459
 
@@ -472,6 +473,13 @@ export class ProvingOrchestrator implements EpochProver {
472
473
  this.logger.debug('Block root rollup proof not built yet, skipping header check.');
473
474
  return;
474
475
  }
476
+
477
+ const newArchive = provingState.getBuiltArchive();
478
+ if (!newArchive) {
479
+ this.logger.debug('Archive snapshot not yet captured, skipping header check.');
480
+ return;
481
+ }
482
+
475
483
  const header = await buildHeaderFromCircuitOutputs(output);
476
484
 
477
485
  if (!(await header.hash()).equals(await builtBlockHeader.hash())) {
@@ -480,11 +488,7 @@ export class ProvingOrchestrator implements EpochProver {
480
488
  return;
481
489
  }
482
490
 
483
- // Get db for this block
484
491
  const blockNumber = provingState.blockNumber;
485
- const db = this.dbs.get(blockNumber)!.fork;
486
-
487
- const newArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, db);
488
492
  const syncedArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, this.dbProvider.getSnapshot(blockNumber));
489
493
  if (!syncedArchive.equals(newArchive)) {
490
494
  this.logger.error(
@@ -502,12 +506,6 @@ export class ProvingOrchestrator implements EpochProver {
502
506
  provingState.reject(`New archive mismatch.`);
503
507
  return;
504
508
  }
505
-
506
- // TODO(palla/prover): This closes the fork only on the happy path. If this epoch orchestrator
507
- // is aborted and never reaches this point, it will leak the fork. We need to add a global cleanup,
508
- // but have to make sure it only runs once all operations are completed, otherwise some function here
509
- // will attempt to access the fork after it was closed.
510
- void this.cleanupDBFork(blockNumber);
511
509
  }
512
510
 
513
511
  /**
@@ -523,6 +521,19 @@ export class ProvingOrchestrator implements EpochProver {
523
521
  }
524
522
 
525
523
  this.provingState?.cancel();
524
+
525
+ for (const [blockNumber, db] of this.dbs.entries()) {
526
+ void db.close().catch(err => this.logger.error(`Error closing db for block ${blockNumber}`, err));
527
+ }
528
+ this.dbs.clear();
529
+ }
530
+
531
+ private getDbForBlock(blockNumber: BlockNumber): MerkleTreeWriteOperations {
532
+ const db = this.dbs.get(blockNumber);
533
+ if (!db) {
534
+ throw new Error(`World state fork for block ${blockNumber} not found.`);
535
+ }
536
+ return db;
526
537
  }
527
538
 
528
539
  /**
@@ -554,24 +565,6 @@ export class ProvingOrchestrator implements EpochProver {
554
565
  return epochProofResult;
555
566
  }
556
567
 
557
- private async cleanupDBFork(blockNumber: BlockNumber): Promise<void> {
558
- this.logger.debug(`Cleaning up world state fork for ${blockNumber}`);
559
- const fork = this.dbs.get(blockNumber);
560
- if (!fork) {
561
- return;
562
- }
563
-
564
- try {
565
- if (!fork.cleanupPromise) {
566
- fork.cleanupPromise = fork.fork.close();
567
- }
568
- await fork.cleanupPromise;
569
- this.dbs.delete(blockNumber);
570
- } catch (err) {
571
- this.logger.error(`Error closing db for block ${blockNumber}`, err);
572
- }
573
- }
574
-
575
568
  /**
576
569
  * Enqueue a job to be scheduled
577
570
  * @param provingState - The proving state object being operated on
@@ -894,17 +887,15 @@ export class ProvingOrchestrator implements EpochProver {
894
887
  const leafLocation = provingState.setBlockRootRollupProof(result);
895
888
  const checkpointProvingState = provingState.parentCheckpoint;
896
889
 
897
- // If the proofs were slower than the block header building, then we need to try validating the block header hashes here.
890
+ // Verification is called from both here and setBlockCompleted. Whichever runs last
891
+ // will be the first to see all three pieces (header, proof output, archive) and run the checks.
898
892
  await this.verifyBuiltBlockAgainstSyncedState(provingState);
899
893
 
900
894
  if (checkpointProvingState.totalNumBlocks === 1) {
901
- this.checkAndEnqueueCheckpointRootRollup(checkpointProvingState);
895
+ await this.checkAndEnqueueCheckpointRootRollup(checkpointProvingState);
902
896
  } else {
903
- this.checkAndEnqueueNextBlockMergeRollup(checkpointProvingState, leafLocation);
897
+ await this.checkAndEnqueueNextBlockMergeRollup(checkpointProvingState, leafLocation);
904
898
  }
905
-
906
- // We are finished with the block at this point, ensure the fork is cleaned up
907
- void this.cleanupDBFork(provingState.blockNumber);
908
899
  },
909
900
  );
910
901
  }
@@ -1009,14 +1000,14 @@ export class ProvingOrchestrator implements EpochProver {
1009
1000
  },
1010
1001
  signal => this.prover.getBlockMergeRollupProof(inputs, signal, provingState.epochNumber),
1011
1002
  ),
1012
- result => {
1003
+ async result => {
1013
1004
  provingState.setBlockMergeRollupProof(location, result);
1014
- this.checkAndEnqueueNextBlockMergeRollup(provingState, location);
1005
+ await this.checkAndEnqueueNextBlockMergeRollup(provingState, location);
1015
1006
  },
1016
1007
  );
1017
1008
  }
1018
1009
 
1019
- private enqueueCheckpointRootRollup(provingState: CheckpointProvingState) {
1010
+ private async enqueueCheckpointRootRollup(provingState: CheckpointProvingState) {
1020
1011
  if (!provingState.verifyState()) {
1021
1012
  this.logger.debug('Not running checkpoint root rollup. State no longer valid.');
1022
1013
  return;
@@ -1031,7 +1022,7 @@ export class ProvingOrchestrator implements EpochProver {
1031
1022
 
1032
1023
  this.logger.debug(`Enqueuing ${rollupType} for checkpoint ${provingState.index}.`);
1033
1024
 
1034
- const inputs = provingState.getCheckpointRootRollupInputs();
1025
+ const inputs = await provingState.getCheckpointRootRollupInputs();
1035
1026
 
1036
1027
  this.deferredProving(
1037
1028
  provingState,
@@ -1191,25 +1182,28 @@ export class ProvingOrchestrator implements EpochProver {
1191
1182
  this.enqueueBlockRootRollup(provingState);
1192
1183
  }
1193
1184
 
1194
- private checkAndEnqueueNextBlockMergeRollup(provingState: CheckpointProvingState, currentLocation: TreeNodeLocation) {
1185
+ private async checkAndEnqueueNextBlockMergeRollup(
1186
+ provingState: CheckpointProvingState,
1187
+ currentLocation: TreeNodeLocation,
1188
+ ) {
1195
1189
  if (!provingState.isReadyForBlockMerge(currentLocation)) {
1196
1190
  return;
1197
1191
  }
1198
1192
 
1199
1193
  const parentLocation = provingState.getParentLocation(currentLocation);
1200
1194
  if (parentLocation.level === 0) {
1201
- this.checkAndEnqueueCheckpointRootRollup(provingState);
1195
+ await this.checkAndEnqueueCheckpointRootRollup(provingState);
1202
1196
  } else {
1203
1197
  this.enqueueBlockMergeRollup(provingState, parentLocation);
1204
1198
  }
1205
1199
  }
1206
1200
 
1207
- private checkAndEnqueueCheckpointRootRollup(provingState: CheckpointProvingState) {
1201
+ private async checkAndEnqueueCheckpointRootRollup(provingState: CheckpointProvingState) {
1208
1202
  if (!provingState.isReadyForCheckpointRoot()) {
1209
1203
  return;
1210
1204
  }
1211
1205
 
1212
- this.enqueueCheckpointRootRollup(provingState);
1206
+ await this.enqueueCheckpointRootRollup(provingState);
1213
1207
  }
1214
1208
 
1215
1209
  private checkAndEnqueueNextCheckpointMergeRollup(provingState: EpochProvingState, currentLocation: TreeNodeLocation) {
@@ -314,7 +314,7 @@ export class ProvingBroker implements ProvingJobProducer, ProvingJobConsumer, Pr
314
314
  // notify listeners of the cancellation
315
315
  if (!this.resultsCache.has(id)) {
316
316
  this.logger.info(`Cancelling job id=${id}`, { provingJobId: id });
317
- await this.#reportProvingJobError(id, 'Aborted', false);
317
+ await this.#reportProvingJobError(id, 'Aborted', false, undefined, true);
318
318
  }
319
319
  }
320
320
 
@@ -395,6 +395,7 @@ export class ProvingBroker implements ProvingJobProducer, ProvingJobConsumer, Pr
395
395
  err: string,
396
396
  retry = false,
397
397
  filter?: ProvingJobFilter,
398
+ aborted = false,
398
399
  ): Promise<GetProvingJobResponse | undefined> {
399
400
  const info = this.inProgress.get(id);
400
401
  const item = this.jobsCache.get(id);
@@ -455,7 +456,11 @@ export class ProvingBroker implements ProvingJobProducer, ProvingJobConsumer, Pr
455
456
  this.promises.get(id)!.resolve(result);
456
457
  this.completedJobNotifications.push(id);
457
458
 
458
- this.instrumentation.incRejectedJobs(item.type);
459
+ if (aborted) {
460
+ this.instrumentation.incAbortedJobs(item.type);
461
+ } else {
462
+ this.instrumentation.incRejectedJobs(item.type);
463
+ }
459
464
  if (info) {
460
465
  const duration = this.msTimeSource() - info.startedAt;
461
466
  this.instrumentation.recordJobDuration(item.type, duration);
@@ -18,6 +18,7 @@ export class ProvingBrokerInstrumentation {
18
18
  private activeJobs: ObservableGauge;
19
19
  private resolvedJobs: UpDownCounter;
20
20
  private rejectedJobs: UpDownCounter;
21
+ private abortedJobs: UpDownCounter;
21
22
  private timedOutJobs: UpDownCounter;
22
23
  private cachedJobs: UpDownCounter;
23
24
  private totalJobs: UpDownCounter;
@@ -39,6 +40,8 @@ export class ProvingBrokerInstrumentation {
39
40
 
40
41
  this.rejectedJobs = createUpDownCounterWithDefault(meter, Metrics.PROVING_QUEUE_REJECTED_JOBS, provingJobAttrs);
41
42
 
43
+ this.abortedJobs = createUpDownCounterWithDefault(meter, Metrics.PROVING_QUEUE_ABORTED_JOBS, provingJobAttrs);
44
+
42
45
  this.retriedJobs = createUpDownCounterWithDefault(meter, Metrics.PROVING_QUEUE_RETRIED_JOBS, provingJobAttrs);
43
46
 
44
47
  this.timedOutJobs = createUpDownCounterWithDefault(meter, Metrics.PROVING_QUEUE_TIMED_OUT_JOBS, provingJobAttrs);
@@ -72,6 +75,12 @@ export class ProvingBrokerInstrumentation {
72
75
  });
73
76
  }
74
77
 
78
+ incAbortedJobs(proofType: ProvingRequestType) {
79
+ this.abortedJobs.add(1, {
80
+ [Attributes.PROVING_JOB_TYPE]: ProvingRequestType[proofType],
81
+ });
82
+ }
83
+
75
84
  incRetriedJobs(proofType: ProvingRequestType) {
76
85
  this.retriedJobs.add(1, {
77
86
  [Attributes.PROVING_JOB_TYPE]: ProvingRequestType[proofType],