@aztec/sequencer-client 0.0.1-commit.b655e406 → 0.0.1-commit.b6e433891

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 (119) hide show
  1. package/dest/client/index.d.ts +1 -1
  2. package/dest/client/sequencer-client.d.ts +21 -16
  3. package/dest/client/sequencer-client.d.ts.map +1 -1
  4. package/dest/client/sequencer-client.js +75 -28
  5. package/dest/config.d.ts +35 -9
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +113 -42
  8. package/dest/global_variable_builder/global_builder.d.ts +20 -16
  9. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  10. package/dest/global_variable_builder/global_builder.js +54 -40
  11. package/dest/global_variable_builder/index.d.ts +1 -1
  12. package/dest/index.d.ts +2 -3
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +1 -2
  15. package/dest/publisher/config.d.ts +43 -20
  16. package/dest/publisher/config.d.ts.map +1 -1
  17. package/dest/publisher/config.js +109 -34
  18. package/dest/publisher/index.d.ts +2 -1
  19. package/dest/publisher/index.d.ts.map +1 -1
  20. package/dest/publisher/l1_tx_failed_store/factory.d.ts +11 -0
  21. package/dest/publisher/l1_tx_failed_store/factory.d.ts.map +1 -0
  22. package/dest/publisher/l1_tx_failed_store/factory.js +22 -0
  23. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +59 -0
  24. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -0
  25. package/dest/publisher/l1_tx_failed_store/failed_tx_store.js +1 -0
  26. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts +15 -0
  27. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts.map +1 -0
  28. package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.js +34 -0
  29. package/dest/publisher/l1_tx_failed_store/index.d.ts +4 -0
  30. package/dest/publisher/l1_tx_failed_store/index.d.ts.map +1 -0
  31. package/dest/publisher/l1_tx_failed_store/index.js +2 -0
  32. package/dest/publisher/sequencer-publisher-factory.d.ts +15 -6
  33. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  34. package/dest/publisher/sequencer-publisher-factory.js +28 -3
  35. package/dest/publisher/sequencer-publisher-metrics.d.ts +3 -3
  36. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  37. package/dest/publisher/sequencer-publisher-metrics.js +23 -86
  38. package/dest/publisher/sequencer-publisher.d.ts +103 -69
  39. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  40. package/dest/publisher/sequencer-publisher.js +999 -190
  41. package/dest/sequencer/checkpoint_proposal_job.d.ts +108 -0
  42. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
  43. package/dest/sequencer/checkpoint_proposal_job.js +1289 -0
  44. package/dest/sequencer/checkpoint_voter.d.ts +35 -0
  45. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
  46. package/dest/sequencer/checkpoint_voter.js +109 -0
  47. package/dest/sequencer/config.d.ts +3 -2
  48. package/dest/sequencer/config.d.ts.map +1 -1
  49. package/dest/sequencer/errors.d.ts +1 -1
  50. package/dest/sequencer/errors.d.ts.map +1 -1
  51. package/dest/sequencer/events.d.ts +47 -0
  52. package/dest/sequencer/events.d.ts.map +1 -0
  53. package/dest/sequencer/events.js +1 -0
  54. package/dest/sequencer/index.d.ts +4 -2
  55. package/dest/sequencer/index.d.ts.map +1 -1
  56. package/dest/sequencer/index.js +3 -1
  57. package/dest/sequencer/metrics.d.ts +48 -3
  58. package/dest/sequencer/metrics.d.ts.map +1 -1
  59. package/dest/sequencer/metrics.js +243 -50
  60. package/dest/sequencer/sequencer.d.ts +127 -144
  61. package/dest/sequencer/sequencer.d.ts.map +1 -1
  62. package/dest/sequencer/sequencer.js +770 -545
  63. package/dest/sequencer/timetable.d.ts +54 -16
  64. package/dest/sequencer/timetable.d.ts.map +1 -1
  65. package/dest/sequencer/timetable.js +147 -62
  66. package/dest/sequencer/types.d.ts +3 -0
  67. package/dest/sequencer/types.d.ts.map +1 -0
  68. package/dest/sequencer/types.js +1 -0
  69. package/dest/sequencer/utils.d.ts +14 -8
  70. package/dest/sequencer/utils.d.ts.map +1 -1
  71. package/dest/sequencer/utils.js +7 -4
  72. package/dest/test/index.d.ts +6 -7
  73. package/dest/test/index.d.ts.map +1 -1
  74. package/dest/test/mock_checkpoint_builder.d.ts +95 -0
  75. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
  76. package/dest/test/mock_checkpoint_builder.js +231 -0
  77. package/dest/test/utils.d.ts +53 -0
  78. package/dest/test/utils.d.ts.map +1 -0
  79. package/dest/test/utils.js +104 -0
  80. package/package.json +32 -30
  81. package/src/client/sequencer-client.ts +100 -52
  82. package/src/config.ts +132 -51
  83. package/src/global_variable_builder/global_builder.ts +69 -60
  84. package/src/index.ts +1 -7
  85. package/src/publisher/config.ts +139 -50
  86. package/src/publisher/index.ts +3 -0
  87. package/src/publisher/l1_tx_failed_store/factory.ts +32 -0
  88. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +55 -0
  89. package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +46 -0
  90. package/src/publisher/l1_tx_failed_store/index.ts +3 -0
  91. package/src/publisher/sequencer-publisher-factory.ts +45 -11
  92. package/src/publisher/sequencer-publisher-metrics.ts +19 -71
  93. package/src/publisher/sequencer-publisher.ts +717 -248
  94. package/src/sequencer/README.md +531 -0
  95. package/src/sequencer/checkpoint_proposal_job.ts +1049 -0
  96. package/src/sequencer/checkpoint_voter.ts +130 -0
  97. package/src/sequencer/config.ts +2 -1
  98. package/src/sequencer/events.ts +27 -0
  99. package/src/sequencer/index.ts +3 -1
  100. package/src/sequencer/metrics.ts +310 -61
  101. package/src/sequencer/sequencer.ts +541 -735
  102. package/src/sequencer/timetable.ts +178 -83
  103. package/src/sequencer/types.ts +6 -0
  104. package/src/sequencer/utils.ts +18 -9
  105. package/src/test/index.ts +5 -6
  106. package/src/test/mock_checkpoint_builder.ts +323 -0
  107. package/src/test/utils.ts +167 -0
  108. package/dest/sequencer/block_builder.d.ts +0 -27
  109. package/dest/sequencer/block_builder.d.ts.map +0 -1
  110. package/dest/sequencer/block_builder.js +0 -130
  111. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  112. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  113. package/dest/tx_validator/nullifier_cache.js +0 -24
  114. package/dest/tx_validator/tx_validator_factory.d.ts +0 -17
  115. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  116. package/dest/tx_validator/tx_validator_factory.js +0 -53
  117. package/src/sequencer/block_builder.ts +0 -218
  118. package/src/tx_validator/nullifier_cache.ts +0 -30
  119. package/src/tx_validator/tx_validator_factory.ts +0 -132
