@aztec/sequencer-client 0.0.1-commit.f295ac2 → 0.0.1-commit.f504929

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 (75) hide show
  1. package/dest/client/sequencer-client.d.ts +12 -7
  2. package/dest/client/sequencer-client.d.ts.map +1 -1
  3. package/dest/client/sequencer-client.js +15 -4
  4. package/dest/config.d.ts +3 -4
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +29 -21
  7. package/dest/global_variable_builder/global_builder.d.ts +2 -4
  8. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  9. package/dest/global_variable_builder/global_builder.js +2 -2
  10. package/dest/publisher/config.d.ts +35 -17
  11. package/dest/publisher/config.d.ts.map +1 -1
  12. package/dest/publisher/config.js +106 -42
  13. package/dest/publisher/index.d.ts +2 -1
  14. package/dest/publisher/index.d.ts.map +1 -1
  15. package/dest/publisher/l1_tx_failed_store/factory.d.ts +11 -0
  16. package/dest/publisher/l1_tx_failed_store/factory.d.ts.map +1 -0
  17. package/dest/publisher/l1_tx_failed_store/factory.js +22 -0
  18. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +59 -0
  19. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -0
  20. package/dest/publisher/l1_tx_failed_store/failed_tx_store.js +1 -0
  21. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts +15 -0
  22. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts.map +1 -0
  23. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.js +34 -0
  24. package/dest/publisher/l1_tx_failed_store/index.d.ts +4 -0
  25. package/dest/publisher/l1_tx_failed_store/index.d.ts.map +1 -0
  26. package/dest/publisher/l1_tx_failed_store/index.js +2 -0
  27. package/dest/publisher/sequencer-publisher-factory.d.ts +11 -3
  28. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  29. package/dest/publisher/sequencer-publisher-factory.js +27 -2
  30. package/dest/publisher/sequencer-publisher-metrics.d.ts +1 -1
  31. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  32. package/dest/publisher/sequencer-publisher-metrics.js +12 -4
  33. package/dest/publisher/sequencer-publisher.d.ts +26 -8
  34. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  35. package/dest/publisher/sequencer-publisher.js +338 -48
  36. package/dest/sequencer/checkpoint_proposal_job.d.ts +32 -9
  37. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  38. package/dest/sequencer/checkpoint_proposal_job.js +157 -74
  39. package/dest/sequencer/metrics.d.ts +17 -5
  40. package/dest/sequencer/metrics.d.ts.map +1 -1
  41. package/dest/sequencer/metrics.js +111 -30
  42. package/dest/sequencer/sequencer.d.ts +20 -8
  43. package/dest/sequencer/sequencer.d.ts.map +1 -1
  44. package/dest/sequencer/sequencer.js +31 -28
  45. package/dest/sequencer/timetable.d.ts +1 -4
  46. package/dest/sequencer/timetable.d.ts.map +1 -1
  47. package/dest/sequencer/timetable.js +2 -5
  48. package/dest/test/index.d.ts +3 -5
  49. package/dest/test/index.d.ts.map +1 -1
  50. package/dest/test/mock_checkpoint_builder.d.ts +14 -9
  51. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -1
  52. package/dest/test/mock_checkpoint_builder.js +24 -10
  53. package/dest/test/utils.d.ts +8 -8
  54. package/dest/test/utils.d.ts.map +1 -1
  55. package/dest/test/utils.js +10 -9
  56. package/package.json +28 -28
  57. package/src/client/sequencer-client.ts +25 -7
  58. package/src/config.ts +41 -30
  59. package/src/global_variable_builder/global_builder.ts +3 -3
  60. package/src/publisher/config.ts +121 -43
  61. package/src/publisher/index.ts +3 -0
  62. package/src/publisher/l1_tx_failed_store/factory.ts +32 -0
  63. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +55 -0
  64. package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +46 -0
  65. package/src/publisher/l1_tx_failed_store/index.ts +3 -0
  66. package/src/publisher/sequencer-publisher-factory.ts +38 -6
  67. package/src/publisher/sequencer-publisher-metrics.ts +7 -3
  68. package/src/publisher/sequencer-publisher.ts +333 -60
  69. package/src/sequencer/checkpoint_proposal_job.ts +221 -96
  70. package/src/sequencer/metrics.ts +124 -32
  71. package/src/sequencer/sequencer.ts +41 -33
  72. package/src/sequencer/timetable.ts +7 -6
  73. package/src/test/index.ts +2 -4
  74. package/src/test/mock_checkpoint_builder.ts +44 -19
  75. package/src/test/utils.ts +22 -13
