@aztec/validator-client 4.0.0-nightly.20260112 → 4.0.0-nightly.20260114

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 (42) hide show
  1. package/README.md +256 -0
  2. package/dest/block_proposal_handler.d.ts +22 -10
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +347 -76
  5. package/dest/checkpoint_builder.d.ts +70 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +156 -0
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +10 -5
  11. package/dest/duties/validation_service.d.ts +26 -10
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +51 -21
  14. package/dest/factory.d.ts +10 -7
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +2 -2
  17. package/dest/index.d.ts +3 -1
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +2 -0
  20. package/dest/tx_validator/index.d.ts +3 -0
  21. package/dest/tx_validator/index.d.ts.map +1 -0
  22. package/dest/tx_validator/index.js +2 -0
  23. package/dest/tx_validator/nullifier_cache.d.ts +14 -0
  24. package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
  25. package/dest/tx_validator/nullifier_cache.js +24 -0
  26. package/dest/tx_validator/tx_validator_factory.d.ts +18 -0
  27. package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
  28. package/dest/tx_validator/tx_validator_factory.js +53 -0
  29. package/dest/validator.d.ts +39 -15
  30. package/dest/validator.d.ts.map +1 -1
  31. package/dest/validator.js +318 -449
  32. package/package.json +16 -12
  33. package/src/block_proposal_handler.ts +273 -43
  34. package/src/checkpoint_builder.ts +276 -0
  35. package/src/config.ts +10 -5
  36. package/src/duties/validation_service.ts +79 -25
  37. package/src/factory.ts +14 -8
  38. package/src/index.ts +2 -0
  39. package/src/tx_validator/index.ts +2 -0
  40. package/src/tx_validator/nullifier_cache.ts +30 -0
  41. package/src/tx_validator/tx_validator_factory.ts +133 -0
  42. package/src/validator.ts +424 -94
package/src/validator.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
- import { getBlobsPerL1Block } from '@aztec/blob-lib';
2
+ import { type Blob, getBlobsPerL1Block } from '@aztec/blob-lib';
3
3
  import type { EpochCache } from '@aztec/epoch-cache';
4
- import { BlockNumber, EpochNumber } from '@aztec/foundation/branded-types';
4
+ import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
5
5
  import { Fr } from '@aztec/foundation/curves/bn254';
6
+ import { TimeoutError } from '@aztec/foundation/error';
6
7
  import type { EthAddress } from '@aztec/foundation/eth-address';
7
8
  import type { Signature } from '@aztec/foundation/eth-signature';
8
- import { type Logger, createLogger } from '@aztec/foundation/log';
9
+ import { type LogData, type Logger, createLogger } from '@aztec/foundation/log';
10
+ import { retryUntil } from '@aztec/foundation/retry';
9
11
  import { RunningPromise } from '@aztec/foundation/running-promise';
10
12
  import { sleep } from '@aztec/foundation/sleep';
11
13
  import { DateProvider } from '@aztec/foundation/timer';
@@ -14,19 +16,33 @@ import type { P2P, PeerId, TxProvider } from '@aztec/p2p';
14
16
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
15
17
  import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
