@aztec/validator-client 0.0.1-commit.03f7ef2 → 0.0.1-commit.1142ef1

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 (46) hide show
  1. package/README.md +256 -0
  2. package/dest/block_proposal_handler.d.ts +20 -10
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +333 -72
  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 +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 +12 -9
  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/metrics.d.ts +1 -1
  21. package/dest/metrics.d.ts.map +1 -1
  22. package/dest/metrics.js +8 -33
  23. package/dest/tx_validator/index.d.ts +3 -0
  24. package/dest/tx_validator/index.d.ts.map +1 -0
  25. package/dest/tx_validator/index.js +2 -0
  26. package/dest/tx_validator/nullifier_cache.d.ts +14 -0
  27. package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
  28. package/dest/tx_validator/nullifier_cache.js +24 -0
  29. package/dest/tx_validator/tx_validator_factory.d.ts +18 -0
  30. package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
  31. package/dest/tx_validator/tx_validator_factory.js +53 -0
  32. package/dest/validator.d.ts +41 -17
  33. package/dest/validator.d.ts.map +1 -1
  34. package/dest/validator.js +298 -64
  35. package/package.json +16 -12
  36. package/src/block_proposal_handler.ts +249 -39
  37. package/src/checkpoint_builder.ts +267 -0
  38. package/src/config.ts +10 -0
  39. package/src/duties/validation_service.ts +79 -25
  40. package/src/factory.ts +16 -11
  41. package/src/index.ts +2 -0
  42. package/src/metrics.ts +7 -34
  43. package/src/tx_validator/index.ts +2 -0
  44. package/src/tx_validator/nullifier_cache.ts +30 -0
  45. package/src/tx_validator/tx_validator_factory.ts +133 -0
  46. package/src/validator.ts +404 -94
package/dest/validator.js CHANGED
@@ -1,5 +1,8 @@
1
1
  import { getBlobsPerL1Block } from '@aztec/blob-lib';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import { TimeoutError } from '@aztec/foundation/error';
2
4
  import { createLogger } from '@aztec/foundation/log';
5
+ import { retryUntil } from '@aztec/foundation/retry';
3
6
  import { RunningPromise } from '@aztec/foundation/running-promise';
4
7
  import { sleep } from '@aztec/foundation/sleep';
5
8
  import { DateProvider } from '@aztec/foundation/timer';
@@ -27,8 +30,12 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
27
30
  epochCache;
28
31
  p2pClient;
29
32
  blockProposalHandler;
33
+ blockSource;
34
+ checkpointsBuilder;
35
+ worldState;
36
+ l1ToL2MessageSource;
30
37
  config;
31
- fileStoreBlobUploadClient;
38
+ blobClient;
32
39
  dateProvider;
33
40
  tracer;
34
41
  validationService;
@@ -41,8 +48,12 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
41
48
  lastEpochForCommitteeUpdateLoop;
42
49
  epochCacheUpdateLoop;
43
50
  proposersOfInvalidBlocks;
44
- constructor(keyStore, epochCache, p2pClient, blockProposalHandler, config, fileStoreBlobUploadClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
45
- super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.config = config, this.fileStoreBlobUploadClient = fileStoreBlobUploadClient, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set();
51
+ // TODO(palla/mbps): Remove this once checkpoint validation is stable and we can validate all blocks properly.
52
+ // Tracks slots for which we have successfully validated a block proposal, so we can attest to checkpoint proposals for those slots.
53
+ // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
54
+ validatedBlockSlots;
55
+ constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
56
+ super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set(), this.validatedBlockSlots = new Set();
46
57
  // Create child logger with fisherman prefix if in fisherman mode
47
58
  this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
48
59
  this.tracer = telemetry.getTracer('Validator');
@@ -96,13 +107,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
96
107
  this.log.error(`Error updating epoch committee`, err);
97
108
  }
98
109
  }
99
- static new(config, blockBuilder, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, fileStoreBlobUploadClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
110
+ static new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
100
111
  const metrics = new ValidatorMetrics(telemetry);
101
112
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
102
113
  txsPermitted: !config.disableTransactions
103
114
  });
104
- const blockProposalHandler = new BlockProposalHandler(blockBuilder, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider, telemetry);
105
- const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, config, fileStoreBlobUploadClient, dateProvider, telemetry);
115
+ const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider, telemetry);
116
+ const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider, telemetry);
106
117
  return validator;
107
118
  }
