@aztec/validator-client 0.0.1-commit.03f7ef2 → 0.0.1-commit.0b941701

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