@aztec/sequencer-client 0.0.1-commit.b33fc05d0 → 0.0.1-commit.b3d3157a

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 (105) hide show
  1. package/README.md +281 -21
  2. package/dest/client/sequencer-client.d.ts +8 -14
  3. package/dest/client/sequencer-client.d.ts.map +1 -1
  4. package/dest/client/sequencer-client.js +33 -83
  5. package/dest/config.d.ts +5 -1
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +42 -15
  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 +58 -0
  14. package/dest/global_variable_builder/global_builder.d.ts +14 -14
  15. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  16. package/dest/global_variable_builder/global_builder.js +16 -50
  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/index.d.ts +2 -2
  21. package/dest/index.d.ts.map +1 -1
  22. package/dest/index.js +1 -1
  23. package/dest/publisher/config.d.ts +15 -3
  24. package/dest/publisher/config.d.ts.map +1 -1
  25. package/dest/publisher/config.js +19 -4
  26. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +3 -4
  27. package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -1
  28. package/dest/publisher/sequencer-bundle-simulator.d.ts +96 -0
  29. package/dest/publisher/sequencer-bundle-simulator.d.ts.map +1 -0
  30. package/dest/publisher/sequencer-bundle-simulator.js +198 -0
  31. package/dest/publisher/sequencer-publisher-factory.d.ts +3 -5
  32. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  33. package/dest/publisher/sequencer-publisher-factory.js +2 -3
  34. package/dest/publisher/sequencer-publisher.d.ts +73 -65
  35. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  36. package/dest/publisher/sequencer-publisher.js +318 -523
  37. package/dest/sequencer/automine/automine_factory.d.ts +54 -0
  38. package/dest/sequencer/automine/automine_factory.d.ts.map +1 -0
  39. package/dest/sequencer/automine/automine_factory.js +84 -0
  40. package/dest/sequencer/automine/automine_sequencer.d.ts +151 -0
  41. package/dest/sequencer/automine/automine_sequencer.d.ts.map +1 -0
  42. package/dest/sequencer/automine/automine_sequencer.js +509 -0
  43. package/dest/sequencer/chain_state_overrides.d.ts +61 -0
  44. package/dest/sequencer/chain_state_overrides.d.ts.map +1 -0
  45. package/dest/sequencer/chain_state_overrides.js +98 -0
  46. package/dest/sequencer/checkpoint_proposal_job.d.ts +60 -10
  47. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
  48. package/dest/sequencer/checkpoint_proposal_job.js +748 -200
  49. package/dest/sequencer/checkpoint_proposal_job_metrics.d.ts +34 -0
  50. package/dest/sequencer/checkpoint_proposal_job_metrics.d.ts.map +1 -0
  51. package/dest/sequencer/checkpoint_proposal_job_metrics.js +72 -0
  52. package/dest/sequencer/checkpoint_voter.d.ts +1 -2
  53. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -1
  54. package/dest/sequencer/checkpoint_voter.js +2 -5
  55. package/dest/sequencer/events.d.ts +48 -2
  56. package/dest/sequencer/events.d.ts.map +1 -1
  57. package/dest/sequencer/index.d.ts +3 -1
  58. package/dest/sequencer/index.d.ts.map +1 -1
  59. package/dest/sequencer/index.js +2 -0
  60. package/dest/sequencer/metrics.d.ts +13 -10
  61. package/dest/sequencer/metrics.d.ts.map +1 -1
  62. package/dest/sequencer/metrics.js +45 -20
  63. package/dest/sequencer/sequencer.d.ts +63 -13
  64. package/dest/sequencer/sequencer.d.ts.map +1 -1
  65. package/dest/sequencer/sequencer.js +349 -107
  66. package/dest/sequencer/timetable.d.ts +11 -1
  67. package/dest/sequencer/timetable.d.ts.map +1 -1
  68. package/dest/sequencer/timetable.js +41 -36
  69. package/dest/sequencer/types.d.ts +2 -2
  70. package/dest/sequencer/types.d.ts.map +1 -1
  71. package/dest/test/mock_checkpoint_builder.d.ts +4 -4
  72. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -1
  73. package/dest/test/utils.d.ts +1 -1
  74. package/dest/test/utils.d.ts.map +1 -1
  75. package/dest/test/utils.js +7 -6
  76. package/package.json +27 -28
  77. package/src/client/sequencer-client.ts +53 -114
  78. package/src/config.ts +48 -12
  79. package/src/global_variable_builder/README.md +44 -0
  80. package/src/global_variable_builder/fee_predictor.ts +172 -0
  81. package/src/global_variable_builder/fee_provider.ts +75 -0
  82. package/src/global_variable_builder/global_builder.ts +25 -62
  83. package/src/global_variable_builder/index.ts +3 -1
  84. package/src/index.ts +10 -1
  85. package/src/publisher/config.ts +40 -6
  86. package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +3 -1
  87. package/src/publisher/sequencer-bundle-simulator.ts +254 -0
  88. package/src/publisher/sequencer-publisher-factory.ts +3 -6
  89. package/src/publisher/sequencer-publisher.ts +377 -568
  90. package/src/sequencer/README.md +162 -450
  91. package/src/sequencer/automine/README.md +46 -0
  92. package/src/sequencer/automine/automine_factory.ts +145 -0
  93. package/src/sequencer/automine/automine_sequencer.ts +595 -0
  94. package/src/sequencer/chain_state_overrides.ts +162 -0
  95. package/src/sequencer/checkpoint_proposal_job.ts +894 -220
  96. package/src/sequencer/checkpoint_proposal_job_metrics.ts +128 -0
  97. package/src/sequencer/checkpoint_voter.ts +1 -12
  98. package/src/sequencer/events.ts +51 -2
  99. package/src/sequencer/index.ts +2 -0
  100. package/src/sequencer/metrics.ts +57 -24
  101. package/src/sequencer/sequencer.ts +429 -121
  102. package/src/sequencer/timetable.ts +49 -44
  103. package/src/sequencer/types.ts +1 -1
  104. package/src/test/mock_checkpoint_builder.ts +3 -3
  105. package/src/test/utils.ts +28 -10