108
119
  getValidatorAddresses() {
@@ -111,10 +122,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
111
122
  getBlockProposalHandler() {
112
123
  return this.blockProposalHandler;
113
124
  }
114
- // Proxy method for backwards compatibility with tests
115
- reExecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages) {
116
- return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
117
- }
118
125
  signWithAddress(addr, msg) {
119
126
  return this.keyStore.signTypedDataWithAddress(addr, msg);
120
127
  }
@@ -155,41 +162,50 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
155
162
  if (!this.hasRegisteredHandlers) {
156
163
  this.hasRegisteredHandlers = true;
157
164
  this.log.debug(`Registering validator handlers for p2p client`);
158
- const handler = (block, proposalSender)=>this.attestToProposal(block, proposalSender);
159
- this.p2pClient.registerBlockProposalHandler(handler);
165
+ // Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
166
+ const blockHandler = (block, proposalSender)=>this.validateBlockProposal(block, proposalSender);
167
+ this.p2pClient.registerBlockProposalHandler(blockHandler);
168
+ // Checkpoint proposal handler - validates and creates attestations
169
+ // The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
170
+ // and processed separately via the block handler above.
171
+ const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
172
+ this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
160
173
  const myAddresses = this.getValidatorAddresses();
161
174
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
162
175
  await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
163
176
  }
164
177
  }
