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