@aztec/ethereum 0.0.0-test.1 → 0.0.1-commit.b655e406

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 (229) hide show
  1. package/dest/account.d.ts +2 -0
  2. package/dest/account.d.ts.map +1 -0
  3. package/dest/account.js +4 -0
  4. package/dest/client.d.ts +5 -3
  5. package/dest/client.d.ts.map +1 -1
  6. package/dest/client.js +16 -2
  7. package/dest/config.d.ts +107 -16
  8. package/dest/config.d.ts.map +1 -1
  9. package/dest/config.js +456 -22
  10. package/dest/contracts/empire_base.d.ts +21 -6
  11. package/dest/contracts/empire_base.d.ts.map +1 -1
  12. package/dest/contracts/empire_base.js +75 -2
  13. package/dest/contracts/empire_slashing_proposer.d.ts +65 -0
  14. package/dest/contracts/empire_slashing_proposer.d.ts.map +1 -0
  15. package/dest/contracts/empire_slashing_proposer.js +194 -0
  16. package/dest/contracts/errors.d.ts +7 -0
  17. package/dest/contracts/errors.d.ts.map +1 -0
  18. package/dest/contracts/errors.js +12 -0
  19. package/dest/contracts/fee_asset_handler.d.ts +19 -0
  20. package/dest/contracts/fee_asset_handler.d.ts.map +1 -0
  21. package/dest/contracts/fee_asset_handler.js +57 -0
  22. package/dest/contracts/fee_juice.d.ts +5 -6
  23. package/dest/contracts/fee_juice.d.ts.map +1 -1
  24. package/dest/contracts/fee_juice.js +27 -20
  25. package/dest/contracts/governance.d.ts +36 -25
  26. package/dest/contracts/governance.d.ts.map +1 -1
  27. package/dest/contracts/governance.js +87 -84
  28. package/dest/contracts/governance_proposer.d.ts +13 -11
  29. package/dest/contracts/governance_proposer.d.ts.map +1 -1
  30. package/dest/contracts/governance_proposer.js +32 -18
  31. package/dest/contracts/gse.d.ts +32 -0
  32. package/dest/contracts/gse.d.ts.map +1 -0
  33. package/dest/contracts/gse.js +72 -0
  34. package/dest/contracts/inbox.d.ts +26 -0
  35. package/dest/contracts/inbox.d.ts.map +1 -0
  36. package/dest/contracts/inbox.js +45 -0
  37. package/dest/contracts/index.d.ts +8 -2
  38. package/dest/contracts/index.d.ts.map +1 -1
  39. package/dest/contracts/index.js +8 -2
  40. package/dest/contracts/multicall.d.ts +21 -0
  41. package/dest/contracts/multicall.d.ts.map +1 -0
  42. package/dest/contracts/multicall.js +156 -0
  43. package/dest/contracts/registry.d.ts +9 -4
  44. package/dest/contracts/registry.d.ts.map +1 -1
  45. package/dest/contracts/registry.js +44 -16
  46. package/dest/contracts/rollup.d.ts +202 -29
  47. package/dest/contracts/rollup.d.ts.map +1 -1
  48. package/dest/contracts/rollup.js +500 -55
  49. package/dest/contracts/slasher_contract.d.ts +44 -0
  50. package/dest/contracts/slasher_contract.d.ts.map +1 -0
  51. package/dest/contracts/slasher_contract.js +75 -0
  52. package/dest/contracts/tally_slashing_proposer.d.ts +138 -0
  53. package/dest/contracts/tally_slashing_proposer.d.ts.map +1 -0
  54. package/dest/contracts/tally_slashing_proposer.js +313 -0
  55. package/dest/contracts/utils.d.ts +3 -0
  56. package/dest/contracts/utils.d.ts.map +1 -0
  57. package/dest/contracts/utils.js +11 -0
  58. package/dest/deploy_l1_contracts.d.ts +128 -21112
  59. package/dest/deploy_l1_contracts.d.ts.map +1 -1
  60. package/dest/deploy_l1_contracts.js +1204 -418
  61. package/dest/eth-signer/eth-signer.d.ts +21 -0
  62. package/dest/eth-signer/eth-signer.d.ts.map +1 -0
  63. package/dest/eth-signer/eth-signer.js +5 -0
  64. package/dest/eth-signer/index.d.ts +2 -0
  65. package/dest/eth-signer/index.d.ts.map +1 -0
  66. package/dest/eth-signer/index.js +1 -0
  67. package/dest/index.d.ts +6 -2
  68. package/dest/index.d.ts.map +1 -1
  69. package/dest/index.js +6 -2
  70. package/dest/l1_artifacts.d.ts +76184 -0
  71. package/dest/l1_artifacts.d.ts.map +1 -0
  72. package/dest/l1_artifacts.js +166 -0
  73. package/dest/l1_contract_addresses.d.ts +21 -1
  74. package/dest/l1_contract_addresses.d.ts.map +1 -1
  75. package/dest/l1_contract_addresses.js +22 -18
  76. package/dest/l1_reader.d.ts +1 -1
  77. package/dest/l1_reader.d.ts.map +1 -1
  78. package/dest/l1_reader.js +8 -8
  79. package/dest/l1_tx_utils/config.d.ts +59 -0
  80. package/dest/l1_tx_utils/config.d.ts.map +1 -0
  81. package/dest/l1_tx_utils/config.js +73 -0
  82. package/dest/l1_tx_utils/constants.d.ts +6 -0
  83. package/dest/l1_tx_utils/constants.d.ts.map +1 -0
  84. package/dest/l1_tx_utils/constants.js +14 -0
  85. package/dest/l1_tx_utils/factory.d.ts +24 -0
  86. package/dest/l1_tx_utils/factory.d.ts.map +1 -0
  87. package/dest/l1_tx_utils/factory.js +12 -0
  88. package/dest/l1_tx_utils/index.d.ts +10 -0
  89. package/dest/l1_tx_utils/index.d.ts.map +1 -0
  90. package/dest/l1_tx_utils/index.js +10 -0
  91. package/dest/l1_tx_utils/interfaces.d.ts +76 -0
  92. package/dest/l1_tx_utils/interfaces.d.ts.map +1 -0
  93. package/dest/l1_tx_utils/interfaces.js +4 -0
  94. package/dest/l1_tx_utils/l1_tx_utils.d.ts +95 -0
  95. package/dest/l1_tx_utils/l1_tx_utils.d.ts.map +1 -0
  96. package/dest/l1_tx_utils/l1_tx_utils.js +610 -0
  97. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts +26 -0
  98. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts.map +1 -0
  99. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.js +26 -0
  100. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +94 -0
  101. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -0
  102. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +431 -0
  103. package/dest/l1_tx_utils/signer.d.ts +4 -0
  104. package/dest/l1_tx_utils/signer.d.ts.map +1 -0
  105. package/dest/l1_tx_utils/signer.js +16 -0
  106. package/dest/l1_tx_utils/types.d.ts +67 -0
  107. package/dest/l1_tx_utils/types.d.ts.map +1 -0
  108. package/dest/l1_tx_utils/types.js +26 -0
  109. package/dest/l1_tx_utils/utils.d.ts +4 -0
  110. package/dest/l1_tx_utils/utils.d.ts.map +1 -0
  111. package/dest/l1_tx_utils/utils.js +14 -0
  112. package/dest/l1_types.d.ts +6 -0
  113. package/dest/l1_types.d.ts.map +1 -0
  114. package/dest/l1_types.js +1 -0
  115. package/dest/publisher_manager.d.ts +15 -0
  116. package/dest/publisher_manager.d.ts.map +1 -0
  117. package/dest/publisher_manager.js +88 -0
  118. package/dest/queries.d.ts +3 -1
  119. package/dest/queries.d.ts.map +1 -1
  120. package/dest/queries.js +51 -12
  121. package/dest/test/chain_monitor.d.ts +72 -0
  122. package/dest/test/chain_monitor.d.ts.map +1 -0
  123. package/dest/test/chain_monitor.js +216 -0
  124. package/dest/test/delayed_tx_utils.d.ts +7 -2
  125. package/dest/test/delayed_tx_utils.d.ts.map +1 -1
  126. package/dest/test/delayed_tx_utils.js +13 -6
  127. package/dest/{eth_cheat_codes.d.ts → test/eth_cheat_codes.d.ts} +87 -13
  128. package/dest/test/eth_cheat_codes.d.ts.map +1 -0
  129. package/dest/test/eth_cheat_codes.js +552 -0
  130. package/dest/test/eth_cheat_codes_with_state.d.ts +1 -1
  131. package/dest/test/eth_cheat_codes_with_state.d.ts.map +1 -1
  132. package/dest/test/eth_cheat_codes_with_state.js +1 -1
  133. package/dest/test/index.d.ts +3 -0
  134. package/dest/test/index.d.ts.map +1 -1
  135. package/dest/test/index.js +3 -0
  136. package/dest/test/rollup_cheat_codes.d.ts +86 -0
  137. package/dest/test/rollup_cheat_codes.d.ts.map +1 -0
  138. package/dest/test/rollup_cheat_codes.js +268 -0
  139. package/dest/test/start_anvil.d.ts +5 -0
  140. package/dest/test/start_anvil.d.ts.map +1 -1
  141. package/dest/test/start_anvil.js +15 -7
  142. package/dest/test/tx_delayer.d.ts +17 -6
  143. package/dest/test/tx_delayer.d.ts.map +1 -1
  144. package/dest/test/tx_delayer.js +95 -19
  145. package/dest/test/upgrade_utils.d.ts +5 -4
  146. package/dest/test/upgrade_utils.d.ts.map +1 -1
  147. package/dest/test/upgrade_utils.js +23 -16
  148. package/dest/types.d.ts +6 -7
  149. package/dest/types.d.ts.map +1 -1
  150. package/dest/types.js +3 -1
  151. package/dest/utils.d.ts +1 -0
  152. package/dest/utils.d.ts.map +1 -1
  153. package/dest/utils.js +43 -88
  154. package/dest/zkPassportVerifierAddress.d.ts +15 -0
  155. package/dest/zkPassportVerifierAddress.d.ts.map +1 -0
  156. package/dest/zkPassportVerifierAddress.js +11 -0
  157. package/package.json +24 -16
  158. package/src/account.ts +5 -0
  159. package/src/client.ts +42 -4
  160. package/src/config.ts +584 -31
  161. package/src/contracts/empire_base.ts +75 -6
  162. package/src/contracts/empire_slashing_proposer.ts +259 -0
  163. package/src/contracts/errors.ts +13 -0
  164. package/src/contracts/fee_asset_handler.ts +63 -0
  165. package/src/contracts/fee_juice.ts +29 -15
  166. package/src/contracts/governance.ts +80 -77
  167. package/src/contracts/governance_proposer.ts +60 -24
  168. package/src/contracts/gse.ts +88 -0
  169. package/src/contracts/inbox.ts +63 -0
  170. package/src/contracts/index.ts +8 -2
  171. package/src/contracts/multicall.ts +155 -0
  172. package/src/contracts/registry.ts +51 -26
  173. package/src/contracts/rollup.ts +585 -56
  174. package/src/contracts/slasher_contract.ts +89 -0
  175. package/src/contracts/tally_slashing_proposer.ts +315 -0
  176. package/src/contracts/utils.ts +14 -0
  177. package/src/deploy_l1_contracts.ts +1467 -566
  178. package/src/eth-signer/eth-signer.ts +25 -0
  179. package/src/eth-signer/index.ts +1 -0
  180. package/src/index.ts +6 -2
  181. package/src/l1_artifacts.ts +254 -0
  182. package/src/l1_contract_addresses.ts +32 -19
  183. package/src/l1_reader.ts +9 -9
  184. package/src/l1_tx_utils/README.md +177 -0
  185. package/src/l1_tx_utils/config.ts +140 -0
  186. package/src/l1_tx_utils/constants.ts +18 -0
  187. package/src/l1_tx_utils/factory.ts +64 -0
  188. package/src/l1_tx_utils/index.ts +12 -0
  189. package/src/l1_tx_utils/interfaces.ts +86 -0
  190. package/src/l1_tx_utils/l1_tx_utils.ts +718 -0
  191. package/src/l1_tx_utils/l1_tx_utils_with_blobs.ts +77 -0
  192. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +559 -0
  193. package/src/l1_tx_utils/signer.ts +28 -0
  194. package/src/l1_tx_utils/types.ts +85 -0
  195. package/src/l1_tx_utils/utils.ts +16 -0
  196. package/src/l1_types.ts +6 -0
  197. package/src/publisher_manager.ts +106 -0
  198. package/src/queries.ts +70 -15
  199. package/src/test/chain_monitor.ts +243 -0
  200. package/src/test/delayed_tx_utils.ts +34 -6
  201. package/src/test/eth_cheat_codes.ts +582 -0
  202. package/src/test/eth_cheat_codes_with_state.ts +1 -1
  203. package/src/test/index.ts +3 -0
  204. package/src/test/rollup_cheat_codes.ts +310 -0
  205. package/src/test/start_anvil.ts +20 -5
  206. package/src/test/tx_delayer.ts +127 -26
  207. package/src/test/upgrade_utils.ts +30 -21
  208. package/src/types.ts +10 -8
  209. package/src/utils.ts +49 -90
  210. package/src/zkPassportVerifierAddress.ts +15 -0
  211. package/dest/contracts/forwarder.d.ts +0 -24
  212. package/dest/contracts/forwarder.d.ts.map +0 -1
  213. package/dest/contracts/forwarder.js +0 -101
  214. package/dest/contracts/slashing_proposer.d.ts +0 -21
  215. package/dest/contracts/slashing_proposer.d.ts.map +0 -1
  216. package/dest/contracts/slashing_proposer.js +0 -47
  217. package/dest/eth_cheat_codes.d.ts.map +0 -1
  218. package/dest/eth_cheat_codes.js +0 -303
  219. package/dest/l1_tx_utils.d.ts +0 -192
  220. package/dest/l1_tx_utils.d.ts.map +0 -1
  221. package/dest/l1_tx_utils.js +0 -641
  222. package/dest/l1_tx_utils_with_blobs.d.ts +0 -12
  223. package/dest/l1_tx_utils_with_blobs.d.ts.map +0 -1
  224. package/dest/l1_tx_utils_with_blobs.js +0 -64
  225. package/src/contracts/forwarder.ts +0 -132
  226. package/src/contracts/slashing_proposer.ts +0 -51
  227. package/src/eth_cheat_codes.ts +0 -314
  228. package/src/l1_tx_utils.ts +0 -847
  229. package/src/l1_tx_utils_with_blobs.ts +0 -86