165
- async attestToProposal(proposal, proposalSender) {
178
+ /**
179
+ * Validate a block proposal from a peer.
180
+ * Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
181
+ * @returns true if the proposal is valid, false otherwise
182
+ */ async validateBlockProposal(proposal, proposalSender) {
166
183
  const slotNumber = proposal.slotNumber;
167
184
  const proposer = proposal.getSender();
168
185
  // Reject proposals with invalid signatures
169
186
  if (!proposer) {
170
- this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
171
- return undefined;
187
+ this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
188
+ return false;
172
189
  }
173
- // Check that I have any address in current committee before attesting
190
+ // Check if we're in the committee (for metrics purposes)
174
191
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
175
192
  const partOfCommittee = inCommittee.length > 0;
176
193
  const proposalInfo = {
177
194
  ...proposal.toBlockInfo(),
178
195
  proposer: proposer.toString()
179
196
  };
180
- this.log.info(`Received proposal for slot ${slotNumber}`, {
197
+ this.log.info(`Received block proposal for slot ${slotNumber}`, {
181
198
  ...proposalInfo,
182
199
  txHashes: proposal.txHashes.map((t)=>t.toString()),
183
200
  fishermanMode: this.config.fishermanMode || false
184
201
  });
185
- // Reexecute txs if we are part of the committee so we can attest, or if slashing is enabled so we can slash
186
- // invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
202
+ // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
187
203
  // In fisherman mode, we always reexecute to validate proposals.
188
204
  const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
189
- const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.fileStoreBlobUploadClient;
205
+ const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
190
206
  const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
191
207
  if (!validationResult.isValid) {
192
- this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
208
+ this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
193
209
  const reason = validationResult.reason || 'unknown';
194
210
  // Classify failure reason: bad proposal vs node issue
195
211
  const badProposalReasons = [
@@ -202,7 +218,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
202
218
  if (badProposalReasons.includes(reason)) {
203
219
  this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
204
220
  } else {
205
- // Node issues so we can't attest
221
+ // Node issues so we can't validate
206
222
  this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
207
223
  }
208
224
  // Slash invalid block proposals (can happen even when not in committee)
@@ -210,35 +226,79 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
210
226
  this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
211
227
  this.slashInvalidBlock(proposal);
212
228
  }
229
+ return false;
230
+ }
231
+ this.log.info(`Validated block proposal for slot ${slotNumber}`, {
232
+ ...proposalInfo,
233
+ inCommittee: partOfCommittee,
234
+ fishermanMode: this.config.fishermanMode || false
235
+ });
236
+ // TODO(palla/mbps): Remove this once checkpoint validation is stable.
237
+ // Track that we successfully validated a block for this slot, so we can attest to checkpoint proposals for it.
238
+ this.validatedBlockSlots.add(slotNumber);
239
+ return true;
240
+ }
241
+ /**
242
+ * Validate and attest to a checkpoint proposal from a peer.
243
+ * The proposal is received as CheckpointProposalCore (without lastBlock) since
244
+ * the lastBlock is extracted and processed separately via the block handler.
245
+ * @returns Checkpoint attestations if valid, undefined otherwise
246
+ */ async attestToCheckpointProposal(proposal, _proposalSender) {
247
+ const slotNumber = proposal.slotNumber;
248
+ const proposer = proposal.getSender();
249
+ // Reject proposals with invalid signatures
250
+ if (!proposer) {
251
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
213
252
  return undefined;
214
253
  }
215
254
  // Check that I have any address in current committee before attesting
255
+ const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
256
+ const partOfCommittee = inCommittee.length > 0;
257
+ const proposalInfo = {
258
+ slotNumber,
259
+ archive: proposal.archive.toString(),
260
+ proposer: proposer.toString(),
261
+ txCount: proposal.txHashes.length
262
+ };
263
+ this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
264
+ ...proposalInfo,
265
+ txHashes: proposal.txHashes.map((t)=>t.toString()),
266
+ fishermanMode: this.config.fishermanMode || false
267
+ });
268
+ // TODO(palla/mbps): Remove this once checkpoint validation is stable.
269
+ // Check that we have successfully validated a block for this slot before attesting to the checkpoint.
270
+ if (!this.validatedBlockSlots.has(slotNumber)) {
271
+ this.log.warn(`No validated block found for slot ${slotNumber}, refusing to attest to checkpoint`, proposalInfo);
272
+ return undefined;
273
+ }
274
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
275
+ // TODO(palla/mbps): Change default to false once checkpoint validation is stable.
276
+ if (this.config.skipCheckpointProposalValidation !== false) {
277
+ this.log.verbose(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
278
+ } else {
279
+ const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
280
+ if (!validationResult.isValid) {
281
+ this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
282
+ return undefined;
283
+ }
284
+ }
285
+ // Upload blobs to filestore if we can (fire and forget)
286
+ if (this.blobClient.canUpload()) {
287
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
288
+ }
289
+ // Check that I have any address in current committee before attesting
216
290
  // In fisherman mode, we still create attestations for validation even if not in committee
217
291
  if (!partOfCommittee && !this.config.fishermanMode) {
218
292
  this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
219
293
  return undefined;
220
294
  }
221
295
  // Provided all of the above checks pass, we can attest to the proposal
222
- this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} proposal for slot ${slotNumber}`, {
296
+ this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
223
297
  ...proposalInfo,
224
298
  inCommittee: partOfCommittee,
225
299
  fishermanMode: this.config.fishermanMode || false
226
300
  });
227
301
  this.metrics.incSuccessfulAttestations(inCommittee.length);
228
- // Upload blobs to filestore after successful re-execution (fire-and-forget)
229
- if (validationResult.reexecutionResult?.block && this.fileStoreBlobUploadClient) {
230
- void Promise.resolve().then(async ()=>{
231
- try {
232
- const blobFields = validationResult.reexecutionResult.block.getCheckpointBlobFields();
233
- const blobs = getBlobsPerL1Block(blobFields);
234
- await this.fileStoreBlobUploadClient.saveBlobs(blobs, true);
235
- this.log.debug(`Uploaded ${blobs.length} blobs to filestore from re-execution`, proposalInfo);
236
- } catch (err) {
237
- this.log.warn(`Failed to upload blobs from re-execution`, err);
238
- }
239
- });
240
- }
241
- // If the above function does not throw an error, then we can attest to the proposal
242
302
  // Determine which validators should attest
243
303
  let attestors;
244
304
  if (partOfCommittee) {
@@ -255,13 +315,194 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
255
315
  }
256
316
  if (this.config.fishermanMode) {
257
317
  // bail out early and don't save attestations to the pool in fisherman mode
258
- this.log.info(`Creating attestations for proposal for slot ${slotNumber}`, {
318
+ this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
259
319
  ...proposalInfo,
260
320
  attestors: attestors.map((a)=>a.toString())
261
321
  });
262
322
  return undefined;
263
323
  }
264
- return this.createBlockAttestationsFromProposal(proposal, attestors);
324
+ return this.createCheckpointAttestationsFromProposal(proposal, attestors);
325
+ }
326
+ async createCheckpointAttestationsFromProposal(proposal, attestors = []) {
327
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
328
+ await this.p2pClient.addCheckpointAttestations(attestations);
329
+ return attestations;
330
+ }
331
+ /**
332
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
333
+ * @returns Validation result with isValid flag and reason if invalid.
334
+ */ async validateCheckpointProposal(proposal, proposalInfo) {
335
+ const slot = proposal.slotNumber;
336
+ const timeoutSeconds = 10;
337
+ // Wait for last block to sync by archive
338
+ let lastBlockHeader;
339
+ try {
340
+ lastBlockHeader = await retryUntil(async ()=>{
341
+ await this.blockSource.syncImmediate();
342
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
343
+ }, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, timeoutSeconds, 0.5);
344
+ } catch (err) {
345
+ if (err instanceof TimeoutError) {
346
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
347
+ return {
348
+ isValid: false,
349
+ reason: 'last_block_not_found'
350
+ };
351
+ }
352
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
353
+ return {
354
+ isValid: false,
355
+ reason: 'block_fetch_error'
356
+ };
357
+ }
358
+ if (!lastBlockHeader) {
359
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
360
+ return {
361
+ isValid: false,
362
+ reason: 'last_block_not_found'
363
+ };
364
+ }
365
+ // Get the last full block to determine checkpoint number
366
+ const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
367
+ if (!lastBlock) {
368
+ this.log.warn(`Last block ${lastBlockHeader.getBlockNumber()} not found`, proposalInfo);
369
+ return {
370
+ isValid: false,
371
+ reason: 'last_block_not_found'
372
+ };
373
+ }
374
+ const checkpointNumber = lastBlock.checkpointNumber;
375
+ // Get all full blocks for the slot and checkpoint
376
+ const blocks = await this.getBlocksForSlot(slot, lastBlockHeader, checkpointNumber);
377
+ if (blocks.length === 0) {
378
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
379
+ return {
380
+ isValid: false,
381
+ reason: 'no_blocks_for_slot'
382
+ };
383
+ }
384
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
385
+ ...proposalInfo,
386
+ blockNumbers: blocks.map((b)=>b.number)
387
+ });
388
+ // Get checkpoint constants from first block
389
+ const firstBlock = blocks[0];
390
+ const constants = this.extractCheckpointConstants(firstBlock);
391
+ // Get L1-to-L2 messages for this checkpoint
392
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
393
+ // Fork world state at the block before the first block
394
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
395
+ const fork = await this.worldState.fork(parentBlockNumber);
396
+ try {
397
+ // Create checkpoint builder with all existing blocks
398
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, l1ToL2Messages, fork, blocks);
399
+ // Complete the checkpoint to get computed values
400
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
401
+ // Compare checkpoint header with proposal
402
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
403
+ this.log.warn(`Checkpoint header mismatch`, {
404
+ ...proposalInfo,
405
+ computed: computedCheckpoint.header.toInspect(),
406
+ proposal: proposal.checkpointHeader.toInspect()
407
+ });
408
+ return {
409
+ isValid: false,
410
+ reason: 'checkpoint_header_mismatch'
411
+ };
412
+ }
413
+ // Compare archive root with proposal
414
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
415
+ this.log.warn(`Archive root mismatch`, {
416
+ ...proposalInfo,
417
+ computed: computedCheckpoint.archive.root.toString(),
418
+ proposal: proposal.archive.toString()
419
+ });
420
+ return {
421
+ isValid: false,
422
+ reason: 'archive_mismatch'
423
+ };
424
+ }
425
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
426
+ return {
427
+ isValid: true
428
+ };
429
+ } finally{
430
+ await fork.close();
431
+ }
432
+ }
433
+ /**
434
+ * Get all full blocks for a given slot and checkpoint by walking backwards from the last block.
435
+ * Returns blocks in ascending order (earliest to latest).
436
+ * TODO(palla/mbps): Add getL2BlocksForSlot() to L2BlockSource interface for efficiency.
437
+ */ async getBlocksForSlot(slot, lastBlockHeader, checkpointNumber) {
438
+ const blocks = [];
439
+ let currentHeader = lastBlockHeader;
440
+ const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
441
+ while(currentHeader.getSlot() === slot){
442
+ const block = await this.blockSource.getL2BlockNew(currentHeader.getBlockNumber());
443
+ if (!block) {
444
+ this.log.warn(`Block ${currentHeader.getBlockNumber()} not found while getting blocks for slot ${slot}`);
445
+ break;
446
+ }
447
+ if (block.checkpointNumber !== checkpointNumber) {
448
+ break;
449
+ }
450
+ blocks.unshift(block);
451
+ const prevArchive = currentHeader.lastArchive.root;
452
+ if (prevArchive.equals(genesisArchiveRoot)) {
453
+ break;
454
+ }
455
+ const prevHeader = await this.blockSource.getBlockHeaderByArchive(prevArchive);
456
+ if (!prevHeader || prevHeader.getSlot() !== slot) {
457
+ break;
458
+ }
459
+ currentHeader = prevHeader;
460
+ }
461
+ return blocks;
462
+ }
463
+ /**
464
+ * Extract checkpoint global variables from a block.
465
+ */ extractCheckpointConstants(block) {
466
+ const gv = block.header.globalVariables;
467
+ return {
468
+ chainId: gv.chainId,
469
+ version: gv.version,
470
+ slotNumber: gv.slotNumber,
471
+ coinbase: gv.coinbase,
472
+ feeRecipient: gv.feeRecipient,
473
+ gasFees: gv.gasFees
474
+ };
475
+ }
476
+ /**
477
+ * Uploads blobs for a checkpoint to the filestore (fire and forget).
478
+ */ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
479
+ try {
480
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
481
+ if (!lastBlockHeader) {
482
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
483
+ return;
484
+ }
485
+ // Get the last full block to determine checkpoint number
486
+ const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
487
+ if (!lastBlock) {
488
+ this.log.warn(`Failed to get last block for blob upload`, proposalInfo);
489
+ return;
490
+ }
491
+ const blocks = await this.getBlocksForSlot(proposal.slotNumber, lastBlockHeader, lastBlock.checkpointNumber);
492
+ if (blocks.length === 0) {
493
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
494
+ return;
495
+ }
496
+ const blobFields = blocks.flatMap((b)=>b.toBlobFields());
497
+ const blobs = getBlobsPerL1Block(blobFields);
498
+ await this.blobClient.sendBlobsToFilestore(blobs);
499
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
500
+ ...proposalInfo,
501
+ numBlobs: blobs.length
502
+ });
503
+ } catch (err) {
504
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
505
+ }
265
506
  }
266
507
  slashInvalidBlock(proposal) {
267
508
  const proposer = proposal.getSender();
@@ -285,25 +526,23 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
285
526
  }
286
527
  ]);
287
528
  }
288
- // TODO(palla/mbps): Block proposal should not require a checkpoint proposal
289
- async createBlockProposal(blockNumber, header, archive, txs, proposerAddress, options) {
529
+ async createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options) {
290
530
  // TODO(palla/mbps): Prevent double proposals properly
291
- // if (this.previousProposal?.slotNumber === header.slotNumber) {
531
+ // if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
292
532
  // this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
293
533
  // return Promise.resolve(undefined);
294
534
  // }
295
- this.log.info(`Assembling block proposal for block ${blockNumber} slot ${header.slotNumber}`);
296
- const newProposal = await this.validationService.createBlockProposal(header, archive, txs, proposerAddress, {
535
+ this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
536
+ const newProposal = await this.validationService.createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
297
537
  ...options,
298
538
  broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
299
539
  });
300
540
  this.previousProposal = newProposal;
301
541
  return newProposal;
302
542
  }
303
- // TODO(palla/mbps): Effectively create a checkpoint proposal different from a block proposal
304
- createCheckpointProposal(header, archive, txs, proposerAddress, options) {
305
- this.log.info(`Assembling checkpoint proposal for slot ${header.slotNumber}`);
306
- return this.createBlockProposal(0, header, archive, txs, proposerAddress, options);
543
+ async createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options) {
544
+ this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
545
+ return await this.validationService.createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options);
307
546
  }
308
547
  async broadcastBlockProposal(proposal) {
309
548
  await this.p2pClient.broadcastProposal(proposal);
@@ -312,23 +551,23 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
312
551
  return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
313
552
  }
314
553
  async collectOwnAttestations(proposal) {
315
- const slot = proposal.payload.header.slotNumber;
554
+ const slot = proposal.slotNumber;
316
555
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
317
556
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
318
557
  inCommittee
319
558
  });
320
- const attestations = await this.createBlockAttestationsFromProposal(proposal, inCommittee);
559
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
321
560
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
322
561
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
323
562
  // due to inactivity for missed attestations.
324
- void this.p2pClient.broadcastAttestations(attestations).catch((err)=>{
563
+ void this.p2pClient.broadcastCheckpointAttestations(attestations).catch((err)=>{
325
564
  this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
326
565
  });
327
566
  return attestations;
328
567
  }
329
568
  async collectAttestations(proposal, required, deadline) {
330
- // Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
331
- const slot = proposal.payload.header.slotNumber;
569
+ // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
570
+ const slot = proposal.slotNumber;
332
571
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
333
572
  if (+deadline < this.dateProvider.now()) {
334
573
  this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
@@ -339,13 +578,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
339
578
  const myAddresses = this.getValidatorAddresses();
340
579
  let attestations = [];
341
580
  while(true){
342
- // Filter out attestations with a mismatching payload. This should NOT happen since we have verified
581
+ // Filter out attestations with a mismatching archive. This should NOT happen since we have verified
343
582
  // the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
344
- const collectedAttestations = (await this.p2pClient.getAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
345
- if (!attestation.payload.equals(proposal.payload)) {
346
- this.log.warn(`Received attestation for slot ${slot} with mismatched payload from ${attestation.getSender()?.toString()}`, {
347
- attestationPayload: attestation.payload,
348
- proposalPayload: proposal.payload
583
+ const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
584
+ if (!attestation.archive.equals(proposal.archive)) {
585
+ this.log.warn(`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`, {
586
+ attestationArchive: attestation.archive.toString(),
587
+ proposalArchive: proposal.archive.toString()
349
588
  });
350
589
  return false;
351
590
  }
@@ -377,11 +616,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
377
616
  await sleep(this.config.attestationPollingIntervalMs);
378
617
  }
379
618
  }
380
- async createBlockAttestationsFromProposal(proposal, attestors = []) {
381
- const attestations = await this.validationService.attestToProposal(proposal, attestors);
382
- await this.p2pClient.addAttestations(attestations);
383
- return attestations;
384
- }
385
619
  async handleAuthRequest(peer, msg) {
386
620
  const authRequest = AuthRequest.fromBuffer(msg);
387
621
  const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/validator-client",
3
- "version": "0.0.1-commit.03f7ef2",
3
+ "version": "0.0.1-commit.1142ef1",
4
4
  "main": "dest/index.js",
5
5
  "type": "module",
6
6
  "exports": {
@@ -64,17 +64,21 @@
64
64
  ]
65
65
  },
66
66
  "dependencies": {
67
- "@aztec/blob-client": "0.0.1-commit.03f7ef2",
68
- "@aztec/blob-lib": "0.0.1-commit.03f7ef2",
69
- "@aztec/constants": "0.0.1-commit.03f7ef2",
70
- "@aztec/epoch-cache": "0.0.1-commit.03f7ef2",
71
- "@aztec/ethereum": "0.0.1-commit.03f7ef2",
72
- "@aztec/foundation": "0.0.1-commit.03f7ef2",
73
- "@aztec/node-keystore": "0.0.1-commit.03f7ef2",
74
- "@aztec/p2p": "0.0.1-commit.03f7ef2",
75
- "@aztec/slasher": "0.0.1-commit.03f7ef2",
76
- "@aztec/stdlib": "0.0.1-commit.03f7ef2",
77
- "@aztec/telemetry-client": "0.0.1-commit.03f7ef2",
67
+ "@aztec/blob-client": "0.0.1-commit.1142ef1",
68
+ "@aztec/blob-lib": "0.0.1-commit.1142ef1",
69
+ "@aztec/constants": "0.0.1-commit.1142ef1",
70
+ "@aztec/epoch-cache": "0.0.1-commit.1142ef1",
71
+ "@aztec/ethereum": "0.0.1-commit.1142ef1",
72
+ "@aztec/foundation": "0.0.1-commit.1142ef1",
73
+ "@aztec/node-keystore": "0.0.1-commit.1142ef1",
74
+ "@aztec/noir-protocol-circuits-types": "0.0.1-commit.1142ef1",
75
+ "@aztec/p2p": "0.0.1-commit.1142ef1",
76
+ "@aztec/protocol-contracts": "0.0.1-commit.1142ef1",
77
+ "@aztec/prover-client": "0.0.1-commit.1142ef1",
78
+ "@aztec/simulator": "0.0.1-commit.1142ef1",
79
+ "@aztec/slasher": "0.0.1-commit.1142ef1",
80
+ "@aztec/stdlib": "0.0.1-commit.1142ef1",
81
+ "@aztec/telemetry-client": "0.0.1-commit.1142ef1",
78
82
  "koa": "^2.16.1",
79
83
  "koa-router": "^13.1.1",
80
84
  "tslib": "^2.4.0",