@aztec/prover-node 0.0.0-test.0 → 0.0.1-commit.21caa21

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 (82) hide show
  1. package/dest/actions/download-epoch-proving-job.d.ts +18 -0
  2. package/dest/actions/download-epoch-proving-job.d.ts.map +1 -0
  3. package/dest/actions/download-epoch-proving-job.js +37 -0
  4. package/dest/actions/index.d.ts +3 -0
  5. package/dest/actions/index.d.ts.map +1 -0
  6. package/dest/actions/index.js +2 -0
  7. package/dest/actions/rerun-epoch-proving-job.d.ts +11 -0
  8. package/dest/actions/rerun-epoch-proving-job.d.ts.map +1 -0
  9. package/dest/actions/rerun-epoch-proving-job.js +40 -0
  10. package/dest/actions/upload-epoch-proof-failure.d.ts +15 -0
  11. package/dest/actions/upload-epoch-proof-failure.d.ts.map +1 -0
  12. package/dest/actions/upload-epoch-proof-failure.js +78 -0
  13. package/dest/bin/run-failed-epoch.d.ts +2 -0
  14. package/dest/bin/run-failed-epoch.d.ts.map +1 -0
  15. package/dest/bin/run-failed-epoch.js +67 -0
  16. package/dest/config.d.ts +13 -10
  17. package/dest/config.d.ts.map +1 -1
  18. package/dest/config.js +81 -14
  19. package/dest/factory.d.ts +13 -9
  20. package/dest/factory.d.ts.map +1 -1
  21. package/dest/factory.js +95 -31
  22. package/dest/index.d.ts +2 -2
  23. package/dest/index.d.ts.map +1 -1
  24. package/dest/index.js +1 -1
  25. package/dest/job/epoch-proving-job-data.d.ts +17 -0
  26. package/dest/job/epoch-proving-job-data.d.ts.map +1 -0
  27. package/dest/job/epoch-proving-job-data.js +53 -0
  28. package/dest/job/epoch-proving-job.d.ts +25 -17
  29. package/dest/job/epoch-proving-job.d.ts.map +1 -1
  30. package/dest/job/epoch-proving-job.js +155 -51
  31. package/dest/metrics.d.ts +29 -5
  32. package/dest/metrics.d.ts.map +1 -1
  33. package/dest/metrics.js +141 -35
  34. package/dest/monitors/epoch-monitor.d.ts +5 -2
  35. package/dest/monitors/epoch-monitor.d.ts.map +1 -1
  36. package/dest/monitors/epoch-monitor.js +15 -2
  37. package/dest/monitors/index.d.ts +1 -1
  38. package/dest/prover-node-publisher.d.ts +10 -12
  39. package/dest/prover-node-publisher.d.ts.map +1 -1
  40. package/dest/prover-node-publisher.js +62 -62
  41. package/dest/prover-node.d.ts +48 -43
  42. package/dest/prover-node.d.ts.map +1 -1
  43. package/dest/prover-node.js +175 -105
  44. package/dest/prover-publisher-factory.d.ts +21 -0
  45. package/dest/prover-publisher-factory.d.ts.map +1 -0
  46. package/dest/prover-publisher-factory.js +26 -0
  47. package/dest/test/index.d.ts +5 -3
  48. package/dest/test/index.d.ts.map +1 -1
  49. package/dest/test/index.js +1 -3
  50. package/package.json +39 -33
  51. package/src/actions/download-epoch-proving-job.ts +44 -0
  52. package/src/actions/index.ts +2 -0
  53. package/src/actions/rerun-epoch-proving-job.ts +61 -0
  54. package/src/actions/upload-epoch-proof-failure.ts +88 -0
  55. package/src/bin/run-failed-epoch.ts +77 -0
  56. package/src/config.ts +108 -24
  57. package/src/factory.ts +163 -43
  58. package/src/index.ts +1 -1
  59. package/src/job/epoch-proving-job-data.ts +77 -0
  60. package/src/job/epoch-proving-job.ts +222 -52
  61. package/src/metrics.ts +135 -37
  62. package/src/monitors/epoch-monitor.ts +18 -6
  63. package/src/prover-node-publisher.ts +96 -88
  64. package/src/prover-node.ts +213 -136
  65. package/src/prover-publisher-factory.ts +37 -0
  66. package/src/test/index.ts +7 -4
  67. package/dest/http.d.ts +0 -8
  68. package/dest/http.d.ts.map +0 -1
  69. package/dest/http.js +0 -9
  70. package/dest/prover-coordination/config.d.ts +0 -7
  71. package/dest/prover-coordination/config.d.ts.map +0 -1
  72. package/dest/prover-coordination/config.js +0 -11
  73. package/dest/prover-coordination/factory.d.ts +0 -22
  74. package/dest/prover-coordination/factory.d.ts.map +0 -1
  75. package/dest/prover-coordination/factory.js +0 -42
  76. package/dest/prover-coordination/index.d.ts +0 -3
  77. package/dest/prover-coordination/index.d.ts.map +0 -1
  78. package/dest/prover-coordination/index.js +0 -2
  79. package/src/http.ts +0 -13
  80. package/src/prover-coordination/config.ts +0 -17
  81. package/src/prover-coordination/factory.ts +0 -72
  82. package/src/prover-coordination/index.ts +0 -2
