@aztec/sequencer-client 0.0.1-commit.c80b6263 → 0.0.1-commit.cb6bed7c2

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 (73) hide show
  1. package/dest/client/sequencer-client.d.ts +23 -7
  2. package/dest/client/sequencer-client.d.ts.map +1 -1
  3. package/dest/client/sequencer-client.js +99 -16
  4. package/dest/config.d.ts +24 -6
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +40 -28
  7. package/dest/global_variable_builder/global_builder.d.ts +2 -4
  8. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  9. package/dest/publisher/config.d.ts +35 -17
  10. package/dest/publisher/config.d.ts.map +1 -1
  11. package/dest/publisher/config.js +106 -42
  12. package/dest/publisher/index.d.ts +2 -1
  13. package/dest/publisher/index.d.ts.map +1 -1
  14. package/dest/publisher/l1_tx_failed_store/factory.d.ts +11 -0
  15. package/dest/publisher/l1_tx_failed_store/factory.d.ts.map +1 -0
  16. package/dest/publisher/l1_tx_failed_store/factory.js +22 -0
  17. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +59 -0
  18. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -0
  19. package/dest/publisher/l1_tx_failed_store/failed_tx_store.js +1 -0
  20. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts +15 -0
  21. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts.map +1 -0
  22. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.js +34 -0
  23. package/dest/publisher/l1_tx_failed_store/index.d.ts +4 -0
  24. package/dest/publisher/l1_tx_failed_store/index.d.ts.map +1 -0
  25. package/dest/publisher/l1_tx_failed_store/index.js +2 -0
  26. package/dest/publisher/sequencer-publisher-factory.d.ts +11 -3
  27. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  28. package/dest/publisher/sequencer-publisher-factory.js +27 -2
  29. package/dest/publisher/sequencer-publisher.d.ts +26 -8
  30. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  31. package/dest/publisher/sequencer-publisher.js +338 -48
  32. package/dest/sequencer/checkpoint_proposal_job.d.ts +28 -7
  33. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  34. package/dest/sequencer/checkpoint_proposal_job.js +164 -89
  35. package/dest/sequencer/metrics.d.ts +17 -5
  36. package/dest/sequencer/metrics.d.ts.map +1 -1
  37. package/dest/sequencer/metrics.js +86 -15
  38. package/dest/sequencer/sequencer.d.ts +25 -12
  39. package/dest/sequencer/sequencer.d.ts.map +1 -1
  40. package/dest/sequencer/sequencer.js +30 -27
  41. package/dest/sequencer/timetable.d.ts +4 -6
  42. package/dest/sequencer/timetable.d.ts.map +1 -1
  43. package/dest/sequencer/timetable.js +7 -11
  44. package/dest/sequencer/types.d.ts +5 -2
  45. package/dest/sequencer/types.d.ts.map +1 -1
  46. package/dest/test/index.d.ts +3 -5
  47. package/dest/test/index.d.ts.map +1 -1
  48. package/dest/test/mock_checkpoint_builder.d.ts +10 -10
  49. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -1
  50. package/dest/test/mock_checkpoint_builder.js +45 -36
  51. package/dest/test/utils.d.ts +3 -3
  52. package/dest/test/utils.d.ts.map +1 -1
  53. package/dest/test/utils.js +5 -4
  54. package/package.json +28 -28
  55. package/src/client/sequencer-client.ts +135 -18
  56. package/src/config.ts +54 -38
  57. package/src/global_variable_builder/global_builder.ts +1 -1
  58. package/src/publisher/config.ts +121 -43
  59. package/src/publisher/index.ts +3 -0
  60. package/src/publisher/l1_tx_failed_store/factory.ts +32 -0
  61. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +55 -0
  62. package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +46 -0
  63. package/src/publisher/l1_tx_failed_store/index.ts +3 -0
  64. package/src/publisher/sequencer-publisher-factory.ts +38 -6
  65. package/src/publisher/sequencer-publisher.ts +333 -60
  66. package/src/sequencer/checkpoint_proposal_job.ts +223 -113
  67. package/src/sequencer/metrics.ts +92 -18
  68. package/src/sequencer/sequencer.ts +40 -32
  69. package/src/sequencer/timetable.ts +13 -12
  70. package/src/sequencer/types.ts +4 -1
  71. package/src/test/index.ts +2 -4
  72. package/src/test/mock_checkpoint_builder.ts +62 -50
  73. package/src/test/utils.ts +5 -2
