@aztec/sequencer-client 3.0.0-rc.5 → 4.0.0-nightly.20260107

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