@@ -1,28 +1,33 @@
1
- import { compact } from '@aztec/foundation/collection';
1
+ import type { Archiver } from '@aztec/archiver';
2
+ import type { RollupContract } from '@aztec/ethereum';
3
+ import { EpochNumber } from '@aztec/foundation/branded-types';
4
+ import { assertRequired, compact, pick, sum } from '@aztec/foundation/collection';
2
5
  import { memoize } from '@aztec/foundation/decorators';
6
+ import type { Fr } from '@aztec/foundation/fields';
3
7
  import { createLogger } from '@aztec/foundation/log';
4
- import { RunningPromise } from '@aztec/foundation/running-promise';
5
8
  import { DateProvider } from '@aztec/foundation/timer';
6
- import type { Maybe } from '@aztec/foundation/types';
7
- import type { P2P } from '@aztec/p2p';
9
+ import type { DataStoreConfig } from '@aztec/kv-store/config';
10
+ import type { P2PClient } from '@aztec/p2p';
8
11
  import { PublicProcessorFactory } from '@aztec/simulator/server';
9
12
  import type { L2Block, L2BlockSource } from '@aztec/stdlib/block';
13
+ import type { ChainConfig } from '@aztec/stdlib/config';
10
14
  import type { ContractDataSource } from '@aztec/stdlib/contract';
