@aztec/sequencer-client 0.0.0-test.1 → 0.0.1-commit.0b941701

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 (135) hide show
  1. package/dest/client/index.d.ts +1 -1
  2. package/dest/client/sequencer-client.d.ts +31 -31
  3. package/dest/client/sequencer-client.d.ts.map +1 -1
  4. package/dest/client/sequencer-client.js +82 -60
  5. package/dest/config.d.ts +15 -16
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +118 -70
  8. package/dest/global_variable_builder/global_builder.d.ts +26 -15
  9. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  10. package/dest/global_variable_builder/global_builder.js +62 -44
  11. package/dest/global_variable_builder/index.d.ts +1 -1
  12. package/dest/index.d.ts +2 -4
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +1 -3
  15. package/dest/publisher/config.d.ts +15 -12
  16. package/dest/publisher/config.d.ts.map +1 -1
  17. package/dest/publisher/config.js +32 -19
  18. package/dest/publisher/index.d.ts +3 -1
  19. package/dest/publisher/index.d.ts.map +1 -1
  20. package/dest/publisher/index.js +3 -0
  21. package/dest/publisher/sequencer-publisher-factory.d.ts +44 -0
  22. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -0
  23. package/dest/publisher/sequencer-publisher-factory.js +51 -0
  24. package/dest/publisher/sequencer-publisher-metrics.d.ts +5 -4
  25. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  26. package/dest/publisher/sequencer-publisher-metrics.js +26 -62
  27. package/dest/publisher/sequencer-publisher.d.ts +134 -87
  28. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  29. package/dest/publisher/sequencer-publisher.js +1146 -249
  30. package/dest/sequencer/checkpoint_proposal_job.d.ts +79 -0
  31. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
  32. package/dest/sequencer/checkpoint_proposal_job.js +1165 -0
  33. package/dest/sequencer/checkpoint_voter.d.ts +35 -0
  34. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
  35. package/dest/sequencer/checkpoint_voter.js +109 -0
  36. package/dest/sequencer/config.d.ts +7 -1
  37. package/dest/sequencer/config.d.ts.map +1 -1
  38. package/dest/sequencer/errors.d.ts +11 -0
  39. package/dest/sequencer/errors.d.ts.map +1 -0
  40. package/dest/sequencer/errors.js +15 -0
  41. package/dest/sequencer/events.d.ts +46 -0
  42. package/dest/sequencer/events.d.ts.map +1 -0
  43. package/dest/sequencer/events.js +1 -0
  44. package/dest/sequencer/index.d.ts +4 -2
  45. package/dest/sequencer/index.d.ts.map +1 -1
  46. package/dest/sequencer/index.js +3 -1
  47. package/dest/sequencer/metrics.d.ts +48 -12
  48. package/dest/sequencer/metrics.d.ts.map +1 -1
  49. package/dest/sequencer/metrics.js +204 -69
  50. package/dest/sequencer/sequencer.d.ts +144 -137
  51. package/dest/sequencer/sequencer.d.ts.map +1 -1
  52. package/dest/sequencer/sequencer.js +967 -525
  53. package/dest/sequencer/timetable.d.ts +76 -24
  54. package/dest/sequencer/timetable.d.ts.map +1 -1
  55. package/dest/sequencer/timetable.js +177 -61
  56. package/dest/sequencer/types.d.ts +3 -0
  57. package/dest/sequencer/types.d.ts.map +1 -0
  58. package/dest/sequencer/types.js +1 -0
  59. package/dest/sequencer/utils.d.ts +20 -38
  60. package/dest/sequencer/utils.d.ts.map +1 -1
  61. package/dest/sequencer/utils.js +12 -47
  62. package/dest/test/index.d.ts +9 -1
  63. package/dest/test/index.d.ts.map +1 -1
  64. package/dest/test/index.js +0 -4
  65. package/dest/test/mock_checkpoint_builder.d.ts +95 -0
  66. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
  67. package/dest/test/mock_checkpoint_builder.js +222 -0
  68. package/dest/test/utils.d.ts +53 -0
  69. package/dest/test/utils.d.ts.map +1 -0
  70. package/dest/test/utils.js +103 -0
  71. package/package.json +47 -45
  72. package/src/client/sequencer-client.ts +106 -107
  73. package/src/config.ts +131 -81
  74. package/src/global_variable_builder/global_builder.ts +84 -55
  75. package/src/index.ts +1 -3
  76. package/src/publisher/config.ts +45 -32
  77. package/src/publisher/index.ts +4 -0
  78. package/src/publisher/sequencer-publisher-factory.ts +92 -0
  79. package/src/publisher/sequencer-publisher-metrics.ts +30 -64
  80. package/src/publisher/sequencer-publisher.ts +967 -295
  81. package/src/sequencer/README.md +531 -0
  82. package/src/sequencer/checkpoint_proposal_job.ts +845 -0
  83. package/src/sequencer/checkpoint_voter.ts +130 -0
  84. package/src/sequencer/config.ts +8 -0
  85. package/src/sequencer/errors.ts +21 -0
  86. package/src/sequencer/events.ts +27 -0
  87. package/src/sequencer/index.ts +3 -1
  88. package/src/sequencer/metrics.ts +269 -72
  89. package/src/sequencer/sequencer.ts +708 -588
  90. package/src/sequencer/timetable.ts +221 -62
  91. package/src/sequencer/types.ts +6 -0
  92. package/src/sequencer/utils.ts +28 -60
  93. package/src/test/index.ts +12 -4
  94. package/src/test/mock_checkpoint_builder.ts +311 -0
  95. package/src/test/utils.ts +164 -0
  96. package/dest/sequencer/allowed.d.ts +0 -3
  97. package/dest/sequencer/allowed.d.ts.map +0 -1
  98. package/dest/sequencer/allowed.js +0 -27
  99. package/dest/slasher/factory.d.ts +0 -7
  100. package/dest/slasher/factory.d.ts.map +0 -1
  101. package/dest/slasher/factory.js +0 -8
  102. package/dest/slasher/index.d.ts +0 -3
  103. package/dest/slasher/index.d.ts.map +0 -1
  104. package/dest/slasher/index.js +0 -2
  105. package/dest/slasher/slasher_client.d.ts +0 -75
  106. package/dest/slasher/slasher_client.d.ts.map +0 -1
  107. package/dest/slasher/slasher_client.js +0 -132
  108. package/dest/tx_validator/archive_cache.d.ts +0 -14
  109. package/dest/tx_validator/archive_cache.d.ts.map +0 -1
  110. package/dest/tx_validator/archive_cache.js +0 -22
  111. package/dest/tx_validator/gas_validator.d.ts +0 -14
  112. package/dest/tx_validator/gas_validator.d.ts.map +0 -1
  113. package/dest/tx_validator/gas_validator.js +0 -78
  114. package/dest/tx_validator/nullifier_cache.d.ts +0 -16
  115. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  116. package/dest/tx_validator/nullifier_cache.js +0 -24
  117. package/dest/tx_validator/phases_validator.d.ts +0 -12
  118. package/dest/tx_validator/phases_validator.d.ts.map +0 -1
  119. package/dest/tx_validator/phases_validator.js +0 -80
  120. package/dest/tx_validator/test_utils.d.ts +0 -23
  121. package/dest/tx_validator/test_utils.d.ts.map +0 -1
  122. package/dest/tx_validator/test_utils.js +0 -26
  123. package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
  124. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  125. package/dest/tx_validator/tx_validator_factory.js +0 -50
  126. package/src/sequencer/allowed.ts +0 -36
  127. package/src/slasher/factory.ts +0 -15
  128. package/src/slasher/index.ts +0 -2
  129. package/src/slasher/slasher_client.ts +0 -193
  130. package/src/tx_validator/archive_cache.ts +0 -28
  131. package/src/tx_validator/gas_validator.ts +0 -101
  132. package/src/tx_validator/nullifier_cache.ts +0 -30
  133. package/src/tx_validator/phases_validator.ts +0 -98
  134. package/src/tx_validator/test_utils.ts +0 -48
  135. package/src/tx_validator/tx_validator_factory.ts +0 -120
