@aztec/aztec 0.0.1-commit.993d240 → 0.0.1-commit.9a89641

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 (75) hide show
  1. package/dest/bin/index.js +2 -0
  2. package/dest/cli/aztec_start_action.d.ts +1 -1
  3. package/dest/cli/aztec_start_action.d.ts.map +1 -1
  4. package/dest/cli/aztec_start_action.js +10 -26
  5. package/dest/cli/aztec_start_options.js +1 -1
  6. package/dest/cli/cmds/compile.d.ts +1 -1
  7. package/dest/cli/cmds/compile.d.ts.map +1 -1
  8. package/dest/cli/cmds/compile.js +25 -17
  9. package/dest/cli/cmds/prover.d.ts +4 -0
  10. package/dest/cli/cmds/prover.d.ts.map +1 -0
  11. package/dest/cli/cmds/prover.js +24 -0
  12. package/dest/cli/cmds/standby.d.ts +28 -2
  13. package/dest/cli/cmds/standby.d.ts.map +1 -1
  14. package/dest/cli/cmds/standby.js +45 -2
  15. package/dest/cli/cmds/start_bot.d.ts +1 -1
  16. package/dest/cli/cmds/start_bot.d.ts.map +1 -1
  17. package/dest/cli/cmds/start_bot.js +5 -1
  18. package/dest/cli/cmds/start_node.d.ts +1 -1
  19. package/dest/cli/cmds/start_node.d.ts.map +1 -1
  20. package/dest/cli/cmds/start_node.js +14 -23
  21. package/dest/cli/cmds/utils/artifacts.d.ts +6 -1
  22. package/dest/cli/cmds/utils/artifacts.d.ts.map +1 -1
  23. package/dest/cli/util.d.ts +10 -5
  24. package/dest/cli/util.d.ts.map +1 -1
  25. package/dest/cli/util.js +20 -49
  26. package/dest/examples/token.js +3 -3
  27. package/dest/local-network/auth_registry.d.ts +3 -2
  28. package/dest/local-network/auth_registry.d.ts.map +1 -1
  29. package/dest/local-network/auth_registry.js +5 -3
  30. package/dest/local-network/banana_fpc.d.ts +3 -2
  31. package/dest/local-network/banana_fpc.d.ts.map +1 -1
  32. package/dest/local-network/banana_fpc.js +5 -3
  33. package/dest/local-network/local-network.d.ts +6 -5
  34. package/dest/local-network/local-network.d.ts.map +1 -1
  35. package/dest/local-network/local-network.js +43 -78
  36. package/dest/testing/cheat_codes.d.ts +16 -23
  37. package/dest/testing/cheat_codes.d.ts.map +1 -1
  38. package/dest/testing/cheat_codes.js +20 -82
  39. package/dest/testing/epoch_test_settler.d.ts +2 -2
  40. package/dest/testing/epoch_test_settler.d.ts.map +1 -1
  41. package/dest/testing/epoch_test_settler.js +5 -26
  42. package/dest/testing/index.d.ts +2 -2
  43. package/dest/testing/index.d.ts.map +1 -1
  44. package/dest/testing/index.js +1 -1
  45. package/dest/testing/local-network.d.ts +51 -0
  46. package/dest/testing/local-network.d.ts.map +1 -0
  47. package/dest/testing/local-network.js +60 -0
  48. package/dest/testing/token_allowed_setup.d.ts +9 -4
  49. package/dest/testing/token_allowed_setup.d.ts.map +1 -1
  50. package/dest/testing/token_allowed_setup.js +12 -8
  51. package/package.json +34 -34
  52. package/scripts/templates/counter/contract/src/main.nr +4 -4
  53. package/src/bin/index.ts +2 -0
  54. package/src/cli/aztec_start_action.ts +8 -13
  55. package/src/cli/aztec_start_options.ts +1 -1
  56. package/src/cli/cmds/compile.ts +29 -16
  57. package/src/cli/cmds/prover.ts +42 -0
  58. package/src/cli/cmds/standby.ts +55 -3
  59. package/src/cli/cmds/start_bot.ts +6 -1
  60. package/src/cli/cmds/start_node.ts +17 -23
  61. package/src/cli/cmds/utils/artifacts.ts +5 -0
  62. package/src/cli/util.ts +21 -62
  63. package/src/examples/token.ts +11 -3
  64. package/src/local-network/auth_registry.ts +8 -3
  65. package/src/local-network/banana_fpc.ts +9 -2
  66. package/src/local-network/local-network.ts +48 -89
  67. package/src/testing/cheat_codes.ts +19 -103
  68. package/src/testing/epoch_test_settler.ts +8 -30
  69. package/src/testing/index.ts +1 -1
  70. package/src/testing/local-network.ts +97 -0
  71. package/src/testing/token_allowed_setup.ts +15 -8
  72. package/dest/testing/anvil_test_watcher.d.ts +0 -54
  73. package/dest/testing/anvil_test_watcher.d.ts.map +0 -1
  74. package/dest/testing/anvil_test_watcher.js +0 -235
  75. package/src/testing/anvil_test_watcher.ts +0 -273