@@ -1,8 +1,17 @@
1
- import { NUM_CHECKPOINT_END_MARKER_FIELDS, getNumBlockEndBlobFields } from '@aztec/blob-lib/encoding';
2
- import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB } from '@aztec/constants';
3
1
  import type { EpochCache } from '@aztec/epoch-cache';
4
- import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
2
+ import {
3
+ BlockNumber,
4
+ CheckpointNumber,
5
+ EpochNumber,
6
+ IndexWithinCheckpoint,
7
+ SlotNumber,
8
+ } from '@aztec/foundation/branded-types';
5
9
  import { randomInt } from '@aztec/foundation/crypto/random';
10
+ import {
11
+ flipSignature,
12
+ generateRecoverableSignature,
13
+ generateUnrecoverableSignature,
14
+ } from '@aztec/foundation/crypto/secp256k1-signer';
6
15
  import { Fr } from '@aztec/foundation/curves/bn254';
7
16
  import { EthAddress } from '@aztec/foundation/eth-address';
8
17
  import { Signature } from '@aztec/foundation/eth-signature';
@@ -10,7 +19,7 @@ import { filter } from '@aztec/foundation/iterator';
10
19
  import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
11
20
  import { sleep, sleepUntil } from '@aztec/foundation/sleep';
12
21
  import { type DateProvider, Timer } from '@aztec/foundation/timer';
13
- import { type TypedEventEmitter, unfreeze } from '@aztec/foundation/types';
22
+ import { type TypedEventEmitter, isErrorClass, unfreeze } from '@aztec/foundation/types';
14
23
  import type { P2P } from '@aztec/p2p';
15
24
  import type { SlasherClientInterface } from '@aztec/slasher';
