@aztec/sequencer-client 5.0.0-private.20260319 → 5.0.0-rc.2

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 (101) hide show
  1. package/README.md +281 -21
  2. package/dest/client/sequencer-client.d.ts +8 -3
  3. package/dest/client/sequencer-client.d.ts.map +1 -1
  4. package/dest/client/sequencer-client.js +17 -32
  5. package/dest/config.d.ts +10 -4
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +49 -25
  8. package/dest/global_variable_builder/fee_predictor.d.ts +37 -0
  9. package/dest/global_variable_builder/fee_predictor.d.ts.map +1 -0
  10. package/dest/global_variable_builder/fee_predictor.js +128 -0
  11. package/dest/global_variable_builder/fee_provider.d.ts +21 -0
  12. package/dest/global_variable_builder/fee_provider.d.ts.map +1 -0
  13. package/dest/global_variable_builder/fee_provider.js +67 -0
  14. package/dest/global_variable_builder/global_builder.d.ts +13 -26
  15. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  16. package/dest/global_variable_builder/global_builder.js +9 -67
  17. package/dest/global_variable_builder/index.d.ts +4 -2
  18. package/dest/global_variable_builder/index.d.ts.map +1 -1
  19. package/dest/global_variable_builder/index.js +2 -0
  20. package/dest/publisher/config.d.ts +19 -3
  21. package/dest/publisher/config.d.ts.map +1 -1
  22. package/dest/publisher/config.js +30 -5
  23. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +3 -4
  24. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -1
  25. package/dest/publisher/sequencer-bundle-simulator.d.ts +96 -0
  26. package/dest/publisher/sequencer-bundle-simulator.d.ts.map +1 -0
  27. package/dest/publisher/sequencer-bundle-simulator.js +198 -0
  28. package/dest/publisher/sequencer-publisher-factory.d.ts +3 -5
  29. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  30. package/dest/publisher/sequencer-publisher-factory.js +2 -3
  31. package/dest/publisher/sequencer-publisher.d.ts +84 -65
  32. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  33. package/dest/publisher/sequencer-publisher.js +403 -531
  34. package/dest/sequencer/automine/automine_factory.d.ts +56 -0
  35. package/dest/sequencer/automine/automine_factory.d.ts.map +1 -0
  36. package/dest/sequencer/automine/automine_factory.js +85 -0
  37. package/dest/sequencer/automine/automine_sequencer.d.ts +189 -0
  38. package/dest/sequencer/automine/automine_sequencer.d.ts.map +1 -0
  39. package/dest/sequencer/automine/automine_sequencer.js +689 -0
  40. package/dest/sequencer/automine/index.d.ts +3 -0
  41. package/dest/sequencer/automine/index.d.ts.map +1 -0
  42. package/dest/sequencer/automine/index.js +2 -0
  43. package/dest/sequencer/checkpoint_proposal_job.d.ts +73 -21
  44. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  45. package/dest/sequencer/checkpoint_proposal_job.js +709 -200
  46. package/dest/sequencer/checkpoint_proposal_job_metrics.d.ts +34 -0
  47. package/dest/sequencer/checkpoint_proposal_job_metrics.d.ts.map +1 -0
  48. package/dest/sequencer/checkpoint_proposal_job_metrics.js +72 -0
  49. package/dest/sequencer/checkpoint_voter.d.ts +1 -2
  50. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -1
  51. package/dest/sequencer/checkpoint_voter.js +2 -5
  52. package/dest/sequencer/errors.d.ts +1 -8
  53. package/dest/sequencer/errors.d.ts.map +1 -1
  54. package/dest/sequencer/errors.js +0 -9
  55. package/dest/sequencer/events.d.ts +61 -4
  56. package/dest/sequencer/events.d.ts.map +1 -1
  57. package/dest/sequencer/metrics.d.ts +9 -10
  58. package/dest/sequencer/metrics.d.ts.map +1 -1
  59. package/dest/sequencer/metrics.js +34 -20
  60. package/dest/sequencer/sequencer.d.ts +117 -30
  61. package/dest/sequencer/sequencer.d.ts.map +1 -1
  62. package/dest/sequencer/sequencer.js +483 -158
  63. package/dest/sequencer/types.d.ts +2 -2
  64. package/dest/sequencer/types.d.ts.map +1 -1
  65. package/dest/test/index.d.ts +3 -3
  66. package/dest/test/index.d.ts.map +1 -1
  67. package/dest/test/utils.d.ts +15 -1
  68. package/dest/test/utils.d.ts.map +1 -1
  69. package/dest/test/utils.js +24 -7
  70. package/package.json +28 -27
  71. package/src/client/sequencer-client.ts +29 -41
  72. package/src/config.ts +58 -25
  73. package/src/global_variable_builder/README.md +44 -0
  74. package/src/global_variable_builder/fee_predictor.ts +172 -0
  75. package/src/global_variable_builder/fee_provider.ts +80 -0
  76. package/src/global_variable_builder/global_builder.ts +19 -89
  77. package/src/global_variable_builder/index.ts +3 -1
  78. package/src/publisher/config.ts +62 -7
  79. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +3 -1
  80. package/src/publisher/sequencer-bundle-simulator.ts +254 -0
  81. package/src/publisher/sequencer-publisher-factory.ts +3 -6
  82. package/src/publisher/sequencer-publisher.ts +456 -578
  83. package/src/sequencer/automine/README.md +60 -0
  84. package/src/sequencer/automine/automine_factory.ts +152 -0
  85. package/src/sequencer/automine/automine_sequencer.ts +795 -0
  86. package/src/sequencer/automine/index.ts +6 -0
  87. package/src/sequencer/checkpoint_proposal_job.ts +817 -225
  88. package/src/sequencer/checkpoint_proposal_job_metrics.ts +128 -0
  89. package/src/sequencer/checkpoint_voter.ts +1 -12
  90. package/src/sequencer/errors.ts +0 -15
  91. package/src/sequencer/events.ts +65 -4
  92. package/src/sequencer/metrics.ts +43 -24
  93. package/src/sequencer/sequencer.ts +557 -175
  94. package/src/sequencer/types.ts +1 -1
  95. package/src/test/index.ts +2 -2
  96. package/src/test/utils.ts +60 -10
  97. package/dest/sequencer/timetable.d.ts +0 -88
  98. package/dest/sequencer/timetable.d.ts.map +0 -1
  99. package/dest/sequencer/timetable.js +0 -222
  100. package/src/sequencer/README.md +0 -531
  101. package/src/sequencer/timetable.ts +0 -283