@@ -1,54 +0,0 @@
1
- import { EthCheatCodes } from '@aztec/ethereum/test';
2
- import type { ViemClient } from '@aztec/ethereum/types';
3
- import type { EthAddress } from '@aztec/foundation/eth-address';
4
- import type { TestDateProvider } from '@aztec/foundation/timer';
5
- export type AnvilTestWatcherOpts = {
6
- isLocalNetwork?: boolean;
7
- isMarkingAsProven?: boolean;
8
- };
9
- /**
10
- * Represents a watcher for a rollup contract.
11
- *
12
- * It started on a network like anvil where time traveling is allowed, and auto-mine is turned on
13
- * it will periodically check if the current slot have already been filled, e.g., there was an L2
14
- * block within the slot. And if so, it will time travel into the next slot.
15
- */
16
- export declare class AnvilTestWatcher {
17
- private cheatcodes;
18
- private dateProvider?;
19
- private isLocalNetwork;
20
- private isMarkingAsProven;
21
- private rollup;
22
- private rollupCheatCodes;
23
- private l2SlotDuration;
24
- private filledRunningPromise?;
25
- private syncDateProviderPromise?;
26
- private markingAsProvenRunningPromise?;
27
- private logger;
28
- private getPendingTxCount?;
29
- private isSequencerBuilding?;
30
- private unfilledSlotFirstSeen?;
31
- private proposedTargetSlot?;
32
- constructor(cheatcodes: EthCheatCodes, rollupAddress: EthAddress, l1Client: ViemClient, dateProvider?: TestDateProvider | undefined, opts?: AnvilTestWatcherOpts);
33
- setIsMarkingAsProven(isMarkingAsProven: boolean): void;
34
- setisLocalNetwork(isLocalNetwork: boolean): void;
35
- /** Sets a callback to check for pending txs, used to skip unfilled slots faster when txs are waiting. */
36
- setGetPendingTxCount(fn: () => Promise<number>): void;
37
- /** Sets a callback to check if the sequencer is actively building, to avoid warping while it works. */
38
- setIsSequencerBuilding(fn: () => boolean): void;
39
- /**
40
- * Records the target slot for which the proposer has built a block destined for L1. Used by
41
- * the local-network watcher to fast-forward L1 (and the injected date provider) ahead of the
42
- * pipelined publisher's `sendRequestsAt` sleep so it ends promptly instead of waiting a full
43
- * wall-clock slot. Only ratchets up — late warps for stale slots are no-ops.
44
- */
45
- setProposedTargetSlot(slot: number): void;
46
- start(): Promise<void>;
47
- stop(): Promise<void>;
48
- trigger(): Promise<void>;
49
- markAsProven(): Promise<void>;
50
- syncDateProviderToL1IfBehind(): Promise<void>;
51
- warpTimeIfNeeded(): Promise<void>;
52
- private warpToTimestamp;
53
- }
54
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYW52aWxfdGVzdF93YXRjaGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdGVzdGluZy9hbnZpbF90ZXN0X3dhdGNoZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLGFBQWEsRUFBb0IsTUFBTSxzQkFBc0IsQ0FBQztBQUN2RSxPQUFPLEtBQUssRUFBRSxVQUFVLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUV4RCxPQUFPLEtBQUssRUFBRSxVQUFVLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUdoRSxPQUFPLEtBQUssRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBS2hFLE1BQU0sTUFBTSxvQkFBb0IsR0FBRztJQUNqQyxjQUFjLENBQUMsRUFBRSxPQUFPLENBQUM7SUFDekIsaUJBQWlCLENBQUMsRUFBRSxPQUFPLENBQUM7Q0FDN0IsQ0FBQztBQUVGOzs7Ozs7R0FNRztBQUNILHFCQUFhLGdCQUFnQjtJQThCekIsT0FBTyxDQUFDLFVBQVU7SUFHbEIsT0FBTyxDQUFDLFlBQVksQ0FBQztJQWhDdkIsT0FBTyxDQUFDLGNBQWMsQ0FBQztJQUN2QixPQUFPLENBQUMsaUJBQWlCLENBQUM7SUFFMUIsT0FBTyxDQUFDLE1BQU0sQ0FBc0Q7SUFDcEUsT0FBTyxDQUFDLGdCQUFnQixDQUFtQjtJQUMzQyxPQUFPLEN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AAc,EAAE,OAAO,QAExC;IAED,yGAAyG;IACzG,oBAAoB,CAAC,EAAE,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,QAE7C;IAED,uGAAuG;IACvG,sBAAsB,CAAC,EAAE,EAAE,MAAM,OAAO,QAEvC;IAED;;;;;OAKG;IACH,qBAAqB,CAAC,IAAI,EAAE,MAAM,QAIjC;IAEK,KAAK,kBAyBV;IAEK,IAAI,kBAIT;IAEK,OAAO,kBAIZ;IAEK,YAAY,kBAKjB;IAEK,4BAA4B,kBAsBjC;IAEK,gBAAgB,kBA0ErB;YAMa,eAAe;CAgB9B"}
@@ -1,235 +0,0 @@
1
- import { RollupCheatCodes } from '@aztec/ethereum/test';
2
- import { SlotNumber } from '@aztec/foundation/branded-types';
3
- import { createLogger } from '@aztec/foundation/log';
4
- import { RunningPromise } from '@aztec/foundation/running-promise';
5
- import { RollupAbi } from '@aztec/l1-artifacts/RollupAbi';
6
- import { getAddress, getContract } from 'viem';
7
- /**
8
- * Represents a watcher for a rollup contract.
9
- *
10
- * It started on a network like anvil where time traveling is allowed, and auto-mine is turned on
11
- * it will periodically check if the current slot have already been filled, e.g., there was an L2
12
- * block within the slot. And if so, it will time travel into the next slot.
13
- */ export class AnvilTestWatcher {
14
- cheatcodes;
15
- dateProvider;
16
- isLocalNetwork;
17
- isMarkingAsProven;
18
- rollup;
19
- rollupCheatCodes;
20
- l2SlotDuration;
21
- filledRunningPromise;
22
- syncDateProviderPromise;
23
- markingAsProvenRunningPromise;
24
- logger;
25
- // Optional callback to check if there are pending txs in the mempool.
26
- getPendingTxCount;
27
- // Optional callback to check if the sequencer is actively building a block.
28
- isSequencerBuilding;
29
- // Tracks when we first observed the current unfilled slot with pending txs (real wall time).
30
- unfilledSlotFirstSeen;
31
- // Latest target slot for which the proposer has built a block destined for L1 but which has
32
- // not yet been committed. Set by the proposer-pipelining hook from `block-proposed` events so
33
- // the watcher can advance L1 (and the injected date provider) to the target slot ahead of the
34
- // publisher's `sendRequestsAt` sleep, instead of waiting a full wall-clock slot.
35
- proposedTargetSlot;
36
- constructor(cheatcodes, rollupAddress, l1Client, dateProvider, opts = {}){
37
- this.cheatcodes = cheatcodes;
38
- this.dateProvider = dateProvider;
39
- this.logger = createLogger(`aztecjs:utils:watcher`);
40
- this.rollup = getContract({
41
- address: getAddress(rollupAddress.toString()),
42
- abi: RollupAbi,
43
- client: l1Client
44
- });
45
- this.rollupCheatCodes = new RollupCheatCodes(this.cheatcodes, {
46
- rollupAddress
47
- });
48
- this.isLocalNetwork = opts.isLocalNetwork ?? false;
49
- this.isMarkingAsProven = opts.isMarkingAsProven ?? true;
50
- this.logger.debug(`Watcher created for rollup at ${rollupAddress}`);
51
- }
52
- setIsMarkingAsProven(isMarkingAsProven) {
53
- this.logger.warn(`Watcher is now ${isMarkingAsProven ? 'marking' : 'not marking'} blocks as proven`);
54
- this.isMarkingAsProven = isMarkingAsProven;
55
- }
56
- setisLocalNetwork(isLocalNetwork) {
57
- this.isLocalNetwork = isLocalNetwork;
58
- }
59
- /** Sets a callback to check for pending txs, used to skip unfilled slots faster when txs are waiting. */ setGetPendingTxCount(fn) {
60
- this.getPendingTxCount = fn;
61
- }
62
- /** Sets a callback to check if the sequencer is actively building, to avoid warping while it works. */ setIsSequencerBuilding(fn) {
63
- this.isSequencerBuilding = fn;
64
- }
65
- /**
66
- * Records the target slot for which the proposer has built a block destined for L1. Used by
67
- * the local-network watcher to fast-forward L1 (and the injected date provider) ahead of the
68
- * pipelined publisher's `sendRequestsAt` sleep so it ends promptly instead of waiting a full
69
- * wall-clock slot. Only ratchets up — late warps for stale slots are no-ops.
70
- */ setProposedTargetSlot(slot) {
71
- if (this.proposedTargetSlot === undefined || slot > this.proposedTargetSlot) {
72
- this.proposedTargetSlot = slot;
73
- }
74
- }
75
- async start() {
76
- if (this.filledRunningPromise) {
77
- throw new Error('Watcher already watching for filled slot');
78
- }
79
- const config = await this.rollupCheatCodes.getConfig();
80
- this.l2SlotDuration = config.slotDuration;
81
- // If auto mining is not supported (e.g., we are on a real network), then we
82
- // will simple do nothing. But if on an anvil or the like, this make sure that
83
- // the local network and tests don't break because time is frozen and we never get to
84
- // the next slot.
85
- const isAutoMining = await this.cheatcodes.isAutoMining();
86
- if (isAutoMining) {
87
- this.filledRunningPromise = new RunningPromise(()=>this.warpTimeIfNeeded(), this.logger, 200);
88
- this.filledRunningPromise.start();
89
- this.syncDateProviderPromise = new RunningPromise(()=>this.syncDateProviderToL1IfBehind(), this.logger, 200);
90
- this.syncDateProviderPromise.start();
91
- this.markingAsProvenRunningPromise = new RunningPromise(()=>this.markAsProven(), this.logger, 200);
92
- this.markingAsProvenRunningPromise.start();
93
- this.logger.info(`Watcher started for rollup at ${this.rollup.address}`);
94
- } else {
95
- this.logger.info(`Watcher not started because not auto mining`);
96
- }
97
- }
98
- async stop() {
99
- await this.filledRunningPromise?.stop();
100
- await this.syncDateProviderPromise?.stop();
101
- await this.markingAsProvenRunningPromise?.stop();
102
- }
103
- async trigger() {
104
- await this.filledRunningPromise?.trigger();
105
- await this.syncDateProviderPromise?.trigger();
106
- await this.markingAsProvenRunningPromise?.trigger();
107
- }
108
- async markAsProven() {
109
- if (!this.isMarkingAsProven) {
110
- return;
111
- }
112
- await this.rollupCheatCodes.markAsProven();
113
- }
114
- async syncDateProviderToL1IfBehind() {
115
- // this doesn't apply to the local network, because we don't have a date provider in the local network
116
- if (!this.dateProvider) {
117
- return;
118
- }
119
- const l1Time = await this.cheatcodes.lastBlockTimestamp() * 1000;
120
- const wallTime = this.dateProvider.now();
121
- if (l1Time > wallTime) {
122
- this.logger.warn(`L1 is ahead of wall time. Syncing wall time to L1 time`);
123
- this.dateProvider.setTime(l1Time);
124
- } else if (l1Time + Number(this.l2SlotDuration) * 1000 < wallTime) {
125
- // Warp L1 to the slot boundary at-or-before wall time. Rounding to a slot boundary (rather than
126
- // `ceil(wallTime / 1000)`) keeps this loop's target aligned with `warpTimeIfNeeded`'s
127
- // `nextSlotTimestamp` target, avoiding a race where the two loops pick timestamps a fraction of
128
- // a second apart and one of them is then rejected by anvil as non-monotonic.
129
- const wallSec = Math.floor(wallTime / 1000);
130
- const targetSlot = await this.rollup.read.getSlotAt([
131
- BigInt(wallSec)
132
- ]);
133
- const targetTimestamp = Number(await this.rollup.read.getTimestampForSlot([
134
- targetSlot
135
- ]));
136
- this.logger.warn(`L1 is more than 1 L2 slot behind wall time. Warping to slot ${targetSlot} boundary`);
137
- await this.warpToTimestamp(targetTimestamp);
138
- }
139
- }
140
- async warpTimeIfNeeded() {
141
- try {
142
- const currentSlot = SlotNumber.fromBigInt(await this.rollup.read.getCurrentSlot());
143
- const pendingCheckpointNumber = await this.rollup.read.getPendingCheckpointNumber();
144
- const checkpointLog = await this.rollup.read.getCheckpoint([
145
- pendingCheckpointNumber
146
- ]);
147
- const nextSlot = SlotNumber(currentSlot + 1);
148
- const nextSlotTimestamp = Number(await this.rollup.read.getTimestampForSlot([
149
- BigInt(nextSlot)
150
- ]));
151
- if (BigInt(currentSlot) === checkpointLog.slotNumber) {
152
- // The current slot has been filled, we should jump to the next slot.
153
- if (await this.warpToTimestamp(nextSlotTimestamp)) {
154
- this.logger.info(`Slot ${currentSlot} was filled, jumped to next slot`);
155
- }
156
- return;
157
- }
158
- // If we are not in local network, we don't need to warp time
159
- if (!this.isLocalNetwork) {
160
- return;
161
- }
162
- // Pipelined-publish shortcut: if the proposer has built a block destined for a slot
163
- // beyond the current L1 slot, fast-forward L1 to that slot's timestamp so the publisher's
164
- // `sendRequestsAt(targetSlot)` sleep ends and the multicall mines inside the target slot.
165
- // Without this, the publisher waits up to a full real-time slot for wall clock to catch up.
166
- if (this.proposedTargetSlot !== undefined && this.proposedTargetSlot > currentSlot) {
167
- const targetSlotTimestamp = Number(await this.rollup.read.getTimestampForSlot([
168
- BigInt(this.proposedTargetSlot)
169
- ]));
170
- if (await this.warpToTimestamp(targetSlotTimestamp)) {
171
- this.logger.info(`Warped L1 to target slot ${this.proposedTargetSlot} for pipelined publish`);
172
- }
173
- return;
174
- }
175
- // If there are pending txs and the sequencer missed them, warp quickly (after a 2s real-time debounce) so the
176
- // sequencer can retry in the next slot. Without this, we'd have to wait a full real-time slot duration (~36s) for
177
- // the dateProvider to catch up to the next slot timestamp. We skip the warp if the sequencer is actively building
178
- // to avoid invalidating its in-progress work.
179
- if (this.getPendingTxCount) {
180
- const pendingTxs = await this.getPendingTxCount();
181
- if (pendingTxs > 0) {
182
- if (this.isSequencerBuilding?.()) {
183
- this.unfilledSlotFirstSeen = undefined;
184
- return;
185
- }
186
- const realNow = Date.now();
187
- if (!this.unfilledSlotFirstSeen || this.unfilledSlotFirstSeen.slot !== currentSlot) {
188
- this.unfilledSlotFirstSeen = {
189
- slot: currentSlot,
190
- realTime: realNow
191
- };
192
- return;
193
- }
194
- if (realNow - this.unfilledSlotFirstSeen.realTime > 2000) {
195
- if (await this.warpToTimestamp(nextSlotTimestamp)) {
196
- this.logger.info(`Slot ${currentSlot} was missed with pending txs, jumped to next slot`);
197
- }
198
- this.unfilledSlotFirstSeen = undefined;
199
- }
200
- return;
201
- }
202
- }
203
- // Fallback: warp when the dateProvider time has passed the next slot timestamp.
204
- const currentTimestamp = this.dateProvider?.now() ?? Date.now();
205
- if (currentTimestamp > nextSlotTimestamp * 1000) {
206
- if (await this.warpToTimestamp(nextSlotTimestamp)) {
207
- this.logger.info(`Slot ${currentSlot} was missed, jumped to next slot`);
208
- }
209
- }
210
- } catch {
211
- this.logger.error('mineIfSlotFilled failed');
212
- }
213
- }
214
- /**
215
- * Warps L1 to `timestamp`, unless L1 is already at or past it. Returns true when a warp actually
216
- * happened, false when skipped or on error. Callers use the return value to gate success logs.
217
- */ async warpToTimestamp(timestamp) {
218
- try {
219
- // Anvil rejects evm_setNextBlockTimestamp values <= the current block's timestamp. The two
220
- // watcher loops can race and pick targets a fraction of a second apart; skip here rather than
221
- // letting the second one error out noisily.
222
- const lastTimestamp = await this.cheatcodes.lastBlockTimestamp();
223
- if (timestamp <= lastTimestamp) {
224
- return false;
225
- }
226
- await this.cheatcodes.warp(timestamp, {
227
- resetBlockInterval: true
228
- });
229
- return true;
230
- } catch (e) {
231
- this.logger.error(`Failed to warp to timestamp ${timestamp}: ${e}`);
232
- return false;
233
- }
234
- }
235
- }
@@ -1,273 +0,0 @@
1
- import { EthCheatCodes, RollupCheatCodes } from '@aztec/ethereum/test';
2
- import type { ViemClient } from '@aztec/ethereum/types';
3
- import { SlotNumber } from '@aztec/foundation/branded-types';
4
- import type { EthAddress } from '@aztec/foundation/eth-address';
5
- import { type Logger, createLogger } from '@aztec/foundation/log';
6
- import { RunningPromise } from '@aztec/foundation/running-promise';
7
- import type { TestDateProvider } from '@aztec/foundation/timer';
8
- import { RollupAbi } from '@aztec/l1-artifacts/RollupAbi';
9
-
10
- import { type GetContractReturnType, getAddress, getContract } from 'viem';
11
-
12
- export type AnvilTestWatcherOpts = {
13
- isLocalNetwork?: boolean;
14
- isMarkingAsProven?: boolean;
15
- };
16
-
17
- /**
18
- * Represents a watcher for a rollup contract.
19
- *
20
- * It started on a network like anvil where time traveling is allowed, and auto-mine is turned on
21
- * it will periodically check if the current slot have already been filled, e.g., there was an L2
22
- * block within the slot. And if so, it will time travel into the next slot.
23
- */
24
- export class AnvilTestWatcher {
25
- private isLocalNetwork;
26
- private isMarkingAsProven;
27
-
28
- private rollup: GetContractReturnType<typeof RollupAbi, ViemClient>;
29
- private rollupCheatCodes: RollupCheatCodes;
30
- private l2SlotDuration!: number;
31
-
32
- private filledRunningPromise?: RunningPromise;
33
- private syncDateProviderPromise?: RunningPromise;
34
- private markingAsProvenRunningPromise?: RunningPromise;
35
-
36
- private logger: Logger = createLogger(`aztecjs:utils:watcher`);
37
-
38
- // Optional callback to check if there are pending txs in the mempool.
39
- private getPendingTxCount?: () => Promise<number>;
40
-
41
- // Optional callback to check if the sequencer is actively building a block.
42
- private isSequencerBuilding?: () => boolean;
43
-
44
- // Tracks when we first observed the current unfilled slot with pending txs (real wall time).
45
- private unfilledSlotFirstSeen?: { slot: number; realTime: number };
46
-
47
- // Latest target slot for which the proposer has built a block destined for L1 but which has
48
- // not yet been committed. Set by the proposer-pipelining hook from `block-proposed` events so
49
- // the watcher can advance L1 (and the injected date provider) to the target slot ahead of the
50
- // publisher's `sendRequestsAt` sleep, instead of waiting a full wall-clock slot.
51
- private proposedTargetSlot?: number;
52
-
53
- constructor(
54
- private cheatcodes: EthCheatCodes,
55
- rollupAddress: EthAddress,
56
- l1Client: ViemClient,
57
- private dateProvider?: TestDateProvider,
58
- opts: AnvilTestWatcherOpts = {},
59
- ) {
60
- this.rollup = getContract({
61
- address: getAddress(rollupAddress.toString()),
62
- abi: RollupAbi,
63
- client: l1Client,
64
- });
65
-
66
- this.rollupCheatCodes = new RollupCheatCodes(this.cheatcodes, {
67
- rollupAddress,
68
- });
69
-
70
- this.isLocalNetwork = opts.isLocalNetwork ?? false;
71
- this.isMarkingAsProven = opts.isMarkingAsProven ?? true;
72
-
73
- this.logger.debug(`Watcher created for rollup at ${rollupAddress}`);
74
- }
75
-
76
- setIsMarkingAsProven(isMarkingAsProven: boolean) {
77
- this.logger.warn(`Watcher is now ${isMarkingAsProven ? 'marking' : 'not marking'} blocks as proven`);
78
- this.isMarkingAsProven = isMarkingAsProven;
79
- }
80
-
81
- setisLocalNetwork(isLocalNetwork: boolean) {
82
- this.isLocalNetwork = isLocalNetwork;
83
- }
84
-
85
- /** Sets a callback to check for pending txs, used to skip unfilled slots faster when txs are waiting. */
86
- setGetPendingTxCount(fn: () => Promise<number>) {
87
- this.getPendingTxCount = fn;
88
- }
89
-
90
- /** Sets a callback to check if the sequencer is actively building, to avoid warping while it works. */
91
- setIsSequencerBuilding(fn: () => boolean) {
92
- this.isSequencerBuilding = fn;
93
- }
94
-
95
- /**
96
- * Records the target slot for which the proposer has built a block destined for L1. Used by
97
- * the local-network watcher to fast-forward L1 (and the injected date provider) ahead of the
98
- * pipelined publisher's `sendRequestsAt` sleep so it ends promptly instead of waiting a full
99
- * wall-clock slot. Only ratchets up — late warps for stale slots are no-ops.
100
- */
101
- setProposedTargetSlot(slot: number) {
102
- if (this.proposedTargetSlot === undefined || slot > this.proposedTargetSlot) {
103
- this.proposedTargetSlot = slot;
104
- }
105
- }
106
-
107
- async start() {
108
- if (this.filledRunningPromise) {
109
- throw new Error('Watcher already watching for filled slot');
110
- }
111
-
112
- const config = await this.rollupCheatCodes.getConfig();
113
- this.l2SlotDuration = config.slotDuration;
114
-
115
- // If auto mining is not supported (e.g., we are on a real network), then we
116
- // will simple do nothing. But if on an anvil or the like, this make sure that
117
- // the local network and tests don't break because time is frozen and we never get to
118
- // the next slot.
119
- const isAutoMining = await this.cheatcodes.isAutoMining();
120
-
121
- if (isAutoMining) {
122
- this.filledRunningPromise = new RunningPromise(() => this.warpTimeIfNeeded(), this.logger, 200);
123
- this.filledRunningPromise.start();
124
- this.syncDateProviderPromise = new RunningPromise(() => this.syncDateProviderToL1IfBehind(), this.logger, 200);
125
- this.syncDateProviderPromise.start();
126
- this.markingAsProvenRunningPromise = new RunningPromise(() => this.markAsProven(), this.logger, 200);
127
- this.markingAsProvenRunningPromise.start();
128
- this.logger.info(`Watcher started for rollup at ${this.rollup.address}`);
129
- } else {
130
- this.logger.info(`Watcher not started because not auto mining`);
131
- }
132
- }
133
-
134
- async stop() {
135
- await this.filledRunningPromise?.stop();
136
- await this.syncDateProviderPromise?.stop();
137
- await this.markingAsProvenRunningPromise?.stop();
138
- }
139
-
140
- async trigger() {
141
- await this.filledRunningPromise?.trigger();
142
- await this.syncDateProviderPromise?.trigger();
143
- await this.markingAsProvenRunningPromise?.trigger();
144
- }
145
-
146
- async markAsProven() {
147
- if (!this.isMarkingAsProven) {
148
- return;
149
- }
150
- await this.rollupCheatCodes.markAsProven();
151
- }
152
-
153
- async syncDateProviderToL1IfBehind() {
154
- // this doesn't apply to the local network, because we don't have a date provider in the local network
155
- if (!this.dateProvider) {
156
- return;
157
- }
158
-
159
- const l1Time = (await this.cheatcodes.lastBlockTimestamp()) * 1000;
160
- const wallTime = this.dateProvider.now();
161
- if (l1Time > wallTime) {
162
- this.logger.warn(`L1 is ahead of wall time. Syncing wall time to L1 time`);
163
- this.dateProvider.setTime(l1Time);
164
- } else if (l1Time + Number(this.l2SlotDuration) * 1000 < wallTime) {
165
- // Warp L1 to the slot boundary at-or-before wall time. Rounding to a slot boundary (rather than
166
- // `ceil(wallTime / 1000)`) keeps this loop's target aligned with `warpTimeIfNeeded`'s
167
- // `nextSlotTimestamp` target, avoiding a race where the two loops pick timestamps a fraction of
168
- // a second apart and one of them is then rejected by anvil as non-monotonic.
169
- const wallSec = Math.floor(wallTime / 1000);
170
- const targetSlot = await this.rollup.read.getSlotAt([BigInt(wallSec)]);
171
- const targetTimestamp = Number(await this.rollup.read.getTimestampForSlot([targetSlot]));
172
- this.logger.warn(`L1 is more than 1 L2 slot behind wall time. Warping to slot ${targetSlot} boundary`);
173
- await this.warpToTimestamp(targetTimestamp);
174
- }
175
- }
176
-
177
- async warpTimeIfNeeded() {
178
- try {
179
- const currentSlot = SlotNumber.fromBigInt(await this.rollup.read.getCurrentSlot());
180
- const pendingCheckpointNumber = await this.rollup.read.getPendingCheckpointNumber();
181
- const checkpointLog = await this.rollup.read.getCheckpoint([pendingCheckpointNumber]);
182
- const nextSlot = SlotNumber(currentSlot + 1);
183
- const nextSlotTimestamp = Number(await this.rollup.read.getTimestampForSlot([BigInt(nextSlot)]));
184
-
185
- if (BigInt(currentSlot) === checkpointLog.slotNumber) {
186
- // The current slot has been filled, we should jump to the next slot.
187
- if (await this.warpToTimestamp(nextSlotTimestamp)) {
188
- this.logger.info(`Slot ${currentSlot} was filled, jumped to next slot`);
189
- }
190
- return;
191
- }
192
-
193
- // If we are not in local network, we don't need to warp time
194
- if (!this.isLocalNetwork) {
195
- return;
196
- }
197
-
198
- // Pipelined-publish shortcut: if the proposer has built a block destined for a slot
199
- // beyond the current L1 slot, fast-forward L1 to that slot's timestamp so the publisher's
200
- // `sendRequestsAt(targetSlot)` sleep ends and the multicall mines inside the target slot.
201
- // Without this, the publisher waits up to a full real-time slot for wall clock to catch up.
202
- if (this.proposedTargetSlot !== undefined && this.proposedTargetSlot > currentSlot) {
203
- const targetSlotTimestamp = Number(
204
- await this.rollup.read.getTimestampForSlot([BigInt(this.proposedTargetSlot)]),
205
- );
206
- if (await this.warpToTimestamp(targetSlotTimestamp)) {
207
- this.logger.info(`Warped L1 to target slot ${this.proposedTargetSlot} for pipelined publish`);
208
- }
209
- return;
210
- }
211
-
212
- // If there are pending txs and the sequencer missed them, warp quickly (after a 2s real-time debounce) so the
213
- // sequencer can retry in the next slot. Without this, we'd have to wait a full real-time slot duration (~36s) for
214
- // the dateProvider to catch up to the next slot timestamp. We skip the warp if the sequencer is actively building
215
- // to avoid invalidating its in-progress work.
216
- if (this.getPendingTxCount) {
217
- const pendingTxs = await this.getPendingTxCount();
218
- if (pendingTxs > 0) {
219
- if (this.isSequencerBuilding?.()) {
220
- this.unfilledSlotFirstSeen = undefined;
221
- return;
222
- }
223
-
224
- const realNow = Date.now();
225
- if (!this.unfilledSlotFirstSeen || this.unfilledSlotFirstSeen.slot !== currentSlot) {
226
- this.unfilledSlotFirstSeen = { slot: currentSlot, realTime: realNow };
227
- return;
228
- }
229
-
230
- if (realNow - this.unfilledSlotFirstSeen.realTime > 2000) {
231
- if (await this.warpToTimestamp(nextSlotTimestamp)) {
232
- this.logger.info(`Slot ${currentSlot} was missed with pending txs, jumped to next slot`);
233
- }
234
- this.unfilledSlotFirstSeen = undefined;
235
- }
236
-
237
- return;
238
- }
239
- }
240
-
241
- // Fallback: warp when the dateProvider time has passed the next slot timestamp.
242
- const currentTimestamp = this.dateProvider?.now() ?? Date.now();
243
- if (currentTimestamp > nextSlotTimestamp * 1000) {
244
- if (await this.warpToTimestamp(nextSlotTimestamp)) {
245
- this.logger.info(`Slot ${currentSlot} was missed, jumped to next slot`);
246
- }
247
- }
248
- } catch {
249
- this.logger.error('mineIfSlotFilled failed');
250
- }
251
- }
252
-
253
- /**
254
- * Warps L1 to `timestamp`, unless L1 is already at or past it. Returns true when a warp actually
255
- * happened, false when skipped or on error. Callers use the return value to gate success logs.
256
- */
257
- private async warpToTimestamp(timestamp: number): Promise<boolean> {
258
- try {
259
- // Anvil rejects evm_setNextBlockTimestamp values <= the current block's timestamp. The two
260
- // watcher loops can race and pick targets a fraction of a second apart; skip here rather than
261
- // letting the second one error out noisily.
262
- const lastTimestamp = await this.cheatcodes.lastBlockTimestamp();
263
- if (timestamp <= lastTimestamp) {
264
- return false;
265
- }
266
- await this.cheatcodes.warp(timestamp, { resetBlockInterval: true });
267
- return true;
268
- } catch (e) {
269
- this.logger.error(`Failed to warp to timestamp ${timestamp}: ${e}`);
270
- return false;
271
- }
272
- }
273
- }