@aztec/aztec 1.2.1 → 2.0.0-nightly.20250813

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 (43) hide show
  1. package/dest/cli/chain_l2_config.d.ts +6 -2
  2. package/dest/cli/chain_l2_config.d.ts.map +1 -1
  3. package/dest/cli/chain_l2_config.js +24 -12
  4. package/dest/cli/cmds/start_node.d.ts.map +1 -1
  5. package/dest/cli/cmds/start_node.js +9 -2
  6. package/dest/cli/cmds/start_p2p_bootstrap.d.ts.map +1 -1
  7. package/dest/cli/cmds/start_p2p_bootstrap.js +3 -2
  8. package/dest/cli/cmds/start_prover_agent.d.ts.map +1 -1
  9. package/dest/cli/cmds/start_prover_agent.js +0 -5
  10. package/dest/cli/cmds/start_prover_node.d.ts.map +1 -1
  11. package/dest/cli/cmds/start_prover_node.js +0 -5
  12. package/dest/examples/token.js +21 -10
  13. package/dest/sandbox/banana_fpc.js +5 -3
  14. package/dest/sandbox/sandbox.d.ts.map +1 -1
  15. package/dest/sandbox/sandbox.js +10 -2
  16. package/dest/sandbox/sponsored_fpc.d.ts.map +1 -1
  17. package/dest/sandbox/sponsored_fpc.js +2 -2
  18. package/dest/testing/anvil_test_watcher.d.ts +34 -0
  19. package/dest/testing/anvil_test_watcher.d.ts.map +1 -0
  20. package/dest/testing/anvil_test_watcher.js +143 -0
  21. package/dest/testing/aztec_cheat_codes.d.ts +59 -0
  22. package/dest/testing/aztec_cheat_codes.d.ts.map +1 -0
  23. package/dest/testing/aztec_cheat_codes.js +62 -0
  24. package/dest/testing/cheat_codes.d.ts +44 -0
  25. package/dest/testing/cheat_codes.d.ts.map +1 -0
  26. package/dest/testing/cheat_codes.js +63 -0
  27. package/dest/testing/index.d.ts +5 -0
  28. package/dest/testing/index.d.ts.map +1 -0
  29. package/dest/testing/index.js +4 -0
  30. package/package.json +33 -31
  31. package/src/cli/chain_l2_config.ts +38 -14
  32. package/src/cli/cmds/start_node.ts +8 -2
  33. package/src/cli/cmds/start_p2p_bootstrap.ts +9 -2
  34. package/src/cli/cmds/start_prover_agent.ts +0 -6
  35. package/src/cli/cmds/start_prover_node.ts +1 -7
  36. package/src/examples/token.ts +11 -10
  37. package/src/sandbox/banana_fpc.ts +5 -5
  38. package/src/sandbox/sandbox.ts +8 -2
  39. package/src/sandbox/sponsored_fpc.ts +7 -2
  40. package/src/testing/anvil_test_watcher.ts +167 -0
  41. package/src/testing/aztec_cheat_codes.ts +77 -0
  42. package/src/testing/cheat_codes.ts +79 -0
  43. package/src/testing/index.ts +4 -0
