@aztec/prover-node 0.0.1-commit.2eb6648a → 0.0.1-commit.2f68f620

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 (44) hide show
  1. package/dest/actions/download-epoch-proving-job.js +1 -1
  2. package/dest/actions/rerun-epoch-proving-job.d.ts +4 -3
  3. package/dest/actions/rerun-epoch-proving-job.d.ts.map +1 -1
  4. package/dest/actions/rerun-epoch-proving-job.js +105 -27
  5. package/dest/actions/upload-epoch-proof-failure.d.ts +2 -2
  6. package/dest/actions/upload-epoch-proof-failure.d.ts.map +1 -1
  7. package/dest/bin/run-failed-epoch.js +6 -5
  8. package/dest/config.d.ts +7 -8
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +23 -19
  11. package/dest/factory.d.ts +19 -13
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +41 -60
  14. package/dest/job/epoch-proving-job.d.ts +6 -2
  15. package/dest/job/epoch-proving-job.d.ts.map +1 -1
  16. package/dest/job/epoch-proving-job.js +206 -35
  17. package/dest/metrics.d.ts +21 -1
  18. package/dest/metrics.d.ts.map +1 -1
  19. package/dest/metrics.js +47 -0
  20. package/dest/monitors/epoch-monitor.d.ts +1 -1
  21. package/dest/monitors/epoch-monitor.d.ts.map +1 -1
  22. package/dest/monitors/epoch-monitor.js +11 -9
  23. package/dest/prover-node-publisher.d.ts +25 -3
  24. package/dest/prover-node-publisher.d.ts.map +1 -1
  25. package/dest/prover-node-publisher.js +206 -9
  26. package/dest/prover-node.d.ts +24 -14
  27. package/dest/prover-node.d.ts.map +1 -1
  28. package/dest/prover-node.js +91 -41
  29. package/dest/prover-publisher-factory.d.ts +6 -4
  30. package/dest/prover-publisher-factory.d.ts.map +1 -1
  31. package/dest/prover-publisher-factory.js +4 -3
  32. package/package.json +23 -22
  33. package/src/actions/download-epoch-proving-job.ts +1 -1
  34. package/src/actions/rerun-epoch-proving-job.ts +17 -6
  35. package/src/actions/upload-epoch-proof-failure.ts +1 -1
  36. package/src/bin/run-failed-epoch.ts +5 -3
  37. package/src/config.ts +33 -31
  38. package/src/factory.ts +69 -103
  39. package/src/job/epoch-proving-job.ts +137 -41
  40. package/src/metrics.ts +71 -0
  41. package/src/monitors/epoch-monitor.ts +5 -6
  42. package/src/prover-node-publisher.ts +241 -14
  43. package/src/prover-node.ts +98 -55
  44. package/src/prover-publisher-factory.ts +8 -5
@@ -1,13 +1,12 @@
1
1
  import type { Archiver } from '@aztec/archiver';
2
2
  import type { RollupContract } from '@aztec/ethereum/contracts';
3
+ import type { Delayer } from '@aztec/ethereum/l1-tx-utils';
3
4
  import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/branded-types';
4
5
  import { assertRequired, compact, pick, sum } from '@aztec/foundation/collection';
5
6
  import type { Fr } from '@aztec/foundation/curves/bn254';
6
7
  import { memoize } from '@aztec/foundation/decorators';
7
8
  import { createLogger } from '@aztec/foundation/log';
8
9
  import { DateProvider } from '@aztec/foundation/timer';
9
- import type { DataStoreConfig } from '@aztec/kv-store/config';
10
- import type { P2PClient } from '@aztec/p2p';
11
10
  import { PublicProcessorFactory } from '@aztec/simulator/server';
12
11
  import type { L2BlockSource } from '@aztec/stdlib/block';
13
12
  import type { Checkpoint } from '@aztec/stdlib/checkpoint';
@@ -16,15 +15,15 @@ import type { ContractDataSource } from '@aztec/stdlib/contract';
16
15
  import { getProofSubmissionDeadlineTimestamp } from '@aztec/stdlib/epoch-helpers';
