@aztec/sequencer-client 0.0.1-commit.e6bd8901 → 0.0.1-commit.ec7ac5448

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 (93) hide show
  1. package/dest/client/sequencer-client.d.ts +17 -7
  2. package/dest/client/sequencer-client.d.ts.map +1 -1
  3. package/dest/client/sequencer-client.js +63 -30
  4. package/dest/config.d.ts +26 -7
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +47 -33
  7. package/dest/global_variable_builder/global_builder.d.ts +15 -10
  8. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  9. package/dest/global_variable_builder/global_builder.js +26 -26
  10. package/dest/global_variable_builder/index.d.ts +2 -2
  11. package/dest/global_variable_builder/index.d.ts.map +1 -1
  12. package/dest/publisher/config.d.ts +47 -17
  13. package/dest/publisher/config.d.ts.map +1 -1
  14. package/dest/publisher/config.js +121 -42
  15. package/dest/publisher/index.d.ts +2 -1
  16. package/dest/publisher/index.d.ts.map +1 -1
  17. package/dest/publisher/l1_tx_failed_store/factory.d.ts +11 -0
  18. package/dest/publisher/l1_tx_failed_store/factory.d.ts.map +1 -0
  19. package/dest/publisher/l1_tx_failed_store/factory.js +22 -0
  20. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +59 -0
  21. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -0
  22. package/dest/publisher/l1_tx_failed_store/failed_tx_store.js +1 -0
  23. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts +15 -0
  24. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts.map +1 -0
  25. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.js +34 -0
  26. package/dest/publisher/l1_tx_failed_store/index.d.ts +4 -0
  27. package/dest/publisher/l1_tx_failed_store/index.d.ts.map +1 -0
  28. package/dest/publisher/l1_tx_failed_store/index.js +2 -0
  29. package/dest/publisher/sequencer-publisher-factory.d.ts +11 -5
  30. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  31. package/dest/publisher/sequencer-publisher-factory.js +27 -3
  32. package/dest/publisher/sequencer-publisher-metrics.d.ts +1 -1
  33. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  34. package/dest/publisher/sequencer-publisher-metrics.js +12 -4
  35. package/dest/publisher/sequencer-publisher.d.ts +75 -49
  36. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  37. package/dest/publisher/sequencer-publisher.js +470 -156
  38. package/dest/sequencer/chain_state_overrides.d.ts +25 -0
  39. package/dest/sequencer/chain_state_overrides.d.ts.map +1 -0
  40. package/dest/sequencer/chain_state_overrides.js +39 -0
  41. package/dest/sequencer/checkpoint_proposal_job.d.ts +51 -13
  42. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  43. package/dest/sequencer/checkpoint_proposal_job.js +414 -239
  44. package/dest/sequencer/checkpoint_voter.d.ts +1 -2
  45. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -1
  46. package/dest/sequencer/checkpoint_voter.js +2 -5
  47. package/dest/sequencer/events.d.ts +2 -1
  48. package/dest/sequencer/events.d.ts.map +1 -1
  49. package/dest/sequencer/metrics.d.ts +25 -6
  50. package/dest/sequencer/metrics.d.ts.map +1 -1
  51. package/dest/sequencer/metrics.js +134 -31
  52. package/dest/sequencer/sequencer.d.ts +44 -17
  53. package/dest/sequencer/sequencer.d.ts.map +1 -1
  54. package/dest/sequencer/sequencer.js +157 -94
  55. package/dest/sequencer/timetable.d.ts +17 -6
  56. package/dest/sequencer/timetable.d.ts.map +1 -1
  57. package/dest/sequencer/timetable.js +51 -46
  58. package/dest/sequencer/types.d.ts +2 -2
  59. package/dest/sequencer/types.d.ts.map +1 -1
  60. package/dest/test/index.d.ts +3 -5
  61. package/dest/test/index.d.ts.map +1 -1
  62. package/dest/test/mock_checkpoint_builder.d.ts +14 -11
  63. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -1
  64. package/dest/test/mock_checkpoint_builder.js +63 -40
  65. package/dest/test/utils.d.ts +3 -3
  66. package/dest/test/utils.d.ts.map +1 -1
  67. package/dest/test/utils.js +5 -4
  68. package/package.json +27 -28
  69. package/src/client/sequencer-client.ts +81 -30
  70. package/src/config.ts +65 -43
  71. package/src/global_variable_builder/global_builder.ts +31 -28
  72. package/src/global_variable_builder/index.ts +1 -1
  73. package/src/publisher/config.ts +153 -43
  74. package/src/publisher/index.ts +3 -0
  75. package/src/publisher/l1_tx_failed_store/factory.ts +32 -0
  76. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +55 -0
  77. package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +46 -0
  78. package/src/publisher/l1_tx_failed_store/index.ts +3 -0
  79. package/src/publisher/sequencer-publisher-factory.ts +38 -9
  80. package/src/publisher/sequencer-publisher-metrics.ts +7 -3
  81. package/src/publisher/sequencer-publisher.ts +531 -214
  82. package/src/sequencer/README.md +83 -13
  83. package/src/sequencer/chain_state_overrides.ts +87 -0
  84. package/src/sequencer/checkpoint_proposal_job.ts +547 -274
  85. package/src/sequencer/checkpoint_voter.ts +1 -12
  86. package/src/sequencer/events.ts +1 -1
  87. package/src/sequencer/metrics.ts +153 -33
  88. package/src/sequencer/sequencer.ts +228 -107
  89. package/src/sequencer/timetable.ts +70 -57
  90. package/src/sequencer/types.ts +1 -1
  91. package/src/test/index.ts +2 -4
  92. package/src/test/mock_checkpoint_builder.ts +84 -56
  93. package/src/test/utils.ts +5 -2
@@ -436,23 +436,25 @@ function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
436
436
  return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
437
437
  }
438
438
  var _dec, _dec1, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _initProto;