@@ -0,0 +1,595 @@
1
+ import type { Archiver } from '@aztec/archiver';
2
+ import type { L1TxUtils } from '@aztec/ethereum/l1-tx-utils';
3
+ import type { EthCheatCodes } from '@aztec/ethereum/test';
4
+ import { BlockNumber, CheckpointNumber, 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 type { AztecAddress } from '@aztec/stdlib/aztec-address';
14
+ import { CommitteeAttestationsAndSigners, type L2Block, type L2BlockSource } from '@aztec/stdlib/block';
15
+ import { getPreviousCheckpointOutHashes } from '@aztec/stdlib/checkpoint';
16
+ import type { ChainConfig } from '@aztec/stdlib/config';
17
+ import {
18
+ type L1RollupConstants,
19
+ getEpochAtSlot,
20
+ getSlotAtTimestamp,
21
+ getTimestampForSlot,
22
+ } from '@aztec/stdlib/epoch-helpers';
23
+ import { InsufficientValidTxsError, type WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
24
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
25
+ import type { CoordinationSignatureContext } from '@aztec/stdlib/p2p';
26
+ import type { FailedTx, Tx } from '@aztec/stdlib/tx';
27
+ import type {
28
+ BuildBlockInCheckpointResult,
29
+ CheckpointBuilder,
30
+ FullNodeCheckpointsBuilder,
31
+ } from '@aztec/validator-client';
32
+
33
+ import type { GlobalVariableBuilder } from '../../global_variable_builder/global_builder.js';
34
+ import type { SequencerPublisherFactory } from '../../publisher/sequencer-publisher-factory.js';
35
+ import type { SequencerPublisher } from '../../publisher/sequencer-publisher.js';
36
+ import type { SequencerConfig } from '../config.js';
37
+
38
+ /**
39
+ * L1 rollup constants needed by the AutomineSequencer. Same as SequencerRollupConstants
40
+ * plus `epochDuration` for the epoch-based checkpoint out-hash lookup.
41
+ */
42
+ export type AutomineSequencerConstants = Pick<
43
+ L1RollupConstants,
44
+ 'ethereumSlotDuration' | 'l1GenesisTime' | 'slotDuration' | 'rollupManaLimit' | 'epochDuration'
45
+ >;
46
+
47
+ /** Dependencies for the AutomineSequencer. */
48
+ export type AutomineSequencerDeps = {
49
+ publisherFactory: SequencerPublisherFactory;
50
+ checkpointsBuilder: FullNodeCheckpointsBuilder;
51
+ globalsBuilder: GlobalVariableBuilder;
52
+ worldState: WorldStateSynchronizer;
53
+ l2BlockSource: L2BlockSource;
54
+ l1ToL2MessageSource: L1ToL2MessageSource;
55
+ /** P2P client; must also expose `sync()` for post-rollback pool recovery. */
56
+ p2pClient: P2P & Pick<ConcreteP2PClient, 'sync'>;
57
+ ethCheatCodes: EthCheatCodes;
58
+ dateProvider: TestDateProvider;
59
+ l1Constants: AutomineSequencerConstants;
60
+ coinbase: EthAddress;
61
+ feeRecipient: AztecAddress;
62
+ signatureContext: CoordinationSignatureContext;
63
+ config: SequencerConfig & Pick<ChainConfig, 'l1ChainId' | 'rollupAddress'>;
64
+ /**
65
+ * Archiver used to push locally-built blocks and proposed checkpoints into the in-memory
66
+ * store, force an immediate L1 sync after publishing, and roll back state during reorgs.
67
+ */
68
+ archiver: Pick<Archiver, 'rollbackTo' | 'addBlock' | 'addProposedCheckpoint' | 'syncImmediate'>;
69
+ /** L1 tx utils whose cached nonces must be reset after an L1 reorg. */
70
+ l1TxUtils: Pick<L1TxUtils, 'resetNonce'>[];
71
+ /**
72
+ * Optional extra cleanup hook awaited inside {@link AutomineSequencer.stop}. Used by the
73
+ * factory to shut down the PublisherManager funding loop after the queue and poller drain.
74
+ */
75
+ stopExtras?: () => Promise<void>;
76
+ /** How often to poll the mempool for new txs while running. Defaults to 50ms. */
77
+ pollIntervalMs?: number;
78
+ log?: Logger;
79
+ };
80
+
81
+ /**
82
+ * Minimal, deterministic, queue-driven sequencer for e2e tests that don't exercise
83
+ * block-building or consensus (e.g. e2e_token, e2e_amm, e2e_authwit).
84
+ *
85
+ * Differences from the production `Sequencer`:
86
+ * - No proposer-turn check (single sequencer).
87
+ * - No sync check, no pipelining, no validator orchestration, no attestations,
88
+ * no slashing, no votes, no P2P proposal gossip, no timetable enforcement.
89
+ * - No event emission — consumers (archiver, world-state, `EpochTestSettler`) observe
90
+ * L1 and the archiver tip rather than sequencer events.
91
+ * - All test-driven time control (warp / mine empty block) goes through a single
92
+ * serial queue alongside mempool-driven builds; the three never interleave.
93
+ *
94
+ * Requires `aztecTargetCommitteeSize == 0` on the deployed rollup so that the
95
+ * L1 `verifyProposer` / `verifyAttestations` short-circuits accept an empty
96
+ * `CommitteeAttestationsAndSigners` (see
97
+ * `l1-contracts/src/core/libraries/rollup/ValidatorSelectionLib.sol:244`).
98
+ */
99
+ export class AutomineSequencer {
100
+ private readonly log: Logger;
101
+ private readonly queue: SerialQueue;
102
+ private readonly pollIntervalMs: number;
103
+ private readonly deps: AutomineSequencerDeps;
104
+
105
+ private running = false;
106
+ private stopped = false;
107
+ private paused = false;
108
+ private mempoolPoller?: RunningPromise;
109
+ private publisher?: SequencerPublisher;
110
+ private attestorAddress?: EthAddress;
111
+
112
+ /** Set while a mempool-driven build is queued but not yet run; used to coalesce. */
113
+ private buildQueued: Promise<L2Block | undefined> | undefined = undefined;
114
+
115
+ /** Last L2 slot we published a checkpoint for (-1 means none yet). */
116
+ private lastBuiltSlot: number = -1;
117
+
118
+ constructor(deps: AutomineSequencerDeps) {
119
+ this.deps = deps;
120
+ this.log = deps.log ?? createLogger('sequencer:automine');
121
+ this.queue = new SerialQueue();
122
+ this.pollIntervalMs = deps.pollIntervalMs ?? 50;
123
+ }
124
+
125
+ /**
126
+ * Starts the sequencer. Switches anvil into automine mode (no interval mining),
127
+ * acquires a publisher, and begins polling the mempool for pending txs.
128
+ */
129
+ public async start(): Promise<void> {
130
+ if (this.running) {
131
+ return;
132
+ }
133
+ this.running = true;
134
+
135
+ await this.deps.ethCheatCodes.setIntervalMining(0, { silent: true });
136
+ await this.deps.ethCheatCodes.setAutomine(true, { silent: true });
137
+
138
+ const { publisher, attestorAddress } = await this.deps.publisherFactory.create();
139
+ this.publisher = publisher;
140
+ this.attestorAddress = attestorAddress;
141
+ this.log.info(`AutomineSequencer started`, {
142
+ publisher: this.publisher.getSenderAddress().toString(),
143
+ attestor: this.attestorAddress.toString(),
144
+ });
145
+
146
+ this.queue.start();
147
+
148
+ // Fallback poller that triggers a build if we discover pending txs without an explicit
149
+ // notification. Tests can also call `buildIfPending()` directly to skip the poll wait.
150
+ this.mempoolPoller = new RunningPromise(() => this.maybeEnqueueBuild(), this.log, this.pollIntervalMs);
151
+ this.mempoolPoller.start();
152
+ }
153
+
154
+ /**
155
+ * Stops the sequencer. Drains the queue, unsubscribes the mempool poller, and runs any
156
+ * registered {@link AutomineSequencerDeps.stopExtras} hook (e.g. the PublisherManager's
157
+ * funding loop). Idempotent: subsequent calls return without re-running cleanup.
158
+ */
159
+ public async stop(): Promise<void> {
160
+ if (this.stopped) {
161
+ return;
162
+ }
163
+ this.stopped = true;
164
+ this.running = false;
165
+ await this.mempoolPoller?.stop();
166
+ await this.queue.end();
167
+ await this.deps.stopExtras?.();
168
+ this.log.info('AutomineSequencer stopped');
169
+ }
170
+
171
+ /**
172
+ * Enqueues a mempool-driven build. Coalesces consecutive calls so a burst of new txs
173
+ * collapses into one build job. Returns the built block (or undefined if nothing was built
174
+ * or the sequencer is paused).
175
+ */
176
+ public buildIfPending(): Promise<L2Block | undefined> {
177
+ if (!this.running || this.paused) {
178
+ return Promise.resolve(undefined);
179
+ }
180
+ if (this.buildQueued) {
181
+ // A build is already queued; coalesce. The pending build will pick up the new txs.
182
+ return this.buildQueued;
183
+ }
184
+ this.buildQueued = this.queue.put(async () => {
185
+ try {
186
+ return await this.runBuild({ allowEmpty: false });
187
+ } finally {
188
+ this.buildQueued = undefined;
189
+ }
190
+ });
191
+ return this.buildQueued;
192
+ }
193
+
194
+ /**
195
+ * Pauses mempool-driven block production. Pending txs accumulate in the pool without being
196
+ * mined. Explicit test-harness operations (`buildEmptyBlock`, `warpTo`, `revertToCheckpoint`)
197
+ * continue to work; this only gates the mempool poller and `buildIfPending`.
198
+ */
199
+ public pause(): void {
200
+ if (this.paused) {
201
+ return;
202
+ }
203
+ this.paused = true;
204
+ this.log.info('AutomineSequencer paused');
205
+ }
206
+
207
+ /** Resumes mempool-driven block production after a previous {@link pause}. */
208
+ public resume(): void {
209
+ if (!this.paused) {
210
+ return;
211
+ }
212
+ this.paused = false;
213
+ this.log.info('AutomineSequencer resumed');
214
+ }
215
+
216
+ /**
217
+ * Updates the in-memory sequencer config. Mirrors {@link SequencerClient.updateConfig} so that
218
+ * `AztecNode.setConfig` (e.g. tests bumping `minTxsPerBlock` to bundle multiple txs into one
219
+ * block) propagates to the AutomineSequencer's gating logic in {@link runBuild}.
220
+ */
221
+ public updateConfig(config: Partial<SequencerConfig>): void {
222
+ Object.assign(this.deps.config, config);
223
+ }
224
+
225
+ /** Returns a snapshot of the current sequencer config (used by pause/resume bookkeeping). */
226
+ public getConfig(): SequencerConfig {
227
+ return this.deps.config;
228
+ }
229
+
230
+ /** Enqueues an empty-block build. Resolves to the built block. */
231
+ public buildEmptyBlock(): Promise<L2Block> {
232
+ return this.queue.put(async () => {
233
+ const block = await this.runBuild({ allowEmpty: true });
234
+ if (!block) {
235
+ throw new Error('buildEmptyBlock: runBuild returned undefined');
236
+ }
237
+ return block;
238
+ });
239
+ }
240
+
241
+ /**
242
+ * Warps L1 timestamp to `targetTimestampSec`. Rounded up to the next aztec-slot
243
+ * boundary so the next build lands on a fresh slot. Atomic with respect to builds —
244
+ * the queue ensures no build is in flight while the warp executes.
245
+ */
246
+ public warpTo(targetTimestampSec: number): Promise<void> {
247
+ return this.queue.put(() => this.runWarp(targetTimestampSec));
248
+ }
249
+
250
+ /** Warps L1 timestamp forward by `deltaSec` seconds from the current L1 time. */
251
+ public warpBy(deltaSec: number): Promise<void> {
252
+ return this.queue.put(async () => {
253
+ const current = await this.deps.ethCheatCodes.lastBlockTimestamp();
254
+ await this.runWarp(current + deltaSec);
255
+ });
256
+ }
257
+
258
+ /**
259
+ * Reorgs L1 so that every L1 block strictly after the one that published
260
+ * `targetCheckpoint` is removed. The archiver, world-state, and date provider
261
+ * are all brought back in sync before the promise resolves.
262
+ *
263
+ * Runs inside the serial queue so it never interleaves with a build or warp.
264
+ */
265
+ public revertToCheckpoint(targetCheckpoint: number): Promise<void> {
266
+ return this.queue.put(() => this.runRevert(targetCheckpoint));
267
+ }
268
+
269
+ /** Awaits the queue draining to a fully idle state. */
270
+ public syncPoint(): Promise<void> {
271
+ return this.queue.syncPoint();
272
+ }
273
+
274
+ /** Called from the mempool poller. Enqueues a build if there are pending txs. */
275
+ private async maybeEnqueueBuild(): Promise<void> {
276
+ if (!this.running || this.paused || this.buildQueued) {
277
+ return;
278
+ }
279
+ try {
280
+ const pending = await this.deps.p2pClient.getPendingTxCount();
281
+ if (pending > 0) {
282
+ // Fire-and-forget; the build result is delivered via `buildIfPending()` callers,
283
+ // not via the poller.
284
+ void this.buildIfPending().catch(err => {
285
+ this.log.error('Mempool-driven build failed', err);
286
+ });
287
+ }
288
+ } catch (err) {
289
+ this.log.warn('Failed to poll mempool', { err: err instanceof Error ? err.message : String(err) });
290
+ }
291
+ }
292
+
293
+ /** Builds one checkpoint with a single block, publishes it, syncs the date provider. */
294
+ private async runBuild({ allowEmpty }: { allowEmpty: boolean }): Promise<L2Block | undefined> {
295
+ if (!this.running || !this.publisher || !this.attestorAddress) {
296
+ return undefined;
297
+ }
298
+
299
+ const txCount = await this.deps.p2pClient.getPendingTxCount();
300
+ // For mempool-driven builds, wait for at least `minTxsPerBlock` pending txs (or 1 if not set)
301
+ // before building. This mirrors the production sequencer's `waitForMinTxs` behavior, and is
302
+ // required for tests that bundle multiple txs into one block via `setConfig({ minTxsPerBlock })`.
303
+ // Explicit empty-block / warp paths pass `allowEmpty: true` and bypass this gate.
304
+ const minRequired = allowEmpty ? 0 : Math.max(this.deps.config.minTxsPerBlock ?? 1, 1);
305
+ if (txCount < minRequired) {
306
+ return undefined;
307
+ }
308
+
309
+ // Decide target slot from the pending block's timestamp — picks up any prior
310
+ // `setNextBlockTimestamp` call (e.g. queued by runWarp) instead of assuming +1 over
311
+ // the last mined block.
312
+ const pendingBlockTs = await this.deps.ethCheatCodes.nextBlockTimestamp();
313
+ let targetSlot = Number(getSlotAtTimestamp(BigInt(pendingBlockTs), this.deps.l1Constants));
314
+ if (targetSlot <= this.lastBuiltSlot) {
315
+ // Pending block doesn't reach a new slot yet; advance to the next slot we own.
316
+ targetSlot = this.lastBuiltSlot + 1;
317
+ }
318
+ const slotBoundaryTs = Number(getTimestampForSlot(SlotNumber(targetSlot), this.deps.l1Constants));
319
+
320
+ // Pre-set anvil's next block timestamp only if the slot boundary is past what's already
321
+ // scheduled. setNextBlockTimestamp rejects values not strictly greater than the last block's
322
+ // timestamp, and pendingBlockTs is always > lastBlockTimestamp, so this guard is sufficient.
323
+ if (slotBoundaryTs > pendingBlockTs) {
324
+ await this.deps.ethCheatCodes.setNextBlockTimestamp(slotBoundaryTs);
325
+ }
326
+
327
+ const tips = await this.deps.l2BlockSource.getL2Tips();
328
+ const syncedToBlockNumber = tips.proposed.number;
329
+
330
+ // Ensure world state has processed the archiver's tip before forking. Without this,
331
+ // world state may still be at the previous block (since it syncs asynchronously from
332
+ // the archiver), and `fork(syncedToBlockNumber)` would fail with
333
+ // "Unable to initialize from future block".
334
+ await this.deps.worldState.syncImmediate(BlockNumber(syncedToBlockNumber));
335
+
336
+ const nextBlockNumber = BlockNumber(syncedToBlockNumber + 1);
337
+ const checkpointNumber = CheckpointNumber(tips.proposedCheckpoint.checkpoint.number + 1);
338
+ const targetEpoch = getEpochAtSlot(SlotNumber(targetSlot), this.deps.l1Constants);
339
+
340
+ this.log.verbose(`Building automine checkpoint`, {
341
+ checkpointNumber,
342
+ blockNumber: nextBlockNumber,
343
+ slot: targetSlot,
344
+ slotTimestamp: slotBoundaryTs,
345
+ txCount,
346
+ allowEmpty,
347
+ });
348
+
349
+ const checkpointGlobals = await this.deps.globalsBuilder.buildCheckpointGlobalVariables(
350
+ this.deps.coinbase,
351
+ this.deps.feeRecipient,
352
+ SlotNumber(targetSlot),
353
+ );
354
+
355
+ const l1ToL2Messages = await this.deps.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
356
+
357
+ const previousCheckpointOutHashes = await getPreviousCheckpointOutHashes({
358
+ blockSource: this.deps.l2BlockSource,
359
+ epoch: targetEpoch,
360
+ checkpointNumber,
361
+ l1Constants: this.deps.l1Constants,
362
+ pipeliningEnabled: false,
363
+ log: this.log,
364
+ });
365
+
366
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
367
+
368
+ await using fork = await this.deps.worldState.fork(syncedToBlockNumber, { closeDelayMs: 0 });
369
+
370
+ const checkpointBuilder = await this.deps.checkpointsBuilder.startCheckpoint(
371
+ checkpointNumber,
372
+ checkpointGlobals,
373
+ feeAssetPriceModifier,
374
+ l1ToL2Messages,
375
+ previousCheckpointOutHashes,
376
+ fork,
377
+ this.log.getBindings(),
378
+ );
379
+
380
+ const pendingTxs = this.deps.p2pClient.iterateEligiblePendingTxs();
381
+
382
+ const buildResult = await this.tryBuildBlock(
383
+ checkpointBuilder,
384
+ pendingTxs,
385
+ nextBlockNumber,
386
+ checkpointGlobals.timestamp,
387
+ allowEmpty,
388
+ checkpointNumber,
389
+ );
390
+ if (!buildResult) {
391
+ return undefined;
392
+ }
393
+
394
+ const checkpoint = await checkpointBuilder.completeCheckpoint();
395
+
396
+ // Empty CommitteeAttestationsAndSigners is accepted on-chain when committee size is 0.
397
+ const emptyAttestations = CommitteeAttestationsAndSigners.empty(this.deps.signatureContext);
398
+ const emptyAttestationsSignature = Signature.empty();
399
+
400
+ // Push the block and proposed checkpoint into the archiver locally BEFORE publishing to L1.
401
+ // This avoids racing the archiver's L1 polling: if the L1 publish happened first, polling
402
+ // could surface the checkpoint and reject our subsequent local push as duplicate. Pushing
403
+ // first means the archiver already has the proposed entry when L1 polling fires; the L1
404
+ // sync path then promotes the existing proposed checkpoint via promoteProposedToCheckpointed
405
+ // rather than re-adding it.
406
+ await this.deps.archiver.addBlock(buildResult.block);
407
+ await this.deps.archiver.addProposedCheckpoint({
408
+ header: checkpoint.header,
409
+ checkpointNumber,
410
+ startBlock: BlockNumber(buildResult.block.number),
411
+ blockCount: 1,
412
+ totalManaUsed: checkpoint.header.totalManaUsed.toBigInt(),
413
+ feeAssetPriceModifier,
414
+ });
415
+
416
+ await this.publisher.enqueueProposeCheckpoint(checkpoint, emptyAttestations, emptyAttestationsSignature);
417
+ // Automine publishes synchronously in the current slot via `sendRequests`. It must NOT use the
418
+ // production `sendRequestsAt` (or `canProposeAt`), which always apply the one-slot pipelining
419
+ // offset — automine is the deliberate non-pipelined exception and builds/publishes in place.
420
+ const result = await this.publisher.sendRequests(SlotNumber(targetSlot));
421
+
422
+ const successful = result?.successfulActions?.find(a => a === 'propose');
423
+ if (!successful) {
424
+ this.log.error('Propose action did not succeed under automine', {
425
+ slot: targetSlot,
426
+ checkpointNumber,
427
+ successful: result?.successfulActions,
428
+ failed: result?.failedActions,
429
+ });
430
+ throw new Error(`AutomineSequencer: propose did not succeed for slot ${targetSlot}`);
431
+ }
432
+
433
+ // Force one full L1-sync cycle synchronously. The local addBlock/addProposedCheckpoint
434
+ // above advances the proposed tip, but tips.checkpointed and the L1->L2 inbox tree state
435
+ // only advance when the archiver observes the L1-confirmed checkpoint via its sync loop.
436
+ await this.deps.archiver.syncImmediate();
437
+
438
+ // Sync the date provider to the L1 block timestamp we just mined.
439
+ const newL1Ts = await this.deps.ethCheatCodes.lastBlockTimestamp();
440
+ this.deps.dateProvider.setTime(newL1Ts * 1000);
441
+
442
+ this.lastBuiltSlot = targetSlot;
443
+
444
+ this.log.verbose(`Automine checkpoint published`, {
445
+ checkpointNumber,
446
+ blockNumber: nextBlockNumber,
447
+ slot: targetSlot,
448
+ l1Timestamp: newL1Ts,
449
+ txCount: buildResult.numTxs,
450
+ });
451
+
452
+ return buildResult.block;
453
+ }
454
+
455
+ /**
456
+ * Warps L1 timestamp to (or past) `targetTimestampSec`, rounded up to the next aztec-slot
457
+ * boundary, by queuing an empty-checkpoint build at that slot. Mines exactly one L1 block
458
+ * (the propose tx auto-mined under anvil's automine mode), with the timestamp pre-set so
459
+ * the mined block lands on the slot boundary.
460
+ */
461
+ private async runWarp(targetTimestampSec: number): Promise<void> {
462
+ const currentL1Ts = await this.deps.ethCheatCodes.lastBlockTimestamp();
463
+ if (targetTimestampSec <= currentL1Ts) {
464
+ this.log.debug(`Warp target ${targetTimestampSec} is not in the future of current L1 ts ${currentL1Ts}`);
465
+ return;
466
+ }
467
+
468
+ // Round up to the next aztec-slot boundary so the next build naturally lands on a new slot.
469
+ const targetSlot = Number(getSlotAtTimestamp(BigInt(targetTimestampSec), this.deps.l1Constants));
470
+ const slotBoundaryTs = Number(getTimestampForSlot(SlotNumber(targetSlot + 1), this.deps.l1Constants));
471
+
472
+ // Queue the next L1 block at the slot boundary timestamp, then build (and publish) an
473
+ // empty L2 checkpoint. The propose tx auto-mines a single L1 block at slotBoundaryTs,
474
+ // and `runBuild` syncs the date provider to the new L1 timestamp.
475
+ await this.deps.ethCheatCodes.setNextBlockTimestamp(slotBoundaryTs);
476
+ await this.runBuild({ allowEmpty: true });
477
+
478
+ this.log.verbose(`Warped L1 to slot boundary`, { slot: targetSlot + 1, timestamp: slotBoundaryTs });
479
+ }
480
+
481
+ /**
482
+ * Rolls L1 back to the block that published `targetCheckpoint`, drops the archiver's
483
+ * in-memory state to match, and resets internal slot bookkeeping.
484
+ */
485
+ private async runRevert(targetCheckpoint: number): Promise<void> {
486
+ const checkpointData = await this.deps.l2BlockSource.getCheckpointData({
487
+ number: CheckpointNumber(targetCheckpoint),
488
+ });
489
+ if (!checkpointData) {
490
+ throw new Error(`AutomineSequencer: checkpoint ${targetCheckpoint} not found in archiver`);
491
+ }
492
+
493
+ const targetL1Block = Number(checkpointData.l1.blockNumber);
494
+ this.log.verbose(`Reverting to checkpoint ${targetCheckpoint}`, {
495
+ targetCheckpoint,
496
+ targetL1Block,
497
+ checkpointSlot: checkpointData.header.slotNumber,
498
+ });
499
+
500
+ // Roll the archiver back to the last block of targetCheckpoint before the L1 reorg,
501
+ // since the archiver needs to fetch the target checkpoint's L1 block hash during rollback.
502
+ const lastBlockInCheckpoint = BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
503
+ await this.deps.archiver.rollbackTo(lastBlockInCheckpoint);
504
+
505
+ // Force the P2P block stream to run one cycle immediately so it processes the
506
+ // chain-pruned event triggered by the archiver rollback above. Without this, the
507
+ // P2P pool may not have restored rolled-back txs to pending by the time the next
508
+ // build runs.
509
+ await this.deps.p2pClient.sync();
510
+
511
+ // Force world-state to process the archiver's prune event immediately, so the next build
512
+ // doesn't try to insert nullifiers that were already in the pruned checkpoints.
513
+ await this.deps.worldState.syncImmediate();
514
+
515
+ // Remove all L1 blocks strictly after the target checkpoint's publish block so that
516
+ // the propose txs for later checkpoints are gone from L1. We use reorg(depth) directly
517
+ // to keep targetL1Block itself as the new chain tip.
518
+ const currentL1Block = await this.deps.ethCheatCodes.publicClient.getBlockNumber();
519
+ const depth = Number(currentL1Block) - targetL1Block;
520
+ if (depth > 0) {
521
+ await this.deps.ethCheatCodes.reorg(depth);
522
+ }
523
+
524
+ // anvil_rollback re-queues the rolled-back txs into the mempool. Clear them so they
525
+ // don't get re-mined, then reset the publisher nonce tracker so the next propose tx
526
+ // uses the correct nonce for the post-reorg chain state.
527
+ await this.deps.ethCheatCodes.rpcCall('anvil_dropAllTransactions', []);
528
+ this.deps.l1TxUtils.forEach(utils => utils.resetNonce());
529
+
530
+ // Reset slot bookkeeping so the next build picks up at the correct slot.
531
+ this.lastBuiltSlot = Number(checkpointData.header.slotNumber);
532
+
533
+ this.log.verbose(`Reverted to checkpoint ${targetCheckpoint}`, {
534
+ targetCheckpoint,
535
+ targetL1Block,
536
+ });
537
+ }
538
+
539
+ /**
540
+ * Wraps `checkpointBuilder.buildBlock` with the failed-tx handling shared by both error
541
+ * and success paths: drops the failed txs from the P2P mempool, and returns `undefined`
542
+ * when `InsufficientValidTxsError` aborts the build (so the caller skips publishing).
543
+ */
544
+ private async tryBuildBlock(
545
+ checkpointBuilder: CheckpointBuilder,
546
+ pendingTxs: AsyncIterableIterator<Tx>,
547
+ nextBlockNumber: BlockNumber,
548
+ timestamp: bigint,
549
+ allowEmpty: boolean,
550
+ checkpointNumber: CheckpointNumber,
551
+ ): Promise<BuildBlockInCheckpointResult | undefined> {
552
+ let buildResult: BuildBlockInCheckpointResult;
553
+ try {
554
+ buildResult = await checkpointBuilder.buildBlock(pendingTxs, nextBlockNumber, timestamp, {
555
+ maxTransactions: this.deps.config.maxTxsPerBlock,
556
+ // Allow empty for explicit-empty builds; require at least 1 valid tx otherwise.
557
+ minValidTxs: allowEmpty ? 0 : 1,
558
+ isBuildingProposal: true,
559
+ maxBlocksPerCheckpoint: 1,
560
+ perBlockAllocationMultiplier: 1,
561
+ });
562
+ } catch (err) {
563
+ // Mirrors production's checkpoint_proposal_job: if every pending tx failed execution and
564
+ // we didn't reach minValidTxs, drop the failed txs from the mempool so they don't block
565
+ // the poller forever, then abort this build with no checkpoint published.
566
+ if (isErrorClass(err, InsufficientValidTxsError)) {
567
+ await this.dropFailedTxsFromP2P(err.failedTxs);
568
+ this.log.verbose(`AutomineSequencer: insufficient valid txs, skipping build`, {
569
+ checkpointNumber,
570
+ processedCount: err.processedCount,
571
+ minRequired: err.minRequired,
572
+ failedCount: err.failedTxs.length,
573
+ });
574
+ return undefined;
575
+ }
576
+ throw err;
577
+ }
578
+
579
+ // Drop any txs that failed execution but didn't trigger InsufficientValidTxsError, so we
580
+ // don't re-pick them up on the next build.
581
+ await this.dropFailedTxsFromP2P(buildResult.failedTxs);
582
+
583
+ return buildResult;
584
+ }
585
+
586
+ /** Removes txs that failed execution from the P2P mempool so they don't get retried. */
587
+ private async dropFailedTxsFromP2P(failedTxs: FailedTx[]): Promise<void> {
588
+ if (failedTxs.length === 0) {
589
+ return;
590
+ }
591
+ const failedTxHashes = failedTxs.map(fail => fail.tx.getTxHash());
592
+ this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
593
+ await this.deps.p2pClient.handleFailedExecution(failedTxHashes);
594
+ }
595
+ }