@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
@@ -5,31 +5,23 @@ function _ts_decorate(decorators, target, key, desc) {
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  }
7
7
  import { getKzg } from '@aztec/blob-lib';
8
- import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
8
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
9
9
  import { NoCommitteeError } from '@aztec/ethereum/contracts';
10
- import { FormattedViemError } from '@aztec/ethereum/utils';
11
10
  import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
12
- import { omit, pick } from '@aztec/foundation/collection';
13
- import { randomInt } from '@aztec/foundation/crypto/random';
11
+ import { merge, omit, pick } from '@aztec/foundation/collection';
14
12
  import { Fr } from '@aztec/foundation/curves/bn254';
15
13
  import { EthAddress } from '@aztec/foundation/eth-address';
16
- import { Signature } from '@aztec/foundation/eth-signature';
17
14
  import { createLogger } from '@aztec/foundation/log';
18
15
  import { RunningPromise } from '@aztec/foundation/running-promise';
19
- import { Timer } from '@aztec/foundation/timer';
20
- import { unfreeze } from '@aztec/foundation/types';
21
- import { CommitteeAttestationsAndSigners, MaliciousCommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
22
16
  import { getSlotAtTimestamp, getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
23
- import { Gas } from '@aztec/stdlib/gas';
24
17
  import { SequencerConfigSchema } from '@aztec/stdlib/interfaces/server';
25
- import { orderAttestations } from '@aztec/stdlib/p2p';
26
- import { CheckpointHeader } from '@aztec/stdlib/rollup';
27
18
  import { pickFromSchema } from '@aztec/stdlib/schemas';
28
19
  import { MerkleTreeId } from '@aztec/stdlib/trees';
29
- import { ContentCommitment } from '@aztec/stdlib/tx';
30
- import { AttestationTimeoutError } from '@aztec/stdlib/validators';
31
- import { Attributes, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
20
+ import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
32
21
  import EventEmitter from 'node:events';
22
+ import { DefaultSequencerConfig } from '../config.js';
23
+ import { CheckpointProposalJob } from './checkpoint_proposal_job.js';
24
+ import { CheckpointVoter } from './checkpoint_voter.js';
33
25
  import { SequencerInterruptedError, SequencerTooSlowError } from './errors.js';
34
26
  import { SequencerMetrics } from './metrics.js';
35
27
  import { SequencerTimetable } from './timetable.js';
@@ -37,12 +29,11 @@ import { SequencerState } from './utils.js';
37
29
  export { SequencerState };
38
30
  /**
39
31
  * Sequencer client
40
- * - Wins a period of time to become the sequencer (depending on finalized protocol).
41
- * - Chooses a set of txs from the tx pool to be in the rollup.
42
- * - Simulate the rollup of txs.
43
- * - Adds proof requests to the request pool (not for this milestone).
44
- * - Receives results to those proofs from the network (repeats as necessary) (not for this milestone).
45
- * - Publishes L1 tx(s) to the rollup contract via RollupPublisher.
32
+ * - Checks whether it is elected as proposer for the next slot
33
+ * - Builds multiple blocks and broadcasts them
34
+ * - Collects attestations for the checkpoint
35
+ * - Publishes the checkpoint to L1
36
+ * - Votes for proposals and slashes on L1
46
37
  */ export class Sequencer extends EventEmitter {
47
38
  publisherFactory;
48
39
  validatorClient;
@@ -52,119 +43,64 @@ export { SequencerState };
52
43
  slasherClient;
53
44
  l2BlockSource;
54
45
  l1ToL2MessageSource;
55
- blockBuilder;
46
+ checkpointsBuilder;
56
47
  l1Constants;
57
48
  dateProvider;
58
49
  epochCache;
59
50
  rollupContract;
60
- config;
61
51
  telemetry;
62
52
  log;
63
53
  runningPromise;
64
- pollingIntervalMs;
65
- maxTxsPerBlock;
66
- minTxsPerBlock;
67
- maxL1TxInclusionTimeIntoSlot;
68
54
  state;
69
- maxBlockSizeInBytes;
70
- maxBlockGas;
71
55
  metrics;
72
- lastBlockPublished;
73
- governanceProposerPayload;
74
56
  /** The last slot for which we attempted to vote when sync failed, to prevent duplicate attempts. */ lastSlotForVoteWhenSyncFailed;
75
- /** The last slot for which we built a validation block in fisherman mode, to prevent duplicate attempts. */ lastSlotForValidationBlock;
57
+ /** The last slot for which we triggered a checkpoint proposal job, to prevent duplicate attempts. */ lastSlotForCheckpointProposalJob;
58
+ /** Last successful checkpoint proposed */ lastCheckpointProposed;
76
59
  /** The last epoch for which we logged strategy comparison in fisherman mode. */ lastEpochForStrategyComparison;
77
60
  /** The maximum number of seconds that the sequencer can be into a slot to transition to a particular state. */ timetable;
78
- enforceTimeTable;
79
61
  // This shouldn't be here as this gets re-created each time we build/propose a block.
80
62
  // But we have a number of tests that abuse/rely on this class having a permanent publisher.
81
63
  // As long as those tests only configure a single publisher they will continue to work.
82
64
  // This will get re-assigned every time the sequencer goes to build a new block to a publisher that is valid
83
65
  // for the block proposer.
66
+ // TODO(palla/mbps): Remove this field and fix tests
84
67
  publisher;
85
- constructor(publisherFactory, validatorClient, globalsBuilder, p2pClient, worldState, slasherClient, l2BlockSource, l1ToL2MessageSource, blockBuilder, l1Constants, dateProvider, epochCache, rollupContract, config, telemetry = getTelemetryClient(), log = createLogger('sequencer')){
86
- super(), this.publisherFactory = publisherFactory, this.validatorClient = validatorClient, this.globalsBuilder = globalsBuilder, this.p2pClient = p2pClient, this.worldState = worldState, this.slasherClient = slasherClient, this.l2BlockSource = l2BlockSource, this.l1ToL2MessageSource = l1ToL2MessageSource, this.blockBuilder = blockBuilder, this.l1Constants = l1Constants, this.dateProvider = dateProvider, this.epochCache = epochCache, this.rollupContract = rollupContract, this.config = config, this.telemetry = telemetry, this.log = log, this.pollingIntervalMs = 1000, this.maxTxsPerBlock = 32, this.minTxsPerBlock = 1, this.maxL1TxInclusionTimeIntoSlot = 0, this.state = SequencerState.STOPPED, this.maxBlockSizeInBytes = 1024 * 1024, this.maxBlockGas = new Gas(100e9, 100e9), this.enforceTimeTable = false;
68
+ /** Config for the sequencer */ config;
69
+ constructor(publisherFactory, validatorClient, globalsBuilder, p2pClient, worldState, slasherClient, l2BlockSource, l1ToL2MessageSource, checkpointsBuilder, l1Constants, dateProvider, epochCache, rollupContract, config, telemetry = getTelemetryClient(), log = createLogger('sequencer')){
70
+ super(), this.publisherFactory = publisherFactory, this.validatorClient = validatorClient, this.globalsBuilder = globalsBuilder, this.p2pClient = p2pClient, this.worldState = worldState, this.slasherClient = slasherClient, this.l2BlockSource = l2BlockSource, this.l1ToL2MessageSource = l1ToL2MessageSource, this.checkpointsBuilder = checkpointsBuilder, this.l1Constants = l1Constants, this.dateProvider = dateProvider, this.epochCache = epochCache, this.rollupContract = rollupContract, this.telemetry = telemetry, this.log = log, this.state = SequencerState.STOPPED, this.config = DefaultSequencerConfig;
87
71
  // Add [FISHERMAN] prefix to logger if in fisherman mode
88
- if (this.config.fishermanMode) {
72
+ if (config.fishermanMode) {
89
73
  this.log = log.createChild('[FISHERMAN]');
90
74
  }
91
75
  this.metrics = new SequencerMetrics(telemetry, this.rollupContract, 'Sequencer');
92
- // Initialize config
93
- this.updateConfig(this.config);
94
- }
95
- get tracer() {
96
- return this.metrics.tracer;
97
- }
98
- getValidatorAddresses() {
99
- return this.validatorClient?.getValidatorAddresses();
100
- }
101
- getConfig() {
102
- return this.config;
103
- }
104
- /**
105
- * Updates sequencer config by the defined values in the config on input.
106
- * @param config - New parameters.
107
- */ updateConfig(config) {
108
- this.log.info(`Sequencer config set`, omit(pickFromSchema(config, SequencerConfigSchema), 'txPublicSetupAllowList'));
109
- if (config.transactionPollingIntervalMS !== undefined) {
110
- this.pollingIntervalMs = config.transactionPollingIntervalMS;
111
- }
112
- if (config.maxTxsPerBlock !== undefined) {
113
- this.maxTxsPerBlock = config.maxTxsPerBlock;
114
- }
115
- if (config.minTxsPerBlock !== undefined) {
116
- this.minTxsPerBlock = config.minTxsPerBlock;
117
- }
118
- if (config.maxDABlockGas !== undefined) {
119
- this.maxBlockGas = new Gas(config.maxDABlockGas, this.maxBlockGas.l2Gas);
120
- }
121
- if (config.maxL2BlockGas !== undefined) {
122
- this.maxBlockGas = new Gas(this.maxBlockGas.daGas, config.maxL2BlockGas);
123
- }
124
- if (config.maxBlockSizeInBytes !== undefined) {
125
- this.maxBlockSizeInBytes = config.maxBlockSizeInBytes;
126
- }
127
- if (config.governanceProposerPayload) {
128
- this.governanceProposerPayload = config.governanceProposerPayload;
129
- }
130
- if (config.maxL1TxInclusionTimeIntoSlot !== undefined) {
131
- this.maxL1TxInclusionTimeIntoSlot = config.maxL1TxInclusionTimeIntoSlot;
132
- }
133
- if (config.enforceTimeTable !== undefined) {
134
- this.enforceTimeTable = config.enforceTimeTable;
135
- }
136
- this.setTimeTable();
137
- // TODO: Just read everything from the config object as needed instead of copying everything into local vars.
138
- // Update all values on this.config that are populated in the config object.
139
- Object.assign(this.config, config);
76
+ this.updateConfig(config);
140
77
  }
141
- setTimeTable() {
78
+ /** Updates sequencer config by the defined values and updates the timetable */ updateConfig(config) {
79
+ const filteredConfig = pickFromSchema(config, SequencerConfigSchema);
80
+ this.log.info(`Updated sequencer config`, omit(filteredConfig, 'txPublicSetupAllowList'));
81
+ this.config = merge(this.config, filteredConfig);
142
82
  this.timetable = new SequencerTimetable({
143
83
  ethereumSlotDuration: this.l1Constants.ethereumSlotDuration,
144
84
  aztecSlotDuration: this.aztecSlotDuration,
145
- maxL1TxInclusionTimeIntoSlot: this.maxL1TxInclusionTimeIntoSlot,
146
- attestationPropagationTime: this.config.attestationPropagationTime,
147
- enforce: this.enforceTimeTable
85
+ l1PublishingTime: this.l1PublishingTime,
86
+ p2pPropagationTime: this.config.attestationPropagationTime,
87
+ blockDurationMs: this.config.blockDurationMs,
88
+ enforce: this.config.enforceTimeTable
148
89
  }, this.metrics, this.log);
149
90
  }
150
- async init() {
151
- // Takes ~3s to precompute some tables.
91
+ /** Initializes the sequencer (precomputes tables and creates a publisher). Takes about 3s. */ async init() {
152
92
  getKzg();
153
93
  this.publisher = (await this.publisherFactory.create(undefined)).publisher;
154
94
  }
155
- /**
156
- * Starts the sequencer and moves to IDLE state.
157
- */ start() {
158
- this.runningPromise = new RunningPromise(this.safeWork.bind(this), this.log, this.pollingIntervalMs);
95
+ /** Starts the sequencer and moves to IDLE state. */ start() {
96
+ this.runningPromise = new RunningPromise(this.safeWork.bind(this), this.log, this.config.sequencerPollingIntervalMS);
159
97
  this.setState(SequencerState.IDLE, undefined, {
160
98
  force: true
161
99
  });
162
100
  this.runningPromise.start();
163
101
  this.log.info('Started sequencer');
164
102
  }
165
- /**
166
- * Stops the sequencer from processing txs and moves to STOPPED state.
167
- */ async stop() {
103
+ /** Stops the sequencer from building blocks and moves to STOPPED state. */ async stop() {
168
104
  this.log.info(`Stopping sequencer`);
169
105
  this.setState(SequencerState.STOPPING, undefined, {
170
106
  force: true
@@ -176,29 +112,80 @@ export { SequencerState };
176
112
  });
177
113
  this.log.info('Stopped sequencer');
178
114
  }
179
- /**
180
- * Returns the current state of the sequencer.
181
- * @returns An object with a state entry with one of SequencerState.
182
- */ status() {
115
+ async safeWork() {
116
+ try {
117
+ await this.work();
118
+ } catch (err) {
119
+ this.emit('checkpoint-error', {
120
+ error: err
121
+ });
122
+ if (err instanceof SequencerTooSlowError) {
123
+ // TODO(palla/mbps): Add missing states
124
+ // Log as warn only if we had to abort halfway through the block proposal
125
+ const logLvl = [
126
+ SequencerState.INITIALIZING_CHECKPOINT,
127
+ SequencerState.PROPOSER_CHECK
128
+ ].includes(err.proposedState) ? 'debug' : 'warn';
129
+ this.log[logLvl](err.message, {
130
+ now: this.dateProvider.nowInSeconds()
131
+ });
132
+ } else {
133
+ // Re-throw other errors
134
+ throw err;
135
+ }
136
+ } finally{
137
+ this.setState(SequencerState.IDLE, undefined);
138
+ }
139
+ }
140
+ /** Returns the current state of the sequencer. */ status() {
183
141
  return {
184
142
  state: this.state
185
143
  };
186
144
  }
187
145
  /**
188
- * @notice Performs most of the sequencer duties:
189
- * - Checks if we are up to date
190
- * - If we are and we are the sequencer, collect txs and build a block
191
- * - Collect attestations for the block
192
- * - Submit block
193
- * - If our block for some reason is not included, revert the state
146
+ * Main sequencer loop:
147
+ * - Checks if we are up to date
148
+ * - If we are and we are the sequencer, collect txs and build blocks
149
+ * - Build multiple blocks per slot when configured
150
+ * - Collect attestations for the final block
151
+ * - Submit checkpoint
194
152
  */ async work() {
195
153
  this.setState(SequencerState.SYNCHRONIZING, undefined);
196
- const { slot, ts, now } = this.epochCache.getEpochAndSlotInNextL1Slot();
197
- // Check we have not already published a block for this slot (cheapest check)
198
- if (this.lastBlockPublished && this.lastBlockPublished.header.getSlot() >= slot) {
199
- this.log.debug(`Cannot propose block at next L2 slot ${slot} since that slot was taken by our own block ${this.lastBlockPublished.number}`);
154
+ const { slot, ts, now, epoch } = this.epochCache.getEpochAndSlotInNextL1Slot();
155
+ // Check if we are synced and it's our slot, grab a publisher, check previous block invalidation, etc
156
+ const checkpointProposalJob = await this.prepareCheckpointProposal(slot, ts, now);
157
+ if (!checkpointProposalJob) {
200
158
  return;
201
159
  }
160
+ // Execute the checkpoint proposal job
161
+ const checkpoint = await checkpointProposalJob.execute();
162
+ // Update last checkpoint proposed (currently unused)
163
+ if (checkpoint) {
164
+ this.lastCheckpointProposed = checkpoint;
165
+ }
166
+ // Log fee strategy comparison if on fisherman
167
+ if (this.config.fishermanMode && (this.lastEpochForStrategyComparison === undefined || epoch > this.lastEpochForStrategyComparison)) {
168
+ this.logStrategyComparison(epoch, checkpointProposalJob.getPublisher());
169
+ this.lastEpochForStrategyComparison = epoch;
170
+ }
171
+ }
172
+ /**
173
+ * Prepares the checkpoint proposal by performing all necessary checks and setup.
174
+ * This is the initial step in the main loop.
175
+ * @returns CheckpointProposalJob if successful, undefined if we are not yet synced or are not the proposer.
176
+ */ async prepareCheckpointProposal(slot, ts, now) {
177
+ // Check we have not already processed this slot (cheapest check)
178
+ // We only check this if enforce timetable is set, since we want to keep processing the same slot if we are not
179
+ // running against actual time (eg when we use sandbox-style automining)
180
+ if (this.lastSlotForCheckpointProposalJob && this.lastSlotForCheckpointProposalJob >= slot && this.config.enforceTimeTable) {
181
+ this.log.trace(`Slot ${slot} has already been processed`);
182
+ return undefined;
183
+ }
184
+ // But if we have already proposed for this slot, the we definitely have to skip it, automining or not
185
+ if (this.lastCheckpointProposed && this.lastCheckpointProposed.header.slotNumber >= slot) {
186
+ this.log.trace(`Slot ${slot} has already been published as checkpoint ${this.lastCheckpointProposed.number}`);
187
+ return undefined;
188
+ }
202
189
  // Check all components are synced to latest as seen by the archiver (queries all subsystems)
203
190
  const syncedTo = await this.checkSync({
204
191
  ts,
@@ -209,18 +196,17 @@ export { SequencerState };
209
196
  slot,
210
197
  ts
211
198
  });
212
- return;
199
+ return undefined;
213
200
  }
214
- const chainTipArchive = syncedTo.archive;
215
- const newBlockNumber = BlockNumber(syncedTo.blockNumber + 1);
216
- const syncLogData = {
201
+ // TODO(palla/mbps): Compute proper checkpoint number
202
+ const checkpointNumber = CheckpointNumber.fromBlockNumber(BlockNumber(syncedTo.blockNumber + 1));
203
+ const logCtx = {
217
204
  now,
218
205
  syncedToL1Ts: syncedTo.l1Timestamp,
219
206
  syncedToL2Slot: getSlotAtTimestamp(syncedTo.l1Timestamp, this.l1Constants),
220
- nextL2Slot: slot,
221
- nextL2SlotTs: ts,
222
- l1SlotDuration: this.l1Constants.ethereumSlotDuration,
223
- newBlockNumber,
207
+ slot,
208
+ slotTs: ts,
209
+ checkpointNumber,
224
210
  isPendingChainValid: pick(syncedTo.pendingChainValidationStatus, 'valid', 'reason', 'invalidIndex')
225
211
  };
226
212
  // Check that we are a proposer for the next slot
@@ -229,229 +215,88 @@ export { SequencerState };
229
215
  // If we are not a proposer check if we should invalidate a invalid block, and bail
230
216
  if (!canPropose) {
231
217
  await this.considerInvalidatingBlock(syncedTo, slot);
232
- return;
233
- }
234
- // In fisherman mode, check if we've already validated this slot to prevent duplicate attempts
235
- if (this.config.fishermanMode) {
236
- if (this.lastSlotForValidationBlock === slot) {
237
- this.log.trace(`Already validated block building for slot ${slot} (skipping)`, {
238
- slot
239
- });
240
- return;
241
- }
242
- this.log.debug(`Building validation block for slot ${slot} (actual proposer: ${proposer?.toString() ?? 'none'})`, {
243
- slot,
244
- proposer: proposer?.toString()
245
- });
246
- // Mark this slot as being validated
247
- this.lastSlotForValidationBlock = slot;
218
+ return undefined;
248
219
  }
249
220
  // Check that the slot is not taken by a block already (should never happen, since only us can propose for this slot)
250
221
  if (syncedTo.block && syncedTo.block.header.getSlot() >= slot) {
251
222
  this.log.warn(`Cannot propose block at next L2 slot ${slot} since that slot was taken by block ${syncedTo.blockNumber}`, {
252
- ...syncLogData,
223
+ ...logCtx,
253
224
  block: syncedTo.block.header.toInspect()
254
225
  });
255
226
  this.metrics.recordBlockProposalPrecheckFailed('slot_already_taken');
256
- return;
227
+ return undefined;
257
228
  }
258
229
  // We now need to get ourselves a publisher.
259
230
  // The returned attestor will be the one we provided if we provided one.
260
231
  // Otherwise it will be a valid attestor for the returned publisher.
261
232
  // In fisherman mode, pass undefined to use the fisherman's own keystore instead of the actual proposer's
262
- const { attestorAddress, publisher } = await this.publisherFactory.create(this.config.fishermanMode ? undefined : proposer);
233
+ const proposerForPublisher = this.config.fishermanMode ? undefined : proposer;
234
+ const { attestorAddress, publisher } = await this.publisherFactory.create(proposerForPublisher);
263
235
  this.log.verbose(`Created publisher at address ${publisher.getSenderAddress()} for attestor ${attestorAddress}`);
264
236
  this.publisher = publisher;
265
237
  // In fisherman mode, set the actual proposer's address for simulations
266
- if (this.config.fishermanMode) {
267
- if (proposer) {
268
- publisher.setProposerAddressForSimulation(proposer);
269
- this.log.debug(`Set proposer address ${proposer} for simulation in fisherman mode`);
270
- }
238
+ if (this.config.fishermanMode && proposer) {
239
+ publisher.setProposerAddressForSimulation(proposer);
240
+ this.log.debug(`Set proposer address ${proposer} for simulation in fisherman mode`);
271
241
  }
272
- // Get proposer credentials
273
- const coinbase = this.validatorClient.getCoinbaseForAttestor(attestorAddress);
274
- const feeRecipient = this.validatorClient.getFeeRecipientForAttestor(attestorAddress);
275
242
  // Prepare invalidation request if the pending chain is invalid (returns undefined if no need)
243
+ // TODO(palla/mbps): We need to invalidate checkpoints, not blocks
276
244
  const invalidateBlock = await publisher.simulateInvalidateBlock(syncedTo.pendingChainValidationStatus);
277
- // Check with the rollup if we can indeed propose at the next L2 slot. This check should not fail
245
+ // Check with the rollup contract if we can indeed propose at the next L2 slot. This check should not fail
278
246
  // if all the previous checks are good, but we do it just in case.
279
- const canProposeCheck = await publisher.canProposeAtNextEthBlock(chainTipArchive, proposer ?? EthAddress.ZERO, invalidateBlock);
247
+ const canProposeCheck = await publisher.canProposeAtNextEthBlock(syncedTo.archive, proposer ?? EthAddress.ZERO, invalidateBlock);
280
248
  if (canProposeCheck === undefined) {
281
- this.log.warn(`Cannot propose block ${newBlockNumber} at slot ${slot} due to failed rollup contract check`, syncLogData);
249
+ this.log.warn(`Cannot propose checkpoint ${checkpointNumber} at slot ${slot} due to failed rollup contract check`, logCtx);
282
250
  this.emit('proposer-rollup-check-failed', {
283
- reason: 'Rollup contract check failed'
251
+ reason: 'Rollup contract check failed',
252
+ slot
284
253
  });
285
254
  this.metrics.recordBlockProposalPrecheckFailed('rollup_contract_check_failed');
286
- return;
287
- } else if (canProposeCheck.slot !== slot) {
255
+ return undefined;
256
+ }
257
+ if (canProposeCheck.slot !== slot) {
288
258
  this.log.warn(`Cannot propose block due to slot mismatch with rollup contract (this can be caused by a clock out of sync). Expected slot ${slot} but got ${canProposeCheck.slot}.`, {
289
- ...syncLogData,
259
+ ...logCtx,
290
260
  rollup: canProposeCheck,
291
- newBlockNumber,
292
261
  expectedSlot: slot
293
262
  });
294
263
  this.emit('proposer-rollup-check-failed', {
295
- reason: 'Slot mismatch'
264
+ reason: 'Slot mismatch',
265
+ slot
296
266
  });
297
267
  this.metrics.recordBlockProposalPrecheckFailed('slot_mismatch');
298
- return;
299
- } else if (canProposeCheck.checkpointNumber !== CheckpointNumber.fromBlockNumber(newBlockNumber)) {
300
- this.log.warn(`Cannot propose block due to block mismatch with rollup contract (this can be caused by a pending archiver sync). Expected block ${newBlockNumber} but got ${canProposeCheck.checkpointNumber}.`, {
301
- ...syncLogData,
268
+ return undefined;
269
+ }
270
+ if (canProposeCheck.checkpointNumber !== checkpointNumber) {
271
+ this.log.warn(`Cannot propose due to block mismatch with rollup contract (this can be caused by a pending archiver sync). Expected checkpoint ${checkpointNumber} but got ${canProposeCheck.checkpointNumber}.`, {
272
+ ...logCtx,
302
273
  rollup: canProposeCheck,
303
- newBlockNumber,
304
274
  expectedSlot: slot
305
275
  });
306
276
  this.emit('proposer-rollup-check-failed', {
307
- reason: 'Block mismatch'
277
+ reason: 'Block mismatch',
278
+ slot
308
279
  });
309
280
  this.metrics.recordBlockProposalPrecheckFailed('block_number_mismatch');
310
- return;
311
- }
312
- this.log.debug(`Can propose block ${newBlockNumber} at slot ${slot} as ${proposer}`, {
313
- ...syncLogData
314
- });
315
- const newGlobalVariables = await this.globalsBuilder.buildGlobalVariables(newBlockNumber, coinbase, feeRecipient, slot);
316
- // Enqueue governance and slashing votes (returns promises that will be awaited later)
317
- // In fisherman mode, we simulate slashing but don't actually publish to L1
318
- const votesPromises = this.enqueueGovernanceAndSlashingVotes(publisher, attestorAddress, slot, newGlobalVariables.timestamp);
319
- // Enqueues block invalidation
320
- if (invalidateBlock && !this.config.skipInvalidateBlockAsProposer) {
321
- publisher.enqueueInvalidateBlock(invalidateBlock);
322
- }
323
- // Actual block building
324
- this.setState(SequencerState.INITIALIZING_PROPOSAL, slot);
325
- this.metrics.incOpenSlot(slot, proposer?.toString() ?? 'unknown');
326
- const block = await this.tryBuildBlockAndEnqueuePublish(slot, proposer, newBlockNumber, publisher, newGlobalVariables, chainTipArchive, invalidateBlock);
327
- // Wait until the voting promises have resolved, so all requests are enqueued
328
- await Promise.all(votesPromises);
329
- // In fisherman mode, we don't publish to L1 but analyze the fees
330
- if (this.config.fishermanMode) {
331
- // Perform L1 fee analysis before clearing requests
332
- // The callback is invoked asynchronously after the next block is mined
333
- const feeAnalysis = await publisher.analyzeL1Fees(BigInt(slot), (analysis)=>{
334
- this.metrics.recordFishermanFeeAnalysis(analysis);
335
- });
336
- // Check if we've moved to a new epoch and log strategy comparison
337
- const currentEpoch = this.epochCache.getEpochAndSlotNow().epoch;
338
- if (this.lastEpochForStrategyComparison === undefined || currentEpoch > this.lastEpochForStrategyComparison) {
339
- this.logStrategyComparison(currentEpoch, publisher);
340
- this.lastEpochForStrategyComparison = currentEpoch;
341
- }
342
- // Clear pending requests (we're not sending them)
343
- publisher.clearPendingRequests();
344
- if (block) {
345
- this.log.info(`Validation block building SUCCEEDED for slot ${slot}`, {
346
- blockNumber: newBlockNumber,
347
- slot: Number(slot),
348
- archive: block.archive.toString(),
349
- txCount: block.body.txEffects.length,
350
- feeAnalysisId: feeAnalysis?.id
351
- });
352
- this.lastBlockPublished = block;
353
- this.metrics.recordBlockProposalSuccess();
354
- } else {
355
- // Block building failed in fisherman mode
356
- this.log.warn(`Validation block building FAILED for slot ${slot}`, {
357
- blockNumber: newBlockNumber,
358
- slot: Number(slot),
359
- feeAnalysisId: feeAnalysis?.id
360
- });
361
- this.metrics.recordBlockProposalFailed('block_build_failed');
362
- }
363
- } else {
364
- // Normal mode: send the tx to L1
365
- const l1Response = await publisher.sendRequests();
366
- const proposedBlock = l1Response?.successfulActions.find((a)=>a === 'propose');
367
- if (proposedBlock) {
368
- this.lastBlockPublished = block;
369
- this.emit('block-published', {
370
- blockNumber: newBlockNumber,
371
- slot: Number(slot)
372
- });
373
- await this.metrics.incFilledSlot(publisher.getSenderAddress().toString(), coinbase);
374
- } else if (block) {
375
- this.emit('block-publish-failed', l1Response ?? {});
376
- }
281
+ return undefined;
377
282
  }
378
- this.setState(SequencerState.IDLE, undefined);
379
- }
380
- /** Tries building a block proposal, and if successful, enqueues it for publishing. */ async tryBuildBlockAndEnqueuePublish(slot, proposer, newBlockNumber, publisher, newGlobalVariables, chainTipArchive, invalidateBlock) {
381
- this.log.verbose(`Preparing proposal for block ${newBlockNumber} at slot ${slot}`, {
382
- proposer,
383
- publisher: publisher.getSenderAddress(),
384
- globalVariables: newGlobalVariables.toInspect(),
385
- chainTipArchive,
386
- blockNumber: newBlockNumber,
387
- slot
388
- });
389
- const proposalHeader = CheckpointHeader.from({
390
- ...newGlobalVariables,
391
- timestamp: newGlobalVariables.timestamp,
392
- lastArchiveRoot: chainTipArchive,
393
- blockHeadersHash: Fr.ZERO,
394
- contentCommitment: ContentCommitment.empty(),
395
- totalManaUsed: Fr.ZERO
283
+ this.lastSlotForCheckpointProposalJob = slot;
284
+ this.log.info(`Preparing checkpoint proposal ${checkpointNumber} at slot ${slot}`, {
285
+ ...logCtx,
286
+ proposer
396
287
  });
397
- let block;
398
- const pendingTxCount = await this.p2pClient.getPendingTxCount();
399
- if (pendingTxCount >= this.minTxsPerBlock) {
400
- // We don't fetch exactly maxTxsPerBlock txs here because we may not need all of them if we hit a limit before,
401
- // and also we may need to fetch more if we don't have enough valid txs.
402
- const pendingTxs = this.p2pClient.iteratePendingTxs();
403
- try {
404
- block = await this.buildBlockAndEnqueuePublish(pendingTxs, proposalHeader, newGlobalVariables, proposer, invalidateBlock, publisher);
405
- } catch (err) {
406
- this.emit('block-build-failed', {
407
- reason: err.message
408
- });
409
- if (err instanceof FormattedViemError) {
410
- this.log.verbose(`Unable to build/enqueue block ${err.message}`);
411
- } else {
412
- this.log.error(`Error building/enqueuing block`, err, {
413
- blockNumber: newBlockNumber,
414
- slot
415
- });
416
- }
417
- this.metrics.recordBlockProposalFailed(err.name || 'unknown_error');
418
- }
419
- } else {
420
- this.log.verbose(`Not enough txs to build block ${newBlockNumber} at slot ${slot} (got ${pendingTxCount} txs, need ${this.minTxsPerBlock})`, {
421
- chainTipArchive,
422
- blockNumber: newBlockNumber,
423
- slot
424
- });
425
- this.emit('tx-count-check-failed', {
426
- minTxs: this.minTxsPerBlock,
427
- availableTxs: pendingTxCount
428
- });
429
- this.metrics.recordBlockProposalFailed('insufficient_txs');
430
- }
431
- return block;
288
+ // Create and return the checkpoint proposal job
289
+ return this.createCheckpointProposalJob(slot, checkpointNumber, syncedTo.blockNumber, proposer, publisher, attestorAddress, invalidateBlock);
432
290
  }
433
- async safeWork() {
434
- try {
435
- await this.work();
436
- } catch (err) {
437
- if (err instanceof SequencerTooSlowError) {
438
- // Log as warn only if we had to abort halfway through the block proposal
439
- const logLvl = [
440
- SequencerState.INITIALIZING_PROPOSAL,
441
- SequencerState.PROPOSER_CHECK
442
- ].includes(err.proposedState) ? 'debug' : 'warn';
443
- this.log[logLvl](err.message, {
444
- now: this.dateProvider.nowInSeconds()
445
- });
446
- } else {
447
- // Re-throw other errors
448
- throw err;
449
- }
450
- } finally{
451
- this.setState(SequencerState.IDLE, undefined);
452
- }
291
+ createCheckpointProposalJob(slot, checkpointNumber, syncedToBlockNumber, proposer, publisher, attestorAddress, invalidateBlock) {
292
+ return new CheckpointProposalJob(slot, checkpointNumber, syncedToBlockNumber, proposer, publisher, attestorAddress, invalidateBlock, this.validatorClient, this.globalsBuilder, this.p2pClient, this.worldState, this.l1ToL2MessageSource, this.checkpointsBuilder, this.l1Constants, this.config, this.timetable, this.slasherClient, this.epochCache, this.dateProvider, this.metrics, this, this.setState.bind(this), this.log);
453
293
  }
454
- setState(proposedState, slotNumber, opts = {}) {
294
+ /**
295
+ * Internal helper for setting the sequencer state and checks if we have enough time left in the slot to transition to the new state.
296
+ * @param proposedState - The new state to transition to.
297
+ * @param slotNumber - The current slot number.
298
+ * @param force - Whether to force the transition even if the sequencer is stopped.
299
+ */ setState(proposedState, slotNumber, opts = {}) {
455
300
  if (this.state === SequencerState.STOPPING && proposedState !== SequencerState.STOPPED && !opts.force) {
456
301
  this.log.warn(`Cannot set sequencer to ${proposedState} as it is stopping.`);
457
302
  throw new SequencerInterruptedError();
@@ -478,221 +323,10 @@ export { SequencerState };
478
323
  oldState: this.state,
479
324
  newState: proposedState,
480
325
  secondsIntoSlot,
481
- slotNumber
326
+ slot: slotNumber
482
327
  });
483
328
  this.state = proposedState;
484
329
  }
485
- async dropFailedTxsFromP2P(failedTxs) {
486
- if (failedTxs.length === 0) {
487
- return;
488
- }
489
- const failedTxData = failedTxs.map((fail)=>fail.tx);
490
- const failedTxHashes = failedTxData.map((tx)=>tx.getTxHash());
491
- this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
492
- await this.p2pClient.deleteTxs(failedTxHashes);
493
- }
494
- getBlockBuilderOptions(slot) {
495
- // Deadline for processing depends on whether we're proposing a block
496
- const secondsIntoSlot = this.getSecondsIntoSlot(slot);
497
- const processingEndTimeWithinSlot = this.timetable.getBlockProposalExecTimeEnd(secondsIntoSlot);
498
- // Deadline is only set if enforceTimeTable is enabled.
499
- const deadline = this.enforceTimeTable ? new Date((this.getSlotStartBuildTimestamp(slot) + processingEndTimeWithinSlot) * 1000) : undefined;
500
- return {
501
- maxTransactions: this.maxTxsPerBlock,
502
- maxBlockSize: this.maxBlockSizeInBytes,
503
- maxBlockGas: this.maxBlockGas,
504
- maxBlobFields: BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB,
505
- deadline
506
- };
507
- }
508
- /**
509
- * @notice Build and propose a block to the chain
510
- *
511
- * @dev MUST throw instead of exiting early to ensure that world-state
512
- * is being rolled back if the block is dropped.
513
- *
514
- * @param pendingTxs - Iterable of pending transactions to construct the block from
515
- * @param proposalHeader - The partial header constructed for the proposal
516
- * @param newGlobalVariables - The global variables for the new block
517
- * @param proposerAddress - The address of the proposer
518
- */ async buildBlockAndEnqueuePublish(pendingTxs, proposalHeader, newGlobalVariables, proposerAddress, invalidateBlock, publisher) {
519
- await publisher.validateBlockHeader(proposalHeader, invalidateBlock);
520
- const blockNumber = newGlobalVariables.blockNumber;
521
- const checkpointNumber = CheckpointNumber.fromBlockNumber(blockNumber);
522
- const slot = proposalHeader.slotNumber;
523
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
524
- const workTimer = new Timer();
525
- this.setState(SequencerState.CREATING_BLOCK, slot);
526
- try {
527
- const blockBuilderOptions = this.getBlockBuilderOptions(slot);
528
- const buildBlockRes = await this.blockBuilder.buildBlock(pendingTxs, l1ToL2Messages, newGlobalVariables, blockBuilderOptions);
529
- const { publicGas, block, publicProcessorDuration, numTxs, numMsgs, blockBuildingTimer, usedTxs, failedTxs } = buildBlockRes;
530
- const blockBuildDuration = workTimer.ms();
531
- await this.dropFailedTxsFromP2P(failedTxs);
532
- const minTxsPerBlock = this.minTxsPerBlock;
533
- if (numTxs < minTxsPerBlock) {
534
- this.log.warn(`Block ${blockNumber} has too few txs to be proposed (got ${numTxs} but required ${minTxsPerBlock})`, {
535
- slot,
536
- blockNumber,
537
- numTxs
538
- });
539
- throw new Error(`Block has too few successful txs to be proposed`);
540
- }
541
- // TODO(@PhilWindle) We should probably periodically check for things like another
542
- // block being published before ours instead of just waiting on our block
543
- await publisher.validateBlockHeader(block.getCheckpointHeader(), invalidateBlock);
544
- const blockStats = {
545
- eventName: 'l2-block-built',
546
- creator: proposerAddress?.toString() ?? publisher.getSenderAddress().toString(),
547
- duration: workTimer.ms(),
548
- publicProcessDuration: publicProcessorDuration,
549
- rollupCircuitsDuration: blockBuildingTimer.ms(),
550
- ...block.getStats()
551
- };
552
- const blockHash = await block.hash();
553
- const txHashes = block.body.txEffects.map((tx)=>tx.txHash);
554
- this.log.info(`Built block ${block.number} for slot ${slot} with ${numTxs} txs and ${numMsgs} messages. ${publicGas.l2Gas / workTimer.s()} mana/s`, {
555
- blockHash,
556
- globalVariables: block.header.globalVariables.toInspect(),
557
- txHashes,
558
- ...blockStats
559
- });
560
- // In fisherman mode, skip attestation collection
561
- let attestationsAndSigners;
562
- if (this.config.fishermanMode) {
563
- this.log.debug('Skipping attestation collection');
564
- attestationsAndSigners = CommitteeAttestationsAndSigners.empty();
565
- } else {
566
- this.log.debug('Collecting attestations');
567
- attestationsAndSigners = await this.collectAttestations(block, usedTxs, proposerAddress);
568
- this.log.verbose(`Collected ${attestationsAndSigners.attestations.length} attestations for block ${blockNumber} at slot ${slot}`, {
569
- blockHash,
570
- blockNumber,
571
- slot
572
- });
573
- }
574
- // In fisherman mode, skip attestation signing
575
- const attestationsAndSignersSignature = this.config.fishermanMode || !this.validatorClient ? Signature.empty() : await this.validatorClient.signAttestationsAndSigners(attestationsAndSigners, proposerAddress ?? publisher.getSenderAddress());
576
- await this.enqueuePublishL2Block(block, attestationsAndSigners, attestationsAndSignersSignature, invalidateBlock, publisher);
577
- this.metrics.recordBuiltBlock(blockBuildDuration, publicGas.l2Gas);
578
- return block;
579
- } catch (err) {
580
- this.metrics.recordFailedBlock();
581
- throw err;
582
- }
583
- }
584
- async collectAttestations(block, txs, proposerAddress) {
585
- const { committee, seed, epoch } = await this.epochCache.getCommittee(block.slot);
586
- // We checked above that the committee is defined, so this should never happen.
587
- if (!committee) {
588
- throw new Error('No committee when collecting attestations');
589
- }
590
- if (committee.length === 0) {
591
- this.log.verbose(`Attesting committee is empty`);
592
- return CommitteeAttestationsAndSigners.empty();
593
- } else {
594
- this.log.debug(`Attesting committee length is ${committee.length}`);
595
- }
596
- if (!this.validatorClient) {
597
- throw new Error('Missing validator client: Cannot collect attestations');
598
- }
599
- const numberOfRequiredAttestations = Math.floor(committee.length * 2 / 3) + 1;
600
- const slotNumber = block.header.globalVariables.slotNumber;
601
- this.setState(SequencerState.COLLECTING_ATTESTATIONS, slotNumber);
602
- this.log.debug('Creating block proposal for validators');
603
- const blockProposalOptions = {
604
- publishFullTxs: !!this.config.publishTxsWithProposals,
605
- broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
606
- };
607
- const proposal = await this.validatorClient.createBlockProposal(block.header.globalVariables.blockNumber, block.getCheckpointHeader(), block.archive.root, txs, proposerAddress, blockProposalOptions);
608
- if (!proposal) {
609
- throw new Error(`Failed to create block proposal`);
610
- }
611
- if (this.config.skipCollectingAttestations) {
612
- this.log.warn('Skipping attestation collection as per config (attesting with own keys only)');
613
- const attestations = await this.validatorClient?.collectOwnAttestations(proposal);
614
- return new CommitteeAttestationsAndSigners(orderAttestations(attestations ?? [], committee));
615
- }
616
- this.log.debug('Broadcasting block proposal to validators');
617
- await this.validatorClient.broadcastBlockProposal(proposal);
618
- const attestationTimeAllowed = this.enforceTimeTable ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_BLOCK) : this.aztecSlotDuration;
619
- this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
620
- const timer = new Timer();
621
- let collectedAttestationsCount = 0;
622
- try {
623
- const attestationDeadline = new Date(this.dateProvider.now() + attestationTimeAllowed * 1000);
624
- const attestations = await this.validatorClient.collectAttestations(proposal, numberOfRequiredAttestations, attestationDeadline);
625
- collectedAttestationsCount = attestations.length;
626
- // note: the smart contract requires that the signatures are provided in the order of the committee
627
- const sorted = orderAttestations(attestations, committee);
628
- // manipulate the attestations if we've been configured to do so
629
- if (this.config.injectFakeAttestation || this.config.shuffleAttestationOrdering) {
630
- return this.manipulateAttestations(block, epoch, seed, committee, sorted);
631
- }
632
- return new CommitteeAttestationsAndSigners(sorted);
633
- } catch (err) {
634
- if (err && err instanceof AttestationTimeoutError) {
635
- collectedAttestationsCount = err.collectedCount;
636
- }
637
- throw err;
638
- } finally{
639
- this.metrics.recordCollectedAttestations(collectedAttestationsCount, timer.ms());
640
- }
641
- }
642
- /** Breaks the attestations before publishing based on attack configs */ manipulateAttestations(block, epoch, seed, committee, attestations) {
643
- // Compute the proposer index in the committee, since we dont want to tweak it.
644
- // Otherwise, the L1 rollup contract will reject the block outright.
645
- const proposerIndex = Number(this.epochCache.computeProposerIndex(block.slot, epoch, seed, BigInt(committee.length)));
646
- if (this.config.injectFakeAttestation) {
647
- // Find non-empty attestations that are not from the proposer
648
- const nonProposerIndices = [];
649
- for(let i = 0; i < attestations.length; i++){
650
- if (!attestations[i].signature.isEmpty() && i !== proposerIndex) {
651
- nonProposerIndices.push(i);
652
- }
653
- }
654
- if (nonProposerIndices.length > 0) {
655
- const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
656
- this.log.warn(`Injecting fake attestation in block ${block.number} at index ${targetIndex}`);
657
- unfreeze(attestations[targetIndex]).signature = Signature.random();
658
- }
659
- return new CommitteeAttestationsAndSigners(attestations);
660
- }
661
- if (this.config.shuffleAttestationOrdering) {
662
- this.log.warn(`Shuffling attestation ordering in block ${block.number} (proposer index ${proposerIndex})`);
663
- const shuffled = [
664
- ...attestations
665
- ];
666
- const [i, j] = [
667
- (proposerIndex + 1) % shuffled.length,
668
- (proposerIndex + 2) % shuffled.length
669
- ];
670
- const valueI = shuffled[i];
671
- const valueJ = shuffled[j];
672
- shuffled[i] = valueJ;
673
- shuffled[j] = valueI;
674
- const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
675
- return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
676
- }
677
- return new CommitteeAttestationsAndSigners(attestations);
678
- }
679
- /**
680
- * Publishes the L2Block to the rollup contract.
681
- * @param block - The L2Block to be published.
682
- */ async enqueuePublishL2Block(block, attestationsAndSigners, attestationsAndSignersSignature, invalidateBlock, publisher) {
683
- // Publishes new block to the network and awaits the tx to be mined
684
- this.setState(SequencerState.PUBLISHING_BLOCK, block.header.globalVariables.slotNumber);
685
- // Time out tx at the end of the slot
686
- const slot = block.header.globalVariables.slotNumber;
687
- const txTimeoutAt = new Date((this.getSlotStartBuildTimestamp(slot) + this.aztecSlotDuration) * 1000);
688
- const enqueued = await publisher.enqueueProposeL2Block(block, attestationsAndSigners, attestationsAndSignersSignature, {
689
- txTimeoutAt,
690
- forcePendingBlockNumber: invalidateBlock?.forcePendingBlockNumber
691
- });
692
- if (!enqueued) {
693
- throw new Error(`Failed to enqueue publish of block ${block.number}`);
694
- }
695
- }
696
330
  /**
697
331
  * Returns whether all dependencies have caught up.
698
332
  * We don't check against the previous block submitted since it may have been reorg'd out.
@@ -744,6 +378,7 @@ export { SequencerState };
744
378
  pendingChainValidationStatus
745
379
  };
746
380
  }
381
+ // TODO(palla/mbps): This should be a new L2Block
747
382
  const block = await this.l2BlockSource.getBlock(blockNumber);
748
383
  if (!block) {
749
384
  // this shouldn't really happen because a moment ago we checked that all components were in sync
@@ -759,52 +394,6 @@ export { SequencerState };
759
394
  };
760
395
  }
761
396
  /**
762
- * Enqueues governance and slashing votes with the publisher. Does not block.
763
- * @param publisher - The publisher to enqueue votes with
764
- * @param attestorAddress - The attestor address to use for signing
765
- * @param slot - The slot number
766
- * @param timestamp - The timestamp for the votes
767
- * @param context - Optional context for logging (e.g., block number)
768
- * @returns A tuple of [governanceEnqueued, slashingEnqueued]
769
- */ enqueueGovernanceAndSlashingVotes(publisher, attestorAddress, slot, timestamp) {
770
- try {
771
- const signerFn = (msg)=>this.validatorClient.signWithAddress(attestorAddress, msg).then((s)=>s.toString());
772
- const enqueueGovernancePromise = this.governanceProposerPayload && !this.governanceProposerPayload.isZero() ? publisher.enqueueGovernanceCastSignal(this.governanceProposerPayload, slot, timestamp, attestorAddress, signerFn).catch((err)=>{
773
- this.log.error(`Error enqueuing governance vote`, err, {
774
- slot
775
- });
776
- return false;
777
- }) : undefined;
778
- const enqueueSlashingPromise = this.slasherClient ? this.slasherClient.getProposerActions(slot).then((actions)=>{
779
- // Record metrics for fisherman mode
780
- if (this.config.fishermanMode && actions.length > 0) {
781
- this.log.debug(`Fisherman mode: simulating ${actions.length} slashing action(s) for slot ${slot}`, {
782
- slot,
783
- actionCount: actions.length
784
- });
785
- this.metrics.recordSlashingAttempt(actions.length);
786
- }
787
- // Enqueue the actions to fully simulate L1 tx building (they won't be sent in fisherman mode)
788
- return publisher.enqueueSlashingActions(actions, slot, timestamp, attestorAddress, signerFn);
789
- }).catch((err)=>{
790
- this.log.error(`Error enqueuing slashing actions`, err, {
791
- slot
792
- });
793
- return false;
794
- }) : undefined;
795
- return [
796
- enqueueGovernancePromise,
797
- enqueueSlashingPromise
798
- ];
799
- } catch (err) {
800
- this.log.error(`Error enqueueing governance and slashing votes`, err);
801
- return [
802
- undefined,
803
- undefined
804
- ];
805
- }
806
- }
807
- /**
808
397
  * Checks if we are the proposer for the next slot.
809
398
  * @returns True if we can propose, and the proposer address (undefined if anyone can propose)
810
399
  */ async checkCanPropose(slot) {
@@ -860,32 +449,32 @@ export { SequencerState };
860
449
  * Tries to vote on slashing actions and governance when the sync check fails but we're past the max time for initializing a proposal.
861
450
  * This allows the sequencer to participate in governance/slashing votes even when it cannot build blocks.
862
451
  */ async tryVoteWhenSyncFails(args) {
863
- const { slot, ts } = args;
452
+ const { slot } = args;
864
453
  // Prevent duplicate attempts in the same slot
865
454
  if (this.lastSlotForVoteWhenSyncFailed === slot) {
866
- this.log.debug(`Already attempted to vote in slot ${slot} (skipping)`);
455
+ this.log.trace(`Already attempted to vote in slot ${slot} (skipping)`);
867
456
  return;
868
457
  }
869
458
  // Check if we're past the max time for initializing a proposal
870
459
  const secondsIntoSlot = this.getSecondsIntoSlot(slot);
871
- const maxAllowedTime = this.timetable.getMaxAllowedTime(SequencerState.INITIALIZING_PROPOSAL);
460
+ const maxAllowedTime = this.timetable.getMaxAllowedTime(SequencerState.INITIALIZING_CHECKPOINT);
872
461
  // If we haven't exceeded the time limit for initializing a proposal, don't proceed with voting
873
462
  // We use INITIALIZING_PROPOSAL time limit because if we're past that, we can't build a block anyway
874
463
  if (maxAllowedTime === undefined || secondsIntoSlot <= maxAllowedTime) {
875
- this.log.trace(`Not attempting to vote since there is still for block building`, {
464
+ this.log.trace(`Not attempting to vote since there is still time for block building`, {
876
465
  secondsIntoSlot,
877
466
  maxAllowedTime
878
467
  });
879
468
  return;
880
469
  }
881
- this.log.debug(`Sync for slot ${slot} failed, checking for voting opportunities`, {
470
+ this.log.trace(`Sync for slot ${slot} failed, checking for voting opportunities`, {
882
471
  secondsIntoSlot,
883
472
  maxAllowedTime
884
473
  });
885
474
  // Check if we're a proposer or proposal is open
886
475
  const [canPropose, proposer] = await this.checkCanPropose(slot);
887
476
  if (!canPropose) {
888
- this.log.debug(`Cannot vote in slot ${slot} since we are not a proposer`, {
477
+ this.log.trace(`Cannot vote in slot ${slot} since we are not a proposer`, {
889
478
  slot,
890
479
  proposer
891
480
  });
@@ -899,10 +488,11 @@ export { SequencerState };
899
488
  attestorAddress,
900
489
  slot
901
490
  });
902
- // Enqueue governance and slashing votes using the shared helper method
903
- const votesPromises = this.enqueueGovernanceAndSlashingVotes(publisher, attestorAddress, slot, ts);
904
- await Promise.all(votesPromises);
905
- if (votesPromises.every((p)=>!p)) {
491
+ // Enqueue governance and slashing votes
492
+ const voter = new CheckpointVoter(slot, publisher, attestorAddress, this.validatorClient, this.slasherClient, this.l1Constants, this.config, this.metrics, this.log);
493
+ const votesPromises = voter.enqueueVotes();
494
+ const votes = await Promise.all(votesPromises);
495
+ if (votes.every((p)=>!p)) {
906
496
  this.log.debug(`No votes to enqueue for slot ${slot}`);
907
497
  return;
908
498
  }
@@ -971,16 +561,7 @@ export { SequencerState };
971
561
  publisher.clearPendingRequests();
972
562
  }
973
563
  }
974
- getSlotStartBuildTimestamp(slotNumber) {
975
- return getSlotStartBuildTimestamp(slotNumber, this.l1Constants);
976
- }
977
- getSecondsIntoSlot(slotNumber) {
978
- const slotStartTimestamp = this.getSlotStartBuildTimestamp(slotNumber);
979
- return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
980
- }
981
- /**
982
- * Logs strategy comparison statistics at the end of each epoch in fisherman mode
983
- */ logStrategyComparison(epoch, publisher) {
564
+ logStrategyComparison(epoch, publisher) {
984
565
  const feeAnalyzer = publisher.getL1FeeAnalyzer();
985
566
  if (!feeAnalyzer) {
986
567
  return;
@@ -1006,6 +587,13 @@ export { SequencerState };
1006
587
  }))
1007
588
  });
1008
589
  }
590
+ getSlotStartBuildTimestamp(slotNumber) {
591
+ return getSlotStartBuildTimestamp(slotNumber, this.l1Constants);
592
+ }
593
+ getSecondsIntoSlot(slotNumber) {
594
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp(slotNumber);
595
+ return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
596
+ }
1009
597
  get aztecSlotDuration() {
1010
598
  return this.l1Constants.slotDuration;
1011
599
  }
@@ -1015,24 +603,19 @@ export { SequencerState };
1015
603
  getSlasherClient() {
1016
604
  return this.slasherClient;
1017
605
  }
606
+ get tracer() {
607
+ return this.metrics.tracer;
608
+ }
609
+ getValidatorAddresses() {
610
+ return this.validatorClient?.getValidatorAddresses();
611
+ }
612
+ getConfig() {
613
+ return this.config;
614
+ }
615
+ get l1PublishingTime() {
616
+ return this.config.l1PublishingTime ?? this.l1Constants.ethereumSlotDuration;
617
+ }
1018
618
  }
1019
619
  _ts_decorate([
1020
620
  trackSpan('Sequencer.work')
1021
621
  ], Sequencer.prototype, "safeWork", null);
1022
- _ts_decorate([
1023
- trackSpan('Sequencer.buildBlockAndEnqueuePublish', (_validTxs, _proposalHeader, newGlobalVariables)=>({
1024
- [Attributes.BLOCK_NUMBER]: newGlobalVariables.blockNumber
1025
- }))
1026
- ], Sequencer.prototype, "buildBlockAndEnqueuePublish", null);
1027
- _ts_decorate([
1028
- trackSpan('Sequencer.collectAttestations', (block, txHashes)=>({
1029
- [Attributes.BLOCK_NUMBER]: block.number,
1030
- [Attributes.BLOCK_ARCHIVE]: block.archive.toString(),
1031
- [Attributes.BLOCK_TXS_COUNT]: txHashes.length
1032
- }))
1033
- ], Sequencer.prototype, "collectAttestations", null);
1034
- _ts_decorate([
1035
- trackSpan('Sequencer.enqueuePublishL2Block', (block)=>({
1036
- [Attributes.BLOCK_NUMBER]: block.number
1037
- }))
1038
- ], Sequencer.prototype, "enqueuePublishL2Block", null);