@aztec/validator-client 0.0.1-commit.9d2bcf6d → 0.0.1-commit.9ef841308

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 (51) hide show
  1. package/README.md +60 -18
  2. package/dest/checkpoint_builder.d.ts +21 -8
  3. package/dest/checkpoint_builder.d.ts.map +1 -1
  4. package/dest/checkpoint_builder.js +124 -46
  5. package/dest/config.d.ts +1 -1
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +26 -6
  8. package/dest/duties/validation_service.d.ts +2 -2
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +6 -12
  11. package/dest/factory.d.ts +7 -4
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +6 -5
  14. package/dest/index.d.ts +2 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -2
  17. package/dest/key_store/ha_key_store.js +1 -1
  18. package/dest/metrics.d.ts +10 -2
  19. package/dest/metrics.d.ts.map +1 -1
  20. package/dest/metrics.js +12 -0
  21. package/dest/proposal_handler.d.ts +94 -0
  22. package/dest/proposal_handler.d.ts.map +1 -0
  23. package/dest/{block_proposal_handler.js → proposal_handler.js} +377 -67
  24. package/dest/validator.d.ts +35 -21
  25. package/dest/validator.d.ts.map +1 -1
  26. package/dest/validator.js +177 -218
  27. package/package.json +19 -19
  28. package/src/checkpoint_builder.ts +142 -39
  29. package/src/config.ts +26 -6
  30. package/src/duties/validation_service.ts +12 -11
  31. package/src/factory.ts +9 -3
  32. package/src/index.ts +1 -2
  33. package/src/key_store/ha_key_store.ts +1 -1
  34. package/src/metrics.ts +19 -1
  35. package/src/proposal_handler.ts +907 -0
  36. package/src/validator.ts +240 -248
  37. package/dest/block_proposal_handler.d.ts +0 -63
  38. package/dest/block_proposal_handler.d.ts.map +0 -1
  39. package/dest/tx_validator/index.d.ts +0 -3
  40. package/dest/tx_validator/index.d.ts.map +0 -1
  41. package/dest/tx_validator/index.js +0 -2
  42. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  43. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  44. package/dest/tx_validator/nullifier_cache.js +0 -24
  45. package/dest/tx_validator/tx_validator_factory.d.ts +0 -19
  46. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  47. package/dest/tx_validator/tx_validator_factory.js +0 -54
  48. package/src/block_proposal_handler.ts +0 -555
  49. package/src/tx_validator/index.ts +0 -2
  50. package/src/tx_validator/nullifier_cache.ts +0 -30
  51. package/src/tx_validator/tx_validator_factory.ts +0 -154