11
- import { getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
15
+ import { getProofSubmissionDeadlineTimestamp } from '@aztec/stdlib/epoch-helpers';
12
16
  import {
13
17
  type EpochProverManager,
14
18
  EpochProvingJobTerminalState,
15
- type ProverCoordination,
16
19
  type ProverNodeApi,
17
20
  type Service,
21
+ type WorldStateSyncStatus,
18
22
  type WorldStateSynchronizer,
19
23
  tryStop,
20
24
  } from '@aztec/stdlib/interfaces/server';
21
25
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
22
26
  import type { P2PClientType } from '@aztec/stdlib/p2p';
23
- import type { Tx, TxHash } from '@aztec/stdlib/tx';
27
+ import type { Tx } from '@aztec/stdlib/tx';
24
28
  import {
25
29
  Attributes,
30
+ L1Metrics,
26
31
  type TelemetryClient,
27
32
  type Traceable,
28
33
  type Tracer,
@@ -30,66 +35,71 @@ import {
30
35
  trackSpan,
31
36
  } from '@aztec/telemetry-client';
32
37
 
38
+ import { uploadEpochProofFailure } from './actions/upload-epoch-proof-failure.js';
39
+ import type { SpecificProverNodeConfig } from './config.js';
40
+ import type { EpochProvingJobData } from './job/epoch-proving-job-data.js';
33
41
  import { EpochProvingJob, type EpochProvingJobState } from './job/epoch-proving-job.js';
34
- import { ProverNodeMetrics } from './metrics.js';
42
+ import { ProverNodeJobMetrics, ProverNodeRewardsMetrics } from './metrics.js';
35
43
  import type { EpochMonitor, EpochMonitorHandler } from './monitors/epoch-monitor.js';
36
44
  import type { ProverNodePublisher } from './prover-node-publisher.js';
45
+ import type { ProverPublisherFactory } from './prover-publisher-factory.js';
37
46
 
38
- export type ProverNodeOptions = {
39
- pollingIntervalMs: number;
40
- maxPendingJobs: number;
41
- maxParallelBlocksPerEpoch: number;
42
- txGatheringTimeoutMs: number;
43
- txGatheringIntervalMs: number;
44
- txGatheringMaxParallelRequests: number;
45
- };
47
+ type ProverNodeOptions = SpecificProverNodeConfig & Partial<DataStoreOptions>;
48
+ type DataStoreOptions = Pick<DataStoreConfig, 'dataDirectory'> & Pick<ChainConfig, 'l1ChainId' | 'rollupVersion'>;
46
49
 
47
50
  /**
48
- * An Aztec Prover Node is a standalone process that monitors the unfinalised chain on L1 for unproven blocks,
49
- * submits bids for proving them, and monitors if they are accepted. If so, the prover node fetches the txs
50
- * from a tx source in the p2p network or an external node, re-executes their public functions, creates a rollup
51
+ * An Aztec Prover Node is a standalone process that monitors the unfinalized chain on L1 for unproven epochs,
52
+ * fetches their txs from the p2p network or external nodes, re-executes their public functions, creates a rollup
51
53
  * proof for the epoch, and submits it to L1.
52
54
  */
53
55
  export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable {
54
56
  private log = createLogger('prover-node');
55
57
  private dateProvider = new DateProvider();
56
58
 
57
- private latestEpochWeAreProving: bigint | undefined;
58
59
  private jobs: Map<string, EpochProvingJob> = new Map();
59
- private cachedEpochData: { epochNumber: bigint; blocks: L2Block[]; txs: Tx[] } | undefined = undefined;
60
- private options: ProverNodeOptions;
61
- private metrics: ProverNodeMetrics;
62
-
63
- private txFetcher: RunningPromise;
64
- private lastBlockNumber: number | undefined;
60
+ private config: ProverNodeOptions;
61
+ private jobMetrics: ProverNodeJobMetrics;
62
+ private rewardsMetrics: ProverNodeRewardsMetrics;
65
63
 
66
64
  public readonly tracer: Tracer;
67
65
 
66
+ protected publisher: ProverNodePublisher | undefined;
67
+
68
68
  constructor(
69
69
  protected readonly prover: EpochProverManager,
70
- protected readonly publisher: ProverNodePublisher,
71
- protected readonly l2BlockSource: L2BlockSource & Maybe<Service>,
70
+ protected readonly publisherFactory: ProverPublisherFactory,
71
+ protected readonly l2BlockSource: L2BlockSource & Partial<Service>,
72
72
  protected readonly l1ToL2MessageSource: L1ToL2MessageSource,
73
73
  protected readonly contractDataSource: ContractDataSource,
74
74
  protected readonly worldState: WorldStateSynchronizer,
75
- protected readonly coordination: ProverCoordination & Maybe<Service>,
75
+ protected readonly p2pClient: Pick<P2PClient<P2PClientType.Prover>, 'getTxProvider'> & Partial<Service>,
76
76
  protected readonly epochsMonitor: EpochMonitor,
77
- options: Partial<ProverNodeOptions> = {},
77
+ protected readonly rollupContract: RollupContract,
78
+ protected readonly l1Metrics: L1Metrics,
79
+ config: Partial<ProverNodeOptions> = {},
78
80
  protected readonly telemetryClient: TelemetryClient = getTelemetryClient(),
79
81
  ) {
80
- this.options = {
81
- pollingIntervalMs: 1_000,
82
- maxPendingJobs: 100,
83
- maxParallelBlocksPerEpoch: 32,
84
- txGatheringTimeoutMs: 60_000,
82
+ this.config = {
83
+ proverNodePollingIntervalMs: 1_000,
84
+ proverNodeMaxPendingJobs: 100,
85
+ proverNodeMaxParallelBlocksPerEpoch: 32,
85
86
  txGatheringIntervalMs: 1_000,
86
- txGatheringMaxParallelRequests: 100,
87
- ...compact(options),
87
+ txGatheringBatchSize: 10,
88
+ txGatheringMaxParallelRequestsPerNode: 100,
89
+ txGatheringTimeoutMs: 120_000,
90
+ proverNodeFailedEpochStore: undefined,
91
+ proverNodeEpochProvingDelayMs: undefined,
92
+ ...compact(config),
88
93
  };
89
94
 
90
- this.metrics = new ProverNodeMetrics(telemetryClient, 'ProverNode');
95
+ this.validateConfig();
96
+
97
+ const meter = telemetryClient.getMeter('ProverNode');
91
98
  this.tracer = telemetryClient.getTracer('ProverNode');
92
- this.txFetcher = new RunningPromise(() => this.checkForTxs(), this.log, this.options.txGatheringIntervalMs);
99
+
100
+ this.jobMetrics = new ProverNodeJobMetrics(meter, telemetryClient.getTracer('EpochProvingJob'));
101
+
102
+ this.rewardsMetrics = new ProverNodeRewardsMetrics(meter, this.prover.getProverId(), rollupContract);
93
103
  }
94
104
 
95
105
  public getProverId() {
@@ -97,44 +107,49 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
97
107
  }
98
108
 
99
109
  public getP2P() {
100
- const asP2PClient = this.coordination as P2P<P2PClientType.Prover>;
101
- if (typeof asP2PClient.isP2PClient === 'function' && asP2PClient.isP2PClient()) {
102
- return asP2PClient;
103
- }
104
- return undefined;
110
+ return this.p2pClient;
105
111
  }
106
112
 
107
113
  /**
108
114
  * Handles an epoch being completed by starting a proof for it if there are no active jobs for it.
109
115
  * @param epochNumber - The epoch number that was just completed.
116
+ * @returns false if there is an error, true otherwise
110
117
  */
111
- async handleEpochReadyToProve(epochNumber: bigint): Promise<void> {
118
+ async handleEpochReadyToProve(epochNumber: EpochNumber): Promise<boolean> {
112
119
  try {
113
- this.log.debug('jobs', JSON.stringify(this.jobs, null, 2));
120
+ this.log.debug(`Running jobs as ${epochNumber} is ready to prove`, {
121
+ jobs: Array.from(this.jobs.values()).map(job => `${job.getEpochNumber()}:${job.getId()}`),
122
+ });
114
123
  const activeJobs = await this.getActiveJobsForEpoch(epochNumber);
115
124
  if (activeJobs.length > 0) {
116
- this.log.info(`Not starting proof for ${epochNumber} since there are active jobs`);
117
- return;
125
+ this.log.warn(`Not starting proof for ${epochNumber} since there are active jobs for the epoch`, {
126
+ activeJobs: activeJobs.map(job => job.uuid),
127
+ });
128
+ return true;
118
129
  }
119
- // TODO: we probably want to skip starting a proof if we are too far into the current epoch
120
130
  await this.startProof(epochNumber);
131
+ return true;
121
132
  } catch (err) {
122
133
  if (err instanceof EmptyEpochError) {
123
134
  this.log.info(`Not starting proof for ${epochNumber} since no blocks were found`);
124
135
  } else {
125
136
  this.log.error(`Error handling epoch completed`, err);
126
137
  }
138
+ return false;
127
139
  }
128
140
  }
129
141
 
130
142
  /**
131
- * Starts the prover node so it periodically checks for unproven epochs in the unfinalised chain from L1 and
143
+ * Starts the prover node so it periodically checks for unproven epochs in the unfinalized chain from L1 and
132
144
  * starts proving jobs for them.
133
145
  */
134
- start() {
135
- this.txFetcher.start();
146
+ async start() {
136
147
  this.epochsMonitor.start(this);
137
- this.log.info('Started ProverNode', this.options);
148
+ await this.publisherFactory.start();
149
+ this.publisher = await this.publisherFactory.create();
150
+ await this.rewardsMetrics.start();
151
+ this.l1Metrics.start();
152
+ this.log.info(`Started Prover Node with prover id ${this.prover.getProverId().toString()}`, this.config);
138
153
  }
139
154
 
140
155
  /**
@@ -142,32 +157,76 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
142
157
  */
143
158
  async stop() {
144
159
  this.log.info('Stopping ProverNode');
145
- await this.txFetcher.stop();
146
160
  await this.epochsMonitor.stop();
147
161
  await this.prover.stop();
162
+ await tryStop(this.p2pClient);
148
163
  await tryStop(this.l2BlockSource);
149
- this.publisher.interrupt();
164
+ await tryStop(this.publisherFactory);
165
+ this.publisher?.interrupt();
150
166
  await Promise.all(Array.from(this.jobs.values()).map(job => job.stop()));
151
167
  await this.worldState.stop();
152
- await tryStop(this.coordination);
168
+ this.rewardsMetrics.stop();
169
+ this.l1Metrics.stop();
153
170
  await this.telemetryClient.stop();
154
171
  this.log.info('Stopped ProverNode');
155
172
  }
156
173
 
157
- /**
158
- * Creates a proof for a block range. Returns once the proof has been submitted to L1.
159
- */
160
- public async prove(epochNumber: number | bigint) {
161
- const job = await this.createProvingJob(BigInt(epochNumber));
162
- return job.run();
174
+ /** Returns world state status. */
175
+ public async getWorldStateSyncStatus(): Promise<WorldStateSyncStatus> {
176
+ const { syncSummary } = await this.worldState.status();
177
+ return syncSummary;
178
+ }
179
+
180
+ /** Returns archiver status. */
181
+ public getL2Tips() {
182
+ return this.l2BlockSource.getL2Tips();
163
183
  }
164
184
 
165
185
  /**
166
186
  * Starts a proving process and returns immediately.
167
187
  */
168
- public async startProof(epochNumber: number | bigint) {
169
- const job = await this.createProvingJob(BigInt(epochNumber));
170
- void job.run().catch(err => this.log.error(`Error proving epoch ${epochNumber}`, err));
188
+ public async startProof(epochNumber: EpochNumber) {
189
+ const job = await this.createProvingJob(epochNumber, { skipEpochCheck: true });
190
+ void this.runJob(job);
191
+ }
192
+
193
+ private async runJob(job: EpochProvingJob) {
194
+ const epochNumber = job.getEpochNumber();
195
+ const ctx = { id: job.getId(), epochNumber, state: undefined as EpochProvingJobState | undefined };
196
+
197
+ try {
198
+ await job.run();
199
+ const state = job.getState();
200
+ ctx.state = state;
201
+
202
+ if (state === 'reorg') {
203
+ this.log.warn(`Running new job for epoch ${epochNumber} due to reorg`, ctx);
204
+ await this.createProvingJob(epochNumber);
205
+ } else if (state === 'failed') {
206
+ this.log.error(`Job for ${epochNumber} exited with state ${state}`, ctx);
207
+ await this.tryUploadEpochFailure(job);
208
+ } else {
209
+ this.log.verbose(`Job for ${epochNumber} exited with state ${state}`, ctx);
210
+ }
211
+ } catch (err) {
212
+ this.log.error(`Error proving epoch ${epochNumber}`, err, ctx);
213
+ } finally {
214
+ this.jobs.delete(job.getId());
215
+ }
216
+ }
217
+
218
+ protected async tryUploadEpochFailure(job: EpochProvingJob) {
219
+ if (this.config.proverNodeFailedEpochStore) {
220
+ return await uploadEpochProofFailure(
221
+ this.config.proverNodeFailedEpochStore,
222
+ job.getId(),
223
+ job.getProvingData(),
224
+ this.l2BlockSource as Archiver,
225
+ this.worldState,
226
+ assertRequired(pick(this.config, 'l1ChainId', 'rollupVersion', 'dataDirectory')),
227
+ this.log,
228
+ );
229
+ }
171
230
  }
172
231
 
173
232
  /**
@@ -180,62 +239,58 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
180
239
  /**
181
240
  * Returns an array of jobs being processed.
182
241
  */
183
- public getJobs(): Promise<{ uuid: string; status: EpochProvingJobState; epochNumber: number }[]> {
242
+ public getJobs(): Promise<{ uuid: string; status: EpochProvingJobState; epochNumber: EpochNumber }[]> {
184
243
  return Promise.resolve(
185
244
  Array.from(this.jobs.entries()).map(([uuid, job]) => ({
186
245
  uuid,
187
246
  status: job.getState(),
188
- epochNumber: Number(job.getEpochNumber()),
247
+ epochNumber: job.getEpochNumber(),
189
248
  })),
190
249
  );
191
250
  }
192
251
 
193
252
  protected async getActiveJobsForEpoch(
194
- epochBigInt: bigint,
253
+ epochNumber: EpochNumber,
195
254
  ): Promise<{ uuid: string; status: EpochProvingJobState }[]> {
196
255
  const jobs = await this.getJobs();
197
- const epochNumber = Number(epochBigInt);
198
256
  return jobs.filter(job => job.epochNumber === epochNumber && !EpochProvingJobTerminalState.includes(job.status));
199
257
  }
200
258
 
201
259
  private checkMaximumPendingJobs() {
202
- const { maxPendingJobs } = this.options;
203
- return maxPendingJobs === 0 || this.jobs.size < maxPendingJobs;
260
+ const { proverNodeMaxPendingJobs: maxPendingJobs } = this.config;
261
+ if (maxPendingJobs > 0 && this.jobs.size >= maxPendingJobs) {
262
+ throw new Error(`Maximum pending proving jobs ${maxPendingJobs} reached. Cannot create new job.`);
263
+ }
204
264
  }
205
265
 
206
- @trackSpan('ProverNode.createProvingJob', epochNumber => ({ [Attributes.EPOCH_NUMBER]: Number(epochNumber) }))
207
- private async createProvingJob(epochNumber: bigint) {
208
- if (!this.checkMaximumPendingJobs()) {
209
- throw new Error(`Maximum pending proving jobs ${this.options.maxPendingJobs} reached. Cannot create new job.`);
210
- }
266
+ @trackSpan('ProverNode.createProvingJob', epochNumber => ({ [Attributes.EPOCH_NUMBER]: epochNumber }))
267
+ private async createProvingJob(epochNumber: EpochNumber, opts: { skipEpochCheck?: boolean } = {}) {
268
+ this.checkMaximumPendingJobs();
211
269
 
212
- // Gather blocks for this epoch
213
- const cachedEpochData = this.cachedEpochData?.epochNumber === epochNumber ? this.cachedEpochData : undefined;
214
- const { blocks, txs } = cachedEpochData ?? (await this.gatherEpochData(epochNumber));
270
+ this.publisher = await this.publisherFactory.create();
215
271
 
216
- const fromBlock = blocks[0].number;
217
- const toBlock = blocks.at(-1)!.number;
272
+ // Gather all data for this epoch
273
+ const epochData = await this.gatherEpochData(epochNumber);
218
274
 
219
- // Fast forward world state to right before the target block and get a fork
275
+ const fromBlock = epochData.blocks[0].number;
276
+ const toBlock = epochData.blocks.at(-1)!.number;
220
277
  this.log.verbose(`Creating proving job for epoch ${epochNumber} for block range ${fromBlock} to ${toBlock}`);
278
+
279
+ // Fast forward world state to right before the target block and get a fork
221
280
  await this.worldState.syncImmediate(toBlock);
222
281
 
223
- // Create a processor using the forked world state
282
+ // Create a processor factory
224
283
  const publicProcessorFactory = new PublicProcessorFactory(
225
284
  this.contractDataSource,
226
285
  this.dateProvider,
227
286
  this.telemetryClient,
228
287
  );
229
288
 
230
- const cleanUp = () => {
231
- this.jobs.delete(job.getId());
232
- return Promise.resolve();
233
- };
289
+ // Set deadline for this job to run. It will abort if it takes too long.
290
+ const deadlineTs = getProofSubmissionDeadlineTimestamp(epochNumber, await this.getL1Constants());
291
+ const deadline = new Date(Number(deadlineTs) * 1000);
234
292
 
235
- const [_, endTimestamp] = getTimestampRangeForEpoch(epochNumber + 1n, await this.getL1Constants());
236
- const deadline = new Date(Number(endTimestamp) * 1000);
237
-
238
- const job = this.doCreateEpochProvingJob(epochNumber, deadline, blocks, txs, publicProcessorFactory, cleanUp);
293
+ const job = this.doCreateEpochProvingJob(epochData, deadline, publicProcessorFactory, this.publisher, opts);
239
294
  this.jobs.set(job.getId(), job);
240
295
  return job;
241
296
  }
@@ -245,32 +300,20 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
245
300
  return this.l2BlockSource.getL1Constants();
246
301
  }
247
302
 
248
- /** Monitors for new blocks and requests their txs from the p2p layer to ensure they are available for proving. */
249
- @trackSpan('ProverNode.checkForTxs')
250
- private async checkForTxs() {
251
- const blockNumber = await this.l2BlockSource.getBlockNumber();
252
- if (this.lastBlockNumber === undefined || blockNumber > this.lastBlockNumber) {
253
- const block = await this.l2BlockSource.getBlock(blockNumber);
254
- if (!block) {
255
- return;
256
- }
257
- const txHashes = block.body.txEffects.map(tx => tx.txHash);
258
- this.log.verbose(`Fetching ${txHashes.length} tx hashes for block number ${blockNumber} from coordination`);
259
- await this.coordination.getTxsByHash(txHashes); // This stores the txs in the tx pool, no need to persist them here
260
- this.lastBlockNumber = blockNumber;
261
- }
262
- }
263
-
264
- @trackSpan('ProverNode.gatherEpochData', epochNumber => ({ [Attributes.EPOCH_NUMBER]: Number(epochNumber) }))
265
- private async gatherEpochData(epochNumber: bigint) {
266
- // Gather blocks for this epoch and their txs
303
+ @trackSpan('ProverNode.gatherEpochData', epochNumber => ({ [Attributes.EPOCH_NUMBER]: epochNumber }))
304
+ private async gatherEpochData(epochNumber: EpochNumber): Promise<EpochProvingJobData> {
267
305
  const blocks = await this.gatherBlocks(epochNumber);
268
- const txs = await this.gatherTxs(epochNumber, blocks);
269
-
270
- return { blocks, txs };
306
+ const txArray = await this.gatherTxs(epochNumber, blocks);
307
+ const txs = new Map<string, Tx>(txArray.map(tx => [tx.getTxHash().toString(), tx]));
308
+ const l1ToL2Messages = await this.gatherMessages(epochNumber, blocks);
309
+ const previousBlockHeader = await this.gatherPreviousBlockHeader(epochNumber, blocks[0]);
310
+ const [lastBlock] = await this.l2BlockSource.getPublishedBlocks(blocks.at(-1)!.number, 1);
311
+ const attestations = lastBlock?.attestations ?? [];
312
+
313
+ return { blocks, txs, l1ToL2Messages, epochNumber, previousBlockHeader, attestations };
271
314
  }
272
315
 
273
- private async gatherBlocks(epochNumber: bigint) {
316
+ private async gatherBlocks(epochNumber: EpochNumber) {
274
317
  const blocks = await this.l2BlockSource.getBlocksForEpoch(epochNumber);
275
318
  if (blocks.length === 0) {
276
319
  throw new EmptyEpochError(epochNumber);
@@ -278,46 +321,65 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
278
321
  return blocks;
279
322
  }
280
323
 
281
- private async gatherTxs(epochNumber: bigint, blocks: L2Block[]) {
282
- const txsToFind: TxHash[] = blocks.flatMap(block => block.body.txEffects.map(tx => tx.txHash));
283
- const txs = await this.coordination.getTxsByHash(txsToFind);
324
+ private async gatherTxs(epochNumber: EpochNumber, blocks: L2Block[]) {
325
+ const deadline = new Date(this.dateProvider.now() + this.config.txGatheringTimeoutMs);
326
+ const txProvider = this.p2pClient.getTxProvider();
327
+ const txsByBlock = await Promise.all(blocks.map(block => txProvider.getTxsForBlock(block, { deadline })));
328
+ const txs = txsByBlock.map(({ txs }) => txs).flat();
329
+ const missingTxs = txsByBlock.map(({ missingTxs }) => missingTxs).flat();
284
330
 
285
- if (txs.length === txsToFind.length) {
331
+ if (missingTxs.length === 0) {
286
332
  this.log.verbose(`Gathered all ${txs.length} txs for epoch ${epochNumber}`, { epochNumber });
287
333
  return txs;
288
334
  }
289
335
 
290
- const txHashesFound = await Promise.all(txs.map(tx => tx.getTxHash()));
291
- const missingTxHashes = txsToFind
292
- .filter(txHashToFind => !txHashesFound.some(txHashFound => txHashToFind.equals(txHashFound)))
293
- .join(', ');
336
+ throw new Error(`Txs not found for epoch ${epochNumber}: ${missingTxs.map(hash => hash.toString()).join(', ')}`);
337
+ }
294
338
 
295
- throw new Error(`Txs not found for epoch ${epochNumber}: ${missingTxHashes}`);
339
+ private async gatherMessages(epochNumber: EpochNumber, blocks: L2Block[]) {
340
+ const messages = await Promise.all(blocks.map(b => this.l1ToL2MessageSource.getL1ToL2Messages(b.number)));
341
+ const messageCount = sum(messages.map(m => m.length));
342
+ this.log.verbose(`Gathered all ${messageCount} messages for epoch ${epochNumber}`, { epochNumber });
343
+ const messagesByBlock: Record<number, Fr[]> = {};
344
+ for (let i = 0; i < blocks.length; i++) {
345
+ messagesByBlock[blocks[i].number] = messages[i];
346
+ }
347
+ return messagesByBlock;
348
+ }
349
+
350
+ private async gatherPreviousBlockHeader(epochNumber: EpochNumber, initialBlock: L2Block) {
351
+ const previousBlockNumber = initialBlock.number - 1;
352
+ const header = await (previousBlockNumber === 0
353
+ ? this.worldState.getCommitted().getInitialHeader()
354
+ : this.l2BlockSource.getBlockHeader(previousBlockNumber));
355
+
356
+ if (!header) {
357
+ throw new Error(`Previous block header ${initialBlock.number} not found for proving epoch ${epochNumber}`);
358
+ }
359
+
360
+ this.log.verbose(`Gathered previous block header ${header.getBlockNumber()} for epoch ${epochNumber}`);
361
+ return header;
296
362
  }
297
363
 
298
364
  /** Extracted for testing purposes. */
299
365
  protected doCreateEpochProvingJob(
300
- epochNumber: bigint,
366
+ data: EpochProvingJobData,
301
367
  deadline: Date | undefined,
302
- blocks: L2Block[],
303
- txs: Tx[],
304
368
  publicProcessorFactory: PublicProcessorFactory,
305
- cleanUp: () => Promise<void>,
369
+ publisher: ProverNodePublisher,
370
+ opts: { skipEpochCheck?: boolean } = {},
306
371
  ) {
372
+ const { proverNodeMaxParallelBlocksPerEpoch: parallelBlockLimit, proverNodeDisableProofPublish } = this.config;
307
373
  return new EpochProvingJob(
374
+ data,
308
375
  this.worldState,
309
- epochNumber,
310
- blocks,
311
- txs,
312
376
  this.prover.createEpochProver(),
313
377
  publicProcessorFactory,
314
- this.publisher,
378
+ publisher,
315
379
  this.l2BlockSource,
316
- this.l1ToL2MessageSource,
317
- this.metrics,
380
+ this.jobMetrics,
318
381
  deadline,
319
- { parallelBlockLimit: this.options.maxParallelBlocksPerEpoch },
320
- cleanUp,
382
+ { parallelBlockLimit, skipSubmitProof: proverNodeDisableProofPublish, ...opts },
321
383
  );
322
384
  }
323
385
 
@@ -325,10 +387,25 @@ export class ProverNode implements EpochMonitorHandler, ProverNodeApi, Traceable
325
387
  protected async triggerMonitors() {
326
388
  await this.epochsMonitor.work();
327
389
  }
390
+
391
+ private validateConfig() {
392
+ if (
393
+ this.config.proverNodeFailedEpochStore &&
394
+ (!this.config.dataDirectory || !this.config.l1ChainId || !this.config.rollupVersion)
395
+ ) {
396
+ this.log.warn(
397
+ `Invalid prover-node config (missing dataDirectory, l1ChainId, or rollupVersion)`,
398
+ pick(this.config, 'proverNodeFailedEpochStore', 'dataDirectory', 'l1ChainId', 'rollupVersion'),
399
+ );
400
+ throw new Error(
401
+ 'All of dataDirectory, l1ChainId, and rollupVersion are required if proverNodeFailedEpochStore is set.',
402
+ );
403
+ }
404
+ }
328
405
  }
329
406
 
330
407
  class EmptyEpochError extends Error {
331
- constructor(epochNumber: bigint) {
408
+ constructor(epochNumber: EpochNumber) {
332
409
  super(`No blocks found for epoch ${epochNumber}`);
333
410
  this.name = 'EmptyEpochError';
334
411
  }
@@ -0,0 +1,37 @@
1
+ import type { L1TxUtils, PublisherManager, RollupContract } from '@aztec/ethereum';
2
+ import type { PublisherConfig, TxSenderConfig } from '@aztec/sequencer-client';
3
+ import type { TelemetryClient } from '@aztec/telemetry-client';
4
+
5
+ import { ProverNodePublisher } from './prover-node-publisher.js';
6
+
7
+ export class ProverPublisherFactory {
8
+ constructor(
9
+ private config: TxSenderConfig & PublisherConfig,
10
+ private deps: {
11
+ rollupContract: RollupContract;
12
+ publisherManager: PublisherManager<L1TxUtils>;
13
+ telemetry?: TelemetryClient;
14
+ },
15
+ ) {}
16
+
17
+ public async start() {
18
+ await this.deps.publisherManager.loadState();
19
+ }
20
+
21
+ public stop() {
22
+ this.deps.publisherManager.interrupt();
23
+ }
24
+
25
+ /**
26
+ * Creates a new Prover Publisher instance.
27
+ * @returns A new ProverNodePublisher instance.
28
+ */
29
+ public async create(): Promise<ProverNodePublisher> {
30
+ const l1Publisher = await this.deps.publisherManager.getAvailablePublisher();
31
+ return new ProverNodePublisher(this.config, {
32
+ rollupContract: this.deps.rollupContract,
33
+ l1TxUtils: l1Publisher,
34
+ telemetry: this.deps.telemetry,
35
+ });
36
+ }
37
+ }
package/src/test/index.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  import type { EpochProverManager } from '@aztec/stdlib/interfaces/server';
2
2
 
3
+ import type { EpochProvingJob } from '../job/epoch-proving-job.js';
3
4
  import type { ProverNodePublisher } from '../prover-node-publisher.js';
4
5
  import { ProverNode } from '../prover-node.js';
5
6
 
6
- class TestProverNode_ extends ProverNode {
7
- public override prover!: EpochProverManager;
8
- public override publisher!: ProverNodePublisher;
7
+ abstract class TestProverNodeClass extends ProverNode {
8
+ declare public prover: EpochProverManager;
9
+ declare public publisher: ProverNodePublisher;
10
+
11
+ public abstract override tryUploadEpochFailure(job: EpochProvingJob): Promise<string | undefined>;
9
12
  }
10
13
 
11
- export type TestProverNode = TestProverNode_;
14
+ export type TestProverNode = TestProverNodeClass;
package/dest/http.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import type { ProverNode } from './prover-node.js';
2
- /**
3
- * Wrap a ProverNode instance with a JSON RPC HTTP server.
4
- * @param node - The ProverNode
5
- * @returns An JSON-RPC HTTP server
6
- */
7
- export declare function createProverNodeRpcServer(node: ProverNode): import("@aztec/foundation/json-rpc/server").SafeJsonRpcServer;
8
- //# sourceMappingURL=http.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,UAAU,iEAEzD"}
package/dest/http.js DELETED
@@ -1,9 +0,0 @@
1
- import { ProverNodeApiSchema } from '@aztec/stdlib/interfaces/server';
2
- import { createTracedJsonRpcServer } from '@aztec/telemetry-client';
3
- /**
4
- * Wrap a ProverNode instance with a JSON RPC HTTP server.
5
- * @param node - The ProverNode
6
- * @returns An JSON-RPC HTTP server
7
- */ export function createProverNodeRpcServer(node) {
8
- return createTracedJsonRpcServer(node, ProverNodeApiSchema);
9
- }
@@ -1,7 +0,0 @@
1
- import { type ConfigMappingsType } from '@aztec/foundation/config';
2
- export type ProverCoordinationConfig = {
3
- proverCoordinationNodeUrl: string | undefined;
4
- };
5
- export declare const proverCoordinationConfigMappings: ConfigMappingsType<ProverCoordinationConfig>;
6
- export declare function getTxProviderConfigFromEnv(): ProverCoordinationConfig;
7
- //# sourceMappingURL=config.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/prover-coordination/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,kBAAkB,EAAyB,MAAM,0BAA0B,CAAC;AAE1F,MAAM,MAAM,wBAAwB,GAAG;IACrC,yBAAyB,EAAE,MAAM,GAAG,SAAS,CAAC;CAC/C,CAAC;AAEF,eAAO,MAAM,gCAAgC,EAAE,kBAAkB,CAAC,wBAAwB,CAMzF,CAAC;AAEF,wBAAgB,0BAA0B,IAAI,wBAAwB,CAErE"}
@@ -1,11 +0,0 @@
1
- import { getConfigFromMappings } from '@aztec/foundation/config';
2
- export const proverCoordinationConfigMappings = {
3
- proverCoordinationNodeUrl: {
4
- env: 'PROVER_COORDINATION_NODE_URL',
5
- description: 'The URL of the tx provider node',
6
- parseEnv: (val)=>val
7
- }
8
- };
9
- export function getTxProviderConfigFromEnv() {
10
- return getConfigFromMappings(proverCoordinationConfigMappings);
11
- }