@aztec/sequencer-client 0.0.1-commit.96bb3f7 → 0.0.1-commit.96dac018d

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 (91) 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 +17 -14
  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/index.d.ts +2 -2
  11. package/dest/index.d.ts.map +1 -1
  12. package/dest/index.js +1 -1
  13. package/dest/publisher/config.d.ts +35 -17
  14. package/dest/publisher/config.d.ts.map +1 -1
  15. package/dest/publisher/config.js +106 -42
  16. package/dest/publisher/index.d.ts +2 -1
  17. package/dest/publisher/index.d.ts.map +1 -1
  18. package/dest/publisher/l1_tx_failed_store/factory.d.ts +11 -0
  19. package/dest/publisher/l1_tx_failed_store/factory.d.ts.map +1 -0
  20. package/dest/publisher/l1_tx_failed_store/factory.js +22 -0
  21. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +59 -0
  22. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -0
  23. package/dest/publisher/l1_tx_failed_store/failed_tx_store.js +1 -0
  24. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts +15 -0
  25. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts.map +1 -0
  26. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.js +34 -0
  27. package/dest/publisher/l1_tx_failed_store/index.d.ts +4 -0
  28. package/dest/publisher/l1_tx_failed_store/index.d.ts.map +1 -0
  29. package/dest/publisher/l1_tx_failed_store/index.js +2 -0
  30. package/dest/publisher/sequencer-publisher-factory.d.ts +11 -3
  31. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  32. package/dest/publisher/sequencer-publisher-factory.js +13 -2
  33. package/dest/publisher/sequencer-publisher-metrics.d.ts +1 -1
  34. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  35. package/dest/publisher/sequencer-publisher-metrics.js +12 -4
  36. package/dest/publisher/sequencer-publisher.d.ts +22 -8
  37. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  38. package/dest/publisher/sequencer-publisher.js +297 -47
  39. package/dest/sequencer/checkpoint_proposal_job.d.ts +34 -9
  40. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  41. package/dest/sequencer/checkpoint_proposal_job.js +171 -41
  42. package/dest/sequencer/checkpoint_voter.d.ts +3 -2
  43. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -1
  44. package/dest/sequencer/checkpoint_voter.js +34 -10
  45. package/dest/sequencer/index.d.ts +1 -2
  46. package/dest/sequencer/index.d.ts.map +1 -1
  47. package/dest/sequencer/index.js +0 -1
  48. package/dest/sequencer/metrics.d.ts +17 -5
  49. package/dest/sequencer/metrics.d.ts.map +1 -1
  50. package/dest/sequencer/metrics.js +111 -30
  51. package/dest/sequencer/sequencer.d.ts +31 -13
  52. package/dest/sequencer/sequencer.d.ts.map +1 -1
  53. package/dest/sequencer/sequencer.js +95 -36
  54. package/dest/sequencer/timetable.d.ts +1 -4
  55. package/dest/sequencer/timetable.d.ts.map +1 -1
  56. package/dest/sequencer/timetable.js +1 -4
  57. package/dest/test/index.d.ts +3 -5
  58. package/dest/test/index.d.ts.map +1 -1
  59. package/dest/test/mock_checkpoint_builder.d.ts +19 -13
  60. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -1
  61. package/dest/test/mock_checkpoint_builder.js +31 -11
  62. package/dest/test/utils.d.ts +8 -8
  63. package/dest/test/utils.d.ts.map +1 -1
  64. package/dest/test/utils.js +12 -11
  65. package/package.json +30 -28
  66. package/src/client/sequencer-client.ts +25 -7
  67. package/src/config.ts +27 -22
  68. package/src/global_variable_builder/global_builder.ts +3 -3
  69. package/src/index.ts +1 -6
  70. package/src/publisher/config.ts +121 -43
  71. package/src/publisher/index.ts +3 -0
  72. package/src/publisher/l1_tx_failed_store/factory.ts +32 -0
  73. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +55 -0
  74. package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +46 -0
  75. package/src/publisher/l1_tx_failed_store/index.ts +3 -0
  76. package/src/publisher/sequencer-publisher-factory.ts +23 -6
  77. package/src/publisher/sequencer-publisher-metrics.ts +7 -3
  78. package/src/publisher/sequencer-publisher.ts +274 -53
  79. package/src/sequencer/checkpoint_proposal_job.ts +243 -59
  80. package/src/sequencer/checkpoint_voter.ts +32 -7
  81. package/src/sequencer/index.ts +0 -1
  82. package/src/sequencer/metrics.ts +124 -32
  83. package/src/sequencer/sequencer.ts +118 -38
  84. package/src/sequencer/timetable.ts +6 -5
  85. package/src/test/index.ts +2 -4
  86. package/src/test/mock_checkpoint_builder.ts +75 -34
  87. package/src/test/utils.ts +24 -14
  88. package/dest/sequencer/block_builder.d.ts +0 -26
  89. package/dest/sequencer/block_builder.d.ts.map +0 -1
  90. package/dest/sequencer/block_builder.js +0 -129
  91. package/src/sequencer/block_builder.ts +0 -216