@@ -1,759 +1,879 @@
1
- import type { L2Block } from '@aztec/aztec.js';
1
+ import { getKzg } from '@aztec/blob-lib';
2
2
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
3
- import { omit } from '@aztec/foundation/collection';
3
+ import type { EpochCache } from '@aztec/epoch-cache';
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';
4
8
  import { EthAddress } from '@aztec/foundation/eth-address';
5
- import type { Signature } from '@aztec/foundation/eth-signature';
6
- import { Fr } from '@aztec/foundation/fields';
7
9
  import { createLogger } from '@aztec/foundation/log';
8
10
  import { RunningPromise } from '@aztec/foundation/running-promise';
9
- import { type DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
11
+ import type { DateProvider } from '@aztec/foundation/timer';
12
+ import type { TypedEventEmitter } from '@aztec/foundation/types';
10
13
  import type { P2P } from '@aztec/p2p';
11
- import type { BlockBuilderFactory } from '@aztec/prover-client/block-builder';
12
- import type { PublicProcessorFactory } from '@aztec/simulator/server';
13
- import { AztecAddress } from '@aztec/stdlib/aztec-address';
14
- import type { L2BlockSource } from '@aztec/stdlib/block';
15
- import type { ContractDataSource } from '@aztec/stdlib/contract';
16
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
17
- import { Gas } from '@aztec/stdlib/gas';
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';
18
18
  import {
19
- type AllowedElement,
19
+ type ResolvedSequencerConfig,
20
+ type SequencerConfig,
20
21
  SequencerConfigSchema,
21
22
  type WorldStateSynchronizer,
22
- type WorldStateSynchronizerStatus,
23
23
  } from '@aztec/stdlib/interfaces/server';
24
24
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
25
25
  import { pickFromSchema } from '@aztec/stdlib/schemas';
26
- import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
27
- import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
28
- import {
29
- BlockHeader,
30
- ContentCommitment,
31
- type GlobalVariables,
32
- StateReference,
33
- Tx,
34
- type TxHash,
35
- } from '@aztec/stdlib/tx';
26
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
36
27
  import { Attributes, type TelemetryClient, type Tracer, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
37
- import type { ValidatorClient } from '@aztec/validator-client';
28
+ import { FullNodeCheckpointsBuilder, type ValidatorClient } from '@aztec/validator-client';
29
+
30
+ import EventEmitter from 'node:events';
38
31
 
32
+ import { DefaultSequencerConfig } from '../config.js';
39
33
  import type { GlobalVariableBuilder } from '../global_variable_builder/global_builder.js';
40
- import { type SequencerPublisher, VoteType } from '../publisher/sequencer-publisher.js';
41
- import type { SlasherClient } from '../slasher/slasher_client.js';
42
- import { createValidatorsForBlockBuilding } from '../tx_validator/tx_validator_factory.js';
43
- import { getDefaultAllowedSetupFunctions } from './allowed.js';
44
- import type { SequencerConfig } from './config.js';
34
+ import type { SequencerPublisherFactory } from '../publisher/sequencer-publisher-factory.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';
38
+ import { SequencerInterruptedError, SequencerTooSlowError } from './errors.js';
39
+ import type { SequencerEvents } from './events.js';
45
40
  import { SequencerMetrics } from './metrics.js';
46
- import { SequencerTimetable, SequencerTooSlowError } from './timetable.js';
47
- import { SequencerState, orderAttestations } from './utils.js';
41
+ import { SequencerTimetable } from './timetable.js';
42
+ import type { SequencerRollupConstants } from './types.js';
43
+ import { SequencerState } from './utils.js';
48
44
 
49
45
  export { SequencerState };
50
46
 
51
- type SequencerRollupConstants = Pick<L1RollupConstants, 'ethereumSlotDuration' | 'l1GenesisTime' | 'slotDuration'>;
52
-
53
47
  /**
54
48
  * Sequencer client
55
- * - Wins a period of time to become the sequencer (depending on finalized protocol).
56
- * - Chooses a set of txs from the tx pool to be in the rollup.
57
- * - Simulate the rollup of txs.
58
- * - Adds proof requests to the request pool (not for this milestone).
59
- * - Receives results to those proofs from the network (repeats as necessary) (not for this milestone).
60
- * - 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
61
54
  */
62
- export class Sequencer {
55
+ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<SequencerEvents>) {
63
56
  private runningPromise?: RunningPromise;
64
- private pollingIntervalMs: number = 1000;
65
- private maxTxsPerBlock = 32;
66
- private minTxsPerBlock = 1;
67
- private maxL1TxInclusionTimeIntoSlot = 0;
68
- // TODO: zero values should not be allowed for the following 2 values in PROD
69
- private _coinbase = EthAddress.ZERO;
70
- private _feeRecipient = AztecAddress.ZERO;
71
57
  private state = SequencerState.STOPPED;
72
- private allowedInSetup: AllowedElement[] = [];
73
- private maxBlockSizeInBytes: number = 1024 * 1024;
74
- private maxBlockGas: Gas = new Gas(100e9, 100e9);
75
58
  private metrics: SequencerMetrics;
76
- private isFlushing: boolean = false;
59
+
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 triggered a checkpoint proposal job, to prevent duplicate attempts. */
64
+ private lastSlotForCheckpointProposalJob: SlotNumber | undefined;
65
+
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;
77
71
 
78
72
  /** The maximum number of seconds that the sequencer can be into a slot to transition to a particular state. */
79
73
  protected timetable!: SequencerTimetable;
80
74
 
81
- protected enforceTimeTable: boolean = false;
75
+ // This shouldn't be here as this gets re-created each time we build/propose a block.
76
+ // But we have a number of tests that abuse/rely on this class having a permanent publisher.
77
+ // As long as those tests only configure a single publisher they will continue to work.
78
+ // This will get re-assigned every time the sequencer goes to build a new block to a publisher that is valid
79
+ // for the block proposer.
80
+ // TODO(palla/mbps): Remove this field and fix tests
81
+ protected publisher: SequencerPublisher | undefined;
82
+
83
+ /** Config for the sequencer */
84
+ protected config: ResolvedSequencerConfig = DefaultSequencerConfig;
82
85
 
83
86
  constructor(
84
- protected publisher: SequencerPublisher,
85
- protected validatorClient: ValidatorClient | undefined, // During migration the validator client can be inactive
87
+ protected publisherFactory: SequencerPublisherFactory,
88
+ protected validatorClient: ValidatorClient,
86
89
  protected globalsBuilder: GlobalVariableBuilder,
87
90
  protected p2pClient: P2P,
88
91
  protected worldState: WorldStateSynchronizer,
89
- protected slasherClient: SlasherClient,
90
- protected blockBuilderFactory: BlockBuilderFactory,
91
- protected l2BlockSource: L2BlockSource,
92
+ protected slasherClient: SlasherClientInterface | undefined,
93
+ protected l2BlockSource: L2BlockSource & L2BlockSink,
92
94
  protected l1ToL2MessageSource: L1ToL2MessageSource,
93
- protected publicProcessorFactory: PublicProcessorFactory,
94
- protected contractDataSource: ContractDataSource,
95
+ protected checkpointsBuilder: FullNodeCheckpointsBuilder,
95
96
  protected l1Constants: SequencerRollupConstants,
96
97
  protected dateProvider: DateProvider,
97
- protected config: SequencerConfig = {},
98
- telemetry: TelemetryClient = getTelemetryClient(),
98
+ protected epochCache: EpochCache,
99
+ protected rollupContract: RollupContract,
100
+ config: SequencerConfig,
101
+ protected telemetry: TelemetryClient = getTelemetryClient(),
99
102
  protected log = createLogger('sequencer'),
100
103
  ) {
101
- this.metrics = new SequencerMetrics(telemetry, () => this.state, 'Sequencer');
102
-
103
- // Register the block builder with the validator client for re-execution
104
- this.validatorClient?.registerBlockBuilder(this.buildBlock.bind(this));
105
-
106
- // Register the slasher on the publisher to fetch slashing payloads
107
- this.publisher.registerSlashPayloadGetter(this.slasherClient.getSlashPayload.bind(this.slasherClient));
108
- }
109
-
110
- get tracer(): Tracer {
111
- return this.metrics.tracer;
112
- }
113
-
114
- /**
115
- * Updates sequencer config.
116
- * @param config - New parameters.
117
- */
118
- public async updateConfig(config: SequencerConfig) {
119
- this.log.info(`Sequencer config set`, omit(pickFromSchema(config, SequencerConfigSchema), 'allowedInSetup'));
104
+ super();
120
105
 
121
- if (config.transactionPollingIntervalMS !== undefined) {
122
- this.pollingIntervalMs = config.transactionPollingIntervalMS;
123
- }
124
- if (config.maxTxsPerBlock !== undefined) {
125
- this.maxTxsPerBlock = config.maxTxsPerBlock;
126
- }
127
- if (config.minTxsPerBlock !== undefined) {
128
- this.minTxsPerBlock = config.minTxsPerBlock;
129
- }
130
- if (config.maxDABlockGas !== undefined) {
131
- this.maxBlockGas = new Gas(config.maxDABlockGas, this.maxBlockGas.l2Gas);
132
- }
133
- if (config.maxL2BlockGas !== undefined) {
134
- this.maxBlockGas = new Gas(this.maxBlockGas.daGas, config.maxL2BlockGas);
135
- }
136
- if (config.coinbase) {
137
- this._coinbase = config.coinbase;
138
- }
139
- if (config.feeRecipient) {
140
- this._feeRecipient = config.feeRecipient;
141
- }
142
- if (config.allowedInSetup) {
143
- this.allowedInSetup = config.allowedInSetup;
144
- } else {
145
- this.allowedInSetup = await getDefaultAllowedSetupFunctions();
146
- }
147
- if (config.maxBlockSizeInBytes !== undefined) {
148
- this.maxBlockSizeInBytes = config.maxBlockSizeInBytes;
149
- }
150
- if (config.governanceProposerPayload) {
151
- this.publisher.setGovernancePayload(config.governanceProposerPayload);
152
- }
153
- if (config.maxL1TxInclusionTimeIntoSlot !== undefined) {
154
- this.maxL1TxInclusionTimeIntoSlot = config.maxL1TxInclusionTimeIntoSlot;
106
+ // Add [FISHERMAN] prefix to logger if in fisherman mode
107
+ if (config.fishermanMode) {
108
+ this.log = log.createChild('[FISHERMAN]');
155
109
  }
156
- if (config.enforceTimeTable !== undefined) {
157
- this.enforceTimeTable = config.enforceTimeTable;
158
- }
159
-
160
- this.setTimeTable();
161
110
 
162
- // TODO: Just read everything from the config object as needed instead of copying everything into local vars.
163
- this.config = config;
111
+ this.metrics = new SequencerMetrics(telemetry, this.rollupContract, 'Sequencer');
112
+ this.updateConfig(config);
164
113
  }
165
114
 
166
- 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);
167
120
  this.timetable = new SequencerTimetable(
168
- this.l1Constants.ethereumSlotDuration,
169
- this.aztecSlotDuration,
170
- this.maxL1TxInclusionTimeIntoSlot,
171
- this.enforceTimeTable,
121
+ {
122
+ ethereumSlotDuration: this.l1Constants.ethereumSlotDuration,
123
+ aztecSlotDuration: this.aztecSlotDuration,
124
+ l1PublishingTime: this.l1PublishingTime,
125
+ p2pPropagationTime: this.config.attestationPropagationTime,
126
+ blockDurationMs: this.config.blockDurationMs,
127
+ enforce: this.config.enforceTimeTable,
128
+ },
172
129
  this.metrics,
173
130
  this.log,
174
131
  );
175
- this.log.verbose(`Sequencer timetable updated`, { enforceTimeTable: this.enforceTimeTable });
176
132
  }
177
133
 
178
- /**
179
- * Starts the sequencer and moves to IDLE state.
180
- */
181
- public async start() {
182
- await this.updateConfig(this.config);
183
- this.runningPromise = new RunningPromise(this.work.bind(this), this.log, this.pollingIntervalMs);
184
- this.setState(SequencerState.IDLE, 0n, true /** force */);
134
+ /** Initializes the sequencer (precomputes tables and creates a publisher). Takes about 3s. */
135
+ public async init() {
136
+ getKzg();
137
+ this.publisher = (await this.publisherFactory.create(undefined)).publisher;
138
+ }
139
+
140
+ /** Starts the sequencer and moves to IDLE state. */
141
+ public start() {
142
+ this.runningPromise = new RunningPromise(
143
+ this.safeWork.bind(this),
144
+ this.log,
145
+ this.config.sequencerPollingIntervalMS,
146
+ );
147
+ this.setState(SequencerState.IDLE, undefined, { force: true });
185
148
  this.runningPromise.start();
186
- this.log.info(`Sequencer started with address ${this.publisher.getSenderAddress().toString()}`);
149
+ this.log.info('Started sequencer');
187
150
  }
188
151
 
189
- /**
190
- * Stops the sequencer from processing txs and moves to STOPPED state.
191
- */
152
+ /** Stops the sequencer from building blocks and moves to STOPPED state. */
192
153
  public async stop(): Promise<void> {
193
- this.log.debug(`Stopping sequencer`);
194
- await this.validatorClient?.stop();
154
+ this.log.info(`Stopping sequencer`);
155
+ this.setState(SequencerState.STOPPING, undefined, { force: true });
156
+ this.publisher?.interrupt();
195
157
  await this.runningPromise?.stop();
196
- this.slasherClient.stop();
197
- this.publisher.interrupt();
198
- this.setState(SequencerState.STOPPED, 0n, true /** force */);
158
+ this.setState(SequencerState.STOPPED, undefined, { force: true });
199
159
  this.log.info('Stopped sequencer');
200
160
  }
201
161
 
202
- /**
203
- * Starts a previously stopped sequencer.
204
- */
205
- public restart() {
206
- this.log.info('Restarting sequencer');
207
- this.publisher.restart();
208
- this.runningPromise!.start();
209
- this.setState(SequencerState.IDLE, 0n, true /** force */);
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
+ }
210
184
  }
211
185
 
212
- /**
213
- * Returns the current state of the sequencer.
214
- * @returns An object with a state entry with one of SequencerState.
215
- */
186
+ /** Returns the current state of the sequencer. */
216
187
  public status() {
217
188
  return { state: this.state };
218
189
  }
219
190
 
220
- /** Forces the sequencer to bypass all time and tx count checks for the next block and build anyway. */
221
- public flush() {
222
- this.isFlushing = true;
223
- }
224
-
225
191
  /**
226
- * @notice Performs most of the sequencer duties:
227
- * - Checks if we are up to date
228
- * - If we are and we are the sequencer, collect txs and build a block
229
- * - Collect attestations for the block
230
- * - Submit block
231
- * - 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
232
198
  */
233
- protected async doRealWork() {
234
- this.setState(SequencerState.SYNCHRONIZING, 0n);
235
- // Update state when the previous block has been synced
236
- const chainTip = await this.getChainTip();
237
- // Do not go forward with new block if the previous one has not been mined and processed
238
- if (!chainTip) {
199
+ @trackSpan('Sequencer.work')
200
+ protected async work() {
201
+ this.setState(SequencerState.SYNCHRONIZING, undefined);
202
+ const { slot, ts, now, epoch } = this.epochCache.getEpochAndSlotInNextL1Slot();
203
+
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) {
239
207
  return;
240
208
  }
241
209
 
242
- this.setState(SequencerState.PROPOSER_CHECK, 0n);
210
+ // Execute the checkpoint proposal job
211
+ const checkpoint = await checkpointProposalJob.execute();
243
212
 
244
- const newBlockNumber = chainTip.blockNumber + 1;
245
-
246
- // If we cannot find a tip archive, assume genesis.
247
- const chainTipArchive = chainTip.archive;
248
-
249
- const slot = await this.slotForProposal(chainTipArchive.toBuffer(), BigInt(newBlockNumber));
250
- if (!slot) {
251
- this.log.debug(`Cannot propose block ${newBlockNumber}`);
252
- return;
213
+ // Update last checkpoint proposed (currently unused)
214
+ if (checkpoint) {
215
+ this.lastCheckpointProposed = checkpoint;
253
216
  }
254
217
 
255
- this.log.debug(`Can propose block ${newBlockNumber} at slot ${slot}`);
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
+ }
256
226
 
257
- const newGlobalVariables = await this.globalsBuilder.buildGlobalVariables(
258
- new Fr(newBlockNumber),
259
- this._coinbase,
260
- this._feeRecipient,
261
- slot,
262
- );
227
+ return checkpoint;
228
+ }
263
229
 
264
- const enqueueGovernanceVotePromise = this.publisher.enqueueCastVote(
265
- slot,
266
- newGlobalVariables.timestamp.toBigInt(),
267
- VoteType.GOVERNANCE,
268
- );
269
- const enqueueSlashingVotePromise = this.publisher.enqueueCastVote(
270
- slot,
271
- newGlobalVariables.timestamp.toBigInt(),
272
- VoteType.SLASHING,
273
- );
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
+ }
274
253
 
275
- this.setState(SequencerState.INITIALIZING_PROPOSAL, slot);
276
- this.log.verbose(`Preparing proposal for block ${newBlockNumber} at slot ${slot}`, {
277
- chainTipArchive,
278
- blockNumber: newBlockNumber,
279
- slot,
280
- });
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
+ }
281
259
 
282
- // If I created a "partial" header here that should make our job much easier.
283
- const proposalHeader = new BlockHeader(
284
- new AppendOnlyTreeSnapshot(chainTipArchive, 1),
285
- ContentCommitment.empty(),
286
- StateReference.empty(),
287
- newGlobalVariables,
288
- Fr.ZERO,
289
- Fr.ZERO,
290
- );
260
+ // Check all components are synced to latest as seen by the archiver (queries all subsystems)
261
+ const syncedTo = await this.checkSync({ ts, slot });
262
+ if (!syncedTo) {
263
+ await this.tryVoteWhenSyncFails({ slot, ts });
264
+ return undefined;
265
+ }
291
266
 
292
- let finishedFlushing = false;
293
- const pendingTxCount = await this.p2pClient.getPendingTxCount();
294
- if (pendingTxCount >= this.minTxsPerBlock || this.isFlushing) {
295
- // We don't fetch exactly maxTxsPerBlock txs here because we may not need all of them if we hit a limit before,
296
- // and also we may need to fetch more if we don't have enough valid txs.
297
- const pendingTxs = this.p2pClient.iteratePendingTxs();
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);
298
270
 
299
- await this.buildBlockAndEnqueuePublish(pendingTxs, proposalHeader).catch(err => {
300
- this.log.error(`Error building/enqueuing block`, err, { blockNumber: newBlockNumber, slot });
301
- });
302
- finishedFlushing = true;
303
- } else {
304
- this.log.debug(
305
- `Not enough txs to build block ${newBlockNumber} at slot ${slot}: got ${pendingTxCount} txs, need ${this.minTxsPerBlock}`,
306
- );
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;
307
284
  }