439
- import { NUM_CHECKPOINT_END_MARKER_FIELDS, getNumBlockEndBlobFields } from '@aztec/blob-lib/encoding';
440
- import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB } from '@aztec/constants';
441
- import { BlockNumber } from '@aztec/foundation/branded-types';
439
+ import { BlockNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
442
440
  import { randomInt } from '@aztec/foundation/crypto/random';
443
- import { Signature } from '@aztec/foundation/eth-signature';
441
+ import { flipSignature, generateRecoverableSignature, generateUnrecoverableSignature } from '@aztec/foundation/crypto/secp256k1-signer';
444
442
  import { filter } from '@aztec/foundation/iterator';
443
+ import { createLogger } from '@aztec/foundation/log';
445
444
  import { sleep, sleepUntil } from '@aztec/foundation/sleep';
446
445
  import { Timer } from '@aztec/foundation/timer';
447
- import { unfreeze } from '@aztec/foundation/types';
446
+ import { isErrorClass, unfreeze } from '@aztec/foundation/types';
448
447
  import { CommitteeAttestationsAndSigners, MaliciousCommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
449
- import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
448
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
449
+ import { computeQuorum, getSlotStartBuildTimestamp, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
450
450
  import { Gas } from '@aztec/stdlib/gas';
451
+ import { InsufficientValidTxsError } from '@aztec/stdlib/interfaces/server';
451
452
  import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
452
- import { orderAttestations } from '@aztec/stdlib/p2p';
453
+ import { orderAttestations, trimAttestations } from '@aztec/stdlib/p2p';
453
454
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
454
455
  import { Attributes, trackSpan } from '@aztec/telemetry-client';
455
456
  import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
457
+ import { buildPipelinedParentSimulationOverridesPlan, buildSubmissionSimulationOverridesPlan } from './chain_state_overrides.js';
456
458
  import { CheckpointVoter } from './checkpoint_voter.js';
457
459
  import { SequencerInterruptedError } from './errors.js';
458
460
  import { SequencerState } from './utils.js';
@@ -461,7 +463,7 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
461
463
  return {
462
464
  // nullish operator needed for tests
463
465
  [Attributes.COINBASE]: this.validatorClient.getCoinbaseForAttestor(this.attestorAddress)?.toString(),
464
- [Attributes.SLOT_NUMBER]: this.slot
466
+ [Attributes.SLOT_NUMBER]: this.targetSlot
465
467
  };
466
468
  }), _dec2 = trackSpan('CheckpointProposalJob.buildBlocksForCheckpoint'), _dec3 = trackSpan('CheckpointProposalJob.waitUntilNextSubslot'), _dec4 = trackSpan('CheckpointProposalJob.buildSingleBlock'), _dec5 = trackSpan('CheckpointProposalJob.waitForMinTxs'), _dec6 = trackSpan('CheckpointProposalJob.waitForAttestations'), _dec7 = trackSpan('CheckpointProposalJob.waitUntilTimeInSlot');
467
469
  /**
@@ -470,8 +472,9 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
470
472
  * as well as enqueueing votes for slashing and governance proposals. This class is created from
471
473
  * the Sequencer once the check for being the proposer for the slot has succeeded.
472
474
  */ export class CheckpointProposalJob {
473
- epoch;
474
- slot;
475
+ slotNow;
476
+ targetSlot;
477
+ targetEpoch;
475
478
  checkpointNumber;
476
479
  syncedToBlockNumber;
477
480
  proposer;
@@ -495,8 +498,8 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
495
498
  metrics;
496
499
  eventEmitter;
497
500
  setStateFn;
498
- log;
499
501
  tracer;
502
+ proposedCheckpointData;
500
503
  static{
501
504
  ({ e: [_initProto] } = _apply_decs_2203_r(this, [
502
505
  [
@@ -541,10 +544,14 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
541
544
  ]
542
545
  ], []));
543
546
  }
544
- constructor(epoch, slot, checkpointNumber, syncedToBlockNumber, // TODO(palla/mbps): Can we remove the proposer in favor of attestorAddress? Need to check fisherman-node flows.
545
- proposer, publisher, attestorAddress, invalidateCheckpoint, validatorClient, globalsBuilder, p2pClient, worldState, l1ToL2MessageSource, l2BlockSource, checkpointsBuilder, blockSink, l1Constants, config, timetable, slasherClient, epochCache, dateProvider, metrics, eventEmitter, setStateFn, log, tracer){
546
- this.epoch = epoch;
547
- this.slot = slot;
547
+ log;
548
+ /** Tracks the fire-and-forget L1 submission promise so it can be awaited during shutdown. */ pendingL1Submission;
549
+ /** Pipelined parent chain state used while building and later submitting this checkpoint. */ pipelinedParentSimulationOverridesPlan;
550
+ constructor(slotNow, targetSlot, targetEpoch, checkpointNumber, syncedToBlockNumber, // TODO(palla/mbps): Can we remove the proposer in favor of attestorAddress? Need to check fisherman-node flows.
551
+ proposer, publisher, attestorAddress, invalidateCheckpoint, validatorClient, globalsBuilder, p2pClient, worldState, l1ToL2MessageSource, l2BlockSource, checkpointsBuilder, blockSink, l1Constants, config, timetable, slasherClient, epochCache, dateProvider, metrics, eventEmitter, setStateFn, tracer, bindings, proposedCheckpointData){
552
+ this.slotNow = slotNow;
553
+ this.targetSlot = targetSlot;
554
+ this.targetEpoch = targetEpoch;
548
555
  this.checkpointNumber = checkpointNumber;
549
556
  this.syncedToBlockNumber = syncedToBlockNumber;
550
557
  this.proposer = proposer;
@@ -568,49 +575,140 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
568
575
  this.metrics = metrics;
569
576
  this.eventEmitter = eventEmitter;
570
577
  this.setStateFn = setStateFn;
571
- this.log = log;
572
578
  this.tracer = tracer;
579
+ this.proposedCheckpointData = proposedCheckpointData;
573
580
  _initProto(this);
581
+ this.log = createLogger('sequencer:checkpoint-proposal', {
582
+ ...bindings,
583
+ instanceId: `slot-${this.slotNow}`
584
+ });
585
+ }
586
+ /** Awaits the pending L1 submission if one is in progress. Call during shutdown. */ async awaitPendingSubmission() {
587
+ this.log.info('Awaiting pending L1 payload submission');
588
+ await this.pendingL1Submission;
574
589
  }
575
590
  /**
576
591
  * Executes the checkpoint proposal job.
577
- * Returns the published checkpoint if successful, undefined otherwise.
592
+ * Builds blocks, assembles checkpoint, and broadcasts the proposal (blocking).
593
+ * Attestation collection, signing, and L1 submission are backgrounded so the
594
+ * work loop can return to IDLE immediately for consecutive slot proposals.
595
+ * Returns the built checkpoint if successful, undefined otherwise.
578
596
  */ async execute() {
579
597
  // Enqueue governance and slashing votes (returns promises that will be awaited later)
580
598
  // In fisherman mode, we simulate slashing but don't actually publish to L1
581
599
  // These are constant for the whole slot, so we only enqueue them once
582
- const votesPromises = new CheckpointVoter(this.slot, this.publisher, this.attestorAddress, this.validatorClient, this.slasherClient, this.l1Constants, this.config, this.metrics, this.log).enqueueVotes();
583
- // Build and propose the checkpoint. This will enqueue the request on the publisher if a checkpoint is built.
584
- const checkpoint = await this.proposeCheckpoint();
585
- // Wait until the voting promises have resolved, so all requests are enqueued (not sent)
586
- await Promise.all(votesPromises);
587
- if (checkpoint) {
588
- this.metrics.recordBlockProposalSuccess();
600
+ const votesPromises = new CheckpointVoter(this.targetSlot, this.publisher, this.attestorAddress, this.validatorClient, this.slasherClient, this.l1Constants, this.config, this.metrics, this.log).enqueueVotes();
601
+ // Build blocks, assemble checkpoint, and broadcast proposal (BLOCKING).
602
+ // Returns after broadcast — attestation collection is deferred.
603
+ const broadcast = await this.proposeCheckpoint();
604
+ if (!broadcast) {
605
+ await Promise.all(votesPromises);
606
+ // Still submit votes even without a checkpoint
607
+ if (!this.config.fishermanMode) {
608
+ this.pendingL1Submission = this.publisher.sendRequestsAt(this.dateProvider.nowAsDate()).then(()=>{});
609
+ }
610
+ return undefined;
589
611
  }
612
+ const { checkpoint } = broadcast;
613
+ this.metrics.recordCheckpointProposalSuccess();
590
614
  // Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
591
615
  if (this.config.fishermanMode) {
592
616
  await this.handleCheckpointEndAsFisherman(checkpoint);
593
- return;
617
+ return checkpoint;
594
618
  }
595
- // Then send everything to L1
596
- const l1Response = await this.publisher.sendRequests();
597
- const proposedAction = l1Response?.successfulActions.find((a)=>a === 'propose');
598
- if (proposedAction) {
599
- this.eventEmitter.emit('checkpoint-published', {
600
- checkpoint: this.checkpointNumber,
601
- slot: this.slot
619
+ // Background the attestation signing → L1 pipeline so the work loop is unblocked
620
+ this.pendingL1Submission = this.waitForAttestationsAndEnqueueSubmissionAsync(broadcast, votesPromises);
621
+ // Return the built checkpoint immediately — the work loop is now unblocked
622
+ return checkpoint;
623
+ }
624
+ /**
625
+ * Background pipeline: collects attestations, signs them, enqueues the checkpoint, and submits to L1.
626
+ * Runs as a fire-and-forget task stored in `pendingL1Submission` so the work loop is unblocked.
627
+ */ async waitForAttestationsAndEnqueueSubmissionAsync(broadcast, votesPromises) {
628
+ const { checkpoint, proposal, blockProposedAt } = broadcast;
629
+ try {
630
+ await Promise.all(votesPromises);
631
+ this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.targetSlot);
632
+ const attestations = await this.waitForAttestations(proposal);
633
+ this.metrics.recordCheckpointAttestationDelay(this.dateProvider.now() - blockProposedAt);
634
+ // Proposer must sign over the attestations before pushing them to L1
635
+ const signer = this.proposer ?? this.publisher.getSenderAddress();
636
+ let attestationsSignature;
637
+ try {
638
+ attestationsSignature = await this.validatorClient.signAttestationsAndSigners(attestations, signer, this.targetSlot, this.checkpointNumber);
639
+ } catch (err) {
640
+ if (this.handleHASigningError(err, 'Attestations signature')) {
641
+ return;
642
+ }
643
+ throw err;
644
+ }
645
+ // Enqueue the checkpoint for L1 submission
646
+ await this.enqueueCheckpointForSubmission({
647
+ checkpoint,
648
+ attestations,
649
+ attestationsSignature
602
650
  });
603
- const coinbase = checkpoint?.header.coinbase;
604
- await this.metrics.incFilledSlot(this.publisher.getSenderAddress().toString(), coinbase);
605
- return checkpoint;
606
- } else if (checkpoint) {
651
+ // Compute the earliest time to submit: pipeline slot start when pipelining, now otherwise.
652
+ const submitAfter = this.epochCache.isProposerPipeliningEnabled() ? new Date(Number(getTimestampForSlot(this.targetSlot, this.l1Constants)) * 1000) : new Date(this.dateProvider.now());
653
+ const l1Response = await this.publisher.sendRequestsAt(submitAfter);
654
+ const proposedAction = l1Response?.successfulActions.find((a)=>a === 'propose');
655
+ if (proposedAction) {
656
+ this.eventEmitter.emit('checkpoint-published', {
657
+ checkpoint: this.checkpointNumber,
658
+ slot: this.targetSlot
659
+ });
660
+ const coinbase = checkpoint.header.coinbase;
661
+ await this.metrics.incFilledSlot(this.publisher.getSenderAddress().toString(), coinbase);
662
+ } else {
663
+ this.eventEmitter.emit('checkpoint-publish-failed', {
664
+ ...l1Response,
665
+ slot: this.targetSlot
666
+ });
667
+ if (this.epochCache.isProposerPipeliningEnabled()) {
668
+ this.metrics.recordPipelineDiscard();
669
+ }
670
+ }
671
+ } catch (err) {
672
+ if (err instanceof SequencerInterruptedError) {
673
+ return;
674
+ }
675
+ this.log.error(`Background attestation/L1 pipeline failed for slot ${this.targetSlot}`, err);
607
676
  this.eventEmitter.emit('checkpoint-publish-failed', {
608
- ...l1Response,
609
- slot: this.slot
677
+ slot: this.targetSlot
610
678
  });
611
- return undefined;
679
+ if (this.epochCache.isProposerPipeliningEnabled()) {
680
+ this.metrics.recordPipelineDiscard();
681
+ }
612
682
  }
613
683
  }
684
+ /** Enqueues the checkpoint for L1 submission. Called after pipeline sleep in execute(). */ async enqueueCheckpointForSubmission(result) {
685
+ const { checkpoint, attestations, attestationsSignature } = result;
686
+ this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.targetSlot);
687
+ const aztecSlotDuration = this.l1Constants.slotDuration;
688
+ const submissionSlotStart = Number(getTimestampForSlot(this.targetSlot, this.l1Constants));
689
+ const txTimeoutAt = new Date((submissionSlotStart + aztecSlotDuration) * 1000);
690
+ // If we have been configured to potentially skip publishing checkpoint then roll the dice here
691
+ if (this.config.skipPublishingCheckpointsPercent !== undefined && this.config.skipPublishingCheckpointsPercent > 0) {
692
+ const roll = Math.max(0, randomInt(100));
693
+ if (roll < this.config.skipPublishingCheckpointsPercent) {
694
+ this.log.warn(`Skipping publishing proposal for checkpoint ${checkpoint.number}. Configured percentage: ${this.config.skipPublishingCheckpointsPercent}, generated value: ${roll}`);
695
+ return;
696
+ }
697
+ }
698
+ const isPipelining = this.epochCache.isProposerPipeliningEnabled();
699
+ const submissionSimulationOverridesPlan = buildSubmissionSimulationOverridesPlan({
700
+ pipelinedParentPlan: this.pipelinedParentSimulationOverridesPlan,
701
+ invalidateToPendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber,
702
+ lastArchiveRoot: checkpoint.header.lastArchiveRoot,
703
+ pipeliningEnabled: isPipelining
704
+ });
705
+ await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
706
+ txTimeoutAt,
707
+ ...submissionSimulationOverridesPlan ? {
708
+ simulationOverridesPlan: submissionSimulationOverridesPlan
709
+ } : {}
710
+ });
711
+ }
614
712
  async proposeCheckpoint() {
615
713
  try {
616
714
  const env = {
@@ -623,25 +721,42 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
623
721
  const coinbase = this.validatorClient.getCoinbaseForAttestor(this.attestorAddress);
624
722
  const feeRecipient = this.validatorClient.getFeeRecipientForAttestor(this.attestorAddress);
625
723
  // Start the checkpoint
626
- this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.slot);
627
- this.metrics.incOpenSlot(this.slot, this.proposer?.toString() ?? 'unknown');
724
+ this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.targetSlot);
725
+ this.log.info(`Starting checkpoint proposal`, {
726
+ buildSlot: this.slotNow,
727
+ submissionSlot: this.targetSlot,
728
+ pipelining: this.epochCache.isProposerPipeliningEnabled(),
729
+ proposer: this.proposer?.toString(),
730
+ coinbase: coinbase.toString()
731
+ });
732
+ this.metrics.incOpenSlot(this.targetSlot, this.proposer?.toString() ?? 'unknown');
628
733
  // Enqueues checkpoint invalidation (constant for the whole slot)
629
734
  if (this.invalidateCheckpoint && !this.config.skipInvalidateBlockAsProposer) {
630
735
  this.publisher.enqueueInvalidateCheckpoint(this.invalidateCheckpoint);
631
736
  }
632
- // Create checkpoint builder for the slot
633
- const checkpointGlobalVariables = await this.globalsBuilder.buildCheckpointGlobalVariables(coinbase, feeRecipient, this.slot);
737
+ // Create checkpoint builder for the slot.
738
+ // When pipelining, force the proposed checkpoint number and fee header to our parent so the
739
+ // fee computation sees the same chain tip that L1 will see once the previous pipelined checkpoint lands.
740
+ const isPipelining = this.epochCache.isProposerPipeliningEnabled();
741
+ this.pipelinedParentSimulationOverridesPlan = isPipelining ? await buildPipelinedParentSimulationOverridesPlan({
742
+ checkpointNumber: this.checkpointNumber,
743
+ proposedCheckpointData: this.proposedCheckpointData,
744
+ rollup: this.publisher.rollupContract,
745
+ log: this.log
746
+ }) : undefined;
747
+ const checkpointGlobalVariables = await this.globalsBuilder.buildCheckpointGlobalVariables(coinbase, feeRecipient, this.targetSlot, this.pipelinedParentSimulationOverridesPlan);
634
748
  // Collect L1 to L2 messages for the checkpoint and compute their hash
635
749
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(this.checkpointNumber);
636
750
  const inHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
637
751
  // Collect the out hashes of all the checkpoints before this one in the same epoch
638
- const previousCheckpoints = (await this.l2BlockSource.getCheckpointsForEpoch(this.epoch)).filter((c)=>c.number < this.checkpointNumber);
639
- const previousCheckpointOutHashes = previousCheckpoints.map((c)=>c.getCheckpointOutHash());
752
+ const previousCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(this.targetEpoch)).filter((c)=>c.checkpointNumber < this.checkpointNumber).map((c)=>c.checkpointOutHash);
753
+ // Get the fee asset price modifier from the oracle
754
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
640
755
  const fork = _ts_add_disposable_resource(env, await this.worldState.fork(this.syncedToBlockNumber, {
641
756
  closeDelayMs: 12_000
642
- }), false);
757
+ }), true);
643
758
  // Create checkpoint builder for the entire slot
