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