308
285
 
309
- await enqueueGovernanceVotePromise.catch(err => {
310
- this.log.error(`Error enqueuing governance vote`, err, { blockNumber: newBlockNumber, slot });
311
- });
312
- await enqueueSlashingVotePromise.catch(err => {
313
- this.log.error(`Error enqueuing slashing vote`, err, { blockNumber: newBlockNumber, slot });
314
- });
286
+ // Next checkpoint follows from the last synced one
287
+ const checkpointNumber = CheckpointNumber(syncedTo.checkpointNumber + 1);
315
288
 
316
- await this.publisher.sendRequests();
317
-
318
- if (finishedFlushing) {
319
- this.isFlushing = false;
289
+ const logCtx = {
290
+ now,
291
+ syncedToL1Ts: syncedTo.l1Timestamp,
292
+ syncedToL2Slot: getSlotAtTimestamp(syncedTo.l1Timestamp, this.l1Constants),
293
+ slot,
294
+ slotTs: ts,
295
+ checkpointNumber,
296
+ isPendingChainValid: pick(syncedTo.pendingChainValidationStatus, 'valid', 'reason', 'invalidIndex'),
297
+ };
298
+
299
+ // Check that we are a proposer for the next slot
300
+ this.setState(SequencerState.PROPOSER_CHECK, slot);
301
+ const [canPropose, proposer] = await this.checkCanPropose(slot);
302
+
303
+ // If we are not a proposer check if we should invalidate an invalid checkpoint, and bail
304
+ if (!canPropose) {
305
+ await this.considerInvalidatingCheckpoint(syncedTo, slot);
306
+ return undefined;
320
307
  }
321
308
 
322
- this.setState(SequencerState.IDLE, 0n);
323
- }
309
+ // Check that the slot is not taken by a block already (should never happen, since only us can propose for this slot)
310
+ if (syncedTo.block && syncedTo.block.header.getSlot() >= slot) {
311
+ this.log.warn(
312
+ `Cannot propose block at next L2 slot ${slot} since that slot was taken by block ${syncedTo.blockNumber}`,
313
+ { ...logCtx, block: syncedTo.block.header.toInspect() },
314
+ );
315
+ this.metrics.recordBlockProposalPrecheckFailed('slot_already_taken');
316
+ return undefined;
317
+ }
324
318
 