@@ -1,40 +1,50 @@
1
+ import { NUM_CHECKPOINT_END_MARKER_FIELDS, getNumBlockEndBlobFields } from '@aztec/blob-lib/encoding';
1
2
  import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB } from '@aztec/constants';
2
3
  import type { EpochCache } from '@aztec/epoch-cache';
3
- 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';
4
11
  import { randomInt } from '@aztec/foundation/crypto/random';
5
12
  import { Fr } from '@aztec/foundation/curves/bn254';
6
13
  import { EthAddress } from '@aztec/foundation/eth-address';
7
14
  import { Signature } from '@aztec/foundation/eth-signature';
8
15
  import { filter } from '@aztec/foundation/iterator';
9
- import type { Logger } from '@aztec/foundation/log';
16
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
10
17
  import { sleep, sleepUntil } from '@aztec/foundation/sleep';
11
18
  import { type DateProvider, Timer } from '@aztec/foundation/timer';
12
- import { type TypedEventEmitter, unfreeze } from '@aztec/foundation/types';
19
+ import { type TypedEventEmitter, isErrorClass, unfreeze } from '@aztec/foundation/types';
13
20
  import type { P2P } from '@aztec/p2p';
14
21
  import type { SlasherClientInterface } from '@aztec/slasher';
15
22
  import {
16
23
  CommitteeAttestation,
17
24
  CommitteeAttestationsAndSigners,
18
- L2BlockNew,
25
+ L2Block,
19
26
  type L2BlockSink,
27
+ type L2BlockSource,
20
28
  MaliciousCommitteeAttestationsAndSigners,
21
29
  } from '@aztec/stdlib/block';
22
30
  import type { Checkpoint } from '@aztec/stdlib/checkpoint';
23
31
  import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
24
32
  import { Gas } from '@aztec/stdlib/gas';