17
16
  import {
18
17
  type EpochProverManager,
19
- EpochProvingJobTerminalState,
18
+ type ITxProvider,
20
19
  type ProverNodeApi,
21
20
  type Service,
22
21
  type WorldStateSyncStatus,
23
22
  type WorldStateSynchronizer,
24
23
  tryStop,
25
24
  } from '@aztec/stdlib/interfaces/server';
25
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
26
26
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
27
- import type { P2PClientType } from '@aztec/stdlib/p2p';
28
27
  import type { Tx } from '@aztec/stdlib/tx';
29
28
  import {
30
29
  Attributes,
@@ -39,7 +38,7 @@ import {
39
38
  import { uploadEpochProofFailure } from './actions/upload-epoch-proof-failure.js';
40
39
  import type { SpecificProverNodeConfig } from './config.js';
41
40
  import type { EpochProvingJobData } from './job/epoch-proving-job-data.js';
42
- import { EpochProvingJob, type EpochProvingJobState } from './job/epoch-proving-job.js';
41
+ import { EpochProvingJob, type EpochProvingJobOptions, type EpochProvingJobState } from './job/epoch-proving-job.js';
43
42
  import { ProverNodeJobMetrics, ProverNodeRewardsMetrics } from './metrics.js';
44
43
  import type { EpochMonitor, EpochMonitorHandler } from './monitors/epoch-monitor.js';
45
44
  import type { ProverNodePublisher } from './prover-node-publisher.js';
@@ -55,12 +54,12 @@ type DataStoreOptions = Pick<DataStoreConfig, 'dataDirectory'> & Pick<ChainConfi
55
54
  */
56
55
  export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable {
57
56
  private log = createLogger('prover-node');
58
- private dateProvider = new DateProvider();
59
57
 
60
58
  private jobs: Map<string, EpochProvingJob> = new Map();
61
59
  private config: ProverNodeOptions;
62
60
  private jobMetrics: ProverNodeJobMetrics;
63
61
  private rewardsMetrics: ProverNodeRewardsMetrics;
62
+ private startingProofEpochs: Set<EpochNumber> = new Set();
64
63
 
65
64
  public readonly tracer: Tracer;
66
65
 
@@ -73,17 +72,19 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
73
72
  protected readonly l1ToL2MessageSource: L1ToL2MessageSource,
74
73
  protected readonly contractDataSource: ContractDataSource,
75
74
  protected readonly worldState: WorldStateSynchronizer,
76
- protected readonly p2pClient: Pick<P2PClient<P2PClientType.Prover>, 'getTxProvider'> & Partial<Service>,
75
+ protected readonly p2pClient: { getTxProvider(): ITxProvider } & Partial<Service>,
77
76
  protected readonly epochsMonitor: EpochMonitor,
78
77
  protected readonly rollupContract: RollupContract,
79
78
  protected readonly l1Metrics: L1Metrics,
80
79
  config: Partial<ProverNodeOptions> = {},
81
80
  protected readonly telemetryClient: TelemetryClient = getTelemetryClient(),
81
+ private delayer?: Delayer,
82
+ private readonly dateProvider: DateProvider = new DateProvider(),
82
83
  ) {
83
84
  this.config = {
84
85
  proverNodePollingIntervalMs: 1_000,
85
86
  proverNodeMaxPendingJobs: 100,
86
- proverNodeMaxParallelBlocksPerEpoch: 32,
87
+ proverNodeMaxParallelBlocksPerEpoch: 0,
87
88
  txGatheringIntervalMs: 1_000,
88
89
  txGatheringBatchSize: 10,
89
90
  txGatheringMaxParallelRequestsPerNode: 100,
@@ -111,6 +112,11 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
111
112
  return this.p2pClient;
112
113
  }
113
114
 
115
+ /** Returns the shared tx delayer for prover L1 txs, if enabled. Test-only. */
116
+ public getDelayer(): Delayer | undefined {
117
+ return this.delayer;
118
+ }
119
+
114
120
  /**
115
121
  * Handles an epoch being completed by starting a proof for it if there are no active jobs for it.
116
122
  * @param epochNumber - The epoch number that was just completed.
@@ -121,15 +127,7 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
121
127
  this.log.debug(`Running jobs as ${epochNumber} is ready to prove`, {
122
128
  jobs: Array.from(this.jobs.values()).map(job => `${job.getEpochNumber()}:${job.getId()}`),
123
129
  });
124
- const activeJobs = await this.getActiveJobsForEpoch(epochNumber);
125
- if (activeJobs.length > 0) {
126
- this.log.warn(`Not starting proof for ${epochNumber} since there are active jobs for the epoch`, {
127
- activeJobs: activeJobs.map(job => job.uuid),
128
- });
129
- return true;
130
- }
131
- await this.startProof(epochNumber);
132
- return true;
130
+ return await this.startProofIfNeeded(epochNumber);
133
131
  } catch (err) {
134
132
  if (err instanceof EmptyEpochError) {
135
133
  this.log.info(`Not starting proof for ${epochNumber} since no blocks were found`);
@@ -155,17 +153,15 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
155
153
 
156
154
  /**
157
155
  * Stops the prover node and all its dependencies.
156
+ * Resources not owned by this node (shared with the parent aztec-node) are skipped.
158
157
  */
159
158
  async stop() {
160
159
  this.log.info('Stopping ProverNode');
161
160
  await this.epochsMonitor.stop();
162
- await this.prover.stop();
163
- await tryStop(this.p2pClient);
164
- await tryStop(this.l2BlockSource);
165
- await tryStop(this.publisherFactory);
166
161
  this.publisher?.interrupt();
167
162
  await Promise.all(Array.from(this.jobs.values()).map(job => job.stop()));
168
- await this.worldState.stop();
163
+ await this.prover.stop();
164
+ await tryStop(this.publisherFactory);
169
165
  this.rewardsMetrics.stop();
170
166
  this.l1Metrics.stop();
171
167
  await this.telemetryClient.stop();
@@ -187,7 +183,38 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
187
183
  * Starts a proving process and returns immediately.
188
184
  */
189
185
  public async startProof(epochNumber: EpochNumber) {
190
- const job = await this.createProvingJob(epochNumber, { skipEpochCheck: true });
186
+ await this.startProofIfNeeded(epochNumber, { skipEpochCheck: true });
187
+ }
188
+
189
+ private async startProofIfNeeded(epochNumber: EpochNumber, opts: EpochProvingJobOptions = {}): Promise<boolean> {
190
+ if (this.startingProofEpochs.has(epochNumber)) {
191
+ this.log.warn(`Not starting proof for ${epochNumber} since a proof is already being started for the epoch`, {
192
+ epochNumber,
193
+ });
194
+ return false;
195
+ }
196
+
197
+ this.startingProofEpochs.add(epochNumber);
198
+
199
+ try {
200
+ const activeJobs = await this.activeJobsCoverEpoch(epochNumber);
201
+ if (activeJobs.length > 0) {
202
+ this.log.warn(`Not starting proof for ${epochNumber} since an active job already covers the epoch`, {
203
+ epochNumber,
204
+ activeJobs,
205
+ });
206
+ return true;
207
+ }
208
+
209
+ await this.startProofInternal(epochNumber, opts);
210
+ return true;
211
+ } finally {
212
+ this.startingProofEpochs.delete(epochNumber);
213
+ }
214
+ }
215
+
216
+ private async startProofInternal(epochNumber: EpochNumber, opts: EpochProvingJobOptions = {}) {
217
+ const job = await this.createProvingJob(epochNumber, opts);
191
218
  void this.runJob(job);
192
219
  }
193
220
 
@@ -241,20 +268,42 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
241
268
  * Returns an array of jobs being processed.
242
269
  */
243
270
  public getJobs(): Promise<{ uuid: string; status: EpochProvingJobState; epochNumber: EpochNumber }[]> {
244
- return Promise.resolve(
245
- Array.from(this.jobs.entries()).map(([uuid, job]) => ({
246
- uuid,
247
- status: job.getState(),
248
- epochNumber: job.getEpochNumber(),
249
- })),
250
- );
271
+ return Promise.resolve(this.getJobsInternal());
251
272
  }
252
273
 
253
- protected async getActiveJobsForEpoch(
254
- epochNumber: EpochNumber,
255
- ): Promise<{ uuid: string; status: EpochProvingJobState }[]> {
256
- const jobs = await this.getJobs();
257
- return jobs.filter(job => job.epochNumber === epochNumber && !EpochProvingJobTerminalState.includes(job.status));
274
+ private getJobsInternal(): { uuid: string; status: EpochProvingJobState; epochNumber: EpochNumber }[] {
275
+ return Array.from(this.jobs.entries()).map(([uuid, job]) => ({
276
+ uuid,
277
+ status: job.getState(),
278
+ epochNumber: job.getEpochNumber(),
279
+ }));
280
+ }
281
+
282
+ private async activeJobsCoverEpoch(epochNumber: EpochNumber): Promise<string[]> {
283
+ const checkpoints = await this.l2BlockSource.getCheckpointsData({ epoch: epochNumber });
284
+ if (checkpoints.length === 0) {
285
+ return [];
286
+ }
287
+
288
+ const firstCheckpoint = checkpoints.at(0)!.checkpointNumber;
289
+ const latestCheckpoint = checkpoints.at(-1)!.checkpointNumber;
290
+
291
+ const jobs: string[] = [];
292
+ for (const job of this.jobs.values()) {
293
+ if (job.getEpochNumber() !== epochNumber) {
294
+ continue;
295
+ }
296
+
297
+ const jobCheckpoints = job.getProvingData().checkpoints;
298
+ const checkpointOverlap =
299
+ jobCheckpoints.at(0)!.number <= firstCheckpoint && jobCheckpoints.at(-1)!.number >= latestCheckpoint;
300
+
301
+ if (checkpointOverlap && !['failed', 'stopped', 'timed-out'].includes(job.getState())) {
302
+ jobs.push(job.getId());
303
+ }
304
+ }
305
+
306
+ return jobs;
258
307
  }
259
308
 
260
309
  private checkMaximumPendingJobs() {
@@ -275,13 +324,15 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
275
324
  const fromCheckpoint = epochData.checkpoints[0].number;
276
325
  const toCheckpoint = epochData.checkpoints.at(-1)!.number;
277
326
  const fromBlock = epochData.checkpoints[0].blocks[0].number;
278
- const toBlock = epochData.checkpoints.at(-1)!.blocks.at(-1)!.number;
327
+ const lastBlock = epochData.checkpoints.at(-1)!.blocks.at(-1)!;
328
+ const toBlock = lastBlock.number;
279
329
  this.log.verbose(
280
330
  `Creating proving job for epoch ${epochNumber} for checkpoint range ${fromCheckpoint} to ${toCheckpoint} and block range ${fromBlock} to ${toBlock}`,
281
331
  );
282
332
 
283
333
  // Fast forward world state to right before the target block and get a fork
284
- await this.worldState.syncImmediate(toBlock);
334
+ const lastBlockHash = await lastBlock.header.hash();
335
+ await this.worldState.syncImmediate(toBlock, lastBlockHash);
285
336
 
286
337
  // Create a processor factory
287
338
  const publicProcessorFactory = new PublicProcessorFactory(
@@ -306,26 +357,21 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
306
357
 
307
358
  @trackSpan('ProverNode.gatherEpochData', epochNumber => ({ [Attributes.EPOCH_NUMBER]: epochNumber }))
308
359
  private async gatherEpochData(epochNumber: EpochNumber): Promise<EpochProvingJobData> {
309
- const checkpoints = await this.gatherCheckpoints(epochNumber);
360
+ const publishedCheckpoints = await this.l2BlockSource.getCheckpoints({ epoch: epochNumber });
361
+ if (publishedCheckpoints.length === 0) {
362
+ throw new EmptyEpochError(epochNumber);
363
+ }
364
+ const checkpoints = publishedCheckpoints.map(p => p.checkpoint);
365
+ const attestations = publishedCheckpoints.at(-1)?.attestations ?? [];
310
366
  const txArray = await this.gatherTxs(epochNumber, checkpoints);
311
367
  const txs = new Map<string, Tx>(txArray.map(tx => [tx.getTxHash().toString(), tx]));
312
368
  const l1ToL2Messages = await this.gatherMessages(epochNumber, checkpoints);
313
369
  const [firstBlock] = checkpoints[0].blocks;
314
370
  const previousBlockHeader = await this.gatherPreviousBlockHeader(epochNumber, firstBlock.number - 1);
315
- const [lastPublishedCheckpoint] = await this.l2BlockSource.getCheckpoints(checkpoints.at(-1)!.number, 1);
316
- const attestations = lastPublishedCheckpoint?.attestations ?? [];
317
371
 
318
372
  return { checkpoints, txs, l1ToL2Messages, epochNumber, previousBlockHeader, attestations };
319
373
  }
320
374
 
321
- private async gatherCheckpoints(epochNumber: EpochNumber) {
322
- const checkpoints = await this.l2BlockSource.getCheckpointsForEpoch(epochNumber);
323
- if (checkpoints.length === 0) {
324
- throw new EmptyEpochError(epochNumber);
325
- }
326
- return checkpoints;
327
- }
328
-
329
375
  private async gatherTxs(epochNumber: EpochNumber, checkpoints: Checkpoint[]) {
330
376
  const deadline = new Date(this.dateProvider.now() + this.config.txGatheringTimeoutMs);
331
377
  const txProvider = this.p2pClient.getTxProvider();
@@ -354,16 +400,13 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
354
400
  }
355
401
 
356
402
  private async gatherPreviousBlockHeader(epochNumber: EpochNumber, previousBlockNumber: number) {
357
- const header = await (previousBlockNumber === 0
358
- ? this.worldState.getCommitted().getInitialHeader()
359
- : this.l2BlockSource.getBlockHeader(BlockNumber(previousBlockNumber)));
360
-
361
- if (!header) {
403
+ const data = await this.l2BlockSource.getBlockData({ number: BlockNumber(previousBlockNumber) });
404
+ if (!data?.header) {
362
405
  throw new Error(`Previous block header ${previousBlockNumber} not found for proving epoch ${epochNumber}`);
363
406
  }
364
407
 
365
- this.log.verbose(`Gathered previous block header ${header.getBlockNumber()} for epoch ${epochNumber}`);
366
- return header;
408
+ this.log.verbose(`Gathered previous block header ${data.header.getBlockNumber()} for epoch ${epochNumber}`);
409
+ return data.header;
367
410
  }
368
411
 
369
412
  /** Extracted for testing purposes. */
@@ -1,29 +1,31 @@
1
1
  import type { RollupContract } from '@aztec/ethereum/contracts';
2
2
  import type { L1TxUtils } from '@aztec/ethereum/l1-tx-utils';
3
3
  import type { PublisherManager } from '@aztec/ethereum/publisher-manager';
4
+ import type { EthAddress } from '@aztec/foundation/eth-address';
4
5
  import type { LoggerBindings } from '@aztec/foundation/log';
5
- import type { PublisherConfig, TxSenderConfig } from '@aztec/sequencer-client';
6
+ import type { ProverPublisherConfig, ProverTxSenderConfig } from '@aztec/sequencer-client';
6
7
  import type { TelemetryClient } from '@aztec/telemetry-client';
7
8
 
8
9
  import { ProverNodePublisher } from './prover-node-publisher.js';
9
10
 
10
11
  export class ProverPublisherFactory {
11
12
  constructor(
12
- private config: TxSenderConfig & PublisherConfig,
13
+ private config: ProverTxSenderConfig & ProverPublisherConfig,
13
14
  private deps: {
14
15
  rollupContract: RollupContract;
15
16
  publisherManager: PublisherManager<L1TxUtils>;
17
+ proofSubmissionTarget?: EthAddress;
16
18
  telemetry?: TelemetryClient;
17
19
  },
18
20
  private bindings?: LoggerBindings,
19
21
  ) {}
20
22
 
21
23
  public async start() {
22
- await this.deps.publisherManager.loadState();
24
+ await this.deps.publisherManager.start();
23
25
  }
24
26
 
25
- public stop() {
26
- this.deps.publisherManager.interrupt();
27
+ public async stop() {
28
+ await this.deps.publisherManager.stop();
27
29
  }
28
30
 
29
31
  /**
@@ -37,6 +39,7 @@ export class ProverPublisherFactory {
37
39
  {
38
40
  rollupContract: this.deps.rollupContract,
39
41
  l1TxUtils: l1Publisher,
42
+ proofSubmissionTarget: this.deps.proofSubmissionTarget,
40
43
  telemetry: this.deps.telemetry,
41
44
  },
42
45
  this.bindings,