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

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 (102) hide show
  1. package/dest/client/index.d.ts +1 -1
  2. package/dest/client/sequencer-client.d.ts +10 -9
  3. package/dest/client/sequencer-client.d.ts.map +1 -1
  4. package/dest/client/sequencer-client.js +32 -24
  5. package/dest/config.d.ts +12 -5
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +75 -28
  8. package/dest/global_variable_builder/global_builder.d.ts +19 -13
  9. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  10. package/dest/global_variable_builder/global_builder.js +41 -28
  11. package/dest/global_variable_builder/index.d.ts +1 -1
  12. package/dest/index.d.ts +2 -2
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +1 -1
  15. package/dest/publisher/config.d.ts +9 -4
  16. package/dest/publisher/config.d.ts.map +1 -1
  17. package/dest/publisher/config.js +14 -3
  18. package/dest/publisher/index.d.ts +1 -1
  19. package/dest/publisher/sequencer-publisher-factory.d.ts +5 -4
  20. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  21. package/dest/publisher/sequencer-publisher-factory.js +1 -1
  22. package/dest/publisher/sequencer-publisher-metrics.d.ts +3 -3
  23. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  24. package/dest/publisher/sequencer-publisher.d.ts +66 -53
  25. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  26. package/dest/publisher/sequencer-publisher.js +230 -120
  27. package/dest/sequencer/block_builder.d.ts +4 -5
  28. package/dest/sequencer/block_builder.d.ts.map +1 -1
  29. package/dest/sequencer/block_builder.js +9 -10
  30. package/dest/sequencer/checkpoint_builder.d.ts +63 -0
  31. package/dest/sequencer/checkpoint_builder.d.ts.map +1 -0
  32. package/dest/sequencer/checkpoint_builder.js +131 -0
  33. package/dest/sequencer/checkpoint_proposal_job.d.ts +74 -0
  34. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
  35. package/dest/sequencer/checkpoint_proposal_job.js +642 -0
  36. package/dest/sequencer/checkpoint_voter.d.ts +34 -0
  37. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
  38. package/dest/sequencer/checkpoint_voter.js +85 -0
  39. package/dest/sequencer/config.d.ts +3 -2
  40. package/dest/sequencer/config.d.ts.map +1 -1
  41. package/dest/sequencer/errors.d.ts +1 -1
  42. package/dest/sequencer/errors.d.ts.map +1 -1
  43. package/dest/sequencer/events.d.ts +46 -0
  44. package/dest/sequencer/events.d.ts.map +1 -0
  45. package/dest/sequencer/events.js +1 -0
  46. package/dest/sequencer/index.d.ts +5 -1
  47. package/dest/sequencer/index.d.ts.map +1 -1
  48. package/dest/sequencer/index.js +4 -0
  49. package/dest/sequencer/metrics.d.ts +32 -3
  50. package/dest/sequencer/metrics.d.ts.map +1 -1
  51. package/dest/sequencer/metrics.js +192 -0
  52. package/dest/sequencer/sequencer.d.ts +96 -138
  53. package/dest/sequencer/sequencer.d.ts.map +1 -1
  54. package/dest/sequencer/sequencer.js +247 -479
  55. package/dest/sequencer/timetable.d.ts +54 -14
  56. package/dest/sequencer/timetable.d.ts.map +1 -1
  57. package/dest/sequencer/timetable.js +148 -59
  58. package/dest/sequencer/types.d.ts +3 -0
  59. package/dest/sequencer/types.d.ts.map +1 -0
  60. package/dest/sequencer/types.js +1 -0
  61. package/dest/sequencer/utils.d.ts +14 -8
  62. package/dest/sequencer/utils.d.ts.map +1 -1
  63. package/dest/sequencer/utils.js +7 -4
  64. package/dest/test/index.d.ts +4 -2
  65. package/dest/test/index.d.ts.map +1 -1
  66. package/dest/test/mock_checkpoint_builder.d.ts +83 -0
  67. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
  68. package/dest/test/mock_checkpoint_builder.js +179 -0
  69. package/dest/test/utils.d.ts +49 -0
  70. package/dest/test/utils.d.ts.map +1 -0
  71. package/dest/test/utils.js +94 -0
  72. package/dest/tx_validator/nullifier_cache.d.ts +1 -1
  73. package/dest/tx_validator/nullifier_cache.d.ts.map +1 -1
  74. package/dest/tx_validator/tx_validator_factory.d.ts +4 -3
  75. package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -1
  76. package/dest/tx_validator/tx_validator_factory.js +1 -1
  77. package/package.json +31 -30
  78. package/src/client/sequencer-client.ts +28 -38
  79. package/src/config.ts +81 -32
  80. package/src/global_variable_builder/global_builder.ts +56 -48
  81. package/src/index.ts +2 -0
  82. package/src/publisher/config.ts +20 -9
  83. package/src/publisher/sequencer-publisher-factory.ts +7 -5
  84. package/src/publisher/sequencer-publisher-metrics.ts +2 -2
  85. package/src/publisher/sequencer-publisher.ts +328 -161
  86. package/src/sequencer/README.md +531 -0
  87. package/src/sequencer/block_builder.ts +12 -13
  88. package/src/sequencer/checkpoint_builder.ts +217 -0
  89. package/src/sequencer/checkpoint_proposal_job.ts +706 -0
  90. package/src/sequencer/checkpoint_voter.ts +105 -0
  91. package/src/sequencer/config.ts +2 -1
  92. package/src/sequencer/events.ts +27 -0
  93. package/src/sequencer/index.ts +4 -0
  94. package/src/sequencer/metrics.ts +254 -3
  95. package/src/sequencer/sequencer.ts +360 -674
  96. package/src/sequencer/timetable.ts +173 -79
  97. package/src/sequencer/types.ts +6 -0
  98. package/src/sequencer/utils.ts +18 -9
  99. package/src/test/index.ts +3 -1
  100. package/src/test/mock_checkpoint_builder.ts +247 -0
  101. package/src/test/utils.ts +137 -0
  102. package/src/tx_validator/tx_validator_factory.ts +3 -2
@@ -4,29 +4,24 @@ function _ts_decorate(decorators, target, key, desc) {
4
4
  else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  }