25
- import type {
26
- PublicProcessorLimits,
27
- ResolvedSequencerConfig,
28
- WorldStateSynchronizer,
33
+ import {
34
+ NoValidTxsError,
35
+ type PublicProcessorLimits,
36
+ type ResolvedSequencerConfig,
37
+ type WorldStateSynchronizer,
29
38
  } from '@aztec/stdlib/interfaces/server';
30
39
  import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
31
40
  import type { BlockProposalOptions, CheckpointProposal, CheckpointProposalOptions } from '@aztec/stdlib/p2p';
32
- import { orderAttestations } from '@aztec/stdlib/p2p';
41
+ import { orderAttestations, trimAttestations } from '@aztec/stdlib/p2p';
33
42
  import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
34
43
  import { type FailedTx, Tx } from '@aztec/stdlib/tx';
35
44
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
36
45
  import { Attributes, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
37
46
  import { CheckpointBuilder, type FullNodeCheckpointsBuilder, type ValidatorClient } from '@aztec/validator-client';
47
+ import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
38
48
 
39
49
  import type { GlobalVariableBuilder } from '../global_variable_builder/global_builder.js';
40
50
  import type { InvalidateCheckpointRequest, SequencerPublisher } from '../publisher/sequencer-publisher.js';
@@ -56,7 +66,10 @@ const TXS_POLLING_MS = 500;
56
66
  * the Sequencer once the check for being the proposer for the slot has succeeded.
57
67
  */
58
68
  export class CheckpointProposalJob implements Traceable {
69
+ protected readonly log: Logger;
70
+
59
71
  constructor(
72
+ private readonly epoch: EpochNumber,
60
73
  private readonly slot: SlotNumber,
61
74
  private readonly checkpointNumber: CheckpointNumber,
62
75
  private readonly syncedToBlockNumber: BlockNumber,
@@ -70,6 +83,7 @@ export class CheckpointProposalJob implements Traceable {
70
83
  private readonly p2pClient: P2P,
71
84
  private readonly worldState: WorldStateSynchronizer,
72
85
  private readonly l1ToL2MessageSource: L1ToL2MessageSource,
86
+ private readonly l2BlockSource: L2BlockSource,
73
87
  private readonly checkpointsBuilder: FullNodeCheckpointsBuilder,
74
88
  private readonly blockSink: L2BlockSink,
75
89
  private readonly l1Constants: SequencerRollupConstants,
@@ -81,9 +95,11 @@ export class CheckpointProposalJob implements Traceable {
81
95
  private readonly metrics: SequencerMetrics,
82
96
  private readonly eventEmitter: TypedEventEmitter<SequencerEvents>,
83
97
  private readonly setStateFn: (state: SequencerState, slot?: SlotNumber) => void,
84
- protected readonly log: Logger,
85
98
  public readonly tracer: Tracer,
86
- ) {}
99
+ bindings?: LoggerBindings,
100
+ ) {
101
+ this.log = createLogger('sequencer:checkpoint-proposal', { ...bindings, instanceId: `slot-${slot}` });
102
+ }
87
103
 
88
104
  /**
89
105
  * Executes the checkpoint proposal job.
@@ -113,7 +129,7 @@ export class CheckpointProposalJob implements Traceable {
113
129
  await Promise.all(votesPromises);
114
130
 
115
131
  if (checkpoint) {
116
- this.metrics.recordBlockProposalSuccess();
132
+ this.metrics.recordCheckpointProposalSuccess();
117
133
  }
118
134
 
119
135
  // Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
@@ -169,15 +185,26 @@ export class CheckpointProposalJob implements Traceable {
169
185
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(this.checkpointNumber);
170
186
  const inHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
171
187
 
188
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
189
+ const previousCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(this.epoch))
190
+ .filter(c => c.checkpointNumber < this.checkpointNumber)
191
+ .map(c => c.checkpointOutHash);
192
+
193
+ // Get the fee asset price modifier from the oracle
194
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
195
+
172
196
  // Create a long-lived forked world state for the checkpoint builder
173
- using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
197
+ await using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
174
198
 
175
199
  // Create checkpoint builder for the entire slot
176
200
  const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(
177
201
  this.checkpointNumber,
178
202
  checkpointGlobalVariables,
203
+ feeAssetPriceModifier,
179
204
  l1ToL2Messages,
205
+ previousCheckpointOutHashes,
180
206
  fork,
207
+ this.log.getBindings(),
181
208
  );
182
209
 
183
210
  // Options for the validator client when creating block and checkpoint proposals
@@ -191,13 +218,29 @@ export class CheckpointProposalJob implements Traceable {
191
218
  broadcastInvalidCheckpointProposal: this.config.broadcastInvalidBlockProposal,
192
219
  };
193
220
 
194
- // Main loop: build blocks for the checkpoint
195
- const { blocksInCheckpoint, blockPendingBroadcast } = await this.buildBlocksForCheckpoint(
196
- checkpointBuilder,
197
- checkpointGlobalVariables.timestamp,
198
- inHash,
199
- blockProposalOptions,
200
- );
221
+ let blocksInCheckpoint: L2Block[] = [];
222
+ let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
223
+ const checkpointBuildTimer = new Timer();
224
+
225
+ try {
226
+ // Main loop: build blocks for the checkpoint
227
+ const result = await this.buildBlocksForCheckpoint(
228
+ checkpointBuilder,
229
+ checkpointGlobalVariables.timestamp,
230
+ inHash,
231
+ blockProposalOptions,
232
+ );
233
+ blocksInCheckpoint = result.blocksInCheckpoint;
234
+ blockPendingBroadcast = result.blockPendingBroadcast;
235
+ } catch (err) {
236
+ // These errors are expected in HA mode, so we yield and let another HA node handle the slot
237
+ // The only distinction between the 2 errors is SlashingProtectionError throws when the payload is different,
238
+ // which is normal for block building (may have picked different txs)
239
+ if (this.handleHASigningError(err, 'Block proposal')) {
240
+ return undefined;
241
+ }
242
+ throw err;
243
+ }
201
244
 
202
245
  if (blocksInCheckpoint.length === 0) {
203
246
  this.log.warn(`No blocks were built for slot ${this.slot}`, { slot: this.slot });
@@ -205,11 +248,28 @@ export class CheckpointProposalJob implements Traceable {
205
248
  return undefined;
206
249
  }
207
250
 
251
+ const minBlocksForCheckpoint = this.config.minBlocksForCheckpoint;
252
+ if (minBlocksForCheckpoint !== undefined && blocksInCheckpoint.length < minBlocksForCheckpoint) {
253
+ this.log.warn(
254
+ `Checkpoint has fewer blocks than minimum (${blocksInCheckpoint.length} < ${minBlocksForCheckpoint}), skipping proposal`,
255
+ { slot: this.slot, blocksBuilt: blocksInCheckpoint.length, minBlocksForCheckpoint },
256
+ );
257
+ return undefined;
258
+ }
259
+
208
260
  // Assemble and broadcast the checkpoint proposal, including the last block that was not
209
261
  // broadcasted yet, and wait to collect the committee attestations.
210
262
  this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.slot);
211
263
  const checkpoint = await checkpointBuilder.completeCheckpoint();
212
264
 
265
+ // Record checkpoint-level build metrics
266
+ this.metrics.recordCheckpointBuild(
267
+ checkpointBuildTimer.ms(),
268
+ blocksInCheckpoint.length,
269
+ checkpoint.getStats().txCount,
270
+ Number(checkpoint.header.totalManaUsed.toBigInt()),
271
+ );
272
+
213
273
  // Do not collect attestations nor publish to L1 in fisherman mode
214
274
  if (this.config.fishermanMode) {
215
275
  this.log.info(
@@ -236,6 +296,7 @@ export class CheckpointProposalJob implements Traceable {
236
296
  const proposal = await this.validatorClient.createCheckpointProposal(
237
297
  checkpoint.header,
238
298
  checkpoint.archive.root,
299
+ feeAssetPriceModifier,
239
300
  lastBlock,
240
301
  this.proposer,
241
302
  checkpointProposalOptions,
@@ -252,13 +313,43 @@ export class CheckpointProposalJob implements Traceable {
252
313
 
253
314
  // Proposer must sign over the attestations before pushing them to L1
254
315
  const signer = this.proposer ?? this.publisher.getSenderAddress();
255
- const attestationsSignature = await this.validatorClient.signAttestationsAndSigners(attestations, signer);
316
+ let attestationsSignature: Signature;
317
+ try {
318
+ attestationsSignature = await this.validatorClient.signAttestationsAndSigners(
319
+ attestations,
320
+ signer,
321
+ this.slot,
322
+ this.checkpointNumber,
323
+ );
324
+ } catch (err) {
325
+ // We shouldn't really get here since we yield to another HA node
326
+ // as soon as we see these errors when creating block or checkpoint proposals.
327
+ if (this.handleHASigningError(err, 'Attestations signature')) {
328
+ return undefined;
329
+ }
330
+ throw err;
331
+ }
256
332
 
257
333
  // Enqueue publishing the checkpoint to L1
258
334
  this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.slot);
259
335
  const aztecSlotDuration = this.l1Constants.slotDuration;
260
336
  const slotStartBuildTimestamp = this.getSlotStartBuildTimestamp();
261
337
  const txTimeoutAt = new Date((slotStartBuildTimestamp + aztecSlotDuration) * 1000);
338
+
339
+ // If we have been configured to potentially skip publishing checkpoint then roll the dice here
340
+ if (
341
+ this.config.skipPublishingCheckpointsPercent !== undefined &&
342
+ this.config.skipPublishingCheckpointsPercent > 0
343
+ ) {
344
+ const result = Math.max(0, randomInt(100));
345
+ if (result < this.config.skipPublishingCheckpointsPercent) {
346
+ this.log.warn(
347
+ `Skipping publishing proposal for checkpoint ${checkpoint.number}. Configured percentage: ${this.config.skipPublishingCheckpointsPercent}, generated value: ${result}`,
348
+ );
349
+ return checkpoint;
350
+ }
351
+ }
352
+
262
353
  await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
263
354
  txTimeoutAt,
264
355
  forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber,
@@ -266,6 +357,11 @@ export class CheckpointProposalJob implements Traceable {
266
357
 
267
358
  return checkpoint;
268
359
  } catch (err) {
360
+ if (err && (err instanceof DutyAlreadySignedError || err instanceof SlashingProtectionError)) {
361
+ // swallow this error. It's already been logged by a function deeper in the stack
362
+ return undefined;
363
+ }
364
+
269
365
  this.log.error(`Error building checkpoint at slot ${this.slot}`, err);
270
366
  return undefined;
271
367
  }
@@ -281,19 +377,22 @@ export class CheckpointProposalJob implements Traceable {
281
377
  inHash: Fr,
282
378
  blockProposalOptions: BlockProposalOptions,
283
379
  ): Promise<{
284
- blocksInCheckpoint: L2BlockNew[];
285
- blockPendingBroadcast: { block: L2BlockNew; txs: Tx[] } | undefined;
380
+ blocksInCheckpoint: L2Block[];
381
+ blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined;
286
382
  }> {
287
- const blocksInCheckpoint: L2BlockNew[] = [];
383
+ const blocksInCheckpoint: L2Block[] = [];
288
384
  const txHashesAlreadyIncluded = new Set<string>();
289
385
  const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
290
386
 
387
+ // Remaining blob fields available for blocks (checkpoint end marker already subtracted)
388
+ let remainingBlobFields = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB - NUM_CHECKPOINT_END_MARKER_FIELDS;
389
+
291
390
  // Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
292
- let blockPendingBroadcast: { block: L2BlockNew; txs: Tx[] } | undefined = undefined;
391
+ let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
293
392
 
294
393
  while (true) {
295
394
  const blocksBuilt = blocksInCheckpoint.length;
296
- const indexWithinCheckpoint = blocksBuilt;
395
+ const indexWithinCheckpoint = IndexWithinCheckpoint(blocksBuilt);
297
396
  const blockNumber = BlockNumber(initialBlockNumber + blocksBuilt);
298
397
 
299
398
  const secondsIntoSlot = this.getSecondsIntoSlot();
@@ -320,8 +419,10 @@ export class CheckpointProposalJob implements Traceable {
320
419
  blockNumber,
321
420
  indexWithinCheckpoint,
322
421
  txHashesAlreadyIncluded,
422
+ remainingBlobFields,
323
423
  });
324
424
 
425
+ // TODO(palla/mbps): Review these conditions. We may want to keep trying in some scenarios.
325
426
  if (!buildResult && timingInfo.isLastBlock) {
326
427
  // If no block was produced due to not enough txs and this was the last subslot, exit
327
428
  break;
@@ -344,13 +445,21 @@ export class CheckpointProposalJob implements Traceable {
344
445
  break;
345
446
  }
346
447
 
347
- const { block, usedTxs } = buildResult;
448
+ const { block, usedTxs, remainingBlobFields: newRemainingBlobFields } = buildResult;
348
449
  blocksInCheckpoint.push(block);
349
450
 
451
+ // Update remaining blob fields for the next block
452
+ remainingBlobFields = newRemainingBlobFields;
453
+
350
454
  // Sync the proposed block to the archiver to make it available
351
455
  // Note that the checkpoint builder uses its own fork so it should not need to wait for this syncing
352
456
  // Eventually we should refactor the checkpoint builder to not need a separate long-lived fork
353
- await this.syncProposedBlockToArchiver(block);
457
+ // Fire and forget - don't block the critical path, but log errors
458
+ this.syncProposedBlockToArchiver(block).catch(err => {
459
+ this.log.error(`Failed to sync proposed block ${block.number} to archiver`, { blockNumber: block.number, err });
460
+ });
461
+
462
+ usedTxs.forEach(tx => txHashesAlreadyIncluded.add(tx.txHash.toString()));
354
463
 
355
464
  // If this is the last block, exit the loop now so we start collecting attestations
356
465
  if (timingInfo.isLastBlock) {
@@ -400,19 +509,27 @@ export class CheckpointProposalJob implements Traceable {
400
509
 
401
510
  /** Builds a single block. Called from the main block building loop. */
402
511
  @trackSpan('CheckpointProposalJob.buildSingleBlock')
403
- private async buildSingleBlock(
512
+ protected async buildSingleBlock(
404
513
  checkpointBuilder: CheckpointBuilder,
405
514
  opts: {
406
515
  forceCreate?: boolean;
407
516
  blockTimestamp: bigint;
408
517
  blockNumber: BlockNumber;
409
- indexWithinCheckpoint: number;
518
+ indexWithinCheckpoint: IndexWithinCheckpoint;
410
519
  buildDeadline: Date | undefined;
411
520
  txHashesAlreadyIncluded: Set<string>;
521
+ remainingBlobFields: number;
412
522
  },
413
- ): Promise<{ block: L2BlockNew; usedTxs: Tx[] } | { error: Error } | undefined> {
414
- const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded } =
415
- opts;
523
+ ): Promise<{ block: L2Block; usedTxs: Tx[]; remainingBlobFields: number } | { error: Error } | undefined> {
524
+ const {
525
+ blockTimestamp,
526
+ forceCreate,
527
+ blockNumber,
528
+ indexWithinCheckpoint,
529
+ buildDeadline,
530
+ txHashesAlreadyIncluded,
531
+ remainingBlobFields,
532
+ } = opts;
416
533
 
417
534
  this.log.verbose(
418
535
  `Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.slot}`,
@@ -436,7 +553,7 @@ export class CheckpointProposalJob implements Traceable {
436
553
  // Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
437
554
  // just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
438
555
  const pendingTxs = filter(
439
- this.p2pClient.iteratePendingTxs(),
556
+ this.p2pClient.iterateEligiblePendingTxs(),
440
557
  tx => !txHashesAlreadyIncluded.has(tx.txHash.toString()),
441
558
  );
442
559
 
@@ -445,46 +562,52 @@ export class CheckpointProposalJob implements Traceable {
445
562
  { slot: this.slot, blockNumber, indexWithinCheckpoint },
446
563
  );
447
564
  this.setStateFn(SequencerState.CREATING_BLOCK, this.slot);
565
+
566
+ // Calculate blob fields limit for txs (remaining capacity - this block's end overhead)
567
+ const blockEndOverhead = getNumBlockEndBlobFields(indexWithinCheckpoint === 0);
568
+ const maxBlobFieldsForTxs = remainingBlobFields - blockEndOverhead;
569
+
448
570
  const blockBuilderOptions: PublicProcessorLimits = {
449
571
  maxTransactions: this.config.maxTxsPerBlock,
450
572
  maxBlockSize: this.config.maxBlockSizeInBytes,
451
573
  maxBlockGas: new Gas(this.config.maxDABlockGas, this.config.maxL2BlockGas),
452
- maxBlobFields: BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB,
574
+ maxBlobFields: maxBlobFieldsForTxs,
453
575
  deadline: buildDeadline,
454
576
  };
455
577
 
456
578
  // Actually build the block by executing txs
457
- const workTimer = new Timer();
458
- const { publicGas, block, publicProcessorDuration, numTxs, blockBuildingTimer, usedTxs, failedTxs } =
459
- await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
460
- const blockBuildDuration = workTimer.ms();
579
+ const buildResult = await this.buildSingleBlockWithCheckpointBuilder(
580
+ checkpointBuilder,
581
+ pendingTxs,
582
+ blockNumber,
583
+ blockTimestamp,
584
+ blockBuilderOptions,
585
+ );
461
586
 
462
587
  // If any txs failed during execution, drop them from the mempool so we don't pick them up again
463
- await this.dropFailedTxsFromP2P(failedTxs);
588
+ await this.dropFailedTxsFromP2P(buildResult.failedTxs);
464
589
 
465
590
  // Check if we have created a block with enough txs. If there were invalid txs in the pool, or if execution took
466
591
  // too long, then we may not get to minTxsPerBlock after executing public functions.
467
592
  const minValidTxs = this.config.minValidTxsPerBlock ?? minTxs;
468
- if (!forceCreate && numTxs < minValidTxs) {
593
+ const numTxs = buildResult.status === 'no-valid-txs' ? 0 : buildResult.numTxs;
594
+ if (buildResult.status === 'no-valid-txs' || (!forceCreate && numTxs < minValidTxs)) {
469
595
  this.log.warn(
470
- `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed (got ${numTxs} but required ${minValidTxs})`,
471
- { slot: this.slot, blockNumber, numTxs, indexWithinCheckpoint },
596
+ `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed`,
597
+ { slot: this.slot, blockNumber, numTxs, indexWithinCheckpoint, minValidTxs, buildResult: buildResult.status },
472
598
  );
473
- this.eventEmitter.emit('block-tx-count-check-failed', {
474
- minTxs: minValidTxs,
475
- availableTxs: numTxs,
476
- slot: this.slot,
477
- });
599
+ this.eventEmitter.emit('block-build-failed', { reason: `Insufficient valid txs`, slot: this.slot });
478
600
  this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
479
601
  return undefined;
480
602
  }
481
603
 
482
604
  // Block creation succeeded, emit stats and metrics
605
+ const { publicGas, block, publicProcessorDuration, usedTxs, usedTxBlobFields, blockBuildDuration } = buildResult;
606
+
483
607
  const blockStats = {
484
608
  eventName: 'l2-block-built',
485
609
  duration: blockBuildDuration,
486
610
  publicProcessDuration: publicProcessorDuration,
487
- rollupCircuitsDuration: blockBuildingTimer.ms(),
488
611
  ...block.getStats(),
489
612
  } satisfies L2BlockBuiltStats;
490
613
 
@@ -500,7 +623,7 @@ export class CheckpointProposalJob implements Traceable {
500
623
  this.eventEmitter.emit('block-proposed', { blockNumber: block.number, slot: this.slot });
501
624
  this.metrics.recordBuiltBlock(blockBuildDuration, publicGas.l2Gas);
502
625
 
503
- return { block, usedTxs };
626
+ return { block, usedTxs, remainingBlobFields: maxBlobFieldsForTxs - usedTxBlobFields };
504
627
  } catch (err: any) {
505
628
  this.eventEmitter.emit('block-build-failed', { reason: err.message, slot: this.slot });
506
629
  this.log.error(`Error building block`, err, { blockNumber, slot: this.slot });
@@ -510,17 +633,40 @@ export class CheckpointProposalJob implements Traceable {
510
633
  }
511
634
  }
512
635
 
636
+ /** Uses the checkpoint builder to build a block, catching specific txs */
637
+ private async buildSingleBlockWithCheckpointBuilder(
638
+ checkpointBuilder: CheckpointBuilder,
639
+ pendingTxs: AsyncIterable<Tx>,
640
+ blockNumber: BlockNumber,
641
+ blockTimestamp: bigint,
642
+ blockBuilderOptions: PublicProcessorLimits,
643
+ ) {
644
+ try {
645
+ const workTimer = new Timer();
646
+ const result = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
647
+ const blockBuildDuration = workTimer.ms();
648
+ return { ...result, blockBuildDuration, status: 'success' as const };
649
+ } catch (err: unknown) {
650
+ if (isErrorClass(err, NoValidTxsError)) {
651
+ return { failedTxs: err.failedTxs, status: 'no-valid-txs' as const };
652
+ }
653
+ throw err;
654
+ }
655
+ }
656
+
513
657
  /** Waits until minTxs are available on the pool for building a block. */
514
658
  @trackSpan('CheckpointProposalJob.waitForMinTxs')
515
659
  private async waitForMinTxs(opts: {
516
660
  forceCreate?: boolean;
517
661
  blockNumber: BlockNumber;
518
- indexWithinCheckpoint: number;
662
+ indexWithinCheckpoint: IndexWithinCheckpoint;
519
663
  buildDeadline: Date | undefined;
520
664
  }): Promise<{ canStartBuilding: boolean; availableTxs: number }> {
521
- const minTxs = this.config.minTxsPerBlock;
522
665
  const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
523
666
 
667
+ // We only allow a block with 0 txs in the first block of the checkpoint
668
+ const minTxs = indexWithinCheckpoint > 0 && this.config.minTxsPerBlock === 0 ? 1 : this.config.minTxsPerBlock;
669
+
524
670
  // Deadline is undefined if we are not enforcing the timetable, meaning we'll exit immediately when out of time
525
671
  const startBuildingDeadline = buildDeadline
526
672
  ? new Date(buildDeadline.getTime() - this.timetable.minExecutionTime * 1000)
@@ -541,7 +687,7 @@ export class CheckpointProposalJob implements Traceable {
541
687
  `Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (have ${availableTxs} but need ${minTxs})`,
542
688
  { blockNumber, slot: this.slot, indexWithinCheckpoint },
543
689
  );
544
- await sleep(TXS_POLLING_MS);
690
+ await this.waitForTxsPollingInterval();
545
691
  availableTxs = await this.p2pClient.getPendingTxCount();
546
692
  }
547
693
 
@@ -582,7 +728,7 @@ export class CheckpointProposalJob implements Traceable {
582
728
  const attestationTimeAllowed = this.config.enforceTimeTable
583
729
  ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_CHECKPOINT)!
584
730
  : this.l1Constants.slotDuration;
585
- const attestationDeadline = new Date(this.dateProvider.now() + attestationTimeAllowed * 1000);
731
+ const attestationDeadline = new Date((this.getSlotStartBuildTimestamp() + attestationTimeAllowed) * 1000);
586
732
 
587
733
  this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
588
734
 
@@ -597,8 +743,20 @@ export class CheckpointProposalJob implements Traceable {
597
743
 
598
744
  collectedAttestationsCount = attestations.length;
599
745
 
746
+ // Trim attestations to minimum required to save L1 calldata gas
747
+ const localAddresses = this.validatorClient.getValidatorAddresses();
748
+ const trimmed = trimAttestations(
749
+ attestations,
750
+ numberOfRequiredAttestations,
751
+ this.attestorAddress,
752
+ localAddresses,
753
+ );
754
+ if (trimmed.length < attestations.length) {
755
+ this.log.debug(`Trimmed attestations from ${attestations.length} to ${trimmed.length} for L1 submission`);
756
+ }
757
+
600
758
  // Rollup contract requires that the signatures are provided in the order of the committee
601
- const sorted = orderAttestations(attestations, committee);
759
+ const sorted = orderAttestations(trimmed, committee);
602
760
 
603
761
  // Manipulate the attestations if we've been configured to do so
604
762
  if (this.config.injectFakeAttestation || this.config.shuffleAttestationOrdering) {
@@ -670,7 +828,7 @@ export class CheckpointProposalJob implements Traceable {
670
828
  const failedTxData = failedTxs.map(fail => fail.tx);
671
829
  const failedTxHashes = failedTxData.map(tx => tx.getTxHash());
672
830
  this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
673
- await this.p2pClient.deleteTxs(failedTxHashes);
831
+ await this.p2pClient.handleFailedExecution(failedTxHashes);
674
832
  }
675
833
 
676
834
  /**
@@ -678,8 +836,7 @@ export class CheckpointProposalJob implements Traceable {
678
836
  * Gossip doesn't echo messages back to the sender, so the proposer's archiver/world-state
679
837
  * would never receive its own block without this explicit sync.
680
838
  */
681
- private async syncProposedBlockToArchiver(block: L2BlockNew): Promise<void> {
682
- // TODO(palla/mbps): Change default to false once block sync is stable.
839
+ private async syncProposedBlockToArchiver(block: L2Block): Promise<void> {
683
840
  if (this.config.skipPushProposedBlocksToArchiver !== false) {
684
841
  this.log.warn(`Skipping push of proposed block ${block.number} to archiver`, {
685
842
  blockNumber: block.number,
@@ -713,12 +870,34 @@ export class CheckpointProposalJob implements Traceable {
713
870
  slot: this.slot,
714
871
  feeAnalysisId: feeAnalysis?.id,
715
872
  });
716
- this.metrics.recordBlockProposalFailed('block_build_failed');
873
+ this.metrics.recordCheckpointProposalFailed('block_build_failed');
717
874
  }
718
875
 
719
876
  this.publisher.clearPendingRequests();
720
877
  }
721
878
 
879
+ /**
880
+ * Helper to handle HA double-signing errors. Returns true if the error was handled (caller should yield).
881
+ */
882
+ private handleHASigningError(err: any, errorContext: string): boolean {
883
+ if (err instanceof DutyAlreadySignedError) {
884
+ this.log.info(`${errorContext} for slot ${this.slot} already signed by another HA node, yielding`, {
885
+ slot: this.slot,
886
+ signedByNode: err.signedByNode,
887
+ });
888
+ return true;
889
+ }
890
+ if (err instanceof SlashingProtectionError) {
891
+ this.log.info(`${errorContext} for slot ${this.slot} blocked by slashing protection, yielding`, {
892
+ slot: this.slot,
893
+ existingMessageHash: err.existingMessageHash,
894
+ attemptedMessageHash: err.attemptedMessageHash,
895
+ });
896
+ return true;
897
+ }
898
+ return false;
899
+ }
900
+
722
901
  /** Waits until a specific time within the current slot */
723
902
  @trackSpan('CheckpointProposalJob.waitUntilTimeInSlot')
724
903
  protected async waitUntilTimeInSlot(targetSecondsIntoSlot: number): Promise<void> {
@@ -727,6 +906,11 @@ export class CheckpointProposalJob implements Traceable {
727
906
  await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
728
907
  }
729
908
 
909
+ /** Waits the polling interval for transactions. Extracted for test overriding. */
910
+ protected async waitForTxsPollingInterval(): Promise<void> {
911
+ await sleep(TXS_POLLING_MS);
912
+ }
913
+
730
914
  private getSlotStartBuildTimestamp(): number {
731
915
  return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
732
916
  }
@@ -5,6 +5,8 @@ import type { SlasherClientInterface } from '@aztec/slasher';
5
5
  import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
6
6
  import type { ResolvedSequencerConfig } from '@aztec/stdlib/interfaces/server';
7
7
  import type { ValidatorClient } from '@aztec/validator-client';
8
+ import { DutyAlreadySignedError } from '@aztec/validator-ha-signer/errors';
9
+ import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
8
10
 
9
11
  import type { TypedDataDefinition } from 'viem';
10
12
 
@@ -17,7 +19,8 @@ import type { SequencerRollupConstants } from './types.js';
17
19
  */
18
20
  export class CheckpointVoter {
19
21
  private slotTimestamp: bigint;
20
- private signer: (msg: TypedDataDefinition) => Promise<`0x${string}`>;
22
+ private governanceSigner: (msg: TypedDataDefinition) => Promise<`0x${string}`>;
23
+ private slashingSigner: (msg: TypedDataDefinition) => Promise<`0x${string}`>;
21
24
 
22
25
  constructor(
23
26
  private readonly slot: SlotNumber,
@@ -31,8 +34,16 @@ export class CheckpointVoter {
31
34
  private readonly log: Logger,
32
35
  ) {
33
36
  this.slotTimestamp = getTimestampForSlot(this.slot, this.l1Constants);
34
- this.signer = (msg: TypedDataDefinition) =>
35
- this.validatorClient.signWithAddress(this.attestorAddress, msg).then(s => s.toString());
37
+
38
+ // Create separate signers with appropriate duty contexts for governance and slashing votes
39
+ // These use HA protection to ensure only one node signs per slot/duty
40
+ const governanceContext: SigningContext = { slot: this.slot, dutyType: DutyType.GOVERNANCE_VOTE };
41
+ this.governanceSigner = (msg: TypedDataDefinition) =>
42
+ this.validatorClient.signWithAddress(this.attestorAddress, msg, governanceContext).then(s => s.toString());
43
+
44
+ const slashingContext: SigningContext = { slot: this.slot, dutyType: DutyType.SLASHING_VOTE };
45
+ this.slashingSigner = (msg: TypedDataDefinition) =>
46
+ this.validatorClient.signWithAddress(this.attestorAddress, msg, slashingContext).then(s => s.toString());
36
47
  }
37
48
 
38
49
  /**
@@ -68,10 +79,17 @@ export class CheckpointVoter {
68
79
  this.slot,
69
80
  this.slotTimestamp,
70
81
  this.attestorAddress,
71
- this.signer,
82
+ this.governanceSigner,
72
83
  );
73
84
  } catch (err) {
74
- this.log.error(`Error enqueuing governance vote`, err, { slot: this.slot });
85
+ if (err instanceof DutyAlreadySignedError) {
86
+ this.log.info(`Governance vote already signed by another node`, {
87
+ slot: this.slot,
88
+ signedByNode: err.signedByNode,
89
+ });
90
+ } else {
91
+ this.log.error(`Error enqueueing governance vote`, err);
92
+ }
75
93
  return false;
76
94
  }
77
95
  }
@@ -95,10 +113,17 @@ export class CheckpointVoter {
95
113
  this.slot,
96
114
  this.slotTimestamp,
97
115
  this.attestorAddress,
98
- this.signer,
116
+ this.slashingSigner,
99
117
  );
100
118
  } catch (err) {
101
- this.log.error(`Error enqueuing slashing vote`, err, { slot: this.slot });
119
+ if (err instanceof DutyAlreadySignedError) {
120
+ this.log.info(`Slashing vote already signed by another node`, {
121
+ slot: this.slot,
122
+ signedByNode: err.signedByNode,
123
+ });
124
+ } else {
125
+ this.log.error(`Error enqueueing slashing vote`, err);
126
+ }
102
127
  return false;
103
128
  }
104
129
  }
@@ -1,4 +1,3 @@
1
- export * from './block_builder.js';
2
1
  export * from './checkpoint_proposal_job.js';
3
2
  export * from './checkpoint_voter.js';
4
3
  export * from './config.js';