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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/dest/client/sequencer-client.d.ts +9 -8
  2. package/dest/client/sequencer-client.d.ts.map +1 -1
  3. package/dest/client/sequencer-client.js +28 -24
  4. package/dest/config.d.ts +7 -1
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +63 -26
  7. package/dest/global_variable_builder/global_builder.d.ts +16 -8
  8. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  9. package/dest/global_variable_builder/global_builder.js +37 -28
  10. package/dest/index.d.ts +2 -2
  11. package/dest/index.d.ts.map +1 -1
  12. package/dest/index.js +1 -1
  13. package/dest/publisher/config.d.ts +3 -3
  14. package/dest/publisher/config.d.ts.map +1 -1
  15. package/dest/publisher/config.js +2 -2
  16. package/dest/publisher/sequencer-publisher-factory.d.ts +3 -3
  17. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  18. package/dest/publisher/sequencer-publisher-factory.js +1 -1
  19. package/dest/publisher/sequencer-publisher-metrics.d.ts +3 -3
  20. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  21. package/dest/publisher/sequencer-publisher.d.ts +27 -23
  22. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  23. package/dest/publisher/sequencer-publisher.js +119 -64
  24. package/dest/sequencer/block_builder.d.ts +1 -3
  25. package/dest/sequencer/block_builder.d.ts.map +1 -1
  26. package/dest/sequencer/block_builder.js +4 -2
  27. package/dest/sequencer/checkpoint_builder.d.ts +63 -0
  28. package/dest/sequencer/checkpoint_builder.d.ts.map +1 -0
  29. package/dest/sequencer/checkpoint_builder.js +131 -0
  30. package/dest/sequencer/checkpoint_proposal_job.d.ts +74 -0
  31. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
  32. package/dest/sequencer/checkpoint_proposal_job.js +642 -0
  33. package/dest/sequencer/checkpoint_voter.d.ts +34 -0
  34. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
  35. package/dest/sequencer/checkpoint_voter.js +85 -0
  36. package/dest/sequencer/events.d.ts +46 -0
  37. package/dest/sequencer/events.d.ts.map +1 -0
  38. package/dest/sequencer/events.js +1 -0
  39. package/dest/sequencer/index.d.ts +5 -1
  40. package/dest/sequencer/index.d.ts.map +1 -1
  41. package/dest/sequencer/index.js +4 -0
  42. package/dest/sequencer/metrics.d.ts +21 -1
  43. package/dest/sequencer/metrics.d.ts.map +1 -1
  44. package/dest/sequencer/metrics.js +154 -0
  45. package/dest/sequencer/sequencer.d.ts +91 -125
  46. package/dest/sequencer/sequencer.d.ts.map +1 -1
  47. package/dest/sequencer/sequencer.js +581 -582
  48. package/dest/sequencer/timetable.d.ts +54 -14
  49. package/dest/sequencer/timetable.d.ts.map +1 -1
  50. package/dest/sequencer/timetable.js +148 -59
  51. package/dest/sequencer/types.d.ts +3 -0
  52. package/dest/sequencer/types.d.ts.map +1 -0
  53. package/dest/sequencer/types.js +1 -0
  54. package/dest/sequencer/utils.d.ts +14 -8
  55. package/dest/sequencer/utils.d.ts.map +1 -1
  56. package/dest/sequencer/utils.js +7 -4
  57. package/dest/test/index.d.ts +3 -1
  58. package/dest/test/index.d.ts.map +1 -1
  59. package/dest/test/mock_checkpoint_builder.d.ts +83 -0
  60. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
  61. package/dest/test/mock_checkpoint_builder.js +179 -0
  62. package/dest/test/utils.d.ts +49 -0
  63. package/dest/test/utils.d.ts.map +1 -0
  64. package/dest/test/utils.js +94 -0
  65. package/package.json +27 -27
  66. package/src/client/sequencer-client.ts +24 -31
  67. package/src/config.ts +68 -25
  68. package/src/global_variable_builder/global_builder.ts +47 -41
  69. package/src/index.ts +2 -0
  70. package/src/publisher/config.ts +3 -3
  71. package/src/publisher/sequencer-publisher-factory.ts +3 -3
  72. package/src/publisher/sequencer-publisher-metrics.ts +2 -2
  73. package/src/publisher/sequencer-publisher.ts +170 -74
  74. package/src/sequencer/README.md +531 -0
  75. package/src/sequencer/block_builder.ts +4 -1
  76. package/src/sequencer/checkpoint_builder.ts +217 -0
  77. package/src/sequencer/checkpoint_proposal_job.ts +706 -0
  78. package/src/sequencer/checkpoint_voter.ts +105 -0
  79. package/src/sequencer/events.ts +27 -0
  80. package/src/sequencer/index.ts +4 -0
  81. package/src/sequencer/metrics.ts +202 -0
  82. package/src/sequencer/sequencer.ts +300 -779
  83. package/src/sequencer/timetable.ts +173 -79
  84. package/src/sequencer/types.ts +6 -0
  85. package/src/sequencer/utils.ts +18 -9
  86. package/src/test/index.ts +2 -0
  87. package/src/test/mock_checkpoint_builder.ts +247 -0
  88. package/src/test/utils.ts +137 -0
@@ -1,245 +1,156 @@
1
- import { L2Block } from '@aztec/aztec.js/block';
2
1
  import { getKzg } from '@aztec/blob-lib';
3
- import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
4
3
  import type { EpochCache } from '@aztec/epoch-cache';
5
4
  import { NoCommitteeError, type RollupContract } from '@aztec/ethereum/contracts';
6
- import { FormattedViemError } from '@aztec/ethereum/utils';
7
5
  import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
8
- import { omit, pick } from '@aztec/foundation/collection';
9
- import { randomInt } from '@aztec/foundation/crypto/random';
6
+ import { merge, omit, pick } from '@aztec/foundation/collection';
10
7
  import { Fr } from '@aztec/foundation/curves/bn254';
11
8
  import { EthAddress } from '@aztec/foundation/eth-address';
12
- import { Signature } from '@aztec/foundation/eth-signature';
13
9
  import { createLogger } from '@aztec/foundation/log';
14
10
  import { RunningPromise } from '@aztec/foundation/running-promise';
15
- import { type DateProvider, Timer } from '@aztec/foundation/timer';
16
- import { type TypedEventEmitter, unfreeze } from '@aztec/foundation/types';
11
+ import type { DateProvider } from '@aztec/foundation/timer';
12
+ import type { TypedEventEmitter } from '@aztec/foundation/types';
17
13
  import type { P2P } from '@aztec/p2p';
18
14
  import type { SlasherClientInterface } from '@aztec/slasher';