644
- const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(this.checkpointNumber, checkpointGlobalVariables, l1ToL2Messages, previousCheckpointOutHashes, fork);
759
+ const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(this.checkpointNumber, checkpointGlobalVariables, feeAssetPriceModifier, l1ToL2Messages, previousCheckpointOutHashes, fork, this.log.getBindings());
645
760
  // Options for the validator client when creating block and checkpoint proposals
646
761
  const blockProposalOptions = {
647
762
  publishFullTxs: !!this.config.publishTxsWithProposals,
@@ -653,6 +768,7 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
653
768
  };
654
769
  let blocksInCheckpoint = [];
655
770
  let blockPendingBroadcast = undefined;
771
+ const checkpointBuildTimer = new Timer();
656
772
  try {
657
773
  // Main loop: build blocks for the checkpoint
658
774
  const result = await this.buildBlocksForCheckpoint(checkpointBuilder, checkpointGlobalVariables.timestamp, inHash, blockProposalOptions);
@@ -662,107 +778,89 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
662
778
  // These errors are expected in HA mode, so we yield and let another HA node handle the slot
663
779
  // The only distinction between the 2 errors is SlashingProtectionError throws when the payload is different,
664
780
  // which is normal for block building (may have picked different txs)
665
- if (err instanceof DutyAlreadySignedError) {
666
- this.log.info(`Checkpoint proposal for slot ${this.slot} already signed by another HA node, yielding`, {
667
- slot: this.slot,
668
- signedByNode: err.signedByNode
669
- });
670
- return undefined;
671
- }
672
- if (err instanceof SlashingProtectionError) {
673
- this.log.info(`Checkpoint proposal for slot ${this.slot} blocked by slashing protection, yielding`, {
674
- slot: this.slot,
675
- existingMessageHash: err.existingMessageHash,
676
- attemptedMessageHash: err.attemptedMessageHash
677
- });
781
+ if (this.handleHASigningError(err, 'Block proposal')) {
678
782
  return undefined;
679
783
  }
680
784
  throw err;
681
785
  }
682
786
  if (blocksInCheckpoint.length === 0) {
683
- this.log.warn(`No blocks were built for slot ${this.slot}`, {
684
- slot: this.slot
787
+ this.log.warn(`No blocks were built for slot ${this.targetSlot}`, {
788
+ slot: this.targetSlot
685
789
  });
686
790
  this.eventEmitter.emit('checkpoint-empty', {
687
- slot: this.slot
791
+ slot: this.targetSlot
792
+ });
793
+ return undefined;
794
+ }
795
+ const minBlocksForCheckpoint = this.config.minBlocksForCheckpoint;
796
+ if (minBlocksForCheckpoint !== undefined && blocksInCheckpoint.length < minBlocksForCheckpoint) {
797
+ this.log.warn(`Checkpoint has fewer blocks than minimum (${blocksInCheckpoint.length} < ${minBlocksForCheckpoint}), skipping proposal`, {
798
+ slot: this.targetSlot,
799
+ blocksBuilt: blocksInCheckpoint.length,
800
+ minBlocksForCheckpoint
688
801
  });
689
802
  return undefined;
690
803
  }
691
804
  // Assemble and broadcast the checkpoint proposal, including the last block that was not
692
805
  // broadcasted yet, and wait to collect the committee attestations.
693
- this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.slot);
806
+ this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.targetSlot);
694
807
  const checkpoint = await checkpointBuilder.completeCheckpoint();
695
- // Do not collect attestations nor publish to L1 in fisherman mode
808
+ // Final validation: per-block limits are only checked if the operator set them explicitly.
809
+ // Otherwise, checkpoint-level budgets were already enforced by the redistribution logic.
810
+ try {
811
+ validateCheckpoint(checkpoint, {
812
+ rollupManaLimit: this.l1Constants.rollupManaLimit,
813
+ maxL2BlockGas: this.config.maxL2BlockGas,
814
+ maxDABlockGas: this.config.maxDABlockGas,
815
+ maxTxsPerBlock: this.config.maxTxsPerBlock,
816
+ maxTxsPerCheckpoint: this.config.maxTxsPerCheckpoint
817
+ });
818
+ } catch (err) {
819
+ this.log.error(`Built an invalid checkpoint at slot ${this.slotNow} (skipping proposal)`, err, {
820
+ checkpoint: checkpoint.header.toInspect()
821
+ });
822
+ return undefined;
823
+ }
824
+ // Record checkpoint-level build metrics
825
+ this.metrics.recordCheckpointBuild(checkpointBuildTimer.ms(), blocksInCheckpoint.length, checkpoint.getStats().txCount, Number(checkpoint.header.totalManaUsed.toBigInt()));
826
+ // In fisherman mode, return the checkpoint without broadcasting or collecting attestations
696
827
  if (this.config.fishermanMode) {
697
- this.log.info(`Built checkpoint for slot ${this.slot} with ${blocksInCheckpoint.length} blocks. ` + `Skipping proposal in fisherman mode.`, {
698
- slot: this.slot,
828
+ this.log.info(`Built checkpoint for slot ${this.targetSlot} with ${blocksInCheckpoint.length} blocks. ` + `Skipping proposal in fisherman mode.`, {
829
+ slot: this.targetSlot,
699
830
  checkpoint: checkpoint.header.toInspect(),
700
831
  blocksBuilt: blocksInCheckpoint.length
701
832
  });
702
833
  this.metrics.recordCheckpointSuccess();
703
- return checkpoint;
834
+ // Return a broadcast result with a dummy proposal — fisherman mode skips attestation collection
835
+ return {
836
+ checkpoint,
837
+ proposal: undefined,
838
+ blockProposedAt: this.dateProvider.now()
839
+ };
704
840
  }
705
- // Include the block pending broadcast in the checkpoint proposal if any
706
- const lastBlock = blockPendingBroadcast && {
707
- blockHeader: blockPendingBroadcast.block.header,
708
- indexWithinCheckpoint: blockPendingBroadcast.block.indexWithinCheckpoint,
709
- txs: blockPendingBroadcast.txs
710
- };
711
841
  // Create the checkpoint proposal and broadcast it
712
- const proposal = await this.validatorClient.createCheckpointProposal(checkpoint.header, checkpoint.archive.root, lastBlock, this.proposer, checkpointProposalOptions);
842
+ const proposal = await this.validatorClient.createCheckpointProposal(checkpoint.header, checkpoint.archive.root, feeAssetPriceModifier, blockPendingBroadcast, this.proposer, checkpointProposalOptions);
713
843
  const blockProposedAt = this.dateProvider.now();
714
844
  await this.p2pClient.broadcastCheckpointProposal(proposal);
715
- this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.slot);
716
- const attestations = await this.waitForAttestations(proposal);
717
- const blockAttestedAt = this.dateProvider.now();
718
- this.metrics.recordCheckpointAttestationDelay(blockAttestedAt - blockProposedAt);
719
- // Proposer must sign over the attestations before pushing them to L1
720
- const signer = this.proposer ?? this.publisher.getSenderAddress();
721
- let attestationsSignature;
722
- try {
723
- attestationsSignature = await this.validatorClient.signAttestationsAndSigners(attestations, signer, this.slot, this.checkpointNumber);
724
- } catch (err) {
725
- // We shouldn't really get here since we yield to another HA node
726
- // as soon as we see these errors when creating block proposals.
727
- if (err instanceof DutyAlreadySignedError) {
728
- this.log.info(`Attestations signature for slot ${this.slot} already signed by another HA node, yielding`, {
729
- slot: this.slot,
730
- signedByNode: err.signedByNode
731
- });
732
- return undefined;
733
- }
734
- if (err instanceof SlashingProtectionError) {
735
- this.log.info(`Attestations signature for slot ${this.slot} blocked by slashing protection, yielding`, {
736
- slot: this.slot,
737
- existingMessageHash: err.existingMessageHash,
738
- attemptedMessageHash: err.attemptedMessageHash
739
- });
740
- return undefined;
741
- }
742
- throw err;
743
- }
744
- // Enqueue publishing the checkpoint to L1
745
- this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.slot);
746
- const aztecSlotDuration = this.l1Constants.slotDuration;
747
- const slotStartBuildTimestamp = this.getSlotStartBuildTimestamp();
748
- const txTimeoutAt = new Date((slotStartBuildTimestamp + aztecSlotDuration) * 1000);
749
- await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
750
- txTimeoutAt,
751
- forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber
752
- });
753
- return checkpoint;
845
+ // Return immediately after broadcast — attestation collection happens in the background
846
+ return {
847
+ checkpoint,
848
+ proposal,
849
+ blockProposedAt
850
+ };
754
851
  } catch (e) {
755
852
  env.error = e;
756
853
  env.hasError = true;
757
854
  } finally{
758
- _ts_dispose_resources(env);
855
+ const result = _ts_dispose_resources(env);
856
+ if (result) await result;
759
857
  }
