@aztec-labs/ethereum 6.0.0-nightly.20260829

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 (272) hide show
  1. package/README.md +3 -0
  2. package/dest/account.d.ts +2 -0
  3. package/dest/account.d.ts.map +1 -0
  4. package/dest/account.js +4 -0
  5. package/dest/chain.d.ts +25 -0
  6. package/dest/chain.d.ts.map +1 -0
  7. package/dest/chain.js +53 -0
  8. package/dest/client.d.ts +42 -0
  9. package/dest/client.d.ts.map +1 -0
  10. package/dest/client.js +104 -0
  11. package/dest/config.d.ts +103 -0
  12. package/dest/config.d.ts.map +1 -0
  13. package/dest/config.js +233 -0
  14. package/dest/constants.d.ts +4 -0
  15. package/dest/constants.d.ts.map +1 -0
  16. package/dest/constants.js +2 -0
  17. package/dest/contracts/chain_state_override.d.ts +81 -0
  18. package/dest/contracts/chain_state_override.d.ts.map +1 -0
  19. package/dest/contracts/chain_state_override.js +139 -0
  20. package/dest/contracts/empire_base.d.ts +30 -0
  21. package/dest/contracts/empire_base.d.ts.map +1 -0
  22. package/dest/contracts/empire_base.js +84 -0
  23. package/dest/contracts/errors.d.ts +7 -0
  24. package/dest/contracts/errors.d.ts.map +1 -0
  25. package/dest/contracts/errors.js +12 -0
  26. package/dest/contracts/fee_asset_handler.d.ts +20 -0
  27. package/dest/contracts/fee_asset_handler.d.ts.map +1 -0
  28. package/dest/contracts/fee_asset_handler.js +59 -0
  29. package/dest/contracts/fee_asset_price_oracle.d.ts +105 -0
  30. package/dest/contracts/fee_asset_price_oracle.d.ts.map +1 -0
  31. package/dest/contracts/fee_asset_price_oracle.js +656 -0
  32. package/dest/contracts/fee_juice.d.ts +14 -0
  33. package/dest/contracts/fee_juice.d.ts.map +1 -0
  34. package/dest/contracts/fee_juice.js +59 -0
  35. package/dest/contracts/governance.d.ts +192 -0
  36. package/dest/contracts/governance.d.ts.map +1 -0
  37. package/dest/contracts/governance.js +471 -0
  38. package/dest/contracts/governance_proposer.d.ts +63 -0
  39. package/dest/contracts/governance_proposer.d.ts.map +1 -0
  40. package/dest/contracts/governance_proposer.js +536 -0
  41. package/dest/contracts/gse.d.ts +38 -0
  42. package/dest/contracts/gse.d.ts.map +1 -0
  43. package/dest/contracts/gse.js +81 -0
  44. package/dest/contracts/inbox.d.ts +94 -0
  45. package/dest/contracts/inbox.d.ts.map +1 -0
  46. package/dest/contracts/inbox.js +141 -0
  47. package/dest/contracts/index.d.ts +18 -0
  48. package/dest/contracts/index.d.ts.map +1 -0
  49. package/dest/contracts/index.js +17 -0
  50. package/dest/contracts/log.d.ts +13 -0
  51. package/dest/contracts/log.d.ts.map +1 -0
  52. package/dest/contracts/log.js +1 -0
  53. package/dest/contracts/multicall.d.ts +138 -0
  54. package/dest/contracts/multicall.d.ts.map +1 -0
  55. package/dest/contracts/multicall.js +266 -0
  56. package/dest/contracts/outbox.d.ts +64 -0
  57. package/dest/contracts/outbox.d.ts.map +1 -0
  58. package/dest/contracts/outbox.js +113 -0
  59. package/dest/contracts/registry.d.ts +31 -0
  60. package/dest/contracts/registry.d.ts.map +1 -0
  61. package/dest/contracts/registry.js +142 -0
  62. package/dest/contracts/rollup.d.ts +5836 -0
  63. package/dest/contracts/rollup.d.ts.map +1 -0
  64. package/dest/contracts/rollup.js +1612 -0
  65. package/dest/contracts/slasher_contract.d.ts +44 -0
  66. package/dest/contracts/slasher_contract.d.ts.map +1 -0
  67. package/dest/contracts/slasher_contract.js +75 -0
  68. package/dest/contracts/slashing_proposer.d.ts +174 -0
  69. package/dest/contracts/slashing_proposer.d.ts.map +1 -0
  70. package/dest/contracts/slashing_proposer.js +755 -0
  71. package/dest/contracts/utils.d.ts +3 -0
  72. package/dest/contracts/utils.d.ts.map +1 -0
  73. package/dest/contracts/utils.js +11 -0
  74. package/dest/contracts/watch_event.d.ts +36 -0
  75. package/dest/contracts/watch_event.d.ts.map +1 -0
  76. package/dest/contracts/watch_event.js +66 -0
  77. package/dest/deploy_aztec_l1_contracts.d.ts +267 -0
  78. package/dest/deploy_aztec_l1_contracts.d.ts.map +1 -0
  79. package/dest/deploy_aztec_l1_contracts.js +424 -0
  80. package/dest/deploy_l1_contract.d.ts +68 -0
  81. package/dest/deploy_l1_contract.d.ts.map +1 -0
  82. package/dest/deploy_l1_contract.js +312 -0
  83. package/dest/eth-signer/eth-signer.d.ts +21 -0
  84. package/dest/eth-signer/eth-signer.d.ts.map +1 -0
  85. package/dest/eth-signer/eth-signer.js +5 -0
  86. package/dest/eth-signer/index.d.ts +2 -0
  87. package/dest/eth-signer/index.d.ts.map +1 -0
  88. package/dest/eth-signer/index.js +1 -0
  89. package/dest/forwarder_proxy.d.ts +32 -0
  90. package/dest/forwarder_proxy.d.ts.map +1 -0
  91. package/dest/forwarder_proxy.js +93 -0
  92. package/dest/foundry_binary.d.ts +13 -0
  93. package/dest/foundry_binary.d.ts.map +1 -0
  94. package/dest/foundry_binary.js +52 -0
  95. package/dest/l1_artifacts.d.ts +81896 -0
  96. package/dest/l1_artifacts.d.ts.map +1 -0
  97. package/dest/l1_artifacts.js +151 -0
  98. package/dest/l1_contract_addresses.d.ts +73 -0
  99. package/dest/l1_contract_addresses.d.ts.map +1 -0
  100. package/dest/l1_contract_addresses.js +120 -0
  101. package/dest/l1_reader.d.ts +18 -0
  102. package/dest/l1_reader.d.ts.map +1 -0
  103. package/dest/l1_reader.js +35 -0
  104. package/dest/l1_tx_utils/config.d.ts +65 -0
  105. package/dest/l1_tx_utils/config.d.ts.map +1 -0
  106. package/dest/l1_tx_utils/config.js +109 -0
  107. package/dest/l1_tx_utils/constants.d.ts +12 -0
  108. package/dest/l1_tx_utils/constants.d.ts.map +1 -0
  109. package/dest/l1_tx_utils/constants.js +39 -0
  110. package/dest/l1_tx_utils/factory.d.ts +32 -0
  111. package/dest/l1_tx_utils/factory.d.ts.map +1 -0
  112. package/dest/l1_tx_utils/factory.js +22 -0
  113. package/dest/l1_tx_utils/fee-strategies/index.d.ts +10 -0
  114. package/dest/l1_tx_utils/fee-strategies/index.d.ts.map +1 -0
  115. package/dest/l1_tx_utils/fee-strategies/index.js +12 -0
  116. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts +8 -0
  117. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts.map +1 -0
  118. package/dest/l1_tx_utils/fee-strategies/p75_competitive.js +129 -0
  119. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts +23 -0
  120. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts.map +1 -0
  121. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.js +191 -0
  122. package/dest/l1_tx_utils/fee-strategies/types.d.ts +51 -0
  123. package/dest/l1_tx_utils/fee-strategies/types.d.ts.map +1 -0
  124. package/dest/l1_tx_utils/fee-strategies/types.js +3 -0
  125. package/dest/l1_tx_utils/forwarder_l1_tx_utils.d.ts +41 -0
  126. package/dest/l1_tx_utils/forwarder_l1_tx_utils.d.ts.map +1 -0
  127. package/dest/l1_tx_utils/forwarder_l1_tx_utils.js +42 -0
  128. package/dest/l1_tx_utils/index-blobs.d.ts +3 -0
  129. package/dest/l1_tx_utils/index-blobs.d.ts.map +1 -0
  130. package/dest/l1_tx_utils/index-blobs.js +2 -0
  131. package/dest/l1_tx_utils/index.d.ts +13 -0
  132. package/dest/l1_tx_utils/index.d.ts.map +1 -0
  133. package/dest/l1_tx_utils/index.js +13 -0
  134. package/dest/l1_tx_utils/interfaces.d.ts +76 -0
  135. package/dest/l1_tx_utils/interfaces.d.ts.map +1 -0
  136. package/dest/l1_tx_utils/interfaces.js +4 -0
  137. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts +252 -0
  138. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts.map +1 -0
  139. package/dest/l1_tx_utils/l1_fee_analyzer.js +568 -0
  140. package/dest/l1_tx_utils/l1_tx_utils.d.ts +113 -0
  141. package/dest/l1_tx_utils/l1_tx_utils.d.ts.map +1 -0
  142. package/dest/l1_tx_utils/l1_tx_utils.js +720 -0
  143. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +83 -0
  144. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -0
  145. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +357 -0
  146. package/dest/l1_tx_utils/signer.d.ts +4 -0
  147. package/dest/l1_tx_utils/signer.d.ts.map +1 -0
  148. package/dest/l1_tx_utils/signer.js +16 -0
  149. package/dest/l1_tx_utils/tx_delayer.d.ts +58 -0
  150. package/dest/l1_tx_utils/tx_delayer.d.ts.map +1 -0
  151. package/dest/l1_tx_utils/tx_delayer.js +223 -0
  152. package/dest/l1_tx_utils/types.d.ts +93 -0
  153. package/dest/l1_tx_utils/types.d.ts.map +1 -0
  154. package/dest/l1_tx_utils/types.js +36 -0
  155. package/dest/l1_tx_utils/utils.d.ts +4 -0
  156. package/dest/l1_tx_utils/utils.d.ts.map +1 -0
  157. package/dest/l1_tx_utils/utils.js +14 -0
  158. package/dest/l1_types.d.ts +6 -0
  159. package/dest/l1_types.d.ts.map +1 -0
  160. package/dest/l1_types.js +1 -0
  161. package/dest/publisher_manager.d.ts +42 -0
  162. package/dest/publisher_manager.d.ts.map +1 -0
  163. package/dest/publisher_manager.js +199 -0
  164. package/dest/queries.d.ts +25 -0
  165. package/dest/queries.d.ts.map +1 -0
  166. package/dest/queries.js +144 -0
  167. package/dest/test/blob_kzg_warmup.d.ts +19 -0
  168. package/dest/test/blob_kzg_warmup.d.ts.map +1 -0
  169. package/dest/test/blob_kzg_warmup.js +64 -0
  170. package/dest/test/chain_monitor.d.ts +126 -0
  171. package/dest/test/chain_monitor.d.ts.map +1 -0
  172. package/dest/test/chain_monitor.js +314 -0
  173. package/dest/test/eth_cheat_codes.d.ts +260 -0
  174. package/dest/test/eth_cheat_codes.d.ts.map +1 -0
  175. package/dest/test/eth_cheat_codes.js +608 -0
  176. package/dest/test/eth_cheat_codes_with_state.d.ts +18 -0
  177. package/dest/test/eth_cheat_codes_with_state.d.ts.map +1 -0
  178. package/dest/test/eth_cheat_codes_with_state.js +34 -0
  179. package/dest/test/index.d.ts +8 -0
  180. package/dest/test/index.d.ts.map +1 -0
  181. package/dest/test/index.js +7 -0
  182. package/dest/test/recording_rpc_server.d.ts +18 -0
  183. package/dest/test/recording_rpc_server.d.ts.map +1 -0
  184. package/dest/test/recording_rpc_server.js +68 -0
  185. package/dest/test/rollup_cheat_codes.d.ts +142 -0
  186. package/dest/test/rollup_cheat_codes.d.ts.map +1 -0
  187. package/dest/test/rollup_cheat_codes.js +338 -0
  188. package/dest/test/start_anvil.d.ts +40 -0
  189. package/dest/test/start_anvil.d.ts.map +1 -0
  190. package/dest/test/start_anvil.js +210 -0
  191. package/dest/test/upgrade_utils.d.ts +12 -0
  192. package/dest/test/upgrade_utils.d.ts.map +1 -0
  193. package/dest/test/upgrade_utils.js +111 -0
  194. package/dest/types.d.ts +68 -0
  195. package/dest/types.d.ts.map +1 -0
  196. package/dest/types.js +3 -0
  197. package/dest/utils.d.ts +45 -0
  198. package/dest/utils.d.ts.map +1 -0
  199. package/dest/utils.js +260 -0
  200. package/dest/zkPassportVerifierAddress.d.ts +15 -0
  201. package/dest/zkPassportVerifierAddress.d.ts.map +1 -0
  202. package/dest/zkPassportVerifierAddress.js +11 -0
  203. package/package.json +122 -0
  204. package/src/account.ts +5 -0
  205. package/src/chain.ts +71 -0
  206. package/src/client.ts +165 -0
  207. package/src/config.ts +342 -0
  208. package/src/constants.ts +4 -0
  209. package/src/contracts/README.md +157 -0
  210. package/src/contracts/chain_state_override.ts +203 -0
  211. package/src/contracts/empire_base.ts +89 -0
  212. package/src/contracts/errors.ts +13 -0
  213. package/src/contracts/fee_asset_handler.ts +66 -0
  214. package/src/contracts/fee_asset_price_oracle.ts +284 -0
  215. package/src/contracts/fee_juice.ts +57 -0
  216. package/src/contracts/governance.ts +597 -0
  217. package/src/contracts/governance_proposer.ts +181 -0
  218. package/src/contracts/gse.ts +105 -0
  219. package/src/contracts/inbox.ts +223 -0
  220. package/src/contracts/index.ts +17 -0
  221. package/src/contracts/log.ts +13 -0
  222. package/src/contracts/multicall.ts +298 -0
  223. package/src/contracts/outbox.ts +132 -0
  224. package/src/contracts/registry.ts +161 -0
  225. package/src/contracts/rollup.ts +1502 -0
  226. package/src/contracts/slasher_contract.ts +89 -0
  227. package/src/contracts/slashing_proposer.ts +397 -0
  228. package/src/contracts/utils.ts +14 -0
  229. package/src/contracts/watch_event.ts +109 -0
  230. package/src/deploy_aztec_l1_contracts.ts +663 -0
  231. package/src/deploy_l1_contract.ts +361 -0
  232. package/src/eth-signer/eth-signer.ts +24 -0
  233. package/src/eth-signer/index.ts +1 -0
  234. package/src/forwarder_proxy.ts +107 -0
  235. package/src/foundry_binary.ts +57 -0
  236. package/src/l1_artifacts.ts +231 -0
  237. package/src/l1_contract_addresses.ts +149 -0
  238. package/src/l1_reader.ts +57 -0
  239. package/src/l1_tx_utils/README.md +177 -0
  240. package/src/l1_tx_utils/config.ts +181 -0
  241. package/src/l1_tx_utils/constants.ts +29 -0
  242. package/src/l1_tx_utils/factory.ts +64 -0
  243. package/src/l1_tx_utils/fee-strategies/index.ts +22 -0
  244. package/src/l1_tx_utils/fee-strategies/p75_competitive.ts +162 -0
  245. package/src/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.ts +244 -0
  246. package/src/l1_tx_utils/fee-strategies/types.ts +55 -0
  247. package/src/l1_tx_utils/forwarder_l1_tx_utils.ts +107 -0
  248. package/src/l1_tx_utils/index-blobs.ts +2 -0
  249. package/src/l1_tx_utils/index.ts +15 -0
  250. package/src/l1_tx_utils/interfaces.ts +86 -0
  251. package/src/l1_tx_utils/l1_fee_analyzer.ts +874 -0
  252. package/src/l1_tx_utils/l1_tx_utils.ts +840 -0
  253. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +459 -0
  254. package/src/l1_tx_utils/signer.ts +27 -0
  255. package/src/l1_tx_utils/tx_delayer.ts +302 -0
  256. package/src/l1_tx_utils/types.ts +116 -0
  257. package/src/l1_tx_utils/utils.ts +15 -0
  258. package/src/l1_types.ts +6 -0
  259. package/src/publisher_manager.ts +239 -0
  260. package/src/queries.ts +189 -0
  261. package/src/test/blob_kzg_warmup.ts +65 -0
  262. package/src/test/chain_monitor.ts +389 -0
  263. package/src/test/eth_cheat_codes.ts +624 -0
  264. package/src/test/eth_cheat_codes_with_state.ts +38 -0
  265. package/src/test/index.ts +7 -0
  266. package/src/test/recording_rpc_server.ts +69 -0
  267. package/src/test/rollup_cheat_codes.ts +398 -0
  268. package/src/test/start_anvil.ts +255 -0
  269. package/src/test/upgrade_utils.ts +109 -0
  270. package/src/types.ts +97 -0
  271. package/src/utils.ts +344 -0
  272. package/src/zkPassportVerifierAddress.ts +15 -0