16
25
  import {
@@ -21,17 +30,18 @@ import {
21
30
  type L2BlockSource,
22
31
  MaliciousCommitteeAttestationsAndSigners,
23
32
  } from '@aztec/stdlib/block';
24
- import type { Checkpoint } from '@aztec/stdlib/checkpoint';
33
+ import { type Checkpoint, validateCheckpoint } from '@aztec/stdlib/checkpoint';
25
34
  import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
26
35
  import { Gas } from '@aztec/stdlib/gas';
27
- import type {
28
- PublicProcessorLimits,
29
- ResolvedSequencerConfig,
30
- WorldStateSynchronizer,
36
+ import {
37
+ NoValidTxsError,
38
+ type PublicProcessorLimits,
39
+ type ResolvedSequencerConfig,
40
+ type WorldStateSynchronizer,
31
41
  } from '@aztec/stdlib/interfaces/server';
32
42
  import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
33
43
  import type { BlockProposalOptions, CheckpointProposal, CheckpointProposalOptions } from '@aztec/stdlib/p2p';
34
- import { orderAttestations } from '@aztec/stdlib/p2p';
44
+ import { orderAttestations, trimAttestations } from '@aztec/stdlib/p2p';
35
45
  import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
36
46
  import { type FailedTx, Tx } from '@aztec/stdlib/tx';
37
47
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
@@ -122,7 +132,7 @@ export class CheckpointProposalJob implements Traceable {
122
132
  await Promise.all(votesPromises);
123
133
 
124
134
  if (checkpoint) {
125
- this.metrics.recordBlockProposalSuccess();
135
+ this.metrics.recordCheckpointProposalSuccess();
126
136
  }
127
137
 
128
138
  // Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
@@ -179,18 +189,21 @@ export class CheckpointProposalJob implements Traceable {
179
189
  const inHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
180
190
 
181
191
  // Collect the out hashes of all the checkpoints before this one in the same epoch
182
- const previousCheckpoints = (await this.l2BlockSource.getCheckpointsForEpoch(this.epoch)).filter(
183
- c => c.number < this.checkpointNumber,
184
- );
185
- const previousCheckpointOutHashes = previousCheckpoints.map(c => c.getCheckpointOutHash());
192
+ const previousCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(this.epoch))
193
+ .filter(c => c.checkpointNumber < this.checkpointNumber)
194
+ .map(c => c.checkpointOutHash);
195
+
196
+ // Get the fee asset price modifier from the oracle
197
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
186
198
 
187
199
  // Create a long-lived forked world state for the checkpoint builder
188
- using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
200
+ await using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
189
201
 
190
202
  // Create checkpoint builder for the entire slot
191
203
  const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(
192
204
  this.checkpointNumber,
193
205
  checkpointGlobalVariables,
206
+ feeAssetPriceModifier,
194
207
  l1ToL2Messages,
195
208
  previousCheckpointOutHashes,
196
209
  fork,
@@ -210,6 +223,7 @@ export class CheckpointProposalJob implements Traceable {
210
223
 
211
224
  let blocksInCheckpoint: L2Block[] = [];
212
225
  let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
226
+ const checkpointBuildTimer = new Timer();
213
227
 
214
228
  try {
215
229
  // Main loop: build blocks for the checkpoint
@@ -225,19 +239,7 @@ export class CheckpointProposalJob implements Traceable {
225
239
  // These errors are expected in HA mode, so we yield and let another HA node handle the slot
226
240
  // The only distinction between the 2 errors is SlashingProtectionError throws when the payload is different,
227
241
  // which is normal for block building (may have picked different txs)
228
- if (err instanceof DutyAlreadySignedError) {
229
- this.log.info(`Checkpoint proposal for slot ${this.slot} already signed by another HA node, yielding`, {
230
- slot: this.slot,
231
- signedByNode: err.signedByNode,
232
- });
233
- return undefined;
234
- }
235
- if (err instanceof SlashingProtectionError) {
236
- this.log.info(`Checkpoint proposal for slot ${this.slot} blocked by slashing protection, yielding`, {
237
- slot: this.slot,
238
- existingMessageHash: err.existingMessageHash,
239
- attemptedMessageHash: err.attemptedMessageHash,
240
- });
242
+ if (this.handleHASigningError(err, 'Block proposal')) {
241
243
  return undefined;
242
244
  }
243
245
  throw err;
@@ -249,11 +251,44 @@ export class CheckpointProposalJob implements Traceable {
249
251
  return undefined;
250
252
  }
251
253
 
254
+ const minBlocksForCheckpoint = this.config.minBlocksForCheckpoint;
255
+ if (minBlocksForCheckpoint !== undefined && blocksInCheckpoint.length < minBlocksForCheckpoint) {
256
+ this.log.warn(
257
+ `Checkpoint has fewer blocks than minimum (${blocksInCheckpoint.length} < ${minBlocksForCheckpoint}), skipping proposal`,
258
+ { slot: this.slot, blocksBuilt: blocksInCheckpoint.length, minBlocksForCheckpoint },
259
+ );
260
+ return undefined;
261
+ }
262
+
252
263
  // Assemble and broadcast the checkpoint proposal, including the last block that was not
253
264
  // broadcasted yet, and wait to collect the committee attestations.
254
265
  this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.slot);
255
266
  const checkpoint = await checkpointBuilder.completeCheckpoint();
256
267
 
268
+ // Final validation round for the checkpoint before we propose it, just for safety
269
+ try {
270
+ validateCheckpoint(checkpoint, {
271
+ rollupManaLimit: this.l1Constants.rollupManaLimit,
272
+ maxL2BlockGas: this.config.maxL2BlockGas,
273
+ maxDABlockGas: this.config.maxDABlockGas,
274
+ maxTxsPerBlock: this.config.maxTxsPerBlock,
275
+ maxTxsPerCheckpoint: this.config.maxTxsPerCheckpoint,
276
+ });
277
+ } catch (err) {
278
+ this.log.error(`Built an invalid checkpoint at slot ${this.slot} (skipping proposal)`, err, {
279
+ checkpoint: checkpoint.header.toInspect(),
280
+ });
281
+ return undefined;
282
+ }
283
+
284
+ // Record checkpoint-level build metrics
285
+ this.metrics.recordCheckpointBuild(
286
+ checkpointBuildTimer.ms(),
287
+ blocksInCheckpoint.length,
288
+ checkpoint.getStats().txCount,
289
+ Number(checkpoint.header.totalManaUsed.toBigInt()),
290
+ );
291
+
257
292
  // Do not collect attestations nor publish to L1 in fisherman mode
258
293
  if (this.config.fishermanMode) {
259
294
  this.log.info(
@@ -280,6 +315,7 @@ export class CheckpointProposalJob implements Traceable {
280
315
  const proposal = await this.validatorClient.createCheckpointProposal(
281
316
  checkpoint.header,
282
317
  checkpoint.archive.root,
318
+ feeAssetPriceModifier,
283
319
  lastBlock,
284
320
  this.proposer,
285
321
  checkpointProposalOptions,
@@ -306,20 +342,8 @@ export class CheckpointProposalJob implements Traceable {
306
342
  );
307
343
  } catch (err) {
308
344
  // We shouldn't really get here since we yield to another HA node
309
- // as soon as we see these errors when creating block proposals.
310
- if (err instanceof DutyAlreadySignedError) {
311
- this.log.info(`Attestations signature for slot ${this.slot} already signed by another HA node, yielding`, {
312
- slot: this.slot,
313
- signedByNode: err.signedByNode,
314
- });
315
- return undefined;
316
- }
317
- if (err instanceof SlashingProtectionError) {
318
- this.log.info(`Attestations signature for slot ${this.slot} blocked by slashing protection, yielding`, {
319
- slot: this.slot,
320
- existingMessageHash: err.existingMessageHash,
321
- attemptedMessageHash: err.attemptedMessageHash,
322
- });
345
+ // as soon as we see these errors when creating block or checkpoint proposals.
346
+ if (this.handleHASigningError(err, 'Attestations signature')) {
323
347
  return undefined;
324
348
  }
325
349
  throw err;
@@ -330,6 +354,21 @@ export class CheckpointProposalJob implements Traceable {
330
354
  const aztecSlotDuration = this.l1Constants.slotDuration;
331
355
  const slotStartBuildTimestamp = this.getSlotStartBuildTimestamp();
332
356
  const txTimeoutAt = new Date((slotStartBuildTimestamp + aztecSlotDuration) * 1000);
357
+
358
+ // If we have been configured to potentially skip publishing checkpoint then roll the dice here
359
+ if (
360
+ this.config.skipPublishingCheckpointsPercent !== undefined &&
361
+ this.config.skipPublishingCheckpointsPercent > 0
362
+ ) {
363
+ const result = Math.max(0, randomInt(100));
364
+ if (result < this.config.skipPublishingCheckpointsPercent) {
365
+ this.log.warn(
366
+ `Skipping publishing proposal for checkpoint ${checkpoint.number}. Configured percentage: ${this.config.skipPublishingCheckpointsPercent}, generated value: ${result}`,
367
+ );
368
+ return checkpoint;
369
+ }
370
+ }
371
+
333
372
  await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
334
373
  txTimeoutAt,
335
374
  forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber,
@@ -364,15 +403,12 @@ export class CheckpointProposalJob implements Traceable {
364
403
  const txHashesAlreadyIncluded = new Set<string>();
365
404
  const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
366
405
 
367
- // Remaining blob fields available for blocks (checkpoint end marker already subtracted)
368
- let remainingBlobFields = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB - NUM_CHECKPOINT_END_MARKER_FIELDS;
369
-
370
406
  // Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
371
407
  let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
372
408
 
373
409
  while (true) {
374
410
  const blocksBuilt = blocksInCheckpoint.length;
375
- const indexWithinCheckpoint = blocksBuilt;
411
+ const indexWithinCheckpoint = IndexWithinCheckpoint(blocksBuilt);
376
412
  const blockNumber = BlockNumber(initialBlockNumber + blocksBuilt);
377
413
 
378
414
  const secondsIntoSlot = this.getSecondsIntoSlot();
@@ -399,9 +435,9 @@ export class CheckpointProposalJob implements Traceable {
399
435
  blockNumber,
400
436
  indexWithinCheckpoint,
401
437
  txHashesAlreadyIncluded,
402
- remainingBlobFields,
403
438
  });
404
439
 
440
+ // TODO(palla/mbps): Review these conditions. We may want to keep trying in some scenarios.
405
441
  if (!buildResult && timingInfo.isLastBlock) {
406
442
  // If no block was produced due to not enough txs and this was the last subslot, exit
407
443
  break;
@@ -424,12 +460,9 @@ export class CheckpointProposalJob implements Traceable {
424
460
  break;
425
461
  }
426
462
 
427
- const { block, usedTxs, remainingBlobFields: newRemainingBlobFields } = buildResult;
463
+ const { block, usedTxs } = buildResult;
428
464
  blocksInCheckpoint.push(block);
429
465
 
430
- // Update remaining blob fields for the next block
431
- remainingBlobFields = newRemainingBlobFields;
432
-
433
466
  // Sync the proposed block to the archiver to make it available
434
467
  // Note that the checkpoint builder uses its own fork so it should not need to wait for this syncing
435
468
  // Eventually we should refactor the checkpoint builder to not need a separate long-lived fork
@@ -488,27 +521,19 @@ export class CheckpointProposalJob implements Traceable {
488
521
 
489
522
  /** Builds a single block. Called from the main block building loop. */
490
523
  @trackSpan('CheckpointProposalJob.buildSingleBlock')
491
- private async buildSingleBlock(
524
+ protected async buildSingleBlock(
492
525
  checkpointBuilder: CheckpointBuilder,
493
526
  opts: {
494
527
  forceCreate?: boolean;
495
528
  blockTimestamp: bigint;
496
529
  blockNumber: BlockNumber;
497
- indexWithinCheckpoint: number;
530
+ indexWithinCheckpoint: IndexWithinCheckpoint;
498
531
  buildDeadline: Date | undefined;
499
532
  txHashesAlreadyIncluded: Set<string>;
500
- remainingBlobFields: number;
501
533
  },
502
- ): Promise<{ block: L2Block; usedTxs: Tx[]; remainingBlobFields: number } | { error: Error } | undefined> {
503
- const {
504
- blockTimestamp,
505
- forceCreate,
506
- blockNumber,
507
- indexWithinCheckpoint,
508
- buildDeadline,
509
- txHashesAlreadyIncluded,
510
- remainingBlobFields,
511
- } = opts;
534
+ ): Promise<{ block: L2Block; usedTxs: Tx[] } | { error: Error } | undefined> {
535
+ const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded } =
536
+ opts;
512
537
 
513
538
  this.log.verbose(
514
539
  `Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.slot}`,
@@ -532,7 +557,7 @@ export class CheckpointProposalJob implements Traceable {
532
557
  // Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
533
558
  // just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
534
559
  const pendingTxs = filter(
535
- this.p2pClient.iteratePendingTxs(),
560
+ this.p2pClient.iterateEligiblePendingTxs(),
536
561
  tx => !txHashesAlreadyIncluded.has(tx.txHash.toString()),
537
562
  );
538
563
 
@@ -542,64 +567,57 @@ export class CheckpointProposalJob implements Traceable {
542
567
  );
543
568
  this.setStateFn(SequencerState.CREATING_BLOCK, this.slot);
544
569
 
545
- // Calculate blob fields limit for txs (remaining capacity - this block's end overhead)
546
- const blockEndOverhead = getNumBlockEndBlobFields(indexWithinCheckpoint === 0);
547
- const maxBlobFieldsForTxs = remainingBlobFields - blockEndOverhead;
548
-
570
+ // Per-block limits derived at startup by computeBlockLimits(), further capped
571
+ // by remaining checkpoint-level budgets inside CheckpointBuilder before each block is built.
549
572
  const blockBuilderOptions: PublicProcessorLimits = {
550
573
  maxTransactions: this.config.maxTxsPerBlock,
551
- maxBlockSize: this.config.maxBlockSizeInBytes,
552
- maxBlockGas: new Gas(this.config.maxDABlockGas, this.config.maxL2BlockGas),
553
- maxBlobFields: maxBlobFieldsForTxs,
574
+ maxBlockGas:
575
+ this.config.maxL2BlockGas !== undefined || this.config.maxDABlockGas !== undefined
576
+ ? new Gas(this.config.maxDABlockGas ?? Infinity, this.config.maxL2BlockGas ?? Infinity)
577
+ : undefined,
554
578
  deadline: buildDeadline,
579
+ isBuildingProposal: true,
555
580
  };
556
581
 
557
582
  // Actually build the block by executing txs
558
- const workTimer = new Timer();
559
- const {
560
- publicGas,
561
- block,
562
- publicProcessorDuration,
563
- numTxs,
564
- blockBuildingTimer,
565
- usedTxs,
566
- failedTxs,
567
- usedTxBlobFields,
568
- } = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
569
- const blockBuildDuration = workTimer.ms();
583
+ const buildResult = await this.buildSingleBlockWithCheckpointBuilder(
584
+ checkpointBuilder,
585
+ pendingTxs,
586
+ blockNumber,
587
+ blockTimestamp,
588
+ blockBuilderOptions,
589
+ );
570
590
 
571
591
  // If any txs failed during execution, drop them from the mempool so we don't pick them up again
572
- await this.dropFailedTxsFromP2P(failedTxs);
592
+ await this.dropFailedTxsFromP2P(buildResult.failedTxs);
573
593
 
574
594
  // Check if we have created a block with enough txs. If there were invalid txs in the pool, or if execution took
575
595
  // too long, then we may not get to minTxsPerBlock after executing public functions.
576
596
  const minValidTxs = this.config.minValidTxsPerBlock ?? minTxs;
577
- if (!forceCreate && numTxs < minValidTxs) {
597
+ const numTxs = buildResult.status === 'no-valid-txs' ? 0 : buildResult.numTxs;
598
+ if (buildResult.status === 'no-valid-txs' || (!forceCreate && numTxs < minValidTxs)) {
578
599
  this.log.warn(
579
- `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed (got ${numTxs} but required ${minValidTxs})`,
580
- { slot: this.slot, blockNumber, numTxs, indexWithinCheckpoint },
600
+ `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed`,
601
+ { slot: this.slot, blockNumber, numTxs, indexWithinCheckpoint, minValidTxs, buildResult: buildResult.status },
581
602
  );
582
- this.eventEmitter.emit('block-tx-count-check-failed', {
583
- minTxs: minValidTxs,
584
- availableTxs: numTxs,
585
- slot: this.slot,
586
- });
603
+ this.eventEmitter.emit('block-build-failed', { reason: `Insufficient valid txs`, slot: this.slot });
587
604
  this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
588
605
  return undefined;
589
606
  }
590
607
 
591
608
  // Block creation succeeded, emit stats and metrics
609
+ const { block, publicProcessorDuration, usedTxs, blockBuildDuration } = buildResult;
610
+
592
611
  const blockStats = {
593
612
  eventName: 'l2-block-built',
594
613
  duration: blockBuildDuration,
595
614
  publicProcessDuration: publicProcessorDuration,
596
- rollupCircuitsDuration: blockBuildingTimer.ms(),
597
615
  ...block.getStats(),
598
616
  } satisfies L2BlockBuiltStats;
599
617
 
600
618
  const blockHash = await block.hash();
601
619
  const txHashes = block.body.txEffects.map(tx => tx.txHash);
602
- const manaPerSec = publicGas.l2Gas / (blockBuildDuration / 1000);
620
+ const manaPerSec = block.header.totalManaUsed.toNumberUnsafe() / (blockBuildDuration / 1000);
603
621
 
604
622
  this.log.info(
605
623
  `Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.slot} with ${numTxs} txs`,
@@ -607,9 +625,9 @@ export class CheckpointProposalJob implements Traceable {
607
625
  );
608
626
 
609
627
  this.eventEmitter.emit('block-proposed', { blockNumber: block.number, slot: this.slot });
610
- this.metrics.recordBuiltBlock(blockBuildDuration, publicGas.l2Gas);
628
+ this.metrics.recordBuiltBlock(blockBuildDuration, block.header.totalManaUsed.toNumberUnsafe());
611
629
 
612
- return { block, usedTxs, remainingBlobFields: maxBlobFieldsForTxs - usedTxBlobFields };
630
+ return { block, usedTxs };
613
631
  } catch (err: any) {
614
632
  this.eventEmitter.emit('block-build-failed', { reason: err.message, slot: this.slot });
615
633
  this.log.error(`Error building block`, err, { blockNumber, slot: this.slot });
@@ -619,17 +637,40 @@ export class CheckpointProposalJob implements Traceable {
619
637
  }
620
638
  }
621
639
 
640
+ /** Uses the checkpoint builder to build a block, catching specific txs */
641
+ private async buildSingleBlockWithCheckpointBuilder(
642
+ checkpointBuilder: CheckpointBuilder,
643
+ pendingTxs: AsyncIterable<Tx>,
644
+ blockNumber: BlockNumber,
645
+ blockTimestamp: bigint,
646
+ blockBuilderOptions: PublicProcessorLimits,
647
+ ) {
648
+ try {
649
+ const workTimer = new Timer();
650
+ const result = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
651
+ const blockBuildDuration = workTimer.ms();
652
+ return { ...result, blockBuildDuration, status: 'success' as const };
653
+ } catch (err: unknown) {
654
+ if (isErrorClass(err, NoValidTxsError)) {
655
+ return { failedTxs: err.failedTxs, status: 'no-valid-txs' as const };
656
+ }
657
+ throw err;
658
+ }
659
+ }
660
+
622
661
  /** Waits until minTxs are available on the pool for building a block. */
623
662
  @trackSpan('CheckpointProposalJob.waitForMinTxs')
624
663
  private async waitForMinTxs(opts: {
625
664
  forceCreate?: boolean;
626
665
  blockNumber: BlockNumber;
627
- indexWithinCheckpoint: number;
666
+ indexWithinCheckpoint: IndexWithinCheckpoint;
628
667
  buildDeadline: Date | undefined;
629
668
  }): Promise<{ canStartBuilding: boolean; availableTxs: number }> {
630
- const minTxs = this.config.minTxsPerBlock;
631
669
  const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
632
670
 
671
+ // We only allow a block with 0 txs in the first block of the checkpoint
672
+ const minTxs = indexWithinCheckpoint > 0 && this.config.minTxsPerBlock === 0 ? 1 : this.config.minTxsPerBlock;
673
+
633
674
  // Deadline is undefined if we are not enforcing the timetable, meaning we'll exit immediately when out of time
634
675
  const startBuildingDeadline = buildDeadline
635
676
  ? new Date(buildDeadline.getTime() - this.timetable.minExecutionTime * 1000)
@@ -650,7 +691,7 @@ export class CheckpointProposalJob implements Traceable {
650
691
  `Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (have ${availableTxs} but need ${minTxs})`,
651
692
  { blockNumber, slot: this.slot, indexWithinCheckpoint },
652
693
  );
653
- await sleep(TXS_POLLING_MS);
694
+ await this.waitForTxsPollingInterval();
654
695
  availableTxs = await this.p2pClient.getPendingTxCount();
655
696
  }
656
697
 
@@ -706,11 +747,28 @@ export class CheckpointProposalJob implements Traceable {
706
747
 
707
748
  collectedAttestationsCount = attestations.length;
708
749
 
750
+ // Trim attestations to minimum required to save L1 calldata gas
751
+ const localAddresses = this.validatorClient.getValidatorAddresses();
752
+ const trimmed = trimAttestations(
753
+ attestations,
754
+ numberOfRequiredAttestations,
755
+ this.attestorAddress,
756
+ localAddresses,
757
+ );
758
+ if (trimmed.length < attestations.length) {
759
+ this.log.debug(`Trimmed attestations from ${attestations.length} to ${trimmed.length} for L1 submission`);
760
+ }
761
+
709
762
  // Rollup contract requires that the signatures are provided in the order of the committee
710
- const sorted = orderAttestations(attestations, committee);
763
+ const sorted = orderAttestations(trimmed, committee);
711
764
 
712
765
  // Manipulate the attestations if we've been configured to do so
713
- if (this.config.injectFakeAttestation || this.config.shuffleAttestationOrdering) {
766
+ if (
767
+ this.config.injectFakeAttestation ||
768
+ this.config.injectHighSValueAttestation ||
769
+ this.config.injectUnrecoverableSignatureAttestation ||
770
+ this.config.shuffleAttestationOrdering
771
+ ) {
714
772
  return this.manipulateAttestations(proposal.slotNumber, epoch, seed, committee, sorted);
715
773
  }
716
774
 
@@ -739,7 +797,11 @@ export class CheckpointProposalJob implements Traceable {
739
797
  this.epochCache.computeProposerIndex(slotNumber, epoch, seed, BigInt(committee.length)),
740
798
  );
741
799
 
742
- if (this.config.injectFakeAttestation) {
800
+ if (
801
+ this.config.injectFakeAttestation ||
802
+ this.config.injectHighSValueAttestation ||
803
+ this.config.injectUnrecoverableSignatureAttestation
804
+ ) {
743
805
  // Find non-empty attestations that are not from the proposer
744
806
  const nonProposerIndices: number[] = [];
745
807
  for (let i = 0; i < attestations.length; i++) {
@@ -749,8 +811,20 @@ export class CheckpointProposalJob implements Traceable {
749
811
  }
750
812
  if (nonProposerIndices.length > 0) {
751
813
  const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
752
- this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
753
- unfreeze(attestations[targetIndex]).signature = Signature.random();
814
+ if (this.config.injectHighSValueAttestation) {
815
+ this.log.warn(
816
+ `Injecting high-s value attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
817
+ );
818
+ unfreeze(attestations[targetIndex]).signature = flipSignature(attestations[targetIndex].signature);
819
+ } else if (this.config.injectUnrecoverableSignatureAttestation) {
820
+ this.log.warn(
821
+ `Injecting unrecoverable signature attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
822
+ );
823
+ unfreeze(attestations[targetIndex]).signature = generateUnrecoverableSignature();
824
+ } else {
825
+ this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
826
+ unfreeze(attestations[targetIndex]).signature = generateRecoverableSignature();
827
+ }
754
828
  }
755
829
  return new CommitteeAttestationsAndSigners(attestations);
756
830
  }
@@ -759,11 +833,20 @@ export class CheckpointProposalJob implements Traceable {
759
833
  this.log.warn(`Shuffling attestation ordering in checkpoint for slot ${slotNumber} (proposer #${proposerIndex})`);
760
834
 
761
835
  const shuffled = [...attestations];
762
- const [i, j] = [(proposerIndex + 1) % shuffled.length, (proposerIndex + 2) % shuffled.length];
763
- const valueI = shuffled[i];
764
- const valueJ = shuffled[j];
765
- shuffled[i] = valueJ;
766
- shuffled[j] = valueI;
836
+
837
+ // Find two non-proposer positions that both have non-empty signatures to swap.
838
+ // This ensures the bitmap doesn't change, so the MaliciousCommitteeAttestationsAndSigners
839
+ // signers array stays correctly aligned with L1's committee reconstruction.
840
+ const swappable: number[] = [];
841
+ for (let k = 0; k < shuffled.length; k++) {
842
+ if (!shuffled[k].signature.isEmpty() && k !== proposerIndex) {
843
+ swappable.push(k);
844
+ }
845
+ }
846
+ if (swappable.length >= 2) {
847
+ const [i, j] = [swappable[0], swappable[1]];
848
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
849
+ }
767
850
 
768
851
  const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
769
852
  return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
@@ -779,7 +862,7 @@ export class CheckpointProposalJob implements Traceable {
779
862
  const failedTxData = failedTxs.map(fail => fail.tx);
780
863
  const failedTxHashes = failedTxData.map(tx => tx.getTxHash());
781
864
  this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
782
- await this.p2pClient.deleteTxs(failedTxHashes);
865
+ await this.p2pClient.handleFailedExecution(failedTxHashes);
783
866
  }
784
867
 
785
868
  /**
@@ -821,12 +904,34 @@ export class CheckpointProposalJob implements Traceable {
821
904
  slot: this.slot,
822
905
  feeAnalysisId: feeAnalysis?.id,
823
906
  });
824
- this.metrics.recordBlockProposalFailed('block_build_failed');
907
+ this.metrics.recordCheckpointProposalFailed('block_build_failed');
825
908
  }
826
909
 
827
910
  this.publisher.clearPendingRequests();
828
911
  }
829
912
 
913
+ /**
914
+ * Helper to handle HA double-signing errors. Returns true if the error was handled (caller should yield).
915
+ */
916
+ private handleHASigningError(err: any, errorContext: string): boolean {
917
+ if (err instanceof DutyAlreadySignedError) {
918
+ this.log.info(`${errorContext} for slot ${this.slot} already signed by another HA node, yielding`, {
919
+ slot: this.slot,
920
+ signedByNode: err.signedByNode,
921
+ });
922
+ return true;
923
+ }
924
+ if (err instanceof SlashingProtectionError) {
925
+ this.log.info(`${errorContext} for slot ${this.slot} blocked by slashing protection, yielding`, {
926
+ slot: this.slot,
927
+ existingMessageHash: err.existingMessageHash,
928
+ attemptedMessageHash: err.attemptedMessageHash,
929
+ });
930
+ return true;
931
+ }
932
+ return false;
933
+ }
934
+
830
935
  /** Waits until a specific time within the current slot */
831
936
  @trackSpan('CheckpointProposalJob.waitUntilTimeInSlot')
832
937
  protected async waitUntilTimeInSlot(targetSecondsIntoSlot: number): Promise<void> {
@@ -835,6 +940,11 @@ export class CheckpointProposalJob implements Traceable {
835
940
  await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
836
941
  }
837
942
 
943
+ /** Waits the polling interval for transactions. Extracted for test overriding. */
944
+ protected async waitForTxsPollingInterval(): Promise<void> {
945
+ await sleep(TXS_POLLING_MS);
946
+ }
947
+
838
948
  private getSlotStartBuildTimestamp(): number {
839
949
  return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
840
950
  }