@aztec/prover-client 0.0.1-commit.fcb71a6 → 0.0.1-commit.fffb133c

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 (77) hide show
  1. package/dest/light/lightweight_checkpoint_builder.d.ts +14 -6
  2. package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -1
  3. package/dest/light/lightweight_checkpoint_builder.js +57 -10
  4. package/dest/mocks/test_context.d.ts +4 -2
  5. package/dest/mocks/test_context.d.ts.map +1 -1
  6. package/dest/mocks/test_context.js +14 -3
  7. package/dest/orchestrator/block-building-helpers.d.ts +1 -1
  8. package/dest/orchestrator/block-building-helpers.js +2 -2
  9. package/dest/orchestrator/checkpoint-proving-state.d.ts +15 -2
  10. package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
  11. package/dest/orchestrator/checkpoint-proving-state.js +34 -1
  12. package/dest/orchestrator/epoch-proving-state.d.ts +5 -4
  13. package/dest/orchestrator/epoch-proving-state.d.ts.map +1 -1
  14. package/dest/orchestrator/epoch-proving-state.js +35 -1
  15. package/dest/orchestrator/orchestrator.d.ts +16 -3
  16. package/dest/orchestrator/orchestrator.d.ts.map +1 -1
  17. package/dest/orchestrator/orchestrator.js +471 -48
  18. package/dest/orchestrator/orchestrator_metrics.d.ts +1 -1
  19. package/dest/orchestrator/orchestrator_metrics.d.ts.map +1 -1
  20. package/dest/orchestrator/orchestrator_metrics.js +2 -6
  21. package/dest/orchestrator/tx-proving-state.d.ts +5 -4
  22. package/dest/orchestrator/tx-proving-state.d.ts.map +1 -1
  23. package/dest/orchestrator/tx-proving-state.js +6 -6
  24. package/dest/prover-client/prover-client.d.ts +1 -1
  25. package/dest/prover-client/prover-client.d.ts.map +1 -1
  26. package/dest/prover-client/prover-client.js +2 -2
  27. package/dest/proving_broker/broker_prover_facade.d.ts +4 -3
  28. package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
  29. package/dest/proving_broker/config.d.ts +5 -1
  30. package/dest/proving_broker/config.d.ts.map +1 -1
  31. package/dest/proving_broker/config.js +7 -1
  32. package/dest/proving_broker/proving_agent.d.ts +3 -8
  33. package/dest/proving_broker/proving_agent.d.ts.map +1 -1
  34. package/dest/proving_broker/proving_agent.js +1 -16
  35. package/dest/proving_broker/proving_broker.d.ts +1 -1
  36. package/dest/proving_broker/proving_broker.d.ts.map +1 -1
  37. package/dest/proving_broker/proving_broker.js +1 -10
  38. package/dest/proving_broker/proving_broker_database/persisted.d.ts +3 -2
  39. package/dest/proving_broker/proving_broker_database/persisted.d.ts.map +1 -1
  40. package/dest/proving_broker/proving_broker_database/persisted.js +389 -1
  41. package/dest/proving_broker/proving_broker_instrumentation.d.ts +1 -1
  42. package/dest/proving_broker/proving_broker_instrumentation.d.ts.map +1 -1
  43. package/dest/proving_broker/proving_broker_instrumentation.js +11 -35
  44. package/dest/test/mock_proof_store.d.ts +3 -3
  45. package/dest/test/mock_proof_store.d.ts.map +1 -1
  46. package/dest/test/mock_prover.d.ts +2 -2
  47. package/dest/test/mock_prover.d.ts.map +1 -1
  48. package/dest/test/mock_prover.js +3 -3
  49. package/package.json +16 -17
  50. package/src/light/lightweight_checkpoint_builder.ts +88 -12
  51. package/src/mocks/test_context.ts +11 -1
  52. package/src/orchestrator/block-building-helpers.ts +2 -2
  53. package/src/orchestrator/checkpoint-proving-state.ts +47 -1
  54. package/src/orchestrator/epoch-proving-state.ts +56 -8
  55. package/src/orchestrator/orchestrator.ts +63 -23
  56. package/src/orchestrator/orchestrator_metrics.ts +2 -6
  57. package/src/orchestrator/tx-proving-state.ts +8 -11
  58. package/src/prover-client/prover-client.ts +8 -10
  59. package/src/proving_broker/broker_prover_facade.ts +2 -3
  60. package/src/proving_broker/config.ts +9 -0
  61. package/src/proving_broker/proving_agent.ts +1 -17
  62. package/src/proving_broker/proving_broker.ts +1 -8
  63. package/src/proving_broker/proving_broker_database/persisted.ts +15 -1
  64. package/src/proving_broker/proving_broker_instrumentation.ts +10 -35
  65. package/src/test/mock_prover.ts +1 -8
  66. package/dest/block-factory/index.d.ts +0 -2
  67. package/dest/block-factory/index.d.ts.map +0 -1
  68. package/dest/block-factory/index.js +0 -1
  69. package/dest/block-factory/light.d.ts +0 -38
  70. package/dest/block-factory/light.d.ts.map +0 -1
  71. package/dest/block-factory/light.js +0 -108
  72. package/dest/proving_broker/proving_agent_instrumentation.d.ts +0 -8
  73. package/dest/proving_broker/proving_agent_instrumentation.d.ts.map +0 -1
  74. package/dest/proving_broker/proving_agent_instrumentation.js +0 -16
  75. package/src/block-factory/index.ts +0 -1
  76. package/src/block-factory/light.ts +0 -137
  77. package/src/proving_broker/proving_agent_instrumentation.ts +0 -21
