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