325
- @trackSpan('Sequencer.work')
326
- protected async work() {
327
- try {
328
- await this.doRealWork();
329
- } catch (err) {
330
- if (err instanceof SequencerTooSlowError) {
331
- this.log.warn(err.message);
332
- } else {
333
- // Re-throw other errors
334
- throw err;
335
- }
336
- } finally {
337
- this.setState(SequencerState.IDLE, 0n);
319
+ // We now need to get ourselves a publisher.
320
+ // The returned attestor will be the one we provided if we provided one.
321
+ // Otherwise it will be a valid attestor for the returned publisher.
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);
325
+ this.log.verbose(`Created publisher at address ${publisher.getSenderAddress()} for attestor ${attestorAddress}`);
326
+ this.publisher = publisher;
327
+
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`);
338
332
  }
339
- }
340
333
 
341
- public getForwarderAddress() {
342
- return this.publisher.getForwarderAddress();
343
- }
334
+ // Prepare invalidation request if the pending chain is invalid (returns undefined if no need)
335
+ const invalidateCheckpoint = await publisher.simulateInvalidateCheckpoint(syncedTo.pendingChainValidationStatus);
344
336
 
345
- /**
346
- * Checks if we can propose at the next block and returns the slot number if we can.
347
- * @param tipArchive - The archive of the previous block.
348
- * @param proposalBlockNumber - The block number of the proposal.
349
- * @returns The slot number if we can propose at the next block, otherwise undefined.
350
- */
351
- async slotForProposal(tipArchive: Buffer, proposalBlockNumber: bigint): Promise<bigint | undefined> {
352
- const result = await this.publisher.canProposeAtNextEthBlock(tipArchive);
337
+ // Check with the rollup contract if we can indeed propose at the next L2 slot. This check should not fail
338
+ // if all the previous checks are good, but we do it just in case.
339
+ const canProposeCheck = await publisher.canProposeAtNextEthBlock(
340
+ syncedTo.archive,
341
+ proposer ?? EthAddress.ZERO,
342
+ invalidateCheckpoint,
343
+ );
353
344
 
354
- if (!result) {
345
+ if (canProposeCheck === undefined) {
346
+ this.log.warn(
347
+ `Cannot propose checkpoint ${checkpointNumber} at slot ${slot} due to failed rollup contract check`,
348
+ logCtx,
349
+ );
350
+ this.emit('proposer-rollup-check-failed', { reason: 'Rollup contract check failed', slot });
351
+ this.metrics.recordBlockProposalPrecheckFailed('rollup_contract_check_failed');
355
352
  return undefined;
356
353
  }
357
354
 
358
- const [slot, blockNumber] = result;
355
+ if (canProposeCheck.slot !== slot) {
356
+ this.log.warn(
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}.`,
358
+ { ...logCtx, rollup: canProposeCheck, expectedSlot: slot },
359
+ );
360
+ this.emit('proposer-rollup-check-failed', { reason: 'Slot mismatch', slot });
361
+ this.metrics.recordBlockProposalPrecheckFailed('slot_mismatch');
362
+ return undefined;
363
+ }
359
364
 
360
- if (proposalBlockNumber !== blockNumber) {
361
- const msg = `Sequencer block number mismatch. Expected ${proposalBlockNumber} but got ${blockNumber}.`;
362
- this.log.warn(msg);
363
- throw new Error(msg);
365
+ if (canProposeCheck.checkpointNumber !== checkpointNumber) {
366
+ this.log.warn(
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 },
369
+ );
370
+ this.emit('proposer-rollup-check-failed', { reason: 'Block mismatch', slot });
371
+ this.metrics.recordBlockProposalPrecheckFailed('block_number_mismatch');
372
+ return undefined;
364
373
  }
