@aztec/sequencer-client 0.0.1-commit.5daedc8 → 0.0.1-commit.6201a7b05

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