@@ -0,0 +1,89 @@
1
+ import { SlasherAbi } from '@aztec/l1-artifacts/SlasherAbi';
2
+
3
+ import { EthAddress } from '@aztec-labs/foundation/eth-address';
4
+ import { createLogger } from '@aztec-labs/foundation/log';
5
+ import { type GetContractReturnType, getContract } from 'viem';
6
+
7
+ import type { ViemClient } from '../types.js';
8
+
9
+ /**
10
+ * Typescript wrapper around the Slasher contract.
11
+ */
12
+ export class SlasherContract {
13
+ private contract: GetContractReturnType<typeof SlasherAbi, ViemClient>;
14
+
15
+ constructor(
16
+ private readonly client: ViemClient,
17
+ private readonly address: EthAddress,
18
+ private readonly log = createLogger('slasher-contract'),
19
+ ) {
20
+ this.contract = getContract({
21
+ address: this.address.toString(),
22
+ abi: SlasherAbi,
23
+ client: this.client,
24
+ });
25
+ }
26
+
27
+ /**
28
+ * Checks if a slash payload is vetoed.
29
+ * @param payloadAddress - The address of the payload to check
30
+ * @returns True if the payload is vetoed, false otherwise
31
+ */
32
+ public async isPayloadVetoed(payloadAddress: EthAddress): Promise<boolean> {
33
+ try {
34
+ return await this.contract.read.vetoedPayloads([payloadAddress.toString()]);
35
+ } catch (error) {
36
+ this.log.error(`Error checking if payload ${payloadAddress} is vetoed`, error);
37
+ throw error;
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Checks if slashing is currently enabled. Slashing can be disabled by the vetoer.
43
+ * @returns True if slashing is enabled, false otherwise
44
+ */
45
+ public async isSlashingEnabled(): Promise<boolean> {
46
+ try {
47
+ return await this.contract.read.isSlashingEnabled();
48
+ } catch (error) {
49
+ this.log.error(`Error checking if slashing is enabled`, error);
50
+ throw error;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Gets the current vetoer address.
56
+ * @returns The vetoer address
57
+ */
58
+ public async getVetoer(): Promise<EthAddress> {
59
+ const vetoer = await this.contract.read.VETOER();
60
+ return EthAddress.fromString(vetoer);
61
+ }
62
+
63
+ /**
64
+ * Gets the disable duration by the vetoer.
65
+ * @returns The disable duration in seconds
66
+ */
67
+ public async getSlashingDisableDuration(): Promise<number> {
68
+ const duration = await this.contract.read.SLASHING_DISABLE_DURATION();
69
+ return Number(duration);
70
+ }
71
+
72
+ /**
73
+ * Gets the current governance address.
74
+ * @returns The governance address
75
+ */
76
+ public async getGovernance(): Promise<EthAddress> {
77
+ const governance = await this.contract.read.GOVERNANCE();
78
+ return EthAddress.fromString(governance);
79
+ }
80
+
81
+ /**
82
+ * Gets the current proposer address.
83
+ * @returns The proposer address
84
+ */
85
+ public async getProposer(): Promise<EthAddress> {
86
+ const proposer = await this.contract.read.PROPOSER();
87
+ return EthAddress.fromString(proposer);
88
+ }
89
+ }
@@ -0,0 +1,397 @@
1
+ import { SlasherAbi } from '@aztec/l1-artifacts/SlasherAbi';
2
+ import { SlashingProposerAbi } from '@aztec/l1-artifacts/SlashingProposerAbi';
3
+
4
+ import type { L1TxRequest } from '@aztec-labs/ethereum/l1-tx-utils';
5
+ import type { ViemClient } from '@aztec-labs/ethereum/types';
6
+ import { mergeAbis, tryExtractEvent } from '@aztec-labs/ethereum/utils';
7
+ import { SlotNumber } from '@aztec-labs/foundation/branded-types';
8
+ import { Buffer32 } from '@aztec-labs/foundation/buffer';
9
+ import { memoize } from '@aztec-labs/foundation/decorators';
10
+ import { EthAddress } from '@aztec-labs/foundation/eth-address';
11
+ import { Signature } from '@aztec-labs/foundation/eth-signature';
12
+ import { createLogger } from '@aztec-labs/foundation/log';
13
+ import { hexToBuffer } from '@aztec-labs/foundation/string';
14
+ import {
15
+ type GetContractReturnType,
16
+ type Hex,
17
+ type Log,
18
+ type TypedDataDefinition,
19
+ encodeFunctionData,
20
+ getContract,
21
+ } from 'viem';
22
+
23
+ import { type WatchContractEventOptions, watchContractEvent } from './watch_event.js';
24
+
25
+ /**
26
+ * Wrapper around the SlashingProposer contract that provides
27
+ * a TypeScript interface for interacting with the consensus-based slashing system.
28
+ */
29
+ export class SlashingProposerContract {
30
+ private readonly contract: GetContractReturnType<typeof SlashingProposerAbi, ViemClient>;
31
+ private readonly logger = createLogger('ethereum:slashing_proposer');
32
+
33
+ /**
34
+ * Slash target validators of the last round asked for. Safe to cache because a round's targets are the committees
35
+ * of epochs that had already ended when the round opened (see SLASH_OFFSET_IN_ROUNDS), and those committees are
36
+ * sampled from validator set and randao snapshots taken before the epoch started, so they cannot change while the
37
+ * round is live. The promise is cached rather than the result, so concurrent callers share one in-flight read.
38
+ */
39
+ private slashTargetValidators: { round: bigint; validators: Promise<EthAddress[]> } | undefined;
40
+
41
+ constructor(
42
+ public readonly client: ViemClient,
43
+ address: Hex | EthAddress,
44
+ ) {
45
+ this.contract = getContract({
46
+ address: typeof address === 'string' ? address : address.toString(),
47
+ abi: SlashingProposerAbi,
48
+ client,
49
+ });
50
+ }
51
+
52
+ public get address() {
53
+ return EthAddress.fromString(this.contract.address);
54
+ }
55
+
56
+ public getQuorumSize(): Promise<bigint> {
57
+ return this.contract.read.QUORUM();
58
+ }
59
+
60
+ public getRoundSize(): Promise<bigint> {
61
+ return this.contract.read.ROUND_SIZE();
62
+ }
63
+
64
+ public getCommitteeSize(): Promise<bigint> {
65
+ return this.contract.read.COMMITTEE_SIZE();
66
+ }
67
+
68
+ public getRoundSizeInEpochs(): Promise<bigint> {
69
+ return this.contract.read.ROUND_SIZE_IN_EPOCHS();
70
+ }
71
+
72
+ public getLifetimeInRounds(): Promise<bigint> {
73
+ return this.contract.read.LIFETIME_IN_ROUNDS();
74
+ }
75
+
76
+ public getExecutionDelayInRounds(): Promise<bigint> {
77
+ return this.contract.read.EXECUTION_DELAY_IN_ROUNDS();
78
+ }
79
+
80
+ /** Returns the slash amounts for the three slash unit levels. Immutable on the contract, so memoized. */
81
+ @memoize
82
+ public getSlashingAmounts(): Promise<[bigint, bigint, bigint]> {
83
+ return Promise.all([
84
+ this.contract.read.SLASH_AMOUNT_SMALL(),
85
+ this.contract.read.SLASH_AMOUNT_MEDIUM(),
86
+ this.contract.read.SLASH_AMOUNT_LARGE(),
87
+ ]);
88
+ }
89
+
90
+ public getSlashOffsetInRounds(): Promise<bigint> {
91
+ return this.contract.read.SLASH_OFFSET_IN_ROUNDS();
92
+ }
93
+
94
+ public getCurrentRound(): Promise<bigint> {
95
+ return this.contract.read.getCurrentRound();
96
+ }
97
+
98
+ /**
99
+ * Get round information
100
+ * @param round - The round number to query
101
+ * @returns Round status information
102
+ */
103
+ public async getRound(round: bigint): Promise<{
104
+ isExecuted: boolean;
105
+ voteCount: bigint;
106
+ }> {
107
+ const [isExecuted, voteCount] = await this.contract.read.getRound([round]);
108
+ return { isExecuted, voteCount };
109
+ }
110
+
111
+ /**
112
+ * Check if a round is ready to execute at a given slot
113
+ * @param round - The round number to check
114
+ * @param slot - The slot number to check at
115
+ * @returns Whether the round is ready to execute
116
+ */
117
+ public async isRoundReadyToExecute(round: bigint, slot: SlotNumber): Promise<boolean> {
118
+ return await this.contract.read.isRoundReadyToExecute([round, BigInt(slot)]);
119
+ }
120
+
121
+ /** Returns the slash actions and payload address for a given round (zero if no slash actions) */
122
+ public async getPayload(
123
+ round: bigint,
124
+ ): Promise<{ actions: { slashAmount: bigint; validator: EthAddress }[]; address: EthAddress }> {
125
+ const { result: committees } = await this.contract.simulate.getSlashTargetCommittees([round]);
126
+ const tally = await this.contract.read.getTally([round, committees]);
127
+ const address = await this.contract.read.getPayloadAddress([round, tally]);
128
+ const actions = this.mapSlashActions(tally);
129
+ return { actions, address: EthAddress.fromString(address) };
130
+ }
131
+
132
+ /** Returns the slash actions to be executed for a given round based on votes */
133
+ public async getTally(
134
+ round: bigint,
135
+ ): Promise<{ actions: { slashAmount: bigint; validator: EthAddress }[]; committees: EthAddress[][] }> {
136
+ const { result: committees } = await this.contract.simulate.getSlashTargetCommittees([round]);
137
+ const tally = await this.contract.read.getTally([round, committees]);
138
+ return { actions: this.mapSlashActions(tally), committees: committees.map(c => c.map(EthAddress.fromString)) };
139
+ }
140
+
141
+ private mapSlashActions(
142
+ actions: readonly { slashAmount: bigint; validator: Hex }[],
143
+ ): { slashAmount: bigint; validator: EthAddress }[] {
144
+ return actions.map(({ validator, slashAmount }) => ({
145
+ validator: EthAddress.fromString(validator),
146
+ slashAmount,
147
+ }));
148
+ }
149
+
150
+ /** Tries to extract a VoteCast event from the given logs. */
151
+ public tryExtractVoteCastEvent(logs: Log[]) {
152
+ return tryExtractEvent(logs, this.address.toString(), SlashingProposerAbi, 'VoteCast');
153
+ }
154
+
155
+ /** Tries to extract a RoundExecuted event from the given logs. */
156
+ public tryExtractRoundExecutedEvent(logs: Log[]) {
157
+ return tryExtractEvent(logs, this.address.toString(), SlashingProposerAbi, 'RoundExecuted');
158
+ }
159
+
160
+ /**
161
+ * Create a transaction to vote for slashing offenses
162
+ * @param votes - The encoded votes for slashing
163
+ * @param slot - The slot number for the vote
164
+ * @param signer - The signer to produce the signature
165
+ * @returns L1 transaction request
166
+ */
167
+ public async buildVoteRequestFromSigner(
168
+ votes: Hex,
169
+ slot: SlotNumber,
170
+ signer: (msg: TypedDataDefinition) => Promise<Hex>,
171
+ ): Promise<L1TxRequest> {
172
+ const typedData = this.buildVoteTypedData(votes, slot);
173
+ const signature = Signature.fromString(await signer(typedData));
174
+
175
+ return {
176
+ to: this.contract.address,
177
+ abi: SlashingProposerAbi,
178
+ data: encodeFunctionData({
179
+ abi: SlashingProposerAbi,
180
+ functionName: 'vote',
181
+ args: [votes, signature.toViemSignature()],
182
+ }),
183
+ };
184
+ }
185
+
186
+ /** Returns the typed data definition to EIP712-sign for voting */
187
+ public buildVoteTypedData(votes: Hex, slot: SlotNumber): TypedDataDefinition {
188
+ const domain = {
189
+ name: 'SlashingProposer',
190
+ version: '1',
191
+ chainId: this.client.chain.id,
192
+ verifyingContract: this.contract.address,
193
+ };
194
+
195
+ const types = {
196
+ EIP712Domain: [
197
+ { name: 'name', type: 'string' },
198
+ { name: 'version', type: 'string' },
199
+ { name: 'chainId', type: 'uint256' },
200
+ { name: 'verifyingContract', type: 'address' },
201
+ ],
202
+ Vote: [
203
+ { name: 'votes', type: 'bytes' },
204
+ { name: 'slot', type: 'uint256' },
205
+ ],
206
+ };
207
+
208
+ return { domain, types, primaryType: 'Vote', message: { votes, slot: BigInt(slot) } };
209
+ }
210
+
211
+ /** Gets the digest to sign for voting directly from the contract */
212
+ public async getVoteDataDigest(votes: Hex, slot: SlotNumber): Promise<Buffer32> {
213
+ return Buffer32.fromString(await this.contract.read.getVoteSignatureDigest([votes, BigInt(slot)]));
214
+ }
215
+
216
+ /**
217
+ * Create a transaction to vote for slashing offenses
218
+ * @param votes - The encoded votes for slashing
219
+ * @param signature - The signature from the current proposer
220
+ * @returns L1 transaction request
221
+ */
222
+ public buildVoteRequestWithSignature(votes: Hex, signature: { v: number; r: Hex; s: Hex }): L1TxRequest {
223
+ return {
224
+ to: this.contract.address,
225
+ abi: SlashingProposerAbi,
226
+ data: encodeFunctionData({
227
+ abi: SlashingProposerAbi,
228
+ functionName: 'vote',
229
+ args: [votes, signature],
230
+ }),
231
+ };
232
+ }
233
+
234
+ /**
235
+ * Create a transaction to execute a slashing round
236
+ * @param round - The round number to execute
237
+ * @param committees - The committees for each epoch in the round
238
+ * @returns L1 transaction request
239
+ */
240
+ public buildExecuteRoundRequest(round: bigint, committees: EthAddress[][]): L1TxRequest {
241
+ return {
242
+ to: this.contract.address,
243
+ abi: mergeAbis([SlashingProposerAbi, SlasherAbi]),
244
+ data: encodeFunctionData({
245
+ abi: SlashingProposerAbi,
246
+ functionName: 'executeRound',
247
+ args: [round, committees.map(c => c.map(addr => addr.toString()))],
248
+ }),
249
+ };
250
+ }
251
+
252
+ /**
253
+ * Returns the validators eligible to be voted against in a round, in the order votes encode them. Cached for the
254
+ * last round asked for: reading them runs a committee sampling simulation on L1, and every vote of a round decodes
255
+ * against the same list, so a caller walking a round's votes would otherwise repeat that call per vote. Only the
256
+ * last round is kept since callers move forward round by round.
257
+ */
258
+ public getSlashTargetValidators(round: bigint): Promise<EthAddress[]> {
259
+ const cached = this.slashTargetValidators;
260
+ if (cached?.round === round) {
261
+ return cached.validators;
262
+ }
263
+
264
+ const entry = { round, validators: this.fetchSlashTargetValidators(round) };
265
+ this.slashTargetValidators = entry;
266
+ // A failed read must not stay cached, or every later vote of the round would replay the same rejection
267
+ entry.validators.catch(() => {
268
+ if (this.slashTargetValidators === entry) {
269
+ this.slashTargetValidators = undefined;
270
+ }
271
+ });
272
+ return entry.validators;
273
+ }
274
+
275
+ private async fetchSlashTargetValidators(round: bigint): Promise<EthAddress[]> {
276
+ const { result } = await this.contract.simulate.getSlashTargetCommittees([round]);
277
+ return result.flat().map(validator => EthAddress.fromString(validator));
278
+ }
279
+
280
+ /**
281
+ * Returns the slash amount voted for each target validator by a single vote of a round.
282
+ * @param index - Position of the vote within the round, from 0 (inclusive) to the round's vote count (exclusive)
283
+ */
284
+ public async getVoteAt(round: bigint, index: bigint): Promise<SlashVoteTarget[]> {
285
+ const [validators, vote, slashAmounts] = await Promise.all([
286
+ this.getSlashTargetValidators(round),
287
+ this.contract.read.getVotes([round, index]),
288
+ this.getSlashingAmounts(),
289
+ ]);
290
+ return decodeVote(vote, validators, slashAmounts);
291
+ }
292
+
293
+ /** Returns the last vote emitted for a given round */
294
+ public async getLastVote(round: bigint) {
295
+ const { voteCount } = await this.getRound(round);
296
+ return await this.getVoteAt(round, voteCount - 1n);
297
+ }
298
+
299
+ /**
300
+ * Listen for VoteCast events. Events are delivered by polling `eth_getLogs`: a reorg may re-emit them and
301
+ * removals are never reported, and events mined within roughly one polling interval of subscribing may be missed.
302
+ * @param callback - Callback receiving the round, the vote's index within the round as accepted by `getVoteAt`,
303
+ * and the proposer that cast it
304
+ * @returns Unwatch function
305
+ */
306
+ public listenToVoteCast(
307
+ callback: (args: { round: bigint; voteIndex: bigint; proposer: string }) => unknown,
308
+ options?: WatchContractEventOptions,
309
+ ): () => void {
310
+ return watchContractEvent(
311
+ this.client,
312
+ this.logger,
313
+ {
314
+ address: this.contract.address,
315
+ abi: SlashingProposerAbi,
316
+ eventName: 'VoteCast',
317
+ onLog: log => {
318
+ const { round, voteIndex, proposer } = log.args;
319
+ if (round !== undefined && voteIndex !== undefined && proposer) {
320
+ return callback({ round, voteIndex, proposer });
321
+ }
322
+ },
323
+ },
324
+ options,
325
+ );
326
+ }
327
+
328
+ /**
329
+ * Listen for RoundExecuted events. Events are delivered by polling `eth_getLogs`: a reorg may re-emit them and
330
+ * removals are never reported, and events mined within roughly one polling interval of subscribing may be missed.
331
+ * @param callback - Callback function to handle round executed events
332
+ * @returns Unwatch function
333
+ */
334
+ public listenToRoundExecuted(
335
+ callback: (args: { round: bigint; slashCount: bigint; l1BlockHash: Hex }) => unknown,
336
+ options?: WatchContractEventOptions,
337
+ ): () => void {
338
+ return watchContractEvent(
339
+ this.client,
340
+ this.logger,
341
+ {
342
+ address: this.contract.address,
343
+ abi: SlashingProposerAbi,
344
+ eventName: 'RoundExecuted',
345
+ onLog: log => {
346
+ const { round, slashCount } = log.args;
347
+ if (round !== undefined && slashCount !== undefined) {
348
+ return callback({ round, slashCount, l1BlockHash: log.blockHash });
349
+ }
350
+ },
351
+ },
352
+ options,
353
+ );
354
+ }
355
+ }
356
+
357
+ /**
358
+ * A validator targeted by a slashing vote, with the amount voted. The position is the validator's index in the
359
+ * round's flattened slash target committees — the unit the contract tallies quorum by. A validator sitting in
360
+ * several of the round's committees holds several positions, each with its own tally.
361
+ */
362
+ export type SlashVoteTarget = { validator: EthAddress; slashAmount: bigint; position: number };
363
+
364
+ function decodeVote(vote: Hex, validators: EthAddress[], slashAmounts: [bigint, bigint, bigint]): SlashVoteTarget[] {
365
+ return decodeSlashConsensusVotes(hexToBuffer(vote))
366
+ .map((units, position) => ({
367
+ validator: validators[position],
368
+ slashAmount: slashAmounts[units - 1] ?? 0n,
369
+ position,
370
+ }))
371
+ .filter(v => v.slashAmount > 0n);
372
+ }
373
+
374
+ /**
375
+ * Decodes a Buffer containing slash votes back into an array of numbers.
376
+ * Each vote is represented as a 2-bit value (0, 1, 2, or 3) representing slashing units.
377
+ * @dev This should live in stdlib next to encodeSlashConsensusVotes but is here since we
378
+ * do not have a dependency to stdlib from the ethereum package. We need a larger refactor to fix this.
379
+ * @param buffer - The Buffer containing encoded slash votes
380
+ * @returns An array of numbers representing the slash votes
381
+ */
382
+ export function decodeSlashConsensusVotes(buffer: Buffer): number[] {
383
+ const votes: number[] = [];
384
+ for (let i = 0; i < buffer.length; i++) {
385
+ const voteByte = buffer.readUInt8(i);
386
+ // Decode votes from Solidity's bit order (LSB to MSB)
387
+ // Bits 0-1: validator at index i*4
388
+ // Bits 2-3: validator at index i*4+1
389
+ // Bits 4-5: validator at index i*4+2
390
+ // Bits 6-7: validator at index i*4+3
391
+ votes.push((voteByte >> 0) & 0x03);
392
+ votes.push((voteByte >> 2) & 0x03);
393
+ votes.push((voteByte >> 4) & 0x03);
394
+ votes.push((voteByte >> 6) & 0x03);
395
+ }
396
+ return votes;
397
+ }
@@ -0,0 +1,14 @@
1
+ import type { ViemClient } from '../types.js';
2
+ import { BlockTagTooOldError } from './errors.js';
3
+
4
+ const L1_NON_ARCHIVE_BLOCK_HISTORY_LENGTH = 128n;
5
+
6
+ export async function checkBlockTag(block: bigint | undefined, publicClient: ViemClient) {
7
+ if (block === undefined) {
8
+ return;
9
+ }
10
+ const latestBlock = await publicClient.getBlockNumber();
11
+ if (block < latestBlock - L1_NON_ARCHIVE_BLOCK_HISTORY_LENGTH) {
12
+ throw new BlockTagTooOldError(block, latestBlock);
13
+ }
14
+ }
@@ -0,0 +1,109 @@
1
+ import type { Logger } from '@aztec-labs/foundation/log';
2
+ import { RunningPromise } from '@aztec-labs/foundation/promise';
3
+ import type { Abi, ContractEventName, GetContractEventsReturnType, Hex } from 'viem';
4
+
5
+ import type { ViemClient } from '../types.js';
6
+
7
+ /** Default maximum number of blocks queried per `eth_getLogs` request. */
8
+ const DEFAULT_MAX_BLOCK_RANGE = 100;
9
+
10
+ /** Options for {@link watchContractEvent}. */
11
+ export type WatchContractEventOptions = {
12
+ /** How often to poll for new L1 blocks, in ms. Defaults to the client's polling interval. */
13
+ pollingIntervalMs?: number;
14
+ /** Maximum number of blocks per `eth_getLogs` request; wider catch-up ranges are split into chunks. */
15
+ maxBlockRange?: number;
16
+ };
17
+
18
+ /** Event identity and handler for {@link watchContractEvent}. */
19
+ export type WatchContractEventParameters<
20
+ TAbi extends Abi,
21
+ TEventName extends ContractEventName<TAbi>,
22
+ TStrict extends boolean | undefined = undefined,
23
+ > = {
24
+ address: Hex;
25
+ abi: TAbi;
26
+ eventName: TEventName;
27
+ /** Whether logs must decode against the event's indexed/non-indexed arguments (defaults to false). */
28
+ strict?: TStrict;
29
+ /** Called once per event log. Errors, thrown or from a returned promise, are caught and logged. */
30
+ onLog: (log: GetContractEventsReturnType<TAbi, TEventName, TStrict>[number]) => unknown;
31
+ };
32
+
33
+ /**
34
+ * Watches for contract events by polling `eth_blockNumber` (cached by viem per client) and fetching new logs with
35
+ * ranged `eth_getLogs` requests, never installing server-side filters. Server-side filters are unusable against
36
+ * real-world endpoints: a load balancer routes polls to backends that never saw the filter, and providers report a
37
+ * purged filter with error codes viem does not recognize as "filter gone", so viem's own watchers either churn
38
+ * recreating filters or poll a dead filter id forever while silently missing events.
39
+ *
40
+ * The block that is latest on the first poll becomes the starting cursor, so its events are reported and events from
41
+ * earlier blocks are not. Requests never span more than `maxBlockRange` blocks: catching up after downtime is
42
+ * chunked into multiple requests, and the cursor advances per successful chunk so a failure retries from where it
43
+ * left off on the next tick. A reorg may re-emit events and removals are never reported.
44
+ *
45
+ * @returns A function that stops the watcher.
46
+ */
47
+ export function watchContractEvent<
48
+ TAbi extends Abi,
49
+ TEventName extends ContractEventName<TAbi>,
50
+ TStrict extends boolean | undefined = undefined,
51
+ >(
52
+ client: ViemClient,
53
+ logger: Logger,
54
+ params: WatchContractEventParameters<TAbi, TEventName, TStrict>,
55
+ options: WatchContractEventOptions = {},
56
+ ): () => void {
57
+ const { address, abi, eventName, strict, onLog } = params;
58
+ const pollingIntervalMs = options.pollingIntervalMs ?? client.pollingInterval;
59
+ const maxBlockRange = BigInt(options.maxBlockRange ?? DEFAULT_MAX_BLOCK_RANGE);
60
+
61
+ const logHandlerError = (err: unknown) => logger.error(`Error handling ${eventName} L1 event`, err);
62
+ const deliver = (log: GetContractEventsReturnType<TAbi, TEventName, TStrict>[number]) => {
63
+ try {
64
+ const result = onLog(log);
65
+ if (result instanceof Promise) {
66
+ result.catch(logHandlerError);
67
+ }
68
+ } catch (err) {
69
+ logHandlerError(err);
70
+ }
71
+ };
72
+
73
+ let stopped = false;
74
+ let nextBlock: bigint | undefined;
75
+
76
+ const poll = async () => {
77
+ const latestBlock = await client.getBlockNumber();
78
+ if (nextBlock === undefined) {
79
+ nextBlock = latestBlock;
80
+ return;
81
+ }
82
+ let fromBlock: bigint = nextBlock;
83
+ while (fromBlock <= latestBlock && !stopped) {
84
+ const chunkEnd = fromBlock + maxBlockRange - 1n;
85
+ const toBlock = chunkEnd < latestBlock ? chunkEnd : latestBlock;
86
+ const logs = await client.getContractEvents<TAbi, TEventName, TStrict, bigint, bigint>({
87
+ address,
88
+ abi,
89
+ eventName,
90
+ strict,
91
+ fromBlock,
92
+ toBlock,
93
+ });
94
+ for (const log of logs) {
95
+ deliver(log);
96
+ }
97
+ fromBlock = toBlock + 1n;
98
+ nextBlock = fromBlock;
99
+ }
100
+ };
101
+
102
+ const handlePollError = (err: unknown) => logger.warn(`Error polling for ${eventName} L1 events`, { err });
103
+ const runner = new RunningPromise(poll, logger, pollingIntervalMs, handlePollError).start();
104
+
105
+ return () => {
106
+ stopped = true;
107
+ void runner.stop();
108
+ };
109
+ }