@@ -1,22 +1,33 @@
1
1
  import { NUM_CHECKPOINT_END_MARKER_FIELDS, getNumBlockEndBlobFields } from '@aztec/blob-lib/encoding';
2
2
  import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB } from '@aztec/constants';
3
3
  import type { EpochCache } from '@aztec/epoch-cache';
4
- import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
+ import {
5
+ BlockNumber,
6
+ CheckpointNumber,
7
+ EpochNumber,
8
+ IndexWithinCheckpoint,
9
+ SlotNumber,
10
+ } from '@aztec/foundation/branded-types';
5
11
  import { randomInt } from '@aztec/foundation/crypto/random';
12
+ import {
13
+ flipSignature,
14
+ generateRecoverableSignature,
15
+ generateUnrecoverableSignature,
16
+ } from '@aztec/foundation/crypto/secp256k1-signer';
6
17
  import { Fr } from '@aztec/foundation/curves/bn254';
7
18
  import { EthAddress } from '@aztec/foundation/eth-address';
8
19
  import { Signature } from '@aztec/foundation/eth-signature';
9
20
  import { filter } from '@aztec/foundation/iterator';
10
- import type { Logger } from '@aztec/foundation/log';
21
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
11
22
  import { sleep, sleepUntil } from '@aztec/foundation/sleep';
12
23
  import { type DateProvider, Timer } from '@aztec/foundation/timer';
13
- import { type TypedEventEmitter, unfreeze } from '@aztec/foundation/types';
24
+ import { type TypedEventEmitter, isErrorClass, unfreeze } from '@aztec/foundation/types';
14
25
  import type { P2P } from '@aztec/p2p';
15
26
  import type { SlasherClientInterface } from '@aztec/slasher';