15
+ import type { L2BlockNew, L2BlockSource, ValidateBlockResult } from '@aztec/stdlib/block';
16
+ import type { Checkpoint } from '@aztec/stdlib/checkpoint';
17
+ import { getSlotAtTimestamp, getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
19
18
  import {
20
- CommitteeAttestation,
21
- CommitteeAttestationsAndSigners,
22
- type L2BlockSource,
23
- MaliciousCommitteeAttestationsAndSigners,
24
- type ValidateBlockResult,
25
- } from '@aztec/stdlib/block';
26
- import { type L1RollupConstants, getSlotAtTimestamp, getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
27
- import { Gas } from '@aztec/stdlib/gas';
28
- import {
29
- type IFullNodeBlockBuilder,
30
- type PublicProcessorLimits,
19
+ type ResolvedSequencerConfig,
20
+ type SequencerConfig,
31
21
  SequencerConfigSchema,
32
22
  type WorldStateSynchronizer,
33
23
  } from '@aztec/stdlib/interfaces/server';
34
24
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
35
- import type { BlockProposalOptions } from '@aztec/stdlib/p2p';
36
- import { orderAttestations } from '@aztec/stdlib/p2p';
37
- import { CheckpointHeader } from '@aztec/stdlib/rollup';
38
25
  import { pickFromSchema } from '@aztec/stdlib/schemas';
39
- import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
40
26
  import { MerkleTreeId } from '@aztec/stdlib/trees';
41
- import { ContentCommitment, type FailedTx, GlobalVariables, Tx } from '@aztec/stdlib/tx';
42
- import { AttestationTimeoutError } from '@aztec/stdlib/validators';
43
- import { Attributes, type TelemetryClient, type Tracer, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
27
+ import { type TelemetryClient, type Tracer, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
44
28
  import type { ValidatorClient } from '@aztec/validator-client';
45
29
 
46
30
  import EventEmitter from 'node:events';
47
- import type { TypedDataDefinition } from 'viem';
48
31
 
32
+ import { DefaultSequencerConfig } from '../config.js';
49
33
  import type { GlobalVariableBuilder } from '../global_variable_builder/global_builder.js';
50
34
  import type { SequencerPublisherFactory } from '../publisher/sequencer-publisher-factory.js';
51
- import type { Action, InvalidateBlockRequest, SequencerPublisher } from '../publisher/sequencer-publisher.js';
52
- import type { SequencerConfig } from './config.js';
35
+ import type { InvalidateBlockRequest, SequencerPublisher } from '../publisher/sequencer-publisher.js';
36
+ import { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
37
+ import { CheckpointProposalJob } from './checkpoint_proposal_job.js';
38
+ import { CheckpointVoter } from './checkpoint_voter.js';
53
39
  import { SequencerInterruptedError, SequencerTooSlowError } from './errors.js';
40
+ import type { SequencerEvents } from './events.js';
54
41
  import { SequencerMetrics } from './metrics.js';
55
42
  import { SequencerTimetable } from './timetable.js';
56
- import { SequencerState, type SequencerStateWithSlot } from './utils.js';
43
+ import type { SequencerRollupConstants } from './types.js';
44
+ import { SequencerState } from './utils.js';
57
45
 
58
46
  export { SequencerState };
59
47
 
60
- type SequencerRollupConstants = Pick<L1RollupConstants, 'ethereumSlotDuration' | 'l1GenesisTime' | 'slotDuration'>;
61
-
62
- export type SequencerEvents = {
63
- ['state-changed']: (args: {
64
- oldState: SequencerState;
65
- newState: SequencerState;
66
- secondsIntoSlot?: number;
67
- slotNumber?: SlotNumber;
68
- }) => void;
69
- ['proposer-rollup-check-failed']: (args: { reason: string }) => void;
70
- ['tx-count-check-failed']: (args: { minTxs: number; availableTxs: number }) => void;
71
- ['block-build-failed']: (args: { reason: string }) => void;
72
- ['block-publish-failed']: (args: {
73
- successfulActions?: Action[];
74
- failedActions?: Action[];
75
- sentActions?: Action[];
76
- expiredActions?: Action[];
77
- }) => void;
78
- ['block-published']: (args: { blockNumber: BlockNumber; slot: number }) => void;
79
- };
80
-
81
48
  /**
82
49
  * Sequencer client
83
- * - Wins a period of time to become the sequencer (depending on finalized protocol).
84
- * - Chooses a set of txs from the tx pool to be in the rollup.
85
- * - Simulate the rollup of txs.
86
- * - Adds proof requests to the request pool (not for this milestone).
87
- * - Receives results to those proofs from the network (repeats as necessary) (not for this milestone).
88
- * - Publishes L1 tx(s) to the rollup contract via RollupPublisher.
50
+ * - Checks whether it is elected as proposer for the next slot
51
+ * - Builds multiple blocks and broadcasts them
52
+ * - Collects attestations for the checkpoint
53
+ * - Publishes the checkpoint to L1
54
+ * - Votes for proposals and slashes on L1
89
55
  */
90
56
  export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<SequencerEvents>) {
91
57
  private runningPromise?: RunningPromise;
92
- private pollingIntervalMs: number = 1000;
93
- private maxTxsPerBlock = 32;
94
- private minTxsPerBlock = 1;
95
- private maxL1TxInclusionTimeIntoSlot = 0;
96
58
  private state = SequencerState.STOPPED;
97
- private maxBlockSizeInBytes: number = 1024 * 1024;
98
- private maxBlockGas: Gas = new Gas(100e9, 100e9);
99
59
  private metrics: SequencerMetrics;
100
60
 
101
- private lastBlockPublished: L2Block | undefined;
102
-
103
- private governanceProposerPayload: EthAddress | undefined;
104
-
105
61
  /** The last slot for which we attempted to vote when sync failed, to prevent duplicate attempts. */
106
62
  private lastSlotForVoteWhenSyncFailed: SlotNumber | undefined;
107
63
 
108
- /** The last slot for which we built a validation block in fisherman mode, to prevent duplicate attempts. */
109
- private lastSlotForValidationBlock: SlotNumber | undefined;
64
+ /** The last slot for which we triggered a checkpoint proposal job, to prevent duplicate attempts. */
65
+ private lastSlotForCheckpointProposalJob: SlotNumber | undefined;
66
+
67
+ /** Last successful checkpoint proposed */
68
+ private lastCheckpointProposed: Checkpoint | undefined;
69
+
70
+ /** The last epoch for which we logged strategy comparison in fisherman mode. */
71
+ private lastEpochForStrategyComparison: EpochNumber | undefined;
110
72
 
111
73
  /** The maximum number of seconds that the sequencer can be into a slot to transition to a particular state. */
112
74
  protected timetable!: SequencerTimetable;
113
- protected enforceTimeTable: boolean = false;
114
75
 
115
76
  // This shouldn't be here as this gets re-created each time we build/propose a block.
116
77
  // But we have a number of tests that abuse/rely on this class having a permanent publisher.
117
78
  // As long as those tests only configure a single publisher they will continue to work.
118
79
  // This will get re-assigned every time the sequencer goes to build a new block to a publisher that is valid
119
80
  // for the block proposer.
81
+ // TODO(palla/mbps): Remove this field and fix tests
120
82
  protected publisher: SequencerPublisher | undefined;
121
83
 
84
+ /** Config for the sequencer */
85
+ protected config: ResolvedSequencerConfig = DefaultSequencerConfig;
86
+
122
87
  constructor(
123
88
  protected publisherFactory: SequencerPublisherFactory,
124
- protected validatorClient: ValidatorClient | undefined, // During migration the validator client can be inactive
89
+ protected validatorClient: ValidatorClient,
125
90
  protected globalsBuilder: GlobalVariableBuilder,
126
91
  protected p2pClient: P2P,
127
92
  protected worldState: WorldStateSynchronizer,
128
93
  protected slasherClient: SlasherClientInterface | undefined,
129
94
  protected l2BlockSource: L2BlockSource,
130
95
  protected l1ToL2MessageSource: L1ToL2MessageSource,
131
- protected blockBuilder: IFullNodeBlockBuilder,
96
+ protected checkpointsBuilder: FullNodeCheckpointsBuilder,
132
97
  protected l1Constants: SequencerRollupConstants,
133
98
  protected dateProvider: DateProvider,
134
99
  protected epochCache: EpochCache,
135
100
  protected rollupContract: RollupContract,
136
- protected config: SequencerConfig,
101
+ config: SequencerConfig,
137
102
  protected telemetry: TelemetryClient = getTelemetryClient(),
138
103
  protected log = createLogger('sequencer'),
139
104
  ) {
140
105
  super();
141
106
 
142
107
  // Add [FISHERMAN] prefix to logger if in fisherman mode
143
- if (this.config.fishermanMode) {
108
+ if (config.fishermanMode) {
144
109
  this.log = log.createChild('[FISHERMAN]');
145
110
  }
146
111
 
147
112
  this.metrics = new SequencerMetrics(telemetry, this.rollupContract, 'Sequencer');
148
- // Initialize config
149
- this.updateConfig(this.config);
150
- }
151
-
152
- get tracer(): Tracer {
153
- return this.metrics.tracer;
154
- }
155
-
156
- public getValidatorAddresses() {
157
- return this.validatorClient?.getValidatorAddresses();
158
- }
159
-
160
- public getConfig() {
161
- return this.config;
162
- }
163
-
164
- /**
165
- * Updates sequencer config by the defined values in the config on input.
166
- * @param config - New parameters.
167
- */
168
- public updateConfig(config: SequencerConfig) {
169
- this.log.info(
170
- `Sequencer config set`,
171
- omit(pickFromSchema(config, SequencerConfigSchema), 'txPublicSetupAllowList'),
172
- );
173
-
174
- if (config.transactionPollingIntervalMS !== undefined) {
175
- this.pollingIntervalMs = config.transactionPollingIntervalMS;
176
- }
177
- if (config.maxTxsPerBlock !== undefined) {
178
- this.maxTxsPerBlock = config.maxTxsPerBlock;
179
- }
180
- if (config.minTxsPerBlock !== undefined) {
181
- this.minTxsPerBlock = config.minTxsPerBlock;
182
- }
183
- if (config.maxDABlockGas !== undefined) {
184
- this.maxBlockGas = new Gas(config.maxDABlockGas, this.maxBlockGas.l2Gas);
185
- }
186
- if (config.maxL2BlockGas !== undefined) {
187
- this.maxBlockGas = new Gas(this.maxBlockGas.daGas, config.maxL2BlockGas);
188
- }
189
- if (config.maxBlockSizeInBytes !== undefined) {
190
- this.maxBlockSizeInBytes = config.maxBlockSizeInBytes;
191
- }
192
- if (config.governanceProposerPayload) {
193
- this.governanceProposerPayload = config.governanceProposerPayload;
194
- }
195
- if (config.maxL1TxInclusionTimeIntoSlot !== undefined) {
196
- this.maxL1TxInclusionTimeIntoSlot = config.maxL1TxInclusionTimeIntoSlot;
197
- }
198
- if (config.enforceTimeTable !== undefined) {
199
- this.enforceTimeTable = config.enforceTimeTable;
200
- }
201
-
202
- this.setTimeTable();
203
-
204
- // TODO: Just read everything from the config object as needed instead of copying everything into local vars.
205
-
206
- // Update all values on this.config that are populated in the config object.
207
- Object.assign(this.config, config);
113
+ this.updateConfig(config);
208
114
  }
209
115
 
210
- private setTimeTable() {
116
+ /** Updates sequencer config by the defined values and updates the timetable */
117
+ public updateConfig(config: Partial<SequencerConfig>) {
118
+ const filteredConfig = pickFromSchema(config, SequencerConfigSchema);
119
+ this.log.info(`Updated sequencer config`, omit(filteredConfig, 'txPublicSetupAllowList'));
120
+ this.config = merge(this.config, filteredConfig);
211
121
  this.timetable = new SequencerTimetable(
212
122
  {
213
123
  ethereumSlotDuration: this.l1Constants.ethereumSlotDuration,
214
124
  aztecSlotDuration: this.aztecSlotDuration,
215
- maxL1TxInclusionTimeIntoSlot: this.maxL1TxInclusionTimeIntoSlot,
216
- attestationPropagationTime: this.config.attestationPropagationTime,
217
- enforce: this.enforceTimeTable,
125
+ l1PublishingTime: this.l1PublishingTime,
126
+ p2pPropagationTime: this.config.attestationPropagationTime,
127
+ blockDurationMs: this.config.blockDurationMs,
128
+ enforce: this.config.enforceTimeTable,
218
129
  },
219
130
  this.metrics,
220
131
  this.log,
221
132
  );
222
133
  }
223
134
 
135
+ /** Initializes the sequencer (precomputes tables and creates a publisher). Takes about 3s. */
224
136
  public async init() {
225
- // Takes ~3s to precompute some tables.
226
137
  getKzg();
227
138
  this.publisher = (await this.publisherFactory.create(undefined)).publisher;
228
139
  }
229
140
 
230
- /**
231
- * Starts the sequencer and moves to IDLE state.
232
- */
141
+ /** Starts the sequencer and moves to IDLE state. */
233
142
  public start() {
234
- this.runningPromise = new RunningPromise(this.safeWork.bind(this), this.log, this.pollingIntervalMs);
143
+ this.runningPromise = new RunningPromise(
144
+ this.safeWork.bind(this),
145
+ this.log,
146
+ this.config.sequencerPollingIntervalMS,
147
+ );
235
148
  this.setState(SequencerState.IDLE, undefined, { force: true });
236
149
  this.runningPromise.start();
237
150
  this.log.info('Started sequencer');
238
151
  }
239
152
 
240
- /**
241
- * Stops the sequencer from processing txs and moves to STOPPED state.
242
- */
153
+ /** Stops the sequencer from building blocks and moves to STOPPED state. */
243
154
  public async stop(): Promise<void> {
244
155
  this.log.info(`Stopping sequencer`);
245
156
  this.setState(SequencerState.STOPPING, undefined, { force: true });
@@ -249,52 +160,119 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
249
160
  this.log.info('Stopped sequencer');
250
161
  }
251
162
 
252
- /**
253
- * Returns the current state of the sequencer.
254
- * @returns An object with a state entry with one of SequencerState.
255
- */
163
+ @trackSpan('Sequencer.work')
164
+ /** Main sequencer loop with a try/catch */
165
+ protected async safeWork() {
166
+ try {
167
+ await this.work();
168
+ } catch (err) {
169
+ this.emit('checkpoint-error', { error: err as Error });
170
+ if (err instanceof SequencerTooSlowError) {
171
+ // TODO(palla/mbps): Add missing states
172
+ // Log as warn only if we had to abort halfway through the block proposal
173
+ const logLvl = [SequencerState.INITIALIZING_CHECKPOINT, SequencerState.PROPOSER_CHECK].includes(
174
+ err.proposedState,
175
+ )
176
+ ? ('debug' as const)
177
+ : ('warn' as const);
178
+ this.log[logLvl](err.message, { now: this.dateProvider.nowInSeconds() });
179
+ } else {
180
+ // Re-throw other errors
181
+ throw err;
182
+ }
183
+ } finally {
184
+ this.setState(SequencerState.IDLE, undefined);
185
+ }
186
+ }
187
+
188
+ /** Returns the current state of the sequencer. */
256
189
  public status() {
257
190
  return { state: this.state };
258
191
  }
259
192
 
260
193
  /**
261
- * @notice Performs most of the sequencer duties:
262
- * - Checks if we are up to date
263
- * - If we are and we are the sequencer, collect txs and build a block
264
- * - Collect attestations for the block
265
- * - Submit block
266
- * - If our block for some reason is not included, revert the state
194
+ * Main sequencer loop:
195
+ * - Checks if we are up to date
196
+ * - If we are and we are the sequencer, collect txs and build blocks
197
+ * - Build multiple blocks per slot when configured
198
+ * - Collect attestations for the final block
199
+ * - Submit checkpoint
267
200
  */
268
201
  protected async work() {
269
202
  this.setState(SequencerState.SYNCHRONIZING, undefined);
270
- const { slot, ts, now } = this.epochCache.getEpochAndSlotInNextL1Slot();
203
+ const { slot, ts, now, epoch } = this.epochCache.getEpochAndSlotInNextL1Slot();
271
204
 
272
- // Check we have not already published a block for this slot (cheapest check)
273
- if (this.lastBlockPublished && this.lastBlockPublished.header.getSlot() >= slot) {
274
- this.log.debug(
275
- `Cannot propose block at next L2 slot ${slot} since that slot was taken by our own block ${this.lastBlockPublished.number}`,
276
- );
205
+ // Check if we are synced and it's our slot, grab a publisher, check previous block invalidation, etc
206
+ const checkpointProposalJob = await this.prepareCheckpointProposal(slot, ts, now);
207
+ if (!checkpointProposalJob) {
277
208
  return;
278
209
  }
279
210
 
211
+ // Execute the checkpoint proposal job
212
+ const checkpoint = await checkpointProposalJob.execute();
213
+
214
+ // Update last checkpoint proposed (currently unused)
215
+ if (checkpoint) {
216
+ this.lastCheckpointProposed = checkpoint;
217
+ }
218
+
219
+ // Log fee strategy comparison if on fisherman
220
+ if (
221
+ this.config.fishermanMode &&
222
+ (this.lastEpochForStrategyComparison === undefined || epoch > this.lastEpochForStrategyComparison)
223
+ ) {
224
+ this.logStrategyComparison(epoch, checkpointProposalJob.getPublisher());
225
+ this.lastEpochForStrategyComparison = epoch;
226
+ }
227
+
228
+ return checkpoint;
229
+ }
230
+
231
+ /**
232
+ * Prepares the checkpoint proposal by performing all necessary checks and setup.
233
+ * This is the initial step in the main loop.
234
+ * @returns CheckpointProposalJob if successful, undefined if we are not yet synced or are not the proposer.
235
+ */
236
+ private async prepareCheckpointProposal(
237
+ slot: SlotNumber,
238
+ ts: bigint,
239
+ now: bigint,
240
+ ): Promise<CheckpointProposalJob | undefined> {
241
+ // Check we have not already processed this slot (cheapest check)
242
+ // We only check this if enforce timetable is set, since we want to keep processing the same slot if we are not
243
+ // running against actual time (eg when we use sandbox-style automining)
244
+ if (
245
+ this.lastSlotForCheckpointProposalJob &&
246
+ this.lastSlotForCheckpointProposalJob >= slot &&
247
+ this.config.enforceTimeTable
248
+ ) {
249
+ this.log.trace(`Slot ${slot} has already been processed`);
250
+ return undefined;
251
+ }
252
+
253
+ // But if we have already proposed for this slot, the we definitely have to skip it, automining or not
254
+ if (this.lastCheckpointProposed && this.lastCheckpointProposed.header.slotNumber >= slot) {
255
+ this.log.trace(`Slot ${slot} has already been published as checkpoint ${this.lastCheckpointProposed.number}`);
256
+ return undefined;
257
+ }
258
+
280
259
  // Check all components are synced to latest as seen by the archiver (queries all subsystems)
281
260
  const syncedTo = await this.checkSync({ ts, slot });
282
261
  if (!syncedTo) {
283
262
  await this.tryVoteWhenSyncFails({ slot, ts });
284
- return;
263
+ return undefined;
285
264
  }
286
265
 
287
- const chainTipArchive = syncedTo.archive;
288
- const newBlockNumber = BlockNumber(syncedTo.blockNumber + 1);
266
+ // TODO(palla/mbps): Compute proper checkpoint number
267
+ const checkpointNumber = CheckpointNumber.fromBlockNumber(BlockNumber(syncedTo.blockNumber + 1));
289
268
 
290
- const syncLogData = {
269
+ const logCtx = {
291
270
  now,
292
271
  syncedToL1Ts: syncedTo.l1Timestamp,
293
272
  syncedToL2Slot: getSlotAtTimestamp(syncedTo.l1Timestamp, this.l1Constants),
294
- nextL2Slot: slot,
295
- nextL2SlotTs: ts,
296
- l1SlotDuration: this.l1Constants.ethereumSlotDuration,
297
- newBlockNumber,
273
+ slot,
274
+ slotTs: ts,
275
+ checkpointNumber,
298
276
  isPendingChainValid: pick(syncedTo.pendingChainValidationStatus, 'valid', 'reason', 'invalidIndex'),
299
277
  };
300
278
 
@@ -305,266 +283,138 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
305
283
  // If we are not a proposer check if we should invalidate a invalid block, and bail
306
284
  if (!canPropose) {
307
285
  await this.considerInvalidatingBlock(syncedTo, slot);
308
- return;
309
- }
310
-
311
- // In fisherman mode, check if we've already validated this slot to prevent duplicate attempts
312
- if (this.config.fishermanMode) {
313
- if (this.lastSlotForValidationBlock === slot) {
314
- this.log.trace(`Already validated block building for slot ${slot} (skipping)`, { slot });
315
- return;
316
- }
317
- this.log.debug(
318
- `Building validation block for slot ${slot} (actual proposer: ${proposer?.toString() ?? 'none'})`,
319
- { slot, proposer: proposer?.toString() },
320
- );
321
- // Mark this slot as being validated
322
- this.lastSlotForValidationBlock = slot;
286
+ return undefined;
323
287
  }
324
288
 
325
289
  // Check that the slot is not taken by a block already (should never happen, since only us can propose for this slot)
326
290
  if (syncedTo.block && syncedTo.block.header.getSlot() >= slot) {
327
291
  this.log.warn(
328
292
  `Cannot propose block at next L2 slot ${slot} since that slot was taken by block ${syncedTo.blockNumber}`,
329
- { ...syncLogData, block: syncedTo.block.header.toInspect() },
293
+ { ...logCtx, block: syncedTo.block.header.toInspect() },
330
294
  );
331
295
  this.metrics.recordBlockProposalPrecheckFailed('slot_already_taken');
332
- return;
296
+ return undefined;
333
297
  }
334
298
 
335
299
  // We now need to get ourselves a publisher.
336
300
  // The returned attestor will be the one we provided if we provided one.
337
301
  // Otherwise it will be a valid attestor for the returned publisher.
338
302
  // In fisherman mode, pass undefined to use the fisherman's own keystore instead of the actual proposer's
339
- const { attestorAddress, publisher } = await this.publisherFactory.create(
340
- this.config.fishermanMode ? undefined : proposer,
341
- );
303
+ const proposerForPublisher = this.config.fishermanMode ? undefined : proposer;
304
+ const { attestorAddress, publisher } = await this.publisherFactory.create(proposerForPublisher);
342
305
  this.log.verbose(`Created publisher at address ${publisher.getSenderAddress()} for attestor ${attestorAddress}`);
343
306
  this.publisher = publisher;
344
307
 
345
308
  // In fisherman mode, set the actual proposer's address for simulations
346
- if (this.config.fishermanMode) {
347
- if (proposer) {
348
- publisher.setProposerAddressForSimulation(proposer);
349
- this.log.debug(`Set proposer address ${proposer} for simulation in fisherman mode`);
350
- }
309
+ if (this.config.fishermanMode && proposer) {
310
+ publisher.setProposerAddressForSimulation(proposer);
311
+ this.log.debug(`Set proposer address ${proposer} for simulation in fisherman mode`);
351
312
  }
352
313
 
353
- // Get proposer credentials
354
- const coinbase = this.validatorClient!.getCoinbaseForAttestor(attestorAddress);
355
- const feeRecipient = this.validatorClient!.getFeeRecipientForAttestor(attestorAddress);
356
-
357
314
  // Prepare invalidation request if the pending chain is invalid (returns undefined if no need)
315
+ // TODO(palla/mbps): We need to invalidate checkpoints, not blocks
358
316
  const invalidateBlock = await publisher.simulateInvalidateBlock(syncedTo.pendingChainValidationStatus);
359
317
 
360
- // Check with the rollup if we can indeed propose at the next L2 slot. This check should not fail
318
+ // Check with the rollup contract if we can indeed propose at the next L2 slot. This check should not fail
361
319
  // if all the previous checks are good, but we do it just in case.
362
320
  const canProposeCheck = await publisher.canProposeAtNextEthBlock(
363
- chainTipArchive,
321
+ syncedTo.archive,
364
322
  proposer ?? EthAddress.ZERO,
365
323
  invalidateBlock,
366
324
  );
367
325
 
368
326
  if (canProposeCheck === undefined) {
369
327
  this.log.warn(
370
- `Cannot propose block ${newBlockNumber} at slot ${slot} due to failed rollup contract check`,
371
- syncLogData,
328
+ `Cannot propose checkpoint ${checkpointNumber} at slot ${slot} due to failed rollup contract check`,
329
+ logCtx,
372
330
  );
373
- this.emit('proposer-rollup-check-failed', { reason: 'Rollup contract check failed' });
331
+ this.emit('proposer-rollup-check-failed', { reason: 'Rollup contract check failed', slot });
374
332
  this.metrics.recordBlockProposalPrecheckFailed('rollup_contract_check_failed');
375
- return;
376
- } else if (canProposeCheck.slot !== slot) {
333
+ return undefined;
334
+ }
335
+
336
+ if (canProposeCheck.slot !== slot) {
377
337
  this.log.warn(
378
338
  `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}.`,
379
- { ...syncLogData, rollup: canProposeCheck, newBlockNumber, expectedSlot: slot },
339
+ { ...logCtx, rollup: canProposeCheck, expectedSlot: slot },
380
340
  );
381
- this.emit('proposer-rollup-check-failed', { reason: 'Slot mismatch' });
341
+ this.emit('proposer-rollup-check-failed', { reason: 'Slot mismatch', slot });
382
342
  this.metrics.recordBlockProposalPrecheckFailed('slot_mismatch');
383
- return;
384
- } else if (canProposeCheck.checkpointNumber !== CheckpointNumber.fromBlockNumber(newBlockNumber)) {
343
+ return undefined;
344
+ }
345
+
346
+ if (canProposeCheck.checkpointNumber !== checkpointNumber) {
385
347
  this.log.warn(
386
- `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}.`,
387
- { ...syncLogData, rollup: canProposeCheck, newBlockNumber, expectedSlot: slot },
348
+ `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}.`,
349
+ { ...logCtx, rollup: canProposeCheck, expectedSlot: slot },
388
350
  );
389
- this.emit('proposer-rollup-check-failed', { reason: 'Block mismatch' });
351
+ this.emit('proposer-rollup-check-failed', { reason: 'Block mismatch', slot });
390
352
  this.metrics.recordBlockProposalPrecheckFailed('block_number_mismatch');
391
- return;
353
+ return undefined;
392
354
  }
393
355
 
394
- this.log.debug(`Can propose block ${newBlockNumber} at slot ${slot} as ${proposer}`, { ...syncLogData });
356
+ this.lastSlotForCheckpointProposalJob = slot;
357
+ this.log.info(`Preparing checkpoint proposal ${checkpointNumber} at slot ${slot}`, { ...logCtx, proposer });
395
358
 
396
- const newGlobalVariables = await this.globalsBuilder.buildGlobalVariables(
397
- newBlockNumber,
398
- coinbase,
399
- feeRecipient,
400
- slot,
401
- );
402
-
403
- // Enqueue governance and slashing votes (returns promises that will be awaited later)
404
- // In fisherman mode, we simulate slashing but don't actually publish to L1
405
- const votesPromises = this.enqueueGovernanceAndSlashingVotes(
406
- publisher,
407
- attestorAddress,
408
- slot,
409
- newGlobalVariables.timestamp,
410
- );
411
-
412
- // Enqueues block invalidation
413
- if (invalidateBlock && !this.config.skipInvalidateBlockAsProposer) {
414
- publisher.enqueueInvalidateBlock(invalidateBlock);
415
- }
416
-
417
- // Actual block building
418
- this.setState(SequencerState.INITIALIZING_PROPOSAL, slot);
419
- this.metrics.incOpenSlot(slot, proposer?.toString() ?? 'unknown');
420
- const block: L2Block | undefined = await this.tryBuildBlockAndEnqueuePublish(
359
+ // Create and return the checkpoint proposal job
360
+ return this.createCheckpointProposalJob(
421
361
  slot,
362
+ checkpointNumber,
363
+ syncedTo.blockNumber,
422
364
  proposer,
423
- newBlockNumber,
424
365
  publisher,
425
- newGlobalVariables,
426
- chainTipArchive,
366
+ attestorAddress,
427
367
  invalidateBlock,
428
368
  );
429
-
430
- // Wait until the voting promises have resolved, so all requests are enqueued
431
- await Promise.all(votesPromises);
432
-
433
- // In fisherman mode, we don't publish to L1
434
- if (this.config.fishermanMode) {
435
- // Clear pending requests
436
- publisher.clearPendingRequests();
437
-
438
- if (block) {
439
- this.log.info(`Validation block building SUCCEEDED for slot ${slot}`, {
440
- blockNumber: newBlockNumber,
441
- slot: Number(slot),
442
- archive: block.archive.toString(),
443
- txCount: block.body.txEffects.length,
444
- });
445
- this.lastBlockPublished = block;
446
- this.metrics.recordBlockProposalSuccess();
447
- } else {
448
- // Block building failed in fisherman mode
449
- this.log.warn(`Validation block building FAILED for slot ${slot}`, {
450
- blockNumber: newBlockNumber,
451
- slot: Number(slot),
452
- });
453
- this.metrics.recordBlockProposalFailed('block_build_failed');
454
- }
455
- } else {
456
- // Normal mode: send the tx to L1
457
- const l1Response = await publisher.sendRequests();
458
- const proposedBlock = l1Response?.successfulActions.find(a => a === 'propose');
459
- if (proposedBlock) {
460
- this.lastBlockPublished = block;
461
- this.emit('block-published', { blockNumber: newBlockNumber, slot: Number(slot) });
462
- await this.metrics.incFilledSlot(publisher.getSenderAddress().toString(), coinbase);
463
- } else if (block) {
464
- this.emit('block-publish-failed', l1Response ?? {});
465
- }
466
- }
467
-
468
- this.setState(SequencerState.IDLE, undefined);
469
369
  }
470
370
 
471
- /** Tries building a block proposal, and if successful, enqueues it for publishing. */
472
- private async tryBuildBlockAndEnqueuePublish(
371
+ protected createCheckpointProposalJob(
473
372
  slot: SlotNumber,
373
+ checkpointNumber: CheckpointNumber,
374
+ syncedToBlockNumber: BlockNumber,
474
375
  proposer: EthAddress | undefined,
475
- newBlockNumber: BlockNumber,
476
376
  publisher: SequencerPublisher,
477
- newGlobalVariables: GlobalVariables,
478
- chainTipArchive: Fr,
377
+ attestorAddress: EthAddress,
479
378
  invalidateBlock: InvalidateBlockRequest | undefined,
480
- ) {
481
- this.log.verbose(`Preparing proposal for block ${newBlockNumber} at slot ${slot}`, {
482
- proposer,
483
- publisher: publisher.getSenderAddress(),
484
- globalVariables: newGlobalVariables.toInspect(),
485
- chainTipArchive,
486
- blockNumber: newBlockNumber,
379
+ ): CheckpointProposalJob {
380
+ return new CheckpointProposalJob(
487
381
  slot,
488
- });
489
-
490
- const proposalHeader = CheckpointHeader.from({
491
- ...newGlobalVariables,
492
- timestamp: newGlobalVariables.timestamp,
493
- lastArchiveRoot: chainTipArchive,
494
- blockHeadersHash: Fr.ZERO,
495
- contentCommitment: ContentCommitment.empty(),
496
- totalManaUsed: Fr.ZERO,
497
- });
498
-
499
- let block: L2Block | undefined;
500
-
501
- const pendingTxCount = await this.p2pClient.getPendingTxCount();
502
- if (pendingTxCount >= this.minTxsPerBlock) {
503
- // We don't fetch exactly maxTxsPerBlock txs here because we may not need all of them if we hit a limit before,
504
- // and also we may need to fetch more if we don't have enough valid txs.
505
- const pendingTxs = this.p2pClient.iteratePendingTxs();
506
- try {
507
- block = await this.buildBlockAndEnqueuePublish(
508
- pendingTxs,
509
- proposalHeader,
510
- newGlobalVariables,
511
- proposer,
512
- invalidateBlock,
513
- publisher,
514
- );
515
- } catch (err: any) {
516
- this.emit('block-build-failed', { reason: err.message });
517
- if (err instanceof FormattedViemError) {
518
- this.log.verbose(`Unable to build/enqueue block ${err.message}`);
519
- } else {
520
- this.log.error(`Error building/enqueuing block`, err, { blockNumber: newBlockNumber, slot });
521
- }
522
- this.metrics.recordBlockProposalFailed(err.name || 'unknown_error');
523
- }
524
- } else {
525
- this.log.verbose(
526
- `Not enough txs to build block ${newBlockNumber} at slot ${slot} (got ${pendingTxCount} txs, need ${this.minTxsPerBlock})`,
527
- { chainTipArchive, blockNumber: newBlockNumber, slot },
528
- );
529
- this.emit('tx-count-check-failed', { minTxs: this.minTxsPerBlock, availableTxs: pendingTxCount });
530
- this.metrics.recordBlockProposalFailed('insufficient_txs');
531
- }
532
- return block;
533
- }
534
-
535
- @trackSpan('Sequencer.work')
536
- protected async safeWork() {
537
- try {
538
- await this.work();
539
- } catch (err) {
540
- if (err instanceof SequencerTooSlowError) {
541
- // Log as warn only if we had to abort halfway through the block proposal
542
- const logLvl = [SequencerState.INITIALIZING_PROPOSAL, SequencerState.PROPOSER_CHECK].includes(err.proposedState)
543
- ? ('debug' as const)
544
- : ('warn' as const);
545
- this.log[logLvl](err.message, { now: this.dateProvider.nowInSeconds() });
546
- } else {
547
- // Re-throw other errors
548
- throw err;
549
- }
550
- } finally {
551
- this.setState(SequencerState.IDLE, undefined);
552
- }
382
+ checkpointNumber,
383
+ syncedToBlockNumber,
384
+ proposer,
385
+ publisher,
386
+ attestorAddress,
387
+ invalidateBlock,
388
+ this.validatorClient,
389
+ this.globalsBuilder,
390
+ this.p2pClient,
391
+ this.worldState,
392
+ this.l1ToL2MessageSource,
393
+ this.checkpointsBuilder,
394
+ this.l1Constants,
395
+ this.config,
396
+ this.timetable,
397
+ this.slasherClient,
398
+ this.epochCache,
399
+ this.dateProvider,
400
+ this.metrics,
401
+ this,
402
+ this.setState.bind(this),
403
+ this.log,
404
+ );
553
405
  }
554
406
 
555
407
  /**
556
- * Sets the sequencer state and checks if we have enough time left in the slot to transition to the new state.
408
+ * Internal helper for setting the sequencer state and checks if we have enough time left in the slot to transition to the new state.
557
409
  * @param proposedState - The new state to transition to.
558
410
  * @param slotNumber - The current slot number.
559
411
  * @param force - Whether to force the transition even if the sequencer is stopped.
560
412
  */
561
- setState(proposedState: SequencerStateWithSlot, slotNumber: SlotNumber, opts?: { force?: boolean }): void;
562
- setState(
563
- proposedState: Exclude<SequencerState, SequencerStateWithSlot>,
564
- slotNumber?: undefined,
565
- opts?: { force?: boolean },
566
- ): void;
567
- setState(proposedState: SequencerState, slotNumber: SlotNumber | undefined, opts: { force?: boolean } = {}): void {
413
+ protected setState(
414
+ proposedState: SequencerState,
415
+ slotNumber: SlotNumber | undefined,
416
+ opts: { force?: boolean } = {},
417
+ ): void {
568
418
  if (this.state === SequencerState.STOPPING && proposedState !== SequencerState.STOPPED && !opts.force) {
569
419
  this.log.warn(`Cannot set sequencer to ${proposedState} as it is stopping.`);
570
420
  throw new SequencerInterruptedError();
@@ -590,352 +440,16 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
590
440
  oldState: this.state,
591
441
  newState: proposedState,
592
442
  secondsIntoSlot,
593
- slotNumber,
443
+ slot: slotNumber,
594
444
  });
595
445
  this.state = proposedState;
596
446
  }
597
447
 
598
- private async dropFailedTxsFromP2P(failedTxs: FailedTx[]) {
599
- if (failedTxs.length === 0) {
600
- return;
601
- }
602
- const failedTxData = failedTxs.map(fail => fail.tx);
603
- const failedTxHashes = failedTxData.map(tx => tx.getTxHash());
604
- this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
605
- await this.p2pClient.deleteTxs(failedTxHashes);
606
- }
607
-
608
- protected getBlockBuilderOptions(slot: SlotNumber): PublicProcessorLimits {
609
- // Deadline for processing depends on whether we're proposing a block
610
- const secondsIntoSlot = this.getSecondsIntoSlot(slot);
611
- const processingEndTimeWithinSlot = this.timetable.getBlockProposalExecTimeEnd(secondsIntoSlot);
612
-
613
- // Deadline is only set if enforceTimeTable is enabled.
614
- const deadline = this.enforceTimeTable
615
- ? new Date((this.getSlotStartBuildTimestamp(slot) + processingEndTimeWithinSlot) * 1000)
616
- : undefined;
617
- return {
618
- maxTransactions: this.maxTxsPerBlock,
619
- maxBlockSize: this.maxBlockSizeInBytes,
620
- maxBlockGas: this.maxBlockGas,
621
- maxBlobFields: BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB,
622
- deadline,
623
- };
624
- }
625
-
626
- /**
627
- * @notice Build and propose a block to the chain
628
- *
629
- * @dev MUST throw instead of exiting early to ensure that world-state
630
- * is being rolled back if the block is dropped.
631
- *
632
- * @param pendingTxs - Iterable of pending transactions to construct the block from
633
- * @param proposalHeader - The partial header constructed for the proposal
634
- * @param newGlobalVariables - The global variables for the new block
635
- * @param proposerAddress - The address of the proposer
636
- */
637
- @trackSpan('Sequencer.buildBlockAndEnqueuePublish', (_validTxs, _proposalHeader, newGlobalVariables) => ({
638
- [Attributes.BLOCK_NUMBER]: newGlobalVariables.blockNumber,
639
- }))
640
- private async buildBlockAndEnqueuePublish(
641
- pendingTxs: Iterable<Tx> | AsyncIterable<Tx>,
642
- proposalHeader: CheckpointHeader,
643
- newGlobalVariables: GlobalVariables,
644
- proposerAddress: EthAddress | undefined,
645
- invalidateBlock: InvalidateBlockRequest | undefined,
646
- publisher: SequencerPublisher,
647
- ): Promise<L2Block> {
648
- await publisher.validateBlockHeader(proposalHeader, invalidateBlock);
649
-
650
- const blockNumber = newGlobalVariables.blockNumber;
651
- const checkpointNumber = CheckpointNumber.fromBlockNumber(blockNumber);
652
- const slot = proposalHeader.slotNumber;
653
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
654
-
655
- const workTimer = new Timer();
656
- this.setState(SequencerState.CREATING_BLOCK, slot);
657
-
658
- try {
659
- const blockBuilderOptions = this.getBlockBuilderOptions(slot);
660
- const buildBlockRes = await this.blockBuilder.buildBlock(
661
- pendingTxs,
662
- l1ToL2Messages,
663
- newGlobalVariables,
664
- blockBuilderOptions,
665
- );
666
- const { publicGas, block, publicProcessorDuration, numTxs, numMsgs, blockBuildingTimer, usedTxs, failedTxs } =
667
- buildBlockRes;
668
- const blockBuildDuration = workTimer.ms();
669
- await this.dropFailedTxsFromP2P(failedTxs);
670
-
671
- const minTxsPerBlock = this.minTxsPerBlock;
672
- if (numTxs < minTxsPerBlock) {
673
- this.log.warn(
674
- `Block ${blockNumber} has too few txs to be proposed (got ${numTxs} but required ${minTxsPerBlock})`,
675
- { slot, blockNumber, numTxs },
676
- );
677
- throw new Error(`Block has too few successful txs to be proposed`);
678
- }
679
-
680
- // TODO(@PhilWindle) We should probably periodically check for things like another
681
- // block being published before ours instead of just waiting on our block
682
- await publisher.validateBlockHeader(block.getCheckpointHeader(), invalidateBlock);
683
-
684
- const blockStats: L2BlockBuiltStats = {
685
- eventName: 'l2-block-built',
686
- creator: proposerAddress?.toString() ?? publisher.getSenderAddress().toString(),
687
- duration: workTimer.ms(),
688
- publicProcessDuration: publicProcessorDuration,
689
- rollupCircuitsDuration: blockBuildingTimer.ms(),
690
- ...block.getStats(),
691
- };
692
-
693
- const blockHash = await block.hash();
694
- const txHashes = block.body.txEffects.map(tx => tx.txHash);
695
- this.log.info(
696
- `Built block ${block.number} for slot ${slot} with ${numTxs} txs and ${numMsgs} messages. ${
697
- publicGas.l2Gas / workTimer.s()
698
- } mana/s`,
699
- {
700
- blockHash,
701
- globalVariables: block.header.globalVariables.toInspect(),
702
- txHashes,
703
- ...blockStats,
704
- },
705
- );
706
-
707
- // In fisherman mode, skip attestation collection
708
- let attestationsAndSigners: CommitteeAttestationsAndSigners;
709
- if (this.config.fishermanMode) {
710
- this.log.debug('Skipping attestation collection');
711
- attestationsAndSigners = CommitteeAttestationsAndSigners.empty();
712
- } else {
713
- this.log.debug('Collecting attestations');
714
- attestationsAndSigners = await this.collectAttestations(block, usedTxs, proposerAddress);
715
- this.log.verbose(
716
- `Collected ${attestationsAndSigners.attestations.length} attestations for block ${blockNumber} at slot ${slot}`,
717
- { blockHash, blockNumber, slot },
718
- );
719
- }
720
-
721
- // In fisherman mode, skip attestation signing
722
- const attestationsAndSignersSignature =
723
- this.config.fishermanMode || !this.validatorClient
724
- ? Signature.empty()
725
- : await this.validatorClient.signAttestationsAndSigners(
726
- attestationsAndSigners,
727
- proposerAddress ?? publisher.getSenderAddress(),
728
- );
729
-
730
- await this.enqueuePublishL2Block(
731
- block,
732
- attestationsAndSigners,
733
- attestationsAndSignersSignature,
734
- invalidateBlock,
735
- publisher,
736
- );
737
- this.metrics.recordBuiltBlock(blockBuildDuration, publicGas.l2Gas);
738
- return block;
739
- } catch (err) {
740
- this.metrics.recordFailedBlock();
741
- throw err;
742
- }
743
- }
744
-
745
- @trackSpan('Sequencer.collectAttestations', (block, txHashes) => ({
746
- [Attributes.BLOCK_NUMBER]: block.number,
747
- [Attributes.BLOCK_ARCHIVE]: block.archive.toString(),
748
- [Attributes.BLOCK_TXS_COUNT]: txHashes.length,
749
- }))
750
- protected async collectAttestations(
751
- block: L2Block,
752
- txs: Tx[],
753
- proposerAddress: EthAddress | undefined,
754
- ): Promise<CommitteeAttestationsAndSigners> {
755
- const { committee, seed, epoch } = await this.epochCache.getCommittee(block.slot);
756
-
757
- // We checked above that the committee is defined, so this should never happen.
758
- if (!committee) {
759
- throw new Error('No committee when collecting attestations');
760
- }
761
-
762
- if (committee.length === 0) {
763
- this.log.verbose(`Attesting committee is empty`);
764
- return CommitteeAttestationsAndSigners.empty();
765
- } else {
766
- this.log.debug(`Attesting committee length is ${committee.length}`);
767
- }
768
-
769
- if (!this.validatorClient) {
770
- throw new Error('Missing validator client: Cannot collect attestations');
771
- }
772
-
773
- const numberOfRequiredAttestations = Math.floor((committee.length * 2) / 3) + 1;
774
-
775
- const slotNumber = block.header.globalVariables.slotNumber;
776
- this.setState(SequencerState.COLLECTING_ATTESTATIONS, slotNumber);
777
-
778
- this.log.debug('Creating block proposal for validators');
779
- const blockProposalOptions: BlockProposalOptions = {
780
- publishFullTxs: !!this.config.publishTxsWithProposals,
781
- broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
782
- };
783
- const proposal = await this.validatorClient.createBlockProposal(
784
- block.header.globalVariables.blockNumber,
785
- block.getCheckpointHeader(),
786
- block.archive.root,
787
- txs,
788
- proposerAddress,
789
- blockProposalOptions,
790
- );
791
-
792
- if (!proposal) {
793
- throw new Error(`Failed to create block proposal`);
794
- }
795
-
796
- if (this.config.skipCollectingAttestations) {
797
- this.log.warn('Skipping attestation collection as per config (attesting with own keys only)');
798
- const attestations = await this.validatorClient?.collectOwnAttestations(proposal);
799
- return new CommitteeAttestationsAndSigners(orderAttestations(attestations ?? [], committee));
800
- }
801
-
802
- this.log.debug('Broadcasting block proposal to validators');
803
- await this.validatorClient.broadcastBlockProposal(proposal);
804
-
805
- const attestationTimeAllowed = this.enforceTimeTable
806
- ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_BLOCK)!
807
- : this.aztecSlotDuration;
808
-
809
- this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
810
-
811
- const timer = new Timer();
812
- let collectedAttestationsCount: number = 0;
813
- try {
814
- const attestationDeadline = new Date(this.dateProvider.now() + attestationTimeAllowed * 1000);
815
- const attestations = await this.validatorClient.collectAttestations(
816
- proposal,
817
- numberOfRequiredAttestations,
818
- attestationDeadline,
819
- );
820
-
821
- collectedAttestationsCount = attestations.length;
822
-
823
- // note: the smart contract requires that the signatures are provided in the order of the committee
824
- const sorted = orderAttestations(attestations, committee);
825
-
826
- // manipulate the attestations if we've been configured to do so
827
- if (this.config.injectFakeAttestation || this.config.shuffleAttestationOrdering) {
828
- return this.manipulateAttestations(block, epoch, seed, committee, sorted);
829
- }
830
-
831
- return new CommitteeAttestationsAndSigners(sorted);
832
- } catch (err) {
833
- if (err && err instanceof AttestationTimeoutError) {
834
- collectedAttestationsCount = err.collectedCount;
835
- }
836
- throw err;
837
- } finally {
838
- this.metrics.recordCollectedAttestations(collectedAttestationsCount, timer.ms());
839
- }
840
- }
841
-
842
- /** Breaks the attestations before publishing based on attack configs */
843
- private manipulateAttestations(
844
- block: L2Block,
845
- epoch: EpochNumber,
846
- seed: bigint,
847
- committee: EthAddress[],
848
- attestations: CommitteeAttestation[],
849
- ) {
850
- // Compute the proposer index in the committee, since we dont want to tweak it.
851
- // Otherwise, the L1 rollup contract will reject the block outright.
852
- const proposerIndex = Number(
853
- this.epochCache.computeProposerIndex(block.slot, epoch, seed, BigInt(committee.length)),
854
- );
855
-
856
- if (this.config.injectFakeAttestation) {
857
- // Find non-empty attestations that are not from the proposer
858
- const nonProposerIndices: number[] = [];
859
- for (let i = 0; i < attestations.length; i++) {
860
- if (!attestations[i].signature.isEmpty() && i !== proposerIndex) {
861
- nonProposerIndices.push(i);
862
- }
863
- }
864
- if (nonProposerIndices.length > 0) {
865
- const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
866
- this.log.warn(`Injecting fake attestation in block ${block.number} at index ${targetIndex}`);
867
- unfreeze(attestations[targetIndex]).signature = Signature.random();
868
- }
869
- return new CommitteeAttestationsAndSigners(attestations);
870
- }
871
-
872
- if (this.config.shuffleAttestationOrdering) {
873
- this.log.warn(`Shuffling attestation ordering in block ${block.number} (proposer index ${proposerIndex})`);
874
-
875
- const shuffled = [...attestations];
876
- const [i, j] = [(proposerIndex + 1) % shuffled.length, (proposerIndex + 2) % shuffled.length];
877
- const valueI = shuffled[i];
878
- const valueJ = shuffled[j];
879
- shuffled[i] = valueJ;
880
- shuffled[j] = valueI;
881
-
882
- const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
883
- return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
884
- }
885
-
886
- return new CommitteeAttestationsAndSigners(attestations);
887
- }
888
-
889
- /**
890
- * Publishes the L2Block to the rollup contract.
891
- * @param block - The L2Block to be published.
892
- */
893
- @trackSpan('Sequencer.enqueuePublishL2Block', block => ({
894
- [Attributes.BLOCK_NUMBER]: block.number,
895
- }))
896
- protected async enqueuePublishL2Block(
897
- block: L2Block,
898
- attestationsAndSigners: CommitteeAttestationsAndSigners,
899
- attestationsAndSignersSignature: Signature,
900
- invalidateBlock: InvalidateBlockRequest | undefined,
901
- publisher: SequencerPublisher,
902
- ): Promise<void> {
903
- // Publishes new block to the network and awaits the tx to be mined
904
- this.setState(SequencerState.PUBLISHING_BLOCK, block.header.globalVariables.slotNumber);
905
-
906
- // Time out tx at the end of the slot
907
- const slot = block.header.globalVariables.slotNumber;
908
- const txTimeoutAt = new Date((this.getSlotStartBuildTimestamp(slot) + this.aztecSlotDuration) * 1000);
909
-
910
- const enqueued = await publisher.enqueueProposeL2Block(
911
- block,
912
- attestationsAndSigners,
913
- attestationsAndSignersSignature,
914
- {
915
- txTimeoutAt,
916
- forcePendingBlockNumber: invalidateBlock?.forcePendingBlockNumber,
917
- },
918
- );
919
-
920
- if (!enqueued) {
921
- throw new Error(`Failed to enqueue publish of block ${block.number}`);
922
- }
923
- }
924
-
925
448
  /**
926
449
  * Returns whether all dependencies have caught up.
927
450
  * We don't check against the previous block submitted since it may have been reorg'd out.
928
451
  */
929
- protected async checkSync(args: { ts: bigint; slot: SlotNumber }): Promise<
930
- | {
931
- block?: L2Block;
932
- blockNumber: BlockNumber;
933
- archive: Fr;
934
- l1Timestamp: bigint;
935
- pendingChainValidationStatus: ValidateBlockResult;
936
- }
937
- | undefined
938
- > {
452
+ protected async checkSync(args: { ts: bigint; slot: SlotNumber }): Promise<SequencerSyncCheckResult | undefined> {
939
453
  // Check that the archiver and dependencies have synced to the previous L1 slot at least
940
454
  // TODO(#14766): Archiver reports L1 timestamp based on L1 blocks seen, which means that a missed L1 block will
941
455
  // cause the archiver L1 timestamp to fall behind, and cause this sequencer to start processing one L1 slot later.
@@ -983,7 +497,7 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
983
497
  return { blockNumber: BlockNumber(INITIAL_L2_BLOCK_NUM - 1), archive, l1Timestamp, pendingChainValidationStatus };
984
498
  }
985
499
 
986
- const block = await this.l2BlockSource.getBlock(blockNumber);
500
+ const block = await this.l2BlockSource.getL2BlockNew(blockNumber);
987
501
  if (!block) {
988
502
  // this shouldn't really happen because a moment ago we checked that all components were in sync
989
503
  this.log.error(`Failed to get L2 block ${blockNumber} from the archiver with all components in sync`);
@@ -999,63 +513,6 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
999
513
  };
1000
514
  }
1001
515
 
1002
- /**
1003
- * Enqueues governance and slashing votes with the publisher. Does not block.
1004
- * @param publisher - The publisher to enqueue votes with
1005
- * @param attestorAddress - The attestor address to use for signing
1006
- * @param slot - The slot number
1007
- * @param timestamp - The timestamp for the votes
1008
- * @param context - Optional context for logging (e.g., block number)
1009
- * @returns A tuple of [governanceEnqueued, slashingEnqueued]
1010
- */
1011
- protected enqueueGovernanceAndSlashingVotes(
1012
- publisher: SequencerPublisher,
1013
- attestorAddress: EthAddress,
1014
- slot: SlotNumber,
1015
- timestamp: bigint,
1016
- ): [Promise<boolean> | undefined, Promise<boolean> | undefined] {
1017
- try {
1018
- const signerFn = (msg: TypedDataDefinition) =>
1019
- this.validatorClient!.signWithAddress(attestorAddress, msg).then(s => s.toString());
1020
-
1021
- const enqueueGovernancePromise =
1022
- this.governanceProposerPayload && !this.governanceProposerPayload.isZero()
1023
- ? publisher
1024
- .enqueueGovernanceCastSignal(this.governanceProposerPayload, slot, timestamp, attestorAddress, signerFn)
1025
- .catch(err => {
1026
- this.log.error(`Error enqueuing governance vote`, err, { slot });
1027
- return false;
1028
- })
1029
- : undefined;
1030
-
1031
- const enqueueSlashingPromise = this.slasherClient
1032
- ? this.slasherClient
1033
- .getProposerActions(slot)
1034
- .then(actions => {
1035
- // Record metrics for fisherman mode
1036
- if (this.config.fishermanMode && actions.length > 0) {
1037
- this.log.debug(`Fisherman mode: simulating ${actions.length} slashing action(s) for slot ${slot}`, {
1038
- slot,
1039
- actionCount: actions.length,
1040
- });
1041
- this.metrics.recordSlashingAttempt(actions.length);
1042
- }
1043
- // Enqueue the actions to fully simulate L1 tx building (they won't be sent in fisherman mode)
1044
- return publisher.enqueueSlashingActions(actions, slot, timestamp, attestorAddress, signerFn);
1045
- })
1046
- .catch(err => {
1047
- this.log.error(`Error enqueuing slashing actions`, err, { slot });
1048
- return false;
1049
- })
1050
- : undefined;
1051
-
1052
- return [enqueueGovernancePromise, enqueueSlashingPromise];
1053
- } catch (err) {
1054
- this.log.error(`Error enqueueing governance and slashing votes`, err);
1055
- return [undefined, undefined];
1056
- }
1057
- }
1058
-
1059
516
  /**
1060
517
  * Checks if we are the proposer for the next slot.
1061
518
  * @returns True if we can propose, and the proposer address (undefined if anyone can propose)
@@ -1083,7 +540,7 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
1083
540
  return [true, proposer];
1084
541
  }
1085
542
 
1086
- const validatorAddresses = this.validatorClient!.getValidatorAddresses();
543
+ const validatorAddresses = this.validatorClient.getValidatorAddresses();
1087
544
  const weAreProposer = validatorAddresses.some(addr => addr.equals(proposer));
1088
545
 
1089
546
  if (!weAreProposer) {
@@ -1099,29 +556,29 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
1099
556
  * This allows the sequencer to participate in governance/slashing votes even when it cannot build blocks.
1100
557
  */
1101
558
  protected async tryVoteWhenSyncFails(args: { slot: SlotNumber; ts: bigint }): Promise<void> {
1102
- const { slot, ts } = args;
559
+ const { slot } = args;
1103
560
 
1104
561
  // Prevent duplicate attempts in the same slot
1105
562
  if (this.lastSlotForVoteWhenSyncFailed === slot) {
1106
- this.log.debug(`Already attempted to vote in slot ${slot} (skipping)`);
563
+ this.log.trace(`Already attempted to vote in slot ${slot} (skipping)`);
1107
564
  return;
1108
565
  }
1109
566
 
1110
567
  // Check if we're past the max time for initializing a proposal
1111
568
  const secondsIntoSlot = this.getSecondsIntoSlot(slot);
1112
- const maxAllowedTime = this.timetable.getMaxAllowedTime(SequencerState.INITIALIZING_PROPOSAL);
569
+ const maxAllowedTime = this.timetable.getMaxAllowedTime(SequencerState.INITIALIZING_CHECKPOINT);
1113
570
 
1114
571
  // If we haven't exceeded the time limit for initializing a proposal, don't proceed with voting
1115
572
  // We use INITIALIZING_PROPOSAL time limit because if we're past that, we can't build a block anyway
1116
573
  if (maxAllowedTime === undefined || secondsIntoSlot <= maxAllowedTime) {
1117
- this.log.trace(`Not attempting to vote since there is still for block building`, {
574
+ this.log.trace(`Not attempting to vote since there is still time for block building`, {
1118
575
  secondsIntoSlot,
1119
576
  maxAllowedTime,
1120
577
  });
1121
578
  return;
1122
579
  }
1123
580
 
1124
- this.log.debug(`Sync for slot ${slot} failed, checking for voting opportunities`, {
581
+ this.log.trace(`Sync for slot ${slot} failed, checking for voting opportunities`, {
1125
582
  secondsIntoSlot,
1126
583
  maxAllowedTime,
1127
584
  });
@@ -1129,7 +586,7 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
1129
586
  // Check if we're a proposer or proposal is open
1130
587
  const [canPropose, proposer] = await this.checkCanPropose(slot);
1131
588
  if (!canPropose) {
1132
- this.log.debug(`Cannot vote in slot ${slot} since we are not a proposer`, { slot, proposer });
589
+ this.log.trace(`Cannot vote in slot ${slot} since we are not a proposer`, { slot, proposer });
1133
590
  return;
1134
591
  }
1135
592
 
@@ -1144,11 +601,22 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
1144
601
  slot,
1145
602
  });
1146
603
 
1147
- // Enqueue governance and slashing votes using the shared helper method
1148
- const votesPromises = this.enqueueGovernanceAndSlashingVotes(publisher, attestorAddress, slot, ts);
1149
- await Promise.all(votesPromises);
604
+ // Enqueue governance and slashing votes
605
+ const voter = new CheckpointVoter(
606
+ slot,
607
+ publisher,
608
+ attestorAddress,
609
+ this.validatorClient,
610
+ this.slasherClient,
611
+ this.l1Constants,
612
+ this.config,
613
+ this.metrics,
614
+ this.log,
615
+ );
616
+ const votesPromises = voter.enqueueVotes();
617
+ const votes = await Promise.all(votesPromises);
1150
618
 
1151
- if (votesPromises.every(p => !p)) {
619
+ if (votes.every(p => !p)) {
1152
620
  this.log.debug(`No votes to enqueue for slot ${slot}`);
1153
621
  return;
1154
622
  }
@@ -1164,7 +632,7 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
1164
632
  * and if they fail, any sequencer will try as well.
1165
633
  */
1166
634
  protected async considerInvalidatingBlock(
1167
- syncedTo: NonNullable<Awaited<ReturnType<Sequencer['checkSync']>>>,
635
+ syncedTo: SequencerSyncCheckResult,
1168
636
  currentSlot: SlotNumber,
1169
637
  ): Promise<void> {
1170
638
  const { pendingChainValidationStatus, l1Timestamp } = syncedTo;
@@ -1175,7 +643,7 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
1175
643
  const invalidBlockNumber = pendingChainValidationStatus.block.blockNumber;
1176
644
  const invalidBlockTimestamp = pendingChainValidationStatus.block.timestamp;
1177
645
  const timeSinceChainInvalid = this.dateProvider.nowInSeconds() - Number(invalidBlockTimestamp);
1178
- const ourValidatorAddresses = this.validatorClient!.getValidatorAddresses();
646
+ const ourValidatorAddresses = this.validatorClient.getValidatorAddresses();
1179
647
 
1180
648
  const { secondsBeforeInvalidatingBlockAsCommitteeMember, secondsBeforeInvalidatingBlockAsNonCommitteeMember } =
1181
649
  this.config;
@@ -1252,6 +720,35 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
1252
720
  }
1253
721
  }
1254
722
 
723
+ private logStrategyComparison(epoch: EpochNumber, publisher: SequencerPublisher): void {
724
+ const feeAnalyzer = publisher.getL1FeeAnalyzer();
725
+ if (!feeAnalyzer) {
726
+ return;
727
+ }
728
+
729
+ const comparison = feeAnalyzer.getStrategyComparison();
730
+ if (comparison.length === 0) {
731
+ this.log.debug(`No strategy data available yet for epoch ${epoch}`);
732
+ return;
733
+ }
734
+
735
+ this.log.info(`L1 Fee Strategy Performance Report - End of Epoch ${epoch}`, {
736
+ epoch: Number(epoch),
737
+ totalAnalyses: comparison[0]?.totalAnalyses,
738
+ strategies: comparison.map(s => ({
739
+ id: s.strategyId,
740
+ name: s.strategyName,
741
+ inclusionRate: `${(s.inclusionRate * 100).toFixed(1)}%`,
742
+ inclusionCount: `${s.inclusionCount}/${s.totalAnalyses}`,
743
+ avgCostEth: s.avgEstimatedCostEth.toFixed(6),
744
+ totalCostEth: s.totalEstimatedCostEth.toFixed(6),
745
+ avgOverpaymentEth: s.avgOverpaymentEth.toFixed(6),
746
+ totalOverpaymentEth: s.totalOverpaymentEth.toFixed(6),
747
+ avgPriorityFeeDeltaGwei: s.avgPriorityFeeDeltaGwei.toFixed(2),
748
+ })),
749
+ });
750
+ }
751
+
1255
752
  private getSlotStartBuildTimestamp(slotNumber: SlotNumber): number {
1256
753
  return getSlotStartBuildTimestamp(slotNumber, this.l1Constants);
1257
754
  }
@@ -1261,15 +758,39 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
1261
758
  return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
1262
759
  }
1263
760
 
1264
- get aztecSlotDuration() {
761
+ public get aztecSlotDuration() {
1265
762
  return this.l1Constants.slotDuration;
1266
763
  }
1267
764
 
1268
- get maxL2BlockGas(): number | undefined {
765
+ public get maxL2BlockGas(): number | undefined {
1269
766
  return this.config.maxL2BlockGas;
1270
767
  }
1271
768
 
1272
769
  public getSlasherClient(): SlasherClientInterface | undefined {
1273
770
  return this.slasherClient;
1274
771
  }
772
+
773
+ public get tracer(): Tracer {
774
+ return this.metrics.tracer;
775
+ }
776
+
777
+ public getValidatorAddresses() {
778
+ return this.validatorClient?.getValidatorAddresses();
779
+ }
780
+
781
+ public getConfig() {
782
+ return this.config;
783
+ }
784
+
785
+ private get l1PublishingTime(): number {
786
+ return this.config.l1PublishingTime ?? this.l1Constants.ethereumSlotDuration;
787
+ }
1275
788
  }
789
+
790
+ type SequencerSyncCheckResult = {
791
+ block?: L2BlockNew;
792
+ blockNumber: BlockNumber;
793
+ archive: Fr;
794
+ l1Timestamp: bigint;
795
+ pendingChainValidationStatus: ValidateBlockResult;
796
+ };