@@ -1,18 +1,21 @@
1
1
  import { SpongeBlob, computeBlobsHashFromBlobs, encodeCheckpointEndMarker, getBlobsPerL1Block } from '@aztec/blob-lib';
2
2
  import { NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
3
- import type { CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import { type CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
4
4
  import { padArrayEnd } from '@aztec/foundation/collection';
5
5
  import { Fr } from '@aztec/foundation/curves/bn254';
6
6
  import { createLogger } from '@aztec/foundation/log';
7
- import { L2BlockNew } from '@aztec/stdlib/block';
7
+ import { L2Block } from '@aztec/stdlib/block';
8
8
  import { Checkpoint } from '@aztec/stdlib/checkpoint';
9
9
  import type { MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
10
- import { computeCheckpointOutHash, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
10
+ import {
11
+ accumulateCheckpointOutHashes,
12
+ computeCheckpointOutHash,
13
+ computeInHashFromL1ToL2Messages,
14
+ } from '@aztec/stdlib/messaging';
11
15
  import { CheckpointHeader, computeBlockHeadersHash } from '@aztec/stdlib/rollup';
12
16
  import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
13
17
  import {
14
18
  type CheckpointGlobalVariables,
15
- ContentCommitment,
16
19
  type GlobalVariables,
17
20
  type ProcessedTx,
18
21
  StateReference,
@@ -35,13 +38,14 @@ export class LightweightCheckpointBuilder {
35
38
 
36
39
  private lastArchives: AppendOnlyTreeSnapshot[] = [];
37
40
  private spongeBlob: SpongeBlob;
38
- private blocks: L2BlockNew[] = [];
41
+ private blocks: L2Block[] = [];
39
42
  private blobFields: Fr[] = [];
40
43
 
41
44
  constructor(
42
45
  public readonly checkpointNumber: CheckpointNumber,
43
46
  public readonly constants: CheckpointGlobalVariables,
44
47
  public readonly l1ToL2Messages: Fr[],
48
+ private readonly previousCheckpointOutHashes: Fr[],
45
49
  public readonly db: MerkleTreeWriteOperations,
46
50
  ) {
47
51
  this.spongeBlob = SpongeBlob.init();
@@ -52,6 +56,7 @@ export class LightweightCheckpointBuilder {
52
56
  checkpointNumber: CheckpointNumber,
53
57
  constants: CheckpointGlobalVariables,
54
58
  l1ToL2Messages: Fr[],
59
+ previousCheckpointOutHashes: Fr[],
55
60
  db: MerkleTreeWriteOperations,
56
61
  ): Promise<LightweightCheckpointBuilder> {
57
62
  // Insert l1-to-l2 messages into the tree.
@@ -60,7 +65,73 @@ export class LightweightCheckpointBuilder {
60
65
  padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP),
61
66
  );
62
67
 
63
- return new LightweightCheckpointBuilder(checkpointNumber, constants, l1ToL2Messages, db);
68
+ return new LightweightCheckpointBuilder(
69
+ checkpointNumber,
70
+ constants,
71
+ l1ToL2Messages,
72
+ previousCheckpointOutHashes,
73
+ db,
74
+ );
75
+ }
76
+
77
+ /**
78
+ * Resumes building a checkpoint from existing blocks. This is used for validator re-execution
79
+ * where blocks have already been built and their effects are already in the database.
80
+ * Unlike startNewCheckpoint, this does NOT append l1ToL2Messages to the tree since they
81
+ * were already added when the blocks were originally built.
82
+ */
83
+ static async resumeCheckpoint(
84
+ checkpointNumber: CheckpointNumber,
85
+ constants: CheckpointGlobalVariables,
86
+ l1ToL2Messages: Fr[],
87
+ previousCheckpointOutHashes: Fr[],
88
+ db: MerkleTreeWriteOperations,
89
+ existingBlocks: L2Block[],
90
+ ): Promise<LightweightCheckpointBuilder> {
91
+ const builder = new LightweightCheckpointBuilder(
92
+ checkpointNumber,
93
+ constants,
94
+ l1ToL2Messages,
95
+ previousCheckpointOutHashes,
96
+ db,
97
+ );
98
+
99
+ builder.logger.debug('Resuming checkpoint from existing blocks', {
100
+ checkpointNumber,
101
+ numExistingBlocks: existingBlocks.length,
102
+ blockNumbers: existingBlocks.map(b => b.header.getBlockNumber()),
103
+ });
104
+
105
+ // Validate block order and consistency
106
+ for (let i = 1; i < existingBlocks.length; i++) {
107
+ const prev = existingBlocks[i - 1];
108
+ const curr = existingBlocks[i];
109
+ if (curr.number !== prev.number + 1) {
110
+ throw new Error(`Non-sequential block numbers in resumeCheckpoint: ${prev.number} -> ${curr.number}`);
111
+ }
112
+ if (!prev.archive.root.equals(curr.header.lastArchive.root)) {
113
+ throw new Error(`Archive root mismatch between blocks ${prev.number} and ${curr.number}`);
114
+ }
115
+ }
116
+
117
+ for (let i = 0; i < existingBlocks.length; i++) {
118
+ const block = existingBlocks[i];
119
+ const isFirstBlock = i === 0;
120
+
121
+ if (isFirstBlock) {
122
+ builder.lastArchives.push(block.header.lastArchive);
123
+ }
124
+
125
+ builder.lastArchives.push(block.archive);
126
+
127
+ const blockBlobFields = block.toBlobFields();
128
+ await builder.spongeBlob.absorb(blockBlobFields);
129
+ builder.blobFields.push(...blockBlobFields);
130
+
131
+ builder.blocks.push(block);
132
+ }
133
+
134
+ return builder;
64
135
  }
65
136
 
66
137
  /**
@@ -71,7 +142,7 @@ export class LightweightCheckpointBuilder {
71
142
  globalVariables: GlobalVariables,
72
143
  txs: ProcessedTx[],
73
144
  opts: { insertTxsEffects?: boolean; expectedEndState?: StateReference } = {},
74
- ): Promise<L2BlockNew> {
145
+ ): Promise<L2Block> {
75
146
  const isFirstBlock = this.blocks.length === 0;
76
147
 
77
148
  // Empty blocks are only allowed as the first block in a checkpoint
@@ -120,8 +191,8 @@ export class LightweightCheckpointBuilder {
120
191
  const newArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, this.db);
121
192
  this.lastArchives.push(newArchive);
122
193
 
123
- const indexWithinCheckpoint = this.blocks.length;
124
- const block = new L2BlockNew(newArchive, header, body, this.checkpointNumber, indexWithinCheckpoint);
194
+ const indexWithinCheckpoint = IndexWithinCheckpoint(this.blocks.length);
195
+ const block = new L2Block(newArchive, header, body, this.checkpointNumber, indexWithinCheckpoint);
125
196
  this.blocks.push(block);
126
197
 
127
198
  await this.spongeBlob.absorb(blockBlobFields);
@@ -157,9 +228,11 @@ export class LightweightCheckpointBuilder {
157
228
 
158
229
  const inHash = computeInHashFromL1ToL2Messages(this.l1ToL2Messages);
159
230
 
160
- const outHash = computeCheckpointOutHash(blocks.map(block => block.body.txEffects.map(tx => tx.l2ToL1Msgs)));
161
-
162
231
  const { slotNumber, coinbase, feeRecipient, gasFees } = this.constants;
232
+ const checkpointOutHash = computeCheckpointOutHash(
233
+ blocks.map(block => block.body.txEffects.map(tx => tx.l2ToL1Msgs)),
234
+ );
235
+ const epochOutHash = accumulateCheckpointOutHashes([...this.previousCheckpointOutHashes, checkpointOutHash]);
163
236
 
164
237
  // TODO(palla/mbps): Should we source this from the constants instead?
165
238
  // timestamp of a checkpoint is the timestamp of the last block in the checkpoint.
@@ -169,7 +242,9 @@ export class LightweightCheckpointBuilder {
169
242
 
170
243
  const header = CheckpointHeader.from({
171
244
  lastArchiveRoot: this.lastArchives[0].root,
172
- contentCommitment: new ContentCommitment(blobsHash, inHash, outHash),
245
+ blobsHash,
246
+ inHash,
247
+ epochOutHash,
173
248
  blockHeadersHash,
174
249
  slotNumber,
175
250
  timestamp,
@@ -187,6 +262,7 @@ export class LightweightCheckpointBuilder {
187
262
  this.checkpointNumber,
188
263
  this.constants,
189
264
  [...this.l1ToL2Messages],
265
+ [...this.previousCheckpointOutHashes],
190
266
  this.db,
191
267
  );
192
268
  clone.lastArchives = [...this.lastArchives];
@@ -44,7 +44,9 @@ import { getEnvironmentConfig, getSimulator, makeCheckpointConstants, makeGlobal
44
44
  export class TestContext {
45
45
  private headers: Map<number, BlockHeader> = new Map();
46
46
  private checkpoints: Checkpoint[] = [];
47
+ private checkpointOutHashes: Fr[] = [];
47
48
  private nextCheckpointIndex = 0;
49
+ private nextCheckpointNumber = CheckpointNumber(1);
48
50
  private nextBlockNumber = 1;
49
51
  private epochNumber = 1;
50
52
  private feePayerBalance: Fr;
@@ -150,6 +152,7 @@ export class TestContext {
150
152
 
151
153
  public startNewEpoch() {
152
154
  this.checkpoints = [];
155
+ this.checkpointOutHashes = [];
153
156
  this.nextCheckpointIndex = 0;
154
157
  this.epochNumber++;
155
158
  }
@@ -187,7 +190,8 @@ export class TestContext {
187
190
  }
188
191
 
189
192
  const checkpointIndex = this.nextCheckpointIndex++;
190
- const checkpointNumber = CheckpointNumber(checkpointIndex + 1);
193
+ const checkpointNumber = this.nextCheckpointNumber;
194
+ this.nextCheckpointNumber++;
191
195
  const slotNumber = checkpointNumber * 15; // times an arbitrary number to make it different to the checkpoint number
192
196
 
193
197
  const constants = makeCheckpointConstants(slotNumber, constantOpts);
@@ -204,6 +208,8 @@ export class TestContext {
204
208
 
205
209
  const startBlockNumber = this.nextBlockNumber;
206
210
  const previousBlockHeader = this.getBlockHeader(BlockNumber(startBlockNumber - 1));
211
+ // All blocks in the same slot/checkpoint share the same timestamp.
212
+ const timestamp = BigInt(slotNumber * 26);
207
213
 
208
214
  // Build global variables.
209
215
  const blockGlobalVariables = times(numBlocks, i =>
@@ -211,6 +217,7 @@ export class TestContext {
211
217
  coinbase: constants.coinbase,
212
218
  feeRecipient: constants.feeRecipient,
213
219
  gasFees: constants.gasFees,
220
+ timestamp,
214
221
  }),
215
222
  );
216
223
  this.nextBlockNumber += numBlocks;
@@ -240,10 +247,12 @@ export class TestContext {
240
247
  });
241
248
 
242
249
  const cleanFork = await this.worldState.fork();
250
+ const previousCheckpointOutHashes = this.checkpointOutHashes;
243
251
  const builder = await LightweightCheckpointBuilder.startNewCheckpoint(
244
252
  checkpointNumber,
245
253
  constants,
246
254
  l1ToL2Messages,
255
+ previousCheckpointOutHashes,
247
256
  cleanFork,
248
257
  );
249
258
 
@@ -269,6 +278,7 @@ export class TestContext {
269
278
 
270
279
  const checkpoint = await builder.completeCheckpoint();
271
280
  this.checkpoints.push(checkpoint);
281
+ this.checkpointOutHashes.push(checkpoint.getCheckpointOutHash());
272
282
 
273
283
  return {
274
284
  constants,
@@ -97,7 +97,7 @@ export const insertSideEffectsAndBuildBaseRollupHints = runInSpan(
97
97
 
98
98
  const { nullifierInsertionResult, publicDataInsertionResult } = await insertSideEffects(tx, db);
99
99
 
100
- const blockHash = await tx.data.constants.anchorBlockHeader.hash();
100
+ const blockHash = (await tx.data.constants.anchorBlockHeader.hash()).toField();
101
101
  const anchorBlockArchiveSiblingPath = (
102
102
  await getMembershipWitnessFor(blockHash, MerkleTreeId.ARCHIVE, ARCHIVE_HEIGHT, db)
103
103
  ).siblingPath;
@@ -282,7 +282,7 @@ export const buildHeaderFromCircuitOutputs = runInSpan(
282
282
  chainId: constants.chainId,
283
283
  version: constants.version,
284
284
  blockNumber: BlockNumber(blockRootRollupOutput.previousArchive.nextAvailableLeafIndex),
285
- timestamp: blockRootRollupOutput.endTimestamp,
285
+ timestamp: blockRootRollupOutput.timestamp,
286
286
  slotNumber: constants.slotNumber,
287
287
  coinbase: constants.coinbase,
288
288
  feeRecipient: constants.feeRecipient,
@@ -11,6 +11,7 @@ import {
11
11
  type L1_TO_L2_MSG_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
12
12
  type NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
13
13
  NUM_MSGS_PER_BASE_PARITY,
14
+ OUT_HASH_TREE_HEIGHT,
14
15
  } from '@aztec/constants';
15
16
  import { BlockNumber } from '@aztec/foundation/branded-types';
16
17
  import { padArrayEnd } from '@aztec/foundation/collection';
@@ -19,6 +20,7 @@ import { Fr } from '@aztec/foundation/curves/bn254';
19
20
  import type { Tuple } from '@aztec/foundation/serialize';
20
21
  import { type TreeNodeLocation, UnbalancedTreeStore } from '@aztec/foundation/trees';
21
22
  import type { PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
23
+ import { computeCheckpointOutHash } from '@aztec/stdlib/messaging';
22
24
  import { ParityBasePrivateInputs } from '@aztec/stdlib/parity';
23
25
  import {
24
26
  BlockMergeRollupPrivateInputs,
@@ -38,6 +40,11 @@ import { accumulateBlobs, buildBlobHints, toProofData } from './block-building-h
38
40
  import { BlockProvingState, type ProofState } from './block-proving-state.js';
39
41
  import type { EpochProvingState } from './epoch-proving-state.js';
40
42
 
43
+ type OutHashHint = {
44
+ treeSnapshot: AppendOnlyTreeSnapshot;
45
+ siblingPath: Tuple<Fr, typeof OUT_HASH_TREE_HEIGHT>;
46
+ };
47
+
41
48
  export class CheckpointProvingState {
42
49
  private blockProofs: UnbalancedTreeStore<
43
50
  ProofState<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
@@ -46,6 +53,11 @@ export class CheckpointProvingState {
46
53
  | ProofState<CheckpointRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
47
54
  | undefined;
48
55
  private blocks: (BlockProvingState | undefined)[] = [];
56
+ private previousOutHashHint: OutHashHint | undefined;
57
+ private outHash: Fr | undefined;
58
+ // The snapshot and sibling path after the checkpoint's out hash is inserted.
59
+ // Stored here to be retrieved for the next checkpoint when it's added.
60
+ private newOutHashHint: OutHashHint | undefined;
49
61
  private startBlobAccumulator: BatchedBlobAccumulator | undefined;
50
62
  private endBlobAccumulator: BatchedBlobAccumulator | undefined;
51
63
  private blobFields: Fr[] | undefined;
@@ -195,6 +207,35 @@ export class CheckpointProvingState {
195
207
  return new ParityBasePrivateInputs(messages, this.constants.vkTreeRoot);
196
208
  }
197
209
 
210
+ public setOutHashHint(hint: OutHashHint) {
211
+ this.previousOutHashHint = hint;
212
+ }
213
+
214
+ public getOutHashHint() {
215
+ return this.previousOutHashHint;
216
+ }
217
+
218
+ public accumulateBlockOutHashes() {
219
+ if (this.isAcceptingBlocks() || this.blocks.some(b => !b?.hasEndState())) {
220
+ return;
221
+ }
222
+
223
+ if (!this.outHash) {
224
+ const messagesPerBlock = this.blocks.map(b => b!.getTxEffects().map(tx => tx.l2ToL1Msgs));
225
+ this.outHash = computeCheckpointOutHash(messagesPerBlock);
226
+ }
227
+
228
+ return this.outHash;
229
+ }
230
+
231
+ public setOutHashHintForNextCheckpoint(hint: OutHashHint) {
232
+ this.newOutHashHint = hint;
233
+ }
234
+
235
+ public getOutHashHintForNextCheckpoint() {
236
+ return this.newOutHashHint;
237
+ }
238
+
198
239
  public async accumulateBlobs(startBlobAccumulator: BatchedBlobAccumulator) {
199
240
  if (this.isAcceptingBlocks() || this.blocks.some(b => !b?.hasEndState())) {
200
241
  return;
@@ -236,6 +277,9 @@ export class CheckpointProvingState {
236
277
  if (proofs.length !== nonEmptyProofs.length) {
237
278
  throw new Error('At least one child is not ready for the checkpoint root rollup.');
238
279
  }
280
+ if (!this.previousOutHashHint) {
281
+ throw new Error('Out hash hint is not set.');
282
+ }
239
283
  if (!this.startBlobAccumulator) {
240
284
  throw new Error('Start blob accumulator is not set.');
241
285
  }
@@ -248,6 +292,8 @@ export class CheckpointProvingState {
248
292
  const hints = CheckpointRootRollupHints.from({
249
293
  previousBlockHeader: this.headerOfLastBlockInPreviousCheckpoint,
250
294
  previousArchiveSiblingPath: this.lastArchiveSiblingPath,
295
+ previousOutHash: this.previousOutHashHint.treeSnapshot,
296
+ newOutHashSiblingPath: this.previousOutHashHint.siblingPath,
251
297
  startBlobAccumulator: this.startBlobAccumulator.toBlobAccumulator(),
252
298
  finalBlobChallenges: this.finalBlobBatchingChallenges,
253
299
  blobFields: padArrayEnd(blobFields, Fr.ZERO, FIELDS_PER_BLOB * BLOBS_PER_CHECKPOINT),
@@ -273,7 +319,7 @@ export class CheckpointProvingState {
273
319
 
274
320
  public isReadyForCheckpointRoot() {
275
321
  const allChildProofsReady = this.#getChildProofsForRoot().every(p => !!p);
276
- return allChildProofsReady && !!this.startBlobAccumulator;
322
+ return allChildProofsReady && !!this.previousOutHashHint && !!this.startBlobAccumulator;
277
323
  }
278
324
 
279
325
  public verifyState() {
@@ -1,14 +1,20 @@
1
1
  import { BatchedBlob, BatchedBlobAccumulator, type FinalBlobBatchingChallenges } from '@aztec/blob-lib';
2
- import type {
3
- ARCHIVE_HEIGHT,
4
- L1_TO_L2_MSG_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
5
- NESTED_RECURSIVE_PROOF_LENGTH,
6
- NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
2
+ import {
3
+ type ARCHIVE_HEIGHT,
4
+ type L1_TO_L2_MSG_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
5
+ type NESTED_RECURSIVE_PROOF_LENGTH,
6
+ type NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
7
+ OUT_HASH_TREE_HEIGHT,
7
8
  } from '@aztec/constants';
8
9
  import { BlockNumber, EpochNumber } from '@aztec/foundation/branded-types';
9
- import type { Fr } from '@aztec/foundation/curves/bn254';
10
+ import { Fr } from '@aztec/foundation/curves/bn254';
10
11
  import type { Tuple } from '@aztec/foundation/serialize';
11
- import { type TreeNodeLocation, UnbalancedTreeStore } from '@aztec/foundation/trees';
12
+ import {
13
+ MerkleTreeCalculator,
14
+ type TreeNodeLocation,
15
+ UnbalancedTreeStore,
16
+ shaMerkleHash,
17
+ } from '@aztec/foundation/trees';
12
18
  import type { PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
13
19
  import type { Proof } from '@aztec/stdlib/proofs';
14
20
  import {
@@ -20,7 +26,7 @@ import {
20
26
  RootRollupPrivateInputs,
21
27
  type RootRollupPublicInputs,
22
28
  } from '@aztec/stdlib/rollup';
23
- import type { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
29
+ import { AppendOnlyTreeSnapshot, type MerkleTreeId } from '@aztec/stdlib/trees';
24
30
  import type { BlockHeader } from '@aztec/stdlib/tx';
25
31
 
26
32
  import { toProofData } from './block-building-helpers.js';
@@ -212,6 +218,48 @@ export class EpochProvingState {
212
218
  this.checkpointPaddingProof = { provingOutput };
213
219
  }
214
220
 
221
+ public async accumulateCheckpointOutHashes() {
222
+ const treeCalculator = await MerkleTreeCalculator.create(OUT_HASH_TREE_HEIGHT, undefined, (left, right) =>
223
+ Promise.resolve(shaMerkleHash(left, right)),
224
+ );
225
+
226
+ const computeOutHashHint = async (leaves: Fr[]) => {
227
+ const tree = await treeCalculator.computeTree(leaves.map(l => l.toBuffer()));
228
+ const nextAvailableLeafIndex = leaves.length;
229
+ return {
230
+ treeSnapshot: new AppendOnlyTreeSnapshot(Fr.fromBuffer(tree.root), nextAvailableLeafIndex),
231
+ siblingPath: tree.getSiblingPath(nextAvailableLeafIndex).map(Fr.fromBuffer) as Tuple<
232
+ Fr,
233
+ typeof OUT_HASH_TREE_HEIGHT
234
+ >,
235
+ };
236
+ };
237
+
238
+ let hint = this.checkpoints[0]?.getOutHashHint();
239
+ const outHashes = [];
240
+ for (let i = 0; i < this.totalNumCheckpoints; i++) {
241
+ const checkpoint = this.checkpoints[i];
242
+ if (!checkpoint) {
243
+ break;
244
+ }
245
+
246
+ // If hints are not set yet, it must be the first checkpoint. Compute the hints with an empty tree.
247
+ hint ??= await computeOutHashHint([]);
248
+ checkpoint.setOutHashHint(hint);
249
+
250
+ // Get the out hash for this checkpoint.
251
+ const outHash = checkpoint.accumulateBlockOutHashes();
252
+ if (!outHash) {
253
+ break;
254
+ }
255
+ outHashes.push(outHash);
256
+
257
+ // Get or create hints for the next checkpoint.
258
+ hint = checkpoint.getOutHashHintForNextCheckpoint() ?? (await computeOutHashHint(outHashes));
259
+ checkpoint.setOutHashHintForNextCheckpoint(hint);
260
+ }
261
+ }
262
+
215
263
  public async setBlobAccumulators() {
216
264
  let previousAccumulator = this.startBlobAccumulator;
217
265
  // Accumulate blobs as far as we can for this epoch.
@@ -73,6 +73,11 @@ import { TxProvingState } from './tx-proving-state.js';
73
73
 
74
74
  const logger = createLogger('prover-client:orchestrator');
75
75
 
76
+ type WorldStateFork = {
77
+ fork: MerkleTreeWriteOperations;
78
+ cleanupPromise: Promise<void> | undefined;
79
+ };
80
+
76
81
  /**
77
82
  * Implements an event driven proving scheduler to build the recursive proof tree. The idea being:
78
83
  * 1. Transactions are provided to the scheduler post simulation.
@@ -94,12 +99,13 @@ export class ProvingOrchestrator implements EpochProver {
94
99
  private provingPromise: Promise<ProvingResult> | undefined = undefined;
95
100
  private metrics: ProvingOrchestratorMetrics;
96
101
  // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
97
- private dbs: Map<BlockNumber, MerkleTreeWriteOperations> = new Map();
102
+ private dbs: Map<BlockNumber, WorldStateFork> = new Map();
98
103
 
99
104
  constructor(
100
105
  private dbProvider: ReadonlyWorldStateAccess & ForkMerkleTreeOperations,
101
106
  private prover: ServerCircuitProver,
102
107
  private readonly proverId: EthAddress,
108
+ private readonly cancelJobsOnStop: boolean = false,
103
109
  telemetryClient: TelemetryClient = getTelemetryClient(),
104
110
  ) {
105
111
  this.metrics = new ProvingOrchestratorMetrics(telemetryClient, 'ProvingOrchestrator');
@@ -113,6 +119,10 @@ export class ProvingOrchestrator implements EpochProver {
113
119
  return this.proverId;
114
120
  }
115
121
 
122
+ public getNumActiveForks() {
123
+ return this.dbs.size;
124
+ }
125
+
116
126
  public stop(): Promise<void> {
117
127
  this.cancel();
118
128
  return Promise.resolve();
@@ -143,6 +153,14 @@ export class ProvingOrchestrator implements EpochProver {
143
153
  this.provingPromise = promise;
144
154
  }
145
155
 
156
+ /**
157
+ * Starts a new checkpoint.
158
+ * @param checkpointIndex - The index of the checkpoint in the epoch.
159
+ * @param constants - The constants for this checkpoint.
160
+ * @param l1ToL2Messages - The set of L1 to L2 messages to be inserted at the beginning of this checkpoint.
161
+ * @param totalNumBlocks - The total number of blocks expected in the checkpoint (must be at least one).
162
+ * @param headerOfLastBlockInPreviousCheckpoint - The header of the last block in the previous checkpoint.
163
+ */
146
164
  public async startNewCheckpoint(
147
165
  checkpointIndex: number,
148
166
  constants: CheckpointConstantData,
@@ -163,7 +181,7 @@ export class ProvingOrchestrator implements EpochProver {
163
181
  const db = await this.dbProvider.fork(lastBlockNumber);
164
182
 
165
183
  const firstBlockNumber = BlockNumber(lastBlockNumber + 1);
166
- this.dbs.set(firstBlockNumber, db);
184
+ this.dbs.set(firstBlockNumber, { fork: db, cleanupPromise: undefined });
167
185
 
168
186
  // Get archive sibling path before any block in this checkpoint lands.
169
187
  const lastArchiveSiblingPath = await getLastSiblingPath(MerkleTreeId.ARCHIVE, db);
@@ -221,9 +239,9 @@ export class ProvingOrchestrator implements EpochProver {
221
239
  if (!this.dbs.has(blockNumber)) {
222
240
  // Fork world state at the end of the immediately previous block
223
241
  const db = await this.dbProvider.fork(BlockNumber(blockNumber - 1));
224
- this.dbs.set(blockNumber, db);
242
+ this.dbs.set(blockNumber, { fork: db, cleanupPromise: undefined });
225
243
  }
226
- const db = this.dbs.get(blockNumber)!;
244
+ const db = this.dbs.get(blockNumber)!.fork;
227
245
 
228
246
  // Get archive snapshot and sibling path before any txs in this block lands.
229
247
  const lastArchiveTreeSnapshot = await getTreeSnapshot(MerkleTreeId.ARCHIVE, db);
@@ -255,7 +273,8 @@ export class ProvingOrchestrator implements EpochProver {
255
273
  await endSpongeBlob.absorb(blockEndBlobFields);
256
274
  blockProvingState.setEndSpongeBlob(endSpongeBlob);
257
275
 
258
- // And also try to accumulate the blobs as far as we can:
276
+ // Try to accumulate the out hashes and blobs as far as we can:
277
+ await this.provingState.accumulateCheckpointOutHashes();
259
278
  await this.provingState.setBlobAccumulators();
260
279
  }
261
280
  }
@@ -297,7 +316,7 @@ export class ProvingOrchestrator implements EpochProver {
297
316
 
298
317
  logger.info(`Adding ${txs.length} transactions to block ${blockNumber}`);
299
318
 
300
- const db = this.dbs.get(blockNumber)!;
319
+ const db = this.dbs.get(blockNumber)!.fork;
301
320
  const lastArchive = provingState.lastArchiveTreeSnapshot;
302
321
  const newL1ToL2MessageTreeSnapshot = provingState.newL1ToL2MessageTreeSnapshot;
303
322
  const spongeBlobState = provingState.getStartSpongeBlob().clone();
@@ -352,7 +371,8 @@ export class ProvingOrchestrator implements EpochProver {
352
371
 
353
372
  provingState.setEndSpongeBlob(spongeBlobState);
354
373
 
355
- // Txs have been added to the block. Now try to accumulate the blobs as far as we can:
374
+ // Txs have been added to the block. Now try to accumulate the out hashes and blobs as far as we can:
375
+ await this.provingState.accumulateCheckpointOutHashes();
356
376
  await this.provingState.setBlobAccumulators();
357
377
  }
358
378
 
@@ -425,7 +445,7 @@ export class ProvingOrchestrator implements EpochProver {
425
445
  }
426
446
 
427
447
  // Get db for this block
428
- const db = this.dbs.get(provingState.blockNumber)!;
448
+ const db = this.dbs.get(provingState.blockNumber)!.fork;
429
449
 
430
450
  // Update the archive tree, so we're ready to start processing the next block:
431
451
  logger.verbose(
@@ -461,7 +481,7 @@ export class ProvingOrchestrator implements EpochProver {
461
481
 
462
482
  // Get db for this block
463
483
  const blockNumber = provingState.blockNumber;
464
- const db = this.dbs.get(blockNumber)!;
484
+ const db = this.dbs.get(blockNumber)!.fork;
465
485
 
466
486
  const newArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, db);
467
487
  const syncedArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, this.dbProvider.getSnapshot(blockNumber));
@@ -486,20 +506,19 @@ export class ProvingOrchestrator implements EpochProver {
486
506
  // is aborted and never reaches this point, it will leak the fork. We need to add a global cleanup,
487
507
  // but have to make sure it only runs once all operations are completed, otherwise some function here
488
508
  // will attempt to access the fork after it was closed.
489
- logger.debug(`Cleaning up world state fork for ${blockNumber}`);
490
- void this.dbs
491
- .get(blockNumber)
492
- ?.close()
493
- .then(() => this.dbs.delete(blockNumber))
494
- .catch(err => logger.error(`Error closing db for block ${blockNumber}`, err));
509
+ void this.cleanupDBFork(blockNumber);
495
510
  }
496
511
 
497
512
  /**
498
- * Cancel any further proving
513
+ * Cancel any further proving.
514
+ * If cancelJobsOnStop is true, aborts all pending jobs with the broker (which marks them as 'Aborted').
515
+ * If cancelJobsOnStop is false (default), jobs remain in the broker queue and can be reused on restart/reorg.
499
516
  */
500
517
  public cancel() {
501
- for (const controller of this.pendingProvingJobs) {
502
- controller.abort();
518
+ if (this.cancelJobsOnStop) {
519
+ for (const controller of this.pendingProvingJobs) {
520
+ controller.abort();
521
+ }
503
522
  }
504
523
 
505
524
  this.provingState?.cancel();
@@ -534,6 +553,24 @@ export class ProvingOrchestrator implements EpochProver {
534
553
  return epochProofResult;
535
554
  }
536
555
 
556
+ private async cleanupDBFork(blockNumber: BlockNumber): Promise<void> {
557
+ logger.debug(`Cleaning up world state fork for ${blockNumber}`);
558
+ const fork = this.dbs.get(blockNumber);
559
+ if (!fork) {
560
+ return;
561
+ }
562
+
563
+ try {
564
+ if (!fork.cleanupPromise) {
565
+ fork.cleanupPromise = fork.fork.close();
566
+ }
567
+ await fork.cleanupPromise;
568
+ this.dbs.delete(blockNumber);
569
+ } catch (err) {
570
+ logger.error(`Error closing db for block ${blockNumber}`, err);
571
+ }
572
+ }
573
+
537
574
  /**
538
575
  * Enqueue a job to be scheduled
539
576
  * @param provingState - The proving state object being operated on
@@ -851,19 +888,22 @@ export class ProvingOrchestrator implements EpochProver {
851
888
  },
852
889
  ),
853
890
  async result => {
854
- // If the proofs were slower than the block header building, then we need to try validating the block header hashes here.
855
- await this.verifyBuiltBlockAgainstSyncedState(provingState);
856
-
857
891
  logger.debug(`Completed ${rollupType} proof for block ${provingState.blockNumber}`);
858
892
 
859
893
  const leafLocation = provingState.setBlockRootRollupProof(result);
860
894
  const checkpointProvingState = provingState.parentCheckpoint;
861
895
 
896
+ // If the proofs were slower than the block header building, then we need to try validating the block header hashes here.
897
+ await this.verifyBuiltBlockAgainstSyncedState(provingState);
898
+
862
899
  if (checkpointProvingState.totalNumBlocks === 1) {
863
900
  this.checkAndEnqueueCheckpointRootRollup(checkpointProvingState);
864
901
  } else {
865
902
  this.checkAndEnqueueNextBlockMergeRollup(checkpointProvingState, leafLocation);
866
903
  }
904
+
905
+ // We are finished with the block at this point, ensure the fork is cleaned up
906
+ void this.cleanupDBFork(provingState.blockNumber);
867
907
  },
868
908
  );
869
909
  }
@@ -1219,9 +1259,9 @@ export class ProvingOrchestrator implements EpochProver {
1219
1259
  },
1220
1260
  );
1221
1261
 
1222
- this.deferredProving(provingState, doAvmProving, proofAndVk => {
1262
+ this.deferredProving(provingState, doAvmProving, proof => {
1223
1263
  logger.debug(`Proven VM for tx index: ${txIndex}`);
1224
- txProvingState.setAvmProof(proofAndVk);
1264
+ txProvingState.setAvmProof(proof);
1225
1265
  this.checkAndEnqueueBaseRollup(provingState, txIndex);
1226
1266
  });
1227
1267
  }
@@ -1,4 +1,4 @@
1
- import { type Histogram, Metrics, type TelemetryClient, type Tracer, ValueType } from '@aztec/telemetry-client';
1
+ import { type Histogram, Metrics, type TelemetryClient, type Tracer } from '@aztec/telemetry-client';
2
2
 
3
3
  export class ProvingOrchestratorMetrics {
4
4
  public readonly tracer: Tracer;
@@ -9,11 +9,7 @@ export class ProvingOrchestratorMetrics {
9
9
  this.tracer = client.getTracer(name);
10
10
  const meter = client.getMeter(name);
11
11
 
12
- this.baseRollupInputsDuration = meter.createHistogram(Metrics.PROVING_ORCHESTRATOR_BASE_ROLLUP_INPUTS_DURATION, {
13
- unit: 'ms',
14
- description: 'Duration to build base rollup inputs',
15
- valueType: ValueType.INT,
16
- });
12
+ this.baseRollupInputsDuration = meter.createHistogram(Metrics.PROVING_ORCHESTRATOR_BASE_ROLLUP_INPUTS_DURATION);
17
13
  }
18
14
 
19
15
  recordBaseRollupInputs(durationMs: number) {