16
27
  import {
17
28
  CommitteeAttestation,
18
29
  CommitteeAttestationsAndSigners,
19
- L2BlockNew,
30
+ L2Block,
20
31
  type L2BlockSink,
21
32
  type L2BlockSource,
22
33
  MaliciousCommitteeAttestationsAndSigners,
@@ -24,14 +35,15 @@ import {
24
35
  import type { Checkpoint } from '@aztec/stdlib/checkpoint';
25
36
  import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
26
37
  import { Gas } from '@aztec/stdlib/gas';
27
- import type {
28
- PublicProcessorLimits,
29
- ResolvedSequencerConfig,
30
- WorldStateSynchronizer,
38
+ import {
39
+ NoValidTxsError,
40
+ type PublicProcessorLimits,
41
+ type ResolvedSequencerConfig,
42
+ type WorldStateSynchronizer,
31
43
  } from '@aztec/stdlib/interfaces/server';
32
44
  import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
33
45
  import type { BlockProposalOptions, CheckpointProposal, CheckpointProposalOptions } from '@aztec/stdlib/p2p';
34
- import { orderAttestations } from '@aztec/stdlib/p2p';
46
+ import { orderAttestations, trimAttestations } from '@aztec/stdlib/p2p';
35
47
  import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
36
48
  import { type FailedTx, Tx } from '@aztec/stdlib/tx';
37
49
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
@@ -59,6 +71,8 @@ const TXS_POLLING_MS = 500;
59
71
  * the Sequencer once the check for being the proposer for the slot has succeeded.
60
72
  */
61
73
  export class CheckpointProposalJob implements Traceable {
74
+ protected readonly log: Logger;
75
+
62
76
  constructor(
63
77
  private readonly epoch: EpochNumber,
64
78
  private readonly slot: SlotNumber,
@@ -86,9 +100,11 @@ export class CheckpointProposalJob implements Traceable {
86
100
  private readonly metrics: SequencerMetrics,
87
101
  private readonly eventEmitter: TypedEventEmitter<SequencerEvents>,
88
102
  private readonly setStateFn: (state: SequencerState, slot?: SlotNumber) => void,
89
- protected readonly log: Logger,
90
103
  public readonly tracer: Tracer,
91
- ) {}
104
+ bindings?: LoggerBindings,
105
+ ) {
106
+ this.log = createLogger('sequencer:checkpoint-proposal', { ...bindings, instanceId: `slot-${slot}` });
107
+ }
92
108
 
93
109
  /**
94
110
  * Executes the checkpoint proposal job.
@@ -118,7 +134,7 @@ export class CheckpointProposalJob implements Traceable {
118
134
  await Promise.all(votesPromises);
119
135
 
120
136
  if (checkpoint) {
121
- this.metrics.recordBlockProposalSuccess();
137
+ this.metrics.recordCheckpointProposalSuccess();
122
138
  }
123
139
 
124
140
  // Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
@@ -175,21 +191,25 @@ export class CheckpointProposalJob implements Traceable {
175
191
  const inHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
176
192
 
177
193
  // Collect the out hashes of all the checkpoints before this one in the same epoch
178
- const previousCheckpoints = (await this.l2BlockSource.getCheckpointsForEpoch(this.epoch)).filter(
179
- c => c.number < this.checkpointNumber,
180
- );
181
- const previousCheckpointOutHashes = previousCheckpoints.map(c => c.getCheckpointOutHash());
194
+ const previousCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(this.epoch))
195
+ .filter(c => c.checkpointNumber < this.checkpointNumber)
196
+ .map(c => c.checkpointOutHash);
197
+
198
+ // Get the fee asset price modifier from the oracle
199
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
182
200
 
183
201
  // Create a long-lived forked world state for the checkpoint builder
184
- using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
202
+ await using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
185
203
 
186
204
  // Create checkpoint builder for the entire slot
187
205
  const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(
188
206
  this.checkpointNumber,
189
207
  checkpointGlobalVariables,
208
+ feeAssetPriceModifier,
190
209
  l1ToL2Messages,
191
210
  previousCheckpointOutHashes,
192
211
  fork,
212
+ this.log.getBindings(),
193
213
  );
194
214
 
195
215
  // Options for the validator client when creating block and checkpoint proposals
@@ -203,8 +223,9 @@ export class CheckpointProposalJob implements Traceable {
203
223
  broadcastInvalidCheckpointProposal: this.config.broadcastInvalidBlockProposal,
204
224
  };
205
225
 
206
- let blocksInCheckpoint: L2BlockNew[] = [];
207
- let blockPendingBroadcast: { block: L2BlockNew; txs: Tx[] } | undefined = undefined;
226
+ let blocksInCheckpoint: L2Block[] = [];
227
+ let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
228
+ const checkpointBuildTimer = new Timer();
208
229
 
209
230
  try {
210
231
  // Main loop: build blocks for the checkpoint
@@ -220,19 +241,7 @@ export class CheckpointProposalJob implements Traceable {
220
241
  // These errors are expected in HA mode, so we yield and let another HA node handle the slot
221
242
  // The only distinction between the 2 errors is SlashingProtectionError throws when the payload is different,
222
243
  // which is normal for block building (may have picked different txs)
223
- if (err instanceof DutyAlreadySignedError) {
224
- this.log.info(`Checkpoint proposal for slot ${this.slot} already signed by another HA node, yielding`, {
225
- slot: this.slot,
226
- signedByNode: err.signedByNode,
227
- });
228
- return undefined;
229
- }
230
- if (err instanceof SlashingProtectionError) {
231
- this.log.info(`Checkpoint proposal for slot ${this.slot} blocked by slashing protection, yielding`, {
232
- slot: this.slot,
233
- existingMessageHash: err.existingMessageHash,
234
- attemptedMessageHash: err.attemptedMessageHash,
235
- });
244
+ if (this.handleHASigningError(err, 'Block proposal')) {
236
245
  return undefined;
237
246
  }
238
247
  throw err;
@@ -244,11 +253,28 @@ export class CheckpointProposalJob implements Traceable {
244
253
  return undefined;
245
254
  }
246
255
 
256
+ const minBlocksForCheckpoint = this.config.minBlocksForCheckpoint;
257
+ if (minBlocksForCheckpoint !== undefined && blocksInCheckpoint.length < minBlocksForCheckpoint) {
258
+ this.log.warn(
259
+ `Checkpoint has fewer blocks than minimum (${blocksInCheckpoint.length} < ${minBlocksForCheckpoint}), skipping proposal`,
260
+ { slot: this.slot, blocksBuilt: blocksInCheckpoint.length, minBlocksForCheckpoint },
261
+ );
262
+ return undefined;
263
+ }
264
+
247
265
  // Assemble and broadcast the checkpoint proposal, including the last block that was not
248
266
  // broadcasted yet, and wait to collect the committee attestations.
249
267
  this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.slot);
250
268
  const checkpoint = await checkpointBuilder.completeCheckpoint();
251
269
 
270
+ // Record checkpoint-level build metrics
271
+ this.metrics.recordCheckpointBuild(
272
+ checkpointBuildTimer.ms(),
273
+ blocksInCheckpoint.length,
274
+ checkpoint.getStats().txCount,
275
+ Number(checkpoint.header.totalManaUsed.toBigInt()),
276
+ );
277
+
252
278
  // Do not collect attestations nor publish to L1 in fisherman mode
253
279
  if (this.config.fishermanMode) {
254
280
  this.log.info(
@@ -275,6 +301,7 @@ export class CheckpointProposalJob implements Traceable {
275
301
  const proposal = await this.validatorClient.createCheckpointProposal(
276
302
  checkpoint.header,
277
303
  checkpoint.archive.root,
304
+ feeAssetPriceModifier,
278
305
  lastBlock,
279
306
  this.proposer,
280
307
  checkpointProposalOptions,
@@ -301,20 +328,8 @@ export class CheckpointProposalJob implements Traceable {
301
328
  );
302
329
  } catch (err) {
303
330
  // We shouldn't really get here since we yield to another HA node
304
- // as soon as we see these errors when creating block proposals.
305
- if (err instanceof DutyAlreadySignedError) {
306
- this.log.info(`Attestations signature for slot ${this.slot} already signed by another HA node, yielding`, {
307
- slot: this.slot,
308
- signedByNode: err.signedByNode,
309
- });
310
- return undefined;
311
- }
312
- if (err instanceof SlashingProtectionError) {
313
- this.log.info(`Attestations signature for slot ${this.slot} blocked by slashing protection, yielding`, {
314
- slot: this.slot,
315
- existingMessageHash: err.existingMessageHash,
316
- attemptedMessageHash: err.attemptedMessageHash,
317
- });
331
+ // as soon as we see these errors when creating block or checkpoint proposals.
332
+ if (this.handleHASigningError(err, 'Attestations signature')) {
318
333
  return undefined;
319
334
  }
320
335
  throw err;
@@ -325,6 +340,21 @@ export class CheckpointProposalJob implements Traceable {
325
340
  const aztecSlotDuration = this.l1Constants.slotDuration;
326
341
  const slotStartBuildTimestamp = this.getSlotStartBuildTimestamp();
327
342
  const txTimeoutAt = new Date((slotStartBuildTimestamp + aztecSlotDuration) * 1000);
343
+
344
+ // If we have been configured to potentially skip publishing checkpoint then roll the dice here
345
+ if (
346
+ this.config.skipPublishingCheckpointsPercent !== undefined &&
347
+ this.config.skipPublishingCheckpointsPercent > 0
348
+ ) {
349
+ const result = Math.max(0, randomInt(100));
350
+ if (result < this.config.skipPublishingCheckpointsPercent) {
351
+ this.log.warn(
352
+ `Skipping publishing proposal for checkpoint ${checkpoint.number}. Configured percentage: ${this.config.skipPublishingCheckpointsPercent}, generated value: ${result}`,
353
+ );
354
+ return checkpoint;
355
+ }
356
+ }
357
+
328
358
  await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
329
359
  txTimeoutAt,
330
360
  forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber,
@@ -332,6 +362,11 @@ export class CheckpointProposalJob implements Traceable {
332
362
 
333
363
  return checkpoint;
334
364
  } catch (err) {
365
+ if (err && (err instanceof DutyAlreadySignedError || err instanceof SlashingProtectionError)) {
366
+ // swallow this error. It's already been logged by a function deeper in the stack
367
+ return undefined;
368
+ }
369
+
335
370
  this.log.error(`Error building checkpoint at slot ${this.slot}`, err);
336
371
  return undefined;
337
372
  }
@@ -347,10 +382,10 @@ export class CheckpointProposalJob implements Traceable {
347
382
  inHash: Fr,
348
383
  blockProposalOptions: BlockProposalOptions,
349
384
  ): Promise<{
350
- blocksInCheckpoint: L2BlockNew[];
351
- blockPendingBroadcast: { block: L2BlockNew; txs: Tx[] } | undefined;
385
+ blocksInCheckpoint: L2Block[];
386
+ blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined;
352
387
  }> {
353
- const blocksInCheckpoint: L2BlockNew[] = [];
388
+ const blocksInCheckpoint: L2Block[] = [];
354
389
  const txHashesAlreadyIncluded = new Set<string>();
355
390
  const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
356
391
 
@@ -358,11 +393,11 @@ export class CheckpointProposalJob implements Traceable {
358
393
  let remainingBlobFields = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB - NUM_CHECKPOINT_END_MARKER_FIELDS;
359
394
 
360
395
  // Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
361
- let blockPendingBroadcast: { block: L2BlockNew; txs: Tx[] } | undefined = undefined;
396
+ let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
362
397
 
363
398
  while (true) {
364
399
  const blocksBuilt = blocksInCheckpoint.length;
365
- const indexWithinCheckpoint = blocksBuilt;
400
+ const indexWithinCheckpoint = IndexWithinCheckpoint(blocksBuilt);
366
401
  const blockNumber = BlockNumber(initialBlockNumber + blocksBuilt);
367
402
 
368
403
  const secondsIntoSlot = this.getSecondsIntoSlot();
@@ -392,6 +427,7 @@ export class CheckpointProposalJob implements Traceable {
392
427
  remainingBlobFields,
393
428
  });
394
429
 
430
+ // TODO(palla/mbps): Review these conditions. We may want to keep trying in some scenarios.
395
431
  if (!buildResult && timingInfo.isLastBlock) {
396
432
  // If no block was produced due to not enough txs and this was the last subslot, exit
397
433
  break;
@@ -423,7 +459,12 @@ export class CheckpointProposalJob implements Traceable {
423
459
  // Sync the proposed block to the archiver to make it available
424
460
  // Note that the checkpoint builder uses its own fork so it should not need to wait for this syncing
425
461
  // Eventually we should refactor the checkpoint builder to not need a separate long-lived fork
426
- await this.syncProposedBlockToArchiver(block);
462
+ // Fire and forget - don't block the critical path, but log errors
463
+ this.syncProposedBlockToArchiver(block).catch(err => {
464
+ this.log.error(`Failed to sync proposed block ${block.number} to archiver`, { blockNumber: block.number, err });
465
+ });
466
+
467
+ usedTxs.forEach(tx => txHashesAlreadyIncluded.add(tx.txHash.toString()));
427
468
 
428
469
  // If this is the last block, exit the loop now so we start collecting attestations
429
470
  if (timingInfo.isLastBlock) {
@@ -473,18 +514,18 @@ export class CheckpointProposalJob implements Traceable {
473
514
 
474
515
  /** Builds a single block. Called from the main block building loop. */
475
516
  @trackSpan('CheckpointProposalJob.buildSingleBlock')
476
- private async buildSingleBlock(
517
+ protected async buildSingleBlock(
477
518
  checkpointBuilder: CheckpointBuilder,
478
519
  opts: {
479
520
  forceCreate?: boolean;
480
521
  blockTimestamp: bigint;
481
522
  blockNumber: BlockNumber;
482
- indexWithinCheckpoint: number;
523
+ indexWithinCheckpoint: IndexWithinCheckpoint;
483
524
  buildDeadline: Date | undefined;
484
525
  txHashesAlreadyIncluded: Set<string>;
485
526
  remainingBlobFields: number;
486
527
  },
487
- ): Promise<{ block: L2BlockNew; usedTxs: Tx[]; remainingBlobFields: number } | { error: Error } | undefined> {
528
+ ): Promise<{ block: L2Block; usedTxs: Tx[]; remainingBlobFields: number } | { error: Error } | undefined> {
488
529
  const {
489
530
  blockTimestamp,
490
531
  forceCreate,
@@ -517,7 +558,7 @@ export class CheckpointProposalJob implements Traceable {
517
558
  // Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
518
559
  // just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
519
560
  const pendingTxs = filter(
520
- this.p2pClient.iteratePendingTxs(),
561
+ this.p2pClient.iterateEligiblePendingTxs(),
521
562
  tx => !txHashesAlreadyIncluded.has(tx.txHash.toString()),
522
563
  );
523
564
 
@@ -540,45 +581,38 @@ export class CheckpointProposalJob implements Traceable {
540
581
  };
541
582
 
542
583
  // Actually build the block by executing txs
543
- const workTimer = new Timer();
544
- const {
545
- publicGas,
546
- block,
547
- publicProcessorDuration,
548
- numTxs,
549
- blockBuildingTimer,
550
- usedTxs,
551
- failedTxs,
552
- usedTxBlobFields,
553
- } = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
554
- const blockBuildDuration = workTimer.ms();
584
+ const buildResult = await this.buildSingleBlockWithCheckpointBuilder(
585
+ checkpointBuilder,
586
+ pendingTxs,
587
+ blockNumber,
588
+ blockTimestamp,
589
+ blockBuilderOptions,
590
+ );
555
591
 
556
592
  // If any txs failed during execution, drop them from the mempool so we don't pick them up again
557
- await this.dropFailedTxsFromP2P(failedTxs);
593
+ await this.dropFailedTxsFromP2P(buildResult.failedTxs);
558
594
 
559
595
  // Check if we have created a block with enough txs. If there were invalid txs in the pool, or if execution took
560
596
  // too long, then we may not get to minTxsPerBlock after executing public functions.
561
597
  const minValidTxs = this.config.minValidTxsPerBlock ?? minTxs;
562
- if (!forceCreate && numTxs < minValidTxs) {
598
+ const numTxs = buildResult.status === 'no-valid-txs' ? 0 : buildResult.numTxs;
599
+ if (buildResult.status === 'no-valid-txs' || (!forceCreate && numTxs < minValidTxs)) {
563
600
  this.log.warn(
564
- `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed (got ${numTxs} but required ${minValidTxs})`,
565
- { slot: this.slot, blockNumber, numTxs, indexWithinCheckpoint },
601
+ `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed`,
602
+ { slot: this.slot, blockNumber, numTxs, indexWithinCheckpoint, minValidTxs, buildResult: buildResult.status },
566
603
  );
567
- this.eventEmitter.emit('block-tx-count-check-failed', {
568
- minTxs: minValidTxs,
569
- availableTxs: numTxs,
570
- slot: this.slot,
571
- });
604
+ this.eventEmitter.emit('block-build-failed', { reason: `Insufficient valid txs`, slot: this.slot });
572
605
  this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
573
606
  return undefined;
574
607
  }
575
608
 
576
609
  // Block creation succeeded, emit stats and metrics
610
+ const { publicGas, block, publicProcessorDuration, usedTxs, usedTxBlobFields, blockBuildDuration } = buildResult;
611
+
577
612
  const blockStats = {
578
613
  eventName: 'l2-block-built',
579
614
  duration: blockBuildDuration,
580
615
  publicProcessDuration: publicProcessorDuration,
581
- rollupCircuitsDuration: blockBuildingTimer.ms(),
582
616
  ...block.getStats(),
583
617
  } satisfies L2BlockBuiltStats;
584
618
 
@@ -604,17 +638,40 @@ export class CheckpointProposalJob implements Traceable {
604
638
  }
605
639
  }
606
640
 
641
+ /** Uses the checkpoint builder to build a block, catching specific txs */
642
+ private async buildSingleBlockWithCheckpointBuilder(
643
+ checkpointBuilder: CheckpointBuilder,
644
+ pendingTxs: AsyncIterable<Tx>,
645
+ blockNumber: BlockNumber,
646
+ blockTimestamp: bigint,
647
+ blockBuilderOptions: PublicProcessorLimits,
648
+ ) {
649
+ try {
650
+ const workTimer = new Timer();
651
+ const result = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
652
+ const blockBuildDuration = workTimer.ms();
653
+ return { ...result, blockBuildDuration, status: 'success' as const };
654
+ } catch (err: unknown) {
655
+ if (isErrorClass(err, NoValidTxsError)) {
656
+ return { failedTxs: err.failedTxs, status: 'no-valid-txs' as const };
657
+ }
658
+ throw err;
659
+ }
660
+ }
661
+
607
662
  /** Waits until minTxs are available on the pool for building a block. */
608
663
  @trackSpan('CheckpointProposalJob.waitForMinTxs')
609
664
  private async waitForMinTxs(opts: {
610
665
  forceCreate?: boolean;
611
666
  blockNumber: BlockNumber;
612
- indexWithinCheckpoint: number;
667
+ indexWithinCheckpoint: IndexWithinCheckpoint;
613
668
  buildDeadline: Date | undefined;
614
669
  }): Promise<{ canStartBuilding: boolean; availableTxs: number }> {
615
- const minTxs = this.config.minTxsPerBlock;
616
670
  const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
617
671
 
672
+ // We only allow a block with 0 txs in the first block of the checkpoint
673
+ const minTxs = indexWithinCheckpoint > 0 && this.config.minTxsPerBlock === 0 ? 1 : this.config.minTxsPerBlock;
674
+
618
675
  // Deadline is undefined if we are not enforcing the timetable, meaning we'll exit immediately when out of time
619
676
  const startBuildingDeadline = buildDeadline
620
677
  ? new Date(buildDeadline.getTime() - this.timetable.minExecutionTime * 1000)
@@ -635,7 +692,7 @@ export class CheckpointProposalJob implements Traceable {
635
692
  `Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (have ${availableTxs} but need ${minTxs})`,
636
693
  { blockNumber, slot: this.slot, indexWithinCheckpoint },
637
694
  );
638
- await sleep(TXS_POLLING_MS);
695
+ await this.waitForTxsPollingInterval();
639
696
  availableTxs = await this.p2pClient.getPendingTxCount();
640
697
  }
641
698
 
@@ -676,7 +733,7 @@ export class CheckpointProposalJob implements Traceable {
676
733
  const attestationTimeAllowed = this.config.enforceTimeTable
677
734
  ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_CHECKPOINT)!
678
735
  : this.l1Constants.slotDuration;
679
- const attestationDeadline = new Date(this.dateProvider.now() + attestationTimeAllowed * 1000);
736
+ const attestationDeadline = new Date((this.getSlotStartBuildTimestamp() + attestationTimeAllowed) * 1000);
680
737
 
681
738
  this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
682
739
 
@@ -691,11 +748,28 @@ export class CheckpointProposalJob implements Traceable {
691
748
 
692
749
  collectedAttestationsCount = attestations.length;
693
750
 
751
+ // Trim attestations to minimum required to save L1 calldata gas
752
+ const localAddresses = this.validatorClient.getValidatorAddresses();
753
+ const trimmed = trimAttestations(
754
+ attestations,
755
+ numberOfRequiredAttestations,
756
+ this.attestorAddress,
757
+ localAddresses,
758
+ );
759
+ if (trimmed.length < attestations.length) {
760
+ this.log.debug(`Trimmed attestations from ${attestations.length} to ${trimmed.length} for L1 submission`);
761
+ }
762
+
694
763
  // Rollup contract requires that the signatures are provided in the order of the committee
695
- const sorted = orderAttestations(attestations, committee);
764
+ const sorted = orderAttestations(trimmed, committee);
696
765
 
697
766
  // Manipulate the attestations if we've been configured to do so
698
- if (this.config.injectFakeAttestation || this.config.shuffleAttestationOrdering) {
767
+ if (
768
+ this.config.injectFakeAttestation ||
769
+ this.config.injectHighSValueAttestation ||
770
+ this.config.injectUnrecoverableSignatureAttestation ||
771
+ this.config.shuffleAttestationOrdering
772
+ ) {
699
773
  return this.manipulateAttestations(proposal.slotNumber, epoch, seed, committee, sorted);
700
774
  }
701
775
 
@@ -724,7 +798,11 @@ export class CheckpointProposalJob implements Traceable {
724
798
  this.epochCache.computeProposerIndex(slotNumber, epoch, seed, BigInt(committee.length)),
725
799
  );
726
800
 
727
- if (this.config.injectFakeAttestation) {
801
+ if (
802
+ this.config.injectFakeAttestation ||
803
+ this.config.injectHighSValueAttestation ||
804
+ this.config.injectUnrecoverableSignatureAttestation
805
+ ) {
728
806
  // Find non-empty attestations that are not from the proposer
729
807
  const nonProposerIndices: number[] = [];
730
808
  for (let i = 0; i < attestations.length; i++) {
@@ -734,8 +812,20 @@ export class CheckpointProposalJob implements Traceable {
734
812
  }
735
813
  if (nonProposerIndices.length > 0) {
736
814
  const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
737
- this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
738
- unfreeze(attestations[targetIndex]).signature = Signature.random();
815
+ if (this.config.injectHighSValueAttestation) {
816
+ this.log.warn(
817
+ `Injecting high-s value attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
818
+ );
819
+ unfreeze(attestations[targetIndex]).signature = flipSignature(attestations[targetIndex].signature);
820
+ } else if (this.config.injectUnrecoverableSignatureAttestation) {
821
+ this.log.warn(
822
+ `Injecting unrecoverable signature attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
823
+ );
824
+ unfreeze(attestations[targetIndex]).signature = generateUnrecoverableSignature();
825
+ } else {
826
+ this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
827
+ unfreeze(attestations[targetIndex]).signature = generateRecoverableSignature();
828
+ }
739
829
  }
740
830
  return new CommitteeAttestationsAndSigners(attestations);
741
831
  }
@@ -744,11 +834,20 @@ export class CheckpointProposalJob implements Traceable {
744
834
  this.log.warn(`Shuffling attestation ordering in checkpoint for slot ${slotNumber} (proposer #${proposerIndex})`);
745
835
 
746
836
  const shuffled = [...attestations];
747
- const [i, j] = [(proposerIndex + 1) % shuffled.length, (proposerIndex + 2) % shuffled.length];
748
- const valueI = shuffled[i];
749
- const valueJ = shuffled[j];
750
- shuffled[i] = valueJ;
751
- shuffled[j] = valueI;
837
+
838
+ // Find two non-proposer positions that both have non-empty signatures to swap.
839
+ // This ensures the bitmap doesn't change, so the MaliciousCommitteeAttestationsAndSigners
840
+ // signers array stays correctly aligned with L1's committee reconstruction.
841
+ const swappable: number[] = [];
842
+ for (let k = 0; k < shuffled.length; k++) {
843
+ if (!shuffled[k].signature.isEmpty() && k !== proposerIndex) {
844
+ swappable.push(k);
845
+ }
846
+ }
847
+ if (swappable.length >= 2) {
848
+ const [i, j] = [swappable[0], swappable[1]];
849
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
850
+ }
752
851
 
753
852
  const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
754
853
  return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
@@ -764,7 +863,7 @@ export class CheckpointProposalJob implements Traceable {
764
863
  const failedTxData = failedTxs.map(fail => fail.tx);
765
864
  const failedTxHashes = failedTxData.map(tx => tx.getTxHash());
766
865
  this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
767
- await this.p2pClient.deleteTxs(failedTxHashes);
866
+ await this.p2pClient.handleFailedExecution(failedTxHashes);
768
867
  }
769
868
 
770
869
  /**
@@ -772,8 +871,7 @@ export class CheckpointProposalJob implements Traceable {
772
871
  * Gossip doesn't echo messages back to the sender, so the proposer's archiver/world-state
773
872
  * would never receive its own block without this explicit sync.
774
873
  */
775
- private async syncProposedBlockToArchiver(block: L2BlockNew): Promise<void> {
776
- // TODO(palla/mbps): Change default to false once block sync is stable.
874
+ private async syncProposedBlockToArchiver(block: L2Block): Promise<void> {
777
875
  if (this.config.skipPushProposedBlocksToArchiver !== false) {
778
876
  this.log.warn(`Skipping push of proposed block ${block.number} to archiver`, {
779
877
  blockNumber: block.number,
@@ -807,12 +905,34 @@ export class CheckpointProposalJob implements Traceable {
807
905
  slot: this.slot,
808
906
  feeAnalysisId: feeAnalysis?.id,
809
907
  });
810
- this.metrics.recordBlockProposalFailed('block_build_failed');
908
+ this.metrics.recordCheckpointProposalFailed('block_build_failed');
811
909
  }
812
910
 
813
911
  this.publisher.clearPendingRequests();
814
912
  }
815
913
 
914
+ /**
915
+ * Helper to handle HA double-signing errors. Returns true if the error was handled (caller should yield).
916
+ */
917
+ private handleHASigningError(err: any, errorContext: string): boolean {
918
+ if (err instanceof DutyAlreadySignedError) {
919
+ this.log.info(`${errorContext} for slot ${this.slot} already signed by another HA node, yielding`, {
920
+ slot: this.slot,
921
+ signedByNode: err.signedByNode,
922
+ });
923
+ return true;
924
+ }
925
+ if (err instanceof SlashingProtectionError) {
926
+ this.log.info(`${errorContext} for slot ${this.slot} blocked by slashing protection, yielding`, {
927
+ slot: this.slot,
928
+ existingMessageHash: err.existingMessageHash,
929
+ attemptedMessageHash: err.attemptedMessageHash,
930
+ });
931
+ return true;
932
+ }
933
+ return false;
934
+ }
935
+
816
936
  /** Waits until a specific time within the current slot */
817
937
  @trackSpan('CheckpointProposalJob.waitUntilTimeInSlot')
818
938
  protected async waitUntilTimeInSlot(targetSecondsIntoSlot: number): Promise<void> {
@@ -821,6 +941,11 @@ export class CheckpointProposalJob implements Traceable {
821
941
  await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
822
942
  }
823
943
 
944
+ /** Waits the polling interval for transactions. Extracted for test overriding. */
945
+ protected async waitForTxsPollingInterval(): Promise<void> {
946
+ await sleep(TXS_POLLING_MS);
947
+ }
948
+
824
949
  private getSlotStartBuildTimestamp(): number {
825
950
  return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
826
951
  }