7
- import { BLOBS_PER_BLOCK, FIELDS_PER_BLOB, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
8
- import { FormattedViemError, NoCommitteeError } from '@aztec/ethereum';
9
- import { omit, pick } from '@aztec/foundation/collection';
10
- import { randomInt } from '@aztec/foundation/crypto';
7
+ import { getKzg } from '@aztec/blob-lib';
8
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
9
+ import { NoCommitteeError } from '@aztec/ethereum/contracts';
10
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
11
+ import { merge, omit, pick } from '@aztec/foundation/collection';
12
+ import { Fr } from '@aztec/foundation/curves/bn254';
11
13
  import { EthAddress } from '@aztec/foundation/eth-address';
12
- import { Signature } from '@aztec/foundation/eth-signature';
13
- import { Fr } from '@aztec/foundation/fields';
14
14
  import { createLogger } from '@aztec/foundation/log';
15
15
  import { RunningPromise } from '@aztec/foundation/running-promise';
16
- import { Timer } from '@aztec/foundation/timer';
17
- import { unfreeze } from '@aztec/foundation/types';
18
- import { CommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
19
16
  import { getSlotAtTimestamp, getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
20
- import { Gas } from '@aztec/stdlib/gas';
21
17
  import { SequencerConfigSchema } from '@aztec/stdlib/interfaces/server';
22
- import { orderAttestations } from '@aztec/stdlib/p2p';
23
- import { CheckpointHeader } from '@aztec/stdlib/rollup';
24
18
  import { pickFromSchema } from '@aztec/stdlib/schemas';
25
19
  import { MerkleTreeId } from '@aztec/stdlib/trees';
26
- import { ContentCommitment } from '@aztec/stdlib/tx';
27
- import { AttestationTimeoutError } from '@aztec/stdlib/validators';
28
- import { Attributes, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
20
+ import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
29
21
  import EventEmitter from 'node:events';
22
+ import { DefaultSequencerConfig } from '../config.js';
23
+ import { CheckpointProposalJob } from './checkpoint_proposal_job.js';
24
+ import { CheckpointVoter } from './checkpoint_voter.js';
30
25
  import { SequencerInterruptedError, SequencerTooSlowError } from './errors.js';
31
26
  import { SequencerMetrics } from './metrics.js';
32
27
  import { SequencerTimetable } from './timetable.js';
@@ -34,12 +29,11 @@ import { SequencerState } from './utils.js';
34
29
  export { SequencerState };
35
30
  /**
36
31
  * Sequencer client
37
- * - Wins a period of time to become the sequencer (depending on finalized protocol).
38
- * - Chooses a set of txs from the tx pool to be in the rollup.
39
- * - Simulate the rollup of txs.
40
- * - Adds proof requests to the request pool (not for this milestone).
41
- * - Receives results to those proofs from the network (repeats as necessary) (not for this milestone).
42
- * - Publishes L1 tx(s) to the rollup contract via RollupPublisher.
32
+ * - Checks whether it is elected as proposer for the next slot
33
+ * - Builds multiple blocks and broadcasts them
34
+ * - Collects attestations for the checkpoint
35
+ * - Publishes the checkpoint to L1
36
+ * - Votes for proposals and slashes on L1
43
37
  */ export class Sequencer extends EventEmitter {
44
38
  publisherFactory;
45
39
  validatorClient;
@@ -49,111 +43,64 @@ export { SequencerState };
49
43
  slasherClient;
50
44
  l2BlockSource;
51
45
  l1ToL2MessageSource;
52
- blockBuilder;
46
+ checkpointsBuilder;
53
47
  l1Constants;
54
48
  dateProvider;
55
49
  epochCache;
56
50
  rollupContract;
57
- config;
58
51
  telemetry;
59
52
  log;
60
53
  runningPromise;
61
- pollingIntervalMs;
62
- maxTxsPerBlock;
63
- minTxsPerBlock;
64
- maxL1TxInclusionTimeIntoSlot;
65
54
  state;
66
- maxBlockSizeInBytes;
67
- maxBlockGas;
68
55
  metrics;
69
- lastBlockPublished;
70
- governanceProposerPayload;
71
56
  /** The last slot for which we attempted to vote when sync failed, to prevent duplicate attempts. */ lastSlotForVoteWhenSyncFailed;
57
+ /** The last slot for which we triggered a checkpoint proposal job, to prevent duplicate attempts. */ lastSlotForCheckpointProposalJob;
58
+ /** Last successful checkpoint proposed */ lastCheckpointProposed;
59
+ /** The last epoch for which we logged strategy comparison in fisherman mode. */ lastEpochForStrategyComparison;
72
60
  /** The maximum number of seconds that the sequencer can be into a slot to transition to a particular state. */ timetable;
73
- enforceTimeTable;
74
61
  // This shouldn't be here as this gets re-created each time we build/propose a block.
75
62
  // But we have a number of tests that abuse/rely on this class having a permanent publisher.
76
63
  // As long as those tests only configure a single publisher they will continue to work.
77
64
  // This will get re-assigned every time the sequencer goes to build a new block to a publisher that is valid
78
65
  // for the block proposer.
66
+ // TODO(palla/mbps): Remove this field and fix tests
79
67
  publisher;
80
- constructor(publisherFactory, validatorClient, globalsBuilder, p2pClient, worldState, slasherClient, l2BlockSource, l1ToL2MessageSource, blockBuilder, l1Constants, dateProvider, epochCache, rollupContract, config, telemetry = getTelemetryClient(), log = createLogger('sequencer')){
81
- super(), this.publisherFactory = publisherFactory, this.validatorClient = validatorClient, this.globalsBuilder = globalsBuilder, this.p2pClient = p2pClient, this.worldState = worldState, this.slasherClient = slasherClient, this.l2BlockSource = l2BlockSource, this.l1ToL2MessageSource = l1ToL2MessageSource, this.blockBuilder = blockBuilder, this.l1Constants = l1Constants, this.dateProvider = dateProvider, this.epochCache = epochCache, this.rollupContract = rollupContract, this.config = config, this.telemetry = telemetry, this.log = log, this.pollingIntervalMs = 1000, this.maxTxsPerBlock = 32, this.minTxsPerBlock = 1, this.maxL1TxInclusionTimeIntoSlot = 0, this.state = SequencerState.STOPPED, this.maxBlockSizeInBytes = 1024 * 1024, this.maxBlockGas = new Gas(100e9, 100e9), this.enforceTimeTable = false;
82
- this.metrics = new SequencerMetrics(telemetry, this.rollupContract, 'Sequencer');
83
- // Initialize config
84
- this.updateConfig(this.config);
85
- }
86
- get tracer() {
87
- return this.metrics.tracer;
88
- }
89
- getValidatorAddresses() {
90
- return this.validatorClient?.getValidatorAddresses();
91
- }
92
- getConfig() {
93
- return this.config;
94
- }
95
- /**
96
- * Updates sequencer config by the defined values in the config on input.
97
- * @param config - New parameters.
98
- */ updateConfig(config) {
99
- this.log.info(`Sequencer config set`, omit(pickFromSchema(config, SequencerConfigSchema), 'txPublicSetupAllowList'));
100
- if (config.transactionPollingIntervalMS !== undefined) {
101
- this.pollingIntervalMs = config.transactionPollingIntervalMS;
68
+ /** Config for the sequencer */ config;
69
+ constructor(publisherFactory, validatorClient, globalsBuilder, p2pClient, worldState, slasherClient, l2BlockSource, l1ToL2MessageSource, checkpointsBuilder, l1Constants, dateProvider, epochCache, rollupContract, config, telemetry = getTelemetryClient(), log = createLogger('sequencer')){
70
+ super(), this.publisherFactory = publisherFactory, this.validatorClient = validatorClient, this.globalsBuilder = globalsBuilder, this.p2pClient = p2pClient, this.worldState = worldState, this.slasherClient = slasherClient, this.l2BlockSource = l2BlockSource, this.l1ToL2MessageSource = l1ToL2MessageSource, this.checkpointsBuilder = checkpointsBuilder, this.l1Constants = l1Constants, this.dateProvider = dateProvider, this.epochCache = epochCache, this.rollupContract = rollupContract, this.telemetry = telemetry, this.log = log, this.state = SequencerState.STOPPED, this.config = DefaultSequencerConfig;
71
+ // Add [FISHERMAN] prefix to logger if in fisherman mode
72
+ if (config.fishermanMode) {
73
+ this.log = log.createChild('[FISHERMAN]');
102
74
  }
103
- if (config.maxTxsPerBlock !== undefined) {
104
- this.maxTxsPerBlock = config.maxTxsPerBlock;
105
- }
106
- if (config.minTxsPerBlock !== undefined) {
107
- this.minTxsPerBlock = config.minTxsPerBlock;
108
- }
109
- if (config.maxDABlockGas !== undefined) {
110
- this.maxBlockGas = new Gas(config.maxDABlockGas, this.maxBlockGas.l2Gas);
111
- }
112
- if (config.maxL2BlockGas !== undefined) {
113
- this.maxBlockGas = new Gas(this.maxBlockGas.daGas, config.maxL2BlockGas);
114
- }
115
- if (config.maxBlockSizeInBytes !== undefined) {
116
- this.maxBlockSizeInBytes = config.maxBlockSizeInBytes;
117
- }
118
- if (config.governanceProposerPayload) {
119
- this.governanceProposerPayload = config.governanceProposerPayload;
120
- }
121
- if (config.maxL1TxInclusionTimeIntoSlot !== undefined) {
122
- this.maxL1TxInclusionTimeIntoSlot = config.maxL1TxInclusionTimeIntoSlot;
123
- }
124
- if (config.enforceTimeTable !== undefined) {
125
- this.enforceTimeTable = config.enforceTimeTable;
126
- }
127
- this.setTimeTable();
128
- // TODO: Just read everything from the config object as needed instead of copying everything into local vars.
129
- // Update all values on this.config that are populated in the config object.
130
- Object.assign(this.config, config);
75
+ this.metrics = new SequencerMetrics(telemetry, this.rollupContract, 'Sequencer');
76
+ this.updateConfig(config);
131
77
  }
132
- setTimeTable() {
78
+ /** Updates sequencer config by the defined values and updates the timetable */ updateConfig(config) {
79
+ const filteredConfig = pickFromSchema(config, SequencerConfigSchema);
80
+ this.log.info(`Updated sequencer config`, omit(filteredConfig, 'txPublicSetupAllowList'));
81
+ this.config = merge(this.config, filteredConfig);
133
82
  this.timetable = new SequencerTimetable({
134
83
  ethereumSlotDuration: this.l1Constants.ethereumSlotDuration,
135
84
  aztecSlotDuration: this.aztecSlotDuration,
136
- maxL1TxInclusionTimeIntoSlot: this.maxL1TxInclusionTimeIntoSlot,
137
- attestationPropagationTime: this.config.attestationPropagationTime,
138
- enforce: this.enforceTimeTable
85
+ l1PublishingTime: this.l1PublishingTime,
86
+ p2pPropagationTime: this.config.attestationPropagationTime,
87
+ blockDurationMs: this.config.blockDurationMs,
88
+ enforce: this.config.enforceTimeTable
139
89
  }, this.metrics, this.log);
140
90
  }
141
- async init() {
91
+ /** Initializes the sequencer (precomputes tables and creates a publisher). Takes about 3s. */ async init() {
92
+ getKzg();
142
93
  this.publisher = (await this.publisherFactory.create(undefined)).publisher;
143
94
  }
144
- /**
145
- * Starts the sequencer and moves to IDLE state.
146
- */ start() {
147
- this.runningPromise = new RunningPromise(this.safeWork.bind(this), this.log, this.pollingIntervalMs);
95
+ /** Starts the sequencer and moves to IDLE state. */ start() {
96
+ this.runningPromise = new RunningPromise(this.safeWork.bind(this), this.log, this.config.sequencerPollingIntervalMS);
148
97
  this.setState(SequencerState.IDLE, undefined, {
149
98
  force: true
150
99
  });
151
100
  this.runningPromise.start();
152
101
  this.log.info('Started sequencer');
153
102
  }
154
- /**
155
- * Stops the sequencer from processing txs and moves to STOPPED state.
156
- */ async stop() {
103
+ /** Stops the sequencer from building blocks and moves to STOPPED state. */ async stop() {
157
104
  this.log.info(`Stopping sequencer`);
158
105
  this.setState(SequencerState.STOPPING, undefined, {
159
106
  force: true
@@ -165,29 +112,81 @@ export { SequencerState };
165
112
  });
166
113
  this.log.info('Stopped sequencer');
167
114
  }
168
- /**
169
- * Returns the current state of the sequencer.
170
- * @returns An object with a state entry with one of SequencerState.
171
- */ status() {
115
+ async safeWork() {
116
+ try {
117
+ await this.work();
118
+ } catch (err) {
119
+ this.emit('checkpoint-error', {
120
+ error: err
121
+ });
122
+ if (err instanceof SequencerTooSlowError) {
123
+ // TODO(palla/mbps): Add missing states
124
+ // Log as warn only if we had to abort halfway through the block proposal
125
+ const logLvl = [
126
+ SequencerState.INITIALIZING_CHECKPOINT,
127
+ SequencerState.PROPOSER_CHECK
128
+ ].includes(err.proposedState) ? 'debug' : 'warn';
129
+ this.log[logLvl](err.message, {
130
+ now: this.dateProvider.nowInSeconds()
131
+ });
132
+ } else {
133
+ // Re-throw other errors
134
+ throw err;
135
+ }
136
+ } finally{
137
+ this.setState(SequencerState.IDLE, undefined);
138
+ }
139
+ }
140
+ /** Returns the current state of the sequencer. */ status() {
172
141
  return {
173
142
  state: this.state
174
143
  };
175
144
  }
176
145
  /**
177
- * @notice Performs most of the sequencer duties:
178
- * - Checks if we are up to date
179
- * - If we are and we are the sequencer, collect txs and build a block
180
- * - Collect attestations for the block
181
- * - Submit block
182
- * - If our block for some reason is not included, revert the state
146
+ * Main sequencer loop:
147
+ * - Checks if we are up to date
148
+ * - If we are and we are the sequencer, collect txs and build blocks
149
+ * - Build multiple blocks per slot when configured
150
+ * - Collect attestations for the final block
151
+ * - Submit checkpoint
183
152
  */ async work() {
184
153
  this.setState(SequencerState.SYNCHRONIZING, undefined);
185
- const { slot, ts, now } = this.epochCache.getEpochAndSlotInNextL1Slot();
186
- // Check we have not already published a block for this slot (cheapest check)
187
- if (this.lastBlockPublished && this.lastBlockPublished.header.getSlot() >= slot) {
188
- this.log.debug(`Cannot propose block at next L2 slot ${slot} since that slot was taken by our own block ${this.lastBlockPublished.number}`);
154
+ const { slot, ts, now, epoch } = this.epochCache.getEpochAndSlotInNextL1Slot();
155
+ // Check if we are synced and it's our slot, grab a publisher, check previous block invalidation, etc
156
+ const checkpointProposalJob = await this.prepareCheckpointProposal(slot, ts, now);
157
+ if (!checkpointProposalJob) {
189
158
  return;
190
159
  }
160
+ // Execute the checkpoint proposal job
161
+ const checkpoint = await checkpointProposalJob.execute();
162
+ // Update last checkpoint proposed (currently unused)
163
+ if (checkpoint) {
164
+ this.lastCheckpointProposed = checkpoint;
165
+ }
166
+ // Log fee strategy comparison if on fisherman
167
+ if (this.config.fishermanMode && (this.lastEpochForStrategyComparison === undefined || epoch > this.lastEpochForStrategyComparison)) {
168
+ this.logStrategyComparison(epoch, checkpointProposalJob.getPublisher());
169
+ this.lastEpochForStrategyComparison = epoch;
170
+ }
171
+ return checkpoint;
172
+ }
173
+ /**
174
+ * Prepares the checkpoint proposal by performing all necessary checks and setup.
175
+ * This is the initial step in the main loop.
176
+ * @returns CheckpointProposalJob if successful, undefined if we are not yet synced or are not the proposer.
177
+ */ async prepareCheckpointProposal(slot, ts, now) {
178
+ // Check we have not already processed this slot (cheapest check)
179
+ // We only check this if enforce timetable is set, since we want to keep processing the same slot if we are not
180
+ // running against actual time (eg when we use sandbox-style automining)
181
+ if (this.lastSlotForCheckpointProposalJob && this.lastSlotForCheckpointProposalJob >= slot && this.config.enforceTimeTable) {
182
+ this.log.trace(`Slot ${slot} has already been processed`);
183
+ return undefined;
184
+ }
185
+ // But if we have already proposed for this slot, the we definitely have to skip it, automining or not
186
+ if (this.lastCheckpointProposed && this.lastCheckpointProposed.header.slotNumber >= slot) {
187
+ this.log.trace(`Slot ${slot} has already been published as checkpoint ${this.lastCheckpointProposed.number}`);
188
+ return undefined;
189
+ }
191
190
  // Check all components are synced to latest as seen by the archiver (queries all subsystems)
192
191
  const syncedTo = await this.checkSync({
193
192
  ts,
@@ -198,180 +197,107 @@ export { SequencerState };
198
197
  slot,
199
198
  ts
200
199
  });
201
- return;
200
+ return undefined;
202
201
  }
203
- const chainTipArchive = syncedTo.archive;
204
- const newBlockNumber = syncedTo.blockNumber + 1;
205
- const syncLogData = {
202
+ // TODO(palla/mbps): Compute proper checkpoint number
203
+ const checkpointNumber = CheckpointNumber.fromBlockNumber(BlockNumber(syncedTo.blockNumber + 1));
204
+ const logCtx = {
206
205
  now,
207
206
  syncedToL1Ts: syncedTo.l1Timestamp,
208
207
  syncedToL2Slot: getSlotAtTimestamp(syncedTo.l1Timestamp, this.l1Constants),
209
- nextL2Slot: slot,
210
- nextL2SlotTs: ts,
211
- l1SlotDuration: this.l1Constants.ethereumSlotDuration,
212
- newBlockNumber,
208
+ slot,
209
+ slotTs: ts,
210
+ checkpointNumber,
213
211
  isPendingChainValid: pick(syncedTo.pendingChainValidationStatus, 'valid', 'reason', 'invalidIndex')
214
212
  };
215
213
  // Check that we are a proposer for the next slot
216
214
  this.setState(SequencerState.PROPOSER_CHECK, slot);
217
215
  const [canPropose, proposer] = await this.checkCanPropose(slot);
218
- // If we are not a proposer, check if we should invalidate a invalid block, and bail
216
+ // If we are not a proposer check if we should invalidate a invalid block, and bail
219
217
  if (!canPropose) {
220
218
  await this.considerInvalidatingBlock(syncedTo, slot);
221
- return;
219
+ return undefined;
222
220
  }
223
221
  // Check that the slot is not taken by a block already (should never happen, since only us can propose for this slot)
224
222
  if (syncedTo.block && syncedTo.block.header.getSlot() >= slot) {
225
223
  this.log.warn(`Cannot propose block at next L2 slot ${slot} since that slot was taken by block ${syncedTo.blockNumber}`, {
226
- ...syncLogData,
224
+ ...logCtx,
227
225
  block: syncedTo.block.header.toInspect()
228
226
  });
229
- return;
227
+ this.metrics.recordBlockProposalPrecheckFailed('slot_already_taken');
228
+ return undefined;
230
229
  }
231
230
  // We now need to get ourselves a publisher.
232
231
  // The returned attestor will be the one we provided if we provided one.
233
232
  // Otherwise it will be a valid attestor for the returned publisher.
234
- const { attestorAddress, publisher } = await this.publisherFactory.create(proposer);
233
+ // In fisherman mode, pass undefined to use the fisherman's own keystore instead of the actual proposer's
234
+ const proposerForPublisher = this.config.fishermanMode ? undefined : proposer;
235
+ const { attestorAddress, publisher } = await this.publisherFactory.create(proposerForPublisher);
235
236
  this.log.verbose(`Created publisher at address ${publisher.getSenderAddress()} for attestor ${attestorAddress}`);
236
237
  this.publisher = publisher;
237
- const coinbase = this.validatorClient.getCoinbaseForAttestor(attestorAddress);
238
- const feeRecipient = this.validatorClient.getFeeRecipientForAttestor(attestorAddress);
238
+ // In fisherman mode, set the actual proposer's address for simulations
239
+ if (this.config.fishermanMode && proposer) {
240
+ publisher.setProposerAddressForSimulation(proposer);
241
+ this.log.debug(`Set proposer address ${proposer} for simulation in fisherman mode`);
242
+ }
239
243
  // Prepare invalidation request if the pending chain is invalid (returns undefined if no need)
244
+ // TODO(palla/mbps): We need to invalidate checkpoints, not blocks
240
245
  const invalidateBlock = await publisher.simulateInvalidateBlock(syncedTo.pendingChainValidationStatus);
241
- // Check with the rollup if we can indeed propose at the next L2 slot. This check should not fail
246
+ // Check with the rollup contract if we can indeed propose at the next L2 slot. This check should not fail
242
247
  // if all the previous checks are good, but we do it just in case.
243
- const canProposeCheck = await publisher.canProposeAtNextEthBlock(chainTipArchive, proposer ?? EthAddress.ZERO, invalidateBlock);
248
+ const canProposeCheck = await publisher.canProposeAtNextEthBlock(syncedTo.archive, proposer ?? EthAddress.ZERO, invalidateBlock);
244
249
  if (canProposeCheck === undefined) {
245
- this.log.warn(`Cannot propose block ${newBlockNumber} at slot ${slot} due to failed rollup contract check`, syncLogData);
250
+ this.log.warn(`Cannot propose checkpoint ${checkpointNumber} at slot ${slot} due to failed rollup contract check`, logCtx);
246
251
  this.emit('proposer-rollup-check-failed', {
247
- reason: 'Rollup contract check failed'
252
+ reason: 'Rollup contract check failed',
253
+ slot
248
254
  });
249
- return;
250
- } else if (canProposeCheck.slot !== slot) {
255
+ this.metrics.recordBlockProposalPrecheckFailed('rollup_contract_check_failed');
256
+ return undefined;
257
+ }
258
+ if (canProposeCheck.slot !== slot) {
251
259
  this.log.warn(`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}.`, {
252
- ...syncLogData,
260
+ ...logCtx,
253
261
  rollup: canProposeCheck,
254
- newBlockNumber,
255
262
  expectedSlot: slot
256
263
  });
257
264
  this.emit('proposer-rollup-check-failed', {
258
- reason: 'Slot mismatch'
265
+ reason: 'Slot mismatch',
266
+ slot
259
267
  });
260
- return;
261
- } else if (canProposeCheck.blockNumber !== BigInt(newBlockNumber)) {
262
- this.log.warn(`Cannot propose block due to block mismatch with rollup contract (this can be caused by a pending archiver sync). Expected block ${newBlockNumber} but got ${canProposeCheck.blockNumber}.`, {
263
- ...syncLogData,
268
+ this.metrics.recordBlockProposalPrecheckFailed('slot_mismatch');
269
+ return undefined;
270
+ }
271
+ if (canProposeCheck.checkpointNumber !== checkpointNumber) {
272
+ this.log.warn(`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}.`, {
273
+ ...logCtx,
264
274
  rollup: canProposeCheck,
265
- newBlockNumber,
266
275
  expectedSlot: slot
267
276
  });
268
277
  this.emit('proposer-rollup-check-failed', {
269
- reason: 'Block mismatch'
270
- });
271
- return;
272
- }
273
- this.log.debug(`Can propose block ${newBlockNumber} at slot ${slot} as ${proposer}`, {
274
- ...syncLogData
275
- });
276
- const newGlobalVariables = await this.globalsBuilder.buildGlobalVariables(newBlockNumber, coinbase, feeRecipient, slot);
277
- // Enqueue governance and slashing votes (returns promises that will be awaited later)
278
- const votesPromises = this.enqueueGovernanceAndSlashingVotes(publisher, attestorAddress, slot, newGlobalVariables.timestamp);
279
- // Enqueues block invalidation
280
- if (invalidateBlock && !this.config.skipInvalidateBlockAsProposer) {
281
- publisher.enqueueInvalidateBlock(invalidateBlock);
282
- }
283
- // Actual block building
284
- this.setState(SequencerState.INITIALIZING_PROPOSAL, slot);
285
- const block = await this.tryBuildBlockAndEnqueuePublish(slot, proposer, newBlockNumber, publisher, newGlobalVariables, chainTipArchive, invalidateBlock);
286
- // Wait until the voting promises have resolved, so all requests are enqueued
287
- await Promise.all(votesPromises);
288
- // And send the tx to L1
289
- const l1Response = await publisher.sendRequests();
290
- const proposedBlock = l1Response?.successfulActions.find((a)=>a === 'propose');
291
- if (proposedBlock) {
292
- this.lastBlockPublished = block;
293
- this.emit('block-published', {
294
- blockNumber: newBlockNumber,
295
- slot: Number(slot)
296
- });
297
- await this.metrics.incFilledSlot(publisher.getSenderAddress().toString(), coinbase);
298
- } else if (block) {
299
- this.emit('block-publish-failed', l1Response ?? {});
300
- }
301
- this.setState(SequencerState.IDLE, undefined);
302
- }
303
- /** Tries building a block proposal, and if successful, enqueues it for publishing. */ async tryBuildBlockAndEnqueuePublish(slot, proposer, newBlockNumber, publisher, newGlobalVariables, chainTipArchive, invalidateBlock) {
304
- this.log.verbose(`Preparing proposal for block ${newBlockNumber} at slot ${slot}`, {
305
- proposer,
306
- publisher: publisher.getSenderAddress(),
307
- globalVariables: newGlobalVariables.toInspect(),
308
- chainTipArchive,
309
- blockNumber: newBlockNumber,
310
- slot
311
- });
312
- const proposalHeader = CheckpointHeader.from({
313
- ...newGlobalVariables,
314
- timestamp: newGlobalVariables.timestamp,
315
- lastArchiveRoot: chainTipArchive,
316
- contentCommitment: ContentCommitment.empty(),
317
- totalManaUsed: Fr.ZERO
318
- });
319
- let block;
320
- const pendingTxCount = await this.p2pClient.getPendingTxCount();
321
- if (pendingTxCount >= this.minTxsPerBlock) {
322
- // We don't fetch exactly maxTxsPerBlock txs here because we may not need all of them if we hit a limit before,
323
- // and also we may need to fetch more if we don't have enough valid txs.
324
- const pendingTxs = this.p2pClient.iteratePendingTxs();
325
- try {
326
- block = await this.buildBlockAndEnqueuePublish(pendingTxs, proposalHeader, newGlobalVariables, proposer, invalidateBlock, publisher);
327
- } catch (err) {
328
- this.emit('block-build-failed', {
329
- reason: err.message
330
- });
331
- if (err instanceof FormattedViemError) {
332
- this.log.verbose(`Unable to build/enqueue block ${err.message}`);
333
- } else {
334
- this.log.error(`Error building/enqueuing block`, err, {
335
- blockNumber: newBlockNumber,
336
- slot
337
- });
338
- }
339
- }
340
- } else {
341
- this.log.verbose(`Not enough txs to build block ${newBlockNumber} at slot ${slot} (got ${pendingTxCount} txs, need ${this.minTxsPerBlock})`, {
342
- chainTipArchive,
343
- blockNumber: newBlockNumber,
278
+ reason: 'Block mismatch',
344
279
  slot
345
280
  });
346
- this.emit('tx-count-check-failed', {
347
- minTxs: this.minTxsPerBlock,
348
- availableTxs: pendingTxCount
349
- });
281
+ this.metrics.recordBlockProposalPrecheckFailed('block_number_mismatch');
282
+ return undefined;
350
283
  }
351
- return block;
284
+ this.lastSlotForCheckpointProposalJob = slot;
285
+ this.log.info(`Preparing checkpoint proposal ${checkpointNumber} at slot ${slot}`, {
286
+ ...logCtx,
287
+ proposer
288
+ });
289
+ // Create and return the checkpoint proposal job
290
+ return this.createCheckpointProposalJob(slot, checkpointNumber, syncedTo.blockNumber, proposer, publisher, attestorAddress, invalidateBlock);
352
291
  }
353
- async safeWork() {
354
- try {
355
- await this.work();
356
- } catch (err) {
357
- if (err instanceof SequencerTooSlowError) {
358
- // Log as warn only if we had to abort halfway through the block proposal
359
- const logLvl = [
360
- SequencerState.INITIALIZING_PROPOSAL,
361
- SequencerState.PROPOSER_CHECK
362
- ].includes(err.proposedState) ? 'debug' : 'warn';
363
- this.log[logLvl](err.message, {
364
- now: this.dateProvider.nowInSeconds()
365
- });
366
- } else {
367
- // Re-throw other errors
368
- throw err;
369
- }
370
- } finally{
371
- this.setState(SequencerState.IDLE, undefined);
372
- }
292
+ createCheckpointProposalJob(slot, checkpointNumber, syncedToBlockNumber, proposer, publisher, attestorAddress, invalidateBlock) {
293
+ return new CheckpointProposalJob(slot, checkpointNumber, syncedToBlockNumber, proposer, publisher, attestorAddress, invalidateBlock, this.validatorClient, this.globalsBuilder, this.p2pClient, this.worldState, this.l1ToL2MessageSource, this.checkpointsBuilder, this.l1Constants, this.config, this.timetable, this.slasherClient, this.epochCache, this.dateProvider, this.metrics, this, this.setState.bind(this), this.log);
373
294
  }
374
- setState(proposedState, slotNumber, opts = {}) {
295
+ /**
296
+ * Internal helper for setting the sequencer state and checks if we have enough time left in the slot to transition to the new state.
297
+ * @param proposedState - The new state to transition to.
298
+ * @param slotNumber - The current slot number.
299
+ * @param force - Whether to force the transition even if the sequencer is stopped.
300
+ */ setState(proposedState, slotNumber, opts = {}) {
375
301
  if (this.state === SequencerState.STOPPING && proposedState !== SequencerState.STOPPED && !opts.force) {
376
302
  this.log.warn(`Cannot set sequencer to ${proposedState} as it is stopping.`);
377
303
  throw new SequencerInterruptedError();
@@ -398,181 +324,10 @@ export { SequencerState };
398
324
  oldState: this.state,
399
325
  newState: proposedState,
400
326
  secondsIntoSlot,
401
- slotNumber
327
+ slot: slotNumber
402
328
  });
403
329
  this.state = proposedState;
404
330
  }
405
- async dropFailedTxsFromP2P(failedTxs) {
406
- if (failedTxs.length === 0) {
407
- return;
408
- }
409
- const failedTxData = failedTxs.map((fail)=>fail.tx);
410
- const failedTxHashes = failedTxData.map((tx)=>tx.getTxHash());
411
- this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
412
- await this.p2pClient.deleteTxs(failedTxHashes);
413
- }
414
- getBlockBuilderOptions(slot) {
415
- // Deadline for processing depends on whether we're proposing a block
416
- const secondsIntoSlot = this.getSecondsIntoSlot(slot);
417
- const processingEndTimeWithinSlot = this.timetable.getBlockProposalExecTimeEnd(secondsIntoSlot);
418
- // Deadline is only set if enforceTimeTable is enabled.
419
- const deadline = this.enforceTimeTable ? new Date((this.getSlotStartBuildTimestamp(slot) + processingEndTimeWithinSlot) * 1000) : undefined;
420
- return {
421
- maxTransactions: this.maxTxsPerBlock,
422
- maxBlockSize: this.maxBlockSizeInBytes,
423
- maxBlockGas: this.maxBlockGas,
424
- maxBlobFields: BLOBS_PER_BLOCK * FIELDS_PER_BLOB,
425
- deadline
426
- };
427
- }
428
- /**
429
- * @notice Build and propose a block to the chain
430
- *
431
- * @dev MUST throw instead of exiting early to ensure that world-state
432
- * is being rolled back if the block is dropped.
433
- *
434
- * @param pendingTxs - Iterable of pending transactions to construct the block from
435
- * @param proposalHeader - The partial header constructed for the proposal
436
- * @param newGlobalVariables - The global variables for the new block
437
- * @param proposerAddress - The address of the proposer
438
- */ async buildBlockAndEnqueuePublish(pendingTxs, proposalHeader, newGlobalVariables, proposerAddress, invalidateBlock, publisher) {
439
- await publisher.validateBlockHeader(proposalHeader, invalidateBlock);
440
- const blockNumber = newGlobalVariables.blockNumber;
441
- const slot = proposalHeader.slotNumber.toBigInt();
442
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(blockNumber);
443
- const workTimer = new Timer();
444
- this.setState(SequencerState.CREATING_BLOCK, slot);
445
- try {
446
- const blockBuilderOptions = this.getBlockBuilderOptions(Number(slot));
447
- const buildBlockRes = await this.blockBuilder.buildBlock(pendingTxs, l1ToL2Messages, newGlobalVariables, blockBuilderOptions);
448
- const { publicGas, block, publicProcessorDuration, numTxs, numMsgs, blockBuildingTimer, usedTxs, failedTxs } = buildBlockRes;
449
- const blockBuildDuration = workTimer.ms();
450
- await this.dropFailedTxsFromP2P(failedTxs);
451
- const minTxsPerBlock = this.minTxsPerBlock;
452
- if (numTxs < minTxsPerBlock) {
453
- this.log.warn(`Block ${blockNumber} has too few txs to be proposed (got ${numTxs} but required ${minTxsPerBlock})`, {
454
- slot,
455
- blockNumber,
456
- numTxs
457
- });
458
- throw new Error(`Block has too few successful txs to be proposed`);
459
- }
460
- // TODO(@PhilWindle) We should probably periodically check for things like another
461
- // block being published before ours instead of just waiting on our block
462
- await publisher.validateBlockHeader(block.getCheckpointHeader(), invalidateBlock);
463
- const blockStats = {
464
- eventName: 'l2-block-built',
465
- creator: proposerAddress?.toString() ?? publisher.getSenderAddress().toString(),
466
- duration: workTimer.ms(),
467
- publicProcessDuration: publicProcessorDuration,
468
- rollupCircuitsDuration: blockBuildingTimer.ms(),
469
- ...block.getStats()
470
- };
471
- const blockHash = await block.hash();
472
- const txHashes = block.body.txEffects.map((tx)=>tx.txHash);
473
- this.log.info(`Built block ${block.number} for slot ${slot} with ${numTxs} txs and ${numMsgs} messages. ${publicGas.l2Gas / workTimer.s()} mana/s`, {
474
- blockHash,
475
- globalVariables: block.header.globalVariables.toInspect(),
476
- txHashes,
477
- ...blockStats
478
- });
479
- this.log.debug('Collecting attestations');
480
- const attestations = await this.collectAttestations(block, usedTxs, proposerAddress);
481
- if (attestations !== undefined) {
482
- this.log.verbose(`Collected ${attestations.length} attestations`, {
483
- blockHash,
484
- blockNumber
485
- });
486
- }
487
- const attestationsAndSigners = new CommitteeAttestationsAndSigners(attestations ?? []);
488
- const attestationsAndSignersSignature = this.validatorClient ? await this.validatorClient.signAttestationsAndSigners(attestationsAndSigners, proposerAddress ?? publisher.getSenderAddress()) : Signature.empty();
489
- await this.enqueuePublishL2Block(block, attestationsAndSigners, attestationsAndSignersSignature, invalidateBlock, publisher);
490
- this.metrics.recordBuiltBlock(blockBuildDuration, publicGas.l2Gas);
491
- return block;
492
- } catch (err) {
493
- this.metrics.recordFailedBlock();
494
- throw err;
495
- }
496
- }
497
- async collectAttestations(block, txs, proposerAddress) {
498
- const { committee } = await this.epochCache.getCommittee(block.header.getSlot());
499
- // We checked above that the committee is defined, so this should never happen.
500
- if (!committee) {
501
- throw new Error('No committee when collecting attestations');
502
- }
503
- if (committee.length === 0) {
504
- this.log.verbose(`Attesting committee is empty`);
505
- return undefined;
506
- } else {
507
- this.log.debug(`Attesting committee length is ${committee.length}`);
508
- }
509
- if (!this.validatorClient) {
510
- const msg = 'Missing validator client: Cannot collect attestations';
511
- this.log.error(msg);
512
- throw new Error(msg);
513
- }
514
- const numberOfRequiredAttestations = Math.floor(committee.length * 2 / 3) + 1;
515
- const slotNumber = block.header.globalVariables.slotNumber.toBigInt();
516
- this.setState(SequencerState.COLLECTING_ATTESTATIONS, slotNumber);
517
- this.log.debug('Creating block proposal for validators');
518
- const blockProposalOptions = {
519
- publishFullTxs: !!this.config.publishTxsWithProposals,
520
- broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
521
- };
522
- const proposal = await this.validatorClient.createBlockProposal(block.header.globalVariables.blockNumber, block.getCheckpointHeader(), block.archive.root, block.header.state, txs, proposerAddress, blockProposalOptions);
523
- if (!proposal) {
524
- throw new Error(`Failed to create block proposal`);
525
- }
526
- if (this.config.skipCollectingAttestations) {
527
- this.log.warn('Skipping attestation collection as per config (attesting with own keys only)');
528
- const attestations = await this.validatorClient?.collectOwnAttestations(proposal);
529
- return orderAttestations(attestations ?? [], committee);
530
- }
531
- this.log.debug('Broadcasting block proposal to validators');
532
- await this.validatorClient.broadcastBlockProposal(proposal);
533
- const attestationTimeAllowed = this.enforceTimeTable ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_BLOCK) : this.aztecSlotDuration;
534
- this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
535
- const timer = new Timer();
536
- let collectedAttestationsCount = 0;
537
- try {
538
- const attestationDeadline = new Date(this.dateProvider.now() + attestationTimeAllowed * 1000);
539
- const attestations = await this.validatorClient.collectAttestations(proposal, numberOfRequiredAttestations, attestationDeadline);
540
- collectedAttestationsCount = attestations.length;
541
- // note: the smart contract requires that the signatures are provided in the order of the committee
542
- const sorted = orderAttestations(attestations, committee);
543
- if (this.config.injectFakeAttestation) {
544
- const nonEmpty = sorted.filter((a)=>!a.signature.isEmpty());
545
- const randomIndex = randomInt(nonEmpty.length);
546
- this.log.warn(`Injecting fake attestation in block ${block.number}`);
547
- unfreeze(nonEmpty[randomIndex]).signature = Signature.random();
548
- }
549
- return sorted;
550
- } catch (err) {
551
- if (err && err instanceof AttestationTimeoutError) {
552
- collectedAttestationsCount = err.collectedCount;
553
- }
554
- throw err;
555
- } finally{
556
- this.metrics.recordCollectedAttestations(collectedAttestationsCount, timer.ms());
557
- }
558
- }
559
- /**
560
- * Publishes the L2Block to the rollup contract.
561
- * @param block - The L2Block to be published.
562
- */ async enqueuePublishL2Block(block, attestationsAndSigners, attestationsAndSignersSignature, invalidateBlock, publisher) {
563
- // Publishes new block to the network and awaits the tx to be mined
564
- this.setState(SequencerState.PUBLISHING_BLOCK, block.header.globalVariables.slotNumber.toBigInt());
565
- // Time out tx at the end of the slot
566
- const slot = block.header.globalVariables.slotNumber.toNumber();
567
- const txTimeoutAt = new Date((this.getSlotStartBuildTimestamp(slot) + this.aztecSlotDuration) * 1000);
568
- const enqueued = await publisher.enqueueProposeL2Block(block, attestationsAndSigners, attestationsAndSignersSignature, {
569
- txTimeoutAt,
570
- forcePendingBlockNumber: invalidateBlock?.forcePendingBlockNumber
571
- });
572
- if (!enqueued) {
573
- throw new Error(`Failed to enqueue publish of block ${block.number}`);
574
- }
575
- }
576
331
  /**
577
332
  * Returns whether all dependencies have caught up.
578
333
  * We don't check against the previous block submitted since it may have been reorg'd out.
@@ -601,9 +356,9 @@ export { SequencerState };
601
356
  this.l2BlockSource.getPendingChainValidationStatus()
602
357
  ]);
603
358
  const [worldState, l2BlockSource, p2p, l1ToL2MessageSource, pendingChainValidationStatus] = syncedBlocks;
604
- // The archiver reports 'undefined' hash for the genesis block
605
- // because it doesn't have access to world state to compute it (facepalm)
606
- const result = l2BlockSource.hash === undefined ? worldState.number === 0 && p2p.number === 0 && l1ToL2MessageSource.number === 0 : worldState.hash === l2BlockSource.hash && p2p.hash === l2BlockSource.hash && l1ToL2MessageSource.hash === l2BlockSource.hash;
359
+ // 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,
360
+ // as the world state can compute the new genesis block hash, but other components use the hardcoded constant.
361
+ const result = l2BlockSource.number === 0 && worldState.number === 0 && p2p.number === 0 && l1ToL2MessageSource.number === 0 || worldState.hash === l2BlockSource.hash && p2p.hash === l2BlockSource.hash && l1ToL2MessageSource.hash === l2BlockSource.hash;
607
362
  if (!result) {
608
363
  this.log.debug(`Sequencer sync check failed`, {
609
364
  worldState,
@@ -618,13 +373,13 @@ export { SequencerState };
618
373
  if (blockNumber < INITIAL_L2_BLOCK_NUM) {
619
374
  const archive = new Fr((await this.worldState.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
620
375
  return {
621
- blockNumber: INITIAL_L2_BLOCK_NUM - 1,
376
+ blockNumber: BlockNumber(INITIAL_L2_BLOCK_NUM - 1),
622
377
  archive,
623
378
  l1Timestamp,
624
379
  pendingChainValidationStatus
625
380
  };
626
381
  }
627
- const block = await this.l2BlockSource.getBlock(blockNumber);
382
+ const block = await this.l2BlockSource.getL2BlockNew(blockNumber);
628
383
  if (!block) {
629
384
  // this shouldn't really happen because a moment ago we checked that all components were in sync
630
385
  this.log.error(`Failed to get L2 block ${blockNumber} from the archiver with all components in sync`);
@@ -639,41 +394,6 @@ export { SequencerState };
639
394
  };
640
395
  }
641
396
  /**
642
- * Enqueues governance and slashing votes with the publisher. Does not block.
643
- * @param publisher - The publisher to enqueue votes with
644
- * @param attestorAddress - The attestor address to use for signing
645
- * @param slot - The slot number
646
- * @param timestamp - The timestamp for the votes
647
- * @param context - Optional context for logging (e.g., block number)
648
- * @returns A tuple of [governanceEnqueued, slashingEnqueued]
649
- */ enqueueGovernanceAndSlashingVotes(publisher, attestorAddress, slot, timestamp) {
650
- try {
651
- const signerFn = (msg)=>this.validatorClient.signWithAddress(attestorAddress, msg).then((s)=>s.toString());
652
- const enqueueGovernancePromise = this.governanceProposerPayload && !this.governanceProposerPayload.isZero() ? publisher.enqueueGovernanceCastSignal(this.governanceProposerPayload, slot, timestamp, attestorAddress, signerFn).catch((err)=>{
653
- this.log.error(`Error enqueuing governance vote`, err, {
654
- slot
655
- });
656
- return false;
657
- }) : undefined;
658
- const enqueueSlashingPromise = this.slasherClient ? this.slasherClient.getProposerActions(slot).then((actions)=>publisher.enqueueSlashingActions(actions, slot, timestamp, attestorAddress, signerFn)).catch((err)=>{
659
- this.log.error(`Error enqueuing slashing actions`, err, {
660
- slot
661
- });
662
- return false;
663
- }) : undefined;
664
- return [
665
- enqueueGovernancePromise,
666
- enqueueSlashingPromise
667
- ];
668
- } catch (err) {
669
- this.log.error(`Error enqueueing governance and slashing votes`, err);
670
- return [
671
- undefined,
672
- undefined
673
- ];
674
- }
675
- }
676
- /**
677
397
  * Checks if we are the proposer for the next slot.
678
398
  * @returns True if we can propose, and the proposer address (undefined if anyone can propose)
679
399
  */ async checkCanPropose(slot) {
@@ -701,6 +421,13 @@ export { SequencerState };
701
421
  undefined
702
422
  ];
703
423
  }
424
+ // In fisherman mode, just return the current proposer
425
+ if (this.config.fishermanMode) {
426
+ return [
427
+ true,
428
+ proposer
429
+ ];
430
+ }
704
431
  const validatorAddresses = this.validatorClient.getValidatorAddresses();
705
432
  const weAreProposer = validatorAddresses.some((addr)=>addr.equals(proposer));
706
433
  if (!weAreProposer) {
@@ -722,32 +449,32 @@ export { SequencerState };
722
449
  * Tries to vote on slashing actions and governance when the sync check fails but we're past the max time for initializing a proposal.
723
450
  * This allows the sequencer to participate in governance/slashing votes even when it cannot build blocks.
724
451
  */ async tryVoteWhenSyncFails(args) {
725
- const { slot, ts } = args;
452
+ const { slot } = args;
726
453
  // Prevent duplicate attempts in the same slot
727
454
  if (this.lastSlotForVoteWhenSyncFailed === slot) {
728
- this.log.debug(`Already attempted to vote in slot ${slot} (skipping)`);
455
+ this.log.trace(`Already attempted to vote in slot ${slot} (skipping)`);
729
456
  return;
730
457
  }
731
458
  // Check if we're past the max time for initializing a proposal
732
459
  const secondsIntoSlot = this.getSecondsIntoSlot(slot);
733
- const maxAllowedTime = this.timetable.getMaxAllowedTime(SequencerState.INITIALIZING_PROPOSAL);
460
+ const maxAllowedTime = this.timetable.getMaxAllowedTime(SequencerState.INITIALIZING_CHECKPOINT);
734
461
  // If we haven't exceeded the time limit for initializing a proposal, don't proceed with voting
735
462
  // We use INITIALIZING_PROPOSAL time limit because if we're past that, we can't build a block anyway
736
463
  if (maxAllowedTime === undefined || secondsIntoSlot <= maxAllowedTime) {
737
- this.log.trace(`Not attempting to vote since there is still for block building`, {
464
+ this.log.trace(`Not attempting to vote since there is still time for block building`, {
738
465
  secondsIntoSlot,
739
466
  maxAllowedTime
740
467
  });
741
468
  return;
742
469
  }
743
- this.log.debug(`Sync for slot ${slot} failed, checking for voting opportunities`, {
470
+ this.log.trace(`Sync for slot ${slot} failed, checking for voting opportunities`, {
744
471
  secondsIntoSlot,
745
472
  maxAllowedTime
746
473
  });
747
474
  // Check if we're a proposer or proposal is open
748
475
  const [canPropose, proposer] = await this.checkCanPropose(slot);
749
476
  if (!canPropose) {
750
- this.log.debug(`Cannot vote in slot ${slot} since we are not a proposer`, {
477
+ this.log.trace(`Cannot vote in slot ${slot} since we are not a proposer`, {
751
478
  slot,
752
479
  proposer
753
480
  });
@@ -761,10 +488,11 @@ export { SequencerState };
761
488
  attestorAddress,
762
489
  slot
763
490
  });
764
- // Enqueue governance and slashing votes using the shared helper method
765
- const votesPromises = this.enqueueGovernanceAndSlashingVotes(publisher, attestorAddress, slot, ts);
766
- await Promise.all(votesPromises);
767
- if (votesPromises.every((p)=>!p)) {
491
+ // Enqueue governance and slashing votes
492
+ const voter = new CheckpointVoter(slot, publisher, attestorAddress, this.validatorClient, this.slasherClient, this.l1Constants, this.config, this.metrics, this.log);
493
+ const votesPromises = voter.enqueueVotes();
494
+ const votes = await Promise.all(votesPromises);
495
+ if (votes.every((p)=>!p)) {
768
496
  this.log.debug(`No votes to enqueue for slot ${slot}`);
769
497
  return;
770
498
  }
@@ -783,7 +511,6 @@ export { SequencerState };
783
511
  if (pendingChainValidationStatus.valid) {
784
512
  return;
785
513
  }
786
- const { publisher } = await this.publisherFactory.create(undefined);
787
514
  const invalidBlockNumber = pendingChainValidationStatus.block.blockNumber;
788
515
  const invalidBlockTimestamp = pendingChainValidationStatus.block.timestamp;
789
516
  const timeSinceChainInvalid = this.dateProvider.nowInSeconds() - Number(invalidBlockTimestamp);
@@ -805,6 +532,21 @@ export { SequencerState };
805
532
  this.log.debug(`Not invalidating pending chain`, logData);
806
533
  return;
807
534
  }
535
+ let validatorToUse;
536
+ if (invalidateAsCommitteeMember) {
537
+ // When invalidating as a committee member, use first validator that's actually in the committee
538
+ const { committee } = await this.epochCache.getCommittee(currentSlot);
539
+ if (committee) {
540
+ const committeeSet = new Set(committee.map((addr)=>addr.toString()));
541
+ validatorToUse = ourValidatorAddresses.find((addr)=>committeeSet.has(addr.toString())) ?? ourValidatorAddresses[0];
542
+ } else {
543
+ validatorToUse = ourValidatorAddresses[0];
544
+ }
545
+ } else {
546
+ // When invalidating as a non-committee member, use the first validator
547
+ validatorToUse = ourValidatorAddresses[0];
548
+ }
549
+ const { publisher } = await this.publisherFactory.create(validatorToUse);
808
550
  const invalidateBlock = await publisher.simulateInvalidateBlock(pendingChainValidationStatus);
809
551
  if (!invalidateBlock) {
810
552
  this.log.warn(`Failed to simulate invalidate block`, logData);
@@ -812,7 +554,38 @@ export { SequencerState };
812
554
  }
813
555
  this.log.info(invalidateAsCommitteeMember ? `Invalidating block ${invalidBlockNumber} as committee member` : `Invalidating block ${invalidBlockNumber} as non-committee member`, logData);
814
556
  publisher.enqueueInvalidateBlock(invalidateBlock);
815
- await publisher.sendRequests();
557
+ if (!this.config.fishermanMode) {
558
+ await publisher.sendRequests();
559
+ } else {
560
+ this.log.info('Invalidating block in fisherman mode, clearing pending requests');
561
+ publisher.clearPendingRequests();
562
+ }
563
+ }
564
+ logStrategyComparison(epoch, publisher) {
565
+ const feeAnalyzer = publisher.getL1FeeAnalyzer();
566
+ if (!feeAnalyzer) {
567
+ return;
568
+ }
569
+ const comparison = feeAnalyzer.getStrategyComparison();
570
+ if (comparison.length === 0) {
571
+ this.log.debug(`No strategy data available yet for epoch ${epoch}`);
572
+ return;
573
+ }
574
+ this.log.info(`L1 Fee Strategy Performance Report - End of Epoch ${epoch}`, {
575
+ epoch: Number(epoch),
576
+ totalAnalyses: comparison[0]?.totalAnalyses,
577
+ strategies: comparison.map((s)=>({
578
+ id: s.strategyId,
579
+ name: s.strategyName,
580
+ inclusionRate: `${(s.inclusionRate * 100).toFixed(1)}%`,
581
+ inclusionCount: `${s.inclusionCount}/${s.totalAnalyses}`,
582
+ avgCostEth: s.avgEstimatedCostEth.toFixed(6),
583
+ totalCostEth: s.totalEstimatedCostEth.toFixed(6),
584
+ avgOverpaymentEth: s.avgOverpaymentEth.toFixed(6),
585
+ totalOverpaymentEth: s.totalOverpaymentEth.toFixed(6),
586
+ avgPriorityFeeDeltaGwei: s.avgPriorityFeeDeltaGwei.toFixed(2)
587
+ }))
588
+ });
816
589
  }
817
590
  getSlotStartBuildTimestamp(slotNumber) {
818
591
  return getSlotStartBuildTimestamp(slotNumber, this.l1Constants);
@@ -830,24 +603,19 @@ export { SequencerState };
830
603
  getSlasherClient() {
831
604
  return this.slasherClient;
832
605
  }
606
+ get tracer() {
607
+ return this.metrics.tracer;
608
+ }
609
+ getValidatorAddresses() {
610
+ return this.validatorClient?.getValidatorAddresses();
611
+ }
612
+ getConfig() {
613
+ return this.config;
614
+ }
615
+ get l1PublishingTime() {
616
+ return this.config.l1PublishingTime ?? this.l1Constants.ethereumSlotDuration;
617
+ }
833
618
  }
834
619
  _ts_decorate([
835
620
  trackSpan('Sequencer.work')
836
621
  ], Sequencer.prototype, "safeWork", null);
837
- _ts_decorate([
838
- trackSpan('Sequencer.buildBlockAndEnqueuePublish', (_validTxs, _proposalHeader, newGlobalVariables)=>({
839
- [Attributes.BLOCK_NUMBER]: newGlobalVariables.blockNumber
840
- }))
841
- ], Sequencer.prototype, "buildBlockAndEnqueuePublish", null);
842
- _ts_decorate([
843
- trackSpan('Sequencer.collectAttestations', (block, txHashes)=>({
844
- [Attributes.BLOCK_NUMBER]: block.number,
845
- [Attributes.BLOCK_ARCHIVE]: block.archive.toString(),
846
- [Attributes.BLOCK_TXS_COUNT]: txHashes.length
847
- }))
848
- ], Sequencer.prototype, "collectAttestations", null);
849
- _ts_decorate([
850
- trackSpan('Sequencer.enqueuePublishL2Block', (block)=>({
851
- [Attributes.BLOCK_NUMBER]: block.number
852
- }))
853
- ], Sequencer.prototype, "enqueuePublishL2Block", null);