@aztec/sequencer-client 0.0.1-commit.e2b2873ed → 0.0.1-commit.e304674f1

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