@@ -0,0 +1,1049 @@
1
+ import type { EpochCache } from '@aztec/epoch-cache';
2
+ import {
3
+ BlockNumber,
4
+ CheckpointNumber,
5
+ EpochNumber,
6
+ IndexWithinCheckpoint,
7
+ SlotNumber,
8
+ } from '@aztec/foundation/branded-types';
9
+ import { randomInt } from '@aztec/foundation/crypto/random';
10
+ import {
11
+ flipSignature,
12
+ generateRecoverableSignature,
13
+ generateUnrecoverableSignature,
14
+ } from '@aztec/foundation/crypto/secp256k1-signer';
15
+ import { Fr } from '@aztec/foundation/curves/bn254';
16
+ import { EthAddress } from '@aztec/foundation/eth-address';
17
+ import { Signature } from '@aztec/foundation/eth-signature';
18
+ import { filter } from '@aztec/foundation/iterator';
19
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
20
+ import { sleep, sleepUntil } from '@aztec/foundation/sleep';
21
+ import { type DateProvider, Timer } from '@aztec/foundation/timer';
22
+ import { type TypedEventEmitter, isErrorClass, unfreeze } from '@aztec/foundation/types';
23
+ import type { P2P } from '@aztec/p2p';
24
+ import type { SlasherClientInterface } from '@aztec/slasher';
25
+ import {
26
+ CommitteeAttestation,
27
+ CommitteeAttestationsAndSigners,
28
+ L2Block,
29
+ type L2BlockSink,
30
+ type L2BlockSource,
31
+ MaliciousCommitteeAttestationsAndSigners,
32
+ } from '@aztec/stdlib/block';
33
+ import { type Checkpoint, validateCheckpoint } from '@aztec/stdlib/checkpoint';
34
+ import { getSlotStartBuildTimestamp, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
35
+ import { Gas } from '@aztec/stdlib/gas';
36
+ import {
37
+ type BlockBuilderOptions,
38
+ InsufficientValidTxsError,
39
+ type ResolvedSequencerConfig,
40
+ type WorldStateSynchronizer,
41
+ } from '@aztec/stdlib/interfaces/server';
42
+ import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
43
+ import type {
44
+ BlockProposal,
45
+ BlockProposalOptions,
46
+ CheckpointProposal,
47
+ CheckpointProposalOptions,
48
+ } from '@aztec/stdlib/p2p';
49
+ import { orderAttestations, trimAttestations } from '@aztec/stdlib/p2p';
50
+ import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
51
+ import { type FailedTx, Tx } from '@aztec/stdlib/tx';
52
+ import { AttestationTimeoutError } from '@aztec/stdlib/validators';
53
+ import { Attributes, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
54
+ import { CheckpointBuilder, type FullNodeCheckpointsBuilder, type ValidatorClient } from '@aztec/validator-client';
55
+ import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
56
+
57
+ import type { GlobalVariableBuilder } from '../global_variable_builder/global_builder.js';
58
+ import type { InvalidateCheckpointRequest, SequencerPublisher } from '../publisher/sequencer-publisher.js';
59
+ import { CheckpointVoter } from './checkpoint_voter.js';
60
+ import { SequencerInterruptedError } from './errors.js';
61
+ import type { SequencerEvents } from './events.js';
62
+ import type { SequencerMetrics } from './metrics.js';
63
+ import type { SequencerTimetable } from './timetable.js';
64
+ import type { SequencerRollupConstants } from './types.js';
65
+ import { SequencerState } from './utils.js';
66
+
67
+ /** How much time to sleep while waiting for min transactions to accumulate for a block */
68
+ const TXS_POLLING_MS = 500;
69
+
70
+ /**
71
+ * Handles the execution of a checkpoint proposal after the initial preparation phase.
72
+ * This includes building blocks, collecting attestations, and publishing the checkpoint to L1,
73
+ * as well as enqueueing votes for slashing and governance proposals. This class is created from
74
+ * the Sequencer once the check for being the proposer for the slot has succeeded.
75
+ */
76
+ export class CheckpointProposalJob implements Traceable {
77
+ protected readonly log: Logger;
78
+
79
+ constructor(
80
+ private readonly slotNow: SlotNumber,
81
+ private readonly targetSlot: SlotNumber,
82
+ private readonly epochNow: EpochNumber,
83
+ private readonly targetEpoch: EpochNumber,
84
+ private readonly checkpointNumber: CheckpointNumber,
85
+ private readonly syncedToBlockNumber: BlockNumber,
86
+ // TODO(palla/mbps): Can we remove the proposer in favor of attestorAddress? Need to check fisherman-node flows.
87
+ private readonly proposer: EthAddress | undefined,
88
+ private readonly publisher: SequencerPublisher,
89
+ private readonly attestorAddress: EthAddress,
90
+ private readonly invalidateCheckpoint: InvalidateCheckpointRequest | undefined,
91
+ private readonly validatorClient: ValidatorClient,
92
+ private readonly globalsBuilder: GlobalVariableBuilder,
93
+ private readonly p2pClient: P2P,
94
+ private readonly worldState: WorldStateSynchronizer,
95
+ private readonly l1ToL2MessageSource: L1ToL2MessageSource,
96
+ private readonly l2BlockSource: L2BlockSource,
97
+ private readonly checkpointsBuilder: FullNodeCheckpointsBuilder,
98
+ private readonly blockSink: L2BlockSink,
99
+ private readonly l1Constants: SequencerRollupConstants,
100
+ protected config: ResolvedSequencerConfig,
101
+ protected timetable: SequencerTimetable,
102
+ private readonly slasherClient: SlasherClientInterface | undefined,
103
+ private readonly epochCache: EpochCache,
104
+ private readonly dateProvider: DateProvider,
105
+ private readonly metrics: SequencerMetrics,
106
+ private readonly eventEmitter: TypedEventEmitter<SequencerEvents>,
107
+ private readonly setStateFn: (state: SequencerState, slot?: SlotNumber) => void,
108
+ public readonly tracer: Tracer,
109
+ bindings?: LoggerBindings,
110
+ ) {
111
+ this.log = createLogger('sequencer:checkpoint-proposal', {
112
+ ...bindings,
113
+ instanceId: `slot-${this.slotNow}`,
114
+ });
115
+ }
116
+
117
+ /** The wall-clock slot during which the proposer builds. */
118
+ private get slot(): SlotNumber {
119
+ return this.slotNow;
120
+ }
121
+
122
+ /** The wall-clock epoch. */
123
+ private get epoch(): EpochNumber {
124
+ return this.epochNow;
125
+ }
126
+
127
+ /**
128
+ * Executes the checkpoint proposal job.
129
+ * Returns the published checkpoint if successful, undefined otherwise.
130
+ */
131
+ @trackSpan('CheckpointProposalJob.execute')
132
+ public async execute(): Promise<Checkpoint | undefined> {
133
+ // Enqueue governance and slashing votes (returns promises that will be awaited later)
134
+ // In fisherman mode, we simulate slashing but don't actually publish to L1
135
+ // These are constant for the whole slot, so we only enqueue them once
136
+ const votesPromises = new CheckpointVoter(
137
+ this.targetSlot,
138
+ this.publisher,
139
+ this.attestorAddress,
140
+ this.validatorClient,
141
+ this.slasherClient,
142
+ this.l1Constants,
143
+ this.config,
144
+ this.metrics,
145
+ this.log,
146
+ ).enqueueVotes();
147
+
148
+ // Build and propose the checkpoint. This will enqueue the request on the publisher if a checkpoint is built.
149
+ const checkpoint = await this.proposeCheckpoint();
150
+
151
+ // Wait until the voting promises have resolved, so all requests are enqueued (not sent)
152
+ await Promise.all(votesPromises);
153
+
154
+ if (checkpoint) {
155
+ this.metrics.recordCheckpointProposalSuccess();
156
+ }
157
+
158
+ // Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
159
+ if (this.config.fishermanMode) {
160
+ await this.handleCheckpointEndAsFisherman(checkpoint);
161
+ return;
162
+ }
163
+
164
+ // If pipelining, wait until the submission slot so L1 recognizes the pipelined proposer
165
+ if (this.epochCache.isProposerPipeliningEnabled()) {
166
+ const submissionSlotTimestamp =
167
+ getTimestampForSlot(this.targetSlot, this.l1Constants) - BigInt(this.l1Constants.ethereumSlotDuration);
168
+ this.log.info(`Waiting until submission slot ${this.targetSlot} for L1 submission`, {
169
+ slot: this.slot,
170
+ submissionSlot: this.targetSlot,
171
+ submissionSlotTimestamp,
172
+ });
173
+ await sleepUntil(new Date(Number(submissionSlotTimestamp) * 1000), this.dateProvider.nowAsDate());
174
+
175
+ // After waking, verify the parent checkpoint wasn't pruned during the sleep.
176
+ // We check L1's pending tip directly instead of canProposeAt, which also validates the proposer
177
+ // identity and would fail because the timestamp resolves to a different slot's proposer.
178
+ const l1Tips = await this.publisher.rollupContract.getTips();
179
+ if (l1Tips.pending < this.checkpointNumber - 1) {
180
+ this.log.warn(
181
+ `Parent checkpoint was pruned during pipelining sleep (L1 pending=${l1Tips.pending}, expected>=${this.checkpointNumber - 1}), skipping L1 submission for checkpoint ${this.checkpointNumber}`,
182
+ );
183
+ return undefined;
184
+ }
185
+ }
186
+
187
+ // Then send everything to L1
188
+ const l1Response = await this.publisher.sendRequests();
189
+ const proposedAction = l1Response?.successfulActions.find(a => a === 'propose');
190
+ if (proposedAction) {
191
+ this.eventEmitter.emit('checkpoint-published', { checkpoint: this.checkpointNumber, slot: this.slot });
192
+ const coinbase = checkpoint?.header.coinbase;
193
+ await this.metrics.incFilledSlot(this.publisher.getSenderAddress().toString(), coinbase);
194
+ return checkpoint;
195
+ } else if (checkpoint) {
196
+ this.eventEmitter.emit('checkpoint-publish-failed', { ...l1Response, slot: this.slot });
197
+ return undefined;
198
+ }
199
+ }
200
+
201
+ @trackSpan('CheckpointProposalJob.proposeCheckpoint', function () {
202
+ return {
203
+ // nullish operator needed for tests
204
+ [Attributes.COINBASE]: this.validatorClient.getCoinbaseForAttestor(this.attestorAddress)?.toString(),
205
+ [Attributes.SLOT_NUMBER]: this.targetSlot,
206
+ };
207
+ })
208
+ private async proposeCheckpoint(): Promise<Checkpoint | undefined> {
209
+ try {
210
+ // Get operator configured coinbase and fee recipient for this attestor
211
+ const coinbase = this.validatorClient.getCoinbaseForAttestor(this.attestorAddress);
212
+ const feeRecipient = this.validatorClient.getFeeRecipientForAttestor(this.attestorAddress);
213
+
214
+ // Start the checkpoint
215
+ this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.targetSlot);
216
+ this.log.info(`Starting checkpoint proposal`, {
217
+ buildSlot: this.slot,
218
+ submissionSlot: this.targetSlot,
219
+ pipelining: this.epochCache.isProposerPipeliningEnabled(),
220
+ proposer: this.proposer?.toString(),
221
+ coinbase: coinbase.toString(),
222
+ });
223
+ this.metrics.incOpenSlot(this.targetSlot, this.proposer?.toString() ?? 'unknown');
224
+
225
+ // Enqueues checkpoint invalidation (constant for the whole slot)
226
+ if (this.invalidateCheckpoint && !this.config.skipInvalidateBlockAsProposer) {
227
+ this.publisher.enqueueInvalidateCheckpoint(this.invalidateCheckpoint);
228
+ }
229
+
230
+ // Create checkpoint builder for the slot
231
+ const checkpointGlobalVariables = await this.globalsBuilder.buildCheckpointGlobalVariables(
232
+ coinbase,
233
+ feeRecipient,
234
+ this.targetSlot,
235
+ );
236
+
237
+ // Collect L1 to L2 messages for the checkpoint and compute their hash
238
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(this.checkpointNumber);
239
+ const inHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
240
+
241
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
242
+ const previousCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(this.targetEpoch))
243
+ .filter(c => c.checkpointNumber < this.checkpointNumber)
244
+ .map(c => c.checkpointOutHash);
245
+
246
+ // Get the fee asset price modifier from the oracle
247
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
248
+
249
+ // Create a long-lived forked world state for the checkpoint builder
250
+ await using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
251
+
252
+ // Create checkpoint builder for the entire slot
253
+ const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(
254
+ this.checkpointNumber,
255
+ checkpointGlobalVariables,
256
+ feeAssetPriceModifier,
257
+ l1ToL2Messages,
258
+ previousCheckpointOutHashes,
259
+ fork,
260
+ this.log.getBindings(),
261
+ );
262
+
263
+ // Options for the validator client when creating block and checkpoint proposals
264
+ const blockProposalOptions: BlockProposalOptions = {
265
+ publishFullTxs: !!this.config.publishTxsWithProposals,
266
+ broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
267
+ };
268
+
269
+ const checkpointProposalOptions: CheckpointProposalOptions = {
270
+ publishFullTxs: !!this.config.publishTxsWithProposals,
271
+ broadcastInvalidCheckpointProposal: this.config.broadcastInvalidBlockProposal,
272
+ };
273
+
274
+ let blocksInCheckpoint: L2Block[] = [];
275
+ let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
276
+ const checkpointBuildTimer = new Timer();
277
+
278
+ try {
279
+ // Main loop: build blocks for the checkpoint
280
+ const result = await this.buildBlocksForCheckpoint(
281
+ checkpointBuilder,
282
+ checkpointGlobalVariables.timestamp,
283
+ inHash,
284
+ blockProposalOptions,
285
+ );
286
+ blocksInCheckpoint = result.blocksInCheckpoint;
287
+ blockPendingBroadcast = result.blockPendingBroadcast;
288
+ } catch (err) {
289
+ // These errors are expected in HA mode, so we yield and let another HA node handle the slot
290
+ // The only distinction between the 2 errors is SlashingProtectionError throws when the payload is different,
291
+ // which is normal for block building (may have picked different txs)
292
+ if (this.handleHASigningError(err, 'Block proposal')) {
293
+ return undefined;
294
+ }
295
+ throw err;
296
+ }
297
+
298
+ if (blocksInCheckpoint.length === 0) {
299
+ this.log.warn(`No blocks were built for slot ${this.targetSlot}`, { slot: this.targetSlot });
300
+ this.eventEmitter.emit('checkpoint-empty', { slot: this.targetSlot });
301
+ return undefined;
302
+ }
303
+
304
+ const minBlocksForCheckpoint = this.config.minBlocksForCheckpoint;
305
+ if (minBlocksForCheckpoint !== undefined && blocksInCheckpoint.length < minBlocksForCheckpoint) {
306
+ this.log.warn(
307
+ `Checkpoint has fewer blocks than minimum (${blocksInCheckpoint.length} < ${minBlocksForCheckpoint}), skipping proposal`,
308
+ { slot: this.targetSlot, blocksBuilt: blocksInCheckpoint.length, minBlocksForCheckpoint },
309
+ );
310
+ return undefined;
311
+ }
312
+
313
+ // Assemble and broadcast the checkpoint proposal, including the last block that was not
314
+ // broadcasted yet, and wait to collect the committee attestations.
315
+ this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.targetSlot);
316
+ const checkpoint = await checkpointBuilder.completeCheckpoint();
317
+
318
+ // Final validation: per-block limits are only checked if the operator set them explicitly.
319
+ // Otherwise, checkpoint-level budgets were already enforced by the redistribution logic.
320
+ try {
321
+ validateCheckpoint(checkpoint, {
322
+ rollupManaLimit: this.l1Constants.rollupManaLimit,
323
+ maxL2BlockGas: this.config.maxL2BlockGas,
324
+ maxDABlockGas: this.config.maxDABlockGas,
325
+ maxTxsPerBlock: this.config.maxTxsPerBlock,
326
+ maxTxsPerCheckpoint: this.config.maxTxsPerCheckpoint,
327
+ });
328
+ } catch (err) {
329
+ this.log.error(`Built an invalid checkpoint at slot ${this.slot} (skipping proposal)`, err, {
330
+ checkpoint: checkpoint.header.toInspect(),
331
+ });
332
+ return undefined;
333
+ }
334
+
335
+ // Record checkpoint-level build metrics
336
+ this.metrics.recordCheckpointBuild(
337
+ checkpointBuildTimer.ms(),
338
+ blocksInCheckpoint.length,
339
+ checkpoint.getStats().txCount,
340
+ Number(checkpoint.header.totalManaUsed.toBigInt()),
341
+ );
342
+
343
+ // Do not collect attestations nor publish to L1 in fisherman mode
344
+ if (this.config.fishermanMode) {
345
+ this.log.info(
346
+ `Built checkpoint for slot ${this.targetSlot} with ${blocksInCheckpoint.length} blocks. ` +
347
+ `Skipping proposal in fisherman mode.`,
348
+ {
349
+ slot: this.targetSlot,
350
+ checkpoint: checkpoint.header.toInspect(),
351
+ blocksBuilt: blocksInCheckpoint.length,
352
+ },
353
+ );
354
+ this.metrics.recordCheckpointSuccess();
355
+ return checkpoint;
356
+ }
357
+
358
+ // Include the block pending broadcast in the checkpoint proposal if any
359
+ const lastBlock = blockPendingBroadcast && {
360
+ blockHeader: blockPendingBroadcast.block.header,
361
+ indexWithinCheckpoint: blockPendingBroadcast.block.indexWithinCheckpoint,
362
+ txs: blockPendingBroadcast.txs,
363
+ };
364
+
365
+ // Create the checkpoint proposal and broadcast it
366
+ const proposal = await this.validatorClient.createCheckpointProposal(
367
+ checkpoint.header,
368
+ checkpoint.archive.root,
369
+ feeAssetPriceModifier,
370
+ lastBlock,
371
+ this.proposer,
372
+ checkpointProposalOptions,
373
+ );
374
+
375
+ const blockProposedAt = this.dateProvider.now();
376
+ await this.p2pClient.broadcastCheckpointProposal(proposal);
377
+
378
+ this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.targetSlot);
379
+ const attestations = await this.waitForAttestations(proposal);
380
+ const blockAttestedAt = this.dateProvider.now();
381
+
382
+ this.metrics.recordCheckpointAttestationDelay(blockAttestedAt - blockProposedAt);
383
+
384
+ // Proposer must sign over the attestations before pushing them to L1
385
+ const signer = this.proposer ?? this.publisher.getSenderAddress();
386
+ let attestationsSignature: Signature;
387
+ try {
388
+ attestationsSignature = await this.validatorClient.signAttestationsAndSigners(
389
+ attestations,
390
+ signer,
391
+ this.targetSlot,
392
+ this.checkpointNumber,
393
+ );
394
+ } catch (err) {
395
+ // We shouldn't really get here since we yield to another HA node
396
+ // as soon as we see these errors when creating block or checkpoint proposals.
397
+ if (this.handleHASigningError(err, 'Attestations signature')) {
398
+ return undefined;
399
+ }
400
+ throw err;
401
+ }
402
+
403
+ // Enqueue publishing the checkpoint to L1
404
+ this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.targetSlot);
405
+ const aztecSlotDuration = this.l1Constants.slotDuration;
406
+ const submissionSlotStart = Number(getTimestampForSlot(this.targetSlot, this.l1Constants));
407
+ const txTimeoutAt = new Date((submissionSlotStart + aztecSlotDuration) * 1000);
408
+
409
+ // If we have been configured to potentially skip publishing checkpoint then roll the dice here
410
+ if (
411
+ this.config.skipPublishingCheckpointsPercent !== undefined &&
412
+ this.config.skipPublishingCheckpointsPercent > 0
413
+ ) {
414
+ const result = Math.max(0, randomInt(100));
415
+ if (result < this.config.skipPublishingCheckpointsPercent) {
416
+ this.log.warn(
417
+ `Skipping publishing proposal for checkpoint ${checkpoint.number}. Configured percentage: ${this.config.skipPublishingCheckpointsPercent}, generated value: ${result}`,
418
+ );
419
+ return checkpoint;
420
+ }
421
+ }
422
+
423
+ await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
424
+ txTimeoutAt,
425
+ forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber,
426
+ });
427
+
428
+ return checkpoint;
429
+ } catch (err) {
430
+ if (err && (err instanceof DutyAlreadySignedError || err instanceof SlashingProtectionError)) {
431
+ // swallow this error. It's already been logged by a function deeper in the stack
432
+ return undefined;
433
+ }
434
+
435
+ this.log.error(`Error building checkpoint at slot ${this.slot}`, err);
436
+ return undefined;
437
+ }
438
+ }
439
+
440
+ /**
441
+ * Builds blocks for a checkpoint within the current slot.
442
+ */
443
+ @trackSpan('CheckpointProposalJob.buildBlocksForCheckpoint')
444
+ private async buildBlocksForCheckpoint(
445
+ checkpointBuilder: CheckpointBuilder,
446
+ timestamp: bigint,
447
+ inHash: Fr,
448
+ blockProposalOptions: BlockProposalOptions,
449
+ ): Promise<{
450
+ blocksInCheckpoint: L2Block[];
451
+ blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined;
452
+ }> {
453
+ const blocksInCheckpoint: L2Block[] = [];
454
+ const txHashesAlreadyIncluded = new Set<string>();
455
+ const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
456
+
457
+ // Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
458
+ let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
459
+
460
+ while (true) {
461
+ const blocksBuilt = blocksInCheckpoint.length;
462
+ const indexWithinCheckpoint = IndexWithinCheckpoint(blocksBuilt);
463
+ const blockNumber = BlockNumber(initialBlockNumber + blocksBuilt);
464
+
465
+ const secondsIntoSlot = this.getSecondsIntoSlot();
466
+ const timingInfo = this.timetable.canStartNextBlock(secondsIntoSlot);
467
+
468
+ if (!timingInfo.canStart) {
469
+ this.log.debug(`Not enough time left in slot to start another block`, {
470
+ slot: this.targetSlot,
471
+ blocksBuilt,
472
+ secondsIntoSlot,
473
+ });
474
+ break;
475
+ }
476
+
477
+ const buildResult = await this.buildSingleBlock(checkpointBuilder, {
478
+ // Create all blocks with the same timestamp
479
+ blockTimestamp: timestamp,
480
+ // Create an empty block if we haven't already and this is the last one
481
+ forceCreate: timingInfo.isLastBlock && blocksBuilt === 0 && this.config.buildCheckpointIfEmpty,
482
+ // Build deadline is only set if we are enforcing the timetable
483
+ buildDeadline: timingInfo.deadline
484
+ ? new Date((this.getSlotStartBuildTimestamp() + timingInfo.deadline) * 1000)
485
+ : undefined,
486
+ blockNumber,
487
+ indexWithinCheckpoint,
488
+ txHashesAlreadyIncluded,
489
+ });
490
+
491
+ // TODO(palla/mbps): Review these conditions. We may want to keep trying in some scenarios.
492
+ if (!buildResult && timingInfo.isLastBlock) {
493
+ // If no block was produced due to not enough txs and this was the last subslot, exit
494
+ break;
495
+ } else if (!buildResult && timingInfo.deadline !== undefined) {
496
+ // But if there is still time for more blocks, wait until the next subslot and try again
497
+ await this.waitUntilNextSubslot(timingInfo.deadline);
498
+ continue;
499
+ } else if (!buildResult) {
500
+ // Exit if there is no possibility of building more blocks
501
+ break;
502
+ } else if ('error' in buildResult) {
503
+ // If there was an error building the block, just exit the loop and give up the rest of the slot
504
+ if (!(buildResult.error instanceof SequencerInterruptedError)) {
505
+ this.log.warn(`Halting block building for slot ${this.targetSlot}`, {
506
+ slot: this.targetSlot,
507
+ blocksBuilt,
508
+ error: buildResult.error,
509
+ });
510
+ }
511
+ break;
512
+ }
513
+
514
+ const { block, usedTxs } = buildResult;
515
+ blocksInCheckpoint.push(block);
516
+ usedTxs.forEach(tx => txHashesAlreadyIncluded.add(tx.txHash.toString()));
517
+
518
+ // If this is the last block, sync it to the archiver and exit the loop
519
+ // so we can build the checkpoint and start collecting attestations.
520
+ if (timingInfo.isLastBlock) {
521
+ await this.syncProposedBlockToArchiver(block);
522
+ this.log.verbose(`Completed final block ${blockNumber} for slot ${this.targetSlot}`, {
523
+ slot: this.targetSlot,
524
+ blockNumber,
525
+ blocksBuilt,
526
+ });
527
+ blockPendingBroadcast = { block, txs: usedTxs };
528
+ break;
529
+ }
530
+
531
+ // Broadcast the block proposal (unless we're in fisherman mode) unless the block is the last one,
532
+ // in which case we'll broadcast it along with the checkpoint at the end of the loop.
533
+ // Note that we only send the block to the archiver if we manage to create the proposal, so if there's
534
+ // a HA error we don't pollute our archiver with a block that won't make it to the chain.
535
+ const proposal = await this.createBlockProposal(block, inHash, usedTxs, blockProposalOptions);
536
+
537
+ // Sync the proposed block to the archiver to make it available, only after we've managed to sign the proposal.
538
+ // We wait for the sync to succeed, as this helps catch consistency errors, even if it means we lose some time for block-building.
539
+ // If this throws, we abort the entire checkpoint.
540
+ await this.syncProposedBlockToArchiver(block);
541
+
542
+ // Once we have a signed proposal and the archiver agreed with our proposed block, then we broadcast it.
543
+ proposal && (await this.p2pClient.broadcastProposal(proposal));
544
+
545
+ // Wait until the next block's start time
546
+ await this.waitUntilNextSubslot(timingInfo.deadline);
547
+ }
548
+
549
+ this.log.verbose(`Block building loop completed for slot ${this.targetSlot}`, {
550
+ slot: this.targetSlot,
551
+ blocksBuilt: blocksInCheckpoint.length,
552
+ });
553
+
554
+ return { blocksInCheckpoint, blockPendingBroadcast };
555
+ }
556
+
557
+ /** Creates a block proposal for a given block via the validator client (unless in fisherman mode) */
558
+ private createBlockProposal(
559
+ block: L2Block,
560
+ inHash: Fr,
561
+ usedTxs: Tx[],
562
+ blockProposalOptions: BlockProposalOptions,
563
+ ): Promise<BlockProposal | undefined> {
564
+ if (this.config.fishermanMode) {
565
+ this.log.info(`Skipping block proposal for block ${block.number} in fisherman mode`);
566
+ return Promise.resolve(undefined);
567
+ }
568
+ return this.validatorClient.createBlockProposal(
569
+ block.header,
570
+ block.indexWithinCheckpoint,
571
+ inHash,
572
+ block.archive.root,
573
+ usedTxs,
574
+ this.proposer,
575
+ blockProposalOptions,
576
+ );
577
+ }
578
+
579
+ /** Sleeps until it is time to produce the next block in the slot */
580
+ @trackSpan('CheckpointProposalJob.waitUntilNextSubslot')
581
+ private async waitUntilNextSubslot(nextSubslotStart: number) {
582
+ this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.targetSlot);
583
+ this.log.verbose(`Waiting until time for the next block at ${nextSubslotStart}s into slot`, {
584
+ slot: this.targetSlot,
585
+ });
586
+ await this.waitUntilTimeInSlot(nextSubslotStart);
587
+ }
588
+
589
+ /** Builds a single block. Called from the main block building loop. */
590
+ @trackSpan('CheckpointProposalJob.buildSingleBlock')
591
+ protected async buildSingleBlock(
592
+ checkpointBuilder: CheckpointBuilder,
593
+ opts: {
594
+ forceCreate?: boolean;
595
+ blockTimestamp: bigint;
596
+ blockNumber: BlockNumber;
597
+ indexWithinCheckpoint: IndexWithinCheckpoint;
598
+ buildDeadline: Date | undefined;
599
+ txHashesAlreadyIncluded: Set<string>;
600
+ },
601
+ ): Promise<{ block: L2Block; usedTxs: Tx[] } | { error: Error } | undefined> {
602
+ const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded } =
603
+ opts;
604
+
605
+ this.log.verbose(
606
+ `Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.targetSlot}`,
607
+ { ...checkpointBuilder.getConstantData(), ...opts },
608
+ );
609
+
610
+ try {
611
+ // Wait until we have enough txs to build the block
612
+ const { availableTxs, canStartBuilding, minTxs } = await this.waitForMinTxs(opts);
613
+ if (!canStartBuilding) {
614
+ this.log.warn(
615
+ `Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.targetSlot} (got ${availableTxs} txs but needs ${minTxs})`,
616
+ { blockNumber, slot: this.targetSlot, indexWithinCheckpoint },
617
+ );
618
+ this.eventEmitter.emit('block-tx-count-check-failed', { minTxs, availableTxs, slot: this.targetSlot });
619
+ this.metrics.recordBlockProposalFailed('insufficient_txs');
620
+ return undefined;
621
+ }
622
+
623
+ // Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
624
+ // just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
625
+ const pendingTxs = filter(
626
+ this.p2pClient.iterateEligiblePendingTxs(),
627
+ tx => !txHashesAlreadyIncluded.has(tx.txHash.toString()),
628
+ );
629
+
630
+ this.log.debug(
631
+ `Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.targetSlot} with ${availableTxs} available txs`,
632
+ { slot: this.targetSlot, blockNumber, indexWithinCheckpoint },
633
+ );
634
+ this.setStateFn(SequencerState.CREATING_BLOCK, this.targetSlot);
635
+
636
+ // Per-block limits are operator overrides (from SEQ_MAX_L2_BLOCK_GAS etc.) further capped
637
+ // by remaining checkpoint-level budgets inside CheckpointBuilder before each block is built.
638
+ // minValidTxs is passed into the builder so it can reject the block *before* updating state.
639
+ const minValidTxs = forceCreate ? 0 : (this.config.minValidTxsPerBlock ?? minTxs);
640
+ const blockBuilderOptions: BlockBuilderOptions = {
641
+ maxTransactions: this.config.maxTxsPerBlock,
642
+ maxBlockGas:
643
+ this.config.maxL2BlockGas !== undefined || this.config.maxDABlockGas !== undefined
644
+ ? new Gas(this.config.maxDABlockGas ?? Infinity, this.config.maxL2BlockGas ?? Infinity)
645
+ : undefined,
646
+ deadline: buildDeadline,
647
+ isBuildingProposal: true,
648
+ minValidTxs,
649
+ maxBlocksPerCheckpoint: this.timetable.maxNumberOfBlocks,
650
+ perBlockAllocationMultiplier: this.config.perBlockAllocationMultiplier,
651
+ };
652
+
653
+ // Actually build the block by executing txs. The builder throws InsufficientValidTxsError
654
+ // if the number of successfully processed txs is below minValidTxs, ensuring state is not
655
+ // updated for blocks that will be discarded.
656
+ const buildResult = await this.buildSingleBlockWithCheckpointBuilder(
657
+ checkpointBuilder,
658
+ pendingTxs,
659
+ blockNumber,
660
+ blockTimestamp,
661
+ blockBuilderOptions,
662
+ );
663
+
664
+ // If any txs failed during execution, drop them from the mempool so we don't pick them up again
665
+ await this.dropFailedTxsFromP2P(buildResult.failedTxs);
666
+
667
+ if (buildResult.status === 'insufficient-valid-txs') {
668
+ this.log.warn(
669
+ `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.targetSlot} has too few valid txs to be proposed`,
670
+ {
671
+ slot: this.targetSlot,
672
+ blockNumber,
673
+ numTxs: buildResult.processedCount,
674
+ indexWithinCheckpoint,
675
+ minValidTxs,
676
+ },
677
+ );
678
+ this.eventEmitter.emit('block-build-failed', {
679
+ reason: `Insufficient valid txs`,
680
+ slot: this.targetSlot,
681
+ });
682
+ this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
683
+ return undefined;
684
+ }
685
+
686
+ // Block creation succeeded, emit stats and metrics
687
+ const { block, publicProcessorDuration, usedTxs, blockBuildDuration, numTxs } = buildResult;
688
+
689
+ const blockStats = {
690
+ eventName: 'l2-block-built',
691
+ duration: blockBuildDuration,
692
+ publicProcessDuration: publicProcessorDuration,
693
+ ...block.getStats(),
694
+ } satisfies L2BlockBuiltStats;
695
+
696
+ const blockHash = await block.hash();
697
+ const txHashes = block.body.txEffects.map(tx => tx.txHash);
698
+ const manaPerSec = block.header.totalManaUsed.toNumberUnsafe() / (blockBuildDuration / 1000);
699
+
700
+ this.log.info(
701
+ `Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.targetSlot} with ${numTxs} txs`,
702
+ { blockHash, txHashes, manaPerSec, ...blockStats },
703
+ );
704
+
705
+ this.eventEmitter.emit('block-proposed', {
706
+ blockNumber: block.number,
707
+ slot: this.targetSlot,
708
+ buildSlot: this.slotNow,
709
+ });
710
+ this.metrics.recordBuiltBlock(blockBuildDuration, block.header.totalManaUsed.toNumberUnsafe());
711
+
712
+ return { block, usedTxs };
713
+ } catch (err: any) {
714
+ this.eventEmitter.emit('block-build-failed', {
715
+ reason: err.message,
716
+ slot: this.targetSlot,
717
+ });
718
+ this.log.error(`Error building block`, err, { blockNumber, slot: this.targetSlot });
719
+ this.metrics.recordBlockProposalFailed(err.name || 'unknown_error');
720
+ this.metrics.recordFailedBlock();
721
+ return { error: err };
722
+ }
723
+ }
724
+
725
+ /** Uses the checkpoint builder to build a block, catching InsufficientValidTxsError. */
726
+ private async buildSingleBlockWithCheckpointBuilder(
727
+ checkpointBuilder: CheckpointBuilder,
728
+ pendingTxs: AsyncIterable<Tx>,
729
+ blockNumber: BlockNumber,
730
+ blockTimestamp: bigint,
731
+ blockBuilderOptions: BlockBuilderOptions,
732
+ ) {
733
+ try {
734
+ const workTimer = new Timer();
735
+ const result = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
736
+ const blockBuildDuration = workTimer.ms();
737
+ return { ...result, blockBuildDuration, status: 'success' as const };
738
+ } catch (err: unknown) {
739
+ if (isErrorClass(err, InsufficientValidTxsError)) {
740
+ return {
741
+ failedTxs: err.failedTxs,
742
+ processedCount: err.processedCount,
743
+ status: 'insufficient-valid-txs' as const,
744
+ };
745
+ }
746
+ throw err;
747
+ }
748
+ }
749
+
750
+ /** Waits until minTxs are available on the pool for building a block. */
751
+ @trackSpan('CheckpointProposalJob.waitForMinTxs')
752
+ private async waitForMinTxs(opts: {
753
+ forceCreate?: boolean;
754
+ blockNumber: BlockNumber;
755
+ indexWithinCheckpoint: IndexWithinCheckpoint;
756
+ buildDeadline: Date | undefined;
757
+ }): Promise<{ canStartBuilding: boolean; availableTxs: number; minTxs: number }> {
758
+ const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
759
+
760
+ // We only allow a block with 0 txs in the first block of the checkpoint
761
+ const minTxs = indexWithinCheckpoint > 0 && this.config.minTxsPerBlock === 0 ? 1 : this.config.minTxsPerBlock;
762
+
763
+ // Deadline is undefined if we are not enforcing the timetable, meaning we'll exit immediately when out of time
764
+ const startBuildingDeadline = buildDeadline
765
+ ? new Date(buildDeadline.getTime() - this.timetable.minExecutionTime * 1000)
766
+ : undefined;
767
+
768
+ let availableTxs = await this.p2pClient.getPendingTxCount();
769
+
770
+ while (!forceCreate && availableTxs < minTxs) {
771
+ // If we're past deadline, or we have no deadline, give up
772
+ const now = this.dateProvider.nowAsDate();
773
+ if (startBuildingDeadline === undefined || now >= startBuildingDeadline) {
774
+ return { canStartBuilding: false, availableTxs, minTxs };
775
+ }
776
+
777
+ // Wait a bit before checking again
778
+ this.setStateFn(SequencerState.WAITING_FOR_TXS, this.targetSlot);
779
+ this.log.verbose(
780
+ `Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.targetSlot} (have ${availableTxs} but need ${minTxs})`,
781
+ { blockNumber, slot: this.targetSlot, indexWithinCheckpoint },
782
+ );
783
+ await this.waitForTxsPollingInterval();
784
+ availableTxs = await this.p2pClient.getPendingTxCount();
785
+ }
786
+
787
+ return { canStartBuilding: true, availableTxs, minTxs };
788
+ }
789
+
790
+ /**
791
+ * Waits for enough attestations to be collected via p2p.
792
+ * This is run after all blocks for the checkpoint have been built.
793
+ */
794
+ @trackSpan('CheckpointProposalJob.waitForAttestations')
795
+ private async waitForAttestations(proposal: CheckpointProposal): Promise<CommitteeAttestationsAndSigners> {
796
+ if (this.config.fishermanMode) {
797
+ this.log.debug('Skipping attestation collection in fisherman mode');
798
+ return CommitteeAttestationsAndSigners.empty();
799
+ }
800
+
801
+ const slotNumber = proposal.slotNumber;
802
+ const { committee, seed, epoch } = await this.epochCache.getCommittee(slotNumber);
803
+
804
+ if (!committee) {
805
+ throw new Error('No committee when collecting attestations');
806
+ } else if (committee.length === 0) {
807
+ this.log.verbose(`Attesting committee is empty`);
808
+ return CommitteeAttestationsAndSigners.empty();
809
+ } else {
810
+ this.log.debug(`Attesting committee length is ${committee.length}`, { committee });
811
+ }
812
+
813
+ const numberOfRequiredAttestations = Math.floor((committee.length * 2) / 3) + 1;
814
+
815
+ if (this.config.skipCollectingAttestations) {
816
+ this.log.warn('Skipping attestation collection as per config (attesting with own keys only)');
817
+ const attestations = await this.validatorClient?.collectOwnAttestations(proposal);
818
+ return new CommitteeAttestationsAndSigners(orderAttestations(attestations ?? [], committee));
819
+ }
820
+
821
+ const attestationTimeAllowed = this.config.enforceTimeTable
822
+ ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_CHECKPOINT)!
823
+ : this.l1Constants.slotDuration;
824
+ const attestationDeadline = new Date((this.getSlotStartBuildTimestamp() + attestationTimeAllowed) * 1000);
825
+
826
+ this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
827
+
828
+ const collectAttestationsTimer = new Timer();
829
+ let collectedAttestationsCount: number = 0;
830
+ try {
831
+ const attestations = await this.validatorClient.collectAttestations(
832
+ proposal,
833
+ numberOfRequiredAttestations,
834
+ attestationDeadline,
835
+ );
836
+
837
+ collectedAttestationsCount = attestations.length;
838
+
839
+ // Trim attestations to minimum required to save L1 calldata gas
840
+ const localAddresses = this.validatorClient.getValidatorAddresses();
841
+ const trimmed = trimAttestations(
842
+ attestations,
843
+ numberOfRequiredAttestations,
844
+ this.attestorAddress,
845
+ localAddresses,
846
+ );
847
+ if (trimmed.length < attestations.length) {
848
+ this.log.debug(`Trimmed attestations from ${attestations.length} to ${trimmed.length} for L1 submission`);
849
+ }
850
+
851
+ // Rollup contract requires that the signatures are provided in the order of the committee
852
+ const sorted = orderAttestations(trimmed, committee);
853
+
854
+ // Manipulate the attestations if we've been configured to do so
855
+ if (
856
+ this.config.injectFakeAttestation ||
857
+ this.config.injectHighSValueAttestation ||
858
+ this.config.injectUnrecoverableSignatureAttestation ||
859
+ this.config.shuffleAttestationOrdering
860
+ ) {
861
+ return this.manipulateAttestations(proposal.slotNumber, epoch, seed, committee, sorted);
862
+ }
863
+
864
+ return new CommitteeAttestationsAndSigners(sorted);
865
+ } catch (err) {
866
+ if (err && err instanceof AttestationTimeoutError) {
867
+ collectedAttestationsCount = err.collectedCount;
868
+ }
869
+ throw err;
870
+ } finally {
871
+ this.metrics.recordCollectedAttestations(collectedAttestationsCount, collectAttestationsTimer.ms());
872
+ }
873
+ }
874
+
875
+ /** Breaks the attestations before publishing based on attack configs */
876
+ private manipulateAttestations(
877
+ slotNumber: SlotNumber,
878
+ epoch: EpochNumber,
879
+ seed: bigint,
880
+ committee: EthAddress[],
881
+ attestations: CommitteeAttestation[],
882
+ ) {
883
+ // Compute the proposer index in the committee, since we dont want to tweak it.
884
+ // Otherwise, the L1 rollup contract will reject the block outright.
885
+ const proposerIndex = Number(
886
+ this.epochCache.computeProposerIndex(slotNumber, epoch, seed, BigInt(committee.length)),
887
+ );
888
+
889
+ if (
890
+ this.config.injectFakeAttestation ||
891
+ this.config.injectHighSValueAttestation ||
892
+ this.config.injectUnrecoverableSignatureAttestation
893
+ ) {
894
+ // Find non-empty attestations that are not from the proposer
895
+ const nonProposerIndices: number[] = [];
896
+ for (let i = 0; i < attestations.length; i++) {
897
+ if (!attestations[i].signature.isEmpty() && i !== proposerIndex) {
898
+ nonProposerIndices.push(i);
899
+ }
900
+ }
901
+ if (nonProposerIndices.length > 0) {
902
+ const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
903
+ if (this.config.injectHighSValueAttestation) {
904
+ this.log.warn(
905
+ `Injecting high-s value attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
906
+ );
907
+ unfreeze(attestations[targetIndex]).signature = flipSignature(attestations[targetIndex].signature);
908
+ } else if (this.config.injectUnrecoverableSignatureAttestation) {
909
+ this.log.warn(
910
+ `Injecting unrecoverable signature attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
911
+ );
912
+ unfreeze(attestations[targetIndex]).signature = generateUnrecoverableSignature();
913
+ } else {
914
+ this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
915
+ unfreeze(attestations[targetIndex]).signature = generateRecoverableSignature();
916
+ }
917
+ }
918
+ return new CommitteeAttestationsAndSigners(attestations);
919
+ }
920
+
921
+ if (this.config.shuffleAttestationOrdering) {
922
+ this.log.warn(`Shuffling attestation ordering in checkpoint for slot ${slotNumber} (proposer #${proposerIndex})`);
923
+
924
+ const shuffled = [...attestations];
925
+
926
+ // Find two non-proposer positions that both have non-empty signatures to swap.
927
+ // This ensures the bitmap doesn't change, so the MaliciousCommitteeAttestationsAndSigners
928
+ // signers array stays correctly aligned with L1's committee reconstruction.
929
+ const swappable: number[] = [];
930
+ for (let k = 0; k < shuffled.length; k++) {
931
+ if (!shuffled[k].signature.isEmpty() && k !== proposerIndex) {
932
+ swappable.push(k);
933
+ }
934
+ }
935
+ if (swappable.length >= 2) {
936
+ const [i, j] = [swappable[0], swappable[1]];
937
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
938
+ }
939
+
940
+ const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
941
+ return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
942
+ }
943
+
944
+ return new CommitteeAttestationsAndSigners(attestations);
945
+ }
946
+
947
+ private async dropFailedTxsFromP2P(failedTxs: FailedTx[]) {
948
+ if (failedTxs.length === 0) {
949
+ return;
950
+ }
951
+ const failedTxData = failedTxs.map(fail => fail.tx);
952
+ const failedTxHashes = failedTxData.map(tx => tx.getTxHash());
953
+ this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
954
+ await this.p2pClient.handleFailedExecution(failedTxHashes);
955
+ }
956
+
957
+ /**
958
+ * Adds the proposed block to the archiver so it's available via P2P.
959
+ * Gossip doesn't echo messages back to the sender, so the proposer's archiver/world-state
960
+ * would never receive its own block without this explicit sync.
961
+ */
962
+ private async syncProposedBlockToArchiver(block: L2Block): Promise<void> {
963
+ if (this.config.skipPushProposedBlocksToArchiver !== false) {
964
+ this.log.warn(`Skipping push of proposed block ${block.number} to archiver`, {
965
+ blockNumber: block.number,
966
+ slot: block.header.globalVariables.slotNumber,
967
+ });
968
+ return;
969
+ }
970
+ this.log.debug(`Syncing proposed block ${block.number} to archiver`, {
971
+ blockNumber: block.number,
972
+ slot: block.header.globalVariables.slotNumber,
973
+ });
974
+ await this.blockSink.addBlock(block);
975
+ }
976
+
977
+ /** Runs fee analysis and logs checkpoint outcome as fisherman */
978
+ private async handleCheckpointEndAsFisherman(checkpoint: Checkpoint | undefined) {
979
+ // Perform L1 fee analysis before clearing requests
980
+ // The callback is invoked asynchronously after the next block is mined
981
+ const feeAnalysis = await this.publisher.analyzeL1Fees(this.targetSlot, analysis =>
982
+ this.metrics.recordFishermanFeeAnalysis(analysis),
983
+ );
984
+
985
+ if (checkpoint) {
986
+ this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.targetSlot}`, {
987
+ ...checkpoint.toCheckpointInfo(),
988
+ ...checkpoint.getStats(),
989
+ feeAnalysisId: feeAnalysis?.id,
990
+ });
991
+ } else {
992
+ this.log.warn(`Validation block building FAILED for slot ${this.targetSlot}`, {
993
+ slot: this.targetSlot,
994
+ feeAnalysisId: feeAnalysis?.id,
995
+ });
996
+ this.metrics.recordCheckpointProposalFailed('block_build_failed');
997
+ }
998
+
999
+ this.publisher.clearPendingRequests();
1000
+ }
1001
+
1002
+ /**
1003
+ * Helper to handle HA double-signing errors. Returns true if the error was handled (caller should yield).
1004
+ */
1005
+ private handleHASigningError(err: any, errorContext: string): boolean {
1006
+ if (err instanceof DutyAlreadySignedError) {
1007
+ this.log.info(`${errorContext} for slot ${this.targetSlot} already signed by another HA node, yielding`, {
1008
+ slot: this.targetSlot,
1009
+ signedByNode: err.signedByNode,
1010
+ });
1011
+ return true;
1012
+ }
1013
+ if (err instanceof SlashingProtectionError) {
1014
+ this.log.info(`${errorContext} for slot ${this.targetSlot} blocked by slashing protection, yielding`, {
1015
+ slot: this.targetSlot,
1016
+ existingMessageHash: err.existingMessageHash,
1017
+ attemptedMessageHash: err.attemptedMessageHash,
1018
+ });
1019
+ return true;
1020
+ }
1021
+ return false;
1022
+ }
1023
+
1024
+ /** Waits until a specific time within the current slot */
1025
+ @trackSpan('CheckpointProposalJob.waitUntilTimeInSlot')
1026
+ protected async waitUntilTimeInSlot(targetSecondsIntoSlot: number): Promise<void> {
1027
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp();
1028
+ const targetTimestamp = slotStartTimestamp + targetSecondsIntoSlot;
1029
+ await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
1030
+ }
1031
+
1032
+ /** Waits the polling interval for transactions. Extracted for test overriding. */
1033
+ protected async waitForTxsPollingInterval(): Promise<void> {
1034
+ await sleep(TXS_POLLING_MS);
1035
+ }
1036
+
1037
+ private getSlotStartBuildTimestamp(): number {
1038
+ return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
1039
+ }
1040
+
1041
+ private getSecondsIntoSlot(): number {
1042
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp();
1043
+ return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
1044
+ }
1045
+
1046
+ public getPublisher() {
1047
+ return this.publisher;
1048
+ }
1049
+ }