@aztec/sequencer-client 0.0.1-commit.b655e406 → 0.0.1-commit.d1f2d6c

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