@@ -0,0 +1,795 @@
1
+ import type { Archiver } from '@aztec/archiver';
2
+ import type { L1TxUtils } from '@aztec/ethereum/l1-tx-utils';
3
+ import { type EthCheatCodes, RollupCheatCodes } from '@aztec/ethereum/test';
4
+ import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
5
+ import type { EthAddress } from '@aztec/foundation/eth-address';
6
+ import { Signature } from '@aztec/foundation/eth-signature';
7
+ import { type Logger, createLogger } from '@aztec/foundation/log';
8
+ import { SerialQueue } from '@aztec/foundation/queue';
9
+ import { RunningPromise } from '@aztec/foundation/running-promise';
10
+ import type { TestDateProvider } from '@aztec/foundation/timer';
11
+ import { isErrorClass } from '@aztec/foundation/types';
12
+ import type { P2PClient as ConcreteP2PClient, P2P } from '@aztec/p2p';
13
+ import { settleEpochOutbox } from '@aztec/prover-client/test';
14
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
15
+ import { CommitteeAttestationsAndSigners, type L2Block, type L2BlockSource } from '@aztec/stdlib/block';
16
+ import { getPreviousCheckpointOutHashes } from '@aztec/stdlib/checkpoint';
17
+ import type { ChainConfig } from '@aztec/stdlib/config';
18
+ import {
19
+ type L1RollupConstants,
20
+ getEpochAtSlot,
21
+ getSlotAtTimestamp,
22
+ getTimestampForSlot,
23
+ } from '@aztec/stdlib/epoch-helpers';
24
+ import { InsufficientValidTxsError, type WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
25
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
26
+ import type { CoordinationSignatureContext } from '@aztec/stdlib/p2p';
27
+ import type { FailedTx, Tx } from '@aztec/stdlib/tx';
28
+ import type {
29
+ BuildBlockInCheckpointResult,
30
+ CheckpointBuilder,
31
+ FullNodeCheckpointsBuilder,
32
+ } from '@aztec/validator-client';
33
+
34
+ import type { GlobalVariableBuilder } from '../../global_variable_builder/global_builder.js';
35
+ import type { SequencerPublisherFactory } from '../../publisher/sequencer-publisher-factory.js';
36
+ import type { SequencerPublisher } from '../../publisher/sequencer-publisher.js';
37
+ import type { SequencerConfig } from '../config.js';
38
+
39
+ /**
40
+ * L1 rollup constants needed by the AutomineSequencer. Same as SequencerRollupConstants
41
+ * plus `epochDuration` for the epoch-based checkpoint out-hash lookup.
42
+ */
43
+ export type AutomineSequencerConstants = Pick<
44
+ L1RollupConstants,
45
+ 'ethereumSlotDuration' | 'l1GenesisTime' | 'slotDuration' | 'rollupManaLimit' | 'epochDuration'
46
+ >;
47
+
48
+ /** Dependencies for the AutomineSequencer. */
49
+ export type AutomineSequencerDeps = {
50
+ publisherFactory: SequencerPublisherFactory;
51
+ checkpointsBuilder: FullNodeCheckpointsBuilder;
52
+ globalsBuilder: GlobalVariableBuilder;
53
+ worldState: WorldStateSynchronizer;
54
+ l2BlockSource: L2BlockSource;
55
+ l1ToL2MessageSource: L1ToL2MessageSource;
56
+ /** P2P client; must also expose `sync()` for post-rollback pool recovery. */
57
+ p2pClient: P2P & Pick<ConcreteP2PClient, 'sync'>;
58
+ ethCheatCodes: EthCheatCodes;
59
+ dateProvider: TestDateProvider;
60
+ l1Constants: AutomineSequencerConstants;
61
+ coinbase: EthAddress;
62
+ feeRecipient: AztecAddress;
63
+ signatureContext: CoordinationSignatureContext;
64
+ config: SequencerConfig & Pick<ChainConfig, 'l1ChainId' | 'rollupAddress'>;
65
+ /**
66
+ * Archiver used to push locally-built blocks and proposed checkpoints into the in-memory
67
+ * store, force an immediate L1 sync after publishing, and roll back state during reorgs.
68
+ */
69
+ archiver: Pick<
70
+ Archiver,
71
+ 'rollbackTo' | 'addBlock' | 'addProposedCheckpoint' | 'syncImmediate' | 'removeUncheckpointedBlocksAfter'
72
+ >;
73
+ /** L1 tx utils whose cached nonces must be reset after an L1 reorg. */
74
+ l1TxUtils: Pick<L1TxUtils, 'resetNonce'>[];
75
+ /**
76
+ * Optional extra cleanup hook awaited inside {@link AutomineSequencer.stop}. Used by the
77
+ * factory to shut down the PublisherManager funding loop after the queue and poller drain.
78
+ */
79
+ stopExtras?: () => Promise<void>;
80
+ /** How often to poll the mempool for new txs while running. Defaults to 50ms. */
81
+ pollIntervalMs?: number;
82
+ /**
83
+ * When true, run a loop that synthetically settles epochs (writes outbox roots and advances the
84
+ * proven tip) as checkpoints land, replacing the standalone `EpochTestSettler`. Local-network only;
85
+ * e2e tests leave this off and drive proving explicitly.
86
+ */
87
+ autoSettle?: boolean;
88
+ /** How often the auto-settle / clock-reconcile loop runs while {@link autoSettle} is enabled. Defaults to 200ms. */
89
+ settlePollIntervalMs?: number;
90
+ log?: Logger;
91
+ };
92
+
93
+ /**
94
+ * Minimal, deterministic, queue-driven sequencer for e2e tests that don't exercise
95
+ * block-building or consensus (e.g. e2e_token, e2e_amm, e2e_authwit).
96
+ *
97
+ * Differences from the production `Sequencer`:
98
+ * - No proposer-turn check (single sequencer).
99
+ * - No sync check, no pipelining, no validator orchestration, no attestations,
100
+ * no slashing, no votes, no P2P proposal gossip, no timetable enforcement.
101
+ * - No event emission — consumers (archiver, world-state, `EpochTestSettler`) observe
102
+ * L1 and the archiver tip rather than sequencer events.
103
+ * - All test-driven time control (warp / mine empty block) goes through a single
104
+ * serial queue alongside mempool-driven builds; the three never interleave.
105
+ *
106
+ * Requires `aztecTargetCommitteeSize == 0` on the deployed rollup so that the
107
+ * L1 `verifyProposer` / `verifyAttestations` short-circuits accept an empty
108
+ * `CommitteeAttestationsAndSigners` (see
109
+ * `l1-contracts/src/core/libraries/rollup/ValidatorSelectionLib.sol:244`).
110
+ */
111
+ export class AutomineSequencer {
112
+ private readonly log: Logger;
113
+ private readonly queue: SerialQueue;
114
+ private readonly pollIntervalMs: number;
115
+ private readonly deps: AutomineSequencerDeps;
116
+
117
+ private running = false;
118
+ private stopped = false;
119
+ private paused = false;
120
+ private mempoolPoller?: RunningPromise;
121
+ /** Loop that settles epochs and reconciles the clock while {@link AutomineSequencerDeps.autoSettle} is set. */
122
+ private settler?: RunningPromise;
123
+ private publisher?: SequencerPublisher;
124
+ private attestorAddress?: EthAddress;
125
+
126
+ /** Set while a mempool-driven build is queued but not yet run; used to coalesce. */
127
+ private buildQueued: Promise<L2Block | undefined> | undefined = undefined;
128
+
129
+ /** Last L2 slot we published a checkpoint for (-1 means none yet). */
130
+ private lastBuiltSlot: number = -1;
131
+
132
+ /** Lazily-built cheat codes for synthetic epoch settlement (outbox roots + proven tip). */
133
+ private rollupCheatCodes?: RollupCheatCodes;
134
+
135
+ constructor(deps: AutomineSequencerDeps) {
136
+ this.deps = deps;
137
+ this.log = deps.log ?? createLogger('sequencer:automine');
138
+ this.queue = new SerialQueue();
139
+ this.pollIntervalMs = deps.pollIntervalMs ?? 50;
140
+ }
141
+
142
+ /** Cheat codes for the rollup, built from the same EthCheatCodes the sequencer uses for time control. */
143
+ private getRollupCheatCodes(): RollupCheatCodes {
144
+ this.rollupCheatCodes ??= new RollupCheatCodes(this.deps.ethCheatCodes, {
145
+ rollupAddress: this.deps.config.rollupAddress,
146
+ });
147
+ return this.rollupCheatCodes;
148
+ }
149
+
150
+ /**
151
+ * Starts the sequencer. Switches anvil into automine mode (no interval mining),
152
+ * acquires a publisher, and begins polling the mempool for pending txs.
153
+ */
154
+ public async start(): Promise<void> {
155
+ if (this.running) {
156
+ return;
157
+ }
158
+ this.running = true;
159
+
160
+ await this.deps.ethCheatCodes.setIntervalMining(0, { silent: true });
161
+ await this.deps.ethCheatCodes.setAutomine(true, { silent: true });
162
+
163
+ const { publisher, attestorAddress } = await this.deps.publisherFactory.create();
164
+ this.publisher = publisher;
165
+ this.attestorAddress = attestorAddress;
166
+ this.log.info(`AutomineSequencer started`, {
167
+ publisher: this.publisher.getSenderAddress().toString(),
168
+ attestor: this.attestorAddress.toString(),
169
+ });
170
+
171
+ this.queue.start();
172
+
173
+ // Fallback poller that triggers a build if we discover pending txs without an explicit
174
+ // notification. Tests can also call `buildIfPending()` directly to skip the poll wait.
175
+ this.mempoolPoller = new RunningPromise(() => this.maybeEnqueueBuild(), this.log, this.pollIntervalMs);
176
+ this.mempoolPoller.start();
177
+
178
+ // Local-network only: settle epochs and reconcile the clock to L1 as checkpoints land. Both run
179
+ // through the same serial queue as builds/warps, so they never interleave with a checkpoint build.
180
+ if (this.deps.autoSettle) {
181
+ this.settler = new RunningPromise(() => this.maybeSettle(), this.log, this.deps.settlePollIntervalMs ?? 200);
182
+ this.settler.start();
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Stops the sequencer. Drains the queue, unsubscribes the mempool poller, and runs any
188
+ * registered {@link AutomineSequencerDeps.stopExtras} hook (e.g. the PublisherManager's
189
+ * funding loop). Idempotent: subsequent calls return without re-running cleanup.
190
+ */
191
+ public async stop(): Promise<void> {
192
+ if (this.stopped) {
193
+ return;
194
+ }
195
+ this.stopped = true;
196
+ this.running = false;
197
+ await this.mempoolPoller?.stop();
198
+ await this.settler?.stop();
199
+ await this.queue.end();
200
+ await this.deps.stopExtras?.();
201
+ this.log.info('AutomineSequencer stopped');
202
+ }
203
+
204
+ /**
205
+ * Enqueues a mempool-driven build. Coalesces consecutive calls so a burst of new txs
206
+ * collapses into one build job. Returns the built block (or undefined if nothing was built
207
+ * or the sequencer is paused).
208
+ */
209
+ public buildIfPending(): Promise<L2Block | undefined> {
210
+ if (!this.running || this.paused) {
211
+ return Promise.resolve(undefined);
212
+ }
213
+ if (this.buildQueued) {
214
+ // A build is already queued; coalesce. The pending build will pick up the new txs.
215
+ return this.buildQueued;
216
+ }
217
+ this.buildQueued = this.queue.put(async () => {
218
+ try {
219
+ return await this.runBuild({ allowEmpty: false });
220
+ } finally {
221
+ this.buildQueued = undefined;
222
+ }
223
+ });
224
+ return this.buildQueued;
225
+ }
226
+
227
+ /**
228
+ * Pauses mempool-driven block production. Pending txs accumulate in the pool without being
229
+ * mined. Explicit test-harness operations (`buildEmptyBlock`, `warpTo`, `revertToCheckpoint`)
230
+ * continue to work; this only gates the mempool poller and `buildIfPending`.
231
+ */
232
+ public pause(): void {
233
+ if (this.paused) {
234
+ return;
235
+ }
236
+ this.paused = true;
237
+ this.log.info('AutomineSequencer paused');
238
+ }
239
+
240
+ /** Resumes mempool-driven block production after a previous {@link pause}. */
241
+ public resume(): void {
242
+ if (!this.paused) {
243
+ return;
244
+ }
245
+ this.paused = false;
246
+ this.log.info('AutomineSequencer resumed');
247
+ }
248
+
249
+ /**
250
+ * Updates the in-memory sequencer config. Mirrors {@link SequencerClient.updateConfig} so that
251
+ * `AztecNode.setConfig` (e.g. tests bumping `minTxsPerBlock` to bundle multiple txs into one
252
+ * block) propagates to the AutomineSequencer's gating logic in {@link runBuild}.
253
+ */
254
+ public updateConfig(config: Partial<SequencerConfig>): void {
255
+ Object.assign(this.deps.config, config);
256
+ }
257
+
258
+ /** Returns a snapshot of the current sequencer config (used by pause/resume bookkeeping). */
259
+ public getConfig(): SequencerConfig {
260
+ return this.deps.config;
261
+ }
262
+
263
+ /** Enqueues an empty-block build. Resolves to the built block. */
264
+ public buildEmptyBlock(): Promise<L2Block> {
265
+ return this.queue.put(async () => {
266
+ const block = await this.runBuild({ allowEmpty: true });
267
+ if (!block) {
268
+ throw new Error('buildEmptyBlock: runBuild returned undefined');
269
+ }
270
+ return block;
271
+ });
272
+ }
273
+
274
+ /**
275
+ * Warps L1 timestamp to `targetTimestampSec`. Rounded up to the next aztec-slot
276
+ * boundary so the next build lands on a fresh slot. Atomic with respect to builds —
277
+ * the queue ensures no build is in flight while the warp executes. A no-op when the
278
+ * target is already at or behind the current L1 time (see {@link runWarp}).
279
+ */
280
+ public warpTo(targetTimestampSec: number): Promise<void> {
281
+ return this.queue.put(() => this.runWarp(targetTimestampSec));
282
+ }
283
+
284
+ /**
285
+ * Warps L1 timestamp forward by `deltaSec` seconds from the current L1 time, rounded up to the next
286
+ * aztec-slot boundary. Throws if `deltaSec` is not positive (warping "by" a non-positive amount is a
287
+ * caller bug — unlike {@link warpTo}, which no-ops on a past target).
288
+ */
289
+ public warpBy(deltaSec: number): Promise<void> {
290
+ if (deltaSec <= 0) {
291
+ throw new Error(`warpL2TimeAtLeastBy: duration must be positive, got ${deltaSec} seconds.`);
292
+ }
293
+ return this.queue.put(async () => {
294
+ const current = await this.deps.ethCheatCodes.lastBlockTimestamp();
295
+ await this.runWarp(current + deltaSec);
296
+ });
297
+ }
298
+
299
+ /**
300
+ * Reorgs L1 so that every L1 block strictly after the one that published
301
+ * `targetCheckpoint` is removed. The archiver, world-state, and date provider
302
+ * are all brought back in sync before the promise resolves.
303
+ *
304
+ * Runs inside the serial queue so it never interleaves with a build or warp.
305
+ */
306
+ public revertToCheckpoint(targetCheckpoint: number): Promise<void> {
307
+ return this.queue.put(() => this.runRevert(targetCheckpoint));
308
+ }
309
+
310
+ /** Awaits the queue draining to a fully idle state. */
311
+ public syncPoint(): Promise<void> {
312
+ return this.queue.syncPoint();
313
+ }
314
+
315
+ /**
316
+ * Synthetically "proves" the L2 chain up to `upToCheckpoint` (default: the latest checkpointed
317
+ * checkpoint). For every epoch newly covered — including a partial final epoch — the epoch out hash
318
+ * is written into the L1 Outbox so the L2-to-L1 messages in those checkpoints become consumable,
319
+ * then the rollup's proven tip is advanced to the target. There is no real proof; this is the
320
+ * local-network equivalent of an epoch proof landing on L1.
321
+ *
322
+ * Clamps the target down to the latest checkpointed checkpoint and no-ops when it is already proven.
323
+ * Runs inside the serial queue so it never interleaves with a build or warp.
324
+ *
325
+ * @returns The proven checkpoint after the call (the target, or the existing proven tip on no-op).
326
+ */
327
+ public prove(upToCheckpoint?: CheckpointNumber): Promise<CheckpointNumber> {
328
+ return this.queue.put(() => this.runProve(upToCheckpoint));
329
+ }
330
+
331
+ /**
332
+ * Auto-settle tick (local-network only). Proving up to the latest checkpointed checkpoint also
333
+ * reconciles the clock at the head of {@link runProve}, so this single tick keeps both the
334
+ * proven tip and the date provider current even when no build is happening.
335
+ */
336
+ private async maybeSettle(): Promise<void> {
337
+ if (!this.running) {
338
+ return;
339
+ }
340
+ try {
341
+ await this.prove();
342
+ } catch (err) {
343
+ this.log.warn('Automine auto-settle tick failed', { err: err instanceof Error ? err.message : String(err) });
344
+ }
345
+ }
346
+
347
+ /**
348
+ * Advances the injected date provider to the latest *mined* L1 timestamp when it has fallen behind
349
+ * (e.g. an unrelated L1 tx mined a block between our builds). Never advances to the pending, un-mined
350
+ * timestamp. Keeps node-side consumers of `dateProvider.now()` aligned with L1 without our own builds.
351
+ */
352
+ private async reconcileDateProvider(): Promise<void> {
353
+ const lastTsMs = (await this.deps.ethCheatCodes.lastBlockTimestamp()) * 1000;
354
+ if (lastTsMs > this.deps.dateProvider.now()) {
355
+ this.deps.dateProvider.setTime(lastTsMs);
356
+ }
357
+ }
358
+
359
+ /** Called from the mempool poller. Enqueues a build if there are pending txs. */
360
+ private async maybeEnqueueBuild(): Promise<void> {
361
+ if (!this.running || this.paused || this.buildQueued) {
362
+ return;
363
+ }
364
+ try {
365
+ const pending = await this.deps.p2pClient.getPendingTxCount();
366
+ if (pending > 0) {
367
+ // Fire-and-forget; the build result is delivered via `buildIfPending()` callers,
368
+ // not via the poller.
369
+ void this.buildIfPending().catch(err => {
370
+ this.log.error('Mempool-driven build failed', err);
371
+ });
372
+ }
373
+ } catch (err) {
374
+ this.log.warn('Failed to poll mempool', { err: err instanceof Error ? err.message : String(err) });
375
+ }
376
+ }
377
+
378
+ /**
379
+ * Builds and publishes one checkpoint, returning the built block — or `undefined` when there was
380
+ * nothing to build or the propose failed. A failed propose mines no checkpoint on L1 (it reverts
381
+ * inside the multicall or is never sent), so recovery is purely local: the optimistic archiver insert
382
+ * is rolled back, the block's txs return to the pending pool, and the L1 nonce is reset. No L1 reorg
383
+ * is performed, and the build is not retried inline — once the txs are back in the pending pool the
384
+ * mempool poller re-enqueues a build on its next tick (see {@link maybeEnqueueBuild}).
385
+ */
386
+ private async runBuild({ allowEmpty }: { allowEmpty: boolean }): Promise<L2Block | undefined> {
387
+ if (!this.running || !this.publisher || !this.attestorAddress) {
388
+ return undefined;
389
+ }
390
+ await this.reconcileDateProvider();
391
+
392
+ const txCount = await this.deps.p2pClient.getPendingTxCount();
393
+ // For mempool-driven builds, wait for at least `minTxsPerBlock` pending txs (or 1 if not set)
394
+ // before building. This mirrors the production sequencer's `waitForMinTxs` behavior, and is
395
+ // required for tests that bundle multiple txs into one block via `setConfig({ minTxsPerBlock })`.
396
+ // Explicit empty-block / warp paths pass `allowEmpty: true` and bypass this gate.
397
+ const minRequired = allowEmpty ? 0 : Math.max(this.deps.config.minTxsPerBlock ?? 1, 1);
398
+ if (txCount < minRequired) {
399
+ return undefined;
400
+ }
401
+
402
+ // Decide target slot from the pending block's timestamp — picks up any prior
403
+ // `setNextBlockTimestamp` call (e.g. queued by runWarp) instead of assuming +1 over
404
+ // the last mined block.
405
+ const pendingBlockTs = await this.deps.ethCheatCodes.nextBlockTimestamp();
406
+ let targetSlot = Number(getSlotAtTimestamp(BigInt(pendingBlockTs), this.deps.l1Constants));
407
+ if (targetSlot <= this.lastBuiltSlot) {
408
+ // Pending block doesn't reach a new slot yet; advance to the next slot we own.
409
+ targetSlot = this.lastBuiltSlot + 1;
410
+ }
411
+ const slotBoundaryTs = Number(getTimestampForSlot(SlotNumber(targetSlot), this.deps.l1Constants));
412
+
413
+ // Pre-set anvil's next block timestamp only if the slot boundary is past what's already
414
+ // scheduled. setNextBlockTimestamp rejects values not strictly greater than the last block's
415
+ // timestamp, and pendingBlockTs is always > lastBlockTimestamp, so this guard is sufficient.
416
+ if (slotBoundaryTs > pendingBlockTs) {
417
+ await this.deps.ethCheatCodes.setNextBlockTimestamp(slotBoundaryTs);
418
+ }
419
+
420
+ const [tips, proposedCheckpoint] = await Promise.all([
421
+ this.deps.l2BlockSource.getL2Tips(),
422
+ this.deps.l2BlockSource.getProposedCheckpointData(),
423
+ ]);
424
+ const syncedToBlockNumber = tips.proposed.number;
425
+
426
+ // Ensure world state has processed the archiver's tip before forking. Without this,
427
+ // world state may still be at the previous block (since it syncs asynchronously from
428
+ // the archiver), and `fork(syncedToBlockNumber)` would fail with
429
+ // "Unable to initialize from future block".
430
+ await this.deps.worldState.syncImmediate(BlockNumber(syncedToBlockNumber));
431
+
432
+ const nextBlockNumber = BlockNumber(syncedToBlockNumber + 1);
433
+ const parentCheckpointNumber = proposedCheckpoint?.checkpointNumber ?? tips.checkpointed.checkpoint.number;
434
+ const checkpointNumber = CheckpointNumber(parentCheckpointNumber + 1);
435
+ const targetEpoch = getEpochAtSlot(SlotNumber(targetSlot), this.deps.l1Constants);
436
+
437
+ this.log.verbose(`Building automine checkpoint`, {
438
+ checkpointNumber,
439
+ blockNumber: nextBlockNumber,
440
+ slot: targetSlot,
441
+ slotTimestamp: slotBoundaryTs,
442
+ txCount,
443
+ allowEmpty,
444
+ });
445
+
446
+ const checkpointGlobals = await this.deps.globalsBuilder.buildCheckpointGlobalVariables(
447
+ this.deps.coinbase,
448
+ this.deps.feeRecipient,
449
+ SlotNumber(targetSlot),
450
+ );
451
+
452
+ const l1ToL2Messages = await this.deps.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
453
+
454
+ const previousCheckpointOutHashes = await getPreviousCheckpointOutHashes({
455
+ blockSource: this.deps.l2BlockSource,
456
+ epoch: targetEpoch,
457
+ checkpointNumber,
458
+ l1Constants: this.deps.l1Constants,
459
+ pipeliningEnabled: false,
460
+ log: this.log,
461
+ });
462
+
463
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
464
+
465
+ await using fork = await this.deps.worldState.fork(syncedToBlockNumber, { closeDelayMs: 0 });
466
+
467
+ const checkpointBuilder = await this.deps.checkpointsBuilder.startCheckpoint(
468
+ checkpointNumber,
469
+ checkpointGlobals,
470
+ feeAssetPriceModifier,
471
+ l1ToL2Messages,
472
+ previousCheckpointOutHashes,
473
+ fork,
474
+ this.log.getBindings(),
475
+ );
476
+
477
+ // Block building only executes txs, and automine publishes straight to L1, so the proofs are never needed.
478
+ const pendingTxs = this.deps.p2pClient.iterateEligiblePendingTxs({ includeProof: false });
479
+
480
+ const buildResult = await this.tryBuildBlock(
481
+ checkpointBuilder,
482
+ pendingTxs,
483
+ nextBlockNumber,
484
+ checkpointGlobals.timestamp,
485
+ allowEmpty,
486
+ checkpointNumber,
487
+ );
488
+ if (!buildResult) {
489
+ return undefined;
490
+ }
491
+
492
+ const checkpoint = await checkpointBuilder.completeCheckpoint();
493
+
494
+ // Empty CommitteeAttestationsAndSigners is accepted on-chain when committee size is 0.
495
+ const emptyAttestations = CommitteeAttestationsAndSigners.empty(this.deps.signatureContext);
496
+ const emptyAttestationsSignature = Signature.empty();
497
+
498
+ // Push the block and proposed checkpoint into the archiver locally BEFORE publishing to L1.
499
+ // This avoids racing the archiver's L1 polling: if the L1 publish happened first, polling
500
+ // could surface the checkpoint and reject our subsequent local push as duplicate. Pushing
501
+ // first means the archiver already has the proposed entry when L1 polling fires; the L1
502
+ // sync path then promotes the existing proposed checkpoint via promoteProposedToCheckpointed
503
+ // rather than re-adding it.
504
+ await this.deps.archiver.addBlock(buildResult.block);
505
+ await this.deps.archiver.addProposedCheckpoint({
506
+ header: checkpoint.header,
507
+ checkpointNumber,
508
+ startBlock: BlockNumber(buildResult.block.number),
509
+ blockCount: 1,
510
+ totalManaUsed: checkpoint.header.totalManaUsed.toBigInt(),
511
+ feeAssetPriceModifier,
512
+ });
513
+
514
+ await this.publisher.enqueueProposeCheckpoint(checkpoint, emptyAttestations, emptyAttestationsSignature);
515
+ // Automine publishes synchronously in the current slot via `sendRequests`. It must NOT use the
516
+ // production `sendRequestsAt` (or `canProposeAt`), which always apply the one-slot pipelining
517
+ // offset — automine is the deliberate non-pipelined exception and builds/publishes in place.
518
+ const result = await this.publisher.sendRequests(SlotNumber(targetSlot));
519
+
520
+ const proposeSucceeded = !!result?.successfulActions?.some(action => action === 'propose');
521
+ if (!proposeSucceeded) {
522
+ this.log.warn('Automine propose did not succeed; rolled back optimistic insert, will rebuild from the poller', {
523
+ slot: targetSlot,
524
+ checkpointNumber,
525
+ successful: result?.successfulActions,
526
+ failed: result?.failedActions,
527
+ });
528
+ await this.rollbackOptimisticInsert(syncedToBlockNumber);
529
+ return undefined;
530
+ }
531
+
532
+ // Force one full L1-sync cycle synchronously. The local addBlock/addProposedCheckpoint
533
+ // above advances the proposed tip, but tips.checkpointed and the L1->L2 inbox tree state
534
+ // only advance when the archiver observes the L1-confirmed checkpoint via its sync loop.
535
+ await this.deps.archiver.syncImmediate();
536
+
537
+ // Sync the date provider to the L1 block timestamp we just mined.
538
+ const newL1Ts = await this.deps.ethCheatCodes.lastBlockTimestamp();
539
+ this.deps.dateProvider.setTime(newL1Ts * 1000);
540
+
541
+ // A successful propose is validated on-chain against the mined L1 block's slot, so the mined slot
542
+ // should equal our target; warn (without rolling back — the checkpoint is on L1) if it ever differs.
543
+ const minedSlot = Number(getSlotAtTimestamp(BigInt(newL1Ts), this.deps.l1Constants));
544
+ if (minedSlot !== targetSlot) {
545
+ this.log.warn(`Automine checkpoint mined in slot ${minedSlot} but targeted ${targetSlot}`, {
546
+ minedSlot,
547
+ targetSlot,
548
+ checkpointNumber,
549
+ });
550
+ }
551
+
552
+ this.lastBuiltSlot = targetSlot;
553
+
554
+ this.log.verbose(`Automine checkpoint published`, {
555
+ checkpointNumber,
556
+ blockNumber: nextBlockNumber,
557
+ slot: targetSlot,
558
+ l1Timestamp: newL1Ts,
559
+ txCount: buildResult.numTxs,
560
+ });
561
+
562
+ return buildResult.block;
563
+ }
564
+
565
+ /**
566
+ * Undoes an optimistic archiver insert (uncheckpointed block + proposed checkpoint) without reorging
567
+ * L1: removes the uncheckpointed block and evicts the proposed checkpoint that referenced it, so the
568
+ * proposed tip drops back to `toBlockNumber`. `p2pClient.sync()` then observes the lowered tip and
569
+ * restores the block's txs to the pending pool; `worldState.syncImmediate()` drops any applied effects;
570
+ * and the L1 nonce is reset (a reverted-but-mined propose consumes one) so the build can be retried.
571
+ *
572
+ * Note: `archiver.rollbackTo` is NOT usable here — it is checkpoint-granular and no-ops on a
573
+ * proposed-only tip (the inserted checkpoint is not yet in the checkpointed set).
574
+ */
575
+ private async rollbackOptimisticInsert(toBlockNumber: BlockNumber): Promise<void> {
576
+ await this.deps.archiver.removeUncheckpointedBlocksAfter(toBlockNumber);
577
+ await this.deps.p2pClient.sync();
578
+ await this.deps.worldState.syncImmediate();
579
+ this.deps.l1TxUtils.forEach(utils => utils.resetNonce());
580
+ }
581
+
582
+ /**
583
+ * Warps L1 timestamp to (or past) `targetTimestampSec`, rounded up to the next aztec-slot
584
+ * boundary, by queuing an empty-checkpoint build at that slot. Mines exactly one L1 block
585
+ * (the propose tx auto-mined under anvil's automine mode), with the timestamp pre-set so
586
+ * the mined block lands on the slot boundary.
587
+ */
588
+ private async runWarp(targetTimestampSec: number): Promise<void> {
589
+ await this.reconcileDateProvider();
590
+ const currentL1Ts = await this.deps.ethCheatCodes.lastBlockTimestamp();
591
+ if (targetTimestampSec <= currentL1Ts) {
592
+ this.log.debug(`Warp target ${targetTimestampSec} is not in the future of current L1 ts ${currentL1Ts}`);
593
+ return;
594
+ }
595
+
596
+ // Round up to the next aztec-slot boundary so the next build naturally lands on a new slot.
597
+ const targetSlot = Number(getSlotAtTimestamp(BigInt(targetTimestampSec), this.deps.l1Constants));
598
+ const slotBoundaryTs = Number(getTimestampForSlot(SlotNumber(targetSlot + 1), this.deps.l1Constants));
599
+
600
+ // Queue the next L1 block at the slot boundary timestamp, then build (and publish) an
601
+ // empty L2 checkpoint. The propose tx auto-mines a single L1 block at slotBoundaryTs,
602
+ // and `runBuild` syncs the date provider to the new L1 timestamp.
603
+ await this.deps.ethCheatCodes.setNextBlockTimestamp(slotBoundaryTs);
604
+ await this.runBuild({ allowEmpty: true });
605
+
606
+ this.log.verbose(`Warped L1 to slot boundary`, { slot: targetSlot + 1, timestamp: slotBoundaryTs });
607
+ }
608
+
609
+ /**
610
+ * Rolls L1 back to the block that published `targetCheckpoint`, drops the archiver's
611
+ * in-memory state to match, and resets internal slot bookkeeping.
612
+ */
613
+ private async runRevert(targetCheckpoint: number): Promise<void> {
614
+ const checkpointData = await this.deps.l2BlockSource.getCheckpointData({
615
+ number: CheckpointNumber(targetCheckpoint),
616
+ });
617
+ if (!checkpointData) {
618
+ throw new Error(`AutomineSequencer: checkpoint ${targetCheckpoint} not found in archiver`);
619
+ }
620
+
621
+ const targetL1Block = Number(checkpointData.l1.blockNumber);
622
+ this.log.verbose(`Reverting to checkpoint ${targetCheckpoint}`, {
623
+ targetCheckpoint,
624
+ targetL1Block,
625
+ checkpointSlot: checkpointData.header.slotNumber,
626
+ });
627
+
628
+ // Roll the archiver back to the last block of targetCheckpoint before the L1 reorg,
629
+ // since the archiver needs to fetch the target checkpoint's L1 block hash during rollback.
630
+ const lastBlockInCheckpoint = BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
631
+ await this.deps.archiver.rollbackTo(lastBlockInCheckpoint);
632
+
633
+ // Force the P2P block stream to run one cycle immediately so it processes the
634
+ // chain-pruned event triggered by the archiver rollback above. Without this, the
635
+ // P2P pool may not have restored rolled-back txs to pending by the time the next
636
+ // build runs.
637
+ await this.deps.p2pClient.sync();
638
+
639
+ // Force world-state to process the archiver's prune event immediately, so the next build
640
+ // doesn't try to insert nullifiers that were already in the pruned checkpoints.
641
+ await this.deps.worldState.syncImmediate();
642
+
643
+ // Remove all L1 blocks strictly after the target checkpoint's publish block so that
644
+ // the propose txs for later checkpoints are gone from L1. We use reorg(depth) directly
645
+ // to keep targetL1Block itself as the new chain tip.
646
+ const currentL1Block = await this.deps.ethCheatCodes.publicClient.getBlockNumber();
647
+ const depth = Number(currentL1Block) - targetL1Block;
648
+ if (depth > 0) {
649
+ await this.deps.ethCheatCodes.reorg(depth);
650
+ }
651
+
652
+ // anvil_rollback re-queues the rolled-back txs into the mempool. Clear them so they
653
+ // don't get re-mined, then reset the publisher nonce tracker so the next propose tx
654
+ // uses the correct nonce for the post-reorg chain state.
655
+ await this.deps.ethCheatCodes.rpcCall('anvil_dropAllTransactions', []);
656
+ this.deps.l1TxUtils.forEach(utils => utils.resetNonce());
657
+
658
+ // Reset slot bookkeeping so the next build picks up at the correct slot.
659
+ this.lastBuiltSlot = Number(checkpointData.header.slotNumber);
660
+
661
+ this.log.verbose(`Reverted to checkpoint ${targetCheckpoint}`, {
662
+ targetCheckpoint,
663
+ targetL1Block,
664
+ });
665
+ }
666
+
667
+ /**
668
+ * Writes outbox roots for every epoch newly covered up to `maybeCheckpoint` and advances the proven
669
+ * tip. Settles each fully-covered epoch in full and the final epoch only up to the target checkpoint
670
+ * (a partial epoch, which the AZIP-14 Outbox supports via per-`numCheckpointsInEpoch` roots).
671
+ */
672
+ private async runProve(upToCheckpoint?: CheckpointNumber): Promise<CheckpointNumber> {
673
+ await this.reconcileDateProvider();
674
+ const rollupCheatCodes = this.getRollupCheatCodes();
675
+ const tips = await this.deps.l2BlockSource.getL2Tips();
676
+ const checkpointedTip = tips.checkpointed.checkpoint.number;
677
+
678
+ // Never prove beyond what the archiver has actually checkpointed; default to that tip.
679
+ const target = CheckpointNumber(Math.min(upToCheckpoint ?? checkpointedTip, checkpointedTip));
680
+
681
+ const { proven } = await rollupCheatCodes.getTips();
682
+ if (target <= proven) {
683
+ this.log.debug(`Checkpoint ${target} already proven`, { target, proven, checkpointedTip });
684
+ return proven;
685
+ }
686
+
687
+ const startEpoch = await this.getEpochOfCheckpoint(CheckpointNumber(proven + 1));
688
+ const endEpoch = await this.getEpochOfCheckpoint(target);
689
+ if (startEpoch === undefined || endEpoch === undefined) {
690
+ this.log.warn(`Cannot resolve epoch range to prove up to checkpoint ${target}`, {
691
+ target,
692
+ proven,
693
+ startEpoch,
694
+ endEpoch,
695
+ });
696
+ return proven;
697
+ }
698
+
699
+ for (let epoch = startEpoch; epoch <= endEpoch; epoch++) {
700
+ const lastCovered = await settleEpochOutbox({
701
+ rollupCheatCodes,
702
+ l2BlockSource: this.deps.l2BlockSource,
703
+ epoch: EpochNumber(epoch),
704
+ maxCheckpoint: epoch === endEpoch ? target : undefined,
705
+ log: this.log,
706
+ });
707
+ if (lastCovered === undefined) {
708
+ // An epoch in (proven, target] with no checkpointed blocks — expected when warps skip a whole
709
+ // epoch. Logged so it's distinguishable from the archiver failing to serve the epoch's blocks.
710
+ this.log.debug(`No checkpointed blocks to settle for epoch ${epoch} while proving to ${target}`, {
711
+ epoch,
712
+ target,
713
+ });
714
+ }
715
+ }
716
+
717
+ await rollupCheatCodes.markAsProven(target);
718
+ // Settlement is a direct L1 storage write that mines no block, unlike a real epoch proof landing
719
+ // on L1. The archiver's L1 sync short-circuits while the L1 block hash is unchanged, so it would
720
+ // never re-read the proven tip until the next build/warp mines a block. Mine one empty L1 block so
721
+ // the block hash advances, then force an immediate sync that observes the new proven checkpoint.
722
+ await this.deps.ethCheatCodes.mineEmptyBlock();
723
+ await this.deps.archiver.syncImmediate();
724
+
725
+ this.log.verbose(`Proved up to checkpoint ${target}`, { target, proven, startEpoch, endEpoch });
726
+ return target;
727
+ }
728
+
729
+ /** Resolves the epoch a checkpoint belongs to from its slot, or undefined if the archiver lacks it. */
730
+ private async getEpochOfCheckpoint(checkpointNumber: CheckpointNumber): Promise<number | undefined> {
731
+ const checkpointData = await this.deps.l2BlockSource.getCheckpointData({ number: checkpointNumber });
732
+ if (!checkpointData) {
733
+ return undefined;
734
+ }
735
+ const slot = SlotNumber(Number(checkpointData.header.slotNumber));
736
+ return Number(getEpochAtSlot(slot, this.deps.l1Constants));
737
+ }
738
+
739
+ /**
740
+ * Wraps `checkpointBuilder.buildBlock` with the failed-tx handling shared by both error
741
+ * and success paths: drops the failed txs from the P2P mempool, and returns `undefined`
742
+ * when `InsufficientValidTxsError` aborts the build (so the caller skips publishing).
743
+ */
744
+ private async tryBuildBlock(
745
+ checkpointBuilder: CheckpointBuilder,
746
+ pendingTxs: AsyncIterableIterator<Tx>,
747
+ nextBlockNumber: BlockNumber,
748
+ timestamp: bigint,
749
+ allowEmpty: boolean,
750
+ checkpointNumber: CheckpointNumber,
751
+ ): Promise<BuildBlockInCheckpointResult | undefined> {
752
+ let buildResult: BuildBlockInCheckpointResult;
753
+ try {
754
+ buildResult = await checkpointBuilder.buildBlock(pendingTxs, nextBlockNumber, timestamp, {
755
+ maxTransactions: this.deps.config.maxTxsPerBlock,
756
+ // Allow empty for explicit-empty builds; require at least 1 valid tx otherwise.
757
+ minValidTxs: allowEmpty ? 0 : 1,
758
+ isBuildingProposal: true,
759
+ maxBlocksPerCheckpoint: 1,
760
+ perBlockAllocationMultiplier: 1,
761
+ });
762
+ } catch (err) {
763
+ // Mirrors production's checkpoint_proposal_job: if every pending tx failed execution and
764
+ // we didn't reach minValidTxs, drop the failed txs from the mempool so they don't block
765
+ // the poller forever, then abort this build with no checkpoint published.
766
+ if (isErrorClass(err, InsufficientValidTxsError)) {
767
+ await this.dropFailedTxsFromP2P(err.failedTxs);
768
+ this.log.verbose(`AutomineSequencer: insufficient valid txs, skipping build`, {
769
+ checkpointNumber,
770
+ processedCount: err.processedCount,
771
+ minRequired: err.minRequired,
772
+ failedCount: err.failedTxs.length,
773
+ });
774
+ return undefined;
775
+ }
776
+ throw err;
777
+ }
778
+
779
+ // Drop any txs that failed execution but didn't trigger InsufficientValidTxsError, so we
780
+ // don't re-pick them up on the next build.
781
+ await this.dropFailedTxsFromP2P(buildResult.failedTxs);
782
+
783
+ return buildResult;
784
+ }
785
+
786
+ /** Removes txs that failed execution from the P2P mempool so they don't get retried. */
787
+ private async dropFailedTxsFromP2P(failedTxs: FailedTx[]): Promise<void> {
788
+ if (failedTxs.length === 0) {
789
+ return;
790
+ }
791
+ const failedTxHashes = failedTxs.map(fail => fail.tx.getTxHash());
792
+ this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
793
+ await this.deps.p2pClient.handleFailedExecution(failedTxHashes);
794
+ }
795
+ }