365
- return slot;
374
+
375
+ this.lastSlotForCheckpointProposalJob = slot;
376
+ this.log.info(`Preparing checkpoint proposal ${checkpointNumber} at slot ${slot}`, { ...logCtx, proposer });
377
+
378
+ // Create and return the checkpoint proposal job
379
+ return this.createCheckpointProposalJob(
380
+ epoch,
381
+ slot,
382
+ checkpointNumber,
383
+ syncedTo.blockNumber,
384
+ proposer,
385
+ publisher,
386
+ attestorAddress,
387
+ invalidateCheckpoint,
388
+ );
389
+ }
390
+
391
+ protected createCheckpointProposalJob(
392
+ epoch: EpochNumber,
393
+ slot: SlotNumber,
394
+ checkpointNumber: CheckpointNumber,
395
+ syncedToBlockNumber: BlockNumber,
396
+ proposer: EthAddress | undefined,
397
+ publisher: SequencerPublisher,
398
+ attestorAddress: EthAddress,
399
+ invalidateCheckpoint: InvalidateCheckpointRequest | undefined,
400
+ ): CheckpointProposalJob {
401
+ return new CheckpointProposalJob(
402
+ epoch,
403
+ slot,
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
+ );
366
430
  }
367
431
 
368
432
  /**
369
- * 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.
370
434
  * @param proposedState - The new state to transition to.
371
- * @param currentSlotNumber - The current slot number.
435
+ * @param slotNumber - The current slot number.
372
436
  * @param force - Whether to force the transition even if the sequencer is stopped.
373
- *
374
- * @dev If the `currentSlotNumber` doesn't matter (e.g. transitioning to IDLE), pass in `0n`;
375
- * it is only used to check if we have enough time left in the slot to transition to the new state.
376
437
  */
