@aztec/validator-client 0.0.1-commit.9593d84 → 0.0.1-commit.96bb3f7

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