@aztec/sequencer-client 0.0.1-fake-ceab37513c → 0.0.6-commit.a2d1860fe9

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