@@ -0,0 +1,582 @@
1
+ import { toBigIntBE, toHex } from '@aztec/foundation/bigint-buffer';
2
+ import { keccak256 } from '@aztec/foundation/crypto';
3
+ import { EthAddress } from '@aztec/foundation/eth-address';
4
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
5
+ import { createLogger } from '@aztec/foundation/log';
6
+ import { pluralize } from '@aztec/foundation/string';
7
+ import type { DateProvider, TestDateProvider } from '@aztec/foundation/timer';
8
+
9
+ import { type Hex, type Transaction, createPublicClient, fallback, hexToNumber, http } from 'viem';
10
+
11
+ import type { ViemPublicClient } from '../types.js';
12
+
13
+ /**
14
+ * A class that provides utility functions for interacting with ethereum (L1).
15
+ */
16
+ export class EthCheatCodes {
17
+ public readonly publicClient: ViemPublicClient;
18
+ constructor(
19
+ /**
20
+ * The RPC URL to use for interacting with the chain
21
+ */
22
+ public rpcUrls: string[],
23
+ /**
24
+ * The date provider to use for time operations
25
+ */
26
+ public dateProvider: DateProvider | TestDateProvider,
27
+ /**
28
+ * The logger to use for the eth cheatcodes
29
+ */
30
+ public logger = createLogger('ethereum:cheat_codes'),
31
+ ) {
32
+ this.publicClient = createPublicClient({
33
+ transport: fallback(this.rpcUrls.map(url => http(url))),
34
+ });
35
+ }
36
+
37
+ public rpcCall(method: string, params: any[]) {
38
+ this.logger.debug(`Calling ${method} with params: ${jsonStringify(params)} on ${this.rpcUrls.join(', ')}`);
39
+ return this.doRpcCall(method, params);
40
+ }
41
+
42
+ private async doRpcCall(method: string, params: any[]) {
43
+ return (await this.publicClient.transport.request({
44
+ method,
45
+ params,
46
+ })) as any;
47
+ }
48
+
49
+ /**
50
+ * Get the auto mine status of the underlying chain
51
+ * @returns True if automine is on, false otherwise
52
+ */
53
+ public async isAutoMining(): Promise<boolean> {
54
+ try {
55
+ const res = await this.doRpcCall('anvil_getAutomine', []);
56
+ return res;
57
+ } catch (err) {
58
+ this.logger.error(`Calling "anvil_getAutomine" failed with:`, err);
59
+ }
60
+ return false;
61
+ }
62
+
63
+ /**
64
+ * Get the current blocknumber
65
+ * @returns The current block number
66
+ */
67
+ public async blockNumber(): Promise<number> {
68
+ const res = await this.doRpcCall('eth_blockNumber', []);
69
+ return parseInt(res, 16);
70
+ }
71
+
72
+ /**
73
+ * Get the current chainId
74
+ * @returns The current chainId
75
+ */
76
+ public async chainId(): Promise<number> {
77
+ const res = await this.doRpcCall('eth_chainId', []);
78
+ return parseInt(res, 16);
79
+ }
80
+
81
+ /**
82
+ * Get the current timestamp
83
+ * @returns The current timestamp
84
+ */
85
+ public async timestamp(): Promise<number> {
86
+ const res = await this.doRpcCall('eth_getBlockByNumber', ['latest', true]);
87
+ return parseInt(res.timestamp, 16);
88
+ }
89
+
90
+ /**
91
+ * Advance the chain by a number of blocks
92
+ * @param numberOfBlocks - The number of blocks to mine
93
+ */
94
+ public async mine(numberOfBlocks: number | bigint = 1): Promise<void> {
95
+ await this.doMine(Number(numberOfBlocks));
96
+ this.logger.warn(`Mined ${numberOfBlocks} L1 blocks`);
97
+ }
98
+
99
+ private async doMine(numberOfBlocks = 1): Promise<void> {
100
+ try {
101
+ await this.doRpcCall('hardhat_mine', [numberOfBlocks]);
102
+ } catch (err) {
103
+ throw new Error(`Error mining: ${err}`);
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Mines a single block with evm_mine
109
+ */
110
+ public async evmMine(): Promise<void> {
111
+ try {
112
+ await this.doRpcCall('evm_mine', []);
113
+ this.logger.warn(`Mined 1 L1 block with evm_mine`);
114
+ } catch (err) {
115
+ throw new Error(`Error mining: ${err}`);
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Set the balance of an account
121
+ * @param account - The account to set the balance for
122
+ * @param balance - The balance to set
123
+ */
124
+ public async setBalance(account: EthAddress | Hex, balance: bigint): Promise<void> {
125
+ try {
126
+ await this.rpcCall('anvil_setBalance', [account.toString(), toHex(balance)]);
127
+ } catch (err) {
128
+ throw new Error(`Error setting balance for ${account}: ${err}`);
129
+ }
130
+ this.logger.warn(`Set balance for ${account} to ${balance}`);
131
+ }
132
+
133
+ public async getBalance(account: EthAddress | Hex): Promise<bigint> {
134
+ const res = await this.doRpcCall('eth_getBalance', [account.toString(), 'latest']);
135
+ return BigInt(res);
136
+ }
137
+
138
+ /**
139
+ * Set the interval between successive blocks (block time). This does NOT enable interval mining.
140
+ * @param interval - The interval to use between blocks
141
+ */
142
+ public async setBlockInterval(interval: number): Promise<void> {
143
+ try {
144
+ await this.rpcCall('anvil_setBlockTimestampInterval', [interval]);
145
+ } catch (err) {
146
+ throw new Error(`Error setting block interval: ${err}`);
147
+ }
148
+ this.logger.warn(`Set L1 block interval to ${interval}`);
149
+ }
150
+
151
+ /**
152
+ * Set the next block base fee per gas
153
+ * @param baseFee - The base fee to set
154
+ */
155
+ public async setNextBlockBaseFeePerGas(baseFee: bigint | number): Promise<void> {
156
+ try {
157
+ await this.rpcCall('anvil_setNextBlockBaseFeePerGas', [baseFee.toString()]);
158
+ } catch (err) {
159
+ throw new Error(`Error setting next block base fee per gas: ${err}`);
160
+ }
161
+ this.logger.warn(`Set L1 next block base fee per gas to ${baseFee}`);
162
+ }
163
+
164
+ /**
165
+ * Get interval mining if set.
166
+ * @param seconds - The interval to use between blocks
167
+ */
168
+ public getIntervalMining(): Promise<number | null> {
169
+ try {
170
+ return this.doRpcCall('anvil_getIntervalMining', []);
171
+ } catch (err) {
172
+ throw new Error(`Error getting interval mining: ${err}`);
173
+ }
174
+ }
175
+
176
+ /**
177
+ * Enable interval mining at the given interval (block time)
178
+ * @param seconds - The interval to use between blocks
179
+ */
180
+ public async setIntervalMining(seconds: number, opts: { silent?: boolean } = {}): Promise<void> {
181
+ try {
182
+ await this.rpcCall('anvil_setIntervalMining', [seconds]);
183
+ } catch (err) {
184
+ throw new Error(`Error setting interval mining: ${err}`);
185
+ }
186
+ if (!opts.silent) {
187
+ this.logger.warn(`Set L1 interval mining to ${seconds} seconds`);
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Set the automine status of the underlying anvil chain
193
+ * @param automine - The automine status to set
194
+ */
195
+ public async setAutomine(automine: boolean, opts: { silent?: boolean } = {}): Promise<void> {
196
+ try {
197
+ await this.rpcCall('anvil_setAutomine', [automine]);
198
+ } catch (err) {
199
+ throw new Error(`Error setting automine: ${err}`);
200
+ }
201
+ if (!opts.silent) {
202
+ this.logger.warn(`Set L1 automine to ${automine}`);
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Drop a transaction from the mempool
208
+ * @param txHash - The transaction hash
209
+ */
210
+ public async dropTransaction(txHash: Hex): Promise<void> {
211
+ try {
212
+ await this.rpcCall('anvil_dropTransaction', [txHash]);
213
+ } catch (err) {
214
+ throw new Error(`Error dropping transaction: ${err}`);
215
+ }
216
+ this.logger.warn(`Dropped transaction ${txHash}`);
217
+ }
218
+
219
+ /**
220
+ * Set the next block timestamp
221
+ * @param timestamp - The timestamp to set the next block to
222
+ */
223
+ public async setNextBlockTimestamp(timestamp: number | Date): Promise<void> {
224
+ try {
225
+ await this.rpcCall('evm_setNextBlockTimestamp', [
226
+ timestamp instanceof Date ? Math.floor(timestamp.getTime() / 1000) : timestamp,
227
+ ]);
228
+ } catch (err: any) {
229
+ throw new Error(`Error setting next block timestamp: ${err.message}`);
230
+ }
231
+ this.logger.warn(`Set L1 next block timestamp to ${timestamp}`);
232
+ }
233
+
234
+ /**
235
+ * Set the next block timestamp and mines the block.
236
+ * Optionally resets interval mining so the next block is mined in `blockInterval` seconds from now.
237
+ * Always updates the injected date provider to follow L1 time.
238
+ * @param timestamp - The timestamp to set the next block to
239
+ */
240
+ public async warp(
241
+ timestamp: number | bigint,
242
+ opts: { silent?: boolean; resetBlockInterval?: boolean } = {},
243
+ ): Promise<void> {
244
+ let blockInterval: number | null = null;
245
+ try {
246
+ // Load current block interval and disable it
247
+ if (opts.resetBlockInterval) {
248
+ blockInterval = await this.getIntervalMining();
249
+ if (blockInterval !== null) {
250
+ await this.setIntervalMining(0, { silent: true });
251
+ }
252
+ }
253
+ // Set the timestamp of the next block to be mined
254
+ await this.rpcCall('evm_setNextBlockTimestamp', [Number(timestamp)]);
255
+ // And mine a block so the timestamp goes into effect now
256
+ await this.doMine();
257
+ // Update the injected date provider so it follows L1 time
258
+ if ('setTime' in this.dateProvider) {
259
+ this.dateProvider.setTime(Number(timestamp) * 1000);
260
+ }
261
+ } catch (err) {
262
+ throw new Error(`Error warping: ${err}`);
263
+ } finally {
264
+ // Restore interval mining so the next block is mined in `blockInterval` seconds from this one
265
+ if (opts.resetBlockInterval && blockInterval !== null && blockInterval > 0) {
266
+ await this.setIntervalMining(blockInterval, { silent: true });
267
+ }
268
+ }
269
+ if (!opts.silent) {
270
+ this.logger.warn(`Warped L1 timestamp to ${timestamp}`);
271
+ }
272
+ }
273
+
274
+ /**
275
+ * Load the value at a storage slot of a contract address on eth
276
+ * @param contract - The contract address
277
+ * @param slot - The storage slot
278
+ * @returns - The value at the storage slot
279
+ */
280
+ public async load(contract: EthAddress, slot: bigint): Promise<bigint> {
281
+ const res = await this.rpcCall('eth_getStorageAt', [contract.toString(), toHex(slot), 'latest']);
282
+ return BigInt(res);
283
+ }
284
+
285
+ /**
286
+ * Set the value at a storage slot of a contract address on eth
287
+ * @param contract - The contract address
288
+ * @param slot - The storage slot
289
+ * @param value - The value to set the storage slot to
290
+ */
291
+ public async store(
292
+ contract: EthAddress,
293
+ slot: bigint,
294
+ value: bigint,
295
+ opts: { silent?: boolean } = {},
296
+ ): Promise<void> {
297
+ // for the rpc call, we need to change value to be a 32 byte hex string.
298
+ try {
299
+ await this.rpcCall('hardhat_setStorageAt', [contract.toString(), toHex(slot), toHex(value, true)]);
300
+ } catch (err) {
301
+ throw new Error(`Error setting storage for contract ${contract} at ${slot}: ${err}`);
302
+ }
303
+ if (!opts.silent) {
304
+ this.logger.warn(`Set L1 storage for contract ${contract} at ${slot} to ${value}`);
305
+ }
306
+ }
307
+
308
+ /**
309
+ * Computes the slot value for a given map and key.
310
+ * @param baseSlot - The base slot of the map (specified in Aztec.nr contract)
311
+ * @param key - The key to lookup in the map
312
+ * @returns The storage slot of the value in the map
313
+ */
314
+ public keccak256(baseSlot: bigint, key: bigint): bigint {
315
+ // abi encode (removing the 0x) - concat key and baseSlot (both padded to 32 bytes)
316
+ const abiEncoded = toHex(key, true).substring(2) + toHex(baseSlot, true).substring(2);
317
+ return toBigIntBE(keccak256(Buffer.from(abiEncoded, 'hex')));
318
+ }
319
+
320
+ /**
321
+ * Send transactions impersonating an externally owned account or contract.
322
+ * @param who - The address to impersonate
323
+ */
324
+ public async startImpersonating(who: EthAddress | Hex): Promise<void> {
325
+ try {
326
+ // Since the `who` impersonated will sometimes be a contract without funds, we fund it if needed.
327
+ if ((await this.getBalance(who)) === 0n) {
328
+ await this.setBalance(who, 10n * 10n ** 18n);
329
+ }
330
+
331
+ await this.rpcCall('hardhat_impersonateAccount', [who.toString()]);
332
+ } catch (err) {
333
+ throw new Error(`Error impersonating ${who}: ${err}`);
334
+ }
335
+ this.logger.warn(`Impersonating ${who}`);
336
+ }
337
+
338
+ /**
339
+ * Stop impersonating an account that you are currently impersonating.
340
+ * @param who - The address to stop impersonating
341
+ */
342
+ public async stopImpersonating(who: EthAddress | Hex): Promise<void> {
343
+ try {
344
+ await this.rpcCall('hardhat_stopImpersonatingAccount', [who.toString()]);
345
+ } catch (err) {
346
+ throw new Error(`Error when stopping the impersonation of ${who}: ${err}`);
347
+ }
348
+ this.logger.warn(`Stopped impersonating ${who}`);
349
+ }
350
+
351
+ /**
352
+ * Set the bytecode for a contract
353
+ * @param contract - The contract address
354
+ * @param bytecode - The bytecode to set
355
+ */
356
+ public async etch(contract: EthAddress, bytecode: `0x${string}`): Promise<void> {
357
+ try {
358
+ await this.rpcCall('hardhat_setCode', [contract.toString(), bytecode]);
359
+ } catch (err) {
360
+ throw new Error(`Error setting bytecode for ${contract}: ${err}`);
361
+ }
362
+ this.logger.warn(`Set bytecode for ${contract} to ${bytecode}`);
363
+ }
364
+
365
+ /**
366
+ * Get the bytecode for a contract
367
+ * @param contract - The contract address
368
+ * @returns The bytecode for the contract
369
+ */
370
+ public async getBytecode(contract: EthAddress): Promise<`0x${string}`> {
371
+ return await this.doRpcCall('eth_getCode', [contract.toString(), 'latest']);
372
+ }
373
+
374
+ /**
375
+ * Get the raw transaction object for a given transaction hash
376
+ * @param txHash - The transaction hash
377
+ * @returns The raw transaction
378
+ */
379
+ public async getRawTransaction(txHash: Hex): Promise<`0x${string}`> {
380
+ return await this.doRpcCall('debug_getRawTransaction', [txHash]);
381
+ }
382
+
383
+ /**
384
+ * Get the trace for a given transaction hash
385
+ * @param txHash - The transaction hash
386
+ * @returns The trace
387
+ */
388
+ public async debugTraceTransaction(txHash: Hex): Promise<any> {
389
+ return await this.doRpcCall('debug_traceTransaction', [txHash]);
390
+ }
391
+
392
+ /**
393
+ * Triggers a reorg of the given depth, removing those blocks from the chain.
394
+ * @param depth - The depth of the reorg
395
+ */
396
+ public reorg(depth: number): Promise<void> {
397
+ return this.execWithPausedAnvil(() => {
398
+ return this.rpcCall('anvil_rollback', [depth]);
399
+ });
400
+ }
401
+
402
+ /**
403
+ * Causes Anvil to reorg until the given block number is the new tip
404
+ * @param blockNumber - The block number that's going to be the new tip
405
+ */
406
+ public reorgTo(blockNumber: number): Promise<void> {
407
+ if (blockNumber <= 0) {
408
+ throw new Error(`Can't reorg to block before genesis: ${blockNumber}`);
409
+ }
410
+
411
+ return this.execWithPausedAnvil(async () => {
412
+ const currentTip = await this.publicClient.getBlockNumber();
413
+ if (currentTip < BigInt(blockNumber)) {
414
+ this.logger.warn(
415
+ `Can't call anvil_rollback, chain tip is behind target block: ${currentTip} < ${BigInt(blockNumber)}`,
416
+ );
417
+ return;
418
+ }
419
+
420
+ const depth = Number(currentTip - BigInt(blockNumber) + 1n);
421
+ await this.rpcCall('anvil_rollback', [depth]);
422
+ this.logger.warn(`Reorged L1 chain to block number ${blockNumber} (depth ${depth})`);
423
+ });
424
+ }
425
+
426
+ /**
427
+ * Triggers a reorg of the given depth, optionally replacing it with new blocks.
428
+ * The resulting block height will be the same as the original chain.
429
+ * @param depth - The depth of the reorg
430
+ * @param newBlocks - The blocks to replace the old ones with, each represented as a list of txs.
431
+ */
432
+ public async reorgWithReplacement(
433
+ depth: number,
434
+ newBlocks: (Hex | { to: EthAddress | Hex; input?: Hex; from?: EthAddress | Hex; value?: number | bigint })[][] = [],
435
+ ): Promise<void> {
436
+ this.logger.verbose(`Preparing L1 reorg with depth ${depth}`);
437
+ try {
438
+ await this.rpcCall('anvil_reorg', [
439
+ depth,
440
+ newBlocks.flatMap((txs, index) => txs.map(tx => [typeof tx === 'string' ? tx : { value: 0, ...tx }, index])),
441
+ ]);
442
+ } catch (err) {
443
+ throw new Error(`Error reorging: ${err}`);
444
+ }
445
+ this.logger.warn(`Reorged L1 chain with depth ${depth} and ${newBlocks.length} new blocks`, { depth, newBlocks });
446
+ }
447
+
448
+ public traceTransaction(txHash: Hex): Promise<any> {
449
+ return this.doRpcCall('trace_transaction', [txHash]);
450
+ }
451
+
452
+ public async getTxPoolStatus(): Promise<{ pending: number; queued: number }> {
453
+ const { pending, queued } = await this.doRpcCall('txpool_status', []);
454
+ return { pending: hexToNumber(pending), queued: hexToNumber(queued) };
455
+ }
456
+
457
+ public async getTxPoolContents(): Promise<TxPoolTransaction[]> {
458
+ const txpoolContent = await this.doRpcCall('txpool_content', []);
459
+ return mapTxPoolContent(txpoolContent);
460
+ }
461
+
462
+ /**
463
+ * Mines an empty block by temporarily removing all pending transactions from the mempool,
464
+ * mining a block, and then re-adding the transactions back to the pool.
465
+ */
466
+ public async mineEmptyBlock(blockCount: number = 1): Promise<void> {
467
+ await this.execWithPausedAnvil(async () => {
468
+ // Get all pending and queued transactions from the pool
469
+ const txs = await this.getTxPoolContents();
470
+
471
+ this.logger.debug(`Found ${txs.length} transactions in pool`);
472
+
473
+ // Get raw transactions before dropping them
474
+ const rawTxs: Hex[] = [];
475
+ for (const tx of txs) {
476
+ try {
477
+ const rawTx = await this.doRpcCall('debug_getRawTransaction', [tx.hash]);
478
+ if (rawTx) {
479
+ rawTxs.push(rawTx);
480
+ this.logger.debug(`Got raw tx for ${tx.hash}`);
481
+ } else {
482
+ this.logger.warn(`No raw tx found for ${tx.hash}`);
483
+ }
484
+ } catch {
485
+ this.logger.warn(`Failed to get raw transaction for ${tx.hash}`);
486
+ }
487
+ }
488
+
489
+ this.logger.debug(`Retrieved ${rawTxs.length} raw transactions`);
490
+
491
+ // Drop all transactions from the mempool
492
+ await this.doRpcCall('anvil_dropAllTransactions', []);
493
+
494
+ // Mine an empty block
495
+ await this.doMine(blockCount);
496
+
497
+ // Re-add the transactions to the pool
498
+ for (const rawTx of rawTxs) {
499
+ try {
500
+ const txHash = await this.doRpcCall('eth_sendRawTransaction', [rawTx]);
501
+ this.logger.debug(`Re-added transaction ${txHash}`);
502
+ } catch (err) {
503
+ this.logger.warn(`Failed to re-add transaction: ${err}`);
504
+ }
505
+ }
506
+
507
+ if (rawTxs.length !== txs.length) {
508
+ this.logger.warn(`Failed to add all txs back: had ${txs.length} but re-added ${rawTxs.length}`);
509
+ }
510
+ });
511
+
512
+ this.logger.warn(`Mined ${blockCount} empty L1 ${pluralize('block', blockCount)}`);
513
+ }
514
+
515
+ public async execWithPausedAnvil<T>(fn: () => Promise<T>): Promise<T> {
516
+ const [blockInterval, wasAutoMining] = await Promise.all([this.getIntervalMining(), this.isAutoMining()]);
517
+ try {
518
+ if (blockInterval !== null) {
519
+ await this.setIntervalMining(0, { silent: true });
520
+ }
521
+
522
+ if (wasAutoMining) {
523
+ await this.setAutomine(false, { silent: true });
524
+ }
525
+
526
+ return await fn();
527
+ } finally {
528
+ try {
529
+ // restore automine if necessary
530
+ if (wasAutoMining) {
531
+ await this.setAutomine(true, { silent: true });
532
+ }
533
+ } catch (err) {
534
+ this.logger.warn(`Failed to reenable automining: ${err}`);
535
+ }
536
+
537
+ try {
538
+ // restore automine if necessary
539
+ if (blockInterval !== null) {
540
+ await this.setIntervalMining(blockInterval, { silent: true });
541
+ }
542
+ } catch (err) {
543
+ this.logger.warn(`Failed to reenable interval mining: ${err}`);
544
+ }
545
+ }
546
+ }
547
+
548
+ public async syncDateProvider() {
549
+ const timestamp = await this.timestamp();
550
+ if ('setTime' in this.dateProvider) {
551
+ this.dateProvider.setTime(timestamp * 1000);
552
+ }
553
+ }
554
+ }
555
+
556
+ type TxPoolState = 'pending' | 'queued';
557
+
558
+ interface TxPoolContent {
559
+ pending: Record<Hex, Record<string, Transaction>>;
560
+ queued: Record<Hex, Record<string, Transaction>>;
561
+ }
562
+
563
+ export type TxPoolTransaction = Transaction & {
564
+ poolState: TxPoolState;
565
+ };
566
+
567
+ function mapTxPoolContent(content: TxPoolContent): TxPoolTransaction[] {
568
+ const result: TxPoolTransaction[] = [];
569
+
570
+ const processPool = (pool: Record<Hex, Record<string, Transaction>>, poolState: TxPoolState) => {
571
+ for (const txsByNonce of Object.values(pool)) {
572
+ for (const tx of Object.values(txsByNonce)) {
573
+ result.push({ ...tx, poolState });
574
+ }
575
+ }
576
+ };
577
+
578
+ processPool(content.pending, 'pending');
579
+ processPool(content.queued, 'queued');
580
+
581
+ return result;
582
+ }
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
2
 
3
- import { EthCheatCodes } from '../eth_cheat_codes.js';
3
+ import { EthCheatCodes } from './eth_cheat_codes.js';
4
4
 
5
5
  /**
6
6
  * A class that provides utility functions for interacting with ethereum (L1) dumping/loading state to/from a file.
package/src/test/index.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  export * from './delayed_tx_utils.js';
2
+ export * from './eth_cheat_codes.js';
2
3
  export * from './eth_cheat_codes_with_state.js';
3
4
  export * from './start_anvil.js';
4
5
  export * from './tx_delayer.js';
5
6
  export * from './upgrade_utils.js';
7
+ export * from './chain_monitor.js';
8
+ export * from './rollup_cheat_codes.js';