16
18
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
17
- import type { CommitteeAttestationsAndSigners, L2BlockSource } from '@aztec/stdlib/block';
18
- import type { IFullNodeBlockBuilder, Validator, ValidatorClientFullConfig } from '@aztec/stdlib/interfaces/server';
19
+ import type { CommitteeAttestationsAndSigners, L2BlockNew, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
20
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
21
+ import type {
22
+ CreateCheckpointProposalLastBlockData,
23
+ Validator,
24
+ ValidatorClientFullConfig,
25
+ WorldStateSynchronizer,
26
+ } from '@aztec/stdlib/interfaces/server';
19
27
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
20
- import type { BlockAttestation, BlockProposal, BlockProposalOptions } from '@aztec/stdlib/p2p';
28
+ import type {
29
+ BlockProposal,
30
+ BlockProposalOptions,
31
+ CheckpointAttestation,
32
+ CheckpointProposalCore,
33
+ CheckpointProposalOptions,
34
+ } from '@aztec/stdlib/p2p';
35
+ import { CheckpointProposal } from '@aztec/stdlib/p2p';
21
36
  import type { CheckpointHeader } from '@aztec/stdlib/rollup';
22
- import type { Tx } from '@aztec/stdlib/tx';
37
+ import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
23
38
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
24
- import { Attributes, type TelemetryClient, type Tracer, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
39
+ import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
25
40
 
26
41
  import { EventEmitter } from 'events';
27
42
  import type { TypedDataDefinition } from 'viem';
28
43
 
29
44
  import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
45
+ import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
30
46
  import { ValidationService } from './duties/validation_service.js';
31
47
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
32
48
  import { ValidatorMetrics } from './metrics.js';
@@ -61,11 +77,20 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
61
77
 
62
78
  private proposersOfInvalidBlocks: Set<string> = new Set();
63
79
 
80
+ // TODO(palla/mbps): Remove this once checkpoint validation is stable and we can validate all blocks properly.
81
+ // Tracks slots for which we have successfully validated a block proposal, so we can attest to checkpoint proposals for those slots.
82
+ // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
83
+ private validatedBlockSlots: Set<SlotNumber> = new Set();
84
+
64
85
  protected constructor(
65
86
  private keyStore: NodeKeystoreAdapter,
66
87
  private epochCache: EpochCache,
67
88
  private p2pClient: P2P,
68
89
  private blockProposalHandler: BlockProposalHandler,
90
+ private blockSource: L2BlockSource,
91
+ private checkpointsBuilder: FullNodeCheckpointsBuilder,
92
+ private worldState: WorldStateSynchronizer,
93
+ private l1ToL2MessageSource: L1ToL2MessageSource,
69
94
  private config: ValidatorClientFullConfig,
70
95
  private blobClient: BlobClientInterface,
71
96
  private dateProvider: DateProvider = new DateProvider(),
@@ -143,10 +168,11 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
143
168
 
144
169
  static new(
145
170
  config: ValidatorClientFullConfig,
146
- blockBuilder: IFullNodeBlockBuilder,
171
+ checkpointsBuilder: FullNodeCheckpointsBuilder,
172
+ worldState: WorldStateSynchronizer,
147
173
  epochCache: EpochCache,
148
174
  p2pClient: P2P,
149
- blockSource: L2BlockSource,
175
+ blockSource: L2BlockSource & L2BlockSink,
150
176
  l1ToL2MessageSource: L1ToL2MessageSource,
151
177
  txProvider: TxProvider,
152
178
  keyStoreManager: KeystoreManager,
@@ -159,11 +185,13 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
159
185
  txsPermitted: !config.disableTransactions,
160
186
  });
161
187
  const blockProposalHandler = new BlockProposalHandler(
162
- blockBuilder,
188
+ checkpointsBuilder,
189
+ worldState,
163
190
  blockSource,
164
191
  l1ToL2MessageSource,
165
192
  txProvider,
166
193
  blockProposalValidator,
194
+ epochCache,
167
195
  config,
168
196
  metrics,
169
197
  dateProvider,
@@ -175,6 +203,10 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
175
203
  epochCache,
176
204
  p2pClient,
177
205
  blockProposalHandler,
206
+ blockSource,
207
+ checkpointsBuilder,
208
+ worldState,
209
+ l1ToL2MessageSource,
178
210
  config,
179
211
  blobClient,
180
212
  dateProvider,
@@ -194,16 +226,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
194
226
  return this.blockProposalHandler;
195
227
  }
196
228
 
197
- // Proxy method for backwards compatibility with tests
198
- public reExecuteTransactions(
199
- proposal: BlockProposal,
200
- blockNumber: BlockNumber,
201
- txs: any[],
202
- l1ToL2Messages: Fr[],
203
- ): Promise<any> {
204
- return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
205
- }
206
-
207
229
  public signWithAddress(addr: EthAddress, msg: TypedDataDefinition) {
208
230
  return this.keyStore.signTypedDataWithAddress(addr, msg);
209
231
  }
@@ -253,9 +275,19 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
253
275
  this.hasRegisteredHandlers = true;
254
276
  this.log.debug(`Registering validator handlers for p2p client`);
255
277
 
256
- const handler = (block: BlockProposal, proposalSender: PeerId): Promise<BlockAttestation[] | undefined> =>
257
- this.attestToProposal(block, proposalSender);
258
- this.p2pClient.registerBlockProposalHandler(handler);
278
+ // Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
279
+ const blockHandler = (block: BlockProposal, proposalSender: PeerId): Promise<boolean> =>
280
+ this.validateBlockProposal(block, proposalSender);
281
+ this.p2pClient.registerBlockProposalHandler(blockHandler);
282
+
283
+ // Checkpoint proposal handler - validates and creates attestations
284
+ // The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
285
+ // and processed separately via the block handler above.
286
+ const checkpointHandler = (
287
+ checkpoint: CheckpointProposalCore,
288
+ proposalSender: PeerId,
289
+ ): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
290
+ this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
259
291
 
260
292
  const myAddresses = this.getValidatorAddresses();
261
293
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
@@ -264,33 +296,33 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
264
296
  }
265
297
  }
