@aztec/sequencer-client 0.0.1-commit.f146247c → 0.0.1-commit.f1b29a41e

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 (84) 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 -26
  4. package/dest/config.d.ts +26 -7
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +47 -28
  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 -3
  30. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  31. package/dest/publisher/sequencer-publisher-factory.js +27 -2
  32. package/dest/publisher/sequencer-publisher.d.ts +76 -30
  33. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  34. package/dest/publisher/sequencer-publisher.js +396 -71
  35. package/dest/sequencer/checkpoint_proposal_job.d.ts +39 -8
  36. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  37. package/dest/sequencer/checkpoint_proposal_job.js +368 -196
  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 +42 -17
  47. package/dest/sequencer/sequencer.d.ts.map +1 -1
  48. package/dest/sequencer/sequencer.js +147 -89
  49. package/dest/sequencer/timetable.d.ts +4 -6
  50. package/dest/sequencer/timetable.d.ts.map +1 -1
  51. package/dest/sequencer/timetable.js +7 -11
  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 -23
  64. package/src/config.ts +65 -38
  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 -6
  74. package/src/publisher/sequencer-publisher.ts +442 -95
  75. package/src/sequencer/checkpoint_proposal_job.ts +481 -202
  76. package/src/sequencer/checkpoint_voter.ts +1 -12
  77. package/src/sequencer/events.ts +1 -1
  78. package/src/sequencer/metrics.ts +106 -18
  79. package/src/sequencer/sequencer.ts +212 -105
  80. package/src/sequencer/timetable.ts +13 -12
  81. package/src/sequencer/types.ts +1 -1
  82. package/src/test/index.ts +2 -4
  83. package/src/test/mock_checkpoint_builder.ts +63 -49
  84. package/src/test/utils.ts +5 -2
@@ -1,6 +1,5 @@
1
- import { NUM_CHECKPOINT_END_MARKER_FIELDS, getNumBlockEndBlobFields } from '@aztec/blob-lib/encoding';
2
- import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB } from '@aztec/constants';
3
1
  import type { EpochCache } from '@aztec/epoch-cache';
