@aztec/sequencer-client 3.0.0-nightly.20251221 → 3.0.0-nightly.20251223

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 (79) hide show
  1. package/dest/client/sequencer-client.d.ts +9 -8
  2. package/dest/client/sequencer-client.d.ts.map +1 -1
  3. package/dest/client/sequencer-client.js +28 -24
  4. package/dest/config.d.ts +7 -1
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +63 -26
  7. package/dest/global_variable_builder/global_builder.d.ts +16 -8
  8. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  9. package/dest/global_variable_builder/global_builder.js +35 -26
  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 +3 -3
  14. package/dest/publisher/config.d.ts.map +1 -1
  15. package/dest/publisher/config.js +2 -2
  16. package/dest/publisher/sequencer-publisher-factory.d.ts +3 -3
  17. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  18. package/dest/publisher/sequencer-publisher-factory.js +1 -1
  19. package/dest/publisher/sequencer-publisher-metrics.d.ts +3 -3
  20. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  21. package/dest/publisher/sequencer-publisher.d.ts +11 -24
  22. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  23. package/dest/publisher/sequencer-publisher.js +50 -62
  24. package/dest/sequencer/block_builder.d.ts +1 -3
  25. package/dest/sequencer/block_builder.d.ts.map +1 -1
  26. package/dest/sequencer/block_builder.js +4 -2
  27. package/dest/sequencer/checkpoint_builder.d.ts +63 -0
  28. package/dest/sequencer/checkpoint_builder.d.ts.map +1 -0
  29. package/dest/sequencer/checkpoint_builder.js +131 -0
  30. package/dest/sequencer/checkpoint_proposal_job.d.ts +73 -0
  31. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
  32. package/dest/sequencer/checkpoint_proposal_job.js +638 -0
  33. package/dest/sequencer/checkpoint_voter.d.ts +34 -0
  34. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
  35. package/dest/sequencer/checkpoint_voter.js +85 -0
  36. package/dest/sequencer/events.d.ts +46 -0
  37. package/dest/sequencer/events.d.ts.map +1 -0
  38. package/dest/sequencer/events.js +1 -0
  39. package/dest/sequencer/index.d.ts +5 -1
  40. package/dest/sequencer/index.d.ts.map +1 -1
  41. package/dest/sequencer/index.js +4 -0
  42. package/dest/sequencer/metrics.d.ts +3 -1
  43. package/dest/sequencer/metrics.d.ts.map +1 -1
  44. package/dest/sequencer/metrics.js +9 -0
  45. package/dest/sequencer/sequencer.d.ts +87 -127
  46. package/dest/sequencer/sequencer.d.ts.map +1 -1
  47. package/dest/sequencer/sequencer.js +179 -596
  48. package/dest/sequencer/timetable.d.ts +33 -13
  49. package/dest/sequencer/timetable.d.ts.map +1 -1
  50. package/dest/sequencer/timetable.js +73 -39
  51. package/dest/sequencer/types.d.ts +3 -0
  52. package/dest/sequencer/types.d.ts.map +1 -0
  53. package/dest/sequencer/types.js +1 -0
  54. package/dest/sequencer/utils.d.ts +14 -8
  55. package/dest/sequencer/utils.d.ts.map +1 -1
  56. package/dest/sequencer/utils.js +7 -4
  57. package/dest/test/index.d.ts +3 -1
  58. package/dest/test/index.d.ts.map +1 -1
  59. package/package.json +27 -27
  60. package/src/client/sequencer-client.ts +24 -31
  61. package/src/config.ts +68 -25
  62. package/src/global_variable_builder/global_builder.ts +45 -39
  63. package/src/index.ts +2 -0
  64. package/src/publisher/config.ts +3 -3
  65. package/src/publisher/sequencer-publisher-factory.ts +3 -3
  66. package/src/publisher/sequencer-publisher-metrics.ts +2 -2
  67. package/src/publisher/sequencer-publisher.ts +71 -74
  68. package/src/sequencer/block_builder.ts +4 -1
  69. package/src/sequencer/checkpoint_builder.ts +217 -0
  70. package/src/sequencer/checkpoint_proposal_job.ts +701 -0
  71. package/src/sequencer/checkpoint_voter.ts +105 -0
  72. package/src/sequencer/events.ts +27 -0
  73. package/src/sequencer/index.ts +4 -0
  74. package/src/sequencer/metrics.ts +11 -0
  75. package/src/sequencer/sequencer.ts +275 -804
  76. package/src/sequencer/timetable.ts +84 -49
  77. package/src/sequencer/types.ts +6 -0
  78. package/src/sequencer/utils.ts +18 -9
  79. package/src/test/index.ts +2 -0