266
298
 
267
- @trackSpan('validator.attestToProposal', (proposal, proposalSender) => ({
268
- [Attributes.BLOCK_HASH]: proposal.payload.header.hash.toString(),
269
- [Attributes.PEER_ID]: proposalSender.toString(),
270
- }))
271
- async attestToProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<BlockAttestation[] | undefined> {
299
+ /**
300
+ * Validate a block proposal from a peer.
301
+ * Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
302
+ * @returns true if the proposal is valid, false otherwise
303
+ */
304
+ async validateBlockProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> {
272
305
  const slotNumber = proposal.slotNumber;
273
306
  const proposer = proposal.getSender();
274
307
 
275
308
  // Reject proposals with invalid signatures
276
309
  if (!proposer) {
277
- this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
278
- return undefined;
310
+ this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
311
+ return false;
279
312
  }
280
313
 
281
- // Check that I have any address in current committee before attesting
314
+ // Check if we're in the committee (for metrics purposes)
282
315
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
283
316
  const partOfCommittee = inCommittee.length > 0;
284
317
 
285
318
  const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
286
- this.log.info(`Received proposal for slot ${slotNumber}`, {
319
+ this.log.info(`Received block proposal for slot ${slotNumber}`, {
287
320
  ...proposalInfo,
288
321
  txHashes: proposal.txHashes.map(t => t.toString()),
289
322
  fishermanMode: this.config.fishermanMode || false,
290
323
  });
291
324
 
292
- // Reexecute txs if we are part of the committee so we can attest, or if slashing is enabled so we can slash
293
- // invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
325
+ // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
294
326
  // In fisherman mode, we always reexecute to validate proposals.
295
327
  const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } =
296
328
  this.config;
@@ -308,7 +340,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
308
340
  );
309
341
 
310
342
  if (!validationResult.isValid) {
311
- this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
343
+ this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
312
344
 
313
345
  const reason = validationResult.reason || 'unknown';
314
346
  // Classify failure reason: bad proposal vs node issue
@@ -323,7 +355,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
323
355
  if (badProposalReasons.includes(reason as BlockProposalValidationFailureReason)) {
324
356
  this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
325
357
  } else {
326
- // Node issues so we can't attest
358
+ // Node issues so we can't validate
327
359
  this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
328
360
  }
329
361
 
@@ -336,9 +368,81 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
336
368
  this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
337
369
  this.slashInvalidBlock(proposal);
338
370
  }
371
+ return false;
372
+ }
373
+
374
+ this.log.info(`Validated block proposal for slot ${slotNumber}`, {
375
+ ...proposalInfo,
376
+ inCommittee: partOfCommittee,
377
+ fishermanMode: this.config.fishermanMode || false,
378
+ });
379
+
380
+ // TODO(palla/mbps): Remove this once checkpoint validation is stable.
381
+ // Track that we successfully validated a block for this slot, so we can attest to checkpoint proposals for it.
382
+ this.validatedBlockSlots.add(slotNumber);
383
+
384
+ return true;
385
+ }
386
+
387
+ /**
388
+ * Validate and attest to a checkpoint proposal from a peer.
389
+ * The proposal is received as CheckpointProposalCore (without lastBlock) since
390
+ * the lastBlock is extracted and processed separately via the block handler.
391
+ * @returns Checkpoint attestations if valid, undefined otherwise
392
+ */
393
+ async attestToCheckpointProposal(
394
+ proposal: CheckpointProposalCore,
395
+ _proposalSender: PeerId,
396
+ ): Promise<CheckpointAttestation[] | undefined> {
397
+ const slotNumber = proposal.slotNumber;
398
+ const proposer = proposal.getSender();
399
+
400
+ // Reject proposals with invalid signatures
401
+ if (!proposer) {
402
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
403
+ return undefined;
404
+ }
405
+
406
+ // Check that I have any address in current committee before attesting
407
+ const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
408
+ const partOfCommittee = inCommittee.length > 0;
409
+
410
+ const proposalInfo = {
411
+ slotNumber,
412
+ archive: proposal.archive.toString(),
413
+ proposer: proposer.toString(),
414
+ txCount: proposal.txHashes.length,
415
+ };
416
+ this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
417
+ ...proposalInfo,
418
+ txHashes: proposal.txHashes.map(t => t.toString()),
419
+ fishermanMode: this.config.fishermanMode || false,
420
+ });
421
+
422
+ // TODO(palla/mbps): Remove this once checkpoint validation is stable.
423
+ // Check that we have successfully validated a block for this slot before attesting to the checkpoint.
424
+ if (!this.validatedBlockSlots.has(slotNumber)) {
425
+ this.log.warn(`No validated block found for slot ${slotNumber}, refusing to attest to checkpoint`, proposalInfo);
339
426
  return undefined;
340
427
  }