@@ -0,0 +1,167 @@
1
+ import type { ViemClient } from '@aztec/ethereum';
2
+ import { EthCheatCodes, RollupCheatCodes } from '@aztec/ethereum/test';
3
+ import type { EthAddress } from '@aztec/foundation/eth-address';
4
+ import { type Logger, createLogger } from '@aztec/foundation/log';
5
+ import { RunningPromise } from '@aztec/foundation/running-promise';
6
+ import type { TestDateProvider } from '@aztec/foundation/timer';
7
+ import { RollupAbi } from '@aztec/l1-artifacts/RollupAbi';
8
+
9
+ import { type GetContractReturnType, getAddress, getContract } from 'viem';
10
+
11
+ /**
12
+ * Represents a watcher for a rollup contract.
13
+ *
14
+ * It started on a network like anvil where time traveling is allowed, and auto-mine is turned on
15
+ * it will periodically check if the current slot have already been filled, e.g., there was an L2
16
+ * block within the slot. And if so, it will time travel into the next slot.
17
+ */
18
+ export class AnvilTestWatcher {
19
+ private isSandbox: boolean = false;
20
+
21
+ private rollup: GetContractReturnType<typeof RollupAbi, ViemClient>;
22
+ private rollupCheatCodes: RollupCheatCodes;
23
+ private l2SlotDuration!: bigint;
24
+
25
+ private filledRunningPromise?: RunningPromise;
26
+ private syncDateProviderPromise?: RunningPromise;
27
+ private markingAsProvenRunningPromise?: RunningPromise;
28
+
29
+ private logger: Logger = createLogger(`aztecjs:utils:watcher`);
30
+
31
+ private isMarkingAsProven = true;
32
+
33
+ constructor(
34
+ private cheatcodes: EthCheatCodes,
35
+ rollupAddress: EthAddress,
36
+ l1Client: ViemClient,
37
+ private dateProvider?: TestDateProvider,
38
+ ) {
39
+ this.rollup = getContract({
40
+ address: getAddress(rollupAddress.toString()),
41
+ abi: RollupAbi,
42
+ client: l1Client,
43
+ });
44
+
45
+ this.rollupCheatCodes = new RollupCheatCodes(this.cheatcodes, {
46
+ rollupAddress,
47
+ });
48
+
49
+ this.logger.debug(`Watcher created for rollup at ${rollupAddress}`);
50
+ }
51
+
52
+ setIsMarkingAsProven(isMarkingAsProven: boolean) {
53
+ this.isMarkingAsProven = isMarkingAsProven;
54
+ }
55
+
56
+ setIsSandbox(isSandbox: boolean) {
57
+ this.isSandbox = isSandbox;
58
+ }
59
+
60
+ async start() {
61
+ if (this.filledRunningPromise) {
62
+ throw new Error('Watcher already watching for filled slot');
63
+ }
64
+
65
+ const config = await this.rollupCheatCodes.getConfig();
66
+ this.l2SlotDuration = config.slotDuration;
67
+
68
+ // If auto mining is not supported (e.g., we are on a real network), then we
69
+ // will simple do nothing. But if on an anvil or the like, this make sure that
70
+ // the sandbox and tests don't break because time is frozen and we never get to
71
+ // the next slot.
72
+ const isAutoMining = await this.cheatcodes.isAutoMining();
73
+
74
+ if (isAutoMining) {
75
+ this.filledRunningPromise = new RunningPromise(() => this.warpTimeIfNeeded(), this.logger, 200);
76
+ this.filledRunningPromise.start();
77
+ this.syncDateProviderPromise = new RunningPromise(() => this.syncDateProviderToL1IfBehind(), this.logger, 200);
78
+ this.syncDateProviderPromise.start();
79
+ this.markingAsProvenRunningPromise = new RunningPromise(() => this.markAsProven(), this.logger, 200);
80
+ this.markingAsProvenRunningPromise.start();
81
+ this.logger.info(`Watcher started for rollup at ${this.rollup.address}`);
82
+ } else {
83
+ this.logger.info(`Watcher not started because not auto mining`);
84
+ }
85
+ }
86
+
87
+ async stop() {
88
+ await this.filledRunningPromise?.stop();
89
+ await this.syncDateProviderPromise?.stop();
90
+ await this.markingAsProvenRunningPromise?.stop();
91
+ }
92
+
93
+ async trigger() {
94
+ await this.filledRunningPromise?.trigger();
95
+ await this.syncDateProviderPromise?.trigger();
96
+ await this.markingAsProvenRunningPromise?.trigger();
97
+ }
98
+
99
+ async markAsProven() {
100
+ if (!this.isMarkingAsProven) {
101
+ return;
102
+ }
103
+ await this.rollupCheatCodes.markAsProven();
104
+ }
105
+
106
+ async syncDateProviderToL1IfBehind() {
107
+ // this doesn't apply to the sandbox, because we don't have a date provider in the sandbox
108
+ if (!this.dateProvider) {
109
+ return;
110
+ }
111
+
112
+ const l1Time = (await this.cheatcodes.timestamp()) * 1000;
113
+ const wallTime = this.dateProvider.now();
114
+ if (l1Time > wallTime) {
115
+ this.logger.warn(`L1 is ahead of wall time. Syncing wall time to L1 time`);
116
+ this.dateProvider.setTime(l1Time);
117
+ } else if (l1Time + Number(this.l2SlotDuration) * 1000 < wallTime) {
118
+ this.logger.warn(`L1 is more than 1 L2 slot behind wall time. Warping to wall time`);
119
+ await this.cheatcodes.warp(Math.ceil(wallTime / 1000));
120
+ }
121
+ }
122
+
123
+ async warpTimeIfNeeded() {
124
+ try {
125
+ const currentSlot = await this.rollup.read.getCurrentSlot();
126
+ const pendingBlockNumber = BigInt(await this.rollup.read.getPendingBlockNumber());
127
+ const blockLog = await this.rollup.read.getBlock([pendingBlockNumber]);
128
+ const nextSlotTimestamp = Number(await this.rollup.read.getTimestampForSlot([currentSlot + 1n]));
129
+
130
+ if (currentSlot === blockLog.slotNumber) {
131
+ // We should jump to the next slot
132
+ try {
133
+ await this.cheatcodes.warp(nextSlotTimestamp, {
134
+ resetBlockInterval: true,
135
+ updateDateProvider: this.dateProvider,
136
+ });
137
+ } catch (e) {
138
+ this.logger.error(`Failed to warp to timestamp ${nextSlotTimestamp}: ${e}`);
139
+ }
140
+
141
+ this.logger.info(`Slot ${currentSlot} was filled, jumped to next slot`);
142
+ return;
143
+ }
144
+
145
+ // If we are not in sandbox, we don't need to warp time
146
+ if (!this.isSandbox) {
147
+ return;
148
+ }
149
+
150
+ const currentTimestamp = this.dateProvider?.now() ?? Date.now();
151
+ if (currentTimestamp > nextSlotTimestamp * 1000) {
152
+ try {
153
+ await this.cheatcodes.warp(nextSlotTimestamp, {
154
+ resetBlockInterval: true,
155
+ updateDateProvider: this.dateProvider,
156
+ });
157
+ } catch (e) {
158
+ this.logger.error(`Failed to warp to timestamp ${nextSlotTimestamp}: ${e}`);
159
+ }
160
+
161
+ this.logger.info(`Slot ${currentSlot} was missed, jumped to next slot`);
162
+ }
163
+ } catch {
164
+ this.logger.error('mineIfSlotFilled failed');
165
+ }
166
+ }
167
+ }
@@ -0,0 +1,77 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
+ import { deriveStorageSlotInMap } from '@aztec/stdlib/hash';
5
+ import type { PXE } from '@aztec/stdlib/interfaces/client';
6
+ import type { Note } from '@aztec/stdlib/note';
7
+
8
+ /**
9
+ * A class that provides utility functions for interacting with the aztec chain.
10
+ */
11
+ export class AztecCheatCodes {
12
+ constructor(
13
+ /**
14
+ * The PXE Service to use for interacting with the chain
15
+ */
16
+ public pxe: PXE,
17
+ /**
18
+ * The logger to use for the aztec cheatcodes
19
+ */
20
+ public logger = createLogger('aztecjs:cheat_codes'),
21
+ ) {}
22
+
23
+ /**
24
+ * Computes the slot value for a given map and key.
25
+ * @param mapSlot - The slot of the map (specified in Aztec.nr contract)
26
+ * @param key - The key to lookup in the map
27
+ * @returns The storage slot of the value in the map
28
+ */
29
+ public computeSlotInMap(mapSlot: Fr | bigint, key: Fr | bigint | AztecAddress): Promise<Fr> {
30
+ const keyFr = typeof key === 'bigint' ? new Fr(key) : key.toField();
31
+ return deriveStorageSlotInMap(mapSlot, keyFr);
32
+ }
33
+
34
+ /**
35
+ * Get the current blocknumber
36
+ * @returns The current block number
37
+ */
38
+ public async blockNumber(): Promise<number> {
39
+ return await this.pxe.getBlockNumber();
40
+ }
41
+
42
+ /**
43
+ * Get the current timestamp
44
+ * @returns The current timestamp
45
+ */
46
+ public async timestamp(): Promise<number> {
47
+ const res = await this.pxe.getBlock(await this.blockNumber());
48
+ return Number(res?.header.globalVariables.timestamp ?? 0);
49
+ }
50
+
51
+ /**
52
+ * Loads the value stored at the given slot in the public storage of the given contract.
53
+ * @param who - The address of the contract
54
+ * @param slot - The storage slot to lookup
55
+ * @returns The value stored at the given slot
56
+ */
57
+ public async loadPublic(who: AztecAddress, slot: Fr | bigint): Promise<Fr> {
58
+ const storageValue = await this.pxe.getPublicStorageAt(who, new Fr(slot));
59
+ return storageValue;
60
+ }
61
+
62
+ /**
63
+ * Loads the value stored at the given slot in the private storage of the given contract.
64
+ * @param contract - The address of the contract
65
+ * @param recipient - The address whose public key was used to encrypt the note
66
+ * @param slot - The storage slot to lookup
67
+ * @returns The notes stored at the given slot
68
+ */
69
+ public async loadPrivate(recipient: AztecAddress, contract: AztecAddress, slot: Fr | bigint): Promise<Note[]> {
70
+ const extendedNotes = await this.pxe.getNotes({
71
+ recipient,
72
+ contractAddress: contract,
73
+ storageSlot: new Fr(slot),
74
+ });
75
+ return extendedNotes.map(extendedNote => extendedNote.note);
76
+ }
77
+ }
@@ -0,0 +1,79 @@
1
+ import { retryUntil } from '@aztec/aztec.js';
2
+ import { EthCheatCodes, RollupCheatCodes } from '@aztec/ethereum/test';
3
+ import type { SequencerClient } from '@aztec/sequencer-client';
4
+ import type { AztecNode, PXE } from '@aztec/stdlib/interfaces/client';
5
+
6
+ import { AztecCheatCodes } from './aztec_cheat_codes.js';
7
+
8
+ /**
9
+ * A class that provides utility functions for interacting with the chain.
10
+ */
11
+ export class CheatCodes {
12
+ constructor(
13
+ /** Cheat codes for L1.*/
14
+ public eth: EthCheatCodes,
15
+ /** Cheat codes for Aztec L2. */
16
+ public aztec: AztecCheatCodes,
17
+ /** Cheat codes for the Aztec Rollup contract on L1. */
18
+ public rollup: RollupCheatCodes,
19
+ ) {}
20
+
21
+ static async create(rpcUrls: string[], pxe: PXE): Promise<CheatCodes> {
22
+ const ethCheatCodes = new EthCheatCodes(rpcUrls);
23
+ const aztecCheatCodes = new AztecCheatCodes(pxe);
24
+ const rollupCheatCodes = new RollupCheatCodes(
25
+ ethCheatCodes,
26
+ await pxe.getNodeInfo().then(n => n.l1ContractAddresses),
27
+ );
28
+ return new CheatCodes(ethCheatCodes, aztecCheatCodes, rollupCheatCodes);
29
+ }
30
+
31
+ /**
32
+ * Warps the L1 timestamp to a target timestamp and mines an L2 block that advances the L2 timestamp to at least
33
+ * the target timestamp. L2 timestamp is not advanced exactly to the target timestamp because it is determined
34
+ * by the slot number, which advances in fixed intervals.
35
+ * This is useful for testing time-dependent contract behavior.
36
+ * @param sequencerClient - The sequencer client to use to force an empty block to be mined.
37
+ * @param node - The Aztec node used to query if a new block has been mined.
38
+ * @param targetTimestamp - The target timestamp to warp to (in seconds)
39
+ */
40
+ async warpL2TimeAtLeastTo(sequencerClient: SequencerClient, node: AztecNode, targetTimestamp: bigint | number) {
41
+ const currentL2BlockNumber = await node.getBlockNumber();
42
+
43
+ // We warp the L1 timestamp
44
+ await this.eth.warp(targetTimestamp, { resetBlockInterval: true });
45
+
46
+ // Wait until an L2 block is mined
47
+ const sequencer = sequencerClient.getSequencer();
48
+ const minTxsPerBlock = sequencer.getConfig().minTxsPerBlock;
49
+ sequencer.updateConfig({ minTxsPerBlock: 0 });
50
+
51
+ await retryUntil(
52
+ async () => {
53
+ const newL2BlockNumber = await node.getBlockNumber();
54
+ return newL2BlockNumber > currentL2BlockNumber;
55
+ },
56
+ 'new block after warping L2 time',
57
+ 36,
58
+ 1,
59
+ );
60
+
61
+ // Restore original minTxsPerBlock
62
+ sequencer.updateConfig({ minTxsPerBlock });
63
+ }
64
+
65
+ /**
66
+ * Warps the L1 timestamp forward by a specified duration and mines an L2 block that advances the L2 timestamp at
67
+ * least by the duration. L2 timestamp is not advanced exactly by the duration because it is determined by the slot
68
+ * number, which advances in fixed intervals.
69
+ * This is useful for testing time-dependent contract behavior.
70
+ * @param sequencerClient - The sequencer client to use to force an empty block to be mined.
71
+ * @param node - The Aztec node used to query if a new block has been mined.
72
+ * @param duration - The duration to advance time by (in seconds)
73
+ */
74
+ async warpL2TimeAtLeastBy(sequencerClient: SequencerClient, node: AztecNode, duration: bigint | number) {
75
+ const currentTimestamp = await this.eth.timestamp();
76
+ const targetTimestamp = BigInt(currentTimestamp) + BigInt(duration);
77
+ await this.warpL2TimeAtLeastTo(sequencerClient, node, targetTimestamp);
78
+ }
79
+ }
@@ -0,0 +1,4 @@
1
+ export { AnvilTestWatcher } from './anvil_test_watcher.js';
2
+ export { EthCheatCodes, RollupCheatCodes } from '@aztec/ethereum/test';
3
+ export { AztecCheatCodes } from './aztec_cheat_codes.js';
4
+ export { CheatCodes } from './cheat_codes.js';