377
- setState(proposedState: SequencerState, currentSlotNumber: bigint, force: boolean = false) {
378
- if (this.state === SequencerState.STOPPED && force !== true) {
438
+ protected setState(
439
+ proposedState: SequencerState,
440
+ slotNumber: SlotNumber | undefined,
441
+ opts: { force?: boolean } = {},
442
+ ): void {
443
+ if (this.state === SequencerState.STOPPING && proposedState !== SequencerState.STOPPED && !opts.force) {
444
+ this.log.warn(`Cannot set sequencer to ${proposedState} as it is stopping.`);
445
+ throw new SequencerInterruptedError();
446
+ }
447
+ if (this.state === SequencerState.STOPPED && !opts.force) {
379
448
  this.log.warn(`Cannot set sequencer from ${this.state} to ${proposedState} as it is stopped.`);
380
449
  return;
381
450
  }
382
- const secondsIntoSlot = this.getSecondsIntoSlot(currentSlotNumber);
383
- this.timetable.assertTimeLeft(proposedState, secondsIntoSlot);
384
- this.log.debug(`Transitioning from ${this.state} to ${proposedState}`);
451
+ let secondsIntoSlot = undefined;
452
+ if (slotNumber !== undefined) {
453
+ secondsIntoSlot = this.getSecondsIntoSlot(slotNumber);
454
+ this.timetable.assertTimeLeft(proposedState, secondsIntoSlot);
455
+ }
456
+
457
+ const boringStates = [SequencerState.IDLE, SequencerState.SYNCHRONIZING];
458
+ const logLevel =
459
+ boringStates.includes(proposedState) && boringStates.includes(this.state)
460
+ ? ('trace' as const)
461
+ : ('debug' as const);
462
+ this.log[logLevel](`Transitioning from ${this.state} to ${proposedState}`, { slotNumber, secondsIntoSlot });
463
+
464
+ this.emit('state-changed', {
465
+ oldState: this.state,
466
+ newState: proposedState,
467
+ secondsIntoSlot,
468
+ slot: slotNumber,
469
+ });
385
470
  this.state = proposedState;
386
471
  }
387
472
 
388
473
  /**
389
- * Build a block
390
- *
391
- * Shared between the sequencer and the validator for re-execution
392
- *
393
- * @param pendingTxs - The pending transactions to construct the block from
394
- * @param newGlobalVariables - The global variables for the new block
395
- * @param historicalHeader - The historical header of the parent
396
- * @param opts - Whether to just validate the block as a validator, as opposed to building it as a proposal
474
+ * Returns whether all dependencies have caught up.
475
+ * We don't check against the previous block submitted since it may have been reorg'd out.
397
476
  */
398
- protected async buildBlock(
399
- pendingTxs: Iterable<Tx> | AsyncIterable<Tx>,
400
- newGlobalVariables: GlobalVariables,
401
- opts: { validateOnly?: boolean } = {},
402
- ) {
403
- const blockNumber = newGlobalVariables.blockNumber.toNumber();
404
- const slot = newGlobalVariables.slotNumber.toBigInt();
405
- this.log.debug(`Requesting L1 to L2 messages from contract for block ${blockNumber}`);
406
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(BigInt(blockNumber));
407
- const msgCount = l1ToL2Messages.length;
408
-
409
- this.log.verbose(`Building block ${blockNumber} for slot ${slot}`, {
410
- slot,
411
- blockNumber,
412
- msgCount,
413
- validator: opts.validateOnly,
414
- });
415
-
416
- // Sync to the previous block at least
417
- await this.worldState.syncImmediate(blockNumber - 1);
418
- this.log.debug(`Synced to previous block ${blockNumber - 1}`);
419
-
420
- // NB: separating the dbs because both should update the state
421
- const publicProcessorFork = await this.worldState.fork();
422
- const orchestratorFork = await this.worldState.fork();
423
-
424
- const previousBlockHeader =
425
- (await this.l2BlockSource.getBlock(blockNumber - 1))?.header ?? orchestratorFork.getInitialHeader();
426
-
427
- try {
428
- const processor = this.publicProcessorFactory.create(publicProcessorFork, newGlobalVariables, true);
429
- const blockBuildingTimer = new Timer();
430
- const blockBuilder = this.blockBuilderFactory.create(orchestratorFork);
431
- await blockBuilder.startNewBlock(newGlobalVariables, l1ToL2Messages, previousBlockHeader);
432
-
433
- // Deadline for processing depends on whether we're proposing a block
434
- const secondsIntoSlot = this.getSecondsIntoSlot(slot);
435
- const processingEndTimeWithinSlot = opts.validateOnly
436
- ? this.timetable.getValidatorReexecTimeEnd(secondsIntoSlot)
437
- : this.timetable.getBlockProposalExecTimeEnd(secondsIntoSlot);
438
-
439
- // Deadline is only set if enforceTimeTable is enabled.
440
- const deadline = this.enforceTimeTable
441
- ? new Date((this.getSlotStartTimestamp(slot) + processingEndTimeWithinSlot) * 1000)
442
- : undefined;
443
-
444
- this.log.verbose(`Processing pending txs`, {
477
+ protected async checkSync(args: { ts: bigint; slot: SlotNumber }): Promise<SequencerSyncCheckResult | undefined> {
478
+ // Check that the archiver and dependencies have synced to the previous L1 slot at least
479
+ // TODO(#14766): Archiver reports L1 timestamp based on L1 blocks seen, which means that a missed L1 block will
480
+ // cause the archiver L1 timestamp to fall behind, and cause this sequencer to start processing one L1 slot later.
481
+ const l1Timestamp = await this.l2BlockSource.getL1Timestamp();
482
+ const { slot, ts } = args;
483
+ if (l1Timestamp === undefined || l1Timestamp + BigInt(this.l1Constants.ethereumSlotDuration) < ts) {
484
+ this.log.debug(`Cannot propose block at next L2 slot ${slot} due to pending sync from L1`, {
445
485
  slot,
446
- slotStart: new Date(this.getSlotStartTimestamp(slot) * 1000),
447
- now: new Date(this.dateProvider.now()),
448
- deadline,
486
+ ts,
487
+ l1Timestamp,
449
488
  });
489
+ return undefined;
490
+ }
450
491
 
451
- const validators = createValidatorsForBlockBuilding(
452
- publicProcessorFork,
453
- this.contractDataSource,
454
- newGlobalVariables,
455
- this.allowedInSetup,
456
- );
457
-
458
- // TODO(#11000): Public processor should just handle processing, one tx at a time. It should be responsibility
459
- // of the sequencer to update world state and iterate over txs. We should refactor this along with unifying the
460
- // publicProcessorFork and orchestratorFork, to avoid doing tree insertions twice when building the block.
461
- const proposerLimits = {
462
- maxTransactions: this.maxTxsPerBlock,
463
- maxBlockSize: this.maxBlockSizeInBytes,
464
- maxBlockGas: this.maxBlockGas,
465
- };
466
- const limits = opts.validateOnly ? { deadline } : { deadline, ...proposerLimits };
467
- const [publicProcessorDuration, [processedTxs, failedTxs]] = await elapsed(() =>
468
- processor.process(pendingTxs, limits, validators),
469
- );
470
-
471
- if (!opts.validateOnly && failedTxs.length > 0) {
472
- const failedTxData = failedTxs.map(fail => fail.tx);
473
- const failedTxHashes = await Tx.getHashes(failedTxData);
474
- this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
475
- await this.p2pClient.deleteTxs(failedTxHashes);
476
- }
477
-
478
- if (
479
- !opts.validateOnly && // We check for minTxCount only if we are proposing a block, not if we are validating it
480
- !this.isFlushing && // And we skip the check when flushing, since we want all pending txs to go out, no matter if too few
481
- this.minTxsPerBlock !== undefined &&
482
- processedTxs.length < this.minTxsPerBlock
483
- ) {
484
- this.log.warn(
485
- `Block ${blockNumber} has too few txs to be proposed (got ${processedTxs.length} but required ${this.minTxsPerBlock})`,
486
- { slot, blockNumber, processedTxCount: processedTxs.length },
487
- );
488
- throw new Error(`Block has too few successful txs to be proposed`);
489
- }
492
+ const syncedBlocks = await Promise.all([
493
+ this.worldState.status().then(({ syncSummary }) => ({
494
+ number: syncSummary.latestBlockNumber,
495
+ hash: syncSummary.latestBlockHash,
496
+ })),
497
+ this.l2BlockSource.getL2Tips().then(t => t.proposed),
498
+ this.p2pClient.getStatus().then(p2p => p2p.syncedToL2Block),
499
+ this.l1ToL2MessageSource.getL2Tips().then(t => t.proposed),
500
+ this.l2BlockSource.getPendingChainValidationStatus(),
501
+ ] as const);
490
502
 
491
- const start = process.hrtime.bigint();
492
- await blockBuilder.addTxs(processedTxs);
493
- const end = process.hrtime.bigint();
494
- const duration = Number(end - start) / 1_000;
495
- this.metrics.recordBlockBuilderTreeInsertions(duration);
503
+ const [worldState, l2BlockSource, p2p, l1ToL2MessageSource, pendingChainValidationStatus] = syncedBlocks;
496
504
 
497
- // All real transactions have been added, set the block as full and pad if needed
498
- const block = await blockBuilder.setBlockCompleted();
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.
508
+ const result =
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);
499
513
 
500
- // How much public gas was processed
501
- const publicGas = processedTxs.reduce((acc, tx) => acc.add(tx.gasUsed.publicGas), Gas.empty());
514
+ if (!result) {
515
+ this.log.debug(`Sequencer sync check failed`, { worldState, l2BlockSource, p2p, l1ToL2MessageSource });
516
+ return undefined;
517
+ }
502
518
 
519
+ // Special case for genesis state
520
+ const blockNumber = worldState.number;
521
+ if (blockNumber < INITIAL_L2_BLOCK_NUM) {
522
+ const archive = new Fr((await this.worldState.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
503
523
  return {
504
- block,
505
- publicGas,
506
- publicProcessorDuration,
507
- numMsgs: l1ToL2Messages.length,
508
- numTxs: processedTxs.length,
509
- numFailedTxs: failedTxs.length,
510
- blockBuildingTimer,
524
+ checkpointNumber: CheckpointNumber.ZERO,
525
+ blockNumber: BlockNumber.ZERO,
526
+ archive,
527
+ l1Timestamp,
528
+ pendingChainValidationStatus,
511
529
  };
512
- } finally {
513
- // We create a fresh processor each time to reset any cached state (eg storage writes)
514
- // We wait a bit to close the forks since the processor may still be working on a dangling tx
515
- // which was interrupted due to the processingDeadline being hit.
516
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
517
- setTimeout(async () => {
518
- try {
519
- await publicProcessorFork.close();
520
- await orchestratorFork.close();
521
- } catch (err) {
522
- // This can happen if the sequencer is stopped before we hit this timeout.
523
- this.log.warn(`Error closing forks for block processing`, err);
524
- }
525
- }, 5000);
526
530
  }
531
+
532
+ const block = await this.l2BlockSource.getL2Block(blockNumber);
533
+ if (!block) {
534
+ // this shouldn't really happen because a moment ago we checked that all components were in sync
535
+ this.log.error(`Failed to get L2 block ${blockNumber} from the archiver with all components in sync`);
536
+ return undefined;
537
+ }
538
+
539
+ return {
540
+ block,
541
+ blockNumber: block.number,
542
+ checkpointNumber: block.checkpointNumber,
543
+ archive: block.archive.root,
544
+ l1Timestamp,
545
+ pendingChainValidationStatus,
546
+ };
527
547
  }
528
548
 
529
549
  /**
530
- * @notice Build and propose a block to the chain
531
- *
532
- * @dev MUST throw instead of exiting early to ensure that world-state
533
- * is being rolled back if the block is dropped.
534
- *
535
- * @param pendingTxs - Iterable of pending transactions to construct the block from
536
- * @param proposalHeader - The partial header constructed for the proposal
550
+ * Checks if we are the proposer for the next slot.
551
+ * @returns True if we can propose, and the proposer address (undefined if anyone can propose)
537
552
  */
538
- @trackSpan('Sequencer.buildBlockAndEnqueuePublish', (_validTxs, proposalHeader) => ({
539
- [Attributes.BLOCK_NUMBER]: proposalHeader.globalVariables.blockNumber.toNumber(),
540
- }))
541
- private async buildBlockAndEnqueuePublish(
542
- pendingTxs: Iterable<Tx> | AsyncIterable<Tx>,
543
- proposalHeader: BlockHeader,
544
- ): Promise<void> {
545
- await this.publisher.validateBlockForSubmission(proposalHeader);
546
-
547
- const newGlobalVariables = proposalHeader.globalVariables;
548
- const blockNumber = newGlobalVariables.blockNumber.toNumber();
549
- const slot = newGlobalVariables.slotNumber.toBigInt();
550
-
551
- // this.metrics.recordNewBlock(blockNumber, validTxs.length);
552
- const workTimer = new Timer();
553
- this.setState(SequencerState.CREATING_BLOCK, slot);
553
+ protected async checkCanPropose(slot: SlotNumber): Promise<[boolean, EthAddress | undefined]> {
554
+ let proposer: EthAddress | undefined;
554
555
 
555
556
  try {
556
- const buildBlockRes = await this.buildBlock(pendingTxs, newGlobalVariables);
557
- const { publicGas, block, publicProcessorDuration, numTxs, numMsgs, blockBuildingTimer } = buildBlockRes;
558
- this.metrics.recordBuiltBlock(workTimer.ms(), publicGas.l2Gas);
559
-
560
- // TODO(@PhilWindle) We should probably periodically check for things like another
561
- // block being published before ours instead of just waiting on our block
562
- await this.publisher.validateBlockForSubmission(block.header);
563
-
564
- const blockStats: L2BlockBuiltStats = {
565
- eventName: 'l2-block-built',
566
- creator: this.publisher.getSenderAddress().toString(),
567
- duration: workTimer.ms(),
568
- publicProcessDuration: publicProcessorDuration,
569
- rollupCircuitsDuration: blockBuildingTimer.ms(),
570
- ...block.getStats(),
571
- };
557
+ proposer = await this.epochCache.getProposerAttesterAddressInSlot(slot);
558
+ } catch (e) {
559
+ if (e instanceof NoCommitteeError) {
560
+ this.log.warn(`Cannot propose at next L2 slot ${slot} since the committee does not exist on L1`);
561
+ return [false, undefined];
562
+ }
563
+ this.log.error(`Error getting proposer for slot ${slot}`, e);
564
+ return [false, undefined];
565
+ }
572
566
 
573
- const blockHash = await block.hash();
574
- const txHashes = block.body.txEffects.map(tx => tx.txHash);
575
- this.log.info(
576
- `Built block ${block.number} for slot ${slot} with ${numTxs} txs and ${numMsgs} messages. ${
577
- publicGas.l2Gas / workTimer.s()
578
- } mana/s`,
579
- {
580
- blockHash,
581
- globalVariables: block.header.globalVariables.toInspect(),
582
- txHashes,
583
- ...blockStats,
584
- },
585
- );
567
+ // If proposer is undefined, then the committee is empty and anyone may propose
568
+ if (proposer === undefined) {
569
+ return [true, undefined];
570
+ }
571
+ // In fisherman mode, just return the current proposer
572
+ if (this.config.fishermanMode) {
573
+ return [true, proposer];
574
+ }
586
575
 
587
- this.log.debug('Collecting attestations');
588
- const stopCollectingAttestationsTimer = this.metrics.startCollectingAttestationsTimer();
589
- const attestations = await this.collectAttestations(block, txHashes);
590
- if (attestations !== undefined) {
591
- this.log.verbose(`Collected ${attestations.length} attestations`, { blockHash, blockNumber });
592
- }
593
- stopCollectingAttestationsTimer();
576
+ const validatorAddresses = this.validatorClient.getValidatorAddresses();
577
+ const weAreProposer = validatorAddresses.some(addr => addr.equals(proposer));
594
578
 
595
- return this.enqueuePublishL2Block(block, attestations, txHashes);
596
- } catch (err) {
597
- this.metrics.recordFailedBlock();
598
- throw err;
579
+ if (!weAreProposer) {
580
+ this.log.debug(`Cannot propose at slot ${slot} since we are not a proposer`, { validatorAddresses, proposer });
581
+ return [false, proposer];
599
582
  }
583
+
584
+ return [true, proposer];
600
585
  }
601
586
 
602
- @trackSpan('Sequencer.collectAttestations', (block, txHashes) => ({
603
- [Attributes.BLOCK_NUMBER]: block.number,
604
- [Attributes.BLOCK_ARCHIVE]: block.archive.toString(),
605
- [Attributes.BLOCK_TXS_COUNT]: txHashes.length,
606
- }))
607
- protected async collectAttestations(block: L2Block, txHashes: TxHash[]): Promise<Signature[] | undefined> {
608
- // TODO(https://github.com/AztecProtocol/aztec-packages/issues/7962): inefficient to have a round trip in here - this should be cached
609
- const committee = await this.publisher.getCurrentEpochCommittee();
610
-
611
- if (committee.length === 0) {
612
- this.log.verbose(`Attesting committee is empty`);
613
- return undefined;
614
- } else {
615
- this.log.debug(`Attesting committee length is ${committee.length}`);
587
+ /**
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.
589
+ * This allows the sequencer to participate in governance/slashing votes even when it cannot build blocks.
590
+ */
591
+ @trackSpan('Seqeuencer.tryVoteWhenSyncFails', ({ slot }) => ({ [Attributes.SLOT_NUMBER]: slot }))
592
+ protected async tryVoteWhenSyncFails(args: { slot: SlotNumber; ts: bigint }): Promise<void> {
593
+ const { slot } = args;
594
+
595
+ // Prevent duplicate attempts in the same slot
596
+ if (this.lastSlotForFallbackVote === slot) {
597
+ this.log.trace(`Already attempted to vote in slot ${slot} (skipping)`);
598
+ return;
616
599
  }
617
600
 
618
- if (!this.validatorClient) {
619
- const msg = 'Missing validator client: Cannot collect attestations';
620
- this.log.error(msg);
621
- throw new Error(msg);
601
+ // Check if we're past the max time for initializing a proposal
602
+ const secondsIntoSlot = this.getSecondsIntoSlot(slot);
603
+ const maxAllowedTime = this.timetable.getMaxAllowedTime(SequencerState.INITIALIZING_CHECKPOINT);
604
+
605
+ // If we haven't exceeded the time limit for initializing a proposal, don't proceed with voting
606
+ // We use INITIALIZING_PROPOSAL time limit because if we're past that, we can't build a block anyway
607
+ if (maxAllowedTime === undefined || secondsIntoSlot <= maxAllowedTime) {
608
+ this.log.trace(`Not attempting to vote since there is still time for block building`, {
609
+ secondsIntoSlot,
610
+ maxAllowedTime,
611
+ });
612
+ return;
622
613
  }
623
614
 
624
- const numberOfRequiredAttestations = Math.floor((committee.length * 2) / 3) + 1;
625
- const slotNumber = block.header.globalVariables.slotNumber.toBigInt();
626
- this.setState(SequencerState.COLLECTING_ATTESTATIONS, slotNumber);
615
+ this.log.trace(`Sync for slot ${slot} failed, checking for voting opportunities`, {
616
+ secondsIntoSlot,
617
+ maxAllowedTime,
618
+ });
627
619
 
628
- this.log.debug('Creating block proposal for validators');
629
- const proposal = await this.validatorClient.createBlockProposal(block.header, block.archive.root, txHashes);
630
- if (!proposal) {
631
- const msg = `Failed to create block proposal`;
632
- throw new Error(msg);
620
+ // Check if we're a proposer or proposal is open
621
+ const [canPropose, proposer] = await this.checkCanPropose(slot);
622
+ if (!canPropose) {
623
+ this.log.trace(`Cannot vote in slot ${slot} since we are not a proposer`, { slot, proposer });
624
+ return;
633
625
  }
634
626
 
635
- this.log.debug('Broadcasting block proposal to validators');
636
- this.validatorClient.broadcastBlockProposal(proposal);
627
+ // Mark this slot as attempted
628
+ this.lastSlotForFallbackVote = slot;
629
+
630
+ // Get a publisher for voting
631
+ const { attestorAddress, publisher } = await this.publisherFactory.create(proposer);
632
+
633
+ this.log.debug(`Attempting to vote despite sync failure at slot ${slot}`, {
634
+ attestorAddress,
635
+ slot,
636
+ });
637
637
 
638
- const attestationTimeAllowed = this.enforceTimeTable
639
- ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_BLOCK)!
640
- : this.aztecSlotDuration;
641
- const attestationDeadline = new Date(this.dateProvider.now() + attestationTimeAllowed * 1000);
642
- const attestations = await this.validatorClient.collectAttestations(
643
- proposal,
644
- numberOfRequiredAttestations,
645
- attestationDeadline,
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,
646
649
  );
650
+ const votesPromises = voter.enqueueVotes();
651
+ const votes = await Promise.all(votesPromises);
652
+
653
+ if (votes.every(p => !p)) {
654
+ this.log.debug(`No votes to enqueue for slot ${slot}`);
655
+ return;
656
+ }
647
657
 
648
- // note: the smart contract requires that the signatures are provided in the order of the committee
649
- return orderAttestations(attestations, committee);
658
+ this.log.info(`Voting in slot ${slot} despite sync failure`, { slot });
659
+ await publisher.sendRequests();
650
660
  }
651
661
 
652
662
  /**
653
- * Publishes the L2Block to the rollup contract.
654
- * @param block - The L2Block to be published.
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.
655
665
  */
656
- @trackSpan('Sequencer.enqueuePublishL2Block', block => ({
657
- [Attributes.BLOCK_NUMBER]: block.number,
658
- }))
659
- protected async enqueuePublishL2Block(
660
- block: L2Block,
661
- attestations?: Signature[],
662
- txHashes?: TxHash[],
663
- ): Promise<void> {
664
- // Publishes new block to the network and awaits the tx to be mined
665
- this.setState(SequencerState.PUBLISHING_BLOCK, block.header.globalVariables.slotNumber.toBigInt());
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
+ }
666
678
 
667
- // Time out tx at the end of the slot
668
- const slot = block.header.globalVariables.slotNumber.toNumber();
669
- const txTimeoutAt = new Date((this.getSlotStartTimestamp(slot) + this.aztecSlotDuration) * 1000);
679
+ // Mark this slot as attempted
680
+ this.lastSlotForFallbackVote = slot;
670
681
 
671
- const enqueued = await this.publisher.enqueueProposeL2Block(block, attestations, txHashes, {
672
- txTimeoutAt,
673
- });
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
+ );
674
697
 
675
- if (!enqueued) {
676
- throw new Error(`Failed to enqueue publish of block ${block.number}`);
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;
677
704
  }
705
+
706
+ this.log.info(`Voting in slot ${slot} (escape hatch open)`, { slot });
707
+ await publisher.sendRequests();
678
708
  }
679
709
 
680
710
  /**
681
- * Returns whether all dependencies have caught up.
682
- * We don't check against the previous block submitted since it may have been reorg'd out.
683
- * @returns Boolean indicating if our dependencies are synced to the latest block.
711
+ * Considers invalidating a block if the pending chain is invalid. Depends on how long the invalid block
712
+ * has been there without being invalidated and whether the sequencer is in the committee or not. We always
713
+ * have the proposer try to invalidate, but if they fail, the sequencers in the committee are expected to try,
714
+ * and if they fail, any sequencer will try as well.
684
715
  */
685
- protected async getChainTip(): Promise<{ blockNumber: number; archive: Fr } | undefined> {
686
- const syncedBlocks = await Promise.all([
687
- this.worldState.status().then((s: WorldStateSynchronizerStatus) => {
688
- return {
689
- number: s.syncSummary.latestBlockNumber,
690
- hash: s.syncSummary.latestBlockHash,
691
- };
692
- }),
693
- this.l2BlockSource.getL2Tips().then(t => t.latest),
694
- this.p2pClient.getStatus().then(p2p => p2p.syncedToL2Block),
695
- this.l1ToL2MessageSource.getBlockNumber(),
696
- ] as const);
716
+ protected async considerInvalidatingCheckpoint(
717
+ syncedTo: SequencerSyncCheckResult,
718
+ currentSlot: SlotNumber,
719
+ ): Promise<void> {
720
+ const { pendingChainValidationStatus, l1Timestamp } = syncedTo;
721
+ if (pendingChainValidationStatus.valid) {
722
+ return;
723
+ }
697
724
 
698
- const [worldState, l2BlockSource, p2p, l1ToL2MessageSource] = syncedBlocks;
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();
729
+
730
+ const { secondsBeforeInvalidatingBlockAsCommitteeMember, secondsBeforeInvalidatingBlockAsNonCommitteeMember } =
731
+ this.config;
732
+
733
+ const logData = {
734
+ invalidL1Timestamp: invalidCheckpointTimestamp,
735
+ l1Timestamp,
736
+ invalidCheckpoint: pendingChainValidationStatus.checkpoint,
737
+ secondsBeforeInvalidatingBlockAsCommitteeMember,
738
+ secondsBeforeInvalidatingBlockAsNonCommitteeMember,
739
+ ourValidatorAddresses,
740
+ currentSlot,
741
+ };
742
+
743
+ const inCurrentCommittee = () =>
744
+ this.epochCache
745
+ .getCommittee(currentSlot)
746
+ .then(c => c?.committee?.some(member => ourValidatorAddresses.some(addr => addr.equals(member))));
747
+
748
+ const invalidateAsCommitteeMember =
749
+ secondsBeforeInvalidatingBlockAsCommitteeMember !== undefined &&
750
+ secondsBeforeInvalidatingBlockAsCommitteeMember > 0 &&
751
+ timeSinceChainInvalid > secondsBeforeInvalidatingBlockAsCommitteeMember &&
752
+ (await inCurrentCommittee());
753
+
754
+ const invalidateAsNonCommitteeMember =
755
+ secondsBeforeInvalidatingBlockAsNonCommitteeMember !== undefined &&
756
+ secondsBeforeInvalidatingBlockAsNonCommitteeMember > 0 &&
757
+ timeSinceChainInvalid > secondsBeforeInvalidatingBlockAsNonCommitteeMember;
758
+
759
+ if (!invalidateAsCommitteeMember && !invalidateAsNonCommitteeMember) {
760
+ this.log.debug(`Not invalidating pending chain`, logData);
761
+ return;
762
+ }
699
763
 
700
- const result =
701
- // check that world state has caught up with archiver
702
- // note that the archiver reports undefined hash for the genesis block
703
- // because it doesn't have access to world state to compute it (facepalm)
704
- (l2BlockSource.hash === undefined || worldState.hash === l2BlockSource.hash) &&
705
- // and p2p client and message source are at least at the same block
706
- // this should change to hashes once p2p client handles reorgs
707
- // and once we stop pretending that the l1tol2message source is not
708
- // just the archiver under a different name
709
- (!l2BlockSource.hash || p2p.hash === l2BlockSource.hash) &&
710
- l1ToL2MessageSource === l2BlockSource.number;
711
-
712
- this.log.debug(`Sequencer sync check ${result ? 'succeeded' : 'failed'}`, {
713
- worldStateNumber: worldState.number,
714
- worldStateHash: worldState.hash,
715
- l2BlockSourceNumber: l2BlockSource.number,
716
- l2BlockSourceHash: l2BlockSource.hash,
717
- p2pNumber: p2p.number,
718
- p2pHash: p2p.hash,
719
- l1ToL2MessageSourceNumber: l1ToL2MessageSource,
720
- });
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
+ }
721
779
 
722
- if (!result) {
723
- return undefined;
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);
785
+ return;
724
786
  }
725
- if (worldState.number >= INITIAL_L2_BLOCK_NUM) {
726
- const block = await this.l2BlockSource.getBlock(worldState.number);
727
- if (!block) {
728
- // this shouldn't really happen because a moment ago we checked that all components were in synch
729
- return undefined;
730
- }
731
787
 
732
- return { blockNumber: block.number, archive: block.archive.root };
788
+ this.log.info(
789
+ invalidateAsCommitteeMember
790
+ ? `Invalidating checkpoint ${invalidCheckpointNumber} as committee member`
791
+ : `Invalidating checkpoint ${invalidCheckpointNumber} as non-committee member`,
792
+ logData,
793
+ );
794
+
795
+ publisher.enqueueInvalidateCheckpoint(invalidateCheckpoint);
796
+
797
+ if (!this.config.fishermanMode) {
798
+ await publisher.sendRequests();
733
799
  } else {
734
- const archive = new Fr((await this.worldState.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
735
- return { blockNumber: INITIAL_L2_BLOCK_NUM - 1, archive };
800
+ this.log.info('Invalidating checkpoint in fisherman mode, clearing pending requests');
801
+ publisher.clearPendingRequests();
736
802
  }
737
803
  }
738
804
 
739
- private getSlotStartTimestamp(slotNumber: number | bigint): number {
740
- return Number(this.l1Constants.l1GenesisTime) + Number(slotNumber) * this.l1Constants.slotDuration;
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 {
835
+ return getSlotStartBuildTimestamp(slotNumber, this.l1Constants);
741
836
  }
742
837
 
743
- private getSecondsIntoSlot(slotNumber: number | bigint): number {
744
- const slotStartTimestamp = this.getSlotStartTimestamp(slotNumber);
838
+ private getSecondsIntoSlot(slotNumber: SlotNumber): number {
839
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp(slotNumber);
745
840
  return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
746
841
  }
747
842
 
748
- get aztecSlotDuration() {
843
+ public get aztecSlotDuration() {
749
844
  return this.l1Constants.slotDuration;
750
845
  }
751
846
 
752
- get coinbase(): EthAddress {
753
- return this._coinbase;
847
+ public get maxL2BlockGas(): number | undefined {
848
+ return this.config.maxL2BlockGas;
849
+ }
850
+
851
+ public getSlasherClient(): SlasherClientInterface | undefined {
852
+ return this.slasherClient;
754
853
  }
755
854
 
756
- get feeRecipient(): AztecAddress {
757
- return this._feeRecipient;
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;
758
869
  }
759
870
  }
871
+
872
+ type SequencerSyncCheckResult = {
873
+ block?: L2Block;
874
+ checkpointNumber: CheckpointNumber;
875
+ blockNumber: BlockNumber;
876
+ archive: Fr;
877
+ l1Timestamp: bigint;
878
+ pendingChainValidationStatus: ValidateCheckpointResult;
879
+ };