341
428
 
429
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
430
+ // TODO(palla/mbps): Change default to false once checkpoint validation is stable.
431
+ if (this.config.skipCheckpointProposalValidation !== false) {
432
+ this.log.verbose(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
433
+ } else {
434
+ const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
435
+ if (!validationResult.isValid) {
436
+ this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
437
+ return undefined;
438
+ }
439
+ }
440
+
441
+ // Upload blobs to filestore if we can (fire and forget)
442
+ if (this.blobClient.canUpload()) {
443
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
444
+ }
445
+
342
446
  // Check that I have any address in current committee before attesting
343
447
  // In fisherman mode, we still create attestations for validation even if not in committee
344
448
  if (!partOfCommittee && !this.config.fishermanMode) {
@@ -347,7 +451,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
347
451
  }
348
452
 
349
453
  // Provided all of the above checks pass, we can attest to the proposal
350
- this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} proposal for slot ${slotNumber}`, {
454
+ this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
351
455
  ...proposalInfo,
352
456
  inCommittee: partOfCommittee,
353
457
  fishermanMode: this.config.fishermanMode || false,
@@ -355,21 +459,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
355
459
 
356
460
  this.metrics.incSuccessfulAttestations(inCommittee.length);
357
461
 
358
- // Upload blobs to filestore after successful re-execution (fire-and-forget)
359
- if (validationResult.reexecutionResult?.block && this.blobClient.canUpload()) {
360
- void Promise.resolve().then(async () => {
361
- try {
362
- const blobFields = validationResult.reexecutionResult!.block.getCheckpointBlobFields();
363
- const blobs = getBlobsPerL1Block(blobFields);
364
- await this.blobClient.sendBlobsToFilestore(blobs);
365
- this.log.debug(`Uploaded ${blobs.length} blobs to filestore from re-execution`, proposalInfo);
366
- } catch (err) {
367
- this.log.warn(`Failed to upload blobs from re-execution`, err);
368
- }
369
- });
370
- }
371
-
372
- // If the above function does not throw an error, then we can attest to the proposal
373
462
  // Determine which validators should attest
374
463
  let attestors: EthAddress[];
375
464
  if (partOfCommittee) {
@@ -388,13 +477,244 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
388
477
 
389
478
  if (this.config.fishermanMode) {
390
479
  // bail out early and don't save attestations to the pool in fisherman mode
391
- this.log.info(`Creating attestations for proposal for slot ${slotNumber}`, {
480
+ this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
392
481
  ...proposalInfo,
393
482
  attestors: attestors.map(a => a.toString()),
394
483
  });
395
484
  return undefined;
396
485
  }
397
- return this.createBlockAttestationsFromProposal(proposal, attestors);
486
+
487
+ return this.createCheckpointAttestationsFromProposal(proposal, attestors);
488
+ }
489
+
490
+ private async createCheckpointAttestationsFromProposal(
491
+ proposal: CheckpointProposalCore,
492
+ attestors: EthAddress[] = [],
493
+ ): Promise<CheckpointAttestation[]> {
494
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
495
+ await this.p2pClient.addCheckpointAttestations(attestations);
496
+ return attestations;
497
+ }
498
+
499
+ /**
500
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
501
+ * @returns Validation result with isValid flag and reason if invalid.
502
+ */
503
+ private async validateCheckpointProposal(
504
+ proposal: CheckpointProposalCore,
505
+ proposalInfo: LogData,
506
+ ): Promise<{ isValid: true } | { isValid: false; reason: string }> {
507
+ const slot = proposal.slotNumber;
508
+ const timeoutSeconds = 10;
509
+
510
+ // Wait for last block to sync by archive
511
+ let lastBlockHeader: BlockHeader | undefined;
512
+ try {
513
+ lastBlockHeader = await retryUntil(
514
+ async () => {
515
+ await this.blockSource.syncImmediate();
516
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
517
+ },
518
+ `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
519
+ timeoutSeconds,
520
+ 0.5,
521
+ );
522
+ } catch (err) {
523
+ if (err instanceof TimeoutError) {
524
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
525
+ return { isValid: false, reason: 'last_block_not_found' };
526
+ }
527
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
528
+ return { isValid: false, reason: 'block_fetch_error' };
529
+ }
530
+
531
+ if (!lastBlockHeader) {
532
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
533
+ return { isValid: false, reason: 'last_block_not_found' };
534
+ }
535
+
536
+ // Get the last full block to determine checkpoint number
537
+ const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
538
+ if (!lastBlock) {
539
+ this.log.warn(`Last block ${lastBlockHeader.getBlockNumber()} not found`, proposalInfo);
540
+ return { isValid: false, reason: 'last_block_not_found' };
541
+ }
542
+ const checkpointNumber = lastBlock.checkpointNumber;
543
+
544
+ // Get all full blocks for the slot and checkpoint
545
+ const blocks = await this.getBlocksForSlot(slot, lastBlockHeader, checkpointNumber);
546
+ if (blocks.length === 0) {
547
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
548
+ return { isValid: false, reason: 'no_blocks_for_slot' };
549
+ }
550
+
551
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
552
+ ...proposalInfo,
553
+ blockNumbers: blocks.map(b => b.number),
554
+ });
555
+
556
+ // Get checkpoint constants from first block
557
+ const firstBlock = blocks[0];
558
+ const constants = this.extractCheckpointConstants(firstBlock);
559
+
560
+ // Get L1-to-L2 messages for this checkpoint
561
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
562
+
563
+ // Compute the previous checkpoint out hashes for the epoch.
564
+ // TODO: There can be a more efficient way to get the previous checkpoint out hashes without having to fetch the
565
+ // actual checkpoints and the blocks/txs in them.
566
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
567
+ const previousCheckpoints = (await this.blockSource.getCheckpointsForEpoch(epoch))
568
+ .filter(b => b.number < checkpointNumber)
569
+ .sort((a, b) => a.number - b.number);
570
+ const previousCheckpointOutHashes = previousCheckpoints.map(c => c.getCheckpointOutHash());
571
+
572
+ // Fork world state at the block before the first block
573
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
574
+ const fork = await this.worldState.fork(parentBlockNumber);
575
+
576
+ try {
577
+ // Create checkpoint builder with all existing blocks
578
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
579
+ checkpointNumber,
580
+ constants,
581
+ l1ToL2Messages,
582
+ previousCheckpointOutHashes,
583
+ fork,
584
+ blocks,
585
+ );
586
+
587
+ // Complete the checkpoint to get computed values
588
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
589
+
590
+ // Compare checkpoint header with proposal
591
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
592
+ this.log.warn(`Checkpoint header mismatch`, {
593
+ ...proposalInfo,
594
+ computed: computedCheckpoint.header.toInspect(),
595
+ proposal: proposal.checkpointHeader.toInspect(),
596
+ });
597
+ return { isValid: false, reason: 'checkpoint_header_mismatch' };
598
+ }
599
+
600
+ // Compare archive root with proposal
601
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
602
+ this.log.warn(`Archive root mismatch`, {
603
+ ...proposalInfo,
604
+ computed: computedCheckpoint.archive.root.toString(),
605
+ proposal: proposal.archive.toString(),
606
+ });
607
+ return { isValid: false, reason: 'archive_mismatch' };
608
+ }
609
+
610
+ // Check that the accumulated out hash matches the value in the proposal.
611
+ const computedOutHash = computedCheckpoint.getCheckpointOutHash();
612
+ const proposalOutHash = proposal.checkpointHeader.epochOutHash;
613
+ if (!computedOutHash.equals(proposalOutHash)) {
614
+ this.log.warn(`Epoch out hash mismatch`, {
615
+ proposalOutHash: proposalOutHash.toString(),
616
+ computedOutHash: computedOutHash.toString(),
617
+ ...proposalInfo,
618
+ });
619
+ return { isValid: false, reason: 'out_hash_mismatch' };
620
+ }
621
+
622
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
623
+ return { isValid: true };
624
+ } finally {
625
+ await fork.close();
626
+ }
627
+ }
628
+
629
+ /**
630
+ * Get all full blocks for a given slot and checkpoint by walking backwards from the last block.
631
+ * Returns blocks in ascending order (earliest to latest).
632
+ * TODO(palla/mbps): Add getL2BlocksForSlot() to L2BlockSource interface for efficiency.
633
+ */
634
+ private async getBlocksForSlot(
635
+ slot: SlotNumber,
636
+ lastBlockHeader: BlockHeader,
637
+ checkpointNumber: CheckpointNumber,
638
+ ): Promise<L2BlockNew[]> {
639
+ const blocks: L2BlockNew[] = [];
640
+ let currentHeader = lastBlockHeader;
641
+ const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
642
+
643
+ while (currentHeader.getSlot() === slot) {
644
+ const block = await this.blockSource.getL2BlockNew(currentHeader.getBlockNumber());
645
+ if (!block) {
646
+ this.log.warn(`Block ${currentHeader.getBlockNumber()} not found while getting blocks for slot ${slot}`);
647
+ break;
648
+ }
649
+ if (block.checkpointNumber !== checkpointNumber) {
650
+ break;
651
+ }
652
+ blocks.unshift(block);
653
+
654
+ const prevArchive = currentHeader.lastArchive.root;
655
+ if (prevArchive.equals(genesisArchiveRoot)) {
656
+ break;
657
+ }
658
+
659
+ const prevHeader = await this.blockSource.getBlockHeaderByArchive(prevArchive);
660
+ if (!prevHeader || prevHeader.getSlot() !== slot) {
661
+ break;
662
+ }
663
+ currentHeader = prevHeader;
664
+ }
665
+
666
+ return blocks;
667
+ }
668
+
669
+ /**
670
+ * Extract checkpoint global variables from a block.
671
+ */
672
+ private extractCheckpointConstants(block: L2BlockNew): CheckpointGlobalVariables {
673
+ const gv = block.header.globalVariables;
674
+ return {
675
+ chainId: gv.chainId,
676
+ version: gv.version,
677
+ slotNumber: gv.slotNumber,
678
+ coinbase: gv.coinbase,
679
+ feeRecipient: gv.feeRecipient,
680
+ gasFees: gv.gasFees,
681
+ };
682
+ }
683
+
684
+ /**
685
+ * Uploads blobs for a checkpoint to the filestore (fire and forget).
686
+ */
687
+ private async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
688
+ try {
689
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
690
+ if (!lastBlockHeader) {
691
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
692
+ return;
693
+ }
694
+
695
+ // Get the last full block to determine checkpoint number
696
+ const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
697
+ if (!lastBlock) {
698
+ this.log.warn(`Failed to get last block for blob upload`, proposalInfo);
699
+ return;
700
+ }
701
+
702
+ const blocks = await this.getBlocksForSlot(proposal.slotNumber, lastBlockHeader, lastBlock.checkpointNumber);
703
+ if (blocks.length === 0) {
704
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
705
+ return;
706
+ }
707
+
708
+ const blobFields = blocks.flatMap(b => b.toBlobFields());
709
+ const blobs: Blob[] = getBlobsPerL1Block(blobFields);
710
+ await this.blobClient.sendBlobsToFilestore(blobs);
711
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
712
+ ...proposalInfo,
713
+ numBlobs: blobs.length,
714
+ });
715
+ } catch (err) {
716
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
717
+ }
398
718
  }