760
858
  } catch (err) {
761
859
  if (err && (err instanceof DutyAlreadySignedError || err instanceof SlashingProtectionError)) {
762
860
  // swallow this error. It's already been logged by a function deeper in the stack
763
861
  return undefined;
764
862
  }
765
- this.log.error(`Error building checkpoint at slot ${this.slot}`, err);
863
+ this.log.error(`Error building checkpoint at slot ${this.targetSlot}`, err);
766
864
  return undefined;
767
865
  }
768
866
  }
@@ -772,19 +870,17 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
772
870
  const blocksInCheckpoint = [];
773
871
  const txHashesAlreadyIncluded = new Set();
774
872
  const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
775
- // Remaining blob fields available for blocks (checkpoint end marker already subtracted)
776
- let remainingBlobFields = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB - NUM_CHECKPOINT_END_MARKER_FIELDS;
777
873
  // Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
778
874
  let blockPendingBroadcast = undefined;
779
875
  while(true){
780
876
  const blocksBuilt = blocksInCheckpoint.length;
781
- const indexWithinCheckpoint = blocksBuilt;
877
+ const indexWithinCheckpoint = IndexWithinCheckpoint(blocksBuilt);
782
878
  const blockNumber = BlockNumber(initialBlockNumber + blocksBuilt);
783
879
  const secondsIntoSlot = this.getSecondsIntoSlot();
784
880
  const timingInfo = this.timetable.canStartNextBlock(secondsIntoSlot);
785
881
  if (!timingInfo.canStart) {
786
882
  this.log.debug(`Not enough time left in slot to start another block`, {
787
- slot: this.slot,
883
+ slot: this.targetSlot,
788
884
  blocksBuilt,
789
885
  secondsIntoSlot
790
886
  });
@@ -799,66 +895,57 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
799
895
  buildDeadline: timingInfo.deadline ? new Date((this.getSlotStartBuildTimestamp() + timingInfo.deadline) * 1000) : undefined,
800
896
  blockNumber,
801
897
  indexWithinCheckpoint,
802
- txHashesAlreadyIncluded,
803
- remainingBlobFields
898
+ txHashesAlreadyIncluded
804
899
  });
