@aztec/sequencer-client 0.0.0-test.0

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