@@ -0,0 +1,907 @@
1
+ import type { BlobClientInterface } from '@aztec/blob-client/client';
2
+ import { type Blob, encodeCheckpointBlobDataFromBlocks, getBlobsPerL1Block } from '@aztec/blob-lib';
3
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
4
+ import type { EpochCache } from '@aztec/epoch-cache';
5
+ import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
6
+ import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
7
+ import { pick } from '@aztec/foundation/collection';
8
+ import { Fr } from '@aztec/foundation/curves/bn254';
9
+ import { TimeoutError } from '@aztec/foundation/error';
10
+ import type { LogData } from '@aztec/foundation/log';
11
+ import { createLogger } from '@aztec/foundation/log';
12
+ import { retryUntil } from '@aztec/foundation/retry';
13
+ import { DateProvider, Timer } from '@aztec/foundation/timer';
14
+ import type { P2P, PeerId } from '@aztec/p2p';
15
+ import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
16
+ import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
17
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
18
+ import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
19
+ import { Gas } from '@aztec/stdlib/gas';
20
+ import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
21
+ import {
22
+ type L1ToL2MessageSource,
23
+ accumulateCheckpointOutHashes,
24
+ computeInHashFromL1ToL2Messages,
25
+ } from '@aztec/stdlib/messaging';
26
+ import type { BlockProposal, CheckpointProposalCore } from '@aztec/stdlib/p2p';
27
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
28
+ import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
29
+ import {
30
+ ReExFailedTxsError,
31
+ ReExInitialStateMismatchError,
32
+ ReExStateMismatchError,
33
+ ReExTimeoutError,
34
+ TransactionsNotAvailableError,
35
+ } from '@aztec/stdlib/validators';
36
+ import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
37
+
38
+ import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
39
+ import type { ValidatorMetrics } from './metrics.js';
40
+
41
+ export type BlockProposalValidationFailureReason =
42
+ | 'invalid_proposal'
43
+ | 'parent_block_not_found'
44
+ | 'block_source_not_synced'
45
+ | 'parent_block_wrong_slot'
46
+ | 'in_hash_mismatch'
47
+ | 'global_variables_mismatch'
48
+ | 'block_number_already_exists'
49
+ | 'txs_not_available'
50
+ | 'state_mismatch'
51
+ | 'failed_txs'
52
+ | 'initial_state_mismatch'
53
+ | 'timeout'
54
+ | 'unknown_error';
55
+
56
+ type ReexecuteTransactionsResult = {
57
+ block: L2Block;
58
+ failedTxs: FailedTx[];
59
+ reexecutionTimeMs: number;
60
+ totalManaUsed: number;
61
+ };
62
+
63
+ export type BlockProposalValidationSuccessResult = {
64
+ isValid: true;
65
+ blockNumber: BlockNumber;
66
+ reexecutionResult?: ReexecuteTransactionsResult;
67
+ };
68
+
69
+ export type BlockProposalValidationFailureResult = {
70
+ isValid: false;
71
+ reason: BlockProposalValidationFailureReason;
72
+ blockNumber?: BlockNumber;
73
+ reexecutionResult?: ReexecuteTransactionsResult;
74
+ };
75
+
76
+ export type BlockProposalValidationResult = BlockProposalValidationSuccessResult | BlockProposalValidationFailureResult;
77
+
78
+ export type CheckpointProposalValidationResult = { isValid: true } | { isValid: false; reason: string };
79
+
80
+ type CheckpointComputationResult =
81
+ | { checkpointNumber: CheckpointNumber; reason?: undefined }
82
+ | { checkpointNumber?: undefined; reason: 'invalid_proposal' | 'global_variables_mismatch' };
83
+
84
+ /** Handles block and checkpoint proposals for both validator and non-validator nodes. */
85
+ export class ProposalHandler {
86
+ public readonly tracer: Tracer;
87
+
88
+ constructor(
89
+ private checkpointsBuilder: FullNodeCheckpointsBuilder,
90
+ private worldState: WorldStateSynchronizer,
91
+ private blockSource: L2BlockSource & L2BlockSink,
92
+ private l1ToL2MessageSource: L1ToL2MessageSource,
93
+ private txProvider: ITxProvider,
94
+ private blockProposalValidator: BlockProposalValidator,
95
+ private epochCache: EpochCache,
96
+ private config: ValidatorClientFullConfig,
97
+ private blobClient: BlobClientInterface,
98
+ private metrics?: ValidatorMetrics,
99
+ private dateProvider: DateProvider = new DateProvider(),
100
+ telemetry: TelemetryClient = getTelemetryClient(),
101
+ private log = createLogger('validator:proposal-handler'),
102
+ ) {
103
+ if (config.fishermanMode) {
104
+ this.log = this.log.createChild('[FISHERMAN]');
105
+ }
106
+ this.tracer = telemetry.getTracer('ProposalHandler');
107
+ }
108
+
109
+ /**
110
+ * Registers non-validator handlers for block and checkpoint proposals on the p2p client.
111
+ * Block proposals are always registered. Checkpoint proposals are registered if the blob client can upload.
112
+ */
113
+ register(p2pClient: P2P, shouldReexecute: boolean): ProposalHandler {
114
+ // Non-validator handler that processes or re-executes for monitoring but does not attest.
115
+ // Returns boolean indicating whether the proposal was valid.
116
+ const blockHandler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
117
+ try {
118
+ const { slotNumber, blockNumber } = proposal;
119
+ const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
120
+ if (result.isValid) {
121
+ this.log.info(`Non-validator block proposal ${blockNumber} at slot ${slotNumber} handled`, {
122
+ blockNumber: result.blockNumber,
123
+ slotNumber,
124
+ reexecutionTimeMs: result.reexecutionResult?.reexecutionTimeMs,
125
+ totalManaUsed: result.reexecutionResult?.totalManaUsed,
126
+ numTxs: result.reexecutionResult?.block?.body?.txEffects?.length ?? 0,
127
+ reexecuted: shouldReexecute,
128
+ });
129
+ return true;
130
+ } else {
131
+ this.log.warn(
132
+ `Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`,
133
+ { blockNumber: result.blockNumber, slotNumber, reason: result.reason },
134
+ );
135
+ return false;
136
+ }
137
+ } catch (error) {
138
+ this.log.error('Error processing block proposal in non-validator handler', error);
139
+ return false;
140
+ }
141
+ };
142
+
143
+ p2pClient.registerBlockProposalHandler(blockHandler);
144
+
145
+ // Register checkpoint proposal handler if blob uploads are enabled and we are reexecuting
146
+ if (this.blobClient.canUpload() && shouldReexecute) {
147
+ const checkpointHandler = async (checkpoint: CheckpointProposalCore, _sender: PeerId) => {
148
+ try {
149
+ const proposalInfo = {
150
+ proposalSlotNumber: checkpoint.slotNumber,
151
+ archive: checkpoint.archive.toString(),
152
+ proposer: checkpoint.getSender()?.toString(),
153
+ };
154
+ const result = await this.handleCheckpointProposal(checkpoint, proposalInfo);
155
+ if (result.isValid) {
156
+ this.log.info(`Non-validator checkpoint proposal at slot ${checkpoint.slotNumber} handled`, proposalInfo);
157
+ } else {
158
+ this.log.warn(
159
+ `Non-validator checkpoint proposal at slot ${checkpoint.slotNumber} failed: ${result.reason}`,
160
+ proposalInfo,
161
+ );
162
+ }
163
+ } catch (error) {
164
+ this.log.error('Error processing checkpoint proposal in non-validator handler', error);
165
+ }
166
+ // Non-validators don't attest
167
+ return undefined;
168
+ };
169
+ p2pClient.registerCheckpointProposalHandler(checkpointHandler);
170
+ }
171
+
172
+ return this;
173
+ }
174
+
175
+ async handleBlockProposal(
176
+ proposal: BlockProposal,
177
+ proposalSender: PeerId,
178
+ shouldReexecute: boolean,
179
+ ): Promise<BlockProposalValidationResult> {
180
+ const slotNumber = proposal.slotNumber;
181
+ const proposer = proposal.getSender();
182
+ const config = this.checkpointsBuilder.getConfig();
183
+
184
+ // Reject proposals with invalid signatures
185
+ if (!proposer) {
186
+ this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
187
+ return { isValid: false, reason: 'invalid_proposal' };
188
+ }
189
+
190
+ const proposalInfo = {
191
+ ...proposal.toBlockInfo(),
192
+ proposer: proposer.toString(),
193
+ blockNumber: undefined as BlockNumber | undefined,
194
+ checkpointNumber: undefined as CheckpointNumber | undefined,
195
+ };
196
+
197
+ this.log.info(`Processing proposal for slot ${slotNumber}`, {
198
+ ...proposalInfo,
199
+ txHashes: proposal.txHashes.map(t => t.toString()),
200
+ });
201
+
202
+ // Check that the proposal is from the current proposer, or the next proposer
203
+ // This should have been handled by the p2p layer, but we double check here out of caution
204
+ const validationResult = await this.blockProposalValidator.validate(proposal);
205
+ if (validationResult.result !== 'accept') {
206
+ this.log.warn(`Proposal is not valid, skipping processing`, proposalInfo);
207
+ return { isValid: false, reason: 'invalid_proposal' };
208
+ }
209
+
210
+ // Ensure the block source is synced before checking for existing blocks,
211
+ // since a pending checkpoint prune may remove blocks we'd otherwise find.
212
+ // This affects mostly the block_number_already_exists check, since a pending
213
+ // checkpoint prune could remove a block that would conflict with this proposal.
214
+ // When pipelining is enabled, the proposer builds ahead of L1 submission, so the
215
+ // block source won't have synced to the proposed slot yet. Skip the sync wait to
216
+ // avoid eating into the attestation window.
217
+ if (!this.epochCache.isProposerPipeliningEnabled()) {
218
+ const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
219
+ if (!blockSourceSync) {
220
+ this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
221
+ return { isValid: false, reason: 'block_source_not_synced' };
222
+ }
223
+ }
224
+
225
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail.
226
+ // If we don't find it immediately, we keep retrying for a while; it may be we still
227
+ // need to process other block proposals to get to it.
228
+ const parentBlock = await this.getParentBlock(proposal);
229
+ if (parentBlock === undefined) {
230
+ this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
231
+ return { isValid: false, reason: 'parent_block_not_found' };
232
+ }
233
+
234
+ // Check that the parent block's slot is not greater than the proposal's slot.
235
+ if (parentBlock !== 'genesis' && parentBlock.header.getSlot() > slotNumber) {
236
+ this.log.warn(`Parent block slot is greater than proposal slot, skipping processing`, {
237
+ parentBlockSlot: parentBlock.header.getSlot().toString(),
238
+ proposalSlot: slotNumber.toString(),
239
+ ...proposalInfo,
240
+ });
241
+ return { isValid: false, reason: 'parent_block_wrong_slot' };
242
+ }
243
+
244
+ // Compute the block number based on the parent block
245
+ const blockNumber =
246
+ parentBlock === 'genesis'
247
+ ? BlockNumber(INITIAL_L2_BLOCK_NUM)
248
+ : BlockNumber(parentBlock.header.getBlockNumber() + 1);
249
+ proposalInfo.blockNumber = blockNumber;
250
+
251
+ // Check that this block number does not exist already
252
+ const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
253
+ if (existingBlock) {
254
+ this.log.warn(`Block number ${blockNumber} already exists, skipping processing`, proposalInfo);
255
+ return { isValid: false, blockNumber, reason: 'block_number_already_exists' };
256
+ }
257
+
258
+ // Collect txs from the proposal. We start doing this as early as possible,
259
+ // and we do it even if we don't plan to re-execute the txs, so that we have them if another node needs them.
260
+ const { txs, missingTxs } = await this.txProvider.getTxsForBlockProposal(proposal, blockNumber, {
261
+ pinnedPeer: proposalSender,
262
+ deadline: this.getReexecutionDeadline(slotNumber, config),
263
+ });
264
+
265
+ // If reexecution is disabled, bail. We were just interested in triggering tx collection.
266
+ if (!shouldReexecute) {
267
+ this.log.info(
268
+ `Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
269
+ proposalInfo,
270
+ );
271
+ return { isValid: true, blockNumber };
272
+ }
273
+
274
+ // Compute the checkpoint number for this block and validate checkpoint consistency
275
+ const checkpointResult = this.computeCheckpointNumber(proposal, parentBlock, proposalInfo);
276
+ if (checkpointResult.reason) {
277
+ return { isValid: false, blockNumber, reason: checkpointResult.reason };
278
+ }
279
+ const checkpointNumber = checkpointResult.checkpointNumber;
280
+ proposalInfo.checkpointNumber = checkpointNumber;
281
+
282
+ // Check that I have the same set of l1ToL2Messages as the proposal
283
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
284
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
285
+ const proposalInHash = proposal.inHash;
286
+ if (!computedInHash.equals(proposalInHash)) {
287
+ this.log.warn(`L1 to L2 messages in hash mismatch, skipping processing`, {
288
+ proposalInHash: proposalInHash.toString(),
289
+ computedInHash: computedInHash.toString(),
290
+ ...proposalInfo,
291
+ });
292
+ return { isValid: false, blockNumber, reason: 'in_hash_mismatch' };
293
+ }
294
+
295
+ // Check that all of the transactions in the proposal are available
296
+ if (missingTxs.length > 0) {
297
+ this.log.warn(`Missing ${missingTxs.length} txs to process proposal`, { ...proposalInfo, missingTxs });
298
+ return { isValid: false, blockNumber, reason: 'txs_not_available' };
299
+ }
300
+
301
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
302
+ const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
303
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
304
+ .filter(c => c.checkpointNumber < checkpointNumber)
305
+ .map(c => c.checkpointOutHash);
306
+
307
+ // Try re-executing the transactions in the proposal if needed
308
+ let reexecutionResult;
309
+ try {
310
+ this.log.verbose(`Re-executing transactions in the proposal`, proposalInfo);
311
+ reexecutionResult = await this.reexecuteTransactions(
312
+ proposal,
313
+ blockNumber,
314
+ checkpointNumber,
315
+ txs,
316
+ l1ToL2Messages,
317
+ previousCheckpointOutHashes,
318
+ );
319
+ } catch (error) {
320
+ this.log.error(`Error reexecuting txs while processing block proposal`, error, proposalInfo);
321
+ const reason = this.getReexecuteFailureReason(error);
322
+ return { isValid: false, blockNumber, reason, reexecutionResult };
323
+ }
324
+
325
+ // If we succeeded, push this block into the archiver (unless disabled)
326
+ if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
327
+ await this.blockSource.addBlock(reexecutionResult?.block);
328
+ }
329
+
330
+ this.log.info(
331
+ `Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
332
+ { ...proposalInfo, ...pick(reexecutionResult, 'reexecutionTimeMs', 'totalManaUsed') },
333
+ );
334
+
335
+ return { isValid: true, blockNumber, reexecutionResult };
336
+ }
337
+
338
+ private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
339
+ const parentArchive = proposal.blockHeader.lastArchive.root;
340
+ const slot = proposal.slotNumber;
341
+ const config = this.checkpointsBuilder.getConfig();
342
+ const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
343
+
344
+ if (parentArchive.equals(genesisArchiveRoot)) {
345
+ return 'genesis';
346
+ }
347
+
348
+ const deadline = this.getReexecutionDeadline(slot, config);
349
+ const currentTime = this.dateProvider.now();
350
+ const timeoutDurationMs = deadline.getTime() - currentTime;
351
+
352
+ try {
353
+ return (
354
+ (await this.blockSource.getBlockDataByArchive(parentArchive)) ??
355
+ (timeoutDurationMs <= 0
356
+ ? undefined
357
+ : await retryUntil(
358
+ () => this.blockSource.syncImmediate().then(() => this.blockSource.getBlockDataByArchive(parentArchive)),
359
+ 'force archiver sync',
360
+ timeoutDurationMs / 1000,
361
+ 0.5,
362
+ ))
363
+ );
364
+ } catch (err) {
365
+ if (err instanceof TimeoutError) {
366
+ this.log.debug(`Timed out getting parent block by archive root`, { parentArchive });
367
+ } else {
368
+ this.log.error('Error getting parent block by archive root', err, { parentArchive });
369
+ }
370
+ return undefined;
371
+ }
372
+ }
373
+
374
+ private computeCheckpointNumber(
375
+ proposal: BlockProposal,
376
+ parentBlock: 'genesis' | BlockData,
377
+ proposalInfo: object,
378
+ ): CheckpointComputationResult {
379
+ if (parentBlock === 'genesis') {
380
+ // First block is in checkpoint 1
381
+ if (proposal.indexWithinCheckpoint !== 0) {
382
+ this.log.warn(`First block proposal has non-zero indexWithinCheckpoint`, proposalInfo);
383
+ return { reason: 'invalid_proposal' };
384
+ }
385
+ return { checkpointNumber: CheckpointNumber.INITIAL };
386
+ }
387
+
388
+ if (proposal.indexWithinCheckpoint === 0) {
389
+ // If this is the first block in a new checkpoint, increment the checkpoint number
390
+ if (!(proposal.blockHeader.getSlot() > parentBlock.header.getSlot())) {
391
+ this.log.warn(`Slot should be greater than parent block slot for first block in checkpoint`, proposalInfo);
392
+ return { reason: 'invalid_proposal' };
393
+ }
394
+ return { checkpointNumber: CheckpointNumber(parentBlock.checkpointNumber + 1) };
395
+ }
396
+
397
+ // Otherwise it should follow the previous block in the same checkpoint
398
+ if (proposal.indexWithinCheckpoint !== parentBlock.indexWithinCheckpoint + 1) {
399
+ this.log.warn(`Non-sequential indexWithinCheckpoint`, proposalInfo);
400
+ return { reason: 'invalid_proposal' };
401
+ }
402
+ if (proposal.blockHeader.getSlot() !== parentBlock.header.getSlot()) {
403
+ this.log.warn(`Slot should be equal to parent block slot for non-first block in checkpoint`, proposalInfo);
404
+ return { reason: 'invalid_proposal' };
405
+ }
406
+
407
+ // For non-first blocks in a checkpoint, validate global variables match parent (except blockNumber)
408
+ const validationResult = this.validateNonFirstBlockInCheckpoint(proposal, parentBlock, proposalInfo);
409
+ if (validationResult) {
410
+ return validationResult;
411
+ }
412
+
413
+ return { checkpointNumber: parentBlock.checkpointNumber };
414
+ }
415
+
416
+ /**
417
+ * Validates that a non-first block in a checkpoint has consistent global variables with its parent.
418
+ * For blocks with indexWithinCheckpoint > 0, all global variables except blockNumber must match the parent.
419
+ * @returns A failure result if validation fails, undefined if validation passes
420
+ */
421
+ private validateNonFirstBlockInCheckpoint(
422
+ proposal: BlockProposal,
423
+ parentBlock: BlockData,
424
+ proposalInfo: object,
425
+ ): CheckpointComputationResult | undefined {
426
+ const proposalGlobals = proposal.blockHeader.globalVariables;
427
+ const parentGlobals = parentBlock.header.globalVariables;
428
+
429
+ // All global variables except blockNumber should match the parent
430
+ // blockNumber naturally increments between blocks
431
+ if (!proposalGlobals.chainId.equals(parentGlobals.chainId)) {
432
+ this.log.warn(`Non-first block in checkpoint has mismatched chainId`, {
433
+ ...proposalInfo,
434
+ proposalChainId: proposalGlobals.chainId.toString(),
435
+ parentChainId: parentGlobals.chainId.toString(),
436
+ });
437
+ return { reason: 'global_variables_mismatch' };
438
+ }
439
+
440
+ if (!proposalGlobals.version.equals(parentGlobals.version)) {
441
+ this.log.warn(`Non-first block in checkpoint has mismatched version`, {
442
+ ...proposalInfo,
443
+ proposalVersion: proposalGlobals.version.toString(),
444
+ parentVersion: parentGlobals.version.toString(),
445
+ });
446
+ return { reason: 'global_variables_mismatch' };
447
+ }
448
+
449
+ if (proposalGlobals.slotNumber !== parentGlobals.slotNumber) {
450
+ this.log.warn(`Non-first block in checkpoint has mismatched slotNumber`, {
451
+ ...proposalInfo,
452
+ proposalSlotNumber: proposalGlobals.slotNumber,
453
+ parentSlotNumber: parentGlobals.slotNumber,
454
+ });
455
+ return { reason: 'global_variables_mismatch' };
456
+ }
457
+
458
+ if (proposalGlobals.timestamp !== parentGlobals.timestamp) {
459
+ this.log.warn(`Non-first block in checkpoint has mismatched timestamp`, {
460
+ ...proposalInfo,
461
+ proposalTimestamp: proposalGlobals.timestamp.toString(),
462
+ parentTimestamp: parentGlobals.timestamp.toString(),
463
+ });
464
+ return { reason: 'global_variables_mismatch' };
465
+ }
466
+
467
+ if (!proposalGlobals.coinbase.equals(parentGlobals.coinbase)) {
468
+ this.log.warn(`Non-first block in checkpoint has mismatched coinbase`, {
469
+ ...proposalInfo,
470
+ proposalCoinbase: proposalGlobals.coinbase.toString(),
471
+ parentCoinbase: parentGlobals.coinbase.toString(),
472
+ });
473
+ return { reason: 'global_variables_mismatch' };
474
+ }
475
+
476
+ if (!proposalGlobals.feeRecipient.equals(parentGlobals.feeRecipient)) {
477
+ this.log.warn(`Non-first block in checkpoint has mismatched feeRecipient`, {
478
+ ...proposalInfo,
479
+ proposalFeeRecipient: proposalGlobals.feeRecipient.toString(),
480
+ parentFeeRecipient: parentGlobals.feeRecipient.toString(),
481
+ });
482
+ return { reason: 'global_variables_mismatch' };
483
+ }
484
+
485
+ if (!proposalGlobals.gasFees.equals(parentGlobals.gasFees)) {
486
+ this.log.warn(`Non-first block in checkpoint has mismatched gasFees`, {
487
+ ...proposalInfo,
488
+ proposalGasFees: proposalGlobals.gasFees.toInspect(),
489
+ parentGasFees: parentGlobals.gasFees.toInspect(),
490
+ });
491
+ return { reason: 'global_variables_mismatch' };
492
+ }
493
+
494
+ return undefined;
495
+ }
496
+
497
+ private getReexecutionDeadline(slot: SlotNumber, config: { l1GenesisTime: bigint; slotDuration: number }): Date {
498
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
499
+ return new Date(nextSlotTimestampSeconds * 1000);
500
+ }
501
+
502
+ /** Waits for the block source to sync L1 data up to at least the slot before the given one. */
503
+ private async waitForBlockSourceSync(slot: SlotNumber): Promise<boolean> {
504
+ const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
505
+ const timeoutMs = deadline.getTime() - this.dateProvider.now();
506
+ if (slot === 0) {
507
+ return true;
508
+ }
509
+
510
+ // Make a quick check before triggering an archiver sync
511
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
512
+ if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
513
+ return true;
514
+ }
515
+
516
+ try {
517
+ // Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
518
+ return await retryUntil(
519
+ async () => {
520
+ await this.blockSource.syncImmediate();
521
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
522
+ return syncedSlot !== undefined && syncedSlot + 1 >= slot;
523
+ },
524
+ 'wait for block source sync',
525
+ timeoutMs / 1000,
526
+ 0.5,
527
+ );
528
+ } catch (err) {
529
+ if (err instanceof TimeoutError) {
530
+ this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
531
+ return false;
532
+ } else {
533
+ throw err;
534
+ }
535
+ }
536
+ }
537
+
538
+ private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
539
+ if (err instanceof TransactionsNotAvailableError) {
540
+ return 'txs_not_available';
541
+ } else if (err instanceof ReExInitialStateMismatchError) {
542
+ return 'initial_state_mismatch';
543
+ } else if (err instanceof ReExStateMismatchError) {
544
+ return 'state_mismatch';
545
+ } else if (err instanceof ReExFailedTxsError) {
546
+ return 'failed_txs';
547
+ } else if (err instanceof ReExTimeoutError) {
548
+ return 'timeout';
549
+ } else {
550
+ return 'unknown_error';
551
+ }
552
+ }
553
+
554
+ async reexecuteTransactions(
555
+ proposal: BlockProposal,
556
+ blockNumber: BlockNumber,
557
+ checkpointNumber: CheckpointNumber,
558
+ txs: Tx[],
559
+ l1ToL2Messages: Fr[],
560
+ previousCheckpointOutHashes: Fr[],
561
+ ): Promise<ReexecuteTransactionsResult> {
562
+ const { blockHeader, txHashes } = proposal;
563
+
564
+ // If we do not have all of the transactions, then we should fail
565
+ if (txs.length !== txHashes.length) {
566
+ const foundTxHashes = txs.map(tx => tx.getTxHash());
567
+ const missingTxHashes = txHashes.filter(txHash => !foundTxHashes.includes(txHash));
568
+ throw new TransactionsNotAvailableError(missingTxHashes);
569
+ }
570
+
571
+ const timer = new Timer();
572
+ const slot = proposal.slotNumber;
573
+ const config = this.checkpointsBuilder.getConfig();
574
+
575
+ // Get prior blocks in this checkpoint (same slot before current block)
576
+ const allBlocksInSlot = await this.blockSource.getBlocksForSlot(slot);
577
+ const priorBlocks = allBlocksInSlot.filter(b => b.number < blockNumber && b.header.getSlot() === slot);
578
+
579
+ // Fork before the block to be built
580
+ const parentBlockNumber = BlockNumber(blockNumber - 1);
581
+ await this.worldState.syncImmediate(parentBlockNumber);
582
+ await using fork = await this.worldState.fork(parentBlockNumber);
583
+
584
+ // Verify the fork's archive root matches the proposal's expected last archive.
585
+ // If they don't match, our world state synced to a different chain and reexecution would fail.
586
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
587
+ if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
588
+ throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
589
+ }
590
+
591
+ // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
592
+ const constants: CheckpointGlobalVariables = {
593
+ chainId: new Fr(config.l1ChainId),
594
+ version: new Fr(config.rollupVersion),
595
+ slotNumber: slot,
596
+ timestamp: blockHeader.globalVariables.timestamp,
597
+ coinbase: blockHeader.globalVariables.coinbase,
598
+ feeRecipient: blockHeader.globalVariables.feeRecipient,
599
+ gasFees: blockHeader.globalVariables.gasFees,
600
+ };
601
+
602
+ // Create checkpoint builder with prior blocks
603
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
604
+ checkpointNumber,
605
+ constants,
606
+ 0n, // only takes effect in the following checkpoint.
607
+ l1ToL2Messages,
608
+ previousCheckpointOutHashes,
609
+ fork,
610
+ priorBlocks,
611
+ this.log.getBindings(),
612
+ );
613
+
614
+ // Build the new block
615
+ const deadline = this.getReexecutionDeadline(slot, config);
616
+ const maxBlockGas =
617
+ this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined
618
+ ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
619
+ : undefined;
620
+ const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
621
+ isBuildingProposal: false,
622
+ minValidTxs: 0,
623
+ deadline,
624
+ expectedEndState: blockHeader.state,
625
+ maxTransactions: this.config.validateMaxTxsPerBlock,
626
+ maxBlockGas,
627
+ });
628
+
629
+ const { block, failedTxs } = result;
630
+ const numFailedTxs = failedTxs.length;
631
+
632
+ this.log.verbose(`Block proposal ${blockNumber} at slot ${slot} transaction re-execution complete`, {
633
+ numFailedTxs,
634
+ numProposalTxs: txHashes.length,
635
+ numProcessedTxs: block.body.txEffects.length,
636
+ blockNumber,
637
+ slot,
638
+ });
639
+
640
+ if (numFailedTxs > 0) {
641
+ this.metrics?.recordFailedReexecution(proposal);
642
+ throw new ReExFailedTxsError(numFailedTxs);
643
+ }
644
+
645
+ if (block.body.txEffects.length !== txHashes.length) {
646
+ this.metrics?.recordFailedReexecution(proposal);
647
+ throw new ReExTimeoutError();
648
+ }
649
+
650
+ // Throw a ReExStateMismatchError error if state updates do not match
651
+ // Compare the full block structure (archive and header) from the built block with the proposal
652
+ const archiveMatches = proposal.archive.equals(block.archive.root);
653
+ const headerMatches = proposal.blockHeader.equals(block.header);
654
+ if (!archiveMatches || !headerMatches) {
655
+ this.log.warn(`Re-execution state mismatch for slot ${slot}`, {
656
+ expectedArchive: block.archive.root.toString(),
657
+ actualArchive: proposal.archive.toString(),
658
+ expectedHeader: block.header.toInspect(),
659
+ actualHeader: proposal.blockHeader.toInspect(),
660
+ });
661
+ this.metrics?.recordFailedReexecution(proposal);
662
+ throw new ReExStateMismatchError(proposal.archive, block.archive.root);
663
+ }
664
+
665
+ const reexecutionTimeMs = timer.ms();
666
+ const totalManaUsed = block.header.totalManaUsed.toNumber() / 1e6;
667
+
668
+ this.metrics?.recordReex(reexecutionTimeMs, txs.length, totalManaUsed);
669
+
670
+ return {
671
+ block,
672
+ failedTxs,
673
+ reexecutionTimeMs,
674
+ totalManaUsed,
675
+ };
676
+ }
677
+
678
+ /**
679
+ * Validates a checkpoint proposal and uploads blobs if configured.
680
+ * Used by both non-validator nodes (via register) and the validator client (via delegation).
681
+ */
682
+ async handleCheckpointProposal(
683
+ proposal: CheckpointProposalCore,
684
+ proposalInfo: LogData,
685
+ ): Promise<CheckpointProposalValidationResult> {
686
+ const proposer = proposal.getSender();
687
+ if (!proposer) {
688
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${proposal.slotNumber}`);
689
+ return { isValid: false, reason: 'invalid_signature' };
690
+ }
691
+
692
+ if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
693
+ this.log.warn(
694
+ `Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposal.slotNumber}`,
695
+ );
696
+ return { isValid: false, reason: 'invalid_fee_asset_price_modifier' };
697
+ }
698
+
699
+ const result = await this.validateCheckpointProposal(proposal, proposalInfo);
700
+
701
+ // Upload blobs to filestore if validation passed (fire and forget)
702
+ if (result.isValid) {
703
+ this.tryUploadBlobsForCheckpoint(proposal, proposalInfo);
704
+ }
705
+
706
+ return result;
707
+ }
708
+
709
+ /**
710
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
711
+ * @returns Validation result with isValid flag and reason if invalid.
712
+ */
713
+ async validateCheckpointProposal(
714
+ proposal: CheckpointProposalCore,
715
+ proposalInfo: LogData,
716
+ ): Promise<CheckpointProposalValidationResult> {
717
+ const slot = proposal.slotNumber;
718
+
719
+ // Timeout block syncing at the start of the next slot
720
+ const config = this.checkpointsBuilder.getConfig();
721
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
722
+ const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
723
+
724
+ // Wait for last block to sync by archive
725
+ let lastBlockHeader;
726
+ try {
727
+ lastBlockHeader = await retryUntil(
728
+ async () => {
729
+ await this.blockSource.syncImmediate();
730
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
731
+ },
732
+ `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
733
+ timeoutSeconds,
734
+ 0.5,
735
+ );
736
+ } catch (err) {
737
+ if (err instanceof TimeoutError) {
738
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
739
+ return { isValid: false, reason: 'last_block_not_found' };
740
+ }
741
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
742
+ return { isValid: false, reason: 'block_fetch_error' };
743
+ }
744
+
745
+ if (!lastBlockHeader) {
746
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
747
+ return { isValid: false, reason: 'last_block_not_found' };
748
+ }
749
+
750
+ // Get all full blocks for the slot and checkpoint
751
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
752
+ if (blocks.length === 0) {
753
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
754
+ return { isValid: false, reason: 'no_blocks_for_slot' };
755
+ }
756
+
757
+ // Ensure the last block for this slot matches the archive in the checkpoint proposal
758
+ if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
759
+ this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
760
+ return { isValid: false, reason: 'last_block_archive_mismatch' };
761
+ }
762
+
763
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
764
+ ...proposalInfo,
765
+ blockNumbers: blocks.map(b => b.number),
766
+ });
767
+
768
+ // Get checkpoint constants from first block
769
+ const firstBlock = blocks[0];
770
+ const constants = this.extractCheckpointConstants(firstBlock);
771
+ const checkpointNumber = firstBlock.checkpointNumber;
772
+
773
+ // Get L1-to-L2 messages for this checkpoint
774
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
775
+
776
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
777
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
778
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
779
+ .filter(c => c.checkpointNumber < checkpointNumber)
780
+ .map(c => c.checkpointOutHash);
781
+
782
+ // Fork world state at the block before the first block
783
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
784
+ const fork = await this.worldState.fork(parentBlockNumber);
785
+
786
+ try {
787
+ // Create checkpoint builder with all existing blocks
788
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
789
+ checkpointNumber,
790
+ constants,
791
+ proposal.feeAssetPriceModifier,
792
+ l1ToL2Messages,
793
+ previousCheckpointOutHashes,
794
+ fork,
795
+ blocks,
796
+ this.log.getBindings(),
797
+ );
798
+
799
+ // Complete the checkpoint to get computed values
800
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
801
+
802
+ // Compare checkpoint header with proposal
803
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
804
+ this.log.warn(`Checkpoint header mismatch`, {
805
+ ...proposalInfo,
806
+ computed: computedCheckpoint.header.toInspect(),
807
+ proposal: proposal.checkpointHeader.toInspect(),
808
+ });
809
+ return { isValid: false, reason: 'checkpoint_header_mismatch' };
810
+ }
811
+
812
+ // Compare archive root with proposal
813
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
814
+ this.log.warn(`Archive root mismatch`, {
815
+ ...proposalInfo,
816
+ computed: computedCheckpoint.archive.root.toString(),
817
+ proposal: proposal.archive.toString(),
818
+ });
819
+ return { isValid: false, reason: 'archive_mismatch' };
820
+ }
821
+
822
+ // Check that the accumulated epoch out hash matches the value in the proposal.
823
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
824
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
825
+ const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
826
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
827
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
828
+ this.log.warn(`Epoch out hash mismatch`, {
829
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
830
+ computedEpochOutHash: computedEpochOutHash.toString(),
831
+ checkpointOutHash: checkpointOutHash.toString(),
832
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
833
+ ...proposalInfo,
834
+ });
835
+ return { isValid: false, reason: 'out_hash_mismatch' };
836
+ }
837
+
838
+ // Final round of validations on the checkpoint, just in case.
839
+ try {
840
+ validateCheckpoint(computedCheckpoint, {
841
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
842
+ maxDABlockGas: this.config.validateMaxDABlockGas,
843
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
844
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
845
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint,
846
+ });
847
+ } catch (err) {
848
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
849
+ return { isValid: false, reason: 'checkpoint_validation_failed' };
850
+ }
851
+
852
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
853
+ return { isValid: true };
854
+ } finally {
855
+ await fork.close();
856
+ }
857
+ }
858
+
859
+ /** Extracts checkpoint global variables from a block. */
860
+ private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
861
+ const gv = block.header.globalVariables;
862
+ return {
863
+ chainId: gv.chainId,
864
+ version: gv.version,
865
+ slotNumber: gv.slotNumber,
866
+ timestamp: gv.timestamp,
867
+ coinbase: gv.coinbase,
868
+ feeRecipient: gv.feeRecipient,
869
+ gasFees: gv.gasFees,
870
+ };
871
+ }
872
+
873
+ /** Triggers blob upload for a checkpoint if the blob client can upload (fire and forget). */
874
+ protected tryUploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): void {
875
+ if (this.blobClient.canUpload()) {
876
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
877
+ }
878
+ }
879
+
880
+ /** Uploads blobs for a checkpoint to the filestore. */
881
+ protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
882
+ try {
883
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
884
+ if (!lastBlockHeader) {
885
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
886
+ return;
887
+ }
888
+
889
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
890
+ if (blocks.length === 0) {
891
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
892
+ return;
893
+ }
894
+
895
+ const blockBlobData = blocks.map(b => b.toBlockBlobData());
896
+ const blobFields = encodeCheckpointBlobDataFromBlocks(blockBlobData);
897
+ const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
898
+ await this.blobClient.sendBlobsToFilestore(blobs);
899
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
900
+ ...proposalInfo,
901
+ numBlobs: blobs.length,
902
+ });
903
+ } catch (err) {
904
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
905
+ }
906
+ }
907
+ }