805
- if (!buildResult && timingInfo.isLastBlock) {
806
- break;
807
- } else if (!buildResult && timingInfo.deadline !== undefined) {
808
- // But if there is still time for more blocks, wait until the next subslot and try again
900
+ // If we failed to build the block due to insufficient txs, we try again if there is still time left in the slot
901
+ if ('failure' in buildResult) {
902
+ // If this was the last subslot, or we're running with a single block per slot, we're done
903
+ if (timingInfo.isLastBlock || timingInfo.deadline === undefined) {
904
+ break;
905
+ }
906
+ // Otherwise, if there is still time for more blocks, we wait until the next subslot and try again
809
907
  await this.waitUntilNextSubslot(timingInfo.deadline);
810
908
  continue;
811
- } else if (!buildResult) {
812
- break;
813
- } else if ('error' in buildResult) {
814
- // If there was an error building the block, just exit the loop and give up the rest of the slot
909
+ }
910
+ // If there was an error building the block, we just exit the loop and give up the rest of the slot.
911
+ // We don't want to risk building more blocks if something went wrong.
912
+ if ('error' in buildResult) {
815
913
  if (!(buildResult.error instanceof SequencerInterruptedError)) {
816
- this.log.warn(`Halting block building for slot ${this.slot}`, {
817
- slot: this.slot,
914
+ this.log.warn(`Halting block building for slot ${this.targetSlot}`, {
915
+ slot: this.targetSlot,
818
916
  blocksBuilt,
819
917
  error: buildResult.error
820
918
  });
821
919
  }
822
920
  break;
823
921
  }
824
- const { block, usedTxs, remainingBlobFields: newRemainingBlobFields } = buildResult;
922
+ const { block, usedTxs } = buildResult;
825
923
  blocksInCheckpoint.push(block);
826
- // Update remaining blob fields for the next block
827
- remainingBlobFields = newRemainingBlobFields;
828
- // Sync the proposed block to the archiver to make it available
829
- // Note that the checkpoint builder uses its own fork so it should not need to wait for this syncing
830
- // Eventually we should refactor the checkpoint builder to not need a separate long-lived fork
831
- // Fire and forget - don't block the critical path, but log errors
832
- this.syncProposedBlockToArchiver(block).catch((err)=>{
833
- this.log.error(`Failed to sync proposed block ${block.number} to archiver`, {
834
- blockNumber: block.number,
835
- err
836
- });
837
- });
838
- // If this is the last block, exit the loop now so we start collecting attestations
924
+ usedTxs.forEach((tx)=>txHashesAlreadyIncluded.add(tx.txHash.toString()));
925
+ // Sign the block proposal. This will throw if HA signing fails.
926
+ const proposal = await this.createBlockProposal(block, inHash, usedTxs, blockProposalOptions);
927
+ // Sync the proposed block to the archiver to make it available, only after we've managed to sign the proposal,
928
+ // so we avoid polluting our archive with a block that would fail.
929
+ // We wait for the sync to succeed, as this helps catch consistency errors, even if it means we lose some time for block-building.
930
+ // If this throws, we abort the entire checkpoint.
931
+ await this.syncProposedBlockToArchiver(block);
932
+ // If this is the last block, do not broadcast it, since it will be included in the checkpoint proposal.
839
933
  if (timingInfo.isLastBlock) {
840
- this.log.verbose(`Completed final block ${blockNumber} for slot ${this.slot}`, {
841
- slot: this.slot,
934
+ this.log.verbose(`Completed final block ${blockNumber} for slot ${this.targetSlot}`, {
935
+ slot: this.targetSlot,
842
936
  blockNumber,
843
937
  blocksBuilt
844
938
  });
845
- blockPendingBroadcast = {
846
- block,
847
- txs: usedTxs
848
- };
939
+ blockPendingBroadcast = proposal;
849
940
  break;
850
941
  }
851
- // For non-last blocks, broadcast the block proposal (unless we're in fisherman mode)
852
- // If the block is the last one, we'll broadcast it along with the checkpoint at the end of the loop
853
- if (!this.config.fishermanMode) {
854
- const proposal = await this.validatorClient.createBlockProposal(block.header, block.indexWithinCheckpoint, inHash, block.archive.root, usedTxs, this.proposer, blockProposalOptions);
855
- await this.p2pClient.broadcastProposal(proposal);
856
- }
942
+ // Once we have a signed proposal and the archiver agreed with our proposed block, then we broadcast it.
943
+ proposal && await this.p2pClient.broadcastProposal(proposal);
857
944
  // Wait until the next block's start time
858
945
  await this.waitUntilNextSubslot(timingInfo.deadline);
859
946
  }
860
- this.log.verbose(`Block building loop completed for slot ${this.slot}`, {
861
- slot: this.slot,
947
+ this.log.verbose(`Block building loop completed for slot ${this.targetSlot}`, {
948
+ slot: this.targetSlot,
862
949
  blocksBuilt: blocksInCheckpoint.length
863
950
  });
864
951
  return {
@@ -866,115 +953,127 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
866
953
  blockPendingBroadcast
867
954
  };
868
955
  }
956
+ /** Creates a block proposal for a given block via the validator client (unless in fisherman mode) */ createBlockProposal(block, inHash, usedTxs, blockProposalOptions) {
957
+ if (this.config.fishermanMode) {
958
+ this.log.info(`Skipping block proposal for block ${block.number} in fisherman mode`);
959
+ return Promise.resolve(undefined);
960
+ }
961
+ return this.validatorClient.createBlockProposal(block.header, block.indexWithinCheckpoint, inHash, block.archive.root, usedTxs, this.proposer, blockProposalOptions);
962
+ }
869
963
  /** Sleeps until it is time to produce the next block in the slot */ async waitUntilNextSubslot(nextSubslotStart) {
870
- this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.slot);
964
+ this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.targetSlot);
871
965
  this.log.verbose(`Waiting until time for the next block at ${nextSubslotStart}s into slot`, {
872
- slot: this.slot
966
+ slot: this.targetSlot
873
967
  });
874
968
  await this.waitUntilTimeInSlot(nextSubslotStart);
875
969
  }
876
970
  /** Builds a single block. Called from the main block building loop. */ async buildSingleBlock(checkpointBuilder, opts) {
877
- const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded, remainingBlobFields } = opts;
878
- this.log.verbose(`Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.slot}`, {
971
+ const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded } = opts;
972
+ this.log.verbose(`Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.targetSlot}`, {
879
973
  ...checkpointBuilder.getConstantData(),
880
974
  ...opts
881
975
  });
882
976
  try {
883
977
  // Wait until we have enough txs to build the block
884
- const minTxs = this.config.minTxsPerBlock;
885
- const { availableTxs, canStartBuilding } = await this.waitForMinTxs(opts);
978
+ const { availableTxs, canStartBuilding, minTxs } = await this.waitForMinTxs(opts);
886
979
  if (!canStartBuilding) {
887
- this.log.warn(`Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (got ${availableTxs} txs but needs ${minTxs})`, {
980
+ this.log.warn(`Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.targetSlot} (got ${availableTxs} txs but needs ${minTxs})`, {
888
981
  blockNumber,
889
- slot: this.slot,
982
+ slot: this.targetSlot,
890
983
  indexWithinCheckpoint
891
984
  });
892
985
  this.eventEmitter.emit('block-tx-count-check-failed', {
893
986
  minTxs,
894
987
  availableTxs,
895
- slot: this.slot
988
+ slot: this.targetSlot
896
989
  });
897
990
  this.metrics.recordBlockProposalFailed('insufficient_txs');
898
- return undefined;
991
+ return {
992
+ failure: 'insufficient-txs'
993
+ };
899
994
  }
900
995
  // Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
901
996
  // just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
902
- const pendingTxs = filter(this.p2pClient.iteratePendingTxs(), (tx)=>!txHashesAlreadyIncluded.has(tx.txHash.toString()));
903
- this.log.debug(`Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.slot} with ${availableTxs} available txs`, {
904
- slot: this.slot,
997
+ const pendingTxs = filter(this.p2pClient.iterateEligiblePendingTxs(), (tx)=>!txHashesAlreadyIncluded.has(tx.txHash.toString()));
998
+ this.log.debug(`Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.targetSlot} with ${availableTxs} available txs`, {
999
+ slot: this.targetSlot,
905
1000
  blockNumber,
906
1001
  indexWithinCheckpoint
907
1002
  });
908
- this.setStateFn(SequencerState.CREATING_BLOCK, this.slot);
909
- // Calculate blob fields limit for txs (remaining capacity - this block's end overhead)
910
- const blockEndOverhead = getNumBlockEndBlobFields(indexWithinCheckpoint === 0);
911
- const maxBlobFieldsForTxs = remainingBlobFields - blockEndOverhead;
1003
+ this.setStateFn(SequencerState.CREATING_BLOCK, this.targetSlot);
1004
+ // Per-block limits are operator overrides (from SEQ_MAX_L2_BLOCK_GAS etc.) further capped
1005
+ // by remaining checkpoint-level budgets inside CheckpointBuilder before each block is built.
1006
+ // minValidTxs is passed into the builder so it can reject the block *before* updating state.
1007
+ const minValidTxs = forceCreate ? 0 : this.config.minValidTxsPerBlock ?? minTxs;
912
1008
  const blockBuilderOptions = {
913
1009
  maxTransactions: this.config.maxTxsPerBlock,
914
- maxBlockSize: this.config.maxBlockSizeInBytes,
915
- maxBlockGas: new Gas(this.config.maxDABlockGas, this.config.maxL2BlockGas),
916
- maxBlobFields: maxBlobFieldsForTxs,
917
- deadline: buildDeadline
1010
+ maxBlockGas: this.config.maxL2BlockGas !== undefined || this.config.maxDABlockGas !== undefined ? new Gas(this.config.maxDABlockGas ?? Infinity, this.config.maxL2BlockGas ?? Infinity) : undefined,
1011
+ deadline: buildDeadline,
1012
+ isBuildingProposal: true,
1013
+ minValidTxs,
1014
+ maxBlocksPerCheckpoint: this.timetable.maxNumberOfBlocks,
1015
+ perBlockAllocationMultiplier: this.config.perBlockAllocationMultiplier
918
1016
  };
919
- // Actually build the block by executing txs
920
- const workTimer = new Timer();
921
- const { publicGas, block, publicProcessorDuration, numTxs, blockBuildingTimer, usedTxs, failedTxs, usedTxBlobFields } = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
922
- const blockBuildDuration = workTimer.ms();
1017
+ // Actually build the block by executing txs. The builder throws InsufficientValidTxsError
1018
+ // if the number of successfully processed txs is below minValidTxs, ensuring state is not
1019
+ // updated for blocks that will be discarded.
1020
+ const buildResult = await this.buildSingleBlockWithCheckpointBuilder(checkpointBuilder, pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
923
1021
  // If any txs failed during execution, drop them from the mempool so we don't pick them up again
924
- await this.dropFailedTxsFromP2P(failedTxs);
925
- // Check if we have created a block with enough txs. If there were invalid txs in the pool, or if execution took
926
- // too long, then we may not get to minTxsPerBlock after executing public functions.
927
- const minValidTxs = this.config.minValidTxsPerBlock ?? minTxs;
928
- if (!forceCreate && numTxs < minValidTxs) {
929
- this.log.warn(`Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed (got ${numTxs} but required ${minValidTxs})`, {
930
- slot: this.slot,
1022
+ await this.dropFailedTxsFromP2P(buildResult.failedTxs);
1023
+ if (buildResult.status === 'insufficient-valid-txs') {
1024
+ this.log.warn(`Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.targetSlot} has too few valid txs to be proposed`, {
1025
+ slot: this.targetSlot,
931
1026
  blockNumber,
932
- numTxs,
933
- indexWithinCheckpoint
1027
+ numTxs: buildResult.processedCount,
1028
+ indexWithinCheckpoint,
1029
+ minValidTxs
934
1030
  });
935
- this.eventEmitter.emit('block-tx-count-check-failed', {
936
- minTxs: minValidTxs,
937
- availableTxs: numTxs,
938
- slot: this.slot
1031
+ this.eventEmitter.emit('block-build-failed', {
1032
+ reason: `Insufficient valid txs`,
1033
+ slot: this.targetSlot
939
1034
  });
940
1035
  this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
941
- return undefined;
1036
+ return {
1037
+ failure: 'insufficient-valid-txs'
1038
+ };
942
1039
  }
943
1040
  // Block creation succeeded, emit stats and metrics
1041
+ const { block, publicProcessorDuration, usedTxs, blockBuildDuration, numTxs } = buildResult;
944
1042
  const blockStats = {
945
1043
  eventName: 'l2-block-built',
946
1044
  duration: blockBuildDuration,
947
1045
  publicProcessDuration: publicProcessorDuration,
948
- rollupCircuitsDuration: blockBuildingTimer.ms(),
949
1046
  ...block.getStats()
950
1047
  };
951
1048
  const blockHash = await block.hash();
952
1049
  const txHashes = block.body.txEffects.map((tx)=>tx.txHash);
953
- const manaPerSec = publicGas.l2Gas / (blockBuildDuration / 1000);
954
- this.log.info(`Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.slot} with ${numTxs} txs`, {
1050
+ const manaPerSec = block.header.totalManaUsed.toNumberUnsafe() / (blockBuildDuration / 1000);
1051
+ this.log.info(`Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.targetSlot} with ${numTxs} txs`, {
955
1052
  blockHash,
956
1053
  txHashes,
957
1054
  manaPerSec,
958
1055
  ...blockStats
959
1056
  });
1057
+ // `slot` is the target/submission slot (may be one ahead when pipelining),
1058
+ // `buildSlot` is the wall-clock slot during which the block was actually built.
960
1059
  this.eventEmitter.emit('block-proposed', {
961
1060
  blockNumber: block.number,
962
- slot: this.slot
1061
+ slot: this.targetSlot,
1062
+ buildSlot: this.slotNow
963
1063
  });
964
- this.metrics.recordBuiltBlock(blockBuildDuration, publicGas.l2Gas);
1064
+ this.metrics.recordBuiltBlock(blockBuildDuration, block.header.totalManaUsed.toNumberUnsafe(), this.targetSlot);
965
1065
  return {
966
1066
  block,
967
- usedTxs,
968
- remainingBlobFields: maxBlobFieldsForTxs - usedTxBlobFields
1067
+ usedTxs
969
1068
  };
970
1069
  } catch (err) {
971
1070
  this.eventEmitter.emit('block-build-failed', {
972
1071
  reason: err.message,
973
- slot: this.slot
1072
+ slot: this.targetSlot
974
1073
  });
975
1074
  this.log.error(`Error building block`, err, {
976
1075
  blockNumber,
977
- slot: this.slot
1076
+ slot: this.targetSlot
978
1077
  });
979
1078
  this.metrics.recordBlockProposalFailed(err.name || 'unknown_error');
980
1079
  this.metrics.recordFailedBlock();
@@ -983,9 +1082,31 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
983
1082
  };
984
1083
  }
985
1084
  }
1085
+ /** Uses the checkpoint builder to build a block, catching InsufficientValidTxsError. */ async buildSingleBlockWithCheckpointBuilder(checkpointBuilder, pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions) {
1086
+ try {
1087
+ const workTimer = new Timer();
1088
+ const result = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
1089
+ const blockBuildDuration = workTimer.ms();
1090
+ return {
1091
+ ...result,
1092
+ blockBuildDuration,
1093
+ status: 'success'
1094
+ };
1095
+ } catch (err) {
1096
+ if (isErrorClass(err, InsufficientValidTxsError)) {
1097
+ return {
1098
+ failedTxs: err.failedTxs,
1099
+ processedCount: err.processedCount,
1100
+ status: 'insufficient-valid-txs'
1101
+ };
1102
+ }
1103
+ throw err;
1104
+ }
1105
+ }
986
1106
  /** Waits until minTxs are available on the pool for building a block. */ async waitForMinTxs(opts) {
987
- const minTxs = this.config.minTxsPerBlock;
988
1107
  const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
1108
+ // We only allow a block with 0 txs in the first block of the checkpoint
1109
+ const minTxs = indexWithinCheckpoint > 0 && this.config.minTxsPerBlock === 0 ? 1 : this.config.minTxsPerBlock;
989
1110
  // Deadline is undefined if we are not enforcing the timetable, meaning we'll exit immediately when out of time
990
1111
  const startBuildingDeadline = buildDeadline ? new Date(buildDeadline.getTime() - this.timetable.minExecutionTime * 1000) : undefined;
991
1112
  let availableTxs = await this.p2pClient.getPendingTxCount();
@@ -995,22 +1116,24 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
995
1116
  if (startBuildingDeadline === undefined || now >= startBuildingDeadline) {
996
1117
  return {
997
1118
  canStartBuilding: false,
998
- availableTxs: availableTxs
1119
+ availableTxs,
1120
+ minTxs
999
1121
  };
1000
1122
  }
1001
1123
  // Wait a bit before checking again
1002
- this.setStateFn(SequencerState.WAITING_FOR_TXS, this.slot);
1003
- this.log.verbose(`Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (have ${availableTxs} but need ${minTxs})`, {
1124
+ this.setStateFn(SequencerState.WAITING_FOR_TXS, this.targetSlot);
1125
+ this.log.verbose(`Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.targetSlot} (have ${availableTxs} but need ${minTxs})`, {
1004
1126
  blockNumber,
1005
- slot: this.slot,
1127
+ slot: this.targetSlot,
1006
1128
  indexWithinCheckpoint
1007
1129
  });
1008
- await sleep(TXS_POLLING_MS);
1130
+ await this.waitForTxsPollingInterval();
1009
1131
  availableTxs = await this.p2pClient.getPendingTxCount();
1010
1132
  }
1011
1133
  return {
1012
1134
  canStartBuilding: true,
1013
- availableTxs
1135
+ availableTxs,
1136
+ minTxs
1014
1137
  };
1015
1138
  }
1016
1139
  /**
@@ -1033,24 +1156,30 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
1033
1156
  committee
1034
1157
  });
1035
1158
  }
1036
- const numberOfRequiredAttestations = Math.floor(committee.length * 2 / 3) + 1;
1159
+ const numberOfRequiredAttestations = computeQuorum(committee.length);
1037
1160
  if (this.config.skipCollectingAttestations) {
1038
1161
  this.log.warn('Skipping attestation collection as per config (attesting with own keys only)');
1039
1162
  const attestations = await this.validatorClient?.collectOwnAttestations(proposal);
1040
1163
  return new CommitteeAttestationsAndSigners(orderAttestations(attestations ?? [], committee));
1041
1164
  }
1042
- const attestationTimeAllowed = this.config.enforceTimeTable ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_CHECKPOINT) : this.l1Constants.slotDuration;
1043
- const attestationDeadline = new Date(this.dateProvider.now() + attestationTimeAllowed * 1000);
1165
+ const attestationTimeAllowed = this.config.enforceTimeTable ? this.timetable.getCheckpointAttestationDeadline() : this.l1Constants.slotDuration;
1166
+ const attestationDeadline = new Date((this.getSlotStartBuildTimestamp() + attestationTimeAllowed) * 1000);
1044
1167
  this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
1045
1168
  const collectAttestationsTimer = new Timer();
1046
1169
  let collectedAttestationsCount = 0;
1047
1170
  try {
1048
1171
  const attestations = await this.validatorClient.collectAttestations(proposal, numberOfRequiredAttestations, attestationDeadline);
1049
1172
  collectedAttestationsCount = attestations.length;
1173
+ // Trim attestations to minimum required to save L1 calldata gas
1174
+ const localAddresses = this.validatorClient.getValidatorAddresses();
1175
+ const trimmed = trimAttestations(attestations, numberOfRequiredAttestations, this.attestorAddress, localAddresses);
1176
+ if (trimmed.length < attestations.length) {
1177
+ this.log.debug(`Trimmed attestations from ${attestations.length} to ${trimmed.length} for L1 submission`);
1178
+ }
1050
1179
  // Rollup contract requires that the signatures are provided in the order of the committee
1051
- const sorted = orderAttestations(attestations, committee);
1180
+ const sorted = orderAttestations(trimmed, committee);
1052
1181
  // Manipulate the attestations if we've been configured to do so
1053
- if (this.config.injectFakeAttestation || this.config.shuffleAttestationOrdering) {
1182
+ if (this.config.injectFakeAttestation || this.config.injectHighSValueAttestation || this.config.injectUnrecoverableSignatureAttestation || this.config.shuffleAttestationOrdering) {
1054
1183
  return this.manipulateAttestations(proposal.slotNumber, epoch, seed, committee, sorted);
1055
1184
  }
1056
1185
  return new CommitteeAttestationsAndSigners(sorted);
@@ -1067,7 +1196,7 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
1067
1196
  // Compute the proposer index in the committee, since we dont want to tweak it.
1068
1197
  // Otherwise, the L1 rollup contract will reject the block outright.
1069
1198
  const proposerIndex = Number(this.epochCache.computeProposerIndex(slotNumber, epoch, seed, BigInt(committee.length)));
1070
- if (this.config.injectFakeAttestation) {
1199
+ if (this.config.injectFakeAttestation || this.config.injectHighSValueAttestation || this.config.injectUnrecoverableSignatureAttestation) {
1071
1200
  // Find non-empty attestations that are not from the proposer
1072
1201
  const nonProposerIndices = [];
1073
1202
  for(let i = 0; i < attestations.length; i++){
@@ -1077,8 +1206,16 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
1077
1206
  }
1078
1207
  if (nonProposerIndices.length > 0) {
1079
1208
  const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
1080
- this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
1081
- unfreeze(attestations[targetIndex]).signature = Signature.random();
1209
+ if (this.config.injectHighSValueAttestation) {
1210
+ this.log.warn(`Injecting high-s value attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
1211
+ unfreeze(attestations[targetIndex]).signature = flipSignature(attestations[targetIndex].signature);
1212
+ } else if (this.config.injectUnrecoverableSignatureAttestation) {
1213
+ this.log.warn(`Injecting unrecoverable signature attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
1214
+ unfreeze(attestations[targetIndex]).signature = generateUnrecoverableSignature();
1215
+ } else {
1216
+ this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
1217
+ unfreeze(attestations[targetIndex]).signature = generateRecoverableSignature();
1218
+ }
1082
1219
  }
1083
1220
  return new CommitteeAttestationsAndSigners(attestations);
1084
1221
  }
@@ -1087,14 +1224,25 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
1087
1224
  const shuffled = [
1088
1225
  ...attestations
1089
1226
  ];
1090
- const [i, j] = [
1091
- (proposerIndex + 1) % shuffled.length,
1092
- (proposerIndex + 2) % shuffled.length
1093
- ];
1094
- const valueI = shuffled[i];
1095
- const valueJ = shuffled[j];
1096
- shuffled[i] = valueJ;
1097
- shuffled[j] = valueI;
1227
+ // Find two non-proposer positions that both have non-empty signatures to swap.
1228
+ // This ensures the bitmap doesn't change, so the MaliciousCommitteeAttestationsAndSigners
1229
+ // signers array stays correctly aligned with L1's committee reconstruction.
1230
+ const swappable = [];
1231
+ for(let k = 0; k < shuffled.length; k++){
1232
+ if (!shuffled[k].signature.isEmpty() && k !== proposerIndex) {
1233
+ swappable.push(k);
1234
+ }
1235
+ }
1236
+ if (swappable.length >= 2) {
1237
+ const [i, j] = [
1238
+ swappable[0],
1239
+ swappable[1]
1240
+ ];
1241
+ [shuffled[i], shuffled[j]] = [
1242
+ shuffled[j],
1243
+ shuffled[i]
1244
+ ];
1245
+ }
1098
1246
  const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
1099
1247
  return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
1100
1248
  }
@@ -1107,14 +1255,18 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
1107
1255
  const failedTxData = failedTxs.map((fail)=>fail.tx);
1108
1256
  const failedTxHashes = failedTxData.map((tx)=>tx.getTxHash());
1109
1257
  this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
1110
- await this.p2pClient.deleteTxs(failedTxHashes);
1258
+ await this.p2pClient.handleFailedExecution(failedTxHashes);
1111
1259
  }
1112
1260
  /**
1113
1261
  * Adds the proposed block to the archiver so it's available via P2P.
1114
1262
  * Gossip doesn't echo messages back to the sender, so the proposer's archiver/world-state
1115
1263
  * would never receive its own block without this explicit sync.
1264
+ *
1265
+ * In fisherman mode we skip this push: the fisherman builds blocks locally for validation
1266
+ * and fee analysis only, and pushing them to the archiver causes spurious reorg cascades
1267
+ * whenever the real proposer's block arrives from L1.
1116
1268
  */ async syncProposedBlockToArchiver(block) {
1117
- if (this.config.skipPushProposedBlocksToArchiver !== false) {
1269
+ if (this.config.skipPushProposedBlocksToArchiver || this.config.fishermanMode) {
1118
1270
  this.log.warn(`Skipping push of proposed block ${block.number} to archiver`, {
1119
1271
  blockNumber: block.number,
1120
1272
  slot: block.header.globalVariables.slotNumber
@@ -1130,29 +1282,52 @@ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('Checkpoint
1130
1282
  /** Runs fee analysis and logs checkpoint outcome as fisherman */ async handleCheckpointEndAsFisherman(checkpoint) {
1131
1283
  // Perform L1 fee analysis before clearing requests
1132
1284
  // The callback is invoked asynchronously after the next block is mined
1133
- const feeAnalysis = await this.publisher.analyzeL1Fees(this.slot, (analysis)=>this.metrics.recordFishermanFeeAnalysis(analysis));
1285
+ const feeAnalysis = await this.publisher.analyzeL1Fees(this.targetSlot, (analysis)=>this.metrics.recordFishermanFeeAnalysis(analysis));
1134
1286
  if (checkpoint) {
1135
- this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.slot}`, {
1287
+ this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.targetSlot}`, {
1136
1288
  ...checkpoint.toCheckpointInfo(),
1137
1289
  ...checkpoint.getStats(),
1138
1290
  feeAnalysisId: feeAnalysis?.id
1139
1291
  });
1140
1292
  } else {
1141
- this.log.warn(`Validation block building FAILED for slot ${this.slot}`, {
1142
- slot: this.slot,
1293
+ this.log.warn(`Validation block building FAILED for slot ${this.targetSlot}`, {
1294
+ slot: this.targetSlot,
1143
1295
  feeAnalysisId: feeAnalysis?.id
1144
1296
  });
1145
- this.metrics.recordBlockProposalFailed('block_build_failed');
1297
+ this.metrics.recordCheckpointProposalFailed('block_build_failed');
1146
1298
  }
1147
1299
  this.publisher.clearPendingRequests();
1148
1300
  }
1301
+ /**
1302
+ * Helper to handle HA double-signing errors. Returns true if the error was handled (caller should yield).
1303
+ */ handleHASigningError(err, errorContext) {
1304
+ if (err instanceof DutyAlreadySignedError) {
1305
+ this.log.info(`${errorContext} for slot ${this.targetSlot} already signed by another HA node, yielding`, {
1306
+ slot: this.targetSlot,
1307
+ signedByNode: err.signedByNode
1308
+ });
1309
+ return true;
1310
+ }
1311
+ if (err instanceof SlashingProtectionError) {
1312
+ this.log.info(`${errorContext} for slot ${this.targetSlot} blocked by slashing protection, yielding`, {
1313
+ slot: this.targetSlot,
1314
+ existingMessageHash: err.existingMessageHash,
1315
+ attemptedMessageHash: err.attemptedMessageHash
1316
+ });
1317
+ return true;
1318
+ }
1319
+ return false;
1320
+ }
1149
1321
  /** Waits until a specific time within the current slot */ async waitUntilTimeInSlot(targetSecondsIntoSlot) {
1150
1322
  const slotStartTimestamp = this.getSlotStartBuildTimestamp();
1151
1323
  const targetTimestamp = slotStartTimestamp + targetSecondsIntoSlot;
1152
1324
  await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
1153
1325
  }
1326
+ /** Waits the polling interval for transactions. Extracted for test overriding. */ async waitForTxsPollingInterval() {
1327
+ await sleep(TXS_POLLING_MS);
1328
+ }
1154
1329
  getSlotStartBuildTimestamp() {
1155
- return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
1330
+ return getSlotStartBuildTimestamp(this.slotNow, this.l1Constants);
1156
1331
  }
1157
1332
  getSecondsIntoSlot() {
1158
1333
  const slotStartTimestamp = this.getSlotStartBuildTimestamp();