-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathprover-node.ts
More file actions
410 lines (362 loc) · 14.9 KB
/
prover-node.ts
File metadata and controls
410 lines (362 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import {
type EpochProofClaim,
type EpochProofQuote,
EpochProofQuotePayload,
type EpochProverManager,
type L1ToL2MessageSource,
type L2Block,
type L2BlockSource,
type P2PClientType,
type ProverCoordination,
type ProverNodeApi,
type Service,
type Tx,
type TxHash,
type WorldStateSynchronizer,
getTimestampRangeForEpoch,
tryStop,
} from '@aztec/circuit-types';
import { type ContractDataSource } from '@aztec/circuits.js';
import { compact } from '@aztec/foundation/collection';
import { memoize } from '@aztec/foundation/decorators';
import { createLogger } from '@aztec/foundation/log';
import { RunningPromise } from '@aztec/foundation/running-promise';
import { DateProvider } from '@aztec/foundation/timer';
import { type Maybe } from '@aztec/foundation/types';
import { type P2P } from '@aztec/p2p';
import { PublicProcessorFactory } from '@aztec/simulator/server';
import {
Attributes,
type TelemetryClient,
type Traceable,
type Tracer,
getTelemetryClient,
trackSpan,
} from '@aztec/telemetry-client';
import { type BondManager } from './bond/bond-manager.js';
import { EpochProvingJob, type EpochProvingJobState } from './job/epoch-proving-job.js';
import { ProverNodeMetrics } from './metrics.js';
import { type ClaimsMonitor, type ClaimsMonitorHandler } from './monitors/claims-monitor.js';
import { type EpochMonitor, type EpochMonitorHandler } from './monitors/epoch-monitor.js';
import { type ProverNodePublisher } from './prover-node-publisher.js';
import { type QuoteProvider } from './quote-provider/index.js';
import { type QuoteSigner } from './quote-signer.js';
export type ProverNodeOptions = {
pollingIntervalMs: number;
maxPendingJobs: number;
maxParallelBlocksPerEpoch: number;
txGatheringTimeoutMs: number;
txGatheringIntervalMs: number;
txGatheringMaxParallelRequests: number;
};
/**
* An Aztec Prover Node is a standalone process that monitors the unfinalised chain on L1 for unproven blocks,
* submits bids for proving them, and monitors if they are accepted. If so, the prover node fetches the txs
* from a tx source in the p2p network or an external node, re-executes their public functions, creates a rollup
* proof for the epoch, and submits it to L1.
*/
export class ProverNode implements ClaimsMonitorHandler, EpochMonitorHandler, ProverNodeApi, Traceable {
private log = createLogger('prover-node');
private dateProvider = new DateProvider();
private latestEpochWeAreProving: bigint | undefined;
private jobs: Map<string, EpochProvingJob> = new Map();
private cachedEpochData: { epochNumber: bigint; blocks: L2Block[]; txs: Tx[] } | undefined = undefined;
private options: ProverNodeOptions;
private metrics: ProverNodeMetrics;
private txFetcher: RunningPromise;
private lastBlockNumber: number | undefined;
public readonly tracer: Tracer;
constructor(
protected readonly prover: EpochProverManager,
protected readonly publisher: ProverNodePublisher,
protected readonly l2BlockSource: L2BlockSource & Maybe<Service>,
protected readonly l1ToL2MessageSource: L1ToL2MessageSource,
protected readonly contractDataSource: ContractDataSource,
protected readonly worldState: WorldStateSynchronizer,
protected readonly coordination: ProverCoordination & Maybe<Service>,
protected readonly quoteProvider: QuoteProvider,
protected readonly quoteSigner: QuoteSigner,
protected readonly claimsMonitor: ClaimsMonitor,
protected readonly epochsMonitor: EpochMonitor,
protected readonly bondManager: BondManager,
options: Partial<ProverNodeOptions> = {},
protected readonly telemetryClient: TelemetryClient = getTelemetryClient(),
) {
this.options = {
pollingIntervalMs: 1_000,
maxPendingJobs: 100,
maxParallelBlocksPerEpoch: 32,
txGatheringTimeoutMs: 60_000,
txGatheringIntervalMs: 1_000,
txGatheringMaxParallelRequests: 100,
...compact(options),
};
this.metrics = new ProverNodeMetrics(telemetryClient, 'ProverNode');
this.tracer = telemetryClient.getTracer('ProverNode');
this.txFetcher = new RunningPromise(() => this.checkForTxs(), this.log, this.options.txGatheringIntervalMs);
}
public getP2P() {
const asP2PClient = this.coordination as P2P<P2PClientType.Prover>;
if (typeof asP2PClient.isP2PClient === 'function' && asP2PClient.isP2PClient()) {
return asP2PClient;
}
return undefined;
}
async handleClaim(proofClaim: EpochProofClaim): Promise<void> {
if (proofClaim.epochToProve === this.latestEpochWeAreProving) {
this.log.verbose(`Already proving claim for epoch ${proofClaim.epochToProve}`);
return;
}
const provenEpoch = await this.l2BlockSource.getProvenL2EpochNumber();
if (provenEpoch !== undefined && proofClaim.epochToProve <= provenEpoch) {
this.log.verbose(`Claim for epoch ${proofClaim.epochToProve} is already proven`);
return;
}
try {
await this.startProof(proofClaim.epochToProve);
this.latestEpochWeAreProving = proofClaim.epochToProve;
} catch (err) {
this.log.error(`Error handling claim for epoch ${proofClaim.epochToProve}`, err);
}
try {
// Staked amounts are lowered after a claim, so this is a good time for doing a top-up if needed
await this.bondManager.ensureBond();
} catch (err) {
this.log.error(`Error ensuring prover bond after handling claim for epoch ${proofClaim.epochToProve}`, err);
}
}
/**
* Handles the epoch number to prove when the prover node starts by checking if there
* is an existing claim for it. If not, it creates and sends a quote for it.
* @param epochNumber - The epoch immediately before the current one when the prover node starts.
*/
async handleInitialEpochSync(epochNumber: bigint): Promise<void> {
try {
const claim = await this.publisher.getProofClaim();
if (!claim || claim.epochToProve < epochNumber) {
this.log.verbose(`Handling epoch ${epochNumber} completed as initial sync`);
await this.handleEpochCompleted(epochNumber);
}
} catch (err) {
this.log.error(`Error handling initial epoch sync`, err);
}
}
/**
* Handles an epoch being completed by sending a quote for proving it.
* @param epochNumber - The epoch number that was just completed.
*/
async handleEpochCompleted(epochNumber: bigint): Promise<void> {
try {
// Gather data for the epoch
const epochData = await this.gatherEpochData(epochNumber);
const { blocks } = epochData;
this.cachedEpochData = { epochNumber, ...epochData };
// Construct a quote for the epoch
const partialQuote = await this.quoteProvider.getQuote(Number(epochNumber), blocks);
if (!partialQuote) {
this.log.info(`No quote produced for epoch ${epochNumber}`);
return;
}
// Ensure we have deposited enough funds for sending this quote
await this.bondManager.ensureBond(partialQuote.bondAmount);
// Assemble and sign full quote
const quote = EpochProofQuotePayload.from({
...partialQuote,
epochToProve: BigInt(epochNumber),
prover: this.publisher.getSenderAddress(),
validUntilSlot: partialQuote.validUntilSlot ?? BigInt(Number.MAX_SAFE_INTEGER), // Should we constrain this?
});
const signed = await this.quoteSigner.sign(quote);
// Send it to the coordinator
this.log.info(
`Sending quote for epoch ${epochNumber} with blocks ${blocks[0].number} to ${blocks.at(-1)!.number}`,
quote.toViemArgs(),
);
await this.doSendEpochProofQuote(signed);
} catch (err) {
if (err instanceof EmptyEpochError) {
this.log.info(`Not producing quote for ${epochNumber} since no blocks were found`);
} else {
this.log.error(`Error handling epoch completed`, err);
}
}
}
/**
* Starts the prover node so it periodically checks for unproven epochs in the unfinalised chain from L1 and sends
* quotes for them, as well as monitors the claims for the epochs it has sent quotes for and starts proving jobs.
* This method returns once the prover node has deposited an initial bond into the escrow contract.
*/
async start() {
this.txFetcher.start();
await this.bondManager.ensureBond();
this.epochsMonitor.start(this);
this.claimsMonitor.start(this);
this.log.info('Started ProverNode', this.options);
}
/**
* Stops the prover node and all its dependencies.
*/
async stop() {
this.log.info('Stopping ProverNode');
await this.txFetcher.stop();
await this.epochsMonitor.stop();
await this.claimsMonitor.stop();
await this.prover.stop();
await tryStop(this.l2BlockSource);
this.publisher.interrupt();
await Promise.all(Array.from(this.jobs.values()).map(job => job.stop()));
await this.worldState.stop();
await tryStop(this.coordination);
await this.telemetryClient.stop();
this.log.info('Stopped ProverNode');
}
/** Sends an epoch proof quote to the coordinator. */
public sendEpochProofQuote(quote: EpochProofQuote): Promise<void> {
this.log.info(`Sending quote for epoch`, quote.toViemArgs().quote);
return this.doSendEpochProofQuote(quote);
}
private doSendEpochProofQuote(quote: EpochProofQuote) {
return this.coordination.addEpochProofQuote(quote);
}
/**
* Creates a proof for a block range. Returns once the proof has been submitted to L1.
*/
public async prove(epochNumber: number | bigint) {
const job = await this.createProvingJob(BigInt(epochNumber));
return job.run();
}
/**
* Starts a proving process and returns immediately.
*/
public async startProof(epochNumber: number | bigint) {
const job = await this.createProvingJob(BigInt(epochNumber));
void job.run().catch(err => this.log.error(`Error proving epoch ${epochNumber}`, err));
}
/**
* Returns the prover instance.
*/
public getProver() {
return this.prover;
}
/**
* Returns an array of jobs being processed.
*/
public getJobs(): Promise<{ uuid: string; status: EpochProvingJobState }[]> {
return Promise.resolve(Array.from(this.jobs.entries()).map(([uuid, job]) => ({ uuid, status: job.getState() })));
}
private checkMaximumPendingJobs() {
const { maxPendingJobs } = this.options;
return maxPendingJobs === 0 || this.jobs.size < maxPendingJobs;
}
@trackSpan('ProverNode.createProvingJob', epochNumber => ({ [Attributes.EPOCH_NUMBER]: Number(epochNumber) }))
private async createProvingJob(epochNumber: bigint) {
if (!this.checkMaximumPendingJobs()) {
throw new Error(`Maximum pending proving jobs ${this.options.maxPendingJobs} reached. Cannot create new job.`);
}
// Gather blocks for this epoch
const cachedEpochData = this.cachedEpochData?.epochNumber === epochNumber ? this.cachedEpochData : undefined;
const { blocks, txs } = cachedEpochData ?? (await this.gatherEpochData(epochNumber));
const fromBlock = blocks[0].number;
const toBlock = blocks.at(-1)!.number;
// Fast forward world state to right before the target block and get a fork
this.log.verbose(`Creating proving job for epoch ${epochNumber} for block range ${fromBlock} to ${toBlock}`);
await this.worldState.syncImmediate(fromBlock - 1);
// Create a processor using the forked world state
const publicProcessorFactory = new PublicProcessorFactory(
this.contractDataSource,
this.dateProvider,
this.telemetryClient,
);
const cleanUp = () => {
this.jobs.delete(job.getId());
return Promise.resolve();
};
const [_, endTimestamp] = getTimestampRangeForEpoch(epochNumber + 1n, await this.getL1Constants());
const deadline = new Date(Number(endTimestamp) * 1000);
const job = this.doCreateEpochProvingJob(epochNumber, deadline, blocks, txs, publicProcessorFactory, cleanUp);
this.jobs.set(job.getId(), job);
return job;
}
@memoize
private getL1Constants() {
return this.l2BlockSource.getL1Constants();
}
/** Monitors for new blocks and requests their txs from the p2p layer to ensure they are available for proving. */
@trackSpan('ProverNode.checkForTxs')
private async checkForTxs() {
const blockNumber = await this.l2BlockSource.getBlockNumber();
if (this.lastBlockNumber === undefined || blockNumber > this.lastBlockNumber) {
const block = await this.l2BlockSource.getBlock(blockNumber);
if (!block) {
return;
}
const txHashes = block.body.txEffects.map(tx => tx.txHash);
this.log.verbose(`Fetching ${txHashes.length} for block number ${blockNumber} from coordination`);
await this.coordination.getTxsByHash(txHashes); // This stores the txs in the tx pool, no need to persist them here
this.lastBlockNumber = blockNumber;
}
}
@trackSpan('ProverNode.gatherEpochData', epochNumber => ({ [Attributes.EPOCH_NUMBER]: Number(epochNumber) }))
private async gatherEpochData(epochNumber: bigint) {
// Gather blocks for this epoch and their txs
const blocks = await this.gatherBlocks(epochNumber);
const txs = await this.gatherTxs(epochNumber, blocks);
return { blocks, txs };
}
private async gatherBlocks(epochNumber: bigint) {
const blocks = await this.l2BlockSource.getBlocksForEpoch(epochNumber);
if (blocks.length === 0) {
throw new EmptyEpochError(epochNumber);
}
return blocks;
}
private async gatherTxs(epochNumber: bigint, blocks: L2Block[]) {
const txsToFind: TxHash[] = blocks.flatMap(block => block.body.txEffects.map(tx => tx.txHash));
const txs = await this.coordination.getTxsByHash(txsToFind);
if (txs.length === txsToFind.length) {
this.log.verbose(`Gathered all ${txs.length} txs for epoch ${epochNumber}`, { epochNumber });
return txs;
}
const txHashesFound = await Promise.all(txs.map(tx => tx.getTxHash()));
const missingTxHashes = txsToFind
.filter(txHashToFind => !txHashesFound.some(txHashFound => txHashToFind.equals(txHashFound)))
.join(', ');
throw new Error(`Txs not found for epoch ${epochNumber}: ${missingTxHashes}`);
}
/** Extracted for testing purposes. */
protected doCreateEpochProvingJob(
epochNumber: bigint,
deadline: Date | undefined,
blocks: L2Block[],
txs: Tx[],
publicProcessorFactory: PublicProcessorFactory,
cleanUp: () => Promise<void>,
) {
return new EpochProvingJob(
this.worldState,
epochNumber,
blocks,
txs,
this.prover.createEpochProver(),
publicProcessorFactory,
this.publisher,
this.l2BlockSource,
this.l1ToL2MessageSource,
this.metrics,
deadline,
{ parallelBlockLimit: this.options.maxParallelBlocksPerEpoch },
cleanUp,
);
}
/** Extracted for testing purposes. */
protected async triggerMonitors() {
await this.epochsMonitor.work();
await this.claimsMonitor.work();
}
}
class EmptyEpochError extends Error {
constructor(epochNumber: bigint) {
super(`No blocks found for epoch ${epochNumber}`);
this.name = 'EmptyEpochError';
}
}