@@ -0,0 +1,701 @@
1
+ import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB } from '@aztec/constants';
2
+ import type { EpochCache } from '@aztec/epoch-cache';
3
+ import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
+ import { randomInt } from '@aztec/foundation/crypto/random';
5
+ import { EthAddress } from '@aztec/foundation/eth-address';
6
+ import { Signature } from '@aztec/foundation/eth-signature';
7
+ import { filter } from '@aztec/foundation/iterator';
8
+ import type { Logger } from '@aztec/foundation/log';
9
+ import { sleep, sleepUntil } from '@aztec/foundation/sleep';
10
+ import { type DateProvider, Timer } from '@aztec/foundation/timer';
11
+ import { type TypedEventEmitter, unfreeze } from '@aztec/foundation/types';
12
+ import type { P2P } from '@aztec/p2p';
13
+ import type { SlasherClientInterface } from '@aztec/slasher';
14
+ import {
15
+ CommitteeAttestation,
16
+ CommitteeAttestationsAndSigners,
17
+ L2BlockNew,
18
+ MaliciousCommitteeAttestationsAndSigners,
19
+ } from '@aztec/stdlib/block';
20
+ import type { Checkpoint } from '@aztec/stdlib/checkpoint';
21
+ import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
22
+ import { Gas } from '@aztec/stdlib/gas';
23
+ import type {
24
+ PublicProcessorLimits,
25
+ ResolvedSequencerConfig,
26
+ WorldStateSynchronizer,
27
+ } from '@aztec/stdlib/interfaces/server';
28
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
29
+ import type { BlockProposal, BlockProposalOptions } from '@aztec/stdlib/p2p';
30
+ import { orderAttestations } from '@aztec/stdlib/p2p';
31
+ import { CheckpointHeader } from '@aztec/stdlib/rollup';
32
+ import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
33
+ import { type FailedTx, Tx } from '@aztec/stdlib/tx';
34
+ import { AttestationTimeoutError } from '@aztec/stdlib/validators';
35
+ import type { ValidatorClient } from '@aztec/validator-client';
36
+
37
+ import type { GlobalVariableBuilder } from '../global_variable_builder/global_builder.js';
38
+ import type { InvalidateBlockRequest, SequencerPublisher } from '../publisher/sequencer-publisher.js';
39
+ import { CheckpointBuilder, type FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
40
+ import { CheckpointVoter } from './checkpoint_voter.js';
41
+ import { SequencerInterruptedError } from './errors.js';
42
+ import type { SequencerEvents } from './events.js';
43
+ import type { SequencerMetrics } from './metrics.js';
44
+ import type { SequencerTimetable } from './timetable.js';
45
+ import type { SequencerRollupConstants } from './types.js';
46
+ import { SequencerState } from './utils.js';
47
+
48
+ /** How much time to sleep while waiting for min transactions to accumulate for a block */
49
+ const TXS_POLLING_MS = 500;
50
+
51
+ /** What's the latest time before a block build deadline we're willing to *start* building it */
52
+ const MIN_BLOCK_BUILD_TIME_MS = 1000;
53
+
54
+ /**
55
+ * Handles the execution of a checkpoint proposal after the initial preparation phase.
56
+ * This includes building blocks, collecting attestations, and publishing the checkpoint to L1,
57
+ * as well as enqueueing votes for slashing and governance proposals. This class is created from
58
+ * the Sequencer once the check for being the proposer for the slot has succeeded.
59
+ */
60
+ export class CheckpointProposalJob {
61
+ constructor(
62
+ private readonly slot: SlotNumber,
63
+ private readonly checkpointNumber: CheckpointNumber,
64
+ private readonly syncedToBlockNumber: BlockNumber,
65
+ // TODO(palla/mbps): Can we remove the proposer in favor of attestorAddress? Need to check fisherman-node flows.
66
+ private readonly proposer: EthAddress | undefined,
67
+ private readonly publisher: SequencerPublisher,
68
+ private readonly attestorAddress: EthAddress,
69
+ private readonly invalidateBlock: InvalidateBlockRequest | undefined,
70
+ private readonly validatorClient: ValidatorClient,
71
+ private readonly globalsBuilder: GlobalVariableBuilder,
72
+ private readonly p2pClient: P2P,
73
+ private readonly worldState: WorldStateSynchronizer,
74
+ private readonly l1ToL2MessageSource: L1ToL2MessageSource,
75
+ private readonly checkpointsBuilder: FullNodeCheckpointsBuilder,
76
+ private readonly l1Constants: SequencerRollupConstants,
77
+ private readonly config: ResolvedSequencerConfig,
78
+ private readonly timetable: SequencerTimetable,
79
+ private readonly slasherClient: SlasherClientInterface | undefined,
80
+ private readonly epochCache: EpochCache,
81
+ private readonly dateProvider: DateProvider,
82
+ private readonly metrics: SequencerMetrics,
83
+ private readonly eventEmitter: TypedEventEmitter<SequencerEvents>,
84
+ private readonly setStateFn: (state: SequencerState, slot?: SlotNumber) => void,
85
+ private readonly log: Logger,
86
+ ) {}
87
+
88
+ /**
89
+ * Executes the checkpoint proposal job.
90
+ * Returns the published checkpoint if successful, undefined otherwise.
91
+ */
92
+ public async execute(): Promise<Checkpoint | undefined> {
93
+ // Enqueue governance and slashing votes (returns promises that will be awaited later)
94
+ // In fisherman mode, we simulate slashing but don't actually publish to L1
95
+ // These are constant for the whole slot, so we only enqueue them once
96
+ const votesPromises = new CheckpointVoter(
97
+ this.slot,
98
+ this.publisher,
99
+ this.attestorAddress,
100
+ this.validatorClient,
101
+ this.slasherClient,
102
+ this.l1Constants,
103
+ this.config,
104
+ this.metrics,
105
+ this.log,
106
+ ).enqueueVotes();
107
+
108
+ // Build and propose the checkpoint. This will enqueue the request on the publisher if a checkpoint is built.
109
+ const checkpoint = await this.proposeCheckpoint();
110
+
111
+ // Wait until the voting promises have resolved, so all requests are enqueued (not sent)
112
+ await Promise.all(votesPromises);
113
+
114
+ // Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
115
+ if (this.config.fishermanMode) {
116
+ await this.handleCheckpointEndAsFisherman(checkpoint);
117
+ return;
118
+ }
119
+
120
+ // Then send everything to L1
121
+ const l1Response = await this.publisher.sendRequests();
122
+ const proposedAction = l1Response?.successfulActions.find(a => a === 'propose');
123
+ if (proposedAction) {
124
+ this.eventEmitter.emit('checkpoint-published', { checkpoint: this.checkpointNumber, slot: this.slot });
125
+ const coinbase = checkpoint?.header.coinbase;
126
+ await this.metrics.incFilledSlot(this.publisher.getSenderAddress().toString(), coinbase);
127
+ return checkpoint;
128
+ } else if (checkpoint) {
129
+ this.eventEmitter.emit('checkpoint-publish-failed', { ...l1Response, slot: this.slot });
130
+ return undefined;
131
+ }
132
+ }
133
+
134
+ private async proposeCheckpoint(): Promise<Checkpoint | undefined> {
135
+ try {
136
+ // Get operator configured coinbase and fee recipient for this attestor
137
+ const coinbase = this.validatorClient.getCoinbaseForAttestor(this.attestorAddress);
138
+ const feeRecipient = this.validatorClient.getFeeRecipientForAttestor(this.attestorAddress);
139
+
140
+ // Start the checkpoint
141
+ this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.slot);
142
+ this.metrics.incOpenSlot(this.slot, this.proposer?.toString() ?? 'unknown');
143
+
144
+ // Enqueues block invalidation (constant for the whole slot)
145
+ if (this.invalidateBlock && !this.config.skipInvalidateBlockAsProposer) {
146
+ this.publisher.enqueueInvalidateBlock(this.invalidateBlock);
147
+ }
148
+
149
+ // Create checkpoint builder for the slot
150
+ const checkpointGlobalVariables = await this.globalsBuilder.buildCheckpointGlobalVariables(
151
+ coinbase,
152
+ feeRecipient,
153
+ this.slot,
154
+ );
155
+
156
+ // Collect L1 to L2 messages for the checkpoint
157
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(this.checkpointNumber);
158
+
159
+ // Create a long-lived forked world state for the checkpoint builder
160
+ using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
161
+
162
+ // Create checkpoint builder for the entire slot
163
+ const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(
164
+ this.checkpointNumber,
165
+ checkpointGlobalVariables,
166
+ l1ToL2Messages,
167
+ fork,
168
+ );
169
+
170
+ // Options for the validator client when creating block and checkpoint proposals
171
+ const blockProposalOptions: BlockProposalOptions = {
172
+ publishFullTxs: !!this.config.publishTxsWithProposals,
173
+ broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
174
+ };
175
+
176
+ // Main loop: build blocks for the checkpoint
177
+ const { blocksInCheckpoint, pendingBroadcast } = await this.buildBlocksForCheckpoint(
178
+ checkpointBuilder,
179
+ checkpointGlobalVariables.timestamp,
180
+ blockProposalOptions,
181
+ );
182
+
183
+ if (blocksInCheckpoint.length === 0) {
184
+ this.log.warn(`No blocks were built for slot ${this.slot}`, { slot: this.slot });
185
+ this.eventEmitter.emit('checkpoint-empty', { slot: this.slot });
186
+ return undefined;
187
+ }
188
+
189
+ // Assemble and broadcast the checkpoint proposal, including the last block that was not
190
+ // broadcasted yet, and wait to collect the committee attestations.
191
+ this.setStateFn(SequencerState.FINALIZING_CHECKPOINT, this.slot);
192
+ const checkpoint = await checkpointBuilder.completeCheckpoint();
193
+
194
+ // Do not collect attestations nor publish to L1 in fisherman mode
195
+ if (this.config.fishermanMode) {
196
+ this.log.info(
197
+ `Built checkpoint for slot ${this.slot} with ${blocksInCheckpoint.length} blocks. ` +
198
+ `Skipping proposal in fisherman mode.`,
199
+ {
200
+ slot: this.slot,
201
+ checkpoint: checkpoint.header.toInspect(),
202
+ blocksBuilt: blocksInCheckpoint.length,
203
+ },
204
+ );
205
+ this.metrics.recordCheckpointSuccess();
206
+ return checkpoint;
207
+ }
208
+
209
+ // TODO(palla/mbps): Wire this to the new p2p API once available, including the pendingBroadcast.block
210
+ const proposal = await this.validatorClient.createCheckpointProposal(
211
+ checkpoint.header,
212
+ checkpoint.archive.root,
213
+ pendingBroadcast?.txs ?? [],
214
+ this.proposer,
215
+ blockProposalOptions,
216
+ );
217
+ await this.p2pClient.broadcastProposal(proposal);
218
+
219
+ this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.slot);
220
+ const attestations = await this.waitForAttestations(proposal);
221
+
222
+ // Proposer must sign over the attestations before pushing them to L1
223
+ const signer = this.proposer ?? this.publisher.getSenderAddress();
224
+ const attestationsSignature = await this.validatorClient.signAttestationsAndSigners(attestations, signer);
225
+
226
+ // Enqueue publishing the checkpoint to L1
227
+ this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.slot);
228
+ const aztecSlotDuration = this.l1Constants.slotDuration;
229
+ const slotStartBuildTimestamp = this.getSlotStartBuildTimestamp();
230
+ const txTimeoutAt = new Date((slotStartBuildTimestamp + aztecSlotDuration) * 1000);
231
+ await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
232
+ txTimeoutAt,
233
+ forcePendingBlockNumber: this.invalidateBlock?.forcePendingBlockNumber,
234
+ });
235
+
236
+ return checkpoint;
237
+ } catch (err) {
238
+ this.log.error(`Error building checkpoint at slot ${this.slot}`, err);
239
+ return undefined;
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Builds blocks for a checkpoint within the current slot.
245
+ */
246
+ private async buildBlocksForCheckpoint(
247
+ checkpointBuilder: CheckpointBuilder,
248
+ timestamp: bigint,
249
+ blockProposalOptions: BlockProposalOptions,
250
+ ): Promise<{
251
+ blocksInCheckpoint: L2BlockNew[];
252
+ pendingBroadcast: { block: L2BlockNew; txs: Tx[] } | undefined;
253
+ }> {
254
+ const blocksInCheckpoint: L2BlockNew[] = [];
255
+ const txHashesAlreadyIncluded = new Set<string>();
256
+ const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
257
+
258
+ // Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
259
+ let pendingBroadcast: { block: L2BlockNew; txs: Tx[] } | undefined = undefined;
260
+
261
+ while (true) {
262
+ const blocksBuilt = blocksInCheckpoint.length;
263
+ const indexWithinCheckpoint = blocksBuilt;
264
+ const blockNumber = BlockNumber(initialBlockNumber + blocksBuilt);
265
+
266
+ const secondsIntoSlot = this.getSecondsIntoSlot();
267
+ const timingInfo = this.timetable.canStartNextBlock(secondsIntoSlot);
268
+
269
+ if (!timingInfo.canStart) {
270
+ this.log.debug(`Not enough time left in slot to start another block`, {
271
+ slot: this.slot,
272
+ blocksBuilt,
273
+ secondsIntoSlot,
274
+ });
275
+ break;
276
+ }
277
+
278
+ const buildResult = await this.buildSingleBlock(checkpointBuilder, {
279
+ // Create all blocks with the same timestamp
280
+ blockTimestamp: timestamp,
281
+ // Create an empty block if we haven't already and this is the last one
282
+ forceCreate: timingInfo.isLastBlock && blocksBuilt === 0 && this.config.buildCheckpointIfEmpty,
283
+ // Build deadline is only set if we are enforcing the timetable
284
+ buildDeadline: timingInfo.deadline
285
+ ? new Date((this.getSlotStartBuildTimestamp() + timingInfo.deadline) * 1000)
286
+ : undefined,
287
+ blockNumber,
288
+ indexWithinCheckpoint,
289
+ txHashesAlreadyIncluded,
290
+ });
291
+
292
+ if (!buildResult && timingInfo.isLastBlock) {
293
+ // If no block was produced due to not enough txs and this was the last one, exit
294
+ break;
295
+ } else if (!buildResult) {
296
+ // But if there is still time for more blocks, wait until the next block time and try again
297
+ await this.waitUntilNextBlock(secondsIntoSlot);
298
+ continue;
299
+ } else if ('error' in buildResult) {
300
+ // If there was an error building the block, just exit the loop and give up the rest of the slot
301
+ if (!(buildResult.error instanceof SequencerInterruptedError)) {
302
+ this.log.warn(`Halting block building for slot ${this.slot}`, {
303
+ slot: this.slot,
304
+ blocksBuilt,
305
+ error: buildResult.error,
306
+ });
307
+ }
308
+ break;
309
+ }
310
+
311
+ const { block, usedTxs } = buildResult;
312
+ blocksInCheckpoint.push(block);
313
+
314
+ // Sync the proposed block to the archiver to make it available
315
+ // Note that the checkpoint builder uses its own fork so it should not need to wait for this syncing
316
+ // Eventually we should refactor the checkpoint builder to not need a separate long-lived fork
317
+ await this.syncProposedBlockToArchiver(block);
318
+
319
+ // If this is the last block, exit the loop now so we start collecting attestations
320
+ if (timingInfo.isLastBlock) {
321
+ this.log.verbose(`Completed final block ${blockNumber} for slot ${this.slot}`, {
322
+ slot: this.slot,
323
+ blockNumber,
324
+ blocksBuilt,
325
+ });
326
+ pendingBroadcast = { block, txs: usedTxs };
327
+ break;
328
+ }
329
+
330
+ // For non-last blocks, broadcast the block proposal (unless we're in fisherman mode)
331
+ // If the block is the last one, we'll broadcast it along with the checkpoint at the end of the loop
332
+ if (!this.config.fishermanMode) {
333
+ // TODO(palla/mbps): Wire this to the new p2p API once available
334
+ const proposal = await this.validatorClient.createBlockProposal(
335
+ block.header.globalVariables.blockNumber,
336
+ (await checkpointBuilder.getCheckpoint()).header,
337
+ block.archive.root,
338
+ usedTxs,
339
+ this.proposer,
340
+ blockProposalOptions,
341
+ );
342
+ await this.p2pClient.broadcastProposal(proposal);
343
+ }
344
+
345
+ // Wait until the next block's start time
346
+ await this.waitUntilNextBlock(secondsIntoSlot);
347
+ }
348
+
349
+ this.log.verbose(`Block building loop completed for slot ${this.slot}`, {
350
+ slot: this.slot,
351
+ blocksBuilt: blocksInCheckpoint.length,
352
+ });
353
+
354
+ return {
355
+ blocksInCheckpoint,
356
+ pendingBroadcast,
357
+ };
358
+ }
359
+
360
+ /** Sleeps until it is time to produce the next block in the slot */
361
+ private async waitUntilNextBlock(blockStartedAtSecondsIntoSlot: number) {
362
+ this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.slot);
363
+ const blockDurationSeconds = this.timetable.blockDuration!;
364
+ const nextBlockStart = blockStartedAtSecondsIntoSlot + blockDurationSeconds;
365
+ this.log.verbose(`Waiting until time for the next block at ${nextBlockStart}s into slot`, { slot: this.slot });
366
+ await this.waitUntilTimeInSlot(nextBlockStart);
367
+ }
368
+
369
+ /** Builds a single block. Called from the main block building loop. */
370
+ private async buildSingleBlock(
371
+ checkpointBuilder: CheckpointBuilder,
372
+ opts: {
373
+ forceCreate?: boolean;
374
+ blockTimestamp: bigint;
375
+ blockNumber: BlockNumber;
376
+ indexWithinCheckpoint: number;
377
+ buildDeadline: Date | undefined;
378
+ txHashesAlreadyIncluded: Set<string>;
379
+ },
380
+ ): Promise<{ block: L2BlockNew; usedTxs: Tx[] } | { error: Error } | undefined> {
381
+ const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded } =
382
+ opts;
383
+
384
+ this.log.verbose(
385
+ `Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.slot}`,
386
+ { ...checkpointBuilder.getConstantData(), ...opts },
387
+ );
388
+
389
+ try {
390
+ // Wait until we have enough txs to build the block
391
+ const minTxs = this.config.minTxsPerBlock;
392
+ const { availableTxs, canStartBuilding } = await this.waitForMinTxs(opts);
393
+ if (!canStartBuilding) {
394
+ this.log.warn(
395
+ `Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (got ${availableTxs} txs but needs ${minTxs})`,
396
+ { blockNumber, slot: this.slot, indexWithinCheckpoint },
397
+ );
398
+ this.eventEmitter.emit('block-tx-count-check-failed', { minTxs, availableTxs, slot: this.slot });
399
+ this.metrics.recordBlockProposalFailed('insufficient_txs');
400
+ return undefined;
401
+ }
402
+
403
+ // Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
404
+ // just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
405
+ const pendingTxs = filter(
406
+ this.p2pClient.iteratePendingTxs(),
407
+ tx => !txHashesAlreadyIncluded.has(tx.txHash.toString()),
408
+ );
409
+
410
+ this.log.debug(
411
+ `Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.slot} with ${availableTxs} available txs`,
412
+ { slot: this.slot, blockNumber, indexWithinCheckpoint },
413
+ );
414
+ this.setStateFn(SequencerState.CREATING_BLOCK, this.slot);
415
+ const blockBuilderOptions: PublicProcessorLimits = {
416
+ maxTransactions: this.config.maxTxsPerBlock,
417
+ maxBlockSize: this.config.maxBlockSizeInBytes,
418
+ maxBlockGas: new Gas(this.config.maxDABlockGas, this.config.maxL2BlockGas),
419
+ maxBlobFields: BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB,
420
+ deadline: buildDeadline,
421
+ };
422
+
423
+ // Actually build the block by executing txs
424
+ const workTimer = new Timer();
425
+ const { publicGas, block, publicProcessorDuration, numTxs, blockBuildingTimer, usedTxs, failedTxs } =
426
+ await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
427
+ const blockBuildDuration = workTimer.ms();
428
+
429
+ // If any txs failed during execution, drop them from the mempool so we don't pick them up again
430
+ await this.dropFailedTxsFromP2P(failedTxs);
431
+
432
+ // Check if we have created a block with enough txs. If there were invalid txs in the pool, or if execution took
433
+ // too long, then we may not get to minTxsPerBlock after executing public functions.
434
+ const minValidTxs = this.config.minValidTxsPerBlock ?? minTxs;
435
+ if (!forceCreate && numTxs < minValidTxs) {
436
+ this.log.warn(
437
+ `Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed (got ${numTxs} but required ${minValidTxs})`,
438
+ { slot: this.slot, blockNumber, numTxs, indexWithinCheckpoint },
439
+ );
440
+ this.eventEmitter.emit('block-tx-count-check-failed', {
441
+ minTxs: minValidTxs,
442
+ availableTxs: numTxs,
443
+ slot: this.slot,
444
+ });
445
+ this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
446
+ return undefined;
447
+ }
448
+
449
+ // Block creation succeeded, emit stats and metrics
450
+ const blockStats = {
451
+ eventName: 'l2-block-built',
452
+ duration: blockBuildDuration,
453
+ publicProcessDuration: publicProcessorDuration,
454
+ rollupCircuitsDuration: blockBuildingTimer.ms(),
455
+ ...block.getStats(),
456
+ } satisfies L2BlockBuiltStats;
457
+
458
+ const blockHash = await block.hash();
459
+ const txHashes = block.body.txEffects.map(tx => tx.txHash);
460
+ const manaPerSec = publicGas.l2Gas / (blockBuildDuration / 1000);
461
+
462
+ this.log.info(
463
+ `Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.slot} with ${numTxs} txs`,
464
+ { blockHash, txHashes, manaPerSec, ...blockStats },
465
+ );
466
+
467
+ this.eventEmitter.emit('block-proposed', { blockNumber: block.number, slot: this.slot });
468
+ this.metrics.recordBuiltBlock(blockBuildDuration, publicGas.l2Gas);
469
+
470
+ return { block, usedTxs };
471
+ } catch (err: any) {
472
+ this.eventEmitter.emit('block-build-failed', { reason: err.message, slot: this.slot });
473
+ this.log.error(`Error building block`, err, { blockNumber, slot: this.slot });
474
+ this.metrics.recordBlockProposalFailed(err.name || 'unknown_error');
475
+ this.metrics.recordFailedBlock();
476
+ return { error: err };
477
+ }
478
+ }
479
+
480
+ /** Waits until minTxs are available on the pool for building a block. */
481
+ private async waitForMinTxs(opts: {
482
+ forceCreate?: boolean;
483
+ blockNumber: BlockNumber;
484
+ indexWithinCheckpoint: number;
485
+ buildDeadline: Date | undefined;
486
+ }): Promise<{ canStartBuilding: boolean; availableTxs: number }> {
487
+ const minTxs = this.config.minTxsPerBlock;
488
+ const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
489
+
490
+ // Deadline is undefined if we are not enforcing the timetable, meaning we'll exit immediately when out of time
491
+ const startBuildingDeadline = buildDeadline
492
+ ? new Date(buildDeadline.getTime() - MIN_BLOCK_BUILD_TIME_MS)
493
+ : undefined;
494
+
495
+ let availableTxs = await this.p2pClient.getPendingTxCount();
496
+
497
+ while (!forceCreate && availableTxs < minTxs) {
498
+ // If we're past deadline, or we have no deadline, give up
499
+ const now = this.dateProvider.nowAsDate();
500
+ if (startBuildingDeadline === undefined || now >= startBuildingDeadline) {
501
+ return { canStartBuilding: false, availableTxs: availableTxs };
502
+ }
503
+
504
+ // Wait a bit before checking again
505
+ this.setStateFn(SequencerState.WAITING_FOR_TXS, this.slot);
506
+ this.log.verbose(
507
+ `Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (have ${availableTxs} but need ${minTxs})`,
508
+ { blockNumber, slot: this.slot, indexWithinCheckpoint },
509
+ );
510
+ await sleep(TXS_POLLING_MS);
511
+ availableTxs = await this.p2pClient.getPendingTxCount();
512
+ }
513
+
514
+ return { canStartBuilding: true, availableTxs };
515
+ }
516
+
517
+ /**
518
+ * Waits for enough attestations to be collected via p2p.
519
+ * This is run after all blocks for the checkpoint have been built.
520
+ */
521
+ private async waitForAttestations(proposal: BlockProposal): Promise<CommitteeAttestationsAndSigners> {
522
+ if (this.config.fishermanMode) {
523
+ this.log.debug('Skipping attestation collection in fisherman mode');
524
+ return CommitteeAttestationsAndSigners.empty();
525
+ }
526
+
527
+ const slotNumber = proposal.slotNumber;
528
+ const { committee, seed, epoch } = await this.epochCache.getCommittee(slotNumber);
529
+
530
+ if (!committee) {
531
+ throw new Error('No committee when collecting attestations');
532
+ } else if (committee.length === 0) {
533
+ this.log.verbose(`Attesting committee is empty`);
534
+ return CommitteeAttestationsAndSigners.empty();
535
+ } else {
536
+ this.log.debug(`Attesting committee length is ${committee.length}`, { committee });
537
+ }
538
+
539
+ const numberOfRequiredAttestations = Math.floor((committee.length * 2) / 3) + 1;
540
+
541
+ if (this.config.skipCollectingAttestations) {
542
+ this.log.warn('Skipping attestation collection as per config (attesting with own keys only)');
543
+ const attestations = await this.validatorClient?.collectOwnAttestations(proposal);
544
+ return new CommitteeAttestationsAndSigners(orderAttestations(attestations ?? [], committee));
545
+ }
546
+
547
+ const attestationTimeAllowed = this.config.enforceTimeTable
548
+ ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_CHECKPOINT)!
549
+ : this.l1Constants.slotDuration;
550
+ const attestationDeadline = new Date(this.dateProvider.now() + attestationTimeAllowed * 1000);
551
+
552
+ this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
553
+
554
+ const collectAttestationsTimer = new Timer();
555
+ let collectedAttestationsCount: number = 0;
556
+ try {
557
+ const attestations = await this.validatorClient.collectAttestations(
558
+ proposal,
559
+ numberOfRequiredAttestations,
560
+ attestationDeadline,
561
+ );
562
+
563
+ collectedAttestationsCount = attestations.length;
564
+
565
+ // Rollup contract requires that the signatures are provided in the order of the committee
566
+ const sorted = orderAttestations(attestations, committee);
567
+
568
+ // Manipulate the attestations if we've been configured to do so
569
+ if (this.config.injectFakeAttestation || this.config.shuffleAttestationOrdering) {
570
+ const checkpoint = proposal.payload.header;
571
+ return this.manipulateAttestations(checkpoint, epoch, seed, committee, sorted);
572
+ }
573
+
574
+ return new CommitteeAttestationsAndSigners(sorted);
575
+ } catch (err) {
576
+ if (err && err instanceof AttestationTimeoutError) {
577
+ collectedAttestationsCount = err.collectedCount;
578
+ }
579
+ throw err;
580
+ } finally {
581
+ this.metrics.recordCollectedAttestations(collectedAttestationsCount, collectAttestationsTimer.ms());
582
+ }
583
+ }
584
+
585
+ /** Breaks the attestations before publishing based on attack configs */
586
+ private manipulateAttestations(
587
+ checkpoint: CheckpointHeader,
588
+ epoch: EpochNumber,
589
+ seed: bigint,
590
+ committee: EthAddress[],
591
+ attestations: CommitteeAttestation[],
592
+ ) {
593
+ // Compute the proposer index in the committee, since we dont want to tweak it.
594
+ // Otherwise, the L1 rollup contract will reject the block outright.
595
+ const { slotNumber } = checkpoint;
596
+ const proposerIndex = Number(
597
+ this.epochCache.computeProposerIndex(slotNumber, epoch, seed, BigInt(committee.length)),
598
+ );
599
+
600
+ if (this.config.injectFakeAttestation) {
601
+ // Find non-empty attestations that are not from the proposer
602
+ const nonProposerIndices: number[] = [];
603
+ for (let i = 0; i < attestations.length; i++) {
604
+ if (!attestations[i].signature.isEmpty() && i !== proposerIndex) {
605
+ nonProposerIndices.push(i);
606
+ }
607
+ }
608
+ if (nonProposerIndices.length > 0) {
609
+ const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
610
+ this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
611
+ unfreeze(attestations[targetIndex]).signature = Signature.random();
612
+ }
613
+ return new CommitteeAttestationsAndSigners(attestations);
614
+ }
615
+
616
+ if (this.config.shuffleAttestationOrdering) {
617
+ this.log.warn(`Shuffling attestation ordering in checkpoint for slot ${slotNumber} (proposer #${proposerIndex})`);
618
+
619
+ const shuffled = [...attestations];
620
+ const [i, j] = [(proposerIndex + 1) % shuffled.length, (proposerIndex + 2) % shuffled.length];
621
+ const valueI = shuffled[i];
622
+ const valueJ = shuffled[j];
623
+ shuffled[i] = valueJ;
624
+ shuffled[j] = valueI;
625
+
626
+ const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
627
+ return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
628
+ }
629
+
630
+ return new CommitteeAttestationsAndSigners(attestations);
631
+ }
632
+
633
+ private async dropFailedTxsFromP2P(failedTxs: FailedTx[]) {
634
+ if (failedTxs.length === 0) {
635
+ return;
636
+ }
637
+ const failedTxData = failedTxs.map(fail => fail.tx);
638
+ const failedTxHashes = failedTxData.map(tx => tx.getTxHash());
639
+ this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
640
+ await this.p2pClient.deleteTxs(failedTxHashes);
641
+ }
642
+
643
+ /**
644
+ * Placeholder for pushing block to archiver and waiting for sync.
645
+ * To be implemented when archiver and world-state support proposed blocks.
646
+ */
647
+ private async syncProposedBlockToArchiver(block: L2BlockNew): Promise<void> {
648
+ this.log.debug(`Syncing proposed block ${block.number}`, {
649
+ blockNumber: block.number,
650
+ slot: block.header.globalVariables.slotNumber,
651
+ });
652
+ // TODO(palla/mbps): Implement actual sync to archiver and world-state
653
+ await Promise.resolve();
654
+ }
655
+
656
+ /** Runs fee analysis and logs checkpoint outcome as fisherman */
657
+ private async handleCheckpointEndAsFisherman(checkpoint: Checkpoint | undefined) {
658
+ // Perform L1 fee analysis before clearing requests
659
+ // The callback is invoked asynchronously after the next block is mined
660
+ const feeAnalysis = await this.publisher.analyzeL1Fees(this.slot, analysis =>
661
+ this.metrics.recordFishermanFeeAnalysis(analysis),
662
+ );
663
+
664
+ if (checkpoint) {
665
+ this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.slot}`, {
666
+ ...checkpoint.toCheckpointInfo(),
667
+ ...checkpoint.getStats(),
668
+ feeAnalysisId: feeAnalysis?.id,
669
+ });
670
+ this.metrics.recordBlockProposalSuccess();
671
+ } else {
672
+ this.log.warn(`Validation block building FAILED for slot ${this.slot}`, {
673
+ slot: this.slot,
674
+ feeAnalysisId: feeAnalysis?.id,
675
+ });
676
+ this.metrics.recordBlockProposalFailed('block_build_failed');
677
+ }
678
+
679
+ this.publisher.clearPendingRequests();
680
+ }
681
+
682
+ /** Waits until a specific time within the current slot */
683
+ private async waitUntilTimeInSlot(targetSecondsIntoSlot: number): Promise<void> {
684
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp();
685
+ const targetTimestamp = slotStartTimestamp + targetSecondsIntoSlot;
686
+ await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
687
+ }
688
+
689
+ private getSlotStartBuildTimestamp(): number {
690
+ return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
691
+ }
692
+
693
+ private getSecondsIntoSlot(): number {
694
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp();
695
+ return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
696
+ }
697
+
698
+ public getPublisher() {
699
+ return this.publisher;
700
+ }
701
+ }