2
+ import { type FeeHeader, RollupContract } from '@aztec/ethereum/contracts';
4
3
  import {
5
4
  BlockNumber,
6
5
  CheckpointNumber,
@@ -9,6 +8,11 @@ import {
9
8
  SlotNumber,
10
9
  } from '@aztec/foundation/branded-types';
11
10
  import { randomInt } from '@aztec/foundation/crypto/random';
11
+ import {
12
+ flipSignature,
13
+ generateRecoverableSignature,
14
+ generateUnrecoverableSignature,
15
+ } from '@aztec/foundation/crypto/secp256k1-signer';
12
16
  import { Fr } from '@aztec/foundation/curves/bn254';
13
17
  import { EthAddress } from '@aztec/foundation/eth-address';
14
18
  import { Signature } from '@aztec/foundation/eth-signature';
@@ -27,18 +31,23 @@ import {
27
31
  type L2BlockSource,
28
32
  MaliciousCommitteeAttestationsAndSigners,
29
33
  } from '@aztec/stdlib/block';
30
- import type { Checkpoint } from '@aztec/stdlib/checkpoint';
31
- import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
34
+ import { type Checkpoint, type ProposedCheckpointData, validateCheckpoint } from '@aztec/stdlib/checkpoint';
35
+ import { computeQuorum, getSlotStartBuildTimestamp, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
32
36
  import { Gas } from '@aztec/stdlib/gas';
33
37
  import {
34
- NoValidTxsError,
35
- type PublicProcessorLimits,
38
+ type BlockBuilderOptions,
39
+ InsufficientValidTxsError,
36
40
  type ResolvedSequencerConfig,
37
41
  type WorldStateSynchronizer,
38
42
  } from '@aztec/stdlib/interfaces/server';
39
43
  import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
40
- import type { BlockProposalOptions, CheckpointProposal, CheckpointProposalOptions } from '@aztec/stdlib/p2p';
41
- import { orderAttestations } from '@aztec/stdlib/p2p';
44
+ import type {
45
+ BlockProposal,
46
+ BlockProposalOptions,
47
+ CheckpointProposal,
48
+ CheckpointProposalOptions,
49
+ } from '@aztec/stdlib/p2p';
50
+ import { orderAttestations, trimAttestations } from '@aztec/stdlib/p2p';
42
51
  import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
43
52
  import { type FailedTx, Tx } from '@aztec/stdlib/tx';
44
53
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
@@ -59,6 +68,13 @@ import { SequencerState } from './utils.js';
59
68
  /** How much time to sleep while waiting for min transactions to accumulate for a block */
60
69
  const TXS_POLLING_MS = 500;
61
70
 
71
+ /** Result from proposeCheckpoint when a checkpoint was successfully built and attested. */
72
+ type CheckpointProposalResult = {
73
+ checkpoint: Checkpoint;
74
+ attestations: CommitteeAttestationsAndSigners;
75
+ attestationsSignature: Signature;
76
+ };
77
+
62
78
  /**
63
79
  * Handles the execution of a checkpoint proposal after the initial preparation phase.
64
80
  * This includes building blocks, collecting attestations, and publishing the checkpoint to L1,
@@ -68,9 +84,16 @@ const TXS_POLLING_MS = 500;
68
84
  export class CheckpointProposalJob implements Traceable {
69
85
  protected readonly log: Logger;
70
86
 
87
+ /** Tracks the fire-and-forget L1 submission promise so it can be awaited during shutdown. */
88
+ private pendingL1Submission: Promise<void> | undefined;
89
+
90
+ /** Fee header override computed during proposeCheckpoint, reused in enqueueCheckpointForSubmission. */
91
+ private computedForceProposedFeeHeader?: { checkpointNumber: CheckpointNumber; feeHeader: FeeHeader };
92
+
71
93
  constructor(
72
- private readonly epoch: EpochNumber,
73
- private readonly slot: SlotNumber,
94
+ private readonly slotNow: SlotNumber,
95
+ private readonly targetSlot: SlotNumber,
96
+ private readonly targetEpoch: EpochNumber,
74
97
  private readonly checkpointNumber: CheckpointNumber,
75
98
  private readonly syncedToBlockNumber: BlockNumber,
76
99
  // TODO(palla/mbps): Can we remove the proposer in favor of attestorAddress? Need to check fisherman-node flows.
@@ -97,13 +120,25 @@ export class CheckpointProposalJob implements Traceable {
97
120
  private readonly setStateFn: (state: SequencerState, slot?: SlotNumber) => void,
98
121
  public readonly tracer: Tracer,
99
122
  bindings?: LoggerBindings,
123
+ private readonly proposedCheckpointData?: ProposedCheckpointData,
100
124
  ) {
101
- this.log = createLogger('sequencer:checkpoint-proposal', { ...bindings, instanceId: `slot-${slot}` });
125
+ this.log = createLogger('sequencer:checkpoint-proposal', {
126
+ ...bindings,
127
+ instanceId: `slot-${this.slotNow}`,
128
+ });
129
+ }
130
+
131
+ /** Awaits the pending L1 submission if one is in progress. Call during shutdown. */
132
+ public async awaitPendingSubmission(): Promise<void> {
133
+ this.log.info('Awaiting pending L1 payload submission');
134
+ await this.pendingL1Submission;
102
135
  }
103
136
 
104
137
  /**
105
138
  * Executes the checkpoint proposal job.
106
- * Returns the published checkpoint if successful, undefined otherwise.
139
+ * Builds blocks, collects attestations, enqueues requests, and schedules L1 submission as a
140
+ * background task so the work loop can return to IDLE immediately.
141
+ * Returns the built checkpoint if successful, undefined otherwise.
107
142
  */
108
143
  @trackSpan('CheckpointProposalJob.execute')
109
144
  public async execute(): Promise<Checkpoint | undefined> {
@@ -111,7 +146,7 @@ export class CheckpointProposalJob implements Traceable {
111
146
  // In fisherman mode, we simulate slashing but don't actually publish to L1
112
147
  // These are constant for the whole slot, so we only enqueue them once
113
148
  const votesPromises = new CheckpointVoter(
114
- this.slot,
149
+ this.targetSlot,
115
150
  this.publisher,
116
151
  this.attestorAddress,
117
152
  this.validatorClient,
@@ -122,14 +157,16 @@ export class CheckpointProposalJob implements Traceable {
122
157
  this.log,
123
158
  ).enqueueVotes();
124
159
 
125
- // Build and propose the checkpoint. This will enqueue the request on the publisher if a checkpoint is built.
126
- const checkpoint = await this.proposeCheckpoint();
160
+ // Build and propose the checkpoint. Builds blocks, broadcasts, collects attestations, and signs.
161
+ // Does NOT enqueue to L1 yet — that happens after the pipeline sleep.
162
+ const proposalResult = await this.proposeCheckpoint();
163
+ const checkpoint = proposalResult?.checkpoint;
127
164
 
128
165
  // Wait until the voting promises have resolved, so all requests are enqueued (not sent)
129
166
  await Promise.all(votesPromises);
130
167
 
131
168
  if (checkpoint) {
132
- this.metrics.recordBlockProposalSuccess();
169
+ this.metrics.recordCheckpointProposalSuccess();
133
170
  }
134
171
 
135
172
  // Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
@@ -138,47 +175,135 @@ export class CheckpointProposalJob implements Traceable {
138
175
  return;
139
176
  }
140
177
 
141
- // Then send everything to L1
142
- const l1Response = await this.publisher.sendRequests();
143
- const proposedAction = l1Response?.successfulActions.find(a => a === 'propose');
144
- if (proposedAction) {
145
- this.eventEmitter.emit('checkpoint-published', { checkpoint: this.checkpointNumber, slot: this.slot });
146
- const coinbase = checkpoint?.header.coinbase;
147
- await this.metrics.incFilledSlot(this.publisher.getSenderAddress().toString(), coinbase);
148
- return checkpoint;
149
- } else if (checkpoint) {
150
- this.eventEmitter.emit('checkpoint-publish-failed', { ...l1Response, slot: this.slot });
151
- return undefined;
178
+ // Enqueue the checkpoint for L1 submission
179
+ if (proposalResult) {
180
+ try {
181
+ await this.enqueueCheckpointForSubmission(proposalResult);
182
+ } catch (err) {
183
+ this.log.error(`Failed to enqueue checkpoint for L1 submission at slot ${this.targetSlot}`, err);
184
+ // Continue to sendRequestsAt so votes are still sent
185
+ }
152
186
  }
187
+
188
+ // Compute the earliest time to submit: pipeline slot start when pipelining, now otherwise.
189
+ const submitAfter = this.epochCache.isProposerPipeliningEnabled()
190
+ ? new Date(Number(getTimestampForSlot(this.targetSlot, this.l1Constants)) * 1000)
191
+ : new Date(this.dateProvider.now());
192
+
193
+ // TODO(https://github.com/AztecProtocol/aztec-packages/pull/21250): should discard the pending submission if a reorg occurs underneath
194
+
195
+ // Schedule L1 submission in the background so the work loop returns immediately.
196
+ // The publisher will sleep until submitAfter, then send the bundled requests.
197
+ // The promise is stored so it can be awaited during shutdown.
198
+ this.pendingL1Submission = this.publisher
199
+ .sendRequestsAt(submitAfter)
200
+ .then(async l1Response => {
201
+ const proposedAction = l1Response?.successfulActions.find(a => a === 'propose');
202
+ if (proposedAction) {
203
+ this.eventEmitter.emit('checkpoint-published', { checkpoint: this.checkpointNumber, slot: this.targetSlot });
204
+ const coinbase = checkpoint?.header.coinbase;
205
+ await this.metrics.incFilledSlot(this.publisher.getSenderAddress().toString(), coinbase);
206
+ } else if (checkpoint) {
207
+ this.eventEmitter.emit('checkpoint-publish-failed', { ...l1Response, slot: this.targetSlot });
208
+
209
+ if (this.epochCache.isProposerPipeliningEnabled()) {
210
+ this.metrics.recordPipelineDiscard();
211
+ }
212
+ }
213
+ })
214
+ .catch(err => {
215
+ this.log.error(`Background L1 submission failed for slot ${this.targetSlot}`, err);
216
+ if (checkpoint) {
217
+ this.eventEmitter.emit('checkpoint-publish-failed', { slot: this.targetSlot });
218
+
219
+ if (this.epochCache.isProposerPipeliningEnabled()) {
220
+ this.metrics.recordPipelineDiscard();
221
+ }
222
+ }
223
+ });
224
+
225
+ // Return the built checkpoint immediately — the work loop is now unblocked
226
+ return checkpoint;
227
+ }
228
+
229
+ /** Enqueues the checkpoint for L1 submission. Called after pipeline sleep in execute(). */
230
+ private async enqueueCheckpointForSubmission(result: CheckpointProposalResult): Promise<void> {
231
+ const { checkpoint, attestations, attestationsSignature } = result;
232
+
233
+ this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.targetSlot);
234
+ const aztecSlotDuration = this.l1Constants.slotDuration;
235
+ const submissionSlotStart = Number(getTimestampForSlot(this.targetSlot, this.l1Constants));
236
+ const txTimeoutAt = new Date((submissionSlotStart + aztecSlotDuration) * 1000);
237
+
238
+ // If we have been configured to potentially skip publishing checkpoint then roll the dice here
239
+ if (
240
+ this.config.skipPublishingCheckpointsPercent !== undefined &&
241
+ this.config.skipPublishingCheckpointsPercent > 0
242
+ ) {
243
+ const roll = Math.max(0, randomInt(100));
244
+ if (roll < this.config.skipPublishingCheckpointsPercent) {
245
+ this.log.warn(
246
+ `Skipping publishing proposal for checkpoint ${checkpoint.number}. Configured percentage: ${this.config.skipPublishingCheckpointsPercent}, generated value: ${roll}`,
247
+ );
248
+ return;
249
+ }
250
+ }
251
+
252
+ await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
253
+ txTimeoutAt,
254
+ forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber,
255
+ forceProposedFeeHeader: this.computedForceProposedFeeHeader,
256
+ });
153
257
  }
154
258
 
155
259
  @trackSpan('CheckpointProposalJob.proposeCheckpoint', function () {
156
260
  return {
157
261
  // nullish operator needed for tests
158
262
  [Attributes.COINBASE]: this.validatorClient.getCoinbaseForAttestor(this.attestorAddress)?.toString(),
159
- [Attributes.SLOT_NUMBER]: this.slot,
263
+ [Attributes.SLOT_NUMBER]: this.targetSlot,
160
264
  };
161
265
  })
162
- private async proposeCheckpoint(): Promise<Checkpoint | undefined> {
266
+ private async proposeCheckpoint(): Promise<CheckpointProposalResult | undefined> {
163
267
  try {
164
268
  // Get operator configured coinbase and fee recipient for this attestor
165
269
  const coinbase = this.validatorClient.getCoinbaseForAttestor(this.attestorAddress);
166
270
  const feeRecipient = this.validatorClient.getFeeRecipientForAttestor(this.attestorAddress);
167
271
 
168
272
  // Start the checkpoint
169
- this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.slot);
170
- this.metrics.incOpenSlot(this.slot, this.proposer?.toString() ?? 'unknown');
273
+ this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.targetSlot);
274
+ this.log.info(`Starting checkpoint proposal`, {
275
+ buildSlot: this.slotNow,
276
+ submissionSlot: this.targetSlot,
277
+ pipelining: this.epochCache.isProposerPipeliningEnabled(),
278
+ proposer: this.proposer?.toString(),
279
+ coinbase: coinbase.toString(),
280
+ });
281
+ this.metrics.incOpenSlot(this.targetSlot, this.proposer?.toString() ?? 'unknown');
171
282
 
172
283
  // Enqueues checkpoint invalidation (constant for the whole slot)
173
284
  if (this.invalidateCheckpoint && !this.config.skipInvalidateBlockAsProposer) {
174
285
  this.publisher.enqueueInvalidateCheckpoint(this.invalidateCheckpoint);
175
286
  }
176
287
 
177
- // Create checkpoint builder for the slot
288
+ // Create checkpoint builder for the slot.
289
+ // When pipelining, force the proposed checkpoint number and fee header to our parent so the
290
+ // fee computation sees the same chain tip that L1 will see once the previous pipelined checkpoint lands.
291
+ const isPipelining = this.epochCache.isProposerPipeliningEnabled();
292
+ const parentCheckpointNumber = isPipelining ? CheckpointNumber(this.checkpointNumber - 1) : undefined;
293
+
294
+ // Compute the parent's fee header override when pipelining
295
+ if (isPipelining && this.proposedCheckpointData) {
296
+ this.computedForceProposedFeeHeader = await this.computeForceProposedFeeHeader(parentCheckpointNumber!);
297
+ }
298
+
178
299
  const checkpointGlobalVariables = await this.globalsBuilder.buildCheckpointGlobalVariables(
179
300
  coinbase,
180
301
  feeRecipient,
181
- this.slot,
302
+ this.targetSlot,
303
+ {
304
+ forcePendingCheckpointNumber: parentCheckpointNumber,
305
+ forceProposedFeeHeader: this.computedForceProposedFeeHeader,
306
+ },
182
307
  );
183
308
 
184
309
  // Collect L1 to L2 messages for the checkpoint and compute their hash
@@ -186,18 +311,21 @@ export class CheckpointProposalJob implements Traceable {
186
311
  const inHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
187
312
 
188
313
  // Collect the out hashes of all the checkpoints before this one in the same epoch
189
- const previousCheckpoints = (await this.l2BlockSource.getCheckpointsForEpoch(this.epoch)).filter(
190
- c => c.number < this.checkpointNumber,
191
- );
192
- const previousCheckpointOutHashes = previousCheckpoints.map(c => c.getCheckpointOutHash());
314
+ const previousCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(this.targetEpoch))
315
+ .filter(c => c.checkpointNumber < this.checkpointNumber)
316
+ .map(c => c.checkpointOutHash);
317
+
318
+ // Get the fee asset price modifier from the oracle
319
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
193
320
 
194
321
  // Create a long-lived forked world state for the checkpoint builder
195
- using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
322
+ await using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
196
323
 
197
324
  // Create checkpoint builder for the entire slot
198
325
  const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(
199
326
  this.checkpointNumber,
200
327
  checkpointGlobalVariables,
328
+ feeAssetPriceModifier,
201
329
  l1ToL2Messages,
202
330
  previousCheckpointOutHashes,
203
331
  fork,
@@ -217,6 +345,7 @@ export class CheckpointProposalJob implements Traceable {
217
345
 
218
346
  let blocksInCheckpoint: L2Block[] = [];
219
347
  let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
348
+ const checkpointBuildTimer = new Timer();
220
349
 
221
350
  try {
222
351
  // Main loop: build blocks for the checkpoint
@@ -232,48 +361,74 @@ export class CheckpointProposalJob implements Traceable {
232
361
  // These errors are expected in HA mode, so we yield and let another HA node handle the slot
233
362
  // The only distinction between the 2 errors is SlashingProtectionError throws when the payload is different,
234
363
  // which is normal for block building (may have picked different txs)
235
- if (err instanceof DutyAlreadySignedError) {
236
- this.log.info(`Checkpoint proposal for slot ${this.slot} already signed by another HA node, yielding`, {
237
- slot: this.slot,
238
- signedByNode: err.signedByNode,
239
- });
240
- return undefined;
241
- }
242
- if (err instanceof SlashingProtectionError) {
243
- this.log.info(`Checkpoint proposal for slot ${this.slot} blocked by slashing protection, yielding`, {
244
- slot: this.slot,
245
- existingMessageHash: err.existingMessageHash,
246
- attemptedMessageHash: err.attemptedMessageHash,
247
- });
364
+ if (this.handleHASigningError(err, 'Block proposal')) {
248
365
  return undefined;
249
366
  }
250
367
  throw err;
251
368
  }
252
369
 
253
370
  if (blocksInCheckpoint.length === 0) {
254
- this.log.warn(`No blocks were built for slot ${this.slot}`, { slot: this.slot });
255
- this.eventEmitter.emit('checkpoint-empty', { slot: this.slot });
371
+ this.log.warn(`No blocks were built for slot ${this.targetSlot}`, { slot: this.targetSlot });
372
+ this.eventEmitter.emit('checkpoint-empty', { slot: this.targetSlot });
373
+ return undefined;
374
+ }
375
+
376
+ const minBlocksForCheckpoint = this.config.minBlocksForCheckpoint;
377
+ if (minBlocksForCheckpoint !== undefined && blocksInCheckpoint.length < minBlocksForCheckpoint) {
378
+ this.log.warn(
379
+ `Checkpoint has fewer blocks than minimum (${blocksInCheckpoint.length} < ${minBlocksForCheckpoint}), skipping proposal`,
380
+ { slot: this.targetSlot, blocksBuilt: blocksInCheckpoint.length, minBlocksForCheckpoint },
381
+ );
256
382
  return undefined;
257
383
  }
258
384
 
259
385
  // Assemble and broadcast the checkpoint proposal, including the last block that was not
260
386
  // broadcasted yet, and wait to collect the committee attestations.
261
- this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.slot);
387
+ this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.targetSlot);
262
388
  const checkpoint = await checkpointBuilder.completeCheckpoint();
263
389
 
390
+ // Final validation: per-block limits are only checked if the operator set them explicitly.
391
+ // Otherwise, checkpoint-level budgets were already enforced by the redistribution logic.
392
+ try {
393
+ validateCheckpoint(checkpoint, {
394
+ rollupManaLimit: this.l1Constants.rollupManaLimit,
395
+ maxL2BlockGas: this.config.maxL2BlockGas,
396
+ maxDABlockGas: this.config.maxDABlockGas,
397
+ maxTxsPerBlock: this.config.maxTxsPerBlock,
398
+ maxTxsPerCheckpoint: this.config.maxTxsPerCheckpoint,
399
+ });
400
+ } catch (err) {
401
+ this.log.error(`Built an invalid checkpoint at slot ${this.slotNow} (skipping proposal)`, err, {
402
+ checkpoint: checkpoint.header.toInspect(),
403
+ });
404
+ return undefined;
405
+ }
406
+
407
+ // Record checkpoint-level build metrics
408
+ this.metrics.recordCheckpointBuild(
409
+ checkpointBuildTimer.ms(),
410
+ blocksInCheckpoint.length,
411
+ checkpoint.getStats().txCount,
412
+ Number(checkpoint.header.totalManaUsed.toBigInt()),
413
+ );
414
+
264
415
  // Do not collect attestations nor publish to L1 in fisherman mode
265
416
  if (this.config.fishermanMode) {
266
417
  this.log.info(
267
- `Built checkpoint for slot ${this.slot} with ${blocksInCheckpoint.length} blocks. ` +
418
+ `Built checkpoint for slot ${this.targetSlot} with ${blocksInCheckpoint.length} blocks. ` +
268
419
  `Skipping proposal in fisherman mode.`,
269
420
  {
270
- slot: this.slot,
421
+ slot: this.targetSlot,
271
422
  checkpoint: checkpoint.header.toInspect(),
272
423
  blocksBuilt: blocksInCheckpoint.length,
273
424
  },
274
425
  );
275
426
  this.metrics.recordCheckpointSuccess();
276
- return checkpoint;
427
+ return {
428
+ checkpoint,
429
+ attestations: CommitteeAttestationsAndSigners.empty(),
430
+ attestationsSignature: Signature.empty(),
431
+ };
277
432
  }
278
433
 
279
434
  // Include the block pending broadcast in the checkpoint proposal if any
@@ -287,6 +442,7 @@ export class CheckpointProposalJob implements Traceable {
287
442
  const proposal = await this.validatorClient.createCheckpointProposal(
288
443
  checkpoint.header,
289
444
  checkpoint.archive.root,
445
+ feeAssetPriceModifier,
290
446
  lastBlock,
291
447
  this.proposer,
292
448
  checkpointProposalOptions,
@@ -295,7 +451,7 @@ export class CheckpointProposalJob implements Traceable {
295
451
  const blockProposedAt = this.dateProvider.now();
296
452
  await this.p2pClient.broadcastCheckpointProposal(proposal);
297
453
 
298
- this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.slot);
454
+ this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.targetSlot);
299
455
  const attestations = await this.waitForAttestations(proposal);
300
456
  const blockAttestedAt = this.dateProvider.now();
301
457
 
@@ -308,48 +464,27 @@ export class CheckpointProposalJob implements Traceable {
308
464
  attestationsSignature = await this.validatorClient.signAttestationsAndSigners(
309
465
  attestations,
310
466
  signer,
311
- this.slot,
467
+ this.targetSlot,
312
468
  this.checkpointNumber,
313
469
  );
314
470
  } catch (err) {
315
471
  // We shouldn't really get here since we yield to another HA node
316
- // as soon as we see these errors when creating block proposals.
317
- if (err instanceof DutyAlreadySignedError) {
318
- this.log.info(`Attestations signature for slot ${this.slot} already signed by another HA node, yielding`, {
319
- slot: this.slot,
320
- signedByNode: err.signedByNode,
321
- });
322
- return undefined;
323
- }
324
- if (err instanceof SlashingProtectionError) {
325
- this.log.info(`Attestations signature for slot ${this.slot} blocked by slashing protection, yielding`, {
326
- slot: this.slot,
327
- existingMessageHash: err.existingMessageHash,
328
- attemptedMessageHash: err.attemptedMessageHash,
329
- });
472
+ // as soon as we see these errors when creating block or checkpoint proposals.
473
+ if (this.handleHASigningError(err, 'Attestations signature')) {
330
474
  return undefined;
331
475
  }
332
476
  throw err;
333
477
  }
334
478
 
335
- // Enqueue publishing the checkpoint to L1
336
- this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.slot);
337
- const aztecSlotDuration = this.l1Constants.slotDuration;
338
- const slotStartBuildTimestamp = this.getSlotStartBuildTimestamp();
339
- const txTimeoutAt = new Date((slotStartBuildTimestamp + aztecSlotDuration) * 1000);
340
- await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
341
- txTimeoutAt,
342
- forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber,
343
- });
344
-
345
- return checkpoint;
479
+ // Return the result for the caller to enqueue after the pipeline sleep
480
+ return { checkpoint, attestations, attestationsSignature };
346
481
  } catch (err) {
347
482
  if (err && (err instanceof DutyAlreadySignedError || err instanceof SlashingProtectionError)) {
348
483
  // swallow this error. It's already been logged by a function deeper in the stack
349
484
  return undefined;
350
485
  }
351
486
 
352
- this.log.error(`Error building checkpoint at slot ${this.slot}`, err);
487
+ this.log.error(`Error building checkpoint at slot ${this.targetSlot}`, err);
353
488
  return undefined;
354
489
  }
355
490
  }
@@ -371,9 +506,6 @@ export class CheckpointProposalJob implements Traceable {
371
506
  const txHashesAlreadyIncluded = new Set<string>();
372
507
  const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
373
508
 
374
- // Remaining blob fields available for blocks (checkpoint end marker already subtracted)
375
- let remainingBlobFields = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB - NUM_CHECKPOINT_END_MARKER_FIELDS;
376
-
377
509
  // Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
378
510
  let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
379
511
 
@@ -387,7 +519,7 @@ export class CheckpointProposalJob implements Traceable {
387
519
 
388
520
  if (!timingInfo.canStart) {
389
521
  this.log.debug(`Not enough time left in slot to start another block`, {
390
- slot: this.slot,
522
+ slot: this.targetSlot,
391
523
  blocksBuilt,
392
524
  secondsIntoSlot,
393
525
  });
@@ -406,7 +538,6 @@ export class CheckpointProposalJob implements Traceable {
406
538
  blockNumber,
407
539
  indexWithinCheckpoint,
408
540
  txHashesAlreadyIncluded,
409
- remainingBlobFields,
410
541
  });
411
542
 
412
543
  // TODO(palla/mbps): Review these conditions. We may want to keep trying in some scenarios.
@@ -423,8 +554,8 @@ export class CheckpointProposalJob implements Traceable {
423
554
  } else if ('error' in buildResult) {
424
555
  // If there was an error building the block, just exit the loop and give up the rest of the slot
425
556
  if (!(buildResult.error instanceof SequencerInterruptedError)) {
426
- this.log.warn(`Halting block building for slot ${this.slot}`, {
427
- slot: this.slot,
557
+ this.log.warn(`Halting block building for slot ${this.targetSlot}`, {
558
+ slot: this.targetSlot,
428
559
  blocksBuilt,
429
560
  error: buildResult.error,
430
561
  });
@@ -432,26 +563,16 @@ export class CheckpointProposalJob implements Traceable {
432
563
  break;
433
564
  }
434
565
 
435
- const { block, usedTxs, remainingBlobFields: newRemainingBlobFields } = buildResult;
566
+ const { block, usedTxs } = buildResult;
436
567
  blocksInCheckpoint.push(block);
437
-
438
- // Update remaining blob fields for the next block
439
- remainingBlobFields = newRemainingBlobFields;
440
-
441
- // Sync the proposed block to the archiver to make it available
442
- // Note that the checkpoint builder uses its own fork so it should not need to wait for this syncing
443
- // Eventually we should refactor the checkpoint builder to not need a separate long-lived fork
444
- // Fire and forget - don't block the critical path, but log errors
445
- this.syncProposedBlockToArchiver(block).catch(err => {
446
- this.log.error(`Failed to sync proposed block ${block.number} to archiver`, { blockNumber: block.number, err });
447
- });
448
-
449
568
  usedTxs.forEach(tx => txHashesAlreadyIncluded.add(tx.txHash.toString()));
450
569
 
451
- // If this is the last block, exit the loop now so we start collecting attestations
570
+ // If this is the last block, sync it to the archiver and exit the loop
571
+ // so we can build the checkpoint and start collecting attestations.
452
572
  if (timingInfo.isLastBlock) {
453
- this.log.verbose(`Completed final block ${blockNumber} for slot ${this.slot}`, {
454
- slot: this.slot,
573
+ await this.syncProposedBlockToArchiver(block);
574
+ this.log.verbose(`Completed final block ${blockNumber} for slot ${this.targetSlot}`, {
575
+ slot: this.targetSlot,
455
576
  blockNumber,
456
577
  blocksBuilt,
457
578
  });
@@ -459,38 +580,61 @@ export class CheckpointProposalJob implements Traceable {
459
580
  break;
460
581
  }
461
582
 
462
- // For non-last blocks, broadcast the block proposal (unless we're in fisherman mode)
463
- // If the block is the last one, we'll broadcast it along with the checkpoint at the end of the loop
464
- if (!this.config.fishermanMode) {
465
- const proposal = await this.validatorClient.createBlockProposal(
466
- block.header,
467
- block.indexWithinCheckpoint,
468
- inHash,
469
- block.archive.root,
470
- usedTxs,
471
- this.proposer,
472
- blockProposalOptions,
473
- );
474
- await this.p2pClient.broadcastProposal(proposal);
475
- }
583
+ // Broadcast the block proposal (unless we're in fisherman mode) unless the block is the last one,
584
+ // in which case we'll broadcast it along with the checkpoint at the end of the loop.
585
+ // Note that we only send the block to the archiver if we manage to create the proposal, so if there's
586
+ // a HA error we don't pollute our archiver with a block that won't make it to the chain.
587
+ const proposal = await this.createBlockProposal(block, inHash, usedTxs, blockProposalOptions);
588
+
589
+ // Sync the proposed block to the archiver to make it available, only after we've managed to sign the proposal.
590
+ // We wait for the sync to succeed, as this helps catch consistency errors, even if it means we lose some time for block-building.
591
+ // If this throws, we abort the entire checkpoint.
592
+ await this.syncProposedBlockToArchiver(block);
593
+
594
+ // Once we have a signed proposal and the archiver agreed with our proposed block, then we broadcast it.
595
+ proposal && (await this.p2pClient.broadcastProposal(proposal));
476
596
 
477
597
  // Wait until the next block's start time
478
598
  await this.waitUntilNextSubslot(timingInfo.deadline);
479
599
  }
480
600
 
481
- this.log.verbose(`Block building loop completed for slot ${this.slot}`, {
482
- slot: this.slot,
601
+ this.log.verbose(`Block building loop completed for slot ${this.targetSlot}`, {
602
+ slot: this.targetSlot,
483
603
  blocksBuilt: blocksInCheckpoint.length,
484
604
  });
485
605
 
486
606
  return { blocksInCheckpoint, blockPendingBroadcast };
487
607
  }
488
608
 
609
+ /** Creates a block proposal for a given block via the validator client (unless in fisherman mode) */
610
+ private createBlockProposal(
611
+ block: L2Block,
612
+ inHash: Fr,
613
+ usedTxs: Tx[],
614
+ blockProposalOptions: BlockProposalOptions,
615
+ ): Promise<BlockProposal | undefined> {
616
+ if (this.config.fishermanMode) {
617
+ this.log.info(`Skipping block proposal for block ${block.number} in fisherman mode`);
618
+ return Promise.resolve(undefined);
619
+ }
620
+ return this.validatorClient.createBlockProposal(
621
+ block.header,
622
+ block.indexWithinCheckpoint,
623
+ inHash,
624
+ block.archive.root,
625
+ usedTxs,
626
+ this.proposer,
627
+ blockProposalOptions,
628
+ );
629
+ }
630
+
489
631
  /** Sleeps until it is time to produce the next block in the slot */
490
632
  @trackSpan('CheckpointProposalJob.waitUntilNextSubslot')
491
633
  private async waitUntilNextSubslot(nextSubslotStart: number) {
492
- this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.slot);
493
- this.log.verbose(`Waiting until time for the next block at ${nextSubslotStart}s into slot`, { slot: this.slot });
634
+ this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.targetSlot);
635
+ this.log.verbose(`Waiting until time for the next block at ${nextSubslotStart}s into slot`, {
636
+ slot: this.targetSlot,
637
+ });
494
638
  await this.waitUntilTimeInSlot(nextSubslotStart);
495
639
  }
496
640
 
@@ -505,34 +649,25 @@ export class CheckpointProposalJob implements Traceable {
505
649
  indexWithinCheckpoint: IndexWithinCheckpoint;
506
650
  buildDeadline: Date | undefined;
507
651
  txHashesAlreadyIncluded: Set<string>;
508
- remainingBlobFields: number;
509
652
  },
510
- ): Promise<{ block: L2Block; usedTxs: Tx[]; remainingBlobFields: number } | { error: Error } | undefined> {
511
- const {
512
- blockTimestamp,
513
- forceCreate,
514
- blockNumber,
515
- indexWithinCheckpoint,
516
- buildDeadline,
517
- txHashesAlreadyIncluded,
518
- remainingBlobFields,
519
- } = opts;
653
+ ): Promise<{ block: L2Block; usedTxs: Tx[] } | { error: Error } | undefined> {
654
+ const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded } =
655
+ opts;
520
656
 
521
657
  this.log.verbose(
522
- `Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.slot}`,
658
+ `Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.targetSlot}`,
523
659
  { ...checkpointBuilder.getConstantData(), ...opts },
524
660
  );
525
661
 
526
662
  try {
527
663
  // Wait until we have enough txs to build the block
528
- const minTxs = this.config.minTxsPerBlock;
529
- const { availableTxs, canStartBuilding } = await this.waitForMinTxs(opts);
664
+ const { availableTxs, canStartBuilding, minTxs } = await this.waitForMinTxs(opts);
530
665
  if (!canStartBuilding) {
531
666
  this.log.warn(
532
- `Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (got ${availableTxs} txs but needs ${minTxs})`,
533
- { blockNumber, slot: this.slot, indexWithinCheckpoint },
667
+ `Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.targetSlot} (got ${availableTxs} txs but needs ${minTxs})`,
668
+ { blockNumber, slot: this.targetSlot, indexWithinCheckpoint },
534
669
  );
535
- this.eventEmitter.emit('block-tx-count-check-failed', { minTxs, availableTxs, slot: this.slot });
670
+ this.eventEmitter.emit('block-tx-count-check-failed', { minTxs, availableTxs, slot: this.targetSlot });
536
671
  this.metrics.recordBlockProposalFailed('insufficient_txs');
537
672
  return undefined;
538
673
  }
@@ -540,29 +675,36 @@ export class CheckpointProposalJob implements Traceable {
540
675
  // Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
541
676
  // just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
542
677
  const pendingTxs = filter(
543
- this.p2pClient.iteratePendingTxs(),
678
+ this.p2pClient.iterateEligiblePendingTxs(),
544
679
  tx => !txHashesAlreadyIncluded.has(tx.txHash.toString()),
545
680
  );
546
681
 
547
682
  this.log.debug(
548
- `Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.slot} with ${availableTxs} available txs`,
549
- { slot: this.slot, blockNumber, indexWithinCheckpoint },
683
+ `Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.targetSlot} with ${availableTxs} available txs`,
684
+ { slot: this.targetSlot, blockNumber, indexWithinCheckpoint },
550
685
  );
551
- this.setStateFn(SequencerState.CREATING_BLOCK, this.slot);
686
+ this.setStateFn(SequencerState.CREATING_BLOCK, this.targetSlot);
552
687
 
553
- // Calculate blob fields limit for txs (remaining capacity - this block's end overhead)
554
- const blockEndOverhead = getNumBlockEndBlobFields(indexWithinCheckpoint === 0);
555
- const maxBlobFieldsForTxs = remainingBlobFields - blockEndOverhead;
556
-
557
- const blockBuilderOptions: PublicProcessorLimits = {
688
+ // Per-block limits are operator overrides (from SEQ_MAX_L2_BLOCK_GAS etc.) further capped
689
+ // by remaining checkpoint-level budgets inside CheckpointBuilder before each block is built.
690
+ // minValidTxs is passed into the builder so it can reject the block *before* updating state.
691
+ const minValidTxs = forceCreate ? 0 : (this.config.minValidTxsPerBlock ?? minTxs);
692
+ const blockBuilderOptions: BlockBuilderOptions = {
558
693
  maxTransactions: this.config.maxTxsPerBlock,
559
- maxBlockSize: this.config.maxBlockSizeInBytes,
560
- maxBlockGas: new Gas(this.config.maxDABlockGas, this.config.maxL2BlockGas),
561
- maxBlobFields: maxBlobFieldsForTxs,
694
+ maxBlockGas:
695
+ this.config.maxL2BlockGas !== undefined || this.config.maxDABlockGas !== undefined
696
+ ? new Gas(this.config.maxDABlockGas ?? Infinity, this.config.maxL2BlockGas ?? Infinity)
697
+ : undefined,
562
698
  deadline: buildDeadline,
699
+ isBuildingProposal: true,
700
+ minValidTxs,
701
+ maxBlocksPerCheckpoint: this.timetable.maxNumberOfBlocks,
702
+ perBlockAllocationMultiplier: this.config.perBlockAllocationMultiplier,
563
703
  };
564
704
 
565
- // Actually build the block by executing txs
705
+ // Actually build the block by executing txs. The builder throws InsufficientValidTxsError
706
+ // if the number of successfully processed txs is below minValidTxs, ensuring state is not
707
+ // updated for blocks that will be discarded.
566
708
  const buildResult = await this.buildSingleBlockWithCheckpointBuilder(
567
709
  checkpointBuilder,
568
710
  pendingTxs,
@@ -574,22 +716,27 @@ export class CheckpointProposalJob implements Traceable {
574
716
  // If any txs failed during execution, drop them from the mempool so we don't pick them up again
575
717
  await this.dropFailedTxsFromP2P(buildResult.failedTxs);
576
718
 
577
- // Check if we have created a block with enough txs. If there were invalid txs in the pool, or if execution took
578
- // too long, then we may not get to minTxsPerBlock after executing public functions.
579
- const minValidTxs = this.config.minValidTxsPerBlock ?? minTxs;
580
- const numTxs = buildResult.status === 'no-valid-txs' ? 0 : buildResult.numTxs;
581
- if (buildResult.status === 'no-valid-txs' || (!forceCreate && numTxs < minValidTxs)) {
719
+ if (buildResult.status === 'insufficient-valid-txs') {
582
720
  this.log.warn(
583
- `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed`,
584
- { slot: this.slot, blockNumber, numTxs, indexWithinCheckpoint, minValidTxs, buildResult: buildResult.status },
721
+ `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.targetSlot} has too few valid txs to be proposed`,
722
+ {
723
+ slot: this.targetSlot,
724
+ blockNumber,
725
+ numTxs: buildResult.processedCount,
726
+ indexWithinCheckpoint,
727
+ minValidTxs,
728
+ },
585
729
  );
586
- this.eventEmitter.emit('block-build-failed', { reason: `Insufficient valid txs`, slot: this.slot });
730
+ this.eventEmitter.emit('block-build-failed', {
731
+ reason: `Insufficient valid txs`,
732
+ slot: this.targetSlot,
733
+ });
587
734
  this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
588
735
  return undefined;
589
736
  }
590
737
 
591
738
  // Block creation succeeded, emit stats and metrics
592
- const { publicGas, block, publicProcessorDuration, usedTxs, usedTxBlobFields, blockBuildDuration } = buildResult;
739
+ const { block, publicProcessorDuration, usedTxs, blockBuildDuration, numTxs } = buildResult;
593
740
 
594
741
  const blockStats = {
595
742
  eventName: 'l2-block-built',
@@ -600,33 +747,42 @@ export class CheckpointProposalJob implements Traceable {
600
747
 
601
748
  const blockHash = await block.hash();
602
749
  const txHashes = block.body.txEffects.map(tx => tx.txHash);
603
- const manaPerSec = publicGas.l2Gas / (blockBuildDuration / 1000);
750
+ const manaPerSec = block.header.totalManaUsed.toNumberUnsafe() / (blockBuildDuration / 1000);
604
751
 
605
752
  this.log.info(
606
- `Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.slot} with ${numTxs} txs`,
753
+ `Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.targetSlot} with ${numTxs} txs`,
607
754
  { blockHash, txHashes, manaPerSec, ...blockStats },
608
755
  );
609
756
 
610
- this.eventEmitter.emit('block-proposed', { blockNumber: block.number, slot: this.slot });
611
- this.metrics.recordBuiltBlock(blockBuildDuration, publicGas.l2Gas);
757
+ // `slot` is the target/submission slot (may be one ahead when pipelining),
758
+ // `buildSlot` is the wall-clock slot during which the block was actually built.
759
+ this.eventEmitter.emit('block-proposed', {
760
+ blockNumber: block.number,
761
+ slot: this.targetSlot,
762
+ buildSlot: this.slotNow,
763
+ });
764
+ this.metrics.recordBuiltBlock(blockBuildDuration, block.header.totalManaUsed.toNumberUnsafe());
612
765
 
613
- return { block, usedTxs, remainingBlobFields: maxBlobFieldsForTxs - usedTxBlobFields };
766
+ return { block, usedTxs };
614
767
  } catch (err: any) {
615
- this.eventEmitter.emit('block-build-failed', { reason: err.message, slot: this.slot });
616
- this.log.error(`Error building block`, err, { blockNumber, slot: this.slot });
768
+ this.eventEmitter.emit('block-build-failed', {
769
+ reason: err.message,
770
+ slot: this.targetSlot,
771
+ });
772
+ this.log.error(`Error building block`, err, { blockNumber, slot: this.targetSlot });
617
773
  this.metrics.recordBlockProposalFailed(err.name || 'unknown_error');
618
774
  this.metrics.recordFailedBlock();
619
775
  return { error: err };
620
776
  }
621
777
  }
622
778
 
623
- /** Uses the checkpoint builder to build a block, catching specific txs */
779
+ /** Uses the checkpoint builder to build a block, catching InsufficientValidTxsError. */
624
780
  private async buildSingleBlockWithCheckpointBuilder(
625
781
  checkpointBuilder: CheckpointBuilder,
626
782
  pendingTxs: AsyncIterable<Tx>,
627
783
  blockNumber: BlockNumber,
628
784
  blockTimestamp: bigint,
629
- blockBuilderOptions: PublicProcessorLimits,
785
+ blockBuilderOptions: BlockBuilderOptions,
630
786
  ) {
631
787
  try {
632
788
  const workTimer = new Timer();
@@ -634,8 +790,12 @@ export class CheckpointProposalJob implements Traceable {
634
790
  const blockBuildDuration = workTimer.ms();
635
791
  return { ...result, blockBuildDuration, status: 'success' as const };
636
792
  } catch (err: unknown) {
637
- if (isErrorClass(err, NoValidTxsError)) {
638
- return { failedTxs: err.failedTxs, status: 'no-valid-txs' as const };
793
+ if (isErrorClass(err, InsufficientValidTxsError)) {
794
+ return {
795
+ failedTxs: err.failedTxs,
796
+ processedCount: err.processedCount,
797
+ status: 'insufficient-valid-txs' as const,
798
+ };
639
799
  }
640
800
  throw err;
641
801
  }
@@ -648,7 +808,7 @@ export class CheckpointProposalJob implements Traceable {
648
808
  blockNumber: BlockNumber;
649
809
  indexWithinCheckpoint: IndexWithinCheckpoint;
650
810
  buildDeadline: Date | undefined;
651
- }): Promise<{ canStartBuilding: boolean; availableTxs: number }> {
811
+ }): Promise<{ canStartBuilding: boolean; availableTxs: number; minTxs: number }> {
652
812
  const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
653
813
 
654
814
  // We only allow a block with 0 txs in the first block of the checkpoint
@@ -665,20 +825,20 @@ export class CheckpointProposalJob implements Traceable {
665
825
  // If we're past deadline, or we have no deadline, give up
666
826
  const now = this.dateProvider.nowAsDate();
667
827
  if (startBuildingDeadline === undefined || now >= startBuildingDeadline) {
668
- return { canStartBuilding: false, availableTxs: availableTxs };
828
+ return { canStartBuilding: false, availableTxs, minTxs };
669
829
  }
670
830
 
671
831
  // Wait a bit before checking again
672
- this.setStateFn(SequencerState.WAITING_FOR_TXS, this.slot);
832
+ this.setStateFn(SequencerState.WAITING_FOR_TXS, this.targetSlot);
673
833
  this.log.verbose(
674
- `Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (have ${availableTxs} but need ${minTxs})`,
675
- { blockNumber, slot: this.slot, indexWithinCheckpoint },
834
+ `Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.targetSlot} (have ${availableTxs} but need ${minTxs})`,
835
+ { blockNumber, slot: this.targetSlot, indexWithinCheckpoint },
676
836
  );
677
- await sleep(TXS_POLLING_MS);
837
+ await this.waitForTxsPollingInterval();
678
838
  availableTxs = await this.p2pClient.getPendingTxCount();
679
839
  }
680
840
 
681
- return { canStartBuilding: true, availableTxs };
841
+ return { canStartBuilding: true, availableTxs, minTxs };
682
842
  }
683
843
 
684
844
  /**
@@ -704,7 +864,7 @@ export class CheckpointProposalJob implements Traceable {
704
864
  this.log.debug(`Attesting committee length is ${committee.length}`, { committee });
705
865
  }
706
866
 
707
- const numberOfRequiredAttestations = Math.floor((committee.length * 2) / 3) + 1;
867
+ const numberOfRequiredAttestations = computeQuorum(committee.length);
708
868
 
709
869
  if (this.config.skipCollectingAttestations) {
710
870
  this.log.warn('Skipping attestation collection as per config (attesting with own keys only)');
@@ -730,11 +890,28 @@ export class CheckpointProposalJob implements Traceable {
730
890
 
731
891
  collectedAttestationsCount = attestations.length;
732
892
 
893
+ // Trim attestations to minimum required to save L1 calldata gas
894
+ const localAddresses = this.validatorClient.getValidatorAddresses();
895
+ const trimmed = trimAttestations(
896
+ attestations,
897
+ numberOfRequiredAttestations,
898
+ this.attestorAddress,
899
+ localAddresses,
900
+ );
901
+ if (trimmed.length < attestations.length) {
902
+ this.log.debug(`Trimmed attestations from ${attestations.length} to ${trimmed.length} for L1 submission`);
903
+ }
904
+
733
905
  // Rollup contract requires that the signatures are provided in the order of the committee
734
- const sorted = orderAttestations(attestations, committee);
906
+ const sorted = orderAttestations(trimmed, committee);
735
907
 
736
908
  // Manipulate the attestations if we've been configured to do so
737
- if (this.config.injectFakeAttestation || this.config.shuffleAttestationOrdering) {
909
+ if (
910
+ this.config.injectFakeAttestation ||
911
+ this.config.injectHighSValueAttestation ||
912
+ this.config.injectUnrecoverableSignatureAttestation ||
913
+ this.config.shuffleAttestationOrdering
914
+ ) {
738
915
  return this.manipulateAttestations(proposal.slotNumber, epoch, seed, committee, sorted);
739
916
  }
740
917
 
@@ -763,7 +940,11 @@ export class CheckpointProposalJob implements Traceable {
763
940
  this.epochCache.computeProposerIndex(slotNumber, epoch, seed, BigInt(committee.length)),
764
941
  );
765
942
 
766
- if (this.config.injectFakeAttestation) {
943
+ if (
944
+ this.config.injectFakeAttestation ||
945
+ this.config.injectHighSValueAttestation ||
946
+ this.config.injectUnrecoverableSignatureAttestation
947
+ ) {
767
948
  // Find non-empty attestations that are not from the proposer
768
949
  const nonProposerIndices: number[] = [];
769
950
  for (let i = 0; i < attestations.length; i++) {
@@ -773,8 +954,20 @@ export class CheckpointProposalJob implements Traceable {
773
954
  }
774
955
  if (nonProposerIndices.length > 0) {
775
956
  const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
776
- this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
777
- unfreeze(attestations[targetIndex]).signature = Signature.random();
957
+ if (this.config.injectHighSValueAttestation) {
958
+ this.log.warn(
959
+ `Injecting high-s value attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
960
+ );
961
+ unfreeze(attestations[targetIndex]).signature = flipSignature(attestations[targetIndex].signature);
962
+ } else if (this.config.injectUnrecoverableSignatureAttestation) {
963
+ this.log.warn(
964
+ `Injecting unrecoverable signature attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
965
+ );
966
+ unfreeze(attestations[targetIndex]).signature = generateUnrecoverableSignature();
967
+ } else {
968
+ this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
969
+ unfreeze(attestations[targetIndex]).signature = generateRecoverableSignature();
970
+ }
778
971
  }
779
972
  return new CommitteeAttestationsAndSigners(attestations);
780
973
  }
@@ -783,11 +976,20 @@ export class CheckpointProposalJob implements Traceable {
783
976
  this.log.warn(`Shuffling attestation ordering in checkpoint for slot ${slotNumber} (proposer #${proposerIndex})`);
784
977
 
785
978
  const shuffled = [...attestations];
786
- const [i, j] = [(proposerIndex + 1) % shuffled.length, (proposerIndex + 2) % shuffled.length];
787
- const valueI = shuffled[i];
788
- const valueJ = shuffled[j];
789
- shuffled[i] = valueJ;
790
- shuffled[j] = valueI;
979
+
980
+ // Find two non-proposer positions that both have non-empty signatures to swap.
981
+ // This ensures the bitmap doesn't change, so the MaliciousCommitteeAttestationsAndSigners
982
+ // signers array stays correctly aligned with L1's committee reconstruction.
983
+ const swappable: number[] = [];
984
+ for (let k = 0; k < shuffled.length; k++) {
985
+ if (!shuffled[k].signature.isEmpty() && k !== proposerIndex) {
986
+ swappable.push(k);
987
+ }
988
+ }
989
+ if (swappable.length >= 2) {
990
+ const [i, j] = [swappable[0], swappable[1]];
991
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
992
+ }
791
993
 
792
994
  const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
793
995
  return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
@@ -803,7 +1005,7 @@ export class CheckpointProposalJob implements Traceable {
803
1005
  const failedTxData = failedTxs.map(fail => fail.tx);
804
1006
  const failedTxHashes = failedTxData.map(tx => tx.getTxHash());
805
1007
  this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
806
- await this.p2pClient.deleteTxs(failedTxHashes);
1008
+ await this.p2pClient.handleFailedExecution(failedTxHashes);
807
1009
  }
808
1010
 
809
1011
  /**
@@ -812,7 +1014,7 @@ export class CheckpointProposalJob implements Traceable {
812
1014
  * would never receive its own block without this explicit sync.
813
1015
  */
814
1016
  private async syncProposedBlockToArchiver(block: L2Block): Promise<void> {
815
- if (this.config.skipPushProposedBlocksToArchiver !== false) {
1017
+ if (this.config.skipPushProposedBlocksToArchiver) {
816
1018
  this.log.warn(`Skipping push of proposed block ${block.number} to archiver`, {
817
1019
  blockNumber: block.number,
818
1020
  slot: block.header.globalVariables.slotNumber,
@@ -830,27 +1032,99 @@ export class CheckpointProposalJob implements Traceable {
830
1032
  private async handleCheckpointEndAsFisherman(checkpoint: Checkpoint | undefined) {
831
1033
  // Perform L1 fee analysis before clearing requests
832
1034
  // The callback is invoked asynchronously after the next block is mined
833
- const feeAnalysis = await this.publisher.analyzeL1Fees(this.slot, analysis =>
1035
+ const feeAnalysis = await this.publisher.analyzeL1Fees(this.targetSlot, analysis =>
834
1036
  this.metrics.recordFishermanFeeAnalysis(analysis),
835
1037
  );
836
1038
 
837
1039
  if (checkpoint) {
838
- this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.slot}`, {
1040
+ this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.targetSlot}`, {
839
1041
  ...checkpoint.toCheckpointInfo(),
840
1042
  ...checkpoint.getStats(),
841
1043
  feeAnalysisId: feeAnalysis?.id,
842
1044
  });
843
1045
  } else {
844
- this.log.warn(`Validation block building FAILED for slot ${this.slot}`, {
845
- slot: this.slot,
1046
+ this.log.warn(`Validation block building FAILED for slot ${this.targetSlot}`, {
1047
+ slot: this.targetSlot,
846
1048
  feeAnalysisId: feeAnalysis?.id,
847
1049
  });
848
- this.metrics.recordBlockProposalFailed('block_build_failed');
1050
+ this.metrics.recordCheckpointProposalFailed('block_build_failed');
849
1051
  }
850
1052
 
851
1053
  this.publisher.clearPendingRequests();
852
1054
  }
853
1055
 
1056
+ /**
1057
+ * Helper to handle HA double-signing errors. Returns true if the error was handled (caller should yield).
1058
+ */
1059
+ private handleHASigningError(err: any, errorContext: string): boolean {
1060
+ if (err instanceof DutyAlreadySignedError) {
1061
+ this.log.info(`${errorContext} for slot ${this.targetSlot} already signed by another HA node, yielding`, {
1062
+ slot: this.targetSlot,
1063
+ signedByNode: err.signedByNode,
1064
+ });
1065
+ return true;
1066
+ }
1067
+ if (err instanceof SlashingProtectionError) {
1068
+ this.log.info(`${errorContext} for slot ${this.targetSlot} blocked by slashing protection, yielding`, {
1069
+ slot: this.targetSlot,
1070
+ existingMessageHash: err.existingMessageHash,
1071
+ attemptedMessageHash: err.attemptedMessageHash,
1072
+ });
1073
+ return true;
1074
+ }
1075
+ return false;
1076
+ }
1077
+
1078
+ /**
1079
+ * In times of congestion we need to simulate using the correct fee header override for the previous block
1080
+ * We calculate the correct fee header values.
1081
+ *
1082
+ * If we are in block 1, or the checkpoint we are querying does not exist, we return undefined. However
1083
+ * If we are pipelining - where this function is called, the grandparentCheckpointNumber should always exist
1084
+ * @param parentCheckpointNumber
1085
+ * @returns
1086
+ */
1087
+ protected async computeForceProposedFeeHeader(parentCheckpointNumber: CheckpointNumber): Promise<
1088
+ | {
1089
+ checkpointNumber: CheckpointNumber;
1090
+ feeHeader: FeeHeader;
1091
+ }
1092
+ | undefined
1093
+ > {
1094
+ if (!this.proposedCheckpointData) {
1095
+ return undefined;
1096
+ }
1097
+
1098
+ const rollup = this.publisher.rollupContract;
1099
+ const grandparentCheckpointNumber = CheckpointNumber(this.checkpointNumber - 2);
1100
+ try {
1101
+ const [grandparentCheckpoint, manaTarget] = await Promise.all([
1102
+ rollup.getCheckpoint(grandparentCheckpointNumber),
1103
+ rollup.getManaTarget(),
1104
+ ]);
1105
+
1106
+ if (!grandparentCheckpoint || !grandparentCheckpoint.feeHeader) {
1107
+ this.log.error(
1108
+ `Grandparent checkpoint or its feeHeader is undefined for checkpointNumber=${grandparentCheckpointNumber.toString()}`,
1109
+ );
1110
+ return undefined;
1111
+ } else {
1112
+ const parentFeeHeader = RollupContract.computeChildFeeHeader(
1113
+ grandparentCheckpoint.feeHeader,
1114
+ this.proposedCheckpointData.totalManaUsed,
1115
+ this.proposedCheckpointData.feeAssetPriceModifier,
1116
+ manaTarget,
1117
+ );
1118
+ return { checkpointNumber: parentCheckpointNumber, feeHeader: parentFeeHeader };
1119
+ }
1120
+ } catch (err) {
1121
+ this.log.error(
1122
+ `Failed to fetch grandparent checkpoint or mana target for checkpointNumber=${grandparentCheckpointNumber.toString()}: ${err}`,
1123
+ );
1124
+ return undefined;
1125
+ }
1126
+ }
1127
+
854
1128
  /** Waits until a specific time within the current slot */
855
1129
  @trackSpan('CheckpointProposalJob.waitUntilTimeInSlot')
856
1130
  protected async waitUntilTimeInSlot(targetSecondsIntoSlot: number): Promise<void> {
@@ -859,8 +1133,13 @@ export class CheckpointProposalJob implements Traceable {
859
1133
  await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
860
1134
  }
861
1135
 
1136
+ /** Waits the polling interval for transactions. Extracted for test overriding. */
1137
+ protected async waitForTxsPollingInterval(): Promise<void> {
1138
+ await sleep(TXS_POLLING_MS);
1139
+ }
1140
+
862
1141
  private getSlotStartBuildTimestamp(): number {
863
- return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
1142
+ return getSlotStartBuildTimestamp(this.slotNow, this.l1Constants);
864
1143
  }
865
1144
 
866
1145
  private getSecondsIntoSlot(): number {