399
719
 
400
720
  private slashInvalidBlock(proposal: BlockProposal) {
@@ -424,40 +744,55 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
424
744
  ]);
425
745
  }
426
746
 
427
- // TODO(palla/mbps): Block proposal should not require a checkpoint proposal
428
747
  async createBlockProposal(
429
- blockNumber: BlockNumber,
430
- header: CheckpointHeader,
748
+ blockHeader: BlockHeader,
749
+ indexWithinCheckpoint: number,
750
+ inHash: Fr,
431
751
  archive: Fr,
432
752
  txs: Tx[],
433
753
  proposerAddress: EthAddress | undefined,
434
754
  options: BlockProposalOptions,
435
755
  ): Promise<BlockProposal> {
436
756
  // TODO(palla/mbps): Prevent double proposals properly
437
- // if (this.previousProposal?.slotNumber === header.slotNumber) {
757
+ // if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
438
758
  // this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
439
759
  // return Promise.resolve(undefined);
440
760
  // }
441
761
 
442
- this.log.info(`Assembling block proposal for block ${blockNumber} slot ${header.slotNumber}`);
443
- const newProposal = await this.validationService.createBlockProposal(header, archive, txs, proposerAddress, {
444
- ...options,
445
- broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
446
- });
762
+ this.log.info(
763
+ `Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
764
+ );
765
+ const newProposal = await this.validationService.createBlockProposal(
766
+ blockHeader,
767
+ indexWithinCheckpoint,
768
+ inHash,
769
+ archive,
770
+ txs,
771
+ proposerAddress,
772
+ {
773
+ ...options,
774
+ broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
775
+ },
776
+ );
447
777
  this.previousProposal = newProposal;
448
778
  return newProposal;
449
779
  }
450
780
 
451
- // TODO(palla/mbps): Effectively create a checkpoint proposal different from a block proposal
452
- createCheckpointProposal(
453
- header: CheckpointHeader,
781
+ async createCheckpointProposal(
782
+ checkpointHeader: CheckpointHeader,
454
783
  archive: Fr,
455
- txs: Tx[],
784
+ lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
456
785
  proposerAddress: EthAddress | undefined,
457
- options: BlockProposalOptions,
458
- ): Promise<BlockProposal> {
459
- this.log.info(`Assembling checkpoint proposal for slot ${header.slotNumber}`);
460
- return this.createBlockProposal(0 as BlockNumber, header, archive, txs, proposerAddress, options);
786
+ options: CheckpointProposalOptions,
787
+ ): Promise<CheckpointProposal> {
788
+ this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
789
+ return await this.validationService.createCheckpointProposal(
790
+ checkpointHeader,
791
+ archive,
792
+ lastBlockInfo,
793
+ proposerAddress,
794
+ options,
795
+ );
461
796
  }
462
797
 
463
798
  async broadcastBlockProposal(proposal: BlockProposal): Promise<void> {
@@ -471,24 +806,28 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
471
806
  return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
472
807
  }
473
808
 
474
- async collectOwnAttestations(proposal: BlockProposal): Promise<BlockAttestation[]> {
475
- const slot = proposal.payload.header.slotNumber;
809
+ async collectOwnAttestations(proposal: CheckpointProposal): Promise<CheckpointAttestation[]> {
810
+ const slot = proposal.slotNumber;
476
811
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
477
812
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
478
- const attestations = await this.createBlockAttestationsFromProposal(proposal, inCommittee);
813
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
479
814
 
480
815
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
481
816
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
482
817
  // due to inactivity for missed attestations.
483
- void this.p2pClient.broadcastAttestations(attestations).catch(err => {
818
+ void this.p2pClient.broadcastCheckpointAttestations(attestations).catch(err => {
484
819
  this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
485
820
  });
486
821
  return attestations;
487
822
  }
488
823
 
489
- async collectAttestations(proposal: BlockProposal, required: number, deadline: Date): Promise<BlockAttestation[]> {
490
- // Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
491
- const slot = proposal.payload.header.slotNumber;
824
+ async collectAttestations(
825
+ proposal: CheckpointProposal,
826
+ required: number,
827
+ deadline: Date,
828
+ ): Promise<CheckpointAttestation[]> {
829
+ // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
830
+ const slot = proposal.slotNumber;
492
831
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
493
832
 
494
833
  if (+deadline < this.dateProvider.now()) {
@@ -503,16 +842,16 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
503
842
  const proposalId = proposal.archive.toString();
504
843
  const myAddresses = this.getValidatorAddresses();
505
844
 
506
- let attestations: BlockAttestation[] = [];
845
+ let attestations: CheckpointAttestation[] = [];
507
846
  while (true) {
508
- // Filter out attestations with a mismatching payload. This should NOT happen since we have verified
847
+ // Filter out attestations with a mismatching archive. This should NOT happen since we have verified
509
848
  // the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
510
- const collectedAttestations = (await this.p2pClient.getAttestationsForSlot(slot, proposalId)).filter(
849
+ const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter(
511
850
  attestation => {
512
- if (!attestation.payload.equals(proposal.payload)) {
851
+ if (!attestation.archive.equals(proposal.archive)) {
513
852
  this.log.warn(
514
- `Received attestation for slot ${slot} with mismatched payload from ${attestation.getSender()?.toString()}`,
515
- { attestationPayload: attestation.payload, proposalPayload: proposal.payload },
853
+ `Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`,
854
+ { attestationArchive: attestation.archive.toString(), proposalArchive: proposal.archive.toString() },
516
855
  );
517
856
  return false;
518
857
  }
@@ -553,15 +892,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
553
892
  }
554
893
  }
555
894
 
556
- private async createBlockAttestationsFromProposal(
557
- proposal: BlockProposal,
558
- attestors: EthAddress[] = [],
559
- ): Promise<BlockAttestation[]> {
560
- const attestations = await this.validationService.attestToProposal(proposal, attestors);
561
- await this.p2pClient.addAttestations(attestations);
562
- return attestations;
563
- }
564
-
565
895
  private async handleAuthRequest(peer: PeerId, msg: Buffer): Promise<Buffer> {
566
896
  const authRequest = AuthRequest.fromBuffer(